]>
Commit | Line | Data |
---|---|---|
28f4ec01 | 1 | /* Expand builtin functions. |
818ab71a | 2 | Copyright (C) 1988-2016 Free Software Foundation, Inc. |
28f4ec01 | 3 | |
1322177d | 4 | This file is part of GCC. |
28f4ec01 | 5 | |
1322177d LB |
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 | |
9dcd6f09 | 8 | Software Foundation; either version 3, or (at your option) any later |
1322177d | 9 | version. |
28f4ec01 | 10 | |
1322177d LB |
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. | |
28f4ec01 BS |
15 | |
16 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
28f4ec01 | 19 | |
25ab3b0a RB |
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 | ||
28f4ec01 BS |
24 | #include "config.h" |
25 | #include "system.h" | |
4977bab6 | 26 | #include "coretypes.h" |
c7131fb2 | 27 | #include "backend.h" |
957060b5 AM |
28 | #include "target.h" |
29 | #include "rtl.h" | |
c7131fb2 AM |
30 | #include "tree.h" |
31 | #include "gimple.h" | |
957060b5 AM |
32 | #include "predict.h" |
33 | #include "tm_p.h" | |
34 | #include "stringpool.h" | |
f90aa46c | 35 | #include "tree-vrp.h" |
957060b5 AM |
36 | #include "tree-ssanames.h" |
37 | #include "expmed.h" | |
38 | #include "optabs.h" | |
957060b5 AM |
39 | #include "emit-rtl.h" |
40 | #include "recog.h" | |
957060b5 | 41 | #include "diagnostic-core.h" |
40e23961 | 42 | #include "alias.h" |
40e23961 | 43 | #include "fold-const.h" |
5c1a2e63 | 44 | #include "fold-const-call.h" |
d8a2d370 DN |
45 | #include "stor-layout.h" |
46 | #include "calls.h" | |
47 | #include "varasm.h" | |
48 | #include "tree-object-size.h" | |
d49b6e1e | 49 | #include "realmpfr.h" |
60393bbc | 50 | #include "cfgrtl.h" |
28f4ec01 | 51 | #include "except.h" |
36566b39 PK |
52 | #include "dojump.h" |
53 | #include "explow.h" | |
36566b39 | 54 | #include "stmt.h" |
28f4ec01 | 55 | #include "expr.h" |
e78d8e51 | 56 | #include "libfuncs.h" |
28f4ec01 BS |
57 | #include "output.h" |
58 | #include "typeclass.h" | |
ab393bf1 | 59 | #include "langhooks.h" |
079a182e | 60 | #include "value-prof.h" |
fa19795e | 61 | #include "builtins.h" |
bdea98ca | 62 | #include "asan.h" |
939b37da | 63 | #include "cilk.h" |
d5e254e1 IE |
64 | #include "tree-chkp.h" |
65 | #include "rtl-chkp.h" | |
686ee971 | 66 | #include "internal-fn.h" |
b03ff92e | 67 | #include "case-cfn-macros.h" |
44a845ca | 68 | #include "gimple-fold.h" |
28f4ec01 | 69 | |
81f5094d | 70 | |
fa19795e RS |
71 | struct target_builtins default_target_builtins; |
72 | #if SWITCHABLE_TARGET | |
73 | struct target_builtins *this_target_builtins = &default_target_builtins; | |
74 | #endif | |
75 | ||
9df2c88c | 76 | /* Define the names of the builtin function types and codes. */ |
5e351e96 | 77 | const char *const built_in_class_names[BUILT_IN_LAST] |
9df2c88c RK |
78 | = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"}; |
79 | ||
c6a912da | 80 | #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X, |
7e7e470f | 81 | const char * built_in_names[(int) END_BUILTINS] = |
cb1072f4 KG |
82 | { |
83 | #include "builtins.def" | |
84 | }; | |
9df2c88c | 85 | |
cbf5d0e7 | 86 | /* Setup an array of builtin_info_type, make sure each element decl is |
3ff5f682 | 87 | initialized to NULL_TREE. */ |
cbf5d0e7 | 88 | builtin_info_type builtin_info[(int)END_BUILTINS]; |
3ff5f682 | 89 | |
4e7d7b3d JJ |
90 | /* Non-zero if __builtin_constant_p should be folded right away. */ |
91 | bool force_folding_builtin_constant_p; | |
92 | ||
ef4bddc2 | 93 | static rtx c_readstr (const char *, machine_mode); |
4682ae04 | 94 | static int target_char_cast (tree, char *); |
435bb2a1 | 95 | static rtx get_memory_rtx (tree, tree); |
4682ae04 AJ |
96 | static int apply_args_size (void); |
97 | static int apply_result_size (void); | |
4682ae04 | 98 | static rtx result_vector (int, rtx); |
4682ae04 AJ |
99 | static void expand_builtin_prefetch (tree); |
100 | static rtx expand_builtin_apply_args (void); | |
101 | static rtx expand_builtin_apply_args_1 (void); | |
102 | static rtx expand_builtin_apply (rtx, rtx, rtx); | |
103 | static void expand_builtin_return (rtx); | |
104 | static enum type_class type_to_class (tree); | |
105 | static rtx expand_builtin_classify_type (tree); | |
6c7cf1f0 | 106 | static rtx expand_builtin_mathfn_3 (tree, rtx, rtx); |
1b1562a5 | 107 | static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx); |
4359dc2a | 108 | static rtx expand_builtin_interclass_mathfn (tree, rtx); |
403e54f0 | 109 | static rtx expand_builtin_sincos (tree); |
4359dc2a | 110 | static rtx expand_builtin_cexpi (tree, rtx); |
1856c8dc JH |
111 | static rtx expand_builtin_int_roundingfn (tree, rtx); |
112 | static rtx expand_builtin_int_roundingfn_2 (tree, rtx); | |
8870e212 | 113 | static rtx expand_builtin_next_arg (void); |
4682ae04 AJ |
114 | static rtx expand_builtin_va_start (tree); |
115 | static rtx expand_builtin_va_end (tree); | |
116 | static rtx expand_builtin_va_copy (tree); | |
44e10129 | 117 | static rtx expand_builtin_strcmp (tree, rtx); |
ef4bddc2 RS |
118 | static rtx expand_builtin_strncmp (tree, rtx, machine_mode); |
119 | static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, machine_mode); | |
44e10129 | 120 | static rtx expand_builtin_memcpy (tree, rtx); |
edcf72f3 IE |
121 | static rtx expand_builtin_memcpy_with_bounds (tree, rtx); |
122 | static rtx expand_builtin_memcpy_args (tree, tree, tree, rtx, tree); | |
ef4bddc2 | 123 | static rtx expand_builtin_mempcpy (tree, rtx, machine_mode); |
edcf72f3 | 124 | static rtx expand_builtin_mempcpy_with_bounds (tree, rtx, machine_mode); |
b8698a0f | 125 | static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx, |
edcf72f3 | 126 | machine_mode, int, tree); |
44e10129 MM |
127 | static rtx expand_builtin_strcpy (tree, rtx); |
128 | static rtx expand_builtin_strcpy_args (tree, tree, rtx); | |
ef4bddc2 | 129 | static rtx expand_builtin_stpcpy (tree, rtx, machine_mode); |
44e10129 | 130 | static rtx expand_builtin_strncpy (tree, rtx); |
ef4bddc2 RS |
131 | static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, machine_mode); |
132 | static rtx expand_builtin_memset (tree, rtx, machine_mode); | |
edcf72f3 | 133 | static rtx expand_builtin_memset_with_bounds (tree, rtx, machine_mode); |
ef4bddc2 | 134 | static rtx expand_builtin_memset_args (tree, tree, tree, rtx, machine_mode, tree); |
4682ae04 | 135 | static rtx expand_builtin_bzero (tree); |
ef4bddc2 | 136 | static rtx expand_builtin_strlen (tree, rtx, machine_mode); |
3a42502d | 137 | static rtx expand_builtin_alloca (tree, bool); |
ef4bddc2 | 138 | static rtx expand_builtin_unop (machine_mode, tree, rtx, rtx, optab); |
4682ae04 | 139 | static rtx expand_builtin_frame_address (tree, tree); |
db3927fb | 140 | static tree stabilize_va_list_loc (location_t, tree, int); |
4682ae04 AJ |
141 | static rtx expand_builtin_expect (tree, rtx); |
142 | static tree fold_builtin_constant_p (tree); | |
143 | static tree fold_builtin_classify_type (tree); | |
ab996409 | 144 | static tree fold_builtin_strlen (location_t, tree, tree); |
db3927fb | 145 | static tree fold_builtin_inf (location_t, tree, int); |
db3927fb | 146 | static tree rewrite_call_expr (location_t, tree, int, tree, int, ...); |
fa233e34 | 147 | static bool validate_arg (const_tree, enum tree_code code); |
4682ae04 | 148 | static rtx expand_builtin_fabs (tree, rtx, rtx); |
ef79730c | 149 | static rtx expand_builtin_signbit (tree, rtx); |
db3927fb AH |
150 | static tree fold_builtin_strchr (location_t, tree, tree, tree); |
151 | static tree fold_builtin_memchr (location_t, tree, tree, tree, tree); | |
152 | static tree fold_builtin_memcmp (location_t, tree, tree, tree); | |
153 | static tree fold_builtin_strcmp (location_t, tree, tree); | |
154 | static tree fold_builtin_strncmp (location_t, tree, tree, tree); | |
db3927fb AH |
155 | static tree fold_builtin_isascii (location_t, tree); |
156 | static tree fold_builtin_toascii (location_t, tree); | |
157 | static tree fold_builtin_isdigit (location_t, tree); | |
158 | static tree fold_builtin_fabs (location_t, tree, tree); | |
159 | static tree fold_builtin_abs (location_t, tree, tree); | |
160 | static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code, | |
a35da91f | 161 | enum tree_code); |
2625bb5d RB |
162 | static tree fold_builtin_0 (location_t, tree); |
163 | static tree fold_builtin_1 (location_t, tree, tree); | |
164 | static tree fold_builtin_2 (location_t, tree, tree, tree); | |
165 | static tree fold_builtin_3 (location_t, tree, tree, tree, tree); | |
166 | static tree fold_builtin_varargs (location_t, tree, tree*, int); | |
db3927fb AH |
167 | |
168 | static tree fold_builtin_strpbrk (location_t, tree, tree, tree); | |
169 | static tree fold_builtin_strstr (location_t, tree, tree, tree); | |
170 | static tree fold_builtin_strrchr (location_t, tree, tree, tree); | |
db3927fb AH |
171 | static tree fold_builtin_strspn (location_t, tree, tree); |
172 | static tree fold_builtin_strcspn (location_t, tree, tree); | |
6de9cd9a | 173 | |
10a0d495 | 174 | static rtx expand_builtin_object_size (tree); |
ef4bddc2 | 175 | static rtx expand_builtin_memory_chk (tree, rtx, machine_mode, |
10a0d495 JJ |
176 | enum built_in_function); |
177 | static void maybe_emit_chk_warning (tree, enum built_in_function); | |
178 | static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function); | |
f9555f40 | 179 | static void maybe_emit_free_warning (tree); |
5039610b | 180 | static tree fold_builtin_object_size (tree, tree); |
000ba23d | 181 | |
ad03a744 | 182 | unsigned HOST_WIDE_INT target_newline; |
fef5a0d9 | 183 | unsigned HOST_WIDE_INT target_percent; |
000ba23d KG |
184 | static unsigned HOST_WIDE_INT target_c; |
185 | static unsigned HOST_WIDE_INT target_s; | |
edd7ae68 | 186 | char target_percent_c[3]; |
fef5a0d9 | 187 | char target_percent_s[3]; |
ad03a744 | 188 | char target_percent_s_newline[4]; |
ea91f957 | 189 | static tree do_mpfr_remquo (tree, tree, tree); |
752b7d38 | 190 | static tree do_mpfr_lgamma_r (tree, tree, tree); |
86951993 | 191 | static void expand_builtin_sync_synchronize (void); |
10a0d495 | 192 | |
d7f09764 DN |
193 | /* Return true if NAME starts with __builtin_ or __sync_. */ |
194 | ||
0c1e7e42 | 195 | static bool |
bbf7ce11 | 196 | is_builtin_name (const char *name) |
48ae6c13 | 197 | { |
48ae6c13 RH |
198 | if (strncmp (name, "__builtin_", 10) == 0) |
199 | return true; | |
200 | if (strncmp (name, "__sync_", 7) == 0) | |
201 | return true; | |
86951993 AM |
202 | if (strncmp (name, "__atomic_", 9) == 0) |
203 | return true; | |
b72271b9 | 204 | if (flag_cilkplus |
939b37da BI |
205 | && (!strcmp (name, "__cilkrts_detach") |
206 | || !strcmp (name, "__cilkrts_pop_frame"))) | |
207 | return true; | |
48ae6c13 RH |
208 | return false; |
209 | } | |
6de9cd9a | 210 | |
d7f09764 DN |
211 | |
212 | /* Return true if DECL is a function symbol representing a built-in. */ | |
213 | ||
214 | bool | |
215 | is_builtin_fn (tree decl) | |
216 | { | |
217 | return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl); | |
218 | } | |
219 | ||
bbf7ce11 RAE |
220 | /* Return true if NODE should be considered for inline expansion regardless |
221 | of the optimization level. This means whenever a function is invoked with | |
222 | its "internal" name, which normally contains the prefix "__builtin". */ | |
223 | ||
4cfe7a6c | 224 | bool |
bbf7ce11 RAE |
225 | called_as_built_in (tree node) |
226 | { | |
227 | /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since | |
228 | we want the name used to call the function, not the name it | |
229 | will have. */ | |
230 | const char *name = IDENTIFIER_POINTER (DECL_NAME (node)); | |
231 | return is_builtin_name (name); | |
232 | } | |
233 | ||
644ffefd MJ |
234 | /* Compute values M and N such that M divides (address of EXP - N) and such |
235 | that N < M. If these numbers can be determined, store M in alignp and N in | |
236 | *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to | |
237 | *alignp and any bit-offset to *bitposp. | |
73f6eabc RS |
238 | |
239 | Note that the address (and thus the alignment) computed here is based | |
240 | on the address to which a symbol resolves, whereas DECL_ALIGN is based | |
241 | on the address at which an object is actually located. These two | |
242 | addresses are not always the same. For example, on ARM targets, | |
243 | the address &foo of a Thumb function foo() has the lowest bit set, | |
b0f4a35f | 244 | whereas foo() itself starts on an even address. |
df96b059 | 245 | |
b0f4a35f RG |
246 | If ADDR_P is true we are taking the address of the memory reference EXP |
247 | and thus cannot rely on the access taking place. */ | |
248 | ||
249 | static bool | |
250 | get_object_alignment_2 (tree exp, unsigned int *alignp, | |
251 | unsigned HOST_WIDE_INT *bitposp, bool addr_p) | |
df96b059 | 252 | { |
e80c2726 RG |
253 | HOST_WIDE_INT bitsize, bitpos; |
254 | tree offset; | |
ef4bddc2 | 255 | machine_mode mode; |
ee45a32d | 256 | int unsignedp, reversep, volatilep; |
eae76e53 | 257 | unsigned int align = BITS_PER_UNIT; |
644ffefd | 258 | bool known_alignment = false; |
df96b059 | 259 | |
e80c2726 RG |
260 | /* Get the innermost object and the constant (bitpos) and possibly |
261 | variable (offset) offset of the access. */ | |
ee45a32d | 262 | exp = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode, |
25b75a48 | 263 | &unsignedp, &reversep, &volatilep); |
e80c2726 RG |
264 | |
265 | /* Extract alignment information from the innermost object and | |
266 | possibly adjust bitpos and offset. */ | |
b0f4a35f | 267 | if (TREE_CODE (exp) == FUNCTION_DECL) |
73f6eabc | 268 | { |
b0f4a35f RG |
269 | /* Function addresses can encode extra information besides their |
270 | alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION | |
271 | allows the low bit to be used as a virtual bit, we know | |
272 | that the address itself must be at least 2-byte aligned. */ | |
273 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn) | |
274 | align = 2 * BITS_PER_UNIT; | |
73f6eabc | 275 | } |
b0f4a35f RG |
276 | else if (TREE_CODE (exp) == LABEL_DECL) |
277 | ; | |
278 | else if (TREE_CODE (exp) == CONST_DECL) | |
e80c2726 | 279 | { |
b0f4a35f RG |
280 | /* The alignment of a CONST_DECL is determined by its initializer. */ |
281 | exp = DECL_INITIAL (exp); | |
e80c2726 | 282 | align = TYPE_ALIGN (TREE_TYPE (exp)); |
b0f4a35f RG |
283 | if (CONSTANT_CLASS_P (exp)) |
284 | align = (unsigned) CONSTANT_ALIGNMENT (exp, align); | |
6b00e42d | 285 | |
b0f4a35f | 286 | known_alignment = true; |
e80c2726 | 287 | } |
b0f4a35f | 288 | else if (DECL_P (exp)) |
644ffefd | 289 | { |
b0f4a35f | 290 | align = DECL_ALIGN (exp); |
644ffefd | 291 | known_alignment = true; |
644ffefd | 292 | } |
b0f4a35f RG |
293 | else if (TREE_CODE (exp) == INDIRECT_REF |
294 | || TREE_CODE (exp) == MEM_REF | |
295 | || TREE_CODE (exp) == TARGET_MEM_REF) | |
e80c2726 RG |
296 | { |
297 | tree addr = TREE_OPERAND (exp, 0); | |
644ffefd MJ |
298 | unsigned ptr_align; |
299 | unsigned HOST_WIDE_INT ptr_bitpos; | |
4ceae7e9 | 300 | unsigned HOST_WIDE_INT ptr_bitmask = ~0; |
644ffefd | 301 | |
4ceae7e9 | 302 | /* If the address is explicitely aligned, handle that. */ |
e80c2726 RG |
303 | if (TREE_CODE (addr) == BIT_AND_EXPR |
304 | && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST) | |
305 | { | |
4ceae7e9 RB |
306 | ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)); |
307 | ptr_bitmask *= BITS_PER_UNIT; | |
146ec50f | 308 | align = least_bit_hwi (ptr_bitmask); |
e80c2726 RG |
309 | addr = TREE_OPERAND (addr, 0); |
310 | } | |
644ffefd | 311 | |
b0f4a35f RG |
312 | known_alignment |
313 | = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos); | |
b0f4a35f RG |
314 | align = MAX (ptr_align, align); |
315 | ||
4ceae7e9 RB |
316 | /* Re-apply explicit alignment to the bitpos. */ |
317 | ptr_bitpos &= ptr_bitmask; | |
318 | ||
3c82efd9 RG |
319 | /* The alignment of the pointer operand in a TARGET_MEM_REF |
320 | has to take the variable offset parts into account. */ | |
b0f4a35f | 321 | if (TREE_CODE (exp) == TARGET_MEM_REF) |
1be38ccb | 322 | { |
b0f4a35f RG |
323 | if (TMR_INDEX (exp)) |
324 | { | |
325 | unsigned HOST_WIDE_INT step = 1; | |
326 | if (TMR_STEP (exp)) | |
327 | step = TREE_INT_CST_LOW (TMR_STEP (exp)); | |
146ec50f | 328 | align = MIN (align, least_bit_hwi (step) * BITS_PER_UNIT); |
b0f4a35f RG |
329 | } |
330 | if (TMR_INDEX2 (exp)) | |
331 | align = BITS_PER_UNIT; | |
332 | known_alignment = false; | |
1be38ccb | 333 | } |
644ffefd | 334 | |
b0f4a35f RG |
335 | /* When EXP is an actual memory reference then we can use |
336 | TYPE_ALIGN of a pointer indirection to derive alignment. | |
337 | Do so only if get_pointer_alignment_1 did not reveal absolute | |
3c82efd9 RG |
338 | alignment knowledge and if using that alignment would |
339 | improve the situation. */ | |
340 | if (!addr_p && !known_alignment | |
341 | && TYPE_ALIGN (TREE_TYPE (exp)) > align) | |
342 | align = TYPE_ALIGN (TREE_TYPE (exp)); | |
343 | else | |
344 | { | |
345 | /* Else adjust bitpos accordingly. */ | |
346 | bitpos += ptr_bitpos; | |
347 | if (TREE_CODE (exp) == MEM_REF | |
348 | || TREE_CODE (exp) == TARGET_MEM_REF) | |
807e902e | 349 | bitpos += mem_ref_offset (exp).to_short_addr () * BITS_PER_UNIT; |
3c82efd9 | 350 | } |
e80c2726 | 351 | } |
b0f4a35f | 352 | else if (TREE_CODE (exp) == STRING_CST) |
1be38ccb | 353 | { |
b0f4a35f RG |
354 | /* STRING_CST are the only constant objects we allow to be not |
355 | wrapped inside a CONST_DECL. */ | |
356 | align = TYPE_ALIGN (TREE_TYPE (exp)); | |
b0f4a35f RG |
357 | if (CONSTANT_CLASS_P (exp)) |
358 | align = (unsigned) CONSTANT_ALIGNMENT (exp, align); | |
6b00e42d | 359 | |
b0f4a35f | 360 | known_alignment = true; |
e80c2726 | 361 | } |
e80c2726 RG |
362 | |
363 | /* If there is a non-constant offset part extract the maximum | |
364 | alignment that can prevail. */ | |
eae76e53 | 365 | if (offset) |
e80c2726 | 366 | { |
e75fde1a | 367 | unsigned int trailing_zeros = tree_ctz (offset); |
eae76e53 | 368 | if (trailing_zeros < HOST_BITS_PER_INT) |
e80c2726 | 369 | { |
eae76e53 JJ |
370 | unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT; |
371 | if (inner) | |
372 | align = MIN (align, inner); | |
e80c2726 | 373 | } |
e80c2726 RG |
374 | } |
375 | ||
b0f4a35f RG |
376 | *alignp = align; |
377 | *bitposp = bitpos & (*alignp - 1); | |
644ffefd | 378 | return known_alignment; |
daade206 RG |
379 | } |
380 | ||
b0f4a35f RG |
381 | /* For a memory reference expression EXP compute values M and N such that M |
382 | divides (&EXP - N) and such that N < M. If these numbers can be determined, | |
383 | store M in alignp and N in *BITPOSP and return true. Otherwise return false | |
384 | and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */ | |
385 | ||
386 | bool | |
387 | get_object_alignment_1 (tree exp, unsigned int *alignp, | |
388 | unsigned HOST_WIDE_INT *bitposp) | |
389 | { | |
390 | return get_object_alignment_2 (exp, alignp, bitposp, false); | |
391 | } | |
392 | ||
0eb77834 | 393 | /* Return the alignment in bits of EXP, an object. */ |
daade206 RG |
394 | |
395 | unsigned int | |
0eb77834 | 396 | get_object_alignment (tree exp) |
daade206 RG |
397 | { |
398 | unsigned HOST_WIDE_INT bitpos = 0; | |
399 | unsigned int align; | |
400 | ||
644ffefd | 401 | get_object_alignment_1 (exp, &align, &bitpos); |
daade206 | 402 | |
e80c2726 RG |
403 | /* align and bitpos now specify known low bits of the pointer. |
404 | ptr & (align - 1) == bitpos. */ | |
405 | ||
406 | if (bitpos != 0) | |
146ec50f | 407 | align = least_bit_hwi (bitpos); |
0eb77834 | 408 | return align; |
df96b059 JJ |
409 | } |
410 | ||
644ffefd MJ |
411 | /* For a pointer valued expression EXP compute values M and N such that M |
412 | divides (EXP - N) and such that N < M. If these numbers can be determined, | |
b0f4a35f RG |
413 | store M in alignp and N in *BITPOSP and return true. Return false if |
414 | the results are just a conservative approximation. | |
28f4ec01 | 415 | |
644ffefd | 416 | If EXP is not a pointer, false is returned too. */ |
28f4ec01 | 417 | |
644ffefd MJ |
418 | bool |
419 | get_pointer_alignment_1 (tree exp, unsigned int *alignp, | |
420 | unsigned HOST_WIDE_INT *bitposp) | |
28f4ec01 | 421 | { |
1be38ccb | 422 | STRIP_NOPS (exp); |
6026b73e | 423 | |
1be38ccb | 424 | if (TREE_CODE (exp) == ADDR_EXPR) |
b0f4a35f RG |
425 | return get_object_alignment_2 (TREE_OPERAND (exp, 0), |
426 | alignp, bitposp, true); | |
5fa79de8 RB |
427 | else if (TREE_CODE (exp) == POINTER_PLUS_EXPR) |
428 | { | |
429 | unsigned int align; | |
430 | unsigned HOST_WIDE_INT bitpos; | |
431 | bool res = get_pointer_alignment_1 (TREE_OPERAND (exp, 0), | |
432 | &align, &bitpos); | |
433 | if (TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST) | |
434 | bitpos += TREE_INT_CST_LOW (TREE_OPERAND (exp, 1)) * BITS_PER_UNIT; | |
435 | else | |
436 | { | |
437 | unsigned int trailing_zeros = tree_ctz (TREE_OPERAND (exp, 1)); | |
438 | if (trailing_zeros < HOST_BITS_PER_INT) | |
439 | { | |
440 | unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT; | |
441 | if (inner) | |
442 | align = MIN (align, inner); | |
443 | } | |
444 | } | |
445 | *alignp = align; | |
446 | *bitposp = bitpos & (align - 1); | |
447 | return res; | |
448 | } | |
1be38ccb RG |
449 | else if (TREE_CODE (exp) == SSA_NAME |
450 | && POINTER_TYPE_P (TREE_TYPE (exp))) | |
28f4ec01 | 451 | { |
644ffefd | 452 | unsigned int ptr_align, ptr_misalign; |
1be38ccb | 453 | struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp); |
644ffefd MJ |
454 | |
455 | if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign)) | |
456 | { | |
457 | *bitposp = ptr_misalign * BITS_PER_UNIT; | |
458 | *alignp = ptr_align * BITS_PER_UNIT; | |
5505978a RB |
459 | /* Make sure to return a sensible alignment when the multiplication |
460 | by BITS_PER_UNIT overflowed. */ | |
461 | if (*alignp == 0) | |
462 | *alignp = 1u << (HOST_BITS_PER_INT - 1); | |
b0f4a35f | 463 | /* We cannot really tell whether this result is an approximation. */ |
5f9a167b | 464 | return false; |
644ffefd MJ |
465 | } |
466 | else | |
87c0fb4b RG |
467 | { |
468 | *bitposp = 0; | |
644ffefd MJ |
469 | *alignp = BITS_PER_UNIT; |
470 | return false; | |
87c0fb4b | 471 | } |
28f4ec01 | 472 | } |
44fabee4 RG |
473 | else if (TREE_CODE (exp) == INTEGER_CST) |
474 | { | |
475 | *alignp = BIGGEST_ALIGNMENT; | |
476 | *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT) | |
477 | & (BIGGEST_ALIGNMENT - 1)); | |
478 | return true; | |
479 | } | |
1be38ccb | 480 | |
87c0fb4b | 481 | *bitposp = 0; |
644ffefd MJ |
482 | *alignp = BITS_PER_UNIT; |
483 | return false; | |
28f4ec01 BS |
484 | } |
485 | ||
87c0fb4b RG |
486 | /* Return the alignment in bits of EXP, a pointer valued expression. |
487 | The alignment returned is, by default, the alignment of the thing that | |
488 | EXP points to. If it is not a POINTER_TYPE, 0 is returned. | |
489 | ||
490 | Otherwise, look at the expression to see if we can do better, i.e., if the | |
491 | expression is actually pointing at an object whose alignment is tighter. */ | |
492 | ||
493 | unsigned int | |
494 | get_pointer_alignment (tree exp) | |
495 | { | |
496 | unsigned HOST_WIDE_INT bitpos = 0; | |
497 | unsigned int align; | |
644ffefd MJ |
498 | |
499 | get_pointer_alignment_1 (exp, &align, &bitpos); | |
87c0fb4b RG |
500 | |
501 | /* align and bitpos now specify known low bits of the pointer. | |
502 | ptr & (align - 1) == bitpos. */ | |
503 | ||
504 | if (bitpos != 0) | |
146ec50f | 505 | align = least_bit_hwi (bitpos); |
87c0fb4b RG |
506 | |
507 | return align; | |
508 | } | |
509 | ||
28f4ec01 BS |
510 | /* Compute the length of a C string. TREE_STRING_LENGTH is not the right |
511 | way, because it could contain a zero byte in the middle. | |
512 | TREE_STRING_LENGTH is the size of the character array, not the string. | |
513 | ||
f1ba665b | 514 | ONLY_VALUE should be nonzero if the result is not going to be emitted |
88373ed0 | 515 | into the instruction stream and zero if it is going to be expanded. |
f1ba665b | 516 | E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3 |
ae808627 JJ |
517 | is returned, otherwise NULL, since |
518 | len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not | |
519 | evaluate the side-effects. | |
520 | ||
21e8fb22 RB |
521 | If ONLY_VALUE is two then we do not emit warnings about out-of-bound |
522 | accesses. Note that this implies the result is not going to be emitted | |
523 | into the instruction stream. | |
524 | ||
fed3cef0 RK |
525 | The value returned is of type `ssizetype'. |
526 | ||
28f4ec01 BS |
527 | Unfortunately, string_constant can't access the values of const char |
528 | arrays with initializers, so neither can we do so here. */ | |
529 | ||
6de9cd9a | 530 | tree |
ae808627 | 531 | c_strlen (tree src, int only_value) |
28f4ec01 BS |
532 | { |
533 | tree offset_node; | |
5197bd50 RK |
534 | HOST_WIDE_INT offset; |
535 | int max; | |
520a57c8 | 536 | const char *ptr; |
59d49708 | 537 | location_t loc; |
28f4ec01 | 538 | |
ae808627 JJ |
539 | STRIP_NOPS (src); |
540 | if (TREE_CODE (src) == COND_EXPR | |
541 | && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0)))) | |
542 | { | |
543 | tree len1, len2; | |
544 | ||
545 | len1 = c_strlen (TREE_OPERAND (src, 1), only_value); | |
546 | len2 = c_strlen (TREE_OPERAND (src, 2), only_value); | |
33521f7d | 547 | if (tree_int_cst_equal (len1, len2)) |
ae808627 JJ |
548 | return len1; |
549 | } | |
550 | ||
551 | if (TREE_CODE (src) == COMPOUND_EXPR | |
552 | && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0)))) | |
553 | return c_strlen (TREE_OPERAND (src, 1), only_value); | |
554 | ||
8400e75e | 555 | loc = EXPR_LOC_OR_LOC (src, input_location); |
59d49708 | 556 | |
28f4ec01 BS |
557 | src = string_constant (src, &offset_node); |
558 | if (src == 0) | |
5039610b | 559 | return NULL_TREE; |
fed3cef0 | 560 | |
2dee4af1 | 561 | max = TREE_STRING_LENGTH (src) - 1; |
28f4ec01 | 562 | ptr = TREE_STRING_POINTER (src); |
fed3cef0 | 563 | |
28f4ec01 BS |
564 | if (offset_node && TREE_CODE (offset_node) != INTEGER_CST) |
565 | { | |
566 | /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't | |
567 | compute the offset to the following null if we don't know where to | |
568 | start searching for it. */ | |
569 | int i; | |
fed3cef0 | 570 | |
28f4ec01 BS |
571 | for (i = 0; i < max; i++) |
572 | if (ptr[i] == 0) | |
5039610b | 573 | return NULL_TREE; |
fed3cef0 | 574 | |
28f4ec01 BS |
575 | /* We don't know the starting offset, but we do know that the string |
576 | has no internal zero bytes. We can assume that the offset falls | |
577 | within the bounds of the string; otherwise, the programmer deserves | |
578 | what he gets. Subtract the offset from the length of the string, | |
fed3cef0 RK |
579 | and return that. This would perhaps not be valid if we were dealing |
580 | with named arrays in addition to literal string constants. */ | |
581 | ||
59d49708 | 582 | return size_diffop_loc (loc, size_int (max), offset_node); |
28f4ec01 BS |
583 | } |
584 | ||
585 | /* We have a known offset into the string. Start searching there for | |
5197bd50 | 586 | a null character if we can represent it as a single HOST_WIDE_INT. */ |
1de3d877 | 587 | if (offset_node == 0) |
28f4ec01 | 588 | offset = 0; |
9541ffee | 589 | else if (! tree_fits_shwi_p (offset_node)) |
1de3d877 | 590 | offset = -1; |
28f4ec01 | 591 | else |
9439e9a1 | 592 | offset = tree_to_shwi (offset_node); |
fed3cef0 | 593 | |
b2ed71b6 BE |
594 | /* If the offset is known to be out of bounds, warn, and call strlen at |
595 | runtime. */ | |
3b57ff81 | 596 | if (offset < 0 || offset > max) |
28f4ec01 | 597 | { |
b2ed71b6 | 598 | /* Suppress multiple warnings for propagated constant strings. */ |
3b57ff81 RB |
599 | if (only_value != 2 |
600 | && !TREE_NO_WARNING (src)) | |
b2ed71b6 | 601 | { |
59d49708 | 602 | warning_at (loc, 0, "offset outside bounds of constant string"); |
b2ed71b6 BE |
603 | TREE_NO_WARNING (src) = 1; |
604 | } | |
5039610b | 605 | return NULL_TREE; |
28f4ec01 | 606 | } |
fed3cef0 | 607 | |
28f4ec01 BS |
608 | /* Use strlen to search for the first zero byte. Since any strings |
609 | constructed with build_string will have nulls appended, we win even | |
610 | if we get handed something like (char[4])"abcd". | |
611 | ||
612 | Since OFFSET is our starting index into the string, no further | |
613 | calculation is needed. */ | |
fed3cef0 | 614 | return ssize_int (strlen (ptr + offset)); |
28f4ec01 BS |
615 | } |
616 | ||
807e902e | 617 | /* Return a constant integer corresponding to target reading |
bf06b5d8 | 618 | GET_MODE_BITSIZE (MODE) bits from string constant STR. */ |
ab937357 | 619 | |
57814e5e | 620 | static rtx |
ef4bddc2 | 621 | c_readstr (const char *str, machine_mode mode) |
57814e5e | 622 | { |
57814e5e JJ |
623 | HOST_WIDE_INT ch; |
624 | unsigned int i, j; | |
807e902e | 625 | HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT]; |
57814e5e | 626 | |
298e6adc | 627 | gcc_assert (GET_MODE_CLASS (mode) == MODE_INT); |
807e902e KZ |
628 | unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1) |
629 | / HOST_BITS_PER_WIDE_INT; | |
630 | ||
631 | gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT); | |
632 | for (i = 0; i < len; i++) | |
633 | tmp[i] = 0; | |
5906d013 | 634 | |
57814e5e JJ |
635 | ch = 1; |
636 | for (i = 0; i < GET_MODE_SIZE (mode); i++) | |
637 | { | |
638 | j = i; | |
639 | if (WORDS_BIG_ENDIAN) | |
640 | j = GET_MODE_SIZE (mode) - i - 1; | |
641 | if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN | |
e046112d | 642 | && GET_MODE_SIZE (mode) >= UNITS_PER_WORD) |
57814e5e JJ |
643 | j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1; |
644 | j *= BITS_PER_UNIT; | |
5906d013 | 645 | |
57814e5e JJ |
646 | if (ch) |
647 | ch = (unsigned char) str[i]; | |
807e902e | 648 | tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT); |
57814e5e | 649 | } |
807e902e KZ |
650 | |
651 | wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode)); | |
652 | return immed_wide_int_const (c, mode); | |
57814e5e JJ |
653 | } |
654 | ||
ab937357 | 655 | /* Cast a target constant CST to target CHAR and if that value fits into |
206048bd | 656 | host char type, return zero and put that value into variable pointed to by |
ab937357 JJ |
657 | P. */ |
658 | ||
659 | static int | |
4682ae04 | 660 | target_char_cast (tree cst, char *p) |
ab937357 JJ |
661 | { |
662 | unsigned HOST_WIDE_INT val, hostval; | |
663 | ||
de77ab75 | 664 | if (TREE_CODE (cst) != INTEGER_CST |
ab937357 JJ |
665 | || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT) |
666 | return 1; | |
667 | ||
807e902e | 668 | /* Do not care if it fits or not right here. */ |
de77ab75 | 669 | val = TREE_INT_CST_LOW (cst); |
807e902e | 670 | |
ab937357 | 671 | if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT) |
fecfbfa4 | 672 | val &= (HOST_WIDE_INT_1U << CHAR_TYPE_SIZE) - 1; |
ab937357 JJ |
673 | |
674 | hostval = val; | |
675 | if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT) | |
fecfbfa4 | 676 | hostval &= (HOST_WIDE_INT_1U << HOST_BITS_PER_CHAR) - 1; |
ab937357 JJ |
677 | |
678 | if (val != hostval) | |
679 | return 1; | |
680 | ||
681 | *p = hostval; | |
682 | return 0; | |
683 | } | |
684 | ||
6de9cd9a DN |
685 | /* Similar to save_expr, but assumes that arbitrary code is not executed |
686 | in between the multiple evaluations. In particular, we assume that a | |
687 | non-addressable local variable will not be modified. */ | |
688 | ||
689 | static tree | |
690 | builtin_save_expr (tree exp) | |
691 | { | |
5cbf5c20 RG |
692 | if (TREE_CODE (exp) == SSA_NAME |
693 | || (TREE_ADDRESSABLE (exp) == 0 | |
694 | && (TREE_CODE (exp) == PARM_DECL | |
695 | || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp))))) | |
6de9cd9a DN |
696 | return exp; |
697 | ||
698 | return save_expr (exp); | |
699 | } | |
700 | ||
28f4ec01 BS |
701 | /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT |
702 | times to get the address of either a higher stack frame, or a return | |
703 | address located within it (depending on FNDECL_CODE). */ | |
fed3cef0 | 704 | |
54e62799 | 705 | static rtx |
c6d01079 | 706 | expand_builtin_return_addr (enum built_in_function fndecl_code, int count) |
28f4ec01 BS |
707 | { |
708 | int i; | |
c6d01079 | 709 | rtx tem = INITIAL_FRAME_ADDRESS_RTX; |
579f2946 | 710 | if (tem == NULL_RTX) |
c8f27794 | 711 | { |
579f2946 TS |
712 | /* For a zero count with __builtin_return_address, we don't care what |
713 | frame address we return, because target-specific definitions will | |
714 | override us. Therefore frame pointer elimination is OK, and using | |
715 | the soft frame pointer is OK. | |
716 | ||
717 | For a nonzero count, or a zero count with __builtin_frame_address, | |
718 | we require a stable offset from the current frame pointer to the | |
719 | previous one, so we must use the hard frame pointer, and | |
720 | we must disable frame pointer elimination. */ | |
721 | if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS) | |
722 | tem = frame_pointer_rtx; | |
723 | else | |
724 | { | |
725 | tem = hard_frame_pointer_rtx; | |
c8f27794 | 726 | |
579f2946 TS |
727 | /* Tell reload not to eliminate the frame pointer. */ |
728 | crtl->accesses_prior_frames = 1; | |
729 | } | |
c8f27794 | 730 | } |
c6d01079 | 731 | |
28f4ec01 BS |
732 | if (count > 0) |
733 | SETUP_FRAME_ADDRESSES (); | |
28f4ec01 | 734 | |
224869d9 | 735 | /* On the SPARC, the return address is not in the frame, it is in a |
28f4ec01 BS |
736 | register. There is no way to access it off of the current frame |
737 | pointer, but it can be accessed off the previous frame pointer by | |
738 | reading the value from the register window save area. */ | |
2e612c47 | 739 | if (RETURN_ADDR_IN_PREVIOUS_FRAME && fndecl_code == BUILT_IN_RETURN_ADDRESS) |
28f4ec01 | 740 | count--; |
28f4ec01 BS |
741 | |
742 | /* Scan back COUNT frames to the specified frame. */ | |
743 | for (i = 0; i < count; i++) | |
744 | { | |
745 | /* Assume the dynamic chain pointer is in the word that the | |
746 | frame address points to, unless otherwise specified. */ | |
28f4ec01 | 747 | tem = DYNAMIC_CHAIN_ADDRESS (tem); |
28f4ec01 | 748 | tem = memory_address (Pmode, tem); |
bf877a76 | 749 | tem = gen_frame_mem (Pmode, tem); |
432fd734 | 750 | tem = copy_to_reg (tem); |
28f4ec01 BS |
751 | } |
752 | ||
224869d9 EB |
753 | /* For __builtin_frame_address, return what we've got. But, on |
754 | the SPARC for example, we may have to add a bias. */ | |
28f4ec01 | 755 | if (fndecl_code == BUILT_IN_FRAME_ADDRESS) |
224869d9 | 756 | return FRAME_ADDR_RTX (tem); |
28f4ec01 | 757 | |
224869d9 | 758 | /* For __builtin_return_address, get the return address from that frame. */ |
28f4ec01 BS |
759 | #ifdef RETURN_ADDR_RTX |
760 | tem = RETURN_ADDR_RTX (count, tem); | |
761 | #else | |
762 | tem = memory_address (Pmode, | |
0a81f074 | 763 | plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode))); |
bf877a76 | 764 | tem = gen_frame_mem (Pmode, tem); |
28f4ec01 BS |
765 | #endif |
766 | return tem; | |
767 | } | |
768 | ||
3bdf5ad1 | 769 | /* Alias set used for setjmp buffer. */ |
4862826d | 770 | static alias_set_type setjmp_alias_set = -1; |
3bdf5ad1 | 771 | |
250d07b6 | 772 | /* Construct the leading half of a __builtin_setjmp call. Control will |
4f6c2131 EB |
773 | return to RECEIVER_LABEL. This is also called directly by the SJLJ |
774 | exception handling code. */ | |
28f4ec01 | 775 | |
250d07b6 | 776 | void |
4682ae04 | 777 | expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label) |
28f4ec01 | 778 | { |
ef4bddc2 | 779 | machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL); |
28f4ec01 | 780 | rtx stack_save; |
3bdf5ad1 | 781 | rtx mem; |
28f4ec01 | 782 | |
3bdf5ad1 RK |
783 | if (setjmp_alias_set == -1) |
784 | setjmp_alias_set = new_alias_set (); | |
785 | ||
5ae6cd0d | 786 | buf_addr = convert_memory_address (Pmode, buf_addr); |
28f4ec01 | 787 | |
7d505b82 | 788 | buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX)); |
28f4ec01 | 789 | |
250d07b6 RH |
790 | /* We store the frame pointer and the address of receiver_label in |
791 | the buffer and use the rest of it for the stack save area, which | |
792 | is machine-dependent. */ | |
28f4ec01 | 793 | |
3bdf5ad1 | 794 | mem = gen_rtx_MEM (Pmode, buf_addr); |
ba4828e0 | 795 | set_mem_alias_set (mem, setjmp_alias_set); |
d6da68b9 | 796 | emit_move_insn (mem, targetm.builtin_setjmp_frame_value ()); |
3bdf5ad1 | 797 | |
0a81f074 RS |
798 | mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr, |
799 | GET_MODE_SIZE (Pmode))), | |
ba4828e0 | 800 | set_mem_alias_set (mem, setjmp_alias_set); |
3bdf5ad1 RK |
801 | |
802 | emit_move_insn (validize_mem (mem), | |
250d07b6 | 803 | force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label))); |
28f4ec01 BS |
804 | |
805 | stack_save = gen_rtx_MEM (sa_mode, | |
0a81f074 | 806 | plus_constant (Pmode, buf_addr, |
28f4ec01 | 807 | 2 * GET_MODE_SIZE (Pmode))); |
ba4828e0 | 808 | set_mem_alias_set (stack_save, setjmp_alias_set); |
9eac0f2a | 809 | emit_stack_save (SAVE_NONLOCAL, &stack_save); |
28f4ec01 BS |
810 | |
811 | /* If there is further processing to do, do it. */ | |
95a3fb9d RS |
812 | if (targetm.have_builtin_setjmp_setup ()) |
813 | emit_insn (targetm.gen_builtin_setjmp_setup (buf_addr)); | |
28f4ec01 | 814 | |
ecaebb9e | 815 | /* We have a nonlocal label. */ |
e3b5732b | 816 | cfun->has_nonlocal_label = 1; |
250d07b6 | 817 | } |
28f4ec01 | 818 | |
4f6c2131 | 819 | /* Construct the trailing part of a __builtin_setjmp call. This is |
e90d1568 HPN |
820 | also called directly by the SJLJ exception handling code. |
821 | If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */ | |
250d07b6 RH |
822 | |
823 | void | |
95a3fb9d | 824 | expand_builtin_setjmp_receiver (rtx receiver_label) |
250d07b6 | 825 | { |
531ca746 RH |
826 | rtx chain; |
827 | ||
e90d1568 | 828 | /* Mark the FP as used when we get here, so we have to make sure it's |
28f4ec01 | 829 | marked as used by this function. */ |
c41c1387 | 830 | emit_use (hard_frame_pointer_rtx); |
28f4ec01 BS |
831 | |
832 | /* Mark the static chain as clobbered here so life information | |
833 | doesn't get messed up for it. */ | |
531ca746 RH |
834 | chain = targetm.calls.static_chain (current_function_decl, true); |
835 | if (chain && REG_P (chain)) | |
836 | emit_clobber (chain); | |
28f4ec01 BS |
837 | |
838 | /* Now put in the code to restore the frame pointer, and argument | |
caf93cb0 | 839 | pointer, if needed. */ |
95a3fb9d | 840 | if (! targetm.have_nonlocal_goto ()) |
f1257268 RS |
841 | { |
842 | /* First adjust our frame pointer to its actual value. It was | |
843 | previously set to the start of the virtual area corresponding to | |
844 | the stacked variables when we branched here and now needs to be | |
845 | adjusted to the actual hardware fp value. | |
846 | ||
847 | Assignments to virtual registers are converted by | |
848 | instantiate_virtual_regs into the corresponding assignment | |
849 | to the underlying register (fp in this case) that makes | |
850 | the original assignment true. | |
851 | So the following insn will actually be decrementing fp by | |
852 | STARTING_FRAME_OFFSET. */ | |
853 | emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx); | |
854 | ||
855 | /* Restoring the frame pointer also modifies the hard frame pointer. | |
856 | Mark it used (so that the previous assignment remains live once | |
857 | the frame pointer is eliminated) and clobbered (to represent the | |
858 | implicit update from the assignment). */ | |
859 | emit_use (hard_frame_pointer_rtx); | |
860 | emit_clobber (hard_frame_pointer_rtx); | |
861 | } | |
28f4ec01 | 862 | |
38b0b093 | 863 | if (!HARD_FRAME_POINTER_IS_ARG_POINTER && fixed_regs[ARG_POINTER_REGNUM]) |
28f4ec01 | 864 | { |
e90d1568 HPN |
865 | /* If the argument pointer can be eliminated in favor of the |
866 | frame pointer, we don't need to restore it. We assume here | |
867 | that if such an elimination is present, it can always be used. | |
868 | This is the case on all known machines; if we don't make this | |
869 | assumption, we do unnecessary saving on many machines. */ | |
28f4ec01 | 870 | size_t i; |
8b60264b | 871 | static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS; |
28f4ec01 | 872 | |
b6a1cbae | 873 | for (i = 0; i < ARRAY_SIZE (elim_regs); i++) |
28f4ec01 BS |
874 | if (elim_regs[i].from == ARG_POINTER_REGNUM |
875 | && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM) | |
876 | break; | |
877 | ||
b6a1cbae | 878 | if (i == ARRAY_SIZE (elim_regs)) |
28f4ec01 BS |
879 | { |
880 | /* Now restore our arg pointer from the address at which it | |
278ed218 | 881 | was saved in our stack frame. */ |
2e3f842f | 882 | emit_move_insn (crtl->args.internal_arg_pointer, |
bd60bab2 | 883 | copy_to_reg (get_arg_pointer_save_area ())); |
28f4ec01 BS |
884 | } |
885 | } | |
28f4ec01 | 886 | |
95a3fb9d RS |
887 | if (receiver_label != NULL && targetm.have_builtin_setjmp_receiver ()) |
888 | emit_insn (targetm.gen_builtin_setjmp_receiver (receiver_label)); | |
889 | else if (targetm.have_nonlocal_goto_receiver ()) | |
890 | emit_insn (targetm.gen_nonlocal_goto_receiver ()); | |
28f4ec01 | 891 | else |
95a3fb9d | 892 | { /* Nothing */ } |
bcd7edfe | 893 | |
6fb5fa3c DB |
894 | /* We must not allow the code we just generated to be reordered by |
895 | scheduling. Specifically, the update of the frame pointer must | |
f1257268 | 896 | happen immediately, not later. */ |
6fb5fa3c | 897 | emit_insn (gen_blockage ()); |
250d07b6 | 898 | } |
28f4ec01 | 899 | |
28f4ec01 BS |
900 | /* __builtin_longjmp is passed a pointer to an array of five words (not |
901 | all will be used on all machines). It operates similarly to the C | |
902 | library function of the same name, but is more efficient. Much of | |
4f6c2131 | 903 | the code below is copied from the handling of non-local gotos. */ |
28f4ec01 | 904 | |
54e62799 | 905 | static void |
4682ae04 | 906 | expand_builtin_longjmp (rtx buf_addr, rtx value) |
28f4ec01 | 907 | { |
58f4cf2a DM |
908 | rtx fp, lab, stack; |
909 | rtx_insn *insn, *last; | |
ef4bddc2 | 910 | machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL); |
28f4ec01 | 911 | |
b8698a0f | 912 | /* DRAP is needed for stack realign if longjmp is expanded to current |
2e3f842f L |
913 | function */ |
914 | if (SUPPORTS_STACK_ALIGNMENT) | |
915 | crtl->need_drap = true; | |
916 | ||
3bdf5ad1 RK |
917 | if (setjmp_alias_set == -1) |
918 | setjmp_alias_set = new_alias_set (); | |
919 | ||
5ae6cd0d | 920 | buf_addr = convert_memory_address (Pmode, buf_addr); |
4b6c1672 | 921 | |
28f4ec01 BS |
922 | buf_addr = force_reg (Pmode, buf_addr); |
923 | ||
531ca746 RH |
924 | /* We require that the user must pass a second argument of 1, because |
925 | that is what builtin_setjmp will return. */ | |
298e6adc | 926 | gcc_assert (value == const1_rtx); |
28f4ec01 | 927 | |
d337d653 | 928 | last = get_last_insn (); |
95a3fb9d RS |
929 | if (targetm.have_builtin_longjmp ()) |
930 | emit_insn (targetm.gen_builtin_longjmp (buf_addr)); | |
28f4ec01 | 931 | else |
28f4ec01 BS |
932 | { |
933 | fp = gen_rtx_MEM (Pmode, buf_addr); | |
0a81f074 | 934 | lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr, |
28f4ec01 BS |
935 | GET_MODE_SIZE (Pmode))); |
936 | ||
0a81f074 | 937 | stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr, |
28f4ec01 | 938 | 2 * GET_MODE_SIZE (Pmode))); |
ba4828e0 RK |
939 | set_mem_alias_set (fp, setjmp_alias_set); |
940 | set_mem_alias_set (lab, setjmp_alias_set); | |
941 | set_mem_alias_set (stack, setjmp_alias_set); | |
28f4ec01 BS |
942 | |
943 | /* Pick up FP, label, and SP from the block and jump. This code is | |
944 | from expand_goto in stmt.c; see there for detailed comments. */ | |
95a3fb9d | 945 | if (targetm.have_nonlocal_goto ()) |
28f4ec01 BS |
946 | /* We have to pass a value to the nonlocal_goto pattern that will |
947 | get copied into the static_chain pointer, but it does not matter | |
948 | what that value is, because builtin_setjmp does not use it. */ | |
95a3fb9d | 949 | emit_insn (targetm.gen_nonlocal_goto (value, lab, stack, fp)); |
28f4ec01 | 950 | else |
28f4ec01 BS |
951 | { |
952 | lab = copy_to_reg (lab); | |
953 | ||
c41c1387 RS |
954 | emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode))); |
955 | emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx)); | |
41439bf6 | 956 | |
28f4ec01 | 957 | emit_move_insn (hard_frame_pointer_rtx, fp); |
9eac0f2a | 958 | emit_stack_restore (SAVE_NONLOCAL, stack); |
28f4ec01 | 959 | |
c41c1387 RS |
960 | emit_use (hard_frame_pointer_rtx); |
961 | emit_use (stack_pointer_rtx); | |
28f4ec01 BS |
962 | emit_indirect_jump (lab); |
963 | } | |
964 | } | |
4b01bd16 RH |
965 | |
966 | /* Search backwards and mark the jump insn as a non-local goto. | |
967 | Note that this precludes the use of __builtin_longjmp to a | |
968 | __builtin_setjmp target in the same function. However, we've | |
969 | already cautioned the user that these functions are for | |
970 | internal exception handling use only. */ | |
8206fc89 AM |
971 | for (insn = get_last_insn (); insn; insn = PREV_INSN (insn)) |
972 | { | |
298e6adc | 973 | gcc_assert (insn != last); |
5906d013 | 974 | |
4b4bf941 | 975 | if (JUMP_P (insn)) |
8206fc89 | 976 | { |
65c5f2a6 | 977 | add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx); |
8206fc89 AM |
978 | break; |
979 | } | |
4b4bf941 | 980 | else if (CALL_P (insn)) |
ca7fd9cd | 981 | break; |
8206fc89 | 982 | } |
28f4ec01 BS |
983 | } |
984 | ||
862d0b35 DN |
985 | static inline bool |
986 | more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter) | |
987 | { | |
988 | return (iter->i < iter->n); | |
989 | } | |
990 | ||
991 | /* This function validates the types of a function call argument list | |
992 | against a specified list of tree_codes. If the last specifier is a 0, | |
993 | that represents an ellipses, otherwise the last specifier must be a | |
994 | VOID_TYPE. */ | |
995 | ||
996 | static bool | |
997 | validate_arglist (const_tree callexpr, ...) | |
998 | { | |
999 | enum tree_code code; | |
1000 | bool res = 0; | |
1001 | va_list ap; | |
1002 | const_call_expr_arg_iterator iter; | |
1003 | const_tree arg; | |
1004 | ||
1005 | va_start (ap, callexpr); | |
1006 | init_const_call_expr_arg_iterator (callexpr, &iter); | |
1007 | ||
1008 | do | |
1009 | { | |
1010 | code = (enum tree_code) va_arg (ap, int); | |
1011 | switch (code) | |
1012 | { | |
1013 | case 0: | |
1014 | /* This signifies an ellipses, any further arguments are all ok. */ | |
1015 | res = true; | |
1016 | goto end; | |
1017 | case VOID_TYPE: | |
1018 | /* This signifies an endlink, if no arguments remain, return | |
1019 | true, otherwise return false. */ | |
1020 | res = !more_const_call_expr_args_p (&iter); | |
1021 | goto end; | |
1022 | default: | |
1023 | /* If no parameters remain or the parameter's code does not | |
1024 | match the specified code, return false. Otherwise continue | |
1025 | checking any remaining arguments. */ | |
1026 | arg = next_const_call_expr_arg (&iter); | |
1027 | if (!validate_arg (arg, code)) | |
1028 | goto end; | |
1029 | break; | |
1030 | } | |
1031 | } | |
1032 | while (1); | |
1033 | ||
1034 | /* We need gotos here since we can only have one VA_CLOSE in a | |
1035 | function. */ | |
1036 | end: ; | |
1037 | va_end (ap); | |
1038 | ||
1039 | return res; | |
1040 | } | |
1041 | ||
6de9cd9a DN |
1042 | /* Expand a call to __builtin_nonlocal_goto. We're passed the target label |
1043 | and the address of the save area. */ | |
1044 | ||
1045 | static rtx | |
5039610b | 1046 | expand_builtin_nonlocal_goto (tree exp) |
6de9cd9a DN |
1047 | { |
1048 | tree t_label, t_save_area; | |
58f4cf2a DM |
1049 | rtx r_label, r_save_area, r_fp, r_sp; |
1050 | rtx_insn *insn; | |
6de9cd9a | 1051 | |
5039610b | 1052 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
6de9cd9a DN |
1053 | return NULL_RTX; |
1054 | ||
5039610b SL |
1055 | t_label = CALL_EXPR_ARG (exp, 0); |
1056 | t_save_area = CALL_EXPR_ARG (exp, 1); | |
6de9cd9a | 1057 | |
84217346 | 1058 | r_label = expand_normal (t_label); |
5e89a381 | 1059 | r_label = convert_memory_address (Pmode, r_label); |
84217346 | 1060 | r_save_area = expand_normal (t_save_area); |
5e89a381 | 1061 | r_save_area = convert_memory_address (Pmode, r_save_area); |
bc6d3f91 EB |
1062 | /* Copy the address of the save location to a register just in case it was |
1063 | based on the frame pointer. */ | |
cba2d79f | 1064 | r_save_area = copy_to_reg (r_save_area); |
6de9cd9a DN |
1065 | r_fp = gen_rtx_MEM (Pmode, r_save_area); |
1066 | r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL), | |
0a81f074 RS |
1067 | plus_constant (Pmode, r_save_area, |
1068 | GET_MODE_SIZE (Pmode))); | |
6de9cd9a | 1069 | |
e3b5732b | 1070 | crtl->has_nonlocal_goto = 1; |
6de9cd9a | 1071 | |
6de9cd9a | 1072 | /* ??? We no longer need to pass the static chain value, afaik. */ |
95a3fb9d RS |
1073 | if (targetm.have_nonlocal_goto ()) |
1074 | emit_insn (targetm.gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp)); | |
6de9cd9a | 1075 | else |
6de9cd9a DN |
1076 | { |
1077 | r_label = copy_to_reg (r_label); | |
1078 | ||
c41c1387 RS |
1079 | emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode))); |
1080 | emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx)); | |
caf93cb0 | 1081 | |
bc6d3f91 | 1082 | /* Restore frame pointer for containing function. */ |
6de9cd9a | 1083 | emit_move_insn (hard_frame_pointer_rtx, r_fp); |
9eac0f2a | 1084 | emit_stack_restore (SAVE_NONLOCAL, r_sp); |
caf93cb0 | 1085 | |
6de9cd9a DN |
1086 | /* USE of hard_frame_pointer_rtx added for consistency; |
1087 | not clear if really needed. */ | |
c41c1387 RS |
1088 | emit_use (hard_frame_pointer_rtx); |
1089 | emit_use (stack_pointer_rtx); | |
eae645b6 RS |
1090 | |
1091 | /* If the architecture is using a GP register, we must | |
1092 | conservatively assume that the target function makes use of it. | |
1093 | The prologue of functions with nonlocal gotos must therefore | |
1094 | initialize the GP register to the appropriate value, and we | |
1095 | must then make sure that this value is live at the point | |
1096 | of the jump. (Note that this doesn't necessarily apply | |
1097 | to targets with a nonlocal_goto pattern; they are free | |
1098 | to implement it in their own way. Note also that this is | |
1099 | a no-op if the GP register is a global invariant.) */ | |
1100 | if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM | |
1101 | && fixed_regs[PIC_OFFSET_TABLE_REGNUM]) | |
c41c1387 | 1102 | emit_use (pic_offset_table_rtx); |
eae645b6 | 1103 | |
6de9cd9a DN |
1104 | emit_indirect_jump (r_label); |
1105 | } | |
caf93cb0 | 1106 | |
6de9cd9a DN |
1107 | /* Search backwards to the jump insn and mark it as a |
1108 | non-local goto. */ | |
1109 | for (insn = get_last_insn (); insn; insn = PREV_INSN (insn)) | |
1110 | { | |
4b4bf941 | 1111 | if (JUMP_P (insn)) |
6de9cd9a | 1112 | { |
65c5f2a6 | 1113 | add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx); |
6de9cd9a DN |
1114 | break; |
1115 | } | |
4b4bf941 | 1116 | else if (CALL_P (insn)) |
6de9cd9a DN |
1117 | break; |
1118 | } | |
1119 | ||
1120 | return const0_rtx; | |
1121 | } | |
1122 | ||
2b92e7f5 RK |
1123 | /* __builtin_update_setjmp_buf is passed a pointer to an array of five words |
1124 | (not all will be used on all machines) that was passed to __builtin_setjmp. | |
d33606c3 EB |
1125 | It updates the stack pointer in that block to the current value. This is |
1126 | also called directly by the SJLJ exception handling code. */ | |
2b92e7f5 | 1127 | |
d33606c3 | 1128 | void |
2b92e7f5 RK |
1129 | expand_builtin_update_setjmp_buf (rtx buf_addr) |
1130 | { | |
ef4bddc2 | 1131 | machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL); |
bc6d3f91 | 1132 | rtx stack_save |
2b92e7f5 RK |
1133 | = gen_rtx_MEM (sa_mode, |
1134 | memory_address | |
1135 | (sa_mode, | |
0a81f074 RS |
1136 | plus_constant (Pmode, buf_addr, |
1137 | 2 * GET_MODE_SIZE (Pmode)))); | |
2b92e7f5 | 1138 | |
9eac0f2a | 1139 | emit_stack_save (SAVE_NONLOCAL, &stack_save); |
2b92e7f5 RK |
1140 | } |
1141 | ||
a9ccbb60 JJ |
1142 | /* Expand a call to __builtin_prefetch. For a target that does not support |
1143 | data prefetch, evaluate the memory address argument in case it has side | |
1144 | effects. */ | |
1145 | ||
1146 | static void | |
5039610b | 1147 | expand_builtin_prefetch (tree exp) |
a9ccbb60 JJ |
1148 | { |
1149 | tree arg0, arg1, arg2; | |
5039610b | 1150 | int nargs; |
a9ccbb60 JJ |
1151 | rtx op0, op1, op2; |
1152 | ||
5039610b | 1153 | if (!validate_arglist (exp, POINTER_TYPE, 0)) |
e83d297b JJ |
1154 | return; |
1155 | ||
5039610b SL |
1156 | arg0 = CALL_EXPR_ARG (exp, 0); |
1157 | ||
e83d297b JJ |
1158 | /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to |
1159 | zero (read) and argument 2 (locality) defaults to 3 (high degree of | |
1160 | locality). */ | |
5039610b SL |
1161 | nargs = call_expr_nargs (exp); |
1162 | if (nargs > 1) | |
1163 | arg1 = CALL_EXPR_ARG (exp, 1); | |
e83d297b | 1164 | else |
5039610b SL |
1165 | arg1 = integer_zero_node; |
1166 | if (nargs > 2) | |
1167 | arg2 = CALL_EXPR_ARG (exp, 2); | |
1168 | else | |
9a9d280e | 1169 | arg2 = integer_three_node; |
a9ccbb60 JJ |
1170 | |
1171 | /* Argument 0 is an address. */ | |
1172 | op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL); | |
1173 | ||
1174 | /* Argument 1 (read/write flag) must be a compile-time constant int. */ | |
1175 | if (TREE_CODE (arg1) != INTEGER_CST) | |
1176 | { | |
40b97a2e | 1177 | error ("second argument to %<__builtin_prefetch%> must be a constant"); |
ca7fd9cd | 1178 | arg1 = integer_zero_node; |
a9ccbb60 | 1179 | } |
84217346 | 1180 | op1 = expand_normal (arg1); |
a9ccbb60 JJ |
1181 | /* Argument 1 must be either zero or one. */ |
1182 | if (INTVAL (op1) != 0 && INTVAL (op1) != 1) | |
1183 | { | |
d4ee4d25 | 1184 | warning (0, "invalid second argument to %<__builtin_prefetch%>;" |
40b97a2e | 1185 | " using zero"); |
a9ccbb60 JJ |
1186 | op1 = const0_rtx; |
1187 | } | |
1188 | ||
1189 | /* Argument 2 (locality) must be a compile-time constant int. */ | |
1190 | if (TREE_CODE (arg2) != INTEGER_CST) | |
1191 | { | |
40b97a2e | 1192 | error ("third argument to %<__builtin_prefetch%> must be a constant"); |
a9ccbb60 JJ |
1193 | arg2 = integer_zero_node; |
1194 | } | |
84217346 | 1195 | op2 = expand_normal (arg2); |
a9ccbb60 JJ |
1196 | /* Argument 2 must be 0, 1, 2, or 3. */ |
1197 | if (INTVAL (op2) < 0 || INTVAL (op2) > 3) | |
1198 | { | |
d4ee4d25 | 1199 | warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero"); |
a9ccbb60 JJ |
1200 | op2 = const0_rtx; |
1201 | } | |
1202 | ||
134b044d | 1203 | if (targetm.have_prefetch ()) |
a9ccbb60 | 1204 | { |
a5c7d693 RS |
1205 | struct expand_operand ops[3]; |
1206 | ||
1207 | create_address_operand (&ops[0], op0); | |
1208 | create_integer_operand (&ops[1], INTVAL (op1)); | |
1209 | create_integer_operand (&ops[2], INTVAL (op2)); | |
134b044d | 1210 | if (maybe_expand_insn (targetm.code_for_prefetch, 3, ops)) |
a5c7d693 | 1211 | return; |
a9ccbb60 | 1212 | } |
ad76cef8 | 1213 | |
5ab2f7b7 KH |
1214 | /* Don't do anything with direct references to volatile memory, but |
1215 | generate code to handle other side effects. */ | |
3c0cb5de | 1216 | if (!MEM_P (op0) && side_effects_p (op0)) |
5ab2f7b7 | 1217 | emit_insn (op0); |
a9ccbb60 JJ |
1218 | } |
1219 | ||
3bdf5ad1 | 1220 | /* Get a MEM rtx for expression EXP which is the address of an operand |
435bb2a1 JJ |
1221 | to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is |
1222 | the maximum length of the block of memory that might be accessed or | |
1223 | NULL if unknown. */ | |
3bdf5ad1 | 1224 | |
28f4ec01 | 1225 | static rtx |
435bb2a1 | 1226 | get_memory_rtx (tree exp, tree len) |
28f4ec01 | 1227 | { |
805903b5 JJ |
1228 | tree orig_exp = exp; |
1229 | rtx addr, mem; | |
805903b5 JJ |
1230 | |
1231 | /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived | |
1232 | from its expression, for expr->a.b only <variable>.a.b is recorded. */ | |
1233 | if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp)) | |
1234 | exp = TREE_OPERAND (exp, 0); | |
1235 | ||
1236 | addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL); | |
1237 | mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr)); | |
8ac61af7 | 1238 | |
3bdf5ad1 | 1239 | /* Get an expression we can use to find the attributes to assign to MEM. |
625ed172 | 1240 | First remove any nops. */ |
1043771b | 1241 | while (CONVERT_EXPR_P (exp) |
3bdf5ad1 RK |
1242 | && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0)))) |
1243 | exp = TREE_OPERAND (exp, 0); | |
1244 | ||
625ed172 MM |
1245 | /* Build a MEM_REF representing the whole accessed area as a byte blob, |
1246 | (as builtin stringops may alias with anything). */ | |
1247 | exp = fold_build2 (MEM_REF, | |
1248 | build_array_type (char_type_node, | |
1249 | build_range_type (sizetype, | |
1250 | size_one_node, len)), | |
1251 | exp, build_int_cst (ptr_type_node, 0)); | |
1252 | ||
1253 | /* If the MEM_REF has no acceptable address, try to get the base object | |
1254 | from the original address we got, and build an all-aliasing | |
1255 | unknown-sized access to that one. */ | |
1256 | if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0))) | |
1257 | set_mem_attributes (mem, exp, 0); | |
1258 | else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR | |
1259 | && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0), | |
1260 | 0)))) | |
343fb412 | 1261 | { |
625ed172 MM |
1262 | exp = build_fold_addr_expr (exp); |
1263 | exp = fold_build2 (MEM_REF, | |
1264 | build_array_type (char_type_node, | |
1265 | build_range_type (sizetype, | |
1266 | size_zero_node, | |
1267 | NULL)), | |
1268 | exp, build_int_cst (ptr_type_node, 0)); | |
931e6c29 | 1269 | set_mem_attributes (mem, exp, 0); |
343fb412 | 1270 | } |
625ed172 | 1271 | set_mem_alias_set (mem, 0); |
28f4ec01 BS |
1272 | return mem; |
1273 | } | |
1274 | \f | |
1275 | /* Built-in functions to perform an untyped call and return. */ | |
1276 | ||
fa19795e RS |
1277 | #define apply_args_mode \ |
1278 | (this_target_builtins->x_apply_args_mode) | |
1279 | #define apply_result_mode \ | |
1280 | (this_target_builtins->x_apply_result_mode) | |
28f4ec01 | 1281 | |
28f4ec01 BS |
1282 | /* Return the size required for the block returned by __builtin_apply_args, |
1283 | and initialize apply_args_mode. */ | |
1284 | ||
1285 | static int | |
4682ae04 | 1286 | apply_args_size (void) |
28f4ec01 BS |
1287 | { |
1288 | static int size = -1; | |
cbf5468f AH |
1289 | int align; |
1290 | unsigned int regno; | |
ef4bddc2 | 1291 | machine_mode mode; |
28f4ec01 BS |
1292 | |
1293 | /* The values computed by this function never change. */ | |
1294 | if (size < 0) | |
1295 | { | |
1296 | /* The first value is the incoming arg-pointer. */ | |
1297 | size = GET_MODE_SIZE (Pmode); | |
1298 | ||
1299 | /* The second value is the structure value address unless this is | |
1300 | passed as an "invisible" first argument. */ | |
92f6864c | 1301 | if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0)) |
28f4ec01 BS |
1302 | size += GET_MODE_SIZE (Pmode); |
1303 | ||
1304 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1305 | if (FUNCTION_ARG_REGNO_P (regno)) | |
1306 | { | |
ffa88471 | 1307 | mode = targetm.calls.get_raw_arg_mode (regno); |
33521f7d | 1308 | |
298e6adc | 1309 | gcc_assert (mode != VOIDmode); |
28f4ec01 BS |
1310 | |
1311 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1312 | if (size % align != 0) | |
1313 | size = CEIL (size, align) * align; | |
28f4ec01 BS |
1314 | size += GET_MODE_SIZE (mode); |
1315 | apply_args_mode[regno] = mode; | |
1316 | } | |
1317 | else | |
1318 | { | |
1319 | apply_args_mode[regno] = VOIDmode; | |
28f4ec01 BS |
1320 | } |
1321 | } | |
1322 | return size; | |
1323 | } | |
1324 | ||
1325 | /* Return the size required for the block returned by __builtin_apply, | |
1326 | and initialize apply_result_mode. */ | |
1327 | ||
1328 | static int | |
4682ae04 | 1329 | apply_result_size (void) |
28f4ec01 BS |
1330 | { |
1331 | static int size = -1; | |
1332 | int align, regno; | |
ef4bddc2 | 1333 | machine_mode mode; |
28f4ec01 BS |
1334 | |
1335 | /* The values computed by this function never change. */ | |
1336 | if (size < 0) | |
1337 | { | |
1338 | size = 0; | |
1339 | ||
1340 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
82f81f18 | 1341 | if (targetm.calls.function_value_regno_p (regno)) |
28f4ec01 | 1342 | { |
ffa88471 | 1343 | mode = targetm.calls.get_raw_result_mode (regno); |
33521f7d | 1344 | |
298e6adc | 1345 | gcc_assert (mode != VOIDmode); |
28f4ec01 BS |
1346 | |
1347 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1348 | if (size % align != 0) | |
1349 | size = CEIL (size, align) * align; | |
1350 | size += GET_MODE_SIZE (mode); | |
1351 | apply_result_mode[regno] = mode; | |
1352 | } | |
1353 | else | |
1354 | apply_result_mode[regno] = VOIDmode; | |
1355 | ||
1356 | /* Allow targets that use untyped_call and untyped_return to override | |
1357 | the size so that machine-specific information can be stored here. */ | |
1358 | #ifdef APPLY_RESULT_SIZE | |
1359 | size = APPLY_RESULT_SIZE; | |
1360 | #endif | |
1361 | } | |
1362 | return size; | |
1363 | } | |
1364 | ||
28f4ec01 BS |
1365 | /* Create a vector describing the result block RESULT. If SAVEP is true, |
1366 | the result block is used to save the values; otherwise it is used to | |
1367 | restore the values. */ | |
1368 | ||
1369 | static rtx | |
4682ae04 | 1370 | result_vector (int savep, rtx result) |
28f4ec01 BS |
1371 | { |
1372 | int regno, size, align, nelts; | |
ef4bddc2 | 1373 | machine_mode mode; |
28f4ec01 | 1374 | rtx reg, mem; |
f883e0a7 | 1375 | rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER); |
8d51ecf8 | 1376 | |
28f4ec01 BS |
1377 | size = nelts = 0; |
1378 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1379 | if ((mode = apply_result_mode[regno]) != VOIDmode) | |
1380 | { | |
1381 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1382 | if (size % align != 0) | |
1383 | size = CEIL (size, align) * align; | |
1384 | reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno)); | |
f4ef873c | 1385 | mem = adjust_address (result, mode, size); |
28f4ec01 | 1386 | savevec[nelts++] = (savep |
f7df4a84 RS |
1387 | ? gen_rtx_SET (mem, reg) |
1388 | : gen_rtx_SET (reg, mem)); | |
28f4ec01 BS |
1389 | size += GET_MODE_SIZE (mode); |
1390 | } | |
1391 | return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec)); | |
1392 | } | |
28f4ec01 BS |
1393 | |
1394 | /* Save the state required to perform an untyped call with the same | |
1395 | arguments as were passed to the current function. */ | |
1396 | ||
1397 | static rtx | |
4682ae04 | 1398 | expand_builtin_apply_args_1 (void) |
28f4ec01 | 1399 | { |
88e541e1 | 1400 | rtx registers, tem; |
28f4ec01 | 1401 | int size, align, regno; |
ef4bddc2 | 1402 | machine_mode mode; |
92f6864c | 1403 | rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1); |
28f4ec01 BS |
1404 | |
1405 | /* Create a block where the arg-pointer, structure value address, | |
1406 | and argument registers can be saved. */ | |
1407 | registers = assign_stack_local (BLKmode, apply_args_size (), -1); | |
1408 | ||
1409 | /* Walk past the arg-pointer and structure value address. */ | |
1410 | size = GET_MODE_SIZE (Pmode); | |
92f6864c | 1411 | if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0)) |
28f4ec01 BS |
1412 | size += GET_MODE_SIZE (Pmode); |
1413 | ||
1414 | /* Save each register used in calling a function to the block. */ | |
1415 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1416 | if ((mode = apply_args_mode[regno]) != VOIDmode) | |
1417 | { | |
28f4ec01 BS |
1418 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; |
1419 | if (size % align != 0) | |
1420 | size = CEIL (size, align) * align; | |
1421 | ||
1422 | tem = gen_rtx_REG (mode, INCOMING_REGNO (regno)); | |
1423 | ||
f4ef873c | 1424 | emit_move_insn (adjust_address (registers, mode, size), tem); |
28f4ec01 BS |
1425 | size += GET_MODE_SIZE (mode); |
1426 | } | |
1427 | ||
1428 | /* Save the arg pointer to the block. */ | |
2e3f842f | 1429 | tem = copy_to_reg (crtl->args.internal_arg_pointer); |
88e541e1 | 1430 | /* We need the pointer as the caller actually passed them to us, not |
ac3f5df7 HPN |
1431 | as we might have pretended they were passed. Make sure it's a valid |
1432 | operand, as emit_move_insn isn't expected to handle a PLUS. */ | |
581edfa3 TS |
1433 | if (STACK_GROWS_DOWNWARD) |
1434 | tem | |
1435 | = force_operand (plus_constant (Pmode, tem, | |
1436 | crtl->args.pretend_args_size), | |
1437 | NULL_RTX); | |
88e541e1 | 1438 | emit_move_insn (adjust_address (registers, Pmode, 0), tem); |
33521f7d | 1439 | |
28f4ec01 BS |
1440 | size = GET_MODE_SIZE (Pmode); |
1441 | ||
1442 | /* Save the structure value address unless this is passed as an | |
1443 | "invisible" first argument. */ | |
61f71b34 | 1444 | if (struct_incoming_value) |
28f4ec01 | 1445 | { |
f4ef873c | 1446 | emit_move_insn (adjust_address (registers, Pmode, size), |
61f71b34 | 1447 | copy_to_reg (struct_incoming_value)); |
28f4ec01 BS |
1448 | size += GET_MODE_SIZE (Pmode); |
1449 | } | |
1450 | ||
1451 | /* Return the address of the block. */ | |
1452 | return copy_addr_to_reg (XEXP (registers, 0)); | |
1453 | } | |
1454 | ||
1455 | /* __builtin_apply_args returns block of memory allocated on | |
1456 | the stack into which is stored the arg pointer, structure | |
1457 | value address, static chain, and all the registers that might | |
1458 | possibly be used in performing a function call. The code is | |
1459 | moved to the start of the function so the incoming values are | |
1460 | saved. */ | |
5197bd50 | 1461 | |
28f4ec01 | 1462 | static rtx |
4682ae04 | 1463 | expand_builtin_apply_args (void) |
28f4ec01 BS |
1464 | { |
1465 | /* Don't do __builtin_apply_args more than once in a function. | |
1466 | Save the result of the first call and reuse it. */ | |
1467 | if (apply_args_value != 0) | |
1468 | return apply_args_value; | |
1469 | { | |
1470 | /* When this function is called, it means that registers must be | |
1471 | saved on entry to this function. So we migrate the | |
1472 | call to the first insn of this function. */ | |
1473 | rtx temp; | |
28f4ec01 BS |
1474 | |
1475 | start_sequence (); | |
1476 | temp = expand_builtin_apply_args_1 (); | |
e67d1102 | 1477 | rtx_insn *seq = get_insns (); |
28f4ec01 BS |
1478 | end_sequence (); |
1479 | ||
1480 | apply_args_value = temp; | |
1481 | ||
2f937369 DM |
1482 | /* Put the insns after the NOTE that starts the function. |
1483 | If this is inside a start_sequence, make the outer-level insn | |
28f4ec01 | 1484 | chain current, so the code is placed at the start of the |
1f21b6f4 JJ |
1485 | function. If internal_arg_pointer is a non-virtual pseudo, |
1486 | it needs to be placed after the function that initializes | |
1487 | that pseudo. */ | |
28f4ec01 | 1488 | push_topmost_sequence (); |
1f21b6f4 JJ |
1489 | if (REG_P (crtl->args.internal_arg_pointer) |
1490 | && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER) | |
1491 | emit_insn_before (seq, parm_birth_insn); | |
1492 | else | |
1493 | emit_insn_before (seq, NEXT_INSN (entry_of_function ())); | |
28f4ec01 BS |
1494 | pop_topmost_sequence (); |
1495 | return temp; | |
1496 | } | |
1497 | } | |
1498 | ||
1499 | /* Perform an untyped call and save the state required to perform an | |
1500 | untyped return of whatever value was returned by the given function. */ | |
1501 | ||
1502 | static rtx | |
4682ae04 | 1503 | expand_builtin_apply (rtx function, rtx arguments, rtx argsize) |
28f4ec01 BS |
1504 | { |
1505 | int size, align, regno; | |
ef4bddc2 | 1506 | machine_mode mode; |
58f4cf2a DM |
1507 | rtx incoming_args, result, reg, dest, src; |
1508 | rtx_call_insn *call_insn; | |
28f4ec01 BS |
1509 | rtx old_stack_level = 0; |
1510 | rtx call_fusage = 0; | |
92f6864c | 1511 | rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0); |
28f4ec01 | 1512 | |
5ae6cd0d | 1513 | arguments = convert_memory_address (Pmode, arguments); |
ce2d32cd | 1514 | |
28f4ec01 BS |
1515 | /* Create a block where the return registers can be saved. */ |
1516 | result = assign_stack_local (BLKmode, apply_result_size (), -1); | |
1517 | ||
28f4ec01 BS |
1518 | /* Fetch the arg pointer from the ARGUMENTS block. */ |
1519 | incoming_args = gen_reg_rtx (Pmode); | |
ce2d32cd | 1520 | emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments)); |
581edfa3 TS |
1521 | if (!STACK_GROWS_DOWNWARD) |
1522 | incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize, | |
1523 | incoming_args, 0, OPTAB_LIB_WIDEN); | |
28f4ec01 | 1524 | |
9d53e585 JM |
1525 | /* Push a new argument block and copy the arguments. Do not allow |
1526 | the (potential) memcpy call below to interfere with our stack | |
1527 | manipulations. */ | |
28f4ec01 | 1528 | do_pending_stack_adjust (); |
9d53e585 | 1529 | NO_DEFER_POP; |
28f4ec01 | 1530 | |
f9da5064 | 1531 | /* Save the stack with nonlocal if available. */ |
4476e1a0 | 1532 | if (targetm.have_save_stack_nonlocal ()) |
9eac0f2a | 1533 | emit_stack_save (SAVE_NONLOCAL, &old_stack_level); |
28f4ec01 | 1534 | else |
9eac0f2a | 1535 | emit_stack_save (SAVE_BLOCK, &old_stack_level); |
28f4ec01 | 1536 | |
316d0b19 | 1537 | /* Allocate a block of memory onto the stack and copy the memory |
d3c12306 EB |
1538 | arguments to the outgoing arguments address. We can pass TRUE |
1539 | as the 4th argument because we just saved the stack pointer | |
1540 | and will restore it right after the call. */ | |
3a42502d | 1541 | allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true); |
2e3f842f L |
1542 | |
1543 | /* Set DRAP flag to true, even though allocate_dynamic_stack_space | |
1544 | may have already set current_function_calls_alloca to true. | |
1545 | current_function_calls_alloca won't be set if argsize is zero, | |
1546 | so we have to guarantee need_drap is true here. */ | |
1547 | if (SUPPORTS_STACK_ALIGNMENT) | |
1548 | crtl->need_drap = true; | |
1549 | ||
316d0b19 | 1550 | dest = virtual_outgoing_args_rtx; |
581edfa3 TS |
1551 | if (!STACK_GROWS_DOWNWARD) |
1552 | { | |
1553 | if (CONST_INT_P (argsize)) | |
1554 | dest = plus_constant (Pmode, dest, -INTVAL (argsize)); | |
1555 | else | |
1556 | dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize)); | |
1557 | } | |
8ac61af7 RK |
1558 | dest = gen_rtx_MEM (BLKmode, dest); |
1559 | set_mem_align (dest, PARM_BOUNDARY); | |
1560 | src = gen_rtx_MEM (BLKmode, incoming_args); | |
1561 | set_mem_align (src, PARM_BOUNDARY); | |
44bb111a | 1562 | emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL); |
28f4ec01 BS |
1563 | |
1564 | /* Refer to the argument block. */ | |
1565 | apply_args_size (); | |
1566 | arguments = gen_rtx_MEM (BLKmode, arguments); | |
8ac61af7 | 1567 | set_mem_align (arguments, PARM_BOUNDARY); |
28f4ec01 BS |
1568 | |
1569 | /* Walk past the arg-pointer and structure value address. */ | |
1570 | size = GET_MODE_SIZE (Pmode); | |
61f71b34 | 1571 | if (struct_value) |
28f4ec01 BS |
1572 | size += GET_MODE_SIZE (Pmode); |
1573 | ||
1574 | /* Restore each of the registers previously saved. Make USE insns | |
1575 | for each of these registers for use in making the call. */ | |
1576 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1577 | if ((mode = apply_args_mode[regno]) != VOIDmode) | |
1578 | { | |
1579 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1580 | if (size % align != 0) | |
1581 | size = CEIL (size, align) * align; | |
1582 | reg = gen_rtx_REG (mode, regno); | |
f4ef873c | 1583 | emit_move_insn (reg, adjust_address (arguments, mode, size)); |
28f4ec01 BS |
1584 | use_reg (&call_fusage, reg); |
1585 | size += GET_MODE_SIZE (mode); | |
1586 | } | |
1587 | ||
1588 | /* Restore the structure value address unless this is passed as an | |
1589 | "invisible" first argument. */ | |
1590 | size = GET_MODE_SIZE (Pmode); | |
61f71b34 | 1591 | if (struct_value) |
28f4ec01 BS |
1592 | { |
1593 | rtx value = gen_reg_rtx (Pmode); | |
f4ef873c | 1594 | emit_move_insn (value, adjust_address (arguments, Pmode, size)); |
61f71b34 | 1595 | emit_move_insn (struct_value, value); |
f8cfc6aa | 1596 | if (REG_P (struct_value)) |
61f71b34 | 1597 | use_reg (&call_fusage, struct_value); |
28f4ec01 BS |
1598 | size += GET_MODE_SIZE (Pmode); |
1599 | } | |
1600 | ||
1601 | /* All arguments and registers used for the call are set up by now! */ | |
531ca746 | 1602 | function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0); |
28f4ec01 BS |
1603 | |
1604 | /* Ensure address is valid. SYMBOL_REF is already valid, so no need, | |
1605 | and we don't want to load it into a register as an optimization, | |
1606 | because prepare_call_address already did it if it should be done. */ | |
1607 | if (GET_CODE (function) != SYMBOL_REF) | |
1608 | function = memory_address (FUNCTION_MODE, function); | |
1609 | ||
1610 | /* Generate the actual call instruction and save the return value. */ | |
43c7dca8 RS |
1611 | if (targetm.have_untyped_call ()) |
1612 | { | |
1613 | rtx mem = gen_rtx_MEM (FUNCTION_MODE, function); | |
1614 | emit_call_insn (targetm.gen_untyped_call (mem, result, | |
1615 | result_vector (1, result))); | |
1616 | } | |
58d745ec | 1617 | else if (targetm.have_call_value ()) |
28f4ec01 BS |
1618 | { |
1619 | rtx valreg = 0; | |
1620 | ||
1621 | /* Locate the unique return register. It is not possible to | |
1622 | express a call that sets more than one return register using | |
1623 | call_value; use untyped_call for that. In fact, untyped_call | |
1624 | only needs to save the return registers in the given block. */ | |
1625 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1626 | if ((mode = apply_result_mode[regno]) != VOIDmode) | |
1627 | { | |
58d745ec | 1628 | gcc_assert (!valreg); /* have_untyped_call required. */ |
5906d013 | 1629 | |
28f4ec01 BS |
1630 | valreg = gen_rtx_REG (mode, regno); |
1631 | } | |
1632 | ||
58d745ec RS |
1633 | emit_insn (targetm.gen_call_value (valreg, |
1634 | gen_rtx_MEM (FUNCTION_MODE, function), | |
1635 | const0_rtx, NULL_RTX, const0_rtx)); | |
28f4ec01 | 1636 | |
f4ef873c | 1637 | emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg); |
28f4ec01 BS |
1638 | } |
1639 | else | |
298e6adc | 1640 | gcc_unreachable (); |
28f4ec01 | 1641 | |
ee960939 OH |
1642 | /* Find the CALL insn we just emitted, and attach the register usage |
1643 | information. */ | |
1644 | call_insn = last_call_insn (); | |
1645 | add_function_usage_to (call_insn, call_fusage); | |
28f4ec01 BS |
1646 | |
1647 | /* Restore the stack. */ | |
4476e1a0 | 1648 | if (targetm.have_save_stack_nonlocal ()) |
9eac0f2a | 1649 | emit_stack_restore (SAVE_NONLOCAL, old_stack_level); |
28f4ec01 | 1650 | else |
9eac0f2a | 1651 | emit_stack_restore (SAVE_BLOCK, old_stack_level); |
c3284718 | 1652 | fixup_args_size_notes (call_insn, get_last_insn (), 0); |
28f4ec01 | 1653 | |
9d53e585 JM |
1654 | OK_DEFER_POP; |
1655 | ||
28f4ec01 | 1656 | /* Return the address of the result block. */ |
5ae6cd0d MM |
1657 | result = copy_addr_to_reg (XEXP (result, 0)); |
1658 | return convert_memory_address (ptr_mode, result); | |
28f4ec01 BS |
1659 | } |
1660 | ||
1661 | /* Perform an untyped return. */ | |
1662 | ||
1663 | static void | |
4682ae04 | 1664 | expand_builtin_return (rtx result) |
28f4ec01 BS |
1665 | { |
1666 | int size, align, regno; | |
ef4bddc2 | 1667 | machine_mode mode; |
28f4ec01 | 1668 | rtx reg; |
fee3e72c | 1669 | rtx_insn *call_fusage = 0; |
28f4ec01 | 1670 | |
5ae6cd0d | 1671 | result = convert_memory_address (Pmode, result); |
ce2d32cd | 1672 | |
28f4ec01 BS |
1673 | apply_result_size (); |
1674 | result = gen_rtx_MEM (BLKmode, result); | |
1675 | ||
43c7dca8 | 1676 | if (targetm.have_untyped_return ()) |
28f4ec01 | 1677 | { |
43c7dca8 RS |
1678 | rtx vector = result_vector (0, result); |
1679 | emit_jump_insn (targetm.gen_untyped_return (result, vector)); | |
28f4ec01 BS |
1680 | emit_barrier (); |
1681 | return; | |
1682 | } | |
28f4ec01 BS |
1683 | |
1684 | /* Restore the return value and note that each value is used. */ | |
1685 | size = 0; | |
1686 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1687 | if ((mode = apply_result_mode[regno]) != VOIDmode) | |
1688 | { | |
1689 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1690 | if (size % align != 0) | |
1691 | size = CEIL (size, align) * align; | |
1692 | reg = gen_rtx_REG (mode, INCOMING_REGNO (regno)); | |
f4ef873c | 1693 | emit_move_insn (reg, adjust_address (result, mode, size)); |
28f4ec01 BS |
1694 | |
1695 | push_to_sequence (call_fusage); | |
c41c1387 | 1696 | emit_use (reg); |
28f4ec01 BS |
1697 | call_fusage = get_insns (); |
1698 | end_sequence (); | |
1699 | size += GET_MODE_SIZE (mode); | |
1700 | } | |
1701 | ||
1702 | /* Put the USE insns before the return. */ | |
2f937369 | 1703 | emit_insn (call_fusage); |
28f4ec01 BS |
1704 | |
1705 | /* Return whatever values was restored by jumping directly to the end | |
1706 | of the function. */ | |
6e3077c6 | 1707 | expand_naked_return (); |
28f4ec01 BS |
1708 | } |
1709 | ||
ad82abb8 | 1710 | /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */ |
5197bd50 | 1711 | |
ad82abb8 | 1712 | static enum type_class |
4682ae04 | 1713 | type_to_class (tree type) |
ad82abb8 ZW |
1714 | { |
1715 | switch (TREE_CODE (type)) | |
1716 | { | |
1717 | case VOID_TYPE: return void_type_class; | |
1718 | case INTEGER_TYPE: return integer_type_class; | |
ad82abb8 ZW |
1719 | case ENUMERAL_TYPE: return enumeral_type_class; |
1720 | case BOOLEAN_TYPE: return boolean_type_class; | |
1721 | case POINTER_TYPE: return pointer_type_class; | |
1722 | case REFERENCE_TYPE: return reference_type_class; | |
1723 | case OFFSET_TYPE: return offset_type_class; | |
1724 | case REAL_TYPE: return real_type_class; | |
1725 | case COMPLEX_TYPE: return complex_type_class; | |
1726 | case FUNCTION_TYPE: return function_type_class; | |
1727 | case METHOD_TYPE: return method_type_class; | |
1728 | case RECORD_TYPE: return record_type_class; | |
1729 | case UNION_TYPE: | |
1730 | case QUAL_UNION_TYPE: return union_type_class; | |
1731 | case ARRAY_TYPE: return (TYPE_STRING_FLAG (type) | |
1732 | ? string_type_class : array_type_class); | |
ad82abb8 ZW |
1733 | case LANG_TYPE: return lang_type_class; |
1734 | default: return no_type_class; | |
1735 | } | |
1736 | } | |
8d51ecf8 | 1737 | |
5039610b | 1738 | /* Expand a call EXP to __builtin_classify_type. */ |
5197bd50 | 1739 | |
28f4ec01 | 1740 | static rtx |
5039610b | 1741 | expand_builtin_classify_type (tree exp) |
28f4ec01 | 1742 | { |
5039610b SL |
1743 | if (call_expr_nargs (exp)) |
1744 | return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))); | |
28f4ec01 BS |
1745 | return GEN_INT (no_type_class); |
1746 | } | |
1747 | ||
daa027cc KG |
1748 | /* This helper macro, meant to be used in mathfn_built_in below, |
1749 | determines which among a set of three builtin math functions is | |
1750 | appropriate for a given type mode. The `F' and `L' cases are | |
1751 | automatically generated from the `double' case. */ | |
b03ff92e RS |
1752 | #define CASE_MATHFN(MATHFN) \ |
1753 | CASE_CFN_##MATHFN: \ | |
1754 | fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \ | |
1755 | fcodel = BUILT_IN_##MATHFN##L ; break; | |
bf460eec | 1756 | /* Similar to above, but appends _R after any F/L suffix. */ |
b03ff92e RS |
1757 | #define CASE_MATHFN_REENT(MATHFN) \ |
1758 | case CFN_BUILT_IN_##MATHFN##_R: \ | |
1759 | case CFN_BUILT_IN_##MATHFN##F_R: \ | |
1760 | case CFN_BUILT_IN_##MATHFN##L_R: \ | |
1761 | fcode = BUILT_IN_##MATHFN##_R; fcodef = BUILT_IN_##MATHFN##F_R ; \ | |
1762 | fcodel = BUILT_IN_##MATHFN##L_R ; break; | |
daa027cc | 1763 | |
5c1a2e63 RS |
1764 | /* Return a function equivalent to FN but operating on floating-point |
1765 | values of type TYPE, or END_BUILTINS if no such function exists. | |
b03ff92e RS |
1766 | This is purely an operation on function codes; it does not guarantee |
1767 | that the target actually has an implementation of the function. */ | |
05f41289 | 1768 | |
5c1a2e63 | 1769 | static built_in_function |
b03ff92e | 1770 | mathfn_built_in_2 (tree type, combined_fn fn) |
272f51a3 | 1771 | { |
5c1a2e63 | 1772 | built_in_function fcode, fcodef, fcodel; |
daa027cc KG |
1773 | |
1774 | switch (fn) | |
1775 | { | |
b03ff92e RS |
1776 | CASE_MATHFN (ACOS) |
1777 | CASE_MATHFN (ACOSH) | |
1778 | CASE_MATHFN (ASIN) | |
1779 | CASE_MATHFN (ASINH) | |
1780 | CASE_MATHFN (ATAN) | |
1781 | CASE_MATHFN (ATAN2) | |
1782 | CASE_MATHFN (ATANH) | |
1783 | CASE_MATHFN (CBRT) | |
1784 | CASE_MATHFN (CEIL) | |
1785 | CASE_MATHFN (CEXPI) | |
1786 | CASE_MATHFN (COPYSIGN) | |
1787 | CASE_MATHFN (COS) | |
1788 | CASE_MATHFN (COSH) | |
1789 | CASE_MATHFN (DREM) | |
1790 | CASE_MATHFN (ERF) | |
1791 | CASE_MATHFN (ERFC) | |
1792 | CASE_MATHFN (EXP) | |
1793 | CASE_MATHFN (EXP10) | |
1794 | CASE_MATHFN (EXP2) | |
1795 | CASE_MATHFN (EXPM1) | |
1796 | CASE_MATHFN (FABS) | |
1797 | CASE_MATHFN (FDIM) | |
1798 | CASE_MATHFN (FLOOR) | |
1799 | CASE_MATHFN (FMA) | |
1800 | CASE_MATHFN (FMAX) | |
1801 | CASE_MATHFN (FMIN) | |
1802 | CASE_MATHFN (FMOD) | |
1803 | CASE_MATHFN (FREXP) | |
1804 | CASE_MATHFN (GAMMA) | |
1805 | CASE_MATHFN_REENT (GAMMA) /* GAMMA_R */ | |
1806 | CASE_MATHFN (HUGE_VAL) | |
1807 | CASE_MATHFN (HYPOT) | |
1808 | CASE_MATHFN (ILOGB) | |
1809 | CASE_MATHFN (ICEIL) | |
1810 | CASE_MATHFN (IFLOOR) | |
1811 | CASE_MATHFN (INF) | |
1812 | CASE_MATHFN (IRINT) | |
1813 | CASE_MATHFN (IROUND) | |
1814 | CASE_MATHFN (ISINF) | |
1815 | CASE_MATHFN (J0) | |
1816 | CASE_MATHFN (J1) | |
1817 | CASE_MATHFN (JN) | |
1818 | CASE_MATHFN (LCEIL) | |
1819 | CASE_MATHFN (LDEXP) | |
1820 | CASE_MATHFN (LFLOOR) | |
1821 | CASE_MATHFN (LGAMMA) | |
1822 | CASE_MATHFN_REENT (LGAMMA) /* LGAMMA_R */ | |
1823 | CASE_MATHFN (LLCEIL) | |
1824 | CASE_MATHFN (LLFLOOR) | |
1825 | CASE_MATHFN (LLRINT) | |
1826 | CASE_MATHFN (LLROUND) | |
1827 | CASE_MATHFN (LOG) | |
1828 | CASE_MATHFN (LOG10) | |
1829 | CASE_MATHFN (LOG1P) | |
1830 | CASE_MATHFN (LOG2) | |
1831 | CASE_MATHFN (LOGB) | |
1832 | CASE_MATHFN (LRINT) | |
1833 | CASE_MATHFN (LROUND) | |
1834 | CASE_MATHFN (MODF) | |
1835 | CASE_MATHFN (NAN) | |
1836 | CASE_MATHFN (NANS) | |
1837 | CASE_MATHFN (NEARBYINT) | |
1838 | CASE_MATHFN (NEXTAFTER) | |
1839 | CASE_MATHFN (NEXTTOWARD) | |
1840 | CASE_MATHFN (POW) | |
1841 | CASE_MATHFN (POWI) | |
1842 | CASE_MATHFN (POW10) | |
1843 | CASE_MATHFN (REMAINDER) | |
1844 | CASE_MATHFN (REMQUO) | |
1845 | CASE_MATHFN (RINT) | |
1846 | CASE_MATHFN (ROUND) | |
1847 | CASE_MATHFN (SCALB) | |
1848 | CASE_MATHFN (SCALBLN) | |
1849 | CASE_MATHFN (SCALBN) | |
1850 | CASE_MATHFN (SIGNBIT) | |
1851 | CASE_MATHFN (SIGNIFICAND) | |
1852 | CASE_MATHFN (SIN) | |
1853 | CASE_MATHFN (SINCOS) | |
1854 | CASE_MATHFN (SINH) | |
1855 | CASE_MATHFN (SQRT) | |
1856 | CASE_MATHFN (TAN) | |
1857 | CASE_MATHFN (TANH) | |
1858 | CASE_MATHFN (TGAMMA) | |
1859 | CASE_MATHFN (TRUNC) | |
1860 | CASE_MATHFN (Y0) | |
1861 | CASE_MATHFN (Y1) | |
1862 | CASE_MATHFN (YN) | |
daa027cc | 1863 | |
b03ff92e RS |
1864 | default: |
1865 | return END_BUILTINS; | |
1866 | } | |
daa027cc | 1867 | |
ce58118c | 1868 | if (TYPE_MAIN_VARIANT (type) == double_type_node) |
5c1a2e63 | 1869 | return fcode; |
ce58118c | 1870 | else if (TYPE_MAIN_VARIANT (type) == float_type_node) |
5c1a2e63 | 1871 | return fcodef; |
ce58118c | 1872 | else if (TYPE_MAIN_VARIANT (type) == long_double_type_node) |
5c1a2e63 | 1873 | return fcodel; |
daa027cc | 1874 | else |
5c1a2e63 RS |
1875 | return END_BUILTINS; |
1876 | } | |
1877 | ||
1878 | /* Return mathematic function equivalent to FN but operating directly on TYPE, | |
1879 | if available. If IMPLICIT_P is true use the implicit builtin declaration, | |
1880 | otherwise use the explicit declaration. If we can't do the conversion, | |
1881 | return null. */ | |
1882 | ||
1883 | static tree | |
b03ff92e | 1884 | mathfn_built_in_1 (tree type, combined_fn fn, bool implicit_p) |
5c1a2e63 RS |
1885 | { |
1886 | built_in_function fcode2 = mathfn_built_in_2 (type, fn); | |
1887 | if (fcode2 == END_BUILTINS) | |
5039610b | 1888 | return NULL_TREE; |
e79983f4 MM |
1889 | |
1890 | if (implicit_p && !builtin_decl_implicit_p (fcode2)) | |
1891 | return NULL_TREE; | |
1892 | ||
1893 | return builtin_decl_explicit (fcode2); | |
272f51a3 JH |
1894 | } |
1895 | ||
b03ff92e | 1896 | /* Like mathfn_built_in_1, but always use the implicit array. */ |
05f41289 KG |
1897 | |
1898 | tree | |
b03ff92e | 1899 | mathfn_built_in (tree type, combined_fn fn) |
05f41289 KG |
1900 | { |
1901 | return mathfn_built_in_1 (type, fn, /*implicit=*/ 1); | |
1902 | } | |
1903 | ||
b03ff92e RS |
1904 | /* Like mathfn_built_in_1, but take a built_in_function and |
1905 | always use the implicit array. */ | |
1906 | ||
1907 | tree | |
1908 | mathfn_built_in (tree type, enum built_in_function fn) | |
1909 | { | |
1910 | return mathfn_built_in_1 (type, as_combined_fn (fn), /*implicit=*/ 1); | |
1911 | } | |
1912 | ||
686ee971 RS |
1913 | /* If BUILT_IN_NORMAL function FNDECL has an associated internal function, |
1914 | return its code, otherwise return IFN_LAST. Note that this function | |
1915 | only tests whether the function is defined in internals.def, not whether | |
1916 | it is actually available on the target. */ | |
1917 | ||
1918 | internal_fn | |
1919 | associated_internal_fn (tree fndecl) | |
1920 | { | |
1921 | gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL); | |
1922 | tree return_type = TREE_TYPE (TREE_TYPE (fndecl)); | |
1923 | switch (DECL_FUNCTION_CODE (fndecl)) | |
1924 | { | |
1925 | #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \ | |
1926 | CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME; | |
4959a752 RS |
1927 | #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \ |
1928 | CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME; | |
686ee971 RS |
1929 | #include "internal-fn.def" |
1930 | ||
1931 | CASE_FLT_FN (BUILT_IN_POW10): | |
1932 | return IFN_EXP10; | |
1933 | ||
1934 | CASE_FLT_FN (BUILT_IN_DREM): | |
1935 | return IFN_REMAINDER; | |
1936 | ||
1937 | CASE_FLT_FN (BUILT_IN_SCALBN): | |
1938 | CASE_FLT_FN (BUILT_IN_SCALBLN): | |
1939 | if (REAL_MODE_FORMAT (TYPE_MODE (return_type))->b == 2) | |
1940 | return IFN_LDEXP; | |
1941 | return IFN_LAST; | |
1942 | ||
1943 | default: | |
1944 | return IFN_LAST; | |
1945 | } | |
1946 | } | |
1947 | ||
1948 | /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced | |
1949 | on the current target by a call to an internal function, return the | |
1950 | code of that internal function, otherwise return IFN_LAST. The caller | |
1951 | is responsible for ensuring that any side-effects of the built-in | |
1952 | call are dealt with correctly. E.g. if CALL sets errno, the caller | |
1953 | must decide that the errno result isn't needed or make it available | |
1954 | in some other way. */ | |
1955 | ||
1956 | internal_fn | |
1957 | replacement_internal_fn (gcall *call) | |
1958 | { | |
1959 | if (gimple_call_builtin_p (call, BUILT_IN_NORMAL)) | |
1960 | { | |
1961 | internal_fn ifn = associated_internal_fn (gimple_call_fndecl (call)); | |
1962 | if (ifn != IFN_LAST) | |
1963 | { | |
1964 | tree_pair types = direct_internal_fn_types (ifn, call); | |
d95ab70a RS |
1965 | optimization_type opt_type = bb_optimization_type (gimple_bb (call)); |
1966 | if (direct_internal_fn_supported_p (ifn, types, opt_type)) | |
686ee971 RS |
1967 | return ifn; |
1968 | } | |
1969 | } | |
1970 | return IFN_LAST; | |
1971 | } | |
1972 | ||
1b1562a5 MM |
1973 | /* Expand a call to the builtin trinary math functions (fma). |
1974 | Return NULL_RTX if a normal call should be emitted rather than expanding the | |
1975 | function in-line. EXP is the expression that is a call to the builtin | |
1976 | function; if convenient, the result should be placed in TARGET. | |
1977 | SUBTARGET may be used as the target for computing one of EXP's | |
1978 | operands. */ | |
1979 | ||
1980 | static rtx | |
1981 | expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget) | |
1982 | { | |
1983 | optab builtin_optab; | |
58f4cf2a DM |
1984 | rtx op0, op1, op2, result; |
1985 | rtx_insn *insns; | |
1b1562a5 MM |
1986 | tree fndecl = get_callee_fndecl (exp); |
1987 | tree arg0, arg1, arg2; | |
ef4bddc2 | 1988 | machine_mode mode; |
1b1562a5 MM |
1989 | |
1990 | if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE)) | |
1991 | return NULL_RTX; | |
1992 | ||
1993 | arg0 = CALL_EXPR_ARG (exp, 0); | |
1994 | arg1 = CALL_EXPR_ARG (exp, 1); | |
1995 | arg2 = CALL_EXPR_ARG (exp, 2); | |
1996 | ||
1997 | switch (DECL_FUNCTION_CODE (fndecl)) | |
1998 | { | |
1999 | CASE_FLT_FN (BUILT_IN_FMA): | |
2000 | builtin_optab = fma_optab; break; | |
2001 | default: | |
2002 | gcc_unreachable (); | |
2003 | } | |
2004 | ||
2005 | /* Make a suitable register to place result in. */ | |
2006 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
2007 | ||
2008 | /* Before working hard, check whether the instruction is available. */ | |
2009 | if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing) | |
2010 | return NULL_RTX; | |
2011 | ||
04b80dbb | 2012 | result = gen_reg_rtx (mode); |
1b1562a5 MM |
2013 | |
2014 | /* Always stabilize the argument list. */ | |
2015 | CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0); | |
2016 | CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1); | |
2017 | CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2); | |
2018 | ||
2019 | op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL); | |
2020 | op1 = expand_normal (arg1); | |
2021 | op2 = expand_normal (arg2); | |
2022 | ||
2023 | start_sequence (); | |
2024 | ||
04b80dbb RS |
2025 | /* Compute into RESULT. |
2026 | Set RESULT to wherever the result comes back. */ | |
2027 | result = expand_ternary_op (mode, builtin_optab, op0, op1, op2, | |
2028 | result, 0); | |
1b1562a5 MM |
2029 | |
2030 | /* If we were unable to expand via the builtin, stop the sequence | |
2031 | (without outputting the insns) and call to the library function | |
2032 | with the stabilized argument list. */ | |
04b80dbb | 2033 | if (result == 0) |
1b1562a5 MM |
2034 | { |
2035 | end_sequence (); | |
2036 | return expand_call (exp, target, target == const0_rtx); | |
2037 | } | |
2038 | ||
2039 | /* Output the entire sequence. */ | |
2040 | insns = get_insns (); | |
2041 | end_sequence (); | |
2042 | emit_insn (insns); | |
2043 | ||
04b80dbb | 2044 | return result; |
1b1562a5 MM |
2045 | } |
2046 | ||
6c7cf1f0 | 2047 | /* Expand a call to the builtin sin and cos math functions. |
5039610b | 2048 | Return NULL_RTX if a normal call should be emitted rather than expanding the |
6c7cf1f0 UB |
2049 | function in-line. EXP is the expression that is a call to the builtin |
2050 | function; if convenient, the result should be placed in TARGET. | |
2051 | SUBTARGET may be used as the target for computing one of EXP's | |
2052 | operands. */ | |
2053 | ||
2054 | static rtx | |
2055 | expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget) | |
2056 | { | |
2057 | optab builtin_optab; | |
58f4cf2a DM |
2058 | rtx op0; |
2059 | rtx_insn *insns; | |
6c7cf1f0 | 2060 | tree fndecl = get_callee_fndecl (exp); |
ef4bddc2 | 2061 | machine_mode mode; |
5799f732 | 2062 | tree arg; |
6c7cf1f0 | 2063 | |
5039610b SL |
2064 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
2065 | return NULL_RTX; | |
6c7cf1f0 | 2066 | |
5039610b | 2067 | arg = CALL_EXPR_ARG (exp, 0); |
6c7cf1f0 UB |
2068 | |
2069 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2070 | { | |
ea6a6627 VR |
2071 | CASE_FLT_FN (BUILT_IN_SIN): |
2072 | CASE_FLT_FN (BUILT_IN_COS): | |
6c7cf1f0 UB |
2073 | builtin_optab = sincos_optab; break; |
2074 | default: | |
298e6adc | 2075 | gcc_unreachable (); |
6c7cf1f0 UB |
2076 | } |
2077 | ||
2078 | /* Make a suitable register to place result in. */ | |
2079 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
2080 | ||
6c7cf1f0 | 2081 | /* Check if sincos insn is available, otherwise fallback |
9cf737f8 | 2082 | to sin or cos insn. */ |
947131ba | 2083 | if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing) |
6c7cf1f0 UB |
2084 | switch (DECL_FUNCTION_CODE (fndecl)) |
2085 | { | |
ea6a6627 | 2086 | CASE_FLT_FN (BUILT_IN_SIN): |
6c7cf1f0 | 2087 | builtin_optab = sin_optab; break; |
ea6a6627 | 2088 | CASE_FLT_FN (BUILT_IN_COS): |
6c7cf1f0 UB |
2089 | builtin_optab = cos_optab; break; |
2090 | default: | |
298e6adc | 2091 | gcc_unreachable (); |
6c7cf1f0 | 2092 | } |
6c7cf1f0 UB |
2093 | |
2094 | /* Before working hard, check whether the instruction is available. */ | |
947131ba | 2095 | if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing) |
6c7cf1f0 | 2096 | { |
04b80dbb | 2097 | rtx result = gen_reg_rtx (mode); |
6c7cf1f0 UB |
2098 | |
2099 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may | |
2100 | need to expand the argument again. This way, we will not perform | |
2101 | side-effects more the once. */ | |
5799f732 | 2102 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); |
6c7cf1f0 | 2103 | |
49452c07 | 2104 | op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL); |
6c7cf1f0 | 2105 | |
6c7cf1f0 UB |
2106 | start_sequence (); |
2107 | ||
04b80dbb RS |
2108 | /* Compute into RESULT. |
2109 | Set RESULT to wherever the result comes back. */ | |
6c7cf1f0 UB |
2110 | if (builtin_optab == sincos_optab) |
2111 | { | |
04b80dbb | 2112 | int ok; |
5906d013 | 2113 | |
6c7cf1f0 UB |
2114 | switch (DECL_FUNCTION_CODE (fndecl)) |
2115 | { | |
ea6a6627 | 2116 | CASE_FLT_FN (BUILT_IN_SIN): |
04b80dbb | 2117 | ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0); |
6c7cf1f0 | 2118 | break; |
ea6a6627 | 2119 | CASE_FLT_FN (BUILT_IN_COS): |
04b80dbb | 2120 | ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0); |
6c7cf1f0 UB |
2121 | break; |
2122 | default: | |
298e6adc | 2123 | gcc_unreachable (); |
6c7cf1f0 | 2124 | } |
04b80dbb | 2125 | gcc_assert (ok); |
6c7cf1f0 UB |
2126 | } |
2127 | else | |
04b80dbb | 2128 | result = expand_unop (mode, builtin_optab, op0, result, 0); |
6c7cf1f0 | 2129 | |
04b80dbb | 2130 | if (result != 0) |
6c7cf1f0 | 2131 | { |
6c7cf1f0 UB |
2132 | /* Output the entire sequence. */ |
2133 | insns = get_insns (); | |
2134 | end_sequence (); | |
2135 | emit_insn (insns); | |
04b80dbb | 2136 | return result; |
6c7cf1f0 UB |
2137 | } |
2138 | ||
2139 | /* If we were unable to expand via the builtin, stop the sequence | |
2140 | (without outputting the insns) and call to the library function | |
2141 | with the stabilized argument list. */ | |
2142 | end_sequence (); | |
2143 | } | |
2144 | ||
04b80dbb | 2145 | return expand_call (exp, target, target == const0_rtx); |
6c7cf1f0 UB |
2146 | } |
2147 | ||
44e10129 MM |
2148 | /* Given an interclass math builtin decl FNDECL and it's argument ARG |
2149 | return an RTL instruction code that implements the functionality. | |
2150 | If that isn't possible or available return CODE_FOR_nothing. */ | |
eaee4464 | 2151 | |
44e10129 MM |
2152 | static enum insn_code |
2153 | interclass_mathfn_icode (tree arg, tree fndecl) | |
eaee4464 | 2154 | { |
44e10129 | 2155 | bool errno_set = false; |
2225b9f2 | 2156 | optab builtin_optab = unknown_optab; |
ef4bddc2 | 2157 | machine_mode mode; |
eaee4464 UB |
2158 | |
2159 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2160 | { | |
2161 | CASE_FLT_FN (BUILT_IN_ILOGB): | |
2162 | errno_set = true; builtin_optab = ilogb_optab; break; | |
9ed4207f UB |
2163 | CASE_FLT_FN (BUILT_IN_ISINF): |
2164 | builtin_optab = isinf_optab; break; | |
8a91c45b | 2165 | case BUILT_IN_ISNORMAL: |
0c8d3c2b KG |
2166 | case BUILT_IN_ISFINITE: |
2167 | CASE_FLT_FN (BUILT_IN_FINITE): | |
44e10129 MM |
2168 | case BUILT_IN_FINITED32: |
2169 | case BUILT_IN_FINITED64: | |
2170 | case BUILT_IN_FINITED128: | |
2171 | case BUILT_IN_ISINFD32: | |
2172 | case BUILT_IN_ISINFD64: | |
2173 | case BUILT_IN_ISINFD128: | |
0c8d3c2b KG |
2174 | /* These builtins have no optabs (yet). */ |
2175 | break; | |
eaee4464 UB |
2176 | default: |
2177 | gcc_unreachable (); | |
2178 | } | |
2179 | ||
2180 | /* There's no easy way to detect the case we need to set EDOM. */ | |
2181 | if (flag_errno_math && errno_set) | |
44e10129 | 2182 | return CODE_FOR_nothing; |
eaee4464 UB |
2183 | |
2184 | /* Optab mode depends on the mode of the input argument. */ | |
2185 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
2186 | ||
0c8d3c2b | 2187 | if (builtin_optab) |
947131ba | 2188 | return optab_handler (builtin_optab, mode); |
44e10129 MM |
2189 | return CODE_FOR_nothing; |
2190 | } | |
2191 | ||
2192 | /* Expand a call to one of the builtin math functions that operate on | |
2193 | floating point argument and output an integer result (ilogb, isinf, | |
2194 | isnan, etc). | |
2195 | Return 0 if a normal call should be emitted rather than expanding the | |
2196 | function in-line. EXP is the expression that is a call to the builtin | |
4359dc2a | 2197 | function; if convenient, the result should be placed in TARGET. */ |
44e10129 MM |
2198 | |
2199 | static rtx | |
4359dc2a | 2200 | expand_builtin_interclass_mathfn (tree exp, rtx target) |
44e10129 MM |
2201 | { |
2202 | enum insn_code icode = CODE_FOR_nothing; | |
2203 | rtx op0; | |
2204 | tree fndecl = get_callee_fndecl (exp); | |
ef4bddc2 | 2205 | machine_mode mode; |
44e10129 MM |
2206 | tree arg; |
2207 | ||
2208 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) | |
2209 | return NULL_RTX; | |
2210 | ||
2211 | arg = CALL_EXPR_ARG (exp, 0); | |
2212 | icode = interclass_mathfn_icode (arg, fndecl); | |
2213 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
2214 | ||
eaee4464 UB |
2215 | if (icode != CODE_FOR_nothing) |
2216 | { | |
a5c7d693 | 2217 | struct expand_operand ops[1]; |
58f4cf2a | 2218 | rtx_insn *last = get_last_insn (); |
8a0b1aa4 | 2219 | tree orig_arg = arg; |
eaee4464 UB |
2220 | |
2221 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may | |
2222 | need to expand the argument again. This way, we will not perform | |
2223 | side-effects more the once. */ | |
5799f732 | 2224 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); |
eaee4464 | 2225 | |
4359dc2a | 2226 | op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL); |
eaee4464 UB |
2227 | |
2228 | if (mode != GET_MODE (op0)) | |
2229 | op0 = convert_to_mode (mode, op0, 0); | |
2230 | ||
a5c7d693 RS |
2231 | create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp))); |
2232 | if (maybe_legitimize_operands (icode, 0, 1, ops) | |
2233 | && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN)) | |
2234 | return ops[0].value; | |
2235 | ||
8a0b1aa4 MM |
2236 | delete_insns_since (last); |
2237 | CALL_EXPR_ARG (exp, 0) = orig_arg; | |
eaee4464 UB |
2238 | } |
2239 | ||
44e10129 | 2240 | return NULL_RTX; |
eaee4464 UB |
2241 | } |
2242 | ||
403e54f0 | 2243 | /* Expand a call to the builtin sincos math function. |
5039610b | 2244 | Return NULL_RTX if a normal call should be emitted rather than expanding the |
403e54f0 RG |
2245 | function in-line. EXP is the expression that is a call to the builtin |
2246 | function. */ | |
2247 | ||
2248 | static rtx | |
2249 | expand_builtin_sincos (tree exp) | |
2250 | { | |
2251 | rtx op0, op1, op2, target1, target2; | |
ef4bddc2 | 2252 | machine_mode mode; |
403e54f0 RG |
2253 | tree arg, sinp, cosp; |
2254 | int result; | |
db3927fb | 2255 | location_t loc = EXPR_LOCATION (exp); |
ca818bd9 | 2256 | tree alias_type, alias_off; |
403e54f0 | 2257 | |
5039610b SL |
2258 | if (!validate_arglist (exp, REAL_TYPE, |
2259 | POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
2260 | return NULL_RTX; | |
403e54f0 | 2261 | |
5039610b SL |
2262 | arg = CALL_EXPR_ARG (exp, 0); |
2263 | sinp = CALL_EXPR_ARG (exp, 1); | |
2264 | cosp = CALL_EXPR_ARG (exp, 2); | |
403e54f0 RG |
2265 | |
2266 | /* Make a suitable register to place result in. */ | |
2267 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
2268 | ||
2269 | /* Check if sincos insn is available, otherwise emit the call. */ | |
947131ba | 2270 | if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing) |
403e54f0 RG |
2271 | return NULL_RTX; |
2272 | ||
2273 | target1 = gen_reg_rtx (mode); | |
2274 | target2 = gen_reg_rtx (mode); | |
2275 | ||
84217346 | 2276 | op0 = expand_normal (arg); |
ca818bd9 RG |
2277 | alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true); |
2278 | alias_off = build_int_cst (alias_type, 0); | |
2279 | op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg), | |
2280 | sinp, alias_off)); | |
2281 | op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg), | |
2282 | cosp, alias_off)); | |
403e54f0 RG |
2283 | |
2284 | /* Compute into target1 and target2. | |
2285 | Set TARGET to wherever the result comes back. */ | |
2286 | result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0); | |
2287 | gcc_assert (result); | |
2288 | ||
2289 | /* Move target1 and target2 to the memory locations indicated | |
2290 | by op1 and op2. */ | |
2291 | emit_move_insn (op1, target1); | |
2292 | emit_move_insn (op2, target2); | |
2293 | ||
2294 | return const0_rtx; | |
2295 | } | |
2296 | ||
75c7c595 RG |
2297 | /* Expand a call to the internal cexpi builtin to the sincos math function. |
2298 | EXP is the expression that is a call to the builtin function; if convenient, | |
4359dc2a | 2299 | the result should be placed in TARGET. */ |
75c7c595 RG |
2300 | |
2301 | static rtx | |
4359dc2a | 2302 | expand_builtin_cexpi (tree exp, rtx target) |
75c7c595 RG |
2303 | { |
2304 | tree fndecl = get_callee_fndecl (exp); | |
75c7c595 | 2305 | tree arg, type; |
ef4bddc2 | 2306 | machine_mode mode; |
75c7c595 | 2307 | rtx op0, op1, op2; |
db3927fb | 2308 | location_t loc = EXPR_LOCATION (exp); |
75c7c595 | 2309 | |
5039610b SL |
2310 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
2311 | return NULL_RTX; | |
75c7c595 | 2312 | |
5039610b | 2313 | arg = CALL_EXPR_ARG (exp, 0); |
75c7c595 RG |
2314 | type = TREE_TYPE (arg); |
2315 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
2316 | ||
2317 | /* Try expanding via a sincos optab, fall back to emitting a libcall | |
b54c5497 RG |
2318 | to sincos or cexp. We are sure we have sincos or cexp because cexpi |
2319 | is only generated from sincos, cexp or if we have either of them. */ | |
947131ba | 2320 | if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing) |
75c7c595 RG |
2321 | { |
2322 | op1 = gen_reg_rtx (mode); | |
2323 | op2 = gen_reg_rtx (mode); | |
2324 | ||
4359dc2a | 2325 | op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL); |
75c7c595 RG |
2326 | |
2327 | /* Compute into op1 and op2. */ | |
2328 | expand_twoval_unop (sincos_optab, op0, op2, op1, 0); | |
2329 | } | |
d33d9e47 | 2330 | else if (targetm.libc_has_function (function_sincos)) |
75c7c595 | 2331 | { |
5039610b | 2332 | tree call, fn = NULL_TREE; |
75c7c595 RG |
2333 | tree top1, top2; |
2334 | rtx op1a, op2a; | |
2335 | ||
2336 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF) | |
e79983f4 | 2337 | fn = builtin_decl_explicit (BUILT_IN_SINCOSF); |
75c7c595 | 2338 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI) |
e79983f4 | 2339 | fn = builtin_decl_explicit (BUILT_IN_SINCOS); |
75c7c595 | 2340 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL) |
e79983f4 | 2341 | fn = builtin_decl_explicit (BUILT_IN_SINCOSL); |
5039610b SL |
2342 | else |
2343 | gcc_unreachable (); | |
b8698a0f | 2344 | |
9474e8ab MM |
2345 | op1 = assign_temp (TREE_TYPE (arg), 1, 1); |
2346 | op2 = assign_temp (TREE_TYPE (arg), 1, 1); | |
18ae1560 UB |
2347 | op1a = copy_addr_to_reg (XEXP (op1, 0)); |
2348 | op2a = copy_addr_to_reg (XEXP (op2, 0)); | |
75c7c595 RG |
2349 | top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a); |
2350 | top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a); | |
2351 | ||
75c7c595 RG |
2352 | /* Make sure not to fold the sincos call again. */ |
2353 | call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn); | |
5039610b SL |
2354 | expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)), |
2355 | call, 3, arg, top1, top2)); | |
75c7c595 | 2356 | } |
b54c5497 RG |
2357 | else |
2358 | { | |
9d972b2d | 2359 | tree call, fn = NULL_TREE, narg; |
b54c5497 RG |
2360 | tree ctype = build_complex_type (type); |
2361 | ||
9d972b2d | 2362 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF) |
e79983f4 | 2363 | fn = builtin_decl_explicit (BUILT_IN_CEXPF); |
9d972b2d | 2364 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI) |
e79983f4 | 2365 | fn = builtin_decl_explicit (BUILT_IN_CEXP); |
9d972b2d | 2366 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL) |
e79983f4 | 2367 | fn = builtin_decl_explicit (BUILT_IN_CEXPL); |
5039610b SL |
2368 | else |
2369 | gcc_unreachable (); | |
34a24c11 RG |
2370 | |
2371 | /* If we don't have a decl for cexp create one. This is the | |
2372 | friendliest fallback if the user calls __builtin_cexpi | |
2373 | without full target C99 function support. */ | |
2374 | if (fn == NULL_TREE) | |
2375 | { | |
2376 | tree fntype; | |
2377 | const char *name = NULL; | |
2378 | ||
2379 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF) | |
2380 | name = "cexpf"; | |
2381 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI) | |
2382 | name = "cexp"; | |
2383 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL) | |
2384 | name = "cexpl"; | |
2385 | ||
2386 | fntype = build_function_type_list (ctype, ctype, NULL_TREE); | |
2387 | fn = build_fn_decl (name, fntype); | |
2388 | } | |
2389 | ||
db3927fb | 2390 | narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype, |
b54c5497 RG |
2391 | build_real (type, dconst0), arg); |
2392 | ||
2393 | /* Make sure not to fold the cexp call again. */ | |
2394 | call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn); | |
b8698a0f | 2395 | return expand_expr (build_call_nary (ctype, call, 1, narg), |
49452c07 | 2396 | target, VOIDmode, EXPAND_NORMAL); |
b54c5497 | 2397 | } |
75c7c595 RG |
2398 | |
2399 | /* Now build the proper return type. */ | |
2400 | return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type), | |
2401 | make_tree (TREE_TYPE (arg), op2), | |
2402 | make_tree (TREE_TYPE (arg), op1)), | |
49452c07 | 2403 | target, VOIDmode, EXPAND_NORMAL); |
75c7c595 RG |
2404 | } |
2405 | ||
44e10129 MM |
2406 | /* Conveniently construct a function call expression. FNDECL names the |
2407 | function to be called, N is the number of arguments, and the "..." | |
2408 | parameters are the argument expressions. Unlike build_call_exr | |
2409 | this doesn't fold the call, hence it will always return a CALL_EXPR. */ | |
2410 | ||
2411 | static tree | |
2412 | build_call_nofold_loc (location_t loc, tree fndecl, int n, ...) | |
2413 | { | |
2414 | va_list ap; | |
2415 | tree fntype = TREE_TYPE (fndecl); | |
2416 | tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl); | |
2417 | ||
2418 | va_start (ap, n); | |
2419 | fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap); | |
2420 | va_end (ap); | |
2421 | SET_EXPR_LOCATION (fn, loc); | |
2422 | return fn; | |
2423 | } | |
44e10129 | 2424 | |
0bfa1541 RG |
2425 | /* Expand a call to one of the builtin rounding functions gcc defines |
2426 | as an extension (lfloor and lceil). As these are gcc extensions we | |
2427 | do not need to worry about setting errno to EDOM. | |
d8b42d06 UB |
2428 | If expanding via optab fails, lower expression to (int)(floor(x)). |
2429 | EXP is the expression that is a call to the builtin function; | |
1856c8dc | 2430 | if convenient, the result should be placed in TARGET. */ |
d8b42d06 UB |
2431 | |
2432 | static rtx | |
1856c8dc | 2433 | expand_builtin_int_roundingfn (tree exp, rtx target) |
d8b42d06 | 2434 | { |
c3a4177f | 2435 | convert_optab builtin_optab; |
58f4cf2a DM |
2436 | rtx op0, tmp; |
2437 | rtx_insn *insns; | |
d8b42d06 | 2438 | tree fndecl = get_callee_fndecl (exp); |
d8b42d06 UB |
2439 | enum built_in_function fallback_fn; |
2440 | tree fallback_fndecl; | |
ef4bddc2 | 2441 | machine_mode mode; |
968fc3b6 | 2442 | tree arg; |
d8b42d06 | 2443 | |
5039610b | 2444 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
d8b42d06 UB |
2445 | gcc_unreachable (); |
2446 | ||
5039610b | 2447 | arg = CALL_EXPR_ARG (exp, 0); |
d8b42d06 UB |
2448 | |
2449 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2450 | { | |
6c32ee74 | 2451 | CASE_FLT_FN (BUILT_IN_ICEIL): |
ea6a6627 VR |
2452 | CASE_FLT_FN (BUILT_IN_LCEIL): |
2453 | CASE_FLT_FN (BUILT_IN_LLCEIL): | |
f94b1661 UB |
2454 | builtin_optab = lceil_optab; |
2455 | fallback_fn = BUILT_IN_CEIL; | |
2456 | break; | |
2457 | ||
6c32ee74 | 2458 | CASE_FLT_FN (BUILT_IN_IFLOOR): |
ea6a6627 VR |
2459 | CASE_FLT_FN (BUILT_IN_LFLOOR): |
2460 | CASE_FLT_FN (BUILT_IN_LLFLOOR): | |
d8b42d06 UB |
2461 | builtin_optab = lfloor_optab; |
2462 | fallback_fn = BUILT_IN_FLOOR; | |
2463 | break; | |
2464 | ||
2465 | default: | |
2466 | gcc_unreachable (); | |
2467 | } | |
2468 | ||
2469 | /* Make a suitable register to place result in. */ | |
2470 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
2471 | ||
c3a4177f | 2472 | target = gen_reg_rtx (mode); |
d8b42d06 | 2473 | |
c3a4177f RG |
2474 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may |
2475 | need to expand the argument again. This way, we will not perform | |
2476 | side-effects more the once. */ | |
5799f732 | 2477 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); |
d8b42d06 | 2478 | |
1856c8dc | 2479 | op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL); |
d8b42d06 | 2480 | |
c3a4177f | 2481 | start_sequence (); |
d8b42d06 | 2482 | |
c3a4177f RG |
2483 | /* Compute into TARGET. */ |
2484 | if (expand_sfix_optab (target, op0, builtin_optab)) | |
2485 | { | |
2486 | /* Output the entire sequence. */ | |
2487 | insns = get_insns (); | |
d8b42d06 | 2488 | end_sequence (); |
c3a4177f RG |
2489 | emit_insn (insns); |
2490 | return target; | |
d8b42d06 UB |
2491 | } |
2492 | ||
c3a4177f RG |
2493 | /* If we were unable to expand via the builtin, stop the sequence |
2494 | (without outputting the insns). */ | |
2495 | end_sequence (); | |
2496 | ||
d8b42d06 UB |
2497 | /* Fall back to floating point rounding optab. */ |
2498 | fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn); | |
34a24c11 RG |
2499 | |
2500 | /* For non-C99 targets we may end up without a fallback fndecl here | |
2501 | if the user called __builtin_lfloor directly. In this case emit | |
2502 | a call to the floor/ceil variants nevertheless. This should result | |
2503 | in the best user experience for not full C99 targets. */ | |
2504 | if (fallback_fndecl == NULL_TREE) | |
2505 | { | |
2506 | tree fntype; | |
2507 | const char *name = NULL; | |
2508 | ||
2509 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2510 | { | |
6c32ee74 | 2511 | case BUILT_IN_ICEIL: |
34a24c11 RG |
2512 | case BUILT_IN_LCEIL: |
2513 | case BUILT_IN_LLCEIL: | |
2514 | name = "ceil"; | |
2515 | break; | |
6c32ee74 | 2516 | case BUILT_IN_ICEILF: |
34a24c11 RG |
2517 | case BUILT_IN_LCEILF: |
2518 | case BUILT_IN_LLCEILF: | |
2519 | name = "ceilf"; | |
2520 | break; | |
6c32ee74 | 2521 | case BUILT_IN_ICEILL: |
34a24c11 RG |
2522 | case BUILT_IN_LCEILL: |
2523 | case BUILT_IN_LLCEILL: | |
2524 | name = "ceill"; | |
2525 | break; | |
6c32ee74 | 2526 | case BUILT_IN_IFLOOR: |
34a24c11 RG |
2527 | case BUILT_IN_LFLOOR: |
2528 | case BUILT_IN_LLFLOOR: | |
2529 | name = "floor"; | |
2530 | break; | |
6c32ee74 | 2531 | case BUILT_IN_IFLOORF: |
34a24c11 RG |
2532 | case BUILT_IN_LFLOORF: |
2533 | case BUILT_IN_LLFLOORF: | |
2534 | name = "floorf"; | |
2535 | break; | |
6c32ee74 | 2536 | case BUILT_IN_IFLOORL: |
34a24c11 RG |
2537 | case BUILT_IN_LFLOORL: |
2538 | case BUILT_IN_LLFLOORL: | |
2539 | name = "floorl"; | |
2540 | break; | |
2541 | default: | |
2542 | gcc_unreachable (); | |
2543 | } | |
2544 | ||
2545 | fntype = build_function_type_list (TREE_TYPE (arg), | |
2546 | TREE_TYPE (arg), NULL_TREE); | |
2547 | fallback_fndecl = build_fn_decl (name, fntype); | |
2548 | } | |
2549 | ||
aa493694 | 2550 | exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg); |
d8b42d06 | 2551 | |
39b1ec97 | 2552 | tmp = expand_normal (exp); |
9a002da8 | 2553 | tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp)); |
d8b42d06 UB |
2554 | |
2555 | /* Truncate the result of floating point optab to integer | |
2556 | via expand_fix (). */ | |
2557 | target = gen_reg_rtx (mode); | |
2558 | expand_fix (target, tmp, 0); | |
2559 | ||
2560 | return target; | |
2561 | } | |
2562 | ||
0bfa1541 RG |
2563 | /* Expand a call to one of the builtin math functions doing integer |
2564 | conversion (lrint). | |
2565 | Return 0 if a normal call should be emitted rather than expanding the | |
2566 | function in-line. EXP is the expression that is a call to the builtin | |
1856c8dc | 2567 | function; if convenient, the result should be placed in TARGET. */ |
0bfa1541 RG |
2568 | |
2569 | static rtx | |
1856c8dc | 2570 | expand_builtin_int_roundingfn_2 (tree exp, rtx target) |
0bfa1541 | 2571 | { |
bb7f0423 | 2572 | convert_optab builtin_optab; |
58f4cf2a DM |
2573 | rtx op0; |
2574 | rtx_insn *insns; | |
0bfa1541 | 2575 | tree fndecl = get_callee_fndecl (exp); |
968fc3b6 | 2576 | tree arg; |
ef4bddc2 | 2577 | machine_mode mode; |
ff63ac4d | 2578 | enum built_in_function fallback_fn = BUILT_IN_NONE; |
0bfa1541 | 2579 | |
5039610b SL |
2580 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
2581 | gcc_unreachable (); | |
b8698a0f | 2582 | |
5039610b | 2583 | arg = CALL_EXPR_ARG (exp, 0); |
0bfa1541 RG |
2584 | |
2585 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2586 | { | |
6c32ee74 | 2587 | CASE_FLT_FN (BUILT_IN_IRINT): |
ff63ac4d JJ |
2588 | fallback_fn = BUILT_IN_LRINT; |
2589 | /* FALLTHRU */ | |
0bfa1541 RG |
2590 | CASE_FLT_FN (BUILT_IN_LRINT): |
2591 | CASE_FLT_FN (BUILT_IN_LLRINT): | |
ff63ac4d JJ |
2592 | builtin_optab = lrint_optab; |
2593 | break; | |
6c32ee74 UB |
2594 | |
2595 | CASE_FLT_FN (BUILT_IN_IROUND): | |
ff63ac4d JJ |
2596 | fallback_fn = BUILT_IN_LROUND; |
2597 | /* FALLTHRU */ | |
4d81bf84 RG |
2598 | CASE_FLT_FN (BUILT_IN_LROUND): |
2599 | CASE_FLT_FN (BUILT_IN_LLROUND): | |
ff63ac4d JJ |
2600 | builtin_optab = lround_optab; |
2601 | break; | |
6c32ee74 | 2602 | |
0bfa1541 RG |
2603 | default: |
2604 | gcc_unreachable (); | |
2605 | } | |
2606 | ||
ff63ac4d JJ |
2607 | /* There's no easy way to detect the case we need to set EDOM. */ |
2608 | if (flag_errno_math && fallback_fn == BUILT_IN_NONE) | |
2609 | return NULL_RTX; | |
2610 | ||
0bfa1541 RG |
2611 | /* Make a suitable register to place result in. */ |
2612 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
2613 | ||
ff63ac4d JJ |
2614 | /* There's no easy way to detect the case we need to set EDOM. */ |
2615 | if (!flag_errno_math) | |
2616 | { | |
04b80dbb | 2617 | rtx result = gen_reg_rtx (mode); |
0bfa1541 | 2618 | |
ff63ac4d JJ |
2619 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may |
2620 | need to expand the argument again. This way, we will not perform | |
2621 | side-effects more the once. */ | |
2622 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); | |
0bfa1541 | 2623 | |
ff63ac4d | 2624 | op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL); |
0bfa1541 | 2625 | |
ff63ac4d | 2626 | start_sequence (); |
0bfa1541 | 2627 | |
04b80dbb | 2628 | if (expand_sfix_optab (result, op0, builtin_optab)) |
ff63ac4d JJ |
2629 | { |
2630 | /* Output the entire sequence. */ | |
2631 | insns = get_insns (); | |
2632 | end_sequence (); | |
2633 | emit_insn (insns); | |
04b80dbb | 2634 | return result; |
ff63ac4d JJ |
2635 | } |
2636 | ||
2637 | /* If we were unable to expand via the builtin, stop the sequence | |
2638 | (without outputting the insns) and call to the library function | |
2639 | with the stabilized argument list. */ | |
0bfa1541 RG |
2640 | end_sequence (); |
2641 | } | |
2642 | ||
ff63ac4d JJ |
2643 | if (fallback_fn != BUILT_IN_NONE) |
2644 | { | |
2645 | /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99 | |
2646 | targets, (int) round (x) should never be transformed into | |
2647 | BUILT_IN_IROUND and if __builtin_iround is called directly, emit | |
2648 | a call to lround in the hope that the target provides at least some | |
2649 | C99 functions. This should result in the best user experience for | |
2650 | not full C99 targets. */ | |
b03ff92e RS |
2651 | tree fallback_fndecl = mathfn_built_in_1 |
2652 | (TREE_TYPE (arg), as_combined_fn (fallback_fn), 0); | |
ff63ac4d JJ |
2653 | |
2654 | exp = build_call_nofold_loc (EXPR_LOCATION (exp), | |
2655 | fallback_fndecl, 1, arg); | |
2656 | ||
2657 | target = expand_call (exp, NULL_RTX, target == const0_rtx); | |
9a002da8 | 2658 | target = maybe_emit_group_store (target, TREE_TYPE (exp)); |
ff63ac4d JJ |
2659 | return convert_to_mode (mode, target, 0); |
2660 | } | |
bb7f0423 | 2661 | |
04b80dbb | 2662 | return expand_call (exp, target, target == const0_rtx); |
0bfa1541 RG |
2663 | } |
2664 | ||
5039610b | 2665 | /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if |
17684d46 RG |
2666 | a normal call should be emitted rather than expanding the function |
2667 | in-line. EXP is the expression that is a call to the builtin | |
2668 | function; if convenient, the result should be placed in TARGET. */ | |
2669 | ||
2670 | static rtx | |
4359dc2a | 2671 | expand_builtin_powi (tree exp, rtx target) |
17684d46 | 2672 | { |
17684d46 RG |
2673 | tree arg0, arg1; |
2674 | rtx op0, op1; | |
ef4bddc2 RS |
2675 | machine_mode mode; |
2676 | machine_mode mode2; | |
17684d46 | 2677 | |
5039610b SL |
2678 | if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE)) |
2679 | return NULL_RTX; | |
17684d46 | 2680 | |
5039610b SL |
2681 | arg0 = CALL_EXPR_ARG (exp, 0); |
2682 | arg1 = CALL_EXPR_ARG (exp, 1); | |
17684d46 RG |
2683 | mode = TYPE_MODE (TREE_TYPE (exp)); |
2684 | ||
17684d46 RG |
2685 | /* Emit a libcall to libgcc. */ |
2686 | ||
5039610b | 2687 | /* Mode of the 2nd argument must match that of an int. */ |
0b8495ae FJ |
2688 | mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0); |
2689 | ||
17684d46 RG |
2690 | if (target == NULL_RTX) |
2691 | target = gen_reg_rtx (mode); | |
2692 | ||
4359dc2a | 2693 | op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL); |
17684d46 RG |
2694 | if (GET_MODE (op0) != mode) |
2695 | op0 = convert_to_mode (mode, op0, 0); | |
49452c07 | 2696 | op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL); |
0b8495ae FJ |
2697 | if (GET_MODE (op1) != mode2) |
2698 | op1 = convert_to_mode (mode2, op1, 0); | |
17684d46 | 2699 | |
8a33f100 | 2700 | target = emit_library_call_value (optab_libfunc (powi_optab, mode), |
84b8030f | 2701 | target, LCT_CONST, mode, 2, |
0b8495ae | 2702 | op0, mode, op1, mode2); |
17684d46 RG |
2703 | |
2704 | return target; | |
2705 | } | |
2706 | ||
b8698a0f | 2707 | /* Expand expression EXP which is a call to the strlen builtin. Return |
5039610b | 2708 | NULL_RTX if we failed the caller should emit a normal call, otherwise |
0e9295cf | 2709 | try to get the result in TARGET, if convenient. */ |
3bdf5ad1 | 2710 | |
28f4ec01 | 2711 | static rtx |
5039610b | 2712 | expand_builtin_strlen (tree exp, rtx target, |
ef4bddc2 | 2713 | machine_mode target_mode) |
28f4ec01 | 2714 | { |
5039610b SL |
2715 | if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
2716 | return NULL_RTX; | |
28f4ec01 BS |
2717 | else |
2718 | { | |
a5c7d693 | 2719 | struct expand_operand ops[4]; |
dd05e4fa | 2720 | rtx pat; |
5039610b SL |
2721 | tree len; |
2722 | tree src = CALL_EXPR_ARG (exp, 0); | |
58f4cf2a DM |
2723 | rtx src_reg; |
2724 | rtx_insn *before_strlen; | |
ef4bddc2 | 2725 | machine_mode insn_mode = target_mode; |
a544cfd2 | 2726 | enum insn_code icode = CODE_FOR_nothing; |
1be38ccb | 2727 | unsigned int align; |
712b7a05 RS |
2728 | |
2729 | /* If the length can be computed at compile-time, return it. */ | |
ae808627 | 2730 | len = c_strlen (src, 0); |
712b7a05 | 2731 | if (len) |
8c9b38d7 | 2732 | return expand_expr (len, target, target_mode, EXPAND_NORMAL); |
712b7a05 | 2733 | |
ae808627 JJ |
2734 | /* If the length can be computed at compile-time and is constant |
2735 | integer, but there are side-effects in src, evaluate | |
2736 | src for side-effects, then return len. | |
2737 | E.g. x = strlen (i++ ? "xfoo" + 1 : "bar"); | |
2738 | can be optimized into: i++; x = 3; */ | |
2739 | len = c_strlen (src, 1); | |
2740 | if (len && TREE_CODE (len) == INTEGER_CST) | |
2741 | { | |
2742 | expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
2743 | return expand_expr (len, target, target_mode, EXPAND_NORMAL); | |
2744 | } | |
2745 | ||
0eb77834 | 2746 | align = get_pointer_alignment (src) / BITS_PER_UNIT; |
28f4ec01 | 2747 | |
28f4ec01 BS |
2748 | /* If SRC is not a pointer type, don't do this operation inline. */ |
2749 | if (align == 0) | |
5039610b | 2750 | return NULL_RTX; |
28f4ec01 | 2751 | |
dd05e4fa | 2752 | /* Bail out if we can't compute strlen in the right mode. */ |
28f4ec01 BS |
2753 | while (insn_mode != VOIDmode) |
2754 | { | |
947131ba | 2755 | icode = optab_handler (strlen_optab, insn_mode); |
28f4ec01 | 2756 | if (icode != CODE_FOR_nothing) |
54e43c67 | 2757 | break; |
28f4ec01 BS |
2758 | |
2759 | insn_mode = GET_MODE_WIDER_MODE (insn_mode); | |
2760 | } | |
2761 | if (insn_mode == VOIDmode) | |
5039610b | 2762 | return NULL_RTX; |
28f4ec01 | 2763 | |
dd05e4fa RH |
2764 | /* Make a place to hold the source address. We will not expand |
2765 | the actual source until we are sure that the expansion will | |
2766 | not fail -- there are trees that cannot be expanded twice. */ | |
2767 | src_reg = gen_reg_rtx (Pmode); | |
28f4ec01 | 2768 | |
dd05e4fa RH |
2769 | /* Mark the beginning of the strlen sequence so we can emit the |
2770 | source operand later. */ | |
5ab2f7b7 | 2771 | before_strlen = get_last_insn (); |
28f4ec01 | 2772 | |
a5c7d693 RS |
2773 | create_output_operand (&ops[0], target, insn_mode); |
2774 | create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg)); | |
2775 | create_integer_operand (&ops[2], 0); | |
2776 | create_integer_operand (&ops[3], align); | |
2777 | if (!maybe_expand_insn (icode, 4, ops)) | |
5039610b | 2778 | return NULL_RTX; |
dd05e4fa RH |
2779 | |
2780 | /* Now that we are assured of success, expand the source. */ | |
2781 | start_sequence (); | |
fa465762 | 2782 | pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL); |
dd05e4fa | 2783 | if (pat != src_reg) |
fa465762 L |
2784 | { |
2785 | #ifdef POINTERS_EXTEND_UNSIGNED | |
2786 | if (GET_MODE (pat) != Pmode) | |
2787 | pat = convert_to_mode (Pmode, pat, | |
2788 | POINTERS_EXTEND_UNSIGNED); | |
2789 | #endif | |
2790 | emit_move_insn (src_reg, pat); | |
2791 | } | |
2f937369 | 2792 | pat = get_insns (); |
dd05e4fa | 2793 | end_sequence (); |
fca9f642 RH |
2794 | |
2795 | if (before_strlen) | |
2796 | emit_insn_after (pat, before_strlen); | |
2797 | else | |
2798 | emit_insn_before (pat, get_insns ()); | |
28f4ec01 BS |
2799 | |
2800 | /* Return the value in the proper mode for this function. */ | |
a5c7d693 RS |
2801 | if (GET_MODE (ops[0].value) == target_mode) |
2802 | target = ops[0].value; | |
28f4ec01 | 2803 | else if (target != 0) |
a5c7d693 | 2804 | convert_move (target, ops[0].value, 0); |
28f4ec01 | 2805 | else |
a5c7d693 | 2806 | target = convert_to_mode (target_mode, ops[0].value, 0); |
dd05e4fa RH |
2807 | |
2808 | return target; | |
28f4ec01 BS |
2809 | } |
2810 | } | |
2811 | ||
57814e5e JJ |
2812 | /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE) |
2813 | bytes from constant string DATA + OFFSET and return it as target | |
2814 | constant. */ | |
2815 | ||
2816 | static rtx | |
4682ae04 | 2817 | builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset, |
ef4bddc2 | 2818 | machine_mode mode) |
57814e5e JJ |
2819 | { |
2820 | const char *str = (const char *) data; | |
2821 | ||
298e6adc NS |
2822 | gcc_assert (offset >= 0 |
2823 | && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode) | |
2824 | <= strlen (str) + 1)); | |
57814e5e JJ |
2825 | |
2826 | return c_readstr (str + offset, mode); | |
2827 | } | |
2828 | ||
3918b108 | 2829 | /* LEN specify length of the block of memcpy/memset operation. |
82bb7d4e JH |
2830 | Figure out its range and put it into MIN_SIZE/MAX_SIZE. |
2831 | In some cases we can make very likely guess on max size, then we | |
2832 | set it into PROBABLE_MAX_SIZE. */ | |
3918b108 JH |
2833 | |
2834 | static void | |
2835 | determine_block_size (tree len, rtx len_rtx, | |
2836 | unsigned HOST_WIDE_INT *min_size, | |
82bb7d4e JH |
2837 | unsigned HOST_WIDE_INT *max_size, |
2838 | unsigned HOST_WIDE_INT *probable_max_size) | |
3918b108 JH |
2839 | { |
2840 | if (CONST_INT_P (len_rtx)) | |
2841 | { | |
2738b4c7 | 2842 | *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx); |
3918b108 JH |
2843 | return; |
2844 | } | |
2845 | else | |
2846 | { | |
807e902e | 2847 | wide_int min, max; |
82bb7d4e JH |
2848 | enum value_range_type range_type = VR_UNDEFINED; |
2849 | ||
2850 | /* Determine bounds from the type. */ | |
2851 | if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len)))) | |
2852 | *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len))); | |
2853 | else | |
2854 | *min_size = 0; | |
2855 | if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len)))) | |
2738b4c7 JJ |
2856 | *probable_max_size = *max_size |
2857 | = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len))); | |
82bb7d4e JH |
2858 | else |
2859 | *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx)); | |
2860 | ||
2861 | if (TREE_CODE (len) == SSA_NAME) | |
2862 | range_type = get_range_info (len, &min, &max); | |
2863 | if (range_type == VR_RANGE) | |
3918b108 | 2864 | { |
807e902e | 2865 | if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ()) |
3918b108 | 2866 | *min_size = min.to_uhwi (); |
807e902e | 2867 | if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ()) |
82bb7d4e | 2868 | *probable_max_size = *max_size = max.to_uhwi (); |
3918b108 | 2869 | } |
82bb7d4e | 2870 | else if (range_type == VR_ANTI_RANGE) |
3918b108 | 2871 | { |
70ec86ee | 2872 | /* Anti range 0...N lets us to determine minimal size to N+1. */ |
807e902e | 2873 | if (min == 0) |
82bb7d4e | 2874 | { |
807e902e KZ |
2875 | if (wi::fits_uhwi_p (max) && max.to_uhwi () + 1 != 0) |
2876 | *min_size = max.to_uhwi () + 1; | |
82bb7d4e JH |
2877 | } |
2878 | /* Code like | |
2879 | ||
2880 | int n; | |
2881 | if (n < 100) | |
70ec86ee | 2882 | memcpy (a, b, n) |
82bb7d4e JH |
2883 | |
2884 | Produce anti range allowing negative values of N. We still | |
2885 | can use the information and make a guess that N is not negative. | |
2886 | */ | |
807e902e KZ |
2887 | else if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min)) |
2888 | *probable_max_size = min.to_uhwi () - 1; | |
3918b108 JH |
2889 | } |
2890 | } | |
2891 | gcc_checking_assert (*max_size <= | |
2892 | (unsigned HOST_WIDE_INT) | |
2893 | GET_MODE_MASK (GET_MODE (len_rtx))); | |
2894 | } | |
2895 | ||
edcf72f3 IE |
2896 | /* Helper function to do the actual work for expand_builtin_memcpy. */ |
2897 | ||
2898 | static rtx | |
2899 | expand_builtin_memcpy_args (tree dest, tree src, tree len, rtx target, tree exp) | |
2900 | { | |
2901 | const char *src_str; | |
2902 | unsigned int src_align = get_pointer_alignment (src); | |
2903 | unsigned int dest_align = get_pointer_alignment (dest); | |
2904 | rtx dest_mem, src_mem, dest_addr, len_rtx; | |
2905 | HOST_WIDE_INT expected_size = -1; | |
2906 | unsigned int expected_align = 0; | |
2907 | unsigned HOST_WIDE_INT min_size; | |
2908 | unsigned HOST_WIDE_INT max_size; | |
2909 | unsigned HOST_WIDE_INT probable_max_size; | |
2910 | ||
2911 | /* If DEST is not a pointer type, call the normal function. */ | |
2912 | if (dest_align == 0) | |
2913 | return NULL_RTX; | |
2914 | ||
2915 | /* If either SRC is not a pointer type, don't do this | |
2916 | operation in-line. */ | |
2917 | if (src_align == 0) | |
2918 | return NULL_RTX; | |
2919 | ||
2920 | if (currently_expanding_gimple_stmt) | |
2921 | stringop_block_profile (currently_expanding_gimple_stmt, | |
2922 | &expected_align, &expected_size); | |
2923 | ||
2924 | if (expected_align < dest_align) | |
2925 | expected_align = dest_align; | |
2926 | dest_mem = get_memory_rtx (dest, len); | |
2927 | set_mem_align (dest_mem, dest_align); | |
2928 | len_rtx = expand_normal (len); | |
2929 | determine_block_size (len, len_rtx, &min_size, &max_size, | |
2930 | &probable_max_size); | |
2931 | src_str = c_getstr (src); | |
2932 | ||
2933 | /* If SRC is a string constant and block move would be done | |
2934 | by pieces, we can avoid loading the string from memory | |
2935 | and only stored the computed constants. */ | |
2936 | if (src_str | |
2937 | && CONST_INT_P (len_rtx) | |
2938 | && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1 | |
2939 | && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str, | |
2940 | CONST_CAST (char *, src_str), | |
2941 | dest_align, false)) | |
2942 | { | |
2943 | dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx), | |
2944 | builtin_memcpy_read_str, | |
2945 | CONST_CAST (char *, src_str), | |
2946 | dest_align, false, 0); | |
2947 | dest_mem = force_operand (XEXP (dest_mem, 0), target); | |
2948 | dest_mem = convert_memory_address (ptr_mode, dest_mem); | |
2949 | return dest_mem; | |
2950 | } | |
2951 | ||
2952 | src_mem = get_memory_rtx (src, len); | |
2953 | set_mem_align (src_mem, src_align); | |
2954 | ||
2955 | /* Copy word part most expediently. */ | |
2956 | dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx, | |
2957 | CALL_EXPR_TAILCALL (exp) | |
2958 | ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL, | |
2959 | expected_align, expected_size, | |
2960 | min_size, max_size, probable_max_size); | |
2961 | ||
2962 | if (dest_addr == 0) | |
2963 | { | |
2964 | dest_addr = force_operand (XEXP (dest_mem, 0), target); | |
2965 | dest_addr = convert_memory_address (ptr_mode, dest_addr); | |
2966 | } | |
2967 | ||
2968 | return dest_addr; | |
2969 | } | |
2970 | ||
5039610b SL |
2971 | /* Expand a call EXP to the memcpy builtin. |
2972 | Return NULL_RTX if we failed, the caller should emit a normal call, | |
9cb65f92 | 2973 | otherwise try to get the result in TARGET, if convenient (and in |
8fd3cf4e | 2974 | mode MODE if that's convenient). */ |
5039610b | 2975 | |
28f4ec01 | 2976 | static rtx |
44e10129 | 2977 | expand_builtin_memcpy (tree exp, rtx target) |
28f4ec01 | 2978 | { |
5039610b SL |
2979 | if (!validate_arglist (exp, |
2980 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
2981 | return NULL_RTX; | |
28f4ec01 BS |
2982 | else |
2983 | { | |
5039610b SL |
2984 | tree dest = CALL_EXPR_ARG (exp, 0); |
2985 | tree src = CALL_EXPR_ARG (exp, 1); | |
2986 | tree len = CALL_EXPR_ARG (exp, 2); | |
edcf72f3 IE |
2987 | return expand_builtin_memcpy_args (dest, src, len, target, exp); |
2988 | } | |
2989 | } | |
57814e5e | 2990 | |
edcf72f3 IE |
2991 | /* Expand an instrumented call EXP to the memcpy builtin. |
2992 | Return NULL_RTX if we failed, the caller should emit a normal call, | |
2993 | otherwise try to get the result in TARGET, if convenient (and in | |
2994 | mode MODE if that's convenient). */ | |
28f4ec01 | 2995 | |
edcf72f3 IE |
2996 | static rtx |
2997 | expand_builtin_memcpy_with_bounds (tree exp, rtx target) | |
2998 | { | |
2999 | if (!validate_arglist (exp, | |
3000 | POINTER_TYPE, POINTER_BOUNDS_TYPE, | |
3001 | POINTER_TYPE, POINTER_BOUNDS_TYPE, | |
3002 | INTEGER_TYPE, VOID_TYPE)) | |
3003 | return NULL_RTX; | |
3004 | else | |
3005 | { | |
3006 | tree dest = CALL_EXPR_ARG (exp, 0); | |
3007 | tree src = CALL_EXPR_ARG (exp, 2); | |
3008 | tree len = CALL_EXPR_ARG (exp, 4); | |
3009 | rtx res = expand_builtin_memcpy_args (dest, src, len, target, exp); | |
28f4ec01 | 3010 | |
edcf72f3 IE |
3011 | /* Return src bounds with the result. */ |
3012 | if (res) | |
aa0f70e6 | 3013 | { |
30975f63 | 3014 | rtx bnd = force_reg (targetm.chkp_bound_mode (), |
edcf72f3 IE |
3015 | expand_normal (CALL_EXPR_ARG (exp, 1))); |
3016 | res = chkp_join_splitted_slot (res, bnd); | |
aa0f70e6 | 3017 | } |
edcf72f3 | 3018 | return res; |
28f4ec01 BS |
3019 | } |
3020 | } | |
3021 | ||
5039610b SL |
3022 | /* Expand a call EXP to the mempcpy builtin. |
3023 | Return NULL_RTX if we failed; the caller should emit a normal call, | |
e3e9f108 | 3024 | otherwise try to get the result in TARGET, if convenient (and in |
8fd3cf4e JJ |
3025 | mode MODE if that's convenient). If ENDP is 0 return the |
3026 | destination pointer, if ENDP is 1 return the end pointer ala | |
3027 | mempcpy, and if ENDP is 2 return the end pointer minus one ala | |
3028 | stpcpy. */ | |
e3e9f108 JJ |
3029 | |
3030 | static rtx | |
ef4bddc2 | 3031 | expand_builtin_mempcpy (tree exp, rtx target, machine_mode mode) |
e3e9f108 | 3032 | { |
5039610b SL |
3033 | if (!validate_arglist (exp, |
3034 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3035 | return NULL_RTX; | |
3036 | else | |
3037 | { | |
3038 | tree dest = CALL_EXPR_ARG (exp, 0); | |
3039 | tree src = CALL_EXPR_ARG (exp, 1); | |
3040 | tree len = CALL_EXPR_ARG (exp, 2); | |
3041 | return expand_builtin_mempcpy_args (dest, src, len, | |
edcf72f3 IE |
3042 | target, mode, /*endp=*/ 1, |
3043 | exp); | |
3044 | } | |
3045 | } | |
3046 | ||
3047 | /* Expand an instrumented call EXP to the mempcpy builtin. | |
3048 | Return NULL_RTX if we failed, the caller should emit a normal call, | |
3049 | otherwise try to get the result in TARGET, if convenient (and in | |
3050 | mode MODE if that's convenient). */ | |
3051 | ||
3052 | static rtx | |
3053 | expand_builtin_mempcpy_with_bounds (tree exp, rtx target, machine_mode mode) | |
3054 | { | |
3055 | if (!validate_arglist (exp, | |
3056 | POINTER_TYPE, POINTER_BOUNDS_TYPE, | |
3057 | POINTER_TYPE, POINTER_BOUNDS_TYPE, | |
3058 | INTEGER_TYPE, VOID_TYPE)) | |
3059 | return NULL_RTX; | |
3060 | else | |
3061 | { | |
3062 | tree dest = CALL_EXPR_ARG (exp, 0); | |
3063 | tree src = CALL_EXPR_ARG (exp, 2); | |
3064 | tree len = CALL_EXPR_ARG (exp, 4); | |
3065 | rtx res = expand_builtin_mempcpy_args (dest, src, len, target, | |
3066 | mode, 1, exp); | |
3067 | ||
3068 | /* Return src bounds with the result. */ | |
3069 | if (res) | |
3070 | { | |
30975f63 | 3071 | rtx bnd = force_reg (targetm.chkp_bound_mode (), |
edcf72f3 IE |
3072 | expand_normal (CALL_EXPR_ARG (exp, 1))); |
3073 | res = chkp_join_splitted_slot (res, bnd); | |
3074 | } | |
3075 | return res; | |
5039610b SL |
3076 | } |
3077 | } | |
3078 | ||
3079 | /* Helper function to do the actual work for expand_builtin_mempcpy. The | |
3080 | arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out | |
3081 | so that this can also be called without constructing an actual CALL_EXPR. | |
44e10129 MM |
3082 | The other arguments and return value are the same as for |
3083 | expand_builtin_mempcpy. */ | |
5039610b SL |
3084 | |
3085 | static rtx | |
44e10129 | 3086 | expand_builtin_mempcpy_args (tree dest, tree src, tree len, |
edcf72f3 IE |
3087 | rtx target, machine_mode mode, int endp, |
3088 | tree orig_exp) | |
5039610b | 3089 | { |
edcf72f3 IE |
3090 | tree fndecl = get_callee_fndecl (orig_exp); |
3091 | ||
5039610b | 3092 | /* If return value is ignored, transform mempcpy into memcpy. */ |
edcf72f3 IE |
3093 | if (target == const0_rtx |
3094 | && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP | |
3095 | && builtin_decl_implicit_p (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP)) | |
3096 | { | |
3097 | tree fn = builtin_decl_implicit (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP); | |
3098 | tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3, | |
3099 | dest, src, len); | |
3100 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
3101 | } | |
3102 | else if (target == const0_rtx | |
3103 | && builtin_decl_implicit_p (BUILT_IN_MEMCPY)) | |
8fd3cf4e | 3104 | { |
e79983f4 | 3105 | tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY); |
aa493694 JJ |
3106 | tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3, |
3107 | dest, src, len); | |
0d2a6e08 | 3108 | return expand_expr (result, target, mode, EXPAND_NORMAL); |
8fd3cf4e | 3109 | } |
e3e9f108 JJ |
3110 | else |
3111 | { | |
8fd3cf4e | 3112 | const char *src_str; |
0eb77834 RG |
3113 | unsigned int src_align = get_pointer_alignment (src); |
3114 | unsigned int dest_align = get_pointer_alignment (dest); | |
8fd3cf4e | 3115 | rtx dest_mem, src_mem, len_rtx; |
c22cacf3 | 3116 | |
4d9ef6a9 | 3117 | /* If either SRC or DEST is not a pointer type, don't do this |
c22cacf3 | 3118 | operation in-line. */ |
4d9ef6a9 | 3119 | if (dest_align == 0 || src_align == 0) |
5039610b | 3120 | return NULL_RTX; |
8fd3cf4e | 3121 | |
ea82015c | 3122 | /* If LEN is not constant, call the normal function. */ |
cc269bb6 | 3123 | if (! tree_fits_uhwi_p (len)) |
5039610b | 3124 | return NULL_RTX; |
33521f7d | 3125 | |
84217346 | 3126 | len_rtx = expand_normal (len); |
8fd3cf4e | 3127 | src_str = c_getstr (src); |
e3e9f108 | 3128 | |
8fd3cf4e JJ |
3129 | /* If SRC is a string constant and block move would be done |
3130 | by pieces, we can avoid loading the string from memory | |
3131 | and only stored the computed constants. */ | |
3132 | if (src_str | |
481683e1 | 3133 | && CONST_INT_P (len_rtx) |
8fd3cf4e JJ |
3134 | && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1 |
3135 | && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str, | |
f883e0a7 KG |
3136 | CONST_CAST (char *, src_str), |
3137 | dest_align, false)) | |
8fd3cf4e | 3138 | { |
435bb2a1 | 3139 | dest_mem = get_memory_rtx (dest, len); |
8fd3cf4e JJ |
3140 | set_mem_align (dest_mem, dest_align); |
3141 | dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx), | |
3142 | builtin_memcpy_read_str, | |
f883e0a7 KG |
3143 | CONST_CAST (char *, src_str), |
3144 | dest_align, false, endp); | |
8fd3cf4e | 3145 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
5ae6cd0d | 3146 | dest_mem = convert_memory_address (ptr_mode, dest_mem); |
8fd3cf4e | 3147 | return dest_mem; |
e3e9f108 JJ |
3148 | } |
3149 | ||
481683e1 | 3150 | if (CONST_INT_P (len_rtx) |
8fd3cf4e JJ |
3151 | && can_move_by_pieces (INTVAL (len_rtx), |
3152 | MIN (dest_align, src_align))) | |
3153 | { | |
435bb2a1 | 3154 | dest_mem = get_memory_rtx (dest, len); |
8fd3cf4e | 3155 | set_mem_align (dest_mem, dest_align); |
435bb2a1 | 3156 | src_mem = get_memory_rtx (src, len); |
8fd3cf4e JJ |
3157 | set_mem_align (src_mem, src_align); |
3158 | dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx), | |
3159 | MIN (dest_align, src_align), endp); | |
3160 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); | |
5ae6cd0d | 3161 | dest_mem = convert_memory_address (ptr_mode, dest_mem); |
8fd3cf4e JJ |
3162 | return dest_mem; |
3163 | } | |
3164 | ||
5039610b | 3165 | return NULL_RTX; |
e3e9f108 JJ |
3166 | } |
3167 | } | |
3168 | ||
5039610b | 3169 | /* Expand into a movstr instruction, if one is available. Return NULL_RTX if |
beed8fc0 AO |
3170 | we failed, the caller should emit a normal call, otherwise try to |
3171 | get the result in TARGET, if convenient. If ENDP is 0 return the | |
3172 | destination pointer, if ENDP is 1 return the end pointer ala | |
3173 | mempcpy, and if ENDP is 2 return the end pointer minus one ala | |
3174 | stpcpy. */ | |
3175 | ||
3176 | static rtx | |
3177 | expand_movstr (tree dest, tree src, rtx target, int endp) | |
3178 | { | |
a5c7d693 | 3179 | struct expand_operand ops[3]; |
beed8fc0 AO |
3180 | rtx dest_mem; |
3181 | rtx src_mem; | |
beed8fc0 | 3182 | |
7cff0471 | 3183 | if (!targetm.have_movstr ()) |
5039610b | 3184 | return NULL_RTX; |
beed8fc0 | 3185 | |
435bb2a1 JJ |
3186 | dest_mem = get_memory_rtx (dest, NULL); |
3187 | src_mem = get_memory_rtx (src, NULL); | |
beed8fc0 AO |
3188 | if (!endp) |
3189 | { | |
3190 | target = force_reg (Pmode, XEXP (dest_mem, 0)); | |
3191 | dest_mem = replace_equiv_address (dest_mem, target); | |
beed8fc0 AO |
3192 | } |
3193 | ||
a5c7d693 RS |
3194 | create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode); |
3195 | create_fixed_operand (&ops[1], dest_mem); | |
3196 | create_fixed_operand (&ops[2], src_mem); | |
7cff0471 | 3197 | if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops)) |
7c5425fa | 3198 | return NULL_RTX; |
beed8fc0 | 3199 | |
a5c7d693 | 3200 | if (endp && target != const0_rtx) |
7ce3fc8f | 3201 | { |
a5c7d693 RS |
3202 | target = ops[0].value; |
3203 | /* movstr is supposed to set end to the address of the NUL | |
3204 | terminator. If the caller requested a mempcpy-like return value, | |
3205 | adjust it. */ | |
3206 | if (endp == 1) | |
3207 | { | |
0a81f074 RS |
3208 | rtx tem = plus_constant (GET_MODE (target), |
3209 | gen_lowpart (GET_MODE (target), target), 1); | |
a5c7d693 RS |
3210 | emit_move_insn (target, force_operand (tem, NULL_RTX)); |
3211 | } | |
7ce3fc8f | 3212 | } |
beed8fc0 AO |
3213 | return target; |
3214 | } | |
3215 | ||
b8698a0f L |
3216 | /* Expand expression EXP, which is a call to the strcpy builtin. Return |
3217 | NULL_RTX if we failed the caller should emit a normal call, otherwise | |
5039610b | 3218 | try to get the result in TARGET, if convenient (and in mode MODE if that's |
c2bd38e8 | 3219 | convenient). */ |
fed3cef0 | 3220 | |
28f4ec01 | 3221 | static rtx |
44e10129 | 3222 | expand_builtin_strcpy (tree exp, rtx target) |
28f4ec01 | 3223 | { |
5039610b SL |
3224 | if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
3225 | { | |
3226 | tree dest = CALL_EXPR_ARG (exp, 0); | |
3227 | tree src = CALL_EXPR_ARG (exp, 1); | |
44e10129 | 3228 | return expand_builtin_strcpy_args (dest, src, target); |
5039610b SL |
3229 | } |
3230 | return NULL_RTX; | |
3231 | } | |
3232 | ||
3233 | /* Helper function to do the actual work for expand_builtin_strcpy. The | |
3234 | arguments to the builtin_strcpy call DEST and SRC are broken out | |
3235 | so that this can also be called without constructing an actual CALL_EXPR. | |
3236 | The other arguments and return value are the same as for | |
3237 | expand_builtin_strcpy. */ | |
3238 | ||
3239 | static rtx | |
44e10129 | 3240 | expand_builtin_strcpy_args (tree dest, tree src, rtx target) |
5039610b | 3241 | { |
5039610b | 3242 | return expand_movstr (dest, src, target, /*endp=*/0); |
28f4ec01 BS |
3243 | } |
3244 | ||
5039610b SL |
3245 | /* Expand a call EXP to the stpcpy builtin. |
3246 | Return NULL_RTX if we failed the caller should emit a normal call, | |
9cb65f92 KG |
3247 | otherwise try to get the result in TARGET, if convenient (and in |
3248 | mode MODE if that's convenient). */ | |
3249 | ||
3250 | static rtx | |
ef4bddc2 | 3251 | expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode) |
9cb65f92 | 3252 | { |
5039610b | 3253 | tree dst, src; |
db3927fb | 3254 | location_t loc = EXPR_LOCATION (exp); |
5039610b SL |
3255 | |
3256 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
3257 | return NULL_RTX; | |
3258 | ||
3259 | dst = CALL_EXPR_ARG (exp, 0); | |
3260 | src = CALL_EXPR_ARG (exp, 1); | |
3261 | ||
beed8fc0 | 3262 | /* If return value is ignored, transform stpcpy into strcpy. */ |
e79983f4 | 3263 | if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY)) |
ad4319ec | 3264 | { |
e79983f4 | 3265 | tree fn = builtin_decl_implicit (BUILT_IN_STRCPY); |
aa493694 | 3266 | tree result = build_call_nofold_loc (loc, fn, 2, dst, src); |
0d2a6e08 | 3267 | return expand_expr (result, target, mode, EXPAND_NORMAL); |
ad4319ec | 3268 | } |
9cb65f92 KG |
3269 | else |
3270 | { | |
5039610b | 3271 | tree len, lenp1; |
beed8fc0 | 3272 | rtx ret; |
e3e9f108 | 3273 | |
8fd3cf4e | 3274 | /* Ensure we get an actual string whose length can be evaluated at |
c22cacf3 MS |
3275 | compile-time, not an expression containing a string. This is |
3276 | because the latter will potentially produce pessimized code | |
3277 | when used to produce the return value. */ | |
ae808627 | 3278 | if (! c_getstr (src) || ! (len = c_strlen (src, 0))) |
5039610b | 3279 | return expand_movstr (dst, src, target, /*endp=*/2); |
9cb65f92 | 3280 | |
db3927fb | 3281 | lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1)); |
44e10129 | 3282 | ret = expand_builtin_mempcpy_args (dst, src, lenp1, |
edcf72f3 IE |
3283 | target, mode, /*endp=*/2, |
3284 | exp); | |
beed8fc0 AO |
3285 | |
3286 | if (ret) | |
3287 | return ret; | |
3288 | ||
3289 | if (TREE_CODE (len) == INTEGER_CST) | |
3290 | { | |
84217346 | 3291 | rtx len_rtx = expand_normal (len); |
beed8fc0 | 3292 | |
481683e1 | 3293 | if (CONST_INT_P (len_rtx)) |
beed8fc0 | 3294 | { |
44e10129 | 3295 | ret = expand_builtin_strcpy_args (dst, src, target); |
beed8fc0 AO |
3296 | |
3297 | if (ret) | |
3298 | { | |
3299 | if (! target) | |
58ec6ece SE |
3300 | { |
3301 | if (mode != VOIDmode) | |
3302 | target = gen_reg_rtx (mode); | |
3303 | else | |
3304 | target = gen_reg_rtx (GET_MODE (ret)); | |
3305 | } | |
beed8fc0 AO |
3306 | if (GET_MODE (target) != GET_MODE (ret)) |
3307 | ret = gen_lowpart (GET_MODE (target), ret); | |
3308 | ||
0a81f074 | 3309 | ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx)); |
7ce3fc8f | 3310 | ret = emit_move_insn (target, force_operand (ret, NULL_RTX)); |
298e6adc | 3311 | gcc_assert (ret); |
beed8fc0 AO |
3312 | |
3313 | return target; | |
3314 | } | |
3315 | } | |
3316 | } | |
3317 | ||
5039610b | 3318 | return expand_movstr (dst, src, target, /*endp=*/2); |
9cb65f92 KG |
3319 | } |
3320 | } | |
3321 | ||
57814e5e JJ |
3322 | /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE) |
3323 | bytes from constant string DATA + OFFSET and return it as target | |
3324 | constant. */ | |
3325 | ||
14a43348 | 3326 | rtx |
4682ae04 | 3327 | builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset, |
ef4bddc2 | 3328 | machine_mode mode) |
57814e5e JJ |
3329 | { |
3330 | const char *str = (const char *) data; | |
3331 | ||
3332 | if ((unsigned HOST_WIDE_INT) offset > strlen (str)) | |
3333 | return const0_rtx; | |
3334 | ||
3335 | return c_readstr (str + offset, mode); | |
3336 | } | |
3337 | ||
b8698a0f | 3338 | /* Expand expression EXP, which is a call to the strncpy builtin. Return |
5039610b | 3339 | NULL_RTX if we failed the caller should emit a normal call. */ |
da9e9f08 KG |
3340 | |
3341 | static rtx | |
44e10129 | 3342 | expand_builtin_strncpy (tree exp, rtx target) |
da9e9f08 | 3343 | { |
db3927fb | 3344 | location_t loc = EXPR_LOCATION (exp); |
5039610b SL |
3345 | |
3346 | if (validate_arglist (exp, | |
3347 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
da9e9f08 | 3348 | { |
5039610b SL |
3349 | tree dest = CALL_EXPR_ARG (exp, 0); |
3350 | tree src = CALL_EXPR_ARG (exp, 1); | |
3351 | tree len = CALL_EXPR_ARG (exp, 2); | |
3352 | tree slen = c_strlen (src, 1); | |
57814e5e | 3353 | |
559837f7 | 3354 | /* We must be passed a constant len and src parameter. */ |
cc269bb6 | 3355 | if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen)) |
5039610b | 3356 | return NULL_RTX; |
da9e9f08 | 3357 | |
db3927fb | 3358 | slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1)); |
da9e9f08 KG |
3359 | |
3360 | /* We're required to pad with trailing zeros if the requested | |
c22cacf3 | 3361 | len is greater than strlen(s2)+1. In that case try to |
57814e5e | 3362 | use store_by_pieces, if it fails, punt. */ |
da9e9f08 | 3363 | if (tree_int_cst_lt (slen, len)) |
57814e5e | 3364 | { |
0eb77834 | 3365 | unsigned int dest_align = get_pointer_alignment (dest); |
5039610b | 3366 | const char *p = c_getstr (src); |
57814e5e JJ |
3367 | rtx dest_mem; |
3368 | ||
cc269bb6 | 3369 | if (!p || dest_align == 0 || !tree_fits_uhwi_p (len) |
ae7e9ddd | 3370 | || !can_store_by_pieces (tree_to_uhwi (len), |
57814e5e | 3371 | builtin_strncpy_read_str, |
f883e0a7 KG |
3372 | CONST_CAST (char *, p), |
3373 | dest_align, false)) | |
5039610b | 3374 | return NULL_RTX; |
57814e5e | 3375 | |
435bb2a1 | 3376 | dest_mem = get_memory_rtx (dest, len); |
ae7e9ddd | 3377 | store_by_pieces (dest_mem, tree_to_uhwi (len), |
57814e5e | 3378 | builtin_strncpy_read_str, |
f883e0a7 | 3379 | CONST_CAST (char *, p), dest_align, false, 0); |
44e10129 | 3380 | dest_mem = force_operand (XEXP (dest_mem, 0), target); |
5ae6cd0d | 3381 | dest_mem = convert_memory_address (ptr_mode, dest_mem); |
aa0f70e6 | 3382 | return dest_mem; |
57814e5e | 3383 | } |
da9e9f08 | 3384 | } |
5039610b | 3385 | return NULL_RTX; |
da9e9f08 KG |
3386 | } |
3387 | ||
ab937357 JJ |
3388 | /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE) |
3389 | bytes from constant string DATA + OFFSET and return it as target | |
3390 | constant. */ | |
3391 | ||
34d85166 | 3392 | rtx |
4682ae04 | 3393 | builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED, |
ef4bddc2 | 3394 | machine_mode mode) |
ab937357 JJ |
3395 | { |
3396 | const char *c = (const char *) data; | |
f883e0a7 | 3397 | char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode)); |
ab937357 JJ |
3398 | |
3399 | memset (p, *c, GET_MODE_SIZE (mode)); | |
3400 | ||
3401 | return c_readstr (p, mode); | |
3402 | } | |
3403 | ||
1a887f86 RS |
3404 | /* Callback routine for store_by_pieces. Return the RTL of a register |
3405 | containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned | |
3406 | char value given in the RTL register data. For example, if mode is | |
3407 | 4 bytes wide, return the RTL for 0x01010101*data. */ | |
3408 | ||
3409 | static rtx | |
4682ae04 | 3410 | builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED, |
ef4bddc2 | 3411 | machine_mode mode) |
1a887f86 RS |
3412 | { |
3413 | rtx target, coeff; | |
3414 | size_t size; | |
3415 | char *p; | |
3416 | ||
3417 | size = GET_MODE_SIZE (mode); | |
5ab2f7b7 KH |
3418 | if (size == 1) |
3419 | return (rtx) data; | |
1a887f86 | 3420 | |
f883e0a7 | 3421 | p = XALLOCAVEC (char, size); |
1a887f86 RS |
3422 | memset (p, 1, size); |
3423 | coeff = c_readstr (p, mode); | |
3424 | ||
5ab2f7b7 | 3425 | target = convert_to_mode (mode, (rtx) data, 1); |
1a887f86 RS |
3426 | target = expand_mult (mode, target, coeff, NULL_RTX, 1); |
3427 | return force_reg (mode, target); | |
3428 | } | |
3429 | ||
b8698a0f L |
3430 | /* Expand expression EXP, which is a call to the memset builtin. Return |
3431 | NULL_RTX if we failed the caller should emit a normal call, otherwise | |
5039610b | 3432 | try to get the result in TARGET, if convenient (and in mode MODE if that's |
c2bd38e8 | 3433 | convenient). */ |
fed3cef0 | 3434 | |
28f4ec01 | 3435 | static rtx |
ef4bddc2 | 3436 | expand_builtin_memset (tree exp, rtx target, machine_mode mode) |
28f4ec01 | 3437 | { |
5039610b SL |
3438 | if (!validate_arglist (exp, |
3439 | POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3440 | return NULL_RTX; | |
28f4ec01 BS |
3441 | else |
3442 | { | |
5039610b SL |
3443 | tree dest = CALL_EXPR_ARG (exp, 0); |
3444 | tree val = CALL_EXPR_ARG (exp, 1); | |
3445 | tree len = CALL_EXPR_ARG (exp, 2); | |
3446 | return expand_builtin_memset_args (dest, val, len, target, mode, exp); | |
3447 | } | |
3448 | } | |
28f4ec01 | 3449 | |
edcf72f3 IE |
3450 | /* Expand expression EXP, which is an instrumented call to the memset builtin. |
3451 | Return NULL_RTX if we failed the caller should emit a normal call, otherwise | |
3452 | try to get the result in TARGET, if convenient (and in mode MODE if that's | |
3453 | convenient). */ | |
3454 | ||
3455 | static rtx | |
3456 | expand_builtin_memset_with_bounds (tree exp, rtx target, machine_mode mode) | |
3457 | { | |
3458 | if (!validate_arglist (exp, | |
3459 | POINTER_TYPE, POINTER_BOUNDS_TYPE, | |
3460 | INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3461 | return NULL_RTX; | |
3462 | else | |
3463 | { | |
3464 | tree dest = CALL_EXPR_ARG (exp, 0); | |
3465 | tree val = CALL_EXPR_ARG (exp, 2); | |
3466 | tree len = CALL_EXPR_ARG (exp, 3); | |
3467 | rtx res = expand_builtin_memset_args (dest, val, len, target, mode, exp); | |
3468 | ||
3469 | /* Return src bounds with the result. */ | |
3470 | if (res) | |
3471 | { | |
30975f63 | 3472 | rtx bnd = force_reg (targetm.chkp_bound_mode (), |
edcf72f3 IE |
3473 | expand_normal (CALL_EXPR_ARG (exp, 1))); |
3474 | res = chkp_join_splitted_slot (res, bnd); | |
3475 | } | |
3476 | return res; | |
3477 | } | |
3478 | } | |
3479 | ||
5039610b SL |
3480 | /* Helper function to do the actual work for expand_builtin_memset. The |
3481 | arguments to the builtin_memset call DEST, VAL, and LEN are broken out | |
3482 | so that this can also be called without constructing an actual CALL_EXPR. | |
3483 | The other arguments and return value are the same as for | |
3484 | expand_builtin_memset. */ | |
880864cf | 3485 | |
5039610b SL |
3486 | static rtx |
3487 | expand_builtin_memset_args (tree dest, tree val, tree len, | |
ef4bddc2 | 3488 | rtx target, machine_mode mode, tree orig_exp) |
5039610b SL |
3489 | { |
3490 | tree fndecl, fn; | |
3491 | enum built_in_function fcode; | |
ef4bddc2 | 3492 | machine_mode val_mode; |
5039610b SL |
3493 | char c; |
3494 | unsigned int dest_align; | |
3495 | rtx dest_mem, dest_addr, len_rtx; | |
3496 | HOST_WIDE_INT expected_size = -1; | |
3497 | unsigned int expected_align = 0; | |
3918b108 JH |
3498 | unsigned HOST_WIDE_INT min_size; |
3499 | unsigned HOST_WIDE_INT max_size; | |
82bb7d4e | 3500 | unsigned HOST_WIDE_INT probable_max_size; |
28f4ec01 | 3501 | |
0eb77834 | 3502 | dest_align = get_pointer_alignment (dest); |
079a182e | 3503 | |
5039610b SL |
3504 | /* If DEST is not a pointer type, don't do this operation in-line. */ |
3505 | if (dest_align == 0) | |
3506 | return NULL_RTX; | |
c2bd38e8 | 3507 | |
a5883ba0 MM |
3508 | if (currently_expanding_gimple_stmt) |
3509 | stringop_block_profile (currently_expanding_gimple_stmt, | |
3510 | &expected_align, &expected_size); | |
726a989a | 3511 | |
5039610b SL |
3512 | if (expected_align < dest_align) |
3513 | expected_align = dest_align; | |
880864cf | 3514 | |
5039610b SL |
3515 | /* If the LEN parameter is zero, return DEST. */ |
3516 | if (integer_zerop (len)) | |
3517 | { | |
3518 | /* Evaluate and ignore VAL in case it has side-effects. */ | |
3519 | expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
3520 | return expand_expr (dest, target, mode, EXPAND_NORMAL); | |
3521 | } | |
57e84f18 | 3522 | |
5039610b SL |
3523 | /* Stabilize the arguments in case we fail. */ |
3524 | dest = builtin_save_expr (dest); | |
3525 | val = builtin_save_expr (val); | |
3526 | len = builtin_save_expr (len); | |
1a887f86 | 3527 | |
5039610b | 3528 | len_rtx = expand_normal (len); |
82bb7d4e JH |
3529 | determine_block_size (len, len_rtx, &min_size, &max_size, |
3530 | &probable_max_size); | |
5039610b | 3531 | dest_mem = get_memory_rtx (dest, len); |
8a445129 | 3532 | val_mode = TYPE_MODE (unsigned_char_type_node); |
1a887f86 | 3533 | |
5039610b SL |
3534 | if (TREE_CODE (val) != INTEGER_CST) |
3535 | { | |
3536 | rtx val_rtx; | |
1a887f86 | 3537 | |
5039610b | 3538 | val_rtx = expand_normal (val); |
8a445129 | 3539 | val_rtx = convert_to_mode (val_mode, val_rtx, 0); |
28f4ec01 | 3540 | |
5039610b SL |
3541 | /* Assume that we can memset by pieces if we can store |
3542 | * the coefficients by pieces (in the required modes). | |
3543 | * We can't pass builtin_memset_gen_str as that emits RTL. */ | |
3544 | c = 1; | |
cc269bb6 | 3545 | if (tree_fits_uhwi_p (len) |
ae7e9ddd | 3546 | && can_store_by_pieces (tree_to_uhwi (len), |
cfa31150 SL |
3547 | builtin_memset_read_str, &c, dest_align, |
3548 | true)) | |
5039610b | 3549 | { |
8a445129 | 3550 | val_rtx = force_reg (val_mode, val_rtx); |
ae7e9ddd | 3551 | store_by_pieces (dest_mem, tree_to_uhwi (len), |
cfa31150 SL |
3552 | builtin_memset_gen_str, val_rtx, dest_align, |
3553 | true, 0); | |
5039610b SL |
3554 | } |
3555 | else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx, | |
3556 | dest_align, expected_align, | |
82bb7d4e JH |
3557 | expected_size, min_size, max_size, |
3558 | probable_max_size)) | |
880864cf | 3559 | goto do_libcall; |
b8698a0f | 3560 | |
5039610b SL |
3561 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
3562 | dest_mem = convert_memory_address (ptr_mode, dest_mem); | |
3563 | return dest_mem; | |
3564 | } | |
28f4ec01 | 3565 | |
5039610b SL |
3566 | if (target_char_cast (val, &c)) |
3567 | goto do_libcall; | |
ab937357 | 3568 | |
5039610b SL |
3569 | if (c) |
3570 | { | |
cc269bb6 | 3571 | if (tree_fits_uhwi_p (len) |
ae7e9ddd | 3572 | && can_store_by_pieces (tree_to_uhwi (len), |
cfa31150 SL |
3573 | builtin_memset_read_str, &c, dest_align, |
3574 | true)) | |
ae7e9ddd | 3575 | store_by_pieces (dest_mem, tree_to_uhwi (len), |
cfa31150 | 3576 | builtin_memset_read_str, &c, dest_align, true, 0); |
8a445129 RS |
3577 | else if (!set_storage_via_setmem (dest_mem, len_rtx, |
3578 | gen_int_mode (c, val_mode), | |
5039610b | 3579 | dest_align, expected_align, |
82bb7d4e JH |
3580 | expected_size, min_size, max_size, |
3581 | probable_max_size)) | |
5039610b | 3582 | goto do_libcall; |
b8698a0f | 3583 | |
5039610b SL |
3584 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
3585 | dest_mem = convert_memory_address (ptr_mode, dest_mem); | |
3586 | return dest_mem; | |
3587 | } | |
ab937357 | 3588 | |
5039610b SL |
3589 | set_mem_align (dest_mem, dest_align); |
3590 | dest_addr = clear_storage_hints (dest_mem, len_rtx, | |
3591 | CALL_EXPR_TAILCALL (orig_exp) | |
3592 | ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL, | |
3918b108 | 3593 | expected_align, expected_size, |
82bb7d4e JH |
3594 | min_size, max_size, |
3595 | probable_max_size); | |
28f4ec01 | 3596 | |
5039610b SL |
3597 | if (dest_addr == 0) |
3598 | { | |
3599 | dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX); | |
3600 | dest_addr = convert_memory_address (ptr_mode, dest_addr); | |
3601 | } | |
28f4ec01 | 3602 | |
5039610b | 3603 | return dest_addr; |
880864cf | 3604 | |
5039610b SL |
3605 | do_libcall: |
3606 | fndecl = get_callee_fndecl (orig_exp); | |
3607 | fcode = DECL_FUNCTION_CODE (fndecl); | |
edcf72f3 IE |
3608 | if (fcode == BUILT_IN_MEMSET |
3609 | || fcode == BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP) | |
aa493694 JJ |
3610 | fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3, |
3611 | dest, val, len); | |
5039610b | 3612 | else if (fcode == BUILT_IN_BZERO) |
aa493694 JJ |
3613 | fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2, |
3614 | dest, len); | |
5039610b SL |
3615 | else |
3616 | gcc_unreachable (); | |
44e10129 MM |
3617 | gcc_assert (TREE_CODE (fn) == CALL_EXPR); |
3618 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp); | |
5039610b | 3619 | return expand_call (fn, target, target == const0_rtx); |
28f4ec01 BS |
3620 | } |
3621 | ||
b8698a0f | 3622 | /* Expand expression EXP, which is a call to the bzero builtin. Return |
5039610b | 3623 | NULL_RTX if we failed the caller should emit a normal call. */ |
5197bd50 | 3624 | |
e3a709be | 3625 | static rtx |
8148fe65 | 3626 | expand_builtin_bzero (tree exp) |
e3a709be | 3627 | { |
5039610b | 3628 | tree dest, size; |
db3927fb | 3629 | location_t loc = EXPR_LOCATION (exp); |
e3a709be | 3630 | |
5039610b | 3631 | if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
3477addf | 3632 | return NULL_RTX; |
e3a709be | 3633 | |
5039610b SL |
3634 | dest = CALL_EXPR_ARG (exp, 0); |
3635 | size = CALL_EXPR_ARG (exp, 1); | |
8d51ecf8 | 3636 | |
3477addf | 3637 | /* New argument list transforming bzero(ptr x, int y) to |
c2bd38e8 RS |
3638 | memset(ptr x, int 0, size_t y). This is done this way |
3639 | so that if it isn't expanded inline, we fallback to | |
3640 | calling bzero instead of memset. */ | |
8d51ecf8 | 3641 | |
5039610b | 3642 | return expand_builtin_memset_args (dest, integer_zero_node, |
0d82a1c8 RG |
3643 | fold_convert_loc (loc, |
3644 | size_type_node, size), | |
5039610b | 3645 | const0_rtx, VOIDmode, exp); |
e3a709be KG |
3646 | } |
3647 | ||
a666df60 RS |
3648 | /* Try to expand cmpstr operation ICODE with the given operands. |
3649 | Return the result rtx on success, otherwise return null. */ | |
3650 | ||
3651 | static rtx | |
3652 | expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx, | |
3653 | HOST_WIDE_INT align) | |
3654 | { | |
3655 | machine_mode insn_mode = insn_data[icode].operand[0].mode; | |
3656 | ||
3657 | if (target && (!REG_P (target) || HARD_REGISTER_P (target))) | |
3658 | target = NULL_RTX; | |
3659 | ||
3660 | struct expand_operand ops[4]; | |
3661 | create_output_operand (&ops[0], target, insn_mode); | |
3662 | create_fixed_operand (&ops[1], arg1_rtx); | |
3663 | create_fixed_operand (&ops[2], arg2_rtx); | |
3664 | create_integer_operand (&ops[3], align); | |
3665 | if (maybe_expand_insn (icode, 4, ops)) | |
3666 | return ops[0].value; | |
3667 | return NULL_RTX; | |
3668 | } | |
3669 | ||
2be3b5ce | 3670 | /* Expand expression EXP, which is a call to the memcmp built-in function. |
9b0f6f5e | 3671 | Return NULL_RTX if we failed and the caller should emit a normal call, |
36b85e43 BS |
3672 | otherwise try to get the result in TARGET, if convenient. |
3673 | RESULT_EQ is true if we can relax the returned value to be either zero | |
3674 | or nonzero, without caring about the sign. */ | |
5197bd50 | 3675 | |
28f4ec01 | 3676 | static rtx |
36b85e43 | 3677 | expand_builtin_memcmp (tree exp, rtx target, bool result_eq) |
28f4ec01 | 3678 | { |
5039610b SL |
3679 | if (!validate_arglist (exp, |
3680 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3681 | return NULL_RTX; | |
c2bd38e8 | 3682 | |
7f9f48be RS |
3683 | tree arg1 = CALL_EXPR_ARG (exp, 0); |
3684 | tree arg2 = CALL_EXPR_ARG (exp, 1); | |
3685 | tree len = CALL_EXPR_ARG (exp, 2); | |
36b85e43 BS |
3686 | machine_mode mode = TYPE_MODE (TREE_TYPE (exp)); |
3687 | location_t loc = EXPR_LOCATION (exp); | |
358b8f01 | 3688 | |
7f9f48be RS |
3689 | unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT; |
3690 | unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT; | |
28f4ec01 | 3691 | |
7f9f48be RS |
3692 | /* If we don't have POINTER_TYPE, call the function. */ |
3693 | if (arg1_align == 0 || arg2_align == 0) | |
3694 | return NULL_RTX; | |
28f4ec01 | 3695 | |
7f9f48be RS |
3696 | rtx arg1_rtx = get_memory_rtx (arg1, len); |
3697 | rtx arg2_rtx = get_memory_rtx (arg2, len); | |
36b85e43 | 3698 | rtx len_rtx = expand_normal (fold_convert_loc (loc, sizetype, len)); |
28f4ec01 | 3699 | |
7f9f48be | 3700 | /* Set MEM_SIZE as appropriate. */ |
36b85e43 | 3701 | if (CONST_INT_P (len_rtx)) |
7f9f48be | 3702 | { |
36b85e43 BS |
3703 | set_mem_size (arg1_rtx, INTVAL (len_rtx)); |
3704 | set_mem_size (arg2_rtx, INTVAL (len_rtx)); | |
7f9f48be | 3705 | } |
6cbaec9e | 3706 | |
36b85e43 BS |
3707 | by_pieces_constfn constfn = NULL; |
3708 | ||
3709 | const char *src_str = c_getstr (arg1); | |
3710 | if (src_str == NULL) | |
3711 | src_str = c_getstr (arg2); | |
3712 | else | |
3713 | std::swap (arg1_rtx, arg2_rtx); | |
3714 | ||
3715 | /* If SRC is a string constant and block move would be done | |
3716 | by pieces, we can avoid loading the string from memory | |
3717 | and only stored the computed constants. */ | |
3718 | if (src_str | |
3719 | && CONST_INT_P (len_rtx) | |
3720 | && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1) | |
3721 | constfn = builtin_memcpy_read_str; | |
3722 | ||
3723 | rtx result = emit_block_cmp_hints (arg1_rtx, arg2_rtx, len_rtx, | |
3724 | TREE_TYPE (len), target, | |
3725 | result_eq, constfn, | |
3726 | CONST_CAST (char *, src_str)); | |
3727 | ||
7f9f48be RS |
3728 | if (result) |
3729 | { | |
3730 | /* Return the value in the proper mode for this function. */ | |
3731 | if (GET_MODE (result) == mode) | |
3732 | return result; | |
6cbaec9e | 3733 | |
7f9f48be RS |
3734 | if (target != 0) |
3735 | { | |
3736 | convert_move (target, result, 0); | |
3737 | return target; | |
3738 | } | |
8878e913 | 3739 | |
28f4ec01 | 3740 | return convert_to_mode (mode, result, 0); |
7f9f48be | 3741 | } |
28f4ec01 | 3742 | |
ee516de9 | 3743 | return NULL_RTX; |
c2bd38e8 RS |
3744 | } |
3745 | ||
5039610b | 3746 | /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX |
28f4ec01 BS |
3747 | if we failed the caller should emit a normal call, otherwise try to get |
3748 | the result in TARGET, if convenient. */ | |
fed3cef0 | 3749 | |
28f4ec01 | 3750 | static rtx |
44e10129 | 3751 | expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target) |
28f4ec01 | 3752 | { |
5039610b SL |
3753 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
3754 | return NULL_RTX; | |
8d51ecf8 | 3755 | |
a666df60 RS |
3756 | insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode); |
3757 | insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode); | |
3758 | if (cmpstr_icode != CODE_FOR_nothing || cmpstrn_icode != CODE_FOR_nothing) | |
40c1d5f8 AS |
3759 | { |
3760 | rtx arg1_rtx, arg2_rtx; | |
40c1d5f8 | 3761 | tree fndecl, fn; |
5039610b SL |
3762 | tree arg1 = CALL_EXPR_ARG (exp, 0); |
3763 | tree arg2 = CALL_EXPR_ARG (exp, 1); | |
a666df60 | 3764 | rtx result = NULL_RTX; |
c22cacf3 | 3765 | |
0eb77834 RG |
3766 | unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT; |
3767 | unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT; | |
40c1d5f8 AS |
3768 | |
3769 | /* If we don't have POINTER_TYPE, call the function. */ | |
3770 | if (arg1_align == 0 || arg2_align == 0) | |
5039610b | 3771 | return NULL_RTX; |
2be3b5ce | 3772 | |
40c1d5f8 AS |
3773 | /* Stabilize the arguments in case gen_cmpstr(n)si fail. */ |
3774 | arg1 = builtin_save_expr (arg1); | |
3775 | arg2 = builtin_save_expr (arg2); | |
2be3b5ce | 3776 | |
435bb2a1 JJ |
3777 | arg1_rtx = get_memory_rtx (arg1, NULL); |
3778 | arg2_rtx = get_memory_rtx (arg2, NULL); | |
28f4ec01 | 3779 | |
40c1d5f8 | 3780 | /* Try to call cmpstrsi. */ |
a666df60 RS |
3781 | if (cmpstr_icode != CODE_FOR_nothing) |
3782 | result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx, | |
3783 | MIN (arg1_align, arg2_align)); | |
3784 | ||
40c1d5f8 | 3785 | /* Try to determine at least one length and call cmpstrnsi. */ |
a666df60 | 3786 | if (!result && cmpstrn_icode != CODE_FOR_nothing) |
40c1d5f8 AS |
3787 | { |
3788 | tree len; | |
3789 | rtx arg3_rtx; | |
3790 | ||
40c1d5f8 AS |
3791 | tree len1 = c_strlen (arg1, 1); |
3792 | tree len2 = c_strlen (arg2, 1); | |
3793 | ||
3794 | if (len1) | |
3795 | len1 = size_binop (PLUS_EXPR, ssize_int (1), len1); | |
3796 | if (len2) | |
3797 | len2 = size_binop (PLUS_EXPR, ssize_int (1), len2); | |
3798 | ||
3799 | /* If we don't have a constant length for the first, use the length | |
3800 | of the second, if we know it. We don't require a constant for | |
3801 | this case; some cost analysis could be done if both are available | |
3802 | but neither is constant. For now, assume they're equally cheap, | |
3803 | unless one has side effects. If both strings have constant lengths, | |
3804 | use the smaller. */ | |
3805 | ||
3806 | if (!len1) | |
3807 | len = len2; | |
3808 | else if (!len2) | |
3809 | len = len1; | |
3810 | else if (TREE_SIDE_EFFECTS (len1)) | |
3811 | len = len2; | |
3812 | else if (TREE_SIDE_EFFECTS (len2)) | |
3813 | len = len1; | |
3814 | else if (TREE_CODE (len1) != INTEGER_CST) | |
3815 | len = len2; | |
3816 | else if (TREE_CODE (len2) != INTEGER_CST) | |
3817 | len = len1; | |
3818 | else if (tree_int_cst_lt (len1, len2)) | |
3819 | len = len1; | |
3820 | else | |
3821 | len = len2; | |
3822 | ||
3823 | /* If both arguments have side effects, we cannot optimize. */ | |
a666df60 RS |
3824 | if (len && !TREE_SIDE_EFFECTS (len)) |
3825 | { | |
3826 | arg3_rtx = expand_normal (len); | |
7f9f48be RS |
3827 | result = expand_cmpstrn_or_cmpmem |
3828 | (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len), | |
3829 | arg3_rtx, MIN (arg1_align, arg2_align)); | |
a666df60 | 3830 | } |
40c1d5f8 | 3831 | } |
c43fa1f5 | 3832 | |
a666df60 | 3833 | if (result) |
40c1d5f8 | 3834 | { |
40c1d5f8 | 3835 | /* Return the value in the proper mode for this function. */ |
a666df60 | 3836 | machine_mode mode = TYPE_MODE (TREE_TYPE (exp)); |
40c1d5f8 AS |
3837 | if (GET_MODE (result) == mode) |
3838 | return result; | |
3839 | if (target == 0) | |
3840 | return convert_to_mode (mode, result, 0); | |
3841 | convert_move (target, result, 0); | |
3842 | return target; | |
3843 | } | |
fed3cef0 | 3844 | |
40c1d5f8 AS |
3845 | /* Expand the library call ourselves using a stabilized argument |
3846 | list to avoid re-evaluating the function's arguments twice. */ | |
40c1d5f8 | 3847 | fndecl = get_callee_fndecl (exp); |
aa493694 | 3848 | fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2); |
44e10129 MM |
3849 | gcc_assert (TREE_CODE (fn) == CALL_EXPR); |
3850 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
40c1d5f8 AS |
3851 | return expand_call (fn, target, target == const0_rtx); |
3852 | } | |
5039610b | 3853 | return NULL_RTX; |
2dee4af1 | 3854 | } |
28f4ec01 | 3855 | |
b8698a0f | 3856 | /* Expand expression EXP, which is a call to the strncmp builtin. Return |
5039610b | 3857 | NULL_RTX if we failed the caller should emit a normal call, otherwise try to get |
da9e9f08 | 3858 | the result in TARGET, if convenient. */ |
5197bd50 | 3859 | |
da9e9f08 | 3860 | static rtx |
44e10129 | 3861 | expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target, |
ef4bddc2 | 3862 | ATTRIBUTE_UNUSED machine_mode mode) |
da9e9f08 | 3863 | { |
44e10129 | 3864 | location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp); |
db3927fb | 3865 | |
5039610b SL |
3866 | if (!validate_arglist (exp, |
3867 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3868 | return NULL_RTX; | |
da9e9f08 | 3869 | |
819c1488 | 3870 | /* If c_strlen can determine an expression for one of the string |
40c1d5f8 | 3871 | lengths, and it doesn't have side effects, then emit cmpstrnsi |
2be3b5ce | 3872 | using length MIN(strlen(string)+1, arg3). */ |
a666df60 RS |
3873 | insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode); |
3874 | if (cmpstrn_icode != CODE_FOR_nothing) | |
2be3b5ce RS |
3875 | { |
3876 | tree len, len1, len2; | |
3877 | rtx arg1_rtx, arg2_rtx, arg3_rtx; | |
a666df60 | 3878 | rtx result; |
8148fe65 | 3879 | tree fndecl, fn; |
5039610b SL |
3880 | tree arg1 = CALL_EXPR_ARG (exp, 0); |
3881 | tree arg2 = CALL_EXPR_ARG (exp, 1); | |
3882 | tree arg3 = CALL_EXPR_ARG (exp, 2); | |
c2bd38e8 | 3883 | |
0eb77834 RG |
3884 | unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT; |
3885 | unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT; | |
8d51ecf8 | 3886 | |
ae808627 JJ |
3887 | len1 = c_strlen (arg1, 1); |
3888 | len2 = c_strlen (arg2, 1); | |
2be3b5ce RS |
3889 | |
3890 | if (len1) | |
db3927fb | 3891 | len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1); |
2be3b5ce | 3892 | if (len2) |
db3927fb | 3893 | len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2); |
2be3b5ce RS |
3894 | |
3895 | /* If we don't have a constant length for the first, use the length | |
3896 | of the second, if we know it. We don't require a constant for | |
3897 | this case; some cost analysis could be done if both are available | |
3898 | but neither is constant. For now, assume they're equally cheap, | |
3899 | unless one has side effects. If both strings have constant lengths, | |
3900 | use the smaller. */ | |
3901 | ||
3902 | if (!len1) | |
3903 | len = len2; | |
3904 | else if (!len2) | |
3905 | len = len1; | |
3906 | else if (TREE_SIDE_EFFECTS (len1)) | |
3907 | len = len2; | |
3908 | else if (TREE_SIDE_EFFECTS (len2)) | |
3909 | len = len1; | |
3910 | else if (TREE_CODE (len1) != INTEGER_CST) | |
3911 | len = len2; | |
3912 | else if (TREE_CODE (len2) != INTEGER_CST) | |
3913 | len = len1; | |
3914 | else if (tree_int_cst_lt (len1, len2)) | |
3915 | len = len1; | |
3916 | else | |
3917 | len = len2; | |
819c1488 | 3918 | |
2be3b5ce RS |
3919 | /* If both arguments have side effects, we cannot optimize. */ |
3920 | if (!len || TREE_SIDE_EFFECTS (len)) | |
5039610b | 3921 | return NULL_RTX; |
8d51ecf8 | 3922 | |
2be3b5ce | 3923 | /* The actual new length parameter is MIN(len,arg3). */ |
db3927fb AH |
3924 | len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, |
3925 | fold_convert_loc (loc, TREE_TYPE (len), arg3)); | |
2be3b5ce RS |
3926 | |
3927 | /* If we don't have POINTER_TYPE, call the function. */ | |
3928 | if (arg1_align == 0 || arg2_align == 0) | |
5039610b | 3929 | return NULL_RTX; |
2be3b5ce | 3930 | |
44e10129 MM |
3931 | /* Stabilize the arguments in case gen_cmpstrnsi fails. */ |
3932 | arg1 = builtin_save_expr (arg1); | |
3933 | arg2 = builtin_save_expr (arg2); | |
3934 | len = builtin_save_expr (len); | |
5197bd50 | 3935 | |
44e10129 MM |
3936 | arg1_rtx = get_memory_rtx (arg1, len); |
3937 | arg2_rtx = get_memory_rtx (arg2, len); | |
3938 | arg3_rtx = expand_normal (len); | |
7f9f48be RS |
3939 | result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx, |
3940 | arg2_rtx, TREE_TYPE (len), arg3_rtx, | |
3941 | MIN (arg1_align, arg2_align)); | |
a666df60 | 3942 | if (result) |
44e10129 | 3943 | { |
44e10129 MM |
3944 | /* Return the value in the proper mode for this function. */ |
3945 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
3946 | if (GET_MODE (result) == mode) | |
3947 | return result; | |
3948 | if (target == 0) | |
3949 | return convert_to_mode (mode, result, 0); | |
3950 | convert_move (target, result, 0); | |
3951 | return target; | |
3952 | } | |
5197bd50 | 3953 | |
44e10129 MM |
3954 | /* Expand the library call ourselves using a stabilized argument |
3955 | list to avoid re-evaluating the function's arguments twice. */ | |
3956 | fndecl = get_callee_fndecl (exp); | |
aa493694 JJ |
3957 | fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3, |
3958 | arg1, arg2, len); | |
44e10129 MM |
3959 | gcc_assert (TREE_CODE (fn) == CALL_EXPR); |
3960 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
3961 | return expand_call (fn, target, target == const0_rtx); | |
3962 | } | |
5039610b | 3963 | return NULL_RTX; |
d118937d KG |
3964 | } |
3965 | ||
d3707adb RH |
3966 | /* Expand a call to __builtin_saveregs, generating the result in TARGET, |
3967 | if that's convenient. */ | |
fed3cef0 | 3968 | |
d3707adb | 3969 | rtx |
4682ae04 | 3970 | expand_builtin_saveregs (void) |
28f4ec01 | 3971 | { |
58f4cf2a DM |
3972 | rtx val; |
3973 | rtx_insn *seq; | |
28f4ec01 BS |
3974 | |
3975 | /* Don't do __builtin_saveregs more than once in a function. | |
3976 | Save the result of the first call and reuse it. */ | |
3977 | if (saveregs_value != 0) | |
3978 | return saveregs_value; | |
28f4ec01 | 3979 | |
d3707adb RH |
3980 | /* When this function is called, it means that registers must be |
3981 | saved on entry to this function. So we migrate the call to the | |
3982 | first insn of this function. */ | |
3983 | ||
3984 | start_sequence (); | |
28f4ec01 | 3985 | |
d3707adb | 3986 | /* Do whatever the machine needs done in this case. */ |
61f71b34 | 3987 | val = targetm.calls.expand_builtin_saveregs (); |
28f4ec01 | 3988 | |
d3707adb RH |
3989 | seq = get_insns (); |
3990 | end_sequence (); | |
28f4ec01 | 3991 | |
d3707adb | 3992 | saveregs_value = val; |
28f4ec01 | 3993 | |
2f937369 DM |
3994 | /* Put the insns after the NOTE that starts the function. If this |
3995 | is inside a start_sequence, make the outer-level insn chain current, so | |
d3707adb RH |
3996 | the code is placed at the start of the function. */ |
3997 | push_topmost_sequence (); | |
242229bb | 3998 | emit_insn_after (seq, entry_of_function ()); |
d3707adb RH |
3999 | pop_topmost_sequence (); |
4000 | ||
4001 | return val; | |
28f4ec01 BS |
4002 | } |
4003 | ||
8870e212 | 4004 | /* Expand a call to __builtin_next_arg. */ |
5197bd50 | 4005 | |
28f4ec01 | 4006 | static rtx |
8870e212 | 4007 | expand_builtin_next_arg (void) |
28f4ec01 | 4008 | { |
8870e212 JJ |
4009 | /* Checking arguments is already done in fold_builtin_next_arg |
4010 | that must be called before this function. */ | |
4319e38c | 4011 | return expand_binop (ptr_mode, add_optab, |
38173d38 JH |
4012 | crtl->args.internal_arg_pointer, |
4013 | crtl->args.arg_offset_rtx, | |
28f4ec01 BS |
4014 | NULL_RTX, 0, OPTAB_LIB_WIDEN); |
4015 | } | |
4016 | ||
d3707adb RH |
4017 | /* Make it easier for the backends by protecting the valist argument |
4018 | from multiple evaluations. */ | |
4019 | ||
4020 | static tree | |
db3927fb | 4021 | stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue) |
d3707adb | 4022 | { |
35cbb299 KT |
4023 | tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist)); |
4024 | ||
70f34814 RG |
4025 | /* The current way of determining the type of valist is completely |
4026 | bogus. We should have the information on the va builtin instead. */ | |
4027 | if (!vatype) | |
4028 | vatype = targetm.fn_abi_va_list (cfun->decl); | |
35cbb299 KT |
4029 | |
4030 | if (TREE_CODE (vatype) == ARRAY_TYPE) | |
d3707adb | 4031 | { |
9f720c3e GK |
4032 | if (TREE_SIDE_EFFECTS (valist)) |
4033 | valist = save_expr (valist); | |
8ebecc3b | 4034 | |
9f720c3e | 4035 | /* For this case, the backends will be expecting a pointer to |
35cbb299 KT |
4036 | vatype, but it's possible we've actually been given an array |
4037 | (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)). | |
9f720c3e GK |
4038 | So fix it. */ |
4039 | if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE) | |
daf68dd7 | 4040 | { |
35cbb299 | 4041 | tree p1 = build_pointer_type (TREE_TYPE (vatype)); |
db3927fb | 4042 | valist = build_fold_addr_expr_with_type_loc (loc, valist, p1); |
daf68dd7 | 4043 | } |
d3707adb | 4044 | } |
8ebecc3b | 4045 | else |
d3707adb | 4046 | { |
70f34814 | 4047 | tree pt = build_pointer_type (vatype); |
8ebecc3b | 4048 | |
9f720c3e GK |
4049 | if (! needs_lvalue) |
4050 | { | |
8ebecc3b RH |
4051 | if (! TREE_SIDE_EFFECTS (valist)) |
4052 | return valist; | |
8d51ecf8 | 4053 | |
db3927fb | 4054 | valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist); |
d3707adb | 4055 | TREE_SIDE_EFFECTS (valist) = 1; |
d3707adb | 4056 | } |
9f720c3e | 4057 | |
8ebecc3b | 4058 | if (TREE_SIDE_EFFECTS (valist)) |
9f720c3e | 4059 | valist = save_expr (valist); |
70f34814 RG |
4060 | valist = fold_build2_loc (loc, MEM_REF, |
4061 | vatype, valist, build_int_cst (pt, 0)); | |
d3707adb RH |
4062 | } |
4063 | ||
4064 | return valist; | |
4065 | } | |
4066 | ||
c35d187f RH |
4067 | /* The "standard" definition of va_list is void*. */ |
4068 | ||
4069 | tree | |
4070 | std_build_builtin_va_list (void) | |
4071 | { | |
4072 | return ptr_type_node; | |
4073 | } | |
4074 | ||
35cbb299 KT |
4075 | /* The "standard" abi va_list is va_list_type_node. */ |
4076 | ||
4077 | tree | |
4078 | std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED) | |
4079 | { | |
4080 | return va_list_type_node; | |
4081 | } | |
4082 | ||
4083 | /* The "standard" type of va_list is va_list_type_node. */ | |
4084 | ||
4085 | tree | |
4086 | std_canonical_va_list_type (tree type) | |
4087 | { | |
4088 | tree wtype, htype; | |
4089 | ||
35cbb299 KT |
4090 | wtype = va_list_type_node; |
4091 | htype = type; | |
e65d1ec6 KT |
4092 | /* Treat structure va_list types. */ |
4093 | if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype)) | |
4094 | htype = TREE_TYPE (htype); | |
4095 | else if (TREE_CODE (wtype) == ARRAY_TYPE) | |
35cbb299 KT |
4096 | { |
4097 | /* If va_list is an array type, the argument may have decayed | |
4098 | to a pointer type, e.g. by being passed to another function. | |
4099 | In that case, unwrap both types so that we can compare the | |
4100 | underlying records. */ | |
4101 | if (TREE_CODE (htype) == ARRAY_TYPE | |
4102 | || POINTER_TYPE_P (htype)) | |
4103 | { | |
4104 | wtype = TREE_TYPE (wtype); | |
4105 | htype = TREE_TYPE (htype); | |
4106 | } | |
4107 | } | |
4108 | if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype)) | |
4109 | return va_list_type_node; | |
4110 | ||
4111 | return NULL_TREE; | |
4112 | } | |
4113 | ||
d3707adb RH |
4114 | /* The "standard" implementation of va_start: just assign `nextarg' to |
4115 | the variable. */ | |
5197bd50 | 4116 | |
d3707adb | 4117 | void |
4682ae04 | 4118 | std_expand_builtin_va_start (tree valist, rtx nextarg) |
d3707adb | 4119 | { |
508dabda ILT |
4120 | rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE); |
4121 | convert_move (va_r, nextarg, 0); | |
d5e254e1 IE |
4122 | |
4123 | /* We do not have any valid bounds for the pointer, so | |
4124 | just store zero bounds for it. */ | |
4125 | if (chkp_function_instrumented_p (current_function_decl)) | |
4126 | chkp_expand_bounds_reset_for_mem (valist, | |
4127 | make_tree (TREE_TYPE (valist), | |
4128 | nextarg)); | |
d3707adb RH |
4129 | } |
4130 | ||
5039610b | 4131 | /* Expand EXP, a call to __builtin_va_start. */ |
5197bd50 | 4132 | |
d3707adb | 4133 | static rtx |
5039610b | 4134 | expand_builtin_va_start (tree exp) |
d3707adb RH |
4135 | { |
4136 | rtx nextarg; | |
5039610b | 4137 | tree valist; |
db3927fb | 4138 | location_t loc = EXPR_LOCATION (exp); |
d3707adb | 4139 | |
5039610b | 4140 | if (call_expr_nargs (exp) < 2) |
c69c9b36 | 4141 | { |
db3927fb | 4142 | error_at (loc, "too few arguments to function %<va_start%>"); |
c69c9b36 JM |
4143 | return const0_rtx; |
4144 | } | |
d3707adb | 4145 | |
5039610b | 4146 | if (fold_builtin_next_arg (exp, true)) |
8870e212 | 4147 | return const0_rtx; |
d3147f64 | 4148 | |
8870e212 | 4149 | nextarg = expand_builtin_next_arg (); |
db3927fb | 4150 | valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1); |
d3707adb | 4151 | |
d7bd8aeb JJ |
4152 | if (targetm.expand_builtin_va_start) |
4153 | targetm.expand_builtin_va_start (valist, nextarg); | |
4154 | else | |
4155 | std_expand_builtin_va_start (valist, nextarg); | |
d3707adb RH |
4156 | |
4157 | return const0_rtx; | |
4158 | } | |
4159 | ||
5039610b | 4160 | /* Expand EXP, a call to __builtin_va_end. */ |
3bdf5ad1 | 4161 | |
d3707adb | 4162 | static rtx |
5039610b | 4163 | expand_builtin_va_end (tree exp) |
d3707adb | 4164 | { |
5039610b | 4165 | tree valist = CALL_EXPR_ARG (exp, 0); |
daf68dd7 | 4166 | |
daf68dd7 RH |
4167 | /* Evaluate for side effects, if needed. I hate macros that don't |
4168 | do that. */ | |
4169 | if (TREE_SIDE_EFFECTS (valist)) | |
4170 | expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
d3707adb RH |
4171 | |
4172 | return const0_rtx; | |
4173 | } | |
4174 | ||
5039610b | 4175 | /* Expand EXP, a call to __builtin_va_copy. We do this as a |
d3707adb RH |
4176 | builtin rather than just as an assignment in stdarg.h because of the |
4177 | nastiness of array-type va_list types. */ | |
3bdf5ad1 | 4178 | |
d3707adb | 4179 | static rtx |
5039610b | 4180 | expand_builtin_va_copy (tree exp) |
d3707adb RH |
4181 | { |
4182 | tree dst, src, t; | |
db3927fb | 4183 | location_t loc = EXPR_LOCATION (exp); |
d3707adb | 4184 | |
5039610b SL |
4185 | dst = CALL_EXPR_ARG (exp, 0); |
4186 | src = CALL_EXPR_ARG (exp, 1); | |
d3707adb | 4187 | |
db3927fb AH |
4188 | dst = stabilize_va_list_loc (loc, dst, 1); |
4189 | src = stabilize_va_list_loc (loc, src, 0); | |
d3707adb | 4190 | |
35cbb299 KT |
4191 | gcc_assert (cfun != NULL && cfun->decl != NULL_TREE); |
4192 | ||
4193 | if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE) | |
d3707adb | 4194 | { |
35cbb299 | 4195 | t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src); |
d3707adb RH |
4196 | TREE_SIDE_EFFECTS (t) = 1; |
4197 | expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
4198 | } | |
4199 | else | |
4200 | { | |
8ebecc3b RH |
4201 | rtx dstb, srcb, size; |
4202 | ||
4203 | /* Evaluate to pointers. */ | |
4204 | dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL); | |
4205 | srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL); | |
35cbb299 KT |
4206 | size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)), |
4207 | NULL_RTX, VOIDmode, EXPAND_NORMAL); | |
8ebecc3b | 4208 | |
5ae6cd0d MM |
4209 | dstb = convert_memory_address (Pmode, dstb); |
4210 | srcb = convert_memory_address (Pmode, srcb); | |
ce2d32cd | 4211 | |
8ebecc3b RH |
4212 | /* "Dereference" to BLKmode memories. */ |
4213 | dstb = gen_rtx_MEM (BLKmode, dstb); | |
ba4828e0 | 4214 | set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst)))); |
35cbb299 | 4215 | set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl))); |
8ebecc3b | 4216 | srcb = gen_rtx_MEM (BLKmode, srcb); |
ba4828e0 | 4217 | set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src)))); |
35cbb299 | 4218 | set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl))); |
8ebecc3b RH |
4219 | |
4220 | /* Copy. */ | |
44bb111a | 4221 | emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL); |
d3707adb RH |
4222 | } |
4223 | ||
4224 | return const0_rtx; | |
4225 | } | |
4226 | ||
28f4ec01 BS |
4227 | /* Expand a call to one of the builtin functions __builtin_frame_address or |
4228 | __builtin_return_address. */ | |
5197bd50 | 4229 | |
28f4ec01 | 4230 | static rtx |
5039610b | 4231 | expand_builtin_frame_address (tree fndecl, tree exp) |
28f4ec01 | 4232 | { |
28f4ec01 BS |
4233 | /* The argument must be a nonnegative integer constant. |
4234 | It counts the number of frames to scan up the stack. | |
8423e57c MS |
4235 | The value is either the frame pointer value or the return |
4236 | address saved in that frame. */ | |
5039610b | 4237 | if (call_expr_nargs (exp) == 0) |
28f4ec01 BS |
4238 | /* Warning about missing arg was already issued. */ |
4239 | return const0_rtx; | |
cc269bb6 | 4240 | else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0))) |
28f4ec01 | 4241 | { |
8423e57c | 4242 | error ("invalid argument to %qD", fndecl); |
28f4ec01 BS |
4243 | return const0_rtx; |
4244 | } | |
4245 | else | |
4246 | { | |
8423e57c MS |
4247 | /* Number of frames to scan up the stack. */ |
4248 | unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0)); | |
4249 | ||
4250 | rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count); | |
28f4ec01 BS |
4251 | |
4252 | /* Some ports cannot access arbitrary stack frames. */ | |
4253 | if (tem == NULL) | |
4254 | { | |
8423e57c | 4255 | warning (0, "unsupported argument to %qD", fndecl); |
28f4ec01 BS |
4256 | return const0_rtx; |
4257 | } | |
4258 | ||
8423e57c MS |
4259 | if (count) |
4260 | { | |
4261 | /* Warn since no effort is made to ensure that any frame | |
4262 | beyond the current one exists or can be safely reached. */ | |
4263 | warning (OPT_Wframe_address, "calling %qD with " | |
4264 | "a nonzero argument is unsafe", fndecl); | |
4265 | } | |
4266 | ||
28f4ec01 BS |
4267 | /* For __builtin_frame_address, return what we've got. */ |
4268 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS) | |
4269 | return tem; | |
4270 | ||
f8cfc6aa | 4271 | if (!REG_P (tem) |
28f4ec01 | 4272 | && ! CONSTANT_P (tem)) |
18ae1560 | 4273 | tem = copy_addr_to_reg (tem); |
28f4ec01 BS |
4274 | return tem; |
4275 | } | |
4276 | } | |
4277 | ||
d3c12306 | 4278 | /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we |
3a42502d RH |
4279 | failed and the caller should emit a normal call. CANNOT_ACCUMULATE |
4280 | is the same as for allocate_dynamic_stack_space. */ | |
d5457140 | 4281 | |
28f4ec01 | 4282 | static rtx |
3a42502d | 4283 | expand_builtin_alloca (tree exp, bool cannot_accumulate) |
28f4ec01 BS |
4284 | { |
4285 | rtx op0; | |
d5457140 | 4286 | rtx result; |
13e49da9 TV |
4287 | bool valid_arglist; |
4288 | unsigned int align; | |
4289 | bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp)) | |
4290 | == BUILT_IN_ALLOCA_WITH_ALIGN); | |
28f4ec01 | 4291 | |
13e49da9 TV |
4292 | valid_arglist |
4293 | = (alloca_with_align | |
4294 | ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE) | |
4295 | : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE)); | |
4296 | ||
4297 | if (!valid_arglist) | |
5039610b | 4298 | return NULL_RTX; |
28f4ec01 BS |
4299 | |
4300 | /* Compute the argument. */ | |
5039610b | 4301 | op0 = expand_normal (CALL_EXPR_ARG (exp, 0)); |
28f4ec01 | 4302 | |
13e49da9 TV |
4303 | /* Compute the alignment. */ |
4304 | align = (alloca_with_align | |
4305 | ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1)) | |
4306 | : BIGGEST_ALIGNMENT); | |
4307 | ||
28f4ec01 | 4308 | /* Allocate the desired space. */ |
13e49da9 | 4309 | result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate); |
5ae6cd0d | 4310 | result = convert_memory_address (ptr_mode, result); |
d5457140 RK |
4311 | |
4312 | return result; | |
28f4ec01 BS |
4313 | } |
4314 | ||
ac868f29 EB |
4315 | /* Expand a call to bswap builtin in EXP. |
4316 | Return NULL_RTX if a normal call should be emitted rather than expanding the | |
4317 | function in-line. If convenient, the result should be placed in TARGET. | |
4318 | SUBTARGET may be used as the target for computing one of EXP's operands. */ | |
167fa32c EC |
4319 | |
4320 | static rtx | |
ef4bddc2 | 4321 | expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target, |
ac868f29 | 4322 | rtx subtarget) |
167fa32c | 4323 | { |
167fa32c EC |
4324 | tree arg; |
4325 | rtx op0; | |
4326 | ||
5039610b SL |
4327 | if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE)) |
4328 | return NULL_RTX; | |
167fa32c | 4329 | |
5039610b | 4330 | arg = CALL_EXPR_ARG (exp, 0); |
ac868f29 EB |
4331 | op0 = expand_expr (arg, |
4332 | subtarget && GET_MODE (subtarget) == target_mode | |
4333 | ? subtarget : NULL_RTX, | |
4334 | target_mode, EXPAND_NORMAL); | |
4335 | if (GET_MODE (op0) != target_mode) | |
4336 | op0 = convert_to_mode (target_mode, op0, 1); | |
167fa32c | 4337 | |
ac868f29 | 4338 | target = expand_unop (target_mode, bswap_optab, op0, target, 1); |
167fa32c EC |
4339 | |
4340 | gcc_assert (target); | |
4341 | ||
ac868f29 | 4342 | return convert_to_mode (target_mode, target, 1); |
167fa32c EC |
4343 | } |
4344 | ||
5039610b SL |
4345 | /* Expand a call to a unary builtin in EXP. |
4346 | Return NULL_RTX if a normal call should be emitted rather than expanding the | |
28f4ec01 BS |
4347 | function in-line. If convenient, the result should be placed in TARGET. |
4348 | SUBTARGET may be used as the target for computing one of EXP's operands. */ | |
d5457140 | 4349 | |
28f4ec01 | 4350 | static rtx |
ef4bddc2 | 4351 | expand_builtin_unop (machine_mode target_mode, tree exp, rtx target, |
4682ae04 | 4352 | rtx subtarget, optab op_optab) |
28f4ec01 BS |
4353 | { |
4354 | rtx op0; | |
5039610b SL |
4355 | |
4356 | if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE)) | |
4357 | return NULL_RTX; | |
28f4ec01 BS |
4358 | |
4359 | /* Compute the argument. */ | |
4359dc2a JJ |
4360 | op0 = expand_expr (CALL_EXPR_ARG (exp, 0), |
4361 | (subtarget | |
4362 | && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))) | |
4363 | == GET_MODE (subtarget))) ? subtarget : NULL_RTX, | |
49452c07 | 4364 | VOIDmode, EXPAND_NORMAL); |
2928cd7a | 4365 | /* Compute op, into TARGET if possible. |
28f4ec01 | 4366 | Set TARGET to wherever the result comes back. */ |
5039610b | 4367 | target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))), |
146aef0b | 4368 | op_optab, op0, target, op_optab != clrsb_optab); |
298e6adc | 4369 | gcc_assert (target); |
5906d013 | 4370 | |
6c537d03 | 4371 | return convert_to_mode (target_mode, target, 0); |
28f4ec01 | 4372 | } |
994a57cd | 4373 | |
b8698a0f | 4374 | /* Expand a call to __builtin_expect. We just return our argument |
ef950eba JH |
4375 | as the builtin_expect semantic should've been already executed by |
4376 | tree branch prediction pass. */ | |
994a57cd RH |
4377 | |
4378 | static rtx | |
5039610b | 4379 | expand_builtin_expect (tree exp, rtx target) |
994a57cd | 4380 | { |
451409e4 | 4381 | tree arg; |
994a57cd | 4382 | |
5039610b | 4383 | if (call_expr_nargs (exp) < 2) |
994a57cd | 4384 | return const0_rtx; |
5039610b | 4385 | arg = CALL_EXPR_ARG (exp, 0); |
994a57cd | 4386 | |
5039610b | 4387 | target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL); |
ef950eba | 4388 | /* When guessing was done, the hints should be already stripped away. */ |
1d8381f1 | 4389 | gcc_assert (!flag_guess_branch_prob |
1da2ed5f | 4390 | || optimize == 0 || seen_error ()); |
994a57cd RH |
4391 | return target; |
4392 | } | |
5f2d6cfa | 4393 | |
45d439ac JJ |
4394 | /* Expand a call to __builtin_assume_aligned. We just return our first |
4395 | argument as the builtin_assume_aligned semantic should've been already | |
4396 | executed by CCP. */ | |
4397 | ||
4398 | static rtx | |
4399 | expand_builtin_assume_aligned (tree exp, rtx target) | |
4400 | { | |
4401 | if (call_expr_nargs (exp) < 2) | |
4402 | return const0_rtx; | |
4403 | target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode, | |
4404 | EXPAND_NORMAL); | |
4405 | gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1)) | |
4406 | && (call_expr_nargs (exp) < 3 | |
4407 | || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2)))); | |
4408 | return target; | |
4409 | } | |
4410 | ||
1e188d1e | 4411 | void |
4682ae04 | 4412 | expand_builtin_trap (void) |
9602f5a0 | 4413 | { |
eb6f47fb | 4414 | if (targetm.have_trap ()) |
206604dc | 4415 | { |
eb6f47fb | 4416 | rtx_insn *insn = emit_insn (targetm.gen_trap ()); |
206604dc JJ |
4417 | /* For trap insns when not accumulating outgoing args force |
4418 | REG_ARGS_SIZE note to prevent crossjumping of calls with | |
4419 | different args sizes. */ | |
4420 | if (!ACCUMULATE_OUTGOING_ARGS) | |
4421 | add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta)); | |
4422 | } | |
9602f5a0 | 4423 | else |
ee516de9 EB |
4424 | { |
4425 | tree fn = builtin_decl_implicit (BUILT_IN_ABORT); | |
4426 | tree call_expr = build_call_expr (fn, 0); | |
4427 | expand_call (call_expr, NULL_RTX, false); | |
4428 | } | |
4429 | ||
9602f5a0 RH |
4430 | emit_barrier (); |
4431 | } | |
075ec276 | 4432 | |
468059bc DD |
4433 | /* Expand a call to __builtin_unreachable. We do nothing except emit |
4434 | a barrier saying that control flow will not pass here. | |
4435 | ||
4436 | It is the responsibility of the program being compiled to ensure | |
4437 | that control flow does never reach __builtin_unreachable. */ | |
4438 | static void | |
4439 | expand_builtin_unreachable (void) | |
4440 | { | |
4441 | emit_barrier (); | |
4442 | } | |
4443 | ||
5039610b SL |
4444 | /* Expand EXP, a call to fabs, fabsf or fabsl. |
4445 | Return NULL_RTX if a normal call should be emitted rather than expanding | |
075ec276 RS |
4446 | the function inline. If convenient, the result should be placed |
4447 | in TARGET. SUBTARGET may be used as the target for computing | |
4448 | the operand. */ | |
4449 | ||
4450 | static rtx | |
5039610b | 4451 | expand_builtin_fabs (tree exp, rtx target, rtx subtarget) |
075ec276 | 4452 | { |
ef4bddc2 | 4453 | machine_mode mode; |
075ec276 RS |
4454 | tree arg; |
4455 | rtx op0; | |
4456 | ||
5039610b SL |
4457 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
4458 | return NULL_RTX; | |
075ec276 | 4459 | |
5039610b | 4460 | arg = CALL_EXPR_ARG (exp, 0); |
4cd8e76f | 4461 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); |
075ec276 | 4462 | mode = TYPE_MODE (TREE_TYPE (arg)); |
49452c07 | 4463 | op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL); |
075ec276 RS |
4464 | return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1)); |
4465 | } | |
4466 | ||
5039610b | 4467 | /* Expand EXP, a call to copysign, copysignf, or copysignl. |
046625fa RH |
4468 | Return NULL is a normal call should be emitted rather than expanding the |
4469 | function inline. If convenient, the result should be placed in TARGET. | |
4470 | SUBTARGET may be used as the target for computing the operand. */ | |
4471 | ||
4472 | static rtx | |
5039610b | 4473 | expand_builtin_copysign (tree exp, rtx target, rtx subtarget) |
046625fa RH |
4474 | { |
4475 | rtx op0, op1; | |
4476 | tree arg; | |
4477 | ||
5039610b SL |
4478 | if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE)) |
4479 | return NULL_RTX; | |
046625fa | 4480 | |
5039610b | 4481 | arg = CALL_EXPR_ARG (exp, 0); |
84217346 | 4482 | op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL); |
046625fa | 4483 | |
5039610b | 4484 | arg = CALL_EXPR_ARG (exp, 1); |
84217346 | 4485 | op1 = expand_normal (arg); |
046625fa RH |
4486 | |
4487 | return expand_copysign (op0, op1, target); | |
4488 | } | |
4489 | ||
677feb77 DD |
4490 | /* Expand a call to __builtin___clear_cache. */ |
4491 | ||
4492 | static rtx | |
f2cf13bd | 4493 | expand_builtin___clear_cache (tree exp) |
677feb77 | 4494 | { |
f2cf13bd RS |
4495 | if (!targetm.code_for_clear_cache) |
4496 | { | |
677feb77 | 4497 | #ifdef CLEAR_INSN_CACHE |
f2cf13bd RS |
4498 | /* There is no "clear_cache" insn, and __clear_cache() in libgcc |
4499 | does something. Just do the default expansion to a call to | |
4500 | __clear_cache(). */ | |
4501 | return NULL_RTX; | |
677feb77 | 4502 | #else |
f2cf13bd RS |
4503 | /* There is no "clear_cache" insn, and __clear_cache() in libgcc |
4504 | does nothing. There is no need to call it. Do nothing. */ | |
4505 | return const0_rtx; | |
677feb77 | 4506 | #endif /* CLEAR_INSN_CACHE */ |
f2cf13bd RS |
4507 | } |
4508 | ||
677feb77 DD |
4509 | /* We have a "clear_cache" insn, and it will handle everything. */ |
4510 | tree begin, end; | |
4511 | rtx begin_rtx, end_rtx; | |
677feb77 DD |
4512 | |
4513 | /* We must not expand to a library call. If we did, any | |
4514 | fallback library function in libgcc that might contain a call to | |
4515 | __builtin___clear_cache() would recurse infinitely. */ | |
4516 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
4517 | { | |
4518 | error ("both arguments to %<__builtin___clear_cache%> must be pointers"); | |
4519 | return const0_rtx; | |
4520 | } | |
4521 | ||
f2cf13bd | 4522 | if (targetm.have_clear_cache ()) |
677feb77 | 4523 | { |
a5c7d693 | 4524 | struct expand_operand ops[2]; |
677feb77 DD |
4525 | |
4526 | begin = CALL_EXPR_ARG (exp, 0); | |
4527 | begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL); | |
677feb77 DD |
4528 | |
4529 | end = CALL_EXPR_ARG (exp, 1); | |
4530 | end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL); | |
677feb77 | 4531 | |
a5c7d693 RS |
4532 | create_address_operand (&ops[0], begin_rtx); |
4533 | create_address_operand (&ops[1], end_rtx); | |
f2cf13bd | 4534 | if (maybe_expand_insn (targetm.code_for_clear_cache, 2, ops)) |
a5c7d693 | 4535 | return const0_rtx; |
677feb77 DD |
4536 | } |
4537 | return const0_rtx; | |
677feb77 DD |
4538 | } |
4539 | ||
6de9cd9a DN |
4540 | /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */ |
4541 | ||
4542 | static rtx | |
4543 | round_trampoline_addr (rtx tramp) | |
4544 | { | |
4545 | rtx temp, addend, mask; | |
4546 | ||
4547 | /* If we don't need too much alignment, we'll have been guaranteed | |
4548 | proper alignment by get_trampoline_type. */ | |
4549 | if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY) | |
4550 | return tramp; | |
4551 | ||
4552 | /* Round address up to desired boundary. */ | |
4553 | temp = gen_reg_rtx (Pmode); | |
2f1cd2eb RS |
4554 | addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode); |
4555 | mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode); | |
6de9cd9a DN |
4556 | |
4557 | temp = expand_simple_binop (Pmode, PLUS, tramp, addend, | |
4558 | temp, 0, OPTAB_LIB_WIDEN); | |
4559 | tramp = expand_simple_binop (Pmode, AND, temp, mask, | |
4560 | temp, 0, OPTAB_LIB_WIDEN); | |
4561 | ||
4562 | return tramp; | |
4563 | } | |
4564 | ||
4565 | static rtx | |
183dd130 | 4566 | expand_builtin_init_trampoline (tree exp, bool onstack) |
6de9cd9a DN |
4567 | { |
4568 | tree t_tramp, t_func, t_chain; | |
531ca746 | 4569 | rtx m_tramp, r_tramp, r_chain, tmp; |
6de9cd9a | 4570 | |
5039610b | 4571 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, |
6de9cd9a DN |
4572 | POINTER_TYPE, VOID_TYPE)) |
4573 | return NULL_RTX; | |
4574 | ||
5039610b SL |
4575 | t_tramp = CALL_EXPR_ARG (exp, 0); |
4576 | t_func = CALL_EXPR_ARG (exp, 1); | |
4577 | t_chain = CALL_EXPR_ARG (exp, 2); | |
6de9cd9a | 4578 | |
84217346 | 4579 | r_tramp = expand_normal (t_tramp); |
531ca746 RH |
4580 | m_tramp = gen_rtx_MEM (BLKmode, r_tramp); |
4581 | MEM_NOTRAP_P (m_tramp) = 1; | |
4582 | ||
183dd130 ILT |
4583 | /* If ONSTACK, the TRAMP argument should be the address of a field |
4584 | within the local function's FRAME decl. Either way, let's see if | |
4585 | we can fill in the MEM_ATTRs for this memory. */ | |
531ca746 | 4586 | if (TREE_CODE (t_tramp) == ADDR_EXPR) |
ad2e5b71 | 4587 | set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true); |
531ca746 | 4588 | |
183dd130 ILT |
4589 | /* Creator of a heap trampoline is responsible for making sure the |
4590 | address is aligned to at least STACK_BOUNDARY. Normally malloc | |
4591 | will ensure this anyhow. */ | |
531ca746 RH |
4592 | tmp = round_trampoline_addr (r_tramp); |
4593 | if (tmp != r_tramp) | |
4594 | { | |
4595 | m_tramp = change_address (m_tramp, BLKmode, tmp); | |
4596 | set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT); | |
f5541398 | 4597 | set_mem_size (m_tramp, TRAMPOLINE_SIZE); |
531ca746 RH |
4598 | } |
4599 | ||
4600 | /* The FUNC argument should be the address of the nested function. | |
4601 | Extract the actual function decl to pass to the hook. */ | |
4602 | gcc_assert (TREE_CODE (t_func) == ADDR_EXPR); | |
4603 | t_func = TREE_OPERAND (t_func, 0); | |
4604 | gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL); | |
4605 | ||
84217346 | 4606 | r_chain = expand_normal (t_chain); |
6de9cd9a DN |
4607 | |
4608 | /* Generate insns to initialize the trampoline. */ | |
531ca746 | 4609 | targetm.calls.trampoline_init (m_tramp, t_func, r_chain); |
6de9cd9a | 4610 | |
183dd130 ILT |
4611 | if (onstack) |
4612 | { | |
4613 | trampolines_created = 1; | |
8ffadef9 | 4614 | |
183dd130 ILT |
4615 | warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines, |
4616 | "trampoline generated for nested function %qD", t_func); | |
4617 | } | |
8ffadef9 | 4618 | |
6de9cd9a DN |
4619 | return const0_rtx; |
4620 | } | |
4621 | ||
4622 | static rtx | |
5039610b | 4623 | expand_builtin_adjust_trampoline (tree exp) |
6de9cd9a DN |
4624 | { |
4625 | rtx tramp; | |
4626 | ||
5039610b | 4627 | if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
6de9cd9a DN |
4628 | return NULL_RTX; |
4629 | ||
5039610b | 4630 | tramp = expand_normal (CALL_EXPR_ARG (exp, 0)); |
6de9cd9a | 4631 | tramp = round_trampoline_addr (tramp); |
531ca746 RH |
4632 | if (targetm.calls.trampoline_adjust_address) |
4633 | tramp = targetm.calls.trampoline_adjust_address (tramp); | |
6de9cd9a DN |
4634 | |
4635 | return tramp; | |
4636 | } | |
4637 | ||
0f67fa83 WG |
4638 | /* Expand the call EXP to the built-in signbit, signbitf or signbitl |
4639 | function. The function first checks whether the back end provides | |
4640 | an insn to implement signbit for the respective mode. If not, it | |
4641 | checks whether the floating point format of the value is such that | |
61717a45 FXC |
4642 | the sign bit can be extracted. If that is not the case, error out. |
4643 | EXP is the expression that is a call to the builtin function; if | |
4644 | convenient, the result should be placed in TARGET. */ | |
ef79730c RS |
4645 | static rtx |
4646 | expand_builtin_signbit (tree exp, rtx target) | |
4647 | { | |
4648 | const struct real_format *fmt; | |
ef4bddc2 | 4649 | machine_mode fmode, imode, rmode; |
5039610b | 4650 | tree arg; |
e4fbead1 | 4651 | int word, bitpos; |
d0c9d431 | 4652 | enum insn_code icode; |
ef79730c | 4653 | rtx temp; |
db3927fb | 4654 | location_t loc = EXPR_LOCATION (exp); |
ef79730c | 4655 | |
5039610b SL |
4656 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
4657 | return NULL_RTX; | |
ef79730c | 4658 | |
5039610b | 4659 | arg = CALL_EXPR_ARG (exp, 0); |
ef79730c RS |
4660 | fmode = TYPE_MODE (TREE_TYPE (arg)); |
4661 | rmode = TYPE_MODE (TREE_TYPE (exp)); | |
4662 | fmt = REAL_MODE_FORMAT (fmode); | |
4663 | ||
0f67fa83 WG |
4664 | arg = builtin_save_expr (arg); |
4665 | ||
4666 | /* Expand the argument yielding a RTX expression. */ | |
4667 | temp = expand_normal (arg); | |
4668 | ||
4669 | /* Check if the back end provides an insn that handles signbit for the | |
4670 | argument's mode. */ | |
947131ba | 4671 | icode = optab_handler (signbit_optab, fmode); |
d0c9d431 | 4672 | if (icode != CODE_FOR_nothing) |
0f67fa83 | 4673 | { |
58f4cf2a | 4674 | rtx_insn *last = get_last_insn (); |
0f67fa83 | 4675 | target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp))); |
8a0b1aa4 MM |
4676 | if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN)) |
4677 | return target; | |
4678 | delete_insns_since (last); | |
0f67fa83 WG |
4679 | } |
4680 | ||
ef79730c RS |
4681 | /* For floating point formats without a sign bit, implement signbit |
4682 | as "ARG < 0.0". */ | |
b87a0206 | 4683 | bitpos = fmt->signbit_ro; |
e4fbead1 | 4684 | if (bitpos < 0) |
ef79730c RS |
4685 | { |
4686 | /* But we can't do this if the format supports signed zero. */ | |
61717a45 | 4687 | gcc_assert (!fmt->has_signed_zero || !HONOR_SIGNED_ZEROS (fmode)); |
ef79730c | 4688 | |
db3927fb | 4689 | arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg, |
987b67bc | 4690 | build_real (TREE_TYPE (arg), dconst0)); |
ef79730c RS |
4691 | return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL); |
4692 | } | |
4693 | ||
e4fbead1 | 4694 | if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD) |
ef79730c | 4695 | { |
e4fbead1 | 4696 | imode = int_mode_for_mode (fmode); |
61717a45 | 4697 | gcc_assert (imode != BLKmode); |
e4fbead1 | 4698 | temp = gen_lowpart (imode, temp); |
254878ea RS |
4699 | } |
4700 | else | |
4701 | { | |
e4fbead1 RS |
4702 | imode = word_mode; |
4703 | /* Handle targets with different FP word orders. */ | |
4704 | if (FLOAT_WORDS_BIG_ENDIAN) | |
c22cacf3 | 4705 | word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD; |
e4fbead1 | 4706 | else |
c22cacf3 | 4707 | word = bitpos / BITS_PER_WORD; |
e4fbead1 RS |
4708 | temp = operand_subword_force (temp, word, fmode); |
4709 | bitpos = bitpos % BITS_PER_WORD; | |
4710 | } | |
4711 | ||
210e1852 RS |
4712 | /* Force the intermediate word_mode (or narrower) result into a |
4713 | register. This avoids attempting to create paradoxical SUBREGs | |
4714 | of floating point modes below. */ | |
4715 | temp = force_reg (imode, temp); | |
4716 | ||
e4fbead1 RS |
4717 | /* If the bitpos is within the "result mode" lowpart, the operation |
4718 | can be implement with a single bitwise AND. Otherwise, we need | |
4719 | a right shift and an AND. */ | |
4720 | ||
4721 | if (bitpos < GET_MODE_BITSIZE (rmode)) | |
4722 | { | |
807e902e | 4723 | wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode)); |
ef79730c | 4724 | |
515e442a | 4725 | if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode)) |
e4fbead1 | 4726 | temp = gen_lowpart (rmode, temp); |
254878ea | 4727 | temp = expand_binop (rmode, and_optab, temp, |
807e902e | 4728 | immed_wide_int_const (mask, rmode), |
e4fbead1 | 4729 | NULL_RTX, 1, OPTAB_LIB_WIDEN); |
ef79730c | 4730 | } |
e4fbead1 RS |
4731 | else |
4732 | { | |
4733 | /* Perform a logical right shift to place the signbit in the least | |
c22cacf3 | 4734 | significant bit, then truncate the result to the desired mode |
e4fbead1 | 4735 | and mask just this bit. */ |
eb6c3df1 | 4736 | temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1); |
e4fbead1 RS |
4737 | temp = gen_lowpart (rmode, temp); |
4738 | temp = expand_binop (rmode, and_optab, temp, const1_rtx, | |
4739 | NULL_RTX, 1, OPTAB_LIB_WIDEN); | |
4740 | } | |
4741 | ||
ef79730c RS |
4742 | return temp; |
4743 | } | |
d1c38823 ZD |
4744 | |
4745 | /* Expand fork or exec calls. TARGET is the desired target of the | |
5039610b | 4746 | call. EXP is the call. FN is the |
d1c38823 ZD |
4747 | identificator of the actual function. IGNORE is nonzero if the |
4748 | value is to be ignored. */ | |
4749 | ||
4750 | static rtx | |
5039610b | 4751 | expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore) |
d1c38823 ZD |
4752 | { |
4753 | tree id, decl; | |
4754 | tree call; | |
4755 | ||
4756 | /* If we are not profiling, just call the function. */ | |
4757 | if (!profile_arc_flag) | |
4758 | return NULL_RTX; | |
4759 | ||
4760 | /* Otherwise call the wrapper. This should be equivalent for the rest of | |
4761 | compiler, so the code does not diverge, and the wrapper may run the | |
2b8a92de | 4762 | code necessary for keeping the profiling sane. */ |
d1c38823 ZD |
4763 | |
4764 | switch (DECL_FUNCTION_CODE (fn)) | |
4765 | { | |
4766 | case BUILT_IN_FORK: | |
4767 | id = get_identifier ("__gcov_fork"); | |
4768 | break; | |
4769 | ||
4770 | case BUILT_IN_EXECL: | |
4771 | id = get_identifier ("__gcov_execl"); | |
4772 | break; | |
4773 | ||
4774 | case BUILT_IN_EXECV: | |
4775 | id = get_identifier ("__gcov_execv"); | |
4776 | break; | |
4777 | ||
4778 | case BUILT_IN_EXECLP: | |
4779 | id = get_identifier ("__gcov_execlp"); | |
4780 | break; | |
4781 | ||
4782 | case BUILT_IN_EXECLE: | |
4783 | id = get_identifier ("__gcov_execle"); | |
4784 | break; | |
4785 | ||
4786 | case BUILT_IN_EXECVP: | |
4787 | id = get_identifier ("__gcov_execvp"); | |
4788 | break; | |
4789 | ||
4790 | case BUILT_IN_EXECVE: | |
4791 | id = get_identifier ("__gcov_execve"); | |
4792 | break; | |
4793 | ||
4794 | default: | |
298e6adc | 4795 | gcc_unreachable (); |
d1c38823 ZD |
4796 | } |
4797 | ||
c2255bc4 AH |
4798 | decl = build_decl (DECL_SOURCE_LOCATION (fn), |
4799 | FUNCTION_DECL, id, TREE_TYPE (fn)); | |
d1c38823 ZD |
4800 | DECL_EXTERNAL (decl) = 1; |
4801 | TREE_PUBLIC (decl) = 1; | |
4802 | DECL_ARTIFICIAL (decl) = 1; | |
4803 | TREE_NOTHROW (decl) = 1; | |
ac382b62 JM |
4804 | DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT; |
4805 | DECL_VISIBILITY_SPECIFIED (decl) = 1; | |
db3927fb | 4806 | call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0); |
d1c38823 | 4807 | return expand_call (call, target, ignore); |
5039610b | 4808 | } |
b8698a0f | 4809 | |
48ae6c13 RH |
4810 | |
4811 | \f | |
02ee605c RH |
4812 | /* Reconstitute a mode for a __sync intrinsic operation. Since the type of |
4813 | the pointer in these functions is void*, the tree optimizers may remove | |
4814 | casts. The mode computed in expand_builtin isn't reliable either, due | |
4815 | to __sync_bool_compare_and_swap. | |
4816 | ||
4817 | FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the | |
4818 | group of builtins. This gives us log2 of the mode size. */ | |
4819 | ||
ef4bddc2 | 4820 | static inline machine_mode |
02ee605c RH |
4821 | get_builtin_sync_mode (int fcode_diff) |
4822 | { | |
2de0aa52 HPN |
4823 | /* The size is not negotiable, so ask not to get BLKmode in return |
4824 | if the target indicates that a smaller size would be better. */ | |
4825 | return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0); | |
02ee605c RH |
4826 | } |
4827 | ||
1387fef3 AS |
4828 | /* Expand the memory expression LOC and return the appropriate memory operand |
4829 | for the builtin_sync operations. */ | |
4830 | ||
4831 | static rtx | |
ef4bddc2 | 4832 | get_builtin_sync_mem (tree loc, machine_mode mode) |
1387fef3 AS |
4833 | { |
4834 | rtx addr, mem; | |
4835 | ||
f46835f5 JJ |
4836 | addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM); |
4837 | addr = convert_memory_address (Pmode, addr); | |
1387fef3 AS |
4838 | |
4839 | /* Note that we explicitly do not want any alias information for this | |
4840 | memory, so that we kill all other live memories. Otherwise we don't | |
4841 | satisfy the full barrier semantics of the intrinsic. */ | |
4842 | mem = validize_mem (gen_rtx_MEM (mode, addr)); | |
4843 | ||
1be38ccb RG |
4844 | /* The alignment needs to be at least according to that of the mode. */ |
4845 | set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode), | |
0eb77834 | 4846 | get_pointer_alignment (loc))); |
9cd9e512 | 4847 | set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER); |
1387fef3 AS |
4848 | MEM_VOLATILE_P (mem) = 1; |
4849 | ||
4850 | return mem; | |
4851 | } | |
4852 | ||
86951993 AM |
4853 | /* Make sure an argument is in the right mode. |
4854 | EXP is the tree argument. | |
4855 | MODE is the mode it should be in. */ | |
4856 | ||
4857 | static rtx | |
ef4bddc2 | 4858 | expand_expr_force_mode (tree exp, machine_mode mode) |
86951993 AM |
4859 | { |
4860 | rtx val; | |
ef4bddc2 | 4861 | machine_mode old_mode; |
86951993 AM |
4862 | |
4863 | val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL); | |
4864 | /* If VAL is promoted to a wider mode, convert it back to MODE. Take care | |
4865 | of CONST_INTs, where we know the old_mode only from the call argument. */ | |
4866 | ||
4867 | old_mode = GET_MODE (val); | |
4868 | if (old_mode == VOIDmode) | |
4869 | old_mode = TYPE_MODE (TREE_TYPE (exp)); | |
4870 | val = convert_modes (mode, old_mode, val, 1); | |
4871 | return val; | |
4872 | } | |
4873 | ||
4874 | ||
48ae6c13 | 4875 | /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics. |
5039610b | 4876 | EXP is the CALL_EXPR. CODE is the rtx code |
48ae6c13 RH |
4877 | that corresponds to the arithmetic or logical operation from the name; |
4878 | an exception here is that NOT actually means NAND. TARGET is an optional | |
4879 | place for us to store the results; AFTER is true if this is the | |
86951993 | 4880 | fetch_and_xxx form. */ |
48ae6c13 RH |
4881 | |
4882 | static rtx | |
ef4bddc2 | 4883 | expand_builtin_sync_operation (machine_mode mode, tree exp, |
02ee605c | 4884 | enum rtx_code code, bool after, |
86951993 | 4885 | rtx target) |
48ae6c13 | 4886 | { |
1387fef3 | 4887 | rtx val, mem; |
c2255bc4 | 4888 | location_t loc = EXPR_LOCATION (exp); |
48ae6c13 | 4889 | |
23462d4d UB |
4890 | if (code == NOT && warn_sync_nand) |
4891 | { | |
4892 | tree fndecl = get_callee_fndecl (exp); | |
4893 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
4894 | ||
4895 | static bool warned_f_a_n, warned_n_a_f; | |
4896 | ||
4897 | switch (fcode) | |
4898 | { | |
e0a8ecf2 AM |
4899 | case BUILT_IN_SYNC_FETCH_AND_NAND_1: |
4900 | case BUILT_IN_SYNC_FETCH_AND_NAND_2: | |
4901 | case BUILT_IN_SYNC_FETCH_AND_NAND_4: | |
4902 | case BUILT_IN_SYNC_FETCH_AND_NAND_8: | |
4903 | case BUILT_IN_SYNC_FETCH_AND_NAND_16: | |
23462d4d UB |
4904 | if (warned_f_a_n) |
4905 | break; | |
4906 | ||
e79983f4 | 4907 | fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N); |
c2255bc4 | 4908 | inform (loc, "%qD changed semantics in GCC 4.4", fndecl); |
23462d4d UB |
4909 | warned_f_a_n = true; |
4910 | break; | |
4911 | ||
e0a8ecf2 AM |
4912 | case BUILT_IN_SYNC_NAND_AND_FETCH_1: |
4913 | case BUILT_IN_SYNC_NAND_AND_FETCH_2: | |
4914 | case BUILT_IN_SYNC_NAND_AND_FETCH_4: | |
4915 | case BUILT_IN_SYNC_NAND_AND_FETCH_8: | |
4916 | case BUILT_IN_SYNC_NAND_AND_FETCH_16: | |
23462d4d UB |
4917 | if (warned_n_a_f) |
4918 | break; | |
4919 | ||
e79983f4 | 4920 | fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N); |
c2255bc4 | 4921 | inform (loc, "%qD changed semantics in GCC 4.4", fndecl); |
23462d4d UB |
4922 | warned_n_a_f = true; |
4923 | break; | |
4924 | ||
4925 | default: | |
4926 | gcc_unreachable (); | |
4927 | } | |
4928 | } | |
4929 | ||
48ae6c13 | 4930 | /* Expand the operands. */ |
5039610b | 4931 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); |
86951993 | 4932 | val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode); |
48ae6c13 | 4933 | |
46b35980 | 4934 | return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST, |
86951993 | 4935 | after); |
48ae6c13 RH |
4936 | } |
4937 | ||
4938 | /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap | |
5039610b | 4939 | intrinsics. EXP is the CALL_EXPR. IS_BOOL is |
48ae6c13 RH |
4940 | true if this is the boolean form. TARGET is a place for us to store the |
4941 | results; this is NOT optional if IS_BOOL is true. */ | |
4942 | ||
4943 | static rtx | |
ef4bddc2 | 4944 | expand_builtin_compare_and_swap (machine_mode mode, tree exp, |
02ee605c | 4945 | bool is_bool, rtx target) |
48ae6c13 | 4946 | { |
1387fef3 | 4947 | rtx old_val, new_val, mem; |
f0409b19 | 4948 | rtx *pbool, *poval; |
48ae6c13 RH |
4949 | |
4950 | /* Expand the operands. */ | |
5039610b | 4951 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); |
86951993 AM |
4952 | old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode); |
4953 | new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode); | |
48ae6c13 | 4954 | |
f0409b19 RH |
4955 | pbool = poval = NULL; |
4956 | if (target != const0_rtx) | |
4957 | { | |
4958 | if (is_bool) | |
4959 | pbool = ⌖ | |
4960 | else | |
4961 | poval = ⌖ | |
4962 | } | |
4963 | if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val, | |
46b35980 AM |
4964 | false, MEMMODEL_SYNC_SEQ_CST, |
4965 | MEMMODEL_SYNC_SEQ_CST)) | |
86951993 | 4966 | return NULL_RTX; |
5039610b | 4967 | |
86951993 | 4968 | return target; |
48ae6c13 RH |
4969 | } |
4970 | ||
4971 | /* Expand the __sync_lock_test_and_set intrinsic. Note that the most | |
4972 | general form is actually an atomic exchange, and some targets only | |
4973 | support a reduced form with the second argument being a constant 1. | |
b8698a0f | 4974 | EXP is the CALL_EXPR; TARGET is an optional place for us to store |
5039610b | 4975 | the results. */ |
48ae6c13 RH |
4976 | |
4977 | static rtx | |
ef4bddc2 | 4978 | expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp, |
86951993 | 4979 | rtx target) |
48ae6c13 | 4980 | { |
1387fef3 | 4981 | rtx val, mem; |
48ae6c13 RH |
4982 | |
4983 | /* Expand the operands. */ | |
5039610b | 4984 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); |
86951993 AM |
4985 | val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode); |
4986 | ||
744accb2 | 4987 | return expand_sync_lock_test_and_set (target, mem, val); |
86951993 AM |
4988 | } |
4989 | ||
4990 | /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */ | |
4991 | ||
4992 | static void | |
ef4bddc2 | 4993 | expand_builtin_sync_lock_release (machine_mode mode, tree exp) |
86951993 AM |
4994 | { |
4995 | rtx mem; | |
4996 | ||
4997 | /* Expand the operands. */ | |
4998 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
4999 | ||
46b35980 | 5000 | expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true); |
86951993 AM |
5001 | } |
5002 | ||
5003 | /* Given an integer representing an ``enum memmodel'', verify its | |
5004 | correctness and return the memory model enum. */ | |
5005 | ||
5006 | static enum memmodel | |
5007 | get_memmodel (tree exp) | |
5008 | { | |
5009 | rtx op; | |
5dcfdccd | 5010 | unsigned HOST_WIDE_INT val; |
8d9fdb49 MP |
5011 | source_location loc |
5012 | = expansion_point_location_if_in_system_header (input_location); | |
86951993 AM |
5013 | |
5014 | /* If the parameter is not a constant, it's a run time value so we'll just | |
5015 | convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */ | |
5016 | if (TREE_CODE (exp) != INTEGER_CST) | |
5017 | return MEMMODEL_SEQ_CST; | |
5018 | ||
5019 | op = expand_normal (exp); | |
5dcfdccd KY |
5020 | |
5021 | val = INTVAL (op); | |
5022 | if (targetm.memmodel_check) | |
5023 | val = targetm.memmodel_check (val); | |
5024 | else if (val & ~MEMMODEL_MASK) | |
5025 | { | |
8d9fdb49 MP |
5026 | warning_at (loc, OPT_Winvalid_memory_model, |
5027 | "unknown architecture specifier in memory model to builtin"); | |
5dcfdccd KY |
5028 | return MEMMODEL_SEQ_CST; |
5029 | } | |
5030 | ||
46b35980 AM |
5031 | /* Should never see a user explicit SYNC memodel model, so >= LAST works. */ |
5032 | if (memmodel_base (val) >= MEMMODEL_LAST) | |
86951993 | 5033 | { |
8d9fdb49 MP |
5034 | warning_at (loc, OPT_Winvalid_memory_model, |
5035 | "invalid memory model argument to builtin"); | |
86951993 AM |
5036 | return MEMMODEL_SEQ_CST; |
5037 | } | |
5dcfdccd | 5038 | |
8673b671 AM |
5039 | /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so |
5040 | be conservative and promote consume to acquire. */ | |
5041 | if (val == MEMMODEL_CONSUME) | |
5042 | val = MEMMODEL_ACQUIRE; | |
5043 | ||
5dcfdccd | 5044 | return (enum memmodel) val; |
86951993 AM |
5045 | } |
5046 | ||
5047 | /* Expand the __atomic_exchange intrinsic: | |
5048 | TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel) | |
5049 | EXP is the CALL_EXPR. | |
5050 | TARGET is an optional place for us to store the results. */ | |
5051 | ||
5052 | static rtx | |
ef4bddc2 | 5053 | expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target) |
86951993 AM |
5054 | { |
5055 | rtx val, mem; | |
5056 | enum memmodel model; | |
5057 | ||
5058 | model = get_memmodel (CALL_EXPR_ARG (exp, 2)); | |
86951993 AM |
5059 | |
5060 | if (!flag_inline_atomics) | |
5061 | return NULL_RTX; | |
5062 | ||
5063 | /* Expand the operands. */ | |
5064 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
5065 | val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode); | |
5066 | ||
744accb2 | 5067 | return expand_atomic_exchange (target, mem, val, model); |
86951993 AM |
5068 | } |
5069 | ||
5070 | /* Expand the __atomic_compare_exchange intrinsic: | |
5071 | bool __atomic_compare_exchange (TYPE *object, TYPE *expect, | |
5072 | TYPE desired, BOOL weak, | |
5073 | enum memmodel success, | |
5074 | enum memmodel failure) | |
5075 | EXP is the CALL_EXPR. | |
5076 | TARGET is an optional place for us to store the results. */ | |
5077 | ||
5078 | static rtx | |
ef4bddc2 | 5079 | expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp, |
86951993 AM |
5080 | rtx target) |
5081 | { | |
58f4cf2a DM |
5082 | rtx expect, desired, mem, oldval; |
5083 | rtx_code_label *label; | |
86951993 AM |
5084 | enum memmodel success, failure; |
5085 | tree weak; | |
5086 | bool is_weak; | |
8d9fdb49 MP |
5087 | source_location loc |
5088 | = expansion_point_location_if_in_system_header (input_location); | |
86951993 AM |
5089 | |
5090 | success = get_memmodel (CALL_EXPR_ARG (exp, 4)); | |
5091 | failure = get_memmodel (CALL_EXPR_ARG (exp, 5)); | |
5092 | ||
77df5327 AM |
5093 | if (failure > success) |
5094 | { | |
8d9fdb49 MP |
5095 | warning_at (loc, OPT_Winvalid_memory_model, |
5096 | "failure memory model cannot be stronger than success " | |
5097 | "memory model for %<__atomic_compare_exchange%>"); | |
77df5327 AM |
5098 | success = MEMMODEL_SEQ_CST; |
5099 | } | |
5100 | ||
46b35980 | 5101 | if (is_mm_release (failure) || is_mm_acq_rel (failure)) |
86951993 | 5102 | { |
8d9fdb49 MP |
5103 | warning_at (loc, OPT_Winvalid_memory_model, |
5104 | "invalid failure memory model for " | |
5105 | "%<__atomic_compare_exchange%>"); | |
77df5327 AM |
5106 | failure = MEMMODEL_SEQ_CST; |
5107 | success = MEMMODEL_SEQ_CST; | |
86951993 AM |
5108 | } |
5109 | ||
77df5327 | 5110 | |
86951993 AM |
5111 | if (!flag_inline_atomics) |
5112 | return NULL_RTX; | |
5113 | ||
5114 | /* Expand the operands. */ | |
5115 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
5116 | ||
5117 | expect = expand_normal (CALL_EXPR_ARG (exp, 1)); | |
5118 | expect = convert_memory_address (Pmode, expect); | |
215770ad | 5119 | expect = gen_rtx_MEM (mode, expect); |
86951993 AM |
5120 | desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode); |
5121 | ||
5122 | weak = CALL_EXPR_ARG (exp, 3); | |
5123 | is_weak = false; | |
9439e9a1 | 5124 | if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0) |
86951993 AM |
5125 | is_weak = true; |
5126 | ||
672ce939 RH |
5127 | if (target == const0_rtx) |
5128 | target = NULL; | |
672ce939 | 5129 | |
2fdc29e8 RH |
5130 | /* Lest the rtl backend create a race condition with an imporoper store |
5131 | to memory, always create a new pseudo for OLDVAL. */ | |
5132 | oldval = NULL; | |
5133 | ||
5134 | if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired, | |
f0409b19 | 5135 | is_weak, success, failure)) |
86951993 AM |
5136 | return NULL_RTX; |
5137 | ||
672ce939 RH |
5138 | /* Conditionally store back to EXPECT, lest we create a race condition |
5139 | with an improper store to memory. */ | |
5140 | /* ??? With a rearrangement of atomics at the gimple level, we can handle | |
5141 | the normal case where EXPECT is totally private, i.e. a register. At | |
5142 | which point the store can be unconditional. */ | |
5143 | label = gen_label_rtx (); | |
f8940d4a JG |
5144 | emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL, |
5145 | GET_MODE (target), 1, label); | |
672ce939 RH |
5146 | emit_move_insn (expect, oldval); |
5147 | emit_label (label); | |
215770ad | 5148 | |
86951993 AM |
5149 | return target; |
5150 | } | |
5151 | ||
849a76a5 JJ |
5152 | /* Helper function for expand_ifn_atomic_compare_exchange - expand |
5153 | internal ATOMIC_COMPARE_EXCHANGE call into __atomic_compare_exchange_N | |
5154 | call. The weak parameter must be dropped to match the expected parameter | |
5155 | list and the expected argument changed from value to pointer to memory | |
5156 | slot. */ | |
5157 | ||
5158 | static void | |
5159 | expand_ifn_atomic_compare_exchange_into_call (gcall *call, machine_mode mode) | |
5160 | { | |
5161 | unsigned int z; | |
5162 | vec<tree, va_gc> *vec; | |
5163 | ||
5164 | vec_alloc (vec, 5); | |
5165 | vec->quick_push (gimple_call_arg (call, 0)); | |
5166 | tree expected = gimple_call_arg (call, 1); | |
5167 | rtx x = assign_stack_temp_for_type (mode, GET_MODE_SIZE (mode), | |
5168 | TREE_TYPE (expected)); | |
5169 | rtx expd = expand_expr (expected, x, mode, EXPAND_NORMAL); | |
5170 | if (expd != x) | |
5171 | emit_move_insn (x, expd); | |
5172 | tree v = make_tree (TREE_TYPE (expected), x); | |
5173 | vec->quick_push (build1 (ADDR_EXPR, | |
5174 | build_pointer_type (TREE_TYPE (expected)), v)); | |
5175 | vec->quick_push (gimple_call_arg (call, 2)); | |
5176 | /* Skip the boolean weak parameter. */ | |
5177 | for (z = 4; z < 6; z++) | |
5178 | vec->quick_push (gimple_call_arg (call, z)); | |
5179 | built_in_function fncode | |
5180 | = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1 | |
5181 | + exact_log2 (GET_MODE_SIZE (mode))); | |
5182 | tree fndecl = builtin_decl_explicit (fncode); | |
5183 | tree fn = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fndecl)), | |
5184 | fndecl); | |
5185 | tree exp = build_call_vec (boolean_type_node, fn, vec); | |
5186 | tree lhs = gimple_call_lhs (call); | |
5187 | rtx boolret = expand_call (exp, NULL_RTX, lhs == NULL_TREE); | |
5188 | if (lhs) | |
5189 | { | |
5190 | rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE); | |
5191 | if (GET_MODE (boolret) != mode) | |
5192 | boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1); | |
5193 | x = force_reg (mode, x); | |
5194 | write_complex_part (target, boolret, true); | |
5195 | write_complex_part (target, x, false); | |
5196 | } | |
5197 | } | |
5198 | ||
5199 | /* Expand IFN_ATOMIC_COMPARE_EXCHANGE internal function. */ | |
5200 | ||
5201 | void | |
5202 | expand_ifn_atomic_compare_exchange (gcall *call) | |
5203 | { | |
5204 | int size = tree_to_shwi (gimple_call_arg (call, 3)) & 255; | |
5205 | gcc_assert (size == 1 || size == 2 || size == 4 || size == 8 || size == 16); | |
5206 | machine_mode mode = mode_for_size (BITS_PER_UNIT * size, MODE_INT, 0); | |
5207 | rtx expect, desired, mem, oldval, boolret; | |
5208 | enum memmodel success, failure; | |
5209 | tree lhs; | |
5210 | bool is_weak; | |
5211 | source_location loc | |
5212 | = expansion_point_location_if_in_system_header (gimple_location (call)); | |
5213 | ||
5214 | success = get_memmodel (gimple_call_arg (call, 4)); | |
5215 | failure = get_memmodel (gimple_call_arg (call, 5)); | |
5216 | ||
5217 | if (failure > success) | |
5218 | { | |
5219 | warning_at (loc, OPT_Winvalid_memory_model, | |
5220 | "failure memory model cannot be stronger than success " | |
5221 | "memory model for %<__atomic_compare_exchange%>"); | |
5222 | success = MEMMODEL_SEQ_CST; | |
5223 | } | |
5224 | ||
5225 | if (is_mm_release (failure) || is_mm_acq_rel (failure)) | |
5226 | { | |
5227 | warning_at (loc, OPT_Winvalid_memory_model, | |
5228 | "invalid failure memory model for " | |
5229 | "%<__atomic_compare_exchange%>"); | |
5230 | failure = MEMMODEL_SEQ_CST; | |
5231 | success = MEMMODEL_SEQ_CST; | |
5232 | } | |
5233 | ||
5234 | if (!flag_inline_atomics) | |
5235 | { | |
5236 | expand_ifn_atomic_compare_exchange_into_call (call, mode); | |
5237 | return; | |
5238 | } | |
5239 | ||
5240 | /* Expand the operands. */ | |
5241 | mem = get_builtin_sync_mem (gimple_call_arg (call, 0), mode); | |
5242 | ||
5243 | expect = expand_expr_force_mode (gimple_call_arg (call, 1), mode); | |
5244 | desired = expand_expr_force_mode (gimple_call_arg (call, 2), mode); | |
5245 | ||
5246 | is_weak = (tree_to_shwi (gimple_call_arg (call, 3)) & 256) != 0; | |
5247 | ||
5248 | boolret = NULL; | |
5249 | oldval = NULL; | |
5250 | ||
5251 | if (!expand_atomic_compare_and_swap (&boolret, &oldval, mem, expect, desired, | |
5252 | is_weak, success, failure)) | |
5253 | { | |
5254 | expand_ifn_atomic_compare_exchange_into_call (call, mode); | |
5255 | return; | |
5256 | } | |
5257 | ||
5258 | lhs = gimple_call_lhs (call); | |
5259 | if (lhs) | |
5260 | { | |
5261 | rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE); | |
5262 | if (GET_MODE (boolret) != mode) | |
5263 | boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1); | |
5264 | write_complex_part (target, boolret, true); | |
5265 | write_complex_part (target, oldval, false); | |
5266 | } | |
5267 | } | |
5268 | ||
86951993 AM |
5269 | /* Expand the __atomic_load intrinsic: |
5270 | TYPE __atomic_load (TYPE *object, enum memmodel) | |
5271 | EXP is the CALL_EXPR. | |
5272 | TARGET is an optional place for us to store the results. */ | |
5273 | ||
5274 | static rtx | |
ef4bddc2 | 5275 | expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target) |
86951993 AM |
5276 | { |
5277 | rtx mem; | |
5278 | enum memmodel model; | |
5279 | ||
5280 | model = get_memmodel (CALL_EXPR_ARG (exp, 1)); | |
46b35980 | 5281 | if (is_mm_release (model) || is_mm_acq_rel (model)) |
86951993 | 5282 | { |
8d9fdb49 MP |
5283 | source_location loc |
5284 | = expansion_point_location_if_in_system_header (input_location); | |
5285 | warning_at (loc, OPT_Winvalid_memory_model, | |
5286 | "invalid memory model for %<__atomic_load%>"); | |
77df5327 | 5287 | model = MEMMODEL_SEQ_CST; |
86951993 AM |
5288 | } |
5289 | ||
5290 | if (!flag_inline_atomics) | |
5291 | return NULL_RTX; | |
5292 | ||
5293 | /* Expand the operand. */ | |
5294 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
5295 | ||
5296 | return expand_atomic_load (target, mem, model); | |
5297 | } | |
5298 | ||
5299 | ||
5300 | /* Expand the __atomic_store intrinsic: | |
5301 | void __atomic_store (TYPE *object, TYPE desired, enum memmodel) | |
5302 | EXP is the CALL_EXPR. | |
5303 | TARGET is an optional place for us to store the results. */ | |
5304 | ||
5305 | static rtx | |
ef4bddc2 | 5306 | expand_builtin_atomic_store (machine_mode mode, tree exp) |
86951993 AM |
5307 | { |
5308 | rtx mem, val; | |
5309 | enum memmodel model; | |
5310 | ||
5311 | model = get_memmodel (CALL_EXPR_ARG (exp, 2)); | |
46b35980 AM |
5312 | if (!(is_mm_relaxed (model) || is_mm_seq_cst (model) |
5313 | || is_mm_release (model))) | |
86951993 | 5314 | { |
8d9fdb49 MP |
5315 | source_location loc |
5316 | = expansion_point_location_if_in_system_header (input_location); | |
5317 | warning_at (loc, OPT_Winvalid_memory_model, | |
5318 | "invalid memory model for %<__atomic_store%>"); | |
77df5327 | 5319 | model = MEMMODEL_SEQ_CST; |
86951993 AM |
5320 | } |
5321 | ||
5322 | if (!flag_inline_atomics) | |
5323 | return NULL_RTX; | |
5324 | ||
5325 | /* Expand the operands. */ | |
5326 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
5327 | val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode); | |
5328 | ||
0669295b | 5329 | return expand_atomic_store (mem, val, model, false); |
86951993 AM |
5330 | } |
5331 | ||
5332 | /* Expand the __atomic_fetch_XXX intrinsic: | |
5333 | TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel) | |
5334 | EXP is the CALL_EXPR. | |
5335 | TARGET is an optional place for us to store the results. | |
5336 | CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR. | |
5337 | FETCH_AFTER is true if returning the result of the operation. | |
5338 | FETCH_AFTER is false if returning the value before the operation. | |
5339 | IGNORE is true if the result is not used. | |
5340 | EXT_CALL is the correct builtin for an external call if this cannot be | |
5341 | resolved to an instruction sequence. */ | |
5342 | ||
5343 | static rtx | |
ef4bddc2 | 5344 | expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target, |
86951993 AM |
5345 | enum rtx_code code, bool fetch_after, |
5346 | bool ignore, enum built_in_function ext_call) | |
5347 | { | |
5348 | rtx val, mem, ret; | |
5349 | enum memmodel model; | |
5350 | tree fndecl; | |
5351 | tree addr; | |
5352 | ||
5353 | model = get_memmodel (CALL_EXPR_ARG (exp, 2)); | |
5354 | ||
5355 | /* Expand the operands. */ | |
5356 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
5357 | val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode); | |
5358 | ||
5359 | /* Only try generating instructions if inlining is turned on. */ | |
5360 | if (flag_inline_atomics) | |
5361 | { | |
5362 | ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after); | |
5363 | if (ret) | |
5364 | return ret; | |
5365 | } | |
5366 | ||
5367 | /* Return if a different routine isn't needed for the library call. */ | |
5368 | if (ext_call == BUILT_IN_NONE) | |
5369 | return NULL_RTX; | |
5370 | ||
5371 | /* Change the call to the specified function. */ | |
5372 | fndecl = get_callee_fndecl (exp); | |
5373 | addr = CALL_EXPR_FN (exp); | |
5374 | STRIP_NOPS (addr); | |
5375 | ||
5376 | gcc_assert (TREE_OPERAND (addr, 0) == fndecl); | |
c3284718 | 5377 | TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call); |
86951993 AM |
5378 | |
5379 | /* Expand the call here so we can emit trailing code. */ | |
5380 | ret = expand_call (exp, target, ignore); | |
5381 | ||
5382 | /* Replace the original function just in case it matters. */ | |
5383 | TREE_OPERAND (addr, 0) = fndecl; | |
5384 | ||
5385 | /* Then issue the arithmetic correction to return the right result. */ | |
5386 | if (!ignore) | |
154b68db AM |
5387 | { |
5388 | if (code == NOT) | |
5389 | { | |
5390 | ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true, | |
5391 | OPTAB_LIB_WIDEN); | |
5392 | ret = expand_simple_unop (mode, NOT, ret, target, true); | |
5393 | } | |
5394 | else | |
5395 | ret = expand_simple_binop (mode, code, ret, val, target, true, | |
5396 | OPTAB_LIB_WIDEN); | |
5397 | } | |
86951993 AM |
5398 | return ret; |
5399 | } | |
5400 | ||
adedd5c1 JJ |
5401 | /* Expand IFN_ATOMIC_BIT_TEST_AND_* internal function. */ |
5402 | ||
5403 | void | |
5404 | expand_ifn_atomic_bit_test_and (gcall *call) | |
5405 | { | |
5406 | tree ptr = gimple_call_arg (call, 0); | |
5407 | tree bit = gimple_call_arg (call, 1); | |
5408 | tree flag = gimple_call_arg (call, 2); | |
5409 | tree lhs = gimple_call_lhs (call); | |
5410 | enum memmodel model = MEMMODEL_SYNC_SEQ_CST; | |
5411 | machine_mode mode = TYPE_MODE (TREE_TYPE (flag)); | |
5412 | enum rtx_code code; | |
5413 | optab optab; | |
5414 | struct expand_operand ops[5]; | |
5415 | ||
5416 | gcc_assert (flag_inline_atomics); | |
5417 | ||
5418 | if (gimple_call_num_args (call) == 4) | |
5419 | model = get_memmodel (gimple_call_arg (call, 3)); | |
5420 | ||
5421 | rtx mem = get_builtin_sync_mem (ptr, mode); | |
5422 | rtx val = expand_expr_force_mode (bit, mode); | |
5423 | ||
5424 | switch (gimple_call_internal_fn (call)) | |
5425 | { | |
5426 | case IFN_ATOMIC_BIT_TEST_AND_SET: | |
5427 | code = IOR; | |
5428 | optab = atomic_bit_test_and_set_optab; | |
5429 | break; | |
5430 | case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT: | |
5431 | code = XOR; | |
5432 | optab = atomic_bit_test_and_complement_optab; | |
5433 | break; | |
5434 | case IFN_ATOMIC_BIT_TEST_AND_RESET: | |
5435 | code = AND; | |
5436 | optab = atomic_bit_test_and_reset_optab; | |
5437 | break; | |
5438 | default: | |
5439 | gcc_unreachable (); | |
5440 | } | |
5441 | ||
5442 | if (lhs == NULL_TREE) | |
5443 | { | |
5444 | val = expand_simple_binop (mode, ASHIFT, const1_rtx, | |
5445 | val, NULL_RTX, true, OPTAB_DIRECT); | |
5446 | if (code == AND) | |
5447 | val = expand_simple_unop (mode, NOT, val, NULL_RTX, true); | |
5448 | expand_atomic_fetch_op (const0_rtx, mem, val, code, model, false); | |
5449 | return; | |
5450 | } | |
5451 | ||
5452 | rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE); | |
5453 | enum insn_code icode = direct_optab_handler (optab, mode); | |
5454 | gcc_assert (icode != CODE_FOR_nothing); | |
5455 | create_output_operand (&ops[0], target, mode); | |
5456 | create_fixed_operand (&ops[1], mem); | |
5457 | create_convert_operand_to (&ops[2], val, mode, true); | |
5458 | create_integer_operand (&ops[3], model); | |
5459 | create_integer_operand (&ops[4], integer_onep (flag)); | |
5460 | if (maybe_expand_insn (icode, 5, ops)) | |
5461 | return; | |
5462 | ||
5463 | rtx bitval = val; | |
5464 | val = expand_simple_binop (mode, ASHIFT, const1_rtx, | |
5465 | val, NULL_RTX, true, OPTAB_DIRECT); | |
5466 | rtx maskval = val; | |
5467 | if (code == AND) | |
5468 | val = expand_simple_unop (mode, NOT, val, NULL_RTX, true); | |
5469 | rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, val, | |
5470 | code, model, false); | |
5471 | if (integer_onep (flag)) | |
5472 | { | |
5473 | result = expand_simple_binop (mode, ASHIFTRT, result, bitval, | |
5474 | NULL_RTX, true, OPTAB_DIRECT); | |
5475 | result = expand_simple_binop (mode, AND, result, const1_rtx, target, | |
5476 | true, OPTAB_DIRECT); | |
5477 | } | |
5478 | else | |
5479 | result = expand_simple_binop (mode, AND, result, maskval, target, true, | |
5480 | OPTAB_DIRECT); | |
5481 | if (result != target) | |
5482 | emit_move_insn (target, result); | |
5483 | } | |
5484 | ||
d660c35e AM |
5485 | /* Expand an atomic clear operation. |
5486 | void _atomic_clear (BOOL *obj, enum memmodel) | |
5487 | EXP is the call expression. */ | |
5488 | ||
5489 | static rtx | |
5490 | expand_builtin_atomic_clear (tree exp) | |
5491 | { | |
ef4bddc2 | 5492 | machine_mode mode; |
d660c35e AM |
5493 | rtx mem, ret; |
5494 | enum memmodel model; | |
5495 | ||
5496 | mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0); | |
5497 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
5498 | model = get_memmodel (CALL_EXPR_ARG (exp, 1)); | |
5499 | ||
46b35980 | 5500 | if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model)) |
d660c35e | 5501 | { |
8d9fdb49 MP |
5502 | source_location loc |
5503 | = expansion_point_location_if_in_system_header (input_location); | |
5504 | warning_at (loc, OPT_Winvalid_memory_model, | |
5505 | "invalid memory model for %<__atomic_store%>"); | |
77df5327 | 5506 | model = MEMMODEL_SEQ_CST; |
d660c35e AM |
5507 | } |
5508 | ||
5509 | /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release. | |
5510 | Failing that, a store is issued by __atomic_store. The only way this can | |
5511 | fail is if the bool type is larger than a word size. Unlikely, but | |
5512 | handle it anyway for completeness. Assume a single threaded model since | |
5513 | there is no atomic support in this case, and no barriers are required. */ | |
5514 | ret = expand_atomic_store (mem, const0_rtx, model, true); | |
5515 | if (!ret) | |
5516 | emit_move_insn (mem, const0_rtx); | |
5517 | return const0_rtx; | |
5518 | } | |
5519 | ||
5520 | /* Expand an atomic test_and_set operation. | |
5521 | bool _atomic_test_and_set (BOOL *obj, enum memmodel) | |
5522 | EXP is the call expression. */ | |
5523 | ||
5524 | static rtx | |
744accb2 | 5525 | expand_builtin_atomic_test_and_set (tree exp, rtx target) |
d660c35e | 5526 | { |
744accb2 | 5527 | rtx mem; |
d660c35e | 5528 | enum memmodel model; |
ef4bddc2 | 5529 | machine_mode mode; |
d660c35e AM |
5530 | |
5531 | mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0); | |
5532 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); | |
5533 | model = get_memmodel (CALL_EXPR_ARG (exp, 1)); | |
5534 | ||
744accb2 | 5535 | return expand_atomic_test_and_set (target, mem, model); |
d660c35e AM |
5536 | } |
5537 | ||
5538 | ||
86951993 AM |
5539 | /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on |
5540 | this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */ | |
5541 | ||
5542 | static tree | |
5543 | fold_builtin_atomic_always_lock_free (tree arg0, tree arg1) | |
5544 | { | |
5545 | int size; | |
ef4bddc2 | 5546 | machine_mode mode; |
86951993 AM |
5547 | unsigned int mode_align, type_align; |
5548 | ||
5549 | if (TREE_CODE (arg0) != INTEGER_CST) | |
5550 | return NULL_TREE; | |
48ae6c13 | 5551 | |
86951993 AM |
5552 | size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT; |
5553 | mode = mode_for_size (size, MODE_INT, 0); | |
5554 | mode_align = GET_MODE_ALIGNMENT (mode); | |
5555 | ||
310055e7 JW |
5556 | if (TREE_CODE (arg1) == INTEGER_CST) |
5557 | { | |
5558 | unsigned HOST_WIDE_INT val = UINTVAL (expand_normal (arg1)); | |
5559 | ||
5560 | /* Either this argument is null, or it's a fake pointer encoding | |
5561 | the alignment of the object. */ | |
146ec50f | 5562 | val = least_bit_hwi (val); |
310055e7 JW |
5563 | val *= BITS_PER_UNIT; |
5564 | ||
5565 | if (val == 0 || mode_align < val) | |
5566 | type_align = mode_align; | |
5567 | else | |
5568 | type_align = val; | |
5569 | } | |
86951993 AM |
5570 | else |
5571 | { | |
5572 | tree ttype = TREE_TYPE (arg1); | |
5573 | ||
5574 | /* This function is usually invoked and folded immediately by the front | |
5575 | end before anything else has a chance to look at it. The pointer | |
5576 | parameter at this point is usually cast to a void *, so check for that | |
5577 | and look past the cast. */ | |
625a9766 | 5578 | if (CONVERT_EXPR_P (arg1) && POINTER_TYPE_P (ttype) |
86951993 AM |
5579 | && VOID_TYPE_P (TREE_TYPE (ttype))) |
5580 | arg1 = TREE_OPERAND (arg1, 0); | |
5581 | ||
5582 | ttype = TREE_TYPE (arg1); | |
5583 | gcc_assert (POINTER_TYPE_P (ttype)); | |
5584 | ||
5585 | /* Get the underlying type of the object. */ | |
5586 | ttype = TREE_TYPE (ttype); | |
5587 | type_align = TYPE_ALIGN (ttype); | |
5588 | } | |
5589 | ||
026c3cfd | 5590 | /* If the object has smaller alignment, the lock free routines cannot |
86951993 AM |
5591 | be used. */ |
5592 | if (type_align < mode_align) | |
58d38fd2 | 5593 | return boolean_false_node; |
86951993 AM |
5594 | |
5595 | /* Check if a compare_and_swap pattern exists for the mode which represents | |
5596 | the required size. The pattern is not allowed to fail, so the existence | |
5597 | of the pattern indicates support is present. */ | |
cedb4a1a | 5598 | if (can_compare_and_swap_p (mode, true)) |
58d38fd2 | 5599 | return boolean_true_node; |
86951993 | 5600 | else |
58d38fd2 | 5601 | return boolean_false_node; |
86951993 AM |
5602 | } |
5603 | ||
5604 | /* Return true if the parameters to call EXP represent an object which will | |
5605 | always generate lock free instructions. The first argument represents the | |
5606 | size of the object, and the second parameter is a pointer to the object | |
5607 | itself. If NULL is passed for the object, then the result is based on | |
5608 | typical alignment for an object of the specified size. Otherwise return | |
5609 | false. */ | |
5610 | ||
5611 | static rtx | |
5612 | expand_builtin_atomic_always_lock_free (tree exp) | |
5613 | { | |
5614 | tree size; | |
5615 | tree arg0 = CALL_EXPR_ARG (exp, 0); | |
5616 | tree arg1 = CALL_EXPR_ARG (exp, 1); | |
5617 | ||
5618 | if (TREE_CODE (arg0) != INTEGER_CST) | |
5619 | { | |
5620 | error ("non-constant argument 1 to __atomic_always_lock_free"); | |
5621 | return const0_rtx; | |
5622 | } | |
5623 | ||
5624 | size = fold_builtin_atomic_always_lock_free (arg0, arg1); | |
58d38fd2 | 5625 | if (size == boolean_true_node) |
86951993 AM |
5626 | return const1_rtx; |
5627 | return const0_rtx; | |
5628 | } | |
5629 | ||
5630 | /* Return a one or zero if it can be determined that object ARG1 of size ARG | |
5631 | is lock free on this architecture. */ | |
5632 | ||
5633 | static tree | |
5634 | fold_builtin_atomic_is_lock_free (tree arg0, tree arg1) | |
5635 | { | |
5636 | if (!flag_inline_atomics) | |
5637 | return NULL_TREE; | |
5638 | ||
5639 | /* If it isn't always lock free, don't generate a result. */ | |
58d38fd2 JJ |
5640 | if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node) |
5641 | return boolean_true_node; | |
86951993 AM |
5642 | |
5643 | return NULL_TREE; | |
5644 | } | |
5645 | ||
5646 | /* Return true if the parameters to call EXP represent an object which will | |
5647 | always generate lock free instructions. The first argument represents the | |
5648 | size of the object, and the second parameter is a pointer to the object | |
5649 | itself. If NULL is passed for the object, then the result is based on | |
5650 | typical alignment for an object of the specified size. Otherwise return | |
5651 | NULL*/ | |
5652 | ||
5653 | static rtx | |
5654 | expand_builtin_atomic_is_lock_free (tree exp) | |
5655 | { | |
5656 | tree size; | |
5657 | tree arg0 = CALL_EXPR_ARG (exp, 0); | |
5658 | tree arg1 = CALL_EXPR_ARG (exp, 1); | |
5659 | ||
5660 | if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0))) | |
5661 | { | |
5662 | error ("non-integer argument 1 to __atomic_is_lock_free"); | |
5663 | return NULL_RTX; | |
5664 | } | |
5665 | ||
5666 | if (!flag_inline_atomics) | |
5667 | return NULL_RTX; | |
5668 | ||
5669 | /* If the value is known at compile time, return the RTX for it. */ | |
5670 | size = fold_builtin_atomic_is_lock_free (arg0, arg1); | |
58d38fd2 | 5671 | if (size == boolean_true_node) |
86951993 AM |
5672 | return const1_rtx; |
5673 | ||
5674 | return NULL_RTX; | |
5675 | } | |
5676 | ||
86951993 AM |
5677 | /* Expand the __atomic_thread_fence intrinsic: |
5678 | void __atomic_thread_fence (enum memmodel) | |
5679 | EXP is the CALL_EXPR. */ | |
5680 | ||
5681 | static void | |
5682 | expand_builtin_atomic_thread_fence (tree exp) | |
5683 | { | |
c39169c8 RH |
5684 | enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0)); |
5685 | expand_mem_thread_fence (model); | |
86951993 AM |
5686 | } |
5687 | ||
5688 | /* Expand the __atomic_signal_fence intrinsic: | |
5689 | void __atomic_signal_fence (enum memmodel) | |
5690 | EXP is the CALL_EXPR. */ | |
5691 | ||
5692 | static void | |
5693 | expand_builtin_atomic_signal_fence (tree exp) | |
5694 | { | |
c39169c8 RH |
5695 | enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0)); |
5696 | expand_mem_signal_fence (model); | |
48ae6c13 RH |
5697 | } |
5698 | ||
5699 | /* Expand the __sync_synchronize intrinsic. */ | |
5700 | ||
5701 | static void | |
e0a8ecf2 | 5702 | expand_builtin_sync_synchronize (void) |
48ae6c13 | 5703 | { |
46b35980 | 5704 | expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST); |
48ae6c13 RH |
5705 | } |
5706 | ||
f959607b CLT |
5707 | static rtx |
5708 | expand_builtin_thread_pointer (tree exp, rtx target) | |
5709 | { | |
5710 | enum insn_code icode; | |
5711 | if (!validate_arglist (exp, VOID_TYPE)) | |
5712 | return const0_rtx; | |
5713 | icode = direct_optab_handler (get_thread_pointer_optab, Pmode); | |
5714 | if (icode != CODE_FOR_nothing) | |
5715 | { | |
5716 | struct expand_operand op; | |
b8a542c6 AP |
5717 | /* If the target is not sutitable then create a new target. */ |
5718 | if (target == NULL_RTX | |
5719 | || !REG_P (target) | |
5720 | || GET_MODE (target) != Pmode) | |
f959607b CLT |
5721 | target = gen_reg_rtx (Pmode); |
5722 | create_output_operand (&op, target, Pmode); | |
5723 | expand_insn (icode, 1, &op); | |
5724 | return target; | |
5725 | } | |
5726 | error ("__builtin_thread_pointer is not supported on this target"); | |
5727 | return const0_rtx; | |
5728 | } | |
5729 | ||
5730 | static void | |
5731 | expand_builtin_set_thread_pointer (tree exp) | |
5732 | { | |
5733 | enum insn_code icode; | |
5734 | if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) | |
5735 | return; | |
5736 | icode = direct_optab_handler (set_thread_pointer_optab, Pmode); | |
5737 | if (icode != CODE_FOR_nothing) | |
5738 | { | |
5739 | struct expand_operand op; | |
5740 | rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX, | |
5741 | Pmode, EXPAND_NORMAL); | |
5440a1b0 | 5742 | create_input_operand (&op, val, Pmode); |
f959607b CLT |
5743 | expand_insn (icode, 1, &op); |
5744 | return; | |
5745 | } | |
5746 | error ("__builtin_set_thread_pointer is not supported on this target"); | |
5747 | } | |
5748 | ||
28f4ec01 | 5749 | \f |
862d0b35 DN |
5750 | /* Emit code to restore the current value of stack. */ |
5751 | ||
5752 | static void | |
5753 | expand_stack_restore (tree var) | |
5754 | { | |
58f4cf2a DM |
5755 | rtx_insn *prev; |
5756 | rtx sa = expand_normal (var); | |
862d0b35 DN |
5757 | |
5758 | sa = convert_memory_address (Pmode, sa); | |
5759 | ||
5760 | prev = get_last_insn (); | |
5761 | emit_stack_restore (SAVE_BLOCK, sa); | |
d33606c3 EB |
5762 | |
5763 | record_new_stack_level (); | |
5764 | ||
862d0b35 DN |
5765 | fixup_args_size_notes (prev, get_last_insn (), 0); |
5766 | } | |
5767 | ||
862d0b35 DN |
5768 | /* Emit code to save the current value of stack. */ |
5769 | ||
5770 | static rtx | |
5771 | expand_stack_save (void) | |
5772 | { | |
5773 | rtx ret = NULL_RTX; | |
5774 | ||
862d0b35 DN |
5775 | emit_stack_save (SAVE_BLOCK, &ret); |
5776 | return ret; | |
5777 | } | |
5778 | ||
41dbbb37 | 5779 | |
28f4ec01 BS |
5780 | /* Expand an expression EXP that calls a built-in function, |
5781 | with result going to TARGET if that's convenient | |
5782 | (and in mode MODE if that's convenient). | |
5783 | SUBTARGET may be used as the target for computing one of EXP's operands. | |
5784 | IGNORE is nonzero if the value is to be ignored. */ | |
5785 | ||
5786 | rtx | |
ef4bddc2 | 5787 | expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode, |
4682ae04 | 5788 | int ignore) |
28f4ec01 | 5789 | { |
2f503025 | 5790 | tree fndecl = get_callee_fndecl (exp); |
28f4ec01 | 5791 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); |
ef4bddc2 | 5792 | machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp)); |
9e3920e9 | 5793 | int flags; |
28f4ec01 | 5794 | |
d51151b2 JJ |
5795 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) |
5796 | return targetm.expand_builtin (exp, target, subtarget, mode, ignore); | |
5797 | ||
bdea98ca MO |
5798 | /* When ASan is enabled, we don't want to expand some memory/string |
5799 | builtins and rely on libsanitizer's hooks. This allows us to avoid | |
5800 | redundant checks and be sure, that possible overflow will be detected | |
5801 | by ASan. */ | |
5802 | ||
5803 | if ((flag_sanitize & SANITIZE_ADDRESS) && asan_intercepted_p (fcode)) | |
5804 | return expand_call (exp, target, ignore); | |
5805 | ||
28f4ec01 BS |
5806 | /* When not optimizing, generate calls to library functions for a certain |
5807 | set of builtins. */ | |
d25225de | 5808 | if (!optimize |
48ae6c13 | 5809 | && !called_as_built_in (fndecl) |
63bf9a90 JH |
5810 | && fcode != BUILT_IN_FORK |
5811 | && fcode != BUILT_IN_EXECL | |
5812 | && fcode != BUILT_IN_EXECV | |
5813 | && fcode != BUILT_IN_EXECLP | |
5814 | && fcode != BUILT_IN_EXECLE | |
5815 | && fcode != BUILT_IN_EXECVP | |
5816 | && fcode != BUILT_IN_EXECVE | |
f9555f40 | 5817 | && fcode != BUILT_IN_ALLOCA |
13e49da9 | 5818 | && fcode != BUILT_IN_ALLOCA_WITH_ALIGN |
d5e254e1 IE |
5819 | && fcode != BUILT_IN_FREE |
5820 | && fcode != BUILT_IN_CHKP_SET_PTR_BOUNDS | |
5821 | && fcode != BUILT_IN_CHKP_INIT_PTR_BOUNDS | |
5822 | && fcode != BUILT_IN_CHKP_NULL_PTR_BOUNDS | |
5823 | && fcode != BUILT_IN_CHKP_COPY_PTR_BOUNDS | |
5824 | && fcode != BUILT_IN_CHKP_NARROW_PTR_BOUNDS | |
5825 | && fcode != BUILT_IN_CHKP_STORE_PTR_BOUNDS | |
5826 | && fcode != BUILT_IN_CHKP_CHECK_PTR_LBOUNDS | |
5827 | && fcode != BUILT_IN_CHKP_CHECK_PTR_UBOUNDS | |
5828 | && fcode != BUILT_IN_CHKP_CHECK_PTR_BOUNDS | |
5829 | && fcode != BUILT_IN_CHKP_GET_PTR_LBOUND | |
5830 | && fcode != BUILT_IN_CHKP_GET_PTR_UBOUND | |
5831 | && fcode != BUILT_IN_CHKP_BNDRET) | |
d25225de | 5832 | return expand_call (exp, target, ignore); |
28f4ec01 | 5833 | |
0a45ec5c RS |
5834 | /* The built-in function expanders test for target == const0_rtx |
5835 | to determine whether the function's result will be ignored. */ | |
5836 | if (ignore) | |
5837 | target = const0_rtx; | |
5838 | ||
5839 | /* If the result of a pure or const built-in function is ignored, and | |
5840 | none of its arguments are volatile, we can avoid expanding the | |
5841 | built-in call and just evaluate the arguments for side-effects. */ | |
5842 | if (target == const0_rtx | |
9e3920e9 JJ |
5843 | && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE)) |
5844 | && !(flags & ECF_LOOPING_CONST_OR_PURE)) | |
0a45ec5c RS |
5845 | { |
5846 | bool volatilep = false; | |
5847 | tree arg; | |
5039610b | 5848 | call_expr_arg_iterator iter; |
0a45ec5c | 5849 | |
5039610b SL |
5850 | FOR_EACH_CALL_EXPR_ARG (arg, iter, exp) |
5851 | if (TREE_THIS_VOLATILE (arg)) | |
0a45ec5c RS |
5852 | { |
5853 | volatilep = true; | |
5854 | break; | |
5855 | } | |
5856 | ||
5857 | if (! volatilep) | |
5858 | { | |
5039610b SL |
5859 | FOR_EACH_CALL_EXPR_ARG (arg, iter, exp) |
5860 | expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
0a45ec5c RS |
5861 | return const0_rtx; |
5862 | } | |
5863 | } | |
5864 | ||
edcf72f3 IE |
5865 | /* expand_builtin_with_bounds is supposed to be used for |
5866 | instrumented builtin calls. */ | |
d5e254e1 IE |
5867 | gcc_assert (!CALL_WITH_BOUNDS_P (exp)); |
5868 | ||
28f4ec01 BS |
5869 | switch (fcode) |
5870 | { | |
ea6a6627 | 5871 | CASE_FLT_FN (BUILT_IN_FABS): |
6dc198e3 | 5872 | CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS): |
e2323f5b PB |
5873 | case BUILT_IN_FABSD32: |
5874 | case BUILT_IN_FABSD64: | |
5875 | case BUILT_IN_FABSD128: | |
5039610b | 5876 | target = expand_builtin_fabs (exp, target, subtarget); |
075ec276 | 5877 | if (target) |
c22cacf3 | 5878 | return target; |
075ec276 RS |
5879 | break; |
5880 | ||
ea6a6627 | 5881 | CASE_FLT_FN (BUILT_IN_COPYSIGN): |
6dc198e3 | 5882 | CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN): |
5039610b | 5883 | target = expand_builtin_copysign (exp, target, subtarget); |
046625fa RH |
5884 | if (target) |
5885 | return target; | |
5886 | break; | |
5887 | ||
5906d013 EC |
5888 | /* Just do a normal library call if we were unable to fold |
5889 | the values. */ | |
ea6a6627 | 5890 | CASE_FLT_FN (BUILT_IN_CABS): |
075ec276 | 5891 | break; |
28f4ec01 | 5892 | |
1b1562a5 MM |
5893 | CASE_FLT_FN (BUILT_IN_FMA): |
5894 | target = expand_builtin_mathfn_ternary (exp, target, subtarget); | |
5895 | if (target) | |
5896 | return target; | |
5897 | break; | |
5898 | ||
eaee4464 UB |
5899 | CASE_FLT_FN (BUILT_IN_ILOGB): |
5900 | if (! flag_unsafe_math_optimizations) | |
5901 | break; | |
9ed4207f | 5902 | CASE_FLT_FN (BUILT_IN_ISINF): |
0c8d3c2b KG |
5903 | CASE_FLT_FN (BUILT_IN_FINITE): |
5904 | case BUILT_IN_ISFINITE: | |
8a91c45b | 5905 | case BUILT_IN_ISNORMAL: |
4359dc2a | 5906 | target = expand_builtin_interclass_mathfn (exp, target); |
eaee4464 UB |
5907 | if (target) |
5908 | return target; | |
5909 | break; | |
5910 | ||
6c32ee74 | 5911 | CASE_FLT_FN (BUILT_IN_ICEIL): |
ea6a6627 VR |
5912 | CASE_FLT_FN (BUILT_IN_LCEIL): |
5913 | CASE_FLT_FN (BUILT_IN_LLCEIL): | |
5914 | CASE_FLT_FN (BUILT_IN_LFLOOR): | |
6c32ee74 | 5915 | CASE_FLT_FN (BUILT_IN_IFLOOR): |
ea6a6627 | 5916 | CASE_FLT_FN (BUILT_IN_LLFLOOR): |
1856c8dc | 5917 | target = expand_builtin_int_roundingfn (exp, target); |
d8b42d06 UB |
5918 | if (target) |
5919 | return target; | |
5920 | break; | |
5921 | ||
6c32ee74 | 5922 | CASE_FLT_FN (BUILT_IN_IRINT): |
0bfa1541 RG |
5923 | CASE_FLT_FN (BUILT_IN_LRINT): |
5924 | CASE_FLT_FN (BUILT_IN_LLRINT): | |
6c32ee74 | 5925 | CASE_FLT_FN (BUILT_IN_IROUND): |
4d81bf84 RG |
5926 | CASE_FLT_FN (BUILT_IN_LROUND): |
5927 | CASE_FLT_FN (BUILT_IN_LLROUND): | |
1856c8dc | 5928 | target = expand_builtin_int_roundingfn_2 (exp, target); |
0bfa1541 RG |
5929 | if (target) |
5930 | return target; | |
5931 | break; | |
5932 | ||
ea6a6627 | 5933 | CASE_FLT_FN (BUILT_IN_POWI): |
4359dc2a | 5934 | target = expand_builtin_powi (exp, target); |
17684d46 RG |
5935 | if (target) |
5936 | return target; | |
5937 | break; | |
5938 | ||
75c7c595 | 5939 | CASE_FLT_FN (BUILT_IN_CEXPI): |
4359dc2a | 5940 | target = expand_builtin_cexpi (exp, target); |
75c7c595 RG |
5941 | gcc_assert (target); |
5942 | return target; | |
5943 | ||
ea6a6627 VR |
5944 | CASE_FLT_FN (BUILT_IN_SIN): |
5945 | CASE_FLT_FN (BUILT_IN_COS): | |
6c7cf1f0 UB |
5946 | if (! flag_unsafe_math_optimizations) |
5947 | break; | |
5948 | target = expand_builtin_mathfn_3 (exp, target, subtarget); | |
5949 | if (target) | |
5950 | return target; | |
5951 | break; | |
5952 | ||
403e54f0 RG |
5953 | CASE_FLT_FN (BUILT_IN_SINCOS): |
5954 | if (! flag_unsafe_math_optimizations) | |
5955 | break; | |
5956 | target = expand_builtin_sincos (exp); | |
5957 | if (target) | |
5958 | return target; | |
5959 | break; | |
5960 | ||
28f4ec01 BS |
5961 | case BUILT_IN_APPLY_ARGS: |
5962 | return expand_builtin_apply_args (); | |
5963 | ||
5964 | /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes | |
5965 | FUNCTION with a copy of the parameters described by | |
5966 | ARGUMENTS, and ARGSIZE. It returns a block of memory | |
5967 | allocated on the stack into which is stored all the registers | |
5968 | that might possibly be used for returning the result of a | |
5969 | function. ARGUMENTS is the value returned by | |
5970 | __builtin_apply_args. ARGSIZE is the number of bytes of | |
5971 | arguments that must be copied. ??? How should this value be | |
5972 | computed? We'll also need a safe worst case value for varargs | |
5973 | functions. */ | |
5974 | case BUILT_IN_APPLY: | |
5039610b | 5975 | if (!validate_arglist (exp, POINTER_TYPE, |
019fa094 | 5976 | POINTER_TYPE, INTEGER_TYPE, VOID_TYPE) |
5039610b | 5977 | && !validate_arglist (exp, REFERENCE_TYPE, |
019fa094 | 5978 | POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
28f4ec01 BS |
5979 | return const0_rtx; |
5980 | else | |
5981 | { | |
28f4ec01 BS |
5982 | rtx ops[3]; |
5983 | ||
5039610b SL |
5984 | ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0)); |
5985 | ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1)); | |
5986 | ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2)); | |
28f4ec01 BS |
5987 | |
5988 | return expand_builtin_apply (ops[0], ops[1], ops[2]); | |
5989 | } | |
5990 | ||
5991 | /* __builtin_return (RESULT) causes the function to return the | |
5992 | value described by RESULT. RESULT is address of the block of | |
5993 | memory returned by __builtin_apply. */ | |
5994 | case BUILT_IN_RETURN: | |
5039610b SL |
5995 | if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
5996 | expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0))); | |
28f4ec01 BS |
5997 | return const0_rtx; |
5998 | ||
5999 | case BUILT_IN_SAVEREGS: | |
d3707adb | 6000 | return expand_builtin_saveregs (); |
28f4ec01 | 6001 | |
6ef5231b JJ |
6002 | case BUILT_IN_VA_ARG_PACK: |
6003 | /* All valid uses of __builtin_va_arg_pack () are removed during | |
6004 | inlining. */ | |
c94ed7a1 | 6005 | error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp); |
6ef5231b JJ |
6006 | return const0_rtx; |
6007 | ||
ab0e176c JJ |
6008 | case BUILT_IN_VA_ARG_PACK_LEN: |
6009 | /* All valid uses of __builtin_va_arg_pack_len () are removed during | |
6010 | inlining. */ | |
c94ed7a1 | 6011 | error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp); |
ab0e176c JJ |
6012 | return const0_rtx; |
6013 | ||
28f4ec01 BS |
6014 | /* Return the address of the first anonymous stack arg. */ |
6015 | case BUILT_IN_NEXT_ARG: | |
5039610b | 6016 | if (fold_builtin_next_arg (exp, false)) |
c22cacf3 | 6017 | return const0_rtx; |
8870e212 | 6018 | return expand_builtin_next_arg (); |
28f4ec01 | 6019 | |
677feb77 DD |
6020 | case BUILT_IN_CLEAR_CACHE: |
6021 | target = expand_builtin___clear_cache (exp); | |
6022 | if (target) | |
6023 | return target; | |
6024 | break; | |
6025 | ||
28f4ec01 | 6026 | case BUILT_IN_CLASSIFY_TYPE: |
5039610b | 6027 | return expand_builtin_classify_type (exp); |
28f4ec01 BS |
6028 | |
6029 | case BUILT_IN_CONSTANT_P: | |
6de9cd9a | 6030 | return const0_rtx; |
28f4ec01 BS |
6031 | |
6032 | case BUILT_IN_FRAME_ADDRESS: | |
6033 | case BUILT_IN_RETURN_ADDRESS: | |
5039610b | 6034 | return expand_builtin_frame_address (fndecl, exp); |
28f4ec01 BS |
6035 | |
6036 | /* Returns the address of the area where the structure is returned. | |
6037 | 0 otherwise. */ | |
6038 | case BUILT_IN_AGGREGATE_INCOMING_ADDRESS: | |
5039610b | 6039 | if (call_expr_nargs (exp) != 0 |
ca7fd9cd | 6040 | || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))) |
3c0cb5de | 6041 | || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl)))) |
ca7fd9cd | 6042 | return const0_rtx; |
28f4ec01 | 6043 | else |
ca7fd9cd | 6044 | return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0); |
28f4ec01 BS |
6045 | |
6046 | case BUILT_IN_ALLOCA: | |
13e49da9 | 6047 | case BUILT_IN_ALLOCA_WITH_ALIGN: |
d3c12306 EB |
6048 | /* If the allocation stems from the declaration of a variable-sized |
6049 | object, it cannot accumulate. */ | |
63d2a353 | 6050 | target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp)); |
28f4ec01 BS |
6051 | if (target) |
6052 | return target; | |
6053 | break; | |
6054 | ||
6de9cd9a DN |
6055 | case BUILT_IN_STACK_SAVE: |
6056 | return expand_stack_save (); | |
6057 | ||
6058 | case BUILT_IN_STACK_RESTORE: | |
5039610b | 6059 | expand_stack_restore (CALL_EXPR_ARG (exp, 0)); |
6de9cd9a DN |
6060 | return const0_rtx; |
6061 | ||
ac868f29 | 6062 | case BUILT_IN_BSWAP16: |
167fa32c EC |
6063 | case BUILT_IN_BSWAP32: |
6064 | case BUILT_IN_BSWAP64: | |
ac868f29 | 6065 | target = expand_builtin_bswap (target_mode, exp, target, subtarget); |
167fa32c EC |
6066 | if (target) |
6067 | return target; | |
6068 | break; | |
6069 | ||
ea6a6627 | 6070 | CASE_INT_FN (BUILT_IN_FFS): |
5039610b | 6071 | target = expand_builtin_unop (target_mode, exp, target, |
6c537d03 | 6072 | subtarget, ffs_optab); |
2928cd7a RH |
6073 | if (target) |
6074 | return target; | |
6075 | break; | |
6076 | ||
ea6a6627 | 6077 | CASE_INT_FN (BUILT_IN_CLZ): |
5039610b | 6078 | target = expand_builtin_unop (target_mode, exp, target, |
6c537d03 | 6079 | subtarget, clz_optab); |
2928cd7a RH |
6080 | if (target) |
6081 | return target; | |
6082 | break; | |
6083 | ||
ea6a6627 | 6084 | CASE_INT_FN (BUILT_IN_CTZ): |
5039610b | 6085 | target = expand_builtin_unop (target_mode, exp, target, |
6c537d03 | 6086 | subtarget, ctz_optab); |
2928cd7a RH |
6087 | if (target) |
6088 | return target; | |
6089 | break; | |
6090 | ||
3801c801 | 6091 | CASE_INT_FN (BUILT_IN_CLRSB): |
3801c801 BS |
6092 | target = expand_builtin_unop (target_mode, exp, target, |
6093 | subtarget, clrsb_optab); | |
6094 | if (target) | |
6095 | return target; | |
6096 | break; | |
6097 | ||
ea6a6627 | 6098 | CASE_INT_FN (BUILT_IN_POPCOUNT): |
5039610b | 6099 | target = expand_builtin_unop (target_mode, exp, target, |
6c537d03 | 6100 | subtarget, popcount_optab); |
2928cd7a RH |
6101 | if (target) |
6102 | return target; | |
6103 | break; | |
6104 | ||
ea6a6627 | 6105 | CASE_INT_FN (BUILT_IN_PARITY): |
5039610b | 6106 | target = expand_builtin_unop (target_mode, exp, target, |
6c537d03 | 6107 | subtarget, parity_optab); |
28f4ec01 BS |
6108 | if (target) |
6109 | return target; | |
6110 | break; | |
6111 | ||
6112 | case BUILT_IN_STRLEN: | |
5039610b | 6113 | target = expand_builtin_strlen (exp, target, target_mode); |
28f4ec01 BS |
6114 | if (target) |
6115 | return target; | |
6116 | break; | |
6117 | ||
6118 | case BUILT_IN_STRCPY: | |
44e10129 | 6119 | target = expand_builtin_strcpy (exp, target); |
28f4ec01 BS |
6120 | if (target) |
6121 | return target; | |
6122 | break; | |
8d51ecf8 | 6123 | |
da9e9f08 | 6124 | case BUILT_IN_STRNCPY: |
44e10129 | 6125 | target = expand_builtin_strncpy (exp, target); |
da9e9f08 KG |
6126 | if (target) |
6127 | return target; | |
6128 | break; | |
8d51ecf8 | 6129 | |
9cb65f92 | 6130 | case BUILT_IN_STPCPY: |
609ae0e2 | 6131 | target = expand_builtin_stpcpy (exp, target, mode); |
9cb65f92 KG |
6132 | if (target) |
6133 | return target; | |
6134 | break; | |
6135 | ||
28f4ec01 | 6136 | case BUILT_IN_MEMCPY: |
44e10129 | 6137 | target = expand_builtin_memcpy (exp, target); |
9cb65f92 KG |
6138 | if (target) |
6139 | return target; | |
6140 | break; | |
6141 | ||
6142 | case BUILT_IN_MEMPCPY: | |
5039610b | 6143 | target = expand_builtin_mempcpy (exp, target, mode); |
28f4ec01 BS |
6144 | if (target) |
6145 | return target; | |
6146 | break; | |
6147 | ||
6148 | case BUILT_IN_MEMSET: | |
5039610b | 6149 | target = expand_builtin_memset (exp, target, mode); |
28f4ec01 BS |
6150 | if (target) |
6151 | return target; | |
6152 | break; | |
6153 | ||
e3a709be | 6154 | case BUILT_IN_BZERO: |
8148fe65 | 6155 | target = expand_builtin_bzero (exp); |
e3a709be KG |
6156 | if (target) |
6157 | return target; | |
6158 | break; | |
6159 | ||
28f4ec01 | 6160 | case BUILT_IN_STRCMP: |
44e10129 | 6161 | target = expand_builtin_strcmp (exp, target); |
28f4ec01 BS |
6162 | if (target) |
6163 | return target; | |
6164 | break; | |
6165 | ||
da9e9f08 KG |
6166 | case BUILT_IN_STRNCMP: |
6167 | target = expand_builtin_strncmp (exp, target, mode); | |
6168 | if (target) | |
6169 | return target; | |
6170 | break; | |
6171 | ||
4b2a62db | 6172 | case BUILT_IN_BCMP: |
28f4ec01 | 6173 | case BUILT_IN_MEMCMP: |
36b85e43 BS |
6174 | case BUILT_IN_MEMCMP_EQ: |
6175 | target = expand_builtin_memcmp (exp, target, fcode == BUILT_IN_MEMCMP_EQ); | |
28f4ec01 BS |
6176 | if (target) |
6177 | return target; | |
36b85e43 BS |
6178 | if (fcode == BUILT_IN_MEMCMP_EQ) |
6179 | { | |
6180 | tree newdecl = builtin_decl_explicit (BUILT_IN_MEMCMP); | |
6181 | TREE_OPERAND (exp, 1) = build_fold_addr_expr (newdecl); | |
6182 | } | |
28f4ec01 | 6183 | break; |
28f4ec01 BS |
6184 | |
6185 | case BUILT_IN_SETJMP: | |
4f6c2131 EB |
6186 | /* This should have been lowered to the builtins below. */ |
6187 | gcc_unreachable (); | |
6188 | ||
6189 | case BUILT_IN_SETJMP_SETUP: | |
6190 | /* __builtin_setjmp_setup is passed a pointer to an array of five words | |
6191 | and the receiver label. */ | |
5039610b | 6192 | if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
4f6c2131 | 6193 | { |
5039610b | 6194 | rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget, |
4f6c2131 | 6195 | VOIDmode, EXPAND_NORMAL); |
5039610b | 6196 | tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0); |
e67d1102 | 6197 | rtx_insn *label_r = label_rtx (label); |
4f6c2131 EB |
6198 | |
6199 | /* This is copied from the handling of non-local gotos. */ | |
6200 | expand_builtin_setjmp_setup (buf_addr, label_r); | |
6201 | nonlocal_goto_handler_labels | |
b5241a5a | 6202 | = gen_rtx_INSN_LIST (VOIDmode, label_r, |
4f6c2131 EB |
6203 | nonlocal_goto_handler_labels); |
6204 | /* ??? Do not let expand_label treat us as such since we would | |
6205 | not want to be both on the list of non-local labels and on | |
6206 | the list of forced labels. */ | |
6207 | FORCED_LABEL (label) = 0; | |
6208 | return const0_rtx; | |
6209 | } | |
6210 | break; | |
6211 | ||
4f6c2131 EB |
6212 | case BUILT_IN_SETJMP_RECEIVER: |
6213 | /* __builtin_setjmp_receiver is passed the receiver label. */ | |
5039610b | 6214 | if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
4f6c2131 | 6215 | { |
5039610b | 6216 | tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0); |
e67d1102 | 6217 | rtx_insn *label_r = label_rtx (label); |
4f6c2131 EB |
6218 | |
6219 | expand_builtin_setjmp_receiver (label_r); | |
6220 | return const0_rtx; | |
6221 | } | |
250d07b6 | 6222 | break; |
28f4ec01 BS |
6223 | |
6224 | /* __builtin_longjmp is passed a pointer to an array of five words. | |
6225 | It's similar to the C library longjmp function but works with | |
6226 | __builtin_setjmp above. */ | |
6227 | case BUILT_IN_LONGJMP: | |
5039610b | 6228 | if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
28f4ec01 | 6229 | { |
5039610b | 6230 | rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget, |
84217346 | 6231 | VOIDmode, EXPAND_NORMAL); |
5039610b | 6232 | rtx value = expand_normal (CALL_EXPR_ARG (exp, 1)); |
28f4ec01 BS |
6233 | |
6234 | if (value != const1_rtx) | |
6235 | { | |
9e637a26 | 6236 | error ("%<__builtin_longjmp%> second argument must be 1"); |
28f4ec01 BS |
6237 | return const0_rtx; |
6238 | } | |
6239 | ||
6240 | expand_builtin_longjmp (buf_addr, value); | |
6241 | return const0_rtx; | |
6242 | } | |
4f6c2131 | 6243 | break; |
28f4ec01 | 6244 | |
6de9cd9a | 6245 | case BUILT_IN_NONLOCAL_GOTO: |
5039610b | 6246 | target = expand_builtin_nonlocal_goto (exp); |
6de9cd9a DN |
6247 | if (target) |
6248 | return target; | |
6249 | break; | |
6250 | ||
2b92e7f5 RK |
6251 | /* This updates the setjmp buffer that is its argument with the value |
6252 | of the current stack pointer. */ | |
6253 | case BUILT_IN_UPDATE_SETJMP_BUF: | |
5039610b | 6254 | if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
2b92e7f5 RK |
6255 | { |
6256 | rtx buf_addr | |
5039610b | 6257 | = expand_normal (CALL_EXPR_ARG (exp, 0)); |
2b92e7f5 RK |
6258 | |
6259 | expand_builtin_update_setjmp_buf (buf_addr); | |
6260 | return const0_rtx; | |
6261 | } | |
6262 | break; | |
6263 | ||
28f4ec01 | 6264 | case BUILT_IN_TRAP: |
9602f5a0 | 6265 | expand_builtin_trap (); |
28f4ec01 BS |
6266 | return const0_rtx; |
6267 | ||
468059bc DD |
6268 | case BUILT_IN_UNREACHABLE: |
6269 | expand_builtin_unreachable (); | |
6270 | return const0_rtx; | |
6271 | ||
ea6a6627 | 6272 | CASE_FLT_FN (BUILT_IN_SIGNBIT): |
44aea9ac JJ |
6273 | case BUILT_IN_SIGNBITD32: |
6274 | case BUILT_IN_SIGNBITD64: | |
6275 | case BUILT_IN_SIGNBITD128: | |
ef79730c RS |
6276 | target = expand_builtin_signbit (exp, target); |
6277 | if (target) | |
6278 | return target; | |
6279 | break; | |
6280 | ||
28f4ec01 BS |
6281 | /* Various hooks for the DWARF 2 __throw routine. */ |
6282 | case BUILT_IN_UNWIND_INIT: | |
6283 | expand_builtin_unwind_init (); | |
6284 | return const0_rtx; | |
6285 | case BUILT_IN_DWARF_CFA: | |
6286 | return virtual_cfa_rtx; | |
6287 | #ifdef DWARF2_UNWIND_INFO | |
9c80ff25 RH |
6288 | case BUILT_IN_DWARF_SP_COLUMN: |
6289 | return expand_builtin_dwarf_sp_column (); | |
d9d5c9de | 6290 | case BUILT_IN_INIT_DWARF_REG_SIZES: |
5039610b | 6291 | expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0)); |
d9d5c9de | 6292 | return const0_rtx; |
28f4ec01 BS |
6293 | #endif |
6294 | case BUILT_IN_FROB_RETURN_ADDR: | |
5039610b | 6295 | return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0)); |
28f4ec01 | 6296 | case BUILT_IN_EXTRACT_RETURN_ADDR: |
5039610b | 6297 | return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0)); |
28f4ec01 | 6298 | case BUILT_IN_EH_RETURN: |
5039610b SL |
6299 | expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0), |
6300 | CALL_EXPR_ARG (exp, 1)); | |
28f4ec01 | 6301 | return const0_rtx; |
52a11cbf | 6302 | case BUILT_IN_EH_RETURN_DATA_REGNO: |
5039610b | 6303 | return expand_builtin_eh_return_data_regno (exp); |
c76362b4 | 6304 | case BUILT_IN_EXTEND_POINTER: |
5039610b | 6305 | return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0)); |
1d65f45c RH |
6306 | case BUILT_IN_EH_POINTER: |
6307 | return expand_builtin_eh_pointer (exp); | |
6308 | case BUILT_IN_EH_FILTER: | |
6309 | return expand_builtin_eh_filter (exp); | |
6310 | case BUILT_IN_EH_COPY_VALUES: | |
6311 | return expand_builtin_eh_copy_values (exp); | |
c76362b4 | 6312 | |
6c535c69 | 6313 | case BUILT_IN_VA_START: |
5039610b | 6314 | return expand_builtin_va_start (exp); |
d3707adb | 6315 | case BUILT_IN_VA_END: |
5039610b | 6316 | return expand_builtin_va_end (exp); |
d3707adb | 6317 | case BUILT_IN_VA_COPY: |
5039610b | 6318 | return expand_builtin_va_copy (exp); |
994a57cd | 6319 | case BUILT_IN_EXPECT: |
5039610b | 6320 | return expand_builtin_expect (exp, target); |
45d439ac JJ |
6321 | case BUILT_IN_ASSUME_ALIGNED: |
6322 | return expand_builtin_assume_aligned (exp, target); | |
a9ccbb60 | 6323 | case BUILT_IN_PREFETCH: |
5039610b | 6324 | expand_builtin_prefetch (exp); |
a9ccbb60 JJ |
6325 | return const0_rtx; |
6326 | ||
6de9cd9a | 6327 | case BUILT_IN_INIT_TRAMPOLINE: |
183dd130 ILT |
6328 | return expand_builtin_init_trampoline (exp, true); |
6329 | case BUILT_IN_INIT_HEAP_TRAMPOLINE: | |
6330 | return expand_builtin_init_trampoline (exp, false); | |
6de9cd9a | 6331 | case BUILT_IN_ADJUST_TRAMPOLINE: |
5039610b | 6332 | return expand_builtin_adjust_trampoline (exp); |
6de9cd9a | 6333 | |
d1c38823 ZD |
6334 | case BUILT_IN_FORK: |
6335 | case BUILT_IN_EXECL: | |
6336 | case BUILT_IN_EXECV: | |
6337 | case BUILT_IN_EXECLP: | |
6338 | case BUILT_IN_EXECLE: | |
6339 | case BUILT_IN_EXECVP: | |
6340 | case BUILT_IN_EXECVE: | |
5039610b | 6341 | target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore); |
d1c38823 ZD |
6342 | if (target) |
6343 | return target; | |
6344 | break; | |
28f4ec01 | 6345 | |
e0a8ecf2 AM |
6346 | case BUILT_IN_SYNC_FETCH_AND_ADD_1: |
6347 | case BUILT_IN_SYNC_FETCH_AND_ADD_2: | |
6348 | case BUILT_IN_SYNC_FETCH_AND_ADD_4: | |
6349 | case BUILT_IN_SYNC_FETCH_AND_ADD_8: | |
6350 | case BUILT_IN_SYNC_FETCH_AND_ADD_16: | |
6351 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1); | |
86951993 | 6352 | target = expand_builtin_sync_operation (mode, exp, PLUS, false, target); |
48ae6c13 RH |
6353 | if (target) |
6354 | return target; | |
6355 | break; | |
6356 | ||
e0a8ecf2 AM |
6357 | case BUILT_IN_SYNC_FETCH_AND_SUB_1: |
6358 | case BUILT_IN_SYNC_FETCH_AND_SUB_2: | |
6359 | case BUILT_IN_SYNC_FETCH_AND_SUB_4: | |
6360 | case BUILT_IN_SYNC_FETCH_AND_SUB_8: | |
6361 | case BUILT_IN_SYNC_FETCH_AND_SUB_16: | |
6362 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1); | |
86951993 | 6363 | target = expand_builtin_sync_operation (mode, exp, MINUS, false, target); |
48ae6c13 RH |
6364 | if (target) |
6365 | return target; | |
6366 | break; | |
6367 | ||
e0a8ecf2 AM |
6368 | case BUILT_IN_SYNC_FETCH_AND_OR_1: |
6369 | case BUILT_IN_SYNC_FETCH_AND_OR_2: | |
6370 | case BUILT_IN_SYNC_FETCH_AND_OR_4: | |
6371 | case BUILT_IN_SYNC_FETCH_AND_OR_8: | |
6372 | case BUILT_IN_SYNC_FETCH_AND_OR_16: | |
6373 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1); | |
86951993 | 6374 | target = expand_builtin_sync_operation (mode, exp, IOR, false, target); |
48ae6c13 RH |
6375 | if (target) |
6376 | return target; | |
6377 | break; | |
6378 | ||
e0a8ecf2 AM |
6379 | case BUILT_IN_SYNC_FETCH_AND_AND_1: |
6380 | case BUILT_IN_SYNC_FETCH_AND_AND_2: | |
6381 | case BUILT_IN_SYNC_FETCH_AND_AND_4: | |
6382 | case BUILT_IN_SYNC_FETCH_AND_AND_8: | |
6383 | case BUILT_IN_SYNC_FETCH_AND_AND_16: | |
6384 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1); | |
86951993 | 6385 | target = expand_builtin_sync_operation (mode, exp, AND, false, target); |
48ae6c13 RH |
6386 | if (target) |
6387 | return target; | |
6388 | break; | |
6389 | ||
e0a8ecf2 AM |
6390 | case BUILT_IN_SYNC_FETCH_AND_XOR_1: |
6391 | case BUILT_IN_SYNC_FETCH_AND_XOR_2: | |
6392 | case BUILT_IN_SYNC_FETCH_AND_XOR_4: | |
6393 | case BUILT_IN_SYNC_FETCH_AND_XOR_8: | |
6394 | case BUILT_IN_SYNC_FETCH_AND_XOR_16: | |
6395 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1); | |
86951993 | 6396 | target = expand_builtin_sync_operation (mode, exp, XOR, false, target); |
48ae6c13 RH |
6397 | if (target) |
6398 | return target; | |
6399 | break; | |
6400 | ||
e0a8ecf2 AM |
6401 | case BUILT_IN_SYNC_FETCH_AND_NAND_1: |
6402 | case BUILT_IN_SYNC_FETCH_AND_NAND_2: | |
6403 | case BUILT_IN_SYNC_FETCH_AND_NAND_4: | |
6404 | case BUILT_IN_SYNC_FETCH_AND_NAND_8: | |
6405 | case BUILT_IN_SYNC_FETCH_AND_NAND_16: | |
6406 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1); | |
86951993 | 6407 | target = expand_builtin_sync_operation (mode, exp, NOT, false, target); |
48ae6c13 RH |
6408 | if (target) |
6409 | return target; | |
6410 | break; | |
6411 | ||
e0a8ecf2 AM |
6412 | case BUILT_IN_SYNC_ADD_AND_FETCH_1: |
6413 | case BUILT_IN_SYNC_ADD_AND_FETCH_2: | |
6414 | case BUILT_IN_SYNC_ADD_AND_FETCH_4: | |
6415 | case BUILT_IN_SYNC_ADD_AND_FETCH_8: | |
6416 | case BUILT_IN_SYNC_ADD_AND_FETCH_16: | |
6417 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1); | |
86951993 | 6418 | target = expand_builtin_sync_operation (mode, exp, PLUS, true, target); |
48ae6c13 RH |
6419 | if (target) |
6420 | return target; | |
6421 | break; | |
6422 | ||
e0a8ecf2 AM |
6423 | case BUILT_IN_SYNC_SUB_AND_FETCH_1: |
6424 | case BUILT_IN_SYNC_SUB_AND_FETCH_2: | |
6425 | case BUILT_IN_SYNC_SUB_AND_FETCH_4: | |
6426 | case BUILT_IN_SYNC_SUB_AND_FETCH_8: | |
6427 | case BUILT_IN_SYNC_SUB_AND_FETCH_16: | |
6428 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1); | |
86951993 | 6429 | target = expand_builtin_sync_operation (mode, exp, MINUS, true, target); |
48ae6c13 RH |
6430 | if (target) |
6431 | return target; | |
6432 | break; | |
6433 | ||
e0a8ecf2 AM |
6434 | case BUILT_IN_SYNC_OR_AND_FETCH_1: |
6435 | case BUILT_IN_SYNC_OR_AND_FETCH_2: | |
6436 | case BUILT_IN_SYNC_OR_AND_FETCH_4: | |
6437 | case BUILT_IN_SYNC_OR_AND_FETCH_8: | |
6438 | case BUILT_IN_SYNC_OR_AND_FETCH_16: | |
6439 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1); | |
86951993 | 6440 | target = expand_builtin_sync_operation (mode, exp, IOR, true, target); |
48ae6c13 RH |
6441 | if (target) |
6442 | return target; | |
6443 | break; | |
6444 | ||
e0a8ecf2 AM |
6445 | case BUILT_IN_SYNC_AND_AND_FETCH_1: |
6446 | case BUILT_IN_SYNC_AND_AND_FETCH_2: | |
6447 | case BUILT_IN_SYNC_AND_AND_FETCH_4: | |
6448 | case BUILT_IN_SYNC_AND_AND_FETCH_8: | |
6449 | case BUILT_IN_SYNC_AND_AND_FETCH_16: | |
6450 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1); | |
86951993 | 6451 | target = expand_builtin_sync_operation (mode, exp, AND, true, target); |
48ae6c13 RH |
6452 | if (target) |
6453 | return target; | |
6454 | break; | |
6455 | ||
e0a8ecf2 AM |
6456 | case BUILT_IN_SYNC_XOR_AND_FETCH_1: |
6457 | case BUILT_IN_SYNC_XOR_AND_FETCH_2: | |
6458 | case BUILT_IN_SYNC_XOR_AND_FETCH_4: | |
6459 | case BUILT_IN_SYNC_XOR_AND_FETCH_8: | |
6460 | case BUILT_IN_SYNC_XOR_AND_FETCH_16: | |
6461 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1); | |
86951993 | 6462 | target = expand_builtin_sync_operation (mode, exp, XOR, true, target); |
48ae6c13 RH |
6463 | if (target) |
6464 | return target; | |
6465 | break; | |
6466 | ||
e0a8ecf2 AM |
6467 | case BUILT_IN_SYNC_NAND_AND_FETCH_1: |
6468 | case BUILT_IN_SYNC_NAND_AND_FETCH_2: | |
6469 | case BUILT_IN_SYNC_NAND_AND_FETCH_4: | |
6470 | case BUILT_IN_SYNC_NAND_AND_FETCH_8: | |
6471 | case BUILT_IN_SYNC_NAND_AND_FETCH_16: | |
6472 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1); | |
86951993 | 6473 | target = expand_builtin_sync_operation (mode, exp, NOT, true, target); |
48ae6c13 RH |
6474 | if (target) |
6475 | return target; | |
6476 | break; | |
6477 | ||
e0a8ecf2 AM |
6478 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1: |
6479 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2: | |
6480 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4: | |
6481 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8: | |
6482 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16: | |
5b5513d0 RH |
6483 | if (mode == VOIDmode) |
6484 | mode = TYPE_MODE (boolean_type_node); | |
48ae6c13 RH |
6485 | if (!target || !register_operand (target, mode)) |
6486 | target = gen_reg_rtx (mode); | |
02ee605c | 6487 | |
e0a8ecf2 AM |
6488 | mode = get_builtin_sync_mode |
6489 | (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1); | |
5039610b | 6490 | target = expand_builtin_compare_and_swap (mode, exp, true, target); |
48ae6c13 RH |
6491 | if (target) |
6492 | return target; | |
6493 | break; | |
6494 | ||
e0a8ecf2 AM |
6495 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1: |
6496 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2: | |
6497 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4: | |
6498 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8: | |
6499 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16: | |
6500 | mode = get_builtin_sync_mode | |
6501 | (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1); | |
5039610b | 6502 | target = expand_builtin_compare_and_swap (mode, exp, false, target); |
48ae6c13 RH |
6503 | if (target) |
6504 | return target; | |
6505 | break; | |
6506 | ||
e0a8ecf2 AM |
6507 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1: |
6508 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2: | |
6509 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4: | |
6510 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8: | |
6511 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16: | |
6512 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1); | |
6513 | target = expand_builtin_sync_lock_test_and_set (mode, exp, target); | |
48ae6c13 RH |
6514 | if (target) |
6515 | return target; | |
6516 | break; | |
6517 | ||
e0a8ecf2 AM |
6518 | case BUILT_IN_SYNC_LOCK_RELEASE_1: |
6519 | case BUILT_IN_SYNC_LOCK_RELEASE_2: | |
6520 | case BUILT_IN_SYNC_LOCK_RELEASE_4: | |
6521 | case BUILT_IN_SYNC_LOCK_RELEASE_8: | |
6522 | case BUILT_IN_SYNC_LOCK_RELEASE_16: | |
6523 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1); | |
6524 | expand_builtin_sync_lock_release (mode, exp); | |
48ae6c13 RH |
6525 | return const0_rtx; |
6526 | ||
e0a8ecf2 AM |
6527 | case BUILT_IN_SYNC_SYNCHRONIZE: |
6528 | expand_builtin_sync_synchronize (); | |
48ae6c13 RH |
6529 | return const0_rtx; |
6530 | ||
86951993 AM |
6531 | case BUILT_IN_ATOMIC_EXCHANGE_1: |
6532 | case BUILT_IN_ATOMIC_EXCHANGE_2: | |
6533 | case BUILT_IN_ATOMIC_EXCHANGE_4: | |
6534 | case BUILT_IN_ATOMIC_EXCHANGE_8: | |
6535 | case BUILT_IN_ATOMIC_EXCHANGE_16: | |
6536 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1); | |
6537 | target = expand_builtin_atomic_exchange (mode, exp, target); | |
6538 | if (target) | |
6539 | return target; | |
6540 | break; | |
6541 | ||
6542 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1: | |
6543 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2: | |
6544 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4: | |
6545 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8: | |
6546 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16: | |
e351ae85 AM |
6547 | { |
6548 | unsigned int nargs, z; | |
9771b263 | 6549 | vec<tree, va_gc> *vec; |
e351ae85 AM |
6550 | |
6551 | mode = | |
6552 | get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1); | |
6553 | target = expand_builtin_atomic_compare_exchange (mode, exp, target); | |
6554 | if (target) | |
6555 | return target; | |
6556 | ||
6557 | /* If this is turned into an external library call, the weak parameter | |
6558 | must be dropped to match the expected parameter list. */ | |
6559 | nargs = call_expr_nargs (exp); | |
9771b263 | 6560 | vec_alloc (vec, nargs - 1); |
e351ae85 | 6561 | for (z = 0; z < 3; z++) |
9771b263 | 6562 | vec->quick_push (CALL_EXPR_ARG (exp, z)); |
e351ae85 AM |
6563 | /* Skip the boolean weak parameter. */ |
6564 | for (z = 4; z < 6; z++) | |
9771b263 | 6565 | vec->quick_push (CALL_EXPR_ARG (exp, z)); |
e351ae85 AM |
6566 | exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec); |
6567 | break; | |
6568 | } | |
86951993 AM |
6569 | |
6570 | case BUILT_IN_ATOMIC_LOAD_1: | |
6571 | case BUILT_IN_ATOMIC_LOAD_2: | |
6572 | case BUILT_IN_ATOMIC_LOAD_4: | |
6573 | case BUILT_IN_ATOMIC_LOAD_8: | |
6574 | case BUILT_IN_ATOMIC_LOAD_16: | |
6575 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1); | |
6576 | target = expand_builtin_atomic_load (mode, exp, target); | |
6577 | if (target) | |
6578 | return target; | |
6579 | break; | |
6580 | ||
6581 | case BUILT_IN_ATOMIC_STORE_1: | |
6582 | case BUILT_IN_ATOMIC_STORE_2: | |
6583 | case BUILT_IN_ATOMIC_STORE_4: | |
6584 | case BUILT_IN_ATOMIC_STORE_8: | |
6585 | case BUILT_IN_ATOMIC_STORE_16: | |
6586 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1); | |
6587 | target = expand_builtin_atomic_store (mode, exp); | |
6588 | if (target) | |
6589 | return const0_rtx; | |
6590 | break; | |
6591 | ||
6592 | case BUILT_IN_ATOMIC_ADD_FETCH_1: | |
6593 | case BUILT_IN_ATOMIC_ADD_FETCH_2: | |
6594 | case BUILT_IN_ATOMIC_ADD_FETCH_4: | |
6595 | case BUILT_IN_ATOMIC_ADD_FETCH_8: | |
6596 | case BUILT_IN_ATOMIC_ADD_FETCH_16: | |
6597 | { | |
6598 | enum built_in_function lib; | |
6599 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1); | |
6600 | lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 + | |
6601 | (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1)); | |
6602 | target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true, | |
6603 | ignore, lib); | |
6604 | if (target) | |
6605 | return target; | |
6606 | break; | |
6607 | } | |
6608 | case BUILT_IN_ATOMIC_SUB_FETCH_1: | |
6609 | case BUILT_IN_ATOMIC_SUB_FETCH_2: | |
6610 | case BUILT_IN_ATOMIC_SUB_FETCH_4: | |
6611 | case BUILT_IN_ATOMIC_SUB_FETCH_8: | |
6612 | case BUILT_IN_ATOMIC_SUB_FETCH_16: | |
6613 | { | |
6614 | enum built_in_function lib; | |
6615 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1); | |
6616 | lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 + | |
6617 | (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1)); | |
6618 | target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true, | |
6619 | ignore, lib); | |
6620 | if (target) | |
6621 | return target; | |
6622 | break; | |
6623 | } | |
6624 | case BUILT_IN_ATOMIC_AND_FETCH_1: | |
6625 | case BUILT_IN_ATOMIC_AND_FETCH_2: | |
6626 | case BUILT_IN_ATOMIC_AND_FETCH_4: | |
6627 | case BUILT_IN_ATOMIC_AND_FETCH_8: | |
6628 | case BUILT_IN_ATOMIC_AND_FETCH_16: | |
6629 | { | |
6630 | enum built_in_function lib; | |
6631 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1); | |
6632 | lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 + | |
6633 | (fcode - BUILT_IN_ATOMIC_AND_FETCH_1)); | |
6634 | target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true, | |
6635 | ignore, lib); | |
6636 | if (target) | |
6637 | return target; | |
6638 | break; | |
6639 | } | |
6640 | case BUILT_IN_ATOMIC_NAND_FETCH_1: | |
6641 | case BUILT_IN_ATOMIC_NAND_FETCH_2: | |
6642 | case BUILT_IN_ATOMIC_NAND_FETCH_4: | |
6643 | case BUILT_IN_ATOMIC_NAND_FETCH_8: | |
6644 | case BUILT_IN_ATOMIC_NAND_FETCH_16: | |
6645 | { | |
6646 | enum built_in_function lib; | |
6647 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1); | |
6648 | lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 + | |
6649 | (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1)); | |
6650 | target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true, | |
6651 | ignore, lib); | |
6652 | if (target) | |
6653 | return target; | |
6654 | break; | |
6655 | } | |
6656 | case BUILT_IN_ATOMIC_XOR_FETCH_1: | |
6657 | case BUILT_IN_ATOMIC_XOR_FETCH_2: | |
6658 | case BUILT_IN_ATOMIC_XOR_FETCH_4: | |
6659 | case BUILT_IN_ATOMIC_XOR_FETCH_8: | |
6660 | case BUILT_IN_ATOMIC_XOR_FETCH_16: | |
6661 | { | |
6662 | enum built_in_function lib; | |
6663 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1); | |
6664 | lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 + | |
6665 | (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1)); | |
6666 | target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true, | |
6667 | ignore, lib); | |
6668 | if (target) | |
6669 | return target; | |
6670 | break; | |
6671 | } | |
6672 | case BUILT_IN_ATOMIC_OR_FETCH_1: | |
6673 | case BUILT_IN_ATOMIC_OR_FETCH_2: | |
6674 | case BUILT_IN_ATOMIC_OR_FETCH_4: | |
6675 | case BUILT_IN_ATOMIC_OR_FETCH_8: | |
6676 | case BUILT_IN_ATOMIC_OR_FETCH_16: | |
6677 | { | |
6678 | enum built_in_function lib; | |
6679 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1); | |
6680 | lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 + | |
6681 | (fcode - BUILT_IN_ATOMIC_OR_FETCH_1)); | |
6682 | target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true, | |
6683 | ignore, lib); | |
6684 | if (target) | |
6685 | return target; | |
6686 | break; | |
6687 | } | |
6688 | case BUILT_IN_ATOMIC_FETCH_ADD_1: | |
6689 | case BUILT_IN_ATOMIC_FETCH_ADD_2: | |
6690 | case BUILT_IN_ATOMIC_FETCH_ADD_4: | |
6691 | case BUILT_IN_ATOMIC_FETCH_ADD_8: | |
6692 | case BUILT_IN_ATOMIC_FETCH_ADD_16: | |
6693 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1); | |
6694 | target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false, | |
6695 | ignore, BUILT_IN_NONE); | |
6696 | if (target) | |
6697 | return target; | |
6698 | break; | |
6699 | ||
6700 | case BUILT_IN_ATOMIC_FETCH_SUB_1: | |
6701 | case BUILT_IN_ATOMIC_FETCH_SUB_2: | |
6702 | case BUILT_IN_ATOMIC_FETCH_SUB_4: | |
6703 | case BUILT_IN_ATOMIC_FETCH_SUB_8: | |
6704 | case BUILT_IN_ATOMIC_FETCH_SUB_16: | |
6705 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1); | |
6706 | target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false, | |
6707 | ignore, BUILT_IN_NONE); | |
6708 | if (target) | |
6709 | return target; | |
6710 | break; | |
6711 | ||
6712 | case BUILT_IN_ATOMIC_FETCH_AND_1: | |
6713 | case BUILT_IN_ATOMIC_FETCH_AND_2: | |
6714 | case BUILT_IN_ATOMIC_FETCH_AND_4: | |
6715 | case BUILT_IN_ATOMIC_FETCH_AND_8: | |
6716 | case BUILT_IN_ATOMIC_FETCH_AND_16: | |
6717 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1); | |
6718 | target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false, | |
6719 | ignore, BUILT_IN_NONE); | |
6720 | if (target) | |
6721 | return target; | |
6722 | break; | |
6723 | ||
6724 | case BUILT_IN_ATOMIC_FETCH_NAND_1: | |
6725 | case BUILT_IN_ATOMIC_FETCH_NAND_2: | |
6726 | case BUILT_IN_ATOMIC_FETCH_NAND_4: | |
6727 | case BUILT_IN_ATOMIC_FETCH_NAND_8: | |
6728 | case BUILT_IN_ATOMIC_FETCH_NAND_16: | |
6729 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1); | |
6730 | target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false, | |
6731 | ignore, BUILT_IN_NONE); | |
6732 | if (target) | |
6733 | return target; | |
6734 | break; | |
6735 | ||
6736 | case BUILT_IN_ATOMIC_FETCH_XOR_1: | |
6737 | case BUILT_IN_ATOMIC_FETCH_XOR_2: | |
6738 | case BUILT_IN_ATOMIC_FETCH_XOR_4: | |
6739 | case BUILT_IN_ATOMIC_FETCH_XOR_8: | |
6740 | case BUILT_IN_ATOMIC_FETCH_XOR_16: | |
6741 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1); | |
6742 | target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false, | |
6743 | ignore, BUILT_IN_NONE); | |
6744 | if (target) | |
6745 | return target; | |
6746 | break; | |
6747 | ||
6748 | case BUILT_IN_ATOMIC_FETCH_OR_1: | |
6749 | case BUILT_IN_ATOMIC_FETCH_OR_2: | |
6750 | case BUILT_IN_ATOMIC_FETCH_OR_4: | |
6751 | case BUILT_IN_ATOMIC_FETCH_OR_8: | |
6752 | case BUILT_IN_ATOMIC_FETCH_OR_16: | |
6753 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1); | |
6754 | target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false, | |
6755 | ignore, BUILT_IN_NONE); | |
6756 | if (target) | |
6757 | return target; | |
6758 | break; | |
d660c35e AM |
6759 | |
6760 | case BUILT_IN_ATOMIC_TEST_AND_SET: | |
744accb2 | 6761 | return expand_builtin_atomic_test_and_set (exp, target); |
d660c35e AM |
6762 | |
6763 | case BUILT_IN_ATOMIC_CLEAR: | |
6764 | return expand_builtin_atomic_clear (exp); | |
86951993 AM |
6765 | |
6766 | case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE: | |
6767 | return expand_builtin_atomic_always_lock_free (exp); | |
6768 | ||
6769 | case BUILT_IN_ATOMIC_IS_LOCK_FREE: | |
6770 | target = expand_builtin_atomic_is_lock_free (exp); | |
6771 | if (target) | |
6772 | return target; | |
6773 | break; | |
6774 | ||
6775 | case BUILT_IN_ATOMIC_THREAD_FENCE: | |
6776 | expand_builtin_atomic_thread_fence (exp); | |
6777 | return const0_rtx; | |
6778 | ||
6779 | case BUILT_IN_ATOMIC_SIGNAL_FENCE: | |
6780 | expand_builtin_atomic_signal_fence (exp); | |
6781 | return const0_rtx; | |
6782 | ||
10a0d495 JJ |
6783 | case BUILT_IN_OBJECT_SIZE: |
6784 | return expand_builtin_object_size (exp); | |
6785 | ||
6786 | case BUILT_IN_MEMCPY_CHK: | |
6787 | case BUILT_IN_MEMPCPY_CHK: | |
6788 | case BUILT_IN_MEMMOVE_CHK: | |
6789 | case BUILT_IN_MEMSET_CHK: | |
6790 | target = expand_builtin_memory_chk (exp, target, mode, fcode); | |
6791 | if (target) | |
6792 | return target; | |
6793 | break; | |
6794 | ||
6795 | case BUILT_IN_STRCPY_CHK: | |
6796 | case BUILT_IN_STPCPY_CHK: | |
6797 | case BUILT_IN_STRNCPY_CHK: | |
f3fc9b80 | 6798 | case BUILT_IN_STPNCPY_CHK: |
10a0d495 | 6799 | case BUILT_IN_STRCAT_CHK: |
1c2fc017 | 6800 | case BUILT_IN_STRNCAT_CHK: |
10a0d495 JJ |
6801 | case BUILT_IN_SNPRINTF_CHK: |
6802 | case BUILT_IN_VSNPRINTF_CHK: | |
6803 | maybe_emit_chk_warning (exp, fcode); | |
6804 | break; | |
6805 | ||
6806 | case BUILT_IN_SPRINTF_CHK: | |
6807 | case BUILT_IN_VSPRINTF_CHK: | |
6808 | maybe_emit_sprintf_chk_warning (exp, fcode); | |
6809 | break; | |
6810 | ||
f9555f40 | 6811 | case BUILT_IN_FREE: |
a3a704a4 MH |
6812 | if (warn_free_nonheap_object) |
6813 | maybe_emit_free_warning (exp); | |
f9555f40 JJ |
6814 | break; |
6815 | ||
f959607b CLT |
6816 | case BUILT_IN_THREAD_POINTER: |
6817 | return expand_builtin_thread_pointer (exp, target); | |
6818 | ||
6819 | case BUILT_IN_SET_THREAD_POINTER: | |
6820 | expand_builtin_set_thread_pointer (exp); | |
6821 | return const0_rtx; | |
6822 | ||
939b37da BI |
6823 | case BUILT_IN_CILK_DETACH: |
6824 | expand_builtin_cilk_detach (exp); | |
6825 | return const0_rtx; | |
6826 | ||
6827 | case BUILT_IN_CILK_POP_FRAME: | |
6828 | expand_builtin_cilk_pop_frame (exp); | |
6829 | return const0_rtx; | |
6830 | ||
d5e254e1 IE |
6831 | case BUILT_IN_CHKP_INIT_PTR_BOUNDS: |
6832 | case BUILT_IN_CHKP_NULL_PTR_BOUNDS: | |
6833 | case BUILT_IN_CHKP_COPY_PTR_BOUNDS: | |
6834 | case BUILT_IN_CHKP_CHECK_PTR_LBOUNDS: | |
6835 | case BUILT_IN_CHKP_CHECK_PTR_UBOUNDS: | |
6836 | case BUILT_IN_CHKP_CHECK_PTR_BOUNDS: | |
6837 | case BUILT_IN_CHKP_SET_PTR_BOUNDS: | |
6838 | case BUILT_IN_CHKP_NARROW_PTR_BOUNDS: | |
6839 | case BUILT_IN_CHKP_STORE_PTR_BOUNDS: | |
6840 | case BUILT_IN_CHKP_GET_PTR_LBOUND: | |
6841 | case BUILT_IN_CHKP_GET_PTR_UBOUND: | |
6842 | /* We allow user CHKP builtins if Pointer Bounds | |
6843 | Checker is off. */ | |
6844 | if (!chkp_function_instrumented_p (current_function_decl)) | |
6845 | { | |
6846 | if (fcode == BUILT_IN_CHKP_SET_PTR_BOUNDS | |
6847 | || fcode == BUILT_IN_CHKP_NARROW_PTR_BOUNDS | |
6848 | || fcode == BUILT_IN_CHKP_INIT_PTR_BOUNDS | |
6849 | || fcode == BUILT_IN_CHKP_NULL_PTR_BOUNDS | |
6850 | || fcode == BUILT_IN_CHKP_COPY_PTR_BOUNDS) | |
6851 | return expand_normal (CALL_EXPR_ARG (exp, 0)); | |
6852 | else if (fcode == BUILT_IN_CHKP_GET_PTR_LBOUND) | |
6853 | return expand_normal (size_zero_node); | |
6854 | else if (fcode == BUILT_IN_CHKP_GET_PTR_UBOUND) | |
6855 | return expand_normal (size_int (-1)); | |
6856 | else | |
6857 | return const0_rtx; | |
6858 | } | |
6859 | /* FALLTHROUGH */ | |
6860 | ||
6861 | case BUILT_IN_CHKP_BNDMK: | |
6862 | case BUILT_IN_CHKP_BNDSTX: | |
6863 | case BUILT_IN_CHKP_BNDCL: | |
6864 | case BUILT_IN_CHKP_BNDCU: | |
6865 | case BUILT_IN_CHKP_BNDLDX: | |
6866 | case BUILT_IN_CHKP_BNDRET: | |
6867 | case BUILT_IN_CHKP_INTERSECT: | |
6868 | case BUILT_IN_CHKP_NARROW: | |
6869 | case BUILT_IN_CHKP_EXTRACT_LOWER: | |
6870 | case BUILT_IN_CHKP_EXTRACT_UPPER: | |
6871 | /* Software implementation of Pointer Bounds Checker is NYI. | |
6872 | Target support is required. */ | |
6873 | error ("Your target platform does not support -fcheck-pointer-bounds"); | |
6874 | break; | |
6875 | ||
41dbbb37 | 6876 | case BUILT_IN_ACC_ON_DEVICE: |
164453bb NS |
6877 | /* Do library call, if we failed to expand the builtin when |
6878 | folding. */ | |
41dbbb37 TS |
6879 | break; |
6880 | ||
e62f4abc | 6881 | default: /* just do library call, if unknown builtin */ |
84b8b0e0 | 6882 | break; |
28f4ec01 BS |
6883 | } |
6884 | ||
6885 | /* The switch statement above can drop through to cause the function | |
6886 | to be called normally. */ | |
6887 | return expand_call (exp, target, ignore); | |
6888 | } | |
b0b3afb2 | 6889 | |
edcf72f3 IE |
6890 | /* Similar to expand_builtin but is used for instrumented calls. */ |
6891 | ||
6892 | rtx | |
6893 | expand_builtin_with_bounds (tree exp, rtx target, | |
6894 | rtx subtarget ATTRIBUTE_UNUSED, | |
6895 | machine_mode mode, int ignore) | |
6896 | { | |
6897 | tree fndecl = get_callee_fndecl (exp); | |
6898 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
6899 | ||
6900 | gcc_assert (CALL_WITH_BOUNDS_P (exp)); | |
6901 | ||
6902 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) | |
6903 | return targetm.expand_builtin (exp, target, subtarget, mode, ignore); | |
6904 | ||
6905 | gcc_assert (fcode > BEGIN_CHKP_BUILTINS | |
6906 | && fcode < END_CHKP_BUILTINS); | |
6907 | ||
6908 | switch (fcode) | |
6909 | { | |
6910 | case BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP: | |
6911 | target = expand_builtin_memcpy_with_bounds (exp, target); | |
6912 | if (target) | |
6913 | return target; | |
6914 | break; | |
6915 | ||
6916 | case BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP: | |
6917 | target = expand_builtin_mempcpy_with_bounds (exp, target, mode); | |
6918 | if (target) | |
6919 | return target; | |
6920 | break; | |
6921 | ||
6922 | case BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP: | |
6923 | target = expand_builtin_memset_with_bounds (exp, target, mode); | |
6924 | if (target) | |
6925 | return target; | |
6926 | break; | |
6927 | ||
6928 | default: | |
6929 | break; | |
6930 | } | |
6931 | ||
6932 | /* The switch statement above can drop through to cause the function | |
6933 | to be called normally. */ | |
6934 | return expand_call (exp, target, ignore); | |
6935 | } | |
6936 | ||
4977bab6 | 6937 | /* Determine whether a tree node represents a call to a built-in |
feda1845 RS |
6938 | function. If the tree T is a call to a built-in function with |
6939 | the right number of arguments of the appropriate types, return | |
6940 | the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT. | |
6941 | Otherwise the return value is END_BUILTINS. */ | |
4682ae04 | 6942 | |
4977bab6 | 6943 | enum built_in_function |
fa233e34 | 6944 | builtin_mathfn_code (const_tree t) |
4977bab6 | 6945 | { |
fa233e34 KG |
6946 | const_tree fndecl, arg, parmlist; |
6947 | const_tree argtype, parmtype; | |
6948 | const_call_expr_arg_iterator iter; | |
4977bab6 ZW |
6949 | |
6950 | if (TREE_CODE (t) != CALL_EXPR | |
5039610b | 6951 | || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR) |
4977bab6 ZW |
6952 | return END_BUILTINS; |
6953 | ||
2f503025 JM |
6954 | fndecl = get_callee_fndecl (t); |
6955 | if (fndecl == NULL_TREE | |
feda1845 | 6956 | || TREE_CODE (fndecl) != FUNCTION_DECL |
4977bab6 ZW |
6957 | || ! DECL_BUILT_IN (fndecl) |
6958 | || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) | |
6959 | return END_BUILTINS; | |
6960 | ||
feda1845 | 6961 | parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); |
fa233e34 | 6962 | init_const_call_expr_arg_iterator (t, &iter); |
feda1845 | 6963 | for (; parmlist; parmlist = TREE_CHAIN (parmlist)) |
c0a47a61 | 6964 | { |
feda1845 RS |
6965 | /* If a function doesn't take a variable number of arguments, |
6966 | the last element in the list will have type `void'. */ | |
6967 | parmtype = TREE_VALUE (parmlist); | |
6968 | if (VOID_TYPE_P (parmtype)) | |
6969 | { | |
fa233e34 | 6970 | if (more_const_call_expr_args_p (&iter)) |
feda1845 RS |
6971 | return END_BUILTINS; |
6972 | return DECL_FUNCTION_CODE (fndecl); | |
6973 | } | |
6974 | ||
fa233e34 | 6975 | if (! more_const_call_expr_args_p (&iter)) |
c0a47a61 | 6976 | return END_BUILTINS; |
b8698a0f | 6977 | |
fa233e34 | 6978 | arg = next_const_call_expr_arg (&iter); |
5039610b | 6979 | argtype = TREE_TYPE (arg); |
feda1845 RS |
6980 | |
6981 | if (SCALAR_FLOAT_TYPE_P (parmtype)) | |
6982 | { | |
6983 | if (! SCALAR_FLOAT_TYPE_P (argtype)) | |
6984 | return END_BUILTINS; | |
6985 | } | |
6986 | else if (COMPLEX_FLOAT_TYPE_P (parmtype)) | |
6987 | { | |
6988 | if (! COMPLEX_FLOAT_TYPE_P (argtype)) | |
6989 | return END_BUILTINS; | |
6990 | } | |
6991 | else if (POINTER_TYPE_P (parmtype)) | |
6992 | { | |
6993 | if (! POINTER_TYPE_P (argtype)) | |
6994 | return END_BUILTINS; | |
6995 | } | |
6996 | else if (INTEGRAL_TYPE_P (parmtype)) | |
6997 | { | |
6998 | if (! INTEGRAL_TYPE_P (argtype)) | |
6999 | return END_BUILTINS; | |
7000 | } | |
7001 | else | |
c0a47a61 | 7002 | return END_BUILTINS; |
c0a47a61 RS |
7003 | } |
7004 | ||
feda1845 | 7005 | /* Variable-length argument list. */ |
4977bab6 ZW |
7006 | return DECL_FUNCTION_CODE (fndecl); |
7007 | } | |
7008 | ||
5039610b SL |
7009 | /* Fold a call to __builtin_constant_p, if we know its argument ARG will |
7010 | evaluate to a constant. */ | |
b0b3afb2 BS |
7011 | |
7012 | static tree | |
5039610b | 7013 | fold_builtin_constant_p (tree arg) |
b0b3afb2 | 7014 | { |
b0b3afb2 BS |
7015 | /* We return 1 for a numeric type that's known to be a constant |
7016 | value at compile-time or for an aggregate type that's a | |
7017 | literal constant. */ | |
5039610b | 7018 | STRIP_NOPS (arg); |
b0b3afb2 BS |
7019 | |
7020 | /* If we know this is a constant, emit the constant of one. */ | |
5039610b SL |
7021 | if (CONSTANT_CLASS_P (arg) |
7022 | || (TREE_CODE (arg) == CONSTRUCTOR | |
7023 | && TREE_CONSTANT (arg))) | |
b0b3afb2 | 7024 | return integer_one_node; |
5039610b | 7025 | if (TREE_CODE (arg) == ADDR_EXPR) |
fb664a2c | 7026 | { |
5039610b | 7027 | tree op = TREE_OPERAND (arg, 0); |
fb664a2c RG |
7028 | if (TREE_CODE (op) == STRING_CST |
7029 | || (TREE_CODE (op) == ARRAY_REF | |
7030 | && integer_zerop (TREE_OPERAND (op, 1)) | |
7031 | && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST)) | |
7032 | return integer_one_node; | |
7033 | } | |
b0b3afb2 | 7034 | |
0dcd3840 RH |
7035 | /* If this expression has side effects, show we don't know it to be a |
7036 | constant. Likewise if it's a pointer or aggregate type since in | |
7037 | those case we only want literals, since those are only optimized | |
13104975 ZW |
7038 | when generating RTL, not later. |
7039 | And finally, if we are compiling an initializer, not code, we | |
7040 | need to return a definite result now; there's not going to be any | |
7041 | more optimization done. */ | |
5039610b SL |
7042 | if (TREE_SIDE_EFFECTS (arg) |
7043 | || AGGREGATE_TYPE_P (TREE_TYPE (arg)) | |
7044 | || POINTER_TYPE_P (TREE_TYPE (arg)) | |
63b48197 | 7045 | || cfun == 0 |
4e7d7b3d JJ |
7046 | || folding_initializer |
7047 | || force_folding_builtin_constant_p) | |
b0b3afb2 BS |
7048 | return integer_zero_node; |
7049 | ||
5039610b | 7050 | return NULL_TREE; |
b0b3afb2 BS |
7051 | } |
7052 | ||
419ce103 AN |
7053 | /* Create builtin_expect with PRED and EXPECTED as its arguments and |
7054 | return it as a truthvalue. */ | |
6de9cd9a DN |
7055 | |
7056 | static tree | |
ed9c79e1 JJ |
7057 | build_builtin_expect_predicate (location_t loc, tree pred, tree expected, |
7058 | tree predictor) | |
6de9cd9a | 7059 | { |
419ce103 | 7060 | tree fn, arg_types, pred_type, expected_type, call_expr, ret_type; |
6de9cd9a | 7061 | |
e79983f4 | 7062 | fn = builtin_decl_explicit (BUILT_IN_EXPECT); |
419ce103 AN |
7063 | arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
7064 | ret_type = TREE_TYPE (TREE_TYPE (fn)); | |
7065 | pred_type = TREE_VALUE (arg_types); | |
7066 | expected_type = TREE_VALUE (TREE_CHAIN (arg_types)); | |
7067 | ||
db3927fb AH |
7068 | pred = fold_convert_loc (loc, pred_type, pred); |
7069 | expected = fold_convert_loc (loc, expected_type, expected); | |
ed9c79e1 JJ |
7070 | call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected, |
7071 | predictor); | |
419ce103 AN |
7072 | |
7073 | return build2 (NE_EXPR, TREE_TYPE (pred), call_expr, | |
7074 | build_int_cst (ret_type, 0)); | |
7075 | } | |
7076 | ||
7077 | /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return | |
7078 | NULL_TREE if no simplification is possible. */ | |
7079 | ||
ed9c79e1 JJ |
7080 | tree |
7081 | fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2) | |
419ce103 | 7082 | { |
be31603a | 7083 | tree inner, fndecl, inner_arg0; |
419ce103 AN |
7084 | enum tree_code code; |
7085 | ||
be31603a KT |
7086 | /* Distribute the expected value over short-circuiting operators. |
7087 | See through the cast from truthvalue_type_node to long. */ | |
7088 | inner_arg0 = arg0; | |
625a9766 | 7089 | while (CONVERT_EXPR_P (inner_arg0) |
be31603a KT |
7090 | && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0)) |
7091 | && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0)))) | |
7092 | inner_arg0 = TREE_OPERAND (inner_arg0, 0); | |
7093 | ||
419ce103 AN |
7094 | /* If this is a builtin_expect within a builtin_expect keep the |
7095 | inner one. See through a comparison against a constant. It | |
7096 | might have been added to create a thruthvalue. */ | |
be31603a KT |
7097 | inner = inner_arg0; |
7098 | ||
419ce103 AN |
7099 | if (COMPARISON_CLASS_P (inner) |
7100 | && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST) | |
7101 | inner = TREE_OPERAND (inner, 0); | |
7102 | ||
7103 | if (TREE_CODE (inner) == CALL_EXPR | |
7104 | && (fndecl = get_callee_fndecl (inner)) | |
7105 | && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL | |
7106 | && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT) | |
7107 | return arg0; | |
7108 | ||
be31603a | 7109 | inner = inner_arg0; |
419ce103 AN |
7110 | code = TREE_CODE (inner); |
7111 | if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR) | |
7112 | { | |
7113 | tree op0 = TREE_OPERAND (inner, 0); | |
7114 | tree op1 = TREE_OPERAND (inner, 1); | |
7115 | ||
ed9c79e1 JJ |
7116 | op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2); |
7117 | op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2); | |
419ce103 AN |
7118 | inner = build2 (code, TREE_TYPE (inner), op0, op1); |
7119 | ||
db3927fb | 7120 | return fold_convert_loc (loc, TREE_TYPE (arg0), inner); |
419ce103 AN |
7121 | } |
7122 | ||
7123 | /* If the argument isn't invariant then there's nothing else we can do. */ | |
be31603a | 7124 | if (!TREE_CONSTANT (inner_arg0)) |
5039610b | 7125 | return NULL_TREE; |
6de9cd9a | 7126 | |
419ce103 AN |
7127 | /* If we expect that a comparison against the argument will fold to |
7128 | a constant return the constant. In practice, this means a true | |
7129 | constant or the address of a non-weak symbol. */ | |
be31603a | 7130 | inner = inner_arg0; |
6de9cd9a DN |
7131 | STRIP_NOPS (inner); |
7132 | if (TREE_CODE (inner) == ADDR_EXPR) | |
7133 | { | |
7134 | do | |
7135 | { | |
7136 | inner = TREE_OPERAND (inner, 0); | |
7137 | } | |
7138 | while (TREE_CODE (inner) == COMPONENT_REF | |
7139 | || TREE_CODE (inner) == ARRAY_REF); | |
5f26a230 JJ |
7140 | if ((TREE_CODE (inner) == VAR_DECL |
7141 | || TREE_CODE (inner) == FUNCTION_DECL) | |
7142 | && DECL_WEAK (inner)) | |
5039610b | 7143 | return NULL_TREE; |
6de9cd9a DN |
7144 | } |
7145 | ||
419ce103 AN |
7146 | /* Otherwise, ARG0 already has the proper type for the return value. */ |
7147 | return arg0; | |
6de9cd9a DN |
7148 | } |
7149 | ||
5039610b | 7150 | /* Fold a call to __builtin_classify_type with argument ARG. */ |
5197bd50 | 7151 | |
ad82abb8 | 7152 | static tree |
5039610b | 7153 | fold_builtin_classify_type (tree arg) |
ad82abb8 | 7154 | { |
5039610b | 7155 | if (arg == 0) |
45a2c477 | 7156 | return build_int_cst (integer_type_node, no_type_class); |
ad82abb8 | 7157 | |
45a2c477 | 7158 | return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg))); |
ad82abb8 ZW |
7159 | } |
7160 | ||
5039610b | 7161 | /* Fold a call to __builtin_strlen with argument ARG. */ |
667bbbbb EC |
7162 | |
7163 | static tree | |
ab996409 | 7164 | fold_builtin_strlen (location_t loc, tree type, tree arg) |
667bbbbb | 7165 | { |
5039610b | 7166 | if (!validate_arg (arg, POINTER_TYPE)) |
667bbbbb EC |
7167 | return NULL_TREE; |
7168 | else | |
7169 | { | |
5039610b | 7170 | tree len = c_strlen (arg, 0); |
667bbbbb EC |
7171 | |
7172 | if (len) | |
ab996409 | 7173 | return fold_convert_loc (loc, type, len); |
667bbbbb EC |
7174 | |
7175 | return NULL_TREE; | |
7176 | } | |
7177 | } | |
7178 | ||
ab5e2615 RH |
7179 | /* Fold a call to __builtin_inf or __builtin_huge_val. */ |
7180 | ||
7181 | static tree | |
db3927fb | 7182 | fold_builtin_inf (location_t loc, tree type, int warn) |
ab5e2615 | 7183 | { |
efdc7e19 RH |
7184 | REAL_VALUE_TYPE real; |
7185 | ||
6d84156b JM |
7186 | /* __builtin_inff is intended to be usable to define INFINITY on all |
7187 | targets. If an infinity is not available, INFINITY expands "to a | |
7188 | positive constant of type float that overflows at translation | |
7189 | time", footnote "In this case, using INFINITY will violate the | |
7190 | constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4). | |
7191 | Thus we pedwarn to ensure this constraint violation is | |
7192 | diagnosed. */ | |
ab5e2615 | 7193 | if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn) |
db3927fb | 7194 | pedwarn (loc, 0, "target format does not support infinity"); |
ab5e2615 | 7195 | |
efdc7e19 RH |
7196 | real_inf (&real); |
7197 | return build_real (type, real); | |
ab5e2615 RH |
7198 | } |
7199 | ||
75c7c595 RG |
7200 | /* Fold function call to builtin sincos, sincosf, or sincosl. Return |
7201 | NULL_TREE if no simplification can be made. */ | |
7202 | ||
7203 | static tree | |
db3927fb AH |
7204 | fold_builtin_sincos (location_t loc, |
7205 | tree arg0, tree arg1, tree arg2) | |
75c7c595 | 7206 | { |
5039610b | 7207 | tree type; |
5c1a2e63 | 7208 | tree fndecl, call = NULL_TREE; |
75c7c595 | 7209 | |
5039610b SL |
7210 | if (!validate_arg (arg0, REAL_TYPE) |
7211 | || !validate_arg (arg1, POINTER_TYPE) | |
7212 | || !validate_arg (arg2, POINTER_TYPE)) | |
75c7c595 RG |
7213 | return NULL_TREE; |
7214 | ||
75c7c595 | 7215 | type = TREE_TYPE (arg0); |
75c7c595 RG |
7216 | |
7217 | /* Calculate the result when the argument is a constant. */ | |
b03ff92e | 7218 | built_in_function fn = mathfn_built_in_2 (type, CFN_BUILT_IN_CEXPI); |
5c1a2e63 | 7219 | if (fn == END_BUILTINS) |
75c7c595 RG |
7220 | return NULL_TREE; |
7221 | ||
5c1a2e63 RS |
7222 | /* Canonicalize sincos to cexpi. */ |
7223 | if (TREE_CODE (arg0) == REAL_CST) | |
7224 | { | |
7225 | tree complex_type = build_complex_type (type); | |
d7ebef06 | 7226 | call = fold_const_call (as_combined_fn (fn), complex_type, arg0); |
5c1a2e63 RS |
7227 | } |
7228 | if (!call) | |
7229 | { | |
7230 | if (!targetm.libc_has_function (function_c99_math_complex) | |
7231 | || !builtin_decl_implicit_p (fn)) | |
7232 | return NULL_TREE; | |
7233 | fndecl = builtin_decl_explicit (fn); | |
7234 | call = build_call_expr_loc (loc, fndecl, 1, arg0); | |
7235 | call = builtin_save_expr (call); | |
7236 | } | |
75c7c595 | 7237 | |
928c19bb | 7238 | return build2 (COMPOUND_EXPR, void_type_node, |
75c7c595 | 7239 | build2 (MODIFY_EXPR, void_type_node, |
db3927fb | 7240 | build_fold_indirect_ref_loc (loc, arg1), |
5c1a2e63 | 7241 | fold_build1_loc (loc, IMAGPART_EXPR, type, call)), |
75c7c595 | 7242 | build2 (MODIFY_EXPR, void_type_node, |
db3927fb | 7243 | build_fold_indirect_ref_loc (loc, arg2), |
5c1a2e63 | 7244 | fold_build1_loc (loc, REALPART_EXPR, type, call))); |
75c7c595 RG |
7245 | } |
7246 | ||
2a5fce6d PC |
7247 | /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the |
7248 | arguments to the call, and TYPE is its return type. | |
7249 | Return NULL_TREE if no simplification can be made. */ | |
7250 | ||
7251 | static tree | |
db3927fb | 7252 | fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type) |
2a5fce6d PC |
7253 | { |
7254 | if (!validate_arg (arg1, POINTER_TYPE) | |
7255 | || !validate_arg (arg2, INTEGER_TYPE) | |
7256 | || !validate_arg (len, INTEGER_TYPE)) | |
7257 | return NULL_TREE; | |
7258 | else | |
7259 | { | |
7260 | const char *p1; | |
7261 | ||
7262 | if (TREE_CODE (arg2) != INTEGER_CST | |
cc269bb6 | 7263 | || !tree_fits_uhwi_p (len)) |
2a5fce6d PC |
7264 | return NULL_TREE; |
7265 | ||
7266 | p1 = c_getstr (arg1); | |
7267 | if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0) | |
7268 | { | |
7269 | char c; | |
7270 | const char *r; | |
7271 | tree tem; | |
7272 | ||
7273 | if (target_char_cast (arg2, &c)) | |
7274 | return NULL_TREE; | |
7275 | ||
ae7e9ddd | 7276 | r = (const char *) memchr (p1, c, tree_to_uhwi (len)); |
2a5fce6d PC |
7277 | |
7278 | if (r == NULL) | |
7279 | return build_int_cst (TREE_TYPE (arg1), 0); | |
7280 | ||
5d49b6a7 | 7281 | tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1); |
db3927fb | 7282 | return fold_convert_loc (loc, type, tem); |
2a5fce6d PC |
7283 | } |
7284 | return NULL_TREE; | |
7285 | } | |
7286 | } | |
7287 | ||
5039610b SL |
7288 | /* Fold function call to builtin memcmp with arguments ARG1 and ARG2. |
7289 | Return NULL_TREE if no simplification can be made. */ | |
5bb650ec RS |
7290 | |
7291 | static tree | |
db3927fb | 7292 | fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len) |
5bb650ec | 7293 | { |
5039610b SL |
7294 | if (!validate_arg (arg1, POINTER_TYPE) |
7295 | || !validate_arg (arg2, POINTER_TYPE) | |
7296 | || !validate_arg (len, INTEGER_TYPE)) | |
7297 | return NULL_TREE; | |
5bb650ec RS |
7298 | |
7299 | /* If the LEN parameter is zero, return zero. */ | |
7300 | if (integer_zerop (len)) | |
db3927fb | 7301 | return omit_two_operands_loc (loc, integer_type_node, integer_zero_node, |
01847e9d | 7302 | arg1, arg2); |
5bb650ec RS |
7303 | |
7304 | /* If ARG1 and ARG2 are the same (and not volatile), return zero. */ | |
7305 | if (operand_equal_p (arg1, arg2, 0)) | |
db3927fb | 7306 | return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len); |
01847e9d | 7307 | |
01847e9d RS |
7308 | /* If len parameter is one, return an expression corresponding to |
7309 | (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */ | |
ae7e9ddd | 7310 | if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1) |
01847e9d RS |
7311 | { |
7312 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
baab454a UW |
7313 | tree cst_uchar_ptr_node |
7314 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
7315 | ||
db3927fb AH |
7316 | tree ind1 |
7317 | = fold_convert_loc (loc, integer_type_node, | |
7318 | build1 (INDIRECT_REF, cst_uchar_node, | |
7319 | fold_convert_loc (loc, | |
7320 | cst_uchar_ptr_node, | |
01847e9d | 7321 | arg1))); |
db3927fb AH |
7322 | tree ind2 |
7323 | = fold_convert_loc (loc, integer_type_node, | |
7324 | build1 (INDIRECT_REF, cst_uchar_node, | |
7325 | fold_convert_loc (loc, | |
7326 | cst_uchar_ptr_node, | |
01847e9d | 7327 | arg2))); |
db3927fb | 7328 | return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2); |
01847e9d | 7329 | } |
5bb650ec | 7330 | |
5039610b | 7331 | return NULL_TREE; |
5bb650ec RS |
7332 | } |
7333 | ||
5039610b SL |
7334 | /* Fold function call to builtin strcmp with arguments ARG1 and ARG2. |
7335 | Return NULL_TREE if no simplification can be made. */ | |
5bb650ec RS |
7336 | |
7337 | static tree | |
db3927fb | 7338 | fold_builtin_strcmp (location_t loc, tree arg1, tree arg2) |
5bb650ec | 7339 | { |
5039610b SL |
7340 | if (!validate_arg (arg1, POINTER_TYPE) |
7341 | || !validate_arg (arg2, POINTER_TYPE)) | |
7342 | return NULL_TREE; | |
5bb650ec RS |
7343 | |
7344 | /* If ARG1 and ARG2 are the same (and not volatile), return zero. */ | |
7345 | if (operand_equal_p (arg1, arg2, 0)) | |
01847e9d | 7346 | return integer_zero_node; |
5bb650ec | 7347 | |
01847e9d | 7348 | /* If the second arg is "", return *(const unsigned char*)arg1. */ |
df838ef0 | 7349 | const char *p2 = c_getstr (arg2); |
01847e9d RS |
7350 | if (p2 && *p2 == '\0') |
7351 | { | |
7352 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
baab454a UW |
7353 | tree cst_uchar_ptr_node |
7354 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
7355 | ||
db3927fb AH |
7356 | return fold_convert_loc (loc, integer_type_node, |
7357 | build1 (INDIRECT_REF, cst_uchar_node, | |
7358 | fold_convert_loc (loc, | |
7359 | cst_uchar_ptr_node, | |
7360 | arg1))); | |
01847e9d RS |
7361 | } |
7362 | ||
7363 | /* If the first arg is "", return -*(const unsigned char*)arg2. */ | |
df838ef0 | 7364 | const char *p1 = c_getstr (arg1); |
01847e9d RS |
7365 | if (p1 && *p1 == '\0') |
7366 | { | |
7367 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
baab454a UW |
7368 | tree cst_uchar_ptr_node |
7369 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
7370 | ||
db3927fb AH |
7371 | tree temp |
7372 | = fold_convert_loc (loc, integer_type_node, | |
7373 | build1 (INDIRECT_REF, cst_uchar_node, | |
7374 | fold_convert_loc (loc, | |
7375 | cst_uchar_ptr_node, | |
01847e9d | 7376 | arg2))); |
db3927fb | 7377 | return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp); |
5bb650ec RS |
7378 | } |
7379 | ||
5039610b | 7380 | return NULL_TREE; |
5bb650ec RS |
7381 | } |
7382 | ||
5039610b SL |
7383 | /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN. |
7384 | Return NULL_TREE if no simplification can be made. */ | |
5bb650ec RS |
7385 | |
7386 | static tree | |
db3927fb | 7387 | fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len) |
5bb650ec | 7388 | { |
5039610b SL |
7389 | if (!validate_arg (arg1, POINTER_TYPE) |
7390 | || !validate_arg (arg2, POINTER_TYPE) | |
7391 | || !validate_arg (len, INTEGER_TYPE)) | |
7392 | return NULL_TREE; | |
5bb650ec RS |
7393 | |
7394 | /* If the LEN parameter is zero, return zero. */ | |
7395 | if (integer_zerop (len)) | |
db3927fb | 7396 | return omit_two_operands_loc (loc, integer_type_node, integer_zero_node, |
01847e9d | 7397 | arg1, arg2); |
5bb650ec RS |
7398 | |
7399 | /* If ARG1 and ARG2 are the same (and not volatile), return zero. */ | |
7400 | if (operand_equal_p (arg1, arg2, 0)) | |
db3927fb | 7401 | return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len); |
5bb650ec | 7402 | |
01847e9d RS |
7403 | /* If the second arg is "", and the length is greater than zero, |
7404 | return *(const unsigned char*)arg1. */ | |
df838ef0 | 7405 | const char *p2 = c_getstr (arg2); |
01847e9d RS |
7406 | if (p2 && *p2 == '\0' |
7407 | && TREE_CODE (len) == INTEGER_CST | |
7408 | && tree_int_cst_sgn (len) == 1) | |
7409 | { | |
7410 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
baab454a UW |
7411 | tree cst_uchar_ptr_node |
7412 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
7413 | ||
db3927fb AH |
7414 | return fold_convert_loc (loc, integer_type_node, |
7415 | build1 (INDIRECT_REF, cst_uchar_node, | |
7416 | fold_convert_loc (loc, | |
7417 | cst_uchar_ptr_node, | |
7418 | arg1))); | |
01847e9d RS |
7419 | } |
7420 | ||
7421 | /* If the first arg is "", and the length is greater than zero, | |
7422 | return -*(const unsigned char*)arg2. */ | |
df838ef0 | 7423 | const char *p1 = c_getstr (arg1); |
01847e9d RS |
7424 | if (p1 && *p1 == '\0' |
7425 | && TREE_CODE (len) == INTEGER_CST | |
7426 | && tree_int_cst_sgn (len) == 1) | |
7427 | { | |
7428 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
baab454a UW |
7429 | tree cst_uchar_ptr_node |
7430 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
7431 | ||
db3927fb AH |
7432 | tree temp = fold_convert_loc (loc, integer_type_node, |
7433 | build1 (INDIRECT_REF, cst_uchar_node, | |
7434 | fold_convert_loc (loc, | |
7435 | cst_uchar_ptr_node, | |
7436 | arg2))); | |
7437 | return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp); | |
01847e9d RS |
7438 | } |
7439 | ||
7440 | /* If len parameter is one, return an expression corresponding to | |
7441 | (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */ | |
ae7e9ddd | 7442 | if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1) |
01847e9d RS |
7443 | { |
7444 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
baab454a UW |
7445 | tree cst_uchar_ptr_node |
7446 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
7447 | ||
db3927fb AH |
7448 | tree ind1 = fold_convert_loc (loc, integer_type_node, |
7449 | build1 (INDIRECT_REF, cst_uchar_node, | |
7450 | fold_convert_loc (loc, | |
7451 | cst_uchar_ptr_node, | |
7452 | arg1))); | |
7453 | tree ind2 = fold_convert_loc (loc, integer_type_node, | |
7454 | build1 (INDIRECT_REF, cst_uchar_node, | |
7455 | fold_convert_loc (loc, | |
7456 | cst_uchar_ptr_node, | |
7457 | arg2))); | |
7458 | return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2); | |
5bb650ec RS |
7459 | } |
7460 | ||
5039610b | 7461 | return NULL_TREE; |
5bb650ec RS |
7462 | } |
7463 | ||
5039610b | 7464 | /* Fold a call to builtin isascii with argument ARG. */ |
df0785d6 KG |
7465 | |
7466 | static tree | |
db3927fb | 7467 | fold_builtin_isascii (location_t loc, tree arg) |
df0785d6 | 7468 | { |
5039610b SL |
7469 | if (!validate_arg (arg, INTEGER_TYPE)) |
7470 | return NULL_TREE; | |
df0785d6 KG |
7471 | else |
7472 | { | |
7473 | /* Transform isascii(c) -> ((c & ~0x7f) == 0). */ | |
6728ee79 | 7474 | arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg, |
45a2c477 | 7475 | build_int_cst (integer_type_node, |
6728ee79 | 7476 | ~ (unsigned HOST_WIDE_INT) 0x7f)); |
db3927fb | 7477 | return fold_build2_loc (loc, EQ_EXPR, integer_type_node, |
45a2c477 | 7478 | arg, integer_zero_node); |
df0785d6 KG |
7479 | } |
7480 | } | |
7481 | ||
5039610b | 7482 | /* Fold a call to builtin toascii with argument ARG. */ |
df0785d6 KG |
7483 | |
7484 | static tree | |
db3927fb | 7485 | fold_builtin_toascii (location_t loc, tree arg) |
df0785d6 | 7486 | { |
5039610b SL |
7487 | if (!validate_arg (arg, INTEGER_TYPE)) |
7488 | return NULL_TREE; | |
b8698a0f | 7489 | |
5039610b | 7490 | /* Transform toascii(c) -> (c & 0x7f). */ |
db3927fb | 7491 | return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg, |
45a2c477 | 7492 | build_int_cst (integer_type_node, 0x7f)); |
df0785d6 KG |
7493 | } |
7494 | ||
5039610b | 7495 | /* Fold a call to builtin isdigit with argument ARG. */ |
61218d19 KG |
7496 | |
7497 | static tree | |
db3927fb | 7498 | fold_builtin_isdigit (location_t loc, tree arg) |
61218d19 | 7499 | { |
5039610b SL |
7500 | if (!validate_arg (arg, INTEGER_TYPE)) |
7501 | return NULL_TREE; | |
61218d19 KG |
7502 | else |
7503 | { | |
7504 | /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */ | |
c5ff069d ZW |
7505 | /* According to the C standard, isdigit is unaffected by locale. |
7506 | However, it definitely is affected by the target character set. */ | |
c5ff069d ZW |
7507 | unsigned HOST_WIDE_INT target_digit0 |
7508 | = lang_hooks.to_target_charset ('0'); | |
7509 | ||
7510 | if (target_digit0 == 0) | |
7511 | return NULL_TREE; | |
7512 | ||
db3927fb | 7513 | arg = fold_convert_loc (loc, unsigned_type_node, arg); |
6728ee79 MM |
7514 | arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg, |
7515 | build_int_cst (unsigned_type_node, target_digit0)); | |
db3927fb | 7516 | return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg, |
5cdc4a26 | 7517 | build_int_cst (unsigned_type_node, 9)); |
61218d19 KG |
7518 | } |
7519 | } | |
ef79730c | 7520 | |
5039610b | 7521 | /* Fold a call to fabs, fabsf or fabsl with argument ARG. */ |
9655d83b RS |
7522 | |
7523 | static tree | |
db3927fb | 7524 | fold_builtin_fabs (location_t loc, tree arg, tree type) |
9655d83b | 7525 | { |
5039610b SL |
7526 | if (!validate_arg (arg, REAL_TYPE)) |
7527 | return NULL_TREE; | |
9655d83b | 7528 | |
db3927fb | 7529 | arg = fold_convert_loc (loc, type, arg); |
db3927fb | 7530 | return fold_build1_loc (loc, ABS_EXPR, type, arg); |
9655d83b RS |
7531 | } |
7532 | ||
5039610b | 7533 | /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */ |
9655d83b RS |
7534 | |
7535 | static tree | |
db3927fb | 7536 | fold_builtin_abs (location_t loc, tree arg, tree type) |
9655d83b | 7537 | { |
5039610b SL |
7538 | if (!validate_arg (arg, INTEGER_TYPE)) |
7539 | return NULL_TREE; | |
9655d83b | 7540 | |
db3927fb | 7541 | arg = fold_convert_loc (loc, type, arg); |
db3927fb | 7542 | return fold_build1_loc (loc, ABS_EXPR, type, arg); |
9655d83b RS |
7543 | } |
7544 | ||
16949072 RG |
7545 | /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */ |
7546 | ||
7547 | static tree | |
7548 | fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type) | |
7549 | { | |
4e48e02b | 7550 | /* ??? Only expand to FMA_EXPR if it's directly supported. */ |
16949072 | 7551 | if (validate_arg (arg0, REAL_TYPE) |
c3284718 | 7552 | && validate_arg (arg1, REAL_TYPE) |
4e48e02b RS |
7553 | && validate_arg (arg2, REAL_TYPE) |
7554 | && optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing) | |
7555 | return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2); | |
16949072 | 7556 | |
16949072 RG |
7557 | return NULL_TREE; |
7558 | } | |
7559 | ||
527cab20 KG |
7560 | /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */ |
7561 | ||
7562 | static tree | |
db3927fb | 7563 | fold_builtin_carg (location_t loc, tree arg, tree type) |
527cab20 | 7564 | { |
c128599a KG |
7565 | if (validate_arg (arg, COMPLEX_TYPE) |
7566 | && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE) | |
527cab20 KG |
7567 | { |
7568 | tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2); | |
b8698a0f | 7569 | |
527cab20 KG |
7570 | if (atan2_fn) |
7571 | { | |
5039610b | 7572 | tree new_arg = builtin_save_expr (arg); |
db3927fb AH |
7573 | tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg); |
7574 | tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg); | |
7575 | return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg); | |
527cab20 KG |
7576 | } |
7577 | } | |
b8698a0f | 7578 | |
527cab20 KG |
7579 | return NULL_TREE; |
7580 | } | |
7581 | ||
7a2a25ab KG |
7582 | /* Fold a call to builtin frexp, we can assume the base is 2. */ |
7583 | ||
7584 | static tree | |
db3927fb | 7585 | fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype) |
7a2a25ab KG |
7586 | { |
7587 | if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE)) | |
7588 | return NULL_TREE; | |
b8698a0f | 7589 | |
7a2a25ab | 7590 | STRIP_NOPS (arg0); |
b8698a0f | 7591 | |
7a2a25ab KG |
7592 | if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0))) |
7593 | return NULL_TREE; | |
b8698a0f | 7594 | |
db3927fb | 7595 | arg1 = build_fold_indirect_ref_loc (loc, arg1); |
7a2a25ab KG |
7596 | |
7597 | /* Proceed if a valid pointer type was passed in. */ | |
7598 | if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node) | |
7599 | { | |
7600 | const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0); | |
7601 | tree frac, exp; | |
b8698a0f | 7602 | |
7a2a25ab KG |
7603 | switch (value->cl) |
7604 | { | |
7605 | case rvc_zero: | |
7606 | /* For +-0, return (*exp = 0, +-0). */ | |
7607 | exp = integer_zero_node; | |
7608 | frac = arg0; | |
7609 | break; | |
7610 | case rvc_nan: | |
7611 | case rvc_inf: | |
7612 | /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */ | |
db3927fb | 7613 | return omit_one_operand_loc (loc, rettype, arg0, arg1); |
7a2a25ab KG |
7614 | case rvc_normal: |
7615 | { | |
7616 | /* Since the frexp function always expects base 2, and in | |
7617 | GCC normalized significands are already in the range | |
7618 | [0.5, 1.0), we have exactly what frexp wants. */ | |
7619 | REAL_VALUE_TYPE frac_rvt = *value; | |
7620 | SET_REAL_EXP (&frac_rvt, 0); | |
7621 | frac = build_real (rettype, frac_rvt); | |
45a2c477 | 7622 | exp = build_int_cst (integer_type_node, REAL_EXP (value)); |
7a2a25ab KG |
7623 | } |
7624 | break; | |
7625 | default: | |
7626 | gcc_unreachable (); | |
7627 | } | |
b8698a0f | 7628 | |
7a2a25ab | 7629 | /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */ |
db3927fb | 7630 | arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp); |
7a2a25ab | 7631 | TREE_SIDE_EFFECTS (arg1) = 1; |
db3927fb | 7632 | return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac); |
7a2a25ab KG |
7633 | } |
7634 | ||
7635 | return NULL_TREE; | |
7636 | } | |
7637 | ||
3d577eaf KG |
7638 | /* Fold a call to builtin modf. */ |
7639 | ||
7640 | static tree | |
db3927fb | 7641 | fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype) |
3d577eaf KG |
7642 | { |
7643 | if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE)) | |
7644 | return NULL_TREE; | |
b8698a0f | 7645 | |
3d577eaf | 7646 | STRIP_NOPS (arg0); |
b8698a0f | 7647 | |
3d577eaf KG |
7648 | if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0))) |
7649 | return NULL_TREE; | |
b8698a0f | 7650 | |
db3927fb | 7651 | arg1 = build_fold_indirect_ref_loc (loc, arg1); |
3d577eaf KG |
7652 | |
7653 | /* Proceed if a valid pointer type was passed in. */ | |
7654 | if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype)) | |
7655 | { | |
7656 | const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0); | |
7657 | REAL_VALUE_TYPE trunc, frac; | |
7658 | ||
7659 | switch (value->cl) | |
7660 | { | |
7661 | case rvc_nan: | |
7662 | case rvc_zero: | |
7663 | /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */ | |
7664 | trunc = frac = *value; | |
7665 | break; | |
7666 | case rvc_inf: | |
7667 | /* For +-Inf, return (*arg1 = arg0, +-0). */ | |
7668 | frac = dconst0; | |
7669 | frac.sign = value->sign; | |
7670 | trunc = *value; | |
7671 | break; | |
7672 | case rvc_normal: | |
7673 | /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */ | |
7674 | real_trunc (&trunc, VOIDmode, value); | |
7675 | real_arithmetic (&frac, MINUS_EXPR, value, &trunc); | |
7676 | /* If the original number was negative and already | |
7677 | integral, then the fractional part is -0.0. */ | |
7678 | if (value->sign && frac.cl == rvc_zero) | |
7679 | frac.sign = value->sign; | |
7680 | break; | |
7681 | } | |
b8698a0f | 7682 | |
3d577eaf | 7683 | /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */ |
db3927fb | 7684 | arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, |
3d577eaf KG |
7685 | build_real (rettype, trunc)); |
7686 | TREE_SIDE_EFFECTS (arg1) = 1; | |
db3927fb | 7687 | return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, |
3d577eaf KG |
7688 | build_real (rettype, frac)); |
7689 | } | |
b8698a0f | 7690 | |
3d577eaf KG |
7691 | return NULL_TREE; |
7692 | } | |
7693 | ||
44e10129 MM |
7694 | /* Given a location LOC, an interclass builtin function decl FNDECL |
7695 | and its single argument ARG, return an folded expression computing | |
7696 | the same, or NULL_TREE if we either couldn't or didn't want to fold | |
7697 | (the latter happen if there's an RTL instruction available). */ | |
7698 | ||
7699 | static tree | |
7700 | fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg) | |
7701 | { | |
ef4bddc2 | 7702 | machine_mode mode; |
44e10129 MM |
7703 | |
7704 | if (!validate_arg (arg, REAL_TYPE)) | |
7705 | return NULL_TREE; | |
7706 | ||
7707 | if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing) | |
7708 | return NULL_TREE; | |
7709 | ||
7710 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
7711 | ||
02cf2861 AM |
7712 | bool is_ibm_extended = MODE_COMPOSITE_P (mode); |
7713 | ||
44e10129 MM |
7714 | /* If there is no optab, try generic code. */ |
7715 | switch (DECL_FUNCTION_CODE (fndecl)) | |
7716 | { | |
7717 | tree result; | |
7718 | ||
7719 | CASE_FLT_FN (BUILT_IN_ISINF): | |
7720 | { | |
7721 | /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */ | |
e79983f4 | 7722 | tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER); |
02cf2861 | 7723 | tree type = TREE_TYPE (arg); |
44e10129 MM |
7724 | REAL_VALUE_TYPE r; |
7725 | char buf[128]; | |
7726 | ||
02cf2861 AM |
7727 | if (is_ibm_extended) |
7728 | { | |
7729 | /* NaN and Inf are encoded in the high-order double value | |
7730 | only. The low-order value is not significant. */ | |
7731 | type = double_type_node; | |
7732 | mode = DFmode; | |
7733 | arg = fold_build1_loc (loc, NOP_EXPR, type, arg); | |
7734 | } | |
44e10129 MM |
7735 | get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf)); |
7736 | real_from_string (&r, buf); | |
7737 | result = build_call_expr (isgr_fn, 2, | |
7738 | fold_build1_loc (loc, ABS_EXPR, type, arg), | |
7739 | build_real (type, r)); | |
7740 | return result; | |
7741 | } | |
7742 | CASE_FLT_FN (BUILT_IN_FINITE): | |
7743 | case BUILT_IN_ISFINITE: | |
7744 | { | |
7745 | /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */ | |
e79983f4 | 7746 | tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL); |
02cf2861 | 7747 | tree type = TREE_TYPE (arg); |
44e10129 MM |
7748 | REAL_VALUE_TYPE r; |
7749 | char buf[128]; | |
7750 | ||
02cf2861 AM |
7751 | if (is_ibm_extended) |
7752 | { | |
7753 | /* NaN and Inf are encoded in the high-order double value | |
7754 | only. The low-order value is not significant. */ | |
7755 | type = double_type_node; | |
7756 | mode = DFmode; | |
7757 | arg = fold_build1_loc (loc, NOP_EXPR, type, arg); | |
7758 | } | |
44e10129 MM |
7759 | get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf)); |
7760 | real_from_string (&r, buf); | |
7761 | result = build_call_expr (isle_fn, 2, | |
7762 | fold_build1_loc (loc, ABS_EXPR, type, arg), | |
7763 | build_real (type, r)); | |
7764 | /*result = fold_build2_loc (loc, UNGT_EXPR, | |
7765 | TREE_TYPE (TREE_TYPE (fndecl)), | |
7766 | fold_build1_loc (loc, ABS_EXPR, type, arg), | |
7767 | build_real (type, r)); | |
7768 | result = fold_build1_loc (loc, TRUTH_NOT_EXPR, | |
7769 | TREE_TYPE (TREE_TYPE (fndecl)), | |
7770 | result);*/ | |
7771 | return result; | |
7772 | } | |
7773 | case BUILT_IN_ISNORMAL: | |
7774 | { | |
7775 | /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) & | |
7776 | islessequal(fabs(x),DBL_MAX). */ | |
e79983f4 | 7777 | tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL); |
02cf2861 AM |
7778 | tree type = TREE_TYPE (arg); |
7779 | tree orig_arg, max_exp, min_exp; | |
7780 | machine_mode orig_mode = mode; | |
44e10129 MM |
7781 | REAL_VALUE_TYPE rmax, rmin; |
7782 | char buf[128]; | |
7783 | ||
02cf2861 AM |
7784 | orig_arg = arg = builtin_save_expr (arg); |
7785 | if (is_ibm_extended) | |
7786 | { | |
7787 | /* Use double to test the normal range of IBM extended | |
7788 | precision. Emin for IBM extended precision is | |
7789 | different to emin for IEEE double, being 53 higher | |
7790 | since the low double exponent is at least 53 lower | |
7791 | than the high double exponent. */ | |
7792 | type = double_type_node; | |
7793 | mode = DFmode; | |
7794 | arg = fold_build1_loc (loc, NOP_EXPR, type, arg); | |
7795 | } | |
7796 | arg = fold_build1_loc (loc, ABS_EXPR, type, arg); | |
7797 | ||
44e10129 MM |
7798 | get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf)); |
7799 | real_from_string (&rmax, buf); | |
02cf2861 | 7800 | sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (orig_mode)->emin - 1); |
44e10129 | 7801 | real_from_string (&rmin, buf); |
02cf2861 AM |
7802 | max_exp = build_real (type, rmax); |
7803 | min_exp = build_real (type, rmin); | |
7804 | ||
7805 | max_exp = build_call_expr (isle_fn, 2, arg, max_exp); | |
7806 | if (is_ibm_extended) | |
7807 | { | |
7808 | /* Testing the high end of the range is done just using | |
7809 | the high double, using the same test as isfinite(). | |
7810 | For the subnormal end of the range we first test the | |
7811 | high double, then if its magnitude is equal to the | |
7812 | limit of 0x1p-969, we test whether the low double is | |
7813 | non-zero and opposite sign to the high double. */ | |
7814 | tree const islt_fn = builtin_decl_explicit (BUILT_IN_ISLESS); | |
7815 | tree const isgt_fn = builtin_decl_explicit (BUILT_IN_ISGREATER); | |
7816 | tree gt_min = build_call_expr (isgt_fn, 2, arg, min_exp); | |
7817 | tree eq_min = fold_build2 (EQ_EXPR, integer_type_node, | |
7818 | arg, min_exp); | |
7819 | tree as_complex = build1 (VIEW_CONVERT_EXPR, | |
7820 | complex_double_type_node, orig_arg); | |
7821 | tree hi_dbl = build1 (REALPART_EXPR, type, as_complex); | |
7822 | tree lo_dbl = build1 (IMAGPART_EXPR, type, as_complex); | |
7823 | tree zero = build_real (type, dconst0); | |
7824 | tree hilt = build_call_expr (islt_fn, 2, hi_dbl, zero); | |
7825 | tree lolt = build_call_expr (islt_fn, 2, lo_dbl, zero); | |
7826 | tree logt = build_call_expr (isgt_fn, 2, lo_dbl, zero); | |
7827 | tree ok_lo = fold_build1 (TRUTH_NOT_EXPR, integer_type_node, | |
7828 | fold_build3 (COND_EXPR, | |
7829 | integer_type_node, | |
7830 | hilt, logt, lolt)); | |
7831 | eq_min = fold_build2 (TRUTH_ANDIF_EXPR, integer_type_node, | |
7832 | eq_min, ok_lo); | |
7833 | min_exp = fold_build2 (TRUTH_ORIF_EXPR, integer_type_node, | |
7834 | gt_min, eq_min); | |
7835 | } | |
7836 | else | |
7837 | { | |
7838 | tree const isge_fn | |
7839 | = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL); | |
7840 | min_exp = build_call_expr (isge_fn, 2, arg, min_exp); | |
7841 | } | |
7842 | result = fold_build2 (BIT_AND_EXPR, integer_type_node, | |
7843 | max_exp, min_exp); | |
44e10129 MM |
7844 | return result; |
7845 | } | |
7846 | default: | |
7847 | break; | |
7848 | } | |
7849 | ||
7850 | return NULL_TREE; | |
7851 | } | |
7852 | ||
64a9295a | 7853 | /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite. |
5039610b | 7854 | ARG is the argument for the call. */ |
64a9295a PB |
7855 | |
7856 | static tree | |
db3927fb | 7857 | fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index) |
64a9295a | 7858 | { |
64a9295a | 7859 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
64a9295a | 7860 | |
5039610b | 7861 | if (!validate_arg (arg, REAL_TYPE)) |
83322951 | 7862 | return NULL_TREE; |
64a9295a | 7863 | |
64a9295a PB |
7864 | switch (builtin_index) |
7865 | { | |
7866 | case BUILT_IN_ISINF: | |
3d3dbadd | 7867 | if (!HONOR_INFINITIES (arg)) |
db3927fb | 7868 | return omit_one_operand_loc (loc, type, integer_zero_node, arg); |
64a9295a | 7869 | |
64a9295a PB |
7870 | return NULL_TREE; |
7871 | ||
05f41289 KG |
7872 | case BUILT_IN_ISINF_SIGN: |
7873 | { | |
7874 | /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */ | |
7875 | /* In a boolean context, GCC will fold the inner COND_EXPR to | |
7876 | 1. So e.g. "if (isinf_sign(x))" would be folded to just | |
7877 | "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */ | |
72f52f30 | 7878 | tree signbit_fn = builtin_decl_explicit (BUILT_IN_SIGNBIT); |
e79983f4 | 7879 | tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF); |
05f41289 KG |
7880 | tree tmp = NULL_TREE; |
7881 | ||
7882 | arg = builtin_save_expr (arg); | |
7883 | ||
7884 | if (signbit_fn && isinf_fn) | |
7885 | { | |
db3927fb AH |
7886 | tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg); |
7887 | tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg); | |
05f41289 | 7888 | |
db3927fb | 7889 | signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node, |
05f41289 | 7890 | signbit_call, integer_zero_node); |
db3927fb | 7891 | isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node, |
05f41289 | 7892 | isinf_call, integer_zero_node); |
b8698a0f | 7893 | |
db3927fb | 7894 | tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call, |
05f41289 | 7895 | integer_minus_one_node, integer_one_node); |
db3927fb AH |
7896 | tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, |
7897 | isinf_call, tmp, | |
05f41289 KG |
7898 | integer_zero_node); |
7899 | } | |
7900 | ||
7901 | return tmp; | |
7902 | } | |
7903 | ||
0c8d3c2b | 7904 | case BUILT_IN_ISFINITE: |
1b457aa4 | 7905 | if (!HONOR_NANS (arg) |
3d3dbadd | 7906 | && !HONOR_INFINITIES (arg)) |
db3927fb | 7907 | return omit_one_operand_loc (loc, type, integer_one_node, arg); |
64a9295a | 7908 | |
64a9295a PB |
7909 | return NULL_TREE; |
7910 | ||
7911 | case BUILT_IN_ISNAN: | |
1b457aa4 | 7912 | if (!HONOR_NANS (arg)) |
db3927fb | 7913 | return omit_one_operand_loc (loc, type, integer_zero_node, arg); |
64a9295a | 7914 | |
02cf2861 AM |
7915 | { |
7916 | bool is_ibm_extended = MODE_COMPOSITE_P (TYPE_MODE (TREE_TYPE (arg))); | |
7917 | if (is_ibm_extended) | |
7918 | { | |
7919 | /* NaN and Inf are encoded in the high-order double value | |
7920 | only. The low-order value is not significant. */ | |
7921 | arg = fold_build1_loc (loc, NOP_EXPR, double_type_node, arg); | |
7922 | } | |
7923 | } | |
64a9295a | 7924 | arg = builtin_save_expr (arg); |
db3927fb | 7925 | return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg); |
64a9295a PB |
7926 | |
7927 | default: | |
298e6adc | 7928 | gcc_unreachable (); |
64a9295a PB |
7929 | } |
7930 | } | |
7931 | ||
3bf5906b KG |
7932 | /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...). |
7933 | This builtin will generate code to return the appropriate floating | |
7934 | point classification depending on the value of the floating point | |
7935 | number passed in. The possible return values must be supplied as | |
32101f99 | 7936 | int arguments to the call in the following order: FP_NAN, FP_INFINITE, |
3bf5906b KG |
7937 | FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly |
7938 | one floating point argument which is "type generic". */ | |
7939 | ||
7940 | static tree | |
a6a0570f | 7941 | fold_builtin_fpclassify (location_t loc, tree *args, int nargs) |
3bf5906b | 7942 | { |
32101f99 KG |
7943 | tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero, |
7944 | arg, type, res, tmp; | |
ef4bddc2 | 7945 | machine_mode mode; |
3bf5906b KG |
7946 | REAL_VALUE_TYPE r; |
7947 | char buf[128]; | |
b8698a0f | 7948 | |
3bf5906b | 7949 | /* Verify the required arguments in the original call. */ |
a6a0570f RB |
7950 | if (nargs != 6 |
7951 | || !validate_arg (args[0], INTEGER_TYPE) | |
7952 | || !validate_arg (args[1], INTEGER_TYPE) | |
7953 | || !validate_arg (args[2], INTEGER_TYPE) | |
7954 | || !validate_arg (args[3], INTEGER_TYPE) | |
7955 | || !validate_arg (args[4], INTEGER_TYPE) | |
7956 | || !validate_arg (args[5], REAL_TYPE)) | |
3bf5906b | 7957 | return NULL_TREE; |
b8698a0f | 7958 | |
a6a0570f RB |
7959 | fp_nan = args[0]; |
7960 | fp_infinite = args[1]; | |
7961 | fp_normal = args[2]; | |
7962 | fp_subnormal = args[3]; | |
7963 | fp_zero = args[4]; | |
7964 | arg = args[5]; | |
3bf5906b KG |
7965 | type = TREE_TYPE (arg); |
7966 | mode = TYPE_MODE (type); | |
db3927fb | 7967 | arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg)); |
3bf5906b | 7968 | |
b8698a0f | 7969 | /* fpclassify(x) -> |
3bf5906b | 7970 | isnan(x) ? FP_NAN : |
32101f99 | 7971 | (fabs(x) == Inf ? FP_INFINITE : |
3bf5906b KG |
7972 | (fabs(x) >= DBL_MIN ? FP_NORMAL : |
7973 | (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */ | |
b8698a0f | 7974 | |
db3927fb | 7975 | tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg, |
3bf5906b | 7976 | build_real (type, dconst0)); |
db3927fb AH |
7977 | res = fold_build3_loc (loc, COND_EXPR, integer_type_node, |
7978 | tmp, fp_zero, fp_subnormal); | |
3bf5906b KG |
7979 | |
7980 | sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1); | |
7981 | real_from_string (&r, buf); | |
db3927fb AH |
7982 | tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node, |
7983 | arg, build_real (type, r)); | |
7984 | res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res); | |
b8698a0f | 7985 | |
3bf5906b KG |
7986 | if (HONOR_INFINITIES (mode)) |
7987 | { | |
7988 | real_inf (&r); | |
db3927fb | 7989 | tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg, |
3bf5906b | 7990 | build_real (type, r)); |
db3927fb AH |
7991 | res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, |
7992 | fp_infinite, res); | |
3bf5906b KG |
7993 | } |
7994 | ||
7995 | if (HONOR_NANS (mode)) | |
7996 | { | |
db3927fb AH |
7997 | tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg); |
7998 | res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan); | |
3bf5906b | 7999 | } |
b8698a0f | 8000 | |
3bf5906b KG |
8001 | return res; |
8002 | } | |
8003 | ||
08039bd8 | 8004 | /* Fold a call to an unordered comparison function such as |
a35da91f | 8005 | __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function |
5039610b | 8006 | being called and ARG0 and ARG1 are the arguments for the call. |
64a9295a PB |
8007 | UNORDERED_CODE and ORDERED_CODE are comparison codes that give |
8008 | the opposite of the desired result. UNORDERED_CODE is used | |
8009 | for modes that can hold NaNs and ORDERED_CODE is used for | |
8010 | the rest. */ | |
08039bd8 RS |
8011 | |
8012 | static tree | |
db3927fb | 8013 | fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1, |
08039bd8 RS |
8014 | enum tree_code unordered_code, |
8015 | enum tree_code ordered_code) | |
8016 | { | |
14f661f1 | 8017 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
08039bd8 | 8018 | enum tree_code code; |
1aeaea8d GK |
8019 | tree type0, type1; |
8020 | enum tree_code code0, code1; | |
8021 | tree cmp_type = NULL_TREE; | |
08039bd8 | 8022 | |
1aeaea8d GK |
8023 | type0 = TREE_TYPE (arg0); |
8024 | type1 = TREE_TYPE (arg1); | |
c22cacf3 | 8025 | |
1aeaea8d GK |
8026 | code0 = TREE_CODE (type0); |
8027 | code1 = TREE_CODE (type1); | |
c22cacf3 | 8028 | |
1aeaea8d GK |
8029 | if (code0 == REAL_TYPE && code1 == REAL_TYPE) |
8030 | /* Choose the wider of two real types. */ | |
8031 | cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1) | |
8032 | ? type0 : type1; | |
8033 | else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE) | |
8034 | cmp_type = type0; | |
8035 | else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE) | |
8036 | cmp_type = type1; | |
c22cacf3 | 8037 | |
db3927fb AH |
8038 | arg0 = fold_convert_loc (loc, cmp_type, arg0); |
8039 | arg1 = fold_convert_loc (loc, cmp_type, arg1); | |
14f661f1 RS |
8040 | |
8041 | if (unordered_code == UNORDERED_EXPR) | |
8042 | { | |
1b457aa4 | 8043 | if (!HONOR_NANS (arg0)) |
db3927fb AH |
8044 | return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1); |
8045 | return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1); | |
14f661f1 | 8046 | } |
08039bd8 | 8047 | |
1b457aa4 | 8048 | code = HONOR_NANS (arg0) ? unordered_code : ordered_code; |
db3927fb AH |
8049 | return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, |
8050 | fold_build2_loc (loc, code, type, arg0, arg1)); | |
08039bd8 RS |
8051 | } |
8052 | ||
1304953e JJ |
8053 | /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal |
8054 | arithmetics if it can never overflow, or into internal functions that | |
8055 | return both result of arithmetics and overflowed boolean flag in | |
44a845ca MS |
8056 | a complex integer result, or some other check for overflow. |
8057 | Similarly fold __builtin_{add,sub,mul}_overflow_p to just the overflow | |
8058 | checking part of that. */ | |
1304953e JJ |
8059 | |
8060 | static tree | |
8061 | fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode, | |
8062 | tree arg0, tree arg1, tree arg2) | |
8063 | { | |
8064 | enum internal_fn ifn = IFN_LAST; | |
44a845ca MS |
8065 | /* The code of the expression corresponding to the type-generic |
8066 | built-in, or ERROR_MARK for the type-specific ones. */ | |
8067 | enum tree_code opcode = ERROR_MARK; | |
8068 | bool ovf_only = false; | |
8069 | ||
1304953e JJ |
8070 | switch (fcode) |
8071 | { | |
44a845ca MS |
8072 | case BUILT_IN_ADD_OVERFLOW_P: |
8073 | ovf_only = true; | |
8074 | /* FALLTHRU */ | |
1304953e | 8075 | case BUILT_IN_ADD_OVERFLOW: |
44a845ca MS |
8076 | opcode = PLUS_EXPR; |
8077 | /* FALLTHRU */ | |
1304953e JJ |
8078 | case BUILT_IN_SADD_OVERFLOW: |
8079 | case BUILT_IN_SADDL_OVERFLOW: | |
8080 | case BUILT_IN_SADDLL_OVERFLOW: | |
8081 | case BUILT_IN_UADD_OVERFLOW: | |
8082 | case BUILT_IN_UADDL_OVERFLOW: | |
8083 | case BUILT_IN_UADDLL_OVERFLOW: | |
8084 | ifn = IFN_ADD_OVERFLOW; | |
8085 | break; | |
44a845ca MS |
8086 | case BUILT_IN_SUB_OVERFLOW_P: |
8087 | ovf_only = true; | |
8088 | /* FALLTHRU */ | |
1304953e | 8089 | case BUILT_IN_SUB_OVERFLOW: |
44a845ca MS |
8090 | opcode = MINUS_EXPR; |
8091 | /* FALLTHRU */ | |
1304953e JJ |
8092 | case BUILT_IN_SSUB_OVERFLOW: |
8093 | case BUILT_IN_SSUBL_OVERFLOW: | |
8094 | case BUILT_IN_SSUBLL_OVERFLOW: | |
8095 | case BUILT_IN_USUB_OVERFLOW: | |
8096 | case BUILT_IN_USUBL_OVERFLOW: | |
8097 | case BUILT_IN_USUBLL_OVERFLOW: | |
8098 | ifn = IFN_SUB_OVERFLOW; | |
8099 | break; | |
44a845ca MS |
8100 | case BUILT_IN_MUL_OVERFLOW_P: |
8101 | ovf_only = true; | |
8102 | /* FALLTHRU */ | |
1304953e | 8103 | case BUILT_IN_MUL_OVERFLOW: |
44a845ca MS |
8104 | opcode = MULT_EXPR; |
8105 | /* FALLTHRU */ | |
1304953e JJ |
8106 | case BUILT_IN_SMUL_OVERFLOW: |
8107 | case BUILT_IN_SMULL_OVERFLOW: | |
8108 | case BUILT_IN_SMULLL_OVERFLOW: | |
8109 | case BUILT_IN_UMUL_OVERFLOW: | |
8110 | case BUILT_IN_UMULL_OVERFLOW: | |
8111 | case BUILT_IN_UMULLL_OVERFLOW: | |
8112 | ifn = IFN_MUL_OVERFLOW; | |
8113 | break; | |
8114 | default: | |
8115 | gcc_unreachable (); | |
8116 | } | |
44a845ca MS |
8117 | |
8118 | /* For the "generic" overloads, the first two arguments can have different | |
8119 | types and the last argument determines the target type to use to check | |
8120 | for overflow. The arguments of the other overloads all have the same | |
8121 | type. */ | |
8122 | tree type = ovf_only ? TREE_TYPE (arg2) : TREE_TYPE (TREE_TYPE (arg2)); | |
8123 | ||
8124 | /* For the __builtin_{add,sub,mul}_overflow_p builtins, when the first two | |
8125 | arguments are constant, attempt to fold the built-in call into a constant | |
8126 | expression indicating whether or not it detected an overflow. */ | |
8127 | if (ovf_only | |
8128 | && TREE_CODE (arg0) == INTEGER_CST | |
8129 | && TREE_CODE (arg1) == INTEGER_CST) | |
8130 | /* Perform the computation in the target type and check for overflow. */ | |
8131 | return omit_one_operand_loc (loc, boolean_type_node, | |
8132 | arith_overflowed_p (opcode, type, arg0, arg1) | |
8133 | ? boolean_true_node : boolean_false_node, | |
8134 | arg2); | |
8135 | ||
1304953e JJ |
8136 | tree ctype = build_complex_type (type); |
8137 | tree call = build_call_expr_internal_loc (loc, ifn, ctype, | |
8138 | 2, arg0, arg1); | |
8139 | tree tgt = save_expr (call); | |
8140 | tree intres = build1_loc (loc, REALPART_EXPR, type, tgt); | |
8141 | tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt); | |
8142 | ovfres = fold_convert_loc (loc, boolean_type_node, ovfres); | |
44a845ca MS |
8143 | |
8144 | if (ovf_only) | |
8145 | return omit_one_operand_loc (loc, boolean_type_node, ovfres, arg2); | |
8146 | ||
8147 | tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2); | |
1304953e JJ |
8148 | tree store |
8149 | = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres); | |
8150 | return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres); | |
8151 | } | |
8152 | ||
b25aad5f MS |
8153 | /* Fold a call to __builtin_FILE to a constant string. */ |
8154 | ||
8155 | static inline tree | |
8156 | fold_builtin_FILE (location_t loc) | |
8157 | { | |
8158 | if (const char *fname = LOCATION_FILE (loc)) | |
8159 | return build_string_literal (strlen (fname) + 1, fname); | |
8160 | ||
8161 | return build_string_literal (1, ""); | |
8162 | } | |
8163 | ||
8164 | /* Fold a call to __builtin_FUNCTION to a constant string. */ | |
8165 | ||
8166 | static inline tree | |
8167 | fold_builtin_FUNCTION () | |
8168 | { | |
8169 | if (current_function_decl) | |
8170 | { | |
8171 | const char *name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl)); | |
8172 | return build_string_literal (strlen (name) + 1, name); | |
8173 | } | |
8174 | ||
8175 | return build_string_literal (1, ""); | |
8176 | } | |
8177 | ||
8178 | /* Fold a call to __builtin_LINE to an integer constant. */ | |
8179 | ||
8180 | static inline tree | |
8181 | fold_builtin_LINE (location_t loc, tree type) | |
8182 | { | |
8183 | return build_int_cst (type, LOCATION_LINE (loc)); | |
8184 | } | |
8185 | ||
5039610b | 8186 | /* Fold a call to built-in function FNDECL with 0 arguments. |
2625bb5d | 8187 | This function returns NULL_TREE if no simplification was possible. */ |
b0b3afb2 | 8188 | |
6de9cd9a | 8189 | static tree |
2625bb5d | 8190 | fold_builtin_0 (location_t loc, tree fndecl) |
b0b3afb2 | 8191 | { |
c0a47a61 | 8192 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
5039610b | 8193 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); |
a0d2281e | 8194 | switch (fcode) |
b0b3afb2 | 8195 | { |
b25aad5f MS |
8196 | case BUILT_IN_FILE: |
8197 | return fold_builtin_FILE (loc); | |
8198 | ||
8199 | case BUILT_IN_FUNCTION: | |
8200 | return fold_builtin_FUNCTION (); | |
8201 | ||
8202 | case BUILT_IN_LINE: | |
8203 | return fold_builtin_LINE (loc, type); | |
8204 | ||
5039610b | 8205 | CASE_FLT_FN (BUILT_IN_INF): |
6dc198e3 | 8206 | CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF): |
5039610b SL |
8207 | case BUILT_IN_INFD32: |
8208 | case BUILT_IN_INFD64: | |
8209 | case BUILT_IN_INFD128: | |
db3927fb | 8210 | return fold_builtin_inf (loc, type, true); |
d3147f64 | 8211 | |
5039610b | 8212 | CASE_FLT_FN (BUILT_IN_HUGE_VAL): |
6dc198e3 | 8213 | CASE_FLT_FN_FLOATN_NX (BUILT_IN_HUGE_VAL): |
db3927fb | 8214 | return fold_builtin_inf (loc, type, false); |
d3147f64 | 8215 | |
5039610b SL |
8216 | case BUILT_IN_CLASSIFY_TYPE: |
8217 | return fold_builtin_classify_type (NULL_TREE); | |
d3147f64 | 8218 | |
5039610b SL |
8219 | default: |
8220 | break; | |
8221 | } | |
8222 | return NULL_TREE; | |
8223 | } | |
d3147f64 | 8224 | |
5039610b | 8225 | /* Fold a call to built-in function FNDECL with 1 argument, ARG0. |
2625bb5d | 8226 | This function returns NULL_TREE if no simplification was possible. */ |
d3147f64 | 8227 | |
5039610b | 8228 | static tree |
2625bb5d | 8229 | fold_builtin_1 (location_t loc, tree fndecl, tree arg0) |
5039610b SL |
8230 | { |
8231 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); | |
8232 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
5c1a2e63 RS |
8233 | |
8234 | if (TREE_CODE (arg0) == ERROR_MARK) | |
8235 | return NULL_TREE; | |
8236 | ||
d7ebef06 | 8237 | if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0)) |
5c1a2e63 RS |
8238 | return ret; |
8239 | ||
5039610b SL |
8240 | switch (fcode) |
8241 | { | |
b0b3afb2 | 8242 | case BUILT_IN_CONSTANT_P: |
d3147f64 | 8243 | { |
5039610b | 8244 | tree val = fold_builtin_constant_p (arg0); |
d3147f64 | 8245 | |
d3147f64 EC |
8246 | /* Gimplification will pull the CALL_EXPR for the builtin out of |
8247 | an if condition. When not optimizing, we'll not CSE it back. | |
8248 | To avoid link error types of regressions, return false now. */ | |
8249 | if (!val && !optimize) | |
8250 | val = integer_zero_node; | |
8251 | ||
8252 | return val; | |
8253 | } | |
b0b3afb2 | 8254 | |
ad82abb8 | 8255 | case BUILT_IN_CLASSIFY_TYPE: |
5039610b | 8256 | return fold_builtin_classify_type (arg0); |
ad82abb8 | 8257 | |
b0b3afb2 | 8258 | case BUILT_IN_STRLEN: |
ab996409 | 8259 | return fold_builtin_strlen (loc, type, arg0); |
b0b3afb2 | 8260 | |
ea6a6627 | 8261 | CASE_FLT_FN (BUILT_IN_FABS): |
6dc198e3 | 8262 | CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS): |
e2323f5b PB |
8263 | case BUILT_IN_FABSD32: |
8264 | case BUILT_IN_FABSD64: | |
8265 | case BUILT_IN_FABSD128: | |
db3927fb | 8266 | return fold_builtin_fabs (loc, arg0, type); |
9655d83b RS |
8267 | |
8268 | case BUILT_IN_ABS: | |
8269 | case BUILT_IN_LABS: | |
8270 | case BUILT_IN_LLABS: | |
8271 | case BUILT_IN_IMAXABS: | |
db3927fb | 8272 | return fold_builtin_abs (loc, arg0, type); |
07bae5ad | 8273 | |
ea6a6627 | 8274 | CASE_FLT_FN (BUILT_IN_CONJ): |
c128599a | 8275 | if (validate_arg (arg0, COMPLEX_TYPE) |
b8698a0f | 8276 | && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE) |
db3927fb | 8277 | return fold_build1_loc (loc, CONJ_EXPR, type, arg0); |
5039610b | 8278 | break; |
aa6c7c3a | 8279 | |
ea6a6627 | 8280 | CASE_FLT_FN (BUILT_IN_CREAL): |
c128599a | 8281 | if (validate_arg (arg0, COMPLEX_TYPE) |
b8698a0f | 8282 | && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE) |
6f3d1a5e | 8283 | return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0)); |
5039610b | 8284 | break; |
aa6c7c3a | 8285 | |
ea6a6627 | 8286 | CASE_FLT_FN (BUILT_IN_CIMAG): |
376da68e KG |
8287 | if (validate_arg (arg0, COMPLEX_TYPE) |
8288 | && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE) | |
db3927fb | 8289 | return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0)); |
5039610b | 8290 | break; |
aa6c7c3a | 8291 | |
5c1a2e63 RS |
8292 | CASE_FLT_FN (BUILT_IN_CARG): |
8293 | return fold_builtin_carg (loc, arg0, type); | |
43272bf5 | 8294 | |
5c1a2e63 RS |
8295 | case BUILT_IN_ISASCII: |
8296 | return fold_builtin_isascii (loc, arg0); | |
b8698a0f | 8297 | |
5c1a2e63 RS |
8298 | case BUILT_IN_TOASCII: |
8299 | return fold_builtin_toascii (loc, arg0); | |
b8698a0f | 8300 | |
5c1a2e63 RS |
8301 | case BUILT_IN_ISDIGIT: |
8302 | return fold_builtin_isdigit (loc, arg0); | |
b8698a0f | 8303 | |
5c1a2e63 RS |
8304 | CASE_FLT_FN (BUILT_IN_FINITE): |
8305 | case BUILT_IN_FINITED32: | |
8306 | case BUILT_IN_FINITED64: | |
8307 | case BUILT_IN_FINITED128: | |
8308 | case BUILT_IN_ISFINITE: | |
8309 | { | |
8310 | tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE); | |
8311 | if (ret) | |
8312 | return ret; | |
8313 | return fold_builtin_interclass_mathfn (loc, fndecl, arg0); | |
8314 | } | |
b8698a0f | 8315 | |
5c1a2e63 RS |
8316 | CASE_FLT_FN (BUILT_IN_ISINF): |
8317 | case BUILT_IN_ISINFD32: | |
8318 | case BUILT_IN_ISINFD64: | |
8319 | case BUILT_IN_ISINFD128: | |
8320 | { | |
8321 | tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF); | |
8322 | if (ret) | |
8323 | return ret; | |
8324 | return fold_builtin_interclass_mathfn (loc, fndecl, arg0); | |
8325 | } | |
b8698a0f | 8326 | |
5c1a2e63 RS |
8327 | case BUILT_IN_ISNORMAL: |
8328 | return fold_builtin_interclass_mathfn (loc, fndecl, arg0); | |
b8698a0f | 8329 | |
5c1a2e63 RS |
8330 | case BUILT_IN_ISINF_SIGN: |
8331 | return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN); | |
b8698a0f | 8332 | |
5c1a2e63 RS |
8333 | CASE_FLT_FN (BUILT_IN_ISNAN): |
8334 | case BUILT_IN_ISNAND32: | |
8335 | case BUILT_IN_ISNAND64: | |
8336 | case BUILT_IN_ISNAND128: | |
8337 | return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN); | |
b8698a0f | 8338 | |
5c1a2e63 RS |
8339 | case BUILT_IN_FREE: |
8340 | if (integer_zerop (arg0)) | |
8341 | return build_empty_stmt (loc); | |
abcc43f5 | 8342 | break; |
07bae5ad | 8343 | |
5c1a2e63 | 8344 | default: |
4835c978 | 8345 | break; |
5c1a2e63 | 8346 | } |
4977bab6 | 8347 | |
5c1a2e63 | 8348 | return NULL_TREE; |
e19f6bde | 8349 | |
5c1a2e63 | 8350 | } |
b53fed56 | 8351 | |
5c1a2e63 RS |
8352 | /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1. |
8353 | This function returns NULL_TREE if no simplification was possible. */ | |
5039610b SL |
8354 | |
8355 | static tree | |
2625bb5d | 8356 | fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1) |
5039610b SL |
8357 | { |
8358 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); | |
8359 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
8360 | ||
5c1a2e63 RS |
8361 | if (TREE_CODE (arg0) == ERROR_MARK |
8362 | || TREE_CODE (arg1) == ERROR_MARK) | |
8363 | return NULL_TREE; | |
ea91f957 | 8364 | |
d7ebef06 | 8365 | if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0, arg1)) |
5c1a2e63 | 8366 | return ret; |
752b7d38 | 8367 | |
5c1a2e63 RS |
8368 | switch (fcode) |
8369 | { | |
752b7d38 KG |
8370 | CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */ |
8371 | CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */ | |
8372 | if (validate_arg (arg0, REAL_TYPE) | |
c3284718 | 8373 | && validate_arg (arg1, POINTER_TYPE)) |
752b7d38 KG |
8374 | return do_mpfr_lgamma_r (arg0, arg1, type); |
8375 | break; | |
5039610b | 8376 | |
7a2a25ab | 8377 | CASE_FLT_FN (BUILT_IN_FREXP): |
db3927fb | 8378 | return fold_builtin_frexp (loc, arg0, arg1, type); |
7a2a25ab | 8379 | |
3d577eaf | 8380 | CASE_FLT_FN (BUILT_IN_MODF): |
db3927fb | 8381 | return fold_builtin_modf (loc, arg0, arg1, type); |
3d577eaf | 8382 | |
5039610b | 8383 | case BUILT_IN_STRSTR: |
db3927fb | 8384 | return fold_builtin_strstr (loc, arg0, arg1, type); |
5039610b | 8385 | |
5039610b | 8386 | case BUILT_IN_STRSPN: |
db3927fb | 8387 | return fold_builtin_strspn (loc, arg0, arg1); |
5039610b SL |
8388 | |
8389 | case BUILT_IN_STRCSPN: | |
db3927fb | 8390 | return fold_builtin_strcspn (loc, arg0, arg1); |
5039610b SL |
8391 | |
8392 | case BUILT_IN_STRCHR: | |
8393 | case BUILT_IN_INDEX: | |
db3927fb | 8394 | return fold_builtin_strchr (loc, arg0, arg1, type); |
5039610b SL |
8395 | |
8396 | case BUILT_IN_STRRCHR: | |
8397 | case BUILT_IN_RINDEX: | |
db3927fb | 8398 | return fold_builtin_strrchr (loc, arg0, arg1, type); |
5039610b | 8399 | |
5039610b | 8400 | case BUILT_IN_STRCMP: |
db3927fb | 8401 | return fold_builtin_strcmp (loc, arg0, arg1); |
5039610b SL |
8402 | |
8403 | case BUILT_IN_STRPBRK: | |
db3927fb | 8404 | return fold_builtin_strpbrk (loc, arg0, arg1, type); |
5039610b SL |
8405 | |
8406 | case BUILT_IN_EXPECT: | |
ed9c79e1 | 8407 | return fold_builtin_expect (loc, arg0, arg1, NULL_TREE); |
5039610b | 8408 | |
08039bd8 | 8409 | case BUILT_IN_ISGREATER: |
db3927fb AH |
8410 | return fold_builtin_unordered_cmp (loc, fndecl, |
8411 | arg0, arg1, UNLE_EXPR, LE_EXPR); | |
08039bd8 | 8412 | case BUILT_IN_ISGREATEREQUAL: |
db3927fb AH |
8413 | return fold_builtin_unordered_cmp (loc, fndecl, |
8414 | arg0, arg1, UNLT_EXPR, LT_EXPR); | |
08039bd8 | 8415 | case BUILT_IN_ISLESS: |
db3927fb AH |
8416 | return fold_builtin_unordered_cmp (loc, fndecl, |
8417 | arg0, arg1, UNGE_EXPR, GE_EXPR); | |
08039bd8 | 8418 | case BUILT_IN_ISLESSEQUAL: |
db3927fb AH |
8419 | return fold_builtin_unordered_cmp (loc, fndecl, |
8420 | arg0, arg1, UNGT_EXPR, GT_EXPR); | |
08039bd8 | 8421 | case BUILT_IN_ISLESSGREATER: |
db3927fb AH |
8422 | return fold_builtin_unordered_cmp (loc, fndecl, |
8423 | arg0, arg1, UNEQ_EXPR, EQ_EXPR); | |
08039bd8 | 8424 | case BUILT_IN_ISUNORDERED: |
db3927fb AH |
8425 | return fold_builtin_unordered_cmp (loc, fndecl, |
8426 | arg0, arg1, UNORDERED_EXPR, | |
a35da91f | 8427 | NOP_EXPR); |
08039bd8 | 8428 | |
d3147f64 EC |
8429 | /* We do the folding for va_start in the expander. */ |
8430 | case BUILT_IN_VA_START: | |
8431 | break; | |
a32e70c3 | 8432 | |
10a0d495 | 8433 | case BUILT_IN_OBJECT_SIZE: |
5039610b | 8434 | return fold_builtin_object_size (arg0, arg1); |
10a0d495 | 8435 | |
86951993 AM |
8436 | case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE: |
8437 | return fold_builtin_atomic_always_lock_free (arg0, arg1); | |
8438 | ||
8439 | case BUILT_IN_ATOMIC_IS_LOCK_FREE: | |
8440 | return fold_builtin_atomic_is_lock_free (arg0, arg1); | |
8441 | ||
5039610b SL |
8442 | default: |
8443 | break; | |
8444 | } | |
8445 | return NULL_TREE; | |
8446 | } | |
8447 | ||
8448 | /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1, | |
2625bb5d | 8449 | and ARG2. |
5039610b SL |
8450 | This function returns NULL_TREE if no simplification was possible. */ |
8451 | ||
8452 | static tree | |
db3927fb | 8453 | fold_builtin_3 (location_t loc, tree fndecl, |
2625bb5d | 8454 | tree arg0, tree arg1, tree arg2) |
5039610b SL |
8455 | { |
8456 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); | |
8457 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
5c1a2e63 RS |
8458 | |
8459 | if (TREE_CODE (arg0) == ERROR_MARK | |
8460 | || TREE_CODE (arg1) == ERROR_MARK | |
8461 | || TREE_CODE (arg2) == ERROR_MARK) | |
8462 | return NULL_TREE; | |
8463 | ||
d7ebef06 RS |
8464 | if (tree ret = fold_const_call (as_combined_fn (fcode), type, |
8465 | arg0, arg1, arg2)) | |
5c1a2e63 RS |
8466 | return ret; |
8467 | ||
5039610b SL |
8468 | switch (fcode) |
8469 | { | |
8470 | ||
8471 | CASE_FLT_FN (BUILT_IN_SINCOS): | |
db3927fb | 8472 | return fold_builtin_sincos (loc, arg0, arg1, arg2); |
5039610b SL |
8473 | |
8474 | CASE_FLT_FN (BUILT_IN_FMA): | |
16949072 | 8475 | return fold_builtin_fma (loc, arg0, arg1, arg2, type); |
5039610b | 8476 | |
ea91f957 KG |
8477 | CASE_FLT_FN (BUILT_IN_REMQUO): |
8478 | if (validate_arg (arg0, REAL_TYPE) | |
c3284718 RS |
8479 | && validate_arg (arg1, REAL_TYPE) |
8480 | && validate_arg (arg2, POINTER_TYPE)) | |
ea91f957 KG |
8481 | return do_mpfr_remquo (arg0, arg1, arg2); |
8482 | break; | |
ea91f957 | 8483 | |
5039610b | 8484 | case BUILT_IN_STRNCMP: |
db3927fb | 8485 | return fold_builtin_strncmp (loc, arg0, arg1, arg2); |
5039610b | 8486 | |
2a5fce6d | 8487 | case BUILT_IN_MEMCHR: |
db3927fb | 8488 | return fold_builtin_memchr (loc, arg0, arg1, arg2, type); |
2a5fce6d | 8489 | |
5039610b SL |
8490 | case BUILT_IN_BCMP: |
8491 | case BUILT_IN_MEMCMP: | |
db3927fb | 8492 | return fold_builtin_memcmp (loc, arg0, arg1, arg2);; |
5039610b | 8493 | |
ed9c79e1 JJ |
8494 | case BUILT_IN_EXPECT: |
8495 | return fold_builtin_expect (loc, arg0, arg1, arg2); | |
8496 | ||
1304953e JJ |
8497 | case BUILT_IN_ADD_OVERFLOW: |
8498 | case BUILT_IN_SUB_OVERFLOW: | |
8499 | case BUILT_IN_MUL_OVERFLOW: | |
44a845ca MS |
8500 | case BUILT_IN_ADD_OVERFLOW_P: |
8501 | case BUILT_IN_SUB_OVERFLOW_P: | |
8502 | case BUILT_IN_MUL_OVERFLOW_P: | |
1304953e JJ |
8503 | case BUILT_IN_SADD_OVERFLOW: |
8504 | case BUILT_IN_SADDL_OVERFLOW: | |
8505 | case BUILT_IN_SADDLL_OVERFLOW: | |
8506 | case BUILT_IN_SSUB_OVERFLOW: | |
8507 | case BUILT_IN_SSUBL_OVERFLOW: | |
8508 | case BUILT_IN_SSUBLL_OVERFLOW: | |
8509 | case BUILT_IN_SMUL_OVERFLOW: | |
8510 | case BUILT_IN_SMULL_OVERFLOW: | |
8511 | case BUILT_IN_SMULLL_OVERFLOW: | |
8512 | case BUILT_IN_UADD_OVERFLOW: | |
8513 | case BUILT_IN_UADDL_OVERFLOW: | |
8514 | case BUILT_IN_UADDLL_OVERFLOW: | |
8515 | case BUILT_IN_USUB_OVERFLOW: | |
8516 | case BUILT_IN_USUBL_OVERFLOW: | |
8517 | case BUILT_IN_USUBLL_OVERFLOW: | |
8518 | case BUILT_IN_UMUL_OVERFLOW: | |
8519 | case BUILT_IN_UMULL_OVERFLOW: | |
8520 | case BUILT_IN_UMULLL_OVERFLOW: | |
8521 | return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2); | |
8522 | ||
b0b3afb2 BS |
8523 | default: |
8524 | break; | |
8525 | } | |
5039610b SL |
8526 | return NULL_TREE; |
8527 | } | |
b0b3afb2 | 8528 | |
5039610b | 8529 | /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS |
a6a0570f RB |
8530 | arguments. IGNORE is true if the result of the |
8531 | function call is ignored. This function returns NULL_TREE if no | |
8532 | simplification was possible. */ | |
b8698a0f | 8533 | |
3d2cf79f | 8534 | tree |
2625bb5d | 8535 | fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool) |
5039610b SL |
8536 | { |
8537 | tree ret = NULL_TREE; | |
f4577fcd | 8538 | |
5039610b SL |
8539 | switch (nargs) |
8540 | { | |
8541 | case 0: | |
2625bb5d | 8542 | ret = fold_builtin_0 (loc, fndecl); |
5039610b SL |
8543 | break; |
8544 | case 1: | |
2625bb5d | 8545 | ret = fold_builtin_1 (loc, fndecl, args[0]); |
5039610b SL |
8546 | break; |
8547 | case 2: | |
2625bb5d | 8548 | ret = fold_builtin_2 (loc, fndecl, args[0], args[1]); |
5039610b SL |
8549 | break; |
8550 | case 3: | |
2625bb5d | 8551 | ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]); |
5039610b | 8552 | break; |
5039610b | 8553 | default: |
2625bb5d | 8554 | ret = fold_builtin_varargs (loc, fndecl, args, nargs); |
5039610b SL |
8555 | break; |
8556 | } | |
8557 | if (ret) | |
8558 | { | |
726a989a | 8559 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); |
db3927fb | 8560 | SET_EXPR_LOCATION (ret, loc); |
5039610b SL |
8561 | TREE_NO_WARNING (ret) = 1; |
8562 | return ret; | |
8563 | } | |
8564 | return NULL_TREE; | |
8565 | } | |
8566 | ||
862d0b35 DN |
8567 | /* Construct a new CALL_EXPR to FNDECL using the tail of the argument |
8568 | list ARGS along with N new arguments in NEWARGS. SKIP is the number | |
8569 | of arguments in ARGS to be omitted. OLDNARGS is the number of | |
8570 | elements in ARGS. */ | |
5039610b SL |
8571 | |
8572 | static tree | |
862d0b35 DN |
8573 | rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args, |
8574 | int skip, tree fndecl, int n, va_list newargs) | |
5039610b | 8575 | { |
862d0b35 DN |
8576 | int nargs = oldnargs - skip + n; |
8577 | tree *buffer; | |
5039610b | 8578 | |
862d0b35 | 8579 | if (n > 0) |
5039610b | 8580 | { |
862d0b35 | 8581 | int i, j; |
5039610b | 8582 | |
862d0b35 DN |
8583 | buffer = XALLOCAVEC (tree, nargs); |
8584 | for (i = 0; i < n; i++) | |
8585 | buffer[i] = va_arg (newargs, tree); | |
8586 | for (j = skip; j < oldnargs; j++, i++) | |
8587 | buffer[i] = args[j]; | |
8588 | } | |
8589 | else | |
8590 | buffer = args + skip; | |
3bf5906b | 8591 | |
862d0b35 DN |
8592 | return build_call_expr_loc_array (loc, fndecl, nargs, buffer); |
8593 | } | |
5039610b | 8594 | |
0889e9bc JJ |
8595 | /* Return true if FNDECL shouldn't be folded right now. |
8596 | If a built-in function has an inline attribute always_inline | |
8597 | wrapper, defer folding it after always_inline functions have | |
8598 | been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking | |
8599 | might not be performed. */ | |
8600 | ||
e7f9dae0 | 8601 | bool |
0889e9bc JJ |
8602 | avoid_folding_inline_builtin (tree fndecl) |
8603 | { | |
8604 | return (DECL_DECLARED_INLINE_P (fndecl) | |
8605 | && DECL_DISREGARD_INLINE_LIMITS (fndecl) | |
8606 | && cfun | |
8607 | && !cfun->always_inline_functions_inlined | |
8608 | && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl))); | |
8609 | } | |
8610 | ||
6de9cd9a | 8611 | /* A wrapper function for builtin folding that prevents warnings for |
caf93cb0 | 8612 | "statement without effect" and the like, caused by removing the |
6de9cd9a DN |
8613 | call node earlier than the warning is generated. */ |
8614 | ||
8615 | tree | |
db3927fb | 8616 | fold_call_expr (location_t loc, tree exp, bool ignore) |
6de9cd9a | 8617 | { |
5039610b SL |
8618 | tree ret = NULL_TREE; |
8619 | tree fndecl = get_callee_fndecl (exp); | |
8620 | if (fndecl | |
8621 | && TREE_CODE (fndecl) == FUNCTION_DECL | |
6ef5231b JJ |
8622 | && DECL_BUILT_IN (fndecl) |
8623 | /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized | |
8624 | yet. Defer folding until we see all the arguments | |
8625 | (after inlining). */ | |
8626 | && !CALL_EXPR_VA_ARG_PACK (exp)) | |
8627 | { | |
8628 | int nargs = call_expr_nargs (exp); | |
8629 | ||
8630 | /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but | |
8631 | instead last argument is __builtin_va_arg_pack (). Defer folding | |
8632 | even in that case, until arguments are finalized. */ | |
8633 | if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR) | |
8634 | { | |
8635 | tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1)); | |
8636 | if (fndecl2 | |
8637 | && TREE_CODE (fndecl2) == FUNCTION_DECL | |
8638 | && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL | |
8639 | && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK) | |
8640 | return NULL_TREE; | |
8641 | } | |
8642 | ||
0889e9bc JJ |
8643 | if (avoid_folding_inline_builtin (fndecl)) |
8644 | return NULL_TREE; | |
8645 | ||
5039610b | 8646 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) |
f311c3b4 NF |
8647 | return targetm.fold_builtin (fndecl, call_expr_nargs (exp), |
8648 | CALL_EXPR_ARGP (exp), ignore); | |
5039610b SL |
8649 | else |
8650 | { | |
a6a0570f RB |
8651 | tree *args = CALL_EXPR_ARGP (exp); |
8652 | ret = fold_builtin_n (loc, fndecl, args, nargs, ignore); | |
5039610b | 8653 | if (ret) |
db3927fb | 8654 | return ret; |
5039610b | 8655 | } |
6de9cd9a | 8656 | } |
5039610b SL |
8657 | return NULL_TREE; |
8658 | } | |
b8698a0f | 8659 | |
a6a0570f RB |
8660 | /* Fold a CALL_EXPR with type TYPE with FN as the function expression. |
8661 | N arguments are passed in the array ARGARRAY. Return a folded | |
8662 | expression or NULL_TREE if no simplification was possible. */ | |
4977bab6 ZW |
8663 | |
8664 | tree | |
a6a0570f | 8665 | fold_builtin_call_array (location_t loc, tree, |
94a0dd7b SL |
8666 | tree fn, |
8667 | int n, | |
8668 | tree *argarray) | |
6385a28f | 8669 | { |
a6a0570f RB |
8670 | if (TREE_CODE (fn) != ADDR_EXPR) |
8671 | return NULL_TREE; | |
5039610b | 8672 | |
a6a0570f RB |
8673 | tree fndecl = TREE_OPERAND (fn, 0); |
8674 | if (TREE_CODE (fndecl) == FUNCTION_DECL | |
8675 | && DECL_BUILT_IN (fndecl)) | |
8676 | { | |
8677 | /* If last argument is __builtin_va_arg_pack (), arguments to this | |
8678 | function are not finalized yet. Defer folding until they are. */ | |
8679 | if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR) | |
8680 | { | |
8681 | tree fndecl2 = get_callee_fndecl (argarray[n - 1]); | |
8682 | if (fndecl2 | |
8683 | && TREE_CODE (fndecl2) == FUNCTION_DECL | |
8684 | && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL | |
8685 | && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK) | |
8686 | return NULL_TREE; | |
8687 | } | |
8688 | if (avoid_folding_inline_builtin (fndecl)) | |
8689 | return NULL_TREE; | |
8690 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) | |
8691 | return targetm.fold_builtin (fndecl, n, argarray, false); | |
8692 | else | |
8693 | return fold_builtin_n (loc, fndecl, argarray, n, false); | |
8694 | } | |
5039610b | 8695 | |
a6a0570f | 8696 | return NULL_TREE; |
5039610b SL |
8697 | } |
8698 | ||
43ea30dc NF |
8699 | /* Construct a new CALL_EXPR using the tail of the argument list of EXP |
8700 | along with N new arguments specified as the "..." parameters. SKIP | |
8701 | is the number of arguments in EXP to be omitted. This function is used | |
8702 | to do varargs-to-varargs transformations. */ | |
8703 | ||
8704 | static tree | |
8705 | rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...) | |
8706 | { | |
8707 | va_list ap; | |
8708 | tree t; | |
8709 | ||
8710 | va_start (ap, n); | |
8711 | t = rewrite_call_expr_valist (loc, call_expr_nargs (exp), | |
8712 | CALL_EXPR_ARGP (exp), skip, fndecl, n, ap); | |
8713 | va_end (ap); | |
5039610b | 8714 | |
43ea30dc | 8715 | return t; |
5039610b SL |
8716 | } |
8717 | ||
8718 | /* Validate a single argument ARG against a tree code CODE representing | |
8719 | a type. */ | |
b8698a0f | 8720 | |
5039610b | 8721 | static bool |
fa233e34 | 8722 | validate_arg (const_tree arg, enum tree_code code) |
5039610b SL |
8723 | { |
8724 | if (!arg) | |
8725 | return false; | |
8726 | else if (code == POINTER_TYPE) | |
8727 | return POINTER_TYPE_P (TREE_TYPE (arg)); | |
4cd8e76f RG |
8728 | else if (code == INTEGER_TYPE) |
8729 | return INTEGRAL_TYPE_P (TREE_TYPE (arg)); | |
5039610b | 8730 | return code == TREE_CODE (TREE_TYPE (arg)); |
6385a28f | 8731 | } |
019fa094 | 8732 | |
726a989a RB |
8733 | /* This function validates the types of a function call argument list |
8734 | against a specified list of tree_codes. If the last specifier is a 0, | |
8735 | that represents an ellipses, otherwise the last specifier must be a | |
8736 | VOID_TYPE. | |
8737 | ||
8738 | This is the GIMPLE version of validate_arglist. Eventually we want to | |
8739 | completely convert builtins.c to work from GIMPLEs and the tree based | |
8740 | validate_arglist will then be removed. */ | |
8741 | ||
8742 | bool | |
538dd0b7 | 8743 | validate_gimple_arglist (const gcall *call, ...) |
726a989a RB |
8744 | { |
8745 | enum tree_code code; | |
8746 | bool res = 0; | |
8747 | va_list ap; | |
8748 | const_tree arg; | |
8749 | size_t i; | |
8750 | ||
8751 | va_start (ap, call); | |
8752 | i = 0; | |
8753 | ||
8754 | do | |
8755 | { | |
72b5577d | 8756 | code = (enum tree_code) va_arg (ap, int); |
726a989a RB |
8757 | switch (code) |
8758 | { | |
8759 | case 0: | |
8760 | /* This signifies an ellipses, any further arguments are all ok. */ | |
8761 | res = true; | |
8762 | goto end; | |
8763 | case VOID_TYPE: | |
8764 | /* This signifies an endlink, if no arguments remain, return | |
8765 | true, otherwise return false. */ | |
8766 | res = (i == gimple_call_num_args (call)); | |
8767 | goto end; | |
8768 | default: | |
8769 | /* If no parameters remain or the parameter's code does not | |
8770 | match the specified code, return false. Otherwise continue | |
8771 | checking any remaining arguments. */ | |
8772 | arg = gimple_call_arg (call, i++); | |
8773 | if (!validate_arg (arg, code)) | |
8774 | goto end; | |
8775 | break; | |
8776 | } | |
8777 | } | |
8778 | while (1); | |
8779 | ||
8780 | /* We need gotos here since we can only have one VA_CLOSE in a | |
8781 | function. */ | |
8782 | end: ; | |
8783 | va_end (ap); | |
8784 | ||
8785 | return res; | |
8786 | } | |
8787 | ||
f6155fda SS |
8788 | /* Default target-specific builtin expander that does nothing. */ |
8789 | ||
8790 | rtx | |
4682ae04 AJ |
8791 | default_expand_builtin (tree exp ATTRIBUTE_UNUSED, |
8792 | rtx target ATTRIBUTE_UNUSED, | |
8793 | rtx subtarget ATTRIBUTE_UNUSED, | |
ef4bddc2 | 8794 | machine_mode mode ATTRIBUTE_UNUSED, |
4682ae04 | 8795 | int ignore ATTRIBUTE_UNUSED) |
f6155fda SS |
8796 | { |
8797 | return NULL_RTX; | |
8798 | } | |
34ee7f82 | 8799 | |
7dc61d6c KG |
8800 | /* Returns true is EXP represents data that would potentially reside |
8801 | in a readonly section. */ | |
8802 | ||
fef5a0d9 | 8803 | bool |
7dc61d6c KG |
8804 | readonly_data_expr (tree exp) |
8805 | { | |
8806 | STRIP_NOPS (exp); | |
8807 | ||
aef0afc4 UW |
8808 | if (TREE_CODE (exp) != ADDR_EXPR) |
8809 | return false; | |
8810 | ||
8811 | exp = get_base_address (TREE_OPERAND (exp, 0)); | |
8812 | if (!exp) | |
8813 | return false; | |
8814 | ||
8815 | /* Make sure we call decl_readonly_section only for trees it | |
8816 | can handle (since it returns true for everything it doesn't | |
8817 | understand). */ | |
caf93cb0 | 8818 | if (TREE_CODE (exp) == STRING_CST |
aef0afc4 UW |
8819 | || TREE_CODE (exp) == CONSTRUCTOR |
8820 | || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp))) | |
8821 | return decl_readonly_section (exp, 0); | |
7dc61d6c KG |
8822 | else |
8823 | return false; | |
8824 | } | |
6de9cd9a | 8825 | |
5039610b SL |
8826 | /* Simplify a call to the strstr builtin. S1 and S2 are the arguments |
8827 | to the call, and TYPE is its return type. | |
6de9cd9a | 8828 | |
5039610b | 8829 | Return NULL_TREE if no simplification was possible, otherwise return the |
6de9cd9a DN |
8830 | simplified form of the call as a tree. |
8831 | ||
8832 | The simplified form may be a constant or other expression which | |
8833 | computes the same value, but in a more efficient manner (including | |
8834 | calls to other builtin functions). | |
8835 | ||
8836 | The call may contain arguments which need to be evaluated, but | |
8837 | which are not useful to determine the result of the call. In | |
8838 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
8839 | COMPOUND_EXPR will be an argument which must be evaluated. | |
8840 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
8841 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
8842 | form of the builtin function call. */ | |
8843 | ||
8844 | static tree | |
db3927fb | 8845 | fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type) |
6de9cd9a | 8846 | { |
5039610b SL |
8847 | if (!validate_arg (s1, POINTER_TYPE) |
8848 | || !validate_arg (s2, POINTER_TYPE)) | |
8849 | return NULL_TREE; | |
6de9cd9a DN |
8850 | else |
8851 | { | |
6de9cd9a DN |
8852 | tree fn; |
8853 | const char *p1, *p2; | |
8854 | ||
8855 | p2 = c_getstr (s2); | |
8856 | if (p2 == NULL) | |
5039610b | 8857 | return NULL_TREE; |
6de9cd9a DN |
8858 | |
8859 | p1 = c_getstr (s1); | |
8860 | if (p1 != NULL) | |
8861 | { | |
8862 | const char *r = strstr (p1, p2); | |
5fcfe0b2 | 8863 | tree tem; |
6de9cd9a | 8864 | |
6de9cd9a | 8865 | if (r == NULL) |
5212068f | 8866 | return build_int_cst (TREE_TYPE (s1), 0); |
b953ebd6 RS |
8867 | |
8868 | /* Return an offset into the constant string argument. */ | |
5d49b6a7 | 8869 | tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1); |
db3927fb | 8870 | return fold_convert_loc (loc, type, tem); |
6de9cd9a DN |
8871 | } |
8872 | ||
817f9ef2 JW |
8873 | /* The argument is const char *, and the result is char *, so we need |
8874 | a type conversion here to avoid a warning. */ | |
6de9cd9a | 8875 | if (p2[0] == '\0') |
db3927fb | 8876 | return fold_convert_loc (loc, type, s1); |
6de9cd9a DN |
8877 | |
8878 | if (p2[1] != '\0') | |
5039610b | 8879 | return NULL_TREE; |
6de9cd9a | 8880 | |
e79983f4 | 8881 | fn = builtin_decl_implicit (BUILT_IN_STRCHR); |
6de9cd9a | 8882 | if (!fn) |
5039610b | 8883 | return NULL_TREE; |
6de9cd9a DN |
8884 | |
8885 | /* New argument list transforming strstr(s1, s2) to | |
8886 | strchr(s1, s2[0]). */ | |
45a2c477 RG |
8887 | return build_call_expr_loc (loc, fn, 2, s1, |
8888 | build_int_cst (integer_type_node, p2[0])); | |
6de9cd9a DN |
8889 | } |
8890 | } | |
8891 | ||
5039610b SL |
8892 | /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to |
8893 | the call, and TYPE is its return type. | |
6de9cd9a | 8894 | |
5039610b | 8895 | Return NULL_TREE if no simplification was possible, otherwise return the |
6de9cd9a DN |
8896 | simplified form of the call as a tree. |
8897 | ||
8898 | The simplified form may be a constant or other expression which | |
8899 | computes the same value, but in a more efficient manner (including | |
8900 | calls to other builtin functions). | |
8901 | ||
8902 | The call may contain arguments which need to be evaluated, but | |
8903 | which are not useful to determine the result of the call. In | |
8904 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
8905 | COMPOUND_EXPR will be an argument which must be evaluated. | |
8906 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
8907 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
8908 | form of the builtin function call. */ | |
8909 | ||
8910 | static tree | |
db3927fb | 8911 | fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type) |
6de9cd9a | 8912 | { |
5039610b SL |
8913 | if (!validate_arg (s1, POINTER_TYPE) |
8914 | || !validate_arg (s2, INTEGER_TYPE)) | |
8915 | return NULL_TREE; | |
6de9cd9a DN |
8916 | else |
8917 | { | |
6de9cd9a DN |
8918 | const char *p1; |
8919 | ||
8920 | if (TREE_CODE (s2) != INTEGER_CST) | |
5039610b | 8921 | return NULL_TREE; |
6de9cd9a DN |
8922 | |
8923 | p1 = c_getstr (s1); | |
8924 | if (p1 != NULL) | |
8925 | { | |
8926 | char c; | |
8927 | const char *r; | |
5fcfe0b2 | 8928 | tree tem; |
6de9cd9a DN |
8929 | |
8930 | if (target_char_cast (s2, &c)) | |
5039610b | 8931 | return NULL_TREE; |
6de9cd9a DN |
8932 | |
8933 | r = strchr (p1, c); | |
8934 | ||
8935 | if (r == NULL) | |
5212068f | 8936 | return build_int_cst (TREE_TYPE (s1), 0); |
6de9cd9a DN |
8937 | |
8938 | /* Return an offset into the constant string argument. */ | |
5d49b6a7 | 8939 | tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1); |
db3927fb | 8940 | return fold_convert_loc (loc, type, tem); |
6de9cd9a | 8941 | } |
5039610b | 8942 | return NULL_TREE; |
6de9cd9a DN |
8943 | } |
8944 | } | |
8945 | ||
5039610b SL |
8946 | /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to |
8947 | the call, and TYPE is its return type. | |
6de9cd9a | 8948 | |
5039610b | 8949 | Return NULL_TREE if no simplification was possible, otherwise return the |
6de9cd9a DN |
8950 | simplified form of the call as a tree. |
8951 | ||
8952 | The simplified form may be a constant or other expression which | |
8953 | computes the same value, but in a more efficient manner (including | |
8954 | calls to other builtin functions). | |
8955 | ||
8956 | The call may contain arguments which need to be evaluated, but | |
8957 | which are not useful to determine the result of the call. In | |
8958 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
8959 | COMPOUND_EXPR will be an argument which must be evaluated. | |
8960 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
8961 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
8962 | form of the builtin function call. */ | |
8963 | ||
8964 | static tree | |
db3927fb | 8965 | fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type) |
6de9cd9a | 8966 | { |
5039610b SL |
8967 | if (!validate_arg (s1, POINTER_TYPE) |
8968 | || !validate_arg (s2, INTEGER_TYPE)) | |
8969 | return NULL_TREE; | |
6de9cd9a DN |
8970 | else |
8971 | { | |
6de9cd9a DN |
8972 | tree fn; |
8973 | const char *p1; | |
8974 | ||
8975 | if (TREE_CODE (s2) != INTEGER_CST) | |
5039610b | 8976 | return NULL_TREE; |
6de9cd9a DN |
8977 | |
8978 | p1 = c_getstr (s1); | |
8979 | if (p1 != NULL) | |
8980 | { | |
8981 | char c; | |
8982 | const char *r; | |
5fcfe0b2 | 8983 | tree tem; |
6de9cd9a DN |
8984 | |
8985 | if (target_char_cast (s2, &c)) | |
5039610b | 8986 | return NULL_TREE; |
6de9cd9a DN |
8987 | |
8988 | r = strrchr (p1, c); | |
8989 | ||
8990 | if (r == NULL) | |
5212068f | 8991 | return build_int_cst (TREE_TYPE (s1), 0); |
6de9cd9a DN |
8992 | |
8993 | /* Return an offset into the constant string argument. */ | |
5d49b6a7 | 8994 | tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1); |
db3927fb | 8995 | return fold_convert_loc (loc, type, tem); |
6de9cd9a DN |
8996 | } |
8997 | ||
8998 | if (! integer_zerop (s2)) | |
5039610b | 8999 | return NULL_TREE; |
6de9cd9a | 9000 | |
e79983f4 | 9001 | fn = builtin_decl_implicit (BUILT_IN_STRCHR); |
6de9cd9a | 9002 | if (!fn) |
5039610b | 9003 | return NULL_TREE; |
6de9cd9a DN |
9004 | |
9005 | /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */ | |
db3927fb | 9006 | return build_call_expr_loc (loc, fn, 2, s1, s2); |
6de9cd9a DN |
9007 | } |
9008 | } | |
9009 | ||
5039610b SL |
9010 | /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments |
9011 | to the call, and TYPE is its return type. | |
6de9cd9a | 9012 | |
5039610b | 9013 | Return NULL_TREE if no simplification was possible, otherwise return the |
6de9cd9a DN |
9014 | simplified form of the call as a tree. |
9015 | ||
9016 | The simplified form may be a constant or other expression which | |
9017 | computes the same value, but in a more efficient manner (including | |
9018 | calls to other builtin functions). | |
9019 | ||
9020 | The call may contain arguments which need to be evaluated, but | |
9021 | which are not useful to determine the result of the call. In | |
9022 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
9023 | COMPOUND_EXPR will be an argument which must be evaluated. | |
9024 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
9025 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
9026 | form of the builtin function call. */ | |
9027 | ||
9028 | static tree | |
db3927fb | 9029 | fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type) |
6de9cd9a | 9030 | { |
5039610b SL |
9031 | if (!validate_arg (s1, POINTER_TYPE) |
9032 | || !validate_arg (s2, POINTER_TYPE)) | |
9033 | return NULL_TREE; | |
6de9cd9a DN |
9034 | else |
9035 | { | |
6de9cd9a DN |
9036 | tree fn; |
9037 | const char *p1, *p2; | |
9038 | ||
9039 | p2 = c_getstr (s2); | |
9040 | if (p2 == NULL) | |
5039610b | 9041 | return NULL_TREE; |
6de9cd9a DN |
9042 | |
9043 | p1 = c_getstr (s1); | |
9044 | if (p1 != NULL) | |
9045 | { | |
9046 | const char *r = strpbrk (p1, p2); | |
5fcfe0b2 | 9047 | tree tem; |
6de9cd9a DN |
9048 | |
9049 | if (r == NULL) | |
5212068f | 9050 | return build_int_cst (TREE_TYPE (s1), 0); |
6de9cd9a DN |
9051 | |
9052 | /* Return an offset into the constant string argument. */ | |
5d49b6a7 | 9053 | tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1); |
db3927fb | 9054 | return fold_convert_loc (loc, type, tem); |
6de9cd9a DN |
9055 | } |
9056 | ||
9057 | if (p2[0] == '\0') | |
d6dc556b RS |
9058 | /* strpbrk(x, "") == NULL. |
9059 | Evaluate and ignore s1 in case it had side-effects. */ | |
db3927fb | 9060 | return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1); |
6de9cd9a DN |
9061 | |
9062 | if (p2[1] != '\0') | |
5039610b | 9063 | return NULL_TREE; /* Really call strpbrk. */ |
6de9cd9a | 9064 | |
e79983f4 | 9065 | fn = builtin_decl_implicit (BUILT_IN_STRCHR); |
6de9cd9a | 9066 | if (!fn) |
5039610b | 9067 | return NULL_TREE; |
6de9cd9a DN |
9068 | |
9069 | /* New argument list transforming strpbrk(s1, s2) to | |
9070 | strchr(s1, s2[0]). */ | |
45a2c477 RG |
9071 | return build_call_expr_loc (loc, fn, 2, s1, |
9072 | build_int_cst (integer_type_node, p2[0])); | |
6de9cd9a DN |
9073 | } |
9074 | } | |
9075 | ||
5039610b SL |
9076 | /* Simplify a call to the strspn builtin. S1 and S2 are the arguments |
9077 | to the call. | |
6de9cd9a | 9078 | |
5039610b | 9079 | Return NULL_TREE if no simplification was possible, otherwise return the |
6de9cd9a DN |
9080 | simplified form of the call as a tree. |
9081 | ||
9082 | The simplified form may be a constant or other expression which | |
9083 | computes the same value, but in a more efficient manner (including | |
9084 | calls to other builtin functions). | |
9085 | ||
9086 | The call may contain arguments which need to be evaluated, but | |
9087 | which are not useful to determine the result of the call. In | |
9088 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
9089 | COMPOUND_EXPR will be an argument which must be evaluated. | |
9090 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
9091 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
9092 | form of the builtin function call. */ | |
9093 | ||
9094 | static tree | |
db3927fb | 9095 | fold_builtin_strspn (location_t loc, tree s1, tree s2) |
6de9cd9a | 9096 | { |
5039610b SL |
9097 | if (!validate_arg (s1, POINTER_TYPE) |
9098 | || !validate_arg (s2, POINTER_TYPE)) | |
9099 | return NULL_TREE; | |
6de9cd9a DN |
9100 | else |
9101 | { | |
6de9cd9a DN |
9102 | const char *p1 = c_getstr (s1), *p2 = c_getstr (s2); |
9103 | ||
5039610b | 9104 | /* If either argument is "", return NULL_TREE. */ |
6de9cd9a | 9105 | if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0')) |
08039bd8 RS |
9106 | /* Evaluate and ignore both arguments in case either one has |
9107 | side-effects. */ | |
db3927fb | 9108 | return omit_two_operands_loc (loc, size_type_node, size_zero_node, |
08039bd8 | 9109 | s1, s2); |
5039610b | 9110 | return NULL_TREE; |
6de9cd9a DN |
9111 | } |
9112 | } | |
9113 | ||
5039610b SL |
9114 | /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments |
9115 | to the call. | |
6de9cd9a | 9116 | |
5039610b | 9117 | Return NULL_TREE if no simplification was possible, otherwise return the |
6de9cd9a DN |
9118 | simplified form of the call as a tree. |
9119 | ||
9120 | The simplified form may be a constant or other expression which | |
9121 | computes the same value, but in a more efficient manner (including | |
9122 | calls to other builtin functions). | |
9123 | ||
9124 | The call may contain arguments which need to be evaluated, but | |
9125 | which are not useful to determine the result of the call. In | |
9126 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
9127 | COMPOUND_EXPR will be an argument which must be evaluated. | |
9128 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
9129 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
9130 | form of the builtin function call. */ | |
9131 | ||
9132 | static tree | |
db3927fb | 9133 | fold_builtin_strcspn (location_t loc, tree s1, tree s2) |
6de9cd9a | 9134 | { |
5039610b SL |
9135 | if (!validate_arg (s1, POINTER_TYPE) |
9136 | || !validate_arg (s2, POINTER_TYPE)) | |
9137 | return NULL_TREE; | |
6de9cd9a DN |
9138 | else |
9139 | { | |
5039610b | 9140 | /* If the first argument is "", return NULL_TREE. */ |
df838ef0 | 9141 | const char *p1 = c_getstr (s1); |
6de9cd9a DN |
9142 | if (p1 && *p1 == '\0') |
9143 | { | |
9144 | /* Evaluate and ignore argument s2 in case it has | |
9145 | side-effects. */ | |
db3927fb | 9146 | return omit_one_operand_loc (loc, size_type_node, |
002bd9f0 | 9147 | size_zero_node, s2); |
6de9cd9a DN |
9148 | } |
9149 | ||
9150 | /* If the second argument is "", return __builtin_strlen(s1). */ | |
df838ef0 | 9151 | const char *p2 = c_getstr (s2); |
6de9cd9a DN |
9152 | if (p2 && *p2 == '\0') |
9153 | { | |
e79983f4 | 9154 | tree fn = builtin_decl_implicit (BUILT_IN_STRLEN); |
6de9cd9a DN |
9155 | |
9156 | /* If the replacement _DECL isn't initialized, don't do the | |
9157 | transformation. */ | |
9158 | if (!fn) | |
5039610b | 9159 | return NULL_TREE; |
6de9cd9a | 9160 | |
db3927fb | 9161 | return build_call_expr_loc (loc, fn, 1, s1); |
6de9cd9a | 9162 | } |
5039610b | 9163 | return NULL_TREE; |
6de9cd9a DN |
9164 | } |
9165 | } | |
9166 | ||
5039610b | 9167 | /* Fold the next_arg or va_start call EXP. Returns true if there was an error |
2efcfa4e AP |
9168 | produced. False otherwise. This is done so that we don't output the error |
9169 | or warning twice or three times. */ | |
726a989a | 9170 | |
2efcfa4e | 9171 | bool |
5039610b | 9172 | fold_builtin_next_arg (tree exp, bool va_start_p) |
6de9cd9a DN |
9173 | { |
9174 | tree fntype = TREE_TYPE (current_function_decl); | |
5039610b SL |
9175 | int nargs = call_expr_nargs (exp); |
9176 | tree arg; | |
34c88790 DS |
9177 | /* There is good chance the current input_location points inside the |
9178 | definition of the va_start macro (perhaps on the token for | |
9179 | builtin) in a system header, so warnings will not be emitted. | |
9180 | Use the location in real source code. */ | |
9181 | source_location current_location = | |
9182 | linemap_unwind_to_first_non_reserved_loc (line_table, input_location, | |
9183 | NULL); | |
6de9cd9a | 9184 | |
f38958e8 | 9185 | if (!stdarg_p (fntype)) |
2efcfa4e AP |
9186 | { |
9187 | error ("%<va_start%> used in function with fixed args"); | |
9188 | return true; | |
9189 | } | |
5039610b SL |
9190 | |
9191 | if (va_start_p) | |
8870e212 | 9192 | { |
5039610b SL |
9193 | if (va_start_p && (nargs != 2)) |
9194 | { | |
9195 | error ("wrong number of arguments to function %<va_start%>"); | |
9196 | return true; | |
9197 | } | |
9198 | arg = CALL_EXPR_ARG (exp, 1); | |
8870e212 JJ |
9199 | } |
9200 | /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0) | |
9201 | when we checked the arguments and if needed issued a warning. */ | |
5039610b | 9202 | else |
6de9cd9a | 9203 | { |
5039610b SL |
9204 | if (nargs == 0) |
9205 | { | |
9206 | /* Evidently an out of date version of <stdarg.h>; can't validate | |
9207 | va_start's second argument, but can still work as intended. */ | |
34c88790 | 9208 | warning_at (current_location, |
b9c8da34 DS |
9209 | OPT_Wvarargs, |
9210 | "%<__builtin_next_arg%> called without an argument"); | |
5039610b SL |
9211 | return true; |
9212 | } | |
9213 | else if (nargs > 1) | |
c22cacf3 | 9214 | { |
5039610b | 9215 | error ("wrong number of arguments to function %<__builtin_next_arg%>"); |
c22cacf3 MS |
9216 | return true; |
9217 | } | |
5039610b SL |
9218 | arg = CALL_EXPR_ARG (exp, 0); |
9219 | } | |
9220 | ||
4e3825db MM |
9221 | if (TREE_CODE (arg) == SSA_NAME) |
9222 | arg = SSA_NAME_VAR (arg); | |
9223 | ||
5039610b | 9224 | /* We destructively modify the call to be __builtin_va_start (ap, 0) |
b8698a0f | 9225 | or __builtin_next_arg (0) the first time we see it, after checking |
5039610b SL |
9226 | the arguments and if needed issuing a warning. */ |
9227 | if (!integer_zerop (arg)) | |
9228 | { | |
9229 | tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl)); | |
8870e212 | 9230 | |
6de9cd9a DN |
9231 | /* Strip off all nops for the sake of the comparison. This |
9232 | is not quite the same as STRIP_NOPS. It does more. | |
9233 | We must also strip off INDIRECT_EXPR for C++ reference | |
9234 | parameters. */ | |
1043771b | 9235 | while (CONVERT_EXPR_P (arg) |
6de9cd9a DN |
9236 | || TREE_CODE (arg) == INDIRECT_REF) |
9237 | arg = TREE_OPERAND (arg, 0); | |
9238 | if (arg != last_parm) | |
c22cacf3 | 9239 | { |
118f3b19 KH |
9240 | /* FIXME: Sometimes with the tree optimizers we can get the |
9241 | not the last argument even though the user used the last | |
9242 | argument. We just warn and set the arg to be the last | |
9243 | argument so that we will get wrong-code because of | |
9244 | it. */ | |
34c88790 | 9245 | warning_at (current_location, |
b9c8da34 | 9246 | OPT_Wvarargs, |
34c88790 | 9247 | "second parameter of %<va_start%> not last named argument"); |
2efcfa4e | 9248 | } |
2985f531 MLI |
9249 | |
9250 | /* Undefined by C99 7.15.1.4p4 (va_start): | |
9251 | "If the parameter parmN is declared with the register storage | |
9252 | class, with a function or array type, or with a type that is | |
9253 | not compatible with the type that results after application of | |
9254 | the default argument promotions, the behavior is undefined." | |
9255 | */ | |
9256 | else if (DECL_REGISTER (arg)) | |
34c88790 DS |
9257 | { |
9258 | warning_at (current_location, | |
b9c8da34 | 9259 | OPT_Wvarargs, |
9c582551 | 9260 | "undefined behavior when second parameter of " |
34c88790 DS |
9261 | "%<va_start%> is declared with %<register%> storage"); |
9262 | } | |
2985f531 | 9263 | |
8870e212 | 9264 | /* We want to verify the second parameter just once before the tree |
c22cacf3 MS |
9265 | optimizers are run and then avoid keeping it in the tree, |
9266 | as otherwise we could warn even for correct code like: | |
9267 | void foo (int i, ...) | |
9268 | { va_list ap; i++; va_start (ap, i); va_end (ap); } */ | |
5039610b SL |
9269 | if (va_start_p) |
9270 | CALL_EXPR_ARG (exp, 1) = integer_zero_node; | |
9271 | else | |
9272 | CALL_EXPR_ARG (exp, 0) = integer_zero_node; | |
2efcfa4e AP |
9273 | } |
9274 | return false; | |
6de9cd9a DN |
9275 | } |
9276 | ||
9277 | ||
5039610b | 9278 | /* Expand a call EXP to __builtin_object_size. */ |
10a0d495 | 9279 | |
9b2b7279 | 9280 | static rtx |
10a0d495 JJ |
9281 | expand_builtin_object_size (tree exp) |
9282 | { | |
9283 | tree ost; | |
9284 | int object_size_type; | |
9285 | tree fndecl = get_callee_fndecl (exp); | |
10a0d495 | 9286 | |
5039610b | 9287 | if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
10a0d495 | 9288 | { |
c94ed7a1 JJ |
9289 | error ("%Kfirst argument of %D must be a pointer, second integer constant", |
9290 | exp, fndecl); | |
10a0d495 JJ |
9291 | expand_builtin_trap (); |
9292 | return const0_rtx; | |
9293 | } | |
9294 | ||
5039610b | 9295 | ost = CALL_EXPR_ARG (exp, 1); |
10a0d495 JJ |
9296 | STRIP_NOPS (ost); |
9297 | ||
9298 | if (TREE_CODE (ost) != INTEGER_CST | |
9299 | || tree_int_cst_sgn (ost) < 0 | |
9300 | || compare_tree_int (ost, 3) > 0) | |
9301 | { | |
c94ed7a1 JJ |
9302 | error ("%Klast argument of %D is not integer constant between 0 and 3", |
9303 | exp, fndecl); | |
10a0d495 JJ |
9304 | expand_builtin_trap (); |
9305 | return const0_rtx; | |
9306 | } | |
9307 | ||
9439e9a1 | 9308 | object_size_type = tree_to_shwi (ost); |
10a0d495 JJ |
9309 | |
9310 | return object_size_type < 2 ? constm1_rtx : const0_rtx; | |
9311 | } | |
9312 | ||
9313 | /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin. | |
9314 | FCODE is the BUILT_IN_* to use. | |
5039610b | 9315 | Return NULL_RTX if we failed; the caller should emit a normal call, |
10a0d495 JJ |
9316 | otherwise try to get the result in TARGET, if convenient (and in |
9317 | mode MODE if that's convenient). */ | |
9318 | ||
9319 | static rtx | |
ef4bddc2 | 9320 | expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode, |
10a0d495 JJ |
9321 | enum built_in_function fcode) |
9322 | { | |
10a0d495 JJ |
9323 | tree dest, src, len, size; |
9324 | ||
5039610b | 9325 | if (!validate_arglist (exp, |
10a0d495 JJ |
9326 | POINTER_TYPE, |
9327 | fcode == BUILT_IN_MEMSET_CHK | |
9328 | ? INTEGER_TYPE : POINTER_TYPE, | |
9329 | INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
5039610b | 9330 | return NULL_RTX; |
10a0d495 | 9331 | |
5039610b SL |
9332 | dest = CALL_EXPR_ARG (exp, 0); |
9333 | src = CALL_EXPR_ARG (exp, 1); | |
9334 | len = CALL_EXPR_ARG (exp, 2); | |
9335 | size = CALL_EXPR_ARG (exp, 3); | |
10a0d495 | 9336 | |
cc269bb6 | 9337 | if (! tree_fits_uhwi_p (size)) |
5039610b | 9338 | return NULL_RTX; |
10a0d495 | 9339 | |
cc269bb6 | 9340 | if (tree_fits_uhwi_p (len) || integer_all_onesp (size)) |
10a0d495 JJ |
9341 | { |
9342 | tree fn; | |
9343 | ||
9344 | if (! integer_all_onesp (size) && tree_int_cst_lt (size, len)) | |
9345 | { | |
9bd9f738 RG |
9346 | warning_at (tree_nonartificial_location (exp), |
9347 | 0, "%Kcall to %D will always overflow destination buffer", | |
9348 | exp, get_callee_fndecl (exp)); | |
5039610b | 9349 | return NULL_RTX; |
10a0d495 JJ |
9350 | } |
9351 | ||
10a0d495 JJ |
9352 | fn = NULL_TREE; |
9353 | /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume | |
9354 | mem{cpy,pcpy,move,set} is available. */ | |
9355 | switch (fcode) | |
9356 | { | |
9357 | case BUILT_IN_MEMCPY_CHK: | |
e79983f4 | 9358 | fn = builtin_decl_explicit (BUILT_IN_MEMCPY); |
10a0d495 JJ |
9359 | break; |
9360 | case BUILT_IN_MEMPCPY_CHK: | |
e79983f4 | 9361 | fn = builtin_decl_explicit (BUILT_IN_MEMPCPY); |
10a0d495 JJ |
9362 | break; |
9363 | case BUILT_IN_MEMMOVE_CHK: | |
e79983f4 | 9364 | fn = builtin_decl_explicit (BUILT_IN_MEMMOVE); |
10a0d495 JJ |
9365 | break; |
9366 | case BUILT_IN_MEMSET_CHK: | |
e79983f4 | 9367 | fn = builtin_decl_explicit (BUILT_IN_MEMSET); |
10a0d495 JJ |
9368 | break; |
9369 | default: | |
9370 | break; | |
9371 | } | |
9372 | ||
9373 | if (! fn) | |
5039610b | 9374 | return NULL_RTX; |
10a0d495 | 9375 | |
aa493694 | 9376 | fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len); |
44e10129 MM |
9377 | gcc_assert (TREE_CODE (fn) == CALL_EXPR); |
9378 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
10a0d495 JJ |
9379 | return expand_expr (fn, target, mode, EXPAND_NORMAL); |
9380 | } | |
9381 | else if (fcode == BUILT_IN_MEMSET_CHK) | |
5039610b | 9382 | return NULL_RTX; |
10a0d495 JJ |
9383 | else |
9384 | { | |
0eb77834 | 9385 | unsigned int dest_align = get_pointer_alignment (dest); |
10a0d495 JJ |
9386 | |
9387 | /* If DEST is not a pointer type, call the normal function. */ | |
9388 | if (dest_align == 0) | |
5039610b | 9389 | return NULL_RTX; |
10a0d495 JJ |
9390 | |
9391 | /* If SRC and DEST are the same (and not volatile), do nothing. */ | |
9392 | if (operand_equal_p (src, dest, 0)) | |
9393 | { | |
9394 | tree expr; | |
9395 | ||
9396 | if (fcode != BUILT_IN_MEMPCPY_CHK) | |
9397 | { | |
9398 | /* Evaluate and ignore LEN in case it has side-effects. */ | |
9399 | expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
9400 | return expand_expr (dest, target, mode, EXPAND_NORMAL); | |
9401 | } | |
9402 | ||
5d49b6a7 | 9403 | expr = fold_build_pointer_plus (dest, len); |
10a0d495 JJ |
9404 | return expand_expr (expr, target, mode, EXPAND_NORMAL); |
9405 | } | |
9406 | ||
9407 | /* __memmove_chk special case. */ | |
9408 | if (fcode == BUILT_IN_MEMMOVE_CHK) | |
9409 | { | |
0eb77834 | 9410 | unsigned int src_align = get_pointer_alignment (src); |
10a0d495 JJ |
9411 | |
9412 | if (src_align == 0) | |
5039610b | 9413 | return NULL_RTX; |
10a0d495 JJ |
9414 | |
9415 | /* If src is categorized for a readonly section we can use | |
9416 | normal __memcpy_chk. */ | |
9417 | if (readonly_data_expr (src)) | |
9418 | { | |
e79983f4 | 9419 | tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK); |
10a0d495 | 9420 | if (!fn) |
5039610b | 9421 | return NULL_RTX; |
aa493694 JJ |
9422 | fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4, |
9423 | dest, src, len, size); | |
44e10129 MM |
9424 | gcc_assert (TREE_CODE (fn) == CALL_EXPR); |
9425 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
10a0d495 JJ |
9426 | return expand_expr (fn, target, mode, EXPAND_NORMAL); |
9427 | } | |
9428 | } | |
5039610b | 9429 | return NULL_RTX; |
10a0d495 JJ |
9430 | } |
9431 | } | |
9432 | ||
9433 | /* Emit warning if a buffer overflow is detected at compile time. */ | |
9434 | ||
9435 | static void | |
9436 | maybe_emit_chk_warning (tree exp, enum built_in_function fcode) | |
9437 | { | |
5039610b | 9438 | int is_strlen = 0; |
10a0d495 | 9439 | tree len, size; |
9bd9f738 | 9440 | location_t loc = tree_nonartificial_location (exp); |
10a0d495 JJ |
9441 | |
9442 | switch (fcode) | |
9443 | { | |
9444 | case BUILT_IN_STRCPY_CHK: | |
9445 | case BUILT_IN_STPCPY_CHK: | |
9446 | /* For __strcat_chk the warning will be emitted only if overflowing | |
9447 | by at least strlen (dest) + 1 bytes. */ | |
9448 | case BUILT_IN_STRCAT_CHK: | |
5039610b SL |
9449 | len = CALL_EXPR_ARG (exp, 1); |
9450 | size = CALL_EXPR_ARG (exp, 2); | |
10a0d495 JJ |
9451 | is_strlen = 1; |
9452 | break; | |
1c2fc017 | 9453 | case BUILT_IN_STRNCAT_CHK: |
10a0d495 | 9454 | case BUILT_IN_STRNCPY_CHK: |
f3fc9b80 | 9455 | case BUILT_IN_STPNCPY_CHK: |
5039610b SL |
9456 | len = CALL_EXPR_ARG (exp, 2); |
9457 | size = CALL_EXPR_ARG (exp, 3); | |
10a0d495 JJ |
9458 | break; |
9459 | case BUILT_IN_SNPRINTF_CHK: | |
9460 | case BUILT_IN_VSNPRINTF_CHK: | |
5039610b SL |
9461 | len = CALL_EXPR_ARG (exp, 1); |
9462 | size = CALL_EXPR_ARG (exp, 3); | |
10a0d495 JJ |
9463 | break; |
9464 | default: | |
9465 | gcc_unreachable (); | |
9466 | } | |
9467 | ||
10a0d495 JJ |
9468 | if (!len || !size) |
9469 | return; | |
9470 | ||
cc269bb6 | 9471 | if (! tree_fits_uhwi_p (size) || integer_all_onesp (size)) |
10a0d495 JJ |
9472 | return; |
9473 | ||
9474 | if (is_strlen) | |
9475 | { | |
9476 | len = c_strlen (len, 1); | |
cc269bb6 | 9477 | if (! len || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size)) |
10a0d495 JJ |
9478 | return; |
9479 | } | |
1c2fc017 JJ |
9480 | else if (fcode == BUILT_IN_STRNCAT_CHK) |
9481 | { | |
5039610b | 9482 | tree src = CALL_EXPR_ARG (exp, 1); |
cc269bb6 | 9483 | if (! src || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size)) |
1c2fc017 JJ |
9484 | return; |
9485 | src = c_strlen (src, 1); | |
cc269bb6 | 9486 | if (! src || ! tree_fits_uhwi_p (src)) |
1c2fc017 | 9487 | { |
9bd9f738 RG |
9488 | warning_at (loc, 0, "%Kcall to %D might overflow destination buffer", |
9489 | exp, get_callee_fndecl (exp)); | |
1c2fc017 JJ |
9490 | return; |
9491 | } | |
9492 | else if (tree_int_cst_lt (src, size)) | |
9493 | return; | |
9494 | } | |
cc269bb6 | 9495 | else if (! tree_fits_uhwi_p (len) || ! tree_int_cst_lt (size, len)) |
10a0d495 JJ |
9496 | return; |
9497 | ||
9bd9f738 RG |
9498 | warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer", |
9499 | exp, get_callee_fndecl (exp)); | |
10a0d495 JJ |
9500 | } |
9501 | ||
9502 | /* Emit warning if a buffer overflow is detected at compile time | |
9503 | in __sprintf_chk/__vsprintf_chk calls. */ | |
9504 | ||
9505 | static void | |
9506 | maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode) | |
9507 | { | |
451409e4 | 9508 | tree size, len, fmt; |
10a0d495 | 9509 | const char *fmt_str; |
5039610b | 9510 | int nargs = call_expr_nargs (exp); |
10a0d495 JJ |
9511 | |
9512 | /* Verify the required arguments in the original call. */ | |
b8698a0f | 9513 | |
5039610b | 9514 | if (nargs < 4) |
10a0d495 | 9515 | return; |
5039610b SL |
9516 | size = CALL_EXPR_ARG (exp, 2); |
9517 | fmt = CALL_EXPR_ARG (exp, 3); | |
10a0d495 | 9518 | |
cc269bb6 | 9519 | if (! tree_fits_uhwi_p (size) || integer_all_onesp (size)) |
10a0d495 JJ |
9520 | return; |
9521 | ||
9522 | /* Check whether the format is a literal string constant. */ | |
9523 | fmt_str = c_getstr (fmt); | |
9524 | if (fmt_str == NULL) | |
9525 | return; | |
9526 | ||
62e5bf5d | 9527 | if (!init_target_chars ()) |
000ba23d KG |
9528 | return; |
9529 | ||
10a0d495 | 9530 | /* If the format doesn't contain % args or %%, we know its size. */ |
000ba23d | 9531 | if (strchr (fmt_str, target_percent) == 0) |
10a0d495 JJ |
9532 | len = build_int_cstu (size_type_node, strlen (fmt_str)); |
9533 | /* If the format is "%s" and first ... argument is a string literal, | |
9534 | we know it too. */ | |
5039610b SL |
9535 | else if (fcode == BUILT_IN_SPRINTF_CHK |
9536 | && strcmp (fmt_str, target_percent_s) == 0) | |
10a0d495 JJ |
9537 | { |
9538 | tree arg; | |
9539 | ||
5039610b | 9540 | if (nargs < 5) |
10a0d495 | 9541 | return; |
5039610b | 9542 | arg = CALL_EXPR_ARG (exp, 4); |
10a0d495 JJ |
9543 | if (! POINTER_TYPE_P (TREE_TYPE (arg))) |
9544 | return; | |
9545 | ||
9546 | len = c_strlen (arg, 1); | |
cc269bb6 | 9547 | if (!len || ! tree_fits_uhwi_p (len)) |
10a0d495 JJ |
9548 | return; |
9549 | } | |
9550 | else | |
9551 | return; | |
9552 | ||
9553 | if (! tree_int_cst_lt (len, size)) | |
9bd9f738 RG |
9554 | warning_at (tree_nonartificial_location (exp), |
9555 | 0, "%Kcall to %D will always overflow destination buffer", | |
9556 | exp, get_callee_fndecl (exp)); | |
10a0d495 JJ |
9557 | } |
9558 | ||
f9555f40 JJ |
9559 | /* Emit warning if a free is called with address of a variable. */ |
9560 | ||
9561 | static void | |
9562 | maybe_emit_free_warning (tree exp) | |
9563 | { | |
9564 | tree arg = CALL_EXPR_ARG (exp, 0); | |
9565 | ||
9566 | STRIP_NOPS (arg); | |
9567 | if (TREE_CODE (arg) != ADDR_EXPR) | |
9568 | return; | |
9569 | ||
9570 | arg = get_base_address (TREE_OPERAND (arg, 0)); | |
70f34814 | 9571 | if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF) |
f9555f40 JJ |
9572 | return; |
9573 | ||
9574 | if (SSA_VAR_P (arg)) | |
a3a704a4 MH |
9575 | warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object, |
9576 | "%Kattempt to free a non-heap object %qD", exp, arg); | |
f9555f40 | 9577 | else |
a3a704a4 MH |
9578 | warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object, |
9579 | "%Kattempt to free a non-heap object", exp); | |
f9555f40 JJ |
9580 | } |
9581 | ||
5039610b SL |
9582 | /* Fold a call to __builtin_object_size with arguments PTR and OST, |
9583 | if possible. */ | |
10a0d495 | 9584 | |
9b2b7279 | 9585 | static tree |
5039610b | 9586 | fold_builtin_object_size (tree ptr, tree ost) |
10a0d495 | 9587 | { |
88e06841 | 9588 | unsigned HOST_WIDE_INT bytes; |
10a0d495 JJ |
9589 | int object_size_type; |
9590 | ||
5039610b SL |
9591 | if (!validate_arg (ptr, POINTER_TYPE) |
9592 | || !validate_arg (ost, INTEGER_TYPE)) | |
9593 | return NULL_TREE; | |
10a0d495 | 9594 | |
10a0d495 JJ |
9595 | STRIP_NOPS (ost); |
9596 | ||
9597 | if (TREE_CODE (ost) != INTEGER_CST | |
9598 | || tree_int_cst_sgn (ost) < 0 | |
9599 | || compare_tree_int (ost, 3) > 0) | |
5039610b | 9600 | return NULL_TREE; |
10a0d495 | 9601 | |
9439e9a1 | 9602 | object_size_type = tree_to_shwi (ost); |
10a0d495 JJ |
9603 | |
9604 | /* __builtin_object_size doesn't evaluate side-effects in its arguments; | |
9605 | if there are any side-effects, it returns (size_t) -1 for types 0 and 1 | |
9606 | and (size_t) 0 for types 2 and 3. */ | |
9607 | if (TREE_SIDE_EFFECTS (ptr)) | |
2ac7cbb5 | 9608 | return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0); |
10a0d495 JJ |
9609 | |
9610 | if (TREE_CODE (ptr) == ADDR_EXPR) | |
88e06841 | 9611 | { |
05a64756 | 9612 | compute_builtin_object_size (ptr, object_size_type, &bytes); |
807e902e | 9613 | if (wi::fits_to_tree_p (bytes, size_type_node)) |
88e06841 AS |
9614 | return build_int_cstu (size_type_node, bytes); |
9615 | } | |
10a0d495 JJ |
9616 | else if (TREE_CODE (ptr) == SSA_NAME) |
9617 | { | |
10a0d495 JJ |
9618 | /* If object size is not known yet, delay folding until |
9619 | later. Maybe subsequent passes will help determining | |
9620 | it. */ | |
05a64756 MS |
9621 | if (compute_builtin_object_size (ptr, object_size_type, &bytes) |
9622 | && wi::fits_to_tree_p (bytes, size_type_node)) | |
88e06841 | 9623 | return build_int_cstu (size_type_node, bytes); |
10a0d495 JJ |
9624 | } |
9625 | ||
88e06841 | 9626 | return NULL_TREE; |
10a0d495 JJ |
9627 | } |
9628 | ||
862d0b35 DN |
9629 | /* Builtins with folding operations that operate on "..." arguments |
9630 | need special handling; we need to store the arguments in a convenient | |
9631 | data structure before attempting any folding. Fortunately there are | |
9632 | only a few builtins that fall into this category. FNDECL is the | |
2625bb5d | 9633 | function, EXP is the CALL_EXPR for the call. */ |
862d0b35 DN |
9634 | |
9635 | static tree | |
2625bb5d | 9636 | fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs) |
862d0b35 DN |
9637 | { |
9638 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
9639 | tree ret = NULL_TREE; | |
9640 | ||
9641 | switch (fcode) | |
9642 | { | |
862d0b35 | 9643 | case BUILT_IN_FPCLASSIFY: |
a6a0570f | 9644 | ret = fold_builtin_fpclassify (loc, args, nargs); |
862d0b35 DN |
9645 | break; |
9646 | ||
9647 | default: | |
9648 | break; | |
9649 | } | |
9650 | if (ret) | |
9651 | { | |
9652 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); | |
9653 | SET_EXPR_LOCATION (ret, loc); | |
9654 | TREE_NO_WARNING (ret) = 1; | |
9655 | return ret; | |
9656 | } | |
9657 | return NULL_TREE; | |
9658 | } | |
9659 | ||
000ba23d KG |
9660 | /* Initialize format string characters in the target charset. */ |
9661 | ||
fef5a0d9 | 9662 | bool |
000ba23d KG |
9663 | init_target_chars (void) |
9664 | { | |
9665 | static bool init; | |
9666 | if (!init) | |
9667 | { | |
9668 | target_newline = lang_hooks.to_target_charset ('\n'); | |
9669 | target_percent = lang_hooks.to_target_charset ('%'); | |
9670 | target_c = lang_hooks.to_target_charset ('c'); | |
9671 | target_s = lang_hooks.to_target_charset ('s'); | |
9672 | if (target_newline == 0 || target_percent == 0 || target_c == 0 | |
9673 | || target_s == 0) | |
9674 | return false; | |
9675 | ||
9676 | target_percent_c[0] = target_percent; | |
9677 | target_percent_c[1] = target_c; | |
9678 | target_percent_c[2] = '\0'; | |
9679 | ||
9680 | target_percent_s[0] = target_percent; | |
9681 | target_percent_s[1] = target_s; | |
9682 | target_percent_s[2] = '\0'; | |
9683 | ||
9684 | target_percent_s_newline[0] = target_percent; | |
9685 | target_percent_s_newline[1] = target_s; | |
9686 | target_percent_s_newline[2] = target_newline; | |
9687 | target_percent_s_newline[3] = '\0'; | |
c22cacf3 | 9688 | |
000ba23d KG |
9689 | init = true; |
9690 | } | |
9691 | return true; | |
9692 | } | |
1f3f1f68 | 9693 | |
4413d881 KG |
9694 | /* Helper function for do_mpfr_arg*(). Ensure M is a normal number |
9695 | and no overflow/underflow occurred. INEXACT is true if M was not | |
2f8e468b | 9696 | exactly calculated. TYPE is the tree type for the result. This |
4413d881 KG |
9697 | function assumes that you cleared the MPFR flags and then |
9698 | calculated M to see if anything subsequently set a flag prior to | |
9699 | entering this function. Return NULL_TREE if any checks fail. */ | |
9700 | ||
9701 | static tree | |
62e5bf5d | 9702 | do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact) |
4413d881 KG |
9703 | { |
9704 | /* Proceed iff we get a normal number, i.e. not NaN or Inf and no | |
9705 | overflow/underflow occurred. If -frounding-math, proceed iff the | |
9706 | result of calling FUNC was exact. */ | |
62e5bf5d | 9707 | if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p () |
4413d881 KG |
9708 | && (!flag_rounding_math || !inexact)) |
9709 | { | |
9710 | REAL_VALUE_TYPE rr; | |
9711 | ||
205a4d09 | 9712 | real_from_mpfr (&rr, m, type, GMP_RNDN); |
4413d881 KG |
9713 | /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value, |
9714 | check for overflow/underflow. If the REAL_VALUE_TYPE is zero | |
9715 | but the mpft_t is not, then we underflowed in the | |
9716 | conversion. */ | |
4c8c70e0 | 9717 | if (real_isfinite (&rr) |
4413d881 KG |
9718 | && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0)) |
9719 | { | |
9720 | REAL_VALUE_TYPE rmode; | |
9721 | ||
9722 | real_convert (&rmode, TYPE_MODE (type), &rr); | |
9723 | /* Proceed iff the specified mode can hold the value. */ | |
9724 | if (real_identical (&rmode, &rr)) | |
9725 | return build_real (type, rmode); | |
9726 | } | |
9727 | } | |
9728 | return NULL_TREE; | |
9729 | } | |
9730 | ||
c128599a KG |
9731 | /* Helper function for do_mpc_arg*(). Ensure M is a normal complex |
9732 | number and no overflow/underflow occurred. INEXACT is true if M | |
9733 | was not exactly calculated. TYPE is the tree type for the result. | |
9734 | This function assumes that you cleared the MPFR flags and then | |
9735 | calculated M to see if anything subsequently set a flag prior to | |
ca75b926 KG |
9736 | entering this function. Return NULL_TREE if any checks fail, if |
9737 | FORCE_CONVERT is true, then bypass the checks. */ | |
c128599a KG |
9738 | |
9739 | static tree | |
ca75b926 | 9740 | do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert) |
c128599a KG |
9741 | { |
9742 | /* Proceed iff we get a normal number, i.e. not NaN or Inf and no | |
9743 | overflow/underflow occurred. If -frounding-math, proceed iff the | |
9744 | result of calling FUNC was exact. */ | |
ca75b926 KG |
9745 | if (force_convert |
9746 | || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m)) | |
9747 | && !mpfr_overflow_p () && !mpfr_underflow_p () | |
9748 | && (!flag_rounding_math || !inexact))) | |
c128599a KG |
9749 | { |
9750 | REAL_VALUE_TYPE re, im; | |
9751 | ||
14aa6352 DE |
9752 | real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN); |
9753 | real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN); | |
c128599a KG |
9754 | /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values, |
9755 | check for overflow/underflow. If the REAL_VALUE_TYPE is zero | |
9756 | but the mpft_t is not, then we underflowed in the | |
9757 | conversion. */ | |
ca75b926 KG |
9758 | if (force_convert |
9759 | || (real_isfinite (&re) && real_isfinite (&im) | |
9760 | && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0) | |
9761 | && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0))) | |
c128599a KG |
9762 | { |
9763 | REAL_VALUE_TYPE re_mode, im_mode; | |
9764 | ||
9765 | real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re); | |
9766 | real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im); | |
9767 | /* Proceed iff the specified mode can hold the value. */ | |
ca75b926 KG |
9768 | if (force_convert |
9769 | || (real_identical (&re_mode, &re) | |
9770 | && real_identical (&im_mode, &im))) | |
c128599a KG |
9771 | return build_complex (type, build_real (TREE_TYPE (type), re_mode), |
9772 | build_real (TREE_TYPE (type), im_mode)); | |
9773 | } | |
9774 | } | |
9775 | return NULL_TREE; | |
9776 | } | |
c128599a | 9777 | |
ea91f957 KG |
9778 | /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set |
9779 | the pointer *(ARG_QUO) and return the result. The type is taken | |
9780 | from the type of ARG0 and is used for setting the precision of the | |
9781 | calculation and results. */ | |
9782 | ||
9783 | static tree | |
9784 | do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo) | |
9785 | { | |
9786 | tree const type = TREE_TYPE (arg0); | |
9787 | tree result = NULL_TREE; | |
b8698a0f | 9788 | |
ea91f957 KG |
9789 | STRIP_NOPS (arg0); |
9790 | STRIP_NOPS (arg1); | |
b8698a0f | 9791 | |
ea91f957 KG |
9792 | /* To proceed, MPFR must exactly represent the target floating point |
9793 | format, which only happens when the target base equals two. */ | |
9794 | if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2 | |
9795 | && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0) | |
9796 | && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)) | |
9797 | { | |
9798 | const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0); | |
9799 | const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1); | |
9800 | ||
4c8c70e0 | 9801 | if (real_isfinite (ra0) && real_isfinite (ra1)) |
ea91f957 | 9802 | { |
3e479de3 UW |
9803 | const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type)); |
9804 | const int prec = fmt->p; | |
9805 | const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN; | |
ea91f957 KG |
9806 | tree result_rem; |
9807 | long integer_quo; | |
9808 | mpfr_t m0, m1; | |
9809 | ||
9810 | mpfr_inits2 (prec, m0, m1, NULL); | |
9811 | mpfr_from_real (m0, ra0, GMP_RNDN); | |
9812 | mpfr_from_real (m1, ra1, GMP_RNDN); | |
9813 | mpfr_clear_flags (); | |
3e479de3 | 9814 | mpfr_remquo (m0, &integer_quo, m0, m1, rnd); |
ea91f957 KG |
9815 | /* Remquo is independent of the rounding mode, so pass |
9816 | inexact=0 to do_mpfr_ckconv(). */ | |
9817 | result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0); | |
9818 | mpfr_clears (m0, m1, NULL); | |
9819 | if (result_rem) | |
9820 | { | |
9821 | /* MPFR calculates quo in the host's long so it may | |
9822 | return more bits in quo than the target int can hold | |
9823 | if sizeof(host long) > sizeof(target int). This can | |
9824 | happen even for native compilers in LP64 mode. In | |
9825 | these cases, modulo the quo value with the largest | |
9826 | number that the target int can hold while leaving one | |
9827 | bit for the sign. */ | |
9828 | if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE) | |
9829 | integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1)); | |
9830 | ||
9831 | /* Dereference the quo pointer argument. */ | |
9832 | arg_quo = build_fold_indirect_ref (arg_quo); | |
9833 | /* Proceed iff a valid pointer type was passed in. */ | |
9834 | if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node) | |
9835 | { | |
9836 | /* Set the value. */ | |
45a2c477 RG |
9837 | tree result_quo |
9838 | = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo, | |
9839 | build_int_cst (TREE_TYPE (arg_quo), | |
9840 | integer_quo)); | |
ea91f957 KG |
9841 | TREE_SIDE_EFFECTS (result_quo) = 1; |
9842 | /* Combine the quo assignment with the rem. */ | |
9843 | result = non_lvalue (fold_build2 (COMPOUND_EXPR, type, | |
9844 | result_quo, result_rem)); | |
9845 | } | |
9846 | } | |
9847 | } | |
9848 | } | |
9849 | return result; | |
9850 | } | |
752b7d38 KG |
9851 | |
9852 | /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the | |
9853 | resulting value as a tree with type TYPE. The mpfr precision is | |
9854 | set to the precision of TYPE. We assume that this mpfr function | |
9855 | returns zero if the result could be calculated exactly within the | |
9856 | requested precision. In addition, the integer pointer represented | |
9857 | by ARG_SG will be dereferenced and set to the appropriate signgam | |
9858 | (-1,1) value. */ | |
9859 | ||
9860 | static tree | |
9861 | do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type) | |
9862 | { | |
9863 | tree result = NULL_TREE; | |
9864 | ||
9865 | STRIP_NOPS (arg); | |
b8698a0f | 9866 | |
752b7d38 KG |
9867 | /* To proceed, MPFR must exactly represent the target floating point |
9868 | format, which only happens when the target base equals two. Also | |
9869 | verify ARG is a constant and that ARG_SG is an int pointer. */ | |
9870 | if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2 | |
9871 | && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg) | |
9872 | && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE | |
9873 | && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node) | |
9874 | { | |
9875 | const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg); | |
9876 | ||
9877 | /* In addition to NaN and Inf, the argument cannot be zero or a | |
9878 | negative integer. */ | |
4c8c70e0 | 9879 | if (real_isfinite (ra) |
752b7d38 | 9880 | && ra->cl != rvc_zero |
c3284718 | 9881 | && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type)))) |
752b7d38 | 9882 | { |
3e479de3 UW |
9883 | const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type)); |
9884 | const int prec = fmt->p; | |
9885 | const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN; | |
752b7d38 KG |
9886 | int inexact, sg; |
9887 | mpfr_t m; | |
9888 | tree result_lg; | |
9889 | ||
9890 | mpfr_init2 (m, prec); | |
9891 | mpfr_from_real (m, ra, GMP_RNDN); | |
9892 | mpfr_clear_flags (); | |
3e479de3 | 9893 | inexact = mpfr_lgamma (m, &sg, m, rnd); |
752b7d38 KG |
9894 | result_lg = do_mpfr_ckconv (m, type, inexact); |
9895 | mpfr_clear (m); | |
9896 | if (result_lg) | |
9897 | { | |
9898 | tree result_sg; | |
9899 | ||
9900 | /* Dereference the arg_sg pointer argument. */ | |
9901 | arg_sg = build_fold_indirect_ref (arg_sg); | |
9902 | /* Assign the signgam value into *arg_sg. */ | |
9903 | result_sg = fold_build2 (MODIFY_EXPR, | |
9904 | TREE_TYPE (arg_sg), arg_sg, | |
45a2c477 | 9905 | build_int_cst (TREE_TYPE (arg_sg), sg)); |
752b7d38 KG |
9906 | TREE_SIDE_EFFECTS (result_sg) = 1; |
9907 | /* Combine the signgam assignment with the lgamma result. */ | |
9908 | result = non_lvalue (fold_build2 (COMPOUND_EXPR, type, | |
9909 | result_sg, result_lg)); | |
9910 | } | |
9911 | } | |
9912 | } | |
9913 | ||
9914 | return result; | |
9915 | } | |
726a989a | 9916 | |
a41d064d KG |
9917 | /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument |
9918 | mpc function FUNC on it and return the resulting value as a tree | |
9919 | with type TYPE. The mpfr precision is set to the precision of | |
9920 | TYPE. We assume that function FUNC returns zero if the result | |
ca75b926 KG |
9921 | could be calculated exactly within the requested precision. If |
9922 | DO_NONFINITE is true, then fold expressions containing Inf or NaN | |
9923 | in the arguments and/or results. */ | |
a41d064d | 9924 | |
2f440f6a | 9925 | tree |
ca75b926 | 9926 | do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite, |
a41d064d KG |
9927 | int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t)) |
9928 | { | |
9929 | tree result = NULL_TREE; | |
b8698a0f | 9930 | |
a41d064d KG |
9931 | STRIP_NOPS (arg0); |
9932 | STRIP_NOPS (arg1); | |
9933 | ||
9934 | /* To proceed, MPFR must exactly represent the target floating point | |
9935 | format, which only happens when the target base equals two. */ | |
9936 | if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0) | |
9937 | && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE | |
9938 | && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1) | |
9939 | && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE | |
9940 | && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2) | |
9941 | { | |
9942 | const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0)); | |
9943 | const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0)); | |
9944 | const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1)); | |
9945 | const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1)); | |
9946 | ||
ca75b926 KG |
9947 | if (do_nonfinite |
9948 | || (real_isfinite (re0) && real_isfinite (im0) | |
9949 | && real_isfinite (re1) && real_isfinite (im1))) | |
a41d064d KG |
9950 | { |
9951 | const struct real_format *const fmt = | |
9952 | REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type))); | |
9953 | const int prec = fmt->p; | |
9954 | const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN; | |
9955 | const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN; | |
9956 | int inexact; | |
9957 | mpc_t m0, m1; | |
b8698a0f | 9958 | |
a41d064d KG |
9959 | mpc_init2 (m0, prec); |
9960 | mpc_init2 (m1, prec); | |
c3284718 RS |
9961 | mpfr_from_real (mpc_realref (m0), re0, rnd); |
9962 | mpfr_from_real (mpc_imagref (m0), im0, rnd); | |
9963 | mpfr_from_real (mpc_realref (m1), re1, rnd); | |
9964 | mpfr_from_real (mpc_imagref (m1), im1, rnd); | |
a41d064d KG |
9965 | mpfr_clear_flags (); |
9966 | inexact = func (m0, m0, m1, crnd); | |
ca75b926 | 9967 | result = do_mpc_ckconv (m0, type, inexact, do_nonfinite); |
a41d064d KG |
9968 | mpc_clear (m0); |
9969 | mpc_clear (m1); | |
9970 | } | |
9971 | } | |
9972 | ||
9973 | return result; | |
9974 | } | |
c128599a | 9975 | |
726a989a RB |
9976 | /* A wrapper function for builtin folding that prevents warnings for |
9977 | "statement without effect" and the like, caused by removing the | |
9978 | call node earlier than the warning is generated. */ | |
9979 | ||
9980 | tree | |
538dd0b7 | 9981 | fold_call_stmt (gcall *stmt, bool ignore) |
726a989a RB |
9982 | { |
9983 | tree ret = NULL_TREE; | |
9984 | tree fndecl = gimple_call_fndecl (stmt); | |
db3927fb | 9985 | location_t loc = gimple_location (stmt); |
726a989a RB |
9986 | if (fndecl |
9987 | && TREE_CODE (fndecl) == FUNCTION_DECL | |
9988 | && DECL_BUILT_IN (fndecl) | |
9989 | && !gimple_call_va_arg_pack_p (stmt)) | |
9990 | { | |
9991 | int nargs = gimple_call_num_args (stmt); | |
8897c9ce NF |
9992 | tree *args = (nargs > 0 |
9993 | ? gimple_call_arg_ptr (stmt, 0) | |
9994 | : &error_mark_node); | |
726a989a | 9995 | |
0889e9bc JJ |
9996 | if (avoid_folding_inline_builtin (fndecl)) |
9997 | return NULL_TREE; | |
726a989a RB |
9998 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) |
9999 | { | |
8897c9ce | 10000 | return targetm.fold_builtin (fndecl, nargs, args, ignore); |
726a989a RB |
10001 | } |
10002 | else | |
10003 | { | |
a6a0570f | 10004 | ret = fold_builtin_n (loc, fndecl, args, nargs, ignore); |
726a989a RB |
10005 | if (ret) |
10006 | { | |
10007 | /* Propagate location information from original call to | |
10008 | expansion of builtin. Otherwise things like | |
10009 | maybe_emit_chk_warning, that operate on the expansion | |
10010 | of a builtin, will use the wrong location information. */ | |
10011 | if (gimple_has_location (stmt)) | |
10012 | { | |
10013 | tree realret = ret; | |
10014 | if (TREE_CODE (ret) == NOP_EXPR) | |
10015 | realret = TREE_OPERAND (ret, 0); | |
10016 | if (CAN_HAVE_LOCATION_P (realret) | |
10017 | && !EXPR_HAS_LOCATION (realret)) | |
db3927fb | 10018 | SET_EXPR_LOCATION (realret, loc); |
726a989a RB |
10019 | return realret; |
10020 | } | |
10021 | return ret; | |
10022 | } | |
10023 | } | |
10024 | } | |
10025 | return NULL_TREE; | |
10026 | } | |
d7f09764 | 10027 | |
e79983f4 | 10028 | /* Look up the function in builtin_decl that corresponds to DECL |
d7f09764 DN |
10029 | and set ASMSPEC as its user assembler name. DECL must be a |
10030 | function decl that declares a builtin. */ | |
10031 | ||
10032 | void | |
10033 | set_builtin_user_assembler_name (tree decl, const char *asmspec) | |
10034 | { | |
d7f09764 DN |
10035 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL |
10036 | && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL | |
10037 | && asmspec != 0); | |
10038 | ||
ee516de9 | 10039 | tree builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl)); |
ce835863 | 10040 | set_user_assembler_name (builtin, asmspec); |
ee516de9 EB |
10041 | |
10042 | if (DECL_FUNCTION_CODE (decl) == BUILT_IN_FFS | |
10043 | && INT_TYPE_SIZE < BITS_PER_WORD) | |
d7f09764 | 10044 | { |
ee516de9 EB |
10045 | set_user_assembler_libfunc ("ffs", asmspec); |
10046 | set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0), | |
10047 | "ffs"); | |
d7f09764 DN |
10048 | } |
10049 | } | |
bec922f0 SL |
10050 | |
10051 | /* Return true if DECL is a builtin that expands to a constant or similarly | |
10052 | simple code. */ | |
10053 | bool | |
10054 | is_simple_builtin (tree decl) | |
10055 | { | |
10056 | if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) | |
10057 | switch (DECL_FUNCTION_CODE (decl)) | |
10058 | { | |
10059 | /* Builtins that expand to constants. */ | |
10060 | case BUILT_IN_CONSTANT_P: | |
10061 | case BUILT_IN_EXPECT: | |
10062 | case BUILT_IN_OBJECT_SIZE: | |
10063 | case BUILT_IN_UNREACHABLE: | |
10064 | /* Simple register moves or loads from stack. */ | |
45d439ac | 10065 | case BUILT_IN_ASSUME_ALIGNED: |
bec922f0 SL |
10066 | case BUILT_IN_RETURN_ADDRESS: |
10067 | case BUILT_IN_EXTRACT_RETURN_ADDR: | |
10068 | case BUILT_IN_FROB_RETURN_ADDR: | |
10069 | case BUILT_IN_RETURN: | |
10070 | case BUILT_IN_AGGREGATE_INCOMING_ADDRESS: | |
10071 | case BUILT_IN_FRAME_ADDRESS: | |
10072 | case BUILT_IN_VA_END: | |
10073 | case BUILT_IN_STACK_SAVE: | |
10074 | case BUILT_IN_STACK_RESTORE: | |
10075 | /* Exception state returns or moves registers around. */ | |
10076 | case BUILT_IN_EH_FILTER: | |
10077 | case BUILT_IN_EH_POINTER: | |
10078 | case BUILT_IN_EH_COPY_VALUES: | |
10079 | return true; | |
10080 | ||
10081 | default: | |
10082 | return false; | |
10083 | } | |
10084 | ||
10085 | return false; | |
10086 | } | |
10087 | ||
10088 | /* Return true if DECL is a builtin that is not expensive, i.e., they are | |
10089 | most probably expanded inline into reasonably simple code. This is a | |
10090 | superset of is_simple_builtin. */ | |
10091 | bool | |
10092 | is_inexpensive_builtin (tree decl) | |
10093 | { | |
10094 | if (!decl) | |
10095 | return false; | |
10096 | else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD) | |
10097 | return true; | |
10098 | else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) | |
10099 | switch (DECL_FUNCTION_CODE (decl)) | |
10100 | { | |
10101 | case BUILT_IN_ABS: | |
10102 | case BUILT_IN_ALLOCA: | |
13e49da9 | 10103 | case BUILT_IN_ALLOCA_WITH_ALIGN: |
ac868f29 | 10104 | case BUILT_IN_BSWAP16: |
bec922f0 SL |
10105 | case BUILT_IN_BSWAP32: |
10106 | case BUILT_IN_BSWAP64: | |
10107 | case BUILT_IN_CLZ: | |
10108 | case BUILT_IN_CLZIMAX: | |
10109 | case BUILT_IN_CLZL: | |
10110 | case BUILT_IN_CLZLL: | |
10111 | case BUILT_IN_CTZ: | |
10112 | case BUILT_IN_CTZIMAX: | |
10113 | case BUILT_IN_CTZL: | |
10114 | case BUILT_IN_CTZLL: | |
10115 | case BUILT_IN_FFS: | |
10116 | case BUILT_IN_FFSIMAX: | |
10117 | case BUILT_IN_FFSL: | |
10118 | case BUILT_IN_FFSLL: | |
10119 | case BUILT_IN_IMAXABS: | |
10120 | case BUILT_IN_FINITE: | |
10121 | case BUILT_IN_FINITEF: | |
10122 | case BUILT_IN_FINITEL: | |
10123 | case BUILT_IN_FINITED32: | |
10124 | case BUILT_IN_FINITED64: | |
10125 | case BUILT_IN_FINITED128: | |
10126 | case BUILT_IN_FPCLASSIFY: | |
10127 | case BUILT_IN_ISFINITE: | |
10128 | case BUILT_IN_ISINF_SIGN: | |
10129 | case BUILT_IN_ISINF: | |
10130 | case BUILT_IN_ISINFF: | |
10131 | case BUILT_IN_ISINFL: | |
10132 | case BUILT_IN_ISINFD32: | |
10133 | case BUILT_IN_ISINFD64: | |
10134 | case BUILT_IN_ISINFD128: | |
10135 | case BUILT_IN_ISNAN: | |
10136 | case BUILT_IN_ISNANF: | |
10137 | case BUILT_IN_ISNANL: | |
10138 | case BUILT_IN_ISNAND32: | |
10139 | case BUILT_IN_ISNAND64: | |
10140 | case BUILT_IN_ISNAND128: | |
10141 | case BUILT_IN_ISNORMAL: | |
10142 | case BUILT_IN_ISGREATER: | |
10143 | case BUILT_IN_ISGREATEREQUAL: | |
10144 | case BUILT_IN_ISLESS: | |
10145 | case BUILT_IN_ISLESSEQUAL: | |
10146 | case BUILT_IN_ISLESSGREATER: | |
10147 | case BUILT_IN_ISUNORDERED: | |
10148 | case BUILT_IN_VA_ARG_PACK: | |
10149 | case BUILT_IN_VA_ARG_PACK_LEN: | |
10150 | case BUILT_IN_VA_COPY: | |
10151 | case BUILT_IN_TRAP: | |
10152 | case BUILT_IN_SAVEREGS: | |
10153 | case BUILT_IN_POPCOUNTL: | |
10154 | case BUILT_IN_POPCOUNTLL: | |
10155 | case BUILT_IN_POPCOUNTIMAX: | |
10156 | case BUILT_IN_POPCOUNT: | |
10157 | case BUILT_IN_PARITYL: | |
10158 | case BUILT_IN_PARITYLL: | |
10159 | case BUILT_IN_PARITYIMAX: | |
10160 | case BUILT_IN_PARITY: | |
10161 | case BUILT_IN_LABS: | |
10162 | case BUILT_IN_LLABS: | |
10163 | case BUILT_IN_PREFETCH: | |
41dbbb37 | 10164 | case BUILT_IN_ACC_ON_DEVICE: |
bec922f0 SL |
10165 | return true; |
10166 | ||
10167 | default: | |
10168 | return is_simple_builtin (decl); | |
10169 | } | |
10170 | ||
10171 | return false; | |
10172 | } |