]>
Commit | Line | Data |
---|---|---|
53800dbe | 1 | /* Expand builtin functions. |
61f1dccc | 2 | Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
f0b5f617 | 3 | 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
f96bd2bf | 4 | Free Software Foundation, Inc. |
53800dbe | 5 | |
f12b58b3 | 6 | This file is part of GCC. |
53800dbe | 7 | |
f12b58b3 | 8 | GCC is free software; you can redistribute it and/or modify it under |
9 | the terms of the GNU General Public License as published by the Free | |
8c4c00c1 | 10 | Software Foundation; either version 3, or (at your option) any later |
f12b58b3 | 11 | version. |
53800dbe | 12 | |
f12b58b3 | 13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
53800dbe | 17 | |
18 | You should have received a copy of the GNU General Public License | |
8c4c00c1 | 19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ | |
53800dbe | 21 | |
22 | #include "config.h" | |
23 | #include "system.h" | |
805e22b2 | 24 | #include "coretypes.h" |
25 | #include "tm.h" | |
53800dbe | 26 | #include "machmode.h" |
ef258422 | 27 | #include "real.h" |
53800dbe | 28 | #include "rtl.h" |
29 | #include "tree.h" | |
9ff0637e | 30 | #include "tree-gimple.h" |
53800dbe | 31 | #include "flags.h" |
32 | #include "regs.h" | |
33 | #include "hard-reg-set.h" | |
34 | #include "except.h" | |
35 | #include "function.h" | |
53800dbe | 36 | #include "insn-config.h" |
37 | #include "expr.h" | |
d8fc4d0b | 38 | #include "optabs.h" |
39 | #include "libfuncs.h" | |
53800dbe | 40 | #include "recog.h" |
41 | #include "output.h" | |
42 | #include "typeclass.h" | |
53800dbe | 43 | #include "toplev.h" |
689df48e | 44 | #include "predict.h" |
1dd6c958 | 45 | #include "tm_p.h" |
fc2a2dcb | 46 | #include "target.h" |
63c62881 | 47 | #include "langhooks.h" |
0ec80471 | 48 | #include "basic-block.h" |
fd62c19a | 49 | #include "tree-mudflap.h" |
5a84fdd6 | 50 | #include "tree-flow.h" |
162719b3 | 51 | #include "value-prof.h" |
07311427 | 52 | #include "diagnostic.h" |
53800dbe | 53 | |
726e2588 | 54 | #ifndef PAD_VARARGS_DOWN |
55 | #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN | |
56 | #endif | |
57 | ||
ab7943b9 | 58 | /* Define the names of the builtin function types and codes. */ |
8934cb0c | 59 | const char *const built_in_class_names[4] |
ab7943b9 | 60 | = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"}; |
61 | ||
9cfddb70 | 62 | #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X, |
0dfc45b5 | 63 | const char * built_in_names[(int) END_BUILTINS] = |
4e9d90c7 | 64 | { |
65 | #include "builtins.def" | |
66 | }; | |
67 | #undef DEF_BUILTIN | |
ab7943b9 | 68 | |
df94cd3b | 69 | /* Setup an array of _DECL trees, make sure each element is |
70 | initialized to NULL_TREE. */ | |
d2d4bdde | 71 | tree built_in_decls[(int) END_BUILTINS]; |
0a68165a | 72 | /* Declarations used when constructing the builtin implicitly in the compiler. |
73 | It may be NULL_TREE when this is invalid (for instance runtime is not | |
4ee9c684 | 74 | required to implement the function call in all cases). */ |
0a68165a | 75 | tree implicit_built_in_decls[(int) END_BUILTINS]; |
df94cd3b | 76 | |
aecda0d6 | 77 | static const char *c_getstr (tree); |
78 | static rtx c_readstr (const char *, enum machine_mode); | |
79 | static int target_char_cast (tree, char *); | |
d8ae1baa | 80 | static rtx get_memory_rtx (tree, tree); |
aecda0d6 | 81 | static int apply_args_size (void); |
82 | static int apply_result_size (void); | |
d8c9779c | 83 | #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return) |
aecda0d6 | 84 | static rtx result_vector (int, rtx); |
d8c9779c | 85 | #endif |
843d08a9 | 86 | static void expand_builtin_update_setjmp_buf (rtx); |
aecda0d6 | 87 | static void expand_builtin_prefetch (tree); |
88 | static rtx expand_builtin_apply_args (void); | |
89 | static rtx expand_builtin_apply_args_1 (void); | |
90 | static rtx expand_builtin_apply (rtx, rtx, rtx); | |
91 | static void expand_builtin_return (rtx); | |
92 | static enum type_class type_to_class (tree); | |
93 | static rtx expand_builtin_classify_type (tree); | |
94 | static void expand_errno_check (tree, rtx); | |
95 | static rtx expand_builtin_mathfn (tree, rtx, rtx); | |
96 | static rtx expand_builtin_mathfn_2 (tree, rtx, rtx); | |
6b43bae4 | 97 | static rtx expand_builtin_mathfn_3 (tree, rtx, rtx); |
a67a90e5 | 98 | static rtx expand_builtin_interclass_mathfn (tree, rtx, rtx); |
c3147c1a | 99 | static rtx expand_builtin_sincos (tree); |
d735c391 | 100 | static rtx expand_builtin_cexpi (tree, rtx, rtx); |
ad52b9b7 | 101 | static rtx expand_builtin_int_roundingfn (tree, rtx, rtx); |
7d3afc77 | 102 | static rtx expand_builtin_int_roundingfn_2 (tree, rtx, rtx); |
aecda0d6 | 103 | static rtx expand_builtin_args_info (tree); |
79012a9d | 104 | static rtx expand_builtin_next_arg (void); |
aecda0d6 | 105 | static rtx expand_builtin_va_start (tree); |
106 | static rtx expand_builtin_va_end (tree); | |
107 | static rtx expand_builtin_va_copy (tree); | |
7959b13b | 108 | static rtx expand_builtin_memchr (tree, rtx, enum machine_mode); |
c2f47e15 | 109 | static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode); |
aecda0d6 | 110 | static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode); |
111 | static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode); | |
112 | static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode); | |
7da1412b | 113 | static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode); |
aecda0d6 | 114 | static rtx expand_builtin_strncat (tree, rtx, enum machine_mode); |
115 | static rtx expand_builtin_strspn (tree, rtx, enum machine_mode); | |
116 | static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode); | |
117 | static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode); | |
c2f47e15 | 118 | static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode); |
119 | static rtx expand_builtin_mempcpy_args (tree, tree, tree, tree, rtx, | |
120 | enum machine_mode, int); | |
121 | static rtx expand_builtin_memmove (tree, rtx, enum machine_mode, int); | |
122 | static rtx expand_builtin_memmove_args (tree, tree, tree, tree, rtx, | |
123 | enum machine_mode, int); | |
124 | static rtx expand_builtin_bcopy (tree, int); | |
1d89860b | 125 | static rtx expand_builtin_strcpy (tree, tree, rtx, enum machine_mode); |
c2f47e15 | 126 | static rtx expand_builtin_strcpy_args (tree, tree, tree, rtx, enum machine_mode); |
aecda0d6 | 127 | static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode); |
aecda0d6 | 128 | static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode); |
aecda0d6 | 129 | static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode); |
c2f47e15 | 130 | static rtx expand_builtin_memset (tree, rtx, enum machine_mode); |
131 | static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree); | |
aecda0d6 | 132 | static rtx expand_builtin_bzero (tree); |
133 | static rtx expand_builtin_strlen (tree, rtx, enum machine_mode); | |
c2f47e15 | 134 | static rtx expand_builtin_strstr (tree, rtx, enum machine_mode); |
135 | static rtx expand_builtin_strpbrk (tree, rtx, enum machine_mode); | |
136 | static rtx expand_builtin_strchr (tree, rtx, enum machine_mode); | |
137 | static rtx expand_builtin_strrchr (tree, rtx, enum machine_mode); | |
aecda0d6 | 138 | static rtx expand_builtin_alloca (tree, rtx); |
139 | static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab); | |
140 | static rtx expand_builtin_frame_address (tree, tree); | |
19bf118a | 141 | static rtx expand_builtin_fputs (tree, rtx, bool); |
142 | static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool); | |
143 | static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool); | |
6411575e | 144 | static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode); |
aecda0d6 | 145 | static tree stabilize_va_list (tree, int); |
146 | static rtx expand_builtin_expect (tree, rtx); | |
147 | static tree fold_builtin_constant_p (tree); | |
76f5a783 | 148 | static tree fold_builtin_expect (tree, tree); |
aecda0d6 | 149 | static tree fold_builtin_classify_type (tree); |
e6e27594 | 150 | static tree fold_builtin_strlen (tree); |
aecda0d6 | 151 | static tree fold_builtin_inf (tree, int); |
152 | static tree fold_builtin_nan (tree, tree, int); | |
c2f47e15 | 153 | static tree rewrite_call_expr (tree, int, tree, int, ...); |
b7bf20db | 154 | static bool validate_arg (const_tree, enum tree_code code); |
277f8dd2 | 155 | static bool integer_valued_real_p (tree); |
76b9b24b | 156 | static tree fold_trunc_transparent_mathfn (tree, tree); |
aecda0d6 | 157 | static bool readonly_data_expr (tree); |
158 | static rtx expand_builtin_fabs (tree, rtx, rtx); | |
27f261ef | 159 | static rtx expand_builtin_signbit (tree, rtx); |
e6e27594 | 160 | static tree fold_builtin_sqrt (tree, tree); |
161 | static tree fold_builtin_cbrt (tree, tree); | |
c2f47e15 | 162 | static tree fold_builtin_pow (tree, tree, tree, tree); |
163 | static tree fold_builtin_powi (tree, tree, tree, tree); | |
e6e27594 | 164 | static tree fold_builtin_cos (tree, tree, tree); |
cacdc1af | 165 | static tree fold_builtin_cosh (tree, tree, tree); |
bffb7645 | 166 | static tree fold_builtin_tan (tree, tree); |
7169af54 | 167 | static tree fold_builtin_trunc (tree, tree); |
168 | static tree fold_builtin_floor (tree, tree); | |
169 | static tree fold_builtin_ceil (tree, tree); | |
170 | static tree fold_builtin_round (tree, tree); | |
ad52b9b7 | 171 | static tree fold_builtin_int_roundingfn (tree, tree); |
10b9666f | 172 | static tree fold_builtin_bitop (tree, tree); |
c2f47e15 | 173 | static tree fold_builtin_memory_op (tree, tree, tree, tree, bool, int); |
174 | static tree fold_builtin_strchr (tree, tree, tree); | |
7959b13b | 175 | static tree fold_builtin_memchr (tree, tree, tree, tree); |
c2f47e15 | 176 | static tree fold_builtin_memcmp (tree, tree, tree); |
177 | static tree fold_builtin_strcmp (tree, tree); | |
178 | static tree fold_builtin_strncmp (tree, tree, tree); | |
64c18e8d | 179 | static tree fold_builtin_signbit (tree, tree); |
c2f47e15 | 180 | static tree fold_builtin_copysign (tree, tree, tree, tree); |
467214fd | 181 | static tree fold_builtin_isascii (tree); |
182 | static tree fold_builtin_toascii (tree); | |
183 | static tree fold_builtin_isdigit (tree); | |
d1aade50 | 184 | static tree fold_builtin_fabs (tree, tree); |
185 | static tree fold_builtin_abs (tree, tree); | |
c2f47e15 | 186 | static tree fold_builtin_unordered_cmp (tree, tree, tree, enum tree_code, |
d5019fe8 | 187 | enum tree_code); |
c2f47e15 | 188 | static tree fold_builtin_n (tree, tree *, int, bool); |
189 | static tree fold_builtin_0 (tree, bool); | |
3d3d84b8 | 190 | static tree fold_builtin_1 (tree, tree, bool); |
c2f47e15 | 191 | static tree fold_builtin_2 (tree, tree, tree, bool); |
192 | static tree fold_builtin_3 (tree, tree, tree, tree, bool); | |
193 | static tree fold_builtin_4 (tree, tree, tree, tree, tree, bool); | |
194 | static tree fold_builtin_varargs (tree, tree, bool); | |
195 | ||
196 | static tree fold_builtin_strpbrk (tree, tree, tree); | |
197 | static tree fold_builtin_strstr (tree, tree, tree); | |
198 | static tree fold_builtin_strrchr (tree, tree, tree); | |
199 | static tree fold_builtin_strcat (tree, tree); | |
200 | static tree fold_builtin_strncat (tree, tree, tree); | |
201 | static tree fold_builtin_strspn (tree, tree); | |
202 | static tree fold_builtin_strcspn (tree, tree); | |
203 | static tree fold_builtin_sprintf (tree, tree, tree, int); | |
4ee9c684 | 204 | |
0a39fd54 | 205 | static rtx expand_builtin_object_size (tree); |
206 | static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode, | |
207 | enum built_in_function); | |
208 | static void maybe_emit_chk_warning (tree, enum built_in_function); | |
209 | static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function); | |
c2f47e15 | 210 | static tree fold_builtin_object_size (tree, tree); |
211 | static tree fold_builtin_strcat_chk (tree, tree, tree, tree); | |
212 | static tree fold_builtin_strncat_chk (tree, tree, tree, tree, tree); | |
0a39fd54 | 213 | static tree fold_builtin_sprintf_chk (tree, enum built_in_function); |
c2f47e15 | 214 | static tree fold_builtin_printf (tree, tree, tree, bool, enum built_in_function); |
215 | static tree fold_builtin_fprintf (tree, tree, tree, tree, bool, | |
216 | enum built_in_function); | |
99eabcc1 | 217 | static bool init_target_chars (void); |
218 | ||
219 | static unsigned HOST_WIDE_INT target_newline; | |
220 | static unsigned HOST_WIDE_INT target_percent; | |
221 | static unsigned HOST_WIDE_INT target_c; | |
222 | static unsigned HOST_WIDE_INT target_s; | |
223 | static char target_percent_c[3]; | |
224 | static char target_percent_s[3]; | |
225 | static char target_percent_s_newline[4]; | |
728bac60 | 226 | static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t), |
227 | const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool); | |
f0c477f2 | 228 | static tree do_mpfr_arg2 (tree, tree, tree, |
229 | int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t)); | |
9917422b | 230 | static tree do_mpfr_arg3 (tree, tree, tree, tree, |
231 | int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t)); | |
d92f994c | 232 | static tree do_mpfr_sincos (tree, tree, tree); |
65dd1378 | 233 | #if MPFR_VERSION >= MPFR_VERSION_NUM(2,3,0) |
234 | static tree do_mpfr_bessel_n (tree, tree, tree, | |
235 | int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t), | |
236 | const REAL_VALUE_TYPE *, bool); | |
e5407ca6 | 237 | static tree do_mpfr_remquo (tree, tree, tree); |
e84da7c1 | 238 | static tree do_mpfr_lgamma_r (tree, tree, tree); |
65dd1378 | 239 | #endif |
0a39fd54 | 240 | |
b6a5fc45 | 241 | /* Return true if NODE should be considered for inline expansion regardless |
242 | of the optimization level. This means whenever a function is invoked with | |
243 | its "internal" name, which normally contains the prefix "__builtin". */ | |
244 | ||
245 | static bool called_as_built_in (tree node) | |
246 | { | |
247 | const char *name = IDENTIFIER_POINTER (DECL_NAME (node)); | |
248 | if (strncmp (name, "__builtin_", 10) == 0) | |
249 | return true; | |
250 | if (strncmp (name, "__sync_", 7) == 0) | |
251 | return true; | |
252 | return false; | |
253 | } | |
4ee9c684 | 254 | |
53800dbe | 255 | /* Return the alignment in bits of EXP, a pointer valued expression. |
256 | But don't return more than MAX_ALIGN no matter what. | |
257 | The alignment returned is, by default, the alignment of the thing that | |
27d0c333 | 258 | EXP points to. If it is not a POINTER_TYPE, 0 is returned. |
53800dbe | 259 | |
260 | Otherwise, look at the expression to see if we can do better, i.e., if the | |
261 | expression is actually pointing at an object whose alignment is tighter. */ | |
262 | ||
f656b751 | 263 | int |
aecda0d6 | 264 | get_pointer_alignment (tree exp, unsigned int max_align) |
53800dbe | 265 | { |
27d0c333 | 266 | unsigned int align, inner; |
53800dbe | 267 | |
189575ff | 268 | /* We rely on TER to compute accurate alignment information. */ |
269 | if (!(optimize && flag_tree_ter)) | |
270 | return 0; | |
271 | ||
535e2026 | 272 | if (!POINTER_TYPE_P (TREE_TYPE (exp))) |
273 | return 0; | |
274 | ||
53800dbe | 275 | align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp))); |
276 | align = MIN (align, max_align); | |
277 | ||
278 | while (1) | |
279 | { | |
280 | switch (TREE_CODE (exp)) | |
281 | { | |
72dd6141 | 282 | CASE_CONVERT: |
53800dbe | 283 | exp = TREE_OPERAND (exp, 0); |
552752f7 | 284 | if (! POINTER_TYPE_P (TREE_TYPE (exp))) |
53800dbe | 285 | return align; |
325d1c45 | 286 | |
53800dbe | 287 | inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp))); |
288 | align = MIN (inner, max_align); | |
289 | break; | |
290 | ||
0de36bdb | 291 | case POINTER_PLUS_EXPR: |
53800dbe | 292 | /* If sum of pointer + int, restrict our maximum alignment to that |
293 | imposed by the integer. If not, we can't do any better than | |
294 | ALIGN. */ | |
325d1c45 | 295 | if (! host_integerp (TREE_OPERAND (exp, 1), 1)) |
53800dbe | 296 | return align; |
297 | ||
0994d2ed | 298 | while (((tree_low_cst (TREE_OPERAND (exp, 1), 1)) |
299 | & (max_align / BITS_PER_UNIT - 1)) | |
53800dbe | 300 | != 0) |
301 | max_align >>= 1; | |
302 | ||
303 | exp = TREE_OPERAND (exp, 0); | |
304 | break; | |
305 | ||
306 | case ADDR_EXPR: | |
307 | /* See what we are pointing at and look at its alignment. */ | |
308 | exp = TREE_OPERAND (exp, 0); | |
3c15318e | 309 | inner = max_align; |
d2df7679 | 310 | if (handled_component_p (exp)) |
1eee24cf | 311 | { |
d2df7679 | 312 | HOST_WIDE_INT bitsize, bitpos; |
313 | tree offset; | |
314 | enum machine_mode mode; | |
315 | int unsignedp, volatilep; | |
316 | ||
317 | exp = get_inner_reference (exp, &bitsize, &bitpos, &offset, | |
318 | &mode, &unsignedp, &volatilep, true); | |
319 | if (bitpos) | |
320 | inner = MIN (inner, (unsigned) (bitpos & -bitpos)); | |
321 | if (offset && TREE_CODE (offset) == PLUS_EXPR | |
322 | && host_integerp (TREE_OPERAND (offset, 1), 1)) | |
323 | { | |
324 | /* Any overflow in calculating offset_bits won't change | |
325 | the alignment. */ | |
326 | unsigned offset_bits | |
327 | = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1) | |
328 | * BITS_PER_UNIT); | |
329 | ||
330 | if (offset_bits) | |
331 | inner = MIN (inner, (offset_bits & -offset_bits)); | |
332 | offset = TREE_OPERAND (offset, 0); | |
333 | } | |
334 | if (offset && TREE_CODE (offset) == MULT_EXPR | |
335 | && host_integerp (TREE_OPERAND (offset, 1), 1)) | |
336 | { | |
337 | /* Any overflow in calculating offset_factor won't change | |
338 | the alignment. */ | |
339 | unsigned offset_factor | |
340 | = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1) | |
341 | * BITS_PER_UNIT); | |
342 | ||
343 | if (offset_factor) | |
344 | inner = MIN (inner, (offset_factor & -offset_factor)); | |
345 | } | |
346 | else if (offset) | |
347 | inner = MIN (inner, BITS_PER_UNIT); | |
1eee24cf | 348 | } |
6c96b31e | 349 | if (DECL_P (exp)) |
3c15318e | 350 | align = MIN (inner, DECL_ALIGN (exp)); |
53800dbe | 351 | #ifdef CONSTANT_ALIGNMENT |
ce45a448 | 352 | else if (CONSTANT_CLASS_P (exp)) |
e068b646 | 353 | align = MIN (inner, (unsigned)CONSTANT_ALIGNMENT (exp, align)); |
53800dbe | 354 | #endif |
d2df7679 | 355 | else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR |
356 | || TREE_CODE (exp) == INDIRECT_REF) | |
357 | align = MIN (TYPE_ALIGN (TREE_TYPE (exp)), inner); | |
3c15318e | 358 | else |
359 | align = MIN (align, inner); | |
360 | return MIN (align, max_align); | |
53800dbe | 361 | |
362 | default: | |
363 | return align; | |
364 | } | |
365 | } | |
366 | } | |
367 | ||
368 | /* Compute the length of a C string. TREE_STRING_LENGTH is not the right | |
369 | way, because it could contain a zero byte in the middle. | |
370 | TREE_STRING_LENGTH is the size of the character array, not the string. | |
371 | ||
4172d65e | 372 | ONLY_VALUE should be nonzero if the result is not going to be emitted |
c09841f6 | 373 | into the instruction stream and zero if it is going to be expanded. |
4172d65e | 374 | E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3 |
681fab1e | 375 | is returned, otherwise NULL, since |
376 | len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not | |
377 | evaluate the side-effects. | |
378 | ||
902de8ed | 379 | The value returned is of type `ssizetype'. |
380 | ||
53800dbe | 381 | Unfortunately, string_constant can't access the values of const char |
382 | arrays with initializers, so neither can we do so here. */ | |
383 | ||
4ee9c684 | 384 | tree |
681fab1e | 385 | c_strlen (tree src, int only_value) |
53800dbe | 386 | { |
387 | tree offset_node; | |
27d0c333 | 388 | HOST_WIDE_INT offset; |
389 | int max; | |
44acf429 | 390 | const char *ptr; |
53800dbe | 391 | |
681fab1e | 392 | STRIP_NOPS (src); |
393 | if (TREE_CODE (src) == COND_EXPR | |
394 | && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0)))) | |
395 | { | |
396 | tree len1, len2; | |
397 | ||
398 | len1 = c_strlen (TREE_OPERAND (src, 1), only_value); | |
399 | len2 = c_strlen (TREE_OPERAND (src, 2), only_value); | |
0862b7e9 | 400 | if (tree_int_cst_equal (len1, len2)) |
681fab1e | 401 | return len1; |
402 | } | |
403 | ||
404 | if (TREE_CODE (src) == COMPOUND_EXPR | |
405 | && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0)))) | |
406 | return c_strlen (TREE_OPERAND (src, 1), only_value); | |
407 | ||
53800dbe | 408 | src = string_constant (src, &offset_node); |
409 | if (src == 0) | |
c2f47e15 | 410 | return NULL_TREE; |
902de8ed | 411 | |
83d79705 | 412 | max = TREE_STRING_LENGTH (src) - 1; |
53800dbe | 413 | ptr = TREE_STRING_POINTER (src); |
902de8ed | 414 | |
53800dbe | 415 | if (offset_node && TREE_CODE (offset_node) != INTEGER_CST) |
416 | { | |
417 | /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't | |
418 | compute the offset to the following null if we don't know where to | |
419 | start searching for it. */ | |
420 | int i; | |
902de8ed | 421 | |
53800dbe | 422 | for (i = 0; i < max; i++) |
423 | if (ptr[i] == 0) | |
c2f47e15 | 424 | return NULL_TREE; |
902de8ed | 425 | |
53800dbe | 426 | /* We don't know the starting offset, but we do know that the string |
427 | has no internal zero bytes. We can assume that the offset falls | |
428 | within the bounds of the string; otherwise, the programmer deserves | |
429 | what he gets. Subtract the offset from the length of the string, | |
902de8ed | 430 | and return that. This would perhaps not be valid if we were dealing |
431 | with named arrays in addition to literal string constants. */ | |
432 | ||
433 | return size_diffop (size_int (max), offset_node); | |
53800dbe | 434 | } |
435 | ||
436 | /* We have a known offset into the string. Start searching there for | |
27d0c333 | 437 | a null character if we can represent it as a single HOST_WIDE_INT. */ |
dabc4084 | 438 | if (offset_node == 0) |
53800dbe | 439 | offset = 0; |
dabc4084 | 440 | else if (! host_integerp (offset_node, 0)) |
441 | offset = -1; | |
53800dbe | 442 | else |
27d0c333 | 443 | offset = tree_low_cst (offset_node, 0); |
902de8ed | 444 | |
53800dbe | 445 | /* If the offset is known to be out of bounds, warn, and call strlen at |
446 | runtime. */ | |
447 | if (offset < 0 || offset > max) | |
448 | { | |
7e3b64b4 | 449 | /* Suppress multiple warnings for propagated constant strings. */ |
450 | if (! TREE_NO_WARNING (src)) | |
451 | { | |
452 | warning (0, "offset outside bounds of constant string"); | |
453 | TREE_NO_WARNING (src) = 1; | |
454 | } | |
c2f47e15 | 455 | return NULL_TREE; |
53800dbe | 456 | } |
902de8ed | 457 | |
53800dbe | 458 | /* Use strlen to search for the first zero byte. Since any strings |
459 | constructed with build_string will have nulls appended, we win even | |
460 | if we get handed something like (char[4])"abcd". | |
461 | ||
462 | Since OFFSET is our starting index into the string, no further | |
463 | calculation is needed. */ | |
902de8ed | 464 | return ssize_int (strlen (ptr + offset)); |
53800dbe | 465 | } |
466 | ||
83d79705 | 467 | /* Return a char pointer for a C string if it is a string constant |
468 | or sum of string constant and integer constant. */ | |
469 | ||
470 | static const char * | |
aecda0d6 | 471 | c_getstr (tree src) |
83d79705 | 472 | { |
473 | tree offset_node; | |
83d79705 | 474 | |
475 | src = string_constant (src, &offset_node); | |
476 | if (src == 0) | |
477 | return 0; | |
478 | ||
8c85fcb7 | 479 | if (offset_node == 0) |
480 | return TREE_STRING_POINTER (src); | |
481 | else if (!host_integerp (offset_node, 1) | |
482 | || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0) | |
83d79705 | 483 | return 0; |
83d79705 | 484 | |
8c85fcb7 | 485 | return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1); |
83d79705 | 486 | } |
487 | ||
8c85fcb7 | 488 | /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading |
489 | GET_MODE_BITSIZE (MODE) bits from string constant STR. */ | |
ecc318ff | 490 | |
6840589f | 491 | static rtx |
aecda0d6 | 492 | c_readstr (const char *str, enum machine_mode mode) |
6840589f | 493 | { |
494 | HOST_WIDE_INT c[2]; | |
495 | HOST_WIDE_INT ch; | |
496 | unsigned int i, j; | |
497 | ||
64db345d | 498 | gcc_assert (GET_MODE_CLASS (mode) == MODE_INT); |
7d3f6cc7 | 499 | |
6840589f | 500 | c[0] = 0; |
501 | c[1] = 0; | |
502 | ch = 1; | |
503 | for (i = 0; i < GET_MODE_SIZE (mode); i++) | |
504 | { | |
505 | j = i; | |
506 | if (WORDS_BIG_ENDIAN) | |
507 | j = GET_MODE_SIZE (mode) - i - 1; | |
508 | if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN | |
509 | && GET_MODE_SIZE (mode) > UNITS_PER_WORD) | |
510 | j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1; | |
511 | j *= BITS_PER_UNIT; | |
64db345d | 512 | gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT); |
7d3f6cc7 | 513 | |
6840589f | 514 | if (ch) |
515 | ch = (unsigned char) str[i]; | |
516 | c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT); | |
517 | } | |
518 | return immed_double_const (c[0], c[1], mode); | |
519 | } | |
520 | ||
ecc318ff | 521 | /* Cast a target constant CST to target CHAR and if that value fits into |
5206b159 | 522 | host char type, return zero and put that value into variable pointed to by |
ecc318ff | 523 | P. */ |
524 | ||
525 | static int | |
aecda0d6 | 526 | target_char_cast (tree cst, char *p) |
ecc318ff | 527 | { |
528 | unsigned HOST_WIDE_INT val, hostval; | |
529 | ||
27d0c333 | 530 | if (!host_integerp (cst, 1) |
ecc318ff | 531 | || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT) |
532 | return 1; | |
533 | ||
27d0c333 | 534 | val = tree_low_cst (cst, 1); |
ecc318ff | 535 | if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT) |
536 | val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1; | |
537 | ||
538 | hostval = val; | |
539 | if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT) | |
540 | hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1; | |
541 | ||
542 | if (val != hostval) | |
543 | return 1; | |
544 | ||
545 | *p = hostval; | |
546 | return 0; | |
547 | } | |
548 | ||
4ee9c684 | 549 | /* Similar to save_expr, but assumes that arbitrary code is not executed |
550 | in between the multiple evaluations. In particular, we assume that a | |
551 | non-addressable local variable will not be modified. */ | |
552 | ||
553 | static tree | |
554 | builtin_save_expr (tree exp) | |
555 | { | |
556 | if (TREE_ADDRESSABLE (exp) == 0 | |
557 | && (TREE_CODE (exp) == PARM_DECL | |
558 | || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))) | |
559 | return exp; | |
560 | ||
561 | return save_expr (exp); | |
562 | } | |
563 | ||
53800dbe | 564 | /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT |
565 | times to get the address of either a higher stack frame, or a return | |
566 | address located within it (depending on FNDECL_CODE). */ | |
902de8ed | 567 | |
c626df3d | 568 | static rtx |
869d0ef0 | 569 | expand_builtin_return_addr (enum built_in_function fndecl_code, int count) |
53800dbe | 570 | { |
571 | int i; | |
572 | ||
869d0ef0 | 573 | #ifdef INITIAL_FRAME_ADDRESS_RTX |
574 | rtx tem = INITIAL_FRAME_ADDRESS_RTX; | |
575 | #else | |
e3e15c50 | 576 | rtx tem; |
577 | ||
1b74fde7 | 578 | /* For a zero count with __builtin_return_address, we don't care what |
579 | frame address we return, because target-specific definitions will | |
580 | override us. Therefore frame pointer elimination is OK, and using | |
581 | the soft frame pointer is OK. | |
582 | ||
fa7637bd | 583 | For a nonzero count, or a zero count with __builtin_frame_address, |
1b74fde7 | 584 | we require a stable offset from the current frame pointer to the |
585 | previous one, so we must use the hard frame pointer, and | |
e3e15c50 | 586 | we must disable frame pointer elimination. */ |
1b74fde7 | 587 | if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS) |
e3e15c50 | 588 | tem = frame_pointer_rtx; |
a0c938f0 | 589 | else |
e3e15c50 | 590 | { |
591 | tem = hard_frame_pointer_rtx; | |
592 | ||
593 | /* Tell reload not to eliminate the frame pointer. */ | |
18d50ae6 | 594 | crtl->accesses_prior_frames = 1; |
e3e15c50 | 595 | } |
869d0ef0 | 596 | #endif |
597 | ||
53800dbe | 598 | /* Some machines need special handling before we can access |
3a69c60c | 599 | arbitrary frames. For example, on the SPARC, we must first flush |
53800dbe | 600 | all register windows to the stack. */ |
601 | #ifdef SETUP_FRAME_ADDRESSES | |
602 | if (count > 0) | |
603 | SETUP_FRAME_ADDRESSES (); | |
604 | #endif | |
605 | ||
3a69c60c | 606 | /* On the SPARC, the return address is not in the frame, it is in a |
53800dbe | 607 | register. There is no way to access it off of the current frame |
608 | pointer, but it can be accessed off the previous frame pointer by | |
609 | reading the value from the register window save area. */ | |
610 | #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME | |
611 | if (fndecl_code == BUILT_IN_RETURN_ADDRESS) | |
612 | count--; | |
613 | #endif | |
614 | ||
615 | /* Scan back COUNT frames to the specified frame. */ | |
616 | for (i = 0; i < count; i++) | |
617 | { | |
618 | /* Assume the dynamic chain pointer is in the word that the | |
619 | frame address points to, unless otherwise specified. */ | |
620 | #ifdef DYNAMIC_CHAIN_ADDRESS | |
621 | tem = DYNAMIC_CHAIN_ADDRESS (tem); | |
622 | #endif | |
623 | tem = memory_address (Pmode, tem); | |
00060fc2 | 624 | tem = gen_frame_mem (Pmode, tem); |
83fc1478 | 625 | tem = copy_to_reg (tem); |
53800dbe | 626 | } |
627 | ||
3a69c60c | 628 | /* For __builtin_frame_address, return what we've got. But, on |
629 | the SPARC for example, we may have to add a bias. */ | |
53800dbe | 630 | if (fndecl_code == BUILT_IN_FRAME_ADDRESS) |
3a69c60c | 631 | #ifdef FRAME_ADDR_RTX |
632 | return FRAME_ADDR_RTX (tem); | |
633 | #else | |
53800dbe | 634 | return tem; |
3a69c60c | 635 | #endif |
53800dbe | 636 | |
3a69c60c | 637 | /* For __builtin_return_address, get the return address from that frame. */ |
53800dbe | 638 | #ifdef RETURN_ADDR_RTX |
639 | tem = RETURN_ADDR_RTX (count, tem); | |
640 | #else | |
641 | tem = memory_address (Pmode, | |
642 | plus_constant (tem, GET_MODE_SIZE (Pmode))); | |
00060fc2 | 643 | tem = gen_frame_mem (Pmode, tem); |
53800dbe | 644 | #endif |
645 | return tem; | |
646 | } | |
647 | ||
f7c44134 | 648 | /* Alias set used for setjmp buffer. */ |
32c2fdea | 649 | static alias_set_type setjmp_alias_set = -1; |
f7c44134 | 650 | |
6b7f6858 | 651 | /* Construct the leading half of a __builtin_setjmp call. Control will |
2c8a1497 | 652 | return to RECEIVER_LABEL. This is also called directly by the SJLJ |
653 | exception handling code. */ | |
53800dbe | 654 | |
6b7f6858 | 655 | void |
aecda0d6 | 656 | expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label) |
53800dbe | 657 | { |
53800dbe | 658 | enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL); |
53800dbe | 659 | rtx stack_save; |
f7c44134 | 660 | rtx mem; |
53800dbe | 661 | |
f7c44134 | 662 | if (setjmp_alias_set == -1) |
663 | setjmp_alias_set = new_alias_set (); | |
664 | ||
85d654dd | 665 | buf_addr = convert_memory_address (Pmode, buf_addr); |
53800dbe | 666 | |
37ae8504 | 667 | buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX)); |
53800dbe | 668 | |
6b7f6858 | 669 | /* We store the frame pointer and the address of receiver_label in |
670 | the buffer and use the rest of it for the stack save area, which | |
671 | is machine-dependent. */ | |
53800dbe | 672 | |
f7c44134 | 673 | mem = gen_rtx_MEM (Pmode, buf_addr); |
ab6ab77e | 674 | set_mem_alias_set (mem, setjmp_alias_set); |
e3e026e8 | 675 | emit_move_insn (mem, targetm.builtin_setjmp_frame_value ()); |
f7c44134 | 676 | |
677 | mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))), | |
ab6ab77e | 678 | set_mem_alias_set (mem, setjmp_alias_set); |
f7c44134 | 679 | |
680 | emit_move_insn (validize_mem (mem), | |
6b7f6858 | 681 | force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label))); |
53800dbe | 682 | |
683 | stack_save = gen_rtx_MEM (sa_mode, | |
684 | plus_constant (buf_addr, | |
685 | 2 * GET_MODE_SIZE (Pmode))); | |
ab6ab77e | 686 | set_mem_alias_set (stack_save, setjmp_alias_set); |
53800dbe | 687 | emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX); |
688 | ||
689 | /* If there is further processing to do, do it. */ | |
690 | #ifdef HAVE_builtin_setjmp_setup | |
691 | if (HAVE_builtin_setjmp_setup) | |
692 | emit_insn (gen_builtin_setjmp_setup (buf_addr)); | |
693 | #endif | |
694 | ||
6b7f6858 | 695 | /* Tell optimize_save_area_alloca that extra work is going to |
696 | need to go on during alloca. */ | |
18d50ae6 | 697 | cfun->calls_setjmp = 1; |
80ab81b9 | 698 | |
29f09705 | 699 | /* We have a nonlocal label. */ |
18d50ae6 | 700 | cfun->has_nonlocal_label = 1; |
6b7f6858 | 701 | } |
53800dbe | 702 | |
2c8a1497 | 703 | /* Construct the trailing part of a __builtin_setjmp call. This is |
704 | also called directly by the SJLJ exception handling code. */ | |
6b7f6858 | 705 | |
706 | void | |
aecda0d6 | 707 | expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED) |
6b7f6858 | 708 | { |
53800dbe | 709 | /* Clobber the FP when we get here, so we have to make sure it's |
710 | marked as used by this function. */ | |
18b42941 | 711 | emit_use (hard_frame_pointer_rtx); |
53800dbe | 712 | |
713 | /* Mark the static chain as clobbered here so life information | |
714 | doesn't get messed up for it. */ | |
18b42941 | 715 | emit_clobber (static_chain_rtx); |
53800dbe | 716 | |
717 | /* Now put in the code to restore the frame pointer, and argument | |
491e04ef | 718 | pointer, if needed. */ |
53800dbe | 719 | #ifdef HAVE_nonlocal_goto |
720 | if (! HAVE_nonlocal_goto) | |
721 | #endif | |
5a1c3f40 | 722 | { |
723 | emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx); | |
724 | /* This might change the hard frame pointer in ways that aren't | |
725 | apparent to early optimization passes, so force a clobber. */ | |
18b42941 | 726 | emit_clobber (hard_frame_pointer_rtx); |
5a1c3f40 | 727 | } |
53800dbe | 728 | |
729 | #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM | |
730 | if (fixed_regs[ARG_POINTER_REGNUM]) | |
731 | { | |
732 | #ifdef ELIMINABLE_REGS | |
733 | size_t i; | |
e99c3a1d | 734 | static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS; |
53800dbe | 735 | |
3098b2d3 | 736 | for (i = 0; i < ARRAY_SIZE (elim_regs); i++) |
53800dbe | 737 | if (elim_regs[i].from == ARG_POINTER_REGNUM |
738 | && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM) | |
739 | break; | |
740 | ||
3098b2d3 | 741 | if (i == ARRAY_SIZE (elim_regs)) |
53800dbe | 742 | #endif |
743 | { | |
744 | /* Now restore our arg pointer from the address at which it | |
05927e40 | 745 | was saved in our stack frame. */ |
53800dbe | 746 | emit_move_insn (virtual_incoming_args_rtx, |
b079a207 | 747 | copy_to_reg (get_arg_pointer_save_area ())); |
53800dbe | 748 | } |
749 | } | |
750 | #endif | |
751 | ||
752 | #ifdef HAVE_builtin_setjmp_receiver | |
753 | if (HAVE_builtin_setjmp_receiver) | |
6b7f6858 | 754 | emit_insn (gen_builtin_setjmp_receiver (receiver_label)); |
53800dbe | 755 | else |
756 | #endif | |
757 | #ifdef HAVE_nonlocal_goto_receiver | |
758 | if (HAVE_nonlocal_goto_receiver) | |
759 | emit_insn (gen_nonlocal_goto_receiver ()); | |
760 | else | |
761 | #endif | |
6b7f6858 | 762 | { /* Nothing */ } |
57f6bb94 | 763 | |
3072d30e | 764 | /* We must not allow the code we just generated to be reordered by |
765 | scheduling. Specifically, the update of the frame pointer must | |
766 | happen immediately, not later. */ | |
767 | emit_insn (gen_blockage ()); | |
6b7f6858 | 768 | } |
53800dbe | 769 | |
53800dbe | 770 | /* __builtin_longjmp is passed a pointer to an array of five words (not |
771 | all will be used on all machines). It operates similarly to the C | |
772 | library function of the same name, but is more efficient. Much of | |
2c8a1497 | 773 | the code below is copied from the handling of non-local gotos. */ |
53800dbe | 774 | |
c626df3d | 775 | static void |
aecda0d6 | 776 | expand_builtin_longjmp (rtx buf_addr, rtx value) |
53800dbe | 777 | { |
4712c7d6 | 778 | rtx fp, lab, stack, insn, last; |
53800dbe | 779 | enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL); |
780 | ||
f7c44134 | 781 | if (setjmp_alias_set == -1) |
782 | setjmp_alias_set = new_alias_set (); | |
783 | ||
85d654dd | 784 | buf_addr = convert_memory_address (Pmode, buf_addr); |
479e4d5e | 785 | |
53800dbe | 786 | buf_addr = force_reg (Pmode, buf_addr); |
787 | ||
788 | /* We used to store value in static_chain_rtx, but that fails if pointers | |
789 | are smaller than integers. We instead require that the user must pass | |
790 | a second argument of 1, because that is what builtin_setjmp will | |
791 | return. This also makes EH slightly more efficient, since we are no | |
792 | longer copying around a value that we don't care about. */ | |
64db345d | 793 | gcc_assert (value == const1_rtx); |
53800dbe | 794 | |
4712c7d6 | 795 | last = get_last_insn (); |
53800dbe | 796 | #ifdef HAVE_builtin_longjmp |
797 | if (HAVE_builtin_longjmp) | |
798 | emit_insn (gen_builtin_longjmp (buf_addr)); | |
799 | else | |
800 | #endif | |
801 | { | |
802 | fp = gen_rtx_MEM (Pmode, buf_addr); | |
803 | lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr, | |
804 | GET_MODE_SIZE (Pmode))); | |
805 | ||
806 | stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr, | |
807 | 2 * GET_MODE_SIZE (Pmode))); | |
ab6ab77e | 808 | set_mem_alias_set (fp, setjmp_alias_set); |
809 | set_mem_alias_set (lab, setjmp_alias_set); | |
810 | set_mem_alias_set (stack, setjmp_alias_set); | |
53800dbe | 811 | |
812 | /* Pick up FP, label, and SP from the block and jump. This code is | |
813 | from expand_goto in stmt.c; see there for detailed comments. */ | |
03fd9d2c | 814 | #ifdef HAVE_nonlocal_goto |
53800dbe | 815 | if (HAVE_nonlocal_goto) |
816 | /* We have to pass a value to the nonlocal_goto pattern that will | |
817 | get copied into the static_chain pointer, but it does not matter | |
818 | what that value is, because builtin_setjmp does not use it. */ | |
28d202a8 | 819 | emit_insn (gen_nonlocal_goto (value, lab, stack, fp)); |
53800dbe | 820 | else |
821 | #endif | |
822 | { | |
823 | lab = copy_to_reg (lab); | |
824 | ||
18b42941 | 825 | emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode))); |
826 | emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx)); | |
2a871ad1 | 827 | |
53800dbe | 828 | emit_move_insn (hard_frame_pointer_rtx, fp); |
829 | emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX); | |
830 | ||
18b42941 | 831 | emit_use (hard_frame_pointer_rtx); |
832 | emit_use (stack_pointer_rtx); | |
53800dbe | 833 | emit_indirect_jump (lab); |
834 | } | |
835 | } | |
615166bb | 836 | |
837 | /* Search backwards and mark the jump insn as a non-local goto. | |
838 | Note that this precludes the use of __builtin_longjmp to a | |
839 | __builtin_setjmp target in the same function. However, we've | |
840 | already cautioned the user that these functions are for | |
841 | internal exception handling use only. */ | |
449c0509 | 842 | for (insn = get_last_insn (); insn; insn = PREV_INSN (insn)) |
843 | { | |
64db345d | 844 | gcc_assert (insn != last); |
7d3f6cc7 | 845 | |
6d7dc5b9 | 846 | if (JUMP_P (insn)) |
449c0509 | 847 | { |
848 | REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx, | |
849 | REG_NOTES (insn)); | |
850 | break; | |
851 | } | |
6d7dc5b9 | 852 | else if (CALL_P (insn)) |
9342ee68 | 853 | break; |
449c0509 | 854 | } |
53800dbe | 855 | } |
856 | ||
4ee9c684 | 857 | /* Expand a call to __builtin_nonlocal_goto. We're passed the target label |
858 | and the address of the save area. */ | |
859 | ||
860 | static rtx | |
c2f47e15 | 861 | expand_builtin_nonlocal_goto (tree exp) |
4ee9c684 | 862 | { |
863 | tree t_label, t_save_area; | |
864 | rtx r_label, r_save_area, r_fp, r_sp, insn; | |
865 | ||
c2f47e15 | 866 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
4ee9c684 | 867 | return NULL_RTX; |
868 | ||
c2f47e15 | 869 | t_label = CALL_EXPR_ARG (exp, 0); |
870 | t_save_area = CALL_EXPR_ARG (exp, 1); | |
4ee9c684 | 871 | |
8ec3c5c2 | 872 | r_label = expand_normal (t_label); |
3dce56cc | 873 | r_label = convert_memory_address (Pmode, r_label); |
8ec3c5c2 | 874 | r_save_area = expand_normal (t_save_area); |
3dce56cc | 875 | r_save_area = convert_memory_address (Pmode, r_save_area); |
51adbc8a | 876 | /* Copy the address of the save location to a register just in case it was based |
877 | on the frame pointer. */ | |
878 | r_save_area = copy_to_reg (r_save_area); | |
4ee9c684 | 879 | r_fp = gen_rtx_MEM (Pmode, r_save_area); |
880 | r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL), | |
881 | plus_constant (r_save_area, GET_MODE_SIZE (Pmode))); | |
882 | ||
18d50ae6 | 883 | crtl->has_nonlocal_goto = 1; |
4ee9c684 | 884 | |
03fd9d2c | 885 | #ifdef HAVE_nonlocal_goto |
4ee9c684 | 886 | /* ??? We no longer need to pass the static chain value, afaik. */ |
887 | if (HAVE_nonlocal_goto) | |
888 | emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp)); | |
889 | else | |
890 | #endif | |
891 | { | |
892 | r_label = copy_to_reg (r_label); | |
893 | ||
18b42941 | 894 | emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode))); |
895 | emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx)); | |
491e04ef | 896 | |
4ee9c684 | 897 | /* Restore frame pointer for containing function. |
898 | This sets the actual hard register used for the frame pointer | |
899 | to the location of the function's incoming static chain info. | |
900 | The non-local goto handler will then adjust it to contain the | |
901 | proper value and reload the argument pointer, if needed. */ | |
902 | emit_move_insn (hard_frame_pointer_rtx, r_fp); | |
903 | emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX); | |
491e04ef | 904 | |
4ee9c684 | 905 | /* USE of hard_frame_pointer_rtx added for consistency; |
906 | not clear if really needed. */ | |
18b42941 | 907 | emit_use (hard_frame_pointer_rtx); |
908 | emit_use (stack_pointer_rtx); | |
ad0d0af8 | 909 | |
910 | /* If the architecture is using a GP register, we must | |
911 | conservatively assume that the target function makes use of it. | |
912 | The prologue of functions with nonlocal gotos must therefore | |
913 | initialize the GP register to the appropriate value, and we | |
914 | must then make sure that this value is live at the point | |
915 | of the jump. (Note that this doesn't necessarily apply | |
916 | to targets with a nonlocal_goto pattern; they are free | |
917 | to implement it in their own way. Note also that this is | |
918 | a no-op if the GP register is a global invariant.) */ | |
919 | if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM | |
920 | && fixed_regs[PIC_OFFSET_TABLE_REGNUM]) | |
18b42941 | 921 | emit_use (pic_offset_table_rtx); |
ad0d0af8 | 922 | |
4ee9c684 | 923 | emit_indirect_jump (r_label); |
924 | } | |
491e04ef | 925 | |
4ee9c684 | 926 | /* Search backwards to the jump insn and mark it as a |
927 | non-local goto. */ | |
928 | for (insn = get_last_insn (); insn; insn = PREV_INSN (insn)) | |
929 | { | |
6d7dc5b9 | 930 | if (JUMP_P (insn)) |
4ee9c684 | 931 | { |
932 | REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, | |
933 | const0_rtx, REG_NOTES (insn)); | |
934 | break; | |
935 | } | |
6d7dc5b9 | 936 | else if (CALL_P (insn)) |
4ee9c684 | 937 | break; |
938 | } | |
939 | ||
940 | return const0_rtx; | |
941 | } | |
942 | ||
843d08a9 | 943 | /* __builtin_update_setjmp_buf is passed a pointer to an array of five words |
944 | (not all will be used on all machines) that was passed to __builtin_setjmp. | |
945 | It updates the stack pointer in that block to correspond to the current | |
946 | stack pointer. */ | |
947 | ||
948 | static void | |
949 | expand_builtin_update_setjmp_buf (rtx buf_addr) | |
950 | { | |
951 | enum machine_mode sa_mode = Pmode; | |
952 | rtx stack_save; | |
953 | ||
954 | ||
955 | #ifdef HAVE_save_stack_nonlocal | |
956 | if (HAVE_save_stack_nonlocal) | |
957 | sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode; | |
958 | #endif | |
959 | #ifdef STACK_SAVEAREA_MODE | |
960 | sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL); | |
961 | #endif | |
962 | ||
963 | stack_save | |
964 | = gen_rtx_MEM (sa_mode, | |
965 | memory_address | |
966 | (sa_mode, | |
967 | plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode)))); | |
968 | ||
969 | #ifdef HAVE_setjmp | |
970 | if (HAVE_setjmp) | |
971 | emit_insn (gen_setjmp ()); | |
972 | #endif | |
973 | ||
974 | emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX); | |
975 | } | |
976 | ||
5e3608d8 | 977 | /* Expand a call to __builtin_prefetch. For a target that does not support |
978 | data prefetch, evaluate the memory address argument in case it has side | |
979 | effects. */ | |
980 | ||
981 | static void | |
c2f47e15 | 982 | expand_builtin_prefetch (tree exp) |
5e3608d8 | 983 | { |
984 | tree arg0, arg1, arg2; | |
c2f47e15 | 985 | int nargs; |
5e3608d8 | 986 | rtx op0, op1, op2; |
987 | ||
c2f47e15 | 988 | if (!validate_arglist (exp, POINTER_TYPE, 0)) |
26a5cadb | 989 | return; |
990 | ||
c2f47e15 | 991 | arg0 = CALL_EXPR_ARG (exp, 0); |
992 | ||
26a5cadb | 993 | /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to |
994 | zero (read) and argument 2 (locality) defaults to 3 (high degree of | |
995 | locality). */ | |
c2f47e15 | 996 | nargs = call_expr_nargs (exp); |
997 | if (nargs > 1) | |
998 | arg1 = CALL_EXPR_ARG (exp, 1); | |
26a5cadb | 999 | else |
c2f47e15 | 1000 | arg1 = integer_zero_node; |
1001 | if (nargs > 2) | |
1002 | arg2 = CALL_EXPR_ARG (exp, 2); | |
1003 | else | |
1004 | arg2 = build_int_cst (NULL_TREE, 3); | |
5e3608d8 | 1005 | |
1006 | /* Argument 0 is an address. */ | |
1007 | op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL); | |
1008 | ||
1009 | /* Argument 1 (read/write flag) must be a compile-time constant int. */ | |
1010 | if (TREE_CODE (arg1) != INTEGER_CST) | |
1011 | { | |
07e3a3d2 | 1012 | error ("second argument to %<__builtin_prefetch%> must be a constant"); |
9342ee68 | 1013 | arg1 = integer_zero_node; |
5e3608d8 | 1014 | } |
8ec3c5c2 | 1015 | op1 = expand_normal (arg1); |
5e3608d8 | 1016 | /* Argument 1 must be either zero or one. */ |
1017 | if (INTVAL (op1) != 0 && INTVAL (op1) != 1) | |
1018 | { | |
c3ceba8e | 1019 | warning (0, "invalid second argument to %<__builtin_prefetch%>;" |
07e3a3d2 | 1020 | " using zero"); |
5e3608d8 | 1021 | op1 = const0_rtx; |
1022 | } | |
1023 | ||
1024 | /* Argument 2 (locality) must be a compile-time constant int. */ | |
1025 | if (TREE_CODE (arg2) != INTEGER_CST) | |
1026 | { | |
07e3a3d2 | 1027 | error ("third argument to %<__builtin_prefetch%> must be a constant"); |
5e3608d8 | 1028 | arg2 = integer_zero_node; |
1029 | } | |
8ec3c5c2 | 1030 | op2 = expand_normal (arg2); |
5e3608d8 | 1031 | /* Argument 2 must be 0, 1, 2, or 3. */ |
1032 | if (INTVAL (op2) < 0 || INTVAL (op2) > 3) | |
1033 | { | |
c3ceba8e | 1034 | warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero"); |
5e3608d8 | 1035 | op2 = const0_rtx; |
1036 | } | |
1037 | ||
1038 | #ifdef HAVE_prefetch | |
1039 | if (HAVE_prefetch) | |
1040 | { | |
f0ce3b1f | 1041 | if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate) |
4cd21437 | 1042 | (op0, |
f0ce3b1f | 1043 | insn_data[(int) CODE_FOR_prefetch].operand[0].mode)) |
19bf118a | 1044 | || (GET_MODE (op0) != Pmode)) |
9342ee68 | 1045 | { |
85d654dd | 1046 | op0 = convert_memory_address (Pmode, op0); |
9342ee68 | 1047 | op0 = force_reg (Pmode, op0); |
1048 | } | |
5e3608d8 | 1049 | emit_insn (gen_prefetch (op0, op1, op2)); |
1050 | } | |
5e3608d8 | 1051 | #endif |
0a534ba7 | 1052 | |
f0ce3b1f | 1053 | /* Don't do anything with direct references to volatile memory, but |
1054 | generate code to handle other side effects. */ | |
e16ceb8e | 1055 | if (!MEM_P (op0) && side_effects_p (op0)) |
f0ce3b1f | 1056 | emit_insn (op0); |
5e3608d8 | 1057 | } |
1058 | ||
f7c44134 | 1059 | /* Get a MEM rtx for expression EXP which is the address of an operand |
d8ae1baa | 1060 | to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is |
1061 | the maximum length of the block of memory that might be accessed or | |
1062 | NULL if unknown. */ | |
f7c44134 | 1063 | |
53800dbe | 1064 | static rtx |
d8ae1baa | 1065 | get_memory_rtx (tree exp, tree len) |
53800dbe | 1066 | { |
bb2327a8 | 1067 | rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_NORMAL); |
1068 | rtx mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr)); | |
2a631e19 | 1069 | |
f7c44134 | 1070 | /* Get an expression we can use to find the attributes to assign to MEM. |
1071 | If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if | |
1072 | we can. First remove any nops. */ | |
72dd6141 | 1073 | while (CONVERT_EXPR_P (exp) |
f7c44134 | 1074 | && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0)))) |
1075 | exp = TREE_OPERAND (exp, 0); | |
1076 | ||
1077 | if (TREE_CODE (exp) == ADDR_EXPR) | |
a1a25d19 | 1078 | exp = TREE_OPERAND (exp, 0); |
f7c44134 | 1079 | else if (POINTER_TYPE_P (TREE_TYPE (exp))) |
a1a25d19 | 1080 | exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp); |
1081 | else | |
1082 | exp = NULL; | |
1083 | ||
1084 | /* Honor attributes derived from exp, except for the alias set | |
1085 | (as builtin stringops may alias with anything) and the size | |
1086 | (as stringops may access multiple array elements). */ | |
1087 | if (exp) | |
eec8e941 | 1088 | { |
a1a25d19 | 1089 | set_mem_attributes (mem, exp, 0); |
d8ae1baa | 1090 | |
1091 | /* Allow the string and memory builtins to overflow from one | |
1092 | field into another, see http://gcc.gnu.org/PR23561. | |
1093 | Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole | |
1094 | memory accessed by the string or memory builtin will fit | |
1095 | within the field. */ | |
1096 | if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF) | |
1097 | { | |
1098 | tree mem_expr = MEM_EXPR (mem); | |
1099 | HOST_WIDE_INT offset = -1, length = -1; | |
1100 | tree inner = exp; | |
1101 | ||
1102 | while (TREE_CODE (inner) == ARRAY_REF | |
72dd6141 | 1103 | || CONVERT_EXPR_P (inner) |
d8ae1baa | 1104 | || TREE_CODE (inner) == VIEW_CONVERT_EXPR |
1105 | || TREE_CODE (inner) == SAVE_EXPR) | |
1106 | inner = TREE_OPERAND (inner, 0); | |
1107 | ||
1108 | gcc_assert (TREE_CODE (inner) == COMPONENT_REF); | |
1109 | ||
1110 | if (MEM_OFFSET (mem) | |
1111 | && GET_CODE (MEM_OFFSET (mem)) == CONST_INT) | |
1112 | offset = INTVAL (MEM_OFFSET (mem)); | |
1113 | ||
1114 | if (offset >= 0 && len && host_integerp (len, 0)) | |
1115 | length = tree_low_cst (len, 0); | |
1116 | ||
1117 | while (TREE_CODE (inner) == COMPONENT_REF) | |
1118 | { | |
1119 | tree field = TREE_OPERAND (inner, 1); | |
d8ae1baa | 1120 | gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF); |
1121 | gcc_assert (field == TREE_OPERAND (mem_expr, 1)); | |
1122 | ||
6933ebec | 1123 | /* Bitfields are generally not byte-addressable. */ |
1124 | gcc_assert (!DECL_BIT_FIELD (field) | |
1125 | || ((tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1) | |
1126 | % BITS_PER_UNIT) == 0 | |
1127 | && host_integerp (DECL_SIZE (field), 0) | |
1128 | && (TREE_INT_CST_LOW (DECL_SIZE (field)) | |
1129 | % BITS_PER_UNIT) == 0)); | |
1130 | ||
b2f0b2a3 | 1131 | /* If we can prove that the memory starting at XEXP (mem, 0) and |
1132 | ending at XEXP (mem, 0) + LENGTH will fit into this field, we | |
1133 | can keep the COMPONENT_REF in MEM_EXPR. But be careful with | |
1134 | fields without DECL_SIZE_UNIT like flexible array members. */ | |
d8ae1baa | 1135 | if (length >= 0 |
b2f0b2a3 | 1136 | && DECL_SIZE_UNIT (field) |
6933ebec | 1137 | && host_integerp (DECL_SIZE_UNIT (field), 0)) |
d8ae1baa | 1138 | { |
1139 | HOST_WIDE_INT size | |
6933ebec | 1140 | = TREE_INT_CST_LOW (DECL_SIZE_UNIT (field)); |
d8ae1baa | 1141 | if (offset <= size |
1142 | && length <= size | |
1143 | && offset + length <= size) | |
1144 | break; | |
1145 | } | |
1146 | ||
1147 | if (offset >= 0 | |
1148 | && host_integerp (DECL_FIELD_OFFSET (field), 0)) | |
6933ebec | 1149 | offset += TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field)) |
d8ae1baa | 1150 | + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1) |
1151 | / BITS_PER_UNIT; | |
1152 | else | |
1153 | { | |
1154 | offset = -1; | |
1155 | length = -1; | |
1156 | } | |
1157 | ||
1158 | mem_expr = TREE_OPERAND (mem_expr, 0); | |
1159 | inner = TREE_OPERAND (inner, 0); | |
d8ae1baa | 1160 | } |
1161 | ||
1162 | if (mem_expr == NULL) | |
1163 | offset = -1; | |
1164 | if (mem_expr != MEM_EXPR (mem)) | |
1165 | { | |
1166 | set_mem_expr (mem, mem_expr); | |
1167 | set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX); | |
1168 | } | |
1169 | } | |
eec8e941 | 1170 | set_mem_alias_set (mem, 0); |
a1a25d19 | 1171 | set_mem_size (mem, NULL_RTX); |
eec8e941 | 1172 | } |
53800dbe | 1173 | |
53800dbe | 1174 | return mem; |
1175 | } | |
1176 | \f | |
1177 | /* Built-in functions to perform an untyped call and return. */ | |
1178 | ||
1179 | /* For each register that may be used for calling a function, this | |
1180 | gives a mode used to copy the register's value. VOIDmode indicates | |
1181 | the register is not used for calling a function. If the machine | |
1182 | has register windows, this gives only the outbound registers. | |
1183 | INCOMING_REGNO gives the corresponding inbound register. */ | |
1184 | static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER]; | |
1185 | ||
1186 | /* For each register that may be used for returning values, this gives | |
1187 | a mode used to copy the register's value. VOIDmode indicates the | |
1188 | register is not used for returning values. If the machine has | |
1189 | register windows, this gives only the outbound registers. | |
1190 | INCOMING_REGNO gives the corresponding inbound register. */ | |
1191 | static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER]; | |
1192 | ||
1193 | /* For each register that may be used for calling a function, this | |
1194 | gives the offset of that register into the block returned by | |
1195 | __builtin_apply_args. 0 indicates that the register is not | |
1196 | used for calling a function. */ | |
1197 | static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER]; | |
1198 | ||
53800dbe | 1199 | /* Return the size required for the block returned by __builtin_apply_args, |
1200 | and initialize apply_args_mode. */ | |
1201 | ||
1202 | static int | |
aecda0d6 | 1203 | apply_args_size (void) |
53800dbe | 1204 | { |
1205 | static int size = -1; | |
58e9ce8f | 1206 | int align; |
1207 | unsigned int regno; | |
53800dbe | 1208 | enum machine_mode mode; |
1209 | ||
1210 | /* The values computed by this function never change. */ | |
1211 | if (size < 0) | |
1212 | { | |
1213 | /* The first value is the incoming arg-pointer. */ | |
1214 | size = GET_MODE_SIZE (Pmode); | |
1215 | ||
1216 | /* The second value is the structure value address unless this is | |
1217 | passed as an "invisible" first argument. */ | |
6812c89e | 1218 | if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0)) |
53800dbe | 1219 | size += GET_MODE_SIZE (Pmode); |
1220 | ||
1221 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1222 | if (FUNCTION_ARG_REGNO_P (regno)) | |
1223 | { | |
0862b7e9 | 1224 | mode = reg_raw_mode[regno]; |
1225 | ||
64db345d | 1226 | gcc_assert (mode != VOIDmode); |
53800dbe | 1227 | |
1228 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1229 | if (size % align != 0) | |
1230 | size = CEIL (size, align) * align; | |
1231 | apply_args_reg_offset[regno] = size; | |
1232 | size += GET_MODE_SIZE (mode); | |
1233 | apply_args_mode[regno] = mode; | |
1234 | } | |
1235 | else | |
1236 | { | |
1237 | apply_args_mode[regno] = VOIDmode; | |
1238 | apply_args_reg_offset[regno] = 0; | |
1239 | } | |
1240 | } | |
1241 | return size; | |
1242 | } | |
1243 | ||
1244 | /* Return the size required for the block returned by __builtin_apply, | |
1245 | and initialize apply_result_mode. */ | |
1246 | ||
1247 | static int | |
aecda0d6 | 1248 | apply_result_size (void) |
53800dbe | 1249 | { |
1250 | static int size = -1; | |
1251 | int align, regno; | |
1252 | enum machine_mode mode; | |
1253 | ||
1254 | /* The values computed by this function never change. */ | |
1255 | if (size < 0) | |
1256 | { | |
1257 | size = 0; | |
1258 | ||
1259 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1260 | if (FUNCTION_VALUE_REGNO_P (regno)) | |
1261 | { | |
0862b7e9 | 1262 | mode = reg_raw_mode[regno]; |
1263 | ||
64db345d | 1264 | gcc_assert (mode != VOIDmode); |
53800dbe | 1265 | |
1266 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1267 | if (size % align != 0) | |
1268 | size = CEIL (size, align) * align; | |
1269 | size += GET_MODE_SIZE (mode); | |
1270 | apply_result_mode[regno] = mode; | |
1271 | } | |
1272 | else | |
1273 | apply_result_mode[regno] = VOIDmode; | |
1274 | ||
1275 | /* Allow targets that use untyped_call and untyped_return to override | |
1276 | the size so that machine-specific information can be stored here. */ | |
1277 | #ifdef APPLY_RESULT_SIZE | |
1278 | size = APPLY_RESULT_SIZE; | |
1279 | #endif | |
1280 | } | |
1281 | return size; | |
1282 | } | |
1283 | ||
1284 | #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return) | |
1285 | /* Create a vector describing the result block RESULT. If SAVEP is true, | |
1286 | the result block is used to save the values; otherwise it is used to | |
1287 | restore the values. */ | |
1288 | ||
1289 | static rtx | |
aecda0d6 | 1290 | result_vector (int savep, rtx result) |
53800dbe | 1291 | { |
1292 | int regno, size, align, nelts; | |
1293 | enum machine_mode mode; | |
1294 | rtx reg, mem; | |
f0af5a88 | 1295 | rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx)); |
bf8e3599 | 1296 | |
53800dbe | 1297 | size = nelts = 0; |
1298 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1299 | if ((mode = apply_result_mode[regno]) != VOIDmode) | |
1300 | { | |
1301 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1302 | if (size % align != 0) | |
1303 | size = CEIL (size, align) * align; | |
1304 | reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno)); | |
e513d163 | 1305 | mem = adjust_address (result, mode, size); |
53800dbe | 1306 | savevec[nelts++] = (savep |
1307 | ? gen_rtx_SET (VOIDmode, mem, reg) | |
1308 | : gen_rtx_SET (VOIDmode, reg, mem)); | |
1309 | size += GET_MODE_SIZE (mode); | |
1310 | } | |
1311 | return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec)); | |
1312 | } | |
1313 | #endif /* HAVE_untyped_call or HAVE_untyped_return */ | |
1314 | ||
1315 | /* Save the state required to perform an untyped call with the same | |
1316 | arguments as were passed to the current function. */ | |
1317 | ||
1318 | static rtx | |
aecda0d6 | 1319 | expand_builtin_apply_args_1 (void) |
53800dbe | 1320 | { |
1c7e61a7 | 1321 | rtx registers, tem; |
53800dbe | 1322 | int size, align, regno; |
1323 | enum machine_mode mode; | |
6812c89e | 1324 | rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1); |
53800dbe | 1325 | |
1326 | /* Create a block where the arg-pointer, structure value address, | |
1327 | and argument registers can be saved. */ | |
1328 | registers = assign_stack_local (BLKmode, apply_args_size (), -1); | |
1329 | ||
1330 | /* Walk past the arg-pointer and structure value address. */ | |
1331 | size = GET_MODE_SIZE (Pmode); | |
6812c89e | 1332 | if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0)) |
53800dbe | 1333 | size += GET_MODE_SIZE (Pmode); |
1334 | ||
1335 | /* Save each register used in calling a function to the block. */ | |
1336 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1337 | if ((mode = apply_args_mode[regno]) != VOIDmode) | |
1338 | { | |
53800dbe | 1339 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; |
1340 | if (size % align != 0) | |
1341 | size = CEIL (size, align) * align; | |
1342 | ||
1343 | tem = gen_rtx_REG (mode, INCOMING_REGNO (regno)); | |
1344 | ||
e513d163 | 1345 | emit_move_insn (adjust_address (registers, mode, size), tem); |
53800dbe | 1346 | size += GET_MODE_SIZE (mode); |
1347 | } | |
1348 | ||
1349 | /* Save the arg pointer to the block. */ | |
1c7e61a7 | 1350 | tem = copy_to_reg (virtual_incoming_args_rtx); |
f083a92b | 1351 | #ifdef STACK_GROWS_DOWNWARD |
1c7e61a7 | 1352 | /* We need the pointer as the caller actually passed them to us, not |
9d4b544c | 1353 | as we might have pretended they were passed. Make sure it's a valid |
1354 | operand, as emit_move_insn isn't expected to handle a PLUS. */ | |
1355 | tem | |
abe32cce | 1356 | = force_operand (plus_constant (tem, crtl->args.pretend_args_size), |
9d4b544c | 1357 | NULL_RTX); |
1c7e61a7 | 1358 | #endif |
1359 | emit_move_insn (adjust_address (registers, Pmode, 0), tem); | |
0862b7e9 | 1360 | |
53800dbe | 1361 | size = GET_MODE_SIZE (Pmode); |
1362 | ||
1363 | /* Save the structure value address unless this is passed as an | |
1364 | "invisible" first argument. */ | |
45550790 | 1365 | if (struct_incoming_value) |
53800dbe | 1366 | { |
e513d163 | 1367 | emit_move_insn (adjust_address (registers, Pmode, size), |
45550790 | 1368 | copy_to_reg (struct_incoming_value)); |
53800dbe | 1369 | size += GET_MODE_SIZE (Pmode); |
1370 | } | |
1371 | ||
1372 | /* Return the address of the block. */ | |
1373 | return copy_addr_to_reg (XEXP (registers, 0)); | |
1374 | } | |
1375 | ||
1376 | /* __builtin_apply_args returns block of memory allocated on | |
1377 | the stack into which is stored the arg pointer, structure | |
1378 | value address, static chain, and all the registers that might | |
1379 | possibly be used in performing a function call. The code is | |
1380 | moved to the start of the function so the incoming values are | |
1381 | saved. */ | |
27d0c333 | 1382 | |
53800dbe | 1383 | static rtx |
aecda0d6 | 1384 | expand_builtin_apply_args (void) |
53800dbe | 1385 | { |
1386 | /* Don't do __builtin_apply_args more than once in a function. | |
1387 | Save the result of the first call and reuse it. */ | |
1388 | if (apply_args_value != 0) | |
1389 | return apply_args_value; | |
1390 | { | |
1391 | /* When this function is called, it means that registers must be | |
1392 | saved on entry to this function. So we migrate the | |
1393 | call to the first insn of this function. */ | |
1394 | rtx temp; | |
1395 | rtx seq; | |
1396 | ||
1397 | start_sequence (); | |
1398 | temp = expand_builtin_apply_args_1 (); | |
1399 | seq = get_insns (); | |
1400 | end_sequence (); | |
1401 | ||
1402 | apply_args_value = temp; | |
1403 | ||
31d3e01c | 1404 | /* Put the insns after the NOTE that starts the function. |
1405 | If this is inside a start_sequence, make the outer-level insn | |
53800dbe | 1406 | chain current, so the code is placed at the start of the |
1407 | function. */ | |
1408 | push_topmost_sequence (); | |
0ec80471 | 1409 | emit_insn_before (seq, NEXT_INSN (entry_of_function ())); |
53800dbe | 1410 | pop_topmost_sequence (); |
1411 | return temp; | |
1412 | } | |
1413 | } | |
1414 | ||
1415 | /* Perform an untyped call and save the state required to perform an | |
1416 | untyped return of whatever value was returned by the given function. */ | |
1417 | ||
1418 | static rtx | |
aecda0d6 | 1419 | expand_builtin_apply (rtx function, rtx arguments, rtx argsize) |
53800dbe | 1420 | { |
1421 | int size, align, regno; | |
1422 | enum machine_mode mode; | |
2a631e19 | 1423 | rtx incoming_args, result, reg, dest, src, call_insn; |
53800dbe | 1424 | rtx old_stack_level = 0; |
1425 | rtx call_fusage = 0; | |
6812c89e | 1426 | rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0); |
53800dbe | 1427 | |
85d654dd | 1428 | arguments = convert_memory_address (Pmode, arguments); |
726ec87c | 1429 | |
53800dbe | 1430 | /* Create a block where the return registers can be saved. */ |
1431 | result = assign_stack_local (BLKmode, apply_result_size (), -1); | |
1432 | ||
53800dbe | 1433 | /* Fetch the arg pointer from the ARGUMENTS block. */ |
1434 | incoming_args = gen_reg_rtx (Pmode); | |
726ec87c | 1435 | emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments)); |
53800dbe | 1436 | #ifndef STACK_GROWS_DOWNWARD |
ad99e708 | 1437 | incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize, |
1438 | incoming_args, 0, OPTAB_LIB_WIDEN); | |
53800dbe | 1439 | #endif |
1440 | ||
04a46d40 | 1441 | /* Push a new argument block and copy the arguments. Do not allow |
1442 | the (potential) memcpy call below to interfere with our stack | |
1443 | manipulations. */ | |
53800dbe | 1444 | do_pending_stack_adjust (); |
04a46d40 | 1445 | NO_DEFER_POP; |
53800dbe | 1446 | |
2358393e | 1447 | /* Save the stack with nonlocal if available. */ |
53800dbe | 1448 | #ifdef HAVE_save_stack_nonlocal |
1449 | if (HAVE_save_stack_nonlocal) | |
1450 | emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX); | |
1451 | else | |
1452 | #endif | |
1453 | emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX); | |
1454 | ||
59647703 | 1455 | /* Allocate a block of memory onto the stack and copy the memory |
1456 | arguments to the outgoing arguments address. */ | |
1457 | allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT); | |
1458 | dest = virtual_outgoing_args_rtx; | |
1459 | #ifndef STACK_GROWS_DOWNWARD | |
1460 | if (GET_CODE (argsize) == CONST_INT) | |
1461 | dest = plus_constant (dest, -INTVAL (argsize)); | |
1462 | else | |
1463 | dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize)); | |
1464 | #endif | |
2a631e19 | 1465 | dest = gen_rtx_MEM (BLKmode, dest); |
1466 | set_mem_align (dest, PARM_BOUNDARY); | |
1467 | src = gen_rtx_MEM (BLKmode, incoming_args); | |
1468 | set_mem_align (src, PARM_BOUNDARY); | |
0378dbdc | 1469 | emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL); |
53800dbe | 1470 | |
1471 | /* Refer to the argument block. */ | |
1472 | apply_args_size (); | |
1473 | arguments = gen_rtx_MEM (BLKmode, arguments); | |
2a631e19 | 1474 | set_mem_align (arguments, PARM_BOUNDARY); |
53800dbe | 1475 | |
1476 | /* Walk past the arg-pointer and structure value address. */ | |
1477 | size = GET_MODE_SIZE (Pmode); | |
45550790 | 1478 | if (struct_value) |
53800dbe | 1479 | size += GET_MODE_SIZE (Pmode); |
1480 | ||
1481 | /* Restore each of the registers previously saved. Make USE insns | |
1482 | for each of these registers for use in making the call. */ | |
1483 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1484 | if ((mode = apply_args_mode[regno]) != VOIDmode) | |
1485 | { | |
1486 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1487 | if (size % align != 0) | |
1488 | size = CEIL (size, align) * align; | |
1489 | reg = gen_rtx_REG (mode, regno); | |
e513d163 | 1490 | emit_move_insn (reg, adjust_address (arguments, mode, size)); |
53800dbe | 1491 | use_reg (&call_fusage, reg); |
1492 | size += GET_MODE_SIZE (mode); | |
1493 | } | |
1494 | ||
1495 | /* Restore the structure value address unless this is passed as an | |
1496 | "invisible" first argument. */ | |
1497 | size = GET_MODE_SIZE (Pmode); | |
45550790 | 1498 | if (struct_value) |
53800dbe | 1499 | { |
1500 | rtx value = gen_reg_rtx (Pmode); | |
e513d163 | 1501 | emit_move_insn (value, adjust_address (arguments, Pmode, size)); |
45550790 | 1502 | emit_move_insn (struct_value, value); |
8ad4c111 | 1503 | if (REG_P (struct_value)) |
45550790 | 1504 | use_reg (&call_fusage, struct_value); |
53800dbe | 1505 | size += GET_MODE_SIZE (Pmode); |
1506 | } | |
1507 | ||
1508 | /* All arguments and registers used for the call are set up by now! */ | |
4ee9c684 | 1509 | function = prepare_call_address (function, NULL, &call_fusage, 0, 0); |
53800dbe | 1510 | |
1511 | /* Ensure address is valid. SYMBOL_REF is already valid, so no need, | |
1512 | and we don't want to load it into a register as an optimization, | |
1513 | because prepare_call_address already did it if it should be done. */ | |
1514 | if (GET_CODE (function) != SYMBOL_REF) | |
1515 | function = memory_address (FUNCTION_MODE, function); | |
1516 | ||
1517 | /* Generate the actual call instruction and save the return value. */ | |
1518 | #ifdef HAVE_untyped_call | |
1519 | if (HAVE_untyped_call) | |
1520 | emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function), | |
1521 | result, result_vector (1, result))); | |
1522 | else | |
1523 | #endif | |
1524 | #ifdef HAVE_call_value | |
1525 | if (HAVE_call_value) | |
1526 | { | |
1527 | rtx valreg = 0; | |
1528 | ||
1529 | /* Locate the unique return register. It is not possible to | |
1530 | express a call that sets more than one return register using | |
1531 | call_value; use untyped_call for that. In fact, untyped_call | |
1532 | only needs to save the return registers in the given block. */ | |
1533 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1534 | if ((mode = apply_result_mode[regno]) != VOIDmode) | |
1535 | { | |
64db345d | 1536 | gcc_assert (!valreg); /* HAVE_untyped_call required. */ |
7d3f6cc7 | 1537 | |
53800dbe | 1538 | valreg = gen_rtx_REG (mode, regno); |
1539 | } | |
1540 | ||
2ed6c343 | 1541 | emit_call_insn (GEN_CALL_VALUE (valreg, |
53800dbe | 1542 | gen_rtx_MEM (FUNCTION_MODE, function), |
1543 | const0_rtx, NULL_RTX, const0_rtx)); | |
1544 | ||
e513d163 | 1545 | emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg); |
53800dbe | 1546 | } |
1547 | else | |
1548 | #endif | |
64db345d | 1549 | gcc_unreachable (); |
53800dbe | 1550 | |
d5f9786f | 1551 | /* Find the CALL insn we just emitted, and attach the register usage |
1552 | information. */ | |
1553 | call_insn = last_call_insn (); | |
1554 | add_function_usage_to (call_insn, call_fusage); | |
53800dbe | 1555 | |
1556 | /* Restore the stack. */ | |
1557 | #ifdef HAVE_save_stack_nonlocal | |
1558 | if (HAVE_save_stack_nonlocal) | |
1559 | emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX); | |
1560 | else | |
1561 | #endif | |
1562 | emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX); | |
1563 | ||
04a46d40 | 1564 | OK_DEFER_POP; |
1565 | ||
53800dbe | 1566 | /* Return the address of the result block. */ |
85d654dd | 1567 | result = copy_addr_to_reg (XEXP (result, 0)); |
1568 | return convert_memory_address (ptr_mode, result); | |
53800dbe | 1569 | } |
1570 | ||
1571 | /* Perform an untyped return. */ | |
1572 | ||
1573 | static void | |
aecda0d6 | 1574 | expand_builtin_return (rtx result) |
53800dbe | 1575 | { |
1576 | int size, align, regno; | |
1577 | enum machine_mode mode; | |
1578 | rtx reg; | |
1579 | rtx call_fusage = 0; | |
1580 | ||
85d654dd | 1581 | result = convert_memory_address (Pmode, result); |
726ec87c | 1582 | |
53800dbe | 1583 | apply_result_size (); |
1584 | result = gen_rtx_MEM (BLKmode, result); | |
1585 | ||
1586 | #ifdef HAVE_untyped_return | |
1587 | if (HAVE_untyped_return) | |
1588 | { | |
1589 | emit_jump_insn (gen_untyped_return (result, result_vector (0, result))); | |
1590 | emit_barrier (); | |
1591 | return; | |
1592 | } | |
1593 | #endif | |
1594 | ||
1595 | /* Restore the return value and note that each value is used. */ | |
1596 | size = 0; | |
1597 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1598 | if ((mode = apply_result_mode[regno]) != VOIDmode) | |
1599 | { | |
1600 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1601 | if (size % align != 0) | |
1602 | size = CEIL (size, align) * align; | |
1603 | reg = gen_rtx_REG (mode, INCOMING_REGNO (regno)); | |
e513d163 | 1604 | emit_move_insn (reg, adjust_address (result, mode, size)); |
53800dbe | 1605 | |
1606 | push_to_sequence (call_fusage); | |
18b42941 | 1607 | emit_use (reg); |
53800dbe | 1608 | call_fusage = get_insns (); |
1609 | end_sequence (); | |
1610 | size += GET_MODE_SIZE (mode); | |
1611 | } | |
1612 | ||
1613 | /* Put the USE insns before the return. */ | |
31d3e01c | 1614 | emit_insn (call_fusage); |
53800dbe | 1615 | |
1616 | /* Return whatever values was restored by jumping directly to the end | |
1617 | of the function. */ | |
62380d2d | 1618 | expand_naked_return (); |
53800dbe | 1619 | } |
1620 | ||
539a3a92 | 1621 | /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */ |
27d0c333 | 1622 | |
539a3a92 | 1623 | static enum type_class |
aecda0d6 | 1624 | type_to_class (tree type) |
539a3a92 | 1625 | { |
1626 | switch (TREE_CODE (type)) | |
1627 | { | |
1628 | case VOID_TYPE: return void_type_class; | |
1629 | case INTEGER_TYPE: return integer_type_class; | |
539a3a92 | 1630 | case ENUMERAL_TYPE: return enumeral_type_class; |
1631 | case BOOLEAN_TYPE: return boolean_type_class; | |
1632 | case POINTER_TYPE: return pointer_type_class; | |
1633 | case REFERENCE_TYPE: return reference_type_class; | |
1634 | case OFFSET_TYPE: return offset_type_class; | |
1635 | case REAL_TYPE: return real_type_class; | |
1636 | case COMPLEX_TYPE: return complex_type_class; | |
1637 | case FUNCTION_TYPE: return function_type_class; | |
1638 | case METHOD_TYPE: return method_type_class; | |
1639 | case RECORD_TYPE: return record_type_class; | |
1640 | case UNION_TYPE: | |
1641 | case QUAL_UNION_TYPE: return union_type_class; | |
1642 | case ARRAY_TYPE: return (TYPE_STRING_FLAG (type) | |
1643 | ? string_type_class : array_type_class); | |
539a3a92 | 1644 | case LANG_TYPE: return lang_type_class; |
1645 | default: return no_type_class; | |
1646 | } | |
1647 | } | |
bf8e3599 | 1648 | |
c2f47e15 | 1649 | /* Expand a call EXP to __builtin_classify_type. */ |
27d0c333 | 1650 | |
53800dbe | 1651 | static rtx |
c2f47e15 | 1652 | expand_builtin_classify_type (tree exp) |
53800dbe | 1653 | { |
c2f47e15 | 1654 | if (call_expr_nargs (exp)) |
1655 | return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))); | |
53800dbe | 1656 | return GEN_INT (no_type_class); |
1657 | } | |
1658 | ||
07976da7 | 1659 | /* This helper macro, meant to be used in mathfn_built_in below, |
1660 | determines which among a set of three builtin math functions is | |
1661 | appropriate for a given type mode. The `F' and `L' cases are | |
1662 | automatically generated from the `double' case. */ | |
1663 | #define CASE_MATHFN(BUILT_IN_MATHFN) \ | |
1664 | case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \ | |
1665 | fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \ | |
1666 | fcodel = BUILT_IN_MATHFN##L ; break; | |
cd2656b0 | 1667 | /* Similar to above, but appends _R after any F/L suffix. */ |
1668 | #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \ | |
1669 | case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \ | |
1670 | fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \ | |
1671 | fcodel = BUILT_IN_MATHFN##L_R ; break; | |
07976da7 | 1672 | |
1673 | /* Return mathematic function equivalent to FN but operating directly | |
c319d56a | 1674 | on TYPE, if available. If IMPLICIT is true find the function in |
1675 | implicit_built_in_decls[], otherwise use built_in_decls[]. If we | |
1676 | can't do the conversion, return zero. */ | |
1677 | ||
1678 | static tree | |
1679 | mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit) | |
0a68165a | 1680 | { |
c319d56a | 1681 | tree const *const fn_arr |
1682 | = implicit ? implicit_built_in_decls : built_in_decls; | |
07976da7 | 1683 | enum built_in_function fcode, fcodef, fcodel; |
1684 | ||
1685 | switch (fn) | |
1686 | { | |
746114e8 | 1687 | CASE_MATHFN (BUILT_IN_ACOS) |
1688 | CASE_MATHFN (BUILT_IN_ACOSH) | |
1689 | CASE_MATHFN (BUILT_IN_ASIN) | |
1690 | CASE_MATHFN (BUILT_IN_ASINH) | |
07976da7 | 1691 | CASE_MATHFN (BUILT_IN_ATAN) |
746114e8 | 1692 | CASE_MATHFN (BUILT_IN_ATAN2) |
1693 | CASE_MATHFN (BUILT_IN_ATANH) | |
1694 | CASE_MATHFN (BUILT_IN_CBRT) | |
07976da7 | 1695 | CASE_MATHFN (BUILT_IN_CEIL) |
d735c391 | 1696 | CASE_MATHFN (BUILT_IN_CEXPI) |
746114e8 | 1697 | CASE_MATHFN (BUILT_IN_COPYSIGN) |
07976da7 | 1698 | CASE_MATHFN (BUILT_IN_COS) |
746114e8 | 1699 | CASE_MATHFN (BUILT_IN_COSH) |
1700 | CASE_MATHFN (BUILT_IN_DREM) | |
1701 | CASE_MATHFN (BUILT_IN_ERF) | |
1702 | CASE_MATHFN (BUILT_IN_ERFC) | |
07976da7 | 1703 | CASE_MATHFN (BUILT_IN_EXP) |
746114e8 | 1704 | CASE_MATHFN (BUILT_IN_EXP10) |
1705 | CASE_MATHFN (BUILT_IN_EXP2) | |
1706 | CASE_MATHFN (BUILT_IN_EXPM1) | |
1707 | CASE_MATHFN (BUILT_IN_FABS) | |
1708 | CASE_MATHFN (BUILT_IN_FDIM) | |
07976da7 | 1709 | CASE_MATHFN (BUILT_IN_FLOOR) |
746114e8 | 1710 | CASE_MATHFN (BUILT_IN_FMA) |
1711 | CASE_MATHFN (BUILT_IN_FMAX) | |
1712 | CASE_MATHFN (BUILT_IN_FMIN) | |
1713 | CASE_MATHFN (BUILT_IN_FMOD) | |
1714 | CASE_MATHFN (BUILT_IN_FREXP) | |
1715 | CASE_MATHFN (BUILT_IN_GAMMA) | |
cd2656b0 | 1716 | CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */ |
746114e8 | 1717 | CASE_MATHFN (BUILT_IN_HUGE_VAL) |
1718 | CASE_MATHFN (BUILT_IN_HYPOT) | |
1719 | CASE_MATHFN (BUILT_IN_ILOGB) | |
1720 | CASE_MATHFN (BUILT_IN_INF) | |
69b779ea | 1721 | CASE_MATHFN (BUILT_IN_ISINF) |
746114e8 | 1722 | CASE_MATHFN (BUILT_IN_J0) |
1723 | CASE_MATHFN (BUILT_IN_J1) | |
1724 | CASE_MATHFN (BUILT_IN_JN) | |
ac148751 | 1725 | CASE_MATHFN (BUILT_IN_LCEIL) |
746114e8 | 1726 | CASE_MATHFN (BUILT_IN_LDEXP) |
ad52b9b7 | 1727 | CASE_MATHFN (BUILT_IN_LFLOOR) |
746114e8 | 1728 | CASE_MATHFN (BUILT_IN_LGAMMA) |
cd2656b0 | 1729 | CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */ |
ac148751 | 1730 | CASE_MATHFN (BUILT_IN_LLCEIL) |
ad52b9b7 | 1731 | CASE_MATHFN (BUILT_IN_LLFLOOR) |
746114e8 | 1732 | CASE_MATHFN (BUILT_IN_LLRINT) |
1733 | CASE_MATHFN (BUILT_IN_LLROUND) | |
07976da7 | 1734 | CASE_MATHFN (BUILT_IN_LOG) |
746114e8 | 1735 | CASE_MATHFN (BUILT_IN_LOG10) |
1736 | CASE_MATHFN (BUILT_IN_LOG1P) | |
1737 | CASE_MATHFN (BUILT_IN_LOG2) | |
1738 | CASE_MATHFN (BUILT_IN_LOGB) | |
1739 | CASE_MATHFN (BUILT_IN_LRINT) | |
1740 | CASE_MATHFN (BUILT_IN_LROUND) | |
1741 | CASE_MATHFN (BUILT_IN_MODF) | |
1742 | CASE_MATHFN (BUILT_IN_NAN) | |
1743 | CASE_MATHFN (BUILT_IN_NANS) | |
07976da7 | 1744 | CASE_MATHFN (BUILT_IN_NEARBYINT) |
746114e8 | 1745 | CASE_MATHFN (BUILT_IN_NEXTAFTER) |
1746 | CASE_MATHFN (BUILT_IN_NEXTTOWARD) | |
1747 | CASE_MATHFN (BUILT_IN_POW) | |
757c219d | 1748 | CASE_MATHFN (BUILT_IN_POWI) |
746114e8 | 1749 | CASE_MATHFN (BUILT_IN_POW10) |
1750 | CASE_MATHFN (BUILT_IN_REMAINDER) | |
1751 | CASE_MATHFN (BUILT_IN_REMQUO) | |
1752 | CASE_MATHFN (BUILT_IN_RINT) | |
07976da7 | 1753 | CASE_MATHFN (BUILT_IN_ROUND) |
746114e8 | 1754 | CASE_MATHFN (BUILT_IN_SCALB) |
1755 | CASE_MATHFN (BUILT_IN_SCALBLN) | |
1756 | CASE_MATHFN (BUILT_IN_SCALBN) | |
c319d56a | 1757 | CASE_MATHFN (BUILT_IN_SIGNBIT) |
746114e8 | 1758 | CASE_MATHFN (BUILT_IN_SIGNIFICAND) |
07976da7 | 1759 | CASE_MATHFN (BUILT_IN_SIN) |
746114e8 | 1760 | CASE_MATHFN (BUILT_IN_SINCOS) |
1761 | CASE_MATHFN (BUILT_IN_SINH) | |
07976da7 | 1762 | CASE_MATHFN (BUILT_IN_SQRT) |
1763 | CASE_MATHFN (BUILT_IN_TAN) | |
746114e8 | 1764 | CASE_MATHFN (BUILT_IN_TANH) |
1765 | CASE_MATHFN (BUILT_IN_TGAMMA) | |
07976da7 | 1766 | CASE_MATHFN (BUILT_IN_TRUNC) |
746114e8 | 1767 | CASE_MATHFN (BUILT_IN_Y0) |
1768 | CASE_MATHFN (BUILT_IN_Y1) | |
1769 | CASE_MATHFN (BUILT_IN_YN) | |
07976da7 | 1770 | |
0a68165a | 1771 | default: |
c2f47e15 | 1772 | return NULL_TREE; |
0a68165a | 1773 | } |
07976da7 | 1774 | |
96b9f485 | 1775 | if (TYPE_MAIN_VARIANT (type) == double_type_node) |
c319d56a | 1776 | return fn_arr[fcode]; |
96b9f485 | 1777 | else if (TYPE_MAIN_VARIANT (type) == float_type_node) |
c319d56a | 1778 | return fn_arr[fcodef]; |
96b9f485 | 1779 | else if (TYPE_MAIN_VARIANT (type) == long_double_type_node) |
c319d56a | 1780 | return fn_arr[fcodel]; |
07976da7 | 1781 | else |
c2f47e15 | 1782 | return NULL_TREE; |
0a68165a | 1783 | } |
1784 | ||
c319d56a | 1785 | /* Like mathfn_built_in_1(), but always use the implicit array. */ |
1786 | ||
1787 | tree | |
1788 | mathfn_built_in (tree type, enum built_in_function fn) | |
1789 | { | |
1790 | return mathfn_built_in_1 (type, fn, /*implicit=*/ 1); | |
1791 | } | |
1792 | ||
0fd605a5 | 1793 | /* If errno must be maintained, expand the RTL to check if the result, |
1794 | TARGET, of a built-in function call, EXP, is NaN, and if so set | |
1795 | errno to EDOM. */ | |
1796 | ||
1797 | static void | |
aecda0d6 | 1798 | expand_errno_check (tree exp, rtx target) |
0fd605a5 | 1799 | { |
7f05340e | 1800 | rtx lab = gen_label_rtx (); |
0fd605a5 | 1801 | |
7f05340e | 1802 | /* Test the result; if it is NaN, set errno=EDOM because |
1803 | the argument was not in the domain. */ | |
1804 | emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target), | |
1805 | 0, lab); | |
0fd605a5 | 1806 | |
1807 | #ifdef TARGET_EDOM | |
7f05340e | 1808 | /* If this built-in doesn't throw an exception, set errno directly. */ |
c2f47e15 | 1809 | if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0))) |
7f05340e | 1810 | { |
0fd605a5 | 1811 | #ifdef GEN_ERRNO_RTX |
7f05340e | 1812 | rtx errno_rtx = GEN_ERRNO_RTX; |
0fd605a5 | 1813 | #else |
7f05340e | 1814 | rtx errno_rtx |
0fd605a5 | 1815 | = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno")); |
1816 | #endif | |
7f05340e | 1817 | emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM)); |
0fd605a5 | 1818 | emit_label (lab); |
7f05340e | 1819 | return; |
0fd605a5 | 1820 | } |
7f05340e | 1821 | #endif |
1822 | ||
08491912 | 1823 | /* Make sure the library call isn't expanded as a tail call. */ |
1824 | CALL_EXPR_TAILCALL (exp) = 0; | |
1825 | ||
7f05340e | 1826 | /* We can't set errno=EDOM directly; let the library call do it. |
1827 | Pop the arguments right away in case the call gets deleted. */ | |
1828 | NO_DEFER_POP; | |
1829 | expand_call (exp, target, 0); | |
1830 | OK_DEFER_POP; | |
1831 | emit_label (lab); | |
0fd605a5 | 1832 | } |
1833 | ||
6b43bae4 | 1834 | /* Expand a call to one of the builtin math functions (sqrt, exp, or log). |
c2f47e15 | 1835 | Return NULL_RTX if a normal call should be emitted rather than expanding |
1836 | the function in-line. EXP is the expression that is a call to the builtin | |
53800dbe | 1837 | function; if convenient, the result should be placed in TARGET. |
1838 | SUBTARGET may be used as the target for computing one of EXP's operands. */ | |
27d0c333 | 1839 | |
53800dbe | 1840 | static rtx |
aecda0d6 | 1841 | expand_builtin_mathfn (tree exp, rtx target, rtx subtarget) |
53800dbe | 1842 | { |
bf8e3599 | 1843 | optab builtin_optab; |
bd421108 | 1844 | rtx op0, insns, before_call; |
c6e6ecb1 | 1845 | tree fndecl = get_callee_fndecl (exp); |
7f05340e | 1846 | enum machine_mode mode; |
528ee710 | 1847 | bool errno_set = false; |
abfea505 | 1848 | tree arg; |
53800dbe | 1849 | |
c2f47e15 | 1850 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
1851 | return NULL_RTX; | |
53800dbe | 1852 | |
c2f47e15 | 1853 | arg = CALL_EXPR_ARG (exp, 0); |
53800dbe | 1854 | |
1855 | switch (DECL_FUNCTION_CODE (fndecl)) | |
1856 | { | |
4f35b1fc | 1857 | CASE_FLT_FN (BUILT_IN_SQRT): |
7f05340e | 1858 | errno_set = ! tree_expr_nonnegative_p (arg); |
1859 | builtin_optab = sqrt_optab; | |
1860 | break; | |
4f35b1fc | 1861 | CASE_FLT_FN (BUILT_IN_EXP): |
528ee710 | 1862 | errno_set = true; builtin_optab = exp_optab; break; |
4f35b1fc | 1863 | CASE_FLT_FN (BUILT_IN_EXP10): |
1864 | CASE_FLT_FN (BUILT_IN_POW10): | |
750ef9f5 | 1865 | errno_set = true; builtin_optab = exp10_optab; break; |
4f35b1fc | 1866 | CASE_FLT_FN (BUILT_IN_EXP2): |
750ef9f5 | 1867 | errno_set = true; builtin_optab = exp2_optab; break; |
4f35b1fc | 1868 | CASE_FLT_FN (BUILT_IN_EXPM1): |
a6b4eed2 | 1869 | errno_set = true; builtin_optab = expm1_optab; break; |
4f35b1fc | 1870 | CASE_FLT_FN (BUILT_IN_LOGB): |
4efbc641 | 1871 | errno_set = true; builtin_optab = logb_optab; break; |
4f35b1fc | 1872 | CASE_FLT_FN (BUILT_IN_LOG): |
528ee710 | 1873 | errno_set = true; builtin_optab = log_optab; break; |
4f35b1fc | 1874 | CASE_FLT_FN (BUILT_IN_LOG10): |
d3cd9bde | 1875 | errno_set = true; builtin_optab = log10_optab; break; |
4f35b1fc | 1876 | CASE_FLT_FN (BUILT_IN_LOG2): |
d3cd9bde | 1877 | errno_set = true; builtin_optab = log2_optab; break; |
4f35b1fc | 1878 | CASE_FLT_FN (BUILT_IN_LOG1P): |
f474cd93 | 1879 | errno_set = true; builtin_optab = log1p_optab; break; |
4f35b1fc | 1880 | CASE_FLT_FN (BUILT_IN_ASIN): |
8de2f465 | 1881 | builtin_optab = asin_optab; break; |
4f35b1fc | 1882 | CASE_FLT_FN (BUILT_IN_ACOS): |
8de2f465 | 1883 | builtin_optab = acos_optab; break; |
4f35b1fc | 1884 | CASE_FLT_FN (BUILT_IN_TAN): |
528ee710 | 1885 | builtin_optab = tan_optab; break; |
4f35b1fc | 1886 | CASE_FLT_FN (BUILT_IN_ATAN): |
528ee710 | 1887 | builtin_optab = atan_optab; break; |
4f35b1fc | 1888 | CASE_FLT_FN (BUILT_IN_FLOOR): |
528ee710 | 1889 | builtin_optab = floor_optab; break; |
4f35b1fc | 1890 | CASE_FLT_FN (BUILT_IN_CEIL): |
528ee710 | 1891 | builtin_optab = ceil_optab; break; |
4f35b1fc | 1892 | CASE_FLT_FN (BUILT_IN_TRUNC): |
a7cc195f | 1893 | builtin_optab = btrunc_optab; break; |
4f35b1fc | 1894 | CASE_FLT_FN (BUILT_IN_ROUND): |
528ee710 | 1895 | builtin_optab = round_optab; break; |
4f35b1fc | 1896 | CASE_FLT_FN (BUILT_IN_NEARBYINT): |
0ddf4ad9 | 1897 | builtin_optab = nearbyint_optab; |
1898 | if (flag_trapping_math) | |
1899 | break; | |
1900 | /* Else fallthrough and expand as rint. */ | |
4f35b1fc | 1901 | CASE_FLT_FN (BUILT_IN_RINT): |
aef94a0f | 1902 | builtin_optab = rint_optab; break; |
42721db0 | 1903 | default: |
64db345d | 1904 | gcc_unreachable (); |
53800dbe | 1905 | } |
1906 | ||
7f05340e | 1907 | /* Make a suitable register to place result in. */ |
1908 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
fc4eef90 | 1909 | |
7f05340e | 1910 | if (! flag_errno_math || ! HONOR_NANS (mode)) |
1911 | errno_set = false; | |
1912 | ||
bd421108 | 1913 | /* Before working hard, check whether the instruction is available. */ |
99bdde56 | 1914 | if (optab_handler (builtin_optab, mode)->insn_code != CODE_FOR_nothing) |
68e6cb9d | 1915 | { |
bd421108 | 1916 | target = gen_reg_rtx (mode); |
7f05340e | 1917 | |
bd421108 | 1918 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may |
1919 | need to expand the argument again. This way, we will not perform | |
1920 | side-effects more the once. */ | |
abfea505 | 1921 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); |
7f05340e | 1922 | |
1db6d067 | 1923 | op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL); |
7f05340e | 1924 | |
bd421108 | 1925 | start_sequence (); |
53800dbe | 1926 | |
bd421108 | 1927 | /* Compute into TARGET. |
1928 | Set TARGET to wherever the result comes back. */ | |
1929 | target = expand_unop (mode, builtin_optab, op0, target, 0); | |
1930 | ||
1931 | if (target != 0) | |
1932 | { | |
1933 | if (errno_set) | |
1934 | expand_errno_check (exp, target); | |
1935 | ||
1936 | /* Output the entire sequence. */ | |
1937 | insns = get_insns (); | |
1938 | end_sequence (); | |
1939 | emit_insn (insns); | |
1940 | return target; | |
1941 | } | |
1942 | ||
1943 | /* If we were unable to expand via the builtin, stop the sequence | |
1944 | (without outputting the insns) and call to the library function | |
1945 | with the stabilized argument list. */ | |
53800dbe | 1946 | end_sequence (); |
53800dbe | 1947 | } |
1948 | ||
bd421108 | 1949 | before_call = get_last_insn (); |
1950 | ||
1e5b92fa | 1951 | return expand_call (exp, target, target == const0_rtx); |
0fd605a5 | 1952 | } |
1953 | ||
1954 | /* Expand a call to the builtin binary math functions (pow and atan2). | |
c2f47e15 | 1955 | Return NULL_RTX if a normal call should be emitted rather than expanding the |
0fd605a5 | 1956 | function in-line. EXP is the expression that is a call to the builtin |
1957 | function; if convenient, the result should be placed in TARGET. | |
1958 | SUBTARGET may be used as the target for computing one of EXP's | |
1959 | operands. */ | |
1960 | ||
1961 | static rtx | |
aecda0d6 | 1962 | expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget) |
0fd605a5 | 1963 | { |
1964 | optab builtin_optab; | |
1965 | rtx op0, op1, insns; | |
4737caf2 | 1966 | int op1_type = REAL_TYPE; |
c6e6ecb1 | 1967 | tree fndecl = get_callee_fndecl (exp); |
abfea505 | 1968 | tree arg0, arg1; |
7f05340e | 1969 | enum machine_mode mode; |
0fd605a5 | 1970 | bool errno_set = true; |
0fd605a5 | 1971 | |
73a954a1 | 1972 | switch (DECL_FUNCTION_CODE (fndecl)) |
1973 | { | |
1974 | CASE_FLT_FN (BUILT_IN_SCALBN): | |
1975 | CASE_FLT_FN (BUILT_IN_SCALBLN): | |
1976 | CASE_FLT_FN (BUILT_IN_LDEXP): | |
1977 | op1_type = INTEGER_TYPE; | |
1978 | default: | |
1979 | break; | |
1980 | } | |
4737caf2 | 1981 | |
c2f47e15 | 1982 | if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE)) |
1983 | return NULL_RTX; | |
0fd605a5 | 1984 | |
c2f47e15 | 1985 | arg0 = CALL_EXPR_ARG (exp, 0); |
1986 | arg1 = CALL_EXPR_ARG (exp, 1); | |
0fd605a5 | 1987 | |
0fd605a5 | 1988 | switch (DECL_FUNCTION_CODE (fndecl)) |
1989 | { | |
4f35b1fc | 1990 | CASE_FLT_FN (BUILT_IN_POW): |
0fd605a5 | 1991 | builtin_optab = pow_optab; break; |
4f35b1fc | 1992 | CASE_FLT_FN (BUILT_IN_ATAN2): |
0fd605a5 | 1993 | builtin_optab = atan2_optab; break; |
73a954a1 | 1994 | CASE_FLT_FN (BUILT_IN_SCALB): |
1995 | if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2) | |
1996 | return 0; | |
1997 | builtin_optab = scalb_optab; break; | |
1998 | CASE_FLT_FN (BUILT_IN_SCALBN): | |
1999 | CASE_FLT_FN (BUILT_IN_SCALBLN): | |
2000 | if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2) | |
2001 | return 0; | |
2002 | /* Fall through... */ | |
4f35b1fc | 2003 | CASE_FLT_FN (BUILT_IN_LDEXP): |
4737caf2 | 2004 | builtin_optab = ldexp_optab; break; |
4f35b1fc | 2005 | CASE_FLT_FN (BUILT_IN_FMOD): |
80ed5c06 | 2006 | builtin_optab = fmod_optab; break; |
ef722005 | 2007 | CASE_FLT_FN (BUILT_IN_REMAINDER): |
4f35b1fc | 2008 | CASE_FLT_FN (BUILT_IN_DREM): |
ef722005 | 2009 | builtin_optab = remainder_optab; break; |
0fd605a5 | 2010 | default: |
64db345d | 2011 | gcc_unreachable (); |
0fd605a5 | 2012 | } |
2013 | ||
7f05340e | 2014 | /* Make a suitable register to place result in. */ |
2015 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
fc4eef90 | 2016 | |
2017 | /* Before working hard, check whether the instruction is available. */ | |
99bdde56 | 2018 | if (optab_handler (builtin_optab, mode)->insn_code == CODE_FOR_nothing) |
c2f47e15 | 2019 | return NULL_RTX; |
fc4eef90 | 2020 | |
7f05340e | 2021 | target = gen_reg_rtx (mode); |
2022 | ||
2023 | if (! flag_errno_math || ! HONOR_NANS (mode)) | |
2024 | errno_set = false; | |
2025 | ||
4ee9c684 | 2026 | /* Always stabilize the argument list. */ |
abfea505 | 2027 | CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0); |
2028 | CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1); | |
7f05340e | 2029 | |
8ec3c5c2 | 2030 | op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL); |
2031 | op1 = expand_normal (arg1); | |
7f05340e | 2032 | |
7f05340e | 2033 | start_sequence (); |
2034 | ||
0fd605a5 | 2035 | /* Compute into TARGET. |
2036 | Set TARGET to wherever the result comes back. */ | |
7f05340e | 2037 | target = expand_binop (mode, builtin_optab, op0, op1, |
0fd605a5 | 2038 | target, 0, OPTAB_DIRECT); |
53800dbe | 2039 | |
68e6cb9d | 2040 | /* If we were unable to expand via the builtin, stop the sequence |
2041 | (without outputting the insns) and call to the library function | |
2042 | with the stabilized argument list. */ | |
0fd605a5 | 2043 | if (target == 0) |
2044 | { | |
2045 | end_sequence (); | |
68e6cb9d | 2046 | return expand_call (exp, target, target == const0_rtx); |
53800dbe | 2047 | } |
2048 | ||
a4356fb9 | 2049 | if (errno_set) |
2050 | expand_errno_check (exp, target); | |
0fd605a5 | 2051 | |
53800dbe | 2052 | /* Output the entire sequence. */ |
2053 | insns = get_insns (); | |
2054 | end_sequence (); | |
31d3e01c | 2055 | emit_insn (insns); |
bf8e3599 | 2056 | |
53800dbe | 2057 | return target; |
2058 | } | |
2059 | ||
6b43bae4 | 2060 | /* Expand a call to the builtin sin and cos math functions. |
c2f47e15 | 2061 | Return NULL_RTX if a normal call should be emitted rather than expanding the |
6b43bae4 | 2062 | function in-line. EXP is the expression that is a call to the builtin |
2063 | function; if convenient, the result should be placed in TARGET. | |
2064 | SUBTARGET may be used as the target for computing one of EXP's | |
2065 | operands. */ | |
2066 | ||
2067 | static rtx | |
2068 | expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget) | |
2069 | { | |
2070 | optab builtin_optab; | |
7511b819 | 2071 | rtx op0, insns; |
6b43bae4 | 2072 | tree fndecl = get_callee_fndecl (exp); |
6b43bae4 | 2073 | enum machine_mode mode; |
abfea505 | 2074 | tree arg; |
6b43bae4 | 2075 | |
c2f47e15 | 2076 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
2077 | return NULL_RTX; | |
6b43bae4 | 2078 | |
c2f47e15 | 2079 | arg = CALL_EXPR_ARG (exp, 0); |
6b43bae4 | 2080 | |
2081 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2082 | { | |
4f35b1fc | 2083 | CASE_FLT_FN (BUILT_IN_SIN): |
2084 | CASE_FLT_FN (BUILT_IN_COS): | |
6b43bae4 | 2085 | builtin_optab = sincos_optab; break; |
2086 | default: | |
64db345d | 2087 | gcc_unreachable (); |
6b43bae4 | 2088 | } |
2089 | ||
2090 | /* Make a suitable register to place result in. */ | |
2091 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
2092 | ||
6b43bae4 | 2093 | /* Check if sincos insn is available, otherwise fallback |
0bed3869 | 2094 | to sin or cos insn. */ |
99bdde56 | 2095 | if (optab_handler (builtin_optab, mode)->insn_code == CODE_FOR_nothing) |
6b43bae4 | 2096 | switch (DECL_FUNCTION_CODE (fndecl)) |
2097 | { | |
4f35b1fc | 2098 | CASE_FLT_FN (BUILT_IN_SIN): |
6b43bae4 | 2099 | builtin_optab = sin_optab; break; |
4f35b1fc | 2100 | CASE_FLT_FN (BUILT_IN_COS): |
6b43bae4 | 2101 | builtin_optab = cos_optab; break; |
2102 | default: | |
64db345d | 2103 | gcc_unreachable (); |
6b43bae4 | 2104 | } |
6b43bae4 | 2105 | |
2106 | /* Before working hard, check whether the instruction is available. */ | |
99bdde56 | 2107 | if (optab_handler (builtin_optab, mode)->insn_code != CODE_FOR_nothing) |
6b43bae4 | 2108 | { |
2109 | target = gen_reg_rtx (mode); | |
2110 | ||
2111 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may | |
2112 | need to expand the argument again. This way, we will not perform | |
2113 | side-effects more the once. */ | |
abfea505 | 2114 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); |
6b43bae4 | 2115 | |
1db6d067 | 2116 | op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL); |
6b43bae4 | 2117 | |
6b43bae4 | 2118 | start_sequence (); |
2119 | ||
2120 | /* Compute into TARGET. | |
2121 | Set TARGET to wherever the result comes back. */ | |
2122 | if (builtin_optab == sincos_optab) | |
2123 | { | |
64db345d | 2124 | int result; |
7d3f6cc7 | 2125 | |
6b43bae4 | 2126 | switch (DECL_FUNCTION_CODE (fndecl)) |
2127 | { | |
4f35b1fc | 2128 | CASE_FLT_FN (BUILT_IN_SIN): |
64db345d | 2129 | result = expand_twoval_unop (builtin_optab, op0, 0, target, 0); |
6b43bae4 | 2130 | break; |
4f35b1fc | 2131 | CASE_FLT_FN (BUILT_IN_COS): |
64db345d | 2132 | result = expand_twoval_unop (builtin_optab, op0, target, 0, 0); |
6b43bae4 | 2133 | break; |
2134 | default: | |
64db345d | 2135 | gcc_unreachable (); |
6b43bae4 | 2136 | } |
64db345d | 2137 | gcc_assert (result); |
6b43bae4 | 2138 | } |
2139 | else | |
2140 | { | |
2141 | target = expand_unop (mode, builtin_optab, op0, target, 0); | |
2142 | } | |
2143 | ||
2144 | if (target != 0) | |
2145 | { | |
6b43bae4 | 2146 | /* Output the entire sequence. */ |
2147 | insns = get_insns (); | |
2148 | end_sequence (); | |
2149 | emit_insn (insns); | |
2150 | return target; | |
2151 | } | |
2152 | ||
2153 | /* If we were unable to expand via the builtin, stop the sequence | |
2154 | (without outputting the insns) and call to the library function | |
2155 | with the stabilized argument list. */ | |
2156 | end_sequence (); | |
2157 | } | |
2158 | ||
6b43bae4 | 2159 | target = expand_call (exp, target, target == const0_rtx); |
2160 | ||
2161 | return target; | |
2162 | } | |
2163 | ||
a67a90e5 | 2164 | /* Expand a call to one of the builtin math functions that operate on |
2165 | floating point argument and output an integer result (ilogb, isinf, | |
2166 | isnan, etc). | |
2167 | Return 0 if a normal call should be emitted rather than expanding the | |
2168 | function in-line. EXP is the expression that is a call to the builtin | |
2169 | function; if convenient, the result should be placed in TARGET. | |
2170 | SUBTARGET may be used as the target for computing one of EXP's operands. */ | |
2171 | ||
2172 | static rtx | |
2173 | expand_builtin_interclass_mathfn (tree exp, rtx target, rtx subtarget) | |
2174 | { | |
cde061c1 | 2175 | optab builtin_optab = 0; |
2176 | enum insn_code icode = CODE_FOR_nothing; | |
a67a90e5 | 2177 | rtx op0; |
2178 | tree fndecl = get_callee_fndecl (exp); | |
a67a90e5 | 2179 | enum machine_mode mode; |
2180 | bool errno_set = false; | |
abfea505 | 2181 | tree arg; |
a67a90e5 | 2182 | |
c2f47e15 | 2183 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
2184 | return NULL_RTX; | |
a67a90e5 | 2185 | |
c2f47e15 | 2186 | arg = CALL_EXPR_ARG (exp, 0); |
a67a90e5 | 2187 | |
2188 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2189 | { | |
2190 | CASE_FLT_FN (BUILT_IN_ILOGB): | |
2191 | errno_set = true; builtin_optab = ilogb_optab; break; | |
69b779ea | 2192 | CASE_FLT_FN (BUILT_IN_ISINF): |
2193 | builtin_optab = isinf_optab; break; | |
8a1a9cb7 | 2194 | case BUILT_IN_ISNORMAL: |
cde061c1 | 2195 | case BUILT_IN_ISFINITE: |
2196 | CASE_FLT_FN (BUILT_IN_FINITE): | |
2197 | /* These builtins have no optabs (yet). */ | |
2198 | break; | |
a67a90e5 | 2199 | default: |
2200 | gcc_unreachable (); | |
2201 | } | |
2202 | ||
2203 | /* There's no easy way to detect the case we need to set EDOM. */ | |
2204 | if (flag_errno_math && errno_set) | |
2205 | return NULL_RTX; | |
2206 | ||
2207 | /* Optab mode depends on the mode of the input argument. */ | |
2208 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
2209 | ||
cde061c1 | 2210 | if (builtin_optab) |
99bdde56 | 2211 | icode = optab_handler (builtin_optab, mode)->insn_code; |
a67a90e5 | 2212 | |
2213 | /* Before working hard, check whether the instruction is available. */ | |
2214 | if (icode != CODE_FOR_nothing) | |
2215 | { | |
2216 | /* Make a suitable register to place result in. */ | |
2217 | if (!target | |
2218 | || GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp))) | |
2219 | target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp))); | |
2220 | ||
2221 | gcc_assert (insn_data[icode].operand[0].predicate | |
2222 | (target, GET_MODE (target))); | |
2223 | ||
2224 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may | |
2225 | need to expand the argument again. This way, we will not perform | |
2226 | side-effects more the once. */ | |
abfea505 | 2227 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); |
a67a90e5 | 2228 | |
1db6d067 | 2229 | op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL); |
a67a90e5 | 2230 | |
2231 | if (mode != GET_MODE (op0)) | |
2232 | op0 = convert_to_mode (mode, op0, 0); | |
2233 | ||
2234 | /* Compute into TARGET. | |
2235 | Set TARGET to wherever the result comes back. */ | |
2236 | emit_unop_insn (icode, target, op0, UNKNOWN); | |
2237 | return target; | |
2238 | } | |
2239 | ||
06f7a99d | 2240 | /* If there is no optab, try generic code. */ |
2241 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2242 | { | |
2243 | tree result; | |
2244 | ||
2245 | CASE_FLT_FN (BUILT_IN_ISINF): | |
2246 | { | |
2247 | /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */ | |
2248 | tree const isgr_fn = built_in_decls[BUILT_IN_ISGREATER]; | |
2249 | tree const type = TREE_TYPE (arg); | |
2250 | REAL_VALUE_TYPE r; | |
2251 | char buf[128]; | |
2252 | ||
2253 | get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf)); | |
2254 | real_from_string (&r, buf); | |
2255 | result = build_call_expr (isgr_fn, 2, | |
2256 | fold_build1 (ABS_EXPR, type, arg), | |
2257 | build_real (type, r)); | |
2258 | return expand_expr (result, target, VOIDmode, EXPAND_NORMAL); | |
2259 | } | |
cde061c1 | 2260 | CASE_FLT_FN (BUILT_IN_FINITE): |
2261 | case BUILT_IN_ISFINITE: | |
2262 | { | |
2263 | /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */ | |
2264 | tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL]; | |
2265 | tree const type = TREE_TYPE (arg); | |
2266 | REAL_VALUE_TYPE r; | |
2267 | char buf[128]; | |
2268 | ||
2269 | get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf)); | |
2270 | real_from_string (&r, buf); | |
2271 | result = build_call_expr (isle_fn, 2, | |
2272 | fold_build1 (ABS_EXPR, type, arg), | |
2273 | build_real (type, r)); | |
2274 | return expand_expr (result, target, VOIDmode, EXPAND_NORMAL); | |
2275 | } | |
8a1a9cb7 | 2276 | case BUILT_IN_ISNORMAL: |
2277 | { | |
2278 | /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) & | |
2279 | islessequal(fabs(x),DBL_MAX). */ | |
2280 | tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL]; | |
2281 | tree const isge_fn = built_in_decls[BUILT_IN_ISGREATEREQUAL]; | |
2282 | tree const type = TREE_TYPE (arg); | |
2283 | REAL_VALUE_TYPE rmax, rmin; | |
2284 | char buf[128]; | |
2285 | ||
2286 | get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf)); | |
2287 | real_from_string (&rmax, buf); | |
2288 | sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1); | |
2289 | real_from_string (&rmin, buf); | |
2290 | arg = builtin_save_expr (fold_build1 (ABS_EXPR, type, arg)); | |
2291 | result = build_call_expr (isle_fn, 2, arg, | |
2292 | build_real (type, rmax)); | |
2293 | result = fold_build2 (BIT_AND_EXPR, integer_type_node, result, | |
2294 | build_call_expr (isge_fn, 2, arg, | |
2295 | build_real (type, rmin))); | |
2296 | return expand_expr (result, target, VOIDmode, EXPAND_NORMAL); | |
2297 | } | |
06f7a99d | 2298 | default: |
2299 | break; | |
2300 | } | |
2301 | ||
a67a90e5 | 2302 | target = expand_call (exp, target, target == const0_rtx); |
2303 | ||
2304 | return target; | |
2305 | } | |
2306 | ||
c3147c1a | 2307 | /* Expand a call to the builtin sincos math function. |
c2f47e15 | 2308 | Return NULL_RTX if a normal call should be emitted rather than expanding the |
c3147c1a | 2309 | function in-line. EXP is the expression that is a call to the builtin |
2310 | function. */ | |
2311 | ||
2312 | static rtx | |
2313 | expand_builtin_sincos (tree exp) | |
2314 | { | |
2315 | rtx op0, op1, op2, target1, target2; | |
c3147c1a | 2316 | enum machine_mode mode; |
2317 | tree arg, sinp, cosp; | |
2318 | int result; | |
2319 | ||
c2f47e15 | 2320 | if (!validate_arglist (exp, REAL_TYPE, |
2321 | POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
2322 | return NULL_RTX; | |
c3147c1a | 2323 | |
c2f47e15 | 2324 | arg = CALL_EXPR_ARG (exp, 0); |
2325 | sinp = CALL_EXPR_ARG (exp, 1); | |
2326 | cosp = CALL_EXPR_ARG (exp, 2); | |
c3147c1a | 2327 | |
2328 | /* Make a suitable register to place result in. */ | |
2329 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
2330 | ||
2331 | /* Check if sincos insn is available, otherwise emit the call. */ | |
99bdde56 | 2332 | if (optab_handler (sincos_optab, mode)->insn_code == CODE_FOR_nothing) |
c3147c1a | 2333 | return NULL_RTX; |
2334 | ||
2335 | target1 = gen_reg_rtx (mode); | |
2336 | target2 = gen_reg_rtx (mode); | |
2337 | ||
8ec3c5c2 | 2338 | op0 = expand_normal (arg); |
2339 | op1 = expand_normal (build_fold_indirect_ref (sinp)); | |
2340 | op2 = expand_normal (build_fold_indirect_ref (cosp)); | |
c3147c1a | 2341 | |
2342 | /* Compute into target1 and target2. | |
2343 | Set TARGET to wherever the result comes back. */ | |
2344 | result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0); | |
2345 | gcc_assert (result); | |
2346 | ||
2347 | /* Move target1 and target2 to the memory locations indicated | |
2348 | by op1 and op2. */ | |
2349 | emit_move_insn (op1, target1); | |
2350 | emit_move_insn (op2, target2); | |
2351 | ||
2352 | return const0_rtx; | |
2353 | } | |
2354 | ||
d735c391 | 2355 | /* Expand a call to the internal cexpi builtin to the sincos math function. |
2356 | EXP is the expression that is a call to the builtin function; if convenient, | |
2357 | the result should be placed in TARGET. SUBTARGET may be used as the target | |
2358 | for computing one of EXP's operands. */ | |
2359 | ||
2360 | static rtx | |
2361 | expand_builtin_cexpi (tree exp, rtx target, rtx subtarget) | |
2362 | { | |
2363 | tree fndecl = get_callee_fndecl (exp); | |
d735c391 | 2364 | tree arg, type; |
c2f47e15 | 2365 | enum machine_mode mode; |
d735c391 | 2366 | rtx op0, op1, op2; |
2367 | ||
c2f47e15 | 2368 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
2369 | return NULL_RTX; | |
d735c391 | 2370 | |
c2f47e15 | 2371 | arg = CALL_EXPR_ARG (exp, 0); |
d735c391 | 2372 | type = TREE_TYPE (arg); |
2373 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
2374 | ||
2375 | /* Try expanding via a sincos optab, fall back to emitting a libcall | |
18b8d8ae | 2376 | to sincos or cexp. We are sure we have sincos or cexp because cexpi |
2377 | is only generated from sincos, cexp or if we have either of them. */ | |
99bdde56 | 2378 | if (optab_handler (sincos_optab, mode)->insn_code != CODE_FOR_nothing) |
d735c391 | 2379 | { |
2380 | op1 = gen_reg_rtx (mode); | |
2381 | op2 = gen_reg_rtx (mode); | |
2382 | ||
1db6d067 | 2383 | op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL); |
d735c391 | 2384 | |
2385 | /* Compute into op1 and op2. */ | |
2386 | expand_twoval_unop (sincos_optab, op0, op2, op1, 0); | |
2387 | } | |
18b8d8ae | 2388 | else if (TARGET_HAS_SINCOS) |
d735c391 | 2389 | { |
c2f47e15 | 2390 | tree call, fn = NULL_TREE; |
d735c391 | 2391 | tree top1, top2; |
2392 | rtx op1a, op2a; | |
2393 | ||
2394 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF) | |
2395 | fn = built_in_decls[BUILT_IN_SINCOSF]; | |
2396 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI) | |
2397 | fn = built_in_decls[BUILT_IN_SINCOS]; | |
2398 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL) | |
2399 | fn = built_in_decls[BUILT_IN_SINCOSL]; | |
c2f47e15 | 2400 | else |
2401 | gcc_unreachable (); | |
d735c391 | 2402 | |
2403 | op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1); | |
2404 | op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1); | |
2405 | op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0)); | |
2406 | op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0)); | |
2407 | top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a); | |
2408 | top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a); | |
2409 | ||
d735c391 | 2410 | /* Make sure not to fold the sincos call again. */ |
2411 | call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn); | |
c2f47e15 | 2412 | expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)), |
2413 | call, 3, arg, top1, top2)); | |
d735c391 | 2414 | } |
18b8d8ae | 2415 | else |
2416 | { | |
0ecbc158 | 2417 | tree call, fn = NULL_TREE, narg; |
18b8d8ae | 2418 | tree ctype = build_complex_type (type); |
2419 | ||
0ecbc158 | 2420 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF) |
2421 | fn = built_in_decls[BUILT_IN_CEXPF]; | |
2422 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI) | |
2423 | fn = built_in_decls[BUILT_IN_CEXP]; | |
2424 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL) | |
2425 | fn = built_in_decls[BUILT_IN_CEXPL]; | |
c2f47e15 | 2426 | else |
2427 | gcc_unreachable (); | |
fc0dfa6e | 2428 | |
2429 | /* If we don't have a decl for cexp create one. This is the | |
2430 | friendliest fallback if the user calls __builtin_cexpi | |
2431 | without full target C99 function support. */ | |
2432 | if (fn == NULL_TREE) | |
2433 | { | |
2434 | tree fntype; | |
2435 | const char *name = NULL; | |
2436 | ||
2437 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF) | |
2438 | name = "cexpf"; | |
2439 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI) | |
2440 | name = "cexp"; | |
2441 | else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL) | |
2442 | name = "cexpl"; | |
2443 | ||
2444 | fntype = build_function_type_list (ctype, ctype, NULL_TREE); | |
2445 | fn = build_fn_decl (name, fntype); | |
2446 | } | |
2447 | ||
18b8d8ae | 2448 | narg = fold_build2 (COMPLEX_EXPR, ctype, |
2449 | build_real (type, dconst0), arg); | |
2450 | ||
2451 | /* Make sure not to fold the cexp call again. */ | |
2452 | call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn); | |
3737e769 | 2453 | return expand_expr (build_call_nary (ctype, call, 1, narg), |
1db6d067 | 2454 | target, VOIDmode, EXPAND_NORMAL); |
18b8d8ae | 2455 | } |
d735c391 | 2456 | |
2457 | /* Now build the proper return type. */ | |
2458 | return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type), | |
2459 | make_tree (TREE_TYPE (arg), op2), | |
2460 | make_tree (TREE_TYPE (arg), op1)), | |
1db6d067 | 2461 | target, VOIDmode, EXPAND_NORMAL); |
d735c391 | 2462 | } |
2463 | ||
7d3afc77 | 2464 | /* Expand a call to one of the builtin rounding functions gcc defines |
2465 | as an extension (lfloor and lceil). As these are gcc extensions we | |
2466 | do not need to worry about setting errno to EDOM. | |
ad52b9b7 | 2467 | If expanding via optab fails, lower expression to (int)(floor(x)). |
2468 | EXP is the expression that is a call to the builtin function; | |
2469 | if convenient, the result should be placed in TARGET. SUBTARGET may | |
2470 | be used as the target for computing one of EXP's operands. */ | |
2471 | ||
2472 | static rtx | |
2473 | expand_builtin_int_roundingfn (tree exp, rtx target, rtx subtarget) | |
2474 | { | |
9c42dd28 | 2475 | convert_optab builtin_optab; |
ad52b9b7 | 2476 | rtx op0, insns, tmp; |
2477 | tree fndecl = get_callee_fndecl (exp); | |
ad52b9b7 | 2478 | enum built_in_function fallback_fn; |
2479 | tree fallback_fndecl; | |
2480 | enum machine_mode mode; | |
4de0924f | 2481 | tree arg; |
ad52b9b7 | 2482 | |
c2f47e15 | 2483 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
ad52b9b7 | 2484 | gcc_unreachable (); |
2485 | ||
c2f47e15 | 2486 | arg = CALL_EXPR_ARG (exp, 0); |
ad52b9b7 | 2487 | |
2488 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2489 | { | |
4f35b1fc | 2490 | CASE_FLT_FN (BUILT_IN_LCEIL): |
2491 | CASE_FLT_FN (BUILT_IN_LLCEIL): | |
ac148751 | 2492 | builtin_optab = lceil_optab; |
2493 | fallback_fn = BUILT_IN_CEIL; | |
2494 | break; | |
2495 | ||
4f35b1fc | 2496 | CASE_FLT_FN (BUILT_IN_LFLOOR): |
2497 | CASE_FLT_FN (BUILT_IN_LLFLOOR): | |
ad52b9b7 | 2498 | builtin_optab = lfloor_optab; |
2499 | fallback_fn = BUILT_IN_FLOOR; | |
2500 | break; | |
2501 | ||
2502 | default: | |
2503 | gcc_unreachable (); | |
2504 | } | |
2505 | ||
2506 | /* Make a suitable register to place result in. */ | |
2507 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
2508 | ||
9c42dd28 | 2509 | target = gen_reg_rtx (mode); |
ad52b9b7 | 2510 | |
9c42dd28 | 2511 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may |
2512 | need to expand the argument again. This way, we will not perform | |
2513 | side-effects more the once. */ | |
abfea505 | 2514 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); |
ad52b9b7 | 2515 | |
1db6d067 | 2516 | op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL); |
ad52b9b7 | 2517 | |
9c42dd28 | 2518 | start_sequence (); |
ad52b9b7 | 2519 | |
9c42dd28 | 2520 | /* Compute into TARGET. */ |
2521 | if (expand_sfix_optab (target, op0, builtin_optab)) | |
2522 | { | |
2523 | /* Output the entire sequence. */ | |
2524 | insns = get_insns (); | |
ad52b9b7 | 2525 | end_sequence (); |
9c42dd28 | 2526 | emit_insn (insns); |
2527 | return target; | |
ad52b9b7 | 2528 | } |
2529 | ||
9c42dd28 | 2530 | /* If we were unable to expand via the builtin, stop the sequence |
2531 | (without outputting the insns). */ | |
2532 | end_sequence (); | |
2533 | ||
ad52b9b7 | 2534 | /* Fall back to floating point rounding optab. */ |
2535 | fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn); | |
fc0dfa6e | 2536 | |
2537 | /* For non-C99 targets we may end up without a fallback fndecl here | |
2538 | if the user called __builtin_lfloor directly. In this case emit | |
2539 | a call to the floor/ceil variants nevertheless. This should result | |
2540 | in the best user experience for not full C99 targets. */ | |
2541 | if (fallback_fndecl == NULL_TREE) | |
2542 | { | |
2543 | tree fntype; | |
2544 | const char *name = NULL; | |
2545 | ||
2546 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2547 | { | |
2548 | case BUILT_IN_LCEIL: | |
2549 | case BUILT_IN_LLCEIL: | |
2550 | name = "ceil"; | |
2551 | break; | |
2552 | case BUILT_IN_LCEILF: | |
2553 | case BUILT_IN_LLCEILF: | |
2554 | name = "ceilf"; | |
2555 | break; | |
2556 | case BUILT_IN_LCEILL: | |
2557 | case BUILT_IN_LLCEILL: | |
2558 | name = "ceill"; | |
2559 | break; | |
2560 | case BUILT_IN_LFLOOR: | |
2561 | case BUILT_IN_LLFLOOR: | |
2562 | name = "floor"; | |
2563 | break; | |
2564 | case BUILT_IN_LFLOORF: | |
2565 | case BUILT_IN_LLFLOORF: | |
2566 | name = "floorf"; | |
2567 | break; | |
2568 | case BUILT_IN_LFLOORL: | |
2569 | case BUILT_IN_LLFLOORL: | |
2570 | name = "floorl"; | |
2571 | break; | |
2572 | default: | |
2573 | gcc_unreachable (); | |
2574 | } | |
2575 | ||
2576 | fntype = build_function_type_list (TREE_TYPE (arg), | |
2577 | TREE_TYPE (arg), NULL_TREE); | |
2578 | fallback_fndecl = build_fn_decl (name, fntype); | |
2579 | } | |
2580 | ||
c2f47e15 | 2581 | exp = build_call_expr (fallback_fndecl, 1, arg); |
ad52b9b7 | 2582 | |
d4c690af | 2583 | tmp = expand_normal (exp); |
ad52b9b7 | 2584 | |
2585 | /* Truncate the result of floating point optab to integer | |
2586 | via expand_fix (). */ | |
2587 | target = gen_reg_rtx (mode); | |
2588 | expand_fix (target, tmp, 0); | |
2589 | ||
2590 | return target; | |
2591 | } | |
2592 | ||
7d3afc77 | 2593 | /* Expand a call to one of the builtin math functions doing integer |
2594 | conversion (lrint). | |
2595 | Return 0 if a normal call should be emitted rather than expanding the | |
2596 | function in-line. EXP is the expression that is a call to the builtin | |
2597 | function; if convenient, the result should be placed in TARGET. | |
2598 | SUBTARGET may be used as the target for computing one of EXP's operands. */ | |
2599 | ||
2600 | static rtx | |
2601 | expand_builtin_int_roundingfn_2 (tree exp, rtx target, rtx subtarget) | |
2602 | { | |
5f51ee59 | 2603 | convert_optab builtin_optab; |
7d3afc77 | 2604 | rtx op0, insns; |
2605 | tree fndecl = get_callee_fndecl (exp); | |
4de0924f | 2606 | tree arg; |
c2f47e15 | 2607 | enum machine_mode mode; |
7d3afc77 | 2608 | |
2609 | /* There's no easy way to detect the case we need to set EDOM. */ | |
2610 | if (flag_errno_math) | |
2611 | return NULL_RTX; | |
2612 | ||
c2f47e15 | 2613 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
2614 | gcc_unreachable (); | |
2615 | ||
2616 | arg = CALL_EXPR_ARG (exp, 0); | |
7d3afc77 | 2617 | |
2618 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2619 | { | |
2620 | CASE_FLT_FN (BUILT_IN_LRINT): | |
2621 | CASE_FLT_FN (BUILT_IN_LLRINT): | |
2622 | builtin_optab = lrint_optab; break; | |
ef2f1a10 | 2623 | CASE_FLT_FN (BUILT_IN_LROUND): |
2624 | CASE_FLT_FN (BUILT_IN_LLROUND): | |
2625 | builtin_optab = lround_optab; break; | |
7d3afc77 | 2626 | default: |
2627 | gcc_unreachable (); | |
2628 | } | |
2629 | ||
2630 | /* Make a suitable register to place result in. */ | |
2631 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
2632 | ||
5f51ee59 | 2633 | target = gen_reg_rtx (mode); |
7d3afc77 | 2634 | |
5f51ee59 | 2635 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may |
2636 | need to expand the argument again. This way, we will not perform | |
2637 | side-effects more the once. */ | |
abfea505 | 2638 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); |
7d3afc77 | 2639 | |
1db6d067 | 2640 | op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL); |
7d3afc77 | 2641 | |
5f51ee59 | 2642 | start_sequence (); |
7d3afc77 | 2643 | |
5f51ee59 | 2644 | if (expand_sfix_optab (target, op0, builtin_optab)) |
2645 | { | |
2646 | /* Output the entire sequence. */ | |
2647 | insns = get_insns (); | |
7d3afc77 | 2648 | end_sequence (); |
5f51ee59 | 2649 | emit_insn (insns); |
2650 | return target; | |
7d3afc77 | 2651 | } |
2652 | ||
5f51ee59 | 2653 | /* If we were unable to expand via the builtin, stop the sequence |
2654 | (without outputting the insns) and call to the library function | |
2655 | with the stabilized argument list. */ | |
2656 | end_sequence (); | |
2657 | ||
7d3afc77 | 2658 | target = expand_call (exp, target, target == const0_rtx); |
2659 | ||
2660 | return target; | |
2661 | } | |
2662 | ||
f1b844c6 | 2663 | /* To evaluate powi(x,n), the floating point value x raised to the |
2664 | constant integer exponent n, we use a hybrid algorithm that | |
2665 | combines the "window method" with look-up tables. For an | |
2666 | introduction to exponentiation algorithms and "addition chains", | |
2667 | see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth, | |
2668 | "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming", | |
2669 | 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation | |
2670 | Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */ | |
2671 | ||
2672 | /* Provide a default value for POWI_MAX_MULTS, the maximum number of | |
2673 | multiplications to inline before calling the system library's pow | |
2674 | function. powi(x,n) requires at worst 2*bits(n)-2 multiplications, | |
2675 | so this default never requires calling pow, powf or powl. */ | |
0862b7e9 | 2676 | |
f1b844c6 | 2677 | #ifndef POWI_MAX_MULTS |
2678 | #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2) | |
2679 | #endif | |
2680 | ||
2681 | /* The size of the "optimal power tree" lookup table. All | |
2682 | exponents less than this value are simply looked up in the | |
2683 | powi_table below. This threshold is also used to size the | |
2684 | cache of pseudo registers that hold intermediate results. */ | |
2685 | #define POWI_TABLE_SIZE 256 | |
2686 | ||
2687 | /* The size, in bits of the window, used in the "window method" | |
2688 | exponentiation algorithm. This is equivalent to a radix of | |
2689 | (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */ | |
2690 | #define POWI_WINDOW_SIZE 3 | |
2691 | ||
2692 | /* The following table is an efficient representation of an | |
2693 | "optimal power tree". For each value, i, the corresponding | |
2694 | value, j, in the table states than an optimal evaluation | |
2695 | sequence for calculating pow(x,i) can be found by evaluating | |
2696 | pow(x,j)*pow(x,i-j). An optimal power tree for the first | |
2697 | 100 integers is given in Knuth's "Seminumerical algorithms". */ | |
2698 | ||
2699 | static const unsigned char powi_table[POWI_TABLE_SIZE] = | |
2700 | { | |
2701 | 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */ | |
2702 | 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */ | |
2703 | 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */ | |
2704 | 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */ | |
2705 | 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */ | |
2706 | 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */ | |
2707 | 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */ | |
2708 | 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */ | |
2709 | 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */ | |
2710 | 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */ | |
2711 | 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */ | |
2712 | 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */ | |
2713 | 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */ | |
2714 | 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */ | |
2715 | 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */ | |
2716 | 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */ | |
2717 | 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */ | |
2718 | 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */ | |
2719 | 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */ | |
2720 | 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */ | |
2721 | 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */ | |
2722 | 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */ | |
2723 | 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */ | |
2724 | 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */ | |
2725 | 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */ | |
2726 | 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */ | |
2727 | 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */ | |
2728 | 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */ | |
2729 | 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */ | |
2730 | 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */ | |
2731 | 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */ | |
2732 | 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */ | |
2733 | }; | |
2734 | ||
2735 | ||
2736 | /* Return the number of multiplications required to calculate | |
2737 | powi(x,n) where n is less than POWI_TABLE_SIZE. This is a | |
2738 | subroutine of powi_cost. CACHE is an array indicating | |
2739 | which exponents have already been calculated. */ | |
2740 | ||
2741 | static int | |
2742 | powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache) | |
2743 | { | |
2744 | /* If we've already calculated this exponent, then this evaluation | |
2745 | doesn't require any additional multiplications. */ | |
2746 | if (cache[n]) | |
2747 | return 0; | |
2748 | ||
2749 | cache[n] = true; | |
2750 | return powi_lookup_cost (n - powi_table[n], cache) | |
2751 | + powi_lookup_cost (powi_table[n], cache) + 1; | |
2752 | } | |
2753 | ||
2754 | /* Return the number of multiplications required to calculate | |
2755 | powi(x,n) for an arbitrary x, given the exponent N. This | |
2756 | function needs to be kept in sync with expand_powi below. */ | |
2757 | ||
2758 | static int | |
2759 | powi_cost (HOST_WIDE_INT n) | |
2760 | { | |
2761 | bool cache[POWI_TABLE_SIZE]; | |
2762 | unsigned HOST_WIDE_INT digit; | |
2763 | unsigned HOST_WIDE_INT val; | |
2764 | int result; | |
2765 | ||
2766 | if (n == 0) | |
2767 | return 0; | |
2768 | ||
2769 | /* Ignore the reciprocal when calculating the cost. */ | |
2770 | val = (n < 0) ? -n : n; | |
2771 | ||
2772 | /* Initialize the exponent cache. */ | |
2773 | memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool)); | |
2774 | cache[1] = true; | |
2775 | ||
2776 | result = 0; | |
2777 | ||
2778 | while (val >= POWI_TABLE_SIZE) | |
2779 | { | |
2780 | if (val & 1) | |
2781 | { | |
2782 | digit = val & ((1 << POWI_WINDOW_SIZE) - 1); | |
2783 | result += powi_lookup_cost (digit, cache) | |
2784 | + POWI_WINDOW_SIZE + 1; | |
2785 | val >>= POWI_WINDOW_SIZE; | |
2786 | } | |
2787 | else | |
2788 | { | |
2789 | val >>= 1; | |
2790 | result++; | |
2791 | } | |
2792 | } | |
0862b7e9 | 2793 | |
6881dbab | 2794 | return result + powi_lookup_cost (val, cache); |
f1b844c6 | 2795 | } |
2796 | ||
2797 | /* Recursive subroutine of expand_powi. This function takes the array, | |
2798 | CACHE, of already calculated exponents and an exponent N and returns | |
2799 | an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */ | |
2800 | ||
2801 | static rtx | |
2802 | expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache) | |
2803 | { | |
2804 | unsigned HOST_WIDE_INT digit; | |
2805 | rtx target, result; | |
2806 | rtx op0, op1; | |
2807 | ||
2808 | if (n < POWI_TABLE_SIZE) | |
2809 | { | |
2810 | if (cache[n]) | |
a0c938f0 | 2811 | return cache[n]; |
f1b844c6 | 2812 | |
2813 | target = gen_reg_rtx (mode); | |
2814 | cache[n] = target; | |
2815 | ||
2816 | op0 = expand_powi_1 (mode, n - powi_table[n], cache); | |
2817 | op1 = expand_powi_1 (mode, powi_table[n], cache); | |
2818 | } | |
2819 | else if (n & 1) | |
2820 | { | |
2821 | target = gen_reg_rtx (mode); | |
2822 | digit = n & ((1 << POWI_WINDOW_SIZE) - 1); | |
2823 | op0 = expand_powi_1 (mode, n - digit, cache); | |
2824 | op1 = expand_powi_1 (mode, digit, cache); | |
2825 | } | |
2826 | else | |
2827 | { | |
2828 | target = gen_reg_rtx (mode); | |
2829 | op0 = expand_powi_1 (mode, n >> 1, cache); | |
2830 | op1 = op0; | |
2831 | } | |
2832 | ||
2833 | result = expand_mult (mode, op0, op1, target, 0); | |
2834 | if (result != target) | |
2835 | emit_move_insn (target, result); | |
2836 | return target; | |
2837 | } | |
2838 | ||
2839 | /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the | |
2840 | floating point operand in mode MODE, and N is the exponent. This | |
2841 | function needs to be kept in sync with powi_cost above. */ | |
0862b7e9 | 2842 | |
f1b844c6 | 2843 | static rtx |
2844 | expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n) | |
2845 | { | |
2846 | unsigned HOST_WIDE_INT val; | |
2847 | rtx cache[POWI_TABLE_SIZE]; | |
2848 | rtx result; | |
2849 | ||
2850 | if (n == 0) | |
2851 | return CONST1_RTX (mode); | |
2852 | ||
2853 | val = (n < 0) ? -n : n; | |
2854 | ||
19bf118a | 2855 | memset (cache, 0, sizeof (cache)); |
f1b844c6 | 2856 | cache[1] = x; |
2857 | ||
2858 | result = expand_powi_1 (mode, (n < 0) ? -n : n, cache); | |
2859 | ||
2860 | /* If the original exponent was negative, reciprocate the result. */ | |
2861 | if (n < 0) | |
2862 | result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode), | |
2863 | result, NULL_RTX, 0, OPTAB_LIB_WIDEN); | |
2864 | ||
2865 | return result; | |
2866 | } | |
2867 | ||
c2f47e15 | 2868 | /* Expand a call to the pow built-in mathematical function. Return NULL_RTX if |
f1b844c6 | 2869 | a normal call should be emitted rather than expanding the function |
2870 | in-line. EXP is the expression that is a call to the builtin | |
2871 | function; if convenient, the result should be placed in TARGET. */ | |
2872 | ||
2873 | static rtx | |
2874 | expand_builtin_pow (tree exp, rtx target, rtx subtarget) | |
2875 | { | |
c2f47e15 | 2876 | tree arg0, arg1; |
2877 | tree fn, narg0; | |
1c4709b4 | 2878 | tree type = TREE_TYPE (exp); |
2879 | REAL_VALUE_TYPE cint, c, c2; | |
2880 | HOST_WIDE_INT n; | |
2881 | rtx op, op2; | |
2882 | enum machine_mode mode = TYPE_MODE (type); | |
f1b844c6 | 2883 | |
c2f47e15 | 2884 | if (! validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE)) |
2885 | return NULL_RTX; | |
f1b844c6 | 2886 | |
c2f47e15 | 2887 | arg0 = CALL_EXPR_ARG (exp, 0); |
2888 | arg1 = CALL_EXPR_ARG (exp, 1); | |
f1b844c6 | 2889 | |
1c4709b4 | 2890 | if (TREE_CODE (arg1) != REAL_CST |
f96bd2bf | 2891 | || TREE_OVERFLOW (arg1)) |
1c4709b4 | 2892 | return expand_builtin_mathfn_2 (exp, target, subtarget); |
2893 | ||
2894 | /* Handle constant exponents. */ | |
2895 | ||
2896 | /* For integer valued exponents we can expand to an optimal multiplication | |
2897 | sequence using expand_powi. */ | |
2898 | c = TREE_REAL_CST (arg1); | |
2899 | n = real_to_integer (&c); | |
2900 | real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0); | |
2901 | if (real_identical (&c, &cint) | |
2902 | && ((n >= -1 && n <= 2) | |
2903 | || (flag_unsafe_math_optimizations | |
2904 | && !optimize_size | |
2905 | && powi_cost (n) <= POWI_MAX_MULTS))) | |
f1b844c6 | 2906 | { |
1db6d067 | 2907 | op = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL); |
1c4709b4 | 2908 | if (n != 1) |
2909 | { | |
2910 | op = force_reg (mode, op); | |
2911 | op = expand_powi (op, mode, n); | |
2912 | } | |
2913 | return op; | |
2914 | } | |
2915 | ||
2916 | narg0 = builtin_save_expr (arg0); | |
f1b844c6 | 2917 | |
1c4709b4 | 2918 | /* If the exponent is not integer valued, check if it is half of an integer. |
2919 | In this case we can expand to sqrt (x) * x**(n/2). */ | |
2920 | fn = mathfn_built_in (type, BUILT_IN_SQRT); | |
2921 | if (fn != NULL_TREE) | |
2922 | { | |
2923 | real_arithmetic (&c2, MULT_EXPR, &c, &dconst2); | |
2924 | n = real_to_integer (&c2); | |
f1b844c6 | 2925 | real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0); |
1c4709b4 | 2926 | if (real_identical (&c2, &cint) |
2927 | && ((flag_unsafe_math_optimizations | |
2928 | && !optimize_size | |
2929 | && powi_cost (n/2) <= POWI_MAX_MULTS) | |
2930 | || n == 1)) | |
f1b844c6 | 2931 | { |
c2f47e15 | 2932 | tree call_expr = build_call_expr (fn, 1, narg0); |
8697cf4d | 2933 | /* Use expand_expr in case the newly built call expression |
2934 | was folded to a non-call. */ | |
2935 | op = expand_expr (call_expr, subtarget, mode, EXPAND_NORMAL); | |
1c4709b4 | 2936 | if (n != 1) |
8c5cac78 | 2937 | { |
1db6d067 | 2938 | op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL); |
1c4709b4 | 2939 | op2 = force_reg (mode, op2); |
2940 | op2 = expand_powi (op2, mode, abs (n / 2)); | |
2941 | op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX, | |
2942 | 0, OPTAB_LIB_WIDEN); | |
2943 | /* If the original exponent was negative, reciprocate the | |
2944 | result. */ | |
2945 | if (n < 0) | |
2946 | op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode), | |
2947 | op, NULL_RTX, 0, OPTAB_LIB_WIDEN); | |
8c5cac78 | 2948 | } |
1c4709b4 | 2949 | return op; |
f1b844c6 | 2950 | } |
2951 | } | |
b613c2a6 | 2952 | |
1c4709b4 | 2953 | /* Try if the exponent is a third of an integer. In this case |
5a84ba55 | 2954 | we can expand to x**(n/3) * cbrt(x)**(n%3). As cbrt (x) is |
2955 | different from pow (x, 1./3.) due to rounding and behavior | |
2956 | with negative x we need to constrain this transformation to | |
2957 | unsafe math and positive x or finite math. */ | |
1c4709b4 | 2958 | fn = mathfn_built_in (type, BUILT_IN_CBRT); |
5a84ba55 | 2959 | if (fn != NULL_TREE |
2960 | && flag_unsafe_math_optimizations | |
2961 | && (tree_expr_nonnegative_p (arg0) | |
2962 | || !HONOR_NANS (mode))) | |
1c4709b4 | 2963 | { |
3fa759a9 | 2964 | REAL_VALUE_TYPE dconst3; |
2965 | real_from_integer (&dconst3, VOIDmode, 3, 0, 0); | |
1c4709b4 | 2966 | real_arithmetic (&c2, MULT_EXPR, &c, &dconst3); |
2967 | real_round (&c2, mode, &c2); | |
2968 | n = real_to_integer (&c2); | |
2969 | real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0); | |
2970 | real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3); | |
2971 | real_convert (&c2, mode, &c2); | |
2972 | if (real_identical (&c2, &c) | |
2973 | && ((!optimize_size | |
1c4709b4 | 2974 | && powi_cost (n/3) <= POWI_MAX_MULTS) |
2975 | || n == 1)) | |
2976 | { | |
c2f47e15 | 2977 | tree call_expr = build_call_expr (fn, 1,narg0); |
1c4709b4 | 2978 | op = expand_builtin (call_expr, NULL_RTX, subtarget, mode, 0); |
2979 | if (abs (n) % 3 == 2) | |
2980 | op = expand_simple_binop (mode, MULT, op, op, op, | |
2981 | 0, OPTAB_LIB_WIDEN); | |
2982 | if (n != 1) | |
2983 | { | |
1db6d067 | 2984 | op2 = expand_expr (narg0, subtarget, VOIDmode, EXPAND_NORMAL); |
1c4709b4 | 2985 | op2 = force_reg (mode, op2); |
2986 | op2 = expand_powi (op2, mode, abs (n / 3)); | |
2987 | op = expand_simple_binop (mode, MULT, op, op2, NULL_RTX, | |
2988 | 0, OPTAB_LIB_WIDEN); | |
2989 | /* If the original exponent was negative, reciprocate the | |
2990 | result. */ | |
2991 | if (n < 0) | |
2992 | op = expand_binop (mode, sdiv_optab, CONST1_RTX (mode), | |
2993 | op, NULL_RTX, 0, OPTAB_LIB_WIDEN); | |
2994 | } | |
2995 | return op; | |
2996 | } | |
2997 | } | |
2998 | ||
2999 | /* Fall back to optab expansion. */ | |
b613c2a6 | 3000 | return expand_builtin_mathfn_2 (exp, target, subtarget); |
f1b844c6 | 3001 | } |
3002 | ||
c2f47e15 | 3003 | /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if |
757c219d | 3004 | a normal call should be emitted rather than expanding the function |
3005 | in-line. EXP is the expression that is a call to the builtin | |
3006 | function; if convenient, the result should be placed in TARGET. */ | |
3007 | ||
3008 | static rtx | |
3009 | expand_builtin_powi (tree exp, rtx target, rtx subtarget) | |
3010 | { | |
757c219d | 3011 | tree arg0, arg1; |
3012 | rtx op0, op1; | |
3013 | enum machine_mode mode; | |
d0405f40 | 3014 | enum machine_mode mode2; |
757c219d | 3015 | |
c2f47e15 | 3016 | if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE)) |
3017 | return NULL_RTX; | |
757c219d | 3018 | |
c2f47e15 | 3019 | arg0 = CALL_EXPR_ARG (exp, 0); |
3020 | arg1 = CALL_EXPR_ARG (exp, 1); | |
757c219d | 3021 | mode = TYPE_MODE (TREE_TYPE (exp)); |
3022 | ||
3023 | /* Handle constant power. */ | |
3024 | ||
3025 | if (TREE_CODE (arg1) == INTEGER_CST | |
f96bd2bf | 3026 | && !TREE_OVERFLOW (arg1)) |
757c219d | 3027 | { |
3028 | HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1); | |
3029 | ||
3030 | /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact. | |
3031 | Otherwise, check the number of multiplications required. */ | |
3032 | if ((TREE_INT_CST_HIGH (arg1) == 0 | |
3033 | || TREE_INT_CST_HIGH (arg1) == -1) | |
3034 | && ((n >= -1 && n <= 2) | |
3035 | || (! optimize_size | |
3036 | && powi_cost (n) <= POWI_MAX_MULTS))) | |
3037 | { | |
1db6d067 | 3038 | op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL); |
757c219d | 3039 | op0 = force_reg (mode, op0); |
3040 | return expand_powi (op0, mode, n); | |
3041 | } | |
3042 | } | |
3043 | ||
3044 | /* Emit a libcall to libgcc. */ | |
3045 | ||
c2f47e15 | 3046 | /* Mode of the 2nd argument must match that of an int. */ |
d0405f40 | 3047 | mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0); |
3048 | ||
757c219d | 3049 | if (target == NULL_RTX) |
3050 | target = gen_reg_rtx (mode); | |
3051 | ||
1db6d067 | 3052 | op0 = expand_expr (arg0, subtarget, mode, EXPAND_NORMAL); |
757c219d | 3053 | if (GET_MODE (op0) != mode) |
3054 | op0 = convert_to_mode (mode, op0, 0); | |
1db6d067 | 3055 | op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL); |
d0405f40 | 3056 | if (GET_MODE (op1) != mode2) |
3057 | op1 = convert_to_mode (mode2, op1, 0); | |
757c219d | 3058 | |
f36b9f69 | 3059 | target = emit_library_call_value (optab_libfunc (powi_optab, mode), |
2dd6f9ed | 3060 | target, LCT_CONST, mode, 2, |
d0405f40 | 3061 | op0, mode, op1, mode2); |
757c219d | 3062 | |
3063 | return target; | |
3064 | } | |
3065 | ||
c2f47e15 | 3066 | /* Expand expression EXP which is a call to the strlen builtin. Return |
3067 | NULL_RTX if we failed the caller should emit a normal call, otherwise | |
aed0bd19 | 3068 | try to get the result in TARGET, if convenient. */ |
f7c44134 | 3069 | |
53800dbe | 3070 | static rtx |
c2f47e15 | 3071 | expand_builtin_strlen (tree exp, rtx target, |
aecda0d6 | 3072 | enum machine_mode target_mode) |
53800dbe | 3073 | { |
c2f47e15 | 3074 | if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
3075 | return NULL_RTX; | |
53800dbe | 3076 | else |
3077 | { | |
911c0150 | 3078 | rtx pat; |
c2f47e15 | 3079 | tree len; |
3080 | tree src = CALL_EXPR_ARG (exp, 0); | |
911c0150 | 3081 | rtx result, src_reg, char_rtx, before_strlen; |
80cd7a5e | 3082 | enum machine_mode insn_mode = target_mode, char_mode; |
ef2c4a29 | 3083 | enum insn_code icode = CODE_FOR_nothing; |
6248e345 | 3084 | int align; |
3085 | ||
3086 | /* If the length can be computed at compile-time, return it. */ | |
681fab1e | 3087 | len = c_strlen (src, 0); |
6248e345 | 3088 | if (len) |
80cd7a5e | 3089 | return expand_expr (len, target, target_mode, EXPAND_NORMAL); |
6248e345 | 3090 | |
681fab1e | 3091 | /* If the length can be computed at compile-time and is constant |
3092 | integer, but there are side-effects in src, evaluate | |
3093 | src for side-effects, then return len. | |
3094 | E.g. x = strlen (i++ ? "xfoo" + 1 : "bar"); | |
3095 | can be optimized into: i++; x = 3; */ | |
3096 | len = c_strlen (src, 1); | |
3097 | if (len && TREE_CODE (len) == INTEGER_CST) | |
3098 | { | |
3099 | expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
3100 | return expand_expr (len, target, target_mode, EXPAND_NORMAL); | |
3101 | } | |
3102 | ||
6248e345 | 3103 | align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; |
53800dbe | 3104 | |
53800dbe | 3105 | /* If SRC is not a pointer type, don't do this operation inline. */ |
3106 | if (align == 0) | |
c2f47e15 | 3107 | return NULL_RTX; |
53800dbe | 3108 | |
911c0150 | 3109 | /* Bail out if we can't compute strlen in the right mode. */ |
53800dbe | 3110 | while (insn_mode != VOIDmode) |
3111 | { | |
99bdde56 | 3112 | icode = optab_handler (strlen_optab, insn_mode)->insn_code; |
53800dbe | 3113 | if (icode != CODE_FOR_nothing) |
c28ae87f | 3114 | break; |
53800dbe | 3115 | |
3116 | insn_mode = GET_MODE_WIDER_MODE (insn_mode); | |
3117 | } | |
3118 | if (insn_mode == VOIDmode) | |
c2f47e15 | 3119 | return NULL_RTX; |
53800dbe | 3120 | |
3121 | /* Make a place to write the result of the instruction. */ | |
3122 | result = target; | |
3123 | if (! (result != 0 | |
8ad4c111 | 3124 | && REG_P (result) |
53800dbe | 3125 | && GET_MODE (result) == insn_mode |
3126 | && REGNO (result) >= FIRST_PSEUDO_REGISTER)) | |
3127 | result = gen_reg_rtx (insn_mode); | |
3128 | ||
911c0150 | 3129 | /* Make a place to hold the source address. We will not expand |
3130 | the actual source until we are sure that the expansion will | |
3131 | not fail -- there are trees that cannot be expanded twice. */ | |
3132 | src_reg = gen_reg_rtx (Pmode); | |
53800dbe | 3133 | |
911c0150 | 3134 | /* Mark the beginning of the strlen sequence so we can emit the |
3135 | source operand later. */ | |
f0ce3b1f | 3136 | before_strlen = get_last_insn (); |
53800dbe | 3137 | |
53800dbe | 3138 | char_rtx = const0_rtx; |
f7c44134 | 3139 | char_mode = insn_data[(int) icode].operand[2].mode; |
3140 | if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx, | |
3141 | char_mode)) | |
53800dbe | 3142 | char_rtx = copy_to_mode_reg (char_mode, char_rtx); |
3143 | ||
911c0150 | 3144 | pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg), |
3145 | char_rtx, GEN_INT (align)); | |
3146 | if (! pat) | |
c2f47e15 | 3147 | return NULL_RTX; |
911c0150 | 3148 | emit_insn (pat); |
3149 | ||
3150 | /* Now that we are assured of success, expand the source. */ | |
3151 | start_sequence (); | |
c5aba89c | 3152 | pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL); |
911c0150 | 3153 | if (pat != src_reg) |
3154 | emit_move_insn (src_reg, pat); | |
31d3e01c | 3155 | pat = get_insns (); |
911c0150 | 3156 | end_sequence (); |
bceb0d1f | 3157 | |
3158 | if (before_strlen) | |
3159 | emit_insn_after (pat, before_strlen); | |
3160 | else | |
3161 | emit_insn_before (pat, get_insns ()); | |
53800dbe | 3162 | |
3163 | /* Return the value in the proper mode for this function. */ | |
80cd7a5e | 3164 | if (GET_MODE (result) == target_mode) |
911c0150 | 3165 | target = result; |
53800dbe | 3166 | else if (target != 0) |
911c0150 | 3167 | convert_move (target, result, 0); |
53800dbe | 3168 | else |
80cd7a5e | 3169 | target = convert_to_mode (target_mode, result, 0); |
911c0150 | 3170 | |
3171 | return target; | |
53800dbe | 3172 | } |
3173 | } | |
3174 | ||
c2f47e15 | 3175 | /* Expand a call to the strstr builtin. Return NULL_RTX if we failed the |
17f5ea87 | 3176 | caller should emit a normal call, otherwise try to get the result |
3177 | in TARGET, if convenient (and in mode MODE if that's convenient). */ | |
3178 | ||
3179 | static rtx | |
c2f47e15 | 3180 | expand_builtin_strstr (tree exp, rtx target, enum machine_mode mode) |
17f5ea87 | 3181 | { |
c2f47e15 | 3182 | if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
17f5ea87 | 3183 | { |
c2f47e15 | 3184 | tree type = TREE_TYPE (exp); |
3185 | tree result = fold_builtin_strstr (CALL_EXPR_ARG (exp, 0), | |
3186 | CALL_EXPR_ARG (exp, 1), type); | |
2c84ee37 | 3187 | if (result) |
3188 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
17f5ea87 | 3189 | } |
c2f47e15 | 3190 | return NULL_RTX; |
17f5ea87 | 3191 | } |
3192 | ||
c2f47e15 | 3193 | /* Expand a call to the strchr builtin. Return NULL_RTX if we failed the |
46f3a74a | 3194 | caller should emit a normal call, otherwise try to get the result |
3195 | in TARGET, if convenient (and in mode MODE if that's convenient). */ | |
3196 | ||
3197 | static rtx | |
c2f47e15 | 3198 | expand_builtin_strchr (tree exp, rtx target, enum machine_mode mode) |
46f3a74a | 3199 | { |
c2f47e15 | 3200 | if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
46f3a74a | 3201 | { |
c2f47e15 | 3202 | tree type = TREE_TYPE (exp); |
3203 | tree result = fold_builtin_strchr (CALL_EXPR_ARG (exp, 0), | |
3204 | CALL_EXPR_ARG (exp, 1), type); | |
2c84ee37 | 3205 | if (result) |
3206 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
83d79705 | 3207 | |
2c84ee37 | 3208 | /* FIXME: Should use strchrM optab so that ports can optimize this. */ |
83d79705 | 3209 | } |
c2f47e15 | 3210 | return NULL_RTX; |
83d79705 | 3211 | } |
3212 | ||
c2f47e15 | 3213 | /* Expand a call to the strrchr builtin. Return NULL_RTX if we failed the |
83d79705 | 3214 | caller should emit a normal call, otherwise try to get the result |
3215 | in TARGET, if convenient (and in mode MODE if that's convenient). */ | |
3216 | ||
3217 | static rtx | |
c2f47e15 | 3218 | expand_builtin_strrchr (tree exp, rtx target, enum machine_mode mode) |
83d79705 | 3219 | { |
c2f47e15 | 3220 | if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
83d79705 | 3221 | { |
c2f47e15 | 3222 | tree type = TREE_TYPE (exp); |
3223 | tree result = fold_builtin_strrchr (CALL_EXPR_ARG (exp, 0), | |
3224 | CALL_EXPR_ARG (exp, 1), type); | |
2c84ee37 | 3225 | if (result) |
3226 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
83d79705 | 3227 | } |
c2f47e15 | 3228 | return NULL_RTX; |
83d79705 | 3229 | } |
3230 | ||
c2f47e15 | 3231 | /* Expand a call to the strpbrk builtin. Return NULL_RTX if we failed the |
83d79705 | 3232 | caller should emit a normal call, otherwise try to get the result |
3233 | in TARGET, if convenient (and in mode MODE if that's convenient). */ | |
3234 | ||
3235 | static rtx | |
c2f47e15 | 3236 | expand_builtin_strpbrk (tree exp, rtx target, enum machine_mode mode) |
83d79705 | 3237 | { |
c2f47e15 | 3238 | if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
83d79705 | 3239 | { |
c2f47e15 | 3240 | tree type = TREE_TYPE (exp); |
3241 | tree result = fold_builtin_strpbrk (CALL_EXPR_ARG (exp, 0), | |
3242 | CALL_EXPR_ARG (exp, 1), type); | |
2c84ee37 | 3243 | if (result) |
3244 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
46f3a74a | 3245 | } |
c2f47e15 | 3246 | return NULL_RTX; |
46f3a74a | 3247 | } |
3248 | ||
6840589f | 3249 | /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE) |
3250 | bytes from constant string DATA + OFFSET and return it as target | |
3251 | constant. */ | |
3252 | ||
3253 | static rtx | |
aecda0d6 | 3254 | builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset, |
3255 | enum machine_mode mode) | |
6840589f | 3256 | { |
3257 | const char *str = (const char *) data; | |
3258 | ||
64db345d | 3259 | gcc_assert (offset >= 0 |
3260 | && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode) | |
3261 | <= strlen (str) + 1)); | |
6840589f | 3262 | |
3263 | return c_readstr (str + offset, mode); | |
3264 | } | |
3265 | ||
c2f47e15 | 3266 | /* Expand a call EXP to the memcpy builtin. |
3267 | Return NULL_RTX if we failed, the caller should emit a normal call, | |
3b824fa6 | 3268 | otherwise try to get the result in TARGET, if convenient (and in |
9fe0e1b8 | 3269 | mode MODE if that's convenient). */ |
c2f47e15 | 3270 | |
53800dbe | 3271 | static rtx |
5a0de151 | 3272 | expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode) |
53800dbe | 3273 | { |
8e76ec06 | 3274 | tree fndecl = get_callee_fndecl (exp); |
c2f47e15 | 3275 | |
3276 | if (!validate_arglist (exp, | |
3277 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3278 | return NULL_RTX; | |
53800dbe | 3279 | else |
3280 | { | |
c2f47e15 | 3281 | tree dest = CALL_EXPR_ARG (exp, 0); |
3282 | tree src = CALL_EXPR_ARG (exp, 1); | |
3283 | tree len = CALL_EXPR_ARG (exp, 2); | |
6840589f | 3284 | const char *src_str; |
27d0c333 | 3285 | unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT); |
3286 | unsigned int dest_align | |
3287 | = get_pointer_alignment (dest, BIGGEST_ALIGNMENT); | |
53800dbe | 3288 | rtx dest_mem, src_mem, dest_addr, len_rtx; |
c2f47e15 | 3289 | tree result = fold_builtin_memory_op (dest, src, len, |
3290 | TREE_TYPE (TREE_TYPE (fndecl)), | |
4f46f2b9 | 3291 | false, /*endp=*/0); |
162719b3 | 3292 | HOST_WIDE_INT expected_size = -1; |
3293 | unsigned int expected_align = 0; | |
5a0de151 | 3294 | |
3295 | if (result) | |
4f46f2b9 | 3296 | { |
3297 | while (TREE_CODE (result) == COMPOUND_EXPR) | |
3298 | { | |
3299 | expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode, | |
3300 | EXPAND_NORMAL); | |
3301 | result = TREE_OPERAND (result, 1); | |
3302 | } | |
3303 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
3304 | } | |
53800dbe | 3305 | |
6f428e8b | 3306 | /* If DEST is not a pointer type, call the normal function. */ |
3307 | if (dest_align == 0) | |
c2f47e15 | 3308 | return NULL_RTX; |
6f428e8b | 3309 | |
6f428e8b | 3310 | /* If either SRC is not a pointer type, don't do this |
a0c938f0 | 3311 | operation in-line. */ |
6f428e8b | 3312 | if (src_align == 0) |
c2f47e15 | 3313 | return NULL_RTX; |
162719b3 | 3314 | |
3315 | stringop_block_profile (exp, &expected_align, &expected_size); | |
3316 | if (expected_align < dest_align) | |
3317 | expected_align = dest_align; | |
d8ae1baa | 3318 | dest_mem = get_memory_rtx (dest, len); |
2a631e19 | 3319 | set_mem_align (dest_mem, dest_align); |
8ec3c5c2 | 3320 | len_rtx = expand_normal (len); |
6840589f | 3321 | src_str = c_getstr (src); |
3322 | ||
3323 | /* If SRC is a string constant and block move would be done | |
3324 | by pieces, we can avoid loading the string from memory | |
3325 | and only stored the computed constants. */ | |
3326 | if (src_str | |
6840589f | 3327 | && GET_CODE (len_rtx) == CONST_INT |
3328 | && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1 | |
3329 | && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str, | |
4b297e2e | 3330 | (void *) src_str, dest_align, false)) |
6840589f | 3331 | { |
9fe0e1b8 | 3332 | dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx), |
3333 | builtin_memcpy_read_str, | |
4b297e2e | 3334 | (void *) src_str, dest_align, false, 0); |
e5716f7e | 3335 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
85d654dd | 3336 | dest_mem = convert_memory_address (ptr_mode, dest_mem); |
9fe0e1b8 | 3337 | return dest_mem; |
6840589f | 3338 | } |
3339 | ||
d8ae1baa | 3340 | src_mem = get_memory_rtx (src, len); |
2a631e19 | 3341 | set_mem_align (src_mem, src_align); |
53800dbe | 3342 | |
53800dbe | 3343 | /* Copy word part most expediently. */ |
162719b3 | 3344 | dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx, |
3345 | CALL_EXPR_TAILCALL (exp) | |
3346 | ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL, | |
3347 | expected_align, expected_size); | |
53800dbe | 3348 | |
3349 | if (dest_addr == 0) | |
e5716f7e | 3350 | { |
3351 | dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX); | |
85d654dd | 3352 | dest_addr = convert_memory_address (ptr_mode, dest_addr); |
e5716f7e | 3353 | } |
9fe0e1b8 | 3354 | return dest_addr; |
53800dbe | 3355 | } |
3356 | } | |
3357 | ||
c2f47e15 | 3358 | /* Expand a call EXP to the mempcpy builtin. |
3359 | Return NULL_RTX if we failed; the caller should emit a normal call, | |
647661c6 | 3360 | otherwise try to get the result in TARGET, if convenient (and in |
9fe0e1b8 | 3361 | mode MODE if that's convenient). If ENDP is 0 return the |
3362 | destination pointer, if ENDP is 1 return the end pointer ala | |
3363 | mempcpy, and if ENDP is 2 return the end pointer minus one ala | |
3364 | stpcpy. */ | |
647661c6 | 3365 | |
3366 | static rtx | |
c2f47e15 | 3367 | expand_builtin_mempcpy(tree exp, rtx target, enum machine_mode mode) |
647661c6 | 3368 | { |
c2f47e15 | 3369 | if (!validate_arglist (exp, |
3370 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3371 | return NULL_RTX; | |
3372 | else | |
3373 | { | |
3374 | tree dest = CALL_EXPR_ARG (exp, 0); | |
3375 | tree src = CALL_EXPR_ARG (exp, 1); | |
3376 | tree len = CALL_EXPR_ARG (exp, 2); | |
3377 | return expand_builtin_mempcpy_args (dest, src, len, | |
3378 | TREE_TYPE (exp), | |
3379 | target, mode, /*endp=*/ 1); | |
3380 | } | |
3381 | } | |
3382 | ||
3383 | /* Helper function to do the actual work for expand_builtin_mempcpy. The | |
3384 | arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out | |
3385 | so that this can also be called without constructing an actual CALL_EXPR. | |
3386 | TYPE is the return type of the call. The other arguments and return value | |
3387 | are the same as for expand_builtin_mempcpy. */ | |
3388 | ||
3389 | static rtx | |
3390 | expand_builtin_mempcpy_args (tree dest, tree src, tree len, tree type, | |
3391 | rtx target, enum machine_mode mode, int endp) | |
3392 | { | |
3393 | /* If return value is ignored, transform mempcpy into memcpy. */ | |
3394 | if (target == const0_rtx) | |
9fe0e1b8 | 3395 | { |
3396 | tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY]; | |
3397 | ||
3398 | if (!fn) | |
c2f47e15 | 3399 | return NULL_RTX; |
9fe0e1b8 | 3400 | |
c2f47e15 | 3401 | return expand_expr (build_call_expr (fn, 3, dest, src, len), |
9fe0e1b8 | 3402 | target, mode, EXPAND_NORMAL); |
3403 | } | |
647661c6 | 3404 | else |
3405 | { | |
9fe0e1b8 | 3406 | const char *src_str; |
3407 | unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT); | |
3408 | unsigned int dest_align | |
3409 | = get_pointer_alignment (dest, BIGGEST_ALIGNMENT); | |
3410 | rtx dest_mem, src_mem, len_rtx; | |
c2f47e15 | 3411 | tree result = fold_builtin_memory_op (dest, src, len, type, false, endp); |
9fe0e1b8 | 3412 | |
7da1412b | 3413 | if (result) |
4f46f2b9 | 3414 | { |
3415 | while (TREE_CODE (result) == COMPOUND_EXPR) | |
3416 | { | |
3417 | expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode, | |
3418 | EXPAND_NORMAL); | |
3419 | result = TREE_OPERAND (result, 1); | |
3420 | } | |
3421 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
3422 | } | |
a0c938f0 | 3423 | |
7da1412b | 3424 | /* If either SRC or DEST is not a pointer type, don't do this |
a0c938f0 | 3425 | operation in-line. */ |
7da1412b | 3426 | if (dest_align == 0 || src_align == 0) |
c2f47e15 | 3427 | return NULL_RTX; |
9fe0e1b8 | 3428 | |
6217c238 | 3429 | /* If LEN is not constant, call the normal function. */ |
3430 | if (! host_integerp (len, 1)) | |
c2f47e15 | 3431 | return NULL_RTX; |
0862b7e9 | 3432 | |
8ec3c5c2 | 3433 | len_rtx = expand_normal (len); |
9fe0e1b8 | 3434 | src_str = c_getstr (src); |
647661c6 | 3435 | |
9fe0e1b8 | 3436 | /* If SRC is a string constant and block move would be done |
3437 | by pieces, we can avoid loading the string from memory | |
3438 | and only stored the computed constants. */ | |
3439 | if (src_str | |
3440 | && GET_CODE (len_rtx) == CONST_INT | |
3441 | && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1 | |
3442 | && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str, | |
4b297e2e | 3443 | (void *) src_str, dest_align, false)) |
9fe0e1b8 | 3444 | { |
d8ae1baa | 3445 | dest_mem = get_memory_rtx (dest, len); |
9fe0e1b8 | 3446 | set_mem_align (dest_mem, dest_align); |
3447 | dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx), | |
3448 | builtin_memcpy_read_str, | |
4b297e2e | 3449 | (void *) src_str, dest_align, |
3450 | false, endp); | |
9fe0e1b8 | 3451 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
85d654dd | 3452 | dest_mem = convert_memory_address (ptr_mode, dest_mem); |
9fe0e1b8 | 3453 | return dest_mem; |
647661c6 | 3454 | } |
3455 | ||
9fe0e1b8 | 3456 | if (GET_CODE (len_rtx) == CONST_INT |
3457 | && can_move_by_pieces (INTVAL (len_rtx), | |
3458 | MIN (dest_align, src_align))) | |
3459 | { | |
d8ae1baa | 3460 | dest_mem = get_memory_rtx (dest, len); |
9fe0e1b8 | 3461 | set_mem_align (dest_mem, dest_align); |
d8ae1baa | 3462 | src_mem = get_memory_rtx (src, len); |
9fe0e1b8 | 3463 | set_mem_align (src_mem, src_align); |
3464 | dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx), | |
3465 | MIN (dest_align, src_align), endp); | |
3466 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); | |
85d654dd | 3467 | dest_mem = convert_memory_address (ptr_mode, dest_mem); |
9fe0e1b8 | 3468 | return dest_mem; |
3469 | } | |
3470 | ||
c2f47e15 | 3471 | return NULL_RTX; |
647661c6 | 3472 | } |
3473 | } | |
3474 | ||
c2f47e15 | 3475 | /* Expand expression EXP, which is a call to the memmove builtin. Return |
3476 | NULL_RTX if we failed; the caller should emit a normal call. */ | |
c4950093 | 3477 | |
3478 | static rtx | |
c2f47e15 | 3479 | expand_builtin_memmove (tree exp, rtx target, enum machine_mode mode, int ignore) |
c4950093 | 3480 | { |
c2f47e15 | 3481 | if (!validate_arglist (exp, |
3482 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3483 | return NULL_RTX; | |
c4950093 | 3484 | else |
3485 | { | |
c2f47e15 | 3486 | tree dest = CALL_EXPR_ARG (exp, 0); |
3487 | tree src = CALL_EXPR_ARG (exp, 1); | |
3488 | tree len = CALL_EXPR_ARG (exp, 2); | |
3489 | return expand_builtin_memmove_args (dest, src, len, TREE_TYPE (exp), | |
3490 | target, mode, ignore); | |
3491 | } | |
3492 | } | |
7e72af53 | 3493 | |
c2f47e15 | 3494 | /* Helper function to do the actual work for expand_builtin_memmove. The |
3495 | arguments to the builtin_memmove call DEST, SRC, and LEN are broken out | |
3496 | so that this can also be called without constructing an actual CALL_EXPR. | |
3497 | TYPE is the return type of the call. The other arguments and return value | |
3498 | are the same as for expand_builtin_memmove. */ | |
3499 | ||
3500 | static rtx | |
3501 | expand_builtin_memmove_args (tree dest, tree src, tree len, | |
3502 | tree type, rtx target, enum machine_mode mode, | |
3503 | int ignore) | |
3504 | { | |
3505 | tree result = fold_builtin_memory_op (dest, src, len, type, ignore, /*endp=*/3); | |
3506 | ||
3507 | if (result) | |
3508 | { | |
05be68f3 | 3509 | STRIP_TYPE_NOPS (result); |
c2f47e15 | 3510 | while (TREE_CODE (result) == COMPOUND_EXPR) |
4f46f2b9 | 3511 | { |
c2f47e15 | 3512 | expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode, |
3513 | EXPAND_NORMAL); | |
3514 | result = TREE_OPERAND (result, 1); | |
4f46f2b9 | 3515 | } |
c2f47e15 | 3516 | return expand_expr (result, target, mode, EXPAND_NORMAL); |
3517 | } | |
3518 | ||
3519 | /* Otherwise, call the normal function. */ | |
3520 | return NULL_RTX; | |
c4950093 | 3521 | } |
3522 | ||
c2f47e15 | 3523 | /* Expand expression EXP, which is a call to the bcopy builtin. Return |
3524 | NULL_RTX if we failed the caller should emit a normal call. */ | |
c4950093 | 3525 | |
3526 | static rtx | |
c2f47e15 | 3527 | expand_builtin_bcopy (tree exp, int ignore) |
c4950093 | 3528 | { |
0b25db21 | 3529 | tree type = TREE_TYPE (exp); |
c2f47e15 | 3530 | tree src, dest, size; |
c4950093 | 3531 | |
c2f47e15 | 3532 | if (!validate_arglist (exp, |
3533 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
c4950093 | 3534 | return NULL_RTX; |
3535 | ||
c2f47e15 | 3536 | src = CALL_EXPR_ARG (exp, 0); |
3537 | dest = CALL_EXPR_ARG (exp, 1); | |
3538 | size = CALL_EXPR_ARG (exp, 2); | |
c4950093 | 3539 | |
c2f47e15 | 3540 | /* Transform bcopy(ptr x, ptr y, int z) to memmove(ptr y, ptr x, size_t z). |
3541 | This is done this way so that if it isn't expanded inline, we fall | |
3542 | back to calling bcopy instead of memmove. */ | |
3543 | return expand_builtin_memmove_args (dest, src, | |
3544 | fold_convert (sizetype, size), | |
3545 | type, const0_rtx, VOIDmode, | |
3546 | ignore); | |
c4950093 | 3547 | } |
3548 | ||
727c62dd | 3549 | #ifndef HAVE_movstr |
3550 | # define HAVE_movstr 0 | |
3551 | # define CODE_FOR_movstr CODE_FOR_nothing | |
3552 | #endif | |
3553 | ||
c2f47e15 | 3554 | /* Expand into a movstr instruction, if one is available. Return NULL_RTX if |
727c62dd | 3555 | we failed, the caller should emit a normal call, otherwise try to |
3556 | get the result in TARGET, if convenient. If ENDP is 0 return the | |
3557 | destination pointer, if ENDP is 1 return the end pointer ala | |
3558 | mempcpy, and if ENDP is 2 return the end pointer minus one ala | |
3559 | stpcpy. */ | |
3560 | ||
3561 | static rtx | |
3562 | expand_movstr (tree dest, tree src, rtx target, int endp) | |
3563 | { | |
3564 | rtx end; | |
3565 | rtx dest_mem; | |
3566 | rtx src_mem; | |
3567 | rtx insn; | |
3568 | const struct insn_data * data; | |
3569 | ||
3570 | if (!HAVE_movstr) | |
c2f47e15 | 3571 | return NULL_RTX; |
727c62dd | 3572 | |
d8ae1baa | 3573 | dest_mem = get_memory_rtx (dest, NULL); |
3574 | src_mem = get_memory_rtx (src, NULL); | |
727c62dd | 3575 | if (!endp) |
3576 | { | |
3577 | target = force_reg (Pmode, XEXP (dest_mem, 0)); | |
3578 | dest_mem = replace_equiv_address (dest_mem, target); | |
3579 | end = gen_reg_rtx (Pmode); | |
3580 | } | |
3581 | else | |
3582 | { | |
3583 | if (target == 0 || target == const0_rtx) | |
3584 | { | |
3585 | end = gen_reg_rtx (Pmode); | |
3586 | if (target == 0) | |
3587 | target = end; | |
3588 | } | |
3589 | else | |
3590 | end = target; | |
3591 | } | |
3592 | ||
3593 | data = insn_data + CODE_FOR_movstr; | |
3594 | ||
3595 | if (data->operand[0].mode != VOIDmode) | |
3596 | end = gen_lowpart (data->operand[0].mode, end); | |
3597 | ||
3598 | insn = data->genfun (end, dest_mem, src_mem); | |
3599 | ||
64db345d | 3600 | gcc_assert (insn); |
727c62dd | 3601 | |
3602 | emit_insn (insn); | |
3603 | ||
3604 | /* movstr is supposed to set end to the address of the NUL | |
3605 | terminator. If the caller requested a mempcpy-like return value, | |
3606 | adjust it. */ | |
3607 | if (endp == 1 && target != const0_rtx) | |
c5aba89c | 3608 | { |
3609 | rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1); | |
3610 | emit_move_insn (target, force_operand (tem, NULL_RTX)); | |
3611 | } | |
727c62dd | 3612 | |
3613 | return target; | |
3614 | } | |
3615 | ||
c2f47e15 | 3616 | /* Expand expression EXP, which is a call to the strcpy builtin. Return |
3617 | NULL_RTX if we failed the caller should emit a normal call, otherwise | |
3618 | try to get the result in TARGET, if convenient (and in mode MODE if that's | |
6f428e8b | 3619 | convenient). */ |
902de8ed | 3620 | |
53800dbe | 3621 | static rtx |
c2f47e15 | 3622 | expand_builtin_strcpy (tree fndecl, tree exp, rtx target, enum machine_mode mode) |
53800dbe | 3623 | { |
c2f47e15 | 3624 | if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
3625 | { | |
3626 | tree dest = CALL_EXPR_ARG (exp, 0); | |
3627 | tree src = CALL_EXPR_ARG (exp, 1); | |
3628 | return expand_builtin_strcpy_args (fndecl, dest, src, target, mode); | |
3629 | } | |
3630 | return NULL_RTX; | |
3631 | } | |
3632 | ||
3633 | /* Helper function to do the actual work for expand_builtin_strcpy. The | |
3634 | arguments to the builtin_strcpy call DEST and SRC are broken out | |
3635 | so that this can also be called without constructing an actual CALL_EXPR. | |
3636 | The other arguments and return value are the same as for | |
3637 | expand_builtin_strcpy. */ | |
3638 | ||
3639 | static rtx | |
3640 | expand_builtin_strcpy_args (tree fndecl, tree dest, tree src, | |
3641 | rtx target, enum machine_mode mode) | |
3642 | { | |
3643 | tree result = fold_builtin_strcpy (fndecl, dest, src, 0); | |
3644 | if (result) | |
3645 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
3646 | return expand_movstr (dest, src, target, /*endp=*/0); | |
902de8ed | 3647 | |
53800dbe | 3648 | } |
3649 | ||
c2f47e15 | 3650 | /* Expand a call EXP to the stpcpy builtin. |
3651 | Return NULL_RTX if we failed the caller should emit a normal call, | |
3b824fa6 | 3652 | otherwise try to get the result in TARGET, if convenient (and in |
3653 | mode MODE if that's convenient). */ | |
3654 | ||
3655 | static rtx | |
dc369150 | 3656 | expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode) |
3b824fa6 | 3657 | { |
c2f47e15 | 3658 | tree dst, src; |
3659 | ||
3660 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
3661 | return NULL_RTX; | |
3662 | ||
3663 | dst = CALL_EXPR_ARG (exp, 0); | |
3664 | src = CALL_EXPR_ARG (exp, 1); | |
3665 | ||
727c62dd | 3666 | /* If return value is ignored, transform stpcpy into strcpy. */ |
3667 | if (target == const0_rtx) | |
978836e5 | 3668 | { |
3669 | tree fn = implicit_built_in_decls[BUILT_IN_STRCPY]; | |
3670 | if (!fn) | |
c2f47e15 | 3671 | return NULL_RTX; |
978836e5 | 3672 | |
c2f47e15 | 3673 | return expand_expr (build_call_expr (fn, 2, dst, src), |
3674 | target, mode, EXPAND_NORMAL); | |
978836e5 | 3675 | } |
3b824fa6 | 3676 | else |
3677 | { | |
c2f47e15 | 3678 | tree len, lenp1; |
727c62dd | 3679 | rtx ret; |
647661c6 | 3680 | |
9fe0e1b8 | 3681 | /* Ensure we get an actual string whose length can be evaluated at |
a0c938f0 | 3682 | compile-time, not an expression containing a string. This is |
3683 | because the latter will potentially produce pessimized code | |
3684 | when used to produce the return value. */ | |
681fab1e | 3685 | if (! c_getstr (src) || ! (len = c_strlen (src, 0))) |
c2f47e15 | 3686 | return expand_movstr (dst, src, target, /*endp=*/2); |
3b824fa6 | 3687 | |
7d705727 | 3688 | lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1)); |
c2f47e15 | 3689 | ret = expand_builtin_mempcpy_args (dst, src, lenp1, TREE_TYPE (exp), |
3690 | target, mode, /*endp=*/2); | |
727c62dd | 3691 | |
3692 | if (ret) | |
3693 | return ret; | |
3694 | ||
3695 | if (TREE_CODE (len) == INTEGER_CST) | |
3696 | { | |
8ec3c5c2 | 3697 | rtx len_rtx = expand_normal (len); |
727c62dd | 3698 | |
3699 | if (GET_CODE (len_rtx) == CONST_INT) | |
3700 | { | |
c2f47e15 | 3701 | ret = expand_builtin_strcpy_args (get_callee_fndecl (exp), |
3702 | dst, src, target, mode); | |
727c62dd | 3703 | |
3704 | if (ret) | |
3705 | { | |
3706 | if (! target) | |
7ac87324 | 3707 | { |
3708 | if (mode != VOIDmode) | |
3709 | target = gen_reg_rtx (mode); | |
3710 | else | |
3711 | target = gen_reg_rtx (GET_MODE (ret)); | |
3712 | } | |
727c62dd | 3713 | if (GET_MODE (target) != GET_MODE (ret)) |
3714 | ret = gen_lowpart (GET_MODE (target), ret); | |
3715 | ||
c5aba89c | 3716 | ret = plus_constant (ret, INTVAL (len_rtx)); |
3717 | ret = emit_move_insn (target, force_operand (ret, NULL_RTX)); | |
64db345d | 3718 | gcc_assert (ret); |
727c62dd | 3719 | |
3720 | return target; | |
3721 | } | |
3722 | } | |
3723 | } | |
3724 | ||
c2f47e15 | 3725 | return expand_movstr (dst, src, target, /*endp=*/2); |
3b824fa6 | 3726 | } |
3727 | } | |
3728 | ||
6840589f | 3729 | /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE) |
3730 | bytes from constant string DATA + OFFSET and return it as target | |
3731 | constant. */ | |
3732 | ||
09879952 | 3733 | rtx |
aecda0d6 | 3734 | builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset, |
3735 | enum machine_mode mode) | |
6840589f | 3736 | { |
3737 | const char *str = (const char *) data; | |
3738 | ||
3739 | if ((unsigned HOST_WIDE_INT) offset > strlen (str)) | |
3740 | return const0_rtx; | |
3741 | ||
3742 | return c_readstr (str + offset, mode); | |
3743 | } | |
3744 | ||
c2f47e15 | 3745 | /* Expand expression EXP, which is a call to the strncpy builtin. Return |
3746 | NULL_RTX if we failed the caller should emit a normal call. */ | |
ed09096d | 3747 | |
3748 | static rtx | |
8ff6a5cd | 3749 | expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode) |
ed09096d | 3750 | { |
ea726109 | 3751 | tree fndecl = get_callee_fndecl (exp); |
c2f47e15 | 3752 | |
3753 | if (validate_arglist (exp, | |
3754 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
ed09096d | 3755 | { |
c2f47e15 | 3756 | tree dest = CALL_EXPR_ARG (exp, 0); |
3757 | tree src = CALL_EXPR_ARG (exp, 1); | |
3758 | tree len = CALL_EXPR_ARG (exp, 2); | |
3759 | tree slen = c_strlen (src, 1); | |
3760 | tree result = fold_builtin_strncpy (fndecl, dest, src, len, slen); | |
a0c938f0 | 3761 | |
8ff6a5cd | 3762 | if (result) |
4f46f2b9 | 3763 | { |
3764 | while (TREE_CODE (result) == COMPOUND_EXPR) | |
3765 | { | |
3766 | expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode, | |
3767 | EXPAND_NORMAL); | |
3768 | result = TREE_OPERAND (result, 1); | |
3769 | } | |
3770 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
3771 | } | |
6840589f | 3772 | |
8ff6a5cd | 3773 | /* We must be passed a constant len and src parameter. */ |
3774 | if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1)) | |
c2f47e15 | 3775 | return NULL_RTX; |
ed09096d | 3776 | |
3777 | slen = size_binop (PLUS_EXPR, slen, ssize_int (1)); | |
3778 | ||
3779 | /* We're required to pad with trailing zeros if the requested | |
a0c938f0 | 3780 | len is greater than strlen(s2)+1. In that case try to |
6840589f | 3781 | use store_by_pieces, if it fails, punt. */ |
ed09096d | 3782 | if (tree_int_cst_lt (slen, len)) |
6840589f | 3783 | { |
27d0c333 | 3784 | unsigned int dest_align |
3785 | = get_pointer_alignment (dest, BIGGEST_ALIGNMENT); | |
c2f47e15 | 3786 | const char *p = c_getstr (src); |
6840589f | 3787 | rtx dest_mem; |
3788 | ||
27d0c333 | 3789 | if (!p || dest_align == 0 || !host_integerp (len, 1) |
3790 | || !can_store_by_pieces (tree_low_cst (len, 1), | |
6840589f | 3791 | builtin_strncpy_read_str, |
4b297e2e | 3792 | (void *) p, dest_align, false)) |
c2f47e15 | 3793 | return NULL_RTX; |
6840589f | 3794 | |
d8ae1baa | 3795 | dest_mem = get_memory_rtx (dest, len); |
27d0c333 | 3796 | store_by_pieces (dest_mem, tree_low_cst (len, 1), |
6840589f | 3797 | builtin_strncpy_read_str, |
4b297e2e | 3798 | (void *) p, dest_align, false, 0); |
e5716f7e | 3799 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
85d654dd | 3800 | dest_mem = convert_memory_address (ptr_mode, dest_mem); |
e5716f7e | 3801 | return dest_mem; |
6840589f | 3802 | } |
ed09096d | 3803 | } |
c2f47e15 | 3804 | return NULL_RTX; |
ed09096d | 3805 | } |
3806 | ||
ecc318ff | 3807 | /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE) |
3808 | bytes from constant string DATA + OFFSET and return it as target | |
3809 | constant. */ | |
3810 | ||
f656b751 | 3811 | rtx |
aecda0d6 | 3812 | builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED, |
3813 | enum machine_mode mode) | |
ecc318ff | 3814 | { |
3815 | const char *c = (const char *) data; | |
3816 | char *p = alloca (GET_MODE_SIZE (mode)); | |
3817 | ||
3818 | memset (p, *c, GET_MODE_SIZE (mode)); | |
3819 | ||
3820 | return c_readstr (p, mode); | |
3821 | } | |
3822 | ||
a7ec6974 | 3823 | /* Callback routine for store_by_pieces. Return the RTL of a register |
3824 | containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned | |
3825 | char value given in the RTL register data. For example, if mode is | |
3826 | 4 bytes wide, return the RTL for 0x01010101*data. */ | |
3827 | ||
3828 | static rtx | |
aecda0d6 | 3829 | builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED, |
3830 | enum machine_mode mode) | |
a7ec6974 | 3831 | { |
3832 | rtx target, coeff; | |
3833 | size_t size; | |
3834 | char *p; | |
3835 | ||
3836 | size = GET_MODE_SIZE (mode); | |
f0ce3b1f | 3837 | if (size == 1) |
3838 | return (rtx) data; | |
a7ec6974 | 3839 | |
3840 | p = alloca (size); | |
3841 | memset (p, 1, size); | |
3842 | coeff = c_readstr (p, mode); | |
3843 | ||
f0ce3b1f | 3844 | target = convert_to_mode (mode, (rtx) data, 1); |
a7ec6974 | 3845 | target = expand_mult (mode, target, coeff, NULL_RTX, 1); |
3846 | return force_reg (mode, target); | |
3847 | } | |
3848 | ||
c2f47e15 | 3849 | /* Expand expression EXP, which is a call to the memset builtin. Return |
3850 | NULL_RTX if we failed the caller should emit a normal call, otherwise | |
3851 | try to get the result in TARGET, if convenient (and in mode MODE if that's | |
6f428e8b | 3852 | convenient). */ |
902de8ed | 3853 | |
53800dbe | 3854 | static rtx |
c2f47e15 | 3855 | expand_builtin_memset (tree exp, rtx target, enum machine_mode mode) |
53800dbe | 3856 | { |
c2f47e15 | 3857 | if (!validate_arglist (exp, |
3858 | POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3859 | return NULL_RTX; | |
53800dbe | 3860 | else |
3861 | { | |
c2f47e15 | 3862 | tree dest = CALL_EXPR_ARG (exp, 0); |
3863 | tree val = CALL_EXPR_ARG (exp, 1); | |
3864 | tree len = CALL_EXPR_ARG (exp, 2); | |
3865 | return expand_builtin_memset_args (dest, val, len, target, mode, exp); | |
3866 | } | |
3867 | } | |
53800dbe | 3868 | |
c2f47e15 | 3869 | /* Helper function to do the actual work for expand_builtin_memset. The |
3870 | arguments to the builtin_memset call DEST, VAL, and LEN are broken out | |
3871 | so that this can also be called without constructing an actual CALL_EXPR. | |
3872 | The other arguments and return value are the same as for | |
3873 | expand_builtin_memset. */ | |
6b961939 | 3874 | |
c2f47e15 | 3875 | static rtx |
3876 | expand_builtin_memset_args (tree dest, tree val, tree len, | |
3877 | rtx target, enum machine_mode mode, tree orig_exp) | |
3878 | { | |
3879 | tree fndecl, fn; | |
3880 | enum built_in_function fcode; | |
3881 | char c; | |
3882 | unsigned int dest_align; | |
3883 | rtx dest_mem, dest_addr, len_rtx; | |
3884 | HOST_WIDE_INT expected_size = -1; | |
3885 | unsigned int expected_align = 0; | |
53800dbe | 3886 | |
c2f47e15 | 3887 | dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT); |
162719b3 | 3888 | |
c2f47e15 | 3889 | /* If DEST is not a pointer type, don't do this operation in-line. */ |
3890 | if (dest_align == 0) | |
3891 | return NULL_RTX; | |
6f428e8b | 3892 | |
c2f47e15 | 3893 | stringop_block_profile (orig_exp, &expected_align, &expected_size); |
3894 | if (expected_align < dest_align) | |
3895 | expected_align = dest_align; | |
6b961939 | 3896 | |
c2f47e15 | 3897 | /* If the LEN parameter is zero, return DEST. */ |
3898 | if (integer_zerop (len)) | |
3899 | { | |
3900 | /* Evaluate and ignore VAL in case it has side-effects. */ | |
3901 | expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
3902 | return expand_expr (dest, target, mode, EXPAND_NORMAL); | |
3903 | } | |
7a3e5564 | 3904 | |
c2f47e15 | 3905 | /* Stabilize the arguments in case we fail. */ |
3906 | dest = builtin_save_expr (dest); | |
3907 | val = builtin_save_expr (val); | |
3908 | len = builtin_save_expr (len); | |
a7ec6974 | 3909 | |
c2f47e15 | 3910 | len_rtx = expand_normal (len); |
3911 | dest_mem = get_memory_rtx (dest, len); | |
a7ec6974 | 3912 | |
c2f47e15 | 3913 | if (TREE_CODE (val) != INTEGER_CST) |
3914 | { | |
3915 | rtx val_rtx; | |
a7ec6974 | 3916 | |
c2f47e15 | 3917 | val_rtx = expand_normal (val); |
3918 | val_rtx = convert_to_mode (TYPE_MODE (unsigned_char_type_node), | |
3919 | val_rtx, 0); | |
53800dbe | 3920 | |
c2f47e15 | 3921 | /* Assume that we can memset by pieces if we can store |
3922 | * the coefficients by pieces (in the required modes). | |
3923 | * We can't pass builtin_memset_gen_str as that emits RTL. */ | |
3924 | c = 1; | |
3925 | if (host_integerp (len, 1) | |
c2f47e15 | 3926 | && can_store_by_pieces (tree_low_cst (len, 1), |
4b297e2e | 3927 | builtin_memset_read_str, &c, dest_align, |
3928 | true)) | |
c2f47e15 | 3929 | { |
3930 | val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node), | |
3931 | val_rtx); | |
3932 | store_by_pieces (dest_mem, tree_low_cst (len, 1), | |
4b297e2e | 3933 | builtin_memset_gen_str, val_rtx, dest_align, |
3934 | true, 0); | |
c2f47e15 | 3935 | } |
3936 | else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx, | |
3937 | dest_align, expected_align, | |
3938 | expected_size)) | |
6b961939 | 3939 | goto do_libcall; |
c2f47e15 | 3940 | |
3941 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); | |
3942 | dest_mem = convert_memory_address (ptr_mode, dest_mem); | |
3943 | return dest_mem; | |
3944 | } | |
53800dbe | 3945 | |
c2f47e15 | 3946 | if (target_char_cast (val, &c)) |
3947 | goto do_libcall; | |
ecc318ff | 3948 | |
c2f47e15 | 3949 | if (c) |
3950 | { | |
3951 | if (host_integerp (len, 1) | |
c2f47e15 | 3952 | && can_store_by_pieces (tree_low_cst (len, 1), |
4b297e2e | 3953 | builtin_memset_read_str, &c, dest_align, |
3954 | true)) | |
c2f47e15 | 3955 | store_by_pieces (dest_mem, tree_low_cst (len, 1), |
4b297e2e | 3956 | builtin_memset_read_str, &c, dest_align, true, 0); |
c2f47e15 | 3957 | else if (!set_storage_via_setmem (dest_mem, len_rtx, GEN_INT (c), |
3958 | dest_align, expected_align, | |
3959 | expected_size)) | |
3960 | goto do_libcall; | |
3961 | ||
3962 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); | |
3963 | dest_mem = convert_memory_address (ptr_mode, dest_mem); | |
3964 | return dest_mem; | |
3965 | } | |
ecc318ff | 3966 | |
c2f47e15 | 3967 | set_mem_align (dest_mem, dest_align); |
3968 | dest_addr = clear_storage_hints (dest_mem, len_rtx, | |
3969 | CALL_EXPR_TAILCALL (orig_exp) | |
3970 | ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL, | |
3971 | expected_align, expected_size); | |
53800dbe | 3972 | |
c2f47e15 | 3973 | if (dest_addr == 0) |
3974 | { | |
3975 | dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX); | |
3976 | dest_addr = convert_memory_address (ptr_mode, dest_addr); | |
3977 | } | |
53800dbe | 3978 | |
c2f47e15 | 3979 | return dest_addr; |
6b961939 | 3980 | |
c2f47e15 | 3981 | do_libcall: |
3982 | fndecl = get_callee_fndecl (orig_exp); | |
3983 | fcode = DECL_FUNCTION_CODE (fndecl); | |
3984 | if (fcode == BUILT_IN_MEMSET) | |
3985 | fn = build_call_expr (fndecl, 3, dest, val, len); | |
3986 | else if (fcode == BUILT_IN_BZERO) | |
3987 | fn = build_call_expr (fndecl, 2, dest, len); | |
3988 | else | |
3989 | gcc_unreachable (); | |
3990 | if (TREE_CODE (fn) == CALL_EXPR) | |
3991 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp); | |
3992 | return expand_call (fn, target, target == const0_rtx); | |
53800dbe | 3993 | } |
3994 | ||
c2f47e15 | 3995 | /* Expand expression EXP, which is a call to the bzero builtin. Return |
3996 | NULL_RTX if we failed the caller should emit a normal call. */ | |
27d0c333 | 3997 | |
ffc83088 | 3998 | static rtx |
0b25db21 | 3999 | expand_builtin_bzero (tree exp) |
ffc83088 | 4000 | { |
c2f47e15 | 4001 | tree dest, size; |
ffc83088 | 4002 | |
c2f47e15 | 4003 | if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
7369e7ba | 4004 | return NULL_RTX; |
ffc83088 | 4005 | |
c2f47e15 | 4006 | dest = CALL_EXPR_ARG (exp, 0); |
4007 | size = CALL_EXPR_ARG (exp, 1); | |
bf8e3599 | 4008 | |
7369e7ba | 4009 | /* New argument list transforming bzero(ptr x, int y) to |
6f428e8b | 4010 | memset(ptr x, int 0, size_t y). This is done this way |
4011 | so that if it isn't expanded inline, we fallback to | |
4012 | calling bzero instead of memset. */ | |
bf8e3599 | 4013 | |
c2f47e15 | 4014 | return expand_builtin_memset_args (dest, integer_zero_node, |
4015 | fold_convert (sizetype, size), | |
4016 | const0_rtx, VOIDmode, exp); | |
ffc83088 | 4017 | } |
4018 | ||
7959b13b | 4019 | /* Expand a call to the memchr builtin. Return NULL_RTX if we failed the |
4020 | caller should emit a normal call, otherwise try to get the result | |
4021 | in TARGET, if convenient (and in mode MODE if that's convenient). */ | |
4022 | ||
4023 | static rtx | |
4024 | expand_builtin_memchr (tree exp, rtx target, enum machine_mode mode) | |
4025 | { | |
4026 | if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, | |
4027 | INTEGER_TYPE, VOID_TYPE)) | |
4028 | { | |
4029 | tree type = TREE_TYPE (exp); | |
4030 | tree result = fold_builtin_memchr (CALL_EXPR_ARG (exp, 0), | |
4031 | CALL_EXPR_ARG (exp, 1), | |
4032 | CALL_EXPR_ARG (exp, 2), type); | |
4033 | if (result) | |
4034 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
4035 | } | |
4036 | return NULL_RTX; | |
4037 | } | |
4038 | ||
7a3f89b5 | 4039 | /* Expand expression EXP, which is a call to the memcmp built-in function. |
c2f47e15 | 4040 | Return NULL_RTX if we failed and the |
53800dbe | 4041 | caller should emit a normal call, otherwise try to get the result in |
6f428e8b | 4042 | TARGET, if convenient (and in mode MODE, if that's convenient). */ |
27d0c333 | 4043 | |
53800dbe | 4044 | static rtx |
c2f47e15 | 4045 | expand_builtin_memcmp (tree exp, rtx target, enum machine_mode mode) |
53800dbe | 4046 | { |
c2f47e15 | 4047 | if (!validate_arglist (exp, |
4048 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
4049 | return NULL_RTX; | |
218bc19e | 4050 | else |
6f428e8b | 4051 | { |
c2f47e15 | 4052 | tree result = fold_builtin_memcmp (CALL_EXPR_ARG (exp, 0), |
4053 | CALL_EXPR_ARG (exp, 1), | |
4054 | CALL_EXPR_ARG (exp, 2)); | |
218bc19e | 4055 | if (result) |
4056 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
6f428e8b | 4057 | } |
4058 | ||
6ac5504b | 4059 | #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi |
53800dbe | 4060 | { |
0cd832f0 | 4061 | rtx arg1_rtx, arg2_rtx, arg3_rtx; |
53800dbe | 4062 | rtx result; |
0cd832f0 | 4063 | rtx insn; |
c2f47e15 | 4064 | tree arg1 = CALL_EXPR_ARG (exp, 0); |
4065 | tree arg2 = CALL_EXPR_ARG (exp, 1); | |
4066 | tree len = CALL_EXPR_ARG (exp, 2); | |
53800dbe | 4067 | |
4068 | int arg1_align | |
4069 | = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; | |
4070 | int arg2_align | |
4071 | = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; | |
b428c0a5 | 4072 | enum machine_mode insn_mode; |
4073 | ||
4074 | #ifdef HAVE_cmpmemsi | |
4075 | if (HAVE_cmpmemsi) | |
4076 | insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode; | |
4077 | else | |
4078 | #endif | |
6ac5504b | 4079 | #ifdef HAVE_cmpstrnsi |
4080 | if (HAVE_cmpstrnsi) | |
4081 | insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode; | |
b428c0a5 | 4082 | else |
4083 | #endif | |
c2f47e15 | 4084 | return NULL_RTX; |
53800dbe | 4085 | |
4086 | /* If we don't have POINTER_TYPE, call the function. */ | |
4087 | if (arg1_align == 0 || arg2_align == 0) | |
c2f47e15 | 4088 | return NULL_RTX; |
53800dbe | 4089 | |
4090 | /* Make a place to write the result of the instruction. */ | |
4091 | result = target; | |
4092 | if (! (result != 0 | |
8ad4c111 | 4093 | && REG_P (result) && GET_MODE (result) == insn_mode |
53800dbe | 4094 | && REGNO (result) >= FIRST_PSEUDO_REGISTER)) |
4095 | result = gen_reg_rtx (insn_mode); | |
4096 | ||
d8ae1baa | 4097 | arg1_rtx = get_memory_rtx (arg1, len); |
4098 | arg2_rtx = get_memory_rtx (arg2, len); | |
8ec3c5c2 | 4099 | arg3_rtx = expand_normal (len); |
83f88f8e | 4100 | |
4101 | /* Set MEM_SIZE as appropriate. */ | |
4102 | if (GET_CODE (arg3_rtx) == CONST_INT) | |
4103 | { | |
4104 | set_mem_size (arg1_rtx, arg3_rtx); | |
4105 | set_mem_size (arg2_rtx, arg3_rtx); | |
4106 | } | |
4107 | ||
b428c0a5 | 4108 | #ifdef HAVE_cmpmemsi |
4109 | if (HAVE_cmpmemsi) | |
4110 | insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx, | |
4111 | GEN_INT (MIN (arg1_align, arg2_align))); | |
0cd832f0 | 4112 | else |
b428c0a5 | 4113 | #endif |
6ac5504b | 4114 | #ifdef HAVE_cmpstrnsi |
4115 | if (HAVE_cmpstrnsi) | |
4116 | insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx, | |
4117 | GEN_INT (MIN (arg1_align, arg2_align))); | |
b428c0a5 | 4118 | else |
4119 | #endif | |
64db345d | 4120 | gcc_unreachable (); |
0cd832f0 | 4121 | |
4122 | if (insn) | |
4123 | emit_insn (insn); | |
4124 | else | |
2dd6f9ed | 4125 | emit_library_call_value (memcmp_libfunc, result, LCT_PURE, |
0cd832f0 | 4126 | TYPE_MODE (integer_type_node), 3, |
4127 | XEXP (arg1_rtx, 0), Pmode, | |
4128 | XEXP (arg2_rtx, 0), Pmode, | |
4129 | convert_to_mode (TYPE_MODE (sizetype), arg3_rtx, | |
78a8ed03 | 4130 | TYPE_UNSIGNED (sizetype)), |
0cd832f0 | 4131 | TYPE_MODE (sizetype)); |
53800dbe | 4132 | |
4133 | /* Return the value in the proper mode for this function. */ | |
4134 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
4135 | if (GET_MODE (result) == mode) | |
4136 | return result; | |
4137 | else if (target != 0) | |
4138 | { | |
4139 | convert_move (target, result, 0); | |
4140 | return target; | |
4141 | } | |
4142 | else | |
4143 | return convert_to_mode (mode, result, 0); | |
4144 | } | |
83d79705 | 4145 | #endif |
53800dbe | 4146 | |
c2f47e15 | 4147 | return NULL_RTX; |
6f428e8b | 4148 | } |
4149 | ||
c2f47e15 | 4150 | /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX |
53800dbe | 4151 | if we failed the caller should emit a normal call, otherwise try to get |
4152 | the result in TARGET, if convenient. */ | |
902de8ed | 4153 | |
53800dbe | 4154 | static rtx |
aecda0d6 | 4155 | expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode) |
53800dbe | 4156 | { |
c2f47e15 | 4157 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
4158 | return NULL_RTX; | |
218bc19e | 4159 | else |
ef6c187e | 4160 | { |
c2f47e15 | 4161 | tree result = fold_builtin_strcmp (CALL_EXPR_ARG (exp, 0), |
4162 | CALL_EXPR_ARG (exp, 1)); | |
218bc19e | 4163 | if (result) |
4164 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
ef6c187e | 4165 | } |
bf8e3599 | 4166 | |
6ac5504b | 4167 | #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi |
4168 | if (cmpstr_optab[SImode] != CODE_FOR_nothing | |
4169 | || cmpstrn_optab[SImode] != CODE_FOR_nothing) | |
4170 | { | |
4171 | rtx arg1_rtx, arg2_rtx; | |
4172 | rtx result, insn = NULL_RTX; | |
4173 | tree fndecl, fn; | |
c2f47e15 | 4174 | tree arg1 = CALL_EXPR_ARG (exp, 0); |
4175 | tree arg2 = CALL_EXPR_ARG (exp, 1); | |
a0c938f0 | 4176 | |
6ac5504b | 4177 | int arg1_align |
4178 | = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; | |
4179 | int arg2_align | |
4180 | = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; | |
4181 | ||
4182 | /* If we don't have POINTER_TYPE, call the function. */ | |
4183 | if (arg1_align == 0 || arg2_align == 0) | |
c2f47e15 | 4184 | return NULL_RTX; |
7a3f89b5 | 4185 | |
6ac5504b | 4186 | /* Stabilize the arguments in case gen_cmpstr(n)si fail. */ |
4187 | arg1 = builtin_save_expr (arg1); | |
4188 | arg2 = builtin_save_expr (arg2); | |
7a3f89b5 | 4189 | |
d8ae1baa | 4190 | arg1_rtx = get_memory_rtx (arg1, NULL); |
4191 | arg2_rtx = get_memory_rtx (arg2, NULL); | |
53800dbe | 4192 | |
6ac5504b | 4193 | #ifdef HAVE_cmpstrsi |
4194 | /* Try to call cmpstrsi. */ | |
4195 | if (HAVE_cmpstrsi) | |
4196 | { | |
a0c938f0 | 4197 | enum machine_mode insn_mode |
6ac5504b | 4198 | = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode; |
4199 | ||
4200 | /* Make a place to write the result of the instruction. */ | |
4201 | result = target; | |
4202 | if (! (result != 0 | |
4203 | && REG_P (result) && GET_MODE (result) == insn_mode | |
4204 | && REGNO (result) >= FIRST_PSEUDO_REGISTER)) | |
4205 | result = gen_reg_rtx (insn_mode); | |
4206 | ||
4207 | insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, | |
4208 | GEN_INT (MIN (arg1_align, arg2_align))); | |
4209 | } | |
4210 | #endif | |
03fd9d2c | 4211 | #ifdef HAVE_cmpstrnsi |
6ac5504b | 4212 | /* Try to determine at least one length and call cmpstrnsi. */ |
a0c938f0 | 4213 | if (!insn && HAVE_cmpstrnsi) |
6ac5504b | 4214 | { |
4215 | tree len; | |
4216 | rtx arg3_rtx; | |
4217 | ||
a0c938f0 | 4218 | enum machine_mode insn_mode |
6ac5504b | 4219 | = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode; |
4220 | tree len1 = c_strlen (arg1, 1); | |
4221 | tree len2 = c_strlen (arg2, 1); | |
4222 | ||
4223 | if (len1) | |
4224 | len1 = size_binop (PLUS_EXPR, ssize_int (1), len1); | |
4225 | if (len2) | |
4226 | len2 = size_binop (PLUS_EXPR, ssize_int (1), len2); | |
4227 | ||
4228 | /* If we don't have a constant length for the first, use the length | |
4229 | of the second, if we know it. We don't require a constant for | |
4230 | this case; some cost analysis could be done if both are available | |
4231 | but neither is constant. For now, assume they're equally cheap, | |
4232 | unless one has side effects. If both strings have constant lengths, | |
4233 | use the smaller. */ | |
4234 | ||
4235 | if (!len1) | |
4236 | len = len2; | |
4237 | else if (!len2) | |
4238 | len = len1; | |
4239 | else if (TREE_SIDE_EFFECTS (len1)) | |
4240 | len = len2; | |
4241 | else if (TREE_SIDE_EFFECTS (len2)) | |
4242 | len = len1; | |
4243 | else if (TREE_CODE (len1) != INTEGER_CST) | |
4244 | len = len2; | |
4245 | else if (TREE_CODE (len2) != INTEGER_CST) | |
4246 | len = len1; | |
4247 | else if (tree_int_cst_lt (len1, len2)) | |
4248 | len = len1; | |
4249 | else | |
4250 | len = len2; | |
4251 | ||
4252 | /* If both arguments have side effects, we cannot optimize. */ | |
4253 | if (!len || TREE_SIDE_EFFECTS (len)) | |
6b961939 | 4254 | goto do_libcall; |
53800dbe | 4255 | |
8ec3c5c2 | 4256 | arg3_rtx = expand_normal (len); |
902de8ed | 4257 | |
6ac5504b | 4258 | /* Make a place to write the result of the instruction. */ |
4259 | result = target; | |
4260 | if (! (result != 0 | |
4261 | && REG_P (result) && GET_MODE (result) == insn_mode | |
4262 | && REGNO (result) >= FIRST_PSEUDO_REGISTER)) | |
4263 | result = gen_reg_rtx (insn_mode); | |
53800dbe | 4264 | |
6ac5504b | 4265 | insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx, |
4266 | GEN_INT (MIN (arg1_align, arg2_align))); | |
4267 | } | |
4268 | #endif | |
3f8aefe2 | 4269 | |
6ac5504b | 4270 | if (insn) |
4271 | { | |
4272 | emit_insn (insn); | |
3f8aefe2 | 4273 | |
6ac5504b | 4274 | /* Return the value in the proper mode for this function. */ |
4275 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
4276 | if (GET_MODE (result) == mode) | |
4277 | return result; | |
4278 | if (target == 0) | |
4279 | return convert_to_mode (mode, result, 0); | |
4280 | convert_move (target, result, 0); | |
4281 | return target; | |
4282 | } | |
902de8ed | 4283 | |
6ac5504b | 4284 | /* Expand the library call ourselves using a stabilized argument |
4285 | list to avoid re-evaluating the function's arguments twice. */ | |
2694880e | 4286 | #ifdef HAVE_cmpstrnsi |
6b961939 | 4287 | do_libcall: |
2694880e | 4288 | #endif |
6ac5504b | 4289 | fndecl = get_callee_fndecl (exp); |
c2f47e15 | 4290 | fn = build_call_expr (fndecl, 2, arg1, arg2); |
6ac5504b | 4291 | if (TREE_CODE (fn) == CALL_EXPR) |
4292 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
4293 | return expand_call (fn, target, target == const0_rtx); | |
4294 | } | |
7a3f89b5 | 4295 | #endif |
c2f47e15 | 4296 | return NULL_RTX; |
83d79705 | 4297 | } |
53800dbe | 4298 | |
c2f47e15 | 4299 | /* Expand expression EXP, which is a call to the strncmp builtin. Return |
4300 | NULL_RTX if we failed the caller should emit a normal call, otherwise try to get | |
ed09096d | 4301 | the result in TARGET, if convenient. */ |
27d0c333 | 4302 | |
ed09096d | 4303 | static rtx |
aecda0d6 | 4304 | expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode) |
ed09096d | 4305 | { |
c2f47e15 | 4306 | if (!validate_arglist (exp, |
4307 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
4308 | return NULL_RTX; | |
218bc19e | 4309 | else |
ef6c187e | 4310 | { |
c2f47e15 | 4311 | tree result = fold_builtin_strncmp (CALL_EXPR_ARG (exp, 0), |
4312 | CALL_EXPR_ARG (exp, 1), | |
4313 | CALL_EXPR_ARG (exp, 2)); | |
218bc19e | 4314 | if (result) |
4315 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
ef6c187e | 4316 | } |
ed09096d | 4317 | |
6e34e617 | 4318 | /* If c_strlen can determine an expression for one of the string |
6ac5504b | 4319 | lengths, and it doesn't have side effects, then emit cmpstrnsi |
7a3f89b5 | 4320 | using length MIN(strlen(string)+1, arg3). */ |
6ac5504b | 4321 | #ifdef HAVE_cmpstrnsi |
4322 | if (HAVE_cmpstrnsi) | |
7a3f89b5 | 4323 | { |
4324 | tree len, len1, len2; | |
4325 | rtx arg1_rtx, arg2_rtx, arg3_rtx; | |
4326 | rtx result, insn; | |
0b25db21 | 4327 | tree fndecl, fn; |
c2f47e15 | 4328 | tree arg1 = CALL_EXPR_ARG (exp, 0); |
4329 | tree arg2 = CALL_EXPR_ARG (exp, 1); | |
4330 | tree arg3 = CALL_EXPR_ARG (exp, 2); | |
6f428e8b | 4331 | |
7a3f89b5 | 4332 | int arg1_align |
4333 | = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; | |
4334 | int arg2_align | |
4335 | = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; | |
4336 | enum machine_mode insn_mode | |
6ac5504b | 4337 | = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode; |
bf8e3599 | 4338 | |
681fab1e | 4339 | len1 = c_strlen (arg1, 1); |
4340 | len2 = c_strlen (arg2, 1); | |
7a3f89b5 | 4341 | |
4342 | if (len1) | |
4343 | len1 = size_binop (PLUS_EXPR, ssize_int (1), len1); | |
4344 | if (len2) | |
4345 | len2 = size_binop (PLUS_EXPR, ssize_int (1), len2); | |
4346 | ||
4347 | /* If we don't have a constant length for the first, use the length | |
4348 | of the second, if we know it. We don't require a constant for | |
4349 | this case; some cost analysis could be done if both are available | |
4350 | but neither is constant. For now, assume they're equally cheap, | |
4351 | unless one has side effects. If both strings have constant lengths, | |
4352 | use the smaller. */ | |
4353 | ||
4354 | if (!len1) | |
4355 | len = len2; | |
4356 | else if (!len2) | |
4357 | len = len1; | |
4358 | else if (TREE_SIDE_EFFECTS (len1)) | |
4359 | len = len2; | |
4360 | else if (TREE_SIDE_EFFECTS (len2)) | |
4361 | len = len1; | |
4362 | else if (TREE_CODE (len1) != INTEGER_CST) | |
4363 | len = len2; | |
4364 | else if (TREE_CODE (len2) != INTEGER_CST) | |
4365 | len = len1; | |
4366 | else if (tree_int_cst_lt (len1, len2)) | |
4367 | len = len1; | |
4368 | else | |
4369 | len = len2; | |
6e34e617 | 4370 | |
7a3f89b5 | 4371 | /* If both arguments have side effects, we cannot optimize. */ |
4372 | if (!len || TREE_SIDE_EFFECTS (len)) | |
c2f47e15 | 4373 | return NULL_RTX; |
bf8e3599 | 4374 | |
7a3f89b5 | 4375 | /* The actual new length parameter is MIN(len,arg3). */ |
49d00087 | 4376 | len = fold_build2 (MIN_EXPR, TREE_TYPE (len), len, |
4377 | fold_convert (TREE_TYPE (len), arg3)); | |
7a3f89b5 | 4378 | |
4379 | /* If we don't have POINTER_TYPE, call the function. */ | |
4380 | if (arg1_align == 0 || arg2_align == 0) | |
c2f47e15 | 4381 | return NULL_RTX; |
7a3f89b5 | 4382 | |
4383 | /* Make a place to write the result of the instruction. */ | |
4384 | result = target; | |
4385 | if (! (result != 0 | |
8ad4c111 | 4386 | && REG_P (result) && GET_MODE (result) == insn_mode |
7a3f89b5 | 4387 | && REGNO (result) >= FIRST_PSEUDO_REGISTER)) |
4388 | result = gen_reg_rtx (insn_mode); | |
4389 | ||
6ac5504b | 4390 | /* Stabilize the arguments in case gen_cmpstrnsi fails. */ |
4ee9c684 | 4391 | arg1 = builtin_save_expr (arg1); |
4392 | arg2 = builtin_save_expr (arg2); | |
4393 | len = builtin_save_expr (len); | |
3f8aefe2 | 4394 | |
d8ae1baa | 4395 | arg1_rtx = get_memory_rtx (arg1, len); |
4396 | arg2_rtx = get_memory_rtx (arg2, len); | |
8ec3c5c2 | 4397 | arg3_rtx = expand_normal (len); |
6ac5504b | 4398 | insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx, |
4399 | GEN_INT (MIN (arg1_align, arg2_align))); | |
3f8aefe2 | 4400 | if (insn) |
4401 | { | |
4402 | emit_insn (insn); | |
4403 | ||
4404 | /* Return the value in the proper mode for this function. */ | |
4405 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
4406 | if (GET_MODE (result) == mode) | |
4407 | return result; | |
4408 | if (target == 0) | |
4409 | return convert_to_mode (mode, result, 0); | |
4410 | convert_move (target, result, 0); | |
4411 | return target; | |
4412 | } | |
7a3f89b5 | 4413 | |
3f8aefe2 | 4414 | /* Expand the library call ourselves using a stabilized argument |
4415 | list to avoid re-evaluating the function's arguments twice. */ | |
3f8aefe2 | 4416 | fndecl = get_callee_fndecl (exp); |
c2f47e15 | 4417 | fn = build_call_expr (fndecl, 3, arg1, arg2, len); |
0b25db21 | 4418 | if (TREE_CODE (fn) == CALL_EXPR) |
4419 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
4420 | return expand_call (fn, target, target == const0_rtx); | |
7a3f89b5 | 4421 | } |
4422 | #endif | |
c2f47e15 | 4423 | return NULL_RTX; |
ed09096d | 4424 | } |
4425 | ||
49f0327b | 4426 | /* Expand expression EXP, which is a call to the strcat builtin. |
c2f47e15 | 4427 | Return NULL_RTX if we failed the caller should emit a normal call, |
49f0327b | 4428 | otherwise try to get the result in TARGET, if convenient. */ |
27d0c333 | 4429 | |
49f0327b | 4430 | static rtx |
c2f47e15 | 4431 | expand_builtin_strcat (tree fndecl, tree exp, rtx target, enum machine_mode mode) |
49f0327b | 4432 | { |
c2f47e15 | 4433 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
4434 | return NULL_RTX; | |
49f0327b | 4435 | else |
4436 | { | |
c2f47e15 | 4437 | tree dst = CALL_EXPR_ARG (exp, 0); |
4438 | tree src = CALL_EXPR_ARG (exp, 1); | |
49f0327b | 4439 | const char *p = c_getstr (src); |
4440 | ||
1d89860b | 4441 | /* If the string length is zero, return the dst parameter. */ |
a0c938f0 | 4442 | if (p && *p == '\0') |
1d89860b | 4443 | return expand_expr (dst, target, mode, EXPAND_NORMAL); |
a0c938f0 | 4444 | |
1d89860b | 4445 | if (!optimize_size) |
ca71f89b | 4446 | { |
1d89860b | 4447 | /* See if we can store by pieces into (dst + strlen(dst)). */ |
4448 | tree newsrc, newdst, | |
4449 | strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN]; | |
4450 | rtx insns; | |
4451 | ||
4452 | /* Stabilize the argument list. */ | |
4453 | newsrc = builtin_save_expr (src); | |
1d89860b | 4454 | dst = builtin_save_expr (dst); |
4455 | ||
4456 | start_sequence (); | |
4457 | ||
4458 | /* Create strlen (dst). */ | |
c2f47e15 | 4459 | newdst = build_call_expr (strlen_fn, 1, dst); |
0de36bdb | 4460 | /* Create (dst p+ strlen (dst)). */ |
1d89860b | 4461 | |
0de36bdb | 4462 | newdst = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dst), dst, newdst); |
1d89860b | 4463 | newdst = builtin_save_expr (newdst); |
1d89860b | 4464 | |
c2f47e15 | 4465 | if (!expand_builtin_strcpy_args (fndecl, newdst, newsrc, target, mode)) |
ca71f89b | 4466 | { |
1d89860b | 4467 | end_sequence (); /* Stop sequence. */ |
c2f47e15 | 4468 | return NULL_RTX; |
ca71f89b | 4469 | } |
a0c938f0 | 4470 | |
1d89860b | 4471 | /* Output the entire sequence. */ |
4472 | insns = get_insns (); | |
4473 | end_sequence (); | |
4474 | emit_insn (insns); | |
a0c938f0 | 4475 | |
1d89860b | 4476 | return expand_expr (dst, target, mode, EXPAND_NORMAL); |
ca71f89b | 4477 | } |
49f0327b | 4478 | |
c2f47e15 | 4479 | return NULL_RTX; |
49f0327b | 4480 | } |
4481 | } | |
4482 | ||
4483 | /* Expand expression EXP, which is a call to the strncat builtin. | |
c2f47e15 | 4484 | Return NULL_RTX if we failed the caller should emit a normal call, |
49f0327b | 4485 | otherwise try to get the result in TARGET, if convenient. */ |
27d0c333 | 4486 | |
49f0327b | 4487 | static rtx |
c2f47e15 | 4488 | expand_builtin_strncat (tree exp, rtx target, enum machine_mode mode) |
49f0327b | 4489 | { |
c2f47e15 | 4490 | if (validate_arglist (exp, |
4491 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
49f0327b | 4492 | { |
c2f47e15 | 4493 | tree result = fold_builtin_strncat (CALL_EXPR_ARG (exp, 0), |
4494 | CALL_EXPR_ARG (exp, 1), | |
4495 | CALL_EXPR_ARG (exp, 2)); | |
2c84ee37 | 4496 | if (result) |
4497 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
49f0327b | 4498 | } |
c2f47e15 | 4499 | return NULL_RTX; |
49f0327b | 4500 | } |
4501 | ||
4502 | /* Expand expression EXP, which is a call to the strspn builtin. | |
c2f47e15 | 4503 | Return NULL_RTX if we failed the caller should emit a normal call, |
49f0327b | 4504 | otherwise try to get the result in TARGET, if convenient. */ |
27d0c333 | 4505 | |
49f0327b | 4506 | static rtx |
c2f47e15 | 4507 | expand_builtin_strspn (tree exp, rtx target, enum machine_mode mode) |
49f0327b | 4508 | { |
c2f47e15 | 4509 | if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
49f0327b | 4510 | { |
c2f47e15 | 4511 | tree result = fold_builtin_strspn (CALL_EXPR_ARG (exp, 0), |
4512 | CALL_EXPR_ARG (exp, 1)); | |
2c84ee37 | 4513 | if (result) |
4514 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
49f0327b | 4515 | } |
c2f47e15 | 4516 | return NULL_RTX; |
49f0327b | 4517 | } |
4518 | ||
4519 | /* Expand expression EXP, which is a call to the strcspn builtin. | |
c2f47e15 | 4520 | Return NULL_RTX if we failed the caller should emit a normal call, |
49f0327b | 4521 | otherwise try to get the result in TARGET, if convenient. */ |
27d0c333 | 4522 | |
49f0327b | 4523 | static rtx |
c2f47e15 | 4524 | expand_builtin_strcspn (tree exp, rtx target, enum machine_mode mode) |
49f0327b | 4525 | { |
c2f47e15 | 4526 | if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
49f0327b | 4527 | { |
c2f47e15 | 4528 | tree result = fold_builtin_strcspn (CALL_EXPR_ARG (exp, 0), |
4529 | CALL_EXPR_ARG (exp, 1)); | |
2c84ee37 | 4530 | if (result) |
4531 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
49f0327b | 4532 | } |
c2f47e15 | 4533 | return NULL_RTX; |
49f0327b | 4534 | } |
4535 | ||
a66c9326 | 4536 | /* Expand a call to __builtin_saveregs, generating the result in TARGET, |
4537 | if that's convenient. */ | |
902de8ed | 4538 | |
a66c9326 | 4539 | rtx |
aecda0d6 | 4540 | expand_builtin_saveregs (void) |
53800dbe | 4541 | { |
a66c9326 | 4542 | rtx val, seq; |
53800dbe | 4543 | |
4544 | /* Don't do __builtin_saveregs more than once in a function. | |
4545 | Save the result of the first call and reuse it. */ | |
4546 | if (saveregs_value != 0) | |
4547 | return saveregs_value; | |
53800dbe | 4548 | |
a66c9326 | 4549 | /* When this function is called, it means that registers must be |
4550 | saved on entry to this function. So we migrate the call to the | |
4551 | first insn of this function. */ | |
4552 | ||
4553 | start_sequence (); | |
53800dbe | 4554 | |
a66c9326 | 4555 | /* Do whatever the machine needs done in this case. */ |
45550790 | 4556 | val = targetm.calls.expand_builtin_saveregs (); |
53800dbe | 4557 | |
a66c9326 | 4558 | seq = get_insns (); |
4559 | end_sequence (); | |
53800dbe | 4560 | |
a66c9326 | 4561 | saveregs_value = val; |
53800dbe | 4562 | |
31d3e01c | 4563 | /* Put the insns after the NOTE that starts the function. If this |
4564 | is inside a start_sequence, make the outer-level insn chain current, so | |
a66c9326 | 4565 | the code is placed at the start of the function. */ |
4566 | push_topmost_sequence (); | |
0ec80471 | 4567 | emit_insn_after (seq, entry_of_function ()); |
a66c9326 | 4568 | pop_topmost_sequence (); |
4569 | ||
4570 | return val; | |
53800dbe | 4571 | } |
4572 | ||
4573 | /* __builtin_args_info (N) returns word N of the arg space info | |
4574 | for the current function. The number and meanings of words | |
4575 | is controlled by the definition of CUMULATIVE_ARGS. */ | |
f7c44134 | 4576 | |
53800dbe | 4577 | static rtx |
c2f47e15 | 4578 | expand_builtin_args_info (tree exp) |
53800dbe | 4579 | { |
53800dbe | 4580 | int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int); |
abe32cce | 4581 | int *word_ptr = (int *) &crtl->args.info; |
53800dbe | 4582 | |
64db345d | 4583 | gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0); |
53800dbe | 4584 | |
c2f47e15 | 4585 | if (call_expr_nargs (exp) != 0) |
53800dbe | 4586 | { |
c2f47e15 | 4587 | if (!host_integerp (CALL_EXPR_ARG (exp, 0), 0)) |
eb586f2c | 4588 | error ("argument of %<__builtin_args_info%> must be constant"); |
53800dbe | 4589 | else |
4590 | { | |
c2f47e15 | 4591 | HOST_WIDE_INT wordnum = tree_low_cst (CALL_EXPR_ARG (exp, 0), 0); |
53800dbe | 4592 | |
27d0c333 | 4593 | if (wordnum < 0 || wordnum >= nwords) |
eb586f2c | 4594 | error ("argument of %<__builtin_args_info%> out of range"); |
53800dbe | 4595 | else |
4596 | return GEN_INT (word_ptr[wordnum]); | |
4597 | } | |
4598 | } | |
4599 | else | |
eb586f2c | 4600 | error ("missing argument in %<__builtin_args_info%>"); |
53800dbe | 4601 | |
4602 | return const0_rtx; | |
53800dbe | 4603 | } |
4604 | ||
79012a9d | 4605 | /* Expand a call to __builtin_next_arg. */ |
27d0c333 | 4606 | |
53800dbe | 4607 | static rtx |
79012a9d | 4608 | expand_builtin_next_arg (void) |
53800dbe | 4609 | { |
79012a9d | 4610 | /* Checking arguments is already done in fold_builtin_next_arg |
4611 | that must be called before this function. */ | |
940ddc5c | 4612 | return expand_binop (ptr_mode, add_optab, |
abe32cce | 4613 | crtl->args.internal_arg_pointer, |
4614 | crtl->args.arg_offset_rtx, | |
53800dbe | 4615 | NULL_RTX, 0, OPTAB_LIB_WIDEN); |
4616 | } | |
4617 | ||
a66c9326 | 4618 | /* Make it easier for the backends by protecting the valist argument |
4619 | from multiple evaluations. */ | |
4620 | ||
4621 | static tree | |
aecda0d6 | 4622 | stabilize_va_list (tree valist, int needs_lvalue) |
a66c9326 | 4623 | { |
11a61dea | 4624 | if (TREE_CODE (va_list_type_node) == ARRAY_TYPE) |
a66c9326 | 4625 | { |
2d47cc32 | 4626 | if (TREE_SIDE_EFFECTS (valist)) |
4627 | valist = save_expr (valist); | |
11a61dea | 4628 | |
2d47cc32 | 4629 | /* For this case, the backends will be expecting a pointer to |
4630 | TREE_TYPE (va_list_type_node), but it's possible we've | |
4631 | actually been given an array (an actual va_list_type_node). | |
4632 | So fix it. */ | |
4633 | if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE) | |
8a15c04a | 4634 | { |
bf8e3599 | 4635 | tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node)); |
fcdd3ab3 | 4636 | valist = build_fold_addr_expr_with_type (valist, p1); |
8a15c04a | 4637 | } |
a66c9326 | 4638 | } |
11a61dea | 4639 | else |
a66c9326 | 4640 | { |
2d47cc32 | 4641 | tree pt; |
11a61dea | 4642 | |
2d47cc32 | 4643 | if (! needs_lvalue) |
4644 | { | |
11a61dea | 4645 | if (! TREE_SIDE_EFFECTS (valist)) |
4646 | return valist; | |
bf8e3599 | 4647 | |
11a61dea | 4648 | pt = build_pointer_type (va_list_type_node); |
49d00087 | 4649 | valist = fold_build1 (ADDR_EXPR, pt, valist); |
a66c9326 | 4650 | TREE_SIDE_EFFECTS (valist) = 1; |
a66c9326 | 4651 | } |
2d47cc32 | 4652 | |
11a61dea | 4653 | if (TREE_SIDE_EFFECTS (valist)) |
2d47cc32 | 4654 | valist = save_expr (valist); |
fcdd3ab3 | 4655 | valist = build_fold_indirect_ref (valist); |
a66c9326 | 4656 | } |
4657 | ||
4658 | return valist; | |
4659 | } | |
4660 | ||
2e15d750 | 4661 | /* The "standard" definition of va_list is void*. */ |
4662 | ||
4663 | tree | |
4664 | std_build_builtin_va_list (void) | |
4665 | { | |
4666 | return ptr_type_node; | |
4667 | } | |
4668 | ||
a66c9326 | 4669 | /* The "standard" implementation of va_start: just assign `nextarg' to |
4670 | the variable. */ | |
27d0c333 | 4671 | |
a66c9326 | 4672 | void |
aecda0d6 | 4673 | std_expand_builtin_va_start (tree valist, rtx nextarg) |
a66c9326 | 4674 | { |
f03c17bc | 4675 | rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE); |
4676 | convert_move (va_r, nextarg, 0); | |
a66c9326 | 4677 | } |
4678 | ||
c2f47e15 | 4679 | /* Expand EXP, a call to __builtin_va_start. */ |
27d0c333 | 4680 | |
a66c9326 | 4681 | static rtx |
c2f47e15 | 4682 | expand_builtin_va_start (tree exp) |
a66c9326 | 4683 | { |
4684 | rtx nextarg; | |
c2f47e15 | 4685 | tree valist; |
a66c9326 | 4686 | |
c2f47e15 | 4687 | if (call_expr_nargs (exp) < 2) |
cb166087 | 4688 | { |
4689 | error ("too few arguments to function %<va_start%>"); | |
4690 | return const0_rtx; | |
4691 | } | |
a66c9326 | 4692 | |
c2f47e15 | 4693 | if (fold_builtin_next_arg (exp, true)) |
79012a9d | 4694 | return const0_rtx; |
7c2f0500 | 4695 | |
79012a9d | 4696 | nextarg = expand_builtin_next_arg (); |
c2f47e15 | 4697 | valist = stabilize_va_list (CALL_EXPR_ARG (exp, 0), 1); |
a66c9326 | 4698 | |
8a58ed0a | 4699 | if (targetm.expand_builtin_va_start) |
4700 | targetm.expand_builtin_va_start (valist, nextarg); | |
4701 | else | |
4702 | std_expand_builtin_va_start (valist, nextarg); | |
a66c9326 | 4703 | |
4704 | return const0_rtx; | |
4705 | } | |
4706 | ||
a66c9326 | 4707 | /* The "standard" implementation of va_arg: read the value from the |
4708 | current (padded) address and increment by the (padded) size. */ | |
f7c44134 | 4709 | |
e0eca1fa | 4710 | tree |
4711 | std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p) | |
fcdd3ab3 | 4712 | { |
52cc2eeb | 4713 | tree addr, t, type_size, rounded_size, valist_tmp; |
b8e8e485 | 4714 | unsigned HOST_WIDE_INT align, boundary; |
bef380a4 | 4715 | bool indirect; |
52cc2eeb | 4716 | |
4717 | #ifdef ARGS_GROW_DOWNWARD | |
4718 | /* All of the alignment and movement below is for args-grow-up machines. | |
4719 | As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all | |
4720 | implement their own specialized gimplify_va_arg_expr routines. */ | |
64db345d | 4721 | gcc_unreachable (); |
52cc2eeb | 4722 | #endif |
fcdd3ab3 | 4723 | |
bef380a4 | 4724 | indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false); |
4725 | if (indirect) | |
4726 | type = build_pointer_type (type); | |
4727 | ||
52cc2eeb | 4728 | align = PARM_BOUNDARY / BITS_PER_UNIT; |
bcff3604 | 4729 | boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT; |
fcdd3ab3 | 4730 | |
52cc2eeb | 4731 | /* Hoist the valist value into a temporary for the moment. */ |
ecdeeb37 | 4732 | valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL); |
4733 | ||
fcdd3ab3 | 4734 | /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually |
4735 | requires greater alignment, we must perform dynamic alignment. */ | |
85c1f587 | 4736 | if (boundary > align |
4737 | && !integer_zerop (TYPE_SIZE (type))) | |
fcdd3ab3 | 4738 | { |
41076ef6 | 4739 | t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp, |
0de36bdb | 4740 | fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (valist), |
4741 | valist_tmp, size_int (boundary - 1))); | |
52cc2eeb | 4742 | gimplify_and_add (t, pre_p); |
4743 | ||
0de36bdb | 4744 | t = fold_convert (sizetype, valist_tmp); |
41076ef6 | 4745 | t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp, |
0de36bdb | 4746 | fold_convert (TREE_TYPE (valist), |
4747 | fold_build2 (BIT_AND_EXPR, sizetype, t, | |
4748 | size_int (-boundary)))); | |
ecdeeb37 | 4749 | gimplify_and_add (t, pre_p); |
fcdd3ab3 | 4750 | } |
c5dc0c32 | 4751 | else |
4752 | boundary = align; | |
4753 | ||
4754 | /* If the actual alignment is less than the alignment of the type, | |
4755 | adjust the type accordingly so that we don't assume strict alignment | |
f0b5f617 | 4756 | when dereferencing the pointer. */ |
c5dc0c32 | 4757 | boundary *= BITS_PER_UNIT; |
4758 | if (boundary < TYPE_ALIGN (type)) | |
4759 | { | |
4760 | type = build_variant_type_copy (type); | |
4761 | TYPE_ALIGN (type) = boundary; | |
4762 | } | |
fcdd3ab3 | 4763 | |
bcff3604 | 4764 | /* Compute the rounded size of the type. */ |
52cc2eeb | 4765 | type_size = size_in_bytes (type); |
4766 | rounded_size = round_up (type_size, align); | |
4767 | ||
fcdd3ab3 | 4768 | /* Reduce rounded_size so it's sharable with the postqueue. */ |
4769 | gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue); | |
4770 | ||
4771 | /* Get AP. */ | |
ecdeeb37 | 4772 | addr = valist_tmp; |
52cc2eeb | 4773 | if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size)) |
fcdd3ab3 | 4774 | { |
4775 | /* Small args are padded downward. */ | |
49d00087 | 4776 | t = fold_build2 (GT_EXPR, sizetype, rounded_size, size_int (align)); |
4777 | t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node, | |
4778 | size_binop (MINUS_EXPR, rounded_size, type_size)); | |
0de36bdb | 4779 | addr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (addr), addr, t); |
fcdd3ab3 | 4780 | } |
4781 | ||
fcdd3ab3 | 4782 | /* Compute new value for AP. */ |
0de36bdb | 4783 | t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (valist), valist_tmp, rounded_size); |
41076ef6 | 4784 | t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t); |
ecdeeb37 | 4785 | gimplify_and_add (t, pre_p); |
e0eca1fa | 4786 | |
4787 | addr = fold_convert (build_pointer_type (type), addr); | |
fcdd3ab3 | 4788 | |
bef380a4 | 4789 | if (indirect) |
f2462d6c | 4790 | addr = build_va_arg_indirect_ref (addr); |
4791 | ||
4792 | return build_va_arg_indirect_ref (addr); | |
4793 | } | |
a0930a69 | 4794 | |
f2462d6c | 4795 | /* Build an indirect-ref expression over the given TREE, which represents a |
4796 | piece of a va_arg() expansion. */ | |
4797 | tree | |
4798 | build_va_arg_indirect_ref (tree addr) | |
4799 | { | |
a0930a69 | 4800 | addr = build_fold_indirect_ref (addr); |
f2462d6c | 4801 | |
a0930a69 | 4802 | if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */ |
4803 | mf_mark (addr); | |
bef380a4 | 4804 | |
a0930a69 | 4805 | return addr; |
433006a9 | 4806 | } |
4807 | ||
fcdd3ab3 | 4808 | /* Return a dummy expression of type TYPE in order to keep going after an |
4809 | error. */ | |
4810 | ||
4811 | static tree | |
4812 | dummy_object (tree type) | |
4813 | { | |
535664e3 | 4814 | tree t = build_int_cst (build_pointer_type (type), 0); |
fcdd3ab3 | 4815 | return build1 (INDIRECT_REF, type, t); |
4816 | } | |
4817 | ||
2799a2b7 | 4818 | /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a |
4819 | builtin function, but a very special sort of operator. */ | |
fcdd3ab3 | 4820 | |
4821 | enum gimplify_status | |
4822 | gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p) | |
4823 | { | |
4824 | tree promoted_type, want_va_type, have_va_type; | |
4825 | tree valist = TREE_OPERAND (*expr_p, 0); | |
4826 | tree type = TREE_TYPE (*expr_p); | |
4827 | tree t; | |
4828 | ||
4829 | /* Verify that valist is of the proper type. */ | |
fcdd3ab3 | 4830 | want_va_type = va_list_type_node; |
4831 | have_va_type = TREE_TYPE (valist); | |
f43dda35 | 4832 | |
4833 | if (have_va_type == error_mark_node) | |
4834 | return GS_ERROR; | |
4835 | ||
fcdd3ab3 | 4836 | if (TREE_CODE (want_va_type) == ARRAY_TYPE) |
4837 | { | |
4838 | /* If va_list is an array type, the argument may have decayed | |
4839 | to a pointer type, e.g. by being passed to another function. | |
a0c938f0 | 4840 | In that case, unwrap both types so that we can compare the |
fcdd3ab3 | 4841 | underlying records. */ |
4842 | if (TREE_CODE (have_va_type) == ARRAY_TYPE | |
552752f7 | 4843 | || POINTER_TYPE_P (have_va_type)) |
fcdd3ab3 | 4844 | { |
4845 | want_va_type = TREE_TYPE (want_va_type); | |
4846 | have_va_type = TREE_TYPE (have_va_type); | |
4847 | } | |
4848 | } | |
4849 | ||
4850 | if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type)) | |
4851 | { | |
eb586f2c | 4852 | error ("first argument to %<va_arg%> not of type %<va_list%>"); |
e0eca1fa | 4853 | return GS_ERROR; |
fcdd3ab3 | 4854 | } |
4855 | ||
4856 | /* Generate a diagnostic for requesting data of a type that cannot | |
4857 | be passed through `...' due to type promotion at the call site. */ | |
4858 | else if ((promoted_type = lang_hooks.types.type_promotes_to (type)) | |
4859 | != type) | |
4860 | { | |
4861 | static bool gave_help; | |
4862 | ||
4863 | /* Unfortunately, this is merely undefined, rather than a constraint | |
4864 | violation, so we cannot make this an error. If this call is never | |
4865 | executed, the program is still strictly conforming. */ | |
c3ceba8e | 4866 | warning (0, "%qT is promoted to %qT when passed through %<...%>", |
fcdd3ab3 | 4867 | type, promoted_type); |
4868 | if (! gave_help) | |
4869 | { | |
4870 | gave_help = true; | |
2006d7dc | 4871 | inform ("(so you should pass %qT not %qT to %<va_arg%>)", |
fcdd3ab3 | 4872 | promoted_type, type); |
4873 | } | |
4874 | ||
4875 | /* We can, however, treat "undefined" any way we please. | |
4876 | Call abort to encourage the user to fix the program. */ | |
4877 | inform ("if this code is reached, the program will abort"); | |
c2f47e15 | 4878 | t = build_call_expr (implicit_built_in_decls[BUILT_IN_TRAP], 0); |
fcdd3ab3 | 4879 | append_to_statement_list (t, pre_p); |
4880 | ||
4881 | /* This is dead code, but go ahead and finish so that the | |
4882 | mode of the result comes out right. */ | |
4883 | *expr_p = dummy_object (type); | |
4884 | return GS_ALL_DONE; | |
4885 | } | |
4886 | else | |
4887 | { | |
4888 | /* Make it easier for the backends by protecting the valist argument | |
a0c938f0 | 4889 | from multiple evaluations. */ |
e0eca1fa | 4890 | if (TREE_CODE (va_list_type_node) == ARRAY_TYPE) |
4891 | { | |
4892 | /* For this case, the backends will be expecting a pointer to | |
4893 | TREE_TYPE (va_list_type_node), but it's possible we've | |
4894 | actually been given an array (an actual va_list_type_node). | |
4895 | So fix it. */ | |
4896 | if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE) | |
4897 | { | |
4898 | tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node)); | |
4899 | valist = build_fold_addr_expr_with_type (valist, p1); | |
4900 | } | |
4901 | gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue); | |
4902 | } | |
4903 | else | |
4904 | gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue); | |
fcdd3ab3 | 4905 | |
17a6380d | 4906 | if (!targetm.gimplify_va_arg_expr) |
89f18f73 | 4907 | /* FIXME:Once most targets are converted we should merely |
4908 | assert this is non-null. */ | |
fcdd3ab3 | 4909 | return GS_ALL_DONE; |
4910 | ||
17a6380d | 4911 | *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p); |
fcdd3ab3 | 4912 | return GS_OK; |
4913 | } | |
4914 | } | |
4915 | ||
c2f47e15 | 4916 | /* Expand EXP, a call to __builtin_va_end. */ |
f7c44134 | 4917 | |
a66c9326 | 4918 | static rtx |
c2f47e15 | 4919 | expand_builtin_va_end (tree exp) |
a66c9326 | 4920 | { |
c2f47e15 | 4921 | tree valist = CALL_EXPR_ARG (exp, 0); |
8a15c04a | 4922 | |
8a15c04a | 4923 | /* Evaluate for side effects, if needed. I hate macros that don't |
4924 | do that. */ | |
4925 | if (TREE_SIDE_EFFECTS (valist)) | |
4926 | expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
a66c9326 | 4927 | |
4928 | return const0_rtx; | |
4929 | } | |
4930 | ||
c2f47e15 | 4931 | /* Expand EXP, a call to __builtin_va_copy. We do this as a |
a66c9326 | 4932 | builtin rather than just as an assignment in stdarg.h because of the |
4933 | nastiness of array-type va_list types. */ | |
f7c44134 | 4934 | |
a66c9326 | 4935 | static rtx |
c2f47e15 | 4936 | expand_builtin_va_copy (tree exp) |
a66c9326 | 4937 | { |
4938 | tree dst, src, t; | |
4939 | ||
c2f47e15 | 4940 | dst = CALL_EXPR_ARG (exp, 0); |
4941 | src = CALL_EXPR_ARG (exp, 1); | |
a66c9326 | 4942 | |
4943 | dst = stabilize_va_list (dst, 1); | |
4944 | src = stabilize_va_list (src, 0); | |
4945 | ||
4946 | if (TREE_CODE (va_list_type_node) != ARRAY_TYPE) | |
4947 | { | |
41076ef6 | 4948 | t = build2 (MODIFY_EXPR, va_list_type_node, dst, src); |
a66c9326 | 4949 | TREE_SIDE_EFFECTS (t) = 1; |
4950 | expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
4951 | } | |
4952 | else | |
4953 | { | |
11a61dea | 4954 | rtx dstb, srcb, size; |
4955 | ||
4956 | /* Evaluate to pointers. */ | |
4957 | dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL); | |
4958 | srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL); | |
4959 | size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX, | |
4960 | VOIDmode, EXPAND_NORMAL); | |
4961 | ||
85d654dd | 4962 | dstb = convert_memory_address (Pmode, dstb); |
4963 | srcb = convert_memory_address (Pmode, srcb); | |
726ec87c | 4964 | |
11a61dea | 4965 | /* "Dereference" to BLKmode memories. */ |
4966 | dstb = gen_rtx_MEM (BLKmode, dstb); | |
ab6ab77e | 4967 | set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst)))); |
2a631e19 | 4968 | set_mem_align (dstb, TYPE_ALIGN (va_list_type_node)); |
11a61dea | 4969 | srcb = gen_rtx_MEM (BLKmode, srcb); |
ab6ab77e | 4970 | set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src)))); |
2a631e19 | 4971 | set_mem_align (srcb, TYPE_ALIGN (va_list_type_node)); |
11a61dea | 4972 | |
4973 | /* Copy. */ | |
0378dbdc | 4974 | emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL); |
a66c9326 | 4975 | } |
4976 | ||
4977 | return const0_rtx; | |
4978 | } | |
4979 | ||
53800dbe | 4980 | /* Expand a call to one of the builtin functions __builtin_frame_address or |
4981 | __builtin_return_address. */ | |
27d0c333 | 4982 | |
53800dbe | 4983 | static rtx |
c2f47e15 | 4984 | expand_builtin_frame_address (tree fndecl, tree exp) |
53800dbe | 4985 | { |
53800dbe | 4986 | /* The argument must be a nonnegative integer constant. |
4987 | It counts the number of frames to scan up the stack. | |
4988 | The value is the return address saved in that frame. */ | |
c2f47e15 | 4989 | if (call_expr_nargs (exp) == 0) |
53800dbe | 4990 | /* Warning about missing arg was already issued. */ |
4991 | return const0_rtx; | |
c2f47e15 | 4992 | else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1)) |
53800dbe | 4993 | { |
4994 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS) | |
07e3a3d2 | 4995 | error ("invalid argument to %<__builtin_frame_address%>"); |
53800dbe | 4996 | else |
07e3a3d2 | 4997 | error ("invalid argument to %<__builtin_return_address%>"); |
53800dbe | 4998 | return const0_rtx; |
4999 | } | |
5000 | else | |
5001 | { | |
27d0c333 | 5002 | rtx tem |
5003 | = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), | |
c2f47e15 | 5004 | tree_low_cst (CALL_EXPR_ARG (exp, 0), 1)); |
53800dbe | 5005 | |
5006 | /* Some ports cannot access arbitrary stack frames. */ | |
5007 | if (tem == NULL) | |
5008 | { | |
5009 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS) | |
c3ceba8e | 5010 | warning (0, "unsupported argument to %<__builtin_frame_address%>"); |
53800dbe | 5011 | else |
c3ceba8e | 5012 | warning (0, "unsupported argument to %<__builtin_return_address%>"); |
53800dbe | 5013 | return const0_rtx; |
5014 | } | |
5015 | ||
5016 | /* For __builtin_frame_address, return what we've got. */ | |
5017 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS) | |
5018 | return tem; | |
5019 | ||
8ad4c111 | 5020 | if (!REG_P (tem) |
53800dbe | 5021 | && ! CONSTANT_P (tem)) |
5022 | tem = copy_to_mode_reg (Pmode, tem); | |
5023 | return tem; | |
5024 | } | |
5025 | } | |
5026 | ||
c2f47e15 | 5027 | /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if |
53800dbe | 5028 | we failed and the caller should emit a normal call, otherwise try to get |
5029 | the result in TARGET, if convenient. */ | |
15c6cf6b | 5030 | |
53800dbe | 5031 | static rtx |
c2f47e15 | 5032 | expand_builtin_alloca (tree exp, rtx target) |
53800dbe | 5033 | { |
5034 | rtx op0; | |
15c6cf6b | 5035 | rtx result; |
53800dbe | 5036 | |
4ee9c684 | 5037 | /* In -fmudflap-instrumented code, alloca() and __builtin_alloca() |
5038 | should always expand to function calls. These can be intercepted | |
5039 | in libmudflap. */ | |
5040 | if (flag_mudflap) | |
c2f47e15 | 5041 | return NULL_RTX; |
4ee9c684 | 5042 | |
c2f47e15 | 5043 | if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE)) |
5044 | return NULL_RTX; | |
53800dbe | 5045 | |
5046 | /* Compute the argument. */ | |
c2f47e15 | 5047 | op0 = expand_normal (CALL_EXPR_ARG (exp, 0)); |
53800dbe | 5048 | |
5049 | /* Allocate the desired space. */ | |
15c6cf6b | 5050 | result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT); |
85d654dd | 5051 | result = convert_memory_address (ptr_mode, result); |
15c6cf6b | 5052 | |
5053 | return result; | |
53800dbe | 5054 | } |
5055 | ||
c2f47e15 | 5056 | /* Expand a call to a bswap builtin with argument ARG0. MODE |
42791117 | 5057 | is the mode to expand with. */ |
5058 | ||
5059 | static rtx | |
c2f47e15 | 5060 | expand_builtin_bswap (tree exp, rtx target, rtx subtarget) |
42791117 | 5061 | { |
5062 | enum machine_mode mode; | |
5063 | tree arg; | |
5064 | rtx op0; | |
5065 | ||
c2f47e15 | 5066 | if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE)) |
5067 | return NULL_RTX; | |
42791117 | 5068 | |
c2f47e15 | 5069 | arg = CALL_EXPR_ARG (exp, 0); |
42791117 | 5070 | mode = TYPE_MODE (TREE_TYPE (arg)); |
1db6d067 | 5071 | op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL); |
42791117 | 5072 | |
5073 | target = expand_unop (mode, bswap_optab, op0, target, 1); | |
5074 | ||
5075 | gcc_assert (target); | |
5076 | ||
5077 | return convert_to_mode (mode, target, 0); | |
5078 | } | |
5079 | ||
c2f47e15 | 5080 | /* Expand a call to a unary builtin in EXP. |
5081 | Return NULL_RTX if a normal call should be emitted rather than expanding the | |
53800dbe | 5082 | function in-line. If convenient, the result should be placed in TARGET. |
5083 | SUBTARGET may be used as the target for computing one of EXP's operands. */ | |
15c6cf6b | 5084 | |
53800dbe | 5085 | static rtx |
c2f47e15 | 5086 | expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target, |
aecda0d6 | 5087 | rtx subtarget, optab op_optab) |
53800dbe | 5088 | { |
5089 | rtx op0; | |
c2f47e15 | 5090 | |
5091 | if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE)) | |
5092 | return NULL_RTX; | |
53800dbe | 5093 | |
5094 | /* Compute the argument. */ | |
1db6d067 | 5095 | op0 = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget, |
5096 | VOIDmode, EXPAND_NORMAL); | |
6a08d0ab | 5097 | /* Compute op, into TARGET if possible. |
53800dbe | 5098 | Set TARGET to wherever the result comes back. */ |
c2f47e15 | 5099 | target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))), |
6a08d0ab | 5100 | op_optab, op0, target, 1); |
64db345d | 5101 | gcc_assert (target); |
7d3f6cc7 | 5102 | |
efb070c8 | 5103 | return convert_to_mode (target_mode, target, 0); |
53800dbe | 5104 | } |
89cfe6e5 | 5105 | |
df94cd3b | 5106 | /* If the string passed to fputs is a constant and is one character |
2c0e001b | 5107 | long, we attempt to transform this call into __builtin_fputc(). */ |
15c6cf6b | 5108 | |
df94cd3b | 5109 | static rtx |
c2f47e15 | 5110 | expand_builtin_fputs (tree exp, rtx target, bool unlocked) |
df94cd3b | 5111 | { |
2c0e001b | 5112 | /* Verify the arguments in the original call. */ |
c2f47e15 | 5113 | if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
ce1b14f4 | 5114 | { |
c2f47e15 | 5115 | tree result = fold_builtin_fputs (CALL_EXPR_ARG (exp, 0), |
5116 | CALL_EXPR_ARG (exp, 1), | |
5117 | (target == const0_rtx), | |
2c84ee37 | 5118 | unlocked, NULL_TREE); |
5119 | if (result) | |
5120 | return expand_expr (result, target, VOIDmode, EXPAND_NORMAL); | |
ce1b14f4 | 5121 | } |
c2f47e15 | 5122 | return NULL_RTX; |
3311f67b | 5123 | } |
5124 | ||
5a74f77e | 5125 | /* Expand a call to __builtin_expect. We just return our argument |
5126 | as the builtin_expect semantic should've been already executed by | |
5127 | tree branch prediction pass. */ | |
89cfe6e5 | 5128 | |
5129 | static rtx | |
c2f47e15 | 5130 | expand_builtin_expect (tree exp, rtx target) |
89cfe6e5 | 5131 | { |
c2f47e15 | 5132 | tree arg, c; |
89cfe6e5 | 5133 | |
c2f47e15 | 5134 | if (call_expr_nargs (exp) < 2) |
89cfe6e5 | 5135 | return const0_rtx; |
c2f47e15 | 5136 | arg = CALL_EXPR_ARG (exp, 0); |
5137 | c = CALL_EXPR_ARG (exp, 1); | |
89cfe6e5 | 5138 | |
c2f47e15 | 5139 | target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL); |
5a74f77e | 5140 | /* When guessing was done, the hints should be already stripped away. */ |
07311427 | 5141 | gcc_assert (!flag_guess_branch_prob |
5142 | || optimize == 0 || errorcount || sorrycount); | |
89cfe6e5 | 5143 | return target; |
5144 | } | |
689df48e | 5145 | |
c22de3f0 | 5146 | void |
aecda0d6 | 5147 | expand_builtin_trap (void) |
a0ef1725 | 5148 | { |
5149 | #ifdef HAVE_trap | |
5150 | if (HAVE_trap) | |
5151 | emit_insn (gen_trap ()); | |
5152 | else | |
5153 | #endif | |
5154 | emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0); | |
5155 | emit_barrier (); | |
5156 | } | |
78a74442 | 5157 | |
c2f47e15 | 5158 | /* Expand EXP, a call to fabs, fabsf or fabsl. |
5159 | Return NULL_RTX if a normal call should be emitted rather than expanding | |
78a74442 | 5160 | the function inline. If convenient, the result should be placed |
5161 | in TARGET. SUBTARGET may be used as the target for computing | |
5162 | the operand. */ | |
5163 | ||
5164 | static rtx | |
c2f47e15 | 5165 | expand_builtin_fabs (tree exp, rtx target, rtx subtarget) |
78a74442 | 5166 | { |
5167 | enum machine_mode mode; | |
5168 | tree arg; | |
5169 | rtx op0; | |
5170 | ||
c2f47e15 | 5171 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
5172 | return NULL_RTX; | |
78a74442 | 5173 | |
c2f47e15 | 5174 | arg = CALL_EXPR_ARG (exp, 0); |
c7f617c2 | 5175 | CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg); |
78a74442 | 5176 | mode = TYPE_MODE (TREE_TYPE (arg)); |
1db6d067 | 5177 | op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL); |
78a74442 | 5178 | return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1)); |
5179 | } | |
5180 | ||
c2f47e15 | 5181 | /* Expand EXP, a call to copysign, copysignf, or copysignl. |
270436f3 | 5182 | Return NULL is a normal call should be emitted rather than expanding the |
5183 | function inline. If convenient, the result should be placed in TARGET. | |
5184 | SUBTARGET may be used as the target for computing the operand. */ | |
5185 | ||
5186 | static rtx | |
c2f47e15 | 5187 | expand_builtin_copysign (tree exp, rtx target, rtx subtarget) |
270436f3 | 5188 | { |
5189 | rtx op0, op1; | |
5190 | tree arg; | |
5191 | ||
c2f47e15 | 5192 | if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE)) |
5193 | return NULL_RTX; | |
270436f3 | 5194 | |
c2f47e15 | 5195 | arg = CALL_EXPR_ARG (exp, 0); |
8ec3c5c2 | 5196 | op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL); |
270436f3 | 5197 | |
c2f47e15 | 5198 | arg = CALL_EXPR_ARG (exp, 1); |
8ec3c5c2 | 5199 | op1 = expand_normal (arg); |
270436f3 | 5200 | |
5201 | return expand_copysign (op0, op1, target); | |
5202 | } | |
5203 | ||
19bf118a | 5204 | /* Create a new constant string literal and return a char* pointer to it. |
5205 | The STRING_CST value is the LEN characters at STR. */ | |
1e8e9920 | 5206 | tree |
19bf118a | 5207 | build_string_literal (int len, const char *str) |
5208 | { | |
5209 | tree t, elem, index, type; | |
5210 | ||
5211 | t = build_string (len, str); | |
5212 | elem = build_type_variant (char_type_node, 1, 0); | |
40238f64 | 5213 | index = build_index_type (size_int (len - 1)); |
19bf118a | 5214 | type = build_array_type (elem, index); |
5215 | TREE_TYPE (t) = type; | |
5216 | TREE_CONSTANT (t) = 1; | |
5217 | TREE_READONLY (t) = 1; | |
5218 | TREE_STATIC (t) = 1; | |
5219 | ||
19bf118a | 5220 | type = build_pointer_type (elem); |
40238f64 | 5221 | t = build1 (ADDR_EXPR, type, |
5222 | build4 (ARRAY_REF, elem, | |
5223 | t, integer_zero_node, NULL_TREE, NULL_TREE)); | |
19bf118a | 5224 | return t; |
5225 | } | |
5226 | ||
0b25db21 | 5227 | /* Expand EXP, a call to printf or printf_unlocked. |
c2f47e15 | 5228 | Return NULL_RTX if a normal call should be emitted rather than transforming |
19bf118a | 5229 | the function inline. If convenient, the result should be placed in |
0862b7e9 | 5230 | TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked |
19bf118a | 5231 | call. */ |
5232 | static rtx | |
0b25db21 | 5233 | expand_builtin_printf (tree exp, rtx target, enum machine_mode mode, |
19bf118a | 5234 | bool unlocked) |
5235 | { | |
6d77ed92 | 5236 | /* If we're using an unlocked function, assume the other unlocked |
5237 | functions exist explicitly. */ | |
5238 | tree const fn_putchar = unlocked ? built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED] | |
5239 | : implicit_built_in_decls[BUILT_IN_PUTCHAR]; | |
5240 | tree const fn_puts = unlocked ? built_in_decls[BUILT_IN_PUTS_UNLOCKED] | |
5241 | : implicit_built_in_decls[BUILT_IN_PUTS]; | |
19bf118a | 5242 | const char *fmt_str; |
c2f47e15 | 5243 | tree fn = 0; |
5244 | tree fmt, arg; | |
5245 | int nargs = call_expr_nargs (exp); | |
19bf118a | 5246 | |
5247 | /* If the return value is used, don't do the transformation. */ | |
5248 | if (target != const0_rtx) | |
c2f47e15 | 5249 | return NULL_RTX; |
19bf118a | 5250 | |
5251 | /* Verify the required arguments in the original call. */ | |
c2f47e15 | 5252 | if (nargs == 0) |
5253 | return NULL_RTX; | |
5254 | fmt = CALL_EXPR_ARG (exp, 0); | |
552752f7 | 5255 | if (! POINTER_TYPE_P (TREE_TYPE (fmt))) |
c2f47e15 | 5256 | return NULL_RTX; |
19bf118a | 5257 | |
5258 | /* Check whether the format is a literal string constant. */ | |
5259 | fmt_str = c_getstr (fmt); | |
5260 | if (fmt_str == NULL) | |
c2f47e15 | 5261 | return NULL_RTX; |
19bf118a | 5262 | |
d4473c84 | 5263 | if (!init_target_chars ()) |
c2f47e15 | 5264 | return NULL_RTX; |
a0c938f0 | 5265 | |
19bf118a | 5266 | /* If the format specifier was "%s\n", call __builtin_puts(arg). */ |
99eabcc1 | 5267 | if (strcmp (fmt_str, target_percent_s_newline) == 0) |
19bf118a | 5268 | { |
c2f47e15 | 5269 | if ((nargs != 2) |
5270 | || ! POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (exp, 1)))) | |
5271 | return NULL_RTX; | |
5272 | if (fn_puts) | |
5273 | fn = build_call_expr (fn_puts, 1, CALL_EXPR_ARG (exp, 1)); | |
19bf118a | 5274 | } |
5275 | /* If the format specifier was "%c", call __builtin_putchar(arg). */ | |
99eabcc1 | 5276 | else if (strcmp (fmt_str, target_percent_c) == 0) |
19bf118a | 5277 | { |
c2f47e15 | 5278 | if ((nargs != 2) |
5279 | || TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1))) != INTEGER_TYPE) | |
5280 | return NULL_RTX; | |
5281 | if (fn_putchar) | |
5282 | fn = build_call_expr (fn_putchar, 1, CALL_EXPR_ARG (exp, 1)); | |
19bf118a | 5283 | } |
5284 | else | |
5285 | { | |
5286 | /* We can't handle anything else with % args or %% ... yet. */ | |
99eabcc1 | 5287 | if (strchr (fmt_str, target_percent)) |
c2f47e15 | 5288 | return NULL_RTX; |
19bf118a | 5289 | |
c2f47e15 | 5290 | if (nargs > 1) |
5291 | return NULL_RTX; | |
19bf118a | 5292 | |
5293 | /* If the format specifier was "", printf does nothing. */ | |
5294 | if (fmt_str[0] == '\0') | |
5295 | return const0_rtx; | |
5296 | /* If the format specifier has length of 1, call putchar. */ | |
5297 | if (fmt_str[1] == '\0') | |
5298 | { | |
5299 | /* Given printf("c"), (where c is any one character,) | |
5300 | convert "c"[0] to an int and pass that to the replacement | |
5301 | function. */ | |
7016c612 | 5302 | arg = build_int_cst (NULL_TREE, fmt_str[0]); |
c2f47e15 | 5303 | if (fn_putchar) |
5304 | fn = build_call_expr (fn_putchar, 1, arg); | |
19bf118a | 5305 | } |
5306 | else | |
5307 | { | |
5308 | /* If the format specifier was "string\n", call puts("string"). */ | |
5309 | size_t len = strlen (fmt_str); | |
99eabcc1 | 5310 | if ((unsigned char)fmt_str[len - 1] == target_newline) |
19bf118a | 5311 | { |
91c82c20 | 5312 | /* Create a NUL-terminated string that's one char shorter |
19bf118a | 5313 | than the original, stripping off the trailing '\n'. */ |
4acc30e5 | 5314 | char *newstr = alloca (len); |
19bf118a | 5315 | memcpy (newstr, fmt_str, len - 1); |
5316 | newstr[len - 1] = 0; | |
19bf118a | 5317 | arg = build_string_literal (len, newstr); |
c2f47e15 | 5318 | if (fn_puts) |
5319 | fn = build_call_expr (fn_puts, 1, arg); | |
19bf118a | 5320 | } |
5321 | else | |
5322 | /* We'd like to arrange to call fputs(string,stdout) here, | |
5323 | but we need stdout and don't have a way to get it yet. */ | |
c2f47e15 | 5324 | return NULL_RTX; |
19bf118a | 5325 | } |
5326 | } | |
5327 | ||
5328 | if (!fn) | |
c2f47e15 | 5329 | return NULL_RTX; |
0b25db21 | 5330 | if (TREE_CODE (fn) == CALL_EXPR) |
5331 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
5332 | return expand_expr (fn, target, mode, EXPAND_NORMAL); | |
19bf118a | 5333 | } |
5334 | ||
0b25db21 | 5335 | /* Expand EXP, a call to fprintf or fprintf_unlocked. |
c2f47e15 | 5336 | Return NULL_RTX if a normal call should be emitted rather than transforming |
19bf118a | 5337 | the function inline. If convenient, the result should be placed in |
0862b7e9 | 5338 | TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked |
19bf118a | 5339 | call. */ |
5340 | static rtx | |
0b25db21 | 5341 | expand_builtin_fprintf (tree exp, rtx target, enum machine_mode mode, |
a0c938f0 | 5342 | bool unlocked) |
19bf118a | 5343 | { |
6d77ed92 | 5344 | /* If we're using an unlocked function, assume the other unlocked |
5345 | functions exist explicitly. */ | |
5346 | tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED] | |
5347 | : implicit_built_in_decls[BUILT_IN_FPUTC]; | |
5348 | tree const fn_fputs = unlocked ? built_in_decls[BUILT_IN_FPUTS_UNLOCKED] | |
5349 | : implicit_built_in_decls[BUILT_IN_FPUTS]; | |
19bf118a | 5350 | const char *fmt_str; |
c2f47e15 | 5351 | tree fn = 0; |
5352 | tree fmt, fp, arg; | |
5353 | int nargs = call_expr_nargs (exp); | |
19bf118a | 5354 | |
5355 | /* If the return value is used, don't do the transformation. */ | |
5356 | if (target != const0_rtx) | |
c2f47e15 | 5357 | return NULL_RTX; |
19bf118a | 5358 | |
5359 | /* Verify the required arguments in the original call. */ | |
c2f47e15 | 5360 | if (nargs < 2) |
5361 | return NULL_RTX; | |
5362 | fp = CALL_EXPR_ARG (exp, 0); | |
552752f7 | 5363 | if (! POINTER_TYPE_P (TREE_TYPE (fp))) |
c2f47e15 | 5364 | return NULL_RTX; |
5365 | fmt = CALL_EXPR_ARG (exp, 1); | |
552752f7 | 5366 | if (! POINTER_TYPE_P (TREE_TYPE (fmt))) |
c2f47e15 | 5367 | return NULL_RTX; |
19bf118a | 5368 | |
5369 | /* Check whether the format is a literal string constant. */ | |
5370 | fmt_str = c_getstr (fmt); | |
5371 | if (fmt_str == NULL) | |
c2f47e15 | 5372 | return NULL_RTX; |
19bf118a | 5373 | |
d4473c84 | 5374 | if (!init_target_chars ()) |
c2f47e15 | 5375 | return NULL_RTX; |
a0c938f0 | 5376 | |
19bf118a | 5377 | /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */ |
99eabcc1 | 5378 | if (strcmp (fmt_str, target_percent_s) == 0) |
19bf118a | 5379 | { |
c2f47e15 | 5380 | if ((nargs != 3) |
5381 | || ! POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (exp, 2)))) | |
5382 | return NULL_RTX; | |
5383 | arg = CALL_EXPR_ARG (exp, 2); | |
5384 | if (fn_fputs) | |
5385 | fn = build_call_expr (fn_fputs, 2, arg, fp); | |
19bf118a | 5386 | } |
5387 | /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */ | |
99eabcc1 | 5388 | else if (strcmp (fmt_str, target_percent_c) == 0) |
19bf118a | 5389 | { |
c2f47e15 | 5390 | if ((nargs != 3) |
5391 | || TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (exp, 2))) != INTEGER_TYPE) | |
5392 | return NULL_RTX; | |
5393 | arg = CALL_EXPR_ARG (exp, 2); | |
5394 | if (fn_fputc) | |
5395 | fn = build_call_expr (fn_fputc, 2, arg, fp); | |
19bf118a | 5396 | } |
5397 | else | |
5398 | { | |
5399 | /* We can't handle anything else with % args or %% ... yet. */ | |
99eabcc1 | 5400 | if (strchr (fmt_str, target_percent)) |
c2f47e15 | 5401 | return NULL_RTX; |
19bf118a | 5402 | |
c2f47e15 | 5403 | if (nargs > 2) |
5404 | return NULL_RTX; | |
19bf118a | 5405 | |
5406 | /* If the format specifier was "", fprintf does nothing. */ | |
5407 | if (fmt_str[0] == '\0') | |
5408 | { | |
5409 | /* Evaluate and ignore FILE* argument for side-effects. */ | |
5410 | expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
5411 | return const0_rtx; | |
5412 | } | |
5413 | ||
5414 | /* When "string" doesn't contain %, replace all cases of | |
5415 | fprintf(stream,string) with fputs(string,stream). The fputs | |
5416 | builtin will take care of special cases like length == 1. */ | |
c2f47e15 | 5417 | if (fn_fputs) |
5418 | fn = build_call_expr (fn_fputs, 2, fmt, fp); | |
19bf118a | 5419 | } |
5420 | ||
5421 | if (!fn) | |
c2f47e15 | 5422 | return NULL_RTX; |
0b25db21 | 5423 | if (TREE_CODE (fn) == CALL_EXPR) |
5424 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
5425 | return expand_expr (fn, target, mode, EXPAND_NORMAL); | |
19bf118a | 5426 | } |
5427 | ||
c2f47e15 | 5428 | /* Expand a call EXP to sprintf. Return NULL_RTX if |
6411575e | 5429 | a normal call should be emitted rather than expanding the function |
5430 | inline. If convenient, the result should be placed in TARGET with | |
5431 | mode MODE. */ | |
5432 | ||
5433 | static rtx | |
c2f47e15 | 5434 | expand_builtin_sprintf (tree exp, rtx target, enum machine_mode mode) |
6411575e | 5435 | { |
c2f47e15 | 5436 | tree dest, fmt; |
a7a723f0 | 5437 | const char *fmt_str; |
c2f47e15 | 5438 | int nargs = call_expr_nargs (exp); |
6411575e | 5439 | |
5440 | /* Verify the required arguments in the original call. */ | |
c2f47e15 | 5441 | if (nargs < 2) |
5442 | return NULL_RTX; | |
5443 | dest = CALL_EXPR_ARG (exp, 0); | |
552752f7 | 5444 | if (! POINTER_TYPE_P (TREE_TYPE (dest))) |
c2f47e15 | 5445 | return NULL_RTX; |
5446 | fmt = CALL_EXPR_ARG (exp, 0); | |
552752f7 | 5447 | if (! POINTER_TYPE_P (TREE_TYPE (fmt))) |
c2f47e15 | 5448 | return NULL_RTX; |
6411575e | 5449 | |
5450 | /* Check whether the format is a literal string constant. */ | |
a7a723f0 | 5451 | fmt_str = c_getstr (fmt); |
5452 | if (fmt_str == NULL) | |
c2f47e15 | 5453 | return NULL_RTX; |
6411575e | 5454 | |
d4473c84 | 5455 | if (!init_target_chars ()) |
c2f47e15 | 5456 | return NULL_RTX; |
99eabcc1 | 5457 | |
6411575e | 5458 | /* If the format doesn't contain % args or %%, use strcpy. */ |
99eabcc1 | 5459 | if (strchr (fmt_str, target_percent) == 0) |
6411575e | 5460 | { |
5461 | tree fn = implicit_built_in_decls[BUILT_IN_STRCPY]; | |
5462 | tree exp; | |
5463 | ||
c2f47e15 | 5464 | if ((nargs > 2) || ! fn) |
5465 | return NULL_RTX; | |
5466 | expand_expr (build_call_expr (fn, 2, dest, fmt), | |
6411575e | 5467 | const0_rtx, VOIDmode, EXPAND_NORMAL); |
5468 | if (target == const0_rtx) | |
5469 | return const0_rtx; | |
7016c612 | 5470 | exp = build_int_cst (NULL_TREE, strlen (fmt_str)); |
6411575e | 5471 | return expand_expr (exp, target, mode, EXPAND_NORMAL); |
5472 | } | |
a7a723f0 | 5473 | /* If the format is "%s", use strcpy if the result isn't used. */ |
99eabcc1 | 5474 | else if (strcmp (fmt_str, target_percent_s) == 0) |
6411575e | 5475 | { |
a7a723f0 | 5476 | tree fn, arg, len; |
5477 | fn = implicit_built_in_decls[BUILT_IN_STRCPY]; | |
6411575e | 5478 | |
a7a723f0 | 5479 | if (! fn) |
c2f47e15 | 5480 | return NULL_RTX; |
5481 | if (nargs != 3) | |
5482 | return NULL_RTX; | |
5483 | arg = CALL_EXPR_ARG (exp, 2); | |
552752f7 | 5484 | if (! POINTER_TYPE_P (TREE_TYPE (arg))) |
c2f47e15 | 5485 | return NULL_RTX; |
6411575e | 5486 | |
5487 | if (target != const0_rtx) | |
5488 | { | |
681fab1e | 5489 | len = c_strlen (arg, 1); |
a7a723f0 | 5490 | if (! len || TREE_CODE (len) != INTEGER_CST) |
c2f47e15 | 5491 | return NULL_RTX; |
6411575e | 5492 | } |
5493 | else | |
a7a723f0 | 5494 | len = NULL_TREE; |
6411575e | 5495 | |
c2f47e15 | 5496 | expand_expr (build_call_expr (fn, 2, dest, arg), |
6411575e | 5497 | const0_rtx, VOIDmode, EXPAND_NORMAL); |
5498 | ||
5499 | if (target == const0_rtx) | |
5500 | return const0_rtx; | |
a7a723f0 | 5501 | return expand_expr (len, target, mode, EXPAND_NORMAL); |
6411575e | 5502 | } |
5503 | ||
c2f47e15 | 5504 | return NULL_RTX; |
6411575e | 5505 | } |
27f261ef | 5506 | |
4ee9c684 | 5507 | /* Expand a call to either the entry or exit function profiler. */ |
5508 | ||
5509 | static rtx | |
5510 | expand_builtin_profile_func (bool exitp) | |
5511 | { | |
5512 | rtx this, which; | |
5513 | ||
5514 | this = DECL_RTL (current_function_decl); | |
64db345d | 5515 | gcc_assert (MEM_P (this)); |
5516 | this = XEXP (this, 0); | |
4ee9c684 | 5517 | |
5518 | if (exitp) | |
5519 | which = profile_function_exit_libfunc; | |
5520 | else | |
5521 | which = profile_function_entry_libfunc; | |
5522 | ||
5523 | emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode, | |
5524 | expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS, | |
869d0ef0 | 5525 | 0), |
4ee9c684 | 5526 | Pmode); |
5527 | ||
5528 | return const0_rtx; | |
5529 | } | |
5530 | ||
ac8fb6db | 5531 | /* Expand a call to __builtin___clear_cache. */ |
5532 | ||
5533 | static rtx | |
5534 | expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED) | |
5535 | { | |
5536 | #ifndef HAVE_clear_cache | |
5537 | #ifdef CLEAR_INSN_CACHE | |
5538 | /* There is no "clear_cache" insn, and __clear_cache() in libgcc | |
5539 | does something. Just do the default expansion to a call to | |
5540 | __clear_cache(). */ | |
5541 | return NULL_RTX; | |
5542 | #else | |
5543 | /* There is no "clear_cache" insn, and __clear_cache() in libgcc | |
5544 | does nothing. There is no need to call it. Do nothing. */ | |
5545 | return const0_rtx; | |
5546 | #endif /* CLEAR_INSN_CACHE */ | |
5547 | #else | |
5548 | /* We have a "clear_cache" insn, and it will handle everything. */ | |
5549 | tree begin, end; | |
5550 | rtx begin_rtx, end_rtx; | |
5551 | enum insn_code icode; | |
5552 | ||
5553 | /* We must not expand to a library call. If we did, any | |
5554 | fallback library function in libgcc that might contain a call to | |
5555 | __builtin___clear_cache() would recurse infinitely. */ | |
5556 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
5557 | { | |
5558 | error ("both arguments to %<__builtin___clear_cache%> must be pointers"); | |
5559 | return const0_rtx; | |
5560 | } | |
5561 | ||
5562 | if (HAVE_clear_cache) | |
5563 | { | |
5564 | icode = CODE_FOR_clear_cache; | |
5565 | ||
5566 | begin = CALL_EXPR_ARG (exp, 0); | |
5567 | begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL); | |
5568 | begin_rtx = convert_memory_address (Pmode, begin_rtx); | |
5569 | if (!insn_data[icode].operand[0].predicate (begin_rtx, Pmode)) | |
5570 | begin_rtx = copy_to_mode_reg (Pmode, begin_rtx); | |
5571 | ||
5572 | end = CALL_EXPR_ARG (exp, 1); | |
5573 | end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL); | |
5574 | end_rtx = convert_memory_address (Pmode, end_rtx); | |
5575 | if (!insn_data[icode].operand[1].predicate (end_rtx, Pmode)) | |
5576 | end_rtx = copy_to_mode_reg (Pmode, end_rtx); | |
5577 | ||
5578 | emit_insn (gen_clear_cache (begin_rtx, end_rtx)); | |
5579 | } | |
5580 | return const0_rtx; | |
5581 | #endif /* HAVE_clear_cache */ | |
5582 | } | |
5583 | ||
4ee9c684 | 5584 | /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */ |
5585 | ||
5586 | static rtx | |
5587 | round_trampoline_addr (rtx tramp) | |
5588 | { | |
5589 | rtx temp, addend, mask; | |
5590 | ||
5591 | /* If we don't need too much alignment, we'll have been guaranteed | |
5592 | proper alignment by get_trampoline_type. */ | |
5593 | if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY) | |
5594 | return tramp; | |
5595 | ||
5596 | /* Round address up to desired boundary. */ | |
5597 | temp = gen_reg_rtx (Pmode); | |
5598 | addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1); | |
5599 | mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT); | |
5600 | ||
5601 | temp = expand_simple_binop (Pmode, PLUS, tramp, addend, | |
5602 | temp, 0, OPTAB_LIB_WIDEN); | |
5603 | tramp = expand_simple_binop (Pmode, AND, temp, mask, | |
5604 | temp, 0, OPTAB_LIB_WIDEN); | |
5605 | ||
5606 | return tramp; | |
5607 | } | |
5608 | ||
5609 | static rtx | |
c2f47e15 | 5610 | expand_builtin_init_trampoline (tree exp) |
4ee9c684 | 5611 | { |
5612 | tree t_tramp, t_func, t_chain; | |
5613 | rtx r_tramp, r_func, r_chain; | |
5614 | #ifdef TRAMPOLINE_TEMPLATE | |
5615 | rtx blktramp; | |
5616 | #endif | |
5617 | ||
c2f47e15 | 5618 | if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, |
4ee9c684 | 5619 | POINTER_TYPE, VOID_TYPE)) |
5620 | return NULL_RTX; | |
5621 | ||
c2f47e15 | 5622 | t_tramp = CALL_EXPR_ARG (exp, 0); |
5623 | t_func = CALL_EXPR_ARG (exp, 1); | |
5624 | t_chain = CALL_EXPR_ARG (exp, 2); | |
4ee9c684 | 5625 | |
8ec3c5c2 | 5626 | r_tramp = expand_normal (t_tramp); |
5627 | r_func = expand_normal (t_func); | |
5628 | r_chain = expand_normal (t_chain); | |
4ee9c684 | 5629 | |
5630 | /* Generate insns to initialize the trampoline. */ | |
5631 | r_tramp = round_trampoline_addr (r_tramp); | |
5632 | #ifdef TRAMPOLINE_TEMPLATE | |
5633 | blktramp = gen_rtx_MEM (BLKmode, r_tramp); | |
5634 | set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT); | |
5635 | emit_block_move (blktramp, assemble_trampoline_template (), | |
5636 | GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL); | |
5637 | #endif | |
5638 | trampolines_created = 1; | |
5639 | INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain); | |
5640 | ||
5641 | return const0_rtx; | |
5642 | } | |
5643 | ||
5644 | static rtx | |
c2f47e15 | 5645 | expand_builtin_adjust_trampoline (tree exp) |
4ee9c684 | 5646 | { |
5647 | rtx tramp; | |
5648 | ||
c2f47e15 | 5649 | if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
4ee9c684 | 5650 | return NULL_RTX; |
5651 | ||
c2f47e15 | 5652 | tramp = expand_normal (CALL_EXPR_ARG (exp, 0)); |
4ee9c684 | 5653 | tramp = round_trampoline_addr (tramp); |
5654 | #ifdef TRAMPOLINE_ADJUST_ADDRESS | |
5655 | TRAMPOLINE_ADJUST_ADDRESS (tramp); | |
5656 | #endif | |
5657 | ||
5658 | return tramp; | |
5659 | } | |
5660 | ||
93f564d6 | 5661 | /* Expand the call EXP to the built-in signbit, signbitf or signbitl |
5662 | function. The function first checks whether the back end provides | |
5663 | an insn to implement signbit for the respective mode. If not, it | |
5664 | checks whether the floating point format of the value is such that | |
5665 | the sign bit can be extracted. If that is not the case, the | |
5666 | function returns NULL_RTX to indicate that a normal call should be | |
5667 | emitted rather than expanding the function in-line. EXP is the | |
5668 | expression that is a call to the builtin function; if convenient, | |
5669 | the result should be placed in TARGET. */ | |
27f261ef | 5670 | static rtx |
5671 | expand_builtin_signbit (tree exp, rtx target) | |
5672 | { | |
5673 | const struct real_format *fmt; | |
5674 | enum machine_mode fmode, imode, rmode; | |
5675 | HOST_WIDE_INT hi, lo; | |
c2f47e15 | 5676 | tree arg; |
ca4f1f5b | 5677 | int word, bitpos; |
27eda240 | 5678 | enum insn_code icode; |
27f261ef | 5679 | rtx temp; |
5680 | ||
c2f47e15 | 5681 | if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE)) |
5682 | return NULL_RTX; | |
27f261ef | 5683 | |
c2f47e15 | 5684 | arg = CALL_EXPR_ARG (exp, 0); |
27f261ef | 5685 | fmode = TYPE_MODE (TREE_TYPE (arg)); |
5686 | rmode = TYPE_MODE (TREE_TYPE (exp)); | |
5687 | fmt = REAL_MODE_FORMAT (fmode); | |
5688 | ||
93f564d6 | 5689 | arg = builtin_save_expr (arg); |
5690 | ||
5691 | /* Expand the argument yielding a RTX expression. */ | |
5692 | temp = expand_normal (arg); | |
5693 | ||
5694 | /* Check if the back end provides an insn that handles signbit for the | |
5695 | argument's mode. */ | |
27eda240 | 5696 | icode = signbit_optab->handlers [(int) fmode].insn_code; |
5697 | if (icode != CODE_FOR_nothing) | |
93f564d6 | 5698 | { |
5699 | target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp))); | |
27eda240 | 5700 | emit_unop_insn (icode, target, temp, UNKNOWN); |
93f564d6 | 5701 | return target; |
5702 | } | |
5703 | ||
27f261ef | 5704 | /* For floating point formats without a sign bit, implement signbit |
5705 | as "ARG < 0.0". */ | |
8d564692 | 5706 | bitpos = fmt->signbit_ro; |
ca4f1f5b | 5707 | if (bitpos < 0) |
27f261ef | 5708 | { |
5709 | /* But we can't do this if the format supports signed zero. */ | |
5710 | if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode)) | |
c2f47e15 | 5711 | return NULL_RTX; |
27f261ef | 5712 | |
49d00087 | 5713 | arg = fold_build2 (LT_EXPR, TREE_TYPE (exp), arg, |
5714 | build_real (TREE_TYPE (arg), dconst0)); | |
27f261ef | 5715 | return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL); |
5716 | } | |
5717 | ||
ca4f1f5b | 5718 | if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD) |
27f261ef | 5719 | { |
ca4f1f5b | 5720 | imode = int_mode_for_mode (fmode); |
5721 | if (imode == BLKmode) | |
c2f47e15 | 5722 | return NULL_RTX; |
ca4f1f5b | 5723 | temp = gen_lowpart (imode, temp); |
24fd4260 | 5724 | } |
5725 | else | |
5726 | { | |
ca4f1f5b | 5727 | imode = word_mode; |
5728 | /* Handle targets with different FP word orders. */ | |
5729 | if (FLOAT_WORDS_BIG_ENDIAN) | |
a0c938f0 | 5730 | word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD; |
ca4f1f5b | 5731 | else |
a0c938f0 | 5732 | word = bitpos / BITS_PER_WORD; |
ca4f1f5b | 5733 | temp = operand_subword_force (temp, word, fmode); |
5734 | bitpos = bitpos % BITS_PER_WORD; | |
5735 | } | |
5736 | ||
44b0f1d0 | 5737 | /* Force the intermediate word_mode (or narrower) result into a |
5738 | register. This avoids attempting to create paradoxical SUBREGs | |
5739 | of floating point modes below. */ | |
5740 | temp = force_reg (imode, temp); | |
5741 | ||
ca4f1f5b | 5742 | /* If the bitpos is within the "result mode" lowpart, the operation |
5743 | can be implement with a single bitwise AND. Otherwise, we need | |
5744 | a right shift and an AND. */ | |
5745 | ||
5746 | if (bitpos < GET_MODE_BITSIZE (rmode)) | |
5747 | { | |
24fd4260 | 5748 | if (bitpos < HOST_BITS_PER_WIDE_INT) |
27f261ef | 5749 | { |
24fd4260 | 5750 | hi = 0; |
5751 | lo = (HOST_WIDE_INT) 1 << bitpos; | |
27f261ef | 5752 | } |
5753 | else | |
24fd4260 | 5754 | { |
5755 | hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT); | |
5756 | lo = 0; | |
5757 | } | |
27f261ef | 5758 | |
4a46f016 | 5759 | if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode)) |
ca4f1f5b | 5760 | temp = gen_lowpart (rmode, temp); |
24fd4260 | 5761 | temp = expand_binop (rmode, and_optab, temp, |
5762 | immed_double_const (lo, hi, rmode), | |
ca4f1f5b | 5763 | NULL_RTX, 1, OPTAB_LIB_WIDEN); |
27f261ef | 5764 | } |
ca4f1f5b | 5765 | else |
5766 | { | |
5767 | /* Perform a logical right shift to place the signbit in the least | |
a0c938f0 | 5768 | significant bit, then truncate the result to the desired mode |
ca4f1f5b | 5769 | and mask just this bit. */ |
5770 | temp = expand_shift (RSHIFT_EXPR, imode, temp, | |
5771 | build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1); | |
5772 | temp = gen_lowpart (rmode, temp); | |
5773 | temp = expand_binop (rmode, and_optab, temp, const1_rtx, | |
5774 | NULL_RTX, 1, OPTAB_LIB_WIDEN); | |
5775 | } | |
5776 | ||
27f261ef | 5777 | return temp; |
5778 | } | |
73673831 | 5779 | |
5780 | /* Expand fork or exec calls. TARGET is the desired target of the | |
c2f47e15 | 5781 | call. EXP is the call. FN is the |
73673831 | 5782 | identificator of the actual function. IGNORE is nonzero if the |
5783 | value is to be ignored. */ | |
5784 | ||
5785 | static rtx | |
c2f47e15 | 5786 | expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore) |
73673831 | 5787 | { |
5788 | tree id, decl; | |
5789 | tree call; | |
5790 | ||
5791 | /* If we are not profiling, just call the function. */ | |
5792 | if (!profile_arc_flag) | |
5793 | return NULL_RTX; | |
5794 | ||
5795 | /* Otherwise call the wrapper. This should be equivalent for the rest of | |
5796 | compiler, so the code does not diverge, and the wrapper may run the | |
9c9bad97 | 5797 | code necessary for keeping the profiling sane. */ |
73673831 | 5798 | |
5799 | switch (DECL_FUNCTION_CODE (fn)) | |
5800 | { | |
5801 | case BUILT_IN_FORK: | |
5802 | id = get_identifier ("__gcov_fork"); | |
5803 | break; | |
5804 | ||
5805 | case BUILT_IN_EXECL: | |
5806 | id = get_identifier ("__gcov_execl"); | |
5807 | break; | |
5808 | ||
5809 | case BUILT_IN_EXECV: | |
5810 | id = get_identifier ("__gcov_execv"); | |
5811 | break; | |
5812 | ||
5813 | case BUILT_IN_EXECLP: | |
5814 | id = get_identifier ("__gcov_execlp"); | |
5815 | break; | |
5816 | ||
5817 | case BUILT_IN_EXECLE: | |
5818 | id = get_identifier ("__gcov_execle"); | |
5819 | break; | |
5820 | ||
5821 | case BUILT_IN_EXECVP: | |
5822 | id = get_identifier ("__gcov_execvp"); | |
5823 | break; | |
5824 | ||
5825 | case BUILT_IN_EXECVE: | |
5826 | id = get_identifier ("__gcov_execve"); | |
5827 | break; | |
5828 | ||
5829 | default: | |
64db345d | 5830 | gcc_unreachable (); |
73673831 | 5831 | } |
5832 | ||
5833 | decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn)); | |
5834 | DECL_EXTERNAL (decl) = 1; | |
5835 | TREE_PUBLIC (decl) = 1; | |
5836 | DECL_ARTIFICIAL (decl) = 1; | |
5837 | TREE_NOTHROW (decl) = 1; | |
e82d310b | 5838 | DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT; |
5839 | DECL_VISIBILITY_SPECIFIED (decl) = 1; | |
c2f47e15 | 5840 | call = rewrite_call_expr (exp, 0, decl, 0); |
73673831 | 5841 | return expand_call (call, target, ignore); |
c2f47e15 | 5842 | } |
5843 | ||
b6a5fc45 | 5844 | |
5845 | \f | |
3e272de8 | 5846 | /* Reconstitute a mode for a __sync intrinsic operation. Since the type of |
5847 | the pointer in these functions is void*, the tree optimizers may remove | |
5848 | casts. The mode computed in expand_builtin isn't reliable either, due | |
5849 | to __sync_bool_compare_and_swap. | |
5850 | ||
5851 | FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the | |
5852 | group of builtins. This gives us log2 of the mode size. */ | |
5853 | ||
5854 | static inline enum machine_mode | |
5855 | get_builtin_sync_mode (int fcode_diff) | |
5856 | { | |
ad3a13b5 | 5857 | /* The size is not negotiable, so ask not to get BLKmode in return |
5858 | if the target indicates that a smaller size would be better. */ | |
5859 | return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0); | |
3e272de8 | 5860 | } |
5861 | ||
041e0215 | 5862 | /* Expand the memory expression LOC and return the appropriate memory operand |
5863 | for the builtin_sync operations. */ | |
5864 | ||
5865 | static rtx | |
5866 | get_builtin_sync_mem (tree loc, enum machine_mode mode) | |
5867 | { | |
5868 | rtx addr, mem; | |
5869 | ||
1db6d067 | 5870 | addr = expand_expr (loc, NULL_RTX, Pmode, EXPAND_SUM); |
041e0215 | 5871 | |
5872 | /* Note that we explicitly do not want any alias information for this | |
5873 | memory, so that we kill all other live memories. Otherwise we don't | |
5874 | satisfy the full barrier semantics of the intrinsic. */ | |
5875 | mem = validize_mem (gen_rtx_MEM (mode, addr)); | |
5876 | ||
5877 | set_mem_align (mem, get_pointer_alignment (loc, BIGGEST_ALIGNMENT)); | |
c94cfd1c | 5878 | set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER); |
041e0215 | 5879 | MEM_VOLATILE_P (mem) = 1; |
5880 | ||
5881 | return mem; | |
5882 | } | |
5883 | ||
b6a5fc45 | 5884 | /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics. |
c2f47e15 | 5885 | EXP is the CALL_EXPR. CODE is the rtx code |
b6a5fc45 | 5886 | that corresponds to the arithmetic or logical operation from the name; |
5887 | an exception here is that NOT actually means NAND. TARGET is an optional | |
5888 | place for us to store the results; AFTER is true if this is the | |
5889 | fetch_and_xxx form. IGNORE is true if we don't actually care about | |
5890 | the result of the operation at all. */ | |
5891 | ||
5892 | static rtx | |
c2f47e15 | 5893 | expand_builtin_sync_operation (enum machine_mode mode, tree exp, |
3e272de8 | 5894 | enum rtx_code code, bool after, |
b6a5fc45 | 5895 | rtx target, bool ignore) |
5896 | { | |
041e0215 | 5897 | rtx val, mem; |
a8bb7059 | 5898 | enum machine_mode old_mode; |
b6a5fc45 | 5899 | |
5900 | /* Expand the operands. */ | |
c2f47e15 | 5901 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); |
b6a5fc45 | 5902 | |
1db6d067 | 5903 | val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL); |
a8bb7059 | 5904 | /* If VAL is promoted to a wider mode, convert it back to MODE. Take care |
5905 | of CONST_INTs, where we know the old_mode only from the call argument. */ | |
5906 | old_mode = GET_MODE (val); | |
5907 | if (old_mode == VOIDmode) | |
5908 | old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1))); | |
5909 | val = convert_modes (mode, old_mode, val, 1); | |
b6a5fc45 | 5910 | |
b6a5fc45 | 5911 | if (ignore) |
5912 | return expand_sync_operation (mem, val, code); | |
5913 | else | |
5914 | return expand_sync_fetch_operation (mem, val, code, after, target); | |
5915 | } | |
5916 | ||
5917 | /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap | |
c2f47e15 | 5918 | intrinsics. EXP is the CALL_EXPR. IS_BOOL is |
b6a5fc45 | 5919 | true if this is the boolean form. TARGET is a place for us to store the |
5920 | results; this is NOT optional if IS_BOOL is true. */ | |
5921 | ||
5922 | static rtx | |
c2f47e15 | 5923 | expand_builtin_compare_and_swap (enum machine_mode mode, tree exp, |
3e272de8 | 5924 | bool is_bool, rtx target) |
b6a5fc45 | 5925 | { |
041e0215 | 5926 | rtx old_val, new_val, mem; |
a8bb7059 | 5927 | enum machine_mode old_mode; |
b6a5fc45 | 5928 | |
5929 | /* Expand the operands. */ | |
c2f47e15 | 5930 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); |
b6a5fc45 | 5931 | |
c2f47e15 | 5932 | |
1db6d067 | 5933 | old_val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, |
5934 | mode, EXPAND_NORMAL); | |
a8bb7059 | 5935 | /* If VAL is promoted to a wider mode, convert it back to MODE. Take care |
5936 | of CONST_INTs, where we know the old_mode only from the call argument. */ | |
5937 | old_mode = GET_MODE (old_val); | |
5938 | if (old_mode == VOIDmode) | |
5939 | old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1))); | |
5940 | old_val = convert_modes (mode, old_mode, old_val, 1); | |
b6a5fc45 | 5941 | |
1db6d067 | 5942 | new_val = expand_expr (CALL_EXPR_ARG (exp, 2), NULL_RTX, |
5943 | mode, EXPAND_NORMAL); | |
a8bb7059 | 5944 | /* If VAL is promoted to a wider mode, convert it back to MODE. Take care |
5945 | of CONST_INTs, where we know the old_mode only from the call argument. */ | |
5946 | old_mode = GET_MODE (new_val); | |
5947 | if (old_mode == VOIDmode) | |
5948 | old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 2))); | |
5949 | new_val = convert_modes (mode, old_mode, new_val, 1); | |
b6a5fc45 | 5950 | |
b6a5fc45 | 5951 | if (is_bool) |
5952 | return expand_bool_compare_and_swap (mem, old_val, new_val, target); | |
5953 | else | |
5954 | return expand_val_compare_and_swap (mem, old_val, new_val, target); | |
5955 | } | |
5956 | ||
5957 | /* Expand the __sync_lock_test_and_set intrinsic. Note that the most | |
5958 | general form is actually an atomic exchange, and some targets only | |
5959 | support a reduced form with the second argument being a constant 1. | |
c2f47e15 | 5960 | EXP is the CALL_EXPR; TARGET is an optional place for us to store |
5961 | the results. */ | |
b6a5fc45 | 5962 | |
5963 | static rtx | |
c2f47e15 | 5964 | expand_builtin_lock_test_and_set (enum machine_mode mode, tree exp, |
3e272de8 | 5965 | rtx target) |
b6a5fc45 | 5966 | { |
041e0215 | 5967 | rtx val, mem; |
a8bb7059 | 5968 | enum machine_mode old_mode; |
b6a5fc45 | 5969 | |
5970 | /* Expand the operands. */ | |
c2f47e15 | 5971 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); |
1db6d067 | 5972 | val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL); |
a8bb7059 | 5973 | /* If VAL is promoted to a wider mode, convert it back to MODE. Take care |
5974 | of CONST_INTs, where we know the old_mode only from the call argument. */ | |
5975 | old_mode = GET_MODE (val); | |
5976 | if (old_mode == VOIDmode) | |
5977 | old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1))); | |
5978 | val = convert_modes (mode, old_mode, val, 1); | |
b6a5fc45 | 5979 | |
b6a5fc45 | 5980 | return expand_sync_lock_test_and_set (mem, val, target); |
5981 | } | |
5982 | ||
5983 | /* Expand the __sync_synchronize intrinsic. */ | |
5984 | ||
5985 | static void | |
5986 | expand_builtin_synchronize (void) | |
5987 | { | |
82e58396 | 5988 | tree x; |
b6a5fc45 | 5989 | |
5990 | #ifdef HAVE_memory_barrier | |
5991 | if (HAVE_memory_barrier) | |
5992 | { | |
5993 | emit_insn (gen_memory_barrier ()); | |
5994 | return; | |
5995 | } | |
5996 | #endif | |
5997 | ||
82e58396 | 5998 | /* If no explicit memory barrier instruction is available, create an |
5999 | empty asm stmt with a memory clobber. */ | |
6000 | x = build4 (ASM_EXPR, void_type_node, build_string (0, ""), NULL, NULL, | |
6001 | tree_cons (NULL, build_string (6, "memory"), NULL)); | |
6002 | ASM_VOLATILE_P (x) = 1; | |
6003 | expand_asm_expr (x); | |
b6a5fc45 | 6004 | } |
6005 | ||
c2f47e15 | 6006 | /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */ |
b6a5fc45 | 6007 | |
6008 | static void | |
c2f47e15 | 6009 | expand_builtin_lock_release (enum machine_mode mode, tree exp) |
b6a5fc45 | 6010 | { |
b6a5fc45 | 6011 | enum insn_code icode; |
041e0215 | 6012 | rtx mem, insn; |
3e272de8 | 6013 | rtx val = const0_rtx; |
b6a5fc45 | 6014 | |
6015 | /* Expand the operands. */ | |
c2f47e15 | 6016 | mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode); |
b6a5fc45 | 6017 | |
6018 | /* If there is an explicit operation in the md file, use it. */ | |
6019 | icode = sync_lock_release[mode]; | |
6020 | if (icode != CODE_FOR_nothing) | |
6021 | { | |
6022 | if (!insn_data[icode].operand[1].predicate (val, mode)) | |
6023 | val = force_reg (mode, val); | |
6024 | ||
6025 | insn = GEN_FCN (icode) (mem, val); | |
6026 | if (insn) | |
6027 | { | |
6028 | emit_insn (insn); | |
6029 | return; | |
6030 | } | |
6031 | } | |
6032 | ||
6033 | /* Otherwise we can implement this operation by emitting a barrier | |
6034 | followed by a store of zero. */ | |
6035 | expand_builtin_synchronize (); | |
6036 | emit_move_insn (mem, val); | |
6037 | } | |
53800dbe | 6038 | \f |
6039 | /* Expand an expression EXP that calls a built-in function, | |
6040 | with result going to TARGET if that's convenient | |
6041 | (and in mode MODE if that's convenient). | |
6042 | SUBTARGET may be used as the target for computing one of EXP's operands. | |
6043 | IGNORE is nonzero if the value is to be ignored. */ | |
6044 | ||
6045 | rtx | |
aecda0d6 | 6046 | expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode, |
6047 | int ignore) | |
53800dbe | 6048 | { |
c6e6ecb1 | 6049 | tree fndecl = get_callee_fndecl (exp); |
53800dbe | 6050 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); |
efb070c8 | 6051 | enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp)); |
53800dbe | 6052 | |
8305149e | 6053 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) |
883b2e73 | 6054 | return targetm.expand_builtin (exp, target, subtarget, mode, ignore); |
bf8e3599 | 6055 | |
53800dbe | 6056 | /* When not optimizing, generate calls to library functions for a certain |
6057 | set of builtins. */ | |
cd9ff771 | 6058 | if (!optimize |
b6a5fc45 | 6059 | && !called_as_built_in (fndecl) |
cd9ff771 | 6060 | && DECL_ASSEMBLER_NAME_SET_P (fndecl) |
6061 | && fcode != BUILT_IN_ALLOCA) | |
6062 | return expand_call (exp, target, ignore); | |
53800dbe | 6063 | |
8d6d7930 | 6064 | /* The built-in function expanders test for target == const0_rtx |
6065 | to determine whether the function's result will be ignored. */ | |
6066 | if (ignore) | |
6067 | target = const0_rtx; | |
6068 | ||
6069 | /* If the result of a pure or const built-in function is ignored, and | |
6070 | none of its arguments are volatile, we can avoid expanding the | |
6071 | built-in call and just evaluate the arguments for side-effects. */ | |
6072 | if (target == const0_rtx | |
9c2a0c05 | 6073 | && (DECL_PURE_P (fndecl) || TREE_READONLY (fndecl))) |
8d6d7930 | 6074 | { |
6075 | bool volatilep = false; | |
6076 | tree arg; | |
c2f47e15 | 6077 | call_expr_arg_iterator iter; |
8d6d7930 | 6078 | |
c2f47e15 | 6079 | FOR_EACH_CALL_EXPR_ARG (arg, iter, exp) |
6080 | if (TREE_THIS_VOLATILE (arg)) | |
8d6d7930 | 6081 | { |
6082 | volatilep = true; | |
6083 | break; | |
6084 | } | |
6085 | ||
6086 | if (! volatilep) | |
6087 | { | |
c2f47e15 | 6088 | FOR_EACH_CALL_EXPR_ARG (arg, iter, exp) |
6089 | expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
8d6d7930 | 6090 | return const0_rtx; |
6091 | } | |
6092 | } | |
6093 | ||
53800dbe | 6094 | switch (fcode) |
6095 | { | |
4f35b1fc | 6096 | CASE_FLT_FN (BUILT_IN_FABS): |
c2f47e15 | 6097 | target = expand_builtin_fabs (exp, target, subtarget); |
78a74442 | 6098 | if (target) |
a0c938f0 | 6099 | return target; |
78a74442 | 6100 | break; |
6101 | ||
4f35b1fc | 6102 | CASE_FLT_FN (BUILT_IN_COPYSIGN): |
c2f47e15 | 6103 | target = expand_builtin_copysign (exp, target, subtarget); |
270436f3 | 6104 | if (target) |
6105 | return target; | |
6106 | break; | |
6107 | ||
7d3f6cc7 | 6108 | /* Just do a normal library call if we were unable to fold |
6109 | the values. */ | |
4f35b1fc | 6110 | CASE_FLT_FN (BUILT_IN_CABS): |
78a74442 | 6111 | break; |
53800dbe | 6112 | |
4f35b1fc | 6113 | CASE_FLT_FN (BUILT_IN_EXP): |
6114 | CASE_FLT_FN (BUILT_IN_EXP10): | |
6115 | CASE_FLT_FN (BUILT_IN_POW10): | |
6116 | CASE_FLT_FN (BUILT_IN_EXP2): | |
6117 | CASE_FLT_FN (BUILT_IN_EXPM1): | |
6118 | CASE_FLT_FN (BUILT_IN_LOGB): | |
4f35b1fc | 6119 | CASE_FLT_FN (BUILT_IN_LOG): |
6120 | CASE_FLT_FN (BUILT_IN_LOG10): | |
6121 | CASE_FLT_FN (BUILT_IN_LOG2): | |
6122 | CASE_FLT_FN (BUILT_IN_LOG1P): | |
6123 | CASE_FLT_FN (BUILT_IN_TAN): | |
6124 | CASE_FLT_FN (BUILT_IN_ASIN): | |
6125 | CASE_FLT_FN (BUILT_IN_ACOS): | |
6126 | CASE_FLT_FN (BUILT_IN_ATAN): | |
7f3be425 | 6127 | /* Treat these like sqrt only if unsafe math optimizations are allowed, |
6128 | because of possible accuracy problems. */ | |
6129 | if (! flag_unsafe_math_optimizations) | |
53800dbe | 6130 | break; |
4f35b1fc | 6131 | CASE_FLT_FN (BUILT_IN_SQRT): |
6132 | CASE_FLT_FN (BUILT_IN_FLOOR): | |
6133 | CASE_FLT_FN (BUILT_IN_CEIL): | |
6134 | CASE_FLT_FN (BUILT_IN_TRUNC): | |
6135 | CASE_FLT_FN (BUILT_IN_ROUND): | |
6136 | CASE_FLT_FN (BUILT_IN_NEARBYINT): | |
6137 | CASE_FLT_FN (BUILT_IN_RINT): | |
53800dbe | 6138 | target = expand_builtin_mathfn (exp, target, subtarget); |
6139 | if (target) | |
6140 | return target; | |
6141 | break; | |
6142 | ||
a67a90e5 | 6143 | CASE_FLT_FN (BUILT_IN_ILOGB): |
6144 | if (! flag_unsafe_math_optimizations) | |
6145 | break; | |
69b779ea | 6146 | CASE_FLT_FN (BUILT_IN_ISINF): |
cde061c1 | 6147 | CASE_FLT_FN (BUILT_IN_FINITE): |
6148 | case BUILT_IN_ISFINITE: | |
8a1a9cb7 | 6149 | case BUILT_IN_ISNORMAL: |
a67a90e5 | 6150 | target = expand_builtin_interclass_mathfn (exp, target, subtarget); |
6151 | if (target) | |
6152 | return target; | |
6153 | break; | |
6154 | ||
4f35b1fc | 6155 | CASE_FLT_FN (BUILT_IN_LCEIL): |
6156 | CASE_FLT_FN (BUILT_IN_LLCEIL): | |
6157 | CASE_FLT_FN (BUILT_IN_LFLOOR): | |
6158 | CASE_FLT_FN (BUILT_IN_LLFLOOR): | |
ad52b9b7 | 6159 | target = expand_builtin_int_roundingfn (exp, target, subtarget); |
6160 | if (target) | |
6161 | return target; | |
6162 | break; | |
6163 | ||
7d3afc77 | 6164 | CASE_FLT_FN (BUILT_IN_LRINT): |
6165 | CASE_FLT_FN (BUILT_IN_LLRINT): | |
ef2f1a10 | 6166 | CASE_FLT_FN (BUILT_IN_LROUND): |
6167 | CASE_FLT_FN (BUILT_IN_LLROUND): | |
7d3afc77 | 6168 | target = expand_builtin_int_roundingfn_2 (exp, target, subtarget); |
6169 | if (target) | |
6170 | return target; | |
6171 | break; | |
6172 | ||
4f35b1fc | 6173 | CASE_FLT_FN (BUILT_IN_POW): |
f1b844c6 | 6174 | target = expand_builtin_pow (exp, target, subtarget); |
6175 | if (target) | |
6176 | return target; | |
6177 | break; | |
6178 | ||
4f35b1fc | 6179 | CASE_FLT_FN (BUILT_IN_POWI): |
757c219d | 6180 | target = expand_builtin_powi (exp, target, subtarget); |
6181 | if (target) | |
6182 | return target; | |
6183 | break; | |
6184 | ||
4f35b1fc | 6185 | CASE_FLT_FN (BUILT_IN_ATAN2): |
6186 | CASE_FLT_FN (BUILT_IN_LDEXP): | |
73a954a1 | 6187 | CASE_FLT_FN (BUILT_IN_SCALB): |
6188 | CASE_FLT_FN (BUILT_IN_SCALBN): | |
6189 | CASE_FLT_FN (BUILT_IN_SCALBLN): | |
0fd605a5 | 6190 | if (! flag_unsafe_math_optimizations) |
6191 | break; | |
ef722005 | 6192 | |
6193 | CASE_FLT_FN (BUILT_IN_FMOD): | |
6194 | CASE_FLT_FN (BUILT_IN_REMAINDER): | |
6195 | CASE_FLT_FN (BUILT_IN_DREM): | |
0fd605a5 | 6196 | target = expand_builtin_mathfn_2 (exp, target, subtarget); |
6197 | if (target) | |
6198 | return target; | |
6199 | break; | |
6200 | ||
d735c391 | 6201 | CASE_FLT_FN (BUILT_IN_CEXPI): |
6202 | target = expand_builtin_cexpi (exp, target, subtarget); | |
6203 | gcc_assert (target); | |
6204 | return target; | |
6205 | ||
4f35b1fc | 6206 | CASE_FLT_FN (BUILT_IN_SIN): |
6207 | CASE_FLT_FN (BUILT_IN_COS): | |
6b43bae4 | 6208 | if (! flag_unsafe_math_optimizations) |
6209 | break; | |
6210 | target = expand_builtin_mathfn_3 (exp, target, subtarget); | |
6211 | if (target) | |
6212 | return target; | |
6213 | break; | |
6214 | ||
c3147c1a | 6215 | CASE_FLT_FN (BUILT_IN_SINCOS): |
6216 | if (! flag_unsafe_math_optimizations) | |
6217 | break; | |
6218 | target = expand_builtin_sincos (exp); | |
6219 | if (target) | |
6220 | return target; | |
6221 | break; | |
6222 | ||
53800dbe | 6223 | case BUILT_IN_APPLY_ARGS: |
6224 | return expand_builtin_apply_args (); | |
6225 | ||
6226 | /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes | |
6227 | FUNCTION with a copy of the parameters described by | |
6228 | ARGUMENTS, and ARGSIZE. It returns a block of memory | |
6229 | allocated on the stack into which is stored all the registers | |
6230 | that might possibly be used for returning the result of a | |
6231 | function. ARGUMENTS is the value returned by | |
6232 | __builtin_apply_args. ARGSIZE is the number of bytes of | |
6233 | arguments that must be copied. ??? How should this value be | |
6234 | computed? We'll also need a safe worst case value for varargs | |
6235 | functions. */ | |
6236 | case BUILT_IN_APPLY: | |
c2f47e15 | 6237 | if (!validate_arglist (exp, POINTER_TYPE, |
0eb671f7 | 6238 | POINTER_TYPE, INTEGER_TYPE, VOID_TYPE) |
c2f47e15 | 6239 | && !validate_arglist (exp, REFERENCE_TYPE, |
0eb671f7 | 6240 | POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
53800dbe | 6241 | return const0_rtx; |
6242 | else | |
6243 | { | |
53800dbe | 6244 | rtx ops[3]; |
6245 | ||
c2f47e15 | 6246 | ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0)); |
6247 | ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1)); | |
6248 | ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2)); | |
53800dbe | 6249 | |
6250 | return expand_builtin_apply (ops[0], ops[1], ops[2]); | |
6251 | } | |
6252 | ||
6253 | /* __builtin_return (RESULT) causes the function to return the | |
6254 | value described by RESULT. RESULT is address of the block of | |
6255 | memory returned by __builtin_apply. */ | |
6256 | case BUILT_IN_RETURN: | |
c2f47e15 | 6257 | if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
6258 | expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0))); | |
53800dbe | 6259 | return const0_rtx; |
6260 | ||
6261 | case BUILT_IN_SAVEREGS: | |
a66c9326 | 6262 | return expand_builtin_saveregs (); |
53800dbe | 6263 | |
6264 | case BUILT_IN_ARGS_INFO: | |
c2f47e15 | 6265 | return expand_builtin_args_info (exp); |
53800dbe | 6266 | |
48dc2227 | 6267 | case BUILT_IN_VA_ARG_PACK: |
6268 | /* All valid uses of __builtin_va_arg_pack () are removed during | |
6269 | inlining. */ | |
b8c23db3 | 6270 | error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp); |
48dc2227 | 6271 | return const0_rtx; |
6272 | ||
4e1d7ea4 | 6273 | case BUILT_IN_VA_ARG_PACK_LEN: |
6274 | /* All valid uses of __builtin_va_arg_pack_len () are removed during | |
6275 | inlining. */ | |
b8c23db3 | 6276 | error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp); |
4e1d7ea4 | 6277 | return const0_rtx; |
6278 | ||
53800dbe | 6279 | /* Return the address of the first anonymous stack arg. */ |
6280 | case BUILT_IN_NEXT_ARG: | |
c2f47e15 | 6281 | if (fold_builtin_next_arg (exp, false)) |
a0c938f0 | 6282 | return const0_rtx; |
79012a9d | 6283 | return expand_builtin_next_arg (); |
53800dbe | 6284 | |
ac8fb6db | 6285 | case BUILT_IN_CLEAR_CACHE: |
6286 | target = expand_builtin___clear_cache (exp); | |
6287 | if (target) | |
6288 | return target; | |
6289 | break; | |
6290 | ||
53800dbe | 6291 | case BUILT_IN_CLASSIFY_TYPE: |
c2f47e15 | 6292 | return expand_builtin_classify_type (exp); |
53800dbe | 6293 | |
6294 | case BUILT_IN_CONSTANT_P: | |
4ee9c684 | 6295 | return const0_rtx; |
53800dbe | 6296 | |
6297 | case BUILT_IN_FRAME_ADDRESS: | |
6298 | case BUILT_IN_RETURN_ADDRESS: | |
c2f47e15 | 6299 | return expand_builtin_frame_address (fndecl, exp); |
53800dbe | 6300 | |
6301 | /* Returns the address of the area where the structure is returned. | |
6302 | 0 otherwise. */ | |
6303 | case BUILT_IN_AGGREGATE_INCOMING_ADDRESS: | |
c2f47e15 | 6304 | if (call_expr_nargs (exp) != 0 |
9342ee68 | 6305 | || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))) |
e16ceb8e | 6306 | || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl)))) |
9342ee68 | 6307 | return const0_rtx; |
53800dbe | 6308 | else |
9342ee68 | 6309 | return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0); |
53800dbe | 6310 | |
6311 | case BUILT_IN_ALLOCA: | |
c2f47e15 | 6312 | target = expand_builtin_alloca (exp, target); |
53800dbe | 6313 | if (target) |
6314 | return target; | |
6315 | break; | |
6316 | ||
4ee9c684 | 6317 | case BUILT_IN_STACK_SAVE: |
6318 | return expand_stack_save (); | |
6319 | ||
6320 | case BUILT_IN_STACK_RESTORE: | |
c2f47e15 | 6321 | expand_stack_restore (CALL_EXPR_ARG (exp, 0)); |
4ee9c684 | 6322 | return const0_rtx; |
6323 | ||
42791117 | 6324 | case BUILT_IN_BSWAP32: |
6325 | case BUILT_IN_BSWAP64: | |
c2f47e15 | 6326 | target = expand_builtin_bswap (exp, target, subtarget); |
42791117 | 6327 | |
6328 | if (target) | |
6329 | return target; | |
6330 | break; | |
6331 | ||
4f35b1fc | 6332 | CASE_INT_FN (BUILT_IN_FFS): |
5053259b | 6333 | case BUILT_IN_FFSIMAX: |
c2f47e15 | 6334 | target = expand_builtin_unop (target_mode, exp, target, |
efb070c8 | 6335 | subtarget, ffs_optab); |
6a08d0ab | 6336 | if (target) |
6337 | return target; | |
6338 | break; | |
6339 | ||
4f35b1fc | 6340 | CASE_INT_FN (BUILT_IN_CLZ): |
5053259b | 6341 | case BUILT_IN_CLZIMAX: |
c2f47e15 | 6342 | target = expand_builtin_unop (target_mode, exp, target, |
efb070c8 | 6343 | subtarget, clz_optab); |
6a08d0ab | 6344 | if (target) |
6345 | return target; | |
6346 | break; | |
6347 | ||
4f35b1fc | 6348 | CASE_INT_FN (BUILT_IN_CTZ): |
5053259b | 6349 | case BUILT_IN_CTZIMAX: |
c2f47e15 | 6350 | target = expand_builtin_unop (target_mode, exp, target, |
efb070c8 | 6351 | subtarget, ctz_optab); |
6a08d0ab | 6352 | if (target) |
6353 | return target; | |
6354 | break; | |
6355 | ||
4f35b1fc | 6356 | CASE_INT_FN (BUILT_IN_POPCOUNT): |
5053259b | 6357 | case BUILT_IN_POPCOUNTIMAX: |
c2f47e15 | 6358 | target = expand_builtin_unop (target_mode, exp, target, |
efb070c8 | 6359 | subtarget, popcount_optab); |
6a08d0ab | 6360 | if (target) |
6361 | return target; | |
6362 | break; | |
6363 | ||
4f35b1fc | 6364 | CASE_INT_FN (BUILT_IN_PARITY): |
5053259b | 6365 | case BUILT_IN_PARITYIMAX: |
c2f47e15 | 6366 | target = expand_builtin_unop (target_mode, exp, target, |
efb070c8 | 6367 | subtarget, parity_optab); |
53800dbe | 6368 | if (target) |
6369 | return target; | |
6370 | break; | |
6371 | ||
6372 | case BUILT_IN_STRLEN: | |
c2f47e15 | 6373 | target = expand_builtin_strlen (exp, target, target_mode); |
53800dbe | 6374 | if (target) |
6375 | return target; | |
6376 | break; | |
6377 | ||
6378 | case BUILT_IN_STRCPY: | |
c2f47e15 | 6379 | target = expand_builtin_strcpy (fndecl, exp, target, mode); |
53800dbe | 6380 | if (target) |
6381 | return target; | |
6382 | break; | |
bf8e3599 | 6383 | |
ed09096d | 6384 | case BUILT_IN_STRNCPY: |
8ff6a5cd | 6385 | target = expand_builtin_strncpy (exp, target, mode); |
ed09096d | 6386 | if (target) |
6387 | return target; | |
6388 | break; | |
bf8e3599 | 6389 | |
3b824fa6 | 6390 | case BUILT_IN_STPCPY: |
dc369150 | 6391 | target = expand_builtin_stpcpy (exp, target, mode); |
3b824fa6 | 6392 | if (target) |
6393 | return target; | |
6394 | break; | |
6395 | ||
49f0327b | 6396 | case BUILT_IN_STRCAT: |
c2f47e15 | 6397 | target = expand_builtin_strcat (fndecl, exp, target, mode); |
49f0327b | 6398 | if (target) |
6399 | return target; | |
6400 | break; | |
bf8e3599 | 6401 | |
49f0327b | 6402 | case BUILT_IN_STRNCAT: |
c2f47e15 | 6403 | target = expand_builtin_strncat (exp, target, mode); |
49f0327b | 6404 | if (target) |
6405 | return target; | |
6406 | break; | |
bf8e3599 | 6407 | |
49f0327b | 6408 | case BUILT_IN_STRSPN: |
c2f47e15 | 6409 | target = expand_builtin_strspn (exp, target, mode); |
49f0327b | 6410 | if (target) |
6411 | return target; | |
6412 | break; | |
bf8e3599 | 6413 | |
49f0327b | 6414 | case BUILT_IN_STRCSPN: |
c2f47e15 | 6415 | target = expand_builtin_strcspn (exp, target, mode); |
49f0327b | 6416 | if (target) |
6417 | return target; | |
6418 | break; | |
bf8e3599 | 6419 | |
17f5ea87 | 6420 | case BUILT_IN_STRSTR: |
c2f47e15 | 6421 | target = expand_builtin_strstr (exp, target, mode); |
17f5ea87 | 6422 | if (target) |
6423 | return target; | |
6424 | break; | |
bf8e3599 | 6425 | |
46f3a74a | 6426 | case BUILT_IN_STRPBRK: |
c2f47e15 | 6427 | target = expand_builtin_strpbrk (exp, target, mode); |
46f3a74a | 6428 | if (target) |
6429 | return target; | |
6430 | break; | |
bf8e3599 | 6431 | |
398aae36 | 6432 | case BUILT_IN_INDEX: |
83d79705 | 6433 | case BUILT_IN_STRCHR: |
c2f47e15 | 6434 | target = expand_builtin_strchr (exp, target, mode); |
83d79705 | 6435 | if (target) |
6436 | return target; | |
6437 | break; | |
6438 | ||
398aae36 | 6439 | case BUILT_IN_RINDEX: |
83d79705 | 6440 | case BUILT_IN_STRRCHR: |
c2f47e15 | 6441 | target = expand_builtin_strrchr (exp, target, mode); |
83d79705 | 6442 | if (target) |
6443 | return target; | |
6444 | break; | |
6445 | ||
53800dbe | 6446 | case BUILT_IN_MEMCPY: |
5a0de151 | 6447 | target = expand_builtin_memcpy (exp, target, mode); |
3b824fa6 | 6448 | if (target) |
6449 | return target; | |
6450 | break; | |
6451 | ||
6452 | case BUILT_IN_MEMPCPY: | |
c2f47e15 | 6453 | target = expand_builtin_mempcpy (exp, target, mode); |
53800dbe | 6454 | if (target) |
6455 | return target; | |
6456 | break; | |
6457 | ||
c4950093 | 6458 | case BUILT_IN_MEMMOVE: |
c2f47e15 | 6459 | target = expand_builtin_memmove (exp, target, mode, ignore); |
c4950093 | 6460 | if (target) |
6461 | return target; | |
6462 | break; | |
6463 | ||
6464 | case BUILT_IN_BCOPY: | |
c2f47e15 | 6465 | target = expand_builtin_bcopy (exp, ignore); |
c4950093 | 6466 | if (target) |
6467 | return target; | |
6468 | break; | |
6469 | ||
53800dbe | 6470 | case BUILT_IN_MEMSET: |
c2f47e15 | 6471 | target = expand_builtin_memset (exp, target, mode); |
53800dbe | 6472 | if (target) |
6473 | return target; | |
6474 | break; | |
6475 | ||
ffc83088 | 6476 | case BUILT_IN_BZERO: |
0b25db21 | 6477 | target = expand_builtin_bzero (exp); |
ffc83088 | 6478 | if (target) |
6479 | return target; | |
6480 | break; | |
6481 | ||
53800dbe | 6482 | case BUILT_IN_STRCMP: |
83d79705 | 6483 | target = expand_builtin_strcmp (exp, target, mode); |
53800dbe | 6484 | if (target) |
6485 | return target; | |
6486 | break; | |
6487 | ||
ed09096d | 6488 | case BUILT_IN_STRNCMP: |
6489 | target = expand_builtin_strncmp (exp, target, mode); | |
6490 | if (target) | |
6491 | return target; | |
6492 | break; | |
6493 | ||
7959b13b | 6494 | case BUILT_IN_MEMCHR: |
6495 | target = expand_builtin_memchr (exp, target, mode); | |
6496 | if (target) | |
6497 | return target; | |
6498 | break; | |
6499 | ||
071f1696 | 6500 | case BUILT_IN_BCMP: |
53800dbe | 6501 | case BUILT_IN_MEMCMP: |
c2f47e15 | 6502 | target = expand_builtin_memcmp (exp, target, mode); |
53800dbe | 6503 | if (target) |
6504 | return target; | |
6505 | break; | |
53800dbe | 6506 | |
6507 | case BUILT_IN_SETJMP: | |
2c8a1497 | 6508 | /* This should have been lowered to the builtins below. */ |
6509 | gcc_unreachable (); | |
6510 | ||
6511 | case BUILT_IN_SETJMP_SETUP: | |
6512 | /* __builtin_setjmp_setup is passed a pointer to an array of five words | |
6513 | and the receiver label. */ | |
c2f47e15 | 6514 | if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
2c8a1497 | 6515 | { |
c2f47e15 | 6516 | rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget, |
2c8a1497 | 6517 | VOIDmode, EXPAND_NORMAL); |
c2f47e15 | 6518 | tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0); |
2c8a1497 | 6519 | rtx label_r = label_rtx (label); |
6520 | ||
6521 | /* This is copied from the handling of non-local gotos. */ | |
6522 | expand_builtin_setjmp_setup (buf_addr, label_r); | |
6523 | nonlocal_goto_handler_labels | |
6524 | = gen_rtx_EXPR_LIST (VOIDmode, label_r, | |
6525 | nonlocal_goto_handler_labels); | |
6526 | /* ??? Do not let expand_label treat us as such since we would | |
6527 | not want to be both on the list of non-local labels and on | |
6528 | the list of forced labels. */ | |
6529 | FORCED_LABEL (label) = 0; | |
6530 | return const0_rtx; | |
6531 | } | |
6532 | break; | |
6533 | ||
6534 | case BUILT_IN_SETJMP_DISPATCHER: | |
6535 | /* __builtin_setjmp_dispatcher is passed the dispatcher label. */ | |
c2f47e15 | 6536 | if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
2c8a1497 | 6537 | { |
c2f47e15 | 6538 | tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0); |
2c8a1497 | 6539 | rtx label_r = label_rtx (label); |
6540 | ||
6541 | /* Remove the dispatcher label from the list of non-local labels | |
6542 | since the receiver labels have been added to it above. */ | |
6543 | remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels); | |
6544 | return const0_rtx; | |
6545 | } | |
6546 | break; | |
6547 | ||
6548 | case BUILT_IN_SETJMP_RECEIVER: | |
6549 | /* __builtin_setjmp_receiver is passed the receiver label. */ | |
c2f47e15 | 6550 | if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
2c8a1497 | 6551 | { |
c2f47e15 | 6552 | tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0); |
2c8a1497 | 6553 | rtx label_r = label_rtx (label); |
6554 | ||
6555 | expand_builtin_setjmp_receiver (label_r); | |
6556 | return const0_rtx; | |
6557 | } | |
6b7f6858 | 6558 | break; |
53800dbe | 6559 | |
6560 | /* __builtin_longjmp is passed a pointer to an array of five words. | |
6561 | It's similar to the C library longjmp function but works with | |
6562 | __builtin_setjmp above. */ | |
6563 | case BUILT_IN_LONGJMP: | |
c2f47e15 | 6564 | if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
53800dbe | 6565 | { |
c2f47e15 | 6566 | rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget, |
8ec3c5c2 | 6567 | VOIDmode, EXPAND_NORMAL); |
c2f47e15 | 6568 | rtx value = expand_normal (CALL_EXPR_ARG (exp, 1)); |
53800dbe | 6569 | |
6570 | if (value != const1_rtx) | |
6571 | { | |
1e5fcbe2 | 6572 | error ("%<__builtin_longjmp%> second argument must be 1"); |
53800dbe | 6573 | return const0_rtx; |
6574 | } | |
6575 | ||
6576 | expand_builtin_longjmp (buf_addr, value); | |
6577 | return const0_rtx; | |
6578 | } | |
2c8a1497 | 6579 | break; |
53800dbe | 6580 | |
4ee9c684 | 6581 | case BUILT_IN_NONLOCAL_GOTO: |
c2f47e15 | 6582 | target = expand_builtin_nonlocal_goto (exp); |
4ee9c684 | 6583 | if (target) |
6584 | return target; | |
6585 | break; | |
6586 | ||
843d08a9 | 6587 | /* This updates the setjmp buffer that is its argument with the value |
6588 | of the current stack pointer. */ | |
6589 | case BUILT_IN_UPDATE_SETJMP_BUF: | |
c2f47e15 | 6590 | if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE)) |
843d08a9 | 6591 | { |
6592 | rtx buf_addr | |
c2f47e15 | 6593 | = expand_normal (CALL_EXPR_ARG (exp, 0)); |
843d08a9 | 6594 | |
6595 | expand_builtin_update_setjmp_buf (buf_addr); | |
6596 | return const0_rtx; | |
6597 | } | |
6598 | break; | |
6599 | ||
53800dbe | 6600 | case BUILT_IN_TRAP: |
a0ef1725 | 6601 | expand_builtin_trap (); |
53800dbe | 6602 | return const0_rtx; |
6603 | ||
19bf118a | 6604 | case BUILT_IN_PRINTF: |
0b25db21 | 6605 | target = expand_builtin_printf (exp, target, mode, false); |
19bf118a | 6606 | if (target) |
6607 | return target; | |
6608 | break; | |
6609 | ||
6610 | case BUILT_IN_PRINTF_UNLOCKED: | |
0b25db21 | 6611 | target = expand_builtin_printf (exp, target, mode, true); |
19bf118a | 6612 | if (target) |
6613 | return target; | |
6614 | break; | |
6615 | ||
df94cd3b | 6616 | case BUILT_IN_FPUTS: |
c2f47e15 | 6617 | target = expand_builtin_fputs (exp, target, false); |
c013a46e | 6618 | if (target) |
6619 | return target; | |
6620 | break; | |
6621 | case BUILT_IN_FPUTS_UNLOCKED: | |
c2f47e15 | 6622 | target = expand_builtin_fputs (exp, target, true); |
19bf118a | 6623 | if (target) |
6624 | return target; | |
6625 | break; | |
6626 | ||
6627 | case BUILT_IN_FPRINTF: | |
0b25db21 | 6628 | target = expand_builtin_fprintf (exp, target, mode, false); |
19bf118a | 6629 | if (target) |
6630 | return target; | |
6631 | break; | |
6632 | ||
6633 | case BUILT_IN_FPRINTF_UNLOCKED: | |
0b25db21 | 6634 | target = expand_builtin_fprintf (exp, target, mode, true); |
df94cd3b | 6635 | if (target) |
6636 | return target; | |
6637 | break; | |
bf8e3599 | 6638 | |
6411575e | 6639 | case BUILT_IN_SPRINTF: |
c2f47e15 | 6640 | target = expand_builtin_sprintf (exp, target, mode); |
6411575e | 6641 | if (target) |
6642 | return target; | |
6643 | break; | |
6644 | ||
4f35b1fc | 6645 | CASE_FLT_FN (BUILT_IN_SIGNBIT): |
004e23c4 | 6646 | case BUILT_IN_SIGNBITD32: |
6647 | case BUILT_IN_SIGNBITD64: | |
6648 | case BUILT_IN_SIGNBITD128: | |
27f261ef | 6649 | target = expand_builtin_signbit (exp, target); |
6650 | if (target) | |
6651 | return target; | |
6652 | break; | |
6653 | ||
53800dbe | 6654 | /* Various hooks for the DWARF 2 __throw routine. */ |
6655 | case BUILT_IN_UNWIND_INIT: | |
6656 | expand_builtin_unwind_init (); | |
6657 | return const0_rtx; | |
6658 | case BUILT_IN_DWARF_CFA: | |
6659 | return virtual_cfa_rtx; | |
6660 | #ifdef DWARF2_UNWIND_INFO | |
f8f023a5 | 6661 | case BUILT_IN_DWARF_SP_COLUMN: |
6662 | return expand_builtin_dwarf_sp_column (); | |
695e919b | 6663 | case BUILT_IN_INIT_DWARF_REG_SIZES: |
c2f47e15 | 6664 | expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0)); |
695e919b | 6665 | return const0_rtx; |
53800dbe | 6666 | #endif |
6667 | case BUILT_IN_FROB_RETURN_ADDR: | |
c2f47e15 | 6668 | return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0)); |
53800dbe | 6669 | case BUILT_IN_EXTRACT_RETURN_ADDR: |
c2f47e15 | 6670 | return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0)); |
53800dbe | 6671 | case BUILT_IN_EH_RETURN: |
c2f47e15 | 6672 | expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0), |
6673 | CALL_EXPR_ARG (exp, 1)); | |
53800dbe | 6674 | return const0_rtx; |
df4b504c | 6675 | #ifdef EH_RETURN_DATA_REGNO |
6676 | case BUILT_IN_EH_RETURN_DATA_REGNO: | |
c2f47e15 | 6677 | return expand_builtin_eh_return_data_regno (exp); |
df4b504c | 6678 | #endif |
26093bf4 | 6679 | case BUILT_IN_EXTEND_POINTER: |
c2f47e15 | 6680 | return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0)); |
26093bf4 | 6681 | |
7ccc713a | 6682 | case BUILT_IN_VA_START: |
c2f47e15 | 6683 | return expand_builtin_va_start (exp); |
a66c9326 | 6684 | case BUILT_IN_VA_END: |
c2f47e15 | 6685 | return expand_builtin_va_end (exp); |
a66c9326 | 6686 | case BUILT_IN_VA_COPY: |
c2f47e15 | 6687 | return expand_builtin_va_copy (exp); |
89cfe6e5 | 6688 | case BUILT_IN_EXPECT: |
c2f47e15 | 6689 | return expand_builtin_expect (exp, target); |
5e3608d8 | 6690 | case BUILT_IN_PREFETCH: |
c2f47e15 | 6691 | expand_builtin_prefetch (exp); |
5e3608d8 | 6692 | return const0_rtx; |
6693 | ||
4ee9c684 | 6694 | case BUILT_IN_PROFILE_FUNC_ENTER: |
6695 | return expand_builtin_profile_func (false); | |
6696 | case BUILT_IN_PROFILE_FUNC_EXIT: | |
6697 | return expand_builtin_profile_func (true); | |
6698 | ||
6699 | case BUILT_IN_INIT_TRAMPOLINE: | |
c2f47e15 | 6700 | return expand_builtin_init_trampoline (exp); |
4ee9c684 | 6701 | case BUILT_IN_ADJUST_TRAMPOLINE: |
c2f47e15 | 6702 | return expand_builtin_adjust_trampoline (exp); |
4ee9c684 | 6703 | |
73673831 | 6704 | case BUILT_IN_FORK: |
6705 | case BUILT_IN_EXECL: | |
6706 | case BUILT_IN_EXECV: | |
6707 | case BUILT_IN_EXECLP: | |
6708 | case BUILT_IN_EXECLE: | |
6709 | case BUILT_IN_EXECVP: | |
6710 | case BUILT_IN_EXECVE: | |
c2f47e15 | 6711 | target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore); |
73673831 | 6712 | if (target) |
6713 | return target; | |
6714 | break; | |
53800dbe | 6715 | |
b6a5fc45 | 6716 | case BUILT_IN_FETCH_AND_ADD_1: |
6717 | case BUILT_IN_FETCH_AND_ADD_2: | |
6718 | case BUILT_IN_FETCH_AND_ADD_4: | |
6719 | case BUILT_IN_FETCH_AND_ADD_8: | |
27213ba3 | 6720 | case BUILT_IN_FETCH_AND_ADD_16: |
3e272de8 | 6721 | mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1); |
c2f47e15 | 6722 | target = expand_builtin_sync_operation (mode, exp, PLUS, |
b6a5fc45 | 6723 | false, target, ignore); |
6724 | if (target) | |
6725 | return target; | |
6726 | break; | |
6727 | ||
6728 | case BUILT_IN_FETCH_AND_SUB_1: | |
6729 | case BUILT_IN_FETCH_AND_SUB_2: | |
6730 | case BUILT_IN_FETCH_AND_SUB_4: | |
6731 | case BUILT_IN_FETCH_AND_SUB_8: | |
27213ba3 | 6732 | case BUILT_IN_FETCH_AND_SUB_16: |
3e272de8 | 6733 | mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1); |
c2f47e15 | 6734 | target = expand_builtin_sync_operation (mode, exp, MINUS, |
b6a5fc45 | 6735 | false, target, ignore); |
6736 | if (target) | |
6737 | return target; | |
6738 | break; | |
6739 | ||
6740 | case BUILT_IN_FETCH_AND_OR_1: | |
6741 | case BUILT_IN_FETCH_AND_OR_2: | |
6742 | case BUILT_IN_FETCH_AND_OR_4: | |
6743 | case BUILT_IN_FETCH_AND_OR_8: | |
27213ba3 | 6744 | case BUILT_IN_FETCH_AND_OR_16: |
3e272de8 | 6745 | mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1); |
c2f47e15 | 6746 | target = expand_builtin_sync_operation (mode, exp, IOR, |
b6a5fc45 | 6747 | false, target, ignore); |
6748 | if (target) | |
6749 | return target; | |
6750 | break; | |
6751 | ||
6752 | case BUILT_IN_FETCH_AND_AND_1: | |
6753 | case BUILT_IN_FETCH_AND_AND_2: | |
6754 | case BUILT_IN_FETCH_AND_AND_4: | |
6755 | case BUILT_IN_FETCH_AND_AND_8: | |
27213ba3 | 6756 | case BUILT_IN_FETCH_AND_AND_16: |
3e272de8 | 6757 | mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1); |
c2f47e15 | 6758 | target = expand_builtin_sync_operation (mode, exp, AND, |
b6a5fc45 | 6759 | false, target, ignore); |
6760 | if (target) | |
6761 | return target; | |
6762 | break; | |
6763 | ||
6764 | case BUILT_IN_FETCH_AND_XOR_1: | |
6765 | case BUILT_IN_FETCH_AND_XOR_2: | |
6766 | case BUILT_IN_FETCH_AND_XOR_4: | |
6767 | case BUILT_IN_FETCH_AND_XOR_8: | |
27213ba3 | 6768 | case BUILT_IN_FETCH_AND_XOR_16: |
3e272de8 | 6769 | mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1); |
c2f47e15 | 6770 | target = expand_builtin_sync_operation (mode, exp, XOR, |
b6a5fc45 | 6771 | false, target, ignore); |
6772 | if (target) | |
6773 | return target; | |
6774 | break; | |
6775 | ||
6776 | case BUILT_IN_FETCH_AND_NAND_1: | |
6777 | case BUILT_IN_FETCH_AND_NAND_2: | |
6778 | case BUILT_IN_FETCH_AND_NAND_4: | |
6779 | case BUILT_IN_FETCH_AND_NAND_8: | |
27213ba3 | 6780 | case BUILT_IN_FETCH_AND_NAND_16: |
3e272de8 | 6781 | mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1); |
c2f47e15 | 6782 | target = expand_builtin_sync_operation (mode, exp, NOT, |
b6a5fc45 | 6783 | false, target, ignore); |
6784 | if (target) | |
6785 | return target; | |
6786 | break; | |
6787 | ||
6788 | case BUILT_IN_ADD_AND_FETCH_1: | |
6789 | case BUILT_IN_ADD_AND_FETCH_2: | |
6790 | case BUILT_IN_ADD_AND_FETCH_4: | |
6791 | case BUILT_IN_ADD_AND_FETCH_8: | |
27213ba3 | 6792 | case BUILT_IN_ADD_AND_FETCH_16: |
3e272de8 | 6793 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1); |
c2f47e15 | 6794 | target = expand_builtin_sync_operation (mode, exp, PLUS, |
b6a5fc45 | 6795 | true, target, ignore); |
6796 | if (target) | |
6797 | return target; | |
6798 | break; | |
6799 | ||
6800 | case BUILT_IN_SUB_AND_FETCH_1: | |
6801 | case BUILT_IN_SUB_AND_FETCH_2: | |
6802 | case BUILT_IN_SUB_AND_FETCH_4: | |
6803 | case BUILT_IN_SUB_AND_FETCH_8: | |
27213ba3 | 6804 | case BUILT_IN_SUB_AND_FETCH_16: |
3e272de8 | 6805 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1); |
c2f47e15 | 6806 | target = expand_builtin_sync_operation (mode, exp, MINUS, |
b6a5fc45 | 6807 | true, target, ignore); |
6808 | if (target) | |
6809 | return target; | |
6810 | break; | |
6811 | ||
6812 | case BUILT_IN_OR_AND_FETCH_1: | |
6813 | case BUILT_IN_OR_AND_FETCH_2: | |
6814 | case BUILT_IN_OR_AND_FETCH_4: | |
6815 | case BUILT_IN_OR_AND_FETCH_8: | |
27213ba3 | 6816 | case BUILT_IN_OR_AND_FETCH_16: |
3e272de8 | 6817 | mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1); |
c2f47e15 | 6818 | target = expand_builtin_sync_operation (mode, exp, IOR, |
b6a5fc45 | 6819 | true, target, ignore); |
6820 | if (target) | |
6821 | return target; | |
6822 | break; | |
6823 | ||
6824 | case BUILT_IN_AND_AND_FETCH_1: | |
6825 | case BUILT_IN_AND_AND_FETCH_2: | |
6826 | case BUILT_IN_AND_AND_FETCH_4: | |
6827 | case BUILT_IN_AND_AND_FETCH_8: | |
27213ba3 | 6828 | case BUILT_IN_AND_AND_FETCH_16: |
3e272de8 | 6829 | mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1); |
c2f47e15 | 6830 | target = expand_builtin_sync_operation (mode, exp, AND, |
b6a5fc45 | 6831 | true, target, ignore); |
6832 | if (target) | |
6833 | return target; | |
6834 | break; | |
6835 | ||
6836 | case BUILT_IN_XOR_AND_FETCH_1: | |
6837 | case BUILT_IN_XOR_AND_FETCH_2: | |
6838 | case BUILT_IN_XOR_AND_FETCH_4: | |
6839 | case BUILT_IN_XOR_AND_FETCH_8: | |
27213ba3 | 6840 | case BUILT_IN_XOR_AND_FETCH_16: |
3e272de8 | 6841 | mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1); |
c2f47e15 | 6842 | target = expand_builtin_sync_operation (mode, exp, XOR, |
b6a5fc45 | 6843 | true, target, ignore); |
6844 | if (target) | |
6845 | return target; | |
6846 | break; | |
6847 | ||
6848 | case BUILT_IN_NAND_AND_FETCH_1: | |
6849 | case BUILT_IN_NAND_AND_FETCH_2: | |
6850 | case BUILT_IN_NAND_AND_FETCH_4: | |
6851 | case BUILT_IN_NAND_AND_FETCH_8: | |
27213ba3 | 6852 | case BUILT_IN_NAND_AND_FETCH_16: |
3e272de8 | 6853 | mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1); |
c2f47e15 | 6854 | target = expand_builtin_sync_operation (mode, exp, NOT, |
b6a5fc45 | 6855 | true, target, ignore); |
6856 | if (target) | |
6857 | return target; | |
6858 | break; | |
6859 | ||
6860 | case BUILT_IN_BOOL_COMPARE_AND_SWAP_1: | |
6861 | case BUILT_IN_BOOL_COMPARE_AND_SWAP_2: | |
6862 | case BUILT_IN_BOOL_COMPARE_AND_SWAP_4: | |
6863 | case BUILT_IN_BOOL_COMPARE_AND_SWAP_8: | |
27213ba3 | 6864 | case BUILT_IN_BOOL_COMPARE_AND_SWAP_16: |
a601d32a | 6865 | if (mode == VOIDmode) |
6866 | mode = TYPE_MODE (boolean_type_node); | |
b6a5fc45 | 6867 | if (!target || !register_operand (target, mode)) |
6868 | target = gen_reg_rtx (mode); | |
3e272de8 | 6869 | |
6870 | mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1); | |
c2f47e15 | 6871 | target = expand_builtin_compare_and_swap (mode, exp, true, target); |
b6a5fc45 | 6872 | if (target) |
6873 | return target; | |
6874 | break; | |
6875 | ||
6876 | case BUILT_IN_VAL_COMPARE_AND_SWAP_1: | |
6877 | case BUILT_IN_VAL_COMPARE_AND_SWAP_2: | |
6878 | case BUILT_IN_VAL_COMPARE_AND_SWAP_4: | |
6879 | case BUILT_IN_VAL_COMPARE_AND_SWAP_8: | |
27213ba3 | 6880 | case BUILT_IN_VAL_COMPARE_AND_SWAP_16: |
3e272de8 | 6881 | mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1); |
c2f47e15 | 6882 | target = expand_builtin_compare_and_swap (mode, exp, false, target); |
b6a5fc45 | 6883 | if (target) |
6884 | return target; | |
6885 | break; | |
6886 | ||
6887 | case BUILT_IN_LOCK_TEST_AND_SET_1: | |
6888 | case BUILT_IN_LOCK_TEST_AND_SET_2: | |
6889 | case BUILT_IN_LOCK_TEST_AND_SET_4: | |
6890 | case BUILT_IN_LOCK_TEST_AND_SET_8: | |
27213ba3 | 6891 | case BUILT_IN_LOCK_TEST_AND_SET_16: |
3e272de8 | 6892 | mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1); |
c2f47e15 | 6893 | target = expand_builtin_lock_test_and_set (mode, exp, target); |
b6a5fc45 | 6894 | if (target) |
6895 | return target; | |
6896 | break; | |
6897 | ||
6898 | case BUILT_IN_LOCK_RELEASE_1: | |
6899 | case BUILT_IN_LOCK_RELEASE_2: | |
6900 | case BUILT_IN_LOCK_RELEASE_4: | |
6901 | case BUILT_IN_LOCK_RELEASE_8: | |
27213ba3 | 6902 | case BUILT_IN_LOCK_RELEASE_16: |
3e272de8 | 6903 | mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1); |
c2f47e15 | 6904 | expand_builtin_lock_release (mode, exp); |
b6a5fc45 | 6905 | return const0_rtx; |
6906 | ||
6907 | case BUILT_IN_SYNCHRONIZE: | |
6908 | expand_builtin_synchronize (); | |
6909 | return const0_rtx; | |
6910 | ||
0a39fd54 | 6911 | case BUILT_IN_OBJECT_SIZE: |
6912 | return expand_builtin_object_size (exp); | |
6913 | ||
6914 | case BUILT_IN_MEMCPY_CHK: | |
6915 | case BUILT_IN_MEMPCPY_CHK: | |
6916 | case BUILT_IN_MEMMOVE_CHK: | |
6917 | case BUILT_IN_MEMSET_CHK: | |
6918 | target = expand_builtin_memory_chk (exp, target, mode, fcode); | |
6919 | if (target) | |
6920 | return target; | |
6921 | break; | |
6922 | ||
6923 | case BUILT_IN_STRCPY_CHK: | |
6924 | case BUILT_IN_STPCPY_CHK: | |
6925 | case BUILT_IN_STRNCPY_CHK: | |
6926 | case BUILT_IN_STRCAT_CHK: | |
b356dfef | 6927 | case BUILT_IN_STRNCAT_CHK: |
0a39fd54 | 6928 | case BUILT_IN_SNPRINTF_CHK: |
6929 | case BUILT_IN_VSNPRINTF_CHK: | |
6930 | maybe_emit_chk_warning (exp, fcode); | |
6931 | break; | |
6932 | ||
6933 | case BUILT_IN_SPRINTF_CHK: | |
6934 | case BUILT_IN_VSPRINTF_CHK: | |
6935 | maybe_emit_sprintf_chk_warning (exp, fcode); | |
6936 | break; | |
6937 | ||
92482ee0 | 6938 | default: /* just do library call, if unknown builtin */ |
146c1b4f | 6939 | break; |
53800dbe | 6940 | } |
6941 | ||
6942 | /* The switch statement above can drop through to cause the function | |
6943 | to be called normally. */ | |
6944 | return expand_call (exp, target, ignore); | |
6945 | } | |
650e4c94 | 6946 | |
805e22b2 | 6947 | /* Determine whether a tree node represents a call to a built-in |
52203a9d | 6948 | function. If the tree T is a call to a built-in function with |
6949 | the right number of arguments of the appropriate types, return | |
6950 | the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT. | |
6951 | Otherwise the return value is END_BUILTINS. */ | |
aecda0d6 | 6952 | |
805e22b2 | 6953 | enum built_in_function |
b7bf20db | 6954 | builtin_mathfn_code (const_tree t) |
805e22b2 | 6955 | { |
b7bf20db | 6956 | const_tree fndecl, arg, parmlist; |
6957 | const_tree argtype, parmtype; | |
6958 | const_call_expr_arg_iterator iter; | |
805e22b2 | 6959 | |
6960 | if (TREE_CODE (t) != CALL_EXPR | |
c2f47e15 | 6961 | || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR) |
805e22b2 | 6962 | return END_BUILTINS; |
6963 | ||
c6e6ecb1 | 6964 | fndecl = get_callee_fndecl (t); |
6965 | if (fndecl == NULL_TREE | |
52203a9d | 6966 | || TREE_CODE (fndecl) != FUNCTION_DECL |
805e22b2 | 6967 | || ! DECL_BUILT_IN (fndecl) |
6968 | || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) | |
6969 | return END_BUILTINS; | |
6970 | ||
52203a9d | 6971 | parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); |
b7bf20db | 6972 | init_const_call_expr_arg_iterator (t, &iter); |
52203a9d | 6973 | for (; parmlist; parmlist = TREE_CHAIN (parmlist)) |
e9f80ff5 | 6974 | { |
52203a9d | 6975 | /* If a function doesn't take a variable number of arguments, |
6976 | the last element in the list will have type `void'. */ | |
6977 | parmtype = TREE_VALUE (parmlist); | |
6978 | if (VOID_TYPE_P (parmtype)) | |
6979 | { | |
b7bf20db | 6980 | if (more_const_call_expr_args_p (&iter)) |
52203a9d | 6981 | return END_BUILTINS; |
6982 | return DECL_FUNCTION_CODE (fndecl); | |
6983 | } | |
6984 | ||
b7bf20db | 6985 | if (! more_const_call_expr_args_p (&iter)) |
e9f80ff5 | 6986 | return END_BUILTINS; |
c2f47e15 | 6987 | |
b7bf20db | 6988 | arg = next_const_call_expr_arg (&iter); |
c2f47e15 | 6989 | argtype = TREE_TYPE (arg); |
52203a9d | 6990 | |
6991 | if (SCALAR_FLOAT_TYPE_P (parmtype)) | |
6992 | { | |
6993 | if (! SCALAR_FLOAT_TYPE_P (argtype)) | |
6994 | return END_BUILTINS; | |
6995 | } | |
6996 | else if (COMPLEX_FLOAT_TYPE_P (parmtype)) | |
6997 | { | |
6998 | if (! COMPLEX_FLOAT_TYPE_P (argtype)) | |
6999 | return END_BUILTINS; | |
7000 | } | |
7001 | else if (POINTER_TYPE_P (parmtype)) | |
7002 | { | |
7003 | if (! POINTER_TYPE_P (argtype)) | |
7004 | return END_BUILTINS; | |
7005 | } | |
7006 | else if (INTEGRAL_TYPE_P (parmtype)) | |
7007 | { | |
7008 | if (! INTEGRAL_TYPE_P (argtype)) | |
7009 | return END_BUILTINS; | |
7010 | } | |
7011 | else | |
e9f80ff5 | 7012 | return END_BUILTINS; |
e9f80ff5 | 7013 | } |
7014 | ||
52203a9d | 7015 | /* Variable-length argument list. */ |
805e22b2 | 7016 | return DECL_FUNCTION_CODE (fndecl); |
7017 | } | |
7018 | ||
c2f47e15 | 7019 | /* Fold a call to __builtin_constant_p, if we know its argument ARG will |
7020 | evaluate to a constant. */ | |
650e4c94 | 7021 | |
7022 | static tree | |
c2f47e15 | 7023 | fold_builtin_constant_p (tree arg) |
650e4c94 | 7024 | { |
650e4c94 | 7025 | /* We return 1 for a numeric type that's known to be a constant |
7026 | value at compile-time or for an aggregate type that's a | |
7027 | literal constant. */ | |
c2f47e15 | 7028 | STRIP_NOPS (arg); |
650e4c94 | 7029 | |
7030 | /* If we know this is a constant, emit the constant of one. */ | |
c2f47e15 | 7031 | if (CONSTANT_CLASS_P (arg) |
7032 | || (TREE_CODE (arg) == CONSTRUCTOR | |
7033 | && TREE_CONSTANT (arg))) | |
650e4c94 | 7034 | return integer_one_node; |
c2f47e15 | 7035 | if (TREE_CODE (arg) == ADDR_EXPR) |
adcfa3a3 | 7036 | { |
c2f47e15 | 7037 | tree op = TREE_OPERAND (arg, 0); |
adcfa3a3 | 7038 | if (TREE_CODE (op) == STRING_CST |
7039 | || (TREE_CODE (op) == ARRAY_REF | |
7040 | && integer_zerop (TREE_OPERAND (op, 1)) | |
7041 | && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST)) | |
7042 | return integer_one_node; | |
7043 | } | |
650e4c94 | 7044 | |
1fb4300c | 7045 | /* If this expression has side effects, show we don't know it to be a |
7046 | constant. Likewise if it's a pointer or aggregate type since in | |
7047 | those case we only want literals, since those are only optimized | |
f97c71a1 | 7048 | when generating RTL, not later. |
7049 | And finally, if we are compiling an initializer, not code, we | |
7050 | need to return a definite result now; there's not going to be any | |
7051 | more optimization done. */ | |
c2f47e15 | 7052 | if (TREE_SIDE_EFFECTS (arg) |
7053 | || AGGREGATE_TYPE_P (TREE_TYPE (arg)) | |
7054 | || POINTER_TYPE_P (TREE_TYPE (arg)) | |
47be647d | 7055 | || cfun == 0 |
7056 | || folding_initializer) | |
650e4c94 | 7057 | return integer_zero_node; |
7058 | ||
c2f47e15 | 7059 | return NULL_TREE; |
650e4c94 | 7060 | } |
7061 | ||
76f5a783 | 7062 | /* Create builtin_expect with PRED and EXPECTED as its arguments and |
7063 | return it as a truthvalue. */ | |
4ee9c684 | 7064 | |
7065 | static tree | |
76f5a783 | 7066 | build_builtin_expect_predicate (tree pred, tree expected) |
4ee9c684 | 7067 | { |
76f5a783 | 7068 | tree fn, arg_types, pred_type, expected_type, call_expr, ret_type; |
4ee9c684 | 7069 | |
76f5a783 | 7070 | fn = built_in_decls[BUILT_IN_EXPECT]; |
7071 | arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
7072 | ret_type = TREE_TYPE (TREE_TYPE (fn)); | |
7073 | pred_type = TREE_VALUE (arg_types); | |
7074 | expected_type = TREE_VALUE (TREE_CHAIN (arg_types)); | |
7075 | ||
7076 | pred = fold_convert (pred_type, pred); | |
7077 | expected = fold_convert (expected_type, expected); | |
7078 | call_expr = build_call_expr (fn, 2, pred, expected); | |
7079 | ||
7080 | return build2 (NE_EXPR, TREE_TYPE (pred), call_expr, | |
7081 | build_int_cst (ret_type, 0)); | |
7082 | } | |
7083 | ||
7084 | /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return | |
7085 | NULL_TREE if no simplification is possible. */ | |
7086 | ||
7087 | static tree | |
7088 | fold_builtin_expect (tree arg0, tree arg1) | |
7089 | { | |
7090 | tree inner, fndecl; | |
7091 | enum tree_code code; | |
7092 | ||
7093 | /* If this is a builtin_expect within a builtin_expect keep the | |
7094 | inner one. See through a comparison against a constant. It | |
7095 | might have been added to create a thruthvalue. */ | |
7096 | inner = arg0; | |
7097 | if (COMPARISON_CLASS_P (inner) | |
7098 | && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST) | |
7099 | inner = TREE_OPERAND (inner, 0); | |
7100 | ||
7101 | if (TREE_CODE (inner) == CALL_EXPR | |
7102 | && (fndecl = get_callee_fndecl (inner)) | |
7103 | && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL | |
7104 | && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT) | |
7105 | return arg0; | |
7106 | ||
7107 | /* Distribute the expected value over short-circuiting operators. | |
7108 | See through the cast from truthvalue_type_node to long. */ | |
7109 | inner = arg0; | |
7110 | while (TREE_CODE (inner) == NOP_EXPR | |
7111 | && INTEGRAL_TYPE_P (TREE_TYPE (inner)) | |
7112 | && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner, 0)))) | |
7113 | inner = TREE_OPERAND (inner, 0); | |
7114 | ||
7115 | code = TREE_CODE (inner); | |
7116 | if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR) | |
7117 | { | |
7118 | tree op0 = TREE_OPERAND (inner, 0); | |
7119 | tree op1 = TREE_OPERAND (inner, 1); | |
7120 | ||
7121 | op0 = build_builtin_expect_predicate (op0, arg1); | |
7122 | op1 = build_builtin_expect_predicate (op1, arg1); | |
7123 | inner = build2 (code, TREE_TYPE (inner), op0, op1); | |
7124 | ||
7125 | return fold_convert (TREE_TYPE (arg0), inner); | |
7126 | } | |
7127 | ||
7128 | /* If the argument isn't invariant then there's nothing else we can do. */ | |
c7d4e749 | 7129 | if (!TREE_CONSTANT (arg0)) |
c2f47e15 | 7130 | return NULL_TREE; |
4ee9c684 | 7131 | |
76f5a783 | 7132 | /* If we expect that a comparison against the argument will fold to |
7133 | a constant return the constant. In practice, this means a true | |
7134 | constant or the address of a non-weak symbol. */ | |
7135 | inner = arg0; | |
4ee9c684 | 7136 | STRIP_NOPS (inner); |
7137 | if (TREE_CODE (inner) == ADDR_EXPR) | |
7138 | { | |
7139 | do | |
7140 | { | |
7141 | inner = TREE_OPERAND (inner, 0); | |
7142 | } | |
7143 | while (TREE_CODE (inner) == COMPONENT_REF | |
7144 | || TREE_CODE (inner) == ARRAY_REF); | |
7145 | if (DECL_P (inner) && DECL_WEAK (inner)) | |
c2f47e15 | 7146 | return NULL_TREE; |
4ee9c684 | 7147 | } |
7148 | ||
76f5a783 | 7149 | /* Otherwise, ARG0 already has the proper type for the return value. */ |
7150 | return arg0; | |
4ee9c684 | 7151 | } |
7152 | ||
c2f47e15 | 7153 | /* Fold a call to __builtin_classify_type with argument ARG. */ |
27d0c333 | 7154 | |
539a3a92 | 7155 | static tree |
c2f47e15 | 7156 | fold_builtin_classify_type (tree arg) |
539a3a92 | 7157 | { |
c2f47e15 | 7158 | if (arg == 0) |
7016c612 | 7159 | return build_int_cst (NULL_TREE, no_type_class); |
539a3a92 | 7160 | |
c2f47e15 | 7161 | return build_int_cst (NULL_TREE, type_to_class (TREE_TYPE (arg))); |
539a3a92 | 7162 | } |
7163 | ||
c2f47e15 | 7164 | /* Fold a call to __builtin_strlen with argument ARG. */ |
e6e27594 | 7165 | |
7166 | static tree | |
c2f47e15 | 7167 | fold_builtin_strlen (tree arg) |
e6e27594 | 7168 | { |
c2f47e15 | 7169 | if (!validate_arg (arg, POINTER_TYPE)) |
e6e27594 | 7170 | return NULL_TREE; |
7171 | else | |
7172 | { | |
c2f47e15 | 7173 | tree len = c_strlen (arg, 0); |
e6e27594 | 7174 | |
7175 | if (len) | |
7176 | { | |
7177 | /* Convert from the internal "sizetype" type to "size_t". */ | |
7178 | if (size_type_node) | |
7179 | len = fold_convert (size_type_node, len); | |
7180 | return len; | |
7181 | } | |
7182 | ||
7183 | return NULL_TREE; | |
7184 | } | |
7185 | } | |
7186 | ||
92c43e3c | 7187 | /* Fold a call to __builtin_inf or __builtin_huge_val. */ |
7188 | ||
7189 | static tree | |
aecda0d6 | 7190 | fold_builtin_inf (tree type, int warn) |
92c43e3c | 7191 | { |
aa870c1b | 7192 | REAL_VALUE_TYPE real; |
7193 | ||
40f4dbd5 | 7194 | /* __builtin_inff is intended to be usable to define INFINITY on all |
7195 | targets. If an infinity is not available, INFINITY expands "to a | |
7196 | positive constant of type float that overflows at translation | |
7197 | time", footnote "In this case, using INFINITY will violate the | |
7198 | constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4). | |
7199 | Thus we pedwarn to ensure this constraint violation is | |
7200 | diagnosed. */ | |
92c43e3c | 7201 | if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn) |
40f4dbd5 | 7202 | pedwarn ("target format does not support infinity"); |
92c43e3c | 7203 | |
aa870c1b | 7204 | real_inf (&real); |
7205 | return build_real (type, real); | |
92c43e3c | 7206 | } |
7207 | ||
c2f47e15 | 7208 | /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */ |
b0db7939 | 7209 | |
7210 | static tree | |
c2f47e15 | 7211 | fold_builtin_nan (tree arg, tree type, int quiet) |
b0db7939 | 7212 | { |
7213 | REAL_VALUE_TYPE real; | |
7214 | const char *str; | |
7215 | ||
c2f47e15 | 7216 | if (!validate_arg (arg, POINTER_TYPE)) |
7217 | return NULL_TREE; | |
7218 | str = c_getstr (arg); | |
b0db7939 | 7219 | if (!str) |
c2f47e15 | 7220 | return NULL_TREE; |
b0db7939 | 7221 | |
7222 | if (!real_nan (&real, str, quiet, TYPE_MODE (type))) | |
c2f47e15 | 7223 | return NULL_TREE; |
b0db7939 | 7224 | |
7225 | return build_real (type, real); | |
7226 | } | |
7227 | ||
277f8dd2 | 7228 | /* Return true if the floating point expression T has an integer value. |
7229 | We also allow +Inf, -Inf and NaN to be considered integer values. */ | |
7230 | ||
7231 | static bool | |
7232 | integer_valued_real_p (tree t) | |
7233 | { | |
7234 | switch (TREE_CODE (t)) | |
7235 | { | |
7236 | case FLOAT_EXPR: | |
7237 | return true; | |
7238 | ||
7239 | case ABS_EXPR: | |
7240 | case SAVE_EXPR: | |
277f8dd2 | 7241 | return integer_valued_real_p (TREE_OPERAND (t, 0)); |
7242 | ||
7243 | case COMPOUND_EXPR: | |
41076ef6 | 7244 | case MODIFY_EXPR: |
277f8dd2 | 7245 | case BIND_EXPR: |
35cc02b5 | 7246 | return integer_valued_real_p (GENERIC_TREE_OPERAND (t, 1)); |
277f8dd2 | 7247 | |
7248 | case PLUS_EXPR: | |
7249 | case MINUS_EXPR: | |
7250 | case MULT_EXPR: | |
7251 | case MIN_EXPR: | |
7252 | case MAX_EXPR: | |
7253 | return integer_valued_real_p (TREE_OPERAND (t, 0)) | |
7254 | && integer_valued_real_p (TREE_OPERAND (t, 1)); | |
7255 | ||
7256 | case COND_EXPR: | |
7257 | return integer_valued_real_p (TREE_OPERAND (t, 1)) | |
7258 | && integer_valued_real_p (TREE_OPERAND (t, 2)); | |
7259 | ||
7260 | case REAL_CST: | |
0570334c | 7261 | return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t))); |
277f8dd2 | 7262 | |
7263 | case NOP_EXPR: | |
7264 | { | |
7265 | tree type = TREE_TYPE (TREE_OPERAND (t, 0)); | |
7266 | if (TREE_CODE (type) == INTEGER_TYPE) | |
7267 | return true; | |
7268 | if (TREE_CODE (type) == REAL_TYPE) | |
7269 | return integer_valued_real_p (TREE_OPERAND (t, 0)); | |
7270 | break; | |
7271 | } | |
7272 | ||
7273 | case CALL_EXPR: | |
7274 | switch (builtin_mathfn_code (t)) | |
7275 | { | |
4f35b1fc | 7276 | CASE_FLT_FN (BUILT_IN_CEIL): |
7277 | CASE_FLT_FN (BUILT_IN_FLOOR): | |
7278 | CASE_FLT_FN (BUILT_IN_NEARBYINT): | |
7279 | CASE_FLT_FN (BUILT_IN_RINT): | |
7280 | CASE_FLT_FN (BUILT_IN_ROUND): | |
7281 | CASE_FLT_FN (BUILT_IN_TRUNC): | |
277f8dd2 | 7282 | return true; |
7283 | ||
d4a43a03 | 7284 | CASE_FLT_FN (BUILT_IN_FMIN): |
7285 | CASE_FLT_FN (BUILT_IN_FMAX): | |
c2f47e15 | 7286 | return integer_valued_real_p (CALL_EXPR_ARG (t, 0)) |
7287 | && integer_valued_real_p (CALL_EXPR_ARG (t, 1)); | |
d4a43a03 | 7288 | |
277f8dd2 | 7289 | default: |
7290 | break; | |
7291 | } | |
7292 | break; | |
7293 | ||
7294 | default: | |
7295 | break; | |
7296 | } | |
7297 | return false; | |
7298 | } | |
7299 | ||
c2f47e15 | 7300 | /* FNDECL is assumed to be a builtin where truncation can be propagated |
6528f4f4 | 7301 | across (for instance floor((double)f) == (double)floorf (f). |
c2f47e15 | 7302 | Do the transformation for a call with argument ARG. */ |
277f8dd2 | 7303 | |
6528f4f4 | 7304 | static tree |
c2f47e15 | 7305 | fold_trunc_transparent_mathfn (tree fndecl, tree arg) |
6528f4f4 | 7306 | { |
6528f4f4 | 7307 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); |
277f8dd2 | 7308 | |
c2f47e15 | 7309 | if (!validate_arg (arg, REAL_TYPE)) |
7310 | return NULL_TREE; | |
6528f4f4 | 7311 | |
277f8dd2 | 7312 | /* Integer rounding functions are idempotent. */ |
7313 | if (fcode == builtin_mathfn_code (arg)) | |
7314 | return arg; | |
7315 | ||
7316 | /* If argument is already integer valued, and we don't need to worry | |
7317 | about setting errno, there's no need to perform rounding. */ | |
7318 | if (! flag_errno_math && integer_valued_real_p (arg)) | |
7319 | return arg; | |
7320 | ||
7321 | if (optimize) | |
6528f4f4 | 7322 | { |
277f8dd2 | 7323 | tree arg0 = strip_float_extensions (arg); |
2426241c | 7324 | tree ftype = TREE_TYPE (TREE_TYPE (fndecl)); |
6528f4f4 | 7325 | tree newtype = TREE_TYPE (arg0); |
7326 | tree decl; | |
7327 | ||
7328 | if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype) | |
7329 | && (decl = mathfn_built_in (newtype, fcode))) | |
c2f47e15 | 7330 | return fold_convert (ftype, |
7331 | build_call_expr (decl, 1, | |
7332 | fold_convert (newtype, arg0))); | |
6528f4f4 | 7333 | } |
c2f47e15 | 7334 | return NULL_TREE; |
6528f4f4 | 7335 | } |
7336 | ||
c2f47e15 | 7337 | /* FNDECL is assumed to be builtin which can narrow the FP type of |
7338 | the argument, for instance lround((double)f) -> lroundf (f). | |
7339 | Do the transformation for a call with argument ARG. */ | |
9ed65c7f | 7340 | |
7341 | static tree | |
c2f47e15 | 7342 | fold_fixed_mathfn (tree fndecl, tree arg) |
9ed65c7f | 7343 | { |
9ed65c7f | 7344 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); |
9ed65c7f | 7345 | |
c2f47e15 | 7346 | if (!validate_arg (arg, REAL_TYPE)) |
7347 | return NULL_TREE; | |
9ed65c7f | 7348 | |
7349 | /* If argument is already integer valued, and we don't need to worry | |
7350 | about setting errno, there's no need to perform rounding. */ | |
7351 | if (! flag_errno_math && integer_valued_real_p (arg)) | |
49d00087 | 7352 | return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)), arg); |
9ed65c7f | 7353 | |
7354 | if (optimize) | |
7355 | { | |
7356 | tree ftype = TREE_TYPE (arg); | |
7357 | tree arg0 = strip_float_extensions (arg); | |
7358 | tree newtype = TREE_TYPE (arg0); | |
7359 | tree decl; | |
7360 | ||
7361 | if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype) | |
7362 | && (decl = mathfn_built_in (newtype, fcode))) | |
c2f47e15 | 7363 | return build_call_expr (decl, 1, fold_convert (newtype, arg0)); |
9ed65c7f | 7364 | } |
73a0da56 | 7365 | |
7366 | /* Canonicalize llround (x) to lround (x) on LP64 targets where | |
7367 | sizeof (long long) == sizeof (long). */ | |
7368 | if (TYPE_PRECISION (long_long_integer_type_node) | |
7369 | == TYPE_PRECISION (long_integer_type_node)) | |
7370 | { | |
7371 | tree newfn = NULL_TREE; | |
7372 | switch (fcode) | |
7373 | { | |
7374 | CASE_FLT_FN (BUILT_IN_LLCEIL): | |
7375 | newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL); | |
7376 | break; | |
7377 | ||
7378 | CASE_FLT_FN (BUILT_IN_LLFLOOR): | |
7379 | newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR); | |
7380 | break; | |
7381 | ||
7382 | CASE_FLT_FN (BUILT_IN_LLROUND): | |
7383 | newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND); | |
7384 | break; | |
7385 | ||
7386 | CASE_FLT_FN (BUILT_IN_LLRINT): | |
7387 | newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT); | |
7388 | break; | |
7389 | ||
7390 | default: | |
7391 | break; | |
7392 | } | |
7393 | ||
7394 | if (newfn) | |
7395 | { | |
c2f47e15 | 7396 | tree newcall = build_call_expr(newfn, 1, arg); |
73a0da56 | 7397 | return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), newcall); |
7398 | } | |
7399 | } | |
7400 | ||
c2f47e15 | 7401 | return NULL_TREE; |
9ed65c7f | 7402 | } |
7403 | ||
c2f47e15 | 7404 | /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the |
7405 | return type. Return NULL_TREE if no simplification can be made. */ | |
c63f4ad3 | 7406 | |
7407 | static tree | |
c2f47e15 | 7408 | fold_builtin_cabs (tree arg, tree type, tree fndecl) |
c63f4ad3 | 7409 | { |
c2f47e15 | 7410 | tree res; |
c63f4ad3 | 7411 | |
c63f4ad3 | 7412 | if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE |
7413 | || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE) | |
7414 | return NULL_TREE; | |
7415 | ||
b4725390 | 7416 | /* Calculate the result when the argument is a constant. */ |
7417 | if (TREE_CODE (arg) == COMPLEX_CST | |
7418 | && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg), | |
7419 | type, mpfr_hypot))) | |
7420 | return res; | |
7421 | ||
1af0d139 | 7422 | if (TREE_CODE (arg) == COMPLEX_EXPR) |
7423 | { | |
7424 | tree real = TREE_OPERAND (arg, 0); | |
7425 | tree imag = TREE_OPERAND (arg, 1); | |
7426 | ||
7427 | /* If either part is zero, cabs is fabs of the other. */ | |
7428 | if (real_zerop (real)) | |
7429 | return fold_build1 (ABS_EXPR, type, imag); | |
7430 | if (real_zerop (imag)) | |
7431 | return fold_build1 (ABS_EXPR, type, real); | |
7432 | ||
7433 | /* cabs(x+xi) -> fabs(x)*sqrt(2). */ | |
7434 | if (flag_unsafe_math_optimizations | |
7435 | && operand_equal_p (real, imag, OEP_PURE_SAME)) | |
7436 | { | |
2e7ca27b | 7437 | const REAL_VALUE_TYPE sqrt2_trunc |
3fa759a9 | 7438 | = real_value_truncate (TYPE_MODE (type), |
7439 | *get_real_const (rv_sqrt2)); | |
1af0d139 | 7440 | STRIP_NOPS (real); |
7441 | return fold_build2 (MULT_EXPR, type, | |
7442 | fold_build1 (ABS_EXPR, type, real), | |
2e7ca27b | 7443 | build_real (type, sqrt2_trunc)); |
1af0d139 | 7444 | } |
7445 | } | |
c63f4ad3 | 7446 | |
749891b2 | 7447 | /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */ |
7448 | if (TREE_CODE (arg) == NEGATE_EXPR | |
7449 | || TREE_CODE (arg) == CONJ_EXPR) | |
c2f47e15 | 7450 | return build_call_expr (fndecl, 1, TREE_OPERAND (arg, 0)); |
749891b2 | 7451 | |
7d3f6cc7 | 7452 | /* Don't do this when optimizing for size. */ |
7453 | if (flag_unsafe_math_optimizations | |
7454 | && optimize && !optimize_size) | |
c63f4ad3 | 7455 | { |
0da0dbfa | 7456 | tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT); |
c63f4ad3 | 7457 | |
7458 | if (sqrtfn != NULL_TREE) | |
7459 | { | |
c2f47e15 | 7460 | tree rpart, ipart, result; |
c63f4ad3 | 7461 | |
4ee9c684 | 7462 | arg = builtin_save_expr (arg); |
29a6518e | 7463 | |
49d00087 | 7464 | rpart = fold_build1 (REALPART_EXPR, type, arg); |
7465 | ipart = fold_build1 (IMAGPART_EXPR, type, arg); | |
c63f4ad3 | 7466 | |
4ee9c684 | 7467 | rpart = builtin_save_expr (rpart); |
7468 | ipart = builtin_save_expr (ipart); | |
c63f4ad3 | 7469 | |
49d00087 | 7470 | result = fold_build2 (PLUS_EXPR, type, |
7471 | fold_build2 (MULT_EXPR, type, | |
7472 | rpart, rpart), | |
7473 | fold_build2 (MULT_EXPR, type, | |
7474 | ipart, ipart)); | |
c63f4ad3 | 7475 | |
c2f47e15 | 7476 | return build_call_expr (sqrtfn, 1, result); |
c63f4ad3 | 7477 | } |
7478 | } | |
7479 | ||
7480 | return NULL_TREE; | |
7481 | } | |
7482 | ||
c2f47e15 | 7483 | /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG. |
7484 | Return NULL_TREE if no simplification can be made. */ | |
e6e27594 | 7485 | |
7486 | static tree | |
c2f47e15 | 7487 | fold_builtin_sqrt (tree arg, tree type) |
e6e27594 | 7488 | { |
7489 | ||
7490 | enum built_in_function fcode; | |
b4e8ab0c | 7491 | tree res; |
c2f47e15 | 7492 | |
7493 | if (!validate_arg (arg, REAL_TYPE)) | |
e6e27594 | 7494 | return NULL_TREE; |
7495 | ||
b4e8ab0c | 7496 | /* Calculate the result when the argument is a constant. */ |
7497 | if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true))) | |
7498 | return res; | |
7499 | ||
e6e27594 | 7500 | /* Optimize sqrt(expN(x)) = expN(x*0.5). */ |
7501 | fcode = builtin_mathfn_code (arg); | |
7502 | if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode)) | |
7503 | { | |
c2f47e15 | 7504 | tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0); |
49d00087 | 7505 | arg = fold_build2 (MULT_EXPR, type, |
c2f47e15 | 7506 | CALL_EXPR_ARG (arg, 0), |
49d00087 | 7507 | build_real (type, dconsthalf)); |
c2f47e15 | 7508 | return build_call_expr (expfn, 1, arg); |
e6e27594 | 7509 | } |
7510 | ||
7511 | /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */ | |
7512 | if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode)) | |
7513 | { | |
7514 | tree powfn = mathfn_built_in (type, BUILT_IN_POW); | |
7515 | ||
7516 | if (powfn) | |
7517 | { | |
c2f47e15 | 7518 | tree arg0 = CALL_EXPR_ARG (arg, 0); |
e6e27594 | 7519 | tree tree_root; |
7520 | /* The inner root was either sqrt or cbrt. */ | |
7521 | REAL_VALUE_TYPE dconstroot = | |
3fa759a9 | 7522 | BUILTIN_SQRT_P (fcode) ? dconsthalf : *get_real_const (rv_third); |
e6e27594 | 7523 | |
7524 | /* Adjust for the outer root. */ | |
7525 | SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1); | |
7526 | dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot); | |
7527 | tree_root = build_real (type, dconstroot); | |
c2f47e15 | 7528 | return build_call_expr (powfn, 2, arg0, tree_root); |
e6e27594 | 7529 | } |
7530 | } | |
7531 | ||
bc33117f | 7532 | /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */ |
e6e27594 | 7533 | if (flag_unsafe_math_optimizations |
7534 | && (fcode == BUILT_IN_POW | |
7535 | || fcode == BUILT_IN_POWF | |
7536 | || fcode == BUILT_IN_POWL)) | |
7537 | { | |
c2f47e15 | 7538 | tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0); |
7539 | tree arg0 = CALL_EXPR_ARG (arg, 0); | |
7540 | tree arg1 = CALL_EXPR_ARG (arg, 1); | |
bc33117f | 7541 | tree narg1; |
7542 | if (!tree_expr_nonnegative_p (arg0)) | |
7543 | arg0 = build1 (ABS_EXPR, type, arg0); | |
49d00087 | 7544 | narg1 = fold_build2 (MULT_EXPR, type, arg1, |
7545 | build_real (type, dconsthalf)); | |
c2f47e15 | 7546 | return build_call_expr (powfn, 2, arg0, narg1); |
e6e27594 | 7547 | } |
7548 | ||
7549 | return NULL_TREE; | |
7550 | } | |
7551 | ||
c2f47e15 | 7552 | /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG. |
7553 | Return NULL_TREE if no simplification can be made. */ | |
7554 | ||
e6e27594 | 7555 | static tree |
c2f47e15 | 7556 | fold_builtin_cbrt (tree arg, tree type) |
e6e27594 | 7557 | { |
e6e27594 | 7558 | const enum built_in_function fcode = builtin_mathfn_code (arg); |
29f4cd78 | 7559 | tree res; |
e6e27594 | 7560 | |
c2f47e15 | 7561 | if (!validate_arg (arg, REAL_TYPE)) |
e6e27594 | 7562 | return NULL_TREE; |
7563 | ||
29f4cd78 | 7564 | /* Calculate the result when the argument is a constant. */ |
7565 | if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0))) | |
7566 | return res; | |
e6e27594 | 7567 | |
cdfeb715 | 7568 | if (flag_unsafe_math_optimizations) |
e6e27594 | 7569 | { |
cdfeb715 | 7570 | /* Optimize cbrt(expN(x)) -> expN(x/3). */ |
7571 | if (BUILTIN_EXPONENT_P (fcode)) | |
a0c938f0 | 7572 | { |
c2f47e15 | 7573 | tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0); |
cdfeb715 | 7574 | const REAL_VALUE_TYPE third_trunc = |
3fa759a9 | 7575 | real_value_truncate (TYPE_MODE (type), *get_real_const (rv_third)); |
49d00087 | 7576 | arg = fold_build2 (MULT_EXPR, type, |
c2f47e15 | 7577 | CALL_EXPR_ARG (arg, 0), |
49d00087 | 7578 | build_real (type, third_trunc)); |
c2f47e15 | 7579 | return build_call_expr (expfn, 1, arg); |
cdfeb715 | 7580 | } |
e6e27594 | 7581 | |
cdfeb715 | 7582 | /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */ |
7583 | if (BUILTIN_SQRT_P (fcode)) | |
a0c938f0 | 7584 | { |
cdfeb715 | 7585 | tree powfn = mathfn_built_in (type, BUILT_IN_POW); |
e6e27594 | 7586 | |
cdfeb715 | 7587 | if (powfn) |
7588 | { | |
c2f47e15 | 7589 | tree arg0 = CALL_EXPR_ARG (arg, 0); |
cdfeb715 | 7590 | tree tree_root; |
3fa759a9 | 7591 | REAL_VALUE_TYPE dconstroot = *get_real_const (rv_third); |
cdfeb715 | 7592 | |
7593 | SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1); | |
7594 | dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot); | |
7595 | tree_root = build_real (type, dconstroot); | |
c2f47e15 | 7596 | return build_call_expr (powfn, 2, arg0, tree_root); |
cdfeb715 | 7597 | } |
e6e27594 | 7598 | } |
7599 | ||
cdfeb715 | 7600 | /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */ |
7601 | if (BUILTIN_CBRT_P (fcode)) | |
a0c938f0 | 7602 | { |
c2f47e15 | 7603 | tree arg0 = CALL_EXPR_ARG (arg, 0); |
cdfeb715 | 7604 | if (tree_expr_nonnegative_p (arg0)) |
7605 | { | |
7606 | tree powfn = mathfn_built_in (type, BUILT_IN_POW); | |
7607 | ||
7608 | if (powfn) | |
a0c938f0 | 7609 | { |
cdfeb715 | 7610 | tree tree_root; |
7611 | REAL_VALUE_TYPE dconstroot; | |
a0c938f0 | 7612 | |
3fa759a9 | 7613 | real_arithmetic (&dconstroot, MULT_EXPR, |
7614 | get_real_const (rv_third), | |
7615 | get_real_const (rv_third)); | |
cdfeb715 | 7616 | dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot); |
7617 | tree_root = build_real (type, dconstroot); | |
c2f47e15 | 7618 | return build_call_expr (powfn, 2, arg0, tree_root); |
cdfeb715 | 7619 | } |
7620 | } | |
7621 | } | |
a0c938f0 | 7622 | |
cdfeb715 | 7623 | /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */ |
c2f47e15 | 7624 | if (fcode == BUILT_IN_POW |
7625 | || fcode == BUILT_IN_POWF | |
cdfeb715 | 7626 | || fcode == BUILT_IN_POWL) |
a0c938f0 | 7627 | { |
c2f47e15 | 7628 | tree arg00 = CALL_EXPR_ARG (arg, 0); |
7629 | tree arg01 = CALL_EXPR_ARG (arg, 1); | |
cdfeb715 | 7630 | if (tree_expr_nonnegative_p (arg00)) |
7631 | { | |
c2f47e15 | 7632 | tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0); |
cdfeb715 | 7633 | const REAL_VALUE_TYPE dconstroot |
3fa759a9 | 7634 | = real_value_truncate (TYPE_MODE (type), |
7635 | *get_real_const (rv_third)); | |
49d00087 | 7636 | tree narg01 = fold_build2 (MULT_EXPR, type, arg01, |
7637 | build_real (type, dconstroot)); | |
c2f47e15 | 7638 | return build_call_expr (powfn, 2, arg00, narg01); |
cdfeb715 | 7639 | } |
7640 | } | |
e6e27594 | 7641 | } |
7642 | return NULL_TREE; | |
7643 | } | |
7644 | ||
c2f47e15 | 7645 | /* Fold function call to builtin cos, cosf, or cosl with argument ARG. |
7646 | TYPE is the type of the return value. Return NULL_TREE if no | |
7647 | simplification can be made. */ | |
7648 | ||
e6e27594 | 7649 | static tree |
c2f47e15 | 7650 | fold_builtin_cos (tree arg, tree type, tree fndecl) |
e6e27594 | 7651 | { |
e6ab33d8 | 7652 | tree res, narg; |
e6e27594 | 7653 | |
c2f47e15 | 7654 | if (!validate_arg (arg, REAL_TYPE)) |
e6e27594 | 7655 | return NULL_TREE; |
7656 | ||
bffb7645 | 7657 | /* Calculate the result when the argument is a constant. */ |
728bac60 | 7658 | if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0))) |
bffb7645 | 7659 | return res; |
7660 | ||
e6e27594 | 7661 | /* Optimize cos(-x) into cos (x). */ |
e6ab33d8 | 7662 | if ((narg = fold_strip_sign_ops (arg))) |
c2f47e15 | 7663 | return build_call_expr (fndecl, 1, narg); |
e6e27594 | 7664 | |
7665 | return NULL_TREE; | |
7666 | } | |
7667 | ||
c2f47e15 | 7668 | /* Fold function call to builtin cosh, coshf, or coshl with argument ARG. |
7669 | Return NULL_TREE if no simplification can be made. */ | |
7670 | ||
cacdc1af | 7671 | static tree |
c2f47e15 | 7672 | fold_builtin_cosh (tree arg, tree type, tree fndecl) |
cacdc1af | 7673 | { |
c2f47e15 | 7674 | if (validate_arg (arg, REAL_TYPE)) |
cacdc1af | 7675 | { |
cacdc1af | 7676 | tree res, narg; |
7677 | ||
7678 | /* Calculate the result when the argument is a constant. */ | |
7679 | if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0))) | |
7680 | return res; | |
7681 | ||
7682 | /* Optimize cosh(-x) into cosh (x). */ | |
7683 | if ((narg = fold_strip_sign_ops (arg))) | |
c2f47e15 | 7684 | return build_call_expr (fndecl, 1, narg); |
cacdc1af | 7685 | } |
7686 | ||
7687 | return NULL_TREE; | |
7688 | } | |
7689 | ||
c2f47e15 | 7690 | /* Fold function call to builtin tan, tanf, or tanl with argument ARG. |
7691 | Return NULL_TREE if no simplification can be made. */ | |
7692 | ||
e6e27594 | 7693 | static tree |
c2f47e15 | 7694 | fold_builtin_tan (tree arg, tree type) |
e6e27594 | 7695 | { |
7696 | enum built_in_function fcode; | |
29f4cd78 | 7697 | tree res; |
e6e27594 | 7698 | |
c2f47e15 | 7699 | if (!validate_arg (arg, REAL_TYPE)) |
e6e27594 | 7700 | return NULL_TREE; |
7701 | ||
bffb7645 | 7702 | /* Calculate the result when the argument is a constant. */ |
728bac60 | 7703 | if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0))) |
bffb7645 | 7704 | return res; |
7705 | ||
e6e27594 | 7706 | /* Optimize tan(atan(x)) = x. */ |
7707 | fcode = builtin_mathfn_code (arg); | |
7708 | if (flag_unsafe_math_optimizations | |
7709 | && (fcode == BUILT_IN_ATAN | |
7710 | || fcode == BUILT_IN_ATANF | |
7711 | || fcode == BUILT_IN_ATANL)) | |
c2f47e15 | 7712 | return CALL_EXPR_ARG (arg, 0); |
e6e27594 | 7713 | |
7714 | return NULL_TREE; | |
7715 | } | |
7716 | ||
d735c391 | 7717 | /* Fold function call to builtin sincos, sincosf, or sincosl. Return |
7718 | NULL_TREE if no simplification can be made. */ | |
7719 | ||
7720 | static tree | |
c2f47e15 | 7721 | fold_builtin_sincos (tree arg0, tree arg1, tree arg2) |
d735c391 | 7722 | { |
c2f47e15 | 7723 | tree type; |
d735c391 | 7724 | tree res, fn, call; |
7725 | ||
c2f47e15 | 7726 | if (!validate_arg (arg0, REAL_TYPE) |
7727 | || !validate_arg (arg1, POINTER_TYPE) | |
7728 | || !validate_arg (arg2, POINTER_TYPE)) | |
d735c391 | 7729 | return NULL_TREE; |
7730 | ||
d735c391 | 7731 | type = TREE_TYPE (arg0); |
d735c391 | 7732 | |
7733 | /* Calculate the result when the argument is a constant. */ | |
7734 | if ((res = do_mpfr_sincos (arg0, arg1, arg2))) | |
7735 | return res; | |
7736 | ||
7737 | /* Canonicalize sincos to cexpi. */ | |
2a6b4c77 | 7738 | if (!TARGET_C99_FUNCTIONS) |
7739 | return NULL_TREE; | |
d735c391 | 7740 | fn = mathfn_built_in (type, BUILT_IN_CEXPI); |
7741 | if (!fn) | |
7742 | return NULL_TREE; | |
7743 | ||
c2f47e15 | 7744 | call = build_call_expr (fn, 1, arg0); |
d735c391 | 7745 | call = builtin_save_expr (call); |
7746 | ||
7747 | return build2 (COMPOUND_EXPR, type, | |
7748 | build2 (MODIFY_EXPR, void_type_node, | |
7749 | build_fold_indirect_ref (arg1), | |
7750 | build1 (IMAGPART_EXPR, type, call)), | |
7751 | build2 (MODIFY_EXPR, void_type_node, | |
7752 | build_fold_indirect_ref (arg2), | |
7753 | build1 (REALPART_EXPR, type, call))); | |
7754 | } | |
7755 | ||
c5bb2c4b | 7756 | /* Fold function call to builtin cexp, cexpf, or cexpl. Return |
7757 | NULL_TREE if no simplification can be made. */ | |
7758 | ||
7759 | static tree | |
c2f47e15 | 7760 | fold_builtin_cexp (tree arg0, tree type) |
c5bb2c4b | 7761 | { |
c2f47e15 | 7762 | tree rtype; |
c5bb2c4b | 7763 | tree realp, imagp, ifn; |
7764 | ||
c2f47e15 | 7765 | if (!validate_arg (arg0, COMPLEX_TYPE)) |
c5bb2c4b | 7766 | return NULL_TREE; |
7767 | ||
c5bb2c4b | 7768 | rtype = TREE_TYPE (TREE_TYPE (arg0)); |
7769 | ||
7770 | /* In case we can figure out the real part of arg0 and it is constant zero | |
7771 | fold to cexpi. */ | |
2a6b4c77 | 7772 | if (!TARGET_C99_FUNCTIONS) |
7773 | return NULL_TREE; | |
c5bb2c4b | 7774 | ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI); |
7775 | if (!ifn) | |
7776 | return NULL_TREE; | |
7777 | ||
7778 | if ((realp = fold_unary (REALPART_EXPR, rtype, arg0)) | |
7779 | && real_zerop (realp)) | |
7780 | { | |
7781 | tree narg = fold_build1 (IMAGPART_EXPR, rtype, arg0); | |
c2f47e15 | 7782 | return build_call_expr (ifn, 1, narg); |
c5bb2c4b | 7783 | } |
7784 | ||
7785 | /* In case we can easily decompose real and imaginary parts split cexp | |
7786 | to exp (r) * cexpi (i). */ | |
7787 | if (flag_unsafe_math_optimizations | |
7788 | && realp) | |
7789 | { | |
7790 | tree rfn, rcall, icall; | |
7791 | ||
7792 | rfn = mathfn_built_in (rtype, BUILT_IN_EXP); | |
7793 | if (!rfn) | |
7794 | return NULL_TREE; | |
7795 | ||
7796 | imagp = fold_unary (IMAGPART_EXPR, rtype, arg0); | |
7797 | if (!imagp) | |
7798 | return NULL_TREE; | |
7799 | ||
c2f47e15 | 7800 | icall = build_call_expr (ifn, 1, imagp); |
c5bb2c4b | 7801 | icall = builtin_save_expr (icall); |
c2f47e15 | 7802 | rcall = build_call_expr (rfn, 1, realp); |
c5bb2c4b | 7803 | rcall = builtin_save_expr (rcall); |
71bf42bb | 7804 | return fold_build2 (COMPLEX_EXPR, type, |
7805 | fold_build2 (MULT_EXPR, rtype, | |
7806 | rcall, | |
7807 | fold_build1 (REALPART_EXPR, rtype, icall)), | |
7808 | fold_build2 (MULT_EXPR, rtype, | |
7809 | rcall, | |
7810 | fold_build1 (IMAGPART_EXPR, rtype, icall))); | |
c5bb2c4b | 7811 | } |
7812 | ||
7813 | return NULL_TREE; | |
7814 | } | |
7815 | ||
c2f47e15 | 7816 | /* Fold function call to builtin trunc, truncf or truncl with argument ARG. |
7817 | Return NULL_TREE if no simplification can be made. */ | |
277f8dd2 | 7818 | |
7819 | static tree | |
c2f47e15 | 7820 | fold_builtin_trunc (tree fndecl, tree arg) |
277f8dd2 | 7821 | { |
c2f47e15 | 7822 | if (!validate_arg (arg, REAL_TYPE)) |
7823 | return NULL_TREE; | |
277f8dd2 | 7824 | |
7825 | /* Optimize trunc of constant value. */ | |
f96bd2bf | 7826 | if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)) |
277f8dd2 | 7827 | { |
7828 | REAL_VALUE_TYPE r, x; | |
2426241c | 7829 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
277f8dd2 | 7830 | |
7831 | x = TREE_REAL_CST (arg); | |
7832 | real_trunc (&r, TYPE_MODE (type), &x); | |
7833 | return build_real (type, r); | |
7834 | } | |
7835 | ||
c2f47e15 | 7836 | return fold_trunc_transparent_mathfn (fndecl, arg); |
277f8dd2 | 7837 | } |
7838 | ||
c2f47e15 | 7839 | /* Fold function call to builtin floor, floorf or floorl with argument ARG. |
7840 | Return NULL_TREE if no simplification can be made. */ | |
277f8dd2 | 7841 | |
7842 | static tree | |
c2f47e15 | 7843 | fold_builtin_floor (tree fndecl, tree arg) |
277f8dd2 | 7844 | { |
c2f47e15 | 7845 | if (!validate_arg (arg, REAL_TYPE)) |
7846 | return NULL_TREE; | |
277f8dd2 | 7847 | |
7848 | /* Optimize floor of constant value. */ | |
f96bd2bf | 7849 | if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)) |
277f8dd2 | 7850 | { |
7851 | REAL_VALUE_TYPE x; | |
7852 | ||
7853 | x = TREE_REAL_CST (arg); | |
7854 | if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math) | |
7855 | { | |
2426241c | 7856 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
277f8dd2 | 7857 | REAL_VALUE_TYPE r; |
7858 | ||
7859 | real_floor (&r, TYPE_MODE (type), &x); | |
7860 | return build_real (type, r); | |
7861 | } | |
7862 | } | |
7863 | ||
acc2b92e | 7864 | /* Fold floor (x) where x is nonnegative to trunc (x). */ |
7865 | if (tree_expr_nonnegative_p (arg)) | |
30fe8286 | 7866 | { |
7867 | tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC); | |
7868 | if (truncfn) | |
c2f47e15 | 7869 | return build_call_expr (truncfn, 1, arg); |
30fe8286 | 7870 | } |
acc2b92e | 7871 | |
c2f47e15 | 7872 | return fold_trunc_transparent_mathfn (fndecl, arg); |
277f8dd2 | 7873 | } |
7874 | ||
c2f47e15 | 7875 | /* Fold function call to builtin ceil, ceilf or ceill with argument ARG. |
7876 | Return NULL_TREE if no simplification can be made. */ | |
277f8dd2 | 7877 | |
7878 | static tree | |
c2f47e15 | 7879 | fold_builtin_ceil (tree fndecl, tree arg) |
277f8dd2 | 7880 | { |
c2f47e15 | 7881 | if (!validate_arg (arg, REAL_TYPE)) |
7882 | return NULL_TREE; | |
277f8dd2 | 7883 | |
7884 | /* Optimize ceil of constant value. */ | |
f96bd2bf | 7885 | if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)) |
277f8dd2 | 7886 | { |
7887 | REAL_VALUE_TYPE x; | |
7888 | ||
7889 | x = TREE_REAL_CST (arg); | |
7890 | if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math) | |
7891 | { | |
2426241c | 7892 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
277f8dd2 | 7893 | REAL_VALUE_TYPE r; |
7894 | ||
7895 | real_ceil (&r, TYPE_MODE (type), &x); | |
7896 | return build_real (type, r); | |
7897 | } | |
7898 | } | |
7899 | ||
c2f47e15 | 7900 | return fold_trunc_transparent_mathfn (fndecl, arg); |
277f8dd2 | 7901 | } |
7902 | ||
c2f47e15 | 7903 | /* Fold function call to builtin round, roundf or roundl with argument ARG. |
7904 | Return NULL_TREE if no simplification can be made. */ | |
89ab3887 | 7905 | |
7906 | static tree | |
c2f47e15 | 7907 | fold_builtin_round (tree fndecl, tree arg) |
89ab3887 | 7908 | { |
c2f47e15 | 7909 | if (!validate_arg (arg, REAL_TYPE)) |
7910 | return NULL_TREE; | |
89ab3887 | 7911 | |
34f17811 | 7912 | /* Optimize round of constant value. */ |
f96bd2bf | 7913 | if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)) |
89ab3887 | 7914 | { |
7915 | REAL_VALUE_TYPE x; | |
7916 | ||
7917 | x = TREE_REAL_CST (arg); | |
7918 | if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math) | |
7919 | { | |
2426241c | 7920 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
89ab3887 | 7921 | REAL_VALUE_TYPE r; |
7922 | ||
7923 | real_round (&r, TYPE_MODE (type), &x); | |
7924 | return build_real (type, r); | |
7925 | } | |
7926 | } | |
7927 | ||
c2f47e15 | 7928 | return fold_trunc_transparent_mathfn (fndecl, arg); |
89ab3887 | 7929 | } |
7930 | ||
34f17811 | 7931 | /* Fold function call to builtin lround, lroundf or lroundl (or the |
c2f47e15 | 7932 | corresponding long long versions) and other rounding functions. ARG |
7933 | is the argument to the call. Return NULL_TREE if no simplification | |
7934 | can be made. */ | |
34f17811 | 7935 | |
7936 | static tree | |
c2f47e15 | 7937 | fold_builtin_int_roundingfn (tree fndecl, tree arg) |
34f17811 | 7938 | { |
c2f47e15 | 7939 | if (!validate_arg (arg, REAL_TYPE)) |
7940 | return NULL_TREE; | |
34f17811 | 7941 | |
7942 | /* Optimize lround of constant value. */ | |
f96bd2bf | 7943 | if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)) |
34f17811 | 7944 | { |
7945 | const REAL_VALUE_TYPE x = TREE_REAL_CST (arg); | |
7946 | ||
776a7bab | 7947 | if (real_isfinite (&x)) |
34f17811 | 7948 | { |
2426241c | 7949 | tree itype = TREE_TYPE (TREE_TYPE (fndecl)); |
ca9b061d | 7950 | tree ftype = TREE_TYPE (arg); |
7951 | unsigned HOST_WIDE_INT lo2; | |
34f17811 | 7952 | HOST_WIDE_INT hi, lo; |
7953 | REAL_VALUE_TYPE r; | |
7954 | ||
ad52b9b7 | 7955 | switch (DECL_FUNCTION_CODE (fndecl)) |
7956 | { | |
4f35b1fc | 7957 | CASE_FLT_FN (BUILT_IN_LFLOOR): |
7958 | CASE_FLT_FN (BUILT_IN_LLFLOOR): | |
ad52b9b7 | 7959 | real_floor (&r, TYPE_MODE (ftype), &x); |
7960 | break; | |
7961 | ||
4f35b1fc | 7962 | CASE_FLT_FN (BUILT_IN_LCEIL): |
7963 | CASE_FLT_FN (BUILT_IN_LLCEIL): | |
ac148751 | 7964 | real_ceil (&r, TYPE_MODE (ftype), &x); |
7965 | break; | |
7966 | ||
4f35b1fc | 7967 | CASE_FLT_FN (BUILT_IN_LROUND): |
7968 | CASE_FLT_FN (BUILT_IN_LLROUND): | |
ad52b9b7 | 7969 | real_round (&r, TYPE_MODE (ftype), &x); |
7970 | break; | |
7971 | ||
7972 | default: | |
7973 | gcc_unreachable (); | |
7974 | } | |
7975 | ||
34f17811 | 7976 | REAL_VALUE_TO_INT (&lo, &hi, r); |
ca9b061d | 7977 | if (!fit_double_type (lo, hi, &lo2, &hi, itype)) |
7978 | return build_int_cst_wide (itype, lo2, hi); | |
34f17811 | 7979 | } |
7980 | } | |
7981 | ||
acc2b92e | 7982 | switch (DECL_FUNCTION_CODE (fndecl)) |
7983 | { | |
7984 | CASE_FLT_FN (BUILT_IN_LFLOOR): | |
7985 | CASE_FLT_FN (BUILT_IN_LLFLOOR): | |
7986 | /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */ | |
7987 | if (tree_expr_nonnegative_p (arg)) | |
7988 | return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)), | |
7989 | arg); | |
7990 | break; | |
7991 | default:; | |
7992 | } | |
7993 | ||
c2f47e15 | 7994 | return fold_fixed_mathfn (fndecl, arg); |
34f17811 | 7995 | } |
7996 | ||
70fb4c07 | 7997 | /* Fold function call to builtin ffs, clz, ctz, popcount and parity |
c2f47e15 | 7998 | and their long and long long variants (i.e. ffsl and ffsll). ARG is |
7999 | the argument to the call. Return NULL_TREE if no simplification can | |
8000 | be made. */ | |
70fb4c07 | 8001 | |
8002 | static tree | |
c2f47e15 | 8003 | fold_builtin_bitop (tree fndecl, tree arg) |
70fb4c07 | 8004 | { |
c2f47e15 | 8005 | if (!validate_arg (arg, INTEGER_TYPE)) |
70fb4c07 | 8006 | return NULL_TREE; |
8007 | ||
8008 | /* Optimize for constant argument. */ | |
f96bd2bf | 8009 | if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg)) |
70fb4c07 | 8010 | { |
8011 | HOST_WIDE_INT hi, width, result; | |
8012 | unsigned HOST_WIDE_INT lo; | |
7c446c95 | 8013 | tree type; |
70fb4c07 | 8014 | |
8015 | type = TREE_TYPE (arg); | |
8016 | width = TYPE_PRECISION (type); | |
8017 | lo = TREE_INT_CST_LOW (arg); | |
8018 | ||
8019 | /* Clear all the bits that are beyond the type's precision. */ | |
8020 | if (width > HOST_BITS_PER_WIDE_INT) | |
8021 | { | |
8022 | hi = TREE_INT_CST_HIGH (arg); | |
8023 | if (width < 2 * HOST_BITS_PER_WIDE_INT) | |
8024 | hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT)); | |
8025 | } | |
8026 | else | |
8027 | { | |
8028 | hi = 0; | |
8029 | if (width < HOST_BITS_PER_WIDE_INT) | |
8030 | lo &= ~((unsigned HOST_WIDE_INT) (-1) << width); | |
8031 | } | |
8032 | ||
8033 | switch (DECL_FUNCTION_CODE (fndecl)) | |
8034 | { | |
4f35b1fc | 8035 | CASE_INT_FN (BUILT_IN_FFS): |
70fb4c07 | 8036 | if (lo != 0) |
8037 | result = exact_log2 (lo & -lo) + 1; | |
8038 | else if (hi != 0) | |
8039 | result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1; | |
8040 | else | |
8041 | result = 0; | |
8042 | break; | |
8043 | ||
4f35b1fc | 8044 | CASE_INT_FN (BUILT_IN_CLZ): |
70fb4c07 | 8045 | if (hi != 0) |
8046 | result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT; | |
8047 | else if (lo != 0) | |
8048 | result = width - floor_log2 (lo) - 1; | |
8049 | else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result)) | |
8050 | result = width; | |
8051 | break; | |
8052 | ||
4f35b1fc | 8053 | CASE_INT_FN (BUILT_IN_CTZ): |
70fb4c07 | 8054 | if (lo != 0) |
8055 | result = exact_log2 (lo & -lo); | |
8056 | else if (hi != 0) | |
8057 | result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi); | |
8058 | else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result)) | |
8059 | result = width; | |
8060 | break; | |
8061 | ||
4f35b1fc | 8062 | CASE_INT_FN (BUILT_IN_POPCOUNT): |
70fb4c07 | 8063 | result = 0; |
8064 | while (lo) | |
8065 | result++, lo &= lo - 1; | |
8066 | while (hi) | |
8067 | result++, hi &= hi - 1; | |
8068 | break; | |
8069 | ||
4f35b1fc | 8070 | CASE_INT_FN (BUILT_IN_PARITY): |
70fb4c07 | 8071 | result = 0; |
8072 | while (lo) | |
8073 | result++, lo &= lo - 1; | |
8074 | while (hi) | |
8075 | result++, hi &= hi - 1; | |
8076 | result &= 1; | |
8077 | break; | |
8078 | ||
8079 | default: | |
64db345d | 8080 | gcc_unreachable (); |
70fb4c07 | 8081 | } |
8082 | ||
2426241c | 8083 | return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result); |
70fb4c07 | 8084 | } |
8085 | ||
8086 | return NULL_TREE; | |
8087 | } | |
8088 | ||
42791117 | 8089 | /* Fold function call to builtin_bswap and the long and long long |
8090 | variants. Return NULL_TREE if no simplification can be made. */ | |
8091 | static tree | |
c2f47e15 | 8092 | fold_builtin_bswap (tree fndecl, tree arg) |
42791117 | 8093 | { |
c2f47e15 | 8094 | if (! validate_arg (arg, INTEGER_TYPE)) |
8095 | return NULL_TREE; | |
42791117 | 8096 | |
8097 | /* Optimize constant value. */ | |
f96bd2bf | 8098 | if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg)) |
42791117 | 8099 | { |
8100 | HOST_WIDE_INT hi, width, r_hi = 0; | |
8101 | unsigned HOST_WIDE_INT lo, r_lo = 0; | |
8102 | tree type; | |
8103 | ||
8104 | type = TREE_TYPE (arg); | |
8105 | width = TYPE_PRECISION (type); | |
8106 | lo = TREE_INT_CST_LOW (arg); | |
8107 | hi = TREE_INT_CST_HIGH (arg); | |
8108 | ||
8109 | switch (DECL_FUNCTION_CODE (fndecl)) | |
8110 | { | |
8111 | case BUILT_IN_BSWAP32: | |
8112 | case BUILT_IN_BSWAP64: | |
8113 | { | |
8114 | int s; | |
8115 | ||
8116 | for (s = 0; s < width; s += 8) | |
8117 | { | |
8118 | int d = width - s - 8; | |
8119 | unsigned HOST_WIDE_INT byte; | |
8120 | ||
8121 | if (s < HOST_BITS_PER_WIDE_INT) | |
8122 | byte = (lo >> s) & 0xff; | |
8123 | else | |
8124 | byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff; | |
8125 | ||
8126 | if (d < HOST_BITS_PER_WIDE_INT) | |
8127 | r_lo |= byte << d; | |
8128 | else | |
8129 | r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT); | |
8130 | } | |
8131 | } | |
8132 | ||
8133 | break; | |
8134 | ||
8135 | default: | |
8136 | gcc_unreachable (); | |
8137 | } | |
8138 | ||
8139 | if (width < HOST_BITS_PER_WIDE_INT) | |
8140 | return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo); | |
8141 | else | |
8142 | return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi); | |
8143 | } | |
8144 | ||
8145 | return NULL_TREE; | |
8146 | } | |
c2f47e15 | 8147 | |
8918c507 | 8148 | /* Return true if EXPR is the real constant contained in VALUE. */ |
8149 | ||
8150 | static bool | |
8151 | real_dconstp (tree expr, const REAL_VALUE_TYPE *value) | |
8152 | { | |
8153 | STRIP_NOPS (expr); | |
8154 | ||
8155 | return ((TREE_CODE (expr) == REAL_CST | |
f96bd2bf | 8156 | && !TREE_OVERFLOW (expr) |
a0c938f0 | 8157 | && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value)) |
8158 | || (TREE_CODE (expr) == COMPLEX_CST | |
8159 | && real_dconstp (TREE_REALPART (expr), value) | |
8160 | && real_zerop (TREE_IMAGPART (expr)))); | |
8918c507 | 8161 | } |
8162 | ||
8163 | /* A subroutine of fold_builtin to fold the various logarithmic | |
29f4cd78 | 8164 | functions. Return NULL_TREE if no simplification can me made. |
8165 | FUNC is the corresponding MPFR logarithm function. */ | |
8918c507 | 8166 | |
8167 | static tree | |
c2f47e15 | 8168 | fold_builtin_logarithm (tree fndecl, tree arg, |
29f4cd78 | 8169 | int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t)) |
8918c507 | 8170 | { |
c2f47e15 | 8171 | if (validate_arg (arg, REAL_TYPE)) |
8918c507 | 8172 | { |
8918c507 | 8173 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
29f4cd78 | 8174 | tree res; |
8918c507 | 8175 | const enum built_in_function fcode = builtin_mathfn_code (arg); |
0862b7e9 | 8176 | |
29f4cd78 | 8177 | /* Optimize log(e) = 1.0. We're never passed an exact 'e', |
8178 | instead we'll look for 'e' truncated to MODE. So only do | |
8179 | this if flag_unsafe_math_optimizations is set. */ | |
8180 | if (flag_unsafe_math_optimizations && func == mpfr_log) | |
8181 | { | |
8182 | const REAL_VALUE_TYPE e_truncated = | |
3fa759a9 | 8183 | real_value_truncate (TYPE_MODE (type), *get_real_const (rv_e)); |
29f4cd78 | 8184 | if (real_dconstp (arg, &e_truncated)) |
1f5cb383 | 8185 | return build_real (type, dconst1); |
8186 | } | |
0862b7e9 | 8187 | |
29f4cd78 | 8188 | /* Calculate the result when the argument is a constant. */ |
8189 | if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false))) | |
8190 | return res; | |
8191 | ||
8918c507 | 8192 | /* Special case, optimize logN(expN(x)) = x. */ |
8193 | if (flag_unsafe_math_optimizations | |
29f4cd78 | 8194 | && ((func == mpfr_log |
8918c507 | 8195 | && (fcode == BUILT_IN_EXP |
8196 | || fcode == BUILT_IN_EXPF | |
8197 | || fcode == BUILT_IN_EXPL)) | |
29f4cd78 | 8198 | || (func == mpfr_log2 |
8918c507 | 8199 | && (fcode == BUILT_IN_EXP2 |
8200 | || fcode == BUILT_IN_EXP2F | |
8201 | || fcode == BUILT_IN_EXP2L)) | |
29f4cd78 | 8202 | || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode))))) |
c2f47e15 | 8203 | return fold_convert (type, CALL_EXPR_ARG (arg, 0)); |
8918c507 | 8204 | |
ca273d4a | 8205 | /* Optimize logN(func()) for various exponential functions. We |
a0c938f0 | 8206 | want to determine the value "x" and the power "exponent" in |
8207 | order to transform logN(x**exponent) into exponent*logN(x). */ | |
8918c507 | 8208 | if (flag_unsafe_math_optimizations) |
a0c938f0 | 8209 | { |
8918c507 | 8210 | tree exponent = 0, x = 0; |
0862b7e9 | 8211 | |
8918c507 | 8212 | switch (fcode) |
8213 | { | |
4f35b1fc | 8214 | CASE_FLT_FN (BUILT_IN_EXP): |
8918c507 | 8215 | /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */ |
8918c507 | 8216 | x = build_real (type, |
3fa759a9 | 8217 | real_value_truncate (TYPE_MODE (type), |
8218 | *get_real_const (rv_e))); | |
c2f47e15 | 8219 | exponent = CALL_EXPR_ARG (arg, 0); |
8918c507 | 8220 | break; |
4f35b1fc | 8221 | CASE_FLT_FN (BUILT_IN_EXP2): |
8918c507 | 8222 | /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */ |
8223 | x = build_real (type, dconst2); | |
c2f47e15 | 8224 | exponent = CALL_EXPR_ARG (arg, 0); |
8918c507 | 8225 | break; |
4f35b1fc | 8226 | CASE_FLT_FN (BUILT_IN_EXP10): |
8227 | CASE_FLT_FN (BUILT_IN_POW10): | |
8918c507 | 8228 | /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */ |
3fa759a9 | 8229 | { |
8230 | REAL_VALUE_TYPE dconst10; | |
8231 | real_from_integer (&dconst10, VOIDmode, 10, 0, 0); | |
8232 | x = build_real (type, dconst10); | |
8233 | } | |
c2f47e15 | 8234 | exponent = CALL_EXPR_ARG (arg, 0); |
8918c507 | 8235 | break; |
4f35b1fc | 8236 | CASE_FLT_FN (BUILT_IN_SQRT): |
8918c507 | 8237 | /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */ |
c2f47e15 | 8238 | x = CALL_EXPR_ARG (arg, 0); |
8918c507 | 8239 | exponent = build_real (type, dconsthalf); |
8240 | break; | |
4f35b1fc | 8241 | CASE_FLT_FN (BUILT_IN_CBRT): |
8918c507 | 8242 | /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */ |
c2f47e15 | 8243 | x = CALL_EXPR_ARG (arg, 0); |
8918c507 | 8244 | exponent = build_real (type, real_value_truncate (TYPE_MODE (type), |
3fa759a9 | 8245 | *get_real_const (rv_third))); |
8918c507 | 8246 | break; |
4f35b1fc | 8247 | CASE_FLT_FN (BUILT_IN_POW): |
8918c507 | 8248 | /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */ |
c2f47e15 | 8249 | x = CALL_EXPR_ARG (arg, 0); |
8250 | exponent = CALL_EXPR_ARG (arg, 1); | |
8918c507 | 8251 | break; |
8252 | default: | |
8253 | break; | |
8254 | } | |
8255 | ||
8256 | /* Now perform the optimization. */ | |
8257 | if (x && exponent) | |
8258 | { | |
c2f47e15 | 8259 | tree logfn = build_call_expr (fndecl, 1, x); |
49d00087 | 8260 | return fold_build2 (MULT_EXPR, type, exponent, logfn); |
8918c507 | 8261 | } |
8262 | } | |
8263 | } | |
8264 | ||
c2f47e15 | 8265 | return NULL_TREE; |
8918c507 | 8266 | } |
0862b7e9 | 8267 | |
f0c477f2 | 8268 | /* Fold a builtin function call to hypot, hypotf, or hypotl. Return |
8269 | NULL_TREE if no simplification can be made. */ | |
8270 | ||
8271 | static tree | |
c2f47e15 | 8272 | fold_builtin_hypot (tree fndecl, tree arg0, tree arg1, tree type) |
f0c477f2 | 8273 | { |
e6ab33d8 | 8274 | tree res, narg0, narg1; |
f0c477f2 | 8275 | |
c2f47e15 | 8276 | if (!validate_arg (arg0, REAL_TYPE) |
8277 | || !validate_arg (arg1, REAL_TYPE)) | |
f0c477f2 | 8278 | return NULL_TREE; |
8279 | ||
8280 | /* Calculate the result when the argument is a constant. */ | |
8281 | if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot))) | |
8282 | return res; | |
8283 | ||
6c95f21c | 8284 | /* If either argument to hypot has a negate or abs, strip that off. |
8285 | E.g. hypot(-x,fabs(y)) -> hypot(x,y). */ | |
e6ab33d8 | 8286 | narg0 = fold_strip_sign_ops (arg0); |
8287 | narg1 = fold_strip_sign_ops (arg1); | |
8288 | if (narg0 || narg1) | |
8289 | { | |
c2f47e15 | 8290 | return build_call_expr (fndecl, 2, narg0 ? narg0 : arg0, |
8291 | narg1 ? narg1 : arg1); | |
6c95f21c | 8292 | } |
8293 | ||
f0c477f2 | 8294 | /* If either argument is zero, hypot is fabs of the other. */ |
8295 | if (real_zerop (arg0)) | |
8296 | return fold_build1 (ABS_EXPR, type, arg1); | |
8297 | else if (real_zerop (arg1)) | |
8298 | return fold_build1 (ABS_EXPR, type, arg0); | |
8299 | ||
6c95f21c | 8300 | /* hypot(x,x) -> fabs(x)*sqrt(2). */ |
8301 | if (flag_unsafe_math_optimizations | |
8302 | && operand_equal_p (arg0, arg1, OEP_PURE_SAME)) | |
f0c477f2 | 8303 | { |
2e7ca27b | 8304 | const REAL_VALUE_TYPE sqrt2_trunc |
3fa759a9 | 8305 | = real_value_truncate (TYPE_MODE (type), *get_real_const (rv_sqrt2)); |
6c95f21c | 8306 | return fold_build2 (MULT_EXPR, type, |
8307 | fold_build1 (ABS_EXPR, type, arg0), | |
2e7ca27b | 8308 | build_real (type, sqrt2_trunc)); |
f0c477f2 | 8309 | } |
8310 | ||
f0c477f2 | 8311 | return NULL_TREE; |
8312 | } | |
8313 | ||
8314 | ||
e6e27594 | 8315 | /* Fold a builtin function call to pow, powf, or powl. Return |
8316 | NULL_TREE if no simplification can be made. */ | |
8317 | static tree | |
c2f47e15 | 8318 | fold_builtin_pow (tree fndecl, tree arg0, tree arg1, tree type) |
e6e27594 | 8319 | { |
f0c477f2 | 8320 | tree res; |
e6e27594 | 8321 | |
c2f47e15 | 8322 | if (!validate_arg (arg0, REAL_TYPE) |
8323 | || !validate_arg (arg1, REAL_TYPE)) | |
e6e27594 | 8324 | return NULL_TREE; |
8325 | ||
f0c477f2 | 8326 | /* Calculate the result when the argument is a constant. */ |
8327 | if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow))) | |
8328 | return res; | |
8329 | ||
e6e27594 | 8330 | /* Optimize pow(1.0,y) = 1.0. */ |
8331 | if (real_onep (arg0)) | |
8332 | return omit_one_operand (type, build_real (type, dconst1), arg1); | |
8333 | ||
8334 | if (TREE_CODE (arg1) == REAL_CST | |
f96bd2bf | 8335 | && !TREE_OVERFLOW (arg1)) |
e6e27594 | 8336 | { |
198d9bbe | 8337 | REAL_VALUE_TYPE cint; |
e6e27594 | 8338 | REAL_VALUE_TYPE c; |
198d9bbe | 8339 | HOST_WIDE_INT n; |
8340 | ||
e6e27594 | 8341 | c = TREE_REAL_CST (arg1); |
8342 | ||
8343 | /* Optimize pow(x,0.0) = 1.0. */ | |
8344 | if (REAL_VALUES_EQUAL (c, dconst0)) | |
8345 | return omit_one_operand (type, build_real (type, dconst1), | |
8346 | arg0); | |
8347 | ||
8348 | /* Optimize pow(x,1.0) = x. */ | |
8349 | if (REAL_VALUES_EQUAL (c, dconst1)) | |
8350 | return arg0; | |
8351 | ||
8352 | /* Optimize pow(x,-1.0) = 1.0/x. */ | |
8353 | if (REAL_VALUES_EQUAL (c, dconstm1)) | |
49d00087 | 8354 | return fold_build2 (RDIV_EXPR, type, |
8355 | build_real (type, dconst1), arg0); | |
e6e27594 | 8356 | |
8357 | /* Optimize pow(x,0.5) = sqrt(x). */ | |
8358 | if (flag_unsafe_math_optimizations | |
8359 | && REAL_VALUES_EQUAL (c, dconsthalf)) | |
8360 | { | |
8361 | tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT); | |
8362 | ||
8363 | if (sqrtfn != NULL_TREE) | |
c2f47e15 | 8364 | return build_call_expr (sqrtfn, 1, arg0); |
e6e27594 | 8365 | } |
8366 | ||
feb5b3eb | 8367 | /* Optimize pow(x,1.0/3.0) = cbrt(x). */ |
8368 | if (flag_unsafe_math_optimizations) | |
8369 | { | |
8370 | const REAL_VALUE_TYPE dconstroot | |
3fa759a9 | 8371 | = real_value_truncate (TYPE_MODE (type), |
8372 | *get_real_const (rv_third)); | |
feb5b3eb | 8373 | |
8374 | if (REAL_VALUES_EQUAL (c, dconstroot)) | |
8375 | { | |
8376 | tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT); | |
8377 | if (cbrtfn != NULL_TREE) | |
c2f47e15 | 8378 | return build_call_expr (cbrtfn, 1, arg0); |
feb5b3eb | 8379 | } |
8380 | } | |
8381 | ||
198d9bbe | 8382 | /* Check for an integer exponent. */ |
8383 | n = real_to_integer (&c); | |
8384 | real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0); | |
8385 | if (real_identical (&c, &cint)) | |
e6e27594 | 8386 | { |
198d9bbe | 8387 | /* Attempt to evaluate pow at compile-time. */ |
8388 | if (TREE_CODE (arg0) == REAL_CST | |
f96bd2bf | 8389 | && !TREE_OVERFLOW (arg0)) |
e6e27594 | 8390 | { |
8391 | REAL_VALUE_TYPE x; | |
8392 | bool inexact; | |
8393 | ||
8394 | x = TREE_REAL_CST (arg0); | |
8395 | inexact = real_powi (&x, TYPE_MODE (type), &x, n); | |
8396 | if (flag_unsafe_math_optimizations || !inexact) | |
8397 | return build_real (type, x); | |
8398 | } | |
198d9bbe | 8399 | |
8400 | /* Strip sign ops from even integer powers. */ | |
8401 | if ((n & 1) == 0 && flag_unsafe_math_optimizations) | |
8402 | { | |
8403 | tree narg0 = fold_strip_sign_ops (arg0); | |
8404 | if (narg0) | |
c2f47e15 | 8405 | return build_call_expr (fndecl, 2, narg0, arg1); |
198d9bbe | 8406 | } |
e6e27594 | 8407 | } |
8408 | } | |
8409 | ||
cdfeb715 | 8410 | if (flag_unsafe_math_optimizations) |
e6e27594 | 8411 | { |
cdfeb715 | 8412 | const enum built_in_function fcode = builtin_mathfn_code (arg0); |
e6e27594 | 8413 | |
cdfeb715 | 8414 | /* Optimize pow(expN(x),y) = expN(x*y). */ |
8415 | if (BUILTIN_EXPONENT_P (fcode)) | |
a0c938f0 | 8416 | { |
c2f47e15 | 8417 | tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0); |
8418 | tree arg = CALL_EXPR_ARG (arg0, 0); | |
49d00087 | 8419 | arg = fold_build2 (MULT_EXPR, type, arg, arg1); |
c2f47e15 | 8420 | return build_call_expr (expfn, 1, arg); |
cdfeb715 | 8421 | } |
e6e27594 | 8422 | |
cdfeb715 | 8423 | /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */ |
8424 | if (BUILTIN_SQRT_P (fcode)) | |
a0c938f0 | 8425 | { |
c2f47e15 | 8426 | tree narg0 = CALL_EXPR_ARG (arg0, 0); |
49d00087 | 8427 | tree narg1 = fold_build2 (MULT_EXPR, type, arg1, |
8428 | build_real (type, dconsthalf)); | |
c2f47e15 | 8429 | return build_call_expr (fndecl, 2, narg0, narg1); |
cdfeb715 | 8430 | } |
8431 | ||
8432 | /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */ | |
8433 | if (BUILTIN_CBRT_P (fcode)) | |
a0c938f0 | 8434 | { |
c2f47e15 | 8435 | tree arg = CALL_EXPR_ARG (arg0, 0); |
cdfeb715 | 8436 | if (tree_expr_nonnegative_p (arg)) |
8437 | { | |
8438 | const REAL_VALUE_TYPE dconstroot | |
3fa759a9 | 8439 | = real_value_truncate (TYPE_MODE (type), |
8440 | *get_real_const (rv_third)); | |
49d00087 | 8441 | tree narg1 = fold_build2 (MULT_EXPR, type, arg1, |
8442 | build_real (type, dconstroot)); | |
c2f47e15 | 8443 | return build_call_expr (fndecl, 2, arg, narg1); |
cdfeb715 | 8444 | } |
8445 | } | |
a0c938f0 | 8446 | |
cdfeb715 | 8447 | /* Optimize pow(pow(x,y),z) = pow(x,y*z). */ |
c2f47e15 | 8448 | if (fcode == BUILT_IN_POW |
8449 | || fcode == BUILT_IN_POWF | |
8450 | || fcode == BUILT_IN_POWL) | |
a0c938f0 | 8451 | { |
c2f47e15 | 8452 | tree arg00 = CALL_EXPR_ARG (arg0, 0); |
8453 | tree arg01 = CALL_EXPR_ARG (arg0, 1); | |
49d00087 | 8454 | tree narg1 = fold_build2 (MULT_EXPR, type, arg01, arg1); |
c2f47e15 | 8455 | return build_call_expr (fndecl, 2, arg00, narg1); |
cdfeb715 | 8456 | } |
e6e27594 | 8457 | } |
cdfeb715 | 8458 | |
e6e27594 | 8459 | return NULL_TREE; |
8460 | } | |
8461 | ||
c2f47e15 | 8462 | /* Fold a builtin function call to powi, powif, or powil with argument ARG. |
8463 | Return NULL_TREE if no simplification can be made. */ | |
b4d0c20c | 8464 | static tree |
c2f47e15 | 8465 | fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED, |
8466 | tree arg0, tree arg1, tree type) | |
b4d0c20c | 8467 | { |
c2f47e15 | 8468 | if (!validate_arg (arg0, REAL_TYPE) |
8469 | || !validate_arg (arg1, INTEGER_TYPE)) | |
b4d0c20c | 8470 | return NULL_TREE; |
8471 | ||
8472 | /* Optimize pow(1.0,y) = 1.0. */ | |
8473 | if (real_onep (arg0)) | |
8474 | return omit_one_operand (type, build_real (type, dconst1), arg1); | |
8475 | ||
8476 | if (host_integerp (arg1, 0)) | |
8477 | { | |
8478 | HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1); | |
8479 | ||
8480 | /* Evaluate powi at compile-time. */ | |
8481 | if (TREE_CODE (arg0) == REAL_CST | |
f96bd2bf | 8482 | && !TREE_OVERFLOW (arg0)) |
b4d0c20c | 8483 | { |
8484 | REAL_VALUE_TYPE x; | |
8485 | x = TREE_REAL_CST (arg0); | |
8486 | real_powi (&x, TYPE_MODE (type), &x, c); | |
8487 | return build_real (type, x); | |
8488 | } | |
8489 | ||
8490 | /* Optimize pow(x,0) = 1.0. */ | |
8491 | if (c == 0) | |
8492 | return omit_one_operand (type, build_real (type, dconst1), | |
8493 | arg0); | |
8494 | ||
8495 | /* Optimize pow(x,1) = x. */ | |
8496 | if (c == 1) | |
8497 | return arg0; | |
8498 | ||
8499 | /* Optimize pow(x,-1) = 1.0/x. */ | |
8500 | if (c == -1) | |
49d00087 | 8501 | return fold_build2 (RDIV_EXPR, type, |
8502 | build_real (type, dconst1), arg0); | |
b4d0c20c | 8503 | } |
8504 | ||
8505 | return NULL_TREE; | |
8506 | } | |
8507 | ||
8918c507 | 8508 | /* A subroutine of fold_builtin to fold the various exponent |
c2f47e15 | 8509 | functions. Return NULL_TREE if no simplification can be made. |
debf9994 | 8510 | FUNC is the corresponding MPFR exponent function. */ |
8918c507 | 8511 | |
8512 | static tree | |
c2f47e15 | 8513 | fold_builtin_exponent (tree fndecl, tree arg, |
debf9994 | 8514 | int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t)) |
8918c507 | 8515 | { |
c2f47e15 | 8516 | if (validate_arg (arg, REAL_TYPE)) |
8918c507 | 8517 | { |
8918c507 | 8518 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
29f4cd78 | 8519 | tree res; |
debf9994 | 8520 | |
8521 | /* Calculate the result when the argument is a constant. */ | |
728bac60 | 8522 | if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0))) |
debf9994 | 8523 | return res; |
8918c507 | 8524 | |
8525 | /* Optimize expN(logN(x)) = x. */ | |
8526 | if (flag_unsafe_math_optimizations) | |
a0c938f0 | 8527 | { |
8918c507 | 8528 | const enum built_in_function fcode = builtin_mathfn_code (arg); |
8529 | ||
debf9994 | 8530 | if ((func == mpfr_exp |
8918c507 | 8531 | && (fcode == BUILT_IN_LOG |
8532 | || fcode == BUILT_IN_LOGF | |
8533 | || fcode == BUILT_IN_LOGL)) | |
debf9994 | 8534 | || (func == mpfr_exp2 |
8918c507 | 8535 | && (fcode == BUILT_IN_LOG2 |
8536 | || fcode == BUILT_IN_LOG2F | |
8537 | || fcode == BUILT_IN_LOG2L)) | |
debf9994 | 8538 | || (func == mpfr_exp10 |
8918c507 | 8539 | && (fcode == BUILT_IN_LOG10 |
8540 | || fcode == BUILT_IN_LOG10F | |
8541 | || fcode == BUILT_IN_LOG10L))) | |
c2f47e15 | 8542 | return fold_convert (type, CALL_EXPR_ARG (arg, 0)); |
8918c507 | 8543 | } |
8544 | } | |
8545 | ||
c2f47e15 | 8546 | return NULL_TREE; |
8918c507 | 8547 | } |
8548 | ||
e973ffcc | 8549 | /* Return true if VAR is a VAR_DECL or a component thereof. */ |
8550 | ||
8551 | static bool | |
8552 | var_decl_component_p (tree var) | |
8553 | { | |
8554 | tree inner = var; | |
8555 | while (handled_component_p (inner)) | |
8556 | inner = TREE_OPERAND (inner, 0); | |
8557 | return SSA_VAR_P (inner); | |
8558 | } | |
8559 | ||
4f46f2b9 | 8560 | /* Fold function call to builtin memset. Return |
9c8a1629 | 8561 | NULL_TREE if no simplification can be made. */ |
8562 | ||
8563 | static tree | |
c2f47e15 | 8564 | fold_builtin_memset (tree dest, tree c, tree len, tree type, bool ignore) |
9c8a1629 | 8565 | { |
c2f47e15 | 8566 | tree var, ret; |
4f46f2b9 | 8567 | unsigned HOST_WIDE_INT length, cval; |
9c8a1629 | 8568 | |
c2f47e15 | 8569 | if (! validate_arg (dest, POINTER_TYPE) |
8570 | || ! validate_arg (c, INTEGER_TYPE) | |
8571 | || ! validate_arg (len, INTEGER_TYPE)) | |
8572 | return NULL_TREE; | |
9c8a1629 | 8573 | |
4f46f2b9 | 8574 | if (! host_integerp (len, 1)) |
c2f47e15 | 8575 | return NULL_TREE; |
4f46f2b9 | 8576 | |
9c8a1629 | 8577 | /* If the LEN parameter is zero, return DEST. */ |
8578 | if (integer_zerop (len)) | |
4f46f2b9 | 8579 | return omit_one_operand (type, dest, c); |
9c8a1629 | 8580 | |
4f46f2b9 | 8581 | if (! host_integerp (c, 1) || TREE_SIDE_EFFECTS (dest)) |
c2f47e15 | 8582 | return NULL_TREE; |
9c8a1629 | 8583 | |
4f46f2b9 | 8584 | var = dest; |
8585 | STRIP_NOPS (var); | |
8586 | if (TREE_CODE (var) != ADDR_EXPR) | |
c2f47e15 | 8587 | return NULL_TREE; |
4f46f2b9 | 8588 | |
8589 | var = TREE_OPERAND (var, 0); | |
8590 | if (TREE_THIS_VOLATILE (var)) | |
c2f47e15 | 8591 | return NULL_TREE; |
4f46f2b9 | 8592 | |
8593 | if (!INTEGRAL_TYPE_P (TREE_TYPE (var)) | |
8594 | && !POINTER_TYPE_P (TREE_TYPE (var))) | |
c2f47e15 | 8595 | return NULL_TREE; |
4f46f2b9 | 8596 | |
e973ffcc | 8597 | if (! var_decl_component_p (var)) |
c2f47e15 | 8598 | return NULL_TREE; |
e973ffcc | 8599 | |
4f46f2b9 | 8600 | length = tree_low_cst (len, 1); |
8601 | if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (var))) != length | |
8602 | || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT | |
8603 | < (int) length) | |
c2f47e15 | 8604 | return NULL_TREE; |
4f46f2b9 | 8605 | |
8606 | if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) | |
c2f47e15 | 8607 | return NULL_TREE; |
4f46f2b9 | 8608 | |
8609 | if (integer_zerop (c)) | |
8610 | cval = 0; | |
8611 | else | |
8612 | { | |
8613 | if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64) | |
c2f47e15 | 8614 | return NULL_TREE; |
4f46f2b9 | 8615 | |
8616 | cval = tree_low_cst (c, 1); | |
8617 | cval &= 0xff; | |
8618 | cval |= cval << 8; | |
8619 | cval |= cval << 16; | |
8620 | cval |= (cval << 31) << 1; | |
8621 | } | |
8622 | ||
8623 | ret = build_int_cst_type (TREE_TYPE (var), cval); | |
41076ef6 | 8624 | ret = build2 (MODIFY_EXPR, TREE_TYPE (var), var, ret); |
4f46f2b9 | 8625 | if (ignore) |
8626 | return ret; | |
8627 | ||
8628 | return omit_one_operand (type, dest, ret); | |
9c8a1629 | 8629 | } |
8630 | ||
4f46f2b9 | 8631 | /* Fold function call to builtin memset. Return |
9c8a1629 | 8632 | NULL_TREE if no simplification can be made. */ |
8633 | ||
8634 | static tree | |
c2f47e15 | 8635 | fold_builtin_bzero (tree dest, tree size, bool ignore) |
9c8a1629 | 8636 | { |
c2f47e15 | 8637 | if (! validate_arg (dest, POINTER_TYPE) |
8638 | || ! validate_arg (size, INTEGER_TYPE)) | |
8639 | return NULL_TREE; | |
9c8a1629 | 8640 | |
4f46f2b9 | 8641 | if (!ignore) |
c2f47e15 | 8642 | return NULL_TREE; |
a0c938f0 | 8643 | |
4f46f2b9 | 8644 | /* New argument list transforming bzero(ptr x, int y) to |
8645 | memset(ptr x, int 0, size_t y). This is done this way | |
8646 | so that if it isn't expanded inline, we fallback to | |
8647 | calling bzero instead of memset. */ | |
8648 | ||
c2f47e15 | 8649 | return fold_builtin_memset (dest, integer_zero_node, |
8650 | fold_convert (sizetype, size), | |
8651 | void_type_node, ignore); | |
9c8a1629 | 8652 | } |
8653 | ||
4f46f2b9 | 8654 | /* Fold function call to builtin mem{{,p}cpy,move}. Return |
8655 | NULL_TREE if no simplification can be made. | |
8656 | If ENDP is 0, return DEST (like memcpy). | |
8657 | If ENDP is 1, return DEST+LEN (like mempcpy). | |
8658 | If ENDP is 2, return DEST+LEN-1 (like stpcpy). | |
8659 | If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap | |
8660 | (memmove). */ | |
9c8a1629 | 8661 | |
8662 | static tree | |
c2f47e15 | 8663 | fold_builtin_memory_op (tree dest, tree src, tree len, tree type, bool ignore, int endp) |
9c8a1629 | 8664 | { |
c2f47e15 | 8665 | tree destvar, srcvar, expr; |
9c8a1629 | 8666 | |
c2f47e15 | 8667 | if (! validate_arg (dest, POINTER_TYPE) |
8668 | || ! validate_arg (src, POINTER_TYPE) | |
8669 | || ! validate_arg (len, INTEGER_TYPE)) | |
8670 | return NULL_TREE; | |
9c8a1629 | 8671 | |
8672 | /* If the LEN parameter is zero, return DEST. */ | |
8673 | if (integer_zerop (len)) | |
7e72af53 | 8674 | return omit_one_operand (type, dest, src); |
9c8a1629 | 8675 | |
4f46f2b9 | 8676 | /* If SRC and DEST are the same (and not volatile), return |
8677 | DEST{,+LEN,+LEN-1}. */ | |
9c8a1629 | 8678 | if (operand_equal_p (src, dest, 0)) |
4f46f2b9 | 8679 | expr = len; |
8680 | else | |
8681 | { | |
5a84fdd6 | 8682 | tree srctype, desttype; |
3b1757a2 | 8683 | if (endp == 3) |
8684 | { | |
5a84fdd6 | 8685 | int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT); |
8686 | int dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT); | |
8687 | ||
3b1757a2 | 8688 | /* Both DEST and SRC must be pointer types. |
8689 | ??? This is what old code did. Is the testing for pointer types | |
8690 | really mandatory? | |
8691 | ||
8692 | If either SRC is readonly or length is 1, we can use memcpy. */ | |
8693 | if (dest_align && src_align | |
8694 | && (readonly_data_expr (src) | |
5a84fdd6 | 8695 | || (host_integerp (len, 1) |
b4fc673b | 8696 | && (MIN (src_align, dest_align) / BITS_PER_UNIT >= |
5a84fdd6 | 8697 | tree_low_cst (len, 1))))) |
3b1757a2 | 8698 | { |
8699 | tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY]; | |
8700 | if (!fn) | |
c2f47e15 | 8701 | return NULL_TREE; |
8702 | return build_call_expr (fn, 3, dest, src, len); | |
3b1757a2 | 8703 | } |
c2f47e15 | 8704 | return NULL_TREE; |
3b1757a2 | 8705 | } |
4f46f2b9 | 8706 | |
5a84fdd6 | 8707 | if (!host_integerp (len, 0)) |
c2f47e15 | 8708 | return NULL_TREE; |
5a84fdd6 | 8709 | /* FIXME: |
8710 | This logic lose for arguments like (type *)malloc (sizeof (type)), | |
8711 | since we strip the casts of up to VOID return value from malloc. | |
8712 | Perhaps we ought to inherit type from non-VOID argument here? */ | |
8713 | STRIP_NOPS (src); | |
8714 | STRIP_NOPS (dest); | |
8715 | srctype = TREE_TYPE (TREE_TYPE (src)); | |
8716 | desttype = TREE_TYPE (TREE_TYPE (dest)); | |
8717 | if (!srctype || !desttype | |
8718 | || !TYPE_SIZE_UNIT (srctype) | |
8719 | || !TYPE_SIZE_UNIT (desttype) | |
8720 | || TREE_CODE (TYPE_SIZE_UNIT (srctype)) != INTEGER_CST | |
8721 | || TREE_CODE (TYPE_SIZE_UNIT (desttype)) != INTEGER_CST | |
92552a26 | 8722 | || !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len) |
8723 | || !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len)) | |
c2f47e15 | 8724 | return NULL_TREE; |
4f46f2b9 | 8725 | |
5a84fdd6 | 8726 | if (get_pointer_alignment (dest, BIGGEST_ALIGNMENT) |
8727 | < (int) TYPE_ALIGN (desttype) | |
8728 | || (get_pointer_alignment (src, BIGGEST_ALIGNMENT) | |
8729 | < (int) TYPE_ALIGN (srctype))) | |
c2f47e15 | 8730 | return NULL_TREE; |
e973ffcc | 8731 | |
5a84fdd6 | 8732 | if (!ignore) |
8733 | dest = builtin_save_expr (dest); | |
4f46f2b9 | 8734 | |
5a84fdd6 | 8735 | srcvar = build_fold_indirect_ref (src); |
4f46f2b9 | 8736 | if (TREE_THIS_VOLATILE (srcvar)) |
c2f47e15 | 8737 | return NULL_TREE; |
92552a26 | 8738 | if (!tree_int_cst_equal (lang_hooks.expr_size (srcvar), len)) |
c2f47e15 | 8739 | return NULL_TREE; |
5a84fdd6 | 8740 | /* With memcpy, it is possible to bypass aliasing rules, so without |
f0b5f617 | 8741 | this check i.e. execute/20060930-2.c would be misoptimized, because |
5a84fdd6 | 8742 | it use conflicting alias set to hold argument for the memcpy call. |
f0b5f617 | 8743 | This check is probably unnecessary with -fno-strict-aliasing. |
5a84fdd6 | 8744 | Similarly for destvar. See also PR29286. */ |
8745 | if (!var_decl_component_p (srcvar) | |
8746 | /* Accept: memcpy (*char_var, "test", 1); that simplify | |
8747 | to char_var='t'; */ | |
8748 | || is_gimple_min_invariant (srcvar) | |
8749 | || readonly_data_expr (src)) | |
c2f47e15 | 8750 | return NULL_TREE; |
4f46f2b9 | 8751 | |
5a84fdd6 | 8752 | destvar = build_fold_indirect_ref (dest); |
8753 | if (TREE_THIS_VOLATILE (destvar)) | |
c2f47e15 | 8754 | return NULL_TREE; |
92552a26 | 8755 | if (!tree_int_cst_equal (lang_hooks.expr_size (destvar), len)) |
c2f47e15 | 8756 | return NULL_TREE; |
5a84fdd6 | 8757 | if (!var_decl_component_p (destvar)) |
c2f47e15 | 8758 | return NULL_TREE; |
4f46f2b9 | 8759 | |
5a84fdd6 | 8760 | if (srctype == desttype |
2d04fd8d | 8761 | || (gimple_in_ssa_p (cfun) |
548044d8 | 8762 | && useless_type_conversion_p (desttype, srctype))) |
5a84fdd6 | 8763 | expr = srcvar; |
8764 | else if ((INTEGRAL_TYPE_P (TREE_TYPE (srcvar)) | |
4f46f2b9 | 8765 | || POINTER_TYPE_P (TREE_TYPE (srcvar))) |
8766 | && (INTEGRAL_TYPE_P (TREE_TYPE (destvar)) | |
8767 | || POINTER_TYPE_P (TREE_TYPE (destvar)))) | |
8768 | expr = fold_convert (TREE_TYPE (destvar), srcvar); | |
8769 | else | |
8770 | expr = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (destvar), srcvar); | |
41076ef6 | 8771 | expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, expr); |
4f46f2b9 | 8772 | } |
8773 | ||
8774 | if (ignore) | |
8775 | return expr; | |
8776 | ||
8777 | if (endp == 0 || endp == 3) | |
8778 | return omit_one_operand (type, dest, expr); | |
8779 | ||
8780 | if (expr == len) | |
c2f47e15 | 8781 | expr = NULL_TREE; |
4f46f2b9 | 8782 | |
8783 | if (endp == 2) | |
8784 | len = fold_build2 (MINUS_EXPR, TREE_TYPE (len), len, | |
8785 | ssize_int (1)); | |
8786 | ||
0de36bdb | 8787 | dest = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len); |
4f46f2b9 | 8788 | dest = fold_convert (type, dest); |
8789 | if (expr) | |
8790 | dest = omit_one_operand (type, dest, expr); | |
8791 | return dest; | |
8792 | } | |
8793 | ||
c2f47e15 | 8794 | /* Fold function call to builtin strcpy with arguments DEST and SRC. |
8795 | If LEN is not NULL, it represents the length of the string to be | |
8796 | copied. Return NULL_TREE if no simplification can be made. */ | |
9c8a1629 | 8797 | |
f0613857 | 8798 | tree |
c2f47e15 | 8799 | fold_builtin_strcpy (tree fndecl, tree dest, tree src, tree len) |
9c8a1629 | 8800 | { |
c2f47e15 | 8801 | tree fn; |
9c8a1629 | 8802 | |
c2f47e15 | 8803 | if (!validate_arg (dest, POINTER_TYPE) |
8804 | || !validate_arg (src, POINTER_TYPE)) | |
8805 | return NULL_TREE; | |
9c8a1629 | 8806 | |
8807 | /* If SRC and DEST are the same (and not volatile), return DEST. */ | |
8808 | if (operand_equal_p (src, dest, 0)) | |
2426241c | 8809 | return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest); |
9c8a1629 | 8810 | |
f0613857 | 8811 | if (optimize_size) |
c2f47e15 | 8812 | return NULL_TREE; |
f0613857 | 8813 | |
8814 | fn = implicit_built_in_decls[BUILT_IN_MEMCPY]; | |
8815 | if (!fn) | |
c2f47e15 | 8816 | return NULL_TREE; |
f0613857 | 8817 | |
8818 | if (!len) | |
8819 | { | |
8820 | len = c_strlen (src, 1); | |
8821 | if (! len || TREE_SIDE_EFFECTS (len)) | |
c2f47e15 | 8822 | return NULL_TREE; |
f0613857 | 8823 | } |
8824 | ||
8825 | len = size_binop (PLUS_EXPR, len, ssize_int (1)); | |
2426241c | 8826 | return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), |
c2f47e15 | 8827 | build_call_expr (fn, 3, dest, src, len)); |
9c8a1629 | 8828 | } |
8829 | ||
c2f47e15 | 8830 | /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN. |
8831 | If SLEN is not NULL, it represents the length of the source string. | |
8832 | Return NULL_TREE if no simplification can be made. */ | |
9c8a1629 | 8833 | |
f0613857 | 8834 | tree |
c2f47e15 | 8835 | fold_builtin_strncpy (tree fndecl, tree dest, tree src, tree len, tree slen) |
9c8a1629 | 8836 | { |
c2f47e15 | 8837 | tree fn; |
9c8a1629 | 8838 | |
c2f47e15 | 8839 | if (!validate_arg (dest, POINTER_TYPE) |
8840 | || !validate_arg (src, POINTER_TYPE) | |
8841 | || !validate_arg (len, INTEGER_TYPE)) | |
8842 | return NULL_TREE; | |
9c8a1629 | 8843 | |
8844 | /* If the LEN parameter is zero, return DEST. */ | |
8845 | if (integer_zerop (len)) | |
2426241c | 8846 | return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src); |
9c8a1629 | 8847 | |
19226313 | 8848 | /* We can't compare slen with len as constants below if len is not a |
8849 | constant. */ | |
8850 | if (len == 0 || TREE_CODE (len) != INTEGER_CST) | |
c2f47e15 | 8851 | return NULL_TREE; |
19226313 | 8852 | |
f0613857 | 8853 | if (!slen) |
8854 | slen = c_strlen (src, 1); | |
8855 | ||
8856 | /* Now, we must be passed a constant src ptr parameter. */ | |
8857 | if (slen == 0 || TREE_CODE (slen) != INTEGER_CST) | |
c2f47e15 | 8858 | return NULL_TREE; |
f0613857 | 8859 | |
8860 | slen = size_binop (PLUS_EXPR, slen, ssize_int (1)); | |
8861 | ||
8862 | /* We do not support simplification of this case, though we do | |
8863 | support it when expanding trees into RTL. */ | |
8864 | /* FIXME: generate a call to __builtin_memset. */ | |
8865 | if (tree_int_cst_lt (slen, len)) | |
c2f47e15 | 8866 | return NULL_TREE; |
f0613857 | 8867 | |
8868 | /* OK transform into builtin memcpy. */ | |
8869 | fn = implicit_built_in_decls[BUILT_IN_MEMCPY]; | |
8870 | if (!fn) | |
c2f47e15 | 8871 | return NULL_TREE; |
2426241c | 8872 | return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), |
c2f47e15 | 8873 | build_call_expr (fn, 3, dest, src, len)); |
9c8a1629 | 8874 | } |
8875 | ||
7959b13b | 8876 | /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the |
8877 | arguments to the call, and TYPE is its return type. | |
8878 | Return NULL_TREE if no simplification can be made. */ | |
8879 | ||
8880 | static tree | |
8881 | fold_builtin_memchr (tree arg1, tree arg2, tree len, tree type) | |
8882 | { | |
8883 | if (!validate_arg (arg1, POINTER_TYPE) | |
8884 | || !validate_arg (arg2, INTEGER_TYPE) | |
8885 | || !validate_arg (len, INTEGER_TYPE)) | |
8886 | return NULL_TREE; | |
8887 | else | |
8888 | { | |
8889 | const char *p1; | |
8890 | ||
8891 | if (TREE_CODE (arg2) != INTEGER_CST | |
8892 | || !host_integerp (len, 1)) | |
8893 | return NULL_TREE; | |
8894 | ||
8895 | p1 = c_getstr (arg1); | |
8896 | if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0) | |
8897 | { | |
8898 | char c; | |
8899 | const char *r; | |
8900 | tree tem; | |
8901 | ||
8902 | if (target_char_cast (arg2, &c)) | |
8903 | return NULL_TREE; | |
8904 | ||
8905 | r = memchr (p1, c, tree_low_cst (len, 1)); | |
8906 | ||
8907 | if (r == NULL) | |
8908 | return build_int_cst (TREE_TYPE (arg1), 0); | |
8909 | ||
0de36bdb | 8910 | tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (arg1), arg1, |
8911 | size_int (r - p1)); | |
7959b13b | 8912 | return fold_convert (type, tem); |
8913 | } | |
8914 | return NULL_TREE; | |
8915 | } | |
8916 | } | |
8917 | ||
c2f47e15 | 8918 | /* Fold function call to builtin memcmp with arguments ARG1 and ARG2. |
8919 | Return NULL_TREE if no simplification can be made. */ | |
9c8a1629 | 8920 | |
8921 | static tree | |
c2f47e15 | 8922 | fold_builtin_memcmp (tree arg1, tree arg2, tree len) |
9c8a1629 | 8923 | { |
c4fef134 | 8924 | const char *p1, *p2; |
9c8a1629 | 8925 | |
c2f47e15 | 8926 | if (!validate_arg (arg1, POINTER_TYPE) |
8927 | || !validate_arg (arg2, POINTER_TYPE) | |
8928 | || !validate_arg (len, INTEGER_TYPE)) | |
8929 | return NULL_TREE; | |
9c8a1629 | 8930 | |
8931 | /* If the LEN parameter is zero, return zero. */ | |
8932 | if (integer_zerop (len)) | |
c4fef134 | 8933 | return omit_two_operands (integer_type_node, integer_zero_node, |
8934 | arg1, arg2); | |
9c8a1629 | 8935 | |
8936 | /* If ARG1 and ARG2 are the same (and not volatile), return zero. */ | |
8937 | if (operand_equal_p (arg1, arg2, 0)) | |
c4fef134 | 8938 | return omit_one_operand (integer_type_node, integer_zero_node, len); |
8939 | ||
8940 | p1 = c_getstr (arg1); | |
8941 | p2 = c_getstr (arg2); | |
8942 | ||
8943 | /* If all arguments are constant, and the value of len is not greater | |
8944 | than the lengths of arg1 and arg2, evaluate at compile-time. */ | |
8945 | if (host_integerp (len, 1) && p1 && p2 | |
8946 | && compare_tree_int (len, strlen (p1) + 1) <= 0 | |
8947 | && compare_tree_int (len, strlen (p2) + 1) <= 0) | |
8948 | { | |
8949 | const int r = memcmp (p1, p2, tree_low_cst (len, 1)); | |
8950 | ||
8951 | if (r > 0) | |
8952 | return integer_one_node; | |
8953 | else if (r < 0) | |
8954 | return integer_minus_one_node; | |
8955 | else | |
8956 | return integer_zero_node; | |
8957 | } | |
8958 | ||
8959 | /* If len parameter is one, return an expression corresponding to | |
8960 | (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */ | |
8961 | if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1) | |
8962 | { | |
8963 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
4f1b71c0 | 8964 | tree cst_uchar_ptr_node |
8965 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
8966 | ||
c4fef134 | 8967 | tree ind1 = fold_convert (integer_type_node, |
8968 | build1 (INDIRECT_REF, cst_uchar_node, | |
8969 | fold_convert (cst_uchar_ptr_node, | |
8970 | arg1))); | |
8971 | tree ind2 = fold_convert (integer_type_node, | |
8972 | build1 (INDIRECT_REF, cst_uchar_node, | |
8973 | fold_convert (cst_uchar_ptr_node, | |
8974 | arg2))); | |
49d00087 | 8975 | return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2); |
c4fef134 | 8976 | } |
9c8a1629 | 8977 | |
c2f47e15 | 8978 | return NULL_TREE; |
9c8a1629 | 8979 | } |
8980 | ||
c2f47e15 | 8981 | /* Fold function call to builtin strcmp with arguments ARG1 and ARG2. |
8982 | Return NULL_TREE if no simplification can be made. */ | |
9c8a1629 | 8983 | |
8984 | static tree | |
c2f47e15 | 8985 | fold_builtin_strcmp (tree arg1, tree arg2) |
9c8a1629 | 8986 | { |
9c8a1629 | 8987 | const char *p1, *p2; |
8988 | ||
c2f47e15 | 8989 | if (!validate_arg (arg1, POINTER_TYPE) |
8990 | || !validate_arg (arg2, POINTER_TYPE)) | |
8991 | return NULL_TREE; | |
9c8a1629 | 8992 | |
8993 | /* If ARG1 and ARG2 are the same (and not volatile), return zero. */ | |
8994 | if (operand_equal_p (arg1, arg2, 0)) | |
c4fef134 | 8995 | return integer_zero_node; |
9c8a1629 | 8996 | |
8997 | p1 = c_getstr (arg1); | |
8998 | p2 = c_getstr (arg2); | |
8999 | ||
9000 | if (p1 && p2) | |
9001 | { | |
9c8a1629 | 9002 | const int i = strcmp (p1, p2); |
9003 | if (i < 0) | |
c4fef134 | 9004 | return integer_minus_one_node; |
9c8a1629 | 9005 | else if (i > 0) |
c4fef134 | 9006 | return integer_one_node; |
9c8a1629 | 9007 | else |
c4fef134 | 9008 | return integer_zero_node; |
9009 | } | |
9010 | ||
9011 | /* If the second arg is "", return *(const unsigned char*)arg1. */ | |
9012 | if (p2 && *p2 == '\0') | |
9013 | { | |
9014 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
4f1b71c0 | 9015 | tree cst_uchar_ptr_node |
9016 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
9017 | ||
c4fef134 | 9018 | return fold_convert (integer_type_node, |
9019 | build1 (INDIRECT_REF, cst_uchar_node, | |
9020 | fold_convert (cst_uchar_ptr_node, | |
9021 | arg1))); | |
9022 | } | |
9023 | ||
9024 | /* If the first arg is "", return -*(const unsigned char*)arg2. */ | |
9025 | if (p1 && *p1 == '\0') | |
9026 | { | |
9027 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
4f1b71c0 | 9028 | tree cst_uchar_ptr_node |
9029 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
9030 | ||
c4fef134 | 9031 | tree temp = fold_convert (integer_type_node, |
9032 | build1 (INDIRECT_REF, cst_uchar_node, | |
9033 | fold_convert (cst_uchar_ptr_node, | |
9034 | arg2))); | |
49d00087 | 9035 | return fold_build1 (NEGATE_EXPR, integer_type_node, temp); |
9c8a1629 | 9036 | } |
9037 | ||
c2f47e15 | 9038 | return NULL_TREE; |
9c8a1629 | 9039 | } |
9040 | ||
c2f47e15 | 9041 | /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN. |
9042 | Return NULL_TREE if no simplification can be made. */ | |
9c8a1629 | 9043 | |
9044 | static tree | |
c2f47e15 | 9045 | fold_builtin_strncmp (tree arg1, tree arg2, tree len) |
9c8a1629 | 9046 | { |
9c8a1629 | 9047 | const char *p1, *p2; |
9048 | ||
c2f47e15 | 9049 | if (!validate_arg (arg1, POINTER_TYPE) |
9050 | || !validate_arg (arg2, POINTER_TYPE) | |
9051 | || !validate_arg (len, INTEGER_TYPE)) | |
9052 | return NULL_TREE; | |
9c8a1629 | 9053 | |
9054 | /* If the LEN parameter is zero, return zero. */ | |
9055 | if (integer_zerop (len)) | |
c4fef134 | 9056 | return omit_two_operands (integer_type_node, integer_zero_node, |
9057 | arg1, arg2); | |
9c8a1629 | 9058 | |
9059 | /* If ARG1 and ARG2 are the same (and not volatile), return zero. */ | |
9060 | if (operand_equal_p (arg1, arg2, 0)) | |
c4fef134 | 9061 | return omit_one_operand (integer_type_node, integer_zero_node, len); |
9c8a1629 | 9062 | |
9063 | p1 = c_getstr (arg1); | |
9064 | p2 = c_getstr (arg2); | |
9065 | ||
9066 | if (host_integerp (len, 1) && p1 && p2) | |
9067 | { | |
9c8a1629 | 9068 | const int i = strncmp (p1, p2, tree_low_cst (len, 1)); |
c4fef134 | 9069 | if (i > 0) |
9070 | return integer_one_node; | |
9071 | else if (i < 0) | |
9072 | return integer_minus_one_node; | |
9c8a1629 | 9073 | else |
c4fef134 | 9074 | return integer_zero_node; |
9075 | } | |
9076 | ||
9077 | /* If the second arg is "", and the length is greater than zero, | |
9078 | return *(const unsigned char*)arg1. */ | |
9079 | if (p2 && *p2 == '\0' | |
9080 | && TREE_CODE (len) == INTEGER_CST | |
9081 | && tree_int_cst_sgn (len) == 1) | |
9082 | { | |
9083 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
4f1b71c0 | 9084 | tree cst_uchar_ptr_node |
9085 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
9086 | ||
c4fef134 | 9087 | return fold_convert (integer_type_node, |
9088 | build1 (INDIRECT_REF, cst_uchar_node, | |
9089 | fold_convert (cst_uchar_ptr_node, | |
9090 | arg1))); | |
9091 | } | |
9092 | ||
9093 | /* If the first arg is "", and the length is greater than zero, | |
9094 | return -*(const unsigned char*)arg2. */ | |
9095 | if (p1 && *p1 == '\0' | |
9096 | && TREE_CODE (len) == INTEGER_CST | |
9097 | && tree_int_cst_sgn (len) == 1) | |
9098 | { | |
9099 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
4f1b71c0 | 9100 | tree cst_uchar_ptr_node |
9101 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
9102 | ||
c4fef134 | 9103 | tree temp = fold_convert (integer_type_node, |
9104 | build1 (INDIRECT_REF, cst_uchar_node, | |
9105 | fold_convert (cst_uchar_ptr_node, | |
9106 | arg2))); | |
49d00087 | 9107 | return fold_build1 (NEGATE_EXPR, integer_type_node, temp); |
c4fef134 | 9108 | } |
9109 | ||
9110 | /* If len parameter is one, return an expression corresponding to | |
9111 | (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */ | |
9112 | if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1) | |
9113 | { | |
9114 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
4f1b71c0 | 9115 | tree cst_uchar_ptr_node |
9116 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
9117 | ||
c4fef134 | 9118 | tree ind1 = fold_convert (integer_type_node, |
9119 | build1 (INDIRECT_REF, cst_uchar_node, | |
9120 | fold_convert (cst_uchar_ptr_node, | |
9121 | arg1))); | |
9122 | tree ind2 = fold_convert (integer_type_node, | |
9123 | build1 (INDIRECT_REF, cst_uchar_node, | |
9124 | fold_convert (cst_uchar_ptr_node, | |
9125 | arg2))); | |
49d00087 | 9126 | return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2); |
9c8a1629 | 9127 | } |
9128 | ||
c2f47e15 | 9129 | return NULL_TREE; |
9c8a1629 | 9130 | } |
9131 | ||
c2f47e15 | 9132 | /* Fold function call to builtin signbit, signbitf or signbitl with argument |
9133 | ARG. Return NULL_TREE if no simplification can be made. */ | |
27f261ef | 9134 | |
9135 | static tree | |
c2f47e15 | 9136 | fold_builtin_signbit (tree arg, tree type) |
27f261ef | 9137 | { |
c2f47e15 | 9138 | tree temp; |
27f261ef | 9139 | |
c2f47e15 | 9140 | if (!validate_arg (arg, REAL_TYPE)) |
27f261ef | 9141 | return NULL_TREE; |
9142 | ||
27f261ef | 9143 | /* If ARG is a compile-time constant, determine the result. */ |
9144 | if (TREE_CODE (arg) == REAL_CST | |
f96bd2bf | 9145 | && !TREE_OVERFLOW (arg)) |
27f261ef | 9146 | { |
9147 | REAL_VALUE_TYPE c; | |
9148 | ||
9149 | c = TREE_REAL_CST (arg); | |
9150 | temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node; | |
2426241c | 9151 | return fold_convert (type, temp); |
27f261ef | 9152 | } |
9153 | ||
9154 | /* If ARG is non-negative, the result is always zero. */ | |
9155 | if (tree_expr_nonnegative_p (arg)) | |
2426241c | 9156 | return omit_one_operand (type, integer_zero_node, arg); |
27f261ef | 9157 | |
9158 | /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */ | |
9159 | if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg)))) | |
49d00087 | 9160 | return fold_build2 (LT_EXPR, type, arg, |
9161 | build_real (TREE_TYPE (arg), dconst0)); | |
27f261ef | 9162 | |
9163 | return NULL_TREE; | |
9164 | } | |
9165 | ||
c2f47e15 | 9166 | /* Fold function call to builtin copysign, copysignf or copysignl with |
9167 | arguments ARG1 and ARG2. Return NULL_TREE if no simplification can | |
9168 | be made. */ | |
467214fd | 9169 | |
9170 | static tree | |
c2f47e15 | 9171 | fold_builtin_copysign (tree fndecl, tree arg1, tree arg2, tree type) |
467214fd | 9172 | { |
c2f47e15 | 9173 | tree tem; |
467214fd | 9174 | |
c2f47e15 | 9175 | if (!validate_arg (arg1, REAL_TYPE) |
9176 | || !validate_arg (arg2, REAL_TYPE)) | |
467214fd | 9177 | return NULL_TREE; |
9178 | ||
467214fd | 9179 | /* copysign(X,X) is X. */ |
9180 | if (operand_equal_p (arg1, arg2, 0)) | |
9181 | return fold_convert (type, arg1); | |
9182 | ||
9183 | /* If ARG1 and ARG2 are compile-time constants, determine the result. */ | |
9184 | if (TREE_CODE (arg1) == REAL_CST | |
9185 | && TREE_CODE (arg2) == REAL_CST | |
f96bd2bf | 9186 | && !TREE_OVERFLOW (arg1) |
9187 | && !TREE_OVERFLOW (arg2)) | |
467214fd | 9188 | { |
9189 | REAL_VALUE_TYPE c1, c2; | |
9190 | ||
9191 | c1 = TREE_REAL_CST (arg1); | |
9192 | c2 = TREE_REAL_CST (arg2); | |
749680e2 | 9193 | /* c1.sign := c2.sign. */ |
467214fd | 9194 | real_copysign (&c1, &c2); |
9195 | return build_real (type, c1); | |
467214fd | 9196 | } |
9197 | ||
9198 | /* copysign(X, Y) is fabs(X) when Y is always non-negative. | |
9199 | Remember to evaluate Y for side-effects. */ | |
9200 | if (tree_expr_nonnegative_p (arg2)) | |
9201 | return omit_one_operand (type, | |
49d00087 | 9202 | fold_build1 (ABS_EXPR, type, arg1), |
467214fd | 9203 | arg2); |
9204 | ||
198d9bbe | 9205 | /* Strip sign changing operations for the first argument. */ |
9206 | tem = fold_strip_sign_ops (arg1); | |
9207 | if (tem) | |
c2f47e15 | 9208 | return build_call_expr (fndecl, 2, tem, arg2); |
198d9bbe | 9209 | |
467214fd | 9210 | return NULL_TREE; |
9211 | } | |
9212 | ||
c2f47e15 | 9213 | /* Fold a call to builtin isascii with argument ARG. */ |
d49367d4 | 9214 | |
9215 | static tree | |
c2f47e15 | 9216 | fold_builtin_isascii (tree arg) |
d49367d4 | 9217 | { |
c2f47e15 | 9218 | if (!validate_arg (arg, INTEGER_TYPE)) |
9219 | return NULL_TREE; | |
d49367d4 | 9220 | else |
9221 | { | |
9222 | /* Transform isascii(c) -> ((c & ~0x7f) == 0). */ | |
7c446c95 | 9223 | arg = build2 (BIT_AND_EXPR, integer_type_node, arg, |
9224 | build_int_cst (NULL_TREE, | |
7016c612 | 9225 | ~ (unsigned HOST_WIDE_INT) 0x7f)); |
f2532264 | 9226 | return fold_build2 (EQ_EXPR, integer_type_node, |
9227 | arg, integer_zero_node); | |
d49367d4 | 9228 | } |
9229 | } | |
9230 | ||
c2f47e15 | 9231 | /* Fold a call to builtin toascii with argument ARG. */ |
d49367d4 | 9232 | |
9233 | static tree | |
c2f47e15 | 9234 | fold_builtin_toascii (tree arg) |
d49367d4 | 9235 | { |
c2f47e15 | 9236 | if (!validate_arg (arg, INTEGER_TYPE)) |
9237 | return NULL_TREE; | |
9238 | ||
9239 | /* Transform toascii(c) -> (c & 0x7f). */ | |
9240 | return fold_build2 (BIT_AND_EXPR, integer_type_node, arg, | |
9241 | build_int_cst (NULL_TREE, 0x7f)); | |
d49367d4 | 9242 | } |
9243 | ||
c2f47e15 | 9244 | /* Fold a call to builtin isdigit with argument ARG. */ |
df1cf42e | 9245 | |
9246 | static tree | |
c2f47e15 | 9247 | fold_builtin_isdigit (tree arg) |
df1cf42e | 9248 | { |
c2f47e15 | 9249 | if (!validate_arg (arg, INTEGER_TYPE)) |
9250 | return NULL_TREE; | |
df1cf42e | 9251 | else |
9252 | { | |
9253 | /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */ | |
624d37a6 | 9254 | /* According to the C standard, isdigit is unaffected by locale. |
9255 | However, it definitely is affected by the target character set. */ | |
624d37a6 | 9256 | unsigned HOST_WIDE_INT target_digit0 |
9257 | = lang_hooks.to_target_charset ('0'); | |
9258 | ||
9259 | if (target_digit0 == 0) | |
9260 | return NULL_TREE; | |
9261 | ||
c2f47e15 | 9262 | arg = fold_convert (unsigned_type_node, arg); |
05abc81b | 9263 | arg = build2 (MINUS_EXPR, unsigned_type_node, arg, |
624d37a6 | 9264 | build_int_cst (unsigned_type_node, target_digit0)); |
f2532264 | 9265 | return fold_build2 (LE_EXPR, integer_type_node, arg, |
9266 | build_int_cst (unsigned_type_node, 9)); | |
df1cf42e | 9267 | } |
9268 | } | |
27f261ef | 9269 | |
c2f47e15 | 9270 | /* Fold a call to fabs, fabsf or fabsl with argument ARG. */ |
d1aade50 | 9271 | |
9272 | static tree | |
c2f47e15 | 9273 | fold_builtin_fabs (tree arg, tree type) |
d1aade50 | 9274 | { |
c2f47e15 | 9275 | if (!validate_arg (arg, REAL_TYPE)) |
9276 | return NULL_TREE; | |
d1aade50 | 9277 | |
9829e110 | 9278 | arg = fold_convert (type, arg); |
d1aade50 | 9279 | if (TREE_CODE (arg) == REAL_CST) |
9280 | return fold_abs_const (arg, type); | |
49d00087 | 9281 | return fold_build1 (ABS_EXPR, type, arg); |
d1aade50 | 9282 | } |
9283 | ||
c2f47e15 | 9284 | /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */ |
d1aade50 | 9285 | |
9286 | static tree | |
c2f47e15 | 9287 | fold_builtin_abs (tree arg, tree type) |
d1aade50 | 9288 | { |
c2f47e15 | 9289 | if (!validate_arg (arg, INTEGER_TYPE)) |
9290 | return NULL_TREE; | |
d1aade50 | 9291 | |
9829e110 | 9292 | arg = fold_convert (type, arg); |
d1aade50 | 9293 | if (TREE_CODE (arg) == INTEGER_CST) |
9294 | return fold_abs_const (arg, type); | |
49d00087 | 9295 | return fold_build1 (ABS_EXPR, type, arg); |
d1aade50 | 9296 | } |
9297 | ||
d4a43a03 | 9298 | /* Fold a call to builtin fmin or fmax. */ |
9299 | ||
9300 | static tree | |
c2f47e15 | 9301 | fold_builtin_fmin_fmax (tree arg0, tree arg1, tree type, bool max) |
d4a43a03 | 9302 | { |
c2f47e15 | 9303 | if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE)) |
d4a43a03 | 9304 | { |
d4a43a03 | 9305 | /* Calculate the result when the argument is a constant. */ |
9306 | tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min)); | |
9307 | ||
9308 | if (res) | |
9309 | return res; | |
9310 | ||
61fe3555 | 9311 | /* If either argument is NaN, return the other one. Avoid the |
9312 | transformation if we get (and honor) a signalling NaN. Using | |
9313 | omit_one_operand() ensures we create a non-lvalue. */ | |
9314 | if (TREE_CODE (arg0) == REAL_CST | |
9315 | && real_isnan (&TREE_REAL_CST (arg0)) | |
9316 | && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))) | |
9317 | || ! TREE_REAL_CST (arg0).signalling)) | |
9318 | return omit_one_operand (type, arg1, arg0); | |
9319 | if (TREE_CODE (arg1) == REAL_CST | |
9320 | && real_isnan (&TREE_REAL_CST (arg1)) | |
9321 | && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))) | |
9322 | || ! TREE_REAL_CST (arg1).signalling)) | |
9323 | return omit_one_operand (type, arg0, arg1); | |
9324 | ||
d4a43a03 | 9325 | /* Transform fmin/fmax(x,x) -> x. */ |
9326 | if (operand_equal_p (arg0, arg1, OEP_PURE_SAME)) | |
9327 | return omit_one_operand (type, arg0, arg1); | |
9328 | ||
9329 | /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these | |
9330 | functions to return the numeric arg if the other one is NaN. | |
9331 | These tree codes don't honor that, so only transform if | |
9332 | -ffinite-math-only is set. C99 doesn't require -0.0 to be | |
9333 | handled, so we don't have to worry about it either. */ | |
9334 | if (flag_finite_math_only) | |
9335 | return fold_build2 ((max ? MAX_EXPR : MIN_EXPR), type, | |
9336 | fold_convert (type, arg0), | |
9337 | fold_convert (type, arg1)); | |
9338 | } | |
9339 | return NULL_TREE; | |
9340 | } | |
9341 | ||
abe4dcf6 | 9342 | /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */ |
9343 | ||
9344 | static tree | |
c2f47e15 | 9345 | fold_builtin_carg (tree arg, tree type) |
abe4dcf6 | 9346 | { |
c2f47e15 | 9347 | if (validate_arg (arg, COMPLEX_TYPE)) |
abe4dcf6 | 9348 | { |
9349 | tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2); | |
9350 | ||
9351 | if (atan2_fn) | |
9352 | { | |
c2f47e15 | 9353 | tree new_arg = builtin_save_expr (arg); |
9354 | tree r_arg = fold_build1 (REALPART_EXPR, type, new_arg); | |
9355 | tree i_arg = fold_build1 (IMAGPART_EXPR, type, new_arg); | |
9356 | return build_call_expr (atan2_fn, 2, i_arg, r_arg); | |
abe4dcf6 | 9357 | } |
9358 | } | |
9359 | ||
9360 | return NULL_TREE; | |
9361 | } | |
9362 | ||
cb2b9385 | 9363 | /* Fold a call to builtin logb/ilogb. */ |
9364 | ||
9365 | static tree | |
9366 | fold_builtin_logb (tree arg, tree rettype) | |
9367 | { | |
9368 | if (! validate_arg (arg, REAL_TYPE)) | |
9369 | return NULL_TREE; | |
9370 | ||
9371 | STRIP_NOPS (arg); | |
9372 | ||
9373 | if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg)) | |
9374 | { | |
9375 | const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg); | |
9376 | ||
9377 | switch (value->cl) | |
9378 | { | |
9379 | case rvc_nan: | |
9380 | case rvc_inf: | |
9381 | /* If arg is Inf or NaN and we're logb, return it. */ | |
9382 | if (TREE_CODE (rettype) == REAL_TYPE) | |
9383 | return fold_convert (rettype, arg); | |
9384 | /* Fall through... */ | |
9385 | case rvc_zero: | |
9386 | /* Zero may set errno and/or raise an exception for logb, also | |
9387 | for ilogb we don't know FP_ILOGB0. */ | |
9388 | return NULL_TREE; | |
9389 | case rvc_normal: | |
9390 | /* For normal numbers, proceed iff radix == 2. In GCC, | |
9391 | normalized significands are in the range [0.5, 1.0). We | |
9392 | want the exponent as if they were [1.0, 2.0) so get the | |
9393 | exponent and subtract 1. */ | |
9394 | if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2) | |
9395 | return fold_convert (rettype, build_int_cst (NULL_TREE, | |
9396 | REAL_EXP (value)-1)); | |
9397 | break; | |
9398 | } | |
9399 | } | |
9400 | ||
9401 | return NULL_TREE; | |
9402 | } | |
9403 | ||
9404 | /* Fold a call to builtin significand, if radix == 2. */ | |
9405 | ||
9406 | static tree | |
9407 | fold_builtin_significand (tree arg, tree rettype) | |
9408 | { | |
9409 | if (! validate_arg (arg, REAL_TYPE)) | |
9410 | return NULL_TREE; | |
9411 | ||
9412 | STRIP_NOPS (arg); | |
9413 | ||
9414 | if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg)) | |
9415 | { | |
9416 | const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg); | |
9417 | ||
9418 | switch (value->cl) | |
9419 | { | |
9420 | case rvc_zero: | |
9421 | case rvc_nan: | |
9422 | case rvc_inf: | |
9423 | /* If arg is +-0, +-Inf or +-NaN, then return it. */ | |
9424 | return fold_convert (rettype, arg); | |
9425 | case rvc_normal: | |
9426 | /* For normal numbers, proceed iff radix == 2. */ | |
9427 | if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2) | |
9428 | { | |
9429 | REAL_VALUE_TYPE result = *value; | |
9430 | /* In GCC, normalized significands are in the range [0.5, | |
9431 | 1.0). We want them to be [1.0, 2.0) so set the | |
9432 | exponent to 1. */ | |
9433 | SET_REAL_EXP (&result, 1); | |
9434 | return build_real (rettype, result); | |
9435 | } | |
9436 | break; | |
9437 | } | |
9438 | } | |
9439 | ||
9440 | return NULL_TREE; | |
9441 | } | |
9442 | ||
3838b9ae | 9443 | /* Fold a call to builtin frexp, we can assume the base is 2. */ |
9444 | ||
9445 | static tree | |
9446 | fold_builtin_frexp (tree arg0, tree arg1, tree rettype) | |
9447 | { | |
9448 | if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE)) | |
9449 | return NULL_TREE; | |
9450 | ||
9451 | STRIP_NOPS (arg0); | |
9452 | ||
9453 | if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0))) | |
9454 | return NULL_TREE; | |
9455 | ||
9456 | arg1 = build_fold_indirect_ref (arg1); | |
9457 | ||
9458 | /* Proceed if a valid pointer type was passed in. */ | |
9459 | if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node) | |
9460 | { | |
9461 | const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0); | |
9462 | tree frac, exp; | |
9463 | ||
9464 | switch (value->cl) | |
9465 | { | |
9466 | case rvc_zero: | |
9467 | /* For +-0, return (*exp = 0, +-0). */ | |
9468 | exp = integer_zero_node; | |
9469 | frac = arg0; | |
9470 | break; | |
9471 | case rvc_nan: | |
9472 | case rvc_inf: | |
9473 | /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */ | |
9474 | return omit_one_operand (rettype, arg0, arg1); | |
9475 | case rvc_normal: | |
9476 | { | |
9477 | /* Since the frexp function always expects base 2, and in | |
9478 | GCC normalized significands are already in the range | |
9479 | [0.5, 1.0), we have exactly what frexp wants. */ | |
9480 | REAL_VALUE_TYPE frac_rvt = *value; | |
9481 | SET_REAL_EXP (&frac_rvt, 0); | |
9482 | frac = build_real (rettype, frac_rvt); | |
9483 | exp = build_int_cst (NULL_TREE, REAL_EXP (value)); | |
9484 | } | |
9485 | break; | |
9486 | default: | |
9487 | gcc_unreachable (); | |
9488 | } | |
9489 | ||
9490 | /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */ | |
9491 | arg1 = fold_build2 (MODIFY_EXPR, rettype, arg1, exp); | |
9492 | TREE_SIDE_EFFECTS (arg1) = 1; | |
9493 | return fold_build2 (COMPOUND_EXPR, rettype, arg1, frac); | |
9494 | } | |
9495 | ||
9496 | return NULL_TREE; | |
9497 | } | |
9498 | ||
7587301b | 9499 | /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true |
9500 | then we can assume the base is two. If it's false, then we have to | |
9501 | check the mode of the TYPE parameter in certain cases. */ | |
9502 | ||
9503 | static tree | |
9504 | fold_builtin_load_exponent (tree arg0, tree arg1, tree type, bool ldexp) | |
9505 | { | |
9506 | if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE)) | |
9507 | { | |
9508 | STRIP_NOPS (arg0); | |
9509 | STRIP_NOPS (arg1); | |
9510 | ||
9511 | /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */ | |
9512 | if (real_zerop (arg0) || integer_zerop (arg1) | |
9513 | || (TREE_CODE (arg0) == REAL_CST | |
776a7bab | 9514 | && !real_isfinite (&TREE_REAL_CST (arg0)))) |
7587301b | 9515 | return omit_one_operand (type, arg0, arg1); |
9516 | ||
9517 | /* If both arguments are constant, then try to evaluate it. */ | |
9518 | if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2) | |
9519 | && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0) | |
9520 | && host_integerp (arg1, 0)) | |
9521 | { | |
9522 | /* Bound the maximum adjustment to twice the range of the | |
9523 | mode's valid exponents. Use abs to ensure the range is | |
9524 | positive as a sanity check. */ | |
9525 | const long max_exp_adj = 2 * | |
9526 | labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax | |
9527 | - REAL_MODE_FORMAT (TYPE_MODE (type))->emin); | |
9528 | ||
9529 | /* Get the user-requested adjustment. */ | |
9530 | const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0); | |
9531 | ||
9532 | /* The requested adjustment must be inside this range. This | |
9533 | is a preliminary cap to avoid things like overflow, we | |
9534 | may still fail to compute the result for other reasons. */ | |
9535 | if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj) | |
9536 | { | |
9537 | REAL_VALUE_TYPE initial_result; | |
9538 | ||
9539 | real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj); | |
9540 | ||
9541 | /* Ensure we didn't overflow. */ | |
9542 | if (! real_isinf (&initial_result)) | |
9543 | { | |
9544 | const REAL_VALUE_TYPE trunc_result | |
9545 | = real_value_truncate (TYPE_MODE (type), initial_result); | |
9546 | ||
9547 | /* Only proceed if the target mode can hold the | |
9548 | resulting value. */ | |
9549 | if (REAL_VALUES_EQUAL (initial_result, trunc_result)) | |
9550 | return build_real (type, trunc_result); | |
9551 | } | |
9552 | } | |
9553 | } | |
9554 | } | |
9555 | ||
9556 | return NULL_TREE; | |
9557 | } | |
9558 | ||
ebf8b4f5 | 9559 | /* Fold a call to builtin modf. */ |
9560 | ||
9561 | static tree | |
9562 | fold_builtin_modf (tree arg0, tree arg1, tree rettype) | |
9563 | { | |
9564 | if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE)) | |
9565 | return NULL_TREE; | |
9566 | ||
9567 | STRIP_NOPS (arg0); | |
9568 | ||
9569 | if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0))) | |
9570 | return NULL_TREE; | |
9571 | ||
9572 | arg1 = build_fold_indirect_ref (arg1); | |
9573 | ||
9574 | /* Proceed if a valid pointer type was passed in. */ | |
9575 | if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype)) | |
9576 | { | |
9577 | const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0); | |
9578 | REAL_VALUE_TYPE trunc, frac; | |
9579 | ||
9580 | switch (value->cl) | |
9581 | { | |
9582 | case rvc_nan: | |
9583 | case rvc_zero: | |
9584 | /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */ | |
9585 | trunc = frac = *value; | |
9586 | break; | |
9587 | case rvc_inf: | |
9588 | /* For +-Inf, return (*arg1 = arg0, +-0). */ | |
9589 | frac = dconst0; | |
9590 | frac.sign = value->sign; | |
9591 | trunc = *value; | |
9592 | break; | |
9593 | case rvc_normal: | |
9594 | /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */ | |
9595 | real_trunc (&trunc, VOIDmode, value); | |
9596 | real_arithmetic (&frac, MINUS_EXPR, value, &trunc); | |
9597 | /* If the original number was negative and already | |
9598 | integral, then the fractional part is -0.0. */ | |
9599 | if (value->sign && frac.cl == rvc_zero) | |
9600 | frac.sign = value->sign; | |
9601 | break; | |
9602 | } | |
9603 | ||
9604 | /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */ | |
9605 | arg1 = fold_build2 (MODIFY_EXPR, rettype, arg1, | |
9606 | build_real (rettype, trunc)); | |
9607 | TREE_SIDE_EFFECTS (arg1) = 1; | |
9608 | return fold_build2 (COMPOUND_EXPR, rettype, arg1, | |
9609 | build_real (rettype, frac)); | |
9610 | } | |
9611 | ||
9612 | return NULL_TREE; | |
9613 | } | |
9614 | ||
726069ba | 9615 | /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite. |
c2f47e15 | 9616 | ARG is the argument for the call. */ |
726069ba | 9617 | |
9618 | static tree | |
c2f47e15 | 9619 | fold_builtin_classify (tree fndecl, tree arg, int builtin_index) |
726069ba | 9620 | { |
726069ba | 9621 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
726069ba | 9622 | REAL_VALUE_TYPE r; |
9623 | ||
c2f47e15 | 9624 | if (!validate_arg (arg, REAL_TYPE)) |
d43cee80 | 9625 | return NULL_TREE; |
726069ba | 9626 | |
726069ba | 9627 | switch (builtin_index) |
9628 | { | |
9629 | case BUILT_IN_ISINF: | |
b70bfd00 | 9630 | if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg)))) |
a0c938f0 | 9631 | return omit_one_operand (type, integer_zero_node, arg); |
726069ba | 9632 | |
9633 | if (TREE_CODE (arg) == REAL_CST) | |
9634 | { | |
9635 | r = TREE_REAL_CST (arg); | |
9636 | if (real_isinf (&r)) | |
9637 | return real_compare (GT_EXPR, &r, &dconst0) | |
9638 | ? integer_one_node : integer_minus_one_node; | |
9639 | else | |
9640 | return integer_zero_node; | |
9641 | } | |
9642 | ||
9643 | return NULL_TREE; | |
9644 | ||
c319d56a | 9645 | case BUILT_IN_ISINF_SIGN: |
9646 | { | |
9647 | /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */ | |
9648 | /* In a boolean context, GCC will fold the inner COND_EXPR to | |
9649 | 1. So e.g. "if (isinf_sign(x))" would be folded to just | |
9650 | "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */ | |
9651 | tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0); | |
9652 | tree isinf_fn = built_in_decls[BUILT_IN_ISINF]; | |
9653 | tree tmp = NULL_TREE; | |
9654 | ||
9655 | arg = builtin_save_expr (arg); | |
9656 | ||
9657 | if (signbit_fn && isinf_fn) | |
9658 | { | |
9659 | tree signbit_call = build_call_expr (signbit_fn, 1, arg); | |
9660 | tree isinf_call = build_call_expr (isinf_fn, 1, arg); | |
9661 | ||
9662 | signbit_call = fold_build2 (NE_EXPR, integer_type_node, | |
9663 | signbit_call, integer_zero_node); | |
9664 | isinf_call = fold_build2 (NE_EXPR, integer_type_node, | |
9665 | isinf_call, integer_zero_node); | |
9666 | ||
9667 | tmp = fold_build3 (COND_EXPR, integer_type_node, signbit_call, | |
9668 | integer_minus_one_node, integer_one_node); | |
9669 | tmp = fold_build3 (COND_EXPR, integer_type_node, isinf_call, tmp, | |
9670 | integer_zero_node); | |
9671 | } | |
9672 | ||
9673 | return tmp; | |
9674 | } | |
9675 | ||
cde061c1 | 9676 | case BUILT_IN_ISFINITE: |
b70bfd00 | 9677 | if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))) |
9678 | && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg)))) | |
34709e02 | 9679 | return omit_one_operand (type, integer_one_node, arg); |
726069ba | 9680 | |
9681 | if (TREE_CODE (arg) == REAL_CST) | |
9682 | { | |
9683 | r = TREE_REAL_CST (arg); | |
776a7bab | 9684 | return real_isfinite (&r) ? integer_one_node : integer_zero_node; |
726069ba | 9685 | } |
9686 | ||
9687 | return NULL_TREE; | |
9688 | ||
9689 | case BUILT_IN_ISNAN: | |
b70bfd00 | 9690 | if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))) |
a0c938f0 | 9691 | return omit_one_operand (type, integer_zero_node, arg); |
726069ba | 9692 | |
9693 | if (TREE_CODE (arg) == REAL_CST) | |
9694 | { | |
9695 | r = TREE_REAL_CST (arg); | |
9696 | return real_isnan (&r) ? integer_one_node : integer_zero_node; | |
9697 | } | |
9698 | ||
9699 | arg = builtin_save_expr (arg); | |
49d00087 | 9700 | return fold_build2 (UNORDERED_EXPR, type, arg, arg); |
726069ba | 9701 | |
9702 | default: | |
64db345d | 9703 | gcc_unreachable (); |
726069ba | 9704 | } |
9705 | } | |
9706 | ||
19fbe3a4 | 9707 | /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...). |
9708 | This builtin will generate code to return the appropriate floating | |
9709 | point classification depending on the value of the floating point | |
9710 | number passed in. The possible return values must be supplied as | |
921b27c0 | 9711 | int arguments to the call in the following order: FP_NAN, FP_INFINITE, |
19fbe3a4 | 9712 | FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly |
9713 | one floating point argument which is "type generic". */ | |
9714 | ||
9715 | static tree | |
9716 | fold_builtin_fpclassify (tree exp) | |
9717 | { | |
921b27c0 | 9718 | tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero, |
9719 | arg, type, res, tmp; | |
19fbe3a4 | 9720 | enum machine_mode mode; |
9721 | REAL_VALUE_TYPE r; | |
9722 | char buf[128]; | |
9723 | ||
9724 | /* Verify the required arguments in the original call. */ | |
9725 | if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, | |
9726 | INTEGER_TYPE, INTEGER_TYPE, | |
9727 | INTEGER_TYPE, REAL_TYPE, VOID_TYPE)) | |
9728 | return NULL_TREE; | |
9729 | ||
9730 | fp_nan = CALL_EXPR_ARG (exp, 0); | |
921b27c0 | 9731 | fp_infinite = CALL_EXPR_ARG (exp, 1); |
19fbe3a4 | 9732 | fp_normal = CALL_EXPR_ARG (exp, 2); |
9733 | fp_subnormal = CALL_EXPR_ARG (exp, 3); | |
9734 | fp_zero = CALL_EXPR_ARG (exp, 4); | |
9735 | arg = CALL_EXPR_ARG (exp, 5); | |
9736 | type = TREE_TYPE (arg); | |
9737 | mode = TYPE_MODE (type); | |
9738 | arg = builtin_save_expr (fold_build1 (ABS_EXPR, type, arg)); | |
9739 | ||
9740 | /* fpclassify(x) -> | |
9741 | isnan(x) ? FP_NAN : | |
921b27c0 | 9742 | (fabs(x) == Inf ? FP_INFINITE : |
19fbe3a4 | 9743 | (fabs(x) >= DBL_MIN ? FP_NORMAL : |
9744 | (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */ | |
9745 | ||
9746 | tmp = fold_build2 (EQ_EXPR, integer_type_node, arg, | |
9747 | build_real (type, dconst0)); | |
9748 | res = fold_build3 (COND_EXPR, integer_type_node, tmp, fp_zero, fp_subnormal); | |
9749 | ||
9750 | sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1); | |
9751 | real_from_string (&r, buf); | |
9752 | tmp = fold_build2 (GE_EXPR, integer_type_node, arg, build_real (type, r)); | |
9753 | res = fold_build3 (COND_EXPR, integer_type_node, tmp, fp_normal, res); | |
9754 | ||
9755 | if (HONOR_INFINITIES (mode)) | |
9756 | { | |
9757 | real_inf (&r); | |
9758 | tmp = fold_build2 (EQ_EXPR, integer_type_node, arg, | |
9759 | build_real (type, r)); | |
921b27c0 | 9760 | res = fold_build3 (COND_EXPR, integer_type_node, tmp, fp_infinite, res); |
19fbe3a4 | 9761 | } |
9762 | ||
9763 | if (HONOR_NANS (mode)) | |
9764 | { | |
9765 | tmp = fold_build2 (ORDERED_EXPR, integer_type_node, arg, arg); | |
9766 | res = fold_build3 (COND_EXPR, integer_type_node, tmp, res, fp_nan); | |
9767 | } | |
9768 | ||
9769 | return res; | |
9770 | } | |
9771 | ||
9bc9f15f | 9772 | /* Fold a call to an unordered comparison function such as |
d5019fe8 | 9773 | __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function |
c2f47e15 | 9774 | being called and ARG0 and ARG1 are the arguments for the call. |
726069ba | 9775 | UNORDERED_CODE and ORDERED_CODE are comparison codes that give |
9776 | the opposite of the desired result. UNORDERED_CODE is used | |
9777 | for modes that can hold NaNs and ORDERED_CODE is used for | |
9778 | the rest. */ | |
9bc9f15f | 9779 | |
9780 | static tree | |
c2f47e15 | 9781 | fold_builtin_unordered_cmp (tree fndecl, tree arg0, tree arg1, |
9bc9f15f | 9782 | enum tree_code unordered_code, |
9783 | enum tree_code ordered_code) | |
9784 | { | |
859f903a | 9785 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
9bc9f15f | 9786 | enum tree_code code; |
6978db0d | 9787 | tree type0, type1; |
9788 | enum tree_code code0, code1; | |
9789 | tree cmp_type = NULL_TREE; | |
9bc9f15f | 9790 | |
6978db0d | 9791 | type0 = TREE_TYPE (arg0); |
9792 | type1 = TREE_TYPE (arg1); | |
a0c938f0 | 9793 | |
6978db0d | 9794 | code0 = TREE_CODE (type0); |
9795 | code1 = TREE_CODE (type1); | |
a0c938f0 | 9796 | |
6978db0d | 9797 | if (code0 == REAL_TYPE && code1 == REAL_TYPE) |
9798 | /* Choose the wider of two real types. */ | |
9799 | cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1) | |
9800 | ? type0 : type1; | |
9801 | else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE) | |
9802 | cmp_type = type0; | |
9803 | else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE) | |
9804 | cmp_type = type1; | |
a0c938f0 | 9805 | |
6978db0d | 9806 | arg0 = fold_convert (cmp_type, arg0); |
9807 | arg1 = fold_convert (cmp_type, arg1); | |
859f903a | 9808 | |
9809 | if (unordered_code == UNORDERED_EXPR) | |
9810 | { | |
b70bfd00 | 9811 | if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))) |
859f903a | 9812 | return omit_two_operands (type, integer_zero_node, arg0, arg1); |
49d00087 | 9813 | return fold_build2 (UNORDERED_EXPR, type, arg0, arg1); |
859f903a | 9814 | } |
9bc9f15f | 9815 | |
b70bfd00 | 9816 | code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code |
9817 | : ordered_code; | |
49d00087 | 9818 | return fold_build1 (TRUTH_NOT_EXPR, type, |
9819 | fold_build2 (code, type, arg0, arg1)); | |
9bc9f15f | 9820 | } |
9821 | ||
c2f47e15 | 9822 | /* Fold a call to built-in function FNDECL with 0 arguments. |
9823 | IGNORE is true if the result of the function call is ignored. This | |
9824 | function returns NULL_TREE if no simplification was possible. */ | |
650e4c94 | 9825 | |
4ee9c684 | 9826 | static tree |
c2f47e15 | 9827 | fold_builtin_0 (tree fndecl, bool ignore ATTRIBUTE_UNUSED) |
650e4c94 | 9828 | { |
e9f80ff5 | 9829 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
c2f47e15 | 9830 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); |
189b3398 | 9831 | switch (fcode) |
650e4c94 | 9832 | { |
c2f47e15 | 9833 | CASE_FLT_FN (BUILT_IN_INF): |
9834 | case BUILT_IN_INFD32: | |
9835 | case BUILT_IN_INFD64: | |
9836 | case BUILT_IN_INFD128: | |
9837 | return fold_builtin_inf (type, true); | |
7c2f0500 | 9838 | |
c2f47e15 | 9839 | CASE_FLT_FN (BUILT_IN_HUGE_VAL): |
9840 | return fold_builtin_inf (type, false); | |
7c2f0500 | 9841 | |
c2f47e15 | 9842 | case BUILT_IN_CLASSIFY_TYPE: |
9843 | return fold_builtin_classify_type (NULL_TREE); | |
7c2f0500 | 9844 | |
c2f47e15 | 9845 | default: |
9846 | break; | |
9847 | } | |
9848 | return NULL_TREE; | |
9849 | } | |
7c2f0500 | 9850 | |
c2f47e15 | 9851 | /* Fold a call to built-in function FNDECL with 1 argument, ARG0. |
9852 | IGNORE is true if the result of the function call is ignored. This | |
9853 | function returns NULL_TREE if no simplification was possible. */ | |
7c2f0500 | 9854 | |
c2f47e15 | 9855 | static tree |
9856 | fold_builtin_1 (tree fndecl, tree arg0, bool ignore) | |
9857 | { | |
9858 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); | |
9859 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
9860 | switch (fcode) | |
9861 | { | |
7c2f0500 | 9862 | |
650e4c94 | 9863 | case BUILT_IN_CONSTANT_P: |
7c2f0500 | 9864 | { |
c2f47e15 | 9865 | tree val = fold_builtin_constant_p (arg0); |
7c2f0500 | 9866 | |
7c2f0500 | 9867 | /* Gimplification will pull the CALL_EXPR for the builtin out of |
9868 | an if condition. When not optimizing, we'll not CSE it back. | |
9869 | To avoid link error types of regressions, return false now. */ | |
9870 | if (!val && !optimize) | |
9871 | val = integer_zero_node; | |
9872 | ||
9873 | return val; | |
9874 | } | |
650e4c94 | 9875 | |
539a3a92 | 9876 | case BUILT_IN_CLASSIFY_TYPE: |
c2f47e15 | 9877 | return fold_builtin_classify_type (arg0); |
539a3a92 | 9878 | |
650e4c94 | 9879 | case BUILT_IN_STRLEN: |
c2f47e15 | 9880 | return fold_builtin_strlen (arg0); |
650e4c94 | 9881 | |
4f35b1fc | 9882 | CASE_FLT_FN (BUILT_IN_FABS): |
c2f47e15 | 9883 | return fold_builtin_fabs (arg0, type); |
d1aade50 | 9884 | |
9885 | case BUILT_IN_ABS: | |
9886 | case BUILT_IN_LABS: | |
9887 | case BUILT_IN_LLABS: | |
9888 | case BUILT_IN_IMAXABS: | |
c2f47e15 | 9889 | return fold_builtin_abs (arg0, type); |
c63f4ad3 | 9890 | |
4f35b1fc | 9891 | CASE_FLT_FN (BUILT_IN_CONJ): |
c2f47e15 | 9892 | if (validate_arg (arg0, COMPLEX_TYPE)) |
9893 | return fold_build1 (CONJ_EXPR, type, arg0); | |
9894 | break; | |
36d3581d | 9895 | |
4f35b1fc | 9896 | CASE_FLT_FN (BUILT_IN_CREAL): |
c2f47e15 | 9897 | if (validate_arg (arg0, COMPLEX_TYPE)) |
9898 | return non_lvalue (fold_build1 (REALPART_EXPR, type, arg0));; | |
9899 | break; | |
36d3581d | 9900 | |
4f35b1fc | 9901 | CASE_FLT_FN (BUILT_IN_CIMAG): |
c2f47e15 | 9902 | if (validate_arg (arg0, COMPLEX_TYPE)) |
9903 | return non_lvalue (fold_build1 (IMAGPART_EXPR, type, arg0)); | |
9904 | break; | |
36d3581d | 9905 | |
503733d5 | 9906 | CASE_FLT_FN (BUILT_IN_CCOS): |
9907 | CASE_FLT_FN (BUILT_IN_CCOSH): | |
9908 | /* These functions are "even", i.e. f(x) == f(-x). */ | |
c2f47e15 | 9909 | if (validate_arg (arg0, COMPLEX_TYPE)) |
9910 | { | |
9911 | tree narg = fold_strip_sign_ops (arg0); | |
503733d5 | 9912 | if (narg) |
c2f47e15 | 9913 | return build_call_expr (fndecl, 1, narg); |
503733d5 | 9914 | } |
c2f47e15 | 9915 | break; |
503733d5 | 9916 | |
4f35b1fc | 9917 | CASE_FLT_FN (BUILT_IN_CABS): |
c2f47e15 | 9918 | return fold_builtin_cabs (arg0, type, fndecl); |
c63f4ad3 | 9919 | |
abe4dcf6 | 9920 | CASE_FLT_FN (BUILT_IN_CARG): |
c2f47e15 | 9921 | return fold_builtin_carg (arg0, type); |
abe4dcf6 | 9922 | |
4f35b1fc | 9923 | CASE_FLT_FN (BUILT_IN_SQRT): |
c2f47e15 | 9924 | return fold_builtin_sqrt (arg0, type); |
805e22b2 | 9925 | |
4f35b1fc | 9926 | CASE_FLT_FN (BUILT_IN_CBRT): |
c2f47e15 | 9927 | return fold_builtin_cbrt (arg0, type); |
3bc5c41b | 9928 | |
728bac60 | 9929 | CASE_FLT_FN (BUILT_IN_ASIN): |
c2f47e15 | 9930 | if (validate_arg (arg0, REAL_TYPE)) |
9931 | return do_mpfr_arg1 (arg0, type, mpfr_asin, | |
728bac60 | 9932 | &dconstm1, &dconst1, true); |
9933 | break; | |
9934 | ||
9935 | CASE_FLT_FN (BUILT_IN_ACOS): | |
c2f47e15 | 9936 | if (validate_arg (arg0, REAL_TYPE)) |
9937 | return do_mpfr_arg1 (arg0, type, mpfr_acos, | |
728bac60 | 9938 | &dconstm1, &dconst1, true); |
9939 | break; | |
9940 | ||
9941 | CASE_FLT_FN (BUILT_IN_ATAN): | |
c2f47e15 | 9942 | if (validate_arg (arg0, REAL_TYPE)) |
9943 | return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0); | |
728bac60 | 9944 | break; |
9945 | ||
9946 | CASE_FLT_FN (BUILT_IN_ASINH): | |
c2f47e15 | 9947 | if (validate_arg (arg0, REAL_TYPE)) |
9948 | return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0); | |
728bac60 | 9949 | break; |
9950 | ||
9951 | CASE_FLT_FN (BUILT_IN_ACOSH): | |
c2f47e15 | 9952 | if (validate_arg (arg0, REAL_TYPE)) |
9953 | return do_mpfr_arg1 (arg0, type, mpfr_acosh, | |
728bac60 | 9954 | &dconst1, NULL, true); |
9955 | break; | |
9956 | ||
9957 | CASE_FLT_FN (BUILT_IN_ATANH): | |
c2f47e15 | 9958 | if (validate_arg (arg0, REAL_TYPE)) |
9959 | return do_mpfr_arg1 (arg0, type, mpfr_atanh, | |
728bac60 | 9960 | &dconstm1, &dconst1, false); |
9961 | break; | |
9962 | ||
4f35b1fc | 9963 | CASE_FLT_FN (BUILT_IN_SIN): |
c2f47e15 | 9964 | if (validate_arg (arg0, REAL_TYPE)) |
9965 | return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0); | |
728bac60 | 9966 | break; |
77e89269 | 9967 | |
4f35b1fc | 9968 | CASE_FLT_FN (BUILT_IN_COS): |
c2f47e15 | 9969 | return fold_builtin_cos (arg0, type, fndecl); |
9970 | break; | |
77e89269 | 9971 | |
728bac60 | 9972 | CASE_FLT_FN (BUILT_IN_TAN): |
c2f47e15 | 9973 | return fold_builtin_tan (arg0, type); |
d735c391 | 9974 | |
c5bb2c4b | 9975 | CASE_FLT_FN (BUILT_IN_CEXP): |
c2f47e15 | 9976 | return fold_builtin_cexp (arg0, type); |
c5bb2c4b | 9977 | |
d735c391 | 9978 | CASE_FLT_FN (BUILT_IN_CEXPI): |
c2f47e15 | 9979 | if (validate_arg (arg0, REAL_TYPE)) |
9980 | return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE); | |
9981 | break; | |
d92f994c | 9982 | |
728bac60 | 9983 | CASE_FLT_FN (BUILT_IN_SINH): |
c2f47e15 | 9984 | if (validate_arg (arg0, REAL_TYPE)) |
9985 | return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0); | |
728bac60 | 9986 | break; |
9987 | ||
9988 | CASE_FLT_FN (BUILT_IN_COSH): | |
c2f47e15 | 9989 | return fold_builtin_cosh (arg0, type, fndecl); |
728bac60 | 9990 | |
9991 | CASE_FLT_FN (BUILT_IN_TANH): | |
c2f47e15 | 9992 | if (validate_arg (arg0, REAL_TYPE)) |
9993 | return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0); | |
728bac60 | 9994 | break; |
9995 | ||
29f4cd78 | 9996 | CASE_FLT_FN (BUILT_IN_ERF): |
c2f47e15 | 9997 | if (validate_arg (arg0, REAL_TYPE)) |
9998 | return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0); | |
29f4cd78 | 9999 | break; |
10000 | ||
10001 | CASE_FLT_FN (BUILT_IN_ERFC): | |
c2f47e15 | 10002 | if (validate_arg (arg0, REAL_TYPE)) |
10003 | return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0); | |
29f4cd78 | 10004 | break; |
10005 | ||
32dba52b | 10006 | CASE_FLT_FN (BUILT_IN_TGAMMA): |
c2f47e15 | 10007 | if (validate_arg (arg0, REAL_TYPE)) |
10008 | return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0); | |
32dba52b | 10009 | break; |
10010 | ||
4f35b1fc | 10011 | CASE_FLT_FN (BUILT_IN_EXP): |
c2f47e15 | 10012 | return fold_builtin_exponent (fndecl, arg0, mpfr_exp); |
467214fd | 10013 | |
4f35b1fc | 10014 | CASE_FLT_FN (BUILT_IN_EXP2): |
c2f47e15 | 10015 | return fold_builtin_exponent (fndecl, arg0, mpfr_exp2); |
467214fd | 10016 | |
4f35b1fc | 10017 | CASE_FLT_FN (BUILT_IN_EXP10): |
10018 | CASE_FLT_FN (BUILT_IN_POW10): | |
c2f47e15 | 10019 | return fold_builtin_exponent (fndecl, arg0, mpfr_exp10); |
467214fd | 10020 | |
29f4cd78 | 10021 | CASE_FLT_FN (BUILT_IN_EXPM1): |
c2f47e15 | 10022 | if (validate_arg (arg0, REAL_TYPE)) |
10023 | return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0); | |
29f4cd78 | 10024 | break; |
10025 | ||
4f35b1fc | 10026 | CASE_FLT_FN (BUILT_IN_LOG): |
c2f47e15 | 10027 | return fold_builtin_logarithm (fndecl, arg0, mpfr_log); |
467214fd | 10028 | |
4f35b1fc | 10029 | CASE_FLT_FN (BUILT_IN_LOG2): |
c2f47e15 | 10030 | return fold_builtin_logarithm (fndecl, arg0, mpfr_log2); |
467214fd | 10031 | |
4f35b1fc | 10032 | CASE_FLT_FN (BUILT_IN_LOG10): |
c2f47e15 | 10033 | return fold_builtin_logarithm (fndecl, arg0, mpfr_log10); |
29f4cd78 | 10034 | |
10035 | CASE_FLT_FN (BUILT_IN_LOG1P): | |
c2f47e15 | 10036 | if (validate_arg (arg0, REAL_TYPE)) |
10037 | return do_mpfr_arg1 (arg0, type, mpfr_log1p, | |
29f4cd78 | 10038 | &dconstm1, NULL, false); |
10039 | break; | |
805e22b2 | 10040 | |
65dd1378 | 10041 | #if MPFR_VERSION >= MPFR_VERSION_NUM(2,3,0) |
10042 | CASE_FLT_FN (BUILT_IN_J0): | |
10043 | if (validate_arg (arg0, REAL_TYPE)) | |
10044 | return do_mpfr_arg1 (arg0, type, mpfr_j0, | |
10045 | NULL, NULL, 0); | |
10046 | break; | |
10047 | ||
10048 | CASE_FLT_FN (BUILT_IN_J1): | |
10049 | if (validate_arg (arg0, REAL_TYPE)) | |
10050 | return do_mpfr_arg1 (arg0, type, mpfr_j1, | |
10051 | NULL, NULL, 0); | |
10052 | break; | |
6ff9eeff | 10053 | |
10054 | CASE_FLT_FN (BUILT_IN_Y0): | |
10055 | if (validate_arg (arg0, REAL_TYPE)) | |
10056 | return do_mpfr_arg1 (arg0, type, mpfr_y0, | |
10057 | &dconst0, NULL, false); | |
10058 | break; | |
10059 | ||
10060 | CASE_FLT_FN (BUILT_IN_Y1): | |
10061 | if (validate_arg (arg0, REAL_TYPE)) | |
10062 | return do_mpfr_arg1 (arg0, type, mpfr_y1, | |
10063 | &dconst0, NULL, false); | |
10064 | break; | |
65dd1378 | 10065 | #endif |
10066 | ||
4f35b1fc | 10067 | CASE_FLT_FN (BUILT_IN_NAN): |
c4503c0a | 10068 | case BUILT_IN_NAND32: |
10069 | case BUILT_IN_NAND64: | |
10070 | case BUILT_IN_NAND128: | |
c2f47e15 | 10071 | return fold_builtin_nan (arg0, type, true); |
b0db7939 | 10072 | |
4f35b1fc | 10073 | CASE_FLT_FN (BUILT_IN_NANS): |
c2f47e15 | 10074 | return fold_builtin_nan (arg0, type, false); |
b0db7939 | 10075 | |
4f35b1fc | 10076 | CASE_FLT_FN (BUILT_IN_FLOOR): |
c2f47e15 | 10077 | return fold_builtin_floor (fndecl, arg0); |
277f8dd2 | 10078 | |
4f35b1fc | 10079 | CASE_FLT_FN (BUILT_IN_CEIL): |
c2f47e15 | 10080 | return fold_builtin_ceil (fndecl, arg0); |
277f8dd2 | 10081 | |
4f35b1fc | 10082 | CASE_FLT_FN (BUILT_IN_TRUNC): |
c2f47e15 | 10083 | return fold_builtin_trunc (fndecl, arg0); |
277f8dd2 | 10084 | |
4f35b1fc | 10085 | CASE_FLT_FN (BUILT_IN_ROUND): |
c2f47e15 | 10086 | return fold_builtin_round (fndecl, arg0); |
89ab3887 | 10087 | |
4f35b1fc | 10088 | CASE_FLT_FN (BUILT_IN_NEARBYINT): |
10089 | CASE_FLT_FN (BUILT_IN_RINT): | |
c2f47e15 | 10090 | return fold_trunc_transparent_mathfn (fndecl, arg0); |
6528f4f4 | 10091 | |
4f35b1fc | 10092 | CASE_FLT_FN (BUILT_IN_LCEIL): |
10093 | CASE_FLT_FN (BUILT_IN_LLCEIL): | |
10094 | CASE_FLT_FN (BUILT_IN_LFLOOR): | |
10095 | CASE_FLT_FN (BUILT_IN_LLFLOOR): | |
a0c938f0 | 10096 | CASE_FLT_FN (BUILT_IN_LROUND): |
4f35b1fc | 10097 | CASE_FLT_FN (BUILT_IN_LLROUND): |
c2f47e15 | 10098 | return fold_builtin_int_roundingfn (fndecl, arg0); |
34f17811 | 10099 | |
4f35b1fc | 10100 | CASE_FLT_FN (BUILT_IN_LRINT): |
10101 | CASE_FLT_FN (BUILT_IN_LLRINT): | |
c2f47e15 | 10102 | return fold_fixed_mathfn (fndecl, arg0); |
9ed65c7f | 10103 | |
42791117 | 10104 | case BUILT_IN_BSWAP32: |
10105 | case BUILT_IN_BSWAP64: | |
c2f47e15 | 10106 | return fold_builtin_bswap (fndecl, arg0); |
42791117 | 10107 | |
4f35b1fc | 10108 | CASE_INT_FN (BUILT_IN_FFS): |
10109 | CASE_INT_FN (BUILT_IN_CLZ): | |
10110 | CASE_INT_FN (BUILT_IN_CTZ): | |
10111 | CASE_INT_FN (BUILT_IN_POPCOUNT): | |
10112 | CASE_INT_FN (BUILT_IN_PARITY): | |
c2f47e15 | 10113 | return fold_builtin_bitop (fndecl, arg0); |
9c8a1629 | 10114 | |
4f35b1fc | 10115 | CASE_FLT_FN (BUILT_IN_SIGNBIT): |
c2f47e15 | 10116 | return fold_builtin_signbit (arg0, type); |
27f261ef | 10117 | |
cb2b9385 | 10118 | CASE_FLT_FN (BUILT_IN_SIGNIFICAND): |
10119 | return fold_builtin_significand (arg0, type); | |
10120 | ||
10121 | CASE_FLT_FN (BUILT_IN_ILOGB): | |
10122 | CASE_FLT_FN (BUILT_IN_LOGB): | |
10123 | return fold_builtin_logb (arg0, type); | |
10124 | ||
d49367d4 | 10125 | case BUILT_IN_ISASCII: |
c2f47e15 | 10126 | return fold_builtin_isascii (arg0); |
d49367d4 | 10127 | |
10128 | case BUILT_IN_TOASCII: | |
c2f47e15 | 10129 | return fold_builtin_toascii (arg0); |
d49367d4 | 10130 | |
df1cf42e | 10131 | case BUILT_IN_ISDIGIT: |
c2f47e15 | 10132 | return fold_builtin_isdigit (arg0); |
467214fd | 10133 | |
4f35b1fc | 10134 | CASE_FLT_FN (BUILT_IN_FINITE): |
c4503c0a | 10135 | case BUILT_IN_FINITED32: |
10136 | case BUILT_IN_FINITED64: | |
10137 | case BUILT_IN_FINITED128: | |
cde061c1 | 10138 | case BUILT_IN_ISFINITE: |
10139 | return fold_builtin_classify (fndecl, arg0, BUILT_IN_ISFINITE); | |
726069ba | 10140 | |
4f35b1fc | 10141 | CASE_FLT_FN (BUILT_IN_ISINF): |
c4503c0a | 10142 | case BUILT_IN_ISINFD32: |
10143 | case BUILT_IN_ISINFD64: | |
10144 | case BUILT_IN_ISINFD128: | |
c2f47e15 | 10145 | return fold_builtin_classify (fndecl, arg0, BUILT_IN_ISINF); |
726069ba | 10146 | |
c319d56a | 10147 | case BUILT_IN_ISINF_SIGN: |
10148 | return fold_builtin_classify (fndecl, arg0, BUILT_IN_ISINF_SIGN); | |
10149 | ||
4f35b1fc | 10150 | CASE_FLT_FN (BUILT_IN_ISNAN): |
c4503c0a | 10151 | case BUILT_IN_ISNAND32: |
10152 | case BUILT_IN_ISNAND64: | |
10153 | case BUILT_IN_ISNAND128: | |
c2f47e15 | 10154 | return fold_builtin_classify (fndecl, arg0, BUILT_IN_ISNAN); |
10155 | ||
10156 | case BUILT_IN_PRINTF: | |
10157 | case BUILT_IN_PRINTF_UNLOCKED: | |
10158 | case BUILT_IN_VPRINTF: | |
10159 | return fold_builtin_printf (fndecl, arg0, NULL_TREE, ignore, fcode); | |
10160 | ||
10161 | default: | |
10162 | break; | |
10163 | } | |
10164 | ||
10165 | return NULL_TREE; | |
10166 | ||
10167 | } | |
10168 | ||
10169 | /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1. | |
10170 | IGNORE is true if the result of the function call is ignored. This | |
10171 | function returns NULL_TREE if no simplification was possible. */ | |
10172 | ||
10173 | static tree | |
10174 | fold_builtin_2 (tree fndecl, tree arg0, tree arg1, bool ignore) | |
10175 | { | |
10176 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); | |
10177 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
10178 | ||
10179 | switch (fcode) | |
10180 | { | |
65dd1378 | 10181 | #if MPFR_VERSION >= MPFR_VERSION_NUM(2,3,0) |
10182 | CASE_FLT_FN (BUILT_IN_JN): | |
10183 | if (validate_arg (arg0, INTEGER_TYPE) | |
10184 | && validate_arg (arg1, REAL_TYPE)) | |
10185 | return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0); | |
10186 | break; | |
6ff9eeff | 10187 | |
10188 | CASE_FLT_FN (BUILT_IN_YN): | |
10189 | if (validate_arg (arg0, INTEGER_TYPE) | |
10190 | && validate_arg (arg1, REAL_TYPE)) | |
10191 | return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn, | |
10192 | &dconst0, false); | |
10193 | break; | |
e5407ca6 | 10194 | |
10195 | CASE_FLT_FN (BUILT_IN_DREM): | |
10196 | CASE_FLT_FN (BUILT_IN_REMAINDER): | |
10197 | if (validate_arg (arg0, REAL_TYPE) | |
10198 | && validate_arg(arg1, REAL_TYPE)) | |
10199 | return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder); | |
10200 | break; | |
e84da7c1 | 10201 | |
10202 | CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */ | |
10203 | CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */ | |
10204 | if (validate_arg (arg0, REAL_TYPE) | |
10205 | && validate_arg(arg1, POINTER_TYPE)) | |
10206 | return do_mpfr_lgamma_r (arg0, arg1, type); | |
10207 | break; | |
65dd1378 | 10208 | #endif |
c2f47e15 | 10209 | |
10210 | CASE_FLT_FN (BUILT_IN_ATAN2): | |
10211 | if (validate_arg (arg0, REAL_TYPE) | |
10212 | && validate_arg(arg1, REAL_TYPE)) | |
10213 | return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2); | |
10214 | break; | |
10215 | ||
10216 | CASE_FLT_FN (BUILT_IN_FDIM): | |
10217 | if (validate_arg (arg0, REAL_TYPE) | |
10218 | && validate_arg(arg1, REAL_TYPE)) | |
10219 | return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim); | |
10220 | break; | |
10221 | ||
10222 | CASE_FLT_FN (BUILT_IN_HYPOT): | |
10223 | return fold_builtin_hypot (fndecl, arg0, arg1, type); | |
10224 | ||
7587301b | 10225 | CASE_FLT_FN (BUILT_IN_LDEXP): |
10226 | return fold_builtin_load_exponent (arg0, arg1, type, /*ldexp=*/true); | |
10227 | CASE_FLT_FN (BUILT_IN_SCALBN): | |
10228 | CASE_FLT_FN (BUILT_IN_SCALBLN): | |
10229 | return fold_builtin_load_exponent (arg0, arg1, type, /*ldexp=*/false); | |
10230 | ||
3838b9ae | 10231 | CASE_FLT_FN (BUILT_IN_FREXP): |
10232 | return fold_builtin_frexp (arg0, arg1, type); | |
10233 | ||
ebf8b4f5 | 10234 | CASE_FLT_FN (BUILT_IN_MODF): |
10235 | return fold_builtin_modf (arg0, arg1, type); | |
10236 | ||
c2f47e15 | 10237 | case BUILT_IN_BZERO: |
10238 | return fold_builtin_bzero (arg0, arg1, ignore); | |
10239 | ||
10240 | case BUILT_IN_FPUTS: | |
10241 | return fold_builtin_fputs (arg0, arg1, ignore, false, NULL_TREE); | |
10242 | ||
10243 | case BUILT_IN_FPUTS_UNLOCKED: | |
10244 | return fold_builtin_fputs (arg0, arg1, ignore, true, NULL_TREE); | |
10245 | ||
10246 | case BUILT_IN_STRSTR: | |
10247 | return fold_builtin_strstr (arg0, arg1, type); | |
10248 | ||
10249 | case BUILT_IN_STRCAT: | |
10250 | return fold_builtin_strcat (arg0, arg1); | |
10251 | ||
10252 | case BUILT_IN_STRSPN: | |
10253 | return fold_builtin_strspn (arg0, arg1); | |
10254 | ||
10255 | case BUILT_IN_STRCSPN: | |
10256 | return fold_builtin_strcspn (arg0, arg1); | |
10257 | ||
10258 | case BUILT_IN_STRCHR: | |
10259 | case BUILT_IN_INDEX: | |
10260 | return fold_builtin_strchr (arg0, arg1, type); | |
10261 | ||
10262 | case BUILT_IN_STRRCHR: | |
10263 | case BUILT_IN_RINDEX: | |
10264 | return fold_builtin_strrchr (arg0, arg1, type); | |
10265 | ||
10266 | case BUILT_IN_STRCPY: | |
10267 | return fold_builtin_strcpy (fndecl, arg0, arg1, NULL_TREE); | |
10268 | ||
10269 | case BUILT_IN_STRCMP: | |
10270 | return fold_builtin_strcmp (arg0, arg1); | |
10271 | ||
10272 | case BUILT_IN_STRPBRK: | |
10273 | return fold_builtin_strpbrk (arg0, arg1, type); | |
10274 | ||
10275 | case BUILT_IN_EXPECT: | |
76f5a783 | 10276 | return fold_builtin_expect (arg0, arg1); |
c2f47e15 | 10277 | |
10278 | CASE_FLT_FN (BUILT_IN_POW): | |
10279 | return fold_builtin_pow (fndecl, arg0, arg1, type); | |
10280 | ||
10281 | CASE_FLT_FN (BUILT_IN_POWI): | |
10282 | return fold_builtin_powi (fndecl, arg0, arg1, type); | |
10283 | ||
10284 | CASE_FLT_FN (BUILT_IN_COPYSIGN): | |
10285 | return fold_builtin_copysign (fndecl, arg0, arg1, type); | |
10286 | ||
10287 | CASE_FLT_FN (BUILT_IN_FMIN): | |
10288 | return fold_builtin_fmin_fmax (arg0, arg1, type, /*max=*/false); | |
10289 | ||
10290 | CASE_FLT_FN (BUILT_IN_FMAX): | |
10291 | return fold_builtin_fmin_fmax (arg0, arg1, type, /*max=*/true); | |
726069ba | 10292 | |
9bc9f15f | 10293 | case BUILT_IN_ISGREATER: |
c2f47e15 | 10294 | return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNLE_EXPR, LE_EXPR); |
9bc9f15f | 10295 | case BUILT_IN_ISGREATEREQUAL: |
c2f47e15 | 10296 | return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNLT_EXPR, LT_EXPR); |
9bc9f15f | 10297 | case BUILT_IN_ISLESS: |
c2f47e15 | 10298 | return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNGE_EXPR, GE_EXPR); |
9bc9f15f | 10299 | case BUILT_IN_ISLESSEQUAL: |
c2f47e15 | 10300 | return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNGT_EXPR, GT_EXPR); |
9bc9f15f | 10301 | case BUILT_IN_ISLESSGREATER: |
c2f47e15 | 10302 | return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNEQ_EXPR, EQ_EXPR); |
9bc9f15f | 10303 | case BUILT_IN_ISUNORDERED: |
c2f47e15 | 10304 | return fold_builtin_unordered_cmp (fndecl, arg0, arg1, UNORDERED_EXPR, |
d5019fe8 | 10305 | NOP_EXPR); |
9bc9f15f | 10306 | |
7c2f0500 | 10307 | /* We do the folding for va_start in the expander. */ |
10308 | case BUILT_IN_VA_START: | |
10309 | break; | |
f0613857 | 10310 | |
c2f47e15 | 10311 | case BUILT_IN_SPRINTF: |
10312 | return fold_builtin_sprintf (arg0, arg1, NULL_TREE, ignore); | |
10313 | ||
0a39fd54 | 10314 | case BUILT_IN_OBJECT_SIZE: |
c2f47e15 | 10315 | return fold_builtin_object_size (arg0, arg1); |
0a39fd54 | 10316 | |
10317 | case BUILT_IN_PRINTF: | |
10318 | case BUILT_IN_PRINTF_UNLOCKED: | |
10319 | case BUILT_IN_VPRINTF: | |
c2f47e15 | 10320 | return fold_builtin_printf (fndecl, arg0, arg1, ignore, fcode); |
10321 | ||
0a39fd54 | 10322 | case BUILT_IN_PRINTF_CHK: |
10323 | case BUILT_IN_VPRINTF_CHK: | |
c2f47e15 | 10324 | if (!validate_arg (arg0, INTEGER_TYPE) |
10325 | || TREE_SIDE_EFFECTS (arg0)) | |
10326 | return NULL_TREE; | |
10327 | else | |
10328 | return fold_builtin_printf (fndecl, arg1, NULL_TREE, ignore, fcode); | |
10329 | break; | |
0a39fd54 | 10330 | |
10331 | case BUILT_IN_FPRINTF: | |
10332 | case BUILT_IN_FPRINTF_UNLOCKED: | |
10333 | case BUILT_IN_VFPRINTF: | |
c2f47e15 | 10334 | return fold_builtin_fprintf (fndecl, arg0, arg1, NULL_TREE, |
10335 | ignore, fcode); | |
10336 | ||
10337 | default: | |
10338 | break; | |
10339 | } | |
10340 | return NULL_TREE; | |
10341 | } | |
10342 | ||
10343 | /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1, | |
10344 | and ARG2. IGNORE is true if the result of the function call is ignored. | |
10345 | This function returns NULL_TREE if no simplification was possible. */ | |
10346 | ||
10347 | static tree | |
10348 | fold_builtin_3 (tree fndecl, tree arg0, tree arg1, tree arg2, bool ignore) | |
10349 | { | |
10350 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); | |
10351 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
10352 | switch (fcode) | |
10353 | { | |
10354 | ||
10355 | CASE_FLT_FN (BUILT_IN_SINCOS): | |
10356 | return fold_builtin_sincos (arg0, arg1, arg2); | |
10357 | ||
10358 | CASE_FLT_FN (BUILT_IN_FMA): | |
10359 | if (validate_arg (arg0, REAL_TYPE) | |
10360 | && validate_arg(arg1, REAL_TYPE) | |
10361 | && validate_arg(arg2, REAL_TYPE)) | |
10362 | return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma); | |
10363 | break; | |
10364 | ||
e5407ca6 | 10365 | #if MPFR_VERSION >= MPFR_VERSION_NUM(2,3,0) |
10366 | CASE_FLT_FN (BUILT_IN_REMQUO): | |
10367 | if (validate_arg (arg0, REAL_TYPE) | |
10368 | && validate_arg(arg1, REAL_TYPE) | |
10369 | && validate_arg(arg2, POINTER_TYPE)) | |
10370 | return do_mpfr_remquo (arg0, arg1, arg2); | |
10371 | break; | |
10372 | #endif | |
10373 | ||
c2f47e15 | 10374 | case BUILT_IN_MEMSET: |
10375 | return fold_builtin_memset (arg0, arg1, arg2, type, ignore); | |
10376 | ||
10377 | case BUILT_IN_BCOPY: | |
10378 | return fold_builtin_memory_op (arg1, arg0, arg2, void_type_node, true, /*endp=*/3); | |
10379 | ||
10380 | case BUILT_IN_MEMCPY: | |
10381 | return fold_builtin_memory_op (arg0, arg1, arg2, type, ignore, /*endp=*/0); | |
10382 | ||
10383 | case BUILT_IN_MEMPCPY: | |
10384 | return fold_builtin_memory_op (arg0, arg1, arg2, type, ignore, /*endp=*/1); | |
10385 | ||
10386 | case BUILT_IN_MEMMOVE: | |
10387 | return fold_builtin_memory_op (arg0, arg1, arg2, type, ignore, /*endp=*/3); | |
10388 | ||
10389 | case BUILT_IN_STRNCAT: | |
10390 | return fold_builtin_strncat (arg0, arg1, arg2); | |
10391 | ||
10392 | case BUILT_IN_STRNCPY: | |
10393 | return fold_builtin_strncpy (fndecl, arg0, arg1, arg2, NULL_TREE); | |
10394 | ||
10395 | case BUILT_IN_STRNCMP: | |
10396 | return fold_builtin_strncmp (arg0, arg1, arg2); | |
10397 | ||
7959b13b | 10398 | case BUILT_IN_MEMCHR: |
10399 | return fold_builtin_memchr (arg0, arg1, arg2, type); | |
10400 | ||
c2f47e15 | 10401 | case BUILT_IN_BCMP: |
10402 | case BUILT_IN_MEMCMP: | |
10403 | return fold_builtin_memcmp (arg0, arg1, arg2);; | |
10404 | ||
10405 | case BUILT_IN_SPRINTF: | |
10406 | return fold_builtin_sprintf (arg0, arg1, arg2, ignore); | |
10407 | ||
10408 | case BUILT_IN_STRCPY_CHK: | |
10409 | case BUILT_IN_STPCPY_CHK: | |
10410 | return fold_builtin_stxcpy_chk (fndecl, arg0, arg1, arg2, NULL_TREE, | |
10411 | ignore, fcode); | |
10412 | ||
10413 | case BUILT_IN_STRCAT_CHK: | |
10414 | return fold_builtin_strcat_chk (fndecl, arg0, arg1, arg2); | |
10415 | ||
10416 | case BUILT_IN_PRINTF_CHK: | |
10417 | case BUILT_IN_VPRINTF_CHK: | |
10418 | if (!validate_arg (arg0, INTEGER_TYPE) | |
10419 | || TREE_SIDE_EFFECTS (arg0)) | |
10420 | return NULL_TREE; | |
10421 | else | |
10422 | return fold_builtin_printf (fndecl, arg1, arg2, ignore, fcode); | |
10423 | break; | |
10424 | ||
10425 | case BUILT_IN_FPRINTF: | |
10426 | case BUILT_IN_FPRINTF_UNLOCKED: | |
10427 | case BUILT_IN_VFPRINTF: | |
10428 | return fold_builtin_fprintf (fndecl, arg0, arg1, arg2, ignore, fcode); | |
10429 | ||
0a39fd54 | 10430 | case BUILT_IN_FPRINTF_CHK: |
10431 | case BUILT_IN_VFPRINTF_CHK: | |
c2f47e15 | 10432 | if (!validate_arg (arg1, INTEGER_TYPE) |
10433 | || TREE_SIDE_EFFECTS (arg1)) | |
10434 | return NULL_TREE; | |
10435 | else | |
10436 | return fold_builtin_fprintf (fndecl, arg0, arg2, NULL_TREE, | |
10437 | ignore, fcode); | |
0a39fd54 | 10438 | |
650e4c94 | 10439 | default: |
10440 | break; | |
10441 | } | |
c2f47e15 | 10442 | return NULL_TREE; |
10443 | } | |
650e4c94 | 10444 | |
c2f47e15 | 10445 | /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1, |
10446 | ARG2, and ARG3. IGNORE is true if the result of the function call is | |
10447 | ignored. This function returns NULL_TREE if no simplification was | |
10448 | possible. */ | |
10449 | ||
10450 | static tree | |
10451 | fold_builtin_4 (tree fndecl, tree arg0, tree arg1, tree arg2, tree arg3, | |
10452 | bool ignore) | |
10453 | { | |
10454 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
10455 | ||
10456 | switch (fcode) | |
10457 | { | |
10458 | case BUILT_IN_MEMCPY_CHK: | |
10459 | case BUILT_IN_MEMPCPY_CHK: | |
10460 | case BUILT_IN_MEMMOVE_CHK: | |
10461 | case BUILT_IN_MEMSET_CHK: | |
10462 | return fold_builtin_memory_chk (fndecl, arg0, arg1, arg2, arg3, | |
10463 | NULL_TREE, ignore, | |
10464 | DECL_FUNCTION_CODE (fndecl)); | |
10465 | ||
10466 | case BUILT_IN_STRNCPY_CHK: | |
10467 | return fold_builtin_strncpy_chk (arg0, arg1, arg2, arg3, NULL_TREE); | |
10468 | ||
10469 | case BUILT_IN_STRNCAT_CHK: | |
10470 | return fold_builtin_strncat_chk (fndecl, arg0, arg1, arg2, arg3); | |
10471 | ||
10472 | case BUILT_IN_FPRINTF_CHK: | |
10473 | case BUILT_IN_VFPRINTF_CHK: | |
10474 | if (!validate_arg (arg1, INTEGER_TYPE) | |
10475 | || TREE_SIDE_EFFECTS (arg1)) | |
10476 | return NULL_TREE; | |
10477 | else | |
10478 | return fold_builtin_fprintf (fndecl, arg0, arg2, arg3, | |
10479 | ignore, fcode); | |
10480 | break; | |
10481 | ||
10482 | default: | |
10483 | break; | |
10484 | } | |
10485 | return NULL_TREE; | |
10486 | } | |
10487 | ||
10488 | /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS | |
10489 | arguments, where NARGS <= 4. IGNORE is true if the result of the | |
10490 | function call is ignored. This function returns NULL_TREE if no | |
10491 | simplification was possible. Note that this only folds builtins with | |
10492 | fixed argument patterns. Foldings that do varargs-to-varargs | |
10493 | transformations, or that match calls with more than 4 arguments, | |
10494 | need to be handled with fold_builtin_varargs instead. */ | |
10495 | ||
10496 | #define MAX_ARGS_TO_FOLD_BUILTIN 4 | |
10497 | ||
10498 | static tree | |
10499 | fold_builtin_n (tree fndecl, tree *args, int nargs, bool ignore) | |
10500 | { | |
10501 | tree ret = NULL_TREE; | |
a7f5bb2d | 10502 | |
c2f47e15 | 10503 | switch (nargs) |
10504 | { | |
10505 | case 0: | |
10506 | ret = fold_builtin_0 (fndecl, ignore); | |
10507 | break; | |
10508 | case 1: | |
10509 | ret = fold_builtin_1 (fndecl, args[0], ignore); | |
10510 | break; | |
10511 | case 2: | |
10512 | ret = fold_builtin_2 (fndecl, args[0], args[1], ignore); | |
10513 | break; | |
10514 | case 3: | |
10515 | ret = fold_builtin_3 (fndecl, args[0], args[1], args[2], ignore); | |
10516 | break; | |
10517 | case 4: | |
10518 | ret = fold_builtin_4 (fndecl, args[0], args[1], args[2], args[3], | |
10519 | ignore); | |
10520 | break; | |
10521 | default: | |
10522 | break; | |
10523 | } | |
10524 | if (ret) | |
10525 | { | |
10526 | ret = build1 (NOP_EXPR, GENERIC_TREE_TYPE (ret), ret); | |
10527 | TREE_NO_WARNING (ret) = 1; | |
10528 | return ret; | |
10529 | } | |
10530 | return NULL_TREE; | |
10531 | } | |
10532 | ||
10533 | /* Builtins with folding operations that operate on "..." arguments | |
10534 | need special handling; we need to store the arguments in a convenient | |
10535 | data structure before attempting any folding. Fortunately there are | |
10536 | only a few builtins that fall into this category. FNDECL is the | |
10537 | function, EXP is the CALL_EXPR for the call, and IGNORE is true if the | |
10538 | result of the function call is ignored. */ | |
10539 | ||
10540 | static tree | |
10541 | fold_builtin_varargs (tree fndecl, tree exp, bool ignore ATTRIBUTE_UNUSED) | |
10542 | { | |
10543 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
10544 | tree ret = NULL_TREE; | |
10545 | ||
10546 | switch (fcode) | |
10547 | { | |
10548 | case BUILT_IN_SPRINTF_CHK: | |
10549 | case BUILT_IN_VSPRINTF_CHK: | |
10550 | ret = fold_builtin_sprintf_chk (exp, fcode); | |
10551 | break; | |
10552 | ||
10553 | case BUILT_IN_SNPRINTF_CHK: | |
10554 | case BUILT_IN_VSNPRINTF_CHK: | |
10555 | ret = fold_builtin_snprintf_chk (exp, NULL_TREE, fcode); | |
19fbe3a4 | 10556 | break; |
10557 | ||
10558 | case BUILT_IN_FPCLASSIFY: | |
10559 | ret = fold_builtin_fpclassify (exp); | |
10560 | break; | |
c2f47e15 | 10561 | |
10562 | default: | |
10563 | break; | |
10564 | } | |
10565 | if (ret) | |
10566 | { | |
10567 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); | |
10568 | TREE_NO_WARNING (ret) = 1; | |
10569 | return ret; | |
10570 | } | |
10571 | return NULL_TREE; | |
650e4c94 | 10572 | } |
7e15618b | 10573 | |
4ee9c684 | 10574 | /* A wrapper function for builtin folding that prevents warnings for |
491e04ef | 10575 | "statement without effect" and the like, caused by removing the |
4ee9c684 | 10576 | call node earlier than the warning is generated. */ |
10577 | ||
10578 | tree | |
c2f47e15 | 10579 | fold_call_expr (tree exp, bool ignore) |
4ee9c684 | 10580 | { |
c2f47e15 | 10581 | tree ret = NULL_TREE; |
10582 | tree fndecl = get_callee_fndecl (exp); | |
10583 | if (fndecl | |
10584 | && TREE_CODE (fndecl) == FUNCTION_DECL | |
48dc2227 | 10585 | && DECL_BUILT_IN (fndecl) |
10586 | /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized | |
10587 | yet. Defer folding until we see all the arguments | |
10588 | (after inlining). */ | |
10589 | && !CALL_EXPR_VA_ARG_PACK (exp)) | |
10590 | { | |
10591 | int nargs = call_expr_nargs (exp); | |
10592 | ||
10593 | /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but | |
10594 | instead last argument is __builtin_va_arg_pack (). Defer folding | |
10595 | even in that case, until arguments are finalized. */ | |
10596 | if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR) | |
10597 | { | |
10598 | tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1)); | |
10599 | if (fndecl2 | |
10600 | && TREE_CODE (fndecl2) == FUNCTION_DECL | |
10601 | && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL | |
10602 | && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK) | |
10603 | return NULL_TREE; | |
10604 | } | |
10605 | ||
c2f47e15 | 10606 | /* FIXME: Don't use a list in this interface. */ |
10607 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) | |
10608 | return targetm.fold_builtin (fndecl, CALL_EXPR_ARGS (exp), ignore); | |
10609 | else | |
10610 | { | |
c2f47e15 | 10611 | if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN) |
10612 | { | |
10613 | tree *args = CALL_EXPR_ARGP (exp); | |
10614 | ret = fold_builtin_n (fndecl, args, nargs, ignore); | |
10615 | } | |
10616 | if (!ret) | |
10617 | ret = fold_builtin_varargs (fndecl, exp, ignore); | |
10618 | if (ret) | |
10619 | { | |
10620 | /* Propagate location information from original call to | |
10621 | expansion of builtin. Otherwise things like | |
10622 | maybe_emit_chk_warning, that operate on the expansion | |
10623 | of a builtin, will use the wrong location information. */ | |
10624 | if (CAN_HAVE_LOCATION_P (exp) && EXPR_HAS_LOCATION (exp)) | |
10625 | { | |
10626 | tree realret = ret; | |
10627 | if (TREE_CODE (ret) == NOP_EXPR) | |
10628 | realret = TREE_OPERAND (ret, 0); | |
10629 | if (CAN_HAVE_LOCATION_P (realret) | |
10630 | && !EXPR_HAS_LOCATION (realret)) | |
10631 | SET_EXPR_LOCATION (realret, EXPR_LOCATION (exp)); | |
55c10931 | 10632 | return realret; |
c2f47e15 | 10633 | } |
10634 | return ret; | |
10635 | } | |
10636 | } | |
4ee9c684 | 10637 | } |
c2f47e15 | 10638 | return NULL_TREE; |
10639 | } | |
10640 | ||
10641 | /* Conveniently construct a function call expression. FNDECL names the | |
10642 | function to be called and ARGLIST is a TREE_LIST of arguments. */ | |
10643 | ||
10644 | tree | |
10645 | build_function_call_expr (tree fndecl, tree arglist) | |
10646 | { | |
10647 | tree fntype = TREE_TYPE (fndecl); | |
10648 | tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl); | |
d01f58f9 | 10649 | int n = list_length (arglist); |
10650 | tree *argarray = (tree *) alloca (n * sizeof (tree)); | |
10651 | int i; | |
10652 | ||
10653 | for (i = 0; i < n; i++, arglist = TREE_CHAIN (arglist)) | |
10654 | argarray[i] = TREE_VALUE (arglist); | |
10655 | return fold_builtin_call_array (TREE_TYPE (fntype), fn, n, argarray); | |
c2f47e15 | 10656 | } |
10657 | ||
10658 | /* Conveniently construct a function call expression. FNDECL names the | |
10659 | function to be called, N is the number of arguments, and the "..." | |
10660 | parameters are the argument expressions. */ | |
10661 | ||
10662 | tree | |
10663 | build_call_expr (tree fndecl, int n, ...) | |
10664 | { | |
10665 | va_list ap; | |
c2f47e15 | 10666 | tree fntype = TREE_TYPE (fndecl); |
10667 | tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl); | |
d01f58f9 | 10668 | tree *argarray = (tree *) alloca (n * sizeof (tree)); |
10669 | int i; | |
c2f47e15 | 10670 | |
10671 | va_start (ap, n); | |
d01f58f9 | 10672 | for (i = 0; i < n; i++) |
10673 | argarray[i] = va_arg (ap, tree); | |
c2f47e15 | 10674 | va_end (ap); |
d01f58f9 | 10675 | return fold_builtin_call_array (TREE_TYPE (fntype), fn, n, argarray); |
4ee9c684 | 10676 | } |
10677 | ||
c2f47e15 | 10678 | /* Construct a CALL_EXPR with type TYPE with FN as the function expression. |
d01f58f9 | 10679 | N arguments are passed in the array ARGARRAY. */ |
805e22b2 | 10680 | |
10681 | tree | |
d01f58f9 | 10682 | fold_builtin_call_array (tree type, |
10683 | tree fn, | |
10684 | int n, | |
10685 | tree *argarray) | |
7e15618b | 10686 | { |
c2f47e15 | 10687 | tree ret = NULL_TREE; |
10688 | int i; | |
10689 | tree exp; | |
10690 | ||
10691 | if (TREE_CODE (fn) == ADDR_EXPR) | |
10692 | { | |
10693 | tree fndecl = TREE_OPERAND (fn, 0); | |
10694 | if (TREE_CODE (fndecl) == FUNCTION_DECL | |
10695 | && DECL_BUILT_IN (fndecl)) | |
10696 | { | |
48dc2227 | 10697 | /* If last argument is __builtin_va_arg_pack (), arguments to this |
10698 | function are not finalized yet. Defer folding until they are. */ | |
10699 | if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR) | |
10700 | { | |
10701 | tree fndecl2 = get_callee_fndecl (argarray[n - 1]); | |
10702 | if (fndecl2 | |
10703 | && TREE_CODE (fndecl2) == FUNCTION_DECL | |
10704 | && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL | |
10705 | && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK) | |
10706 | return build_call_array (type, fn, n, argarray); | |
10707 | } | |
c2f47e15 | 10708 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) |
10709 | { | |
10710 | tree arglist = NULL_TREE; | |
d01f58f9 | 10711 | for (i = n - 1; i >= 0; i--) |
10712 | arglist = tree_cons (NULL_TREE, argarray[i], arglist); | |
c2f47e15 | 10713 | ret = targetm.fold_builtin (fndecl, arglist, false); |
10714 | if (ret) | |
10715 | return ret; | |
10716 | } | |
10717 | else if (n <= MAX_ARGS_TO_FOLD_BUILTIN) | |
10718 | { | |
10719 | /* First try the transformations that don't require consing up | |
10720 | an exp. */ | |
d01f58f9 | 10721 | ret = fold_builtin_n (fndecl, argarray, n, false); |
c2f47e15 | 10722 | if (ret) |
10723 | return ret; | |
10724 | } | |
10725 | ||
10726 | /* If we got this far, we need to build an exp. */ | |
d01f58f9 | 10727 | exp = build_call_array (type, fn, n, argarray); |
c2f47e15 | 10728 | ret = fold_builtin_varargs (fndecl, exp, false); |
10729 | return ret ? ret : exp; | |
10730 | } | |
10731 | } | |
10732 | ||
d01f58f9 | 10733 | return build_call_array (type, fn, n, argarray); |
c2f47e15 | 10734 | } |
10735 | ||
10736 | /* Construct a new CALL_EXPR using the tail of the argument list of EXP | |
10737 | along with N new arguments specified as the "..." parameters. SKIP | |
10738 | is the number of arguments in EXP to be omitted. This function is used | |
10739 | to do varargs-to-varargs transformations. */ | |
10740 | ||
10741 | static tree | |
10742 | rewrite_call_expr (tree exp, int skip, tree fndecl, int n, ...) | |
10743 | { | |
10744 | int oldnargs = call_expr_nargs (exp); | |
10745 | int nargs = oldnargs - skip + n; | |
10746 | tree fntype = TREE_TYPE (fndecl); | |
10747 | tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl); | |
10748 | tree *buffer; | |
10749 | ||
10750 | if (n > 0) | |
10751 | { | |
10752 | int i, j; | |
10753 | va_list ap; | |
7e15618b | 10754 | |
c2f47e15 | 10755 | buffer = alloca (nargs * sizeof (tree)); |
10756 | va_start (ap, n); | |
10757 | for (i = 0; i < n; i++) | |
10758 | buffer[i] = va_arg (ap, tree); | |
10759 | va_end (ap); | |
10760 | for (j = skip; j < oldnargs; j++, i++) | |
10761 | buffer[i] = CALL_EXPR_ARG (exp, j); | |
10762 | } | |
10763 | else | |
10764 | buffer = CALL_EXPR_ARGP (exp) + skip; | |
10765 | ||
10766 | return fold (build_call_array (TREE_TYPE (exp), fn, nargs, buffer)); | |
10767 | } | |
10768 | ||
10769 | /* Validate a single argument ARG against a tree code CODE representing | |
10770 | a type. */ | |
10771 | ||
10772 | static bool | |
b7bf20db | 10773 | validate_arg (const_tree arg, enum tree_code code) |
c2f47e15 | 10774 | { |
10775 | if (!arg) | |
10776 | return false; | |
10777 | else if (code == POINTER_TYPE) | |
10778 | return POINTER_TYPE_P (TREE_TYPE (arg)); | |
c7f617c2 | 10779 | else if (code == INTEGER_TYPE) |
10780 | return INTEGRAL_TYPE_P (TREE_TYPE (arg)); | |
c2f47e15 | 10781 | return code == TREE_CODE (TREE_TYPE (arg)); |
7e15618b | 10782 | } |
0eb671f7 | 10783 | |
10784 | /* This function validates the types of a function call argument list | |
c2f47e15 | 10785 | against a specified list of tree_codes. If the last specifier is a 0, |
10786 | that represents an ellipses, otherwise the last specifier must be a | |
10787 | VOID_TYPE. */ | |
27d0c333 | 10788 | |
c2f47e15 | 10789 | bool |
b7bf20db | 10790 | validate_arglist (const_tree callexpr, ...) |
0eb671f7 | 10791 | { |
0eb671f7 | 10792 | enum tree_code code; |
c2f47e15 | 10793 | bool res = 0; |
ee582a61 | 10794 | va_list ap; |
b7bf20db | 10795 | const_call_expr_arg_iterator iter; |
10796 | const_tree arg; | |
aecda0d6 | 10797 | |
c2f47e15 | 10798 | va_start (ap, callexpr); |
b7bf20db | 10799 | init_const_call_expr_arg_iterator (callexpr, &iter); |
0eb671f7 | 10800 | |
f0ce3b1f | 10801 | do |
0eb671f7 | 10802 | { |
f0ce3b1f | 10803 | code = va_arg (ap, enum tree_code); |
10804 | switch (code) | |
10805 | { | |
10806 | case 0: | |
10807 | /* This signifies an ellipses, any further arguments are all ok. */ | |
c2f47e15 | 10808 | res = true; |
f0ce3b1f | 10809 | goto end; |
10810 | case VOID_TYPE: | |
10811 | /* This signifies an endlink, if no arguments remain, return | |
10812 | true, otherwise return false. */ | |
b7bf20db | 10813 | res = !more_const_call_expr_args_p (&iter); |
f0ce3b1f | 10814 | goto end; |
10815 | default: | |
10816 | /* If no parameters remain or the parameter's code does not | |
10817 | match the specified code, return false. Otherwise continue | |
10818 | checking any remaining arguments. */ | |
b7bf20db | 10819 | arg = next_const_call_expr_arg (&iter); |
c2f47e15 | 10820 | if (!validate_arg (arg, code)) |
f0ce3b1f | 10821 | goto end; |
10822 | break; | |
10823 | } | |
0eb671f7 | 10824 | } |
f0ce3b1f | 10825 | while (1); |
0903457a | 10826 | |
10827 | /* We need gotos here since we can only have one VA_CLOSE in a | |
10828 | function. */ | |
10829 | end: ; | |
ee582a61 | 10830 | va_end (ap); |
0903457a | 10831 | |
10832 | return res; | |
0eb671f7 | 10833 | } |
fc2a2dcb | 10834 | |
fc2a2dcb | 10835 | /* Default target-specific builtin expander that does nothing. */ |
10836 | ||
10837 | rtx | |
aecda0d6 | 10838 | default_expand_builtin (tree exp ATTRIBUTE_UNUSED, |
10839 | rtx target ATTRIBUTE_UNUSED, | |
10840 | rtx subtarget ATTRIBUTE_UNUSED, | |
10841 | enum machine_mode mode ATTRIBUTE_UNUSED, | |
10842 | int ignore ATTRIBUTE_UNUSED) | |
fc2a2dcb | 10843 | { |
10844 | return NULL_RTX; | |
10845 | } | |
c7926a82 | 10846 | |
01537105 | 10847 | /* Returns true is EXP represents data that would potentially reside |
10848 | in a readonly section. */ | |
10849 | ||
10850 | static bool | |
10851 | readonly_data_expr (tree exp) | |
10852 | { | |
10853 | STRIP_NOPS (exp); | |
10854 | ||
9ff0637e | 10855 | if (TREE_CODE (exp) != ADDR_EXPR) |
10856 | return false; | |
10857 | ||
10858 | exp = get_base_address (TREE_OPERAND (exp, 0)); | |
10859 | if (!exp) | |
10860 | return false; | |
10861 | ||
10862 | /* Make sure we call decl_readonly_section only for trees it | |
10863 | can handle (since it returns true for everything it doesn't | |
10864 | understand). */ | |
491e04ef | 10865 | if (TREE_CODE (exp) == STRING_CST |
9ff0637e | 10866 | || TREE_CODE (exp) == CONSTRUCTOR |
10867 | || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp))) | |
10868 | return decl_readonly_section (exp, 0); | |
01537105 | 10869 | else |
10870 | return false; | |
10871 | } | |
4ee9c684 | 10872 | |
c2f47e15 | 10873 | /* Simplify a call to the strstr builtin. S1 and S2 are the arguments |
10874 | to the call, and TYPE is its return type. | |
4ee9c684 | 10875 | |
c2f47e15 | 10876 | Return NULL_TREE if no simplification was possible, otherwise return the |
4ee9c684 | 10877 | simplified form of the call as a tree. |
10878 | ||
10879 | The simplified form may be a constant or other expression which | |
10880 | computes the same value, but in a more efficient manner (including | |
10881 | calls to other builtin functions). | |
10882 | ||
10883 | The call may contain arguments which need to be evaluated, but | |
10884 | which are not useful to determine the result of the call. In | |
10885 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
10886 | COMPOUND_EXPR will be an argument which must be evaluated. | |
10887 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
10888 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
10889 | form of the builtin function call. */ | |
10890 | ||
10891 | static tree | |
c2f47e15 | 10892 | fold_builtin_strstr (tree s1, tree s2, tree type) |
4ee9c684 | 10893 | { |
c2f47e15 | 10894 | if (!validate_arg (s1, POINTER_TYPE) |
10895 | || !validate_arg (s2, POINTER_TYPE)) | |
10896 | return NULL_TREE; | |
4ee9c684 | 10897 | else |
10898 | { | |
4ee9c684 | 10899 | tree fn; |
10900 | const char *p1, *p2; | |
10901 | ||
10902 | p2 = c_getstr (s2); | |
10903 | if (p2 == NULL) | |
c2f47e15 | 10904 | return NULL_TREE; |
4ee9c684 | 10905 | |
10906 | p1 = c_getstr (s1); | |
10907 | if (p1 != NULL) | |
10908 | { | |
10909 | const char *r = strstr (p1, p2); | |
daa1d5f5 | 10910 | tree tem; |
4ee9c684 | 10911 | |
4ee9c684 | 10912 | if (r == NULL) |
779b4c41 | 10913 | return build_int_cst (TREE_TYPE (s1), 0); |
c0c67e38 | 10914 | |
10915 | /* Return an offset into the constant string argument. */ | |
0de36bdb | 10916 | tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1), |
10917 | s1, size_int (r - p1)); | |
daa1d5f5 | 10918 | return fold_convert (type, tem); |
4ee9c684 | 10919 | } |
10920 | ||
7efa231c | 10921 | /* The argument is const char *, and the result is char *, so we need |
10922 | a type conversion here to avoid a warning. */ | |
4ee9c684 | 10923 | if (p2[0] == '\0') |
7efa231c | 10924 | return fold_convert (type, s1); |
4ee9c684 | 10925 | |
10926 | if (p2[1] != '\0') | |
c2f47e15 | 10927 | return NULL_TREE; |
4ee9c684 | 10928 | |
10929 | fn = implicit_built_in_decls[BUILT_IN_STRCHR]; | |
10930 | if (!fn) | |
c2f47e15 | 10931 | return NULL_TREE; |
4ee9c684 | 10932 | |
10933 | /* New argument list transforming strstr(s1, s2) to | |
10934 | strchr(s1, s2[0]). */ | |
c2f47e15 | 10935 | return build_call_expr (fn, 2, s1, build_int_cst (NULL_TREE, p2[0])); |
4ee9c684 | 10936 | } |
10937 | } | |
10938 | ||
c2f47e15 | 10939 | /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to |
10940 | the call, and TYPE is its return type. | |
4ee9c684 | 10941 | |
c2f47e15 | 10942 | Return NULL_TREE if no simplification was possible, otherwise return the |
4ee9c684 | 10943 | simplified form of the call as a tree. |
10944 | ||
10945 | The simplified form may be a constant or other expression which | |
10946 | computes the same value, but in a more efficient manner (including | |
10947 | calls to other builtin functions). | |
10948 | ||
10949 | The call may contain arguments which need to be evaluated, but | |
10950 | which are not useful to determine the result of the call. In | |
10951 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
10952 | COMPOUND_EXPR will be an argument which must be evaluated. | |
10953 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
10954 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
10955 | form of the builtin function call. */ | |
10956 | ||
10957 | static tree | |
c2f47e15 | 10958 | fold_builtin_strchr (tree s1, tree s2, tree type) |
4ee9c684 | 10959 | { |
c2f47e15 | 10960 | if (!validate_arg (s1, POINTER_TYPE) |
10961 | || !validate_arg (s2, INTEGER_TYPE)) | |
10962 | return NULL_TREE; | |
4ee9c684 | 10963 | else |
10964 | { | |
4ee9c684 | 10965 | const char *p1; |
10966 | ||
10967 | if (TREE_CODE (s2) != INTEGER_CST) | |
c2f47e15 | 10968 | return NULL_TREE; |
4ee9c684 | 10969 | |
10970 | p1 = c_getstr (s1); | |
10971 | if (p1 != NULL) | |
10972 | { | |
10973 | char c; | |
10974 | const char *r; | |
daa1d5f5 | 10975 | tree tem; |
4ee9c684 | 10976 | |
10977 | if (target_char_cast (s2, &c)) | |
c2f47e15 | 10978 | return NULL_TREE; |
4ee9c684 | 10979 | |
10980 | r = strchr (p1, c); | |
10981 | ||
10982 | if (r == NULL) | |
779b4c41 | 10983 | return build_int_cst (TREE_TYPE (s1), 0); |
4ee9c684 | 10984 | |
10985 | /* Return an offset into the constant string argument. */ | |
0de36bdb | 10986 | tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1), |
10987 | s1, size_int (r - p1)); | |
daa1d5f5 | 10988 | return fold_convert (type, tem); |
4ee9c684 | 10989 | } |
c2f47e15 | 10990 | return NULL_TREE; |
4ee9c684 | 10991 | } |
10992 | } | |
10993 | ||
c2f47e15 | 10994 | /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to |
10995 | the call, and TYPE is its return type. | |
4ee9c684 | 10996 | |
c2f47e15 | 10997 | Return NULL_TREE if no simplification was possible, otherwise return the |
4ee9c684 | 10998 | simplified form of the call as a tree. |
10999 | ||
11000 | The simplified form may be a constant or other expression which | |
11001 | computes the same value, but in a more efficient manner (including | |
11002 | calls to other builtin functions). | |
11003 | ||
11004 | The call may contain arguments which need to be evaluated, but | |
11005 | which are not useful to determine the result of the call. In | |
11006 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
11007 | COMPOUND_EXPR will be an argument which must be evaluated. | |
11008 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
11009 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
11010 | form of the builtin function call. */ | |
11011 | ||
11012 | static tree | |
c2f47e15 | 11013 | fold_builtin_strrchr (tree s1, tree s2, tree type) |
4ee9c684 | 11014 | { |
c2f47e15 | 11015 | if (!validate_arg (s1, POINTER_TYPE) |
11016 | || !validate_arg (s2, INTEGER_TYPE)) | |
11017 | return NULL_TREE; | |
4ee9c684 | 11018 | else |
11019 | { | |
4ee9c684 | 11020 | tree fn; |
11021 | const char *p1; | |
11022 | ||
11023 | if (TREE_CODE (s2) != INTEGER_CST) | |
c2f47e15 | 11024 | return NULL_TREE; |
4ee9c684 | 11025 | |
11026 | p1 = c_getstr (s1); | |
11027 | if (p1 != NULL) | |
11028 | { | |
11029 | char c; | |
11030 | const char *r; | |
daa1d5f5 | 11031 | tree tem; |
4ee9c684 | 11032 | |
11033 | if (target_char_cast (s2, &c)) | |
c2f47e15 | 11034 | return NULL_TREE; |
4ee9c684 | 11035 | |
11036 | r = strrchr (p1, c); | |
11037 | ||
11038 | if (r == NULL) | |
779b4c41 | 11039 | return build_int_cst (TREE_TYPE (s1), 0); |
4ee9c684 | 11040 | |
11041 | /* Return an offset into the constant string argument. */ | |
0de36bdb | 11042 | tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1), |
11043 | s1, size_int (r - p1)); | |
daa1d5f5 | 11044 | return fold_convert (type, tem); |
4ee9c684 | 11045 | } |
11046 | ||
11047 | if (! integer_zerop (s2)) | |
c2f47e15 | 11048 | return NULL_TREE; |
4ee9c684 | 11049 | |
11050 | fn = implicit_built_in_decls[BUILT_IN_STRCHR]; | |
11051 | if (!fn) | |
c2f47e15 | 11052 | return NULL_TREE; |
4ee9c684 | 11053 | |
11054 | /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */ | |
c2f47e15 | 11055 | return build_call_expr (fn, 2, s1, s2); |
4ee9c684 | 11056 | } |
11057 | } | |
11058 | ||
c2f47e15 | 11059 | /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments |
11060 | to the call, and TYPE is its return type. | |
4ee9c684 | 11061 | |
c2f47e15 | 11062 | Return NULL_TREE if no simplification was possible, otherwise return the |
4ee9c684 | 11063 | simplified form of the call as a tree. |
11064 | ||
11065 | The simplified form may be a constant or other expression which | |
11066 | computes the same value, but in a more efficient manner (including | |
11067 | calls to other builtin functions). | |
11068 | ||
11069 | The call may contain arguments which need to be evaluated, but | |
11070 | which are not useful to determine the result of the call. In | |
11071 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
11072 | COMPOUND_EXPR will be an argument which must be evaluated. | |
11073 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
11074 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
11075 | form of the builtin function call. */ | |
11076 | ||
11077 | static tree | |
c2f47e15 | 11078 | fold_builtin_strpbrk (tree s1, tree s2, tree type) |
4ee9c684 | 11079 | { |
c2f47e15 | 11080 | if (!validate_arg (s1, POINTER_TYPE) |
11081 | || !validate_arg (s2, POINTER_TYPE)) | |
11082 | return NULL_TREE; | |
4ee9c684 | 11083 | else |
11084 | { | |
4ee9c684 | 11085 | tree fn; |
11086 | const char *p1, *p2; | |
11087 | ||
11088 | p2 = c_getstr (s2); | |
11089 | if (p2 == NULL) | |
c2f47e15 | 11090 | return NULL_TREE; |
4ee9c684 | 11091 | |
11092 | p1 = c_getstr (s1); | |
11093 | if (p1 != NULL) | |
11094 | { | |
11095 | const char *r = strpbrk (p1, p2); | |
daa1d5f5 | 11096 | tree tem; |
4ee9c684 | 11097 | |
11098 | if (r == NULL) | |
779b4c41 | 11099 | return build_int_cst (TREE_TYPE (s1), 0); |
4ee9c684 | 11100 | |
11101 | /* Return an offset into the constant string argument. */ | |
0de36bdb | 11102 | tem = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (s1), |
11103 | s1, size_int (r - p1)); | |
daa1d5f5 | 11104 | return fold_convert (type, tem); |
4ee9c684 | 11105 | } |
11106 | ||
11107 | if (p2[0] == '\0') | |
05abc81b | 11108 | /* strpbrk(x, "") == NULL. |
11109 | Evaluate and ignore s1 in case it had side-effects. */ | |
11110 | return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1); | |
4ee9c684 | 11111 | |
11112 | if (p2[1] != '\0') | |
c2f47e15 | 11113 | return NULL_TREE; /* Really call strpbrk. */ |
4ee9c684 | 11114 | |
11115 | fn = implicit_built_in_decls[BUILT_IN_STRCHR]; | |
11116 | if (!fn) | |
c2f47e15 | 11117 | return NULL_TREE; |
4ee9c684 | 11118 | |
11119 | /* New argument list transforming strpbrk(s1, s2) to | |
11120 | strchr(s1, s2[0]). */ | |
c2f47e15 | 11121 | return build_call_expr (fn, 2, s1, build_int_cst (NULL_TREE, p2[0])); |
4ee9c684 | 11122 | } |
11123 | } | |
11124 | ||
c2f47e15 | 11125 | /* Simplify a call to the strcat builtin. DST and SRC are the arguments |
11126 | to the call. | |
4ee9c684 | 11127 | |
c2f47e15 | 11128 | Return NULL_TREE if no simplification was possible, otherwise return the |
4ee9c684 | 11129 | simplified form of the call as a tree. |
11130 | ||
11131 | The simplified form may be a constant or other expression which | |
11132 | computes the same value, but in a more efficient manner (including | |
11133 | calls to other builtin functions). | |
11134 | ||
11135 | The call may contain arguments which need to be evaluated, but | |
11136 | which are not useful to determine the result of the call. In | |
11137 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
11138 | COMPOUND_EXPR will be an argument which must be evaluated. | |
11139 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
11140 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
11141 | form of the builtin function call. */ | |
11142 | ||
11143 | static tree | |
c2f47e15 | 11144 | fold_builtin_strcat (tree dst, tree src) |
4ee9c684 | 11145 | { |
c2f47e15 | 11146 | if (!validate_arg (dst, POINTER_TYPE) |
11147 | || !validate_arg (src, POINTER_TYPE)) | |
11148 | return NULL_TREE; | |
4ee9c684 | 11149 | else |
11150 | { | |
4ee9c684 | 11151 | const char *p = c_getstr (src); |
11152 | ||
11153 | /* If the string length is zero, return the dst parameter. */ | |
11154 | if (p && *p == '\0') | |
11155 | return dst; | |
11156 | ||
c2f47e15 | 11157 | return NULL_TREE; |
4ee9c684 | 11158 | } |
11159 | } | |
11160 | ||
c2f47e15 | 11161 | /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the |
11162 | arguments to the call. | |
4ee9c684 | 11163 | |
c2f47e15 | 11164 | Return NULL_TREE if no simplification was possible, otherwise return the |
4ee9c684 | 11165 | simplified form of the call as a tree. |
11166 | ||
11167 | The simplified form may be a constant or other expression which | |
11168 | computes the same value, but in a more efficient manner (including | |
11169 | calls to other builtin functions). | |
11170 | ||
11171 | The call may contain arguments which need to be evaluated, but | |
11172 | which are not useful to determine the result of the call. In | |
11173 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
11174 | COMPOUND_EXPR will be an argument which must be evaluated. | |
11175 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
11176 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
11177 | form of the builtin function call. */ | |
11178 | ||
11179 | static tree | |
c2f47e15 | 11180 | fold_builtin_strncat (tree dst, tree src, tree len) |
4ee9c684 | 11181 | { |
c2f47e15 | 11182 | if (!validate_arg (dst, POINTER_TYPE) |
11183 | || !validate_arg (src, POINTER_TYPE) | |
11184 | || !validate_arg (len, INTEGER_TYPE)) | |
11185 | return NULL_TREE; | |
4ee9c684 | 11186 | else |
11187 | { | |
4ee9c684 | 11188 | const char *p = c_getstr (src); |
11189 | ||
11190 | /* If the requested length is zero, or the src parameter string | |
0a39fd54 | 11191 | length is zero, return the dst parameter. */ |
4ee9c684 | 11192 | if (integer_zerop (len) || (p && *p == '\0')) |
a0c938f0 | 11193 | return omit_two_operands (TREE_TYPE (dst), dst, src, len); |
4ee9c684 | 11194 | |
11195 | /* If the requested len is greater than or equal to the string | |
a0c938f0 | 11196 | length, call strcat. */ |
4ee9c684 | 11197 | if (TREE_CODE (len) == INTEGER_CST && p |
11198 | && compare_tree_int (len, strlen (p)) >= 0) | |
11199 | { | |
4ee9c684 | 11200 | tree fn = implicit_built_in_decls[BUILT_IN_STRCAT]; |
11201 | ||
11202 | /* If the replacement _DECL isn't initialized, don't do the | |
11203 | transformation. */ | |
11204 | if (!fn) | |
c2f47e15 | 11205 | return NULL_TREE; |
4ee9c684 | 11206 | |
c2f47e15 | 11207 | return build_call_expr (fn, 2, dst, src); |
4ee9c684 | 11208 | } |
c2f47e15 | 11209 | return NULL_TREE; |
4ee9c684 | 11210 | } |
11211 | } | |
11212 | ||
c2f47e15 | 11213 | /* Simplify a call to the strspn builtin. S1 and S2 are the arguments |
11214 | to the call. | |
4ee9c684 | 11215 | |
c2f47e15 | 11216 | Return NULL_TREE if no simplification was possible, otherwise return the |
4ee9c684 | 11217 | simplified form of the call as a tree. |
11218 | ||
11219 | The simplified form may be a constant or other expression which | |
11220 | computes the same value, but in a more efficient manner (including | |
11221 | calls to other builtin functions). | |
11222 | ||
11223 | The call may contain arguments which need to be evaluated, but | |
11224 | which are not useful to determine the result of the call. In | |
11225 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
11226 | COMPOUND_EXPR will be an argument which must be evaluated. | |
11227 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
11228 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
11229 | form of the builtin function call. */ | |
11230 | ||
11231 | static tree | |
c2f47e15 | 11232 | fold_builtin_strspn (tree s1, tree s2) |
4ee9c684 | 11233 | { |
c2f47e15 | 11234 | if (!validate_arg (s1, POINTER_TYPE) |
11235 | || !validate_arg (s2, POINTER_TYPE)) | |
11236 | return NULL_TREE; | |
4ee9c684 | 11237 | else |
11238 | { | |
4ee9c684 | 11239 | const char *p1 = c_getstr (s1), *p2 = c_getstr (s2); |
11240 | ||
11241 | /* If both arguments are constants, evaluate at compile-time. */ | |
11242 | if (p1 && p2) | |
11243 | { | |
11244 | const size_t r = strspn (p1, p2); | |
11245 | return size_int (r); | |
11246 | } | |
11247 | ||
c2f47e15 | 11248 | /* If either argument is "", return NULL_TREE. */ |
4ee9c684 | 11249 | if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0')) |
9bc9f15f | 11250 | /* Evaluate and ignore both arguments in case either one has |
11251 | side-effects. */ | |
11252 | return omit_two_operands (integer_type_node, integer_zero_node, | |
11253 | s1, s2); | |
c2f47e15 | 11254 | return NULL_TREE; |
4ee9c684 | 11255 | } |
11256 | } | |
11257 | ||
c2f47e15 | 11258 | /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments |
11259 | to the call. | |
4ee9c684 | 11260 | |
c2f47e15 | 11261 | Return NULL_TREE if no simplification was possible, otherwise return the |
4ee9c684 | 11262 | simplified form of the call as a tree. |
11263 | ||
11264 | The simplified form may be a constant or other expression which | |
11265 | computes the same value, but in a more efficient manner (including | |
11266 | calls to other builtin functions). | |
11267 | ||
11268 | The call may contain arguments which need to be evaluated, but | |
11269 | which are not useful to determine the result of the call. In | |
11270 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
11271 | COMPOUND_EXPR will be an argument which must be evaluated. | |
11272 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
11273 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
11274 | form of the builtin function call. */ | |
11275 | ||
11276 | static tree | |
c2f47e15 | 11277 | fold_builtin_strcspn (tree s1, tree s2) |
4ee9c684 | 11278 | { |
c2f47e15 | 11279 | if (!validate_arg (s1, POINTER_TYPE) |
11280 | || !validate_arg (s2, POINTER_TYPE)) | |
11281 | return NULL_TREE; | |
4ee9c684 | 11282 | else |
11283 | { | |
4ee9c684 | 11284 | const char *p1 = c_getstr (s1), *p2 = c_getstr (s2); |
11285 | ||
11286 | /* If both arguments are constants, evaluate at compile-time. */ | |
11287 | if (p1 && p2) | |
11288 | { | |
11289 | const size_t r = strcspn (p1, p2); | |
11290 | return size_int (r); | |
11291 | } | |
11292 | ||
c2f47e15 | 11293 | /* If the first argument is "", return NULL_TREE. */ |
4ee9c684 | 11294 | if (p1 && *p1 == '\0') |
11295 | { | |
11296 | /* Evaluate and ignore argument s2 in case it has | |
11297 | side-effects. */ | |
05abc81b | 11298 | return omit_one_operand (integer_type_node, |
11299 | integer_zero_node, s2); | |
4ee9c684 | 11300 | } |
11301 | ||
11302 | /* If the second argument is "", return __builtin_strlen(s1). */ | |
11303 | if (p2 && *p2 == '\0') | |
11304 | { | |
c2f47e15 | 11305 | tree fn = implicit_built_in_decls[BUILT_IN_STRLEN]; |
4ee9c684 | 11306 | |
11307 | /* If the replacement _DECL isn't initialized, don't do the | |
11308 | transformation. */ | |
11309 | if (!fn) | |
c2f47e15 | 11310 | return NULL_TREE; |
4ee9c684 | 11311 | |
c2f47e15 | 11312 | return build_call_expr (fn, 1, s1); |
4ee9c684 | 11313 | } |
c2f47e15 | 11314 | return NULL_TREE; |
4ee9c684 | 11315 | } |
11316 | } | |
11317 | ||
c2f47e15 | 11318 | /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments |
11319 | to the call. IGNORE is true if the value returned | |
f0613857 | 11320 | by the builtin will be ignored. UNLOCKED is true is true if this |
11321 | actually a call to fputs_unlocked. If LEN in non-NULL, it represents | |
11322 | the known length of the string. Return NULL_TREE if no simplification | |
11323 | was possible. */ | |
4ee9c684 | 11324 | |
11325 | tree | |
c2f47e15 | 11326 | fold_builtin_fputs (tree arg0, tree arg1, bool ignore, bool unlocked, tree len) |
4ee9c684 | 11327 | { |
6d77ed92 | 11328 | /* If we're using an unlocked function, assume the other unlocked |
11329 | functions exist explicitly. */ | |
11330 | tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED] | |
4ee9c684 | 11331 | : implicit_built_in_decls[BUILT_IN_FPUTC]; |
6d77ed92 | 11332 | tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED] |
4ee9c684 | 11333 | : implicit_built_in_decls[BUILT_IN_FWRITE]; |
11334 | ||
c403edd4 | 11335 | /* If the return value is used, don't do the transformation. */ |
11336 | if (!ignore) | |
c2f47e15 | 11337 | return NULL_TREE; |
4ee9c684 | 11338 | |
11339 | /* Verify the arguments in the original call. */ | |
c2f47e15 | 11340 | if (!validate_arg (arg0, POINTER_TYPE) |
11341 | || !validate_arg (arg1, POINTER_TYPE)) | |
11342 | return NULL_TREE; | |
4ee9c684 | 11343 | |
f0613857 | 11344 | if (! len) |
c2f47e15 | 11345 | len = c_strlen (arg0, 0); |
4ee9c684 | 11346 | |
11347 | /* Get the length of the string passed to fputs. If the length | |
11348 | can't be determined, punt. */ | |
11349 | if (!len | |
11350 | || TREE_CODE (len) != INTEGER_CST) | |
c2f47e15 | 11351 | return NULL_TREE; |
4ee9c684 | 11352 | |
11353 | switch (compare_tree_int (len, 1)) | |
11354 | { | |
11355 | case -1: /* length is 0, delete the call entirely . */ | |
c2f47e15 | 11356 | return omit_one_operand (integer_type_node, integer_zero_node, arg1);; |
05abc81b | 11357 | |
4ee9c684 | 11358 | case 0: /* length is 1, call fputc. */ |
11359 | { | |
c2f47e15 | 11360 | const char *p = c_getstr (arg0); |
4ee9c684 | 11361 | |
11362 | if (p != NULL) | |
11363 | { | |
c2f47e15 | 11364 | if (fn_fputc) |
11365 | return build_call_expr (fn_fputc, 2, | |
11366 | build_int_cst (NULL_TREE, p[0]), arg1); | |
11367 | else | |
11368 | return NULL_TREE; | |
4ee9c684 | 11369 | } |
11370 | } | |
11371 | /* FALLTHROUGH */ | |
11372 | case 1: /* length is greater than 1, call fwrite. */ | |
11373 | { | |
0bed3869 | 11374 | /* If optimizing for size keep fputs. */ |
4ee9c684 | 11375 | if (optimize_size) |
c2f47e15 | 11376 | return NULL_TREE; |
4ee9c684 | 11377 | /* New argument list transforming fputs(string, stream) to |
11378 | fwrite(string, 1, len, stream). */ | |
c2f47e15 | 11379 | if (fn_fwrite) |
11380 | return build_call_expr (fn_fwrite, 4, arg0, size_one_node, len, arg1); | |
11381 | else | |
11382 | return NULL_TREE; | |
4ee9c684 | 11383 | } |
11384 | default: | |
64db345d | 11385 | gcc_unreachable (); |
4ee9c684 | 11386 | } |
c2f47e15 | 11387 | return NULL_TREE; |
4ee9c684 | 11388 | } |
11389 | ||
c2f47e15 | 11390 | /* Fold the next_arg or va_start call EXP. Returns true if there was an error |
743b0c6a | 11391 | produced. False otherwise. This is done so that we don't output the error |
11392 | or warning twice or three times. */ | |
11393 | bool | |
c2f47e15 | 11394 | fold_builtin_next_arg (tree exp, bool va_start_p) |
4ee9c684 | 11395 | { |
11396 | tree fntype = TREE_TYPE (current_function_decl); | |
c2f47e15 | 11397 | int nargs = call_expr_nargs (exp); |
11398 | tree arg; | |
4ee9c684 | 11399 | |
11400 | if (TYPE_ARG_TYPES (fntype) == 0 | |
11401 | || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype))) | |
11402 | == void_type_node)) | |
743b0c6a | 11403 | { |
11404 | error ("%<va_start%> used in function with fixed args"); | |
11405 | return true; | |
11406 | } | |
c2f47e15 | 11407 | |
11408 | if (va_start_p) | |
79012a9d | 11409 | { |
c2f47e15 | 11410 | if (va_start_p && (nargs != 2)) |
11411 | { | |
11412 | error ("wrong number of arguments to function %<va_start%>"); | |
11413 | return true; | |
11414 | } | |
11415 | arg = CALL_EXPR_ARG (exp, 1); | |
79012a9d | 11416 | } |
11417 | /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0) | |
11418 | when we checked the arguments and if needed issued a warning. */ | |
c2f47e15 | 11419 | else |
4ee9c684 | 11420 | { |
c2f47e15 | 11421 | if (nargs == 0) |
11422 | { | |
11423 | /* Evidently an out of date version of <stdarg.h>; can't validate | |
11424 | va_start's second argument, but can still work as intended. */ | |
11425 | warning (0, "%<__builtin_next_arg%> called without an argument"); | |
11426 | return true; | |
11427 | } | |
11428 | else if (nargs > 1) | |
a0c938f0 | 11429 | { |
c2f47e15 | 11430 | error ("wrong number of arguments to function %<__builtin_next_arg%>"); |
a0c938f0 | 11431 | return true; |
11432 | } | |
c2f47e15 | 11433 | arg = CALL_EXPR_ARG (exp, 0); |
11434 | } | |
11435 | ||
11436 | /* We destructively modify the call to be __builtin_va_start (ap, 0) | |
11437 | or __builtin_next_arg (0) the first time we see it, after checking | |
11438 | the arguments and if needed issuing a warning. */ | |
11439 | if (!integer_zerop (arg)) | |
11440 | { | |
11441 | tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl)); | |
79012a9d | 11442 | |
4ee9c684 | 11443 | /* Strip off all nops for the sake of the comparison. This |
11444 | is not quite the same as STRIP_NOPS. It does more. | |
11445 | We must also strip off INDIRECT_EXPR for C++ reference | |
11446 | parameters. */ | |
72dd6141 | 11447 | while (CONVERT_EXPR_P (arg) |
4ee9c684 | 11448 | || TREE_CODE (arg) == INDIRECT_REF) |
11449 | arg = TREE_OPERAND (arg, 0); | |
11450 | if (arg != last_parm) | |
a0c938f0 | 11451 | { |
b08cf617 | 11452 | /* FIXME: Sometimes with the tree optimizers we can get the |
11453 | not the last argument even though the user used the last | |
11454 | argument. We just warn and set the arg to be the last | |
11455 | argument so that we will get wrong-code because of | |
11456 | it. */ | |
c3ceba8e | 11457 | warning (0, "second parameter of %<va_start%> not last named argument"); |
743b0c6a | 11458 | } |
79012a9d | 11459 | /* We want to verify the second parameter just once before the tree |
a0c938f0 | 11460 | optimizers are run and then avoid keeping it in the tree, |
11461 | as otherwise we could warn even for correct code like: | |
11462 | void foo (int i, ...) | |
11463 | { va_list ap; i++; va_start (ap, i); va_end (ap); } */ | |
c2f47e15 | 11464 | if (va_start_p) |
11465 | CALL_EXPR_ARG (exp, 1) = integer_zero_node; | |
11466 | else | |
11467 | CALL_EXPR_ARG (exp, 0) = integer_zero_node; | |
743b0c6a | 11468 | } |
11469 | return false; | |
4ee9c684 | 11470 | } |
11471 | ||
11472 | ||
c2f47e15 | 11473 | /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG. |
11474 | ORIG may be null if this is a 2-argument call. We don't attempt to | |
11475 | simplify calls with more than 3 arguments. | |
4ee9c684 | 11476 | |
c2f47e15 | 11477 | Return NULL_TREE if no simplification was possible, otherwise return the |
4ee9c684 | 11478 | simplified form of the call as a tree. If IGNORED is true, it means that |
11479 | the caller does not use the returned value of the function. */ | |
11480 | ||
11481 | static tree | |
c2f47e15 | 11482 | fold_builtin_sprintf (tree dest, tree fmt, tree orig, int ignored) |
4ee9c684 | 11483 | { |
c2f47e15 | 11484 | tree call, retval; |
4ee9c684 | 11485 | const char *fmt_str = NULL; |
11486 | ||
11487 | /* Verify the required arguments in the original call. We deal with two | |
11488 | types of sprintf() calls: 'sprintf (str, fmt)' and | |
11489 | 'sprintf (dest, "%s", orig)'. */ | |
c2f47e15 | 11490 | if (!validate_arg (dest, POINTER_TYPE) |
11491 | || !validate_arg (fmt, POINTER_TYPE)) | |
11492 | return NULL_TREE; | |
11493 | if (orig && !validate_arg (orig, POINTER_TYPE)) | |
4ee9c684 | 11494 | return NULL_TREE; |
4ee9c684 | 11495 | |
11496 | /* Check whether the format is a literal string constant. */ | |
11497 | fmt_str = c_getstr (fmt); | |
11498 | if (fmt_str == NULL) | |
11499 | return NULL_TREE; | |
11500 | ||
11501 | call = NULL_TREE; | |
11502 | retval = NULL_TREE; | |
11503 | ||
d4473c84 | 11504 | if (!init_target_chars ()) |
c2f47e15 | 11505 | return NULL_TREE; |
99eabcc1 | 11506 | |
4ee9c684 | 11507 | /* If the format doesn't contain % args or %%, use strcpy. */ |
99eabcc1 | 11508 | if (strchr (fmt_str, target_percent) == NULL) |
4ee9c684 | 11509 | { |
11510 | tree fn = implicit_built_in_decls[BUILT_IN_STRCPY]; | |
11511 | ||
11512 | if (!fn) | |
11513 | return NULL_TREE; | |
11514 | ||
72dfb3f2 | 11515 | /* Don't optimize sprintf (buf, "abc", ptr++). */ |
c2f47e15 | 11516 | if (orig) |
72dfb3f2 | 11517 | return NULL_TREE; |
11518 | ||
4ee9c684 | 11519 | /* Convert sprintf (str, fmt) into strcpy (str, fmt) when |
11520 | 'format' is known to contain no % formats. */ | |
c2f47e15 | 11521 | call = build_call_expr (fn, 2, dest, fmt); |
4ee9c684 | 11522 | if (!ignored) |
7016c612 | 11523 | retval = build_int_cst (NULL_TREE, strlen (fmt_str)); |
4ee9c684 | 11524 | } |
11525 | ||
11526 | /* If the format is "%s", use strcpy if the result isn't used. */ | |
99eabcc1 | 11527 | else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0) |
4ee9c684 | 11528 | { |
c2f47e15 | 11529 | tree fn; |
4ee9c684 | 11530 | fn = implicit_built_in_decls[BUILT_IN_STRCPY]; |
11531 | ||
11532 | if (!fn) | |
11533 | return NULL_TREE; | |
11534 | ||
72dfb3f2 | 11535 | /* Don't crash on sprintf (str1, "%s"). */ |
c2f47e15 | 11536 | if (!orig) |
72dfb3f2 | 11537 | return NULL_TREE; |
11538 | ||
4ee9c684 | 11539 | /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */ |
4ee9c684 | 11540 | if (!ignored) |
11541 | { | |
11542 | retval = c_strlen (orig, 1); | |
11543 | if (!retval || TREE_CODE (retval) != INTEGER_CST) | |
11544 | return NULL_TREE; | |
11545 | } | |
c2f47e15 | 11546 | call = build_call_expr (fn, 2, dest, orig); |
4ee9c684 | 11547 | } |
11548 | ||
11549 | if (call && retval) | |
11550 | { | |
e3b560a6 | 11551 | retval = fold_convert |
4ee9c684 | 11552 | (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])), |
11553 | retval); | |
05abc81b | 11554 | return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval); |
4ee9c684 | 11555 | } |
11556 | else | |
11557 | return call; | |
11558 | } | |
0a39fd54 | 11559 | |
c2f47e15 | 11560 | /* Expand a call EXP to __builtin_object_size. */ |
0a39fd54 | 11561 | |
11562 | rtx | |
11563 | expand_builtin_object_size (tree exp) | |
11564 | { | |
11565 | tree ost; | |
11566 | int object_size_type; | |
11567 | tree fndecl = get_callee_fndecl (exp); | |
0a39fd54 | 11568 | |
c2f47e15 | 11569 | if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
0a39fd54 | 11570 | { |
b8c23db3 | 11571 | error ("%Kfirst argument of %D must be a pointer, second integer constant", |
11572 | exp, fndecl); | |
0a39fd54 | 11573 | expand_builtin_trap (); |
11574 | return const0_rtx; | |
11575 | } | |
11576 | ||
c2f47e15 | 11577 | ost = CALL_EXPR_ARG (exp, 1); |
0a39fd54 | 11578 | STRIP_NOPS (ost); |
11579 | ||
11580 | if (TREE_CODE (ost) != INTEGER_CST | |
11581 | || tree_int_cst_sgn (ost) < 0 | |
11582 | || compare_tree_int (ost, 3) > 0) | |
11583 | { | |
b8c23db3 | 11584 | error ("%Klast argument of %D is not integer constant between 0 and 3", |
11585 | exp, fndecl); | |
0a39fd54 | 11586 | expand_builtin_trap (); |
11587 | return const0_rtx; | |
11588 | } | |
11589 | ||
11590 | object_size_type = tree_low_cst (ost, 0); | |
11591 | ||
11592 | return object_size_type < 2 ? constm1_rtx : const0_rtx; | |
11593 | } | |
11594 | ||
11595 | /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin. | |
11596 | FCODE is the BUILT_IN_* to use. | |
c2f47e15 | 11597 | Return NULL_RTX if we failed; the caller should emit a normal call, |
0a39fd54 | 11598 | otherwise try to get the result in TARGET, if convenient (and in |
11599 | mode MODE if that's convenient). */ | |
11600 | ||
11601 | static rtx | |
11602 | expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode, | |
11603 | enum built_in_function fcode) | |
11604 | { | |
0a39fd54 | 11605 | tree dest, src, len, size; |
11606 | ||
c2f47e15 | 11607 | if (!validate_arglist (exp, |
0a39fd54 | 11608 | POINTER_TYPE, |
11609 | fcode == BUILT_IN_MEMSET_CHK | |
11610 | ? INTEGER_TYPE : POINTER_TYPE, | |
11611 | INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
c2f47e15 | 11612 | return NULL_RTX; |
0a39fd54 | 11613 | |
c2f47e15 | 11614 | dest = CALL_EXPR_ARG (exp, 0); |
11615 | src = CALL_EXPR_ARG (exp, 1); | |
11616 | len = CALL_EXPR_ARG (exp, 2); | |
11617 | size = CALL_EXPR_ARG (exp, 3); | |
0a39fd54 | 11618 | |
11619 | if (! host_integerp (size, 1)) | |
c2f47e15 | 11620 | return NULL_RTX; |
0a39fd54 | 11621 | |
11622 | if (host_integerp (len, 1) || integer_all_onesp (size)) | |
11623 | { | |
11624 | tree fn; | |
11625 | ||
11626 | if (! integer_all_onesp (size) && tree_int_cst_lt (size, len)) | |
11627 | { | |
b8c23db3 | 11628 | warning (0, "%Kcall to %D will always overflow destination buffer", |
11629 | exp, get_callee_fndecl (exp)); | |
c2f47e15 | 11630 | return NULL_RTX; |
0a39fd54 | 11631 | } |
11632 | ||
0a39fd54 | 11633 | fn = NULL_TREE; |
11634 | /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume | |
11635 | mem{cpy,pcpy,move,set} is available. */ | |
11636 | switch (fcode) | |
11637 | { | |
11638 | case BUILT_IN_MEMCPY_CHK: | |
11639 | fn = built_in_decls[BUILT_IN_MEMCPY]; | |
11640 | break; | |
11641 | case BUILT_IN_MEMPCPY_CHK: | |
11642 | fn = built_in_decls[BUILT_IN_MEMPCPY]; | |
11643 | break; | |
11644 | case BUILT_IN_MEMMOVE_CHK: | |
11645 | fn = built_in_decls[BUILT_IN_MEMMOVE]; | |
11646 | break; | |
11647 | case BUILT_IN_MEMSET_CHK: | |
11648 | fn = built_in_decls[BUILT_IN_MEMSET]; | |
11649 | break; | |
11650 | default: | |
11651 | break; | |
11652 | } | |
11653 | ||
11654 | if (! fn) | |
c2f47e15 | 11655 | return NULL_RTX; |
0a39fd54 | 11656 | |
c2f47e15 | 11657 | fn = build_call_expr (fn, 3, dest, src, len); |
6d54e886 | 11658 | STRIP_TYPE_NOPS (fn); |
11659 | while (TREE_CODE (fn) == COMPOUND_EXPR) | |
11660 | { | |
11661 | expand_expr (TREE_OPERAND (fn, 0), const0_rtx, VOIDmode, | |
11662 | EXPAND_NORMAL); | |
11663 | fn = TREE_OPERAND (fn, 1); | |
11664 | } | |
0a39fd54 | 11665 | if (TREE_CODE (fn) == CALL_EXPR) |
11666 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
11667 | return expand_expr (fn, target, mode, EXPAND_NORMAL); | |
11668 | } | |
11669 | else if (fcode == BUILT_IN_MEMSET_CHK) | |
c2f47e15 | 11670 | return NULL_RTX; |
0a39fd54 | 11671 | else |
11672 | { | |
11673 | unsigned int dest_align | |
11674 | = get_pointer_alignment (dest, BIGGEST_ALIGNMENT); | |
11675 | ||
11676 | /* If DEST is not a pointer type, call the normal function. */ | |
11677 | if (dest_align == 0) | |
c2f47e15 | 11678 | return NULL_RTX; |
0a39fd54 | 11679 | |
11680 | /* If SRC and DEST are the same (and not volatile), do nothing. */ | |
11681 | if (operand_equal_p (src, dest, 0)) | |
11682 | { | |
11683 | tree expr; | |
11684 | ||
11685 | if (fcode != BUILT_IN_MEMPCPY_CHK) | |
11686 | { | |
11687 | /* Evaluate and ignore LEN in case it has side-effects. */ | |
11688 | expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
11689 | return expand_expr (dest, target, mode, EXPAND_NORMAL); | |
11690 | } | |
11691 | ||
0de36bdb | 11692 | expr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len); |
0a39fd54 | 11693 | return expand_expr (expr, target, mode, EXPAND_NORMAL); |
11694 | } | |
11695 | ||
11696 | /* __memmove_chk special case. */ | |
11697 | if (fcode == BUILT_IN_MEMMOVE_CHK) | |
11698 | { | |
11699 | unsigned int src_align | |
11700 | = get_pointer_alignment (src, BIGGEST_ALIGNMENT); | |
11701 | ||
11702 | if (src_align == 0) | |
c2f47e15 | 11703 | return NULL_RTX; |
0a39fd54 | 11704 | |
11705 | /* If src is categorized for a readonly section we can use | |
11706 | normal __memcpy_chk. */ | |
11707 | if (readonly_data_expr (src)) | |
11708 | { | |
11709 | tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK]; | |
11710 | if (!fn) | |
c2f47e15 | 11711 | return NULL_RTX; |
11712 | fn = build_call_expr (fn, 4, dest, src, len, size); | |
6d54e886 | 11713 | STRIP_TYPE_NOPS (fn); |
11714 | while (TREE_CODE (fn) == COMPOUND_EXPR) | |
11715 | { | |
11716 | expand_expr (TREE_OPERAND (fn, 0), const0_rtx, VOIDmode, | |
11717 | EXPAND_NORMAL); | |
11718 | fn = TREE_OPERAND (fn, 1); | |
11719 | } | |
0a39fd54 | 11720 | if (TREE_CODE (fn) == CALL_EXPR) |
11721 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
11722 | return expand_expr (fn, target, mode, EXPAND_NORMAL); | |
11723 | } | |
11724 | } | |
c2f47e15 | 11725 | return NULL_RTX; |
0a39fd54 | 11726 | } |
11727 | } | |
11728 | ||
11729 | /* Emit warning if a buffer overflow is detected at compile time. */ | |
11730 | ||
11731 | static void | |
11732 | maybe_emit_chk_warning (tree exp, enum built_in_function fcode) | |
11733 | { | |
c2f47e15 | 11734 | int is_strlen = 0; |
0a39fd54 | 11735 | tree len, size; |
0a39fd54 | 11736 | |
11737 | switch (fcode) | |
11738 | { | |
11739 | case BUILT_IN_STRCPY_CHK: | |
11740 | case BUILT_IN_STPCPY_CHK: | |
11741 | /* For __strcat_chk the warning will be emitted only if overflowing | |
11742 | by at least strlen (dest) + 1 bytes. */ | |
11743 | case BUILT_IN_STRCAT_CHK: | |
c2f47e15 | 11744 | len = CALL_EXPR_ARG (exp, 1); |
11745 | size = CALL_EXPR_ARG (exp, 2); | |
0a39fd54 | 11746 | is_strlen = 1; |
11747 | break; | |
b356dfef | 11748 | case BUILT_IN_STRNCAT_CHK: |
0a39fd54 | 11749 | case BUILT_IN_STRNCPY_CHK: |
c2f47e15 | 11750 | len = CALL_EXPR_ARG (exp, 2); |
11751 | size = CALL_EXPR_ARG (exp, 3); | |
0a39fd54 | 11752 | break; |
11753 | case BUILT_IN_SNPRINTF_CHK: | |
11754 | case BUILT_IN_VSNPRINTF_CHK: | |
c2f47e15 | 11755 | len = CALL_EXPR_ARG (exp, 1); |
11756 | size = CALL_EXPR_ARG (exp, 3); | |
0a39fd54 | 11757 | break; |
11758 | default: | |
11759 | gcc_unreachable (); | |
11760 | } | |
11761 | ||
0a39fd54 | 11762 | if (!len || !size) |
11763 | return; | |
11764 | ||
0a39fd54 | 11765 | if (! host_integerp (size, 1) || integer_all_onesp (size)) |
11766 | return; | |
11767 | ||
11768 | if (is_strlen) | |
11769 | { | |
11770 | len = c_strlen (len, 1); | |
11771 | if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size)) | |
11772 | return; | |
11773 | } | |
b356dfef | 11774 | else if (fcode == BUILT_IN_STRNCAT_CHK) |
11775 | { | |
c2f47e15 | 11776 | tree src = CALL_EXPR_ARG (exp, 1); |
b356dfef | 11777 | if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size)) |
11778 | return; | |
11779 | src = c_strlen (src, 1); | |
11780 | if (! src || ! host_integerp (src, 1)) | |
11781 | { | |
b8c23db3 | 11782 | warning (0, "%Kcall to %D might overflow destination buffer", |
11783 | exp, get_callee_fndecl (exp)); | |
b356dfef | 11784 | return; |
11785 | } | |
11786 | else if (tree_int_cst_lt (src, size)) | |
11787 | return; | |
11788 | } | |
0a39fd54 | 11789 | else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len)) |
11790 | return; | |
11791 | ||
b8c23db3 | 11792 | warning (0, "%Kcall to %D will always overflow destination buffer", |
11793 | exp, get_callee_fndecl (exp)); | |
0a39fd54 | 11794 | } |
11795 | ||
11796 | /* Emit warning if a buffer overflow is detected at compile time | |
11797 | in __sprintf_chk/__vsprintf_chk calls. */ | |
11798 | ||
11799 | static void | |
11800 | maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode) | |
11801 | { | |
0a39fd54 | 11802 | tree dest, size, len, fmt, flag; |
11803 | const char *fmt_str; | |
c2f47e15 | 11804 | int nargs = call_expr_nargs (exp); |
0a39fd54 | 11805 | |
11806 | /* Verify the required arguments in the original call. */ | |
c2f47e15 | 11807 | |
11808 | if (nargs < 4) | |
0a39fd54 | 11809 | return; |
c2f47e15 | 11810 | dest = CALL_EXPR_ARG (exp, 0); |
11811 | flag = CALL_EXPR_ARG (exp, 1); | |
11812 | size = CALL_EXPR_ARG (exp, 2); | |
11813 | fmt = CALL_EXPR_ARG (exp, 3); | |
0a39fd54 | 11814 | |
11815 | if (! host_integerp (size, 1) || integer_all_onesp (size)) | |
11816 | return; | |
11817 | ||
11818 | /* Check whether the format is a literal string constant. */ | |
11819 | fmt_str = c_getstr (fmt); | |
11820 | if (fmt_str == NULL) | |
11821 | return; | |
11822 | ||
d4473c84 | 11823 | if (!init_target_chars ()) |
99eabcc1 | 11824 | return; |
11825 | ||
0a39fd54 | 11826 | /* If the format doesn't contain % args or %%, we know its size. */ |
99eabcc1 | 11827 | if (strchr (fmt_str, target_percent) == 0) |
0a39fd54 | 11828 | len = build_int_cstu (size_type_node, strlen (fmt_str)); |
11829 | /* If the format is "%s" and first ... argument is a string literal, | |
11830 | we know it too. */ | |
c2f47e15 | 11831 | else if (fcode == BUILT_IN_SPRINTF_CHK |
11832 | && strcmp (fmt_str, target_percent_s) == 0) | |
0a39fd54 | 11833 | { |
11834 | tree arg; | |
11835 | ||
c2f47e15 | 11836 | if (nargs < 5) |
0a39fd54 | 11837 | return; |
c2f47e15 | 11838 | arg = CALL_EXPR_ARG (exp, 4); |
0a39fd54 | 11839 | if (! POINTER_TYPE_P (TREE_TYPE (arg))) |
11840 | return; | |
11841 | ||
11842 | len = c_strlen (arg, 1); | |
11843 | if (!len || ! host_integerp (len, 1)) | |
11844 | return; | |
11845 | } | |
11846 | else | |
11847 | return; | |
11848 | ||
11849 | if (! tree_int_cst_lt (len, size)) | |
11850 | { | |
b8c23db3 | 11851 | warning (0, "%Kcall to %D will always overflow destination buffer", |
11852 | exp, get_callee_fndecl (exp)); | |
0a39fd54 | 11853 | } |
11854 | } | |
11855 | ||
c2f47e15 | 11856 | /* Fold a call to __builtin_object_size with arguments PTR and OST, |
11857 | if possible. */ | |
0a39fd54 | 11858 | |
11859 | tree | |
c2f47e15 | 11860 | fold_builtin_object_size (tree ptr, tree ost) |
0a39fd54 | 11861 | { |
c2f47e15 | 11862 | tree ret = NULL_TREE; |
0a39fd54 | 11863 | int object_size_type; |
11864 | ||
c2f47e15 | 11865 | if (!validate_arg (ptr, POINTER_TYPE) |
11866 | || !validate_arg (ost, INTEGER_TYPE)) | |
11867 | return NULL_TREE; | |
0a39fd54 | 11868 | |
0a39fd54 | 11869 | STRIP_NOPS (ost); |
11870 | ||
11871 | if (TREE_CODE (ost) != INTEGER_CST | |
11872 | || tree_int_cst_sgn (ost) < 0 | |
11873 | || compare_tree_int (ost, 3) > 0) | |
c2f47e15 | 11874 | return NULL_TREE; |
0a39fd54 | 11875 | |
11876 | object_size_type = tree_low_cst (ost, 0); | |
11877 | ||
11878 | /* __builtin_object_size doesn't evaluate side-effects in its arguments; | |
11879 | if there are any side-effects, it returns (size_t) -1 for types 0 and 1 | |
11880 | and (size_t) 0 for types 2 and 3. */ | |
11881 | if (TREE_SIDE_EFFECTS (ptr)) | |
697bbc3f | 11882 | return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0); |
0a39fd54 | 11883 | |
11884 | if (TREE_CODE (ptr) == ADDR_EXPR) | |
11885 | ret = build_int_cstu (size_type_node, | |
697bbc3f | 11886 | compute_builtin_object_size (ptr, object_size_type)); |
0a39fd54 | 11887 | |
11888 | else if (TREE_CODE (ptr) == SSA_NAME) | |
11889 | { | |
11890 | unsigned HOST_WIDE_INT bytes; | |
11891 | ||
11892 | /* If object size is not known yet, delay folding until | |
11893 | later. Maybe subsequent passes will help determining | |
11894 | it. */ | |
11895 | bytes = compute_builtin_object_size (ptr, object_size_type); | |
11896 | if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 | |
11897 | ? -1 : 0)) | |
11898 | ret = build_int_cstu (size_type_node, bytes); | |
11899 | } | |
11900 | ||
11901 | if (ret) | |
11902 | { | |
697bbc3f | 11903 | unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (ret); |
11904 | HOST_WIDE_INT high = TREE_INT_CST_HIGH (ret); | |
11905 | if (fit_double_type (low, high, &low, &high, TREE_TYPE (ret))) | |
11906 | ret = NULL_TREE; | |
0a39fd54 | 11907 | } |
11908 | ||
11909 | return ret; | |
11910 | } | |
11911 | ||
11912 | /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin. | |
c2f47e15 | 11913 | DEST, SRC, LEN, and SIZE are the arguments to the call. |
0a39fd54 | 11914 | IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_* |
11915 | code of the builtin. If MAXLEN is not NULL, it is maximum length | |
11916 | passed as third argument. */ | |
11917 | ||
11918 | tree | |
c2f47e15 | 11919 | fold_builtin_memory_chk (tree fndecl, |
11920 | tree dest, tree src, tree len, tree size, | |
11921 | tree maxlen, bool ignore, | |
0a39fd54 | 11922 | enum built_in_function fcode) |
11923 | { | |
c2f47e15 | 11924 | tree fn; |
0a39fd54 | 11925 | |
c2f47e15 | 11926 | if (!validate_arg (dest, POINTER_TYPE) |
11927 | || !validate_arg (src, | |
11928 | (fcode == BUILT_IN_MEMSET_CHK | |
11929 | ? INTEGER_TYPE : POINTER_TYPE)) | |
11930 | || !validate_arg (len, INTEGER_TYPE) | |
11931 | || !validate_arg (size, INTEGER_TYPE)) | |
11932 | return NULL_TREE; | |
0a39fd54 | 11933 | |
11934 | /* If SRC and DEST are the same (and not volatile), return DEST | |
11935 | (resp. DEST+LEN for __mempcpy_chk). */ | |
11936 | if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0)) | |
11937 | { | |
11938 | if (fcode != BUILT_IN_MEMPCPY_CHK) | |
11939 | return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len); | |
11940 | else | |
11941 | { | |
0de36bdb | 11942 | tree temp = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (dest), dest, len); |
0a39fd54 | 11943 | return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), temp); |
11944 | } | |
11945 | } | |
11946 | ||
11947 | if (! host_integerp (size, 1)) | |
c2f47e15 | 11948 | return NULL_TREE; |
0a39fd54 | 11949 | |
11950 | if (! integer_all_onesp (size)) | |
11951 | { | |
11952 | if (! host_integerp (len, 1)) | |
11953 | { | |
11954 | /* If LEN is not constant, try MAXLEN too. | |
11955 | For MAXLEN only allow optimizing into non-_ocs function | |
11956 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
11957 | if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1)) | |
11958 | { | |
11959 | if (fcode == BUILT_IN_MEMPCPY_CHK && ignore) | |
11960 | { | |
11961 | /* (void) __mempcpy_chk () can be optimized into | |
11962 | (void) __memcpy_chk (). */ | |
11963 | fn = built_in_decls[BUILT_IN_MEMCPY_CHK]; | |
11964 | if (!fn) | |
c2f47e15 | 11965 | return NULL_TREE; |
0a39fd54 | 11966 | |
c2f47e15 | 11967 | return build_call_expr (fn, 4, dest, src, len, size); |
0a39fd54 | 11968 | } |
c2f47e15 | 11969 | return NULL_TREE; |
0a39fd54 | 11970 | } |
0a39fd54 | 11971 | } |
ad89623c | 11972 | else |
11973 | maxlen = len; | |
0a39fd54 | 11974 | |
ad89623c | 11975 | if (tree_int_cst_lt (size, maxlen)) |
c2f47e15 | 11976 | return NULL_TREE; |
0a39fd54 | 11977 | } |
11978 | ||
0a39fd54 | 11979 | fn = NULL_TREE; |
11980 | /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume | |
11981 | mem{cpy,pcpy,move,set} is available. */ | |
11982 | switch (fcode) | |
11983 | { | |
11984 | case BUILT_IN_MEMCPY_CHK: | |
11985 | fn = built_in_decls[BUILT_IN_MEMCPY]; | |
11986 | break; | |
11987 | case BUILT_IN_MEMPCPY_CHK: | |
11988 | fn = built_in_decls[BUILT_IN_MEMPCPY]; | |
11989 | break; | |
11990 | case BUILT_IN_MEMMOVE_CHK: | |
11991 | fn = built_in_decls[BUILT_IN_MEMMOVE]; | |
11992 | break; | |
11993 | case BUILT_IN_MEMSET_CHK: | |
11994 | fn = built_in_decls[BUILT_IN_MEMSET]; | |
11995 | break; | |
11996 | default: | |
11997 | break; | |
11998 | } | |
11999 | ||
12000 | if (!fn) | |
c2f47e15 | 12001 | return NULL_TREE; |
0a39fd54 | 12002 | |
c2f47e15 | 12003 | return build_call_expr (fn, 3, dest, src, len); |
0a39fd54 | 12004 | } |
12005 | ||
12006 | /* Fold a call to the __st[rp]cpy_chk builtin. | |
c2f47e15 | 12007 | DEST, SRC, and SIZE are the arguments to the call. |
12008 | IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_* | |
0a39fd54 | 12009 | code of the builtin. If MAXLEN is not NULL, it is maximum length of |
12010 | strings passed as second argument. */ | |
12011 | ||
12012 | tree | |
c2f47e15 | 12013 | fold_builtin_stxcpy_chk (tree fndecl, tree dest, tree src, tree size, |
12014 | tree maxlen, bool ignore, | |
0a39fd54 | 12015 | enum built_in_function fcode) |
12016 | { | |
c2f47e15 | 12017 | tree len, fn; |
0a39fd54 | 12018 | |
c2f47e15 | 12019 | if (!validate_arg (dest, POINTER_TYPE) |
12020 | || !validate_arg (src, POINTER_TYPE) | |
12021 | || !validate_arg (size, INTEGER_TYPE)) | |
12022 | return NULL_TREE; | |
0a39fd54 | 12023 | |
12024 | /* If SRC and DEST are the same (and not volatile), return DEST. */ | |
12025 | if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0)) | |
12026 | return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest); | |
a0c938f0 | 12027 | |
0a39fd54 | 12028 | if (! host_integerp (size, 1)) |
c2f47e15 | 12029 | return NULL_TREE; |
0a39fd54 | 12030 | |
12031 | if (! integer_all_onesp (size)) | |
12032 | { | |
12033 | len = c_strlen (src, 1); | |
12034 | if (! len || ! host_integerp (len, 1)) | |
12035 | { | |
12036 | /* If LEN is not constant, try MAXLEN too. | |
12037 | For MAXLEN only allow optimizing into non-_ocs function | |
12038 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
12039 | if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1)) | |
12040 | { | |
12041 | if (fcode == BUILT_IN_STPCPY_CHK) | |
12042 | { | |
12043 | if (! ignore) | |
c2f47e15 | 12044 | return NULL_TREE; |
0a39fd54 | 12045 | |
12046 | /* If return value of __stpcpy_chk is ignored, | |
12047 | optimize into __strcpy_chk. */ | |
12048 | fn = built_in_decls[BUILT_IN_STRCPY_CHK]; | |
12049 | if (!fn) | |
c2f47e15 | 12050 | return NULL_TREE; |
0a39fd54 | 12051 | |
c2f47e15 | 12052 | return build_call_expr (fn, 3, dest, src, size); |
0a39fd54 | 12053 | } |
12054 | ||
12055 | if (! len || TREE_SIDE_EFFECTS (len)) | |
c2f47e15 | 12056 | return NULL_TREE; |
0a39fd54 | 12057 | |
12058 | /* If c_strlen returned something, but not a constant, | |
12059 | transform __strcpy_chk into __memcpy_chk. */ | |
12060 | fn = built_in_decls[BUILT_IN_MEMCPY_CHK]; | |
12061 | if (!fn) | |
c2f47e15 | 12062 | return NULL_TREE; |
0a39fd54 | 12063 | |
12064 | len = size_binop (PLUS_EXPR, len, ssize_int (1)); | |
0a39fd54 | 12065 | return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), |
c2f47e15 | 12066 | build_call_expr (fn, 4, |
12067 | dest, src, len, size)); | |
0a39fd54 | 12068 | } |
0a39fd54 | 12069 | } |
ad89623c | 12070 | else |
12071 | maxlen = len; | |
12072 | ||
12073 | if (! tree_int_cst_lt (maxlen, size)) | |
c2f47e15 | 12074 | return NULL_TREE; |
0a39fd54 | 12075 | } |
12076 | ||
0a39fd54 | 12077 | /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */ |
12078 | fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK | |
12079 | ? BUILT_IN_STPCPY : BUILT_IN_STRCPY]; | |
12080 | if (!fn) | |
c2f47e15 | 12081 | return NULL_TREE; |
0a39fd54 | 12082 | |
c2f47e15 | 12083 | return build_call_expr (fn, 2, dest, src); |
0a39fd54 | 12084 | } |
12085 | ||
c2f47e15 | 12086 | /* Fold a call to the __strncpy_chk builtin. DEST, SRC, LEN, and SIZE |
12087 | are the arguments to the call. If MAXLEN is not NULL, it is maximum | |
12088 | length passed as third argument. */ | |
0a39fd54 | 12089 | |
12090 | tree | |
c2f47e15 | 12091 | fold_builtin_strncpy_chk (tree dest, tree src, tree len, tree size, |
12092 | tree maxlen) | |
0a39fd54 | 12093 | { |
c2f47e15 | 12094 | tree fn; |
0a39fd54 | 12095 | |
c2f47e15 | 12096 | if (!validate_arg (dest, POINTER_TYPE) |
12097 | || !validate_arg (src, POINTER_TYPE) | |
12098 | || !validate_arg (len, INTEGER_TYPE) | |
12099 | || !validate_arg (size, INTEGER_TYPE)) | |
12100 | return NULL_TREE; | |
0a39fd54 | 12101 | |
12102 | if (! host_integerp (size, 1)) | |
c2f47e15 | 12103 | return NULL_TREE; |
0a39fd54 | 12104 | |
12105 | if (! integer_all_onesp (size)) | |
12106 | { | |
12107 | if (! host_integerp (len, 1)) | |
12108 | { | |
12109 | /* If LEN is not constant, try MAXLEN too. | |
12110 | For MAXLEN only allow optimizing into non-_ocs function | |
12111 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
12112 | if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1)) | |
c2f47e15 | 12113 | return NULL_TREE; |
0a39fd54 | 12114 | } |
ad89623c | 12115 | else |
12116 | maxlen = len; | |
0a39fd54 | 12117 | |
ad89623c | 12118 | if (tree_int_cst_lt (size, maxlen)) |
c2f47e15 | 12119 | return NULL_TREE; |
0a39fd54 | 12120 | } |
12121 | ||
0a39fd54 | 12122 | /* If __builtin_strncpy_chk is used, assume strncpy is available. */ |
12123 | fn = built_in_decls[BUILT_IN_STRNCPY]; | |
12124 | if (!fn) | |
c2f47e15 | 12125 | return NULL_TREE; |
0a39fd54 | 12126 | |
c2f47e15 | 12127 | return build_call_expr (fn, 3, dest, src, len); |
0a39fd54 | 12128 | } |
12129 | ||
c2f47e15 | 12130 | /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE |
12131 | are the arguments to the call. */ | |
0a39fd54 | 12132 | |
12133 | static tree | |
c2f47e15 | 12134 | fold_builtin_strcat_chk (tree fndecl, tree dest, tree src, tree size) |
0a39fd54 | 12135 | { |
c2f47e15 | 12136 | tree fn; |
0a39fd54 | 12137 | const char *p; |
12138 | ||
c2f47e15 | 12139 | if (!validate_arg (dest, POINTER_TYPE) |
12140 | || !validate_arg (src, POINTER_TYPE) | |
12141 | || !validate_arg (size, INTEGER_TYPE)) | |
12142 | return NULL_TREE; | |
0a39fd54 | 12143 | |
12144 | p = c_getstr (src); | |
12145 | /* If the SRC parameter is "", return DEST. */ | |
12146 | if (p && *p == '\0') | |
12147 | return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src); | |
12148 | ||
12149 | if (! host_integerp (size, 1) || ! integer_all_onesp (size)) | |
c2f47e15 | 12150 | return NULL_TREE; |
0a39fd54 | 12151 | |
12152 | /* If __builtin_strcat_chk is used, assume strcat is available. */ | |
12153 | fn = built_in_decls[BUILT_IN_STRCAT]; | |
12154 | if (!fn) | |
c2f47e15 | 12155 | return NULL_TREE; |
0a39fd54 | 12156 | |
c2f47e15 | 12157 | return build_call_expr (fn, 2, dest, src); |
0a39fd54 | 12158 | } |
12159 | ||
c2f47e15 | 12160 | /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC, |
12161 | LEN, and SIZE. */ | |
0a39fd54 | 12162 | |
12163 | static tree | |
c2f47e15 | 12164 | fold_builtin_strncat_chk (tree fndecl, |
12165 | tree dest, tree src, tree len, tree size) | |
0a39fd54 | 12166 | { |
c2f47e15 | 12167 | tree fn; |
0a39fd54 | 12168 | const char *p; |
12169 | ||
c2f47e15 | 12170 | if (!validate_arg (dest, POINTER_TYPE) |
12171 | || !validate_arg (src, POINTER_TYPE) | |
12172 | || !validate_arg (size, INTEGER_TYPE) | |
12173 | || !validate_arg (size, INTEGER_TYPE)) | |
12174 | return NULL_TREE; | |
0a39fd54 | 12175 | |
12176 | p = c_getstr (src); | |
12177 | /* If the SRC parameter is "" or if LEN is 0, return DEST. */ | |
12178 | if (p && *p == '\0') | |
12179 | return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len); | |
12180 | else if (integer_zerop (len)) | |
12181 | return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src); | |
12182 | ||
12183 | if (! host_integerp (size, 1)) | |
c2f47e15 | 12184 | return NULL_TREE; |
0a39fd54 | 12185 | |
12186 | if (! integer_all_onesp (size)) | |
12187 | { | |
12188 | tree src_len = c_strlen (src, 1); | |
12189 | if (src_len | |
12190 | && host_integerp (src_len, 1) | |
12191 | && host_integerp (len, 1) | |
12192 | && ! tree_int_cst_lt (len, src_len)) | |
12193 | { | |
12194 | /* If LEN >= strlen (SRC), optimize into __strcat_chk. */ | |
12195 | fn = built_in_decls[BUILT_IN_STRCAT_CHK]; | |
12196 | if (!fn) | |
c2f47e15 | 12197 | return NULL_TREE; |
0a39fd54 | 12198 | |
c2f47e15 | 12199 | return build_call_expr (fn, 3, dest, src, size); |
0a39fd54 | 12200 | } |
c2f47e15 | 12201 | return NULL_TREE; |
0a39fd54 | 12202 | } |
12203 | ||
0a39fd54 | 12204 | /* If __builtin_strncat_chk is used, assume strncat is available. */ |
12205 | fn = built_in_decls[BUILT_IN_STRNCAT]; | |
12206 | if (!fn) | |
c2f47e15 | 12207 | return NULL_TREE; |
0a39fd54 | 12208 | |
c2f47e15 | 12209 | return build_call_expr (fn, 3, dest, src, len); |
0a39fd54 | 12210 | } |
12211 | ||
c2f47e15 | 12212 | /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if |
0a39fd54 | 12213 | a normal call should be emitted rather than expanding the function |
12214 | inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */ | |
12215 | ||
12216 | static tree | |
c2f47e15 | 12217 | fold_builtin_sprintf_chk (tree exp, enum built_in_function fcode) |
0a39fd54 | 12218 | { |
12219 | tree dest, size, len, fn, fmt, flag; | |
12220 | const char *fmt_str; | |
c2f47e15 | 12221 | int nargs = call_expr_nargs (exp); |
0a39fd54 | 12222 | |
12223 | /* Verify the required arguments in the original call. */ | |
c2f47e15 | 12224 | if (nargs < 4) |
12225 | return NULL_TREE; | |
12226 | dest = CALL_EXPR_ARG (exp, 0); | |
12227 | if (!validate_arg (dest, POINTER_TYPE)) | |
12228 | return NULL_TREE; | |
12229 | flag = CALL_EXPR_ARG (exp, 1); | |
12230 | if (!validate_arg (flag, INTEGER_TYPE)) | |
12231 | return NULL_TREE; | |
12232 | size = CALL_EXPR_ARG (exp, 2); | |
12233 | if (!validate_arg (size, INTEGER_TYPE)) | |
12234 | return NULL_TREE; | |
12235 | fmt = CALL_EXPR_ARG (exp, 3); | |
12236 | if (!validate_arg (fmt, POINTER_TYPE)) | |
12237 | return NULL_TREE; | |
0a39fd54 | 12238 | |
12239 | if (! host_integerp (size, 1)) | |
c2f47e15 | 12240 | return NULL_TREE; |
0a39fd54 | 12241 | |
12242 | len = NULL_TREE; | |
12243 | ||
d4473c84 | 12244 | if (!init_target_chars ()) |
c2f47e15 | 12245 | return NULL_TREE; |
99eabcc1 | 12246 | |
0a39fd54 | 12247 | /* Check whether the format is a literal string constant. */ |
12248 | fmt_str = c_getstr (fmt); | |
12249 | if (fmt_str != NULL) | |
12250 | { | |
12251 | /* If the format doesn't contain % args or %%, we know the size. */ | |
99eabcc1 | 12252 | if (strchr (fmt_str, target_percent) == 0) |
0a39fd54 | 12253 | { |
c2f47e15 | 12254 | if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4) |
0a39fd54 | 12255 | len = build_int_cstu (size_type_node, strlen (fmt_str)); |
12256 | } | |
12257 | /* If the format is "%s" and first ... argument is a string literal, | |
12258 | we know the size too. */ | |
c2f47e15 | 12259 | else if (fcode == BUILT_IN_SPRINTF_CHK |
12260 | && strcmp (fmt_str, target_percent_s) == 0) | |
0a39fd54 | 12261 | { |
12262 | tree arg; | |
12263 | ||
c2f47e15 | 12264 | if (nargs == 5) |
0a39fd54 | 12265 | { |
c2f47e15 | 12266 | arg = CALL_EXPR_ARG (exp, 4); |
12267 | if (validate_arg (arg, POINTER_TYPE)) | |
0a39fd54 | 12268 | { |
12269 | len = c_strlen (arg, 1); | |
12270 | if (! len || ! host_integerp (len, 1)) | |
12271 | len = NULL_TREE; | |
12272 | } | |
12273 | } | |
12274 | } | |
12275 | } | |
12276 | ||
12277 | if (! integer_all_onesp (size)) | |
12278 | { | |
12279 | if (! len || ! tree_int_cst_lt (len, size)) | |
c2f47e15 | 12280 | return NULL_TREE; |
0a39fd54 | 12281 | } |
12282 | ||
12283 | /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0 | |
12284 | or if format doesn't contain % chars or is "%s". */ | |
12285 | if (! integer_zerop (flag)) | |
12286 | { | |
12287 | if (fmt_str == NULL) | |
c2f47e15 | 12288 | return NULL_TREE; |
12289 | if (strchr (fmt_str, target_percent) != NULL | |
12290 | && strcmp (fmt_str, target_percent_s)) | |
12291 | return NULL_TREE; | |
0a39fd54 | 12292 | } |
12293 | ||
0a39fd54 | 12294 | /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */ |
12295 | fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK | |
12296 | ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF]; | |
12297 | if (!fn) | |
c2f47e15 | 12298 | return NULL_TREE; |
0a39fd54 | 12299 | |
c2f47e15 | 12300 | return rewrite_call_expr (exp, 4, fn, 2, dest, fmt); |
0a39fd54 | 12301 | } |
12302 | ||
c2f47e15 | 12303 | /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if |
0a39fd54 | 12304 | a normal call should be emitted rather than expanding the function |
12305 | inline. FCODE is either BUILT_IN_SNPRINTF_CHK or | |
12306 | BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length | |
12307 | passed as second argument. */ | |
12308 | ||
12309 | tree | |
c2f47e15 | 12310 | fold_builtin_snprintf_chk (tree exp, tree maxlen, |
0a39fd54 | 12311 | enum built_in_function fcode) |
12312 | { | |
12313 | tree dest, size, len, fn, fmt, flag; | |
12314 | const char *fmt_str; | |
12315 | ||
12316 | /* Verify the required arguments in the original call. */ | |
c2f47e15 | 12317 | if (call_expr_nargs (exp) < 5) |
12318 | return NULL_TREE; | |
12319 | dest = CALL_EXPR_ARG (exp, 0); | |
12320 | if (!validate_arg (dest, POINTER_TYPE)) | |
12321 | return NULL_TREE; | |
12322 | len = CALL_EXPR_ARG (exp, 1); | |
12323 | if (!validate_arg (len, INTEGER_TYPE)) | |
12324 | return NULL_TREE; | |
12325 | flag = CALL_EXPR_ARG (exp, 2); | |
12326 | if (!validate_arg (flag, INTEGER_TYPE)) | |
12327 | return NULL_TREE; | |
12328 | size = CALL_EXPR_ARG (exp, 3); | |
12329 | if (!validate_arg (size, INTEGER_TYPE)) | |
12330 | return NULL_TREE; | |
12331 | fmt = CALL_EXPR_ARG (exp, 4); | |
12332 | if (!validate_arg (fmt, POINTER_TYPE)) | |
12333 | return NULL_TREE; | |
0a39fd54 | 12334 | |
12335 | if (! host_integerp (size, 1)) | |
c2f47e15 | 12336 | return NULL_TREE; |
0a39fd54 | 12337 | |
12338 | if (! integer_all_onesp (size)) | |
12339 | { | |
12340 | if (! host_integerp (len, 1)) | |
12341 | { | |
12342 | /* If LEN is not constant, try MAXLEN too. | |
12343 | For MAXLEN only allow optimizing into non-_ocs function | |
12344 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
12345 | if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1)) | |
c2f47e15 | 12346 | return NULL_TREE; |
0a39fd54 | 12347 | } |
ad89623c | 12348 | else |
12349 | maxlen = len; | |
0a39fd54 | 12350 | |
ad89623c | 12351 | if (tree_int_cst_lt (size, maxlen)) |
c2f47e15 | 12352 | return NULL_TREE; |
0a39fd54 | 12353 | } |
12354 | ||
d4473c84 | 12355 | if (!init_target_chars ()) |
c2f47e15 | 12356 | return NULL_TREE; |
99eabcc1 | 12357 | |
0a39fd54 | 12358 | /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0 |
12359 | or if format doesn't contain % chars or is "%s". */ | |
12360 | if (! integer_zerop (flag)) | |
12361 | { | |
12362 | fmt_str = c_getstr (fmt); | |
12363 | if (fmt_str == NULL) | |
c2f47e15 | 12364 | return NULL_TREE; |
12365 | if (strchr (fmt_str, target_percent) != NULL | |
12366 | && strcmp (fmt_str, target_percent_s)) | |
12367 | return NULL_TREE; | |
0a39fd54 | 12368 | } |
12369 | ||
0a39fd54 | 12370 | /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is |
12371 | available. */ | |
12372 | fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK | |
12373 | ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF]; | |
12374 | if (!fn) | |
c2f47e15 | 12375 | return NULL_TREE; |
0a39fd54 | 12376 | |
c2f47e15 | 12377 | return rewrite_call_expr (exp, 5, fn, 3, dest, len, fmt); |
0a39fd54 | 12378 | } |
12379 | ||
12380 | /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins. | |
c2f47e15 | 12381 | FMT and ARG are the arguments to the call; we don't fold cases with |
12382 | more than 2 arguments, and ARG may be null if this is a 1-argument case. | |
0a39fd54 | 12383 | |
c2f47e15 | 12384 | Return NULL_TREE if no simplification was possible, otherwise return the |
0a39fd54 | 12385 | simplified form of the call as a tree. FCODE is the BUILT_IN_* |
12386 | code of the function to be simplified. */ | |
12387 | ||
12388 | static tree | |
c2f47e15 | 12389 | fold_builtin_printf (tree fndecl, tree fmt, tree arg, bool ignore, |
0a39fd54 | 12390 | enum built_in_function fcode) |
12391 | { | |
c2f47e15 | 12392 | tree fn_putchar, fn_puts, newarg, call = NULL_TREE; |
0a39fd54 | 12393 | const char *fmt_str = NULL; |
12394 | ||
12395 | /* If the return value is used, don't do the transformation. */ | |
12396 | if (! ignore) | |
c2f47e15 | 12397 | return NULL_TREE; |
0a39fd54 | 12398 | |
12399 | /* Verify the required arguments in the original call. */ | |
c2f47e15 | 12400 | if (!validate_arg (fmt, POINTER_TYPE)) |
12401 | return NULL_TREE; | |
0a39fd54 | 12402 | |
12403 | /* Check whether the format is a literal string constant. */ | |
12404 | fmt_str = c_getstr (fmt); | |
12405 | if (fmt_str == NULL) | |
12406 | return NULL_TREE; | |
12407 | ||
12408 | if (fcode == BUILT_IN_PRINTF_UNLOCKED) | |
12409 | { | |
6d77ed92 | 12410 | /* If we're using an unlocked function, assume the other |
12411 | unlocked functions exist explicitly. */ | |
12412 | fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]; | |
12413 | fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED]; | |
0a39fd54 | 12414 | } |
12415 | else | |
12416 | { | |
12417 | fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR]; | |
12418 | fn_puts = implicit_built_in_decls[BUILT_IN_PUTS]; | |
12419 | } | |
12420 | ||
d4473c84 | 12421 | if (!init_target_chars ()) |
c2f47e15 | 12422 | return NULL_TREE; |
a0c938f0 | 12423 | |
c2f47e15 | 12424 | if (strcmp (fmt_str, target_percent_s) == 0 |
12425 | || strchr (fmt_str, target_percent) == NULL) | |
0a39fd54 | 12426 | { |
12427 | const char *str; | |
12428 | ||
99eabcc1 | 12429 | if (strcmp (fmt_str, target_percent_s) == 0) |
0a39fd54 | 12430 | { |
12431 | if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK) | |
c2f47e15 | 12432 | return NULL_TREE; |
0a39fd54 | 12433 | |
c2f47e15 | 12434 | if (!arg || !validate_arg (arg, POINTER_TYPE)) |
12435 | return NULL_TREE; | |
0a39fd54 | 12436 | |
c2f47e15 | 12437 | str = c_getstr (arg); |
0a39fd54 | 12438 | if (str == NULL) |
c2f47e15 | 12439 | return NULL_TREE; |
0a39fd54 | 12440 | } |
12441 | else | |
12442 | { | |
12443 | /* The format specifier doesn't contain any '%' characters. */ | |
12444 | if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK | |
c2f47e15 | 12445 | && arg) |
12446 | return NULL_TREE; | |
0a39fd54 | 12447 | str = fmt_str; |
12448 | } | |
12449 | ||
12450 | /* If the string was "", printf does nothing. */ | |
12451 | if (str[0] == '\0') | |
12452 | return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0); | |
12453 | ||
12454 | /* If the string has length of 1, call putchar. */ | |
12455 | if (str[1] == '\0') | |
12456 | { | |
12457 | /* Given printf("c"), (where c is any one character,) | |
12458 | convert "c"[0] to an int and pass that to the replacement | |
12459 | function. */ | |
c2f47e15 | 12460 | newarg = build_int_cst (NULL_TREE, str[0]); |
12461 | if (fn_putchar) | |
12462 | call = build_call_expr (fn_putchar, 1, newarg); | |
0a39fd54 | 12463 | } |
12464 | else | |
12465 | { | |
12466 | /* If the string was "string\n", call puts("string"). */ | |
12467 | size_t len = strlen (str); | |
99eabcc1 | 12468 | if ((unsigned char)str[len - 1] == target_newline) |
0a39fd54 | 12469 | { |
12470 | /* Create a NUL-terminated string that's one char shorter | |
12471 | than the original, stripping off the trailing '\n'. */ | |
12472 | char *newstr = alloca (len); | |
12473 | memcpy (newstr, str, len - 1); | |
12474 | newstr[len - 1] = 0; | |
12475 | ||
c2f47e15 | 12476 | newarg = build_string_literal (len, newstr); |
12477 | if (fn_puts) | |
12478 | call = build_call_expr (fn_puts, 1, newarg); | |
0a39fd54 | 12479 | } |
12480 | else | |
12481 | /* We'd like to arrange to call fputs(string,stdout) here, | |
12482 | but we need stdout and don't have a way to get it yet. */ | |
c2f47e15 | 12483 | return NULL_TREE; |
0a39fd54 | 12484 | } |
12485 | } | |
12486 | ||
12487 | /* The other optimizations can be done only on the non-va_list variants. */ | |
12488 | else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK) | |
c2f47e15 | 12489 | return NULL_TREE; |
0a39fd54 | 12490 | |
12491 | /* If the format specifier was "%s\n", call __builtin_puts(arg). */ | |
99eabcc1 | 12492 | else if (strcmp (fmt_str, target_percent_s_newline) == 0) |
0a39fd54 | 12493 | { |
c2f47e15 | 12494 | if (!arg || !validate_arg (arg, POINTER_TYPE)) |
12495 | return NULL_TREE; | |
12496 | if (fn_puts) | |
12497 | call = build_call_expr (fn_puts, 1, arg); | |
0a39fd54 | 12498 | } |
12499 | ||
12500 | /* If the format specifier was "%c", call __builtin_putchar(arg). */ | |
99eabcc1 | 12501 | else if (strcmp (fmt_str, target_percent_c) == 0) |
0a39fd54 | 12502 | { |
c2f47e15 | 12503 | if (!arg || !validate_arg (arg, INTEGER_TYPE)) |
12504 | return NULL_TREE; | |
12505 | if (fn_putchar) | |
12506 | call = build_call_expr (fn_putchar, 1, arg); | |
0a39fd54 | 12507 | } |
12508 | ||
c2f47e15 | 12509 | if (!call) |
12510 | return NULL_TREE; | |
0a39fd54 | 12511 | |
0a39fd54 | 12512 | return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call); |
12513 | } | |
12514 | ||
12515 | /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins. | |
c2f47e15 | 12516 | FP, FMT, and ARG are the arguments to the call. We don't fold calls with |
12517 | more than 3 arguments, and ARG may be null in the 2-argument case. | |
0a39fd54 | 12518 | |
c2f47e15 | 12519 | Return NULL_TREE if no simplification was possible, otherwise return the |
0a39fd54 | 12520 | simplified form of the call as a tree. FCODE is the BUILT_IN_* |
12521 | code of the function to be simplified. */ | |
12522 | ||
12523 | static tree | |
c2f47e15 | 12524 | fold_builtin_fprintf (tree fndecl, tree fp, tree fmt, tree arg, bool ignore, |
0a39fd54 | 12525 | enum built_in_function fcode) |
12526 | { | |
c2f47e15 | 12527 | tree fn_fputc, fn_fputs, call = NULL_TREE; |
0a39fd54 | 12528 | const char *fmt_str = NULL; |
12529 | ||
12530 | /* If the return value is used, don't do the transformation. */ | |
12531 | if (! ignore) | |
c2f47e15 | 12532 | return NULL_TREE; |
0a39fd54 | 12533 | |
12534 | /* Verify the required arguments in the original call. */ | |
c2f47e15 | 12535 | if (!validate_arg (fp, POINTER_TYPE)) |
12536 | return NULL_TREE; | |
12537 | if (!validate_arg (fmt, POINTER_TYPE)) | |
12538 | return NULL_TREE; | |
0a39fd54 | 12539 | |
12540 | /* Check whether the format is a literal string constant. */ | |
12541 | fmt_str = c_getstr (fmt); | |
12542 | if (fmt_str == NULL) | |
12543 | return NULL_TREE; | |
12544 | ||
12545 | if (fcode == BUILT_IN_FPRINTF_UNLOCKED) | |
12546 | { | |
6d77ed92 | 12547 | /* If we're using an unlocked function, assume the other |
12548 | unlocked functions exist explicitly. */ | |
12549 | fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED]; | |
12550 | fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED]; | |
0a39fd54 | 12551 | } |
12552 | else | |
12553 | { | |
12554 | fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC]; | |
12555 | fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS]; | |
12556 | } | |
12557 | ||
d4473c84 | 12558 | if (!init_target_chars ()) |
c2f47e15 | 12559 | return NULL_TREE; |
a0c938f0 | 12560 | |
0a39fd54 | 12561 | /* If the format doesn't contain % args or %%, use strcpy. */ |
99eabcc1 | 12562 | if (strchr (fmt_str, target_percent) == NULL) |
0a39fd54 | 12563 | { |
12564 | if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK | |
c2f47e15 | 12565 | && arg) |
12566 | return NULL_TREE; | |
0a39fd54 | 12567 | |
12568 | /* If the format specifier was "", fprintf does nothing. */ | |
12569 | if (fmt_str[0] == '\0') | |
12570 | { | |
12571 | /* If FP has side-effects, just wait until gimplification is | |
12572 | done. */ | |
12573 | if (TREE_SIDE_EFFECTS (fp)) | |
c2f47e15 | 12574 | return NULL_TREE; |
0a39fd54 | 12575 | |
12576 | return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0); | |
12577 | } | |
12578 | ||
12579 | /* When "string" doesn't contain %, replace all cases of | |
12580 | fprintf (fp, string) with fputs (string, fp). The fputs | |
12581 | builtin will take care of special cases like length == 1. */ | |
c2f47e15 | 12582 | if (fn_fputs) |
12583 | call = build_call_expr (fn_fputs, 2, fmt, fp); | |
0a39fd54 | 12584 | } |
12585 | ||
12586 | /* The other optimizations can be done only on the non-va_list variants. */ | |
12587 | else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK) | |
c2f47e15 | 12588 | return NULL_TREE; |
0a39fd54 | 12589 | |
12590 | /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */ | |
99eabcc1 | 12591 | else if (strcmp (fmt_str, target_percent_s) == 0) |
0a39fd54 | 12592 | { |
c2f47e15 | 12593 | if (!arg || !validate_arg (arg, POINTER_TYPE)) |
12594 | return NULL_TREE; | |
12595 | if (fn_fputs) | |
12596 | call = build_call_expr (fn_fputs, 2, arg, fp); | |
0a39fd54 | 12597 | } |
12598 | ||
12599 | /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */ | |
99eabcc1 | 12600 | else if (strcmp (fmt_str, target_percent_c) == 0) |
0a39fd54 | 12601 | { |
c2f47e15 | 12602 | if (!arg || !validate_arg (arg, INTEGER_TYPE)) |
12603 | return NULL_TREE; | |
12604 | if (fn_fputc) | |
12605 | call = build_call_expr (fn_fputc, 2, arg, fp); | |
0a39fd54 | 12606 | } |
12607 | ||
c2f47e15 | 12608 | if (!call) |
12609 | return NULL_TREE; | |
0a39fd54 | 12610 | return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call); |
12611 | } | |
99eabcc1 | 12612 | |
12613 | /* Initialize format string characters in the target charset. */ | |
12614 | ||
12615 | static bool | |
12616 | init_target_chars (void) | |
12617 | { | |
12618 | static bool init; | |
12619 | if (!init) | |
12620 | { | |
12621 | target_newline = lang_hooks.to_target_charset ('\n'); | |
12622 | target_percent = lang_hooks.to_target_charset ('%'); | |
12623 | target_c = lang_hooks.to_target_charset ('c'); | |
12624 | target_s = lang_hooks.to_target_charset ('s'); | |
12625 | if (target_newline == 0 || target_percent == 0 || target_c == 0 | |
12626 | || target_s == 0) | |
12627 | return false; | |
12628 | ||
12629 | target_percent_c[0] = target_percent; | |
12630 | target_percent_c[1] = target_c; | |
12631 | target_percent_c[2] = '\0'; | |
12632 | ||
12633 | target_percent_s[0] = target_percent; | |
12634 | target_percent_s[1] = target_s; | |
12635 | target_percent_s[2] = '\0'; | |
12636 | ||
12637 | target_percent_s_newline[0] = target_percent; | |
12638 | target_percent_s_newline[1] = target_s; | |
12639 | target_percent_s_newline[2] = target_newline; | |
12640 | target_percent_s_newline[3] = '\0'; | |
a0c938f0 | 12641 | |
99eabcc1 | 12642 | init = true; |
12643 | } | |
12644 | return true; | |
12645 | } | |
bffb7645 | 12646 | |
f0c477f2 | 12647 | /* Helper function for do_mpfr_arg*(). Ensure M is a normal number |
12648 | and no overflow/underflow occurred. INEXACT is true if M was not | |
fa7637bd | 12649 | exactly calculated. TYPE is the tree type for the result. This |
f0c477f2 | 12650 | function assumes that you cleared the MPFR flags and then |
12651 | calculated M to see if anything subsequently set a flag prior to | |
12652 | entering this function. Return NULL_TREE if any checks fail. */ | |
12653 | ||
12654 | static tree | |
d4473c84 | 12655 | do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact) |
f0c477f2 | 12656 | { |
12657 | /* Proceed iff we get a normal number, i.e. not NaN or Inf and no | |
12658 | overflow/underflow occurred. If -frounding-math, proceed iff the | |
12659 | result of calling FUNC was exact. */ | |
d4473c84 | 12660 | if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p () |
f0c477f2 | 12661 | && (!flag_rounding_math || !inexact)) |
12662 | { | |
12663 | REAL_VALUE_TYPE rr; | |
12664 | ||
66fa16e6 | 12665 | real_from_mpfr (&rr, m, type, GMP_RNDN); |
f0c477f2 | 12666 | /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value, |
12667 | check for overflow/underflow. If the REAL_VALUE_TYPE is zero | |
12668 | but the mpft_t is not, then we underflowed in the | |
12669 | conversion. */ | |
776a7bab | 12670 | if (real_isfinite (&rr) |
f0c477f2 | 12671 | && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0)) |
12672 | { | |
12673 | REAL_VALUE_TYPE rmode; | |
12674 | ||
12675 | real_convert (&rmode, TYPE_MODE (type), &rr); | |
12676 | /* Proceed iff the specified mode can hold the value. */ | |
12677 | if (real_identical (&rmode, &rr)) | |
12678 | return build_real (type, rmode); | |
12679 | } | |
12680 | } | |
12681 | return NULL_TREE; | |
12682 | } | |
12683 | ||
bffb7645 | 12684 | /* If argument ARG is a REAL_CST, call the one-argument mpfr function |
12685 | FUNC on it and return the resulting value as a tree with type TYPE. | |
728bac60 | 12686 | If MIN and/or MAX are not NULL, then the supplied ARG must be |
12687 | within those bounds. If INCLUSIVE is true, then MIN/MAX are | |
12688 | acceptable values, otherwise they are not. The mpfr precision is | |
12689 | set to the precision of TYPE. We assume that function FUNC returns | |
12690 | zero if the result could be calculated exactly within the requested | |
12691 | precision. */ | |
bffb7645 | 12692 | |
12693 | static tree | |
728bac60 | 12694 | do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t), |
12695 | const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max, | |
12696 | bool inclusive) | |
bffb7645 | 12697 | { |
12698 | tree result = NULL_TREE; | |
12699 | ||
12700 | STRIP_NOPS (arg); | |
12701 | ||
bd7d6fa4 | 12702 | /* To proceed, MPFR must exactly represent the target floating point |
12703 | format, which only happens when the target base equals two. */ | |
12704 | if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2 | |
f96bd2bf | 12705 | && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)) |
bffb7645 | 12706 | { |
f0c477f2 | 12707 | const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg); |
bffb7645 | 12708 | |
776a7bab | 12709 | if (real_isfinite (ra) |
f0c477f2 | 12710 | && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)) |
12711 | && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max))) | |
bffb7645 | 12712 | { |
f0c477f2 | 12713 | const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p; |
debf9994 | 12714 | int inexact; |
bffb7645 | 12715 | mpfr_t m; |
12716 | ||
12717 | mpfr_init2 (m, prec); | |
66fa16e6 | 12718 | mpfr_from_real (m, ra, GMP_RNDN); |
d4473c84 | 12719 | mpfr_clear_flags (); |
debf9994 | 12720 | inexact = func (m, m, GMP_RNDN); |
f0c477f2 | 12721 | result = do_mpfr_ckconv (m, type, inexact); |
bffb7645 | 12722 | mpfr_clear (m); |
12723 | } | |
12724 | } | |
12725 | ||
12726 | return result; | |
12727 | } | |
f0c477f2 | 12728 | |
12729 | /* If argument ARG is a REAL_CST, call the two-argument mpfr function | |
12730 | FUNC on it and return the resulting value as a tree with type TYPE. | |
12731 | The mpfr precision is set to the precision of TYPE. We assume that | |
12732 | function FUNC returns zero if the result could be calculated | |
12733 | exactly within the requested precision. */ | |
12734 | ||
12735 | static tree | |
12736 | do_mpfr_arg2 (tree arg1, tree arg2, tree type, | |
12737 | int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t)) | |
12738 | { | |
12739 | tree result = NULL_TREE; | |
12740 | ||
12741 | STRIP_NOPS (arg1); | |
12742 | STRIP_NOPS (arg2); | |
12743 | ||
bd7d6fa4 | 12744 | /* To proceed, MPFR must exactly represent the target floating point |
12745 | format, which only happens when the target base equals two. */ | |
12746 | if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2 | |
f96bd2bf | 12747 | && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1) |
12748 | && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)) | |
f0c477f2 | 12749 | { |
12750 | const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1); | |
12751 | const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2); | |
12752 | ||
776a7bab | 12753 | if (real_isfinite (ra1) && real_isfinite (ra2)) |
f0c477f2 | 12754 | { |
12755 | const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p; | |
12756 | int inexact; | |
12757 | mpfr_t m1, m2; | |
12758 | ||
12759 | mpfr_inits2 (prec, m1, m2, NULL); | |
66fa16e6 | 12760 | mpfr_from_real (m1, ra1, GMP_RNDN); |
12761 | mpfr_from_real (m2, ra2, GMP_RNDN); | |
d4473c84 | 12762 | mpfr_clear_flags (); |
f0c477f2 | 12763 | inexact = func (m1, m1, m2, GMP_RNDN); |
12764 | result = do_mpfr_ckconv (m1, type, inexact); | |
12765 | mpfr_clears (m1, m2, NULL); | |
12766 | } | |
12767 | } | |
12768 | ||
12769 | return result; | |
12770 | } | |
d92f994c | 12771 | |
9917422b | 12772 | /* If argument ARG is a REAL_CST, call the three-argument mpfr function |
12773 | FUNC on it and return the resulting value as a tree with type TYPE. | |
12774 | The mpfr precision is set to the precision of TYPE. We assume that | |
12775 | function FUNC returns zero if the result could be calculated | |
12776 | exactly within the requested precision. */ | |
12777 | ||
12778 | static tree | |
12779 | do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type, | |
12780 | int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t)) | |
12781 | { | |
12782 | tree result = NULL_TREE; | |
12783 | ||
12784 | STRIP_NOPS (arg1); | |
12785 | STRIP_NOPS (arg2); | |
12786 | STRIP_NOPS (arg3); | |
12787 | ||
bd7d6fa4 | 12788 | /* To proceed, MPFR must exactly represent the target floating point |
12789 | format, which only happens when the target base equals two. */ | |
12790 | if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2 | |
f96bd2bf | 12791 | && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1) |
12792 | && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2) | |
12793 | && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3)) | |
9917422b | 12794 | { |
12795 | const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1); | |
12796 | const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2); | |
12797 | const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3); | |
12798 | ||
776a7bab | 12799 | if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3)) |
9917422b | 12800 | { |
12801 | const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p; | |
12802 | int inexact; | |
12803 | mpfr_t m1, m2, m3; | |
12804 | ||
12805 | mpfr_inits2 (prec, m1, m2, m3, NULL); | |
66fa16e6 | 12806 | mpfr_from_real (m1, ra1, GMP_RNDN); |
12807 | mpfr_from_real (m2, ra2, GMP_RNDN); | |
12808 | mpfr_from_real (m3, ra3, GMP_RNDN); | |
d4473c84 | 12809 | mpfr_clear_flags (); |
9917422b | 12810 | inexact = func (m1, m1, m2, m3, GMP_RNDN); |
12811 | result = do_mpfr_ckconv (m1, type, inexact); | |
12812 | mpfr_clears (m1, m2, m3, NULL); | |
12813 | } | |
12814 | } | |
12815 | ||
12816 | return result; | |
12817 | } | |
12818 | ||
d92f994c | 12819 | /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set |
12820 | the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values. | |
d735c391 | 12821 | If ARG_SINP and ARG_COSP are NULL then the result is returned |
12822 | as a complex value. | |
d92f994c | 12823 | The type is taken from the type of ARG and is used for setting the |
12824 | precision of the calculation and results. */ | |
12825 | ||
12826 | static tree | |
12827 | do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp) | |
12828 | { | |
bd7d6fa4 | 12829 | tree const type = TREE_TYPE (arg); |
d92f994c | 12830 | tree result = NULL_TREE; |
12831 | ||
12832 | STRIP_NOPS (arg); | |
12833 | ||
bd7d6fa4 | 12834 | /* To proceed, MPFR must exactly represent the target floating point |
12835 | format, which only happens when the target base equals two. */ | |
12836 | if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2 | |
f96bd2bf | 12837 | && TREE_CODE (arg) == REAL_CST |
12838 | && !TREE_OVERFLOW (arg)) | |
d92f994c | 12839 | { |
12840 | const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg); | |
12841 | ||
776a7bab | 12842 | if (real_isfinite (ra)) |
d92f994c | 12843 | { |
d92f994c | 12844 | const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p; |
12845 | tree result_s, result_c; | |
12846 | int inexact; | |
12847 | mpfr_t m, ms, mc; | |
12848 | ||
12849 | mpfr_inits2 (prec, m, ms, mc, NULL); | |
66fa16e6 | 12850 | mpfr_from_real (m, ra, GMP_RNDN); |
d4473c84 | 12851 | mpfr_clear_flags (); |
d92f994c | 12852 | inexact = mpfr_sin_cos (ms, mc, m, GMP_RNDN); |
12853 | result_s = do_mpfr_ckconv (ms, type, inexact); | |
12854 | result_c = do_mpfr_ckconv (mc, type, inexact); | |
12855 | mpfr_clears (m, ms, mc, NULL); | |
12856 | if (result_s && result_c) | |
12857 | { | |
d735c391 | 12858 | /* If we are to return in a complex value do so. */ |
12859 | if (!arg_sinp && !arg_cosp) | |
12860 | return build_complex (build_complex_type (type), | |
12861 | result_c, result_s); | |
12862 | ||
d92f994c | 12863 | /* Dereference the sin/cos pointer arguments. */ |
12864 | arg_sinp = build_fold_indirect_ref (arg_sinp); | |
12865 | arg_cosp = build_fold_indirect_ref (arg_cosp); | |
12866 | /* Proceed if valid pointer type were passed in. */ | |
12867 | if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type) | |
12868 | && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type)) | |
12869 | { | |
12870 | /* Set the values. */ | |
41076ef6 | 12871 | result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp, |
35cc02b5 | 12872 | result_s); |
d92f994c | 12873 | TREE_SIDE_EFFECTS (result_s) = 1; |
41076ef6 | 12874 | result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp, |
35cc02b5 | 12875 | result_c); |
d92f994c | 12876 | TREE_SIDE_EFFECTS (result_c) = 1; |
12877 | /* Combine the assignments into a compound expr. */ | |
12878 | result = non_lvalue (fold_build2 (COMPOUND_EXPR, type, | |
12879 | result_s, result_c)); | |
12880 | } | |
12881 | } | |
12882 | } | |
12883 | } | |
12884 | return result; | |
12885 | } | |
65dd1378 | 12886 | |
12887 | #if MPFR_VERSION >= MPFR_VERSION_NUM(2,3,0) | |
12888 | /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the | |
12889 | two-argument mpfr order N Bessel function FUNC on them and return | |
12890 | the resulting value as a tree with type TYPE. The mpfr precision | |
12891 | is set to the precision of TYPE. We assume that function FUNC | |
12892 | returns zero if the result could be calculated exactly within the | |
12893 | requested precision. */ | |
12894 | static tree | |
12895 | do_mpfr_bessel_n (tree arg1, tree arg2, tree type, | |
12896 | int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t), | |
12897 | const REAL_VALUE_TYPE *min, bool inclusive) | |
12898 | { | |
12899 | tree result = NULL_TREE; | |
12900 | ||
12901 | STRIP_NOPS (arg1); | |
12902 | STRIP_NOPS (arg2); | |
12903 | ||
12904 | /* To proceed, MPFR must exactly represent the target floating point | |
12905 | format, which only happens when the target base equals two. */ | |
12906 | if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2 | |
12907 | && host_integerp (arg1, 0) | |
12908 | && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)) | |
12909 | { | |
12910 | const HOST_WIDE_INT n = tree_low_cst(arg1, 0); | |
12911 | const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2); | |
12912 | ||
12913 | if (n == (long)n | |
776a7bab | 12914 | && real_isfinite (ra) |
65dd1378 | 12915 | && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))) |
12916 | { | |
12917 | const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p; | |
12918 | int inexact; | |
12919 | mpfr_t m; | |
12920 | ||
12921 | mpfr_init2 (m, prec); | |
12922 | mpfr_from_real (m, ra, GMP_RNDN); | |
12923 | mpfr_clear_flags (); | |
12924 | inexact = func (m, n, m, GMP_RNDN); | |
12925 | result = do_mpfr_ckconv (m, type, inexact); | |
12926 | mpfr_clear (m); | |
12927 | } | |
12928 | } | |
12929 | ||
12930 | return result; | |
12931 | } | |
e5407ca6 | 12932 | |
12933 | /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set | |
12934 | the pointer *(ARG_QUO) and return the result. The type is taken | |
12935 | from the type of ARG0 and is used for setting the precision of the | |
12936 | calculation and results. */ | |
12937 | ||
12938 | static tree | |
12939 | do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo) | |
12940 | { | |
12941 | tree const type = TREE_TYPE (arg0); | |
12942 | tree result = NULL_TREE; | |
12943 | ||
12944 | STRIP_NOPS (arg0); | |
12945 | STRIP_NOPS (arg1); | |
12946 | ||
12947 | /* To proceed, MPFR must exactly represent the target floating point | |
12948 | format, which only happens when the target base equals two. */ | |
12949 | if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2 | |
12950 | && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0) | |
12951 | && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)) | |
12952 | { | |
12953 | const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0); | |
12954 | const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1); | |
12955 | ||
776a7bab | 12956 | if (real_isfinite (ra0) && real_isfinite (ra1)) |
e5407ca6 | 12957 | { |
12958 | const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p; | |
12959 | tree result_rem; | |
12960 | long integer_quo; | |
12961 | mpfr_t m0, m1; | |
12962 | ||
12963 | mpfr_inits2 (prec, m0, m1, NULL); | |
12964 | mpfr_from_real (m0, ra0, GMP_RNDN); | |
12965 | mpfr_from_real (m1, ra1, GMP_RNDN); | |
12966 | mpfr_clear_flags (); | |
12967 | mpfr_remquo (m0, &integer_quo, m0, m1, GMP_RNDN); | |
12968 | /* Remquo is independent of the rounding mode, so pass | |
12969 | inexact=0 to do_mpfr_ckconv(). */ | |
12970 | result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0); | |
12971 | mpfr_clears (m0, m1, NULL); | |
12972 | if (result_rem) | |
12973 | { | |
12974 | /* MPFR calculates quo in the host's long so it may | |
12975 | return more bits in quo than the target int can hold | |
12976 | if sizeof(host long) > sizeof(target int). This can | |
12977 | happen even for native compilers in LP64 mode. In | |
12978 | these cases, modulo the quo value with the largest | |
12979 | number that the target int can hold while leaving one | |
12980 | bit for the sign. */ | |
12981 | if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE) | |
12982 | integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1)); | |
12983 | ||
12984 | /* Dereference the quo pointer argument. */ | |
12985 | arg_quo = build_fold_indirect_ref (arg_quo); | |
12986 | /* Proceed iff a valid pointer type was passed in. */ | |
12987 | if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node) | |
12988 | { | |
12989 | /* Set the value. */ | |
12990 | tree result_quo = fold_build2 (MODIFY_EXPR, | |
12991 | TREE_TYPE (arg_quo), arg_quo, | |
12992 | build_int_cst (NULL, integer_quo)); | |
12993 | TREE_SIDE_EFFECTS (result_quo) = 1; | |
12994 | /* Combine the quo assignment with the rem. */ | |
12995 | result = non_lvalue (fold_build2 (COMPOUND_EXPR, type, | |
12996 | result_quo, result_rem)); | |
12997 | } | |
12998 | } | |
12999 | } | |
13000 | } | |
13001 | return result; | |
13002 | } | |
e84da7c1 | 13003 | |
13004 | /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the | |
13005 | resulting value as a tree with type TYPE. The mpfr precision is | |
13006 | set to the precision of TYPE. We assume that this mpfr function | |
13007 | returns zero if the result could be calculated exactly within the | |
13008 | requested precision. In addition, the integer pointer represented | |
13009 | by ARG_SG will be dereferenced and set to the appropriate signgam | |
13010 | (-1,1) value. */ | |
13011 | ||
13012 | static tree | |
13013 | do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type) | |
13014 | { | |
13015 | tree result = NULL_TREE; | |
13016 | ||
13017 | STRIP_NOPS (arg); | |
13018 | ||
13019 | /* To proceed, MPFR must exactly represent the target floating point | |
13020 | format, which only happens when the target base equals two. Also | |
13021 | verify ARG is a constant and that ARG_SG is an int pointer. */ | |
13022 | if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2 | |
13023 | && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg) | |
13024 | && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE | |
13025 | && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node) | |
13026 | { | |
13027 | const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg); | |
13028 | ||
13029 | /* In addition to NaN and Inf, the argument cannot be zero or a | |
13030 | negative integer. */ | |
776a7bab | 13031 | if (real_isfinite (ra) |
e84da7c1 | 13032 | && ra->cl != rvc_zero |
13033 | && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type)))) | |
13034 | { | |
13035 | const int prec = REAL_MODE_FORMAT (TYPE_MODE (type))->p; | |
13036 | int inexact, sg; | |
13037 | mpfr_t m; | |
13038 | tree result_lg; | |
13039 | ||
13040 | mpfr_init2 (m, prec); | |
13041 | mpfr_from_real (m, ra, GMP_RNDN); | |
13042 | mpfr_clear_flags (); | |
13043 | inexact = mpfr_lgamma (m, &sg, m, GMP_RNDN); | |
13044 | result_lg = do_mpfr_ckconv (m, type, inexact); | |
13045 | mpfr_clear (m); | |
13046 | if (result_lg) | |
13047 | { | |
13048 | tree result_sg; | |
13049 | ||
13050 | /* Dereference the arg_sg pointer argument. */ | |
13051 | arg_sg = build_fold_indirect_ref (arg_sg); | |
13052 | /* Assign the signgam value into *arg_sg. */ | |
13053 | result_sg = fold_build2 (MODIFY_EXPR, | |
13054 | TREE_TYPE (arg_sg), arg_sg, | |
13055 | build_int_cst (NULL, sg)); | |
13056 | TREE_SIDE_EFFECTS (result_sg) = 1; | |
13057 | /* Combine the signgam assignment with the lgamma result. */ | |
13058 | result = non_lvalue (fold_build2 (COMPOUND_EXPR, type, | |
13059 | result_sg, result_lg)); | |
13060 | } | |
13061 | } | |
13062 | } | |
13063 | ||
13064 | return result; | |
13065 | } | |
65dd1378 | 13066 | #endif |