]>
Commit | Line | Data |
---|---|---|
53800dbe | 1 | /* Expand builtin functions. |
61f1dccc | 2 | Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
c94cfd1c | 3 | 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. |
53800dbe | 4 | |
f12b58b3 | 5 | This file is part of GCC. |
53800dbe | 6 | |
f12b58b3 | 7 | GCC is free software; you can redistribute it and/or modify it under |
8 | the terms of the GNU General Public License as published by the Free | |
9 | Software Foundation; either version 2, or (at your option) any later | |
10 | version. | |
53800dbe | 11 | |
f12b58b3 | 12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
53800dbe | 16 | |
17 | You should have received a copy of the GNU General Public License | |
f12b58b3 | 18 | along with GCC; see the file COPYING. If not, write to the Free |
67ce556b | 19 | Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA |
20 | 02110-1301, USA. */ | |
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" |
53800dbe | 50 | |
726e2588 | 51 | #ifndef PAD_VARARGS_DOWN |
52 | #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN | |
53 | #endif | |
54 | ||
ab7943b9 | 55 | /* Define the names of the builtin function types and codes. */ |
8934cb0c | 56 | const char *const built_in_class_names[4] |
ab7943b9 | 57 | = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"}; |
58 | ||
9cfddb70 | 59 | #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X, |
0dfc45b5 | 60 | const char * built_in_names[(int) END_BUILTINS] = |
4e9d90c7 | 61 | { |
62 | #include "builtins.def" | |
63 | }; | |
64 | #undef DEF_BUILTIN | |
ab7943b9 | 65 | |
df94cd3b | 66 | /* Setup an array of _DECL trees, make sure each element is |
67 | initialized to NULL_TREE. */ | |
d2d4bdde | 68 | tree built_in_decls[(int) END_BUILTINS]; |
0a68165a | 69 | /* Declarations used when constructing the builtin implicitly in the compiler. |
70 | It may be NULL_TREE when this is invalid (for instance runtime is not | |
4ee9c684 | 71 | required to implement the function call in all cases). */ |
0a68165a | 72 | tree implicit_built_in_decls[(int) END_BUILTINS]; |
df94cd3b | 73 | |
aecda0d6 | 74 | static int get_pointer_alignment (tree, unsigned int); |
aecda0d6 | 75 | static const char *c_getstr (tree); |
76 | static rtx c_readstr (const char *, enum machine_mode); | |
77 | static int target_char_cast (tree, char *); | |
d8ae1baa | 78 | static rtx get_memory_rtx (tree, tree); |
aecda0d6 | 79 | static int apply_args_size (void); |
80 | static int apply_result_size (void); | |
d8c9779c | 81 | #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return) |
aecda0d6 | 82 | static rtx result_vector (int, rtx); |
d8c9779c | 83 | #endif |
843d08a9 | 84 | static void expand_builtin_update_setjmp_buf (rtx); |
aecda0d6 | 85 | static void expand_builtin_prefetch (tree); |
86 | static rtx expand_builtin_apply_args (void); | |
87 | static rtx expand_builtin_apply_args_1 (void); | |
88 | static rtx expand_builtin_apply (rtx, rtx, rtx); | |
89 | static void expand_builtin_return (rtx); | |
90 | static enum type_class type_to_class (tree); | |
91 | static rtx expand_builtin_classify_type (tree); | |
92 | static void expand_errno_check (tree, rtx); | |
93 | static rtx expand_builtin_mathfn (tree, rtx, rtx); | |
94 | static rtx expand_builtin_mathfn_2 (tree, rtx, rtx); | |
6b43bae4 | 95 | static rtx expand_builtin_mathfn_3 (tree, rtx, rtx); |
c3147c1a | 96 | static rtx expand_builtin_sincos (tree); |
ad52b9b7 | 97 | static rtx expand_builtin_int_roundingfn (tree, rtx, rtx); |
7d3afc77 | 98 | static rtx expand_builtin_int_roundingfn_2 (tree, rtx, rtx); |
aecda0d6 | 99 | static rtx expand_builtin_args_info (tree); |
79012a9d | 100 | static rtx expand_builtin_next_arg (void); |
aecda0d6 | 101 | static rtx expand_builtin_va_start (tree); |
102 | static rtx expand_builtin_va_end (tree); | |
103 | static rtx expand_builtin_va_copy (tree); | |
104 | static rtx expand_builtin_memcmp (tree, tree, rtx, enum machine_mode); | |
105 | static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode); | |
106 | static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode); | |
107 | static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode); | |
7da1412b | 108 | static rtx expand_builtin_strcat (tree, tree, rtx, enum machine_mode); |
aecda0d6 | 109 | static rtx expand_builtin_strncat (tree, rtx, enum machine_mode); |
110 | static rtx expand_builtin_strspn (tree, rtx, enum machine_mode); | |
111 | static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode); | |
112 | static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode); | |
7da1412b | 113 | static rtx expand_builtin_mempcpy (tree, tree, rtx, enum machine_mode, int); |
3b1757a2 | 114 | static rtx expand_builtin_memmove (tree, tree, rtx, enum machine_mode); |
0b25db21 | 115 | static rtx expand_builtin_bcopy (tree); |
1d89860b | 116 | static rtx expand_builtin_strcpy (tree, tree, rtx, enum machine_mode); |
aecda0d6 | 117 | static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode); |
118 | static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode); | |
119 | static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode); | |
120 | static rtx builtin_memset_read_str (void *, HOST_WIDE_INT, enum machine_mode); | |
121 | static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode); | |
0b25db21 | 122 | static rtx expand_builtin_memset (tree, rtx, enum machine_mode, tree); |
aecda0d6 | 123 | static rtx expand_builtin_bzero (tree); |
124 | static rtx expand_builtin_strlen (tree, rtx, enum machine_mode); | |
daa1d5f5 | 125 | static rtx expand_builtin_strstr (tree, tree, rtx, enum machine_mode); |
126 | static rtx expand_builtin_strpbrk (tree, tree, rtx, enum machine_mode); | |
127 | static rtx expand_builtin_strchr (tree, tree, rtx, enum machine_mode); | |
128 | static rtx expand_builtin_strrchr (tree, tree, rtx, enum machine_mode); | |
aecda0d6 | 129 | static rtx expand_builtin_alloca (tree, rtx); |
130 | static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab); | |
131 | static rtx expand_builtin_frame_address (tree, tree); | |
19bf118a | 132 | static rtx expand_builtin_fputs (tree, rtx, bool); |
133 | static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool); | |
134 | static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool); | |
6411575e | 135 | static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode); |
aecda0d6 | 136 | static tree stabilize_va_list (tree, int); |
137 | static rtx expand_builtin_expect (tree, rtx); | |
138 | static tree fold_builtin_constant_p (tree); | |
139 | static tree fold_builtin_classify_type (tree); | |
e6e27594 | 140 | static tree fold_builtin_strlen (tree); |
aecda0d6 | 141 | static tree fold_builtin_inf (tree, int); |
142 | static tree fold_builtin_nan (tree, tree, int); | |
143 | static int validate_arglist (tree, ...); | |
277f8dd2 | 144 | static bool integer_valued_real_p (tree); |
76b9b24b | 145 | static tree fold_trunc_transparent_mathfn (tree, tree); |
aecda0d6 | 146 | static bool readonly_data_expr (tree); |
147 | static rtx expand_builtin_fabs (tree, rtx, rtx); | |
27f261ef | 148 | static rtx expand_builtin_signbit (tree, rtx); |
e6e27594 | 149 | static tree fold_builtin_sqrt (tree, tree); |
150 | static tree fold_builtin_cbrt (tree, tree); | |
151 | static tree fold_builtin_pow (tree, tree, tree); | |
b4d0c20c | 152 | static tree fold_builtin_powi (tree, tree, tree); |
e6e27594 | 153 | static tree fold_builtin_sin (tree); |
154 | static tree fold_builtin_cos (tree, tree, tree); | |
155 | static tree fold_builtin_tan (tree); | |
ea67c510 | 156 | static tree fold_builtin_atan (tree, tree); |
7169af54 | 157 | static tree fold_builtin_trunc (tree, tree); |
158 | static tree fold_builtin_floor (tree, tree); | |
159 | static tree fold_builtin_ceil (tree, tree); | |
160 | static tree fold_builtin_round (tree, tree); | |
ad52b9b7 | 161 | static tree fold_builtin_int_roundingfn (tree, tree); |
10b9666f | 162 | static tree fold_builtin_bitop (tree, tree); |
4f46f2b9 | 163 | static tree fold_builtin_memory_op (tree, tree, bool, int); |
daa1d5f5 | 164 | static tree fold_builtin_strchr (tree, tree); |
9c8a1629 | 165 | static tree fold_builtin_memcmp (tree); |
166 | static tree fold_builtin_strcmp (tree); | |
167 | static tree fold_builtin_strncmp (tree); | |
64c18e8d | 168 | static tree fold_builtin_signbit (tree, tree); |
198d9bbe | 169 | static tree fold_builtin_copysign (tree, tree, tree); |
467214fd | 170 | static tree fold_builtin_isascii (tree); |
171 | static tree fold_builtin_toascii (tree); | |
172 | static tree fold_builtin_isdigit (tree); | |
d1aade50 | 173 | static tree fold_builtin_fabs (tree, tree); |
174 | static tree fold_builtin_abs (tree, tree); | |
d5019fe8 | 175 | static tree fold_builtin_unordered_cmp (tree, tree, enum tree_code, |
176 | enum tree_code); | |
3d3d84b8 | 177 | static tree fold_builtin_1 (tree, tree, bool); |
98b40778 | 178 | |
daa1d5f5 | 179 | static tree fold_builtin_strpbrk (tree, tree); |
180 | static tree fold_builtin_strstr (tree, tree); | |
181 | static tree fold_builtin_strrchr (tree, tree); | |
7c2f0500 | 182 | static tree fold_builtin_strcat (tree); |
183 | static tree fold_builtin_strncat (tree); | |
184 | static tree fold_builtin_strspn (tree); | |
185 | static tree fold_builtin_strcspn (tree); | |
7c2f0500 | 186 | static tree fold_builtin_sprintf (tree, int); |
4ee9c684 | 187 | |
0a39fd54 | 188 | static rtx expand_builtin_object_size (tree); |
189 | static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode, | |
190 | enum built_in_function); | |
191 | static void maybe_emit_chk_warning (tree, enum built_in_function); | |
192 | static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function); | |
193 | static tree fold_builtin_object_size (tree); | |
194 | static tree fold_builtin_strcat_chk (tree, tree); | |
195 | static tree fold_builtin_strncat_chk (tree, tree); | |
196 | static tree fold_builtin_sprintf_chk (tree, enum built_in_function); | |
197 | static tree fold_builtin_printf (tree, tree, bool, enum built_in_function); | |
198 | static tree fold_builtin_fprintf (tree, tree, bool, enum built_in_function); | |
99eabcc1 | 199 | static bool init_target_chars (void); |
200 | ||
201 | static unsigned HOST_WIDE_INT target_newline; | |
202 | static unsigned HOST_WIDE_INT target_percent; | |
203 | static unsigned HOST_WIDE_INT target_c; | |
204 | static unsigned HOST_WIDE_INT target_s; | |
205 | static char target_percent_c[3]; | |
206 | static char target_percent_s[3]; | |
207 | static char target_percent_s_newline[4]; | |
0a39fd54 | 208 | |
b6a5fc45 | 209 | /* Return true if NODE should be considered for inline expansion regardless |
210 | of the optimization level. This means whenever a function is invoked with | |
211 | its "internal" name, which normally contains the prefix "__builtin". */ | |
212 | ||
213 | static bool called_as_built_in (tree node) | |
214 | { | |
215 | const char *name = IDENTIFIER_POINTER (DECL_NAME (node)); | |
216 | if (strncmp (name, "__builtin_", 10) == 0) | |
217 | return true; | |
218 | if (strncmp (name, "__sync_", 7) == 0) | |
219 | return true; | |
220 | return false; | |
221 | } | |
4ee9c684 | 222 | |
53800dbe | 223 | /* Return the alignment in bits of EXP, a pointer valued expression. |
224 | But don't return more than MAX_ALIGN no matter what. | |
225 | The alignment returned is, by default, the alignment of the thing that | |
27d0c333 | 226 | EXP points to. If it is not a POINTER_TYPE, 0 is returned. |
53800dbe | 227 | |
228 | Otherwise, look at the expression to see if we can do better, i.e., if the | |
229 | expression is actually pointing at an object whose alignment is tighter. */ | |
230 | ||
231 | static int | |
aecda0d6 | 232 | get_pointer_alignment (tree exp, unsigned int max_align) |
53800dbe | 233 | { |
27d0c333 | 234 | unsigned int align, inner; |
53800dbe | 235 | |
189575ff | 236 | /* We rely on TER to compute accurate alignment information. */ |
237 | if (!(optimize && flag_tree_ter)) | |
238 | return 0; | |
239 | ||
535e2026 | 240 | if (!POINTER_TYPE_P (TREE_TYPE (exp))) |
241 | return 0; | |
242 | ||
53800dbe | 243 | align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp))); |
244 | align = MIN (align, max_align); | |
245 | ||
246 | while (1) | |
247 | { | |
248 | switch (TREE_CODE (exp)) | |
249 | { | |
250 | case NOP_EXPR: | |
251 | case CONVERT_EXPR: | |
252 | case NON_LVALUE_EXPR: | |
253 | exp = TREE_OPERAND (exp, 0); | |
552752f7 | 254 | if (! POINTER_TYPE_P (TREE_TYPE (exp))) |
53800dbe | 255 | return align; |
325d1c45 | 256 | |
53800dbe | 257 | inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp))); |
258 | align = MIN (inner, max_align); | |
259 | break; | |
260 | ||
261 | case PLUS_EXPR: | |
262 | /* If sum of pointer + int, restrict our maximum alignment to that | |
263 | imposed by the integer. If not, we can't do any better than | |
264 | ALIGN. */ | |
325d1c45 | 265 | if (! host_integerp (TREE_OPERAND (exp, 1), 1)) |
53800dbe | 266 | return align; |
267 | ||
0994d2ed | 268 | while (((tree_low_cst (TREE_OPERAND (exp, 1), 1)) |
269 | & (max_align / BITS_PER_UNIT - 1)) | |
53800dbe | 270 | != 0) |
271 | max_align >>= 1; | |
272 | ||
273 | exp = TREE_OPERAND (exp, 0); | |
274 | break; | |
275 | ||
276 | case ADDR_EXPR: | |
277 | /* See what we are pointing at and look at its alignment. */ | |
278 | exp = TREE_OPERAND (exp, 0); | |
3c15318e | 279 | inner = max_align; |
d2df7679 | 280 | if (handled_component_p (exp)) |
1eee24cf | 281 | { |
d2df7679 | 282 | HOST_WIDE_INT bitsize, bitpos; |
283 | tree offset; | |
284 | enum machine_mode mode; | |
285 | int unsignedp, volatilep; | |
286 | ||
287 | exp = get_inner_reference (exp, &bitsize, &bitpos, &offset, | |
288 | &mode, &unsignedp, &volatilep, true); | |
289 | if (bitpos) | |
290 | inner = MIN (inner, (unsigned) (bitpos & -bitpos)); | |
291 | if (offset && TREE_CODE (offset) == PLUS_EXPR | |
292 | && host_integerp (TREE_OPERAND (offset, 1), 1)) | |
293 | { | |
294 | /* Any overflow in calculating offset_bits won't change | |
295 | the alignment. */ | |
296 | unsigned offset_bits | |
297 | = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1) | |
298 | * BITS_PER_UNIT); | |
299 | ||
300 | if (offset_bits) | |
301 | inner = MIN (inner, (offset_bits & -offset_bits)); | |
302 | offset = TREE_OPERAND (offset, 0); | |
303 | } | |
304 | if (offset && TREE_CODE (offset) == MULT_EXPR | |
305 | && host_integerp (TREE_OPERAND (offset, 1), 1)) | |
306 | { | |
307 | /* Any overflow in calculating offset_factor won't change | |
308 | the alignment. */ | |
309 | unsigned offset_factor | |
310 | = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1) | |
311 | * BITS_PER_UNIT); | |
312 | ||
313 | if (offset_factor) | |
314 | inner = MIN (inner, (offset_factor & -offset_factor)); | |
315 | } | |
316 | else if (offset) | |
317 | inner = MIN (inner, BITS_PER_UNIT); | |
1eee24cf | 318 | } |
53800dbe | 319 | if (TREE_CODE (exp) == FUNCTION_DECL) |
3c15318e | 320 | align = FUNCTION_BOUNDARY; |
9308e976 | 321 | else if (DECL_P (exp)) |
3c15318e | 322 | align = MIN (inner, DECL_ALIGN (exp)); |
53800dbe | 323 | #ifdef CONSTANT_ALIGNMENT |
ce45a448 | 324 | else if (CONSTANT_CLASS_P (exp)) |
e068b646 | 325 | align = MIN (inner, (unsigned)CONSTANT_ALIGNMENT (exp, align)); |
53800dbe | 326 | #endif |
d2df7679 | 327 | else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR |
328 | || TREE_CODE (exp) == INDIRECT_REF) | |
329 | align = MIN (TYPE_ALIGN (TREE_TYPE (exp)), inner); | |
3c15318e | 330 | else |
331 | align = MIN (align, inner); | |
332 | return MIN (align, max_align); | |
53800dbe | 333 | |
334 | default: | |
335 | return align; | |
336 | } | |
337 | } | |
338 | } | |
339 | ||
340 | /* Compute the length of a C string. TREE_STRING_LENGTH is not the right | |
341 | way, because it could contain a zero byte in the middle. | |
342 | TREE_STRING_LENGTH is the size of the character array, not the string. | |
343 | ||
4172d65e | 344 | ONLY_VALUE should be nonzero if the result is not going to be emitted |
c09841f6 | 345 | into the instruction stream and zero if it is going to be expanded. |
4172d65e | 346 | E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3 |
681fab1e | 347 | is returned, otherwise NULL, since |
348 | len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not | |
349 | evaluate the side-effects. | |
350 | ||
902de8ed | 351 | The value returned is of type `ssizetype'. |
352 | ||
53800dbe | 353 | Unfortunately, string_constant can't access the values of const char |
354 | arrays with initializers, so neither can we do so here. */ | |
355 | ||
4ee9c684 | 356 | tree |
681fab1e | 357 | c_strlen (tree src, int only_value) |
53800dbe | 358 | { |
359 | tree offset_node; | |
27d0c333 | 360 | HOST_WIDE_INT offset; |
361 | int max; | |
44acf429 | 362 | const char *ptr; |
53800dbe | 363 | |
681fab1e | 364 | STRIP_NOPS (src); |
365 | if (TREE_CODE (src) == COND_EXPR | |
366 | && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0)))) | |
367 | { | |
368 | tree len1, len2; | |
369 | ||
370 | len1 = c_strlen (TREE_OPERAND (src, 1), only_value); | |
371 | len2 = c_strlen (TREE_OPERAND (src, 2), only_value); | |
0862b7e9 | 372 | if (tree_int_cst_equal (len1, len2)) |
681fab1e | 373 | return len1; |
374 | } | |
375 | ||
376 | if (TREE_CODE (src) == COMPOUND_EXPR | |
377 | && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0)))) | |
378 | return c_strlen (TREE_OPERAND (src, 1), only_value); | |
379 | ||
53800dbe | 380 | src = string_constant (src, &offset_node); |
381 | if (src == 0) | |
382 | return 0; | |
902de8ed | 383 | |
83d79705 | 384 | max = TREE_STRING_LENGTH (src) - 1; |
53800dbe | 385 | ptr = TREE_STRING_POINTER (src); |
902de8ed | 386 | |
53800dbe | 387 | if (offset_node && TREE_CODE (offset_node) != INTEGER_CST) |
388 | { | |
389 | /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't | |
390 | compute the offset to the following null if we don't know where to | |
391 | start searching for it. */ | |
392 | int i; | |
902de8ed | 393 | |
53800dbe | 394 | for (i = 0; i < max; i++) |
395 | if (ptr[i] == 0) | |
396 | return 0; | |
902de8ed | 397 | |
53800dbe | 398 | /* We don't know the starting offset, but we do know that the string |
399 | has no internal zero bytes. We can assume that the offset falls | |
400 | within the bounds of the string; otherwise, the programmer deserves | |
401 | what he gets. Subtract the offset from the length of the string, | |
902de8ed | 402 | and return that. This would perhaps not be valid if we were dealing |
403 | with named arrays in addition to literal string constants. */ | |
404 | ||
405 | return size_diffop (size_int (max), offset_node); | |
53800dbe | 406 | } |
407 | ||
408 | /* We have a known offset into the string. Start searching there for | |
27d0c333 | 409 | a null character if we can represent it as a single HOST_WIDE_INT. */ |
dabc4084 | 410 | if (offset_node == 0) |
53800dbe | 411 | offset = 0; |
dabc4084 | 412 | else if (! host_integerp (offset_node, 0)) |
413 | offset = -1; | |
53800dbe | 414 | else |
27d0c333 | 415 | offset = tree_low_cst (offset_node, 0); |
902de8ed | 416 | |
53800dbe | 417 | /* If the offset is known to be out of bounds, warn, and call strlen at |
418 | runtime. */ | |
419 | if (offset < 0 || offset > max) | |
420 | { | |
c3ceba8e | 421 | warning (0, "offset outside bounds of constant string"); |
53800dbe | 422 | return 0; |
423 | } | |
902de8ed | 424 | |
53800dbe | 425 | /* Use strlen to search for the first zero byte. Since any strings |
426 | constructed with build_string will have nulls appended, we win even | |
427 | if we get handed something like (char[4])"abcd". | |
428 | ||
429 | Since OFFSET is our starting index into the string, no further | |
430 | calculation is needed. */ | |
902de8ed | 431 | return ssize_int (strlen (ptr + offset)); |
53800dbe | 432 | } |
433 | ||
83d79705 | 434 | /* Return a char pointer for a C string if it is a string constant |
435 | or sum of string constant and integer constant. */ | |
436 | ||
437 | static const char * | |
aecda0d6 | 438 | c_getstr (tree src) |
83d79705 | 439 | { |
440 | tree offset_node; | |
83d79705 | 441 | |
442 | src = string_constant (src, &offset_node); | |
443 | if (src == 0) | |
444 | return 0; | |
445 | ||
8c85fcb7 | 446 | if (offset_node == 0) |
447 | return TREE_STRING_POINTER (src); | |
448 | else if (!host_integerp (offset_node, 1) | |
449 | || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0) | |
83d79705 | 450 | return 0; |
83d79705 | 451 | |
8c85fcb7 | 452 | return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1); |
83d79705 | 453 | } |
454 | ||
8c85fcb7 | 455 | /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading |
456 | GET_MODE_BITSIZE (MODE) bits from string constant STR. */ | |
ecc318ff | 457 | |
6840589f | 458 | static rtx |
aecda0d6 | 459 | c_readstr (const char *str, enum machine_mode mode) |
6840589f | 460 | { |
461 | HOST_WIDE_INT c[2]; | |
462 | HOST_WIDE_INT ch; | |
463 | unsigned int i, j; | |
464 | ||
64db345d | 465 | gcc_assert (GET_MODE_CLASS (mode) == MODE_INT); |
7d3f6cc7 | 466 | |
6840589f | 467 | c[0] = 0; |
468 | c[1] = 0; | |
469 | ch = 1; | |
470 | for (i = 0; i < GET_MODE_SIZE (mode); i++) | |
471 | { | |
472 | j = i; | |
473 | if (WORDS_BIG_ENDIAN) | |
474 | j = GET_MODE_SIZE (mode) - i - 1; | |
475 | if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN | |
476 | && GET_MODE_SIZE (mode) > UNITS_PER_WORD) | |
477 | j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1; | |
478 | j *= BITS_PER_UNIT; | |
64db345d | 479 | gcc_assert (j <= 2 * HOST_BITS_PER_WIDE_INT); |
7d3f6cc7 | 480 | |
6840589f | 481 | if (ch) |
482 | ch = (unsigned char) str[i]; | |
483 | c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT); | |
484 | } | |
485 | return immed_double_const (c[0], c[1], mode); | |
486 | } | |
487 | ||
ecc318ff | 488 | /* Cast a target constant CST to target CHAR and if that value fits into |
5206b159 | 489 | host char type, return zero and put that value into variable pointed to by |
ecc318ff | 490 | P. */ |
491 | ||
492 | static int | |
aecda0d6 | 493 | target_char_cast (tree cst, char *p) |
ecc318ff | 494 | { |
495 | unsigned HOST_WIDE_INT val, hostval; | |
496 | ||
27d0c333 | 497 | if (!host_integerp (cst, 1) |
ecc318ff | 498 | || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT) |
499 | return 1; | |
500 | ||
27d0c333 | 501 | val = tree_low_cst (cst, 1); |
ecc318ff | 502 | if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT) |
503 | val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1; | |
504 | ||
505 | hostval = val; | |
506 | if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT) | |
507 | hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1; | |
508 | ||
509 | if (val != hostval) | |
510 | return 1; | |
511 | ||
512 | *p = hostval; | |
513 | return 0; | |
514 | } | |
515 | ||
4ee9c684 | 516 | /* Similar to save_expr, but assumes that arbitrary code is not executed |
517 | in between the multiple evaluations. In particular, we assume that a | |
518 | non-addressable local variable will not be modified. */ | |
519 | ||
520 | static tree | |
521 | builtin_save_expr (tree exp) | |
522 | { | |
523 | if (TREE_ADDRESSABLE (exp) == 0 | |
524 | && (TREE_CODE (exp) == PARM_DECL | |
525 | || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))) | |
526 | return exp; | |
527 | ||
528 | return save_expr (exp); | |
529 | } | |
530 | ||
53800dbe | 531 | /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT |
532 | times to get the address of either a higher stack frame, or a return | |
533 | address located within it (depending on FNDECL_CODE). */ | |
902de8ed | 534 | |
c626df3d | 535 | static rtx |
869d0ef0 | 536 | expand_builtin_return_addr (enum built_in_function fndecl_code, int count) |
53800dbe | 537 | { |
538 | int i; | |
539 | ||
869d0ef0 | 540 | #ifdef INITIAL_FRAME_ADDRESS_RTX |
541 | rtx tem = INITIAL_FRAME_ADDRESS_RTX; | |
542 | #else | |
e3e15c50 | 543 | rtx tem; |
544 | ||
1b74fde7 | 545 | /* For a zero count with __builtin_return_address, we don't care what |
546 | frame address we return, because target-specific definitions will | |
547 | override us. Therefore frame pointer elimination is OK, and using | |
548 | the soft frame pointer is OK. | |
549 | ||
550 | For a non-zero count, or a zero count with __builtin_frame_address, | |
551 | we require a stable offset from the current frame pointer to the | |
552 | previous one, so we must use the hard frame pointer, and | |
e3e15c50 | 553 | we must disable frame pointer elimination. */ |
1b74fde7 | 554 | if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS) |
e3e15c50 | 555 | tem = frame_pointer_rtx; |
a0c938f0 | 556 | else |
e3e15c50 | 557 | { |
558 | tem = hard_frame_pointer_rtx; | |
559 | ||
560 | /* Tell reload not to eliminate the frame pointer. */ | |
561 | current_function_accesses_prior_frames = 1; | |
562 | } | |
869d0ef0 | 563 | #endif |
564 | ||
53800dbe | 565 | /* Some machines need special handling before we can access |
3a69c60c | 566 | arbitrary frames. For example, on the SPARC, we must first flush |
53800dbe | 567 | all register windows to the stack. */ |
568 | #ifdef SETUP_FRAME_ADDRESSES | |
569 | if (count > 0) | |
570 | SETUP_FRAME_ADDRESSES (); | |
571 | #endif | |
572 | ||
3a69c60c | 573 | /* On the SPARC, the return address is not in the frame, it is in a |
53800dbe | 574 | register. There is no way to access it off of the current frame |
575 | pointer, but it can be accessed off the previous frame pointer by | |
576 | reading the value from the register window save area. */ | |
577 | #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME | |
578 | if (fndecl_code == BUILT_IN_RETURN_ADDRESS) | |
579 | count--; | |
580 | #endif | |
581 | ||
582 | /* Scan back COUNT frames to the specified frame. */ | |
583 | for (i = 0; i < count; i++) | |
584 | { | |
585 | /* Assume the dynamic chain pointer is in the word that the | |
586 | frame address points to, unless otherwise specified. */ | |
587 | #ifdef DYNAMIC_CHAIN_ADDRESS | |
588 | tem = DYNAMIC_CHAIN_ADDRESS (tem); | |
589 | #endif | |
590 | tem = memory_address (Pmode, tem); | |
00060fc2 | 591 | tem = gen_frame_mem (Pmode, tem); |
83fc1478 | 592 | tem = copy_to_reg (tem); |
53800dbe | 593 | } |
594 | ||
3a69c60c | 595 | /* For __builtin_frame_address, return what we've got. But, on |
596 | the SPARC for example, we may have to add a bias. */ | |
53800dbe | 597 | if (fndecl_code == BUILT_IN_FRAME_ADDRESS) |
3a69c60c | 598 | #ifdef FRAME_ADDR_RTX |
599 | return FRAME_ADDR_RTX (tem); | |
600 | #else | |
53800dbe | 601 | return tem; |
3a69c60c | 602 | #endif |
53800dbe | 603 | |
3a69c60c | 604 | /* For __builtin_return_address, get the return address from that frame. */ |
53800dbe | 605 | #ifdef RETURN_ADDR_RTX |
606 | tem = RETURN_ADDR_RTX (count, tem); | |
607 | #else | |
608 | tem = memory_address (Pmode, | |
609 | plus_constant (tem, GET_MODE_SIZE (Pmode))); | |
00060fc2 | 610 | tem = gen_frame_mem (Pmode, tem); |
53800dbe | 611 | #endif |
612 | return tem; | |
613 | } | |
614 | ||
f7c44134 | 615 | /* Alias set used for setjmp buffer. */ |
616 | static HOST_WIDE_INT setjmp_alias_set = -1; | |
617 | ||
6b7f6858 | 618 | /* Construct the leading half of a __builtin_setjmp call. Control will |
2c8a1497 | 619 | return to RECEIVER_LABEL. This is also called directly by the SJLJ |
620 | exception handling code. */ | |
53800dbe | 621 | |
6b7f6858 | 622 | void |
aecda0d6 | 623 | expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label) |
53800dbe | 624 | { |
53800dbe | 625 | enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL); |
53800dbe | 626 | rtx stack_save; |
f7c44134 | 627 | rtx mem; |
53800dbe | 628 | |
f7c44134 | 629 | if (setjmp_alias_set == -1) |
630 | setjmp_alias_set = new_alias_set (); | |
631 | ||
85d654dd | 632 | buf_addr = convert_memory_address (Pmode, buf_addr); |
53800dbe | 633 | |
37ae8504 | 634 | buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX)); |
53800dbe | 635 | |
6b7f6858 | 636 | /* We store the frame pointer and the address of receiver_label in |
637 | the buffer and use the rest of it for the stack save area, which | |
638 | is machine-dependent. */ | |
53800dbe | 639 | |
f7c44134 | 640 | mem = gen_rtx_MEM (Pmode, buf_addr); |
ab6ab77e | 641 | set_mem_alias_set (mem, setjmp_alias_set); |
e3e026e8 | 642 | emit_move_insn (mem, targetm.builtin_setjmp_frame_value ()); |
f7c44134 | 643 | |
644 | mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))), | |
ab6ab77e | 645 | set_mem_alias_set (mem, setjmp_alias_set); |
f7c44134 | 646 | |
647 | emit_move_insn (validize_mem (mem), | |
6b7f6858 | 648 | force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label))); |
53800dbe | 649 | |
650 | stack_save = gen_rtx_MEM (sa_mode, | |
651 | plus_constant (buf_addr, | |
652 | 2 * GET_MODE_SIZE (Pmode))); | |
ab6ab77e | 653 | set_mem_alias_set (stack_save, setjmp_alias_set); |
53800dbe | 654 | emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX); |
655 | ||
656 | /* If there is further processing to do, do it. */ | |
657 | #ifdef HAVE_builtin_setjmp_setup | |
658 | if (HAVE_builtin_setjmp_setup) | |
659 | emit_insn (gen_builtin_setjmp_setup (buf_addr)); | |
660 | #endif | |
661 | ||
6b7f6858 | 662 | /* Tell optimize_save_area_alloca that extra work is going to |
663 | need to go on during alloca. */ | |
3b0fa6b6 | 664 | current_function_calls_setjmp = 1; |
80ab81b9 | 665 | |
666 | /* Set this so all the registers get saved in our frame; we need to be | |
2c0e001b | 667 | able to copy the saved values for any registers from frames we unwind. */ |
80ab81b9 | 668 | current_function_has_nonlocal_label = 1; |
6b7f6858 | 669 | } |
53800dbe | 670 | |
2c8a1497 | 671 | /* Construct the trailing part of a __builtin_setjmp call. This is |
672 | also called directly by the SJLJ exception handling code. */ | |
6b7f6858 | 673 | |
674 | void | |
aecda0d6 | 675 | expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED) |
6b7f6858 | 676 | { |
53800dbe | 677 | /* Clobber the FP when we get here, so we have to make sure it's |
678 | marked as used by this function. */ | |
679 | emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx)); | |
680 | ||
681 | /* Mark the static chain as clobbered here so life information | |
682 | doesn't get messed up for it. */ | |
683 | emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx)); | |
684 | ||
685 | /* Now put in the code to restore the frame pointer, and argument | |
491e04ef | 686 | pointer, if needed. */ |
53800dbe | 687 | #ifdef HAVE_nonlocal_goto |
688 | if (! HAVE_nonlocal_goto) | |
689 | #endif | |
5a1c3f40 | 690 | { |
691 | emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx); | |
692 | /* This might change the hard frame pointer in ways that aren't | |
693 | apparent to early optimization passes, so force a clobber. */ | |
694 | emit_insn (gen_rtx_CLOBBER (VOIDmode, hard_frame_pointer_rtx)); | |
695 | } | |
53800dbe | 696 | |
697 | #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM | |
698 | if (fixed_regs[ARG_POINTER_REGNUM]) | |
699 | { | |
700 | #ifdef ELIMINABLE_REGS | |
701 | size_t i; | |
e99c3a1d | 702 | static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS; |
53800dbe | 703 | |
3098b2d3 | 704 | for (i = 0; i < ARRAY_SIZE (elim_regs); i++) |
53800dbe | 705 | if (elim_regs[i].from == ARG_POINTER_REGNUM |
706 | && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM) | |
707 | break; | |
708 | ||
3098b2d3 | 709 | if (i == ARRAY_SIZE (elim_regs)) |
53800dbe | 710 | #endif |
711 | { | |
712 | /* Now restore our arg pointer from the address at which it | |
05927e40 | 713 | was saved in our stack frame. */ |
53800dbe | 714 | emit_move_insn (virtual_incoming_args_rtx, |
05927e40 | 715 | copy_to_reg (get_arg_pointer_save_area (cfun))); |
53800dbe | 716 | } |
717 | } | |
718 | #endif | |
719 | ||
720 | #ifdef HAVE_builtin_setjmp_receiver | |
721 | if (HAVE_builtin_setjmp_receiver) | |
6b7f6858 | 722 | emit_insn (gen_builtin_setjmp_receiver (receiver_label)); |
53800dbe | 723 | else |
724 | #endif | |
725 | #ifdef HAVE_nonlocal_goto_receiver | |
726 | if (HAVE_nonlocal_goto_receiver) | |
727 | emit_insn (gen_nonlocal_goto_receiver ()); | |
728 | else | |
729 | #endif | |
6b7f6858 | 730 | { /* Nothing */ } |
57f6bb94 | 731 | |
732 | /* @@@ This is a kludge. Not all machine descriptions define a blockage | |
733 | insn, but we must not allow the code we just generated to be reordered | |
734 | by scheduling. Specifically, the update of the frame pointer must | |
735 | happen immediately, not later. So emit an ASM_INPUT to act as blockage | |
736 | insn. */ | |
737 | emit_insn (gen_rtx_ASM_INPUT (VOIDmode, "")); | |
6b7f6858 | 738 | } |
53800dbe | 739 | |
53800dbe | 740 | /* __builtin_longjmp is passed a pointer to an array of five words (not |
741 | all will be used on all machines). It operates similarly to the C | |
742 | library function of the same name, but is more efficient. Much of | |
2c8a1497 | 743 | the code below is copied from the handling of non-local gotos. */ |
53800dbe | 744 | |
c626df3d | 745 | static void |
aecda0d6 | 746 | expand_builtin_longjmp (rtx buf_addr, rtx value) |
53800dbe | 747 | { |
4712c7d6 | 748 | rtx fp, lab, stack, insn, last; |
53800dbe | 749 | enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL); |
750 | ||
f7c44134 | 751 | if (setjmp_alias_set == -1) |
752 | setjmp_alias_set = new_alias_set (); | |
753 | ||
85d654dd | 754 | buf_addr = convert_memory_address (Pmode, buf_addr); |
479e4d5e | 755 | |
53800dbe | 756 | buf_addr = force_reg (Pmode, buf_addr); |
757 | ||
758 | /* We used to store value in static_chain_rtx, but that fails if pointers | |
759 | are smaller than integers. We instead require that the user must pass | |
760 | a second argument of 1, because that is what builtin_setjmp will | |
761 | return. This also makes EH slightly more efficient, since we are no | |
762 | longer copying around a value that we don't care about. */ | |
64db345d | 763 | gcc_assert (value == const1_rtx); |
53800dbe | 764 | |
4712c7d6 | 765 | last = get_last_insn (); |
53800dbe | 766 | #ifdef HAVE_builtin_longjmp |
767 | if (HAVE_builtin_longjmp) | |
768 | emit_insn (gen_builtin_longjmp (buf_addr)); | |
769 | else | |
770 | #endif | |
771 | { | |
772 | fp = gen_rtx_MEM (Pmode, buf_addr); | |
773 | lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr, | |
774 | GET_MODE_SIZE (Pmode))); | |
775 | ||
776 | stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr, | |
777 | 2 * GET_MODE_SIZE (Pmode))); | |
ab6ab77e | 778 | set_mem_alias_set (fp, setjmp_alias_set); |
779 | set_mem_alias_set (lab, setjmp_alias_set); | |
780 | set_mem_alias_set (stack, setjmp_alias_set); | |
53800dbe | 781 | |
782 | /* Pick up FP, label, and SP from the block and jump. This code is | |
783 | from expand_goto in stmt.c; see there for detailed comments. */ | |
03fd9d2c | 784 | #ifdef HAVE_nonlocal_goto |
53800dbe | 785 | if (HAVE_nonlocal_goto) |
786 | /* We have to pass a value to the nonlocal_goto pattern that will | |
787 | get copied into the static_chain pointer, but it does not matter | |
788 | what that value is, because builtin_setjmp does not use it. */ | |
28d202a8 | 789 | emit_insn (gen_nonlocal_goto (value, lab, stack, fp)); |
53800dbe | 790 | else |
791 | #endif | |
792 | { | |
793 | lab = copy_to_reg (lab); | |
794 | ||
2a871ad1 | 795 | emit_insn (gen_rtx_CLOBBER (VOIDmode, |
796 | gen_rtx_MEM (BLKmode, | |
797 | gen_rtx_SCRATCH (VOIDmode)))); | |
798 | emit_insn (gen_rtx_CLOBBER (VOIDmode, | |
799 | gen_rtx_MEM (BLKmode, | |
800 | hard_frame_pointer_rtx))); | |
801 | ||
53800dbe | 802 | emit_move_insn (hard_frame_pointer_rtx, fp); |
803 | emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX); | |
804 | ||
805 | emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx)); | |
806 | emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx)); | |
807 | emit_indirect_jump (lab); | |
808 | } | |
809 | } | |
615166bb | 810 | |
811 | /* Search backwards and mark the jump insn as a non-local goto. | |
812 | Note that this precludes the use of __builtin_longjmp to a | |
813 | __builtin_setjmp target in the same function. However, we've | |
814 | already cautioned the user that these functions are for | |
815 | internal exception handling use only. */ | |
449c0509 | 816 | for (insn = get_last_insn (); insn; insn = PREV_INSN (insn)) |
817 | { | |
64db345d | 818 | gcc_assert (insn != last); |
7d3f6cc7 | 819 | |
6d7dc5b9 | 820 | if (JUMP_P (insn)) |
449c0509 | 821 | { |
822 | REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx, | |
823 | REG_NOTES (insn)); | |
824 | break; | |
825 | } | |
6d7dc5b9 | 826 | else if (CALL_P (insn)) |
9342ee68 | 827 | break; |
449c0509 | 828 | } |
53800dbe | 829 | } |
830 | ||
4ee9c684 | 831 | /* Expand a call to __builtin_nonlocal_goto. We're passed the target label |
832 | and the address of the save area. */ | |
833 | ||
834 | static rtx | |
835 | expand_builtin_nonlocal_goto (tree arglist) | |
836 | { | |
837 | tree t_label, t_save_area; | |
838 | rtx r_label, r_save_area, r_fp, r_sp, insn; | |
839 | ||
840 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
841 | return NULL_RTX; | |
842 | ||
843 | t_label = TREE_VALUE (arglist); | |
844 | arglist = TREE_CHAIN (arglist); | |
845 | t_save_area = TREE_VALUE (arglist); | |
846 | ||
8ec3c5c2 | 847 | r_label = expand_normal (t_label); |
3dce56cc | 848 | r_label = convert_memory_address (Pmode, r_label); |
8ec3c5c2 | 849 | r_save_area = expand_normal (t_save_area); |
3dce56cc | 850 | r_save_area = convert_memory_address (Pmode, r_save_area); |
4ee9c684 | 851 | r_fp = gen_rtx_MEM (Pmode, r_save_area); |
852 | r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL), | |
853 | plus_constant (r_save_area, GET_MODE_SIZE (Pmode))); | |
854 | ||
855 | current_function_has_nonlocal_goto = 1; | |
856 | ||
03fd9d2c | 857 | #ifdef HAVE_nonlocal_goto |
4ee9c684 | 858 | /* ??? We no longer need to pass the static chain value, afaik. */ |
859 | if (HAVE_nonlocal_goto) | |
860 | emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp)); | |
861 | else | |
862 | #endif | |
863 | { | |
864 | r_label = copy_to_reg (r_label); | |
865 | ||
866 | emit_insn (gen_rtx_CLOBBER (VOIDmode, | |
867 | gen_rtx_MEM (BLKmode, | |
868 | gen_rtx_SCRATCH (VOIDmode)))); | |
869 | ||
870 | emit_insn (gen_rtx_CLOBBER (VOIDmode, | |
871 | gen_rtx_MEM (BLKmode, | |
872 | hard_frame_pointer_rtx))); | |
491e04ef | 873 | |
4ee9c684 | 874 | /* Restore frame pointer for containing function. |
875 | This sets the actual hard register used for the frame pointer | |
876 | to the location of the function's incoming static chain info. | |
877 | The non-local goto handler will then adjust it to contain the | |
878 | proper value and reload the argument pointer, if needed. */ | |
879 | emit_move_insn (hard_frame_pointer_rtx, r_fp); | |
880 | emit_stack_restore (SAVE_NONLOCAL, r_sp, NULL_RTX); | |
491e04ef | 881 | |
4ee9c684 | 882 | /* USE of hard_frame_pointer_rtx added for consistency; |
883 | not clear if really needed. */ | |
884 | emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx)); | |
885 | emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx)); | |
886 | emit_indirect_jump (r_label); | |
887 | } | |
491e04ef | 888 | |
4ee9c684 | 889 | /* Search backwards to the jump insn and mark it as a |
890 | non-local goto. */ | |
891 | for (insn = get_last_insn (); insn; insn = PREV_INSN (insn)) | |
892 | { | |
6d7dc5b9 | 893 | if (JUMP_P (insn)) |
4ee9c684 | 894 | { |
895 | REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, | |
896 | const0_rtx, REG_NOTES (insn)); | |
897 | break; | |
898 | } | |
6d7dc5b9 | 899 | else if (CALL_P (insn)) |
4ee9c684 | 900 | break; |
901 | } | |
902 | ||
903 | return const0_rtx; | |
904 | } | |
905 | ||
843d08a9 | 906 | /* __builtin_update_setjmp_buf is passed a pointer to an array of five words |
907 | (not all will be used on all machines) that was passed to __builtin_setjmp. | |
908 | It updates the stack pointer in that block to correspond to the current | |
909 | stack pointer. */ | |
910 | ||
911 | static void | |
912 | expand_builtin_update_setjmp_buf (rtx buf_addr) | |
913 | { | |
914 | enum machine_mode sa_mode = Pmode; | |
915 | rtx stack_save; | |
916 | ||
917 | ||
918 | #ifdef HAVE_save_stack_nonlocal | |
919 | if (HAVE_save_stack_nonlocal) | |
920 | sa_mode = insn_data[(int) CODE_FOR_save_stack_nonlocal].operand[0].mode; | |
921 | #endif | |
922 | #ifdef STACK_SAVEAREA_MODE | |
923 | sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL); | |
924 | #endif | |
925 | ||
926 | stack_save | |
927 | = gen_rtx_MEM (sa_mode, | |
928 | memory_address | |
929 | (sa_mode, | |
930 | plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode)))); | |
931 | ||
932 | #ifdef HAVE_setjmp | |
933 | if (HAVE_setjmp) | |
934 | emit_insn (gen_setjmp ()); | |
935 | #endif | |
936 | ||
937 | emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX); | |
938 | } | |
939 | ||
5e3608d8 | 940 | /* Expand a call to __builtin_prefetch. For a target that does not support |
941 | data prefetch, evaluate the memory address argument in case it has side | |
942 | effects. */ | |
943 | ||
944 | static void | |
aecda0d6 | 945 | expand_builtin_prefetch (tree arglist) |
5e3608d8 | 946 | { |
947 | tree arg0, arg1, arg2; | |
948 | rtx op0, op1, op2; | |
949 | ||
26a5cadb | 950 | if (!validate_arglist (arglist, POINTER_TYPE, 0)) |
951 | return; | |
952 | ||
5e3608d8 | 953 | arg0 = TREE_VALUE (arglist); |
26a5cadb | 954 | /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to |
955 | zero (read) and argument 2 (locality) defaults to 3 (high degree of | |
956 | locality). */ | |
957 | if (TREE_CHAIN (arglist)) | |
958 | { | |
959 | arg1 = TREE_VALUE (TREE_CHAIN (arglist)); | |
960 | if (TREE_CHAIN (TREE_CHAIN (arglist))) | |
9342ee68 | 961 | arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); |
26a5cadb | 962 | else |
7016c612 | 963 | arg2 = build_int_cst (NULL_TREE, 3); |
26a5cadb | 964 | } |
965 | else | |
966 | { | |
967 | arg1 = integer_zero_node; | |
7016c612 | 968 | arg2 = build_int_cst (NULL_TREE, 3); |
26a5cadb | 969 | } |
5e3608d8 | 970 | |
971 | /* Argument 0 is an address. */ | |
972 | op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL); | |
973 | ||
974 | /* Argument 1 (read/write flag) must be a compile-time constant int. */ | |
975 | if (TREE_CODE (arg1) != INTEGER_CST) | |
976 | { | |
07e3a3d2 | 977 | error ("second argument to %<__builtin_prefetch%> must be a constant"); |
9342ee68 | 978 | arg1 = integer_zero_node; |
5e3608d8 | 979 | } |
8ec3c5c2 | 980 | op1 = expand_normal (arg1); |
5e3608d8 | 981 | /* Argument 1 must be either zero or one. */ |
982 | if (INTVAL (op1) != 0 && INTVAL (op1) != 1) | |
983 | { | |
c3ceba8e | 984 | warning (0, "invalid second argument to %<__builtin_prefetch%>;" |
07e3a3d2 | 985 | " using zero"); |
5e3608d8 | 986 | op1 = const0_rtx; |
987 | } | |
988 | ||
989 | /* Argument 2 (locality) must be a compile-time constant int. */ | |
990 | if (TREE_CODE (arg2) != INTEGER_CST) | |
991 | { | |
07e3a3d2 | 992 | error ("third argument to %<__builtin_prefetch%> must be a constant"); |
5e3608d8 | 993 | arg2 = integer_zero_node; |
994 | } | |
8ec3c5c2 | 995 | op2 = expand_normal (arg2); |
5e3608d8 | 996 | /* Argument 2 must be 0, 1, 2, or 3. */ |
997 | if (INTVAL (op2) < 0 || INTVAL (op2) > 3) | |
998 | { | |
c3ceba8e | 999 | warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero"); |
5e3608d8 | 1000 | op2 = const0_rtx; |
1001 | } | |
1002 | ||
1003 | #ifdef HAVE_prefetch | |
1004 | if (HAVE_prefetch) | |
1005 | { | |
f0ce3b1f | 1006 | if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate) |
4cd21437 | 1007 | (op0, |
f0ce3b1f | 1008 | insn_data[(int) CODE_FOR_prefetch].operand[0].mode)) |
19bf118a | 1009 | || (GET_MODE (op0) != Pmode)) |
9342ee68 | 1010 | { |
85d654dd | 1011 | op0 = convert_memory_address (Pmode, op0); |
9342ee68 | 1012 | op0 = force_reg (Pmode, op0); |
1013 | } | |
5e3608d8 | 1014 | emit_insn (gen_prefetch (op0, op1, op2)); |
1015 | } | |
5e3608d8 | 1016 | #endif |
0a534ba7 | 1017 | |
f0ce3b1f | 1018 | /* Don't do anything with direct references to volatile memory, but |
1019 | generate code to handle other side effects. */ | |
e16ceb8e | 1020 | if (!MEM_P (op0) && side_effects_p (op0)) |
f0ce3b1f | 1021 | emit_insn (op0); |
5e3608d8 | 1022 | } |
1023 | ||
f7c44134 | 1024 | /* Get a MEM rtx for expression EXP which is the address of an operand |
d8ae1baa | 1025 | to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is |
1026 | the maximum length of the block of memory that might be accessed or | |
1027 | NULL if unknown. */ | |
f7c44134 | 1028 | |
53800dbe | 1029 | static rtx |
d8ae1baa | 1030 | get_memory_rtx (tree exp, tree len) |
53800dbe | 1031 | { |
bb2327a8 | 1032 | rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_NORMAL); |
1033 | rtx mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr)); | |
2a631e19 | 1034 | |
f7c44134 | 1035 | /* Get an expression we can use to find the attributes to assign to MEM. |
1036 | If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if | |
1037 | we can. First remove any nops. */ | |
1038 | while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR | |
f0ce3b1f | 1039 | || TREE_CODE (exp) == NON_LVALUE_EXPR) |
f7c44134 | 1040 | && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0)))) |
1041 | exp = TREE_OPERAND (exp, 0); | |
1042 | ||
1043 | if (TREE_CODE (exp) == ADDR_EXPR) | |
a1a25d19 | 1044 | exp = TREE_OPERAND (exp, 0); |
f7c44134 | 1045 | else if (POINTER_TYPE_P (TREE_TYPE (exp))) |
a1a25d19 | 1046 | exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp); |
1047 | else | |
1048 | exp = NULL; | |
1049 | ||
1050 | /* Honor attributes derived from exp, except for the alias set | |
1051 | (as builtin stringops may alias with anything) and the size | |
1052 | (as stringops may access multiple array elements). */ | |
1053 | if (exp) | |
eec8e941 | 1054 | { |
a1a25d19 | 1055 | set_mem_attributes (mem, exp, 0); |
d8ae1baa | 1056 | |
1057 | /* Allow the string and memory builtins to overflow from one | |
1058 | field into another, see http://gcc.gnu.org/PR23561. | |
1059 | Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole | |
1060 | memory accessed by the string or memory builtin will fit | |
1061 | within the field. */ | |
1062 | if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF) | |
1063 | { | |
1064 | tree mem_expr = MEM_EXPR (mem); | |
1065 | HOST_WIDE_INT offset = -1, length = -1; | |
1066 | tree inner = exp; | |
1067 | ||
1068 | while (TREE_CODE (inner) == ARRAY_REF | |
1069 | || TREE_CODE (inner) == NOP_EXPR | |
1070 | || TREE_CODE (inner) == CONVERT_EXPR | |
1071 | || TREE_CODE (inner) == NON_LVALUE_EXPR | |
1072 | || TREE_CODE (inner) == VIEW_CONVERT_EXPR | |
1073 | || TREE_CODE (inner) == SAVE_EXPR) | |
1074 | inner = TREE_OPERAND (inner, 0); | |
1075 | ||
1076 | gcc_assert (TREE_CODE (inner) == COMPONENT_REF); | |
1077 | ||
1078 | if (MEM_OFFSET (mem) | |
1079 | && GET_CODE (MEM_OFFSET (mem)) == CONST_INT) | |
1080 | offset = INTVAL (MEM_OFFSET (mem)); | |
1081 | ||
1082 | if (offset >= 0 && len && host_integerp (len, 0)) | |
1083 | length = tree_low_cst (len, 0); | |
1084 | ||
1085 | while (TREE_CODE (inner) == COMPONENT_REF) | |
1086 | { | |
1087 | tree field = TREE_OPERAND (inner, 1); | |
1088 | gcc_assert (! DECL_BIT_FIELD (field)); | |
1089 | gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF); | |
1090 | gcc_assert (field == TREE_OPERAND (mem_expr, 1)); | |
1091 | ||
1092 | if (length >= 0 | |
1093 | && TYPE_SIZE_UNIT (TREE_TYPE (inner)) | |
1094 | && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0)) | |
1095 | { | |
1096 | HOST_WIDE_INT size | |
1097 | = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (inner)), 0); | |
1098 | /* If we can prove the memory starting at XEXP (mem, 0) | |
1099 | and ending at XEXP (mem, 0) + LENGTH will fit into | |
1100 | this field, we can keep that COMPONENT_REF in MEM_EXPR. */ | |
1101 | if (offset <= size | |
1102 | && length <= size | |
1103 | && offset + length <= size) | |
1104 | break; | |
1105 | } | |
1106 | ||
1107 | if (offset >= 0 | |
1108 | && host_integerp (DECL_FIELD_OFFSET (field), 0)) | |
1109 | offset += tree_low_cst (DECL_FIELD_OFFSET (field), 0) | |
1110 | + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1) | |
1111 | / BITS_PER_UNIT; | |
1112 | else | |
1113 | { | |
1114 | offset = -1; | |
1115 | length = -1; | |
1116 | } | |
1117 | ||
1118 | mem_expr = TREE_OPERAND (mem_expr, 0); | |
1119 | inner = TREE_OPERAND (inner, 0); | |
d8ae1baa | 1120 | } |
1121 | ||
1122 | if (mem_expr == NULL) | |
1123 | offset = -1; | |
1124 | if (mem_expr != MEM_EXPR (mem)) | |
1125 | { | |
1126 | set_mem_expr (mem, mem_expr); | |
1127 | set_mem_offset (mem, offset >= 0 ? GEN_INT (offset) : NULL_RTX); | |
1128 | } | |
1129 | } | |
eec8e941 | 1130 | set_mem_alias_set (mem, 0); |
a1a25d19 | 1131 | set_mem_size (mem, NULL_RTX); |
eec8e941 | 1132 | } |
53800dbe | 1133 | |
53800dbe | 1134 | return mem; |
1135 | } | |
1136 | \f | |
1137 | /* Built-in functions to perform an untyped call and return. */ | |
1138 | ||
1139 | /* For each register that may be used for calling a function, this | |
1140 | gives a mode used to copy the register's value. VOIDmode indicates | |
1141 | the register is not used for calling a function. If the machine | |
1142 | has register windows, this gives only the outbound registers. | |
1143 | INCOMING_REGNO gives the corresponding inbound register. */ | |
1144 | static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER]; | |
1145 | ||
1146 | /* For each register that may be used for returning values, this gives | |
1147 | a mode used to copy the register's value. VOIDmode indicates the | |
1148 | register is not used for returning values. If the machine has | |
1149 | register windows, this gives only the outbound registers. | |
1150 | INCOMING_REGNO gives the corresponding inbound register. */ | |
1151 | static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER]; | |
1152 | ||
1153 | /* For each register that may be used for calling a function, this | |
1154 | gives the offset of that register into the block returned by | |
1155 | __builtin_apply_args. 0 indicates that the register is not | |
1156 | used for calling a function. */ | |
1157 | static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER]; | |
1158 | ||
53800dbe | 1159 | /* Return the size required for the block returned by __builtin_apply_args, |
1160 | and initialize apply_args_mode. */ | |
1161 | ||
1162 | static int | |
aecda0d6 | 1163 | apply_args_size (void) |
53800dbe | 1164 | { |
1165 | static int size = -1; | |
58e9ce8f | 1166 | int align; |
1167 | unsigned int regno; | |
53800dbe | 1168 | enum machine_mode mode; |
1169 | ||
1170 | /* The values computed by this function never change. */ | |
1171 | if (size < 0) | |
1172 | { | |
1173 | /* The first value is the incoming arg-pointer. */ | |
1174 | size = GET_MODE_SIZE (Pmode); | |
1175 | ||
1176 | /* The second value is the structure value address unless this is | |
1177 | passed as an "invisible" first argument. */ | |
6812c89e | 1178 | if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0)) |
53800dbe | 1179 | size += GET_MODE_SIZE (Pmode); |
1180 | ||
1181 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1182 | if (FUNCTION_ARG_REGNO_P (regno)) | |
1183 | { | |
0862b7e9 | 1184 | mode = reg_raw_mode[regno]; |
1185 | ||
64db345d | 1186 | gcc_assert (mode != VOIDmode); |
53800dbe | 1187 | |
1188 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1189 | if (size % align != 0) | |
1190 | size = CEIL (size, align) * align; | |
1191 | apply_args_reg_offset[regno] = size; | |
1192 | size += GET_MODE_SIZE (mode); | |
1193 | apply_args_mode[regno] = mode; | |
1194 | } | |
1195 | else | |
1196 | { | |
1197 | apply_args_mode[regno] = VOIDmode; | |
1198 | apply_args_reg_offset[regno] = 0; | |
1199 | } | |
1200 | } | |
1201 | return size; | |
1202 | } | |
1203 | ||
1204 | /* Return the size required for the block returned by __builtin_apply, | |
1205 | and initialize apply_result_mode. */ | |
1206 | ||
1207 | static int | |
aecda0d6 | 1208 | apply_result_size (void) |
53800dbe | 1209 | { |
1210 | static int size = -1; | |
1211 | int align, regno; | |
1212 | enum machine_mode mode; | |
1213 | ||
1214 | /* The values computed by this function never change. */ | |
1215 | if (size < 0) | |
1216 | { | |
1217 | size = 0; | |
1218 | ||
1219 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1220 | if (FUNCTION_VALUE_REGNO_P (regno)) | |
1221 | { | |
0862b7e9 | 1222 | mode = reg_raw_mode[regno]; |
1223 | ||
64db345d | 1224 | gcc_assert (mode != VOIDmode); |
53800dbe | 1225 | |
1226 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1227 | if (size % align != 0) | |
1228 | size = CEIL (size, align) * align; | |
1229 | size += GET_MODE_SIZE (mode); | |
1230 | apply_result_mode[regno] = mode; | |
1231 | } | |
1232 | else | |
1233 | apply_result_mode[regno] = VOIDmode; | |
1234 | ||
1235 | /* Allow targets that use untyped_call and untyped_return to override | |
1236 | the size so that machine-specific information can be stored here. */ | |
1237 | #ifdef APPLY_RESULT_SIZE | |
1238 | size = APPLY_RESULT_SIZE; | |
1239 | #endif | |
1240 | } | |
1241 | return size; | |
1242 | } | |
1243 | ||
1244 | #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return) | |
1245 | /* Create a vector describing the result block RESULT. If SAVEP is true, | |
1246 | the result block is used to save the values; otherwise it is used to | |
1247 | restore the values. */ | |
1248 | ||
1249 | static rtx | |
aecda0d6 | 1250 | result_vector (int savep, rtx result) |
53800dbe | 1251 | { |
1252 | int regno, size, align, nelts; | |
1253 | enum machine_mode mode; | |
1254 | rtx reg, mem; | |
f0af5a88 | 1255 | rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx)); |
bf8e3599 | 1256 | |
53800dbe | 1257 | size = nelts = 0; |
1258 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1259 | if ((mode = apply_result_mode[regno]) != VOIDmode) | |
1260 | { | |
1261 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1262 | if (size % align != 0) | |
1263 | size = CEIL (size, align) * align; | |
1264 | reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno)); | |
e513d163 | 1265 | mem = adjust_address (result, mode, size); |
53800dbe | 1266 | savevec[nelts++] = (savep |
1267 | ? gen_rtx_SET (VOIDmode, mem, reg) | |
1268 | : gen_rtx_SET (VOIDmode, reg, mem)); | |
1269 | size += GET_MODE_SIZE (mode); | |
1270 | } | |
1271 | return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec)); | |
1272 | } | |
1273 | #endif /* HAVE_untyped_call or HAVE_untyped_return */ | |
1274 | ||
1275 | /* Save the state required to perform an untyped call with the same | |
1276 | arguments as were passed to the current function. */ | |
1277 | ||
1278 | static rtx | |
aecda0d6 | 1279 | expand_builtin_apply_args_1 (void) |
53800dbe | 1280 | { |
1c7e61a7 | 1281 | rtx registers, tem; |
53800dbe | 1282 | int size, align, regno; |
1283 | enum machine_mode mode; | |
6812c89e | 1284 | rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1); |
53800dbe | 1285 | |
1286 | /* Create a block where the arg-pointer, structure value address, | |
1287 | and argument registers can be saved. */ | |
1288 | registers = assign_stack_local (BLKmode, apply_args_size (), -1); | |
1289 | ||
1290 | /* Walk past the arg-pointer and structure value address. */ | |
1291 | size = GET_MODE_SIZE (Pmode); | |
6812c89e | 1292 | if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0)) |
53800dbe | 1293 | size += GET_MODE_SIZE (Pmode); |
1294 | ||
1295 | /* Save each register used in calling a function to the block. */ | |
1296 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1297 | if ((mode = apply_args_mode[regno]) != VOIDmode) | |
1298 | { | |
53800dbe | 1299 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; |
1300 | if (size % align != 0) | |
1301 | size = CEIL (size, align) * align; | |
1302 | ||
1303 | tem = gen_rtx_REG (mode, INCOMING_REGNO (regno)); | |
1304 | ||
e513d163 | 1305 | emit_move_insn (adjust_address (registers, mode, size), tem); |
53800dbe | 1306 | size += GET_MODE_SIZE (mode); |
1307 | } | |
1308 | ||
1309 | /* Save the arg pointer to the block. */ | |
1c7e61a7 | 1310 | tem = copy_to_reg (virtual_incoming_args_rtx); |
f083a92b | 1311 | #ifdef STACK_GROWS_DOWNWARD |
1c7e61a7 | 1312 | /* We need the pointer as the caller actually passed them to us, not |
9d4b544c | 1313 | as we might have pretended they were passed. Make sure it's a valid |
1314 | operand, as emit_move_insn isn't expected to handle a PLUS. */ | |
1315 | tem | |
1316 | = force_operand (plus_constant (tem, current_function_pretend_args_size), | |
1317 | NULL_RTX); | |
1c7e61a7 | 1318 | #endif |
1319 | emit_move_insn (adjust_address (registers, Pmode, 0), tem); | |
0862b7e9 | 1320 | |
53800dbe | 1321 | size = GET_MODE_SIZE (Pmode); |
1322 | ||
1323 | /* Save the structure value address unless this is passed as an | |
1324 | "invisible" first argument. */ | |
45550790 | 1325 | if (struct_incoming_value) |
53800dbe | 1326 | { |
e513d163 | 1327 | emit_move_insn (adjust_address (registers, Pmode, size), |
45550790 | 1328 | copy_to_reg (struct_incoming_value)); |
53800dbe | 1329 | size += GET_MODE_SIZE (Pmode); |
1330 | } | |
1331 | ||
1332 | /* Return the address of the block. */ | |
1333 | return copy_addr_to_reg (XEXP (registers, 0)); | |
1334 | } | |
1335 | ||
1336 | /* __builtin_apply_args returns block of memory allocated on | |
1337 | the stack into which is stored the arg pointer, structure | |
1338 | value address, static chain, and all the registers that might | |
1339 | possibly be used in performing a function call. The code is | |
1340 | moved to the start of the function so the incoming values are | |
1341 | saved. */ | |
27d0c333 | 1342 | |
53800dbe | 1343 | static rtx |
aecda0d6 | 1344 | expand_builtin_apply_args (void) |
53800dbe | 1345 | { |
1346 | /* Don't do __builtin_apply_args more than once in a function. | |
1347 | Save the result of the first call and reuse it. */ | |
1348 | if (apply_args_value != 0) | |
1349 | return apply_args_value; | |
1350 | { | |
1351 | /* When this function is called, it means that registers must be | |
1352 | saved on entry to this function. So we migrate the | |
1353 | call to the first insn of this function. */ | |
1354 | rtx temp; | |
1355 | rtx seq; | |
1356 | ||
1357 | start_sequence (); | |
1358 | temp = expand_builtin_apply_args_1 (); | |
1359 | seq = get_insns (); | |
1360 | end_sequence (); | |
1361 | ||
1362 | apply_args_value = temp; | |
1363 | ||
31d3e01c | 1364 | /* Put the insns after the NOTE that starts the function. |
1365 | If this is inside a start_sequence, make the outer-level insn | |
53800dbe | 1366 | chain current, so the code is placed at the start of the |
1367 | function. */ | |
1368 | push_topmost_sequence (); | |
0ec80471 | 1369 | emit_insn_before (seq, NEXT_INSN (entry_of_function ())); |
53800dbe | 1370 | pop_topmost_sequence (); |
1371 | return temp; | |
1372 | } | |
1373 | } | |
1374 | ||
1375 | /* Perform an untyped call and save the state required to perform an | |
1376 | untyped return of whatever value was returned by the given function. */ | |
1377 | ||
1378 | static rtx | |
aecda0d6 | 1379 | expand_builtin_apply (rtx function, rtx arguments, rtx argsize) |
53800dbe | 1380 | { |
1381 | int size, align, regno; | |
1382 | enum machine_mode mode; | |
2a631e19 | 1383 | rtx incoming_args, result, reg, dest, src, call_insn; |
53800dbe | 1384 | rtx old_stack_level = 0; |
1385 | rtx call_fusage = 0; | |
6812c89e | 1386 | rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0); |
53800dbe | 1387 | |
85d654dd | 1388 | arguments = convert_memory_address (Pmode, arguments); |
726ec87c | 1389 | |
53800dbe | 1390 | /* Create a block where the return registers can be saved. */ |
1391 | result = assign_stack_local (BLKmode, apply_result_size (), -1); | |
1392 | ||
53800dbe | 1393 | /* Fetch the arg pointer from the ARGUMENTS block. */ |
1394 | incoming_args = gen_reg_rtx (Pmode); | |
726ec87c | 1395 | emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments)); |
53800dbe | 1396 | #ifndef STACK_GROWS_DOWNWARD |
ad99e708 | 1397 | incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize, |
1398 | incoming_args, 0, OPTAB_LIB_WIDEN); | |
53800dbe | 1399 | #endif |
1400 | ||
04a46d40 | 1401 | /* Push a new argument block and copy the arguments. Do not allow |
1402 | the (potential) memcpy call below to interfere with our stack | |
1403 | manipulations. */ | |
53800dbe | 1404 | do_pending_stack_adjust (); |
04a46d40 | 1405 | NO_DEFER_POP; |
53800dbe | 1406 | |
2358393e | 1407 | /* Save the stack with nonlocal if available. */ |
53800dbe | 1408 | #ifdef HAVE_save_stack_nonlocal |
1409 | if (HAVE_save_stack_nonlocal) | |
1410 | emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX); | |
1411 | else | |
1412 | #endif | |
1413 | emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX); | |
1414 | ||
59647703 | 1415 | /* Allocate a block of memory onto the stack and copy the memory |
1416 | arguments to the outgoing arguments address. */ | |
1417 | allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT); | |
1418 | dest = virtual_outgoing_args_rtx; | |
1419 | #ifndef STACK_GROWS_DOWNWARD | |
1420 | if (GET_CODE (argsize) == CONST_INT) | |
1421 | dest = plus_constant (dest, -INTVAL (argsize)); | |
1422 | else | |
1423 | dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize)); | |
1424 | #endif | |
2a631e19 | 1425 | dest = gen_rtx_MEM (BLKmode, dest); |
1426 | set_mem_align (dest, PARM_BOUNDARY); | |
1427 | src = gen_rtx_MEM (BLKmode, incoming_args); | |
1428 | set_mem_align (src, PARM_BOUNDARY); | |
0378dbdc | 1429 | emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL); |
53800dbe | 1430 | |
1431 | /* Refer to the argument block. */ | |
1432 | apply_args_size (); | |
1433 | arguments = gen_rtx_MEM (BLKmode, arguments); | |
2a631e19 | 1434 | set_mem_align (arguments, PARM_BOUNDARY); |
53800dbe | 1435 | |
1436 | /* Walk past the arg-pointer and structure value address. */ | |
1437 | size = GET_MODE_SIZE (Pmode); | |
45550790 | 1438 | if (struct_value) |
53800dbe | 1439 | size += GET_MODE_SIZE (Pmode); |
1440 | ||
1441 | /* Restore each of the registers previously saved. Make USE insns | |
1442 | for each of these registers for use in making the call. */ | |
1443 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1444 | if ((mode = apply_args_mode[regno]) != VOIDmode) | |
1445 | { | |
1446 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1447 | if (size % align != 0) | |
1448 | size = CEIL (size, align) * align; | |
1449 | reg = gen_rtx_REG (mode, regno); | |
e513d163 | 1450 | emit_move_insn (reg, adjust_address (arguments, mode, size)); |
53800dbe | 1451 | use_reg (&call_fusage, reg); |
1452 | size += GET_MODE_SIZE (mode); | |
1453 | } | |
1454 | ||
1455 | /* Restore the structure value address unless this is passed as an | |
1456 | "invisible" first argument. */ | |
1457 | size = GET_MODE_SIZE (Pmode); | |
45550790 | 1458 | if (struct_value) |
53800dbe | 1459 | { |
1460 | rtx value = gen_reg_rtx (Pmode); | |
e513d163 | 1461 | emit_move_insn (value, adjust_address (arguments, Pmode, size)); |
45550790 | 1462 | emit_move_insn (struct_value, value); |
8ad4c111 | 1463 | if (REG_P (struct_value)) |
45550790 | 1464 | use_reg (&call_fusage, struct_value); |
53800dbe | 1465 | size += GET_MODE_SIZE (Pmode); |
1466 | } | |
1467 | ||
1468 | /* All arguments and registers used for the call are set up by now! */ | |
4ee9c684 | 1469 | function = prepare_call_address (function, NULL, &call_fusage, 0, 0); |
53800dbe | 1470 | |
1471 | /* Ensure address is valid. SYMBOL_REF is already valid, so no need, | |
1472 | and we don't want to load it into a register as an optimization, | |
1473 | because prepare_call_address already did it if it should be done. */ | |
1474 | if (GET_CODE (function) != SYMBOL_REF) | |
1475 | function = memory_address (FUNCTION_MODE, function); | |
1476 | ||
1477 | /* Generate the actual call instruction and save the return value. */ | |
1478 | #ifdef HAVE_untyped_call | |
1479 | if (HAVE_untyped_call) | |
1480 | emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function), | |
1481 | result, result_vector (1, result))); | |
1482 | else | |
1483 | #endif | |
1484 | #ifdef HAVE_call_value | |
1485 | if (HAVE_call_value) | |
1486 | { | |
1487 | rtx valreg = 0; | |
1488 | ||
1489 | /* Locate the unique return register. It is not possible to | |
1490 | express a call that sets more than one return register using | |
1491 | call_value; use untyped_call for that. In fact, untyped_call | |
1492 | only needs to save the return registers in the given block. */ | |
1493 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1494 | if ((mode = apply_result_mode[regno]) != VOIDmode) | |
1495 | { | |
64db345d | 1496 | gcc_assert (!valreg); /* HAVE_untyped_call required. */ |
7d3f6cc7 | 1497 | |
53800dbe | 1498 | valreg = gen_rtx_REG (mode, regno); |
1499 | } | |
1500 | ||
2ed6c343 | 1501 | emit_call_insn (GEN_CALL_VALUE (valreg, |
53800dbe | 1502 | gen_rtx_MEM (FUNCTION_MODE, function), |
1503 | const0_rtx, NULL_RTX, const0_rtx)); | |
1504 | ||
e513d163 | 1505 | emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg); |
53800dbe | 1506 | } |
1507 | else | |
1508 | #endif | |
64db345d | 1509 | gcc_unreachable (); |
53800dbe | 1510 | |
d5f9786f | 1511 | /* Find the CALL insn we just emitted, and attach the register usage |
1512 | information. */ | |
1513 | call_insn = last_call_insn (); | |
1514 | add_function_usage_to (call_insn, call_fusage); | |
53800dbe | 1515 | |
1516 | /* Restore the stack. */ | |
1517 | #ifdef HAVE_save_stack_nonlocal | |
1518 | if (HAVE_save_stack_nonlocal) | |
1519 | emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX); | |
1520 | else | |
1521 | #endif | |
1522 | emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX); | |
1523 | ||
04a46d40 | 1524 | OK_DEFER_POP; |
1525 | ||
53800dbe | 1526 | /* Return the address of the result block. */ |
85d654dd | 1527 | result = copy_addr_to_reg (XEXP (result, 0)); |
1528 | return convert_memory_address (ptr_mode, result); | |
53800dbe | 1529 | } |
1530 | ||
1531 | /* Perform an untyped return. */ | |
1532 | ||
1533 | static void | |
aecda0d6 | 1534 | expand_builtin_return (rtx result) |
53800dbe | 1535 | { |
1536 | int size, align, regno; | |
1537 | enum machine_mode mode; | |
1538 | rtx reg; | |
1539 | rtx call_fusage = 0; | |
1540 | ||
85d654dd | 1541 | result = convert_memory_address (Pmode, result); |
726ec87c | 1542 | |
53800dbe | 1543 | apply_result_size (); |
1544 | result = gen_rtx_MEM (BLKmode, result); | |
1545 | ||
1546 | #ifdef HAVE_untyped_return | |
1547 | if (HAVE_untyped_return) | |
1548 | { | |
1549 | emit_jump_insn (gen_untyped_return (result, result_vector (0, result))); | |
1550 | emit_barrier (); | |
1551 | return; | |
1552 | } | |
1553 | #endif | |
1554 | ||
1555 | /* Restore the return value and note that each value is used. */ | |
1556 | size = 0; | |
1557 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1558 | if ((mode = apply_result_mode[regno]) != VOIDmode) | |
1559 | { | |
1560 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1561 | if (size % align != 0) | |
1562 | size = CEIL (size, align) * align; | |
1563 | reg = gen_rtx_REG (mode, INCOMING_REGNO (regno)); | |
e513d163 | 1564 | emit_move_insn (reg, adjust_address (result, mode, size)); |
53800dbe | 1565 | |
1566 | push_to_sequence (call_fusage); | |
1567 | emit_insn (gen_rtx_USE (VOIDmode, reg)); | |
1568 | call_fusage = get_insns (); | |
1569 | end_sequence (); | |
1570 | size += GET_MODE_SIZE (mode); | |
1571 | } | |
1572 | ||
1573 | /* Put the USE insns before the return. */ | |
31d3e01c | 1574 | emit_insn (call_fusage); |
53800dbe | 1575 | |
1576 | /* Return whatever values was restored by jumping directly to the end | |
1577 | of the function. */ | |
62380d2d | 1578 | expand_naked_return (); |
53800dbe | 1579 | } |
1580 | ||
539a3a92 | 1581 | /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */ |
27d0c333 | 1582 | |
539a3a92 | 1583 | static enum type_class |
aecda0d6 | 1584 | type_to_class (tree type) |
539a3a92 | 1585 | { |
1586 | switch (TREE_CODE (type)) | |
1587 | { | |
1588 | case VOID_TYPE: return void_type_class; | |
1589 | case INTEGER_TYPE: return integer_type_class; | |
539a3a92 | 1590 | case ENUMERAL_TYPE: return enumeral_type_class; |
1591 | case BOOLEAN_TYPE: return boolean_type_class; | |
1592 | case POINTER_TYPE: return pointer_type_class; | |
1593 | case REFERENCE_TYPE: return reference_type_class; | |
1594 | case OFFSET_TYPE: return offset_type_class; | |
1595 | case REAL_TYPE: return real_type_class; | |
1596 | case COMPLEX_TYPE: return complex_type_class; | |
1597 | case FUNCTION_TYPE: return function_type_class; | |
1598 | case METHOD_TYPE: return method_type_class; | |
1599 | case RECORD_TYPE: return record_type_class; | |
1600 | case UNION_TYPE: | |
1601 | case QUAL_UNION_TYPE: return union_type_class; | |
1602 | case ARRAY_TYPE: return (TYPE_STRING_FLAG (type) | |
1603 | ? string_type_class : array_type_class); | |
539a3a92 | 1604 | case LANG_TYPE: return lang_type_class; |
1605 | default: return no_type_class; | |
1606 | } | |
1607 | } | |
bf8e3599 | 1608 | |
53800dbe | 1609 | /* Expand a call to __builtin_classify_type with arguments found in |
1610 | ARGLIST. */ | |
27d0c333 | 1611 | |
53800dbe | 1612 | static rtx |
aecda0d6 | 1613 | expand_builtin_classify_type (tree arglist) |
53800dbe | 1614 | { |
1615 | if (arglist != 0) | |
539a3a92 | 1616 | return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist)))); |
53800dbe | 1617 | return GEN_INT (no_type_class); |
1618 | } | |
1619 | ||
07976da7 | 1620 | /* This helper macro, meant to be used in mathfn_built_in below, |
1621 | determines which among a set of three builtin math functions is | |
1622 | appropriate for a given type mode. The `F' and `L' cases are | |
1623 | automatically generated from the `double' case. */ | |
1624 | #define CASE_MATHFN(BUILT_IN_MATHFN) \ | |
1625 | case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \ | |
1626 | fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \ | |
1627 | fcodel = BUILT_IN_MATHFN##L ; break; | |
1628 | ||
1629 | /* Return mathematic function equivalent to FN but operating directly | |
1630 | on TYPE, if available. If we can't do the conversion, return zero. */ | |
0a68165a | 1631 | tree |
aecda0d6 | 1632 | mathfn_built_in (tree type, enum built_in_function fn) |
0a68165a | 1633 | { |
07976da7 | 1634 | enum built_in_function fcode, fcodef, fcodel; |
1635 | ||
1636 | switch (fn) | |
1637 | { | |
746114e8 | 1638 | CASE_MATHFN (BUILT_IN_ACOS) |
1639 | CASE_MATHFN (BUILT_IN_ACOSH) | |
1640 | CASE_MATHFN (BUILT_IN_ASIN) | |
1641 | CASE_MATHFN (BUILT_IN_ASINH) | |
07976da7 | 1642 | CASE_MATHFN (BUILT_IN_ATAN) |
746114e8 | 1643 | CASE_MATHFN (BUILT_IN_ATAN2) |
1644 | CASE_MATHFN (BUILT_IN_ATANH) | |
1645 | CASE_MATHFN (BUILT_IN_CBRT) | |
07976da7 | 1646 | CASE_MATHFN (BUILT_IN_CEIL) |
746114e8 | 1647 | CASE_MATHFN (BUILT_IN_COPYSIGN) |
07976da7 | 1648 | CASE_MATHFN (BUILT_IN_COS) |
746114e8 | 1649 | CASE_MATHFN (BUILT_IN_COSH) |
1650 | CASE_MATHFN (BUILT_IN_DREM) | |
1651 | CASE_MATHFN (BUILT_IN_ERF) | |
1652 | CASE_MATHFN (BUILT_IN_ERFC) | |
07976da7 | 1653 | CASE_MATHFN (BUILT_IN_EXP) |
746114e8 | 1654 | CASE_MATHFN (BUILT_IN_EXP10) |
1655 | CASE_MATHFN (BUILT_IN_EXP2) | |
1656 | CASE_MATHFN (BUILT_IN_EXPM1) | |
1657 | CASE_MATHFN (BUILT_IN_FABS) | |
1658 | CASE_MATHFN (BUILT_IN_FDIM) | |
07976da7 | 1659 | CASE_MATHFN (BUILT_IN_FLOOR) |
746114e8 | 1660 | CASE_MATHFN (BUILT_IN_FMA) |
1661 | CASE_MATHFN (BUILT_IN_FMAX) | |
1662 | CASE_MATHFN (BUILT_IN_FMIN) | |
1663 | CASE_MATHFN (BUILT_IN_FMOD) | |
1664 | CASE_MATHFN (BUILT_IN_FREXP) | |
1665 | CASE_MATHFN (BUILT_IN_GAMMA) | |
1666 | CASE_MATHFN (BUILT_IN_HUGE_VAL) | |
1667 | CASE_MATHFN (BUILT_IN_HYPOT) | |
1668 | CASE_MATHFN (BUILT_IN_ILOGB) | |
1669 | CASE_MATHFN (BUILT_IN_INF) | |
1670 | CASE_MATHFN (BUILT_IN_J0) | |
1671 | CASE_MATHFN (BUILT_IN_J1) | |
1672 | CASE_MATHFN (BUILT_IN_JN) | |
ac148751 | 1673 | CASE_MATHFN (BUILT_IN_LCEIL) |
746114e8 | 1674 | CASE_MATHFN (BUILT_IN_LDEXP) |
ad52b9b7 | 1675 | CASE_MATHFN (BUILT_IN_LFLOOR) |
746114e8 | 1676 | CASE_MATHFN (BUILT_IN_LGAMMA) |
ac148751 | 1677 | CASE_MATHFN (BUILT_IN_LLCEIL) |
ad52b9b7 | 1678 | CASE_MATHFN (BUILT_IN_LLFLOOR) |
746114e8 | 1679 | CASE_MATHFN (BUILT_IN_LLRINT) |
1680 | CASE_MATHFN (BUILT_IN_LLROUND) | |
07976da7 | 1681 | CASE_MATHFN (BUILT_IN_LOG) |
746114e8 | 1682 | CASE_MATHFN (BUILT_IN_LOG10) |
1683 | CASE_MATHFN (BUILT_IN_LOG1P) | |
1684 | CASE_MATHFN (BUILT_IN_LOG2) | |
1685 | CASE_MATHFN (BUILT_IN_LOGB) | |
1686 | CASE_MATHFN (BUILT_IN_LRINT) | |
1687 | CASE_MATHFN (BUILT_IN_LROUND) | |
1688 | CASE_MATHFN (BUILT_IN_MODF) | |
1689 | CASE_MATHFN (BUILT_IN_NAN) | |
1690 | CASE_MATHFN (BUILT_IN_NANS) | |
07976da7 | 1691 | CASE_MATHFN (BUILT_IN_NEARBYINT) |
746114e8 | 1692 | CASE_MATHFN (BUILT_IN_NEXTAFTER) |
1693 | CASE_MATHFN (BUILT_IN_NEXTTOWARD) | |
1694 | CASE_MATHFN (BUILT_IN_POW) | |
757c219d | 1695 | CASE_MATHFN (BUILT_IN_POWI) |
746114e8 | 1696 | CASE_MATHFN (BUILT_IN_POW10) |
1697 | CASE_MATHFN (BUILT_IN_REMAINDER) | |
1698 | CASE_MATHFN (BUILT_IN_REMQUO) | |
1699 | CASE_MATHFN (BUILT_IN_RINT) | |
07976da7 | 1700 | CASE_MATHFN (BUILT_IN_ROUND) |
746114e8 | 1701 | CASE_MATHFN (BUILT_IN_SCALB) |
1702 | CASE_MATHFN (BUILT_IN_SCALBLN) | |
1703 | CASE_MATHFN (BUILT_IN_SCALBN) | |
1704 | CASE_MATHFN (BUILT_IN_SIGNIFICAND) | |
07976da7 | 1705 | CASE_MATHFN (BUILT_IN_SIN) |
746114e8 | 1706 | CASE_MATHFN (BUILT_IN_SINCOS) |
1707 | CASE_MATHFN (BUILT_IN_SINH) | |
07976da7 | 1708 | CASE_MATHFN (BUILT_IN_SQRT) |
1709 | CASE_MATHFN (BUILT_IN_TAN) | |
746114e8 | 1710 | CASE_MATHFN (BUILT_IN_TANH) |
1711 | CASE_MATHFN (BUILT_IN_TGAMMA) | |
07976da7 | 1712 | CASE_MATHFN (BUILT_IN_TRUNC) |
746114e8 | 1713 | CASE_MATHFN (BUILT_IN_Y0) |
1714 | CASE_MATHFN (BUILT_IN_Y1) | |
1715 | CASE_MATHFN (BUILT_IN_YN) | |
07976da7 | 1716 | |
0a68165a | 1717 | default: |
07976da7 | 1718 | return 0; |
0a68165a | 1719 | } |
07976da7 | 1720 | |
96b9f485 | 1721 | if (TYPE_MAIN_VARIANT (type) == double_type_node) |
07976da7 | 1722 | return implicit_built_in_decls[fcode]; |
96b9f485 | 1723 | else if (TYPE_MAIN_VARIANT (type) == float_type_node) |
07976da7 | 1724 | return implicit_built_in_decls[fcodef]; |
96b9f485 | 1725 | else if (TYPE_MAIN_VARIANT (type) == long_double_type_node) |
07976da7 | 1726 | return implicit_built_in_decls[fcodel]; |
1727 | else | |
1728 | return 0; | |
0a68165a | 1729 | } |
1730 | ||
0fd605a5 | 1731 | /* If errno must be maintained, expand the RTL to check if the result, |
1732 | TARGET, of a built-in function call, EXP, is NaN, and if so set | |
1733 | errno to EDOM. */ | |
1734 | ||
1735 | static void | |
aecda0d6 | 1736 | expand_errno_check (tree exp, rtx target) |
0fd605a5 | 1737 | { |
7f05340e | 1738 | rtx lab = gen_label_rtx (); |
0fd605a5 | 1739 | |
7f05340e | 1740 | /* Test the result; if it is NaN, set errno=EDOM because |
1741 | the argument was not in the domain. */ | |
1742 | emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target), | |
1743 | 0, lab); | |
0fd605a5 | 1744 | |
1745 | #ifdef TARGET_EDOM | |
7f05340e | 1746 | /* If this built-in doesn't throw an exception, set errno directly. */ |
1747 | if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))) | |
1748 | { | |
0fd605a5 | 1749 | #ifdef GEN_ERRNO_RTX |
7f05340e | 1750 | rtx errno_rtx = GEN_ERRNO_RTX; |
0fd605a5 | 1751 | #else |
7f05340e | 1752 | rtx errno_rtx |
0fd605a5 | 1753 | = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno")); |
1754 | #endif | |
7f05340e | 1755 | emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM)); |
0fd605a5 | 1756 | emit_label (lab); |
7f05340e | 1757 | return; |
0fd605a5 | 1758 | } |
7f05340e | 1759 | #endif |
1760 | ||
1761 | /* We can't set errno=EDOM directly; let the library call do it. | |
1762 | Pop the arguments right away in case the call gets deleted. */ | |
1763 | NO_DEFER_POP; | |
1764 | expand_call (exp, target, 0); | |
1765 | OK_DEFER_POP; | |
1766 | emit_label (lab); | |
0fd605a5 | 1767 | } |
1768 | ||
1769 | ||
6b43bae4 | 1770 | /* Expand a call to one of the builtin math functions (sqrt, exp, or log). |
53800dbe | 1771 | Return 0 if a normal call should be emitted rather than expanding the |
1772 | function in-line. EXP is the expression that is a call to the builtin | |
1773 | function; if convenient, the result should be placed in TARGET. | |
1774 | SUBTARGET may be used as the target for computing one of EXP's operands. */ | |
27d0c333 | 1775 | |
53800dbe | 1776 | static rtx |
aecda0d6 | 1777 | expand_builtin_mathfn (tree exp, rtx target, rtx subtarget) |
53800dbe | 1778 | { |
bf8e3599 | 1779 | optab builtin_optab; |
bd421108 | 1780 | rtx op0, insns, before_call; |
c6e6ecb1 | 1781 | tree fndecl = get_callee_fndecl (exp); |
53800dbe | 1782 | tree arglist = TREE_OPERAND (exp, 1); |
7f05340e | 1783 | enum machine_mode mode; |
528ee710 | 1784 | bool errno_set = false; |
68e6cb9d | 1785 | tree arg, narg; |
53800dbe | 1786 | |
0eb671f7 | 1787 | if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) |
53800dbe | 1788 | return 0; |
1789 | ||
7f05340e | 1790 | arg = TREE_VALUE (arglist); |
53800dbe | 1791 | |
1792 | switch (DECL_FUNCTION_CODE (fndecl)) | |
1793 | { | |
4f35b1fc | 1794 | CASE_FLT_FN (BUILT_IN_SQRT): |
7f05340e | 1795 | errno_set = ! tree_expr_nonnegative_p (arg); |
1796 | builtin_optab = sqrt_optab; | |
1797 | break; | |
4f35b1fc | 1798 | CASE_FLT_FN (BUILT_IN_EXP): |
528ee710 | 1799 | errno_set = true; builtin_optab = exp_optab; break; |
4f35b1fc | 1800 | CASE_FLT_FN (BUILT_IN_EXP10): |
1801 | CASE_FLT_FN (BUILT_IN_POW10): | |
750ef9f5 | 1802 | errno_set = true; builtin_optab = exp10_optab; break; |
4f35b1fc | 1803 | CASE_FLT_FN (BUILT_IN_EXP2): |
750ef9f5 | 1804 | errno_set = true; builtin_optab = exp2_optab; break; |
4f35b1fc | 1805 | CASE_FLT_FN (BUILT_IN_EXPM1): |
a6b4eed2 | 1806 | errno_set = true; builtin_optab = expm1_optab; break; |
4f35b1fc | 1807 | CASE_FLT_FN (BUILT_IN_LOGB): |
4efbc641 | 1808 | errno_set = true; builtin_optab = logb_optab; break; |
4f35b1fc | 1809 | CASE_FLT_FN (BUILT_IN_ILOGB): |
4efbc641 | 1810 | errno_set = true; builtin_optab = ilogb_optab; break; |
4f35b1fc | 1811 | CASE_FLT_FN (BUILT_IN_LOG): |
528ee710 | 1812 | errno_set = true; builtin_optab = log_optab; break; |
4f35b1fc | 1813 | CASE_FLT_FN (BUILT_IN_LOG10): |
d3cd9bde | 1814 | errno_set = true; builtin_optab = log10_optab; break; |
4f35b1fc | 1815 | CASE_FLT_FN (BUILT_IN_LOG2): |
d3cd9bde | 1816 | errno_set = true; builtin_optab = log2_optab; break; |
4f35b1fc | 1817 | CASE_FLT_FN (BUILT_IN_LOG1P): |
f474cd93 | 1818 | errno_set = true; builtin_optab = log1p_optab; break; |
4f35b1fc | 1819 | CASE_FLT_FN (BUILT_IN_ASIN): |
8de2f465 | 1820 | builtin_optab = asin_optab; break; |
4f35b1fc | 1821 | CASE_FLT_FN (BUILT_IN_ACOS): |
8de2f465 | 1822 | builtin_optab = acos_optab; break; |
4f35b1fc | 1823 | CASE_FLT_FN (BUILT_IN_TAN): |
528ee710 | 1824 | builtin_optab = tan_optab; break; |
4f35b1fc | 1825 | CASE_FLT_FN (BUILT_IN_ATAN): |
528ee710 | 1826 | builtin_optab = atan_optab; break; |
4f35b1fc | 1827 | CASE_FLT_FN (BUILT_IN_FLOOR): |
528ee710 | 1828 | builtin_optab = floor_optab; break; |
4f35b1fc | 1829 | CASE_FLT_FN (BUILT_IN_CEIL): |
528ee710 | 1830 | builtin_optab = ceil_optab; break; |
4f35b1fc | 1831 | CASE_FLT_FN (BUILT_IN_TRUNC): |
a7cc195f | 1832 | builtin_optab = btrunc_optab; break; |
4f35b1fc | 1833 | CASE_FLT_FN (BUILT_IN_ROUND): |
528ee710 | 1834 | builtin_optab = round_optab; break; |
4f35b1fc | 1835 | CASE_FLT_FN (BUILT_IN_NEARBYINT): |
528ee710 | 1836 | builtin_optab = nearbyint_optab; break; |
4f35b1fc | 1837 | CASE_FLT_FN (BUILT_IN_RINT): |
aef94a0f | 1838 | builtin_optab = rint_optab; break; |
42721db0 | 1839 | default: |
64db345d | 1840 | gcc_unreachable (); |
53800dbe | 1841 | } |
1842 | ||
7f05340e | 1843 | /* Make a suitable register to place result in. */ |
1844 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
fc4eef90 | 1845 | |
7f05340e | 1846 | if (! flag_errno_math || ! HONOR_NANS (mode)) |
1847 | errno_set = false; | |
1848 | ||
bd421108 | 1849 | /* Before working hard, check whether the instruction is available. */ |
1850 | if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing) | |
68e6cb9d | 1851 | { |
bd421108 | 1852 | target = gen_reg_rtx (mode); |
7f05340e | 1853 | |
bd421108 | 1854 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may |
1855 | need to expand the argument again. This way, we will not perform | |
1856 | side-effects more the once. */ | |
4ee9c684 | 1857 | narg = builtin_save_expr (arg); |
bd421108 | 1858 | if (narg != arg) |
1859 | { | |
9a5acb98 | 1860 | arg = narg; |
bd421108 | 1861 | arglist = build_tree_list (NULL_TREE, arg); |
1862 | exp = build_function_call_expr (fndecl, arglist); | |
1863 | } | |
7f05340e | 1864 | |
bd421108 | 1865 | op0 = expand_expr (arg, subtarget, VOIDmode, 0); |
7f05340e | 1866 | |
bd421108 | 1867 | start_sequence (); |
53800dbe | 1868 | |
bd421108 | 1869 | /* Compute into TARGET. |
1870 | Set TARGET to wherever the result comes back. */ | |
1871 | target = expand_unop (mode, builtin_optab, op0, target, 0); | |
1872 | ||
1873 | if (target != 0) | |
1874 | { | |
1875 | if (errno_set) | |
1876 | expand_errno_check (exp, target); | |
1877 | ||
1878 | /* Output the entire sequence. */ | |
1879 | insns = get_insns (); | |
1880 | end_sequence (); | |
1881 | emit_insn (insns); | |
1882 | return target; | |
1883 | } | |
1884 | ||
1885 | /* If we were unable to expand via the builtin, stop the sequence | |
1886 | (without outputting the insns) and call to the library function | |
1887 | with the stabilized argument list. */ | |
53800dbe | 1888 | end_sequence (); |
53800dbe | 1889 | } |
1890 | ||
bd421108 | 1891 | before_call = get_last_insn (); |
1892 | ||
1893 | target = expand_call (exp, target, target == const0_rtx); | |
53800dbe | 1894 | |
bd421108 | 1895 | /* If this is a sqrt operation and we don't care about errno, try to |
1896 | attach a REG_EQUAL note with a SQRT rtx to the emitted libcall. | |
1897 | This allows the semantics of the libcall to be visible to the RTL | |
1898 | optimizers. */ | |
1899 | if (builtin_optab == sqrt_optab && !errno_set) | |
1900 | { | |
1901 | /* Search backwards through the insns emitted by expand_call looking | |
1902 | for the instruction with the REG_RETVAL note. */ | |
1903 | rtx last = get_last_insn (); | |
1904 | while (last != before_call) | |
1905 | { | |
1906 | if (find_reg_note (last, REG_RETVAL, NULL)) | |
1907 | { | |
1908 | rtx note = find_reg_note (last, REG_EQUAL, NULL); | |
1909 | /* Check that the REQ_EQUAL note is an EXPR_LIST with | |
1910 | two elements, i.e. symbol_ref(sqrt) and the operand. */ | |
1911 | if (note | |
1912 | && GET_CODE (note) == EXPR_LIST | |
1913 | && GET_CODE (XEXP (note, 0)) == EXPR_LIST | |
1914 | && XEXP (XEXP (note, 0), 1) != NULL_RTX | |
1915 | && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX) | |
1916 | { | |
1917 | rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0); | |
1918 | /* Check operand is a register with expected mode. */ | |
1919 | if (operand | |
8ad4c111 | 1920 | && REG_P (operand) |
bd421108 | 1921 | && GET_MODE (operand) == mode) |
1922 | { | |
1923 | /* Replace the REG_EQUAL note with a SQRT rtx. */ | |
1924 | rtx equiv = gen_rtx_SQRT (mode, operand); | |
1925 | set_unique_reg_note (last, REG_EQUAL, equiv); | |
1926 | } | |
1927 | } | |
1928 | break; | |
1929 | } | |
1930 | last = PREV_INSN (last); | |
1931 | } | |
1932 | } | |
0fd605a5 | 1933 | |
1934 | return target; | |
1935 | } | |
1936 | ||
1937 | /* Expand a call to the builtin binary math functions (pow and atan2). | |
1938 | Return 0 if a normal call should be emitted rather than expanding the | |
1939 | function in-line. EXP is the expression that is a call to the builtin | |
1940 | function; if convenient, the result should be placed in TARGET. | |
1941 | SUBTARGET may be used as the target for computing one of EXP's | |
1942 | operands. */ | |
1943 | ||
1944 | static rtx | |
aecda0d6 | 1945 | expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget) |
0fd605a5 | 1946 | { |
1947 | optab builtin_optab; | |
1948 | rtx op0, op1, insns; | |
4737caf2 | 1949 | int op1_type = REAL_TYPE; |
c6e6ecb1 | 1950 | tree fndecl = get_callee_fndecl (exp); |
0fd605a5 | 1951 | tree arglist = TREE_OPERAND (exp, 1); |
68e6cb9d | 1952 | tree arg0, arg1, temp, narg; |
7f05340e | 1953 | enum machine_mode mode; |
0fd605a5 | 1954 | bool errno_set = true; |
1955 | bool stable = true; | |
1956 | ||
4737caf2 | 1957 | if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXP) |
1958 | || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPF) | |
1959 | || (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LDEXPL)) | |
1960 | op1_type = INTEGER_TYPE; | |
1961 | ||
1962 | if (!validate_arglist (arglist, REAL_TYPE, op1_type, VOID_TYPE)) | |
0fd605a5 | 1963 | return 0; |
1964 | ||
1965 | arg0 = TREE_VALUE (arglist); | |
1966 | arg1 = TREE_VALUE (TREE_CHAIN (arglist)); | |
1967 | ||
0fd605a5 | 1968 | switch (DECL_FUNCTION_CODE (fndecl)) |
1969 | { | |
4f35b1fc | 1970 | CASE_FLT_FN (BUILT_IN_POW): |
0fd605a5 | 1971 | builtin_optab = pow_optab; break; |
4f35b1fc | 1972 | CASE_FLT_FN (BUILT_IN_ATAN2): |
0fd605a5 | 1973 | builtin_optab = atan2_optab; break; |
4f35b1fc | 1974 | CASE_FLT_FN (BUILT_IN_LDEXP): |
4737caf2 | 1975 | builtin_optab = ldexp_optab; break; |
4f35b1fc | 1976 | CASE_FLT_FN (BUILT_IN_FMOD): |
80ed5c06 | 1977 | builtin_optab = fmod_optab; break; |
4f35b1fc | 1978 | CASE_FLT_FN (BUILT_IN_DREM): |
80ed5c06 | 1979 | builtin_optab = drem_optab; break; |
0fd605a5 | 1980 | default: |
64db345d | 1981 | gcc_unreachable (); |
0fd605a5 | 1982 | } |
1983 | ||
7f05340e | 1984 | /* Make a suitable register to place result in. */ |
1985 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
fc4eef90 | 1986 | |
1987 | /* Before working hard, check whether the instruction is available. */ | |
1988 | if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing) | |
1989 | return 0; | |
1990 | ||
7f05340e | 1991 | target = gen_reg_rtx (mode); |
1992 | ||
1993 | if (! flag_errno_math || ! HONOR_NANS (mode)) | |
1994 | errno_set = false; | |
1995 | ||
4ee9c684 | 1996 | /* Always stabilize the argument list. */ |
1997 | narg = builtin_save_expr (arg1); | |
68e6cb9d | 1998 | if (narg != arg1) |
7f05340e | 1999 | { |
9a5acb98 | 2000 | arg1 = narg; |
68e6cb9d | 2001 | temp = build_tree_list (NULL_TREE, narg); |
2002 | stable = false; | |
2003 | } | |
2004 | else | |
2005 | temp = TREE_CHAIN (arglist); | |
7f05340e | 2006 | |
4ee9c684 | 2007 | narg = builtin_save_expr (arg0); |
68e6cb9d | 2008 | if (narg != arg0) |
2009 | { | |
9a5acb98 | 2010 | arg0 = narg; |
68e6cb9d | 2011 | arglist = tree_cons (NULL_TREE, narg, temp); |
2012 | stable = false; | |
7f05340e | 2013 | } |
68e6cb9d | 2014 | else if (! stable) |
2015 | arglist = tree_cons (NULL_TREE, arg0, temp); | |
2016 | ||
2017 | if (! stable) | |
2018 | exp = build_function_call_expr (fndecl, arglist); | |
7f05340e | 2019 | |
8ec3c5c2 | 2020 | op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL); |
2021 | op1 = expand_normal (arg1); | |
7f05340e | 2022 | |
7f05340e | 2023 | start_sequence (); |
2024 | ||
0fd605a5 | 2025 | /* Compute into TARGET. |
2026 | Set TARGET to wherever the result comes back. */ | |
7f05340e | 2027 | target = expand_binop (mode, builtin_optab, op0, op1, |
0fd605a5 | 2028 | target, 0, OPTAB_DIRECT); |
53800dbe | 2029 | |
68e6cb9d | 2030 | /* If we were unable to expand via the builtin, stop the sequence |
2031 | (without outputting the insns) and call to the library function | |
2032 | with the stabilized argument list. */ | |
0fd605a5 | 2033 | if (target == 0) |
2034 | { | |
2035 | end_sequence (); | |
68e6cb9d | 2036 | return expand_call (exp, target, target == const0_rtx); |
53800dbe | 2037 | } |
2038 | ||
a4356fb9 | 2039 | if (errno_set) |
2040 | expand_errno_check (exp, target); | |
0fd605a5 | 2041 | |
53800dbe | 2042 | /* Output the entire sequence. */ |
2043 | insns = get_insns (); | |
2044 | end_sequence (); | |
31d3e01c | 2045 | emit_insn (insns); |
bf8e3599 | 2046 | |
53800dbe | 2047 | return target; |
2048 | } | |
2049 | ||
6b43bae4 | 2050 | /* Expand a call to the builtin sin and cos math functions. |
2051 | Return 0 if a normal call should be emitted rather than expanding the | |
2052 | function in-line. EXP is the expression that is a call to the builtin | |
2053 | function; if convenient, the result should be placed in TARGET. | |
2054 | SUBTARGET may be used as the target for computing one of EXP's | |
2055 | operands. */ | |
2056 | ||
2057 | static rtx | |
2058 | expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget) | |
2059 | { | |
2060 | optab builtin_optab; | |
7511b819 | 2061 | rtx op0, insns; |
6b43bae4 | 2062 | tree fndecl = get_callee_fndecl (exp); |
2063 | tree arglist = TREE_OPERAND (exp, 1); | |
2064 | enum machine_mode mode; | |
6b43bae4 | 2065 | tree arg, narg; |
2066 | ||
2067 | if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
2068 | return 0; | |
2069 | ||
2070 | arg = TREE_VALUE (arglist); | |
2071 | ||
2072 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2073 | { | |
4f35b1fc | 2074 | CASE_FLT_FN (BUILT_IN_SIN): |
2075 | CASE_FLT_FN (BUILT_IN_COS): | |
6b43bae4 | 2076 | builtin_optab = sincos_optab; break; |
2077 | default: | |
64db345d | 2078 | gcc_unreachable (); |
6b43bae4 | 2079 | } |
2080 | ||
2081 | /* Make a suitable register to place result in. */ | |
2082 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
2083 | ||
6b43bae4 | 2084 | /* Check if sincos insn is available, otherwise fallback |
0bed3869 | 2085 | to sin or cos insn. */ |
6b43bae4 | 2086 | if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing) { |
2087 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2088 | { | |
4f35b1fc | 2089 | CASE_FLT_FN (BUILT_IN_SIN): |
6b43bae4 | 2090 | builtin_optab = sin_optab; break; |
4f35b1fc | 2091 | CASE_FLT_FN (BUILT_IN_COS): |
6b43bae4 | 2092 | builtin_optab = cos_optab; break; |
2093 | default: | |
64db345d | 2094 | gcc_unreachable (); |
6b43bae4 | 2095 | } |
2096 | } | |
2097 | ||
2098 | /* Before working hard, check whether the instruction is available. */ | |
2099 | if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing) | |
2100 | { | |
2101 | target = gen_reg_rtx (mode); | |
2102 | ||
2103 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may | |
2104 | need to expand the argument again. This way, we will not perform | |
2105 | side-effects more the once. */ | |
2106 | narg = save_expr (arg); | |
2107 | if (narg != arg) | |
2108 | { | |
9a5acb98 | 2109 | arg = narg; |
6b43bae4 | 2110 | arglist = build_tree_list (NULL_TREE, arg); |
2111 | exp = build_function_call_expr (fndecl, arglist); | |
2112 | } | |
2113 | ||
2114 | op0 = expand_expr (arg, subtarget, VOIDmode, 0); | |
2115 | ||
6b43bae4 | 2116 | start_sequence (); |
2117 | ||
2118 | /* Compute into TARGET. | |
2119 | Set TARGET to wherever the result comes back. */ | |
2120 | if (builtin_optab == sincos_optab) | |
2121 | { | |
64db345d | 2122 | int result; |
7d3f6cc7 | 2123 | |
6b43bae4 | 2124 | switch (DECL_FUNCTION_CODE (fndecl)) |
2125 | { | |
4f35b1fc | 2126 | CASE_FLT_FN (BUILT_IN_SIN): |
64db345d | 2127 | result = expand_twoval_unop (builtin_optab, op0, 0, target, 0); |
6b43bae4 | 2128 | break; |
4f35b1fc | 2129 | CASE_FLT_FN (BUILT_IN_COS): |
64db345d | 2130 | result = expand_twoval_unop (builtin_optab, op0, target, 0, 0); |
6b43bae4 | 2131 | break; |
2132 | default: | |
64db345d | 2133 | gcc_unreachable (); |
6b43bae4 | 2134 | } |
64db345d | 2135 | gcc_assert (result); |
6b43bae4 | 2136 | } |
2137 | else | |
2138 | { | |
2139 | target = expand_unop (mode, builtin_optab, op0, target, 0); | |
2140 | } | |
2141 | ||
2142 | if (target != 0) | |
2143 | { | |
6b43bae4 | 2144 | /* Output the entire sequence. */ |
2145 | insns = get_insns (); | |
2146 | end_sequence (); | |
2147 | emit_insn (insns); | |
2148 | return target; | |
2149 | } | |
2150 | ||
2151 | /* If we were unable to expand via the builtin, stop the sequence | |
2152 | (without outputting the insns) and call to the library function | |
2153 | with the stabilized argument list. */ | |
2154 | end_sequence (); | |
2155 | } | |
2156 | ||
6b43bae4 | 2157 | target = expand_call (exp, target, target == const0_rtx); |
2158 | ||
2159 | return target; | |
2160 | } | |
2161 | ||
c3147c1a | 2162 | /* Expand a call to the builtin sincos math function. |
2163 | Return 0 if a normal call should be emitted rather than expanding the | |
2164 | function in-line. EXP is the expression that is a call to the builtin | |
2165 | function. */ | |
2166 | ||
2167 | static rtx | |
2168 | expand_builtin_sincos (tree exp) | |
2169 | { | |
2170 | rtx op0, op1, op2, target1, target2; | |
2171 | tree arglist = TREE_OPERAND (exp, 1); | |
2172 | enum machine_mode mode; | |
2173 | tree arg, sinp, cosp; | |
2174 | int result; | |
2175 | ||
2176 | if (!validate_arglist (arglist, REAL_TYPE, | |
2177 | POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
2178 | return 0; | |
2179 | ||
2180 | arg = TREE_VALUE (arglist); | |
2181 | sinp = TREE_VALUE (TREE_CHAIN (arglist)); | |
2182 | cosp = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
2183 | ||
2184 | /* Make a suitable register to place result in. */ | |
2185 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
2186 | ||
2187 | /* Check if sincos insn is available, otherwise emit the call. */ | |
2188 | if (sincos_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing) | |
2189 | return NULL_RTX; | |
2190 | ||
2191 | target1 = gen_reg_rtx (mode); | |
2192 | target2 = gen_reg_rtx (mode); | |
2193 | ||
8ec3c5c2 | 2194 | op0 = expand_normal (arg); |
2195 | op1 = expand_normal (build_fold_indirect_ref (sinp)); | |
2196 | op2 = expand_normal (build_fold_indirect_ref (cosp)); | |
c3147c1a | 2197 | |
2198 | /* Compute into target1 and target2. | |
2199 | Set TARGET to wherever the result comes back. */ | |
2200 | result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0); | |
2201 | gcc_assert (result); | |
2202 | ||
2203 | /* Move target1 and target2 to the memory locations indicated | |
2204 | by op1 and op2. */ | |
2205 | emit_move_insn (op1, target1); | |
2206 | emit_move_insn (op2, target2); | |
2207 | ||
2208 | return const0_rtx; | |
2209 | } | |
2210 | ||
7d3afc77 | 2211 | /* Expand a call to one of the builtin rounding functions gcc defines |
2212 | as an extension (lfloor and lceil). As these are gcc extensions we | |
2213 | do not need to worry about setting errno to EDOM. | |
ad52b9b7 | 2214 | If expanding via optab fails, lower expression to (int)(floor(x)). |
2215 | EXP is the expression that is a call to the builtin function; | |
2216 | if convenient, the result should be placed in TARGET. SUBTARGET may | |
2217 | be used as the target for computing one of EXP's operands. */ | |
2218 | ||
2219 | static rtx | |
2220 | expand_builtin_int_roundingfn (tree exp, rtx target, rtx subtarget) | |
2221 | { | |
2222 | optab builtin_optab; | |
2223 | rtx op0, insns, tmp; | |
2224 | tree fndecl = get_callee_fndecl (exp); | |
2225 | tree arglist = TREE_OPERAND (exp, 1); | |
2226 | enum built_in_function fallback_fn; | |
2227 | tree fallback_fndecl; | |
2228 | enum machine_mode mode; | |
2229 | tree arg, narg; | |
2230 | ||
2231 | if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
2232 | gcc_unreachable (); | |
2233 | ||
2234 | arg = TREE_VALUE (arglist); | |
2235 | ||
2236 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2237 | { | |
4f35b1fc | 2238 | CASE_FLT_FN (BUILT_IN_LCEIL): |
2239 | CASE_FLT_FN (BUILT_IN_LLCEIL): | |
ac148751 | 2240 | builtin_optab = lceil_optab; |
2241 | fallback_fn = BUILT_IN_CEIL; | |
2242 | break; | |
2243 | ||
4f35b1fc | 2244 | CASE_FLT_FN (BUILT_IN_LFLOOR): |
2245 | CASE_FLT_FN (BUILT_IN_LLFLOOR): | |
ad52b9b7 | 2246 | builtin_optab = lfloor_optab; |
2247 | fallback_fn = BUILT_IN_FLOOR; | |
2248 | break; | |
2249 | ||
2250 | default: | |
2251 | gcc_unreachable (); | |
2252 | } | |
2253 | ||
2254 | /* Make a suitable register to place result in. */ | |
2255 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
2256 | ||
2257 | /* Before working hard, check whether the instruction is available. */ | |
2258 | if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing) | |
2259 | { | |
2260 | target = gen_reg_rtx (mode); | |
2261 | ||
2262 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may | |
2263 | need to expand the argument again. This way, we will not perform | |
2264 | side-effects more the once. */ | |
2265 | narg = builtin_save_expr (arg); | |
2266 | if (narg != arg) | |
2267 | { | |
2268 | arg = narg; | |
2269 | arglist = build_tree_list (NULL_TREE, arg); | |
2270 | exp = build_function_call_expr (fndecl, arglist); | |
2271 | } | |
2272 | ||
2273 | op0 = expand_expr (arg, subtarget, VOIDmode, 0); | |
2274 | ||
2275 | start_sequence (); | |
2276 | ||
2277 | /* Compute into TARGET. | |
2278 | Set TARGET to wherever the result comes back. */ | |
2279 | target = expand_unop (mode, builtin_optab, op0, target, 0); | |
2280 | ||
2281 | if (target != 0) | |
2282 | { | |
2283 | /* Output the entire sequence. */ | |
2284 | insns = get_insns (); | |
2285 | end_sequence (); | |
2286 | emit_insn (insns); | |
2287 | return target; | |
2288 | } | |
2289 | ||
2290 | /* If we were unable to expand via the builtin, stop the sequence | |
2291 | (without outputting the insns). */ | |
2292 | end_sequence (); | |
2293 | } | |
2294 | ||
2295 | /* Fall back to floating point rounding optab. */ | |
2296 | fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn); | |
6f8a2887 | 2297 | /* We shouldn't get here on targets without TARGET_C99_FUNCTIONS. |
2298 | ??? Perhaps convert (int)floorf(x) into (int)floor((double)x). */ | |
2299 | gcc_assert (fallback_fndecl != NULL_TREE); | |
ad52b9b7 | 2300 | exp = build_function_call_expr (fallback_fndecl, arglist); |
2301 | ||
d4c690af | 2302 | tmp = expand_normal (exp); |
ad52b9b7 | 2303 | |
2304 | /* Truncate the result of floating point optab to integer | |
2305 | via expand_fix (). */ | |
2306 | target = gen_reg_rtx (mode); | |
2307 | expand_fix (target, tmp, 0); | |
2308 | ||
2309 | return target; | |
2310 | } | |
2311 | ||
7d3afc77 | 2312 | /* Expand a call to one of the builtin math functions doing integer |
2313 | conversion (lrint). | |
2314 | Return 0 if a normal call should be emitted rather than expanding the | |
2315 | function in-line. EXP is the expression that is a call to the builtin | |
2316 | function; if convenient, the result should be placed in TARGET. | |
2317 | SUBTARGET may be used as the target for computing one of EXP's operands. */ | |
2318 | ||
2319 | static rtx | |
2320 | expand_builtin_int_roundingfn_2 (tree exp, rtx target, rtx subtarget) | |
2321 | { | |
2322 | optab builtin_optab; | |
2323 | rtx op0, insns; | |
2324 | tree fndecl = get_callee_fndecl (exp); | |
2325 | tree arglist = TREE_OPERAND (exp, 1); | |
2326 | enum machine_mode mode; | |
2327 | tree arg, narg; | |
2328 | ||
2329 | /* There's no easy way to detect the case we need to set EDOM. */ | |
2330 | if (flag_errno_math) | |
2331 | return NULL_RTX; | |
2332 | ||
2333 | if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
2334 | return NULL_RTX; | |
2335 | ||
2336 | arg = TREE_VALUE (arglist); | |
2337 | ||
2338 | switch (DECL_FUNCTION_CODE (fndecl)) | |
2339 | { | |
2340 | CASE_FLT_FN (BUILT_IN_LRINT): | |
2341 | CASE_FLT_FN (BUILT_IN_LLRINT): | |
2342 | builtin_optab = lrint_optab; break; | |
2343 | default: | |
2344 | gcc_unreachable (); | |
2345 | } | |
2346 | ||
2347 | /* Make a suitable register to place result in. */ | |
2348 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
2349 | ||
2350 | /* Before working hard, check whether the instruction is available. */ | |
2351 | if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing) | |
2352 | { | |
2353 | target = gen_reg_rtx (mode); | |
2354 | ||
2355 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may | |
2356 | need to expand the argument again. This way, we will not perform | |
2357 | side-effects more the once. */ | |
2358 | narg = builtin_save_expr (arg); | |
2359 | if (narg != arg) | |
2360 | { | |
2361 | arg = narg; | |
2362 | arglist = build_tree_list (NULL_TREE, arg); | |
2363 | exp = build_function_call_expr (fndecl, arglist); | |
2364 | } | |
2365 | ||
2366 | op0 = expand_expr (arg, subtarget, VOIDmode, 0); | |
2367 | ||
2368 | start_sequence (); | |
2369 | ||
2370 | /* Compute into TARGET. | |
2371 | Set TARGET to wherever the result comes back. */ | |
2372 | target = expand_unop (mode, builtin_optab, op0, target, 0); | |
2373 | ||
2374 | if (target != 0) | |
2375 | { | |
2376 | /* Output the entire sequence. */ | |
2377 | insns = get_insns (); | |
2378 | end_sequence (); | |
2379 | emit_insn (insns); | |
2380 | return target; | |
2381 | } | |
2382 | ||
2383 | /* If we were unable to expand via the builtin, stop the sequence | |
2384 | (without outputting the insns) and call to the library function | |
2385 | with the stabilized argument list. */ | |
2386 | end_sequence (); | |
2387 | } | |
2388 | ||
2389 | target = expand_call (exp, target, target == const0_rtx); | |
2390 | ||
2391 | return target; | |
2392 | } | |
2393 | ||
f1b844c6 | 2394 | /* To evaluate powi(x,n), the floating point value x raised to the |
2395 | constant integer exponent n, we use a hybrid algorithm that | |
2396 | combines the "window method" with look-up tables. For an | |
2397 | introduction to exponentiation algorithms and "addition chains", | |
2398 | see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth, | |
2399 | "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming", | |
2400 | 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation | |
2401 | Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */ | |
2402 | ||
2403 | /* Provide a default value for POWI_MAX_MULTS, the maximum number of | |
2404 | multiplications to inline before calling the system library's pow | |
2405 | function. powi(x,n) requires at worst 2*bits(n)-2 multiplications, | |
2406 | so this default never requires calling pow, powf or powl. */ | |
0862b7e9 | 2407 | |
f1b844c6 | 2408 | #ifndef POWI_MAX_MULTS |
2409 | #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2) | |
2410 | #endif | |
2411 | ||
2412 | /* The size of the "optimal power tree" lookup table. All | |
2413 | exponents less than this value are simply looked up in the | |
2414 | powi_table below. This threshold is also used to size the | |
2415 | cache of pseudo registers that hold intermediate results. */ | |
2416 | #define POWI_TABLE_SIZE 256 | |
2417 | ||
2418 | /* The size, in bits of the window, used in the "window method" | |
2419 | exponentiation algorithm. This is equivalent to a radix of | |
2420 | (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */ | |
2421 | #define POWI_WINDOW_SIZE 3 | |
2422 | ||
2423 | /* The following table is an efficient representation of an | |
2424 | "optimal power tree". For each value, i, the corresponding | |
2425 | value, j, in the table states than an optimal evaluation | |
2426 | sequence for calculating pow(x,i) can be found by evaluating | |
2427 | pow(x,j)*pow(x,i-j). An optimal power tree for the first | |
2428 | 100 integers is given in Knuth's "Seminumerical algorithms". */ | |
2429 | ||
2430 | static const unsigned char powi_table[POWI_TABLE_SIZE] = | |
2431 | { | |
2432 | 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */ | |
2433 | 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */ | |
2434 | 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */ | |
2435 | 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */ | |
2436 | 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */ | |
2437 | 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */ | |
2438 | 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */ | |
2439 | 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */ | |
2440 | 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */ | |
2441 | 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */ | |
2442 | 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */ | |
2443 | 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */ | |
2444 | 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */ | |
2445 | 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */ | |
2446 | 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */ | |
2447 | 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */ | |
2448 | 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */ | |
2449 | 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */ | |
2450 | 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */ | |
2451 | 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */ | |
2452 | 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */ | |
2453 | 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */ | |
2454 | 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */ | |
2455 | 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */ | |
2456 | 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */ | |
2457 | 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */ | |
2458 | 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */ | |
2459 | 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */ | |
2460 | 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */ | |
2461 | 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */ | |
2462 | 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */ | |
2463 | 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */ | |
2464 | }; | |
2465 | ||
2466 | ||
2467 | /* Return the number of multiplications required to calculate | |
2468 | powi(x,n) where n is less than POWI_TABLE_SIZE. This is a | |
2469 | subroutine of powi_cost. CACHE is an array indicating | |
2470 | which exponents have already been calculated. */ | |
2471 | ||
2472 | static int | |
2473 | powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache) | |
2474 | { | |
2475 | /* If we've already calculated this exponent, then this evaluation | |
2476 | doesn't require any additional multiplications. */ | |
2477 | if (cache[n]) | |
2478 | return 0; | |
2479 | ||
2480 | cache[n] = true; | |
2481 | return powi_lookup_cost (n - powi_table[n], cache) | |
2482 | + powi_lookup_cost (powi_table[n], cache) + 1; | |
2483 | } | |
2484 | ||
2485 | /* Return the number of multiplications required to calculate | |
2486 | powi(x,n) for an arbitrary x, given the exponent N. This | |
2487 | function needs to be kept in sync with expand_powi below. */ | |
2488 | ||
2489 | static int | |
2490 | powi_cost (HOST_WIDE_INT n) | |
2491 | { | |
2492 | bool cache[POWI_TABLE_SIZE]; | |
2493 | unsigned HOST_WIDE_INT digit; | |
2494 | unsigned HOST_WIDE_INT val; | |
2495 | int result; | |
2496 | ||
2497 | if (n == 0) | |
2498 | return 0; | |
2499 | ||
2500 | /* Ignore the reciprocal when calculating the cost. */ | |
2501 | val = (n < 0) ? -n : n; | |
2502 | ||
2503 | /* Initialize the exponent cache. */ | |
2504 | memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool)); | |
2505 | cache[1] = true; | |
2506 | ||
2507 | result = 0; | |
2508 | ||
2509 | while (val >= POWI_TABLE_SIZE) | |
2510 | { | |
2511 | if (val & 1) | |
2512 | { | |
2513 | digit = val & ((1 << POWI_WINDOW_SIZE) - 1); | |
2514 | result += powi_lookup_cost (digit, cache) | |
2515 | + POWI_WINDOW_SIZE + 1; | |
2516 | val >>= POWI_WINDOW_SIZE; | |
2517 | } | |
2518 | else | |
2519 | { | |
2520 | val >>= 1; | |
2521 | result++; | |
2522 | } | |
2523 | } | |
0862b7e9 | 2524 | |
6881dbab | 2525 | return result + powi_lookup_cost (val, cache); |
f1b844c6 | 2526 | } |
2527 | ||
2528 | /* Recursive subroutine of expand_powi. This function takes the array, | |
2529 | CACHE, of already calculated exponents and an exponent N and returns | |
2530 | an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */ | |
2531 | ||
2532 | static rtx | |
2533 | expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache) | |
2534 | { | |
2535 | unsigned HOST_WIDE_INT digit; | |
2536 | rtx target, result; | |
2537 | rtx op0, op1; | |
2538 | ||
2539 | if (n < POWI_TABLE_SIZE) | |
2540 | { | |
2541 | if (cache[n]) | |
a0c938f0 | 2542 | return cache[n]; |
f1b844c6 | 2543 | |
2544 | target = gen_reg_rtx (mode); | |
2545 | cache[n] = target; | |
2546 | ||
2547 | op0 = expand_powi_1 (mode, n - powi_table[n], cache); | |
2548 | op1 = expand_powi_1 (mode, powi_table[n], cache); | |
2549 | } | |
2550 | else if (n & 1) | |
2551 | { | |
2552 | target = gen_reg_rtx (mode); | |
2553 | digit = n & ((1 << POWI_WINDOW_SIZE) - 1); | |
2554 | op0 = expand_powi_1 (mode, n - digit, cache); | |
2555 | op1 = expand_powi_1 (mode, digit, cache); | |
2556 | } | |
2557 | else | |
2558 | { | |
2559 | target = gen_reg_rtx (mode); | |
2560 | op0 = expand_powi_1 (mode, n >> 1, cache); | |
2561 | op1 = op0; | |
2562 | } | |
2563 | ||
2564 | result = expand_mult (mode, op0, op1, target, 0); | |
2565 | if (result != target) | |
2566 | emit_move_insn (target, result); | |
2567 | return target; | |
2568 | } | |
2569 | ||
2570 | /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the | |
2571 | floating point operand in mode MODE, and N is the exponent. This | |
2572 | function needs to be kept in sync with powi_cost above. */ | |
0862b7e9 | 2573 | |
f1b844c6 | 2574 | static rtx |
2575 | expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n) | |
2576 | { | |
2577 | unsigned HOST_WIDE_INT val; | |
2578 | rtx cache[POWI_TABLE_SIZE]; | |
2579 | rtx result; | |
2580 | ||
2581 | if (n == 0) | |
2582 | return CONST1_RTX (mode); | |
2583 | ||
2584 | val = (n < 0) ? -n : n; | |
2585 | ||
19bf118a | 2586 | memset (cache, 0, sizeof (cache)); |
f1b844c6 | 2587 | cache[1] = x; |
2588 | ||
2589 | result = expand_powi_1 (mode, (n < 0) ? -n : n, cache); | |
2590 | ||
2591 | /* If the original exponent was negative, reciprocate the result. */ | |
2592 | if (n < 0) | |
2593 | result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode), | |
2594 | result, NULL_RTX, 0, OPTAB_LIB_WIDEN); | |
2595 | ||
2596 | return result; | |
2597 | } | |
2598 | ||
2599 | /* Expand a call to the pow built-in mathematical function. Return 0 if | |
2600 | a normal call should be emitted rather than expanding the function | |
2601 | in-line. EXP is the expression that is a call to the builtin | |
2602 | function; if convenient, the result should be placed in TARGET. */ | |
2603 | ||
2604 | static rtx | |
2605 | expand_builtin_pow (tree exp, rtx target, rtx subtarget) | |
2606 | { | |
2607 | tree arglist = TREE_OPERAND (exp, 1); | |
2608 | tree arg0, arg1; | |
2609 | ||
2610 | if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE)) | |
2611 | return 0; | |
2612 | ||
2613 | arg0 = TREE_VALUE (arglist); | |
2614 | arg1 = TREE_VALUE (TREE_CHAIN (arglist)); | |
2615 | ||
8c5cac78 | 2616 | if (TREE_CODE (arg1) == REAL_CST |
f1b844c6 | 2617 | && ! TREE_CONSTANT_OVERFLOW (arg1)) |
2618 | { | |
2619 | REAL_VALUE_TYPE cint; | |
2620 | REAL_VALUE_TYPE c; | |
2621 | HOST_WIDE_INT n; | |
2622 | ||
2623 | c = TREE_REAL_CST (arg1); | |
2624 | n = real_to_integer (&c); | |
2625 | real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0); | |
8c5cac78 | 2626 | if (real_identical (&c, &cint)) |
f1b844c6 | 2627 | { |
8c5cac78 | 2628 | /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact. |
2629 | Otherwise, check the number of multiplications required. | |
2630 | Note that pow never sets errno for an integer exponent. */ | |
2631 | if ((n >= -1 && n <= 2) | |
2632 | || (flag_unsafe_math_optimizations | |
2633 | && ! optimize_size | |
2634 | && powi_cost (n) <= POWI_MAX_MULTS)) | |
2635 | { | |
2636 | enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp)); | |
2637 | rtx op = expand_expr (arg0, subtarget, VOIDmode, 0); | |
2638 | op = force_reg (mode, op); | |
2639 | return expand_powi (op, mode, n); | |
2640 | } | |
f1b844c6 | 2641 | } |
2642 | } | |
b613c2a6 | 2643 | |
2644 | if (! flag_unsafe_math_optimizations) | |
2645 | return NULL_RTX; | |
2646 | return expand_builtin_mathfn_2 (exp, target, subtarget); | |
f1b844c6 | 2647 | } |
2648 | ||
757c219d | 2649 | /* Expand a call to the powi built-in mathematical function. Return 0 if |
2650 | a normal call should be emitted rather than expanding the function | |
2651 | in-line. EXP is the expression that is a call to the builtin | |
2652 | function; if convenient, the result should be placed in TARGET. */ | |
2653 | ||
2654 | static rtx | |
2655 | expand_builtin_powi (tree exp, rtx target, rtx subtarget) | |
2656 | { | |
2657 | tree arglist = TREE_OPERAND (exp, 1); | |
2658 | tree arg0, arg1; | |
2659 | rtx op0, op1; | |
2660 | enum machine_mode mode; | |
d0405f40 | 2661 | enum machine_mode mode2; |
757c219d | 2662 | |
2663 | if (! validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
2664 | return 0; | |
2665 | ||
2666 | arg0 = TREE_VALUE (arglist); | |
2667 | arg1 = TREE_VALUE (TREE_CHAIN (arglist)); | |
2668 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
2669 | ||
2670 | /* Handle constant power. */ | |
2671 | ||
2672 | if (TREE_CODE (arg1) == INTEGER_CST | |
2673 | && ! TREE_CONSTANT_OVERFLOW (arg1)) | |
2674 | { | |
2675 | HOST_WIDE_INT n = TREE_INT_CST_LOW (arg1); | |
2676 | ||
2677 | /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact. | |
2678 | Otherwise, check the number of multiplications required. */ | |
2679 | if ((TREE_INT_CST_HIGH (arg1) == 0 | |
2680 | || TREE_INT_CST_HIGH (arg1) == -1) | |
2681 | && ((n >= -1 && n <= 2) | |
2682 | || (! optimize_size | |
2683 | && powi_cost (n) <= POWI_MAX_MULTS))) | |
2684 | { | |
2685 | op0 = expand_expr (arg0, subtarget, VOIDmode, 0); | |
2686 | op0 = force_reg (mode, op0); | |
2687 | return expand_powi (op0, mode, n); | |
2688 | } | |
2689 | } | |
2690 | ||
2691 | /* Emit a libcall to libgcc. */ | |
2692 | ||
d0405f40 | 2693 | /* Mode of the 2nd argument must match that of an int. */ |
2694 | mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0); | |
2695 | ||
757c219d | 2696 | if (target == NULL_RTX) |
2697 | target = gen_reg_rtx (mode); | |
2698 | ||
2699 | op0 = expand_expr (arg0, subtarget, mode, 0); | |
2700 | if (GET_MODE (op0) != mode) | |
2701 | op0 = convert_to_mode (mode, op0, 0); | |
d0405f40 | 2702 | op1 = expand_expr (arg1, 0, mode2, 0); |
2703 | if (GET_MODE (op1) != mode2) | |
2704 | op1 = convert_to_mode (mode2, op1, 0); | |
757c219d | 2705 | |
2706 | target = emit_library_call_value (powi_optab->handlers[(int) mode].libfunc, | |
2707 | target, LCT_CONST_MAKE_BLOCK, mode, 2, | |
d0405f40 | 2708 | op0, mode, op1, mode2); |
757c219d | 2709 | |
2710 | return target; | |
2711 | } | |
2712 | ||
53800dbe | 2713 | /* Expand expression EXP which is a call to the strlen builtin. Return 0 |
2714 | if we failed the caller should emit a normal call, otherwise | |
aed0bd19 | 2715 | try to get the result in TARGET, if convenient. */ |
f7c44134 | 2716 | |
53800dbe | 2717 | static rtx |
aecda0d6 | 2718 | expand_builtin_strlen (tree arglist, rtx target, |
2719 | enum machine_mode target_mode) | |
53800dbe | 2720 | { |
0eb671f7 | 2721 | if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE)) |
53800dbe | 2722 | return 0; |
2723 | else | |
2724 | { | |
911c0150 | 2725 | rtx pat; |
6248e345 | 2726 | tree len, src = TREE_VALUE (arglist); |
911c0150 | 2727 | rtx result, src_reg, char_rtx, before_strlen; |
80cd7a5e | 2728 | enum machine_mode insn_mode = target_mode, char_mode; |
ef2c4a29 | 2729 | enum insn_code icode = CODE_FOR_nothing; |
6248e345 | 2730 | int align; |
2731 | ||
2732 | /* If the length can be computed at compile-time, return it. */ | |
681fab1e | 2733 | len = c_strlen (src, 0); |
6248e345 | 2734 | if (len) |
80cd7a5e | 2735 | return expand_expr (len, target, target_mode, EXPAND_NORMAL); |
6248e345 | 2736 | |
681fab1e | 2737 | /* If the length can be computed at compile-time and is constant |
2738 | integer, but there are side-effects in src, evaluate | |
2739 | src for side-effects, then return len. | |
2740 | E.g. x = strlen (i++ ? "xfoo" + 1 : "bar"); | |
2741 | can be optimized into: i++; x = 3; */ | |
2742 | len = c_strlen (src, 1); | |
2743 | if (len && TREE_CODE (len) == INTEGER_CST) | |
2744 | { | |
2745 | expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
2746 | return expand_expr (len, target, target_mode, EXPAND_NORMAL); | |
2747 | } | |
2748 | ||
6248e345 | 2749 | align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; |
53800dbe | 2750 | |
53800dbe | 2751 | /* If SRC is not a pointer type, don't do this operation inline. */ |
2752 | if (align == 0) | |
2753 | return 0; | |
2754 | ||
911c0150 | 2755 | /* Bail out if we can't compute strlen in the right mode. */ |
53800dbe | 2756 | while (insn_mode != VOIDmode) |
2757 | { | |
2758 | icode = strlen_optab->handlers[(int) insn_mode].insn_code; | |
2759 | if (icode != CODE_FOR_nothing) | |
c28ae87f | 2760 | break; |
53800dbe | 2761 | |
2762 | insn_mode = GET_MODE_WIDER_MODE (insn_mode); | |
2763 | } | |
2764 | if (insn_mode == VOIDmode) | |
2765 | return 0; | |
2766 | ||
2767 | /* Make a place to write the result of the instruction. */ | |
2768 | result = target; | |
2769 | if (! (result != 0 | |
8ad4c111 | 2770 | && REG_P (result) |
53800dbe | 2771 | && GET_MODE (result) == insn_mode |
2772 | && REGNO (result) >= FIRST_PSEUDO_REGISTER)) | |
2773 | result = gen_reg_rtx (insn_mode); | |
2774 | ||
911c0150 | 2775 | /* Make a place to hold the source address. We will not expand |
2776 | the actual source until we are sure that the expansion will | |
2777 | not fail -- there are trees that cannot be expanded twice. */ | |
2778 | src_reg = gen_reg_rtx (Pmode); | |
53800dbe | 2779 | |
911c0150 | 2780 | /* Mark the beginning of the strlen sequence so we can emit the |
2781 | source operand later. */ | |
f0ce3b1f | 2782 | before_strlen = get_last_insn (); |
53800dbe | 2783 | |
53800dbe | 2784 | char_rtx = const0_rtx; |
f7c44134 | 2785 | char_mode = insn_data[(int) icode].operand[2].mode; |
2786 | if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx, | |
2787 | char_mode)) | |
53800dbe | 2788 | char_rtx = copy_to_mode_reg (char_mode, char_rtx); |
2789 | ||
911c0150 | 2790 | pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg), |
2791 | char_rtx, GEN_INT (align)); | |
2792 | if (! pat) | |
2793 | return 0; | |
2794 | emit_insn (pat); | |
2795 | ||
2796 | /* Now that we are assured of success, expand the source. */ | |
2797 | start_sequence (); | |
c5aba89c | 2798 | pat = expand_expr (src, src_reg, ptr_mode, EXPAND_NORMAL); |
911c0150 | 2799 | if (pat != src_reg) |
2800 | emit_move_insn (src_reg, pat); | |
31d3e01c | 2801 | pat = get_insns (); |
911c0150 | 2802 | end_sequence (); |
bceb0d1f | 2803 | |
2804 | if (before_strlen) | |
2805 | emit_insn_after (pat, before_strlen); | |
2806 | else | |
2807 | emit_insn_before (pat, get_insns ()); | |
53800dbe | 2808 | |
2809 | /* Return the value in the proper mode for this function. */ | |
80cd7a5e | 2810 | if (GET_MODE (result) == target_mode) |
911c0150 | 2811 | target = result; |
53800dbe | 2812 | else if (target != 0) |
911c0150 | 2813 | convert_move (target, result, 0); |
53800dbe | 2814 | else |
80cd7a5e | 2815 | target = convert_to_mode (target_mode, result, 0); |
911c0150 | 2816 | |
2817 | return target; | |
53800dbe | 2818 | } |
2819 | } | |
2820 | ||
17f5ea87 | 2821 | /* Expand a call to the strstr builtin. Return 0 if we failed the |
2822 | caller should emit a normal call, otherwise try to get the result | |
2823 | in TARGET, if convenient (and in mode MODE if that's convenient). */ | |
2824 | ||
2825 | static rtx | |
daa1d5f5 | 2826 | expand_builtin_strstr (tree arglist, tree type, rtx target, enum machine_mode mode) |
17f5ea87 | 2827 | { |
2c84ee37 | 2828 | if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
17f5ea87 | 2829 | { |
daa1d5f5 | 2830 | tree result = fold_builtin_strstr (arglist, type); |
2c84ee37 | 2831 | if (result) |
2832 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
17f5ea87 | 2833 | } |
2c84ee37 | 2834 | return 0; |
17f5ea87 | 2835 | } |
2836 | ||
83d79705 | 2837 | /* Expand a call to the strchr builtin. Return 0 if we failed the |
46f3a74a | 2838 | caller should emit a normal call, otherwise try to get the result |
2839 | in TARGET, if convenient (and in mode MODE if that's convenient). */ | |
2840 | ||
2841 | static rtx | |
daa1d5f5 | 2842 | expand_builtin_strchr (tree arglist, tree type, rtx target, enum machine_mode mode) |
46f3a74a | 2843 | { |
2c84ee37 | 2844 | if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
46f3a74a | 2845 | { |
daa1d5f5 | 2846 | tree result = fold_builtin_strchr (arglist, type); |
2c84ee37 | 2847 | if (result) |
2848 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
83d79705 | 2849 | |
2c84ee37 | 2850 | /* FIXME: Should use strchrM optab so that ports can optimize this. */ |
83d79705 | 2851 | } |
2c84ee37 | 2852 | return 0; |
83d79705 | 2853 | } |
2854 | ||
2855 | /* Expand a call to the strrchr builtin. Return 0 if we failed the | |
2856 | caller should emit a normal call, otherwise try to get the result | |
2857 | in TARGET, if convenient (and in mode MODE if that's convenient). */ | |
2858 | ||
2859 | static rtx | |
daa1d5f5 | 2860 | expand_builtin_strrchr (tree arglist, tree type, rtx target, enum machine_mode mode) |
83d79705 | 2861 | { |
2c84ee37 | 2862 | if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
83d79705 | 2863 | { |
daa1d5f5 | 2864 | tree result = fold_builtin_strrchr (arglist, type); |
2c84ee37 | 2865 | if (result) |
2866 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
83d79705 | 2867 | } |
2c84ee37 | 2868 | return 0; |
83d79705 | 2869 | } |
2870 | ||
2871 | /* Expand a call to the strpbrk builtin. Return 0 if we failed the | |
2872 | caller should emit a normal call, otherwise try to get the result | |
2873 | in TARGET, if convenient (and in mode MODE if that's convenient). */ | |
2874 | ||
2875 | static rtx | |
daa1d5f5 | 2876 | expand_builtin_strpbrk (tree arglist, tree type, rtx target, enum machine_mode mode) |
83d79705 | 2877 | { |
2c84ee37 | 2878 | if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
83d79705 | 2879 | { |
daa1d5f5 | 2880 | tree result = fold_builtin_strpbrk (arglist, type); |
2c84ee37 | 2881 | if (result) |
2882 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
46f3a74a | 2883 | } |
2c84ee37 | 2884 | return 0; |
46f3a74a | 2885 | } |
2886 | ||
6840589f | 2887 | /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE) |
2888 | bytes from constant string DATA + OFFSET and return it as target | |
2889 | constant. */ | |
2890 | ||
2891 | static rtx | |
aecda0d6 | 2892 | builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset, |
2893 | enum machine_mode mode) | |
6840589f | 2894 | { |
2895 | const char *str = (const char *) data; | |
2896 | ||
64db345d | 2897 | gcc_assert (offset >= 0 |
2898 | && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode) | |
2899 | <= strlen (str) + 1)); | |
6840589f | 2900 | |
2901 | return c_readstr (str + offset, mode); | |
2902 | } | |
2903 | ||
6f428e8b | 2904 | /* Expand a call to the memcpy builtin, with arguments in ARGLIST. |
3b824fa6 | 2905 | Return 0 if we failed, the caller should emit a normal call, |
2906 | otherwise try to get the result in TARGET, if convenient (and in | |
9fe0e1b8 | 2907 | mode MODE if that's convenient). */ |
53800dbe | 2908 | static rtx |
5a0de151 | 2909 | expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode) |
53800dbe | 2910 | { |
8e76ec06 | 2911 | tree fndecl = get_callee_fndecl (exp); |
5a0de151 | 2912 | tree arglist = TREE_OPERAND (exp, 1); |
0eb671f7 | 2913 | if (!validate_arglist (arglist, |
2914 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
53800dbe | 2915 | return 0; |
2916 | else | |
2917 | { | |
2918 | tree dest = TREE_VALUE (arglist); | |
2919 | tree src = TREE_VALUE (TREE_CHAIN (arglist)); | |
2920 | tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
6840589f | 2921 | const char *src_str; |
27d0c333 | 2922 | unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT); |
2923 | unsigned int dest_align | |
2924 | = get_pointer_alignment (dest, BIGGEST_ALIGNMENT); | |
53800dbe | 2925 | rtx dest_mem, src_mem, dest_addr, len_rtx; |
4f46f2b9 | 2926 | tree result = fold_builtin_memory_op (arglist, TREE_TYPE (TREE_TYPE (fndecl)), |
2927 | false, /*endp=*/0); | |
5a0de151 | 2928 | |
2929 | if (result) | |
4f46f2b9 | 2930 | { |
2931 | while (TREE_CODE (result) == COMPOUND_EXPR) | |
2932 | { | |
2933 | expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode, | |
2934 | EXPAND_NORMAL); | |
2935 | result = TREE_OPERAND (result, 1); | |
2936 | } | |
2937 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
2938 | } | |
53800dbe | 2939 | |
6f428e8b | 2940 | /* If DEST is not a pointer type, call the normal function. */ |
2941 | if (dest_align == 0) | |
9342ee68 | 2942 | return 0; |
6f428e8b | 2943 | |
6f428e8b | 2944 | /* If either SRC is not a pointer type, don't do this |
a0c938f0 | 2945 | operation in-line. */ |
6f428e8b | 2946 | if (src_align == 0) |
9342ee68 | 2947 | return 0; |
53800dbe | 2948 | |
d8ae1baa | 2949 | dest_mem = get_memory_rtx (dest, len); |
2a631e19 | 2950 | set_mem_align (dest_mem, dest_align); |
8ec3c5c2 | 2951 | len_rtx = expand_normal (len); |
6840589f | 2952 | src_str = c_getstr (src); |
2953 | ||
2954 | /* If SRC is a string constant and block move would be done | |
2955 | by pieces, we can avoid loading the string from memory | |
2956 | and only stored the computed constants. */ | |
2957 | if (src_str | |
6840589f | 2958 | && GET_CODE (len_rtx) == CONST_INT |
2959 | && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1 | |
2960 | && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str, | |
b9a7cc69 | 2961 | (void *) src_str, dest_align)) |
6840589f | 2962 | { |
9fe0e1b8 | 2963 | dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx), |
2964 | builtin_memcpy_read_str, | |
b9a7cc69 | 2965 | (void *) src_str, dest_align, 0); |
e5716f7e | 2966 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
85d654dd | 2967 | dest_mem = convert_memory_address (ptr_mode, dest_mem); |
9fe0e1b8 | 2968 | return dest_mem; |
6840589f | 2969 | } |
2970 | ||
d8ae1baa | 2971 | src_mem = get_memory_rtx (src, len); |
2a631e19 | 2972 | set_mem_align (src_mem, src_align); |
53800dbe | 2973 | |
53800dbe | 2974 | /* Copy word part most expediently. */ |
0378dbdc | 2975 | dest_addr = emit_block_move (dest_mem, src_mem, len_rtx, |
0b25db21 | 2976 | CALL_EXPR_TAILCALL (exp) |
2977 | ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL); | |
53800dbe | 2978 | |
2979 | if (dest_addr == 0) | |
e5716f7e | 2980 | { |
2981 | dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX); | |
85d654dd | 2982 | dest_addr = convert_memory_address (ptr_mode, dest_addr); |
e5716f7e | 2983 | } |
9fe0e1b8 | 2984 | return dest_addr; |
53800dbe | 2985 | } |
2986 | } | |
2987 | ||
647661c6 | 2988 | /* Expand a call to the mempcpy builtin, with arguments in ARGLIST. |
0a39fd54 | 2989 | Return 0 if we failed; the caller should emit a normal call, |
647661c6 | 2990 | otherwise try to get the result in TARGET, if convenient (and in |
9fe0e1b8 | 2991 | mode MODE if that's convenient). If ENDP is 0 return the |
2992 | destination pointer, if ENDP is 1 return the end pointer ala | |
2993 | mempcpy, and if ENDP is 2 return the end pointer minus one ala | |
2994 | stpcpy. */ | |
647661c6 | 2995 | |
2996 | static rtx | |
7da1412b | 2997 | expand_builtin_mempcpy (tree arglist, tree type, rtx target, enum machine_mode mode, |
aecda0d6 | 2998 | int endp) |
647661c6 | 2999 | { |
3000 | if (!validate_arglist (arglist, | |
3001 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3002 | return 0; | |
9fe0e1b8 | 3003 | /* If return value is ignored, transform mempcpy into memcpy. */ |
3004 | else if (target == const0_rtx) | |
3005 | { | |
3006 | tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY]; | |
3007 | ||
3008 | if (!fn) | |
3009 | return 0; | |
3010 | ||
3011 | return expand_expr (build_function_call_expr (fn, arglist), | |
3012 | target, mode, EXPAND_NORMAL); | |
3013 | } | |
647661c6 | 3014 | else |
3015 | { | |
9fe0e1b8 | 3016 | tree dest = TREE_VALUE (arglist); |
3017 | tree src = TREE_VALUE (TREE_CHAIN (arglist)); | |
3018 | tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
3019 | const char *src_str; | |
3020 | unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT); | |
3021 | unsigned int dest_align | |
3022 | = get_pointer_alignment (dest, BIGGEST_ALIGNMENT); | |
3023 | rtx dest_mem, src_mem, len_rtx; | |
4f46f2b9 | 3024 | tree result = fold_builtin_memory_op (arglist, type, false, endp); |
9fe0e1b8 | 3025 | |
7da1412b | 3026 | if (result) |
4f46f2b9 | 3027 | { |
3028 | while (TREE_CODE (result) == COMPOUND_EXPR) | |
3029 | { | |
3030 | expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode, | |
3031 | EXPAND_NORMAL); | |
3032 | result = TREE_OPERAND (result, 1); | |
3033 | } | |
3034 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
3035 | } | |
a0c938f0 | 3036 | |
7da1412b | 3037 | /* If either SRC or DEST is not a pointer type, don't do this |
a0c938f0 | 3038 | operation in-line. */ |
7da1412b | 3039 | if (dest_align == 0 || src_align == 0) |
9fe0e1b8 | 3040 | return 0; |
3041 | ||
6217c238 | 3042 | /* If LEN is not constant, call the normal function. */ |
3043 | if (! host_integerp (len, 1)) | |
3044 | return 0; | |
0862b7e9 | 3045 | |
8ec3c5c2 | 3046 | len_rtx = expand_normal (len); |
9fe0e1b8 | 3047 | src_str = c_getstr (src); |
647661c6 | 3048 | |
9fe0e1b8 | 3049 | /* If SRC is a string constant and block move would be done |
3050 | by pieces, we can avoid loading the string from memory | |
3051 | and only stored the computed constants. */ | |
3052 | if (src_str | |
3053 | && GET_CODE (len_rtx) == CONST_INT | |
3054 | && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1 | |
3055 | && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str, | |
b9a7cc69 | 3056 | (void *) src_str, dest_align)) |
9fe0e1b8 | 3057 | { |
d8ae1baa | 3058 | dest_mem = get_memory_rtx (dest, len); |
9fe0e1b8 | 3059 | set_mem_align (dest_mem, dest_align); |
3060 | dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx), | |
3061 | builtin_memcpy_read_str, | |
b9a7cc69 | 3062 | (void *) src_str, dest_align, endp); |
9fe0e1b8 | 3063 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
85d654dd | 3064 | dest_mem = convert_memory_address (ptr_mode, dest_mem); |
9fe0e1b8 | 3065 | return dest_mem; |
647661c6 | 3066 | } |
3067 | ||
9fe0e1b8 | 3068 | if (GET_CODE (len_rtx) == CONST_INT |
3069 | && can_move_by_pieces (INTVAL (len_rtx), | |
3070 | MIN (dest_align, src_align))) | |
3071 | { | |
d8ae1baa | 3072 | dest_mem = get_memory_rtx (dest, len); |
9fe0e1b8 | 3073 | set_mem_align (dest_mem, dest_align); |
d8ae1baa | 3074 | src_mem = get_memory_rtx (src, len); |
9fe0e1b8 | 3075 | set_mem_align (src_mem, src_align); |
3076 | dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx), | |
3077 | MIN (dest_align, src_align), endp); | |
3078 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); | |
85d654dd | 3079 | dest_mem = convert_memory_address (ptr_mode, dest_mem); |
9fe0e1b8 | 3080 | return dest_mem; |
3081 | } | |
3082 | ||
3083 | return 0; | |
647661c6 | 3084 | } |
3085 | } | |
3086 | ||
c4950093 | 3087 | /* Expand expression EXP, which is a call to the memmove builtin. Return 0 |
0a39fd54 | 3088 | if we failed; the caller should emit a normal call. */ |
c4950093 | 3089 | |
3090 | static rtx | |
7e72af53 | 3091 | expand_builtin_memmove (tree arglist, tree type, rtx target, |
3b1757a2 | 3092 | enum machine_mode mode) |
c4950093 | 3093 | { |
3094 | if (!validate_arglist (arglist, | |
3095 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3096 | return 0; | |
3097 | else | |
3098 | { | |
4f46f2b9 | 3099 | tree result = fold_builtin_memory_op (arglist, type, false, /*endp=*/3); |
7e72af53 | 3100 | |
3101 | if (result) | |
4f46f2b9 | 3102 | { |
3103 | while (TREE_CODE (result) == COMPOUND_EXPR) | |
3104 | { | |
3105 | expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode, | |
3106 | EXPAND_NORMAL); | |
3107 | result = TREE_OPERAND (result, 1); | |
3108 | } | |
3109 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
3110 | } | |
c4950093 | 3111 | |
c4950093 | 3112 | /* Otherwise, call the normal function. */ |
3113 | return 0; | |
3114 | } | |
3115 | } | |
3116 | ||
3117 | /* Expand expression EXP, which is a call to the bcopy builtin. Return 0 | |
3118 | if we failed the caller should emit a normal call. */ | |
3119 | ||
3120 | static rtx | |
0b25db21 | 3121 | expand_builtin_bcopy (tree exp) |
c4950093 | 3122 | { |
0b25db21 | 3123 | tree arglist = TREE_OPERAND (exp, 1); |
3124 | tree type = TREE_TYPE (exp); | |
c4950093 | 3125 | tree src, dest, size, newarglist; |
3126 | ||
3127 | if (!validate_arglist (arglist, | |
3128 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
3129 | return NULL_RTX; | |
3130 | ||
3131 | src = TREE_VALUE (arglist); | |
3132 | dest = TREE_VALUE (TREE_CHAIN (arglist)); | |
3133 | size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
3134 | ||
3135 | /* New argument list transforming bcopy(ptr x, ptr y, int z) to | |
3136 | memmove(ptr y, ptr x, size_t z). This is done this way | |
3137 | so that if it isn't expanded inline, we fallback to | |
3138 | calling bcopy instead of memmove. */ | |
3139 | ||
d7aeca92 | 3140 | newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size)); |
c4950093 | 3141 | newarglist = tree_cons (NULL_TREE, src, newarglist); |
3142 | newarglist = tree_cons (NULL_TREE, dest, newarglist); | |
3143 | ||
3b1757a2 | 3144 | return expand_builtin_memmove (newarglist, type, const0_rtx, VOIDmode); |
c4950093 | 3145 | } |
3146 | ||
727c62dd | 3147 | #ifndef HAVE_movstr |
3148 | # define HAVE_movstr 0 | |
3149 | # define CODE_FOR_movstr CODE_FOR_nothing | |
3150 | #endif | |
3151 | ||
3152 | /* Expand into a movstr instruction, if one is available. Return 0 if | |
3153 | we failed, the caller should emit a normal call, otherwise try to | |
3154 | get the result in TARGET, if convenient. If ENDP is 0 return the | |
3155 | destination pointer, if ENDP is 1 return the end pointer ala | |
3156 | mempcpy, and if ENDP is 2 return the end pointer minus one ala | |
3157 | stpcpy. */ | |
3158 | ||
3159 | static rtx | |
3160 | expand_movstr (tree dest, tree src, rtx target, int endp) | |
3161 | { | |
3162 | rtx end; | |
3163 | rtx dest_mem; | |
3164 | rtx src_mem; | |
3165 | rtx insn; | |
3166 | const struct insn_data * data; | |
3167 | ||
3168 | if (!HAVE_movstr) | |
3169 | return 0; | |
3170 | ||
d8ae1baa | 3171 | dest_mem = get_memory_rtx (dest, NULL); |
3172 | src_mem = get_memory_rtx (src, NULL); | |
727c62dd | 3173 | if (!endp) |
3174 | { | |
3175 | target = force_reg (Pmode, XEXP (dest_mem, 0)); | |
3176 | dest_mem = replace_equiv_address (dest_mem, target); | |
3177 | end = gen_reg_rtx (Pmode); | |
3178 | } | |
3179 | else | |
3180 | { | |
3181 | if (target == 0 || target == const0_rtx) | |
3182 | { | |
3183 | end = gen_reg_rtx (Pmode); | |
3184 | if (target == 0) | |
3185 | target = end; | |
3186 | } | |
3187 | else | |
3188 | end = target; | |
3189 | } | |
3190 | ||
3191 | data = insn_data + CODE_FOR_movstr; | |
3192 | ||
3193 | if (data->operand[0].mode != VOIDmode) | |
3194 | end = gen_lowpart (data->operand[0].mode, end); | |
3195 | ||
3196 | insn = data->genfun (end, dest_mem, src_mem); | |
3197 | ||
64db345d | 3198 | gcc_assert (insn); |
727c62dd | 3199 | |
3200 | emit_insn (insn); | |
3201 | ||
3202 | /* movstr is supposed to set end to the address of the NUL | |
3203 | terminator. If the caller requested a mempcpy-like return value, | |
3204 | adjust it. */ | |
3205 | if (endp == 1 && target != const0_rtx) | |
c5aba89c | 3206 | { |
3207 | rtx tem = plus_constant (gen_lowpart (GET_MODE (target), end), 1); | |
3208 | emit_move_insn (target, force_operand (tem, NULL_RTX)); | |
3209 | } | |
727c62dd | 3210 | |
3211 | return target; | |
3212 | } | |
3213 | ||
53800dbe | 3214 | /* Expand expression EXP, which is a call to the strcpy builtin. Return 0 |
6f428e8b | 3215 | if we failed the caller should emit a normal call, otherwise try to get |
3216 | the result in TARGET, if convenient (and in mode MODE if that's | |
3217 | convenient). */ | |
902de8ed | 3218 | |
53800dbe | 3219 | static rtx |
1d89860b | 3220 | expand_builtin_strcpy (tree fndecl, tree arglist, rtx target, enum machine_mode mode) |
53800dbe | 3221 | { |
dc369150 | 3222 | if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
3223 | { | |
ea726109 | 3224 | tree result = fold_builtin_strcpy (fndecl, arglist, 0); |
dc369150 | 3225 | if (result) |
4f46f2b9 | 3226 | { |
3227 | while (TREE_CODE (result) == COMPOUND_EXPR) | |
3228 | { | |
3229 | expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode, | |
3230 | EXPAND_NORMAL); | |
3231 | result = TREE_OPERAND (result, 1); | |
3232 | } | |
3233 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
3234 | } | |
902de8ed | 3235 | |
dc369150 | 3236 | return expand_movstr (TREE_VALUE (arglist), |
3237 | TREE_VALUE (TREE_CHAIN (arglist)), | |
3238 | target, /*endp=*/0); | |
3239 | } | |
3240 | return 0; | |
53800dbe | 3241 | } |
3242 | ||
3b824fa6 | 3243 | /* Expand a call to the stpcpy builtin, with arguments in ARGLIST. |
3244 | Return 0 if we failed the caller should emit a normal call, | |
3245 | otherwise try to get the result in TARGET, if convenient (and in | |
3246 | mode MODE if that's convenient). */ | |
3247 | ||
3248 | static rtx | |
dc369150 | 3249 | expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode) |
3b824fa6 | 3250 | { |
dc369150 | 3251 | tree arglist = TREE_OPERAND (exp, 1); |
727c62dd | 3252 | /* If return value is ignored, transform stpcpy into strcpy. */ |
3253 | if (target == const0_rtx) | |
978836e5 | 3254 | { |
3255 | tree fn = implicit_built_in_decls[BUILT_IN_STRCPY]; | |
3256 | if (!fn) | |
3257 | return 0; | |
3258 | ||
3259 | return expand_expr (build_function_call_expr (fn, arglist), | |
3260 | target, mode, EXPAND_NORMAL); | |
3261 | } | |
727c62dd | 3262 | |
3b824fa6 | 3263 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
3264 | return 0; | |
3265 | else | |
3266 | { | |
7d705727 | 3267 | tree dst, src, len, lenp1; |
727c62dd | 3268 | tree narglist; |
3269 | rtx ret; | |
647661c6 | 3270 | |
9fe0e1b8 | 3271 | /* Ensure we get an actual string whose length can be evaluated at |
a0c938f0 | 3272 | compile-time, not an expression containing a string. This is |
3273 | because the latter will potentially produce pessimized code | |
3274 | when used to produce the return value. */ | |
c04062e9 | 3275 | src = TREE_VALUE (TREE_CHAIN (arglist)); |
681fab1e | 3276 | if (! c_getstr (src) || ! (len = c_strlen (src, 0))) |
727c62dd | 3277 | return expand_movstr (TREE_VALUE (arglist), |
3278 | TREE_VALUE (TREE_CHAIN (arglist)), | |
3279 | target, /*endp=*/2); | |
3b824fa6 | 3280 | |
6411575e | 3281 | dst = TREE_VALUE (arglist); |
7d705727 | 3282 | lenp1 = size_binop (PLUS_EXPR, len, ssize_int (1)); |
3283 | narglist = build_tree_list (NULL_TREE, lenp1); | |
727c62dd | 3284 | narglist = tree_cons (NULL_TREE, src, narglist); |
3285 | narglist = tree_cons (NULL_TREE, dst, narglist); | |
7da1412b | 3286 | ret = expand_builtin_mempcpy (narglist, TREE_TYPE (exp), |
3287 | target, mode, /*endp=*/2); | |
727c62dd | 3288 | |
3289 | if (ret) | |
3290 | return ret; | |
3291 | ||
3292 | if (TREE_CODE (len) == INTEGER_CST) | |
3293 | { | |
8ec3c5c2 | 3294 | rtx len_rtx = expand_normal (len); |
727c62dd | 3295 | |
3296 | if (GET_CODE (len_rtx) == CONST_INT) | |
3297 | { | |
a0c938f0 | 3298 | ret = expand_builtin_strcpy (get_callee_fndecl (exp), |
1d89860b | 3299 | arglist, target, mode); |
727c62dd | 3300 | |
3301 | if (ret) | |
3302 | { | |
3303 | if (! target) | |
7ac87324 | 3304 | { |
3305 | if (mode != VOIDmode) | |
3306 | target = gen_reg_rtx (mode); | |
3307 | else | |
3308 | target = gen_reg_rtx (GET_MODE (ret)); | |
3309 | } | |
727c62dd | 3310 | if (GET_MODE (target) != GET_MODE (ret)) |
3311 | ret = gen_lowpart (GET_MODE (target), ret); | |
3312 | ||
c5aba89c | 3313 | ret = plus_constant (ret, INTVAL (len_rtx)); |
3314 | ret = emit_move_insn (target, force_operand (ret, NULL_RTX)); | |
64db345d | 3315 | gcc_assert (ret); |
727c62dd | 3316 | |
3317 | return target; | |
3318 | } | |
3319 | } | |
3320 | } | |
3321 | ||
3322 | return expand_movstr (TREE_VALUE (arglist), | |
3323 | TREE_VALUE (TREE_CHAIN (arglist)), | |
3324 | target, /*endp=*/2); | |
3b824fa6 | 3325 | } |
3326 | } | |
3327 | ||
6840589f | 3328 | /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE) |
3329 | bytes from constant string DATA + OFFSET and return it as target | |
3330 | constant. */ | |
3331 | ||
3332 | static rtx | |
aecda0d6 | 3333 | builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset, |
3334 | enum machine_mode mode) | |
6840589f | 3335 | { |
3336 | const char *str = (const char *) data; | |
3337 | ||
3338 | if ((unsigned HOST_WIDE_INT) offset > strlen (str)) | |
3339 | return const0_rtx; | |
3340 | ||
3341 | return c_readstr (str + offset, mode); | |
3342 | } | |
3343 | ||
ed09096d | 3344 | /* Expand expression EXP, which is a call to the strncpy builtin. Return 0 |
3345 | if we failed the caller should emit a normal call. */ | |
3346 | ||
3347 | static rtx | |
8ff6a5cd | 3348 | expand_builtin_strncpy (tree exp, rtx target, enum machine_mode mode) |
ed09096d | 3349 | { |
ea726109 | 3350 | tree fndecl = get_callee_fndecl (exp); |
8ff6a5cd | 3351 | tree arglist = TREE_OPERAND (exp, 1); |
3352 | if (validate_arglist (arglist, | |
3353 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
ed09096d | 3354 | { |
681fab1e | 3355 | tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1); |
ed09096d | 3356 | tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); |
ea726109 | 3357 | tree result = fold_builtin_strncpy (fndecl, arglist, slen); |
a0c938f0 | 3358 | |
8ff6a5cd | 3359 | if (result) |
4f46f2b9 | 3360 | { |
3361 | while (TREE_CODE (result) == COMPOUND_EXPR) | |
3362 | { | |
3363 | expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode, | |
3364 | EXPAND_NORMAL); | |
3365 | result = TREE_OPERAND (result, 1); | |
3366 | } | |
3367 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
3368 | } | |
6840589f | 3369 | |
8ff6a5cd | 3370 | /* We must be passed a constant len and src parameter. */ |
3371 | if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1)) | |
ed09096d | 3372 | return 0; |
3373 | ||
3374 | slen = size_binop (PLUS_EXPR, slen, ssize_int (1)); | |
3375 | ||
3376 | /* We're required to pad with trailing zeros if the requested | |
a0c938f0 | 3377 | len is greater than strlen(s2)+1. In that case try to |
6840589f | 3378 | use store_by_pieces, if it fails, punt. */ |
ed09096d | 3379 | if (tree_int_cst_lt (slen, len)) |
6840589f | 3380 | { |
3381 | tree dest = TREE_VALUE (arglist); | |
27d0c333 | 3382 | unsigned int dest_align |
3383 | = get_pointer_alignment (dest, BIGGEST_ALIGNMENT); | |
6840589f | 3384 | const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist))); |
3385 | rtx dest_mem; | |
3386 | ||
27d0c333 | 3387 | if (!p || dest_align == 0 || !host_integerp (len, 1) |
3388 | || !can_store_by_pieces (tree_low_cst (len, 1), | |
6840589f | 3389 | builtin_strncpy_read_str, |
b9a7cc69 | 3390 | (void *) p, dest_align)) |
6840589f | 3391 | return 0; |
3392 | ||
d8ae1baa | 3393 | dest_mem = get_memory_rtx (dest, len); |
27d0c333 | 3394 | store_by_pieces (dest_mem, tree_low_cst (len, 1), |
6840589f | 3395 | builtin_strncpy_read_str, |
b9a7cc69 | 3396 | (void *) p, dest_align, 0); |
e5716f7e | 3397 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
85d654dd | 3398 | dest_mem = convert_memory_address (ptr_mode, dest_mem); |
e5716f7e | 3399 | return dest_mem; |
6840589f | 3400 | } |
ed09096d | 3401 | } |
8ff6a5cd | 3402 | return 0; |
ed09096d | 3403 | } |
3404 | ||
ecc318ff | 3405 | /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE) |
3406 | bytes from constant string DATA + OFFSET and return it as target | |
3407 | constant. */ | |
3408 | ||
3409 | static rtx | |
aecda0d6 | 3410 | builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED, |
3411 | enum machine_mode mode) | |
ecc318ff | 3412 | { |
3413 | const char *c = (const char *) data; | |
3414 | char *p = alloca (GET_MODE_SIZE (mode)); | |
3415 | ||
3416 | memset (p, *c, GET_MODE_SIZE (mode)); | |
3417 | ||
3418 | return c_readstr (p, mode); | |
3419 | } | |
3420 | ||
a7ec6974 | 3421 | /* Callback routine for store_by_pieces. Return the RTL of a register |
3422 | containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned | |
3423 | char value given in the RTL register data. For example, if mode is | |
3424 | 4 bytes wide, return the RTL for 0x01010101*data. */ | |
3425 | ||
3426 | static rtx | |
aecda0d6 | 3427 | builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED, |
3428 | enum machine_mode mode) | |
a7ec6974 | 3429 | { |
3430 | rtx target, coeff; | |
3431 | size_t size; | |
3432 | char *p; | |
3433 | ||
3434 | size = GET_MODE_SIZE (mode); | |
f0ce3b1f | 3435 | if (size == 1) |
3436 | return (rtx) data; | |
a7ec6974 | 3437 | |
3438 | p = alloca (size); | |
3439 | memset (p, 1, size); | |
3440 | coeff = c_readstr (p, mode); | |
3441 | ||
f0ce3b1f | 3442 | target = convert_to_mode (mode, (rtx) data, 1); |
a7ec6974 | 3443 | target = expand_mult (mode, target, coeff, NULL_RTX, 1); |
3444 | return force_reg (mode, target); | |
3445 | } | |
3446 | ||
53800dbe | 3447 | /* Expand expression EXP, which is a call to the memset builtin. Return 0 |
6f428e8b | 3448 | if we failed the caller should emit a normal call, otherwise try to get |
3449 | the result in TARGET, if convenient (and in mode MODE if that's | |
3450 | convenient). */ | |
902de8ed | 3451 | |
53800dbe | 3452 | static rtx |
0b25db21 | 3453 | expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode, |
3454 | tree orig_exp) | |
53800dbe | 3455 | { |
0eb671f7 | 3456 | if (!validate_arglist (arglist, |
3457 | POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
53800dbe | 3458 | return 0; |
3459 | else | |
3460 | { | |
3461 | tree dest = TREE_VALUE (arglist); | |
3462 | tree val = TREE_VALUE (TREE_CHAIN (arglist)); | |
3463 | tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
6b961939 | 3464 | tree fndecl, fn; |
3465 | enum built_in_function fcode; | |
ecc318ff | 3466 | char c; |
6b961939 | 3467 | unsigned int dest_align; |
53800dbe | 3468 | rtx dest_mem, dest_addr, len_rtx; |
3469 | ||
6b961939 | 3470 | dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT); |
3471 | ||
bf8e3599 | 3472 | /* If DEST is not a pointer type, don't do this |
53800dbe | 3473 | operation in-line. */ |
3474 | if (dest_align == 0) | |
3475 | return 0; | |
3476 | ||
6f428e8b | 3477 | /* If the LEN parameter is zero, return DEST. */ |
9c8a1629 | 3478 | if (integer_zerop (len)) |
9342ee68 | 3479 | { |
3480 | /* Evaluate and ignore VAL in case it has side-effects. */ | |
3481 | expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
3482 | return expand_expr (dest, target, mode, EXPAND_NORMAL); | |
3483 | } | |
6f428e8b | 3484 | |
6b961939 | 3485 | /* Stabilize the arguments in case we fail. */ |
3486 | dest = builtin_save_expr (dest); | |
3487 | val = builtin_save_expr (val); | |
3488 | len = builtin_save_expr (len); | |
3489 | ||
8ec3c5c2 | 3490 | len_rtx = expand_normal (len); |
d8ae1baa | 3491 | dest_mem = get_memory_rtx (dest, len); |
7a3e5564 | 3492 | |
ecc318ff | 3493 | if (TREE_CODE (val) != INTEGER_CST) |
9342ee68 | 3494 | { |
3495 | rtx val_rtx; | |
a7ec6974 | 3496 | |
1007f554 | 3497 | val_rtx = expand_normal (val); |
3498 | val_rtx = convert_to_mode (TYPE_MODE (unsigned_char_type_node), | |
3499 | val_rtx, 0); | |
a7ec6974 | 3500 | |
9342ee68 | 3501 | /* Assume that we can memset by pieces if we can store the |
3502 | * the coefficients by pieces (in the required modes). | |
3503 | * We can't pass builtin_memset_gen_str as that emits RTL. */ | |
3504 | c = 1; | |
7a3e5564 | 3505 | if (host_integerp (len, 1) |
3506 | && !(optimize_size && tree_low_cst (len, 1) > 1) | |
3507 | && can_store_by_pieces (tree_low_cst (len, 1), | |
3508 | builtin_memset_read_str, &c, dest_align)) | |
3509 | { | |
a0c938f0 | 3510 | val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node), |
7a3e5564 | 3511 | val_rtx); |
3512 | store_by_pieces (dest_mem, tree_low_cst (len, 1), | |
3513 | builtin_memset_gen_str, val_rtx, dest_align, 0); | |
3514 | } | |
a0c938f0 | 3515 | else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx, |
6b961939 | 3516 | dest_align)) |
3517 | goto do_libcall; | |
a7ec6974 | 3518 | |
e5716f7e | 3519 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
85d654dd | 3520 | dest_mem = convert_memory_address (ptr_mode, dest_mem); |
e5716f7e | 3521 | return dest_mem; |
9342ee68 | 3522 | } |
53800dbe | 3523 | |
ecc318ff | 3524 | if (target_char_cast (val, &c)) |
6b961939 | 3525 | goto do_libcall; |
53800dbe | 3526 | |
ecc318ff | 3527 | if (c) |
3528 | { | |
7a3e5564 | 3529 | if (host_integerp (len, 1) |
3530 | && !(optimize_size && tree_low_cst (len, 1) > 1) | |
3531 | && can_store_by_pieces (tree_low_cst (len, 1), | |
3532 | builtin_memset_read_str, &c, dest_align)) | |
3533 | store_by_pieces (dest_mem, tree_low_cst (len, 1), | |
3534 | builtin_memset_read_str, &c, dest_align, 0); | |
3535 | else if (!set_storage_via_setmem (dest_mem, len_rtx, GEN_INT (c), | |
3536 | dest_align)) | |
6b961939 | 3537 | goto do_libcall; |
ecc318ff | 3538 | |
e5716f7e | 3539 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
85d654dd | 3540 | dest_mem = convert_memory_address (ptr_mode, dest_mem); |
e5716f7e | 3541 | return dest_mem; |
ecc318ff | 3542 | } |
3543 | ||
2a631e19 | 3544 | set_mem_align (dest_mem, dest_align); |
0b25db21 | 3545 | dest_addr = clear_storage (dest_mem, len_rtx, |
3546 | CALL_EXPR_TAILCALL (orig_exp) | |
3547 | ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL); | |
53800dbe | 3548 | |
3549 | if (dest_addr == 0) | |
e5716f7e | 3550 | { |
3551 | dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX); | |
85d654dd | 3552 | dest_addr = convert_memory_address (ptr_mode, dest_addr); |
e5716f7e | 3553 | } |
53800dbe | 3554 | |
3555 | return dest_addr; | |
6b961939 | 3556 | |
3557 | do_libcall: | |
3558 | fndecl = get_callee_fndecl (orig_exp); | |
3559 | fcode = DECL_FUNCTION_CODE (fndecl); | |
3560 | gcc_assert (fcode == BUILT_IN_MEMSET || fcode == BUILT_IN_BZERO); | |
3561 | arglist = build_tree_list (NULL_TREE, len); | |
3562 | if (fcode == BUILT_IN_MEMSET) | |
3563 | arglist = tree_cons (NULL_TREE, val, arglist); | |
3564 | arglist = tree_cons (NULL_TREE, dest, arglist); | |
3565 | fn = build_function_call_expr (fndecl, arglist); | |
3566 | if (TREE_CODE (fn) == CALL_EXPR) | |
3567 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp); | |
3568 | return expand_call (fn, target, target == const0_rtx); | |
53800dbe | 3569 | } |
3570 | } | |
3571 | ||
ffc83088 | 3572 | /* Expand expression EXP, which is a call to the bzero builtin. Return 0 |
3573 | if we failed the caller should emit a normal call. */ | |
27d0c333 | 3574 | |
ffc83088 | 3575 | static rtx |
0b25db21 | 3576 | expand_builtin_bzero (tree exp) |
ffc83088 | 3577 | { |
0b25db21 | 3578 | tree arglist = TREE_OPERAND (exp, 1); |
7369e7ba | 3579 | tree dest, size, newarglist; |
ffc83088 | 3580 | |
0eb671f7 | 3581 | if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
7369e7ba | 3582 | return NULL_RTX; |
ffc83088 | 3583 | |
0eb671f7 | 3584 | dest = TREE_VALUE (arglist); |
3585 | size = TREE_VALUE (TREE_CHAIN (arglist)); | |
bf8e3599 | 3586 | |
7369e7ba | 3587 | /* New argument list transforming bzero(ptr x, int y) to |
6f428e8b | 3588 | memset(ptr x, int 0, size_t y). This is done this way |
3589 | so that if it isn't expanded inline, we fallback to | |
3590 | calling bzero instead of memset. */ | |
bf8e3599 | 3591 | |
d7aeca92 | 3592 | newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size)); |
7369e7ba | 3593 | newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist); |
3594 | newarglist = tree_cons (NULL_TREE, dest, newarglist); | |
ffc83088 | 3595 | |
0b25db21 | 3596 | return expand_builtin_memset (newarglist, const0_rtx, VOIDmode, exp); |
ffc83088 | 3597 | } |
3598 | ||
7a3f89b5 | 3599 | /* Expand expression EXP, which is a call to the memcmp built-in function. |
53800dbe | 3600 | ARGLIST is the argument list for this call. Return 0 if we failed and the |
3601 | caller should emit a normal call, otherwise try to get the result in | |
6f428e8b | 3602 | TARGET, if convenient (and in mode MODE, if that's convenient). */ |
27d0c333 | 3603 | |
53800dbe | 3604 | static rtx |
aecda0d6 | 3605 | expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target, |
3606 | enum machine_mode mode) | |
53800dbe | 3607 | { |
0eb671f7 | 3608 | if (!validate_arglist (arglist, |
9342ee68 | 3609 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
53800dbe | 3610 | return 0; |
218bc19e | 3611 | else |
6f428e8b | 3612 | { |
218bc19e | 3613 | tree result = fold_builtin_memcmp (arglist); |
3614 | if (result) | |
3615 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
6f428e8b | 3616 | } |
3617 | ||
6ac5504b | 3618 | #if defined HAVE_cmpmemsi || defined HAVE_cmpstrnsi |
53800dbe | 3619 | { |
218bc19e | 3620 | tree arg1 = TREE_VALUE (arglist); |
3621 | tree arg2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
3622 | tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
0cd832f0 | 3623 | rtx arg1_rtx, arg2_rtx, arg3_rtx; |
53800dbe | 3624 | rtx result; |
0cd832f0 | 3625 | rtx insn; |
53800dbe | 3626 | |
3627 | int arg1_align | |
3628 | = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; | |
3629 | int arg2_align | |
3630 | = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; | |
b428c0a5 | 3631 | enum machine_mode insn_mode; |
3632 | ||
3633 | #ifdef HAVE_cmpmemsi | |
3634 | if (HAVE_cmpmemsi) | |
3635 | insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode; | |
3636 | else | |
3637 | #endif | |
6ac5504b | 3638 | #ifdef HAVE_cmpstrnsi |
3639 | if (HAVE_cmpstrnsi) | |
3640 | insn_mode = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode; | |
b428c0a5 | 3641 | else |
3642 | #endif | |
0862b7e9 | 3643 | return 0; |
53800dbe | 3644 | |
3645 | /* If we don't have POINTER_TYPE, call the function. */ | |
3646 | if (arg1_align == 0 || arg2_align == 0) | |
3647 | return 0; | |
3648 | ||
3649 | /* Make a place to write the result of the instruction. */ | |
3650 | result = target; | |
3651 | if (! (result != 0 | |
8ad4c111 | 3652 | && REG_P (result) && GET_MODE (result) == insn_mode |
53800dbe | 3653 | && REGNO (result) >= FIRST_PSEUDO_REGISTER)) |
3654 | result = gen_reg_rtx (insn_mode); | |
3655 | ||
d8ae1baa | 3656 | arg1_rtx = get_memory_rtx (arg1, len); |
3657 | arg2_rtx = get_memory_rtx (arg2, len); | |
8ec3c5c2 | 3658 | arg3_rtx = expand_normal (len); |
83f88f8e | 3659 | |
3660 | /* Set MEM_SIZE as appropriate. */ | |
3661 | if (GET_CODE (arg3_rtx) == CONST_INT) | |
3662 | { | |
3663 | set_mem_size (arg1_rtx, arg3_rtx); | |
3664 | set_mem_size (arg2_rtx, arg3_rtx); | |
3665 | } | |
3666 | ||
b428c0a5 | 3667 | #ifdef HAVE_cmpmemsi |
3668 | if (HAVE_cmpmemsi) | |
3669 | insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx, | |
3670 | GEN_INT (MIN (arg1_align, arg2_align))); | |
0cd832f0 | 3671 | else |
b428c0a5 | 3672 | #endif |
6ac5504b | 3673 | #ifdef HAVE_cmpstrnsi |
3674 | if (HAVE_cmpstrnsi) | |
3675 | insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx, | |
3676 | GEN_INT (MIN (arg1_align, arg2_align))); | |
b428c0a5 | 3677 | else |
3678 | #endif | |
64db345d | 3679 | gcc_unreachable (); |
0cd832f0 | 3680 | |
3681 | if (insn) | |
3682 | emit_insn (insn); | |
3683 | else | |
2c5d421b | 3684 | emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK, |
0cd832f0 | 3685 | TYPE_MODE (integer_type_node), 3, |
3686 | XEXP (arg1_rtx, 0), Pmode, | |
3687 | XEXP (arg2_rtx, 0), Pmode, | |
3688 | convert_to_mode (TYPE_MODE (sizetype), arg3_rtx, | |
78a8ed03 | 3689 | TYPE_UNSIGNED (sizetype)), |
0cd832f0 | 3690 | TYPE_MODE (sizetype)); |
53800dbe | 3691 | |
3692 | /* Return the value in the proper mode for this function. */ | |
3693 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
3694 | if (GET_MODE (result) == mode) | |
3695 | return result; | |
3696 | else if (target != 0) | |
3697 | { | |
3698 | convert_move (target, result, 0); | |
3699 | return target; | |
3700 | } | |
3701 | else | |
3702 | return convert_to_mode (mode, result, 0); | |
3703 | } | |
83d79705 | 3704 | #endif |
53800dbe | 3705 | |
6f428e8b | 3706 | return 0; |
3707 | } | |
3708 | ||
53800dbe | 3709 | /* Expand expression EXP, which is a call to the strcmp builtin. Return 0 |
3710 | if we failed the caller should emit a normal call, otherwise try to get | |
3711 | the result in TARGET, if convenient. */ | |
902de8ed | 3712 | |
53800dbe | 3713 | static rtx |
aecda0d6 | 3714 | expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode) |
53800dbe | 3715 | { |
3716 | tree arglist = TREE_OPERAND (exp, 1); | |
3717 | ||
0eb671f7 | 3718 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
53800dbe | 3719 | return 0; |
218bc19e | 3720 | else |
ef6c187e | 3721 | { |
218bc19e | 3722 | tree result = fold_builtin_strcmp (arglist); |
3723 | if (result) | |
3724 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
ef6c187e | 3725 | } |
bf8e3599 | 3726 | |
6ac5504b | 3727 | #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi |
3728 | if (cmpstr_optab[SImode] != CODE_FOR_nothing | |
3729 | || cmpstrn_optab[SImode] != CODE_FOR_nothing) | |
3730 | { | |
3731 | rtx arg1_rtx, arg2_rtx; | |
3732 | rtx result, insn = NULL_RTX; | |
3733 | tree fndecl, fn; | |
a0c938f0 | 3734 | |
6ac5504b | 3735 | tree arg1 = TREE_VALUE (arglist); |
3736 | tree arg2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
3737 | int arg1_align | |
3738 | = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; | |
3739 | int arg2_align | |
3740 | = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; | |
3741 | ||
3742 | /* If we don't have POINTER_TYPE, call the function. */ | |
3743 | if (arg1_align == 0 || arg2_align == 0) | |
3744 | return 0; | |
7a3f89b5 | 3745 | |
6ac5504b | 3746 | /* Stabilize the arguments in case gen_cmpstr(n)si fail. */ |
3747 | arg1 = builtin_save_expr (arg1); | |
3748 | arg2 = builtin_save_expr (arg2); | |
7a3f89b5 | 3749 | |
d8ae1baa | 3750 | arg1_rtx = get_memory_rtx (arg1, NULL); |
3751 | arg2_rtx = get_memory_rtx (arg2, NULL); | |
53800dbe | 3752 | |
6ac5504b | 3753 | #ifdef HAVE_cmpstrsi |
3754 | /* Try to call cmpstrsi. */ | |
3755 | if (HAVE_cmpstrsi) | |
3756 | { | |
a0c938f0 | 3757 | enum machine_mode insn_mode |
6ac5504b | 3758 | = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode; |
3759 | ||
3760 | /* Make a place to write the result of the instruction. */ | |
3761 | result = target; | |
3762 | if (! (result != 0 | |
3763 | && REG_P (result) && GET_MODE (result) == insn_mode | |
3764 | && REGNO (result) >= FIRST_PSEUDO_REGISTER)) | |
3765 | result = gen_reg_rtx (insn_mode); | |
3766 | ||
3767 | insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, | |
3768 | GEN_INT (MIN (arg1_align, arg2_align))); | |
3769 | } | |
3770 | #endif | |
03fd9d2c | 3771 | #ifdef HAVE_cmpstrnsi |
6ac5504b | 3772 | /* Try to determine at least one length and call cmpstrnsi. */ |
a0c938f0 | 3773 | if (!insn && HAVE_cmpstrnsi) |
6ac5504b | 3774 | { |
3775 | tree len; | |
3776 | rtx arg3_rtx; | |
3777 | ||
a0c938f0 | 3778 | enum machine_mode insn_mode |
6ac5504b | 3779 | = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode; |
3780 | tree len1 = c_strlen (arg1, 1); | |
3781 | tree len2 = c_strlen (arg2, 1); | |
3782 | ||
3783 | if (len1) | |
3784 | len1 = size_binop (PLUS_EXPR, ssize_int (1), len1); | |
3785 | if (len2) | |
3786 | len2 = size_binop (PLUS_EXPR, ssize_int (1), len2); | |
3787 | ||
3788 | /* If we don't have a constant length for the first, use the length | |
3789 | of the second, if we know it. We don't require a constant for | |
3790 | this case; some cost analysis could be done if both are available | |
3791 | but neither is constant. For now, assume they're equally cheap, | |
3792 | unless one has side effects. If both strings have constant lengths, | |
3793 | use the smaller. */ | |
3794 | ||
3795 | if (!len1) | |
3796 | len = len2; | |
3797 | else if (!len2) | |
3798 | len = len1; | |
3799 | else if (TREE_SIDE_EFFECTS (len1)) | |
3800 | len = len2; | |
3801 | else if (TREE_SIDE_EFFECTS (len2)) | |
3802 | len = len1; | |
3803 | else if (TREE_CODE (len1) != INTEGER_CST) | |
3804 | len = len2; | |
3805 | else if (TREE_CODE (len2) != INTEGER_CST) | |
3806 | len = len1; | |
3807 | else if (tree_int_cst_lt (len1, len2)) | |
3808 | len = len1; | |
3809 | else | |
3810 | len = len2; | |
3811 | ||
3812 | /* If both arguments have side effects, we cannot optimize. */ | |
3813 | if (!len || TREE_SIDE_EFFECTS (len)) | |
6b961939 | 3814 | goto do_libcall; |
53800dbe | 3815 | |
8ec3c5c2 | 3816 | arg3_rtx = expand_normal (len); |
902de8ed | 3817 | |
6ac5504b | 3818 | /* Make a place to write the result of the instruction. */ |
3819 | result = target; | |
3820 | if (! (result != 0 | |
3821 | && REG_P (result) && GET_MODE (result) == insn_mode | |
3822 | && REGNO (result) >= FIRST_PSEUDO_REGISTER)) | |
3823 | result = gen_reg_rtx (insn_mode); | |
53800dbe | 3824 | |
6ac5504b | 3825 | insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx, |
3826 | GEN_INT (MIN (arg1_align, arg2_align))); | |
3827 | } | |
3828 | #endif | |
3f8aefe2 | 3829 | |
6ac5504b | 3830 | if (insn) |
3831 | { | |
3832 | emit_insn (insn); | |
3f8aefe2 | 3833 | |
6ac5504b | 3834 | /* Return the value in the proper mode for this function. */ |
3835 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
3836 | if (GET_MODE (result) == mode) | |
3837 | return result; | |
3838 | if (target == 0) | |
3839 | return convert_to_mode (mode, result, 0); | |
3840 | convert_move (target, result, 0); | |
3841 | return target; | |
3842 | } | |
902de8ed | 3843 | |
6ac5504b | 3844 | /* Expand the library call ourselves using a stabilized argument |
3845 | list to avoid re-evaluating the function's arguments twice. */ | |
2694880e | 3846 | #ifdef HAVE_cmpstrnsi |
6b961939 | 3847 | do_libcall: |
2694880e | 3848 | #endif |
6ac5504b | 3849 | arglist = build_tree_list (NULL_TREE, arg2); |
3850 | arglist = tree_cons (NULL_TREE, arg1, arglist); | |
3851 | fndecl = get_callee_fndecl (exp); | |
3852 | fn = build_function_call_expr (fndecl, arglist); | |
3853 | if (TREE_CODE (fn) == CALL_EXPR) | |
3854 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
3855 | return expand_call (fn, target, target == const0_rtx); | |
3856 | } | |
7a3f89b5 | 3857 | #endif |
3858 | return 0; | |
83d79705 | 3859 | } |
53800dbe | 3860 | |
ed09096d | 3861 | /* Expand expression EXP, which is a call to the strncmp builtin. Return 0 |
3862 | if we failed the caller should emit a normal call, otherwise try to get | |
3863 | the result in TARGET, if convenient. */ | |
27d0c333 | 3864 | |
ed09096d | 3865 | static rtx |
aecda0d6 | 3866 | expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode) |
ed09096d | 3867 | { |
3868 | tree arglist = TREE_OPERAND (exp, 1); | |
ed09096d | 3869 | |
0eb671f7 | 3870 | if (!validate_arglist (arglist, |
3871 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
ed09096d | 3872 | return 0; |
218bc19e | 3873 | else |
ef6c187e | 3874 | { |
218bc19e | 3875 | tree result = fold_builtin_strncmp (arglist); |
3876 | if (result) | |
3877 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
ef6c187e | 3878 | } |
ed09096d | 3879 | |
6e34e617 | 3880 | /* If c_strlen can determine an expression for one of the string |
6ac5504b | 3881 | lengths, and it doesn't have side effects, then emit cmpstrnsi |
7a3f89b5 | 3882 | using length MIN(strlen(string)+1, arg3). */ |
6ac5504b | 3883 | #ifdef HAVE_cmpstrnsi |
3884 | if (HAVE_cmpstrnsi) | |
7a3f89b5 | 3885 | { |
218bc19e | 3886 | tree arg1 = TREE_VALUE (arglist); |
3887 | tree arg2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
3888 | tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
7a3f89b5 | 3889 | tree len, len1, len2; |
3890 | rtx arg1_rtx, arg2_rtx, arg3_rtx; | |
3891 | rtx result, insn; | |
0b25db21 | 3892 | tree fndecl, fn; |
6f428e8b | 3893 | |
7a3f89b5 | 3894 | int arg1_align |
3895 | = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; | |
3896 | int arg2_align | |
3897 | = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; | |
3898 | enum machine_mode insn_mode | |
6ac5504b | 3899 | = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode; |
bf8e3599 | 3900 | |
681fab1e | 3901 | len1 = c_strlen (arg1, 1); |
3902 | len2 = c_strlen (arg2, 1); | |
7a3f89b5 | 3903 | |
3904 | if (len1) | |
3905 | len1 = size_binop (PLUS_EXPR, ssize_int (1), len1); | |
3906 | if (len2) | |
3907 | len2 = size_binop (PLUS_EXPR, ssize_int (1), len2); | |
3908 | ||
3909 | /* If we don't have a constant length for the first, use the length | |
3910 | of the second, if we know it. We don't require a constant for | |
3911 | this case; some cost analysis could be done if both are available | |
3912 | but neither is constant. For now, assume they're equally cheap, | |
3913 | unless one has side effects. If both strings have constant lengths, | |
3914 | use the smaller. */ | |
3915 | ||
3916 | if (!len1) | |
3917 | len = len2; | |
3918 | else if (!len2) | |
3919 | len = len1; | |
3920 | else if (TREE_SIDE_EFFECTS (len1)) | |
3921 | len = len2; | |
3922 | else if (TREE_SIDE_EFFECTS (len2)) | |
3923 | len = len1; | |
3924 | else if (TREE_CODE (len1) != INTEGER_CST) | |
3925 | len = len2; | |
3926 | else if (TREE_CODE (len2) != INTEGER_CST) | |
3927 | len = len1; | |
3928 | else if (tree_int_cst_lt (len1, len2)) | |
3929 | len = len1; | |
3930 | else | |
3931 | len = len2; | |
6e34e617 | 3932 | |
7a3f89b5 | 3933 | /* If both arguments have side effects, we cannot optimize. */ |
3934 | if (!len || TREE_SIDE_EFFECTS (len)) | |
3935 | return 0; | |
bf8e3599 | 3936 | |
7a3f89b5 | 3937 | /* The actual new length parameter is MIN(len,arg3). */ |
49d00087 | 3938 | len = fold_build2 (MIN_EXPR, TREE_TYPE (len), len, |
3939 | fold_convert (TREE_TYPE (len), arg3)); | |
7a3f89b5 | 3940 | |
3941 | /* If we don't have POINTER_TYPE, call the function. */ | |
3942 | if (arg1_align == 0 || arg2_align == 0) | |
3943 | return 0; | |
3944 | ||
3945 | /* Make a place to write the result of the instruction. */ | |
3946 | result = target; | |
3947 | if (! (result != 0 | |
8ad4c111 | 3948 | && REG_P (result) && GET_MODE (result) == insn_mode |
7a3f89b5 | 3949 | && REGNO (result) >= FIRST_PSEUDO_REGISTER)) |
3950 | result = gen_reg_rtx (insn_mode); | |
3951 | ||
6ac5504b | 3952 | /* Stabilize the arguments in case gen_cmpstrnsi fails. */ |
4ee9c684 | 3953 | arg1 = builtin_save_expr (arg1); |
3954 | arg2 = builtin_save_expr (arg2); | |
3955 | len = builtin_save_expr (len); | |
3f8aefe2 | 3956 | |
d8ae1baa | 3957 | arg1_rtx = get_memory_rtx (arg1, len); |
3958 | arg2_rtx = get_memory_rtx (arg2, len); | |
8ec3c5c2 | 3959 | arg3_rtx = expand_normal (len); |
6ac5504b | 3960 | insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx, |
3961 | GEN_INT (MIN (arg1_align, arg2_align))); | |
3f8aefe2 | 3962 | if (insn) |
3963 | { | |
3964 | emit_insn (insn); | |
3965 | ||
3966 | /* Return the value in the proper mode for this function. */ | |
3967 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
3968 | if (GET_MODE (result) == mode) | |
3969 | return result; | |
3970 | if (target == 0) | |
3971 | return convert_to_mode (mode, result, 0); | |
3972 | convert_move (target, result, 0); | |
3973 | return target; | |
3974 | } | |
7a3f89b5 | 3975 | |
3f8aefe2 | 3976 | /* Expand the library call ourselves using a stabilized argument |
3977 | list to avoid re-evaluating the function's arguments twice. */ | |
3978 | arglist = build_tree_list (NULL_TREE, len); | |
3979 | arglist = tree_cons (NULL_TREE, arg2, arglist); | |
3980 | arglist = tree_cons (NULL_TREE, arg1, arglist); | |
3981 | fndecl = get_callee_fndecl (exp); | |
0b25db21 | 3982 | fn = build_function_call_expr (fndecl, arglist); |
3983 | if (TREE_CODE (fn) == CALL_EXPR) | |
3984 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
3985 | return expand_call (fn, target, target == const0_rtx); | |
7a3f89b5 | 3986 | } |
3987 | #endif | |
3988 | return 0; | |
ed09096d | 3989 | } |
3990 | ||
49f0327b | 3991 | /* Expand expression EXP, which is a call to the strcat builtin. |
3992 | Return 0 if we failed the caller should emit a normal call, | |
3993 | otherwise try to get the result in TARGET, if convenient. */ | |
27d0c333 | 3994 | |
49f0327b | 3995 | static rtx |
1d89860b | 3996 | expand_builtin_strcat (tree fndecl, tree arglist, rtx target, enum machine_mode mode) |
49f0327b | 3997 | { |
0eb671f7 | 3998 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
49f0327b | 3999 | return 0; |
4000 | else | |
4001 | { | |
4002 | tree dst = TREE_VALUE (arglist), | |
1d89860b | 4003 | src = TREE_VALUE (TREE_CHAIN (arglist)); |
49f0327b | 4004 | const char *p = c_getstr (src); |
4005 | ||
1d89860b | 4006 | /* If the string length is zero, return the dst parameter. */ |
a0c938f0 | 4007 | if (p && *p == '\0') |
1d89860b | 4008 | return expand_expr (dst, target, mode, EXPAND_NORMAL); |
a0c938f0 | 4009 | |
1d89860b | 4010 | if (!optimize_size) |
ca71f89b | 4011 | { |
1d89860b | 4012 | /* See if we can store by pieces into (dst + strlen(dst)). */ |
4013 | tree newsrc, newdst, | |
4014 | strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN]; | |
4015 | rtx insns; | |
4016 | ||
4017 | /* Stabilize the argument list. */ | |
4018 | newsrc = builtin_save_expr (src); | |
4019 | if (newsrc != src) | |
4020 | arglist = build_tree_list (NULL_TREE, newsrc); | |
a0c938f0 | 4021 | else |
1d89860b | 4022 | arglist = TREE_CHAIN (arglist); /* Reusing arglist if safe. */ |
4023 | ||
4024 | dst = builtin_save_expr (dst); | |
4025 | ||
4026 | start_sequence (); | |
4027 | ||
4028 | /* Create strlen (dst). */ | |
4029 | newdst = | |
4030 | build_function_call_expr (strlen_fn, | |
4031 | build_tree_list (NULL_TREE, dst)); | |
4032 | /* Create (dst + (cast) strlen (dst)). */ | |
4033 | newdst = fold_convert (TREE_TYPE (dst), newdst); | |
4034 | newdst = fold_build2 (PLUS_EXPR, TREE_TYPE (dst), dst, newdst); | |
4035 | ||
4036 | newdst = builtin_save_expr (newdst); | |
4037 | arglist = tree_cons (NULL_TREE, newdst, arglist); | |
4038 | ||
4039 | if (!expand_builtin_strcpy (fndecl, arglist, target, mode)) | |
ca71f89b | 4040 | { |
1d89860b | 4041 | end_sequence (); /* Stop sequence. */ |
4042 | return 0; | |
ca71f89b | 4043 | } |
a0c938f0 | 4044 | |
1d89860b | 4045 | /* Output the entire sequence. */ |
4046 | insns = get_insns (); | |
4047 | end_sequence (); | |
4048 | emit_insn (insns); | |
a0c938f0 | 4049 | |
1d89860b | 4050 | return expand_expr (dst, target, mode, EXPAND_NORMAL); |
ca71f89b | 4051 | } |
49f0327b | 4052 | |
4053 | return 0; | |
4054 | } | |
4055 | } | |
4056 | ||
4057 | /* Expand expression EXP, which is a call to the strncat builtin. | |
4058 | Return 0 if we failed the caller should emit a normal call, | |
4059 | otherwise try to get the result in TARGET, if convenient. */ | |
27d0c333 | 4060 | |
49f0327b | 4061 | static rtx |
aecda0d6 | 4062 | expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode) |
49f0327b | 4063 | { |
2c84ee37 | 4064 | if (validate_arglist (arglist, |
4065 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
49f0327b | 4066 | { |
2c84ee37 | 4067 | tree result = fold_builtin_strncat (arglist); |
4068 | if (result) | |
4069 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
49f0327b | 4070 | } |
2c84ee37 | 4071 | return 0; |
49f0327b | 4072 | } |
4073 | ||
4074 | /* Expand expression EXP, which is a call to the strspn builtin. | |
4075 | Return 0 if we failed the caller should emit a normal call, | |
4076 | otherwise try to get the result in TARGET, if convenient. */ | |
27d0c333 | 4077 | |
49f0327b | 4078 | static rtx |
aecda0d6 | 4079 | expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode) |
49f0327b | 4080 | { |
2c84ee37 | 4081 | if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
49f0327b | 4082 | { |
2c84ee37 | 4083 | tree result = fold_builtin_strspn (arglist); |
4084 | if (result) | |
4085 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
49f0327b | 4086 | } |
2c84ee37 | 4087 | return 0; |
49f0327b | 4088 | } |
4089 | ||
4090 | /* Expand expression EXP, which is a call to the strcspn builtin. | |
4091 | Return 0 if we failed the caller should emit a normal call, | |
4092 | otherwise try to get the result in TARGET, if convenient. */ | |
27d0c333 | 4093 | |
49f0327b | 4094 | static rtx |
aecda0d6 | 4095 | expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode) |
49f0327b | 4096 | { |
2c84ee37 | 4097 | if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
49f0327b | 4098 | { |
2c84ee37 | 4099 | tree result = fold_builtin_strcspn (arglist); |
4100 | if (result) | |
4101 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
49f0327b | 4102 | } |
2c84ee37 | 4103 | return 0; |
49f0327b | 4104 | } |
4105 | ||
a66c9326 | 4106 | /* Expand a call to __builtin_saveregs, generating the result in TARGET, |
4107 | if that's convenient. */ | |
902de8ed | 4108 | |
a66c9326 | 4109 | rtx |
aecda0d6 | 4110 | expand_builtin_saveregs (void) |
53800dbe | 4111 | { |
a66c9326 | 4112 | rtx val, seq; |
53800dbe | 4113 | |
4114 | /* Don't do __builtin_saveregs more than once in a function. | |
4115 | Save the result of the first call and reuse it. */ | |
4116 | if (saveregs_value != 0) | |
4117 | return saveregs_value; | |
53800dbe | 4118 | |
a66c9326 | 4119 | /* When this function is called, it means that registers must be |
4120 | saved on entry to this function. So we migrate the call to the | |
4121 | first insn of this function. */ | |
4122 | ||
4123 | start_sequence (); | |
53800dbe | 4124 | |
a66c9326 | 4125 | /* Do whatever the machine needs done in this case. */ |
45550790 | 4126 | val = targetm.calls.expand_builtin_saveregs (); |
53800dbe | 4127 | |
a66c9326 | 4128 | seq = get_insns (); |
4129 | end_sequence (); | |
53800dbe | 4130 | |
a66c9326 | 4131 | saveregs_value = val; |
53800dbe | 4132 | |
31d3e01c | 4133 | /* Put the insns after the NOTE that starts the function. If this |
4134 | is inside a start_sequence, make the outer-level insn chain current, so | |
a66c9326 | 4135 | the code is placed at the start of the function. */ |
4136 | push_topmost_sequence (); | |
0ec80471 | 4137 | emit_insn_after (seq, entry_of_function ()); |
a66c9326 | 4138 | pop_topmost_sequence (); |
4139 | ||
4140 | return val; | |
53800dbe | 4141 | } |
4142 | ||
4143 | /* __builtin_args_info (N) returns word N of the arg space info | |
4144 | for the current function. The number and meanings of words | |
4145 | is controlled by the definition of CUMULATIVE_ARGS. */ | |
f7c44134 | 4146 | |
53800dbe | 4147 | static rtx |
aecda0d6 | 4148 | expand_builtin_args_info (tree arglist) |
53800dbe | 4149 | { |
53800dbe | 4150 | int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int); |
4151 | int *word_ptr = (int *) ¤t_function_args_info; | |
53800dbe | 4152 | |
64db345d | 4153 | gcc_assert (sizeof (CUMULATIVE_ARGS) % sizeof (int) == 0); |
53800dbe | 4154 | |
4155 | if (arglist != 0) | |
4156 | { | |
27d0c333 | 4157 | if (!host_integerp (TREE_VALUE (arglist), 0)) |
eb586f2c | 4158 | error ("argument of %<__builtin_args_info%> must be constant"); |
53800dbe | 4159 | else |
4160 | { | |
27d0c333 | 4161 | HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0); |
53800dbe | 4162 | |
27d0c333 | 4163 | if (wordnum < 0 || wordnum >= nwords) |
eb586f2c | 4164 | error ("argument of %<__builtin_args_info%> out of range"); |
53800dbe | 4165 | else |
4166 | return GEN_INT (word_ptr[wordnum]); | |
4167 | } | |
4168 | } | |
4169 | else | |
eb586f2c | 4170 | error ("missing argument in %<__builtin_args_info%>"); |
53800dbe | 4171 | |
4172 | return const0_rtx; | |
53800dbe | 4173 | } |
4174 | ||
79012a9d | 4175 | /* Expand a call to __builtin_next_arg. */ |
27d0c333 | 4176 | |
53800dbe | 4177 | static rtx |
79012a9d | 4178 | expand_builtin_next_arg (void) |
53800dbe | 4179 | { |
79012a9d | 4180 | /* Checking arguments is already done in fold_builtin_next_arg |
4181 | that must be called before this function. */ | |
53800dbe | 4182 | return expand_binop (Pmode, add_optab, |
4183 | current_function_internal_arg_pointer, | |
4184 | current_function_arg_offset_rtx, | |
4185 | NULL_RTX, 0, OPTAB_LIB_WIDEN); | |
4186 | } | |
4187 | ||
a66c9326 | 4188 | /* Make it easier for the backends by protecting the valist argument |
4189 | from multiple evaluations. */ | |
4190 | ||
4191 | static tree | |
aecda0d6 | 4192 | stabilize_va_list (tree valist, int needs_lvalue) |
a66c9326 | 4193 | { |
11a61dea | 4194 | if (TREE_CODE (va_list_type_node) == ARRAY_TYPE) |
a66c9326 | 4195 | { |
2d47cc32 | 4196 | if (TREE_SIDE_EFFECTS (valist)) |
4197 | valist = save_expr (valist); | |
11a61dea | 4198 | |
2d47cc32 | 4199 | /* For this case, the backends will be expecting a pointer to |
4200 | TREE_TYPE (va_list_type_node), but it's possible we've | |
4201 | actually been given an array (an actual va_list_type_node). | |
4202 | So fix it. */ | |
4203 | if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE) | |
8a15c04a | 4204 | { |
bf8e3599 | 4205 | tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node)); |
fcdd3ab3 | 4206 | valist = build_fold_addr_expr_with_type (valist, p1); |
8a15c04a | 4207 | } |
a66c9326 | 4208 | } |
11a61dea | 4209 | else |
a66c9326 | 4210 | { |
2d47cc32 | 4211 | tree pt; |
11a61dea | 4212 | |
2d47cc32 | 4213 | if (! needs_lvalue) |
4214 | { | |
11a61dea | 4215 | if (! TREE_SIDE_EFFECTS (valist)) |
4216 | return valist; | |
bf8e3599 | 4217 | |
11a61dea | 4218 | pt = build_pointer_type (va_list_type_node); |
49d00087 | 4219 | valist = fold_build1 (ADDR_EXPR, pt, valist); |
a66c9326 | 4220 | TREE_SIDE_EFFECTS (valist) = 1; |
a66c9326 | 4221 | } |
2d47cc32 | 4222 | |
11a61dea | 4223 | if (TREE_SIDE_EFFECTS (valist)) |
2d47cc32 | 4224 | valist = save_expr (valist); |
fcdd3ab3 | 4225 | valist = build_fold_indirect_ref (valist); |
a66c9326 | 4226 | } |
4227 | ||
4228 | return valist; | |
4229 | } | |
4230 | ||
2e15d750 | 4231 | /* The "standard" definition of va_list is void*. */ |
4232 | ||
4233 | tree | |
4234 | std_build_builtin_va_list (void) | |
4235 | { | |
4236 | return ptr_type_node; | |
4237 | } | |
4238 | ||
a66c9326 | 4239 | /* The "standard" implementation of va_start: just assign `nextarg' to |
4240 | the variable. */ | |
27d0c333 | 4241 | |
a66c9326 | 4242 | void |
aecda0d6 | 4243 | std_expand_builtin_va_start (tree valist, rtx nextarg) |
a66c9326 | 4244 | { |
4245 | tree t; | |
4246 | ||
05abc81b | 4247 | t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, |
4248 | make_tree (ptr_type_node, nextarg)); | |
a66c9326 | 4249 | TREE_SIDE_EFFECTS (t) = 1; |
4250 | ||
4251 | expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
4252 | } | |
4253 | ||
7ccc713a | 4254 | /* Expand ARGLIST, from a call to __builtin_va_start. */ |
27d0c333 | 4255 | |
a66c9326 | 4256 | static rtx |
aecda0d6 | 4257 | expand_builtin_va_start (tree arglist) |
a66c9326 | 4258 | { |
4259 | rtx nextarg; | |
7ccc713a | 4260 | tree chain, valist; |
a66c9326 | 4261 | |
7ccc713a | 4262 | chain = TREE_CHAIN (arglist); |
a66c9326 | 4263 | |
cb166087 | 4264 | if (!chain) |
4265 | { | |
4266 | error ("too few arguments to function %<va_start%>"); | |
4267 | return const0_rtx; | |
4268 | } | |
a66c9326 | 4269 | |
743b0c6a | 4270 | if (fold_builtin_next_arg (chain)) |
79012a9d | 4271 | return const0_rtx; |
7c2f0500 | 4272 | |
79012a9d | 4273 | nextarg = expand_builtin_next_arg (); |
a66c9326 | 4274 | valist = stabilize_va_list (TREE_VALUE (arglist), 1); |
4275 | ||
4276 | #ifdef EXPAND_BUILTIN_VA_START | |
7df226a2 | 4277 | EXPAND_BUILTIN_VA_START (valist, nextarg); |
a66c9326 | 4278 | #else |
7df226a2 | 4279 | std_expand_builtin_va_start (valist, nextarg); |
a66c9326 | 4280 | #endif |
4281 | ||
4282 | return const0_rtx; | |
4283 | } | |
4284 | ||
a66c9326 | 4285 | /* The "standard" implementation of va_arg: read the value from the |
4286 | current (padded) address and increment by the (padded) size. */ | |
f7c44134 | 4287 | |
e0eca1fa | 4288 | tree |
4289 | std_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p) | |
fcdd3ab3 | 4290 | { |
52cc2eeb | 4291 | tree addr, t, type_size, rounded_size, valist_tmp; |
b8e8e485 | 4292 | unsigned HOST_WIDE_INT align, boundary; |
bef380a4 | 4293 | bool indirect; |
52cc2eeb | 4294 | |
4295 | #ifdef ARGS_GROW_DOWNWARD | |
4296 | /* All of the alignment and movement below is for args-grow-up machines. | |
4297 | As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all | |
4298 | implement their own specialized gimplify_va_arg_expr routines. */ | |
64db345d | 4299 | gcc_unreachable (); |
52cc2eeb | 4300 | #endif |
fcdd3ab3 | 4301 | |
bef380a4 | 4302 | indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false); |
4303 | if (indirect) | |
4304 | type = build_pointer_type (type); | |
4305 | ||
52cc2eeb | 4306 | align = PARM_BOUNDARY / BITS_PER_UNIT; |
bcff3604 | 4307 | boundary = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type) / BITS_PER_UNIT; |
fcdd3ab3 | 4308 | |
52cc2eeb | 4309 | /* Hoist the valist value into a temporary for the moment. */ |
ecdeeb37 | 4310 | valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL); |
4311 | ||
fcdd3ab3 | 4312 | /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually |
4313 | requires greater alignment, we must perform dynamic alignment. */ | |
85c1f587 | 4314 | if (boundary > align |
4315 | && !integer_zerop (TYPE_SIZE (type))) | |
fcdd3ab3 | 4316 | { |
bcff3604 | 4317 | t = fold_convert (TREE_TYPE (valist), size_int (boundary - 1)); |
ecdeeb37 | 4318 | t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp, |
52cc2eeb | 4319 | build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t)); |
4320 | gimplify_and_add (t, pre_p); | |
4321 | ||
bcff3604 | 4322 | t = fold_convert (TREE_TYPE (valist), size_int (-boundary)); |
52cc2eeb | 4323 | t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp, |
4324 | build2 (BIT_AND_EXPR, TREE_TYPE (valist), valist_tmp, t)); | |
ecdeeb37 | 4325 | gimplify_and_add (t, pre_p); |
fcdd3ab3 | 4326 | } |
c5dc0c32 | 4327 | else |
4328 | boundary = align; | |
4329 | ||
4330 | /* If the actual alignment is less than the alignment of the type, | |
4331 | adjust the type accordingly so that we don't assume strict alignment | |
4332 | when deferencing the pointer. */ | |
4333 | boundary *= BITS_PER_UNIT; | |
4334 | if (boundary < TYPE_ALIGN (type)) | |
4335 | { | |
4336 | type = build_variant_type_copy (type); | |
4337 | TYPE_ALIGN (type) = boundary; | |
4338 | } | |
fcdd3ab3 | 4339 | |
bcff3604 | 4340 | /* Compute the rounded size of the type. */ |
52cc2eeb | 4341 | type_size = size_in_bytes (type); |
4342 | rounded_size = round_up (type_size, align); | |
4343 | ||
fcdd3ab3 | 4344 | /* Reduce rounded_size so it's sharable with the postqueue. */ |
4345 | gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue); | |
4346 | ||
4347 | /* Get AP. */ | |
ecdeeb37 | 4348 | addr = valist_tmp; |
52cc2eeb | 4349 | if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size)) |
fcdd3ab3 | 4350 | { |
4351 | /* Small args are padded downward. */ | |
49d00087 | 4352 | t = fold_build2 (GT_EXPR, sizetype, rounded_size, size_int (align)); |
4353 | t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node, | |
4354 | size_binop (MINUS_EXPR, rounded_size, type_size)); | |
52cc2eeb | 4355 | t = fold_convert (TREE_TYPE (addr), t); |
49d00087 | 4356 | addr = fold_build2 (PLUS_EXPR, TREE_TYPE (addr), addr, t); |
fcdd3ab3 | 4357 | } |
4358 | ||
fcdd3ab3 | 4359 | /* Compute new value for AP. */ |
52cc2eeb | 4360 | t = fold_convert (TREE_TYPE (valist), rounded_size); |
4361 | t = build2 (PLUS_EXPR, TREE_TYPE (valist), valist_tmp, t); | |
4362 | t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t); | |
ecdeeb37 | 4363 | gimplify_and_add (t, pre_p); |
e0eca1fa | 4364 | |
4365 | addr = fold_convert (build_pointer_type (type), addr); | |
fcdd3ab3 | 4366 | |
bef380a4 | 4367 | if (indirect) |
f2462d6c | 4368 | addr = build_va_arg_indirect_ref (addr); |
4369 | ||
4370 | return build_va_arg_indirect_ref (addr); | |
4371 | } | |
a0930a69 | 4372 | |
f2462d6c | 4373 | /* Build an indirect-ref expression over the given TREE, which represents a |
4374 | piece of a va_arg() expansion. */ | |
4375 | tree | |
4376 | build_va_arg_indirect_ref (tree addr) | |
4377 | { | |
a0930a69 | 4378 | addr = build_fold_indirect_ref (addr); |
f2462d6c | 4379 | |
a0930a69 | 4380 | if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */ |
4381 | mf_mark (addr); | |
bef380a4 | 4382 | |
a0930a69 | 4383 | return addr; |
433006a9 | 4384 | } |
4385 | ||
fcdd3ab3 | 4386 | /* Return a dummy expression of type TYPE in order to keep going after an |
4387 | error. */ | |
4388 | ||
4389 | static tree | |
4390 | dummy_object (tree type) | |
4391 | { | |
535664e3 | 4392 | tree t = build_int_cst (build_pointer_type (type), 0); |
fcdd3ab3 | 4393 | return build1 (INDIRECT_REF, type, t); |
4394 | } | |
4395 | ||
2799a2b7 | 4396 | /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a |
4397 | builtin function, but a very special sort of operator. */ | |
fcdd3ab3 | 4398 | |
4399 | enum gimplify_status | |
4400 | gimplify_va_arg_expr (tree *expr_p, tree *pre_p, tree *post_p) | |
4401 | { | |
4402 | tree promoted_type, want_va_type, have_va_type; | |
4403 | tree valist = TREE_OPERAND (*expr_p, 0); | |
4404 | tree type = TREE_TYPE (*expr_p); | |
4405 | tree t; | |
4406 | ||
4407 | /* Verify that valist is of the proper type. */ | |
fcdd3ab3 | 4408 | want_va_type = va_list_type_node; |
4409 | have_va_type = TREE_TYPE (valist); | |
f43dda35 | 4410 | |
4411 | if (have_va_type == error_mark_node) | |
4412 | return GS_ERROR; | |
4413 | ||
fcdd3ab3 | 4414 | if (TREE_CODE (want_va_type) == ARRAY_TYPE) |
4415 | { | |
4416 | /* If va_list is an array type, the argument may have decayed | |
4417 | to a pointer type, e.g. by being passed to another function. | |
a0c938f0 | 4418 | In that case, unwrap both types so that we can compare the |
fcdd3ab3 | 4419 | underlying records. */ |
4420 | if (TREE_CODE (have_va_type) == ARRAY_TYPE | |
552752f7 | 4421 | || POINTER_TYPE_P (have_va_type)) |
fcdd3ab3 | 4422 | { |
4423 | want_va_type = TREE_TYPE (want_va_type); | |
4424 | have_va_type = TREE_TYPE (have_va_type); | |
4425 | } | |
4426 | } | |
4427 | ||
4428 | if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type)) | |
4429 | { | |
eb586f2c | 4430 | error ("first argument to %<va_arg%> not of type %<va_list%>"); |
e0eca1fa | 4431 | return GS_ERROR; |
fcdd3ab3 | 4432 | } |
4433 | ||
4434 | /* Generate a diagnostic for requesting data of a type that cannot | |
4435 | be passed through `...' due to type promotion at the call site. */ | |
4436 | else if ((promoted_type = lang_hooks.types.type_promotes_to (type)) | |
4437 | != type) | |
4438 | { | |
4439 | static bool gave_help; | |
4440 | ||
4441 | /* Unfortunately, this is merely undefined, rather than a constraint | |
4442 | violation, so we cannot make this an error. If this call is never | |
4443 | executed, the program is still strictly conforming. */ | |
c3ceba8e | 4444 | warning (0, "%qT is promoted to %qT when passed through %<...%>", |
fcdd3ab3 | 4445 | type, promoted_type); |
4446 | if (! gave_help) | |
4447 | { | |
4448 | gave_help = true; | |
c3ceba8e | 4449 | warning (0, "(so you should pass %qT not %qT to %<va_arg%>)", |
fcdd3ab3 | 4450 | promoted_type, type); |
4451 | } | |
4452 | ||
4453 | /* We can, however, treat "undefined" any way we please. | |
4454 | Call abort to encourage the user to fix the program. */ | |
4455 | inform ("if this code is reached, the program will abort"); | |
4456 | t = build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP], | |
4457 | NULL); | |
4458 | append_to_statement_list (t, pre_p); | |
4459 | ||
4460 | /* This is dead code, but go ahead and finish so that the | |
4461 | mode of the result comes out right. */ | |
4462 | *expr_p = dummy_object (type); | |
4463 | return GS_ALL_DONE; | |
4464 | } | |
4465 | else | |
4466 | { | |
4467 | /* Make it easier for the backends by protecting the valist argument | |
a0c938f0 | 4468 | from multiple evaluations. */ |
e0eca1fa | 4469 | if (TREE_CODE (va_list_type_node) == ARRAY_TYPE) |
4470 | { | |
4471 | /* For this case, the backends will be expecting a pointer to | |
4472 | TREE_TYPE (va_list_type_node), but it's possible we've | |
4473 | actually been given an array (an actual va_list_type_node). | |
4474 | So fix it. */ | |
4475 | if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE) | |
4476 | { | |
4477 | tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node)); | |
4478 | valist = build_fold_addr_expr_with_type (valist, p1); | |
4479 | } | |
4480 | gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue); | |
4481 | } | |
4482 | else | |
4483 | gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue); | |
fcdd3ab3 | 4484 | |
17a6380d | 4485 | if (!targetm.gimplify_va_arg_expr) |
89f18f73 | 4486 | /* FIXME:Once most targets are converted we should merely |
4487 | assert this is non-null. */ | |
fcdd3ab3 | 4488 | return GS_ALL_DONE; |
4489 | ||
17a6380d | 4490 | *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p); |
fcdd3ab3 | 4491 | return GS_OK; |
4492 | } | |
4493 | } | |
4494 | ||
a66c9326 | 4495 | /* Expand ARGLIST, from a call to __builtin_va_end. */ |
f7c44134 | 4496 | |
a66c9326 | 4497 | static rtx |
aecda0d6 | 4498 | expand_builtin_va_end (tree arglist) |
a66c9326 | 4499 | { |
8a15c04a | 4500 | tree valist = TREE_VALUE (arglist); |
4501 | ||
8a15c04a | 4502 | /* Evaluate for side effects, if needed. I hate macros that don't |
4503 | do that. */ | |
4504 | if (TREE_SIDE_EFFECTS (valist)) | |
4505 | expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
a66c9326 | 4506 | |
4507 | return const0_rtx; | |
4508 | } | |
4509 | ||
bf8e3599 | 4510 | /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a |
a66c9326 | 4511 | builtin rather than just as an assignment in stdarg.h because of the |
4512 | nastiness of array-type va_list types. */ | |
f7c44134 | 4513 | |
a66c9326 | 4514 | static rtx |
aecda0d6 | 4515 | expand_builtin_va_copy (tree arglist) |
a66c9326 | 4516 | { |
4517 | tree dst, src, t; | |
4518 | ||
4519 | dst = TREE_VALUE (arglist); | |
4520 | src = TREE_VALUE (TREE_CHAIN (arglist)); | |
4521 | ||
4522 | dst = stabilize_va_list (dst, 1); | |
4523 | src = stabilize_va_list (src, 0); | |
4524 | ||
4525 | if (TREE_CODE (va_list_type_node) != ARRAY_TYPE) | |
4526 | { | |
05abc81b | 4527 | t = build2 (MODIFY_EXPR, va_list_type_node, dst, src); |
a66c9326 | 4528 | TREE_SIDE_EFFECTS (t) = 1; |
4529 | expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
4530 | } | |
4531 | else | |
4532 | { | |
11a61dea | 4533 | rtx dstb, srcb, size; |
4534 | ||
4535 | /* Evaluate to pointers. */ | |
4536 | dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL); | |
4537 | srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL); | |
4538 | size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX, | |
4539 | VOIDmode, EXPAND_NORMAL); | |
4540 | ||
85d654dd | 4541 | dstb = convert_memory_address (Pmode, dstb); |
4542 | srcb = convert_memory_address (Pmode, srcb); | |
726ec87c | 4543 | |
11a61dea | 4544 | /* "Dereference" to BLKmode memories. */ |
4545 | dstb = gen_rtx_MEM (BLKmode, dstb); | |
ab6ab77e | 4546 | set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst)))); |
2a631e19 | 4547 | set_mem_align (dstb, TYPE_ALIGN (va_list_type_node)); |
11a61dea | 4548 | srcb = gen_rtx_MEM (BLKmode, srcb); |
ab6ab77e | 4549 | set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src)))); |
2a631e19 | 4550 | set_mem_align (srcb, TYPE_ALIGN (va_list_type_node)); |
11a61dea | 4551 | |
4552 | /* Copy. */ | |
0378dbdc | 4553 | emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL); |
a66c9326 | 4554 | } |
4555 | ||
4556 | return const0_rtx; | |
4557 | } | |
4558 | ||
53800dbe | 4559 | /* Expand a call to one of the builtin functions __builtin_frame_address or |
4560 | __builtin_return_address. */ | |
27d0c333 | 4561 | |
53800dbe | 4562 | static rtx |
aecda0d6 | 4563 | expand_builtin_frame_address (tree fndecl, tree arglist) |
53800dbe | 4564 | { |
53800dbe | 4565 | /* The argument must be a nonnegative integer constant. |
4566 | It counts the number of frames to scan up the stack. | |
4567 | The value is the return address saved in that frame. */ | |
4568 | if (arglist == 0) | |
4569 | /* Warning about missing arg was already issued. */ | |
4570 | return const0_rtx; | |
27d0c333 | 4571 | else if (! host_integerp (TREE_VALUE (arglist), 1)) |
53800dbe | 4572 | { |
4573 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS) | |
07e3a3d2 | 4574 | error ("invalid argument to %<__builtin_frame_address%>"); |
53800dbe | 4575 | else |
07e3a3d2 | 4576 | error ("invalid argument to %<__builtin_return_address%>"); |
53800dbe | 4577 | return const0_rtx; |
4578 | } | |
4579 | else | |
4580 | { | |
27d0c333 | 4581 | rtx tem |
4582 | = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), | |
869d0ef0 | 4583 | tree_low_cst (TREE_VALUE (arglist), 1)); |
53800dbe | 4584 | |
4585 | /* Some ports cannot access arbitrary stack frames. */ | |
4586 | if (tem == NULL) | |
4587 | { | |
4588 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS) | |
c3ceba8e | 4589 | warning (0, "unsupported argument to %<__builtin_frame_address%>"); |
53800dbe | 4590 | else |
c3ceba8e | 4591 | warning (0, "unsupported argument to %<__builtin_return_address%>"); |
53800dbe | 4592 | return const0_rtx; |
4593 | } | |
4594 | ||
4595 | /* For __builtin_frame_address, return what we've got. */ | |
4596 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS) | |
4597 | return tem; | |
4598 | ||
8ad4c111 | 4599 | if (!REG_P (tem) |
53800dbe | 4600 | && ! CONSTANT_P (tem)) |
4601 | tem = copy_to_mode_reg (Pmode, tem); | |
4602 | return tem; | |
4603 | } | |
4604 | } | |
4605 | ||
4606 | /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if | |
4607 | we failed and the caller should emit a normal call, otherwise try to get | |
4608 | the result in TARGET, if convenient. */ | |
15c6cf6b | 4609 | |
53800dbe | 4610 | static rtx |
aecda0d6 | 4611 | expand_builtin_alloca (tree arglist, rtx target) |
53800dbe | 4612 | { |
4613 | rtx op0; | |
15c6cf6b | 4614 | rtx result; |
53800dbe | 4615 | |
4ee9c684 | 4616 | /* In -fmudflap-instrumented code, alloca() and __builtin_alloca() |
4617 | should always expand to function calls. These can be intercepted | |
4618 | in libmudflap. */ | |
4619 | if (flag_mudflap) | |
4620 | return 0; | |
4621 | ||
0eb671f7 | 4622 | if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE)) |
53800dbe | 4623 | return 0; |
4624 | ||
4625 | /* Compute the argument. */ | |
8ec3c5c2 | 4626 | op0 = expand_normal (TREE_VALUE (arglist)); |
53800dbe | 4627 | |
4628 | /* Allocate the desired space. */ | |
15c6cf6b | 4629 | result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT); |
85d654dd | 4630 | result = convert_memory_address (ptr_mode, result); |
15c6cf6b | 4631 | |
4632 | return result; | |
53800dbe | 4633 | } |
4634 | ||
6a08d0ab | 4635 | /* Expand a call to a unary builtin. The arguments are in ARGLIST. |
53800dbe | 4636 | Return 0 if a normal call should be emitted rather than expanding the |
4637 | function in-line. If convenient, the result should be placed in TARGET. | |
4638 | SUBTARGET may be used as the target for computing one of EXP's operands. */ | |
15c6cf6b | 4639 | |
53800dbe | 4640 | static rtx |
aecda0d6 | 4641 | expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target, |
4642 | rtx subtarget, optab op_optab) | |
53800dbe | 4643 | { |
4644 | rtx op0; | |
0eb671f7 | 4645 | if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE)) |
53800dbe | 4646 | return 0; |
4647 | ||
4648 | /* Compute the argument. */ | |
4649 | op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0); | |
6a08d0ab | 4650 | /* Compute op, into TARGET if possible. |
53800dbe | 4651 | Set TARGET to wherever the result comes back. */ |
4652 | target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))), | |
6a08d0ab | 4653 | op_optab, op0, target, 1); |
64db345d | 4654 | gcc_assert (target); |
7d3f6cc7 | 4655 | |
efb070c8 | 4656 | return convert_to_mode (target_mode, target, 0); |
53800dbe | 4657 | } |
89cfe6e5 | 4658 | |
df94cd3b | 4659 | /* If the string passed to fputs is a constant and is one character |
2c0e001b | 4660 | long, we attempt to transform this call into __builtin_fputc(). */ |
15c6cf6b | 4661 | |
df94cd3b | 4662 | static rtx |
19bf118a | 4663 | expand_builtin_fputs (tree arglist, rtx target, bool unlocked) |
df94cd3b | 4664 | { |
2c0e001b | 4665 | /* Verify the arguments in the original call. */ |
2c84ee37 | 4666 | if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
ce1b14f4 | 4667 | { |
2c84ee37 | 4668 | tree result = fold_builtin_fputs (arglist, (target == const0_rtx), |
4669 | unlocked, NULL_TREE); | |
4670 | if (result) | |
4671 | return expand_expr (result, target, VOIDmode, EXPAND_NORMAL); | |
ce1b14f4 | 4672 | } |
2c84ee37 | 4673 | return 0; |
3311f67b | 4674 | } |
4675 | ||
689df48e | 4676 | /* Expand a call to __builtin_expect. We return our argument and emit a |
4677 | NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in | |
4678 | a non-jump context. */ | |
89cfe6e5 | 4679 | |
4680 | static rtx | |
aecda0d6 | 4681 | expand_builtin_expect (tree arglist, rtx target) |
89cfe6e5 | 4682 | { |
4683 | tree exp, c; | |
4684 | rtx note, rtx_c; | |
4685 | ||
4686 | if (arglist == NULL_TREE | |
4687 | || TREE_CHAIN (arglist) == NULL_TREE) | |
4688 | return const0_rtx; | |
4689 | exp = TREE_VALUE (arglist); | |
4690 | c = TREE_VALUE (TREE_CHAIN (arglist)); | |
4691 | ||
4692 | if (TREE_CODE (c) != INTEGER_CST) | |
4693 | { | |
07e3a3d2 | 4694 | error ("second argument to %<__builtin_expect%> must be a constant"); |
89cfe6e5 | 4695 | c = integer_zero_node; |
4696 | } | |
4697 | ||
4698 | target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL); | |
4699 | ||
4700 | /* Don't bother with expected value notes for integral constants. */ | |
b28bedce | 4701 | if (flag_guess_branch_prob && GET_CODE (target) != CONST_INT) |
89cfe6e5 | 4702 | { |
4703 | /* We do need to force this into a register so that we can be | |
4704 | moderately sure to be able to correctly interpret the branch | |
4705 | condition later. */ | |
4706 | target = force_reg (GET_MODE (target), target); | |
bf8e3599 | 4707 | |
89cfe6e5 | 4708 | rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL); |
4709 | ||
31b97e8f | 4710 | note = emit_note (NOTE_INSN_EXPECTED_VALUE); |
89cfe6e5 | 4711 | NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c); |
4712 | } | |
4713 | ||
4714 | return target; | |
4715 | } | |
689df48e | 4716 | |
4717 | /* Like expand_builtin_expect, except do this in a jump context. This is | |
4718 | called from do_jump if the conditional is a __builtin_expect. Return either | |
31d3e01c | 4719 | a list of insns to emit the jump or NULL if we cannot optimize |
689df48e | 4720 | __builtin_expect. We need to optimize this at jump time so that machines |
4721 | like the PowerPC don't turn the test into a SCC operation, and then jump | |
4722 | based on the test being 0/1. */ | |
4723 | ||
4724 | rtx | |
aecda0d6 | 4725 | expand_builtin_expect_jump (tree exp, rtx if_false_label, rtx if_true_label) |
689df48e | 4726 | { |
4727 | tree arglist = TREE_OPERAND (exp, 1); | |
4728 | tree arg0 = TREE_VALUE (arglist); | |
4729 | tree arg1 = TREE_VALUE (TREE_CHAIN (arglist)); | |
4730 | rtx ret = NULL_RTX; | |
4731 | ||
4732 | /* Only handle __builtin_expect (test, 0) and | |
4733 | __builtin_expect (test, 1). */ | |
4734 | if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE | |
27d0c333 | 4735 | && (integer_zerop (arg1) || integer_onep (arg1))) |
689df48e | 4736 | { |
6372a134 | 4737 | rtx insn, drop_through_label, temp; |
1dc55962 | 4738 | |
689df48e | 4739 | /* Expand the jump insns. */ |
4740 | start_sequence (); | |
4741 | do_jump (arg0, if_false_label, if_true_label); | |
31d3e01c | 4742 | ret = get_insns (); |
15c85053 | 4743 | |
4744 | drop_through_label = get_last_insn (); | |
6d7dc5b9 | 4745 | if (drop_through_label && NOTE_P (drop_through_label)) |
15c85053 | 4746 | drop_through_label = prev_nonnote_insn (drop_through_label); |
6d7dc5b9 | 4747 | if (drop_through_label && !LABEL_P (drop_through_label)) |
15c85053 | 4748 | drop_through_label = NULL_RTX; |
689df48e | 4749 | end_sequence (); |
4750 | ||
15c85053 | 4751 | if (! if_true_label) |
4752 | if_true_label = drop_through_label; | |
4753 | if (! if_false_label) | |
4754 | if_false_label = drop_through_label; | |
55a723b1 | 4755 | |
6c04b2f1 | 4756 | /* Go through and add the expect's to each of the conditional jumps. */ |
31d3e01c | 4757 | insn = ret; |
4758 | while (insn != NULL_RTX) | |
689df48e | 4759 | { |
31d3e01c | 4760 | rtx next = NEXT_INSN (insn); |
689df48e | 4761 | |
6d7dc5b9 | 4762 | if (JUMP_P (insn) && any_condjump_p (insn)) |
689df48e | 4763 | { |
7caedf36 | 4764 | rtx ifelse = SET_SRC (pc_set (insn)); |
6c04b2f1 | 4765 | rtx then_dest = XEXP (ifelse, 1); |
4766 | rtx else_dest = XEXP (ifelse, 2); | |
4767 | int taken = -1; | |
4768 | ||
4769 | /* First check if we recognize any of the labels. */ | |
4770 | if (GET_CODE (then_dest) == LABEL_REF | |
b7b16e57 | 4771 | && XEXP (then_dest, 0) == if_true_label) |
6c04b2f1 | 4772 | taken = 1; |
4773 | else if (GET_CODE (then_dest) == LABEL_REF | |
b7b16e57 | 4774 | && XEXP (then_dest, 0) == if_false_label) |
6c04b2f1 | 4775 | taken = 0; |
4776 | else if (GET_CODE (else_dest) == LABEL_REF | |
b7b16e57 | 4777 | && XEXP (else_dest, 0) == if_false_label) |
6c04b2f1 | 4778 | taken = 1; |
4779 | else if (GET_CODE (else_dest) == LABEL_REF | |
b7b16e57 | 4780 | && XEXP (else_dest, 0) == if_true_label) |
6c04b2f1 | 4781 | taken = 0; |
4782 | /* Otherwise check where we drop through. */ | |
4783 | else if (else_dest == pc_rtx) | |
689df48e | 4784 | { |
6d7dc5b9 | 4785 | if (next && NOTE_P (next)) |
6c04b2f1 | 4786 | next = next_nonnote_insn (next); |
4787 | ||
6d7dc5b9 | 4788 | if (next && JUMP_P (next) |
6c04b2f1 | 4789 | && any_uncondjump_p (next)) |
6372a134 | 4790 | temp = XEXP (SET_SRC (pc_set (next)), 0); |
4791 | else | |
4792 | temp = next; | |
6c04b2f1 | 4793 | |
6372a134 | 4794 | /* TEMP is either a CODE_LABEL, NULL_RTX or something |
6c04b2f1 | 4795 | else that can't possibly match either target label. */ |
6372a134 | 4796 | if (temp == if_false_label) |
6c04b2f1 | 4797 | taken = 1; |
6372a134 | 4798 | else if (temp == if_true_label) |
6c04b2f1 | 4799 | taken = 0; |
689df48e | 4800 | } |
6c04b2f1 | 4801 | else if (then_dest == pc_rtx) |
689df48e | 4802 | { |
6d7dc5b9 | 4803 | if (next && NOTE_P (next)) |
6c04b2f1 | 4804 | next = next_nonnote_insn (next); |
4805 | ||
6d7dc5b9 | 4806 | if (next && JUMP_P (next) |
6c04b2f1 | 4807 | && any_uncondjump_p (next)) |
6372a134 | 4808 | temp = XEXP (SET_SRC (pc_set (next)), 0); |
4809 | else | |
4810 | temp = next; | |
6c04b2f1 | 4811 | |
6372a134 | 4812 | if (temp == if_false_label) |
6c04b2f1 | 4813 | taken = 0; |
6372a134 | 4814 | else if (temp == if_true_label) |
6c04b2f1 | 4815 | taken = 1; |
689df48e | 4816 | } |
6c04b2f1 | 4817 | |
4818 | if (taken != -1) | |
689df48e | 4819 | { |
6c04b2f1 | 4820 | /* If the test is expected to fail, reverse the |
4821 | probabilities. */ | |
4822 | if (integer_zerop (arg1)) | |
4823 | taken = 1 - taken; | |
a0c938f0 | 4824 | predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken); |
689df48e | 4825 | } |
689df48e | 4826 | } |
31d3e01c | 4827 | |
31d3e01c | 4828 | insn = next; |
689df48e | 4829 | } |
689df48e | 4830 | } |
4831 | ||
4832 | return ret; | |
4833 | } | |
a0ef1725 | 4834 | |
c22de3f0 | 4835 | void |
aecda0d6 | 4836 | expand_builtin_trap (void) |
a0ef1725 | 4837 | { |
4838 | #ifdef HAVE_trap | |
4839 | if (HAVE_trap) | |
4840 | emit_insn (gen_trap ()); | |
4841 | else | |
4842 | #endif | |
4843 | emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0); | |
4844 | emit_barrier (); | |
4845 | } | |
78a74442 | 4846 | |
4847 | /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST. | |
4848 | Return 0 if a normal call should be emitted rather than expanding | |
4849 | the function inline. If convenient, the result should be placed | |
4850 | in TARGET. SUBTARGET may be used as the target for computing | |
4851 | the operand. */ | |
4852 | ||
4853 | static rtx | |
aecda0d6 | 4854 | expand_builtin_fabs (tree arglist, rtx target, rtx subtarget) |
78a74442 | 4855 | { |
4856 | enum machine_mode mode; | |
4857 | tree arg; | |
4858 | rtx op0; | |
4859 | ||
4860 | if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
4861 | return 0; | |
4862 | ||
4863 | arg = TREE_VALUE (arglist); | |
4864 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
4865 | op0 = expand_expr (arg, subtarget, VOIDmode, 0); | |
4866 | return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1)); | |
4867 | } | |
4868 | ||
270436f3 | 4869 | /* Expand a call to copysign, copysignf, or copysignl with arguments ARGLIST. |
4870 | Return NULL is a normal call should be emitted rather than expanding the | |
4871 | function inline. If convenient, the result should be placed in TARGET. | |
4872 | SUBTARGET may be used as the target for computing the operand. */ | |
4873 | ||
4874 | static rtx | |
4875 | expand_builtin_copysign (tree arglist, rtx target, rtx subtarget) | |
4876 | { | |
4877 | rtx op0, op1; | |
4878 | tree arg; | |
4879 | ||
4880 | if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE)) | |
4881 | return 0; | |
4882 | ||
4883 | arg = TREE_VALUE (arglist); | |
8ec3c5c2 | 4884 | op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL); |
270436f3 | 4885 | |
4886 | arg = TREE_VALUE (TREE_CHAIN (arglist)); | |
8ec3c5c2 | 4887 | op1 = expand_normal (arg); |
270436f3 | 4888 | |
4889 | return expand_copysign (op0, op1, target); | |
4890 | } | |
4891 | ||
19bf118a | 4892 | /* Create a new constant string literal and return a char* pointer to it. |
4893 | The STRING_CST value is the LEN characters at STR. */ | |
1e8e9920 | 4894 | tree |
19bf118a | 4895 | build_string_literal (int len, const char *str) |
4896 | { | |
4897 | tree t, elem, index, type; | |
4898 | ||
4899 | t = build_string (len, str); | |
4900 | elem = build_type_variant (char_type_node, 1, 0); | |
7016c612 | 4901 | index = build_index_type (build_int_cst (NULL_TREE, len - 1)); |
19bf118a | 4902 | type = build_array_type (elem, index); |
4903 | TREE_TYPE (t) = type; | |
4904 | TREE_CONSTANT (t) = 1; | |
4ee9c684 | 4905 | TREE_INVARIANT (t) = 1; |
19bf118a | 4906 | TREE_READONLY (t) = 1; |
4907 | TREE_STATIC (t) = 1; | |
4908 | ||
4909 | type = build_pointer_type (type); | |
4910 | t = build1 (ADDR_EXPR, type, t); | |
4911 | ||
4912 | type = build_pointer_type (elem); | |
4913 | t = build1 (NOP_EXPR, type, t); | |
4914 | return t; | |
4915 | } | |
4916 | ||
0b25db21 | 4917 | /* Expand EXP, a call to printf or printf_unlocked. |
19bf118a | 4918 | Return 0 if a normal call should be emitted rather than transforming |
4919 | the function inline. If convenient, the result should be placed in | |
0862b7e9 | 4920 | TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked |
19bf118a | 4921 | call. */ |
4922 | static rtx | |
0b25db21 | 4923 | expand_builtin_printf (tree exp, rtx target, enum machine_mode mode, |
19bf118a | 4924 | bool unlocked) |
4925 | { | |
0b25db21 | 4926 | tree arglist = TREE_OPERAND (exp, 1); |
6d77ed92 | 4927 | /* If we're using an unlocked function, assume the other unlocked |
4928 | functions exist explicitly. */ | |
4929 | tree const fn_putchar = unlocked ? built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED] | |
4930 | : implicit_built_in_decls[BUILT_IN_PUTCHAR]; | |
4931 | tree const fn_puts = unlocked ? built_in_decls[BUILT_IN_PUTS_UNLOCKED] | |
4932 | : implicit_built_in_decls[BUILT_IN_PUTS]; | |
19bf118a | 4933 | const char *fmt_str; |
4934 | tree fn, fmt, arg; | |
4935 | ||
4936 | /* If the return value is used, don't do the transformation. */ | |
4937 | if (target != const0_rtx) | |
4938 | return 0; | |
4939 | ||
4940 | /* Verify the required arguments in the original call. */ | |
4941 | if (! arglist) | |
4942 | return 0; | |
4943 | fmt = TREE_VALUE (arglist); | |
552752f7 | 4944 | if (! POINTER_TYPE_P (TREE_TYPE (fmt))) |
19bf118a | 4945 | return 0; |
4946 | arglist = TREE_CHAIN (arglist); | |
4947 | ||
4948 | /* Check whether the format is a literal string constant. */ | |
4949 | fmt_str = c_getstr (fmt); | |
4950 | if (fmt_str == NULL) | |
4951 | return 0; | |
4952 | ||
99eabcc1 | 4953 | if (!init_target_chars()) |
4954 | return 0; | |
a0c938f0 | 4955 | |
19bf118a | 4956 | /* If the format specifier was "%s\n", call __builtin_puts(arg). */ |
99eabcc1 | 4957 | if (strcmp (fmt_str, target_percent_s_newline) == 0) |
19bf118a | 4958 | { |
4959 | if (! arglist | |
a0c938f0 | 4960 | || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))) |
19bf118a | 4961 | || TREE_CHAIN (arglist)) |
4962 | return 0; | |
4963 | fn = fn_puts; | |
4964 | } | |
4965 | /* If the format specifier was "%c", call __builtin_putchar(arg). */ | |
99eabcc1 | 4966 | else if (strcmp (fmt_str, target_percent_c) == 0) |
19bf118a | 4967 | { |
4968 | if (! arglist | |
4969 | || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE | |
4970 | || TREE_CHAIN (arglist)) | |
4971 | return 0; | |
4972 | fn = fn_putchar; | |
4973 | } | |
4974 | else | |
4975 | { | |
4976 | /* We can't handle anything else with % args or %% ... yet. */ | |
99eabcc1 | 4977 | if (strchr (fmt_str, target_percent)) |
a0c938f0 | 4978 | return 0; |
19bf118a | 4979 | |
4980 | if (arglist) | |
4981 | return 0; | |
4982 | ||
4983 | /* If the format specifier was "", printf does nothing. */ | |
4984 | if (fmt_str[0] == '\0') | |
4985 | return const0_rtx; | |
4986 | /* If the format specifier has length of 1, call putchar. */ | |
4987 | if (fmt_str[1] == '\0') | |
4988 | { | |
4989 | /* Given printf("c"), (where c is any one character,) | |
4990 | convert "c"[0] to an int and pass that to the replacement | |
4991 | function. */ | |
7016c612 | 4992 | arg = build_int_cst (NULL_TREE, fmt_str[0]); |
19bf118a | 4993 | arglist = build_tree_list (NULL_TREE, arg); |
4994 | fn = fn_putchar; | |
4995 | } | |
4996 | else | |
4997 | { | |
4998 | /* If the format specifier was "string\n", call puts("string"). */ | |
4999 | size_t len = strlen (fmt_str); | |
99eabcc1 | 5000 | if ((unsigned char)fmt_str[len - 1] == target_newline) |
19bf118a | 5001 | { |
91c82c20 | 5002 | /* Create a NUL-terminated string that's one char shorter |
19bf118a | 5003 | than the original, stripping off the trailing '\n'. */ |
4acc30e5 | 5004 | char *newstr = alloca (len); |
19bf118a | 5005 | memcpy (newstr, fmt_str, len - 1); |
5006 | newstr[len - 1] = 0; | |
5007 | ||
5008 | arg = build_string_literal (len, newstr); | |
5009 | arglist = build_tree_list (NULL_TREE, arg); | |
5010 | fn = fn_puts; | |
5011 | } | |
5012 | else | |
5013 | /* We'd like to arrange to call fputs(string,stdout) here, | |
5014 | but we need stdout and don't have a way to get it yet. */ | |
5015 | return 0; | |
5016 | } | |
5017 | } | |
5018 | ||
5019 | if (!fn) | |
5020 | return 0; | |
0b25db21 | 5021 | fn = build_function_call_expr (fn, arglist); |
5022 | if (TREE_CODE (fn) == CALL_EXPR) | |
5023 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
5024 | return expand_expr (fn, target, mode, EXPAND_NORMAL); | |
19bf118a | 5025 | } |
5026 | ||
0b25db21 | 5027 | /* Expand EXP, a call to fprintf or fprintf_unlocked. |
19bf118a | 5028 | Return 0 if a normal call should be emitted rather than transforming |
5029 | the function inline. If convenient, the result should be placed in | |
0862b7e9 | 5030 | TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked |
19bf118a | 5031 | call. */ |
5032 | static rtx | |
0b25db21 | 5033 | expand_builtin_fprintf (tree exp, rtx target, enum machine_mode mode, |
a0c938f0 | 5034 | bool unlocked) |
19bf118a | 5035 | { |
0b25db21 | 5036 | tree arglist = TREE_OPERAND (exp, 1); |
6d77ed92 | 5037 | /* If we're using an unlocked function, assume the other unlocked |
5038 | functions exist explicitly. */ | |
5039 | tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED] | |
5040 | : implicit_built_in_decls[BUILT_IN_FPUTC]; | |
5041 | tree const fn_fputs = unlocked ? built_in_decls[BUILT_IN_FPUTS_UNLOCKED] | |
5042 | : implicit_built_in_decls[BUILT_IN_FPUTS]; | |
19bf118a | 5043 | const char *fmt_str; |
5044 | tree fn, fmt, fp, arg; | |
5045 | ||
5046 | /* If the return value is used, don't do the transformation. */ | |
5047 | if (target != const0_rtx) | |
5048 | return 0; | |
5049 | ||
5050 | /* Verify the required arguments in the original call. */ | |
5051 | if (! arglist) | |
5052 | return 0; | |
5053 | fp = TREE_VALUE (arglist); | |
552752f7 | 5054 | if (! POINTER_TYPE_P (TREE_TYPE (fp))) |
19bf118a | 5055 | return 0; |
5056 | arglist = TREE_CHAIN (arglist); | |
5057 | if (! arglist) | |
5058 | return 0; | |
5059 | fmt = TREE_VALUE (arglist); | |
552752f7 | 5060 | if (! POINTER_TYPE_P (TREE_TYPE (fmt))) |
19bf118a | 5061 | return 0; |
5062 | arglist = TREE_CHAIN (arglist); | |
5063 | ||
5064 | /* Check whether the format is a literal string constant. */ | |
5065 | fmt_str = c_getstr (fmt); | |
5066 | if (fmt_str == NULL) | |
5067 | return 0; | |
5068 | ||
99eabcc1 | 5069 | if (!init_target_chars()) |
5070 | return 0; | |
a0c938f0 | 5071 | |
19bf118a | 5072 | /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */ |
99eabcc1 | 5073 | if (strcmp (fmt_str, target_percent_s) == 0) |
19bf118a | 5074 | { |
5075 | if (! arglist | |
a0c938f0 | 5076 | || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))) |
19bf118a | 5077 | || TREE_CHAIN (arglist)) |
5078 | return 0; | |
5079 | arg = TREE_VALUE (arglist); | |
5080 | arglist = build_tree_list (NULL_TREE, fp); | |
5081 | arglist = tree_cons (NULL_TREE, arg, arglist); | |
5082 | fn = fn_fputs; | |
5083 | } | |
5084 | /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */ | |
99eabcc1 | 5085 | else if (strcmp (fmt_str, target_percent_c) == 0) |
19bf118a | 5086 | { |
5087 | if (! arglist | |
5088 | || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE | |
5089 | || TREE_CHAIN (arglist)) | |
5090 | return 0; | |
5091 | arg = TREE_VALUE (arglist); | |
5092 | arglist = build_tree_list (NULL_TREE, fp); | |
5093 | arglist = tree_cons (NULL_TREE, arg, arglist); | |
5094 | fn = fn_fputc; | |
5095 | } | |
5096 | else | |
5097 | { | |
5098 | /* We can't handle anything else with % args or %% ... yet. */ | |
99eabcc1 | 5099 | if (strchr (fmt_str, target_percent)) |
a0c938f0 | 5100 | return 0; |
19bf118a | 5101 | |
5102 | if (arglist) | |
5103 | return 0; | |
5104 | ||
5105 | /* If the format specifier was "", fprintf does nothing. */ | |
5106 | if (fmt_str[0] == '\0') | |
5107 | { | |
5108 | /* Evaluate and ignore FILE* argument for side-effects. */ | |
5109 | expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
5110 | return const0_rtx; | |
5111 | } | |
5112 | ||
5113 | /* When "string" doesn't contain %, replace all cases of | |
5114 | fprintf(stream,string) with fputs(string,stream). The fputs | |
5115 | builtin will take care of special cases like length == 1. */ | |
5116 | arglist = build_tree_list (NULL_TREE, fp); | |
5117 | arglist = tree_cons (NULL_TREE, fmt, arglist); | |
5118 | fn = fn_fputs; | |
5119 | } | |
5120 | ||
5121 | if (!fn) | |
5122 | return 0; | |
0b25db21 | 5123 | fn = build_function_call_expr (fn, arglist); |
5124 | if (TREE_CODE (fn) == CALL_EXPR) | |
5125 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
5126 | return expand_expr (fn, target, mode, EXPAND_NORMAL); | |
19bf118a | 5127 | } |
5128 | ||
6411575e | 5129 | /* Expand a call to sprintf with argument list ARGLIST. Return 0 if |
5130 | a normal call should be emitted rather than expanding the function | |
5131 | inline. If convenient, the result should be placed in TARGET with | |
5132 | mode MODE. */ | |
5133 | ||
5134 | static rtx | |
5135 | expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode) | |
5136 | { | |
a7a723f0 | 5137 | tree orig_arglist, dest, fmt; |
5138 | const char *fmt_str; | |
6411575e | 5139 | |
5140 | orig_arglist = arglist; | |
5141 | ||
5142 | /* Verify the required arguments in the original call. */ | |
5143 | if (! arglist) | |
5144 | return 0; | |
5145 | dest = TREE_VALUE (arglist); | |
552752f7 | 5146 | if (! POINTER_TYPE_P (TREE_TYPE (dest))) |
6411575e | 5147 | return 0; |
5148 | arglist = TREE_CHAIN (arglist); | |
5149 | if (! arglist) | |
5150 | return 0; | |
5151 | fmt = TREE_VALUE (arglist); | |
552752f7 | 5152 | if (! POINTER_TYPE_P (TREE_TYPE (fmt))) |
6411575e | 5153 | return 0; |
5154 | arglist = TREE_CHAIN (arglist); | |
5155 | ||
5156 | /* Check whether the format is a literal string constant. */ | |
a7a723f0 | 5157 | fmt_str = c_getstr (fmt); |
5158 | if (fmt_str == NULL) | |
6411575e | 5159 | return 0; |
5160 | ||
99eabcc1 | 5161 | if (!init_target_chars()) |
5162 | return 0; | |
5163 | ||
6411575e | 5164 | /* If the format doesn't contain % args or %%, use strcpy. */ |
99eabcc1 | 5165 | if (strchr (fmt_str, target_percent) == 0) |
6411575e | 5166 | { |
5167 | tree fn = implicit_built_in_decls[BUILT_IN_STRCPY]; | |
5168 | tree exp; | |
5169 | ||
a7a723f0 | 5170 | if (arglist || ! fn) |
6411575e | 5171 | return 0; |
5172 | expand_expr (build_function_call_expr (fn, orig_arglist), | |
5173 | const0_rtx, VOIDmode, EXPAND_NORMAL); | |
5174 | if (target == const0_rtx) | |
5175 | return const0_rtx; | |
7016c612 | 5176 | exp = build_int_cst (NULL_TREE, strlen (fmt_str)); |
6411575e | 5177 | return expand_expr (exp, target, mode, EXPAND_NORMAL); |
5178 | } | |
a7a723f0 | 5179 | /* If the format is "%s", use strcpy if the result isn't used. */ |
99eabcc1 | 5180 | else if (strcmp (fmt_str, target_percent_s) == 0) |
6411575e | 5181 | { |
a7a723f0 | 5182 | tree fn, arg, len; |
5183 | fn = implicit_built_in_decls[BUILT_IN_STRCPY]; | |
6411575e | 5184 | |
a7a723f0 | 5185 | if (! fn) |
6411575e | 5186 | return 0; |
5187 | ||
5188 | if (! arglist || TREE_CHAIN (arglist)) | |
5189 | return 0; | |
5190 | arg = TREE_VALUE (arglist); | |
552752f7 | 5191 | if (! POINTER_TYPE_P (TREE_TYPE (arg))) |
6411575e | 5192 | return 0; |
5193 | ||
5194 | if (target != const0_rtx) | |
5195 | { | |
681fab1e | 5196 | len = c_strlen (arg, 1); |
a7a723f0 | 5197 | if (! len || TREE_CODE (len) != INTEGER_CST) |
6411575e | 5198 | return 0; |
6411575e | 5199 | } |
5200 | else | |
a7a723f0 | 5201 | len = NULL_TREE; |
6411575e | 5202 | |
5203 | arglist = build_tree_list (NULL_TREE, arg); | |
5204 | arglist = tree_cons (NULL_TREE, dest, arglist); | |
a7a723f0 | 5205 | expand_expr (build_function_call_expr (fn, arglist), |
6411575e | 5206 | const0_rtx, VOIDmode, EXPAND_NORMAL); |
5207 | ||
5208 | if (target == const0_rtx) | |
5209 | return const0_rtx; | |
a7a723f0 | 5210 | return expand_expr (len, target, mode, EXPAND_NORMAL); |
6411575e | 5211 | } |
5212 | ||
5213 | return 0; | |
5214 | } | |
27f261ef | 5215 | |
4ee9c684 | 5216 | /* Expand a call to either the entry or exit function profiler. */ |
5217 | ||
5218 | static rtx | |
5219 | expand_builtin_profile_func (bool exitp) | |
5220 | { | |
5221 | rtx this, which; | |
5222 | ||
5223 | this = DECL_RTL (current_function_decl); | |
64db345d | 5224 | gcc_assert (MEM_P (this)); |
5225 | this = XEXP (this, 0); | |
4ee9c684 | 5226 | |
5227 | if (exitp) | |
5228 | which = profile_function_exit_libfunc; | |
5229 | else | |
5230 | which = profile_function_entry_libfunc; | |
5231 | ||
5232 | emit_library_call (which, LCT_NORMAL, VOIDmode, 2, this, Pmode, | |
5233 | expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS, | |
869d0ef0 | 5234 | 0), |
4ee9c684 | 5235 | Pmode); |
5236 | ||
5237 | return const0_rtx; | |
5238 | } | |
5239 | ||
5240 | /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */ | |
5241 | ||
5242 | static rtx | |
5243 | round_trampoline_addr (rtx tramp) | |
5244 | { | |
5245 | rtx temp, addend, mask; | |
5246 | ||
5247 | /* If we don't need too much alignment, we'll have been guaranteed | |
5248 | proper alignment by get_trampoline_type. */ | |
5249 | if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY) | |
5250 | return tramp; | |
5251 | ||
5252 | /* Round address up to desired boundary. */ | |
5253 | temp = gen_reg_rtx (Pmode); | |
5254 | addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1); | |
5255 | mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT); | |
5256 | ||
5257 | temp = expand_simple_binop (Pmode, PLUS, tramp, addend, | |
5258 | temp, 0, OPTAB_LIB_WIDEN); | |
5259 | tramp = expand_simple_binop (Pmode, AND, temp, mask, | |
5260 | temp, 0, OPTAB_LIB_WIDEN); | |
5261 | ||
5262 | return tramp; | |
5263 | } | |
5264 | ||
5265 | static rtx | |
5266 | expand_builtin_init_trampoline (tree arglist) | |
5267 | { | |
5268 | tree t_tramp, t_func, t_chain; | |
5269 | rtx r_tramp, r_func, r_chain; | |
5270 | #ifdef TRAMPOLINE_TEMPLATE | |
5271 | rtx blktramp; | |
5272 | #endif | |
5273 | ||
5274 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, | |
5275 | POINTER_TYPE, VOID_TYPE)) | |
5276 | return NULL_RTX; | |
5277 | ||
5278 | t_tramp = TREE_VALUE (arglist); | |
5279 | arglist = TREE_CHAIN (arglist); | |
5280 | t_func = TREE_VALUE (arglist); | |
5281 | arglist = TREE_CHAIN (arglist); | |
5282 | t_chain = TREE_VALUE (arglist); | |
5283 | ||
8ec3c5c2 | 5284 | r_tramp = expand_normal (t_tramp); |
5285 | r_func = expand_normal (t_func); | |
5286 | r_chain = expand_normal (t_chain); | |
4ee9c684 | 5287 | |
5288 | /* Generate insns to initialize the trampoline. */ | |
5289 | r_tramp = round_trampoline_addr (r_tramp); | |
5290 | #ifdef TRAMPOLINE_TEMPLATE | |
5291 | blktramp = gen_rtx_MEM (BLKmode, r_tramp); | |
5292 | set_mem_align (blktramp, TRAMPOLINE_ALIGNMENT); | |
5293 | emit_block_move (blktramp, assemble_trampoline_template (), | |
5294 | GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL); | |
5295 | #endif | |
5296 | trampolines_created = 1; | |
5297 | INITIALIZE_TRAMPOLINE (r_tramp, r_func, r_chain); | |
5298 | ||
5299 | return const0_rtx; | |
5300 | } | |
5301 | ||
5302 | static rtx | |
5303 | expand_builtin_adjust_trampoline (tree arglist) | |
5304 | { | |
5305 | rtx tramp; | |
5306 | ||
5307 | if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE)) | |
5308 | return NULL_RTX; | |
5309 | ||
8ec3c5c2 | 5310 | tramp = expand_normal (TREE_VALUE (arglist)); |
4ee9c684 | 5311 | tramp = round_trampoline_addr (tramp); |
5312 | #ifdef TRAMPOLINE_ADJUST_ADDRESS | |
5313 | TRAMPOLINE_ADJUST_ADDRESS (tramp); | |
5314 | #endif | |
5315 | ||
5316 | return tramp; | |
5317 | } | |
5318 | ||
27f261ef | 5319 | /* Expand a call to the built-in signbit, signbitf or signbitl function. |
5320 | Return NULL_RTX if a normal call should be emitted rather than expanding | |
5321 | the function in-line. EXP is the expression that is a call to the builtin | |
5322 | function; if convenient, the result should be placed in TARGET. */ | |
5323 | ||
5324 | static rtx | |
5325 | expand_builtin_signbit (tree exp, rtx target) | |
5326 | { | |
5327 | const struct real_format *fmt; | |
5328 | enum machine_mode fmode, imode, rmode; | |
5329 | HOST_WIDE_INT hi, lo; | |
5330 | tree arg, arglist; | |
ca4f1f5b | 5331 | int word, bitpos; |
27f261ef | 5332 | rtx temp; |
5333 | ||
5334 | arglist = TREE_OPERAND (exp, 1); | |
5335 | if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
5336 | return 0; | |
5337 | ||
5338 | arg = TREE_VALUE (arglist); | |
5339 | fmode = TYPE_MODE (TREE_TYPE (arg)); | |
5340 | rmode = TYPE_MODE (TREE_TYPE (exp)); | |
5341 | fmt = REAL_MODE_FORMAT (fmode); | |
5342 | ||
5343 | /* For floating point formats without a sign bit, implement signbit | |
5344 | as "ARG < 0.0". */ | |
8d564692 | 5345 | bitpos = fmt->signbit_ro; |
ca4f1f5b | 5346 | if (bitpos < 0) |
27f261ef | 5347 | { |
5348 | /* But we can't do this if the format supports signed zero. */ | |
5349 | if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode)) | |
5350 | return 0; | |
5351 | ||
49d00087 | 5352 | arg = fold_build2 (LT_EXPR, TREE_TYPE (exp), arg, |
5353 | build_real (TREE_TYPE (arg), dconst0)); | |
27f261ef | 5354 | return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL); |
5355 | } | |
5356 | ||
8ec3c5c2 | 5357 | temp = expand_normal (arg); |
ca4f1f5b | 5358 | if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD) |
27f261ef | 5359 | { |
ca4f1f5b | 5360 | imode = int_mode_for_mode (fmode); |
5361 | if (imode == BLKmode) | |
5362 | return 0; | |
5363 | temp = gen_lowpart (imode, temp); | |
24fd4260 | 5364 | } |
5365 | else | |
5366 | { | |
ca4f1f5b | 5367 | imode = word_mode; |
5368 | /* Handle targets with different FP word orders. */ | |
5369 | if (FLOAT_WORDS_BIG_ENDIAN) | |
a0c938f0 | 5370 | word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD; |
ca4f1f5b | 5371 | else |
a0c938f0 | 5372 | word = bitpos / BITS_PER_WORD; |
ca4f1f5b | 5373 | temp = operand_subword_force (temp, word, fmode); |
5374 | bitpos = bitpos % BITS_PER_WORD; | |
5375 | } | |
5376 | ||
44b0f1d0 | 5377 | /* Force the intermediate word_mode (or narrower) result into a |
5378 | register. This avoids attempting to create paradoxical SUBREGs | |
5379 | of floating point modes below. */ | |
5380 | temp = force_reg (imode, temp); | |
5381 | ||
ca4f1f5b | 5382 | /* If the bitpos is within the "result mode" lowpart, the operation |
5383 | can be implement with a single bitwise AND. Otherwise, we need | |
5384 | a right shift and an AND. */ | |
5385 | ||
5386 | if (bitpos < GET_MODE_BITSIZE (rmode)) | |
5387 | { | |
24fd4260 | 5388 | if (bitpos < HOST_BITS_PER_WIDE_INT) |
27f261ef | 5389 | { |
24fd4260 | 5390 | hi = 0; |
5391 | lo = (HOST_WIDE_INT) 1 << bitpos; | |
27f261ef | 5392 | } |
5393 | else | |
24fd4260 | 5394 | { |
5395 | hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT); | |
5396 | lo = 0; | |
5397 | } | |
27f261ef | 5398 | |
ca4f1f5b | 5399 | if (imode != rmode) |
5400 | temp = gen_lowpart (rmode, temp); | |
24fd4260 | 5401 | temp = expand_binop (rmode, and_optab, temp, |
5402 | immed_double_const (lo, hi, rmode), | |
ca4f1f5b | 5403 | NULL_RTX, 1, OPTAB_LIB_WIDEN); |
27f261ef | 5404 | } |
ca4f1f5b | 5405 | else |
5406 | { | |
5407 | /* Perform a logical right shift to place the signbit in the least | |
a0c938f0 | 5408 | significant bit, then truncate the result to the desired mode |
ca4f1f5b | 5409 | and mask just this bit. */ |
5410 | temp = expand_shift (RSHIFT_EXPR, imode, temp, | |
5411 | build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1); | |
5412 | temp = gen_lowpart (rmode, temp); | |
5413 | temp = expand_binop (rmode, and_optab, temp, const1_rtx, | |
5414 | NULL_RTX, 1, OPTAB_LIB_WIDEN); | |
5415 | } | |
5416 | ||
27f261ef | 5417 | return temp; |
5418 | } | |
73673831 | 5419 | |
5420 | /* Expand fork or exec calls. TARGET is the desired target of the | |
5421 | call. ARGLIST is the list of arguments of the call. FN is the | |
5422 | identificator of the actual function. IGNORE is nonzero if the | |
5423 | value is to be ignored. */ | |
5424 | ||
5425 | static rtx | |
5426 | expand_builtin_fork_or_exec (tree fn, tree arglist, rtx target, int ignore) | |
5427 | { | |
5428 | tree id, decl; | |
5429 | tree call; | |
5430 | ||
5431 | /* If we are not profiling, just call the function. */ | |
5432 | if (!profile_arc_flag) | |
5433 | return NULL_RTX; | |
5434 | ||
5435 | /* Otherwise call the wrapper. This should be equivalent for the rest of | |
5436 | compiler, so the code does not diverge, and the wrapper may run the | |
9c9bad97 | 5437 | code necessary for keeping the profiling sane. */ |
73673831 | 5438 | |
5439 | switch (DECL_FUNCTION_CODE (fn)) | |
5440 | { | |
5441 | case BUILT_IN_FORK: | |
5442 | id = get_identifier ("__gcov_fork"); | |
5443 | break; | |
5444 | ||
5445 | case BUILT_IN_EXECL: | |
5446 | id = get_identifier ("__gcov_execl"); | |
5447 | break; | |
5448 | ||
5449 | case BUILT_IN_EXECV: | |
5450 | id = get_identifier ("__gcov_execv"); | |
5451 | break; | |
5452 | ||
5453 | case BUILT_IN_EXECLP: | |
5454 | id = get_identifier ("__gcov_execlp"); | |
5455 | break; | |
5456 | ||
5457 | case BUILT_IN_EXECLE: | |
5458 | id = get_identifier ("__gcov_execle"); | |
5459 | break; | |
5460 | ||
5461 | case BUILT_IN_EXECVP: | |
5462 | id = get_identifier ("__gcov_execvp"); | |
5463 | break; | |
5464 | ||
5465 | case BUILT_IN_EXECVE: | |
5466 | id = get_identifier ("__gcov_execve"); | |
5467 | break; | |
5468 | ||
5469 | default: | |
64db345d | 5470 | gcc_unreachable (); |
73673831 | 5471 | } |
5472 | ||
5473 | decl = build_decl (FUNCTION_DECL, id, TREE_TYPE (fn)); | |
5474 | DECL_EXTERNAL (decl) = 1; | |
5475 | TREE_PUBLIC (decl) = 1; | |
5476 | DECL_ARTIFICIAL (decl) = 1; | |
5477 | TREE_NOTHROW (decl) = 1; | |
e82d310b | 5478 | DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT; |
5479 | DECL_VISIBILITY_SPECIFIED (decl) = 1; | |
73673831 | 5480 | call = build_function_call_expr (decl, arglist); |
5481 | ||
5482 | return expand_call (call, target, ignore); | |
5483 | } | |
b6a5fc45 | 5484 | |
5485 | \f | |
3e272de8 | 5486 | /* Reconstitute a mode for a __sync intrinsic operation. Since the type of |
5487 | the pointer in these functions is void*, the tree optimizers may remove | |
5488 | casts. The mode computed in expand_builtin isn't reliable either, due | |
5489 | to __sync_bool_compare_and_swap. | |
5490 | ||
5491 | FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the | |
5492 | group of builtins. This gives us log2 of the mode size. */ | |
5493 | ||
5494 | static inline enum machine_mode | |
5495 | get_builtin_sync_mode (int fcode_diff) | |
5496 | { | |
ad3a13b5 | 5497 | /* The size is not negotiable, so ask not to get BLKmode in return |
5498 | if the target indicates that a smaller size would be better. */ | |
5499 | return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0); | |
3e272de8 | 5500 | } |
5501 | ||
041e0215 | 5502 | /* Expand the memory expression LOC and return the appropriate memory operand |
5503 | for the builtin_sync operations. */ | |
5504 | ||
5505 | static rtx | |
5506 | get_builtin_sync_mem (tree loc, enum machine_mode mode) | |
5507 | { | |
5508 | rtx addr, mem; | |
5509 | ||
5510 | addr = expand_expr (loc, NULL, Pmode, EXPAND_SUM); | |
5511 | ||
5512 | /* Note that we explicitly do not want any alias information for this | |
5513 | memory, so that we kill all other live memories. Otherwise we don't | |
5514 | satisfy the full barrier semantics of the intrinsic. */ | |
5515 | mem = validize_mem (gen_rtx_MEM (mode, addr)); | |
5516 | ||
5517 | set_mem_align (mem, get_pointer_alignment (loc, BIGGEST_ALIGNMENT)); | |
c94cfd1c | 5518 | set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER); |
041e0215 | 5519 | MEM_VOLATILE_P (mem) = 1; |
5520 | ||
5521 | return mem; | |
5522 | } | |
5523 | ||
b6a5fc45 | 5524 | /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics. |
a0c938f0 | 5525 | ARGLIST is the operands list to the function. CODE is the rtx code |
b6a5fc45 | 5526 | that corresponds to the arithmetic or logical operation from the name; |
5527 | an exception here is that NOT actually means NAND. TARGET is an optional | |
5528 | place for us to store the results; AFTER is true if this is the | |
5529 | fetch_and_xxx form. IGNORE is true if we don't actually care about | |
5530 | the result of the operation at all. */ | |
5531 | ||
5532 | static rtx | |
3e272de8 | 5533 | expand_builtin_sync_operation (enum machine_mode mode, tree arglist, |
5534 | enum rtx_code code, bool after, | |
b6a5fc45 | 5535 | rtx target, bool ignore) |
5536 | { | |
041e0215 | 5537 | rtx val, mem; |
b6a5fc45 | 5538 | |
5539 | /* Expand the operands. */ | |
041e0215 | 5540 | mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode); |
b6a5fc45 | 5541 | |
5542 | arglist = TREE_CHAIN (arglist); | |
87121034 | 5543 | val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL); |
3e69d97d | 5544 | /* If VAL is promoted to a wider mode, convert it back to MODE. */ |
5545 | val = convert_to_mode (mode, val, 1); | |
b6a5fc45 | 5546 | |
b6a5fc45 | 5547 | if (ignore) |
5548 | return expand_sync_operation (mem, val, code); | |
5549 | else | |
5550 | return expand_sync_fetch_operation (mem, val, code, after, target); | |
5551 | } | |
5552 | ||
5553 | /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap | |
5554 | intrinsics. ARGLIST is the operands list to the function. IS_BOOL is | |
5555 | true if this is the boolean form. TARGET is a place for us to store the | |
5556 | results; this is NOT optional if IS_BOOL is true. */ | |
5557 | ||
5558 | static rtx | |
3e272de8 | 5559 | expand_builtin_compare_and_swap (enum machine_mode mode, tree arglist, |
5560 | bool is_bool, rtx target) | |
b6a5fc45 | 5561 | { |
041e0215 | 5562 | rtx old_val, new_val, mem; |
b6a5fc45 | 5563 | |
5564 | /* Expand the operands. */ | |
041e0215 | 5565 | mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode); |
b6a5fc45 | 5566 | |
5567 | arglist = TREE_CHAIN (arglist); | |
5568 | old_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL); | |
3e69d97d | 5569 | /* If OLD_VAL is promoted to a wider mode, convert it back to MODE. */ |
5570 | old_val = convert_to_mode (mode, old_val, 1); | |
b6a5fc45 | 5571 | |
5572 | arglist = TREE_CHAIN (arglist); | |
5573 | new_val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL); | |
3e69d97d | 5574 | /* If NEW_VAL is promoted to a wider mode, convert it back to MODE. */ |
5575 | new_val = convert_to_mode (mode, new_val, 1); | |
b6a5fc45 | 5576 | |
b6a5fc45 | 5577 | if (is_bool) |
5578 | return expand_bool_compare_and_swap (mem, old_val, new_val, target); | |
5579 | else | |
5580 | return expand_val_compare_and_swap (mem, old_val, new_val, target); | |
5581 | } | |
5582 | ||
5583 | /* Expand the __sync_lock_test_and_set intrinsic. Note that the most | |
5584 | general form is actually an atomic exchange, and some targets only | |
5585 | support a reduced form with the second argument being a constant 1. | |
5586 | ARGLIST is the operands list to the function; TARGET is an optional | |
5587 | place for us to store the results. */ | |
5588 | ||
5589 | static rtx | |
3e272de8 | 5590 | expand_builtin_lock_test_and_set (enum machine_mode mode, tree arglist, |
5591 | rtx target) | |
b6a5fc45 | 5592 | { |
041e0215 | 5593 | rtx val, mem; |
b6a5fc45 | 5594 | |
5595 | /* Expand the operands. */ | |
041e0215 | 5596 | mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode); |
b6a5fc45 | 5597 | |
5598 | arglist = TREE_CHAIN (arglist); | |
5599 | val = expand_expr (TREE_VALUE (arglist), NULL, mode, EXPAND_NORMAL); | |
3e69d97d | 5600 | /* If VAL is promoted to a wider mode, convert it back to MODE. */ |
5601 | val = convert_to_mode (mode, val, 1); | |
b6a5fc45 | 5602 | |
b6a5fc45 | 5603 | return expand_sync_lock_test_and_set (mem, val, target); |
5604 | } | |
5605 | ||
5606 | /* Expand the __sync_synchronize intrinsic. */ | |
5607 | ||
5608 | static void | |
5609 | expand_builtin_synchronize (void) | |
5610 | { | |
82e58396 | 5611 | tree x; |
b6a5fc45 | 5612 | |
5613 | #ifdef HAVE_memory_barrier | |
5614 | if (HAVE_memory_barrier) | |
5615 | { | |
5616 | emit_insn (gen_memory_barrier ()); | |
5617 | return; | |
5618 | } | |
5619 | #endif | |
5620 | ||
82e58396 | 5621 | /* If no explicit memory barrier instruction is available, create an |
5622 | empty asm stmt with a memory clobber. */ | |
5623 | x = build4 (ASM_EXPR, void_type_node, build_string (0, ""), NULL, NULL, | |
5624 | tree_cons (NULL, build_string (6, "memory"), NULL)); | |
5625 | ASM_VOLATILE_P (x) = 1; | |
5626 | expand_asm_expr (x); | |
b6a5fc45 | 5627 | } |
5628 | ||
5629 | /* Expand the __sync_lock_release intrinsic. ARGLIST is the operands list | |
5630 | to the function. */ | |
5631 | ||
5632 | static void | |
3e272de8 | 5633 | expand_builtin_lock_release (enum machine_mode mode, tree arglist) |
b6a5fc45 | 5634 | { |
b6a5fc45 | 5635 | enum insn_code icode; |
041e0215 | 5636 | rtx mem, insn; |
3e272de8 | 5637 | rtx val = const0_rtx; |
b6a5fc45 | 5638 | |
5639 | /* Expand the operands. */ | |
041e0215 | 5640 | mem = get_builtin_sync_mem (TREE_VALUE (arglist), mode); |
b6a5fc45 | 5641 | |
5642 | /* If there is an explicit operation in the md file, use it. */ | |
5643 | icode = sync_lock_release[mode]; | |
5644 | if (icode != CODE_FOR_nothing) | |
5645 | { | |
5646 | if (!insn_data[icode].operand[1].predicate (val, mode)) | |
5647 | val = force_reg (mode, val); | |
5648 | ||
5649 | insn = GEN_FCN (icode) (mem, val); | |
5650 | if (insn) | |
5651 | { | |
5652 | emit_insn (insn); | |
5653 | return; | |
5654 | } | |
5655 | } | |
5656 | ||
5657 | /* Otherwise we can implement this operation by emitting a barrier | |
5658 | followed by a store of zero. */ | |
5659 | expand_builtin_synchronize (); | |
5660 | emit_move_insn (mem, val); | |
5661 | } | |
53800dbe | 5662 | \f |
5663 | /* Expand an expression EXP that calls a built-in function, | |
5664 | with result going to TARGET if that's convenient | |
5665 | (and in mode MODE if that's convenient). | |
5666 | SUBTARGET may be used as the target for computing one of EXP's operands. | |
5667 | IGNORE is nonzero if the value is to be ignored. */ | |
5668 | ||
5669 | rtx | |
aecda0d6 | 5670 | expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode, |
5671 | int ignore) | |
53800dbe | 5672 | { |
c6e6ecb1 | 5673 | tree fndecl = get_callee_fndecl (exp); |
53800dbe | 5674 | tree arglist = TREE_OPERAND (exp, 1); |
5675 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
efb070c8 | 5676 | enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp)); |
53800dbe | 5677 | |
8305149e | 5678 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) |
883b2e73 | 5679 | return targetm.expand_builtin (exp, target, subtarget, mode, ignore); |
bf8e3599 | 5680 | |
53800dbe | 5681 | /* When not optimizing, generate calls to library functions for a certain |
5682 | set of builtins. */ | |
cd9ff771 | 5683 | if (!optimize |
b6a5fc45 | 5684 | && !called_as_built_in (fndecl) |
cd9ff771 | 5685 | && DECL_ASSEMBLER_NAME_SET_P (fndecl) |
5686 | && fcode != BUILT_IN_ALLOCA) | |
5687 | return expand_call (exp, target, ignore); | |
53800dbe | 5688 | |
8d6d7930 | 5689 | /* The built-in function expanders test for target == const0_rtx |
5690 | to determine whether the function's result will be ignored. */ | |
5691 | if (ignore) | |
5692 | target = const0_rtx; | |
5693 | ||
5694 | /* If the result of a pure or const built-in function is ignored, and | |
5695 | none of its arguments are volatile, we can avoid expanding the | |
5696 | built-in call and just evaluate the arguments for side-effects. */ | |
5697 | if (target == const0_rtx | |
5698 | && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl))) | |
5699 | { | |
5700 | bool volatilep = false; | |
5701 | tree arg; | |
5702 | ||
5703 | for (arg = arglist; arg; arg = TREE_CHAIN (arg)) | |
5704 | if (TREE_THIS_VOLATILE (TREE_VALUE (arg))) | |
5705 | { | |
5706 | volatilep = true; | |
5707 | break; | |
5708 | } | |
5709 | ||
5710 | if (! volatilep) | |
5711 | { | |
5712 | for (arg = arglist; arg; arg = TREE_CHAIN (arg)) | |
5713 | expand_expr (TREE_VALUE (arg), const0_rtx, | |
5714 | VOIDmode, EXPAND_NORMAL); | |
5715 | return const0_rtx; | |
5716 | } | |
5717 | } | |
5718 | ||
53800dbe | 5719 | switch (fcode) |
5720 | { | |
4f35b1fc | 5721 | CASE_FLT_FN (BUILT_IN_FABS): |
78a74442 | 5722 | target = expand_builtin_fabs (arglist, target, subtarget); |
5723 | if (target) | |
a0c938f0 | 5724 | return target; |
78a74442 | 5725 | break; |
5726 | ||
4f35b1fc | 5727 | CASE_FLT_FN (BUILT_IN_COPYSIGN): |
270436f3 | 5728 | target = expand_builtin_copysign (arglist, target, subtarget); |
5729 | if (target) | |
5730 | return target; | |
5731 | break; | |
5732 | ||
7d3f6cc7 | 5733 | /* Just do a normal library call if we were unable to fold |
5734 | the values. */ | |
4f35b1fc | 5735 | CASE_FLT_FN (BUILT_IN_CABS): |
78a74442 | 5736 | break; |
53800dbe | 5737 | |
4f35b1fc | 5738 | CASE_FLT_FN (BUILT_IN_EXP): |
5739 | CASE_FLT_FN (BUILT_IN_EXP10): | |
5740 | CASE_FLT_FN (BUILT_IN_POW10): | |
5741 | CASE_FLT_FN (BUILT_IN_EXP2): | |
5742 | CASE_FLT_FN (BUILT_IN_EXPM1): | |
5743 | CASE_FLT_FN (BUILT_IN_LOGB): | |
5744 | CASE_FLT_FN (BUILT_IN_ILOGB): | |
5745 | CASE_FLT_FN (BUILT_IN_LOG): | |
5746 | CASE_FLT_FN (BUILT_IN_LOG10): | |
5747 | CASE_FLT_FN (BUILT_IN_LOG2): | |
5748 | CASE_FLT_FN (BUILT_IN_LOG1P): | |
5749 | CASE_FLT_FN (BUILT_IN_TAN): | |
5750 | CASE_FLT_FN (BUILT_IN_ASIN): | |
5751 | CASE_FLT_FN (BUILT_IN_ACOS): | |
5752 | CASE_FLT_FN (BUILT_IN_ATAN): | |
7f3be425 | 5753 | /* Treat these like sqrt only if unsafe math optimizations are allowed, |
5754 | because of possible accuracy problems. */ | |
5755 | if (! flag_unsafe_math_optimizations) | |
53800dbe | 5756 | break; |
4f35b1fc | 5757 | CASE_FLT_FN (BUILT_IN_SQRT): |
5758 | CASE_FLT_FN (BUILT_IN_FLOOR): | |
5759 | CASE_FLT_FN (BUILT_IN_CEIL): | |
5760 | CASE_FLT_FN (BUILT_IN_TRUNC): | |
5761 | CASE_FLT_FN (BUILT_IN_ROUND): | |
5762 | CASE_FLT_FN (BUILT_IN_NEARBYINT): | |
5763 | CASE_FLT_FN (BUILT_IN_RINT): | |
53800dbe | 5764 | target = expand_builtin_mathfn (exp, target, subtarget); |
5765 | if (target) | |
5766 | return target; | |
5767 | break; | |
5768 | ||
4f35b1fc | 5769 | CASE_FLT_FN (BUILT_IN_LCEIL): |
5770 | CASE_FLT_FN (BUILT_IN_LLCEIL): | |
5771 | CASE_FLT_FN (BUILT_IN_LFLOOR): | |
5772 | CASE_FLT_FN (BUILT_IN_LLFLOOR): | |
ad52b9b7 | 5773 | target = expand_builtin_int_roundingfn (exp, target, subtarget); |
5774 | if (target) | |
5775 | return target; | |
5776 | break; | |
5777 | ||
7d3afc77 | 5778 | CASE_FLT_FN (BUILT_IN_LRINT): |
5779 | CASE_FLT_FN (BUILT_IN_LLRINT): | |
5780 | target = expand_builtin_int_roundingfn_2 (exp, target, subtarget); | |
5781 | if (target) | |
5782 | return target; | |
5783 | break; | |
5784 | ||
4f35b1fc | 5785 | CASE_FLT_FN (BUILT_IN_POW): |
f1b844c6 | 5786 | target = expand_builtin_pow (exp, target, subtarget); |
5787 | if (target) | |
5788 | return target; | |
5789 | break; | |
5790 | ||
4f35b1fc | 5791 | CASE_FLT_FN (BUILT_IN_POWI): |
757c219d | 5792 | target = expand_builtin_powi (exp, target, subtarget); |
5793 | if (target) | |
5794 | return target; | |
5795 | break; | |
5796 | ||
4f35b1fc | 5797 | CASE_FLT_FN (BUILT_IN_ATAN2): |
5798 | CASE_FLT_FN (BUILT_IN_LDEXP): | |
5799 | CASE_FLT_FN (BUILT_IN_FMOD): | |
5800 | CASE_FLT_FN (BUILT_IN_DREM): | |
0fd605a5 | 5801 | if (! flag_unsafe_math_optimizations) |
5802 | break; | |
5803 | target = expand_builtin_mathfn_2 (exp, target, subtarget); | |
5804 | if (target) | |
5805 | return target; | |
5806 | break; | |
5807 | ||
4f35b1fc | 5808 | CASE_FLT_FN (BUILT_IN_SIN): |
5809 | CASE_FLT_FN (BUILT_IN_COS): | |
6b43bae4 | 5810 | if (! flag_unsafe_math_optimizations) |
5811 | break; | |
5812 | target = expand_builtin_mathfn_3 (exp, target, subtarget); | |
5813 | if (target) | |
5814 | return target; | |
5815 | break; | |
5816 | ||
c3147c1a | 5817 | CASE_FLT_FN (BUILT_IN_SINCOS): |
5818 | if (! flag_unsafe_math_optimizations) | |
5819 | break; | |
5820 | target = expand_builtin_sincos (exp); | |
5821 | if (target) | |
5822 | return target; | |
5823 | break; | |
5824 | ||
53800dbe | 5825 | case BUILT_IN_APPLY_ARGS: |
5826 | return expand_builtin_apply_args (); | |
5827 | ||
5828 | /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes | |
5829 | FUNCTION with a copy of the parameters described by | |
5830 | ARGUMENTS, and ARGSIZE. It returns a block of memory | |
5831 | allocated on the stack into which is stored all the registers | |
5832 | that might possibly be used for returning the result of a | |
5833 | function. ARGUMENTS is the value returned by | |
5834 | __builtin_apply_args. ARGSIZE is the number of bytes of | |
5835 | arguments that must be copied. ??? How should this value be | |
5836 | computed? We'll also need a safe worst case value for varargs | |
5837 | functions. */ | |
5838 | case BUILT_IN_APPLY: | |
0eb671f7 | 5839 | if (!validate_arglist (arglist, POINTER_TYPE, |
5840 | POINTER_TYPE, INTEGER_TYPE, VOID_TYPE) | |
5841 | && !validate_arglist (arglist, REFERENCE_TYPE, | |
5842 | POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
53800dbe | 5843 | return const0_rtx; |
5844 | else | |
5845 | { | |
5846 | int i; | |
5847 | tree t; | |
5848 | rtx ops[3]; | |
5849 | ||
5850 | for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++) | |
8ec3c5c2 | 5851 | ops[i] = expand_normal (TREE_VALUE (t)); |
53800dbe | 5852 | |
5853 | return expand_builtin_apply (ops[0], ops[1], ops[2]); | |
5854 | } | |
5855 | ||
5856 | /* __builtin_return (RESULT) causes the function to return the | |
5857 | value described by RESULT. RESULT is address of the block of | |
5858 | memory returned by __builtin_apply. */ | |
5859 | case BUILT_IN_RETURN: | |
0eb671f7 | 5860 | if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE)) |
8ec3c5c2 | 5861 | expand_builtin_return (expand_normal (TREE_VALUE (arglist))); |
53800dbe | 5862 | return const0_rtx; |
5863 | ||
5864 | case BUILT_IN_SAVEREGS: | |
a66c9326 | 5865 | return expand_builtin_saveregs (); |
53800dbe | 5866 | |
5867 | case BUILT_IN_ARGS_INFO: | |
80cd7a5e | 5868 | return expand_builtin_args_info (arglist); |
53800dbe | 5869 | |
5870 | /* Return the address of the first anonymous stack arg. */ | |
5871 | case BUILT_IN_NEXT_ARG: | |
743b0c6a | 5872 | if (fold_builtin_next_arg (arglist)) |
a0c938f0 | 5873 | return const0_rtx; |
79012a9d | 5874 | return expand_builtin_next_arg (); |
53800dbe | 5875 | |
5876 | case BUILT_IN_CLASSIFY_TYPE: | |
5877 | return expand_builtin_classify_type (arglist); | |
5878 | ||
5879 | case BUILT_IN_CONSTANT_P: | |
4ee9c684 | 5880 | return const0_rtx; |
53800dbe | 5881 | |
5882 | case BUILT_IN_FRAME_ADDRESS: | |
5883 | case BUILT_IN_RETURN_ADDRESS: | |
80cd7a5e | 5884 | return expand_builtin_frame_address (fndecl, arglist); |
53800dbe | 5885 | |
5886 | /* Returns the address of the area where the structure is returned. | |
5887 | 0 otherwise. */ | |
5888 | case BUILT_IN_AGGREGATE_INCOMING_ADDRESS: | |
5889 | if (arglist != 0 | |
9342ee68 | 5890 | || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))) |
e16ceb8e | 5891 | || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl)))) |
9342ee68 | 5892 | return const0_rtx; |
53800dbe | 5893 | else |
9342ee68 | 5894 | return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0); |
53800dbe | 5895 | |
5896 | case BUILT_IN_ALLOCA: | |
5897 | target = expand_builtin_alloca (arglist, target); | |
5898 | if (target) | |
5899 | return target; | |
5900 | break; | |
5901 | ||
4ee9c684 | 5902 | case BUILT_IN_STACK_SAVE: |
5903 | return expand_stack_save (); | |
5904 | ||
5905 | case BUILT_IN_STACK_RESTORE: | |
5906 | expand_stack_restore (TREE_VALUE (arglist)); | |
5907 | return const0_rtx; | |
5908 | ||
4f35b1fc | 5909 | CASE_INT_FN (BUILT_IN_FFS): |
5053259b | 5910 | case BUILT_IN_FFSIMAX: |
efb070c8 | 5911 | target = expand_builtin_unop (target_mode, arglist, target, |
5912 | subtarget, ffs_optab); | |
6a08d0ab | 5913 | if (target) |
5914 | return target; | |
5915 | break; | |
5916 | ||
4f35b1fc | 5917 | CASE_INT_FN (BUILT_IN_CLZ): |
5053259b | 5918 | case BUILT_IN_CLZIMAX: |
efb070c8 | 5919 | target = expand_builtin_unop (target_mode, arglist, target, |
5920 | subtarget, clz_optab); | |
6a08d0ab | 5921 | if (target) |
5922 | return target; | |
5923 | break; | |
5924 | ||
4f35b1fc | 5925 | CASE_INT_FN (BUILT_IN_CTZ): |
5053259b | 5926 | case BUILT_IN_CTZIMAX: |
efb070c8 | 5927 | target = expand_builtin_unop (target_mode, arglist, target, |
5928 | subtarget, ctz_optab); | |
6a08d0ab | 5929 | if (target) |
5930 | return target; | |
5931 | break; | |
5932 | ||
4f35b1fc | 5933 | CASE_INT_FN (BUILT_IN_POPCOUNT): |
5053259b | 5934 | case BUILT_IN_POPCOUNTIMAX: |
efb070c8 | 5935 | target = expand_builtin_unop (target_mode, arglist, target, |
5936 | subtarget, popcount_optab); | |
6a08d0ab | 5937 | if (target) |
5938 | return target; | |
5939 | break; | |
5940 | ||
4f35b1fc | 5941 | CASE_INT_FN (BUILT_IN_PARITY): |
5053259b | 5942 | case BUILT_IN_PARITYIMAX: |
efb070c8 | 5943 | target = expand_builtin_unop (target_mode, arglist, target, |
5944 | subtarget, parity_optab); | |
53800dbe | 5945 | if (target) |
5946 | return target; | |
5947 | break; | |
5948 | ||
5949 | case BUILT_IN_STRLEN: | |
80cd7a5e | 5950 | target = expand_builtin_strlen (arglist, target, target_mode); |
53800dbe | 5951 | if (target) |
5952 | return target; | |
5953 | break; | |
5954 | ||
5955 | case BUILT_IN_STRCPY: | |
1d89860b | 5956 | target = expand_builtin_strcpy (fndecl, arglist, target, mode); |
53800dbe | 5957 | if (target) |
5958 | return target; | |
5959 | break; | |
bf8e3599 | 5960 | |
ed09096d | 5961 | case BUILT_IN_STRNCPY: |
8ff6a5cd | 5962 | target = expand_builtin_strncpy (exp, target, mode); |
ed09096d | 5963 | if (target) |
5964 | return target; | |
5965 | break; | |
bf8e3599 | 5966 | |
3b824fa6 | 5967 | case BUILT_IN_STPCPY: |
dc369150 | 5968 | target = expand_builtin_stpcpy (exp, target, mode); |
3b824fa6 | 5969 | if (target) |
5970 | return target; | |
5971 | break; | |
5972 | ||
49f0327b | 5973 | case BUILT_IN_STRCAT: |
1d89860b | 5974 | target = expand_builtin_strcat (fndecl, arglist, target, mode); |
49f0327b | 5975 | if (target) |
5976 | return target; | |
5977 | break; | |
bf8e3599 | 5978 | |
49f0327b | 5979 | case BUILT_IN_STRNCAT: |
5980 | target = expand_builtin_strncat (arglist, target, mode); | |
5981 | if (target) | |
5982 | return target; | |
5983 | break; | |
bf8e3599 | 5984 | |
49f0327b | 5985 | case BUILT_IN_STRSPN: |
5986 | target = expand_builtin_strspn (arglist, target, mode); | |
5987 | if (target) | |
5988 | return target; | |
5989 | break; | |
bf8e3599 | 5990 | |
49f0327b | 5991 | case BUILT_IN_STRCSPN: |
5992 | target = expand_builtin_strcspn (arglist, target, mode); | |
5993 | if (target) | |
5994 | return target; | |
5995 | break; | |
bf8e3599 | 5996 | |
17f5ea87 | 5997 | case BUILT_IN_STRSTR: |
daa1d5f5 | 5998 | target = expand_builtin_strstr (arglist, TREE_TYPE (exp), target, mode); |
17f5ea87 | 5999 | if (target) |
6000 | return target; | |
6001 | break; | |
bf8e3599 | 6002 | |
46f3a74a | 6003 | case BUILT_IN_STRPBRK: |
daa1d5f5 | 6004 | target = expand_builtin_strpbrk (arglist, TREE_TYPE (exp), target, mode); |
46f3a74a | 6005 | if (target) |
6006 | return target; | |
6007 | break; | |
bf8e3599 | 6008 | |
398aae36 | 6009 | case BUILT_IN_INDEX: |
83d79705 | 6010 | case BUILT_IN_STRCHR: |
daa1d5f5 | 6011 | target = expand_builtin_strchr (arglist, TREE_TYPE (exp), target, mode); |
83d79705 | 6012 | if (target) |
6013 | return target; | |
6014 | break; | |
6015 | ||
398aae36 | 6016 | case BUILT_IN_RINDEX: |
83d79705 | 6017 | case BUILT_IN_STRRCHR: |
daa1d5f5 | 6018 | target = expand_builtin_strrchr (arglist, TREE_TYPE (exp), target, mode); |
83d79705 | 6019 | if (target) |
6020 | return target; | |
6021 | break; | |
6022 | ||
53800dbe | 6023 | case BUILT_IN_MEMCPY: |
5a0de151 | 6024 | target = expand_builtin_memcpy (exp, target, mode); |
3b824fa6 | 6025 | if (target) |
6026 | return target; | |
6027 | break; | |
6028 | ||
6029 | case BUILT_IN_MEMPCPY: | |
7da1412b | 6030 | target = expand_builtin_mempcpy (arglist, TREE_TYPE (exp), target, mode, /*endp=*/ 1); |
53800dbe | 6031 | if (target) |
6032 | return target; | |
6033 | break; | |
6034 | ||
c4950093 | 6035 | case BUILT_IN_MEMMOVE: |
0b25db21 | 6036 | target = expand_builtin_memmove (arglist, TREE_TYPE (exp), target, |
3b1757a2 | 6037 | mode); |
c4950093 | 6038 | if (target) |
6039 | return target; | |
6040 | break; | |
6041 | ||
6042 | case BUILT_IN_BCOPY: | |
0b25db21 | 6043 | target = expand_builtin_bcopy (exp); |
c4950093 | 6044 | if (target) |
6045 | return target; | |
6046 | break; | |
6047 | ||
53800dbe | 6048 | case BUILT_IN_MEMSET: |
0b25db21 | 6049 | target = expand_builtin_memset (arglist, target, mode, exp); |
53800dbe | 6050 | if (target) |
6051 | return target; | |
6052 | break; | |
6053 | ||
ffc83088 | 6054 | case BUILT_IN_BZERO: |
0b25db21 | 6055 | target = expand_builtin_bzero (exp); |
ffc83088 | 6056 | if (target) |
6057 | return target; | |
6058 | break; | |
6059 | ||
53800dbe | 6060 | case BUILT_IN_STRCMP: |
83d79705 | 6061 | target = expand_builtin_strcmp (exp, target, mode); |
53800dbe | 6062 | if (target) |
6063 | return target; | |
6064 | break; | |
6065 | ||
ed09096d | 6066 | case BUILT_IN_STRNCMP: |
6067 | target = expand_builtin_strncmp (exp, target, mode); | |
6068 | if (target) | |
6069 | return target; | |
6070 | break; | |
6071 | ||
071f1696 | 6072 | case BUILT_IN_BCMP: |
53800dbe | 6073 | case BUILT_IN_MEMCMP: |
6f428e8b | 6074 | target = expand_builtin_memcmp (exp, arglist, target, mode); |
53800dbe | 6075 | if (target) |
6076 | return target; | |
6077 | break; | |
53800dbe | 6078 | |
6079 | case BUILT_IN_SETJMP: | |
2c8a1497 | 6080 | /* This should have been lowered to the builtins below. */ |
6081 | gcc_unreachable (); | |
6082 | ||
6083 | case BUILT_IN_SETJMP_SETUP: | |
6084 | /* __builtin_setjmp_setup is passed a pointer to an array of five words | |
6085 | and the receiver label. */ | |
6086 | if (validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
6087 | { | |
6088 | rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget, | |
6089 | VOIDmode, EXPAND_NORMAL); | |
6090 | tree label = TREE_OPERAND (TREE_VALUE (TREE_CHAIN (arglist)), 0); | |
6091 | rtx label_r = label_rtx (label); | |
6092 | ||
6093 | /* This is copied from the handling of non-local gotos. */ | |
6094 | expand_builtin_setjmp_setup (buf_addr, label_r); | |
6095 | nonlocal_goto_handler_labels | |
6096 | = gen_rtx_EXPR_LIST (VOIDmode, label_r, | |
6097 | nonlocal_goto_handler_labels); | |
6098 | /* ??? Do not let expand_label treat us as such since we would | |
6099 | not want to be both on the list of non-local labels and on | |
6100 | the list of forced labels. */ | |
6101 | FORCED_LABEL (label) = 0; | |
6102 | return const0_rtx; | |
6103 | } | |
6104 | break; | |
6105 | ||
6106 | case BUILT_IN_SETJMP_DISPATCHER: | |
6107 | /* __builtin_setjmp_dispatcher is passed the dispatcher label. */ | |
6108 | if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE)) | |
6109 | { | |
6110 | tree label = TREE_OPERAND (TREE_VALUE (arglist), 0); | |
6111 | rtx label_r = label_rtx (label); | |
6112 | ||
6113 | /* Remove the dispatcher label from the list of non-local labels | |
6114 | since the receiver labels have been added to it above. */ | |
6115 | remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels); | |
6116 | return const0_rtx; | |
6117 | } | |
6118 | break; | |
6119 | ||
6120 | case BUILT_IN_SETJMP_RECEIVER: | |
6121 | /* __builtin_setjmp_receiver is passed the receiver label. */ | |
6122 | if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE)) | |
6123 | { | |
6124 | tree label = TREE_OPERAND (TREE_VALUE (arglist), 0); | |
6125 | rtx label_r = label_rtx (label); | |
6126 | ||
6127 | expand_builtin_setjmp_receiver (label_r); | |
6128 | return const0_rtx; | |
6129 | } | |
6b7f6858 | 6130 | break; |
53800dbe | 6131 | |
6132 | /* __builtin_longjmp is passed a pointer to an array of five words. | |
6133 | It's similar to the C library longjmp function but works with | |
6134 | __builtin_setjmp above. */ | |
6135 | case BUILT_IN_LONGJMP: | |
2c8a1497 | 6136 | if (validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
53800dbe | 6137 | { |
6138 | rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget, | |
8ec3c5c2 | 6139 | VOIDmode, EXPAND_NORMAL); |
6140 | rtx value = expand_normal (TREE_VALUE (TREE_CHAIN (arglist))); | |
53800dbe | 6141 | |
6142 | if (value != const1_rtx) | |
6143 | { | |
1e5fcbe2 | 6144 | error ("%<__builtin_longjmp%> second argument must be 1"); |
53800dbe | 6145 | return const0_rtx; |
6146 | } | |
6147 | ||
6148 | expand_builtin_longjmp (buf_addr, value); | |
6149 | return const0_rtx; | |
6150 | } | |
2c8a1497 | 6151 | break; |
53800dbe | 6152 | |
4ee9c684 | 6153 | case BUILT_IN_NONLOCAL_GOTO: |
6154 | target = expand_builtin_nonlocal_goto (arglist); | |
6155 | if (target) | |
6156 | return target; | |
6157 | break; | |
6158 | ||
843d08a9 | 6159 | /* This updates the setjmp buffer that is its argument with the value |
6160 | of the current stack pointer. */ | |
6161 | case BUILT_IN_UPDATE_SETJMP_BUF: | |
6162 | if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE)) | |
6163 | { | |
6164 | rtx buf_addr | |
8ec3c5c2 | 6165 | = expand_normal (TREE_VALUE (arglist)); |
843d08a9 | 6166 | |
6167 | expand_builtin_update_setjmp_buf (buf_addr); | |
6168 | return const0_rtx; | |
6169 | } | |
6170 | break; | |
6171 | ||
53800dbe | 6172 | case BUILT_IN_TRAP: |
a0ef1725 | 6173 | expand_builtin_trap (); |
53800dbe | 6174 | return const0_rtx; |
6175 | ||
19bf118a | 6176 | case BUILT_IN_PRINTF: |
0b25db21 | 6177 | target = expand_builtin_printf (exp, target, mode, false); |
19bf118a | 6178 | if (target) |
6179 | return target; | |
6180 | break; | |
6181 | ||
6182 | case BUILT_IN_PRINTF_UNLOCKED: | |
0b25db21 | 6183 | target = expand_builtin_printf (exp, target, mode, true); |
19bf118a | 6184 | if (target) |
6185 | return target; | |
6186 | break; | |
6187 | ||
df94cd3b | 6188 | case BUILT_IN_FPUTS: |
19bf118a | 6189 | target = expand_builtin_fputs (arglist, target, false); |
c013a46e | 6190 | if (target) |
6191 | return target; | |
6192 | break; | |
6193 | case BUILT_IN_FPUTS_UNLOCKED: | |
19bf118a | 6194 | target = expand_builtin_fputs (arglist, target, true); |
6195 | if (target) | |
6196 | return target; | |
6197 | break; | |
6198 | ||
6199 | case BUILT_IN_FPRINTF: | |
0b25db21 | 6200 | target = expand_builtin_fprintf (exp, target, mode, false); |
19bf118a | 6201 | if (target) |
6202 | return target; | |
6203 | break; | |
6204 | ||
6205 | case BUILT_IN_FPRINTF_UNLOCKED: | |
0b25db21 | 6206 | target = expand_builtin_fprintf (exp, target, mode, true); |
df94cd3b | 6207 | if (target) |
6208 | return target; | |
6209 | break; | |
bf8e3599 | 6210 | |
6411575e | 6211 | case BUILT_IN_SPRINTF: |
6212 | target = expand_builtin_sprintf (arglist, target, mode); | |
6213 | if (target) | |
6214 | return target; | |
6215 | break; | |
6216 | ||
4f35b1fc | 6217 | CASE_FLT_FN (BUILT_IN_SIGNBIT): |
27f261ef | 6218 | target = expand_builtin_signbit (exp, target); |
6219 | if (target) | |
6220 | return target; | |
6221 | break; | |
6222 | ||
53800dbe | 6223 | /* Various hooks for the DWARF 2 __throw routine. */ |
6224 | case BUILT_IN_UNWIND_INIT: | |
6225 | expand_builtin_unwind_init (); | |
6226 | return const0_rtx; | |
6227 | case BUILT_IN_DWARF_CFA: | |
6228 | return virtual_cfa_rtx; | |
6229 | #ifdef DWARF2_UNWIND_INFO | |
f8f023a5 | 6230 | case BUILT_IN_DWARF_SP_COLUMN: |
6231 | return expand_builtin_dwarf_sp_column (); | |
695e919b | 6232 | case BUILT_IN_INIT_DWARF_REG_SIZES: |
6233 | expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist)); | |
6234 | return const0_rtx; | |
53800dbe | 6235 | #endif |
6236 | case BUILT_IN_FROB_RETURN_ADDR: | |
6237 | return expand_builtin_frob_return_addr (TREE_VALUE (arglist)); | |
6238 | case BUILT_IN_EXTRACT_RETURN_ADDR: | |
6239 | return expand_builtin_extract_return_addr (TREE_VALUE (arglist)); | |
6240 | case BUILT_IN_EH_RETURN: | |
6241 | expand_builtin_eh_return (TREE_VALUE (arglist), | |
df4b504c | 6242 | TREE_VALUE (TREE_CHAIN (arglist))); |
53800dbe | 6243 | return const0_rtx; |
df4b504c | 6244 | #ifdef EH_RETURN_DATA_REGNO |
6245 | case BUILT_IN_EH_RETURN_DATA_REGNO: | |
6246 | return expand_builtin_eh_return_data_regno (arglist); | |
6247 | #endif | |
26093bf4 | 6248 | case BUILT_IN_EXTEND_POINTER: |
6249 | return expand_builtin_extend_pointer (TREE_VALUE (arglist)); | |
6250 | ||
7ccc713a | 6251 | case BUILT_IN_VA_START: |
a66c9326 | 6252 | case BUILT_IN_STDARG_START: |
7ccc713a | 6253 | return expand_builtin_va_start (arglist); |
a66c9326 | 6254 | case BUILT_IN_VA_END: |
6255 | return expand_builtin_va_end (arglist); | |
6256 | case BUILT_IN_VA_COPY: | |
6257 | return expand_builtin_va_copy (arglist); | |
89cfe6e5 | 6258 | case BUILT_IN_EXPECT: |
6259 | return expand_builtin_expect (arglist, target); | |
5e3608d8 | 6260 | case BUILT_IN_PREFETCH: |
6261 | expand_builtin_prefetch (arglist); | |
6262 | return const0_rtx; | |
6263 | ||
4ee9c684 | 6264 | case BUILT_IN_PROFILE_FUNC_ENTER: |
6265 | return expand_builtin_profile_func (false); | |
6266 | case BUILT_IN_PROFILE_FUNC_EXIT: | |
6267 | return expand_builtin_profile_func (true); | |
6268 | ||
6269 | case BUILT_IN_INIT_TRAMPOLINE: | |
6270 | return expand_builtin_init_trampoline (arglist); | |
6271 | case BUILT_IN_ADJUST_TRAMPOLINE: | |
6272 | return expand_builtin_adjust_trampoline (arglist); | |
6273 | ||
73673831 | 6274 | case BUILT_IN_FORK: |
6275 | case BUILT_IN_EXECL: | |
6276 | case BUILT_IN_EXECV: | |
6277 | case BUILT_IN_EXECLP: | |
6278 | case BUILT_IN_EXECLE: | |
6279 | case BUILT_IN_EXECVP: | |
6280 | case BUILT_IN_EXECVE: | |
6281 | target = expand_builtin_fork_or_exec (fndecl, arglist, target, ignore); | |
6282 | if (target) | |
6283 | return target; | |
6284 | break; | |
53800dbe | 6285 | |
b6a5fc45 | 6286 | case BUILT_IN_FETCH_AND_ADD_1: |
6287 | case BUILT_IN_FETCH_AND_ADD_2: | |
6288 | case BUILT_IN_FETCH_AND_ADD_4: | |
6289 | case BUILT_IN_FETCH_AND_ADD_8: | |
27213ba3 | 6290 | case BUILT_IN_FETCH_AND_ADD_16: |
3e272de8 | 6291 | mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_ADD_1); |
6292 | target = expand_builtin_sync_operation (mode, arglist, PLUS, | |
b6a5fc45 | 6293 | false, target, ignore); |
6294 | if (target) | |
6295 | return target; | |
6296 | break; | |
6297 | ||
6298 | case BUILT_IN_FETCH_AND_SUB_1: | |
6299 | case BUILT_IN_FETCH_AND_SUB_2: | |
6300 | case BUILT_IN_FETCH_AND_SUB_4: | |
6301 | case BUILT_IN_FETCH_AND_SUB_8: | |
27213ba3 | 6302 | case BUILT_IN_FETCH_AND_SUB_16: |
3e272de8 | 6303 | mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_SUB_1); |
6304 | target = expand_builtin_sync_operation (mode, arglist, MINUS, | |
b6a5fc45 | 6305 | false, target, ignore); |
6306 | if (target) | |
6307 | return target; | |
6308 | break; | |
6309 | ||
6310 | case BUILT_IN_FETCH_AND_OR_1: | |
6311 | case BUILT_IN_FETCH_AND_OR_2: | |
6312 | case BUILT_IN_FETCH_AND_OR_4: | |
6313 | case BUILT_IN_FETCH_AND_OR_8: | |
27213ba3 | 6314 | case BUILT_IN_FETCH_AND_OR_16: |
3e272de8 | 6315 | mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_OR_1); |
6316 | target = expand_builtin_sync_operation (mode, arglist, IOR, | |
b6a5fc45 | 6317 | false, target, ignore); |
6318 | if (target) | |
6319 | return target; | |
6320 | break; | |
6321 | ||
6322 | case BUILT_IN_FETCH_AND_AND_1: | |
6323 | case BUILT_IN_FETCH_AND_AND_2: | |
6324 | case BUILT_IN_FETCH_AND_AND_4: | |
6325 | case BUILT_IN_FETCH_AND_AND_8: | |
27213ba3 | 6326 | case BUILT_IN_FETCH_AND_AND_16: |
3e272de8 | 6327 | mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_AND_1); |
6328 | target = expand_builtin_sync_operation (mode, arglist, AND, | |
b6a5fc45 | 6329 | false, target, ignore); |
6330 | if (target) | |
6331 | return target; | |
6332 | break; | |
6333 | ||
6334 | case BUILT_IN_FETCH_AND_XOR_1: | |
6335 | case BUILT_IN_FETCH_AND_XOR_2: | |
6336 | case BUILT_IN_FETCH_AND_XOR_4: | |
6337 | case BUILT_IN_FETCH_AND_XOR_8: | |
27213ba3 | 6338 | case BUILT_IN_FETCH_AND_XOR_16: |
3e272de8 | 6339 | mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_XOR_1); |
6340 | target = expand_builtin_sync_operation (mode, arglist, XOR, | |
b6a5fc45 | 6341 | false, target, ignore); |
6342 | if (target) | |
6343 | return target; | |
6344 | break; | |
6345 | ||
6346 | case BUILT_IN_FETCH_AND_NAND_1: | |
6347 | case BUILT_IN_FETCH_AND_NAND_2: | |
6348 | case BUILT_IN_FETCH_AND_NAND_4: | |
6349 | case BUILT_IN_FETCH_AND_NAND_8: | |
27213ba3 | 6350 | case BUILT_IN_FETCH_AND_NAND_16: |
3e272de8 | 6351 | mode = get_builtin_sync_mode (fcode - BUILT_IN_FETCH_AND_NAND_1); |
6352 | target = expand_builtin_sync_operation (mode, arglist, NOT, | |
b6a5fc45 | 6353 | false, target, ignore); |
6354 | if (target) | |
6355 | return target; | |
6356 | break; | |
6357 | ||
6358 | case BUILT_IN_ADD_AND_FETCH_1: | |
6359 | case BUILT_IN_ADD_AND_FETCH_2: | |
6360 | case BUILT_IN_ADD_AND_FETCH_4: | |
6361 | case BUILT_IN_ADD_AND_FETCH_8: | |
27213ba3 | 6362 | case BUILT_IN_ADD_AND_FETCH_16: |
3e272de8 | 6363 | mode = get_builtin_sync_mode (fcode - BUILT_IN_ADD_AND_FETCH_1); |
6364 | target = expand_builtin_sync_operation (mode, arglist, PLUS, | |
b6a5fc45 | 6365 | true, target, ignore); |
6366 | if (target) | |
6367 | return target; | |
6368 | break; | |
6369 | ||
6370 | case BUILT_IN_SUB_AND_FETCH_1: | |
6371 | case BUILT_IN_SUB_AND_FETCH_2: | |
6372 | case BUILT_IN_SUB_AND_FETCH_4: | |
6373 | case BUILT_IN_SUB_AND_FETCH_8: | |
27213ba3 | 6374 | case BUILT_IN_SUB_AND_FETCH_16: |
3e272de8 | 6375 | mode = get_builtin_sync_mode (fcode - BUILT_IN_SUB_AND_FETCH_1); |
6376 | target = expand_builtin_sync_operation (mode, arglist, MINUS, | |
b6a5fc45 | 6377 | true, target, ignore); |
6378 | if (target) | |
6379 | return target; | |
6380 | break; | |
6381 | ||
6382 | case BUILT_IN_OR_AND_FETCH_1: | |
6383 | case BUILT_IN_OR_AND_FETCH_2: | |
6384 | case BUILT_IN_OR_AND_FETCH_4: | |
6385 | case BUILT_IN_OR_AND_FETCH_8: | |
27213ba3 | 6386 | case BUILT_IN_OR_AND_FETCH_16: |
3e272de8 | 6387 | mode = get_builtin_sync_mode (fcode - BUILT_IN_OR_AND_FETCH_1); |
6388 | target = expand_builtin_sync_operation (mode, arglist, IOR, | |
b6a5fc45 | 6389 | true, target, ignore); |
6390 | if (target) | |
6391 | return target; | |
6392 | break; | |
6393 | ||
6394 | case BUILT_IN_AND_AND_FETCH_1: | |
6395 | case BUILT_IN_AND_AND_FETCH_2: | |
6396 | case BUILT_IN_AND_AND_FETCH_4: | |
6397 | case BUILT_IN_AND_AND_FETCH_8: | |
27213ba3 | 6398 | case BUILT_IN_AND_AND_FETCH_16: |
3e272de8 | 6399 | mode = get_builtin_sync_mode (fcode - BUILT_IN_AND_AND_FETCH_1); |
6400 | target = expand_builtin_sync_operation (mode, arglist, AND, | |
b6a5fc45 | 6401 | true, target, ignore); |
6402 | if (target) | |
6403 | return target; | |
6404 | break; | |
6405 | ||
6406 | case BUILT_IN_XOR_AND_FETCH_1: | |
6407 | case BUILT_IN_XOR_AND_FETCH_2: | |
6408 | case BUILT_IN_XOR_AND_FETCH_4: | |
6409 | case BUILT_IN_XOR_AND_FETCH_8: | |
27213ba3 | 6410 | case BUILT_IN_XOR_AND_FETCH_16: |
3e272de8 | 6411 | mode = get_builtin_sync_mode (fcode - BUILT_IN_XOR_AND_FETCH_1); |
6412 | target = expand_builtin_sync_operation (mode, arglist, XOR, | |
b6a5fc45 | 6413 | true, target, ignore); |
6414 | if (target) | |
6415 | return target; | |
6416 | break; | |
6417 | ||
6418 | case BUILT_IN_NAND_AND_FETCH_1: | |
6419 | case BUILT_IN_NAND_AND_FETCH_2: | |
6420 | case BUILT_IN_NAND_AND_FETCH_4: | |
6421 | case BUILT_IN_NAND_AND_FETCH_8: | |
27213ba3 | 6422 | case BUILT_IN_NAND_AND_FETCH_16: |
3e272de8 | 6423 | mode = get_builtin_sync_mode (fcode - BUILT_IN_NAND_AND_FETCH_1); |
6424 | target = expand_builtin_sync_operation (mode, arglist, NOT, | |
b6a5fc45 | 6425 | true, target, ignore); |
6426 | if (target) | |
6427 | return target; | |
6428 | break; | |
6429 | ||
6430 | case BUILT_IN_BOOL_COMPARE_AND_SWAP_1: | |
6431 | case BUILT_IN_BOOL_COMPARE_AND_SWAP_2: | |
6432 | case BUILT_IN_BOOL_COMPARE_AND_SWAP_4: | |
6433 | case BUILT_IN_BOOL_COMPARE_AND_SWAP_8: | |
27213ba3 | 6434 | case BUILT_IN_BOOL_COMPARE_AND_SWAP_16: |
a601d32a | 6435 | if (mode == VOIDmode) |
6436 | mode = TYPE_MODE (boolean_type_node); | |
b6a5fc45 | 6437 | if (!target || !register_operand (target, mode)) |
6438 | target = gen_reg_rtx (mode); | |
3e272de8 | 6439 | |
6440 | mode = get_builtin_sync_mode (fcode - BUILT_IN_BOOL_COMPARE_AND_SWAP_1); | |
6441 | target = expand_builtin_compare_and_swap (mode, arglist, true, target); | |
b6a5fc45 | 6442 | if (target) |
6443 | return target; | |
6444 | break; | |
6445 | ||
6446 | case BUILT_IN_VAL_COMPARE_AND_SWAP_1: | |
6447 | case BUILT_IN_VAL_COMPARE_AND_SWAP_2: | |
6448 | case BUILT_IN_VAL_COMPARE_AND_SWAP_4: | |
6449 | case BUILT_IN_VAL_COMPARE_AND_SWAP_8: | |
27213ba3 | 6450 | case BUILT_IN_VAL_COMPARE_AND_SWAP_16: |
3e272de8 | 6451 | mode = get_builtin_sync_mode (fcode - BUILT_IN_VAL_COMPARE_AND_SWAP_1); |
6452 | target = expand_builtin_compare_and_swap (mode, arglist, false, target); | |
b6a5fc45 | 6453 | if (target) |
6454 | return target; | |
6455 | break; | |
6456 | ||
6457 | case BUILT_IN_LOCK_TEST_AND_SET_1: | |
6458 | case BUILT_IN_LOCK_TEST_AND_SET_2: | |
6459 | case BUILT_IN_LOCK_TEST_AND_SET_4: | |
6460 | case BUILT_IN_LOCK_TEST_AND_SET_8: | |
27213ba3 | 6461 | case BUILT_IN_LOCK_TEST_AND_SET_16: |
3e272de8 | 6462 | mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_TEST_AND_SET_1); |
6463 | target = expand_builtin_lock_test_and_set (mode, arglist, target); | |
b6a5fc45 | 6464 | if (target) |
6465 | return target; | |
6466 | break; | |
6467 | ||
6468 | case BUILT_IN_LOCK_RELEASE_1: | |
6469 | case BUILT_IN_LOCK_RELEASE_2: | |
6470 | case BUILT_IN_LOCK_RELEASE_4: | |
6471 | case BUILT_IN_LOCK_RELEASE_8: | |
27213ba3 | 6472 | case BUILT_IN_LOCK_RELEASE_16: |
3e272de8 | 6473 | mode = get_builtin_sync_mode (fcode - BUILT_IN_LOCK_RELEASE_1); |
6474 | expand_builtin_lock_release (mode, arglist); | |
b6a5fc45 | 6475 | return const0_rtx; |
6476 | ||
6477 | case BUILT_IN_SYNCHRONIZE: | |
6478 | expand_builtin_synchronize (); | |
6479 | return const0_rtx; | |
6480 | ||
0a39fd54 | 6481 | case BUILT_IN_OBJECT_SIZE: |
6482 | return expand_builtin_object_size (exp); | |
6483 | ||
6484 | case BUILT_IN_MEMCPY_CHK: | |
6485 | case BUILT_IN_MEMPCPY_CHK: | |
6486 | case BUILT_IN_MEMMOVE_CHK: | |
6487 | case BUILT_IN_MEMSET_CHK: | |
6488 | target = expand_builtin_memory_chk (exp, target, mode, fcode); | |
6489 | if (target) | |
6490 | return target; | |
6491 | break; | |
6492 | ||
6493 | case BUILT_IN_STRCPY_CHK: | |
6494 | case BUILT_IN_STPCPY_CHK: | |
6495 | case BUILT_IN_STRNCPY_CHK: | |
6496 | case BUILT_IN_STRCAT_CHK: | |
b356dfef | 6497 | case BUILT_IN_STRNCAT_CHK: |
0a39fd54 | 6498 | case BUILT_IN_SNPRINTF_CHK: |
6499 | case BUILT_IN_VSNPRINTF_CHK: | |
6500 | maybe_emit_chk_warning (exp, fcode); | |
6501 | break; | |
6502 | ||
6503 | case BUILT_IN_SPRINTF_CHK: | |
6504 | case BUILT_IN_VSPRINTF_CHK: | |
6505 | maybe_emit_sprintf_chk_warning (exp, fcode); | |
6506 | break; | |
6507 | ||
92482ee0 | 6508 | default: /* just do library call, if unknown builtin */ |
146c1b4f | 6509 | break; |
53800dbe | 6510 | } |
6511 | ||
6512 | /* The switch statement above can drop through to cause the function | |
6513 | to be called normally. */ | |
6514 | return expand_call (exp, target, ignore); | |
6515 | } | |
650e4c94 | 6516 | |
805e22b2 | 6517 | /* Determine whether a tree node represents a call to a built-in |
52203a9d | 6518 | function. If the tree T is a call to a built-in function with |
6519 | the right number of arguments of the appropriate types, return | |
6520 | the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT. | |
6521 | Otherwise the return value is END_BUILTINS. */ | |
aecda0d6 | 6522 | |
805e22b2 | 6523 | enum built_in_function |
aecda0d6 | 6524 | builtin_mathfn_code (tree t) |
805e22b2 | 6525 | { |
52203a9d | 6526 | tree fndecl, arglist, parmlist; |
6527 | tree argtype, parmtype; | |
805e22b2 | 6528 | |
6529 | if (TREE_CODE (t) != CALL_EXPR | |
6530 | || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR) | |
6531 | return END_BUILTINS; | |
6532 | ||
c6e6ecb1 | 6533 | fndecl = get_callee_fndecl (t); |
6534 | if (fndecl == NULL_TREE | |
52203a9d | 6535 | || TREE_CODE (fndecl) != FUNCTION_DECL |
805e22b2 | 6536 | || ! DECL_BUILT_IN (fndecl) |
6537 | || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) | |
6538 | return END_BUILTINS; | |
6539 | ||
6540 | arglist = TREE_OPERAND (t, 1); | |
52203a9d | 6541 | parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); |
6542 | for (; parmlist; parmlist = TREE_CHAIN (parmlist)) | |
e9f80ff5 | 6543 | { |
52203a9d | 6544 | /* If a function doesn't take a variable number of arguments, |
6545 | the last element in the list will have type `void'. */ | |
6546 | parmtype = TREE_VALUE (parmlist); | |
6547 | if (VOID_TYPE_P (parmtype)) | |
6548 | { | |
6549 | if (arglist) | |
6550 | return END_BUILTINS; | |
6551 | return DECL_FUNCTION_CODE (fndecl); | |
6552 | } | |
6553 | ||
6554 | if (! arglist) | |
e9f80ff5 | 6555 | return END_BUILTINS; |
e9f80ff5 | 6556 | |
52203a9d | 6557 | argtype = TREE_TYPE (TREE_VALUE (arglist)); |
6558 | ||
6559 | if (SCALAR_FLOAT_TYPE_P (parmtype)) | |
6560 | { | |
6561 | if (! SCALAR_FLOAT_TYPE_P (argtype)) | |
6562 | return END_BUILTINS; | |
6563 | } | |
6564 | else if (COMPLEX_FLOAT_TYPE_P (parmtype)) | |
6565 | { | |
6566 | if (! COMPLEX_FLOAT_TYPE_P (argtype)) | |
6567 | return END_BUILTINS; | |
6568 | } | |
6569 | else if (POINTER_TYPE_P (parmtype)) | |
6570 | { | |
6571 | if (! POINTER_TYPE_P (argtype)) | |
6572 | return END_BUILTINS; | |
6573 | } | |
6574 | else if (INTEGRAL_TYPE_P (parmtype)) | |
6575 | { | |
6576 | if (! INTEGRAL_TYPE_P (argtype)) | |
6577 | return END_BUILTINS; | |
6578 | } | |
6579 | else | |
e9f80ff5 | 6580 | return END_BUILTINS; |
52203a9d | 6581 | |
6582 | arglist = TREE_CHAIN (arglist); | |
e9f80ff5 | 6583 | } |
6584 | ||
52203a9d | 6585 | /* Variable-length argument list. */ |
805e22b2 | 6586 | return DECL_FUNCTION_CODE (fndecl); |
6587 | } | |
6588 | ||
650e4c94 | 6589 | /* Fold a call to __builtin_constant_p, if we know it will evaluate to a |
6590 | constant. ARGLIST is the argument list of the call. */ | |
6591 | ||
6592 | static tree | |
aecda0d6 | 6593 | fold_builtin_constant_p (tree arglist) |
650e4c94 | 6594 | { |
6595 | if (arglist == 0) | |
6596 | return 0; | |
6597 | ||
6598 | arglist = TREE_VALUE (arglist); | |
6599 | ||
6600 | /* We return 1 for a numeric type that's known to be a constant | |
6601 | value at compile-time or for an aggregate type that's a | |
6602 | literal constant. */ | |
6603 | STRIP_NOPS (arglist); | |
6604 | ||
6605 | /* If we know this is a constant, emit the constant of one. */ | |
ce45a448 | 6606 | if (CONSTANT_CLASS_P (arglist) |
650e4c94 | 6607 | || (TREE_CODE (arglist) == CONSTRUCTOR |
adcfa3a3 | 6608 | && TREE_CONSTANT (arglist))) |
650e4c94 | 6609 | return integer_one_node; |
adcfa3a3 | 6610 | if (TREE_CODE (arglist) == ADDR_EXPR) |
6611 | { | |
6612 | tree op = TREE_OPERAND (arglist, 0); | |
6613 | if (TREE_CODE (op) == STRING_CST | |
6614 | || (TREE_CODE (op) == ARRAY_REF | |
6615 | && integer_zerop (TREE_OPERAND (op, 1)) | |
6616 | && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST)) | |
6617 | return integer_one_node; | |
6618 | } | |
650e4c94 | 6619 | |
1fb4300c | 6620 | /* If this expression has side effects, show we don't know it to be a |
6621 | constant. Likewise if it's a pointer or aggregate type since in | |
6622 | those case we only want literals, since those are only optimized | |
f97c71a1 | 6623 | when generating RTL, not later. |
6624 | And finally, if we are compiling an initializer, not code, we | |
6625 | need to return a definite result now; there's not going to be any | |
6626 | more optimization done. */ | |
1fb4300c | 6627 | if (TREE_SIDE_EFFECTS (arglist) |
650e4c94 | 6628 | || AGGREGATE_TYPE_P (TREE_TYPE (arglist)) |
f97c71a1 | 6629 | || POINTER_TYPE_P (TREE_TYPE (arglist)) |
47be647d | 6630 | || cfun == 0 |
6631 | || folding_initializer) | |
650e4c94 | 6632 | return integer_zero_node; |
6633 | ||
6634 | return 0; | |
6635 | } | |
6636 | ||
4ee9c684 | 6637 | /* Fold a call to __builtin_expect, if we expect that a comparison against |
6638 | the argument will fold to a constant. In practice, this means a true | |
6639 | constant or the address of a non-weak symbol. ARGLIST is the argument | |
6640 | list of the call. */ | |
6641 | ||
6642 | static tree | |
6643 | fold_builtin_expect (tree arglist) | |
6644 | { | |
6645 | tree arg, inner; | |
6646 | ||
6647 | if (arglist == 0) | |
6648 | return 0; | |
6649 | ||
6650 | arg = TREE_VALUE (arglist); | |
6651 | ||
6652 | /* If the argument isn't invariant, then there's nothing we can do. */ | |
6653 | if (!TREE_INVARIANT (arg)) | |
6654 | return 0; | |
6655 | ||
6656 | /* If we're looking at an address of a weak decl, then do not fold. */ | |
6657 | inner = arg; | |
6658 | STRIP_NOPS (inner); | |
6659 | if (TREE_CODE (inner) == ADDR_EXPR) | |
6660 | { | |
6661 | do | |
6662 | { | |
6663 | inner = TREE_OPERAND (inner, 0); | |
6664 | } | |
6665 | while (TREE_CODE (inner) == COMPONENT_REF | |
6666 | || TREE_CODE (inner) == ARRAY_REF); | |
6667 | if (DECL_P (inner) && DECL_WEAK (inner)) | |
6668 | return 0; | |
6669 | } | |
6670 | ||
6671 | /* Otherwise, ARG already has the proper type for the return value. */ | |
6672 | return arg; | |
6673 | } | |
6674 | ||
539a3a92 | 6675 | /* Fold a call to __builtin_classify_type. */ |
27d0c333 | 6676 | |
539a3a92 | 6677 | static tree |
aecda0d6 | 6678 | fold_builtin_classify_type (tree arglist) |
539a3a92 | 6679 | { |
6680 | if (arglist == 0) | |
7016c612 | 6681 | return build_int_cst (NULL_TREE, no_type_class); |
539a3a92 | 6682 | |
7c446c95 | 6683 | return build_int_cst (NULL_TREE, |
7016c612 | 6684 | type_to_class (TREE_TYPE (TREE_VALUE (arglist)))); |
539a3a92 | 6685 | } |
6686 | ||
e6e27594 | 6687 | /* Fold a call to __builtin_strlen. */ |
6688 | ||
6689 | static tree | |
6690 | fold_builtin_strlen (tree arglist) | |
6691 | { | |
6692 | if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE)) | |
6693 | return NULL_TREE; | |
6694 | else | |
6695 | { | |
6696 | tree len = c_strlen (TREE_VALUE (arglist), 0); | |
6697 | ||
6698 | if (len) | |
6699 | { | |
6700 | /* Convert from the internal "sizetype" type to "size_t". */ | |
6701 | if (size_type_node) | |
6702 | len = fold_convert (size_type_node, len); | |
6703 | return len; | |
6704 | } | |
6705 | ||
6706 | return NULL_TREE; | |
6707 | } | |
6708 | } | |
6709 | ||
92c43e3c | 6710 | /* Fold a call to __builtin_inf or __builtin_huge_val. */ |
6711 | ||
6712 | static tree | |
aecda0d6 | 6713 | fold_builtin_inf (tree type, int warn) |
92c43e3c | 6714 | { |
aa870c1b | 6715 | REAL_VALUE_TYPE real; |
6716 | ||
40f4dbd5 | 6717 | /* __builtin_inff is intended to be usable to define INFINITY on all |
6718 | targets. If an infinity is not available, INFINITY expands "to a | |
6719 | positive constant of type float that overflows at translation | |
6720 | time", footnote "In this case, using INFINITY will violate the | |
6721 | constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4). | |
6722 | Thus we pedwarn to ensure this constraint violation is | |
6723 | diagnosed. */ | |
92c43e3c | 6724 | if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn) |
40f4dbd5 | 6725 | pedwarn ("target format does not support infinity"); |
92c43e3c | 6726 | |
aa870c1b | 6727 | real_inf (&real); |
6728 | return build_real (type, real); | |
92c43e3c | 6729 | } |
6730 | ||
b0db7939 | 6731 | /* Fold a call to __builtin_nan or __builtin_nans. */ |
6732 | ||
6733 | static tree | |
aecda0d6 | 6734 | fold_builtin_nan (tree arglist, tree type, int quiet) |
b0db7939 | 6735 | { |
6736 | REAL_VALUE_TYPE real; | |
6737 | const char *str; | |
6738 | ||
6739 | if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE)) | |
6740 | return 0; | |
6741 | str = c_getstr (TREE_VALUE (arglist)); | |
6742 | if (!str) | |
6743 | return 0; | |
6744 | ||
6745 | if (!real_nan (&real, str, quiet, TYPE_MODE (type))) | |
6746 | return 0; | |
6747 | ||
6748 | return build_real (type, real); | |
6749 | } | |
6750 | ||
277f8dd2 | 6751 | /* Return true if the floating point expression T has an integer value. |
6752 | We also allow +Inf, -Inf and NaN to be considered integer values. */ | |
6753 | ||
6754 | static bool | |
6755 | integer_valued_real_p (tree t) | |
6756 | { | |
6757 | switch (TREE_CODE (t)) | |
6758 | { | |
6759 | case FLOAT_EXPR: | |
6760 | return true; | |
6761 | ||
6762 | case ABS_EXPR: | |
6763 | case SAVE_EXPR: | |
6764 | case NON_LVALUE_EXPR: | |
6765 | return integer_valued_real_p (TREE_OPERAND (t, 0)); | |
6766 | ||
6767 | case COMPOUND_EXPR: | |
6768 | case MODIFY_EXPR: | |
6769 | case BIND_EXPR: | |
6770 | return integer_valued_real_p (TREE_OPERAND (t, 1)); | |
6771 | ||
6772 | case PLUS_EXPR: | |
6773 | case MINUS_EXPR: | |
6774 | case MULT_EXPR: | |
6775 | case MIN_EXPR: | |
6776 | case MAX_EXPR: | |
6777 | return integer_valued_real_p (TREE_OPERAND (t, 0)) | |
6778 | && integer_valued_real_p (TREE_OPERAND (t, 1)); | |
6779 | ||
6780 | case COND_EXPR: | |
6781 | return integer_valued_real_p (TREE_OPERAND (t, 1)) | |
6782 | && integer_valued_real_p (TREE_OPERAND (t, 2)); | |
6783 | ||
6784 | case REAL_CST: | |
6785 | if (! TREE_CONSTANT_OVERFLOW (t)) | |
6786 | { | |
a0c938f0 | 6787 | REAL_VALUE_TYPE c, cint; |
277f8dd2 | 6788 | |
6789 | c = TREE_REAL_CST (t); | |
6790 | real_trunc (&cint, TYPE_MODE (TREE_TYPE (t)), &c); | |
6791 | return real_identical (&c, &cint); | |
6792 | } | |
69d2f735 | 6793 | break; |
277f8dd2 | 6794 | |
6795 | case NOP_EXPR: | |
6796 | { | |
6797 | tree type = TREE_TYPE (TREE_OPERAND (t, 0)); | |
6798 | if (TREE_CODE (type) == INTEGER_TYPE) | |
6799 | return true; | |
6800 | if (TREE_CODE (type) == REAL_TYPE) | |
6801 | return integer_valued_real_p (TREE_OPERAND (t, 0)); | |
6802 | break; | |
6803 | } | |
6804 | ||
6805 | case CALL_EXPR: | |
6806 | switch (builtin_mathfn_code (t)) | |
6807 | { | |
4f35b1fc | 6808 | CASE_FLT_FN (BUILT_IN_CEIL): |
6809 | CASE_FLT_FN (BUILT_IN_FLOOR): | |
6810 | CASE_FLT_FN (BUILT_IN_NEARBYINT): | |
6811 | CASE_FLT_FN (BUILT_IN_RINT): | |
6812 | CASE_FLT_FN (BUILT_IN_ROUND): | |
6813 | CASE_FLT_FN (BUILT_IN_TRUNC): | |
277f8dd2 | 6814 | return true; |
6815 | ||
6816 | default: | |
6817 | break; | |
6818 | } | |
6819 | break; | |
6820 | ||
6821 | default: | |
6822 | break; | |
6823 | } | |
6824 | return false; | |
6825 | } | |
6826 | ||
6827 | /* EXP is assumed to be builtin call where truncation can be propagated | |
6528f4f4 | 6828 | across (for instance floor((double)f) == (double)floorf (f). |
6829 | Do the transformation. */ | |
277f8dd2 | 6830 | |
6528f4f4 | 6831 | static tree |
76b9b24b | 6832 | fold_trunc_transparent_mathfn (tree fndecl, tree arglist) |
6528f4f4 | 6833 | { |
6528f4f4 | 6834 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); |
277f8dd2 | 6835 | tree arg; |
6836 | ||
6837 | if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
6838 | return 0; | |
6528f4f4 | 6839 | |
277f8dd2 | 6840 | arg = TREE_VALUE (arglist); |
6841 | /* Integer rounding functions are idempotent. */ | |
6842 | if (fcode == builtin_mathfn_code (arg)) | |
6843 | return arg; | |
6844 | ||
6845 | /* If argument is already integer valued, and we don't need to worry | |
6846 | about setting errno, there's no need to perform rounding. */ | |
6847 | if (! flag_errno_math && integer_valued_real_p (arg)) | |
6848 | return arg; | |
6849 | ||
6850 | if (optimize) | |
6528f4f4 | 6851 | { |
277f8dd2 | 6852 | tree arg0 = strip_float_extensions (arg); |
2426241c | 6853 | tree ftype = TREE_TYPE (TREE_TYPE (fndecl)); |
6528f4f4 | 6854 | tree newtype = TREE_TYPE (arg0); |
6855 | tree decl; | |
6856 | ||
6857 | if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype) | |
6858 | && (decl = mathfn_built_in (newtype, fcode))) | |
6859 | { | |
6860 | arglist = | |
d7aeca92 | 6861 | build_tree_list (NULL_TREE, fold_convert (newtype, arg0)); |
6862 | return fold_convert (ftype, | |
6863 | build_function_call_expr (decl, arglist)); | |
6528f4f4 | 6864 | } |
6865 | } | |
6866 | return 0; | |
6867 | } | |
6868 | ||
9ed65c7f | 6869 | /* EXP is assumed to be builtin call which can narrow the FP type of |
6870 | the argument, for instance lround((double)f) -> lroundf (f). */ | |
6871 | ||
6872 | static tree | |
7169af54 | 6873 | fold_fixed_mathfn (tree fndecl, tree arglist) |
9ed65c7f | 6874 | { |
9ed65c7f | 6875 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); |
6876 | tree arg; | |
6877 | ||
6878 | if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
6879 | return 0; | |
6880 | ||
6881 | arg = TREE_VALUE (arglist); | |
6882 | ||
6883 | /* If argument is already integer valued, and we don't need to worry | |
6884 | about setting errno, there's no need to perform rounding. */ | |
6885 | if (! flag_errno_math && integer_valued_real_p (arg)) | |
49d00087 | 6886 | return fold_build1 (FIX_TRUNC_EXPR, TREE_TYPE (TREE_TYPE (fndecl)), arg); |
9ed65c7f | 6887 | |
6888 | if (optimize) | |
6889 | { | |
6890 | tree ftype = TREE_TYPE (arg); | |
6891 | tree arg0 = strip_float_extensions (arg); | |
6892 | tree newtype = TREE_TYPE (arg0); | |
6893 | tree decl; | |
6894 | ||
6895 | if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype) | |
6896 | && (decl = mathfn_built_in (newtype, fcode))) | |
6897 | { | |
6898 | arglist = | |
6899 | build_tree_list (NULL_TREE, fold_convert (newtype, arg0)); | |
6900 | return build_function_call_expr (decl, arglist); | |
6901 | } | |
6902 | } | |
73a0da56 | 6903 | |
6904 | /* Canonicalize llround (x) to lround (x) on LP64 targets where | |
6905 | sizeof (long long) == sizeof (long). */ | |
6906 | if (TYPE_PRECISION (long_long_integer_type_node) | |
6907 | == TYPE_PRECISION (long_integer_type_node)) | |
6908 | { | |
6909 | tree newfn = NULL_TREE; | |
6910 | switch (fcode) | |
6911 | { | |
6912 | CASE_FLT_FN (BUILT_IN_LLCEIL): | |
6913 | newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL); | |
6914 | break; | |
6915 | ||
6916 | CASE_FLT_FN (BUILT_IN_LLFLOOR): | |
6917 | newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR); | |
6918 | break; | |
6919 | ||
6920 | CASE_FLT_FN (BUILT_IN_LLROUND): | |
6921 | newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND); | |
6922 | break; | |
6923 | ||
6924 | CASE_FLT_FN (BUILT_IN_LLRINT): | |
6925 | newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT); | |
6926 | break; | |
6927 | ||
6928 | default: | |
6929 | break; | |
6930 | } | |
6931 | ||
6932 | if (newfn) | |
6933 | { | |
6934 | tree newcall = build_function_call_expr (newfn, arglist); | |
6935 | return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), newcall); | |
6936 | } | |
6937 | } | |
6938 | ||
9ed65c7f | 6939 | return 0; |
6940 | } | |
6941 | ||
7d99bd73 | 6942 | /* Fold function call to builtin cabs, cabsf or cabsl. ARGLIST |
749891b2 | 6943 | is the argument list, TYPE is the return type and FNDECL is the |
6944 | original function DECL. Return NULL_TREE if no if no simplification | |
6945 | can be made. */ | |
c63f4ad3 | 6946 | |
6947 | static tree | |
749891b2 | 6948 | fold_builtin_cabs (tree arglist, tree type, tree fndecl) |
c63f4ad3 | 6949 | { |
6950 | tree arg; | |
6951 | ||
6952 | if (!arglist || TREE_CHAIN (arglist)) | |
6953 | return NULL_TREE; | |
6954 | ||
6955 | arg = TREE_VALUE (arglist); | |
6956 | if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE | |
6957 | || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE) | |
6958 | return NULL_TREE; | |
6959 | ||
6960 | /* Evaluate cabs of a constant at compile-time. */ | |
6961 | if (flag_unsafe_math_optimizations | |
6962 | && TREE_CODE (arg) == COMPLEX_CST | |
6963 | && TREE_CODE (TREE_REALPART (arg)) == REAL_CST | |
6964 | && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST | |
6965 | && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg)) | |
6966 | && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg))) | |
6967 | { | |
6968 | REAL_VALUE_TYPE r, i; | |
6969 | ||
6970 | r = TREE_REAL_CST (TREE_REALPART (arg)); | |
6971 | i = TREE_REAL_CST (TREE_IMAGPART (arg)); | |
6972 | ||
6973 | real_arithmetic (&r, MULT_EXPR, &r, &r); | |
6974 | real_arithmetic (&i, MULT_EXPR, &i, &i); | |
6975 | real_arithmetic (&r, PLUS_EXPR, &r, &i); | |
6976 | if (real_sqrt (&r, TYPE_MODE (type), &r) | |
6977 | || ! flag_trapping_math) | |
6978 | return build_real (type, r); | |
6979 | } | |
6980 | ||
6981 | /* If either part is zero, cabs is fabs of the other. */ | |
6982 | if (TREE_CODE (arg) == COMPLEX_EXPR | |
6983 | && real_zerop (TREE_OPERAND (arg, 0))) | |
49d00087 | 6984 | return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1)); |
c63f4ad3 | 6985 | if (TREE_CODE (arg) == COMPLEX_EXPR |
6986 | && real_zerop (TREE_OPERAND (arg, 1))) | |
49d00087 | 6987 | return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0)); |
c63f4ad3 | 6988 | |
749891b2 | 6989 | /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */ |
6990 | if (TREE_CODE (arg) == NEGATE_EXPR | |
6991 | || TREE_CODE (arg) == CONJ_EXPR) | |
6992 | { | |
6993 | tree arglist = build_tree_list (NULL_TREE, TREE_OPERAND (arg, 0)); | |
6994 | return build_function_call_expr (fndecl, arglist); | |
6995 | } | |
6996 | ||
7d3f6cc7 | 6997 | /* Don't do this when optimizing for size. */ |
6998 | if (flag_unsafe_math_optimizations | |
6999 | && optimize && !optimize_size) | |
c63f4ad3 | 7000 | { |
0da0dbfa | 7001 | tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT); |
c63f4ad3 | 7002 | |
7003 | if (sqrtfn != NULL_TREE) | |
7004 | { | |
7005 | tree rpart, ipart, result, arglist; | |
7006 | ||
4ee9c684 | 7007 | arg = builtin_save_expr (arg); |
29a6518e | 7008 | |
49d00087 | 7009 | rpart = fold_build1 (REALPART_EXPR, type, arg); |
7010 | ipart = fold_build1 (IMAGPART_EXPR, type, arg); | |
c63f4ad3 | 7011 | |
4ee9c684 | 7012 | rpart = builtin_save_expr (rpart); |
7013 | ipart = builtin_save_expr (ipart); | |
c63f4ad3 | 7014 | |
49d00087 | 7015 | result = fold_build2 (PLUS_EXPR, type, |
7016 | fold_build2 (MULT_EXPR, type, | |
7017 | rpart, rpart), | |
7018 | fold_build2 (MULT_EXPR, type, | |
7019 | ipart, ipart)); | |
c63f4ad3 | 7020 | |
7021 | arglist = build_tree_list (NULL_TREE, result); | |
7022 | return build_function_call_expr (sqrtfn, arglist); | |
7023 | } | |
7024 | } | |
7025 | ||
7026 | return NULL_TREE; | |
7027 | } | |
7028 | ||
e6e27594 | 7029 | /* Fold a builtin function call to sqrt, sqrtf, or sqrtl. Return |
7030 | NULL_TREE if no simplification can be made. */ | |
7031 | ||
7032 | static tree | |
7033 | fold_builtin_sqrt (tree arglist, tree type) | |
7034 | { | |
7035 | ||
7036 | enum built_in_function fcode; | |
7037 | tree arg = TREE_VALUE (arglist); | |
7038 | ||
7039 | if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
7040 | return NULL_TREE; | |
7041 | ||
7042 | /* Optimize sqrt of constant value. */ | |
7043 | if (TREE_CODE (arg) == REAL_CST | |
7044 | && ! TREE_CONSTANT_OVERFLOW (arg)) | |
7045 | { | |
7046 | REAL_VALUE_TYPE r, x; | |
7047 | ||
7048 | x = TREE_REAL_CST (arg); | |
7049 | if (real_sqrt (&r, TYPE_MODE (type), &x) | |
7050 | || (!flag_trapping_math && !flag_errno_math)) | |
7051 | return build_real (type, r); | |
7052 | } | |
7053 | ||
7054 | /* Optimize sqrt(expN(x)) = expN(x*0.5). */ | |
7055 | fcode = builtin_mathfn_code (arg); | |
7056 | if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode)) | |
7057 | { | |
7058 | tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0); | |
49d00087 | 7059 | arg = fold_build2 (MULT_EXPR, type, |
7060 | TREE_VALUE (TREE_OPERAND (arg, 1)), | |
7061 | build_real (type, dconsthalf)); | |
e6e27594 | 7062 | arglist = build_tree_list (NULL_TREE, arg); |
7063 | return build_function_call_expr (expfn, arglist); | |
7064 | } | |
7065 | ||
7066 | /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */ | |
7067 | if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode)) | |
7068 | { | |
7069 | tree powfn = mathfn_built_in (type, BUILT_IN_POW); | |
7070 | ||
7071 | if (powfn) | |
7072 | { | |
7073 | tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1)); | |
7074 | tree tree_root; | |
7075 | /* The inner root was either sqrt or cbrt. */ | |
7076 | REAL_VALUE_TYPE dconstroot = | |
7077 | BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird; | |
7078 | ||
7079 | /* Adjust for the outer root. */ | |
7080 | SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1); | |
7081 | dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot); | |
7082 | tree_root = build_real (type, dconstroot); | |
7083 | arglist = tree_cons (NULL_TREE, arg0, | |
7084 | build_tree_list (NULL_TREE, tree_root)); | |
7085 | return build_function_call_expr (powfn, arglist); | |
7086 | } | |
7087 | } | |
7088 | ||
bc33117f | 7089 | /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */ |
e6e27594 | 7090 | if (flag_unsafe_math_optimizations |
7091 | && (fcode == BUILT_IN_POW | |
7092 | || fcode == BUILT_IN_POWF | |
7093 | || fcode == BUILT_IN_POWL)) | |
7094 | { | |
7095 | tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0); | |
7096 | tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1)); | |
7097 | tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1))); | |
bc33117f | 7098 | tree narg1; |
7099 | if (!tree_expr_nonnegative_p (arg0)) | |
7100 | arg0 = build1 (ABS_EXPR, type, arg0); | |
49d00087 | 7101 | narg1 = fold_build2 (MULT_EXPR, type, arg1, |
7102 | build_real (type, dconsthalf)); | |
e6e27594 | 7103 | arglist = tree_cons (NULL_TREE, arg0, |
7104 | build_tree_list (NULL_TREE, narg1)); | |
7105 | return build_function_call_expr (powfn, arglist); | |
7106 | } | |
7107 | ||
7108 | return NULL_TREE; | |
7109 | } | |
7110 | ||
7111 | /* Fold a builtin function call to cbrt, cbrtf, or cbrtl. Return | |
7112 | NULL_TREE if no simplification can be made. */ | |
7113 | static tree | |
7114 | fold_builtin_cbrt (tree arglist, tree type) | |
7115 | { | |
7116 | tree arg = TREE_VALUE (arglist); | |
7117 | const enum built_in_function fcode = builtin_mathfn_code (arg); | |
7118 | ||
7119 | if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
7120 | return NULL_TREE; | |
7121 | ||
7122 | /* Optimize cbrt of constant value. */ | |
7123 | if (real_zerop (arg) || real_onep (arg) || real_minus_onep (arg)) | |
7124 | return arg; | |
7125 | ||
cdfeb715 | 7126 | if (flag_unsafe_math_optimizations) |
e6e27594 | 7127 | { |
cdfeb715 | 7128 | /* Optimize cbrt(expN(x)) -> expN(x/3). */ |
7129 | if (BUILTIN_EXPONENT_P (fcode)) | |
a0c938f0 | 7130 | { |
cdfeb715 | 7131 | tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0); |
7132 | const REAL_VALUE_TYPE third_trunc = | |
7133 | real_value_truncate (TYPE_MODE (type), dconstthird); | |
49d00087 | 7134 | arg = fold_build2 (MULT_EXPR, type, |
7135 | TREE_VALUE (TREE_OPERAND (arg, 1)), | |
7136 | build_real (type, third_trunc)); | |
cdfeb715 | 7137 | arglist = build_tree_list (NULL_TREE, arg); |
7138 | return build_function_call_expr (expfn, arglist); | |
7139 | } | |
e6e27594 | 7140 | |
cdfeb715 | 7141 | /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */ |
7142 | if (BUILTIN_SQRT_P (fcode)) | |
a0c938f0 | 7143 | { |
cdfeb715 | 7144 | tree powfn = mathfn_built_in (type, BUILT_IN_POW); |
e6e27594 | 7145 | |
cdfeb715 | 7146 | if (powfn) |
7147 | { | |
7148 | tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1)); | |
7149 | tree tree_root; | |
7150 | REAL_VALUE_TYPE dconstroot = dconstthird; | |
7151 | ||
7152 | SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1); | |
7153 | dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot); | |
7154 | tree_root = build_real (type, dconstroot); | |
7155 | arglist = tree_cons (NULL_TREE, arg0, | |
7156 | build_tree_list (NULL_TREE, tree_root)); | |
7157 | return build_function_call_expr (powfn, arglist); | |
7158 | } | |
e6e27594 | 7159 | } |
7160 | ||
cdfeb715 | 7161 | /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */ |
7162 | if (BUILTIN_CBRT_P (fcode)) | |
a0c938f0 | 7163 | { |
cdfeb715 | 7164 | tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1)); |
7165 | if (tree_expr_nonnegative_p (arg0)) | |
7166 | { | |
7167 | tree powfn = mathfn_built_in (type, BUILT_IN_POW); | |
7168 | ||
7169 | if (powfn) | |
a0c938f0 | 7170 | { |
cdfeb715 | 7171 | tree tree_root; |
7172 | REAL_VALUE_TYPE dconstroot; | |
a0c938f0 | 7173 | |
cdfeb715 | 7174 | real_arithmetic (&dconstroot, MULT_EXPR, &dconstthird, &dconstthird); |
7175 | dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot); | |
7176 | tree_root = build_real (type, dconstroot); | |
7177 | arglist = tree_cons (NULL_TREE, arg0, | |
7178 | build_tree_list (NULL_TREE, tree_root)); | |
7179 | return build_function_call_expr (powfn, arglist); | |
7180 | } | |
7181 | } | |
7182 | } | |
a0c938f0 | 7183 | |
cdfeb715 | 7184 | /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */ |
7185 | if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF | |
7186 | || fcode == BUILT_IN_POWL) | |
a0c938f0 | 7187 | { |
cdfeb715 | 7188 | tree arg00 = TREE_VALUE (TREE_OPERAND (arg, 1)); |
7189 | tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1))); | |
7190 | if (tree_expr_nonnegative_p (arg00)) | |
7191 | { | |
7192 | tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0); | |
7193 | const REAL_VALUE_TYPE dconstroot | |
7194 | = real_value_truncate (TYPE_MODE (type), dconstthird); | |
49d00087 | 7195 | tree narg01 = fold_build2 (MULT_EXPR, type, arg01, |
7196 | build_real (type, dconstroot)); | |
cdfeb715 | 7197 | arglist = tree_cons (NULL_TREE, arg00, |
7198 | build_tree_list (NULL_TREE, narg01)); | |
7199 | return build_function_call_expr (powfn, arglist); | |
7200 | } | |
7201 | } | |
e6e27594 | 7202 | } |
7203 | return NULL_TREE; | |
7204 | } | |
7205 | ||
7206 | /* Fold function call to builtin sin, sinf, or sinl. Return | |
7207 | NULL_TREE if no simplification can be made. */ | |
7208 | static tree | |
7209 | fold_builtin_sin (tree arglist) | |
7210 | { | |
7211 | tree arg = TREE_VALUE (arglist); | |
7212 | ||
7213 | if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
7214 | return NULL_TREE; | |
7215 | ||
7216 | /* Optimize sin (0.0) = 0.0. */ | |
7217 | if (real_zerop (arg)) | |
7218 | return arg; | |
7219 | ||
7220 | return NULL_TREE; | |
7221 | } | |
7222 | ||
7223 | /* Fold function call to builtin cos, cosf, or cosl. Return | |
7224 | NULL_TREE if no simplification can be made. */ | |
7225 | static tree | |
7226 | fold_builtin_cos (tree arglist, tree type, tree fndecl) | |
7227 | { | |
7228 | tree arg = TREE_VALUE (arglist); | |
7229 | ||
7230 | if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
7231 | return NULL_TREE; | |
7232 | ||
7233 | /* Optimize cos (0.0) = 1.0. */ | |
7234 | if (real_zerop (arg)) | |
7235 | return build_real (type, dconst1); | |
7236 | ||
7237 | /* Optimize cos(-x) into cos (x). */ | |
7238 | if (TREE_CODE (arg) == NEGATE_EXPR) | |
7239 | { | |
7240 | tree args = build_tree_list (NULL_TREE, | |
7241 | TREE_OPERAND (arg, 0)); | |
7242 | return build_function_call_expr (fndecl, args); | |
7243 | } | |
7244 | ||
7245 | return NULL_TREE; | |
7246 | } | |
7247 | ||
7248 | /* Fold function call to builtin tan, tanf, or tanl. Return | |
7249 | NULL_TREE if no simplification can be made. */ | |
7250 | static tree | |
7251 | fold_builtin_tan (tree arglist) | |
7252 | { | |
7253 | enum built_in_function fcode; | |
7254 | tree arg = TREE_VALUE (arglist); | |
7255 | ||
7256 | if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
7257 | return NULL_TREE; | |
7258 | ||
7259 | /* Optimize tan(0.0) = 0.0. */ | |
7260 | if (real_zerop (arg)) | |
7261 | return arg; | |
7262 | ||
7263 | /* Optimize tan(atan(x)) = x. */ | |
7264 | fcode = builtin_mathfn_code (arg); | |
7265 | if (flag_unsafe_math_optimizations | |
7266 | && (fcode == BUILT_IN_ATAN | |
7267 | || fcode == BUILT_IN_ATANF | |
7268 | || fcode == BUILT_IN_ATANL)) | |
7269 | return TREE_VALUE (TREE_OPERAND (arg, 1)); | |
7270 | ||
7271 | return NULL_TREE; | |
7272 | } | |
7273 | ||
7274 | /* Fold function call to builtin atan, atanf, or atanl. Return | |
7275 | NULL_TREE if no simplification can be made. */ | |
7276 | ||
7277 | static tree | |
7278 | fold_builtin_atan (tree arglist, tree type) | |
7279 | { | |
7280 | ||
7281 | tree arg = TREE_VALUE (arglist); | |
7282 | ||
7283 | if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
7284 | return NULL_TREE; | |
7285 | ||
7286 | /* Optimize atan(0.0) = 0.0. */ | |
7287 | if (real_zerop (arg)) | |
7288 | return arg; | |
7289 | ||
7290 | /* Optimize atan(1.0) = pi/4. */ | |
7291 | if (real_onep (arg)) | |
7292 | { | |
7293 | REAL_VALUE_TYPE cst; | |
7294 | ||
7295 | real_convert (&cst, TYPE_MODE (type), &dconstpi); | |
7296 | SET_REAL_EXP (&cst, REAL_EXP (&cst) - 2); | |
7297 | return build_real (type, cst); | |
7298 | } | |
7299 | ||
7300 | return NULL_TREE; | |
7301 | } | |
7302 | ||
277f8dd2 | 7303 | /* Fold function call to builtin trunc, truncf or truncl. Return |
7304 | NULL_TREE if no simplification can be made. */ | |
7305 | ||
7306 | static tree | |
7169af54 | 7307 | fold_builtin_trunc (tree fndecl, tree arglist) |
277f8dd2 | 7308 | { |
277f8dd2 | 7309 | tree arg; |
7310 | ||
7311 | if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
7312 | return 0; | |
7313 | ||
7314 | /* Optimize trunc of constant value. */ | |
7315 | arg = TREE_VALUE (arglist); | |
7316 | if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg)) | |
7317 | { | |
7318 | REAL_VALUE_TYPE r, x; | |
2426241c | 7319 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
277f8dd2 | 7320 | |
7321 | x = TREE_REAL_CST (arg); | |
7322 | real_trunc (&r, TYPE_MODE (type), &x); | |
7323 | return build_real (type, r); | |
7324 | } | |
7325 | ||
76b9b24b | 7326 | return fold_trunc_transparent_mathfn (fndecl, arglist); |
277f8dd2 | 7327 | } |
7328 | ||
7329 | /* Fold function call to builtin floor, floorf or floorl. Return | |
7330 | NULL_TREE if no simplification can be made. */ | |
7331 | ||
7332 | static tree | |
7169af54 | 7333 | fold_builtin_floor (tree fndecl, tree arglist) |
277f8dd2 | 7334 | { |
277f8dd2 | 7335 | tree arg; |
7336 | ||
7337 | if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
7338 | return 0; | |
7339 | ||
7340 | /* Optimize floor of constant value. */ | |
7341 | arg = TREE_VALUE (arglist); | |
7342 | if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg)) | |
7343 | { | |
7344 | REAL_VALUE_TYPE x; | |
7345 | ||
7346 | x = TREE_REAL_CST (arg); | |
7347 | if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math) | |
7348 | { | |
2426241c | 7349 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
277f8dd2 | 7350 | REAL_VALUE_TYPE r; |
7351 | ||
7352 | real_floor (&r, TYPE_MODE (type), &x); | |
7353 | return build_real (type, r); | |
7354 | } | |
7355 | } | |
7356 | ||
76b9b24b | 7357 | return fold_trunc_transparent_mathfn (fndecl, arglist); |
277f8dd2 | 7358 | } |
7359 | ||
7360 | /* Fold function call to builtin ceil, ceilf or ceill. Return | |
7361 | NULL_TREE if no simplification can be made. */ | |
7362 | ||
7363 | static tree | |
7169af54 | 7364 | fold_builtin_ceil (tree fndecl, tree arglist) |
277f8dd2 | 7365 | { |
277f8dd2 | 7366 | tree arg; |
7367 | ||
7368 | if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
7369 | return 0; | |
7370 | ||
7371 | /* Optimize ceil of constant value. */ | |
7372 | arg = TREE_VALUE (arglist); | |
7373 | if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg)) | |
7374 | { | |
7375 | REAL_VALUE_TYPE x; | |
7376 | ||
7377 | x = TREE_REAL_CST (arg); | |
7378 | if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math) | |
7379 | { | |
2426241c | 7380 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
277f8dd2 | 7381 | REAL_VALUE_TYPE r; |
7382 | ||
7383 | real_ceil (&r, TYPE_MODE (type), &x); | |
7384 | return build_real (type, r); | |
7385 | } | |
7386 | } | |
7387 | ||
76b9b24b | 7388 | return fold_trunc_transparent_mathfn (fndecl, arglist); |
277f8dd2 | 7389 | } |
7390 | ||
89ab3887 | 7391 | /* Fold function call to builtin round, roundf or roundl. Return |
7392 | NULL_TREE if no simplification can be made. */ | |
7393 | ||
7394 | static tree | |
7169af54 | 7395 | fold_builtin_round (tree fndecl, tree arglist) |
89ab3887 | 7396 | { |
89ab3887 | 7397 | tree arg; |
7398 | ||
7399 | if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
7400 | return 0; | |
7401 | ||
34f17811 | 7402 | /* Optimize round of constant value. */ |
89ab3887 | 7403 | arg = TREE_VALUE (arglist); |
7404 | if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg)) | |
7405 | { | |
7406 | REAL_VALUE_TYPE x; | |
7407 | ||
7408 | x = TREE_REAL_CST (arg); | |
7409 | if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math) | |
7410 | { | |
2426241c | 7411 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
89ab3887 | 7412 | REAL_VALUE_TYPE r; |
7413 | ||
7414 | real_round (&r, TYPE_MODE (type), &x); | |
7415 | return build_real (type, r); | |
7416 | } | |
7417 | } | |
7418 | ||
76b9b24b | 7419 | return fold_trunc_transparent_mathfn (fndecl, arglist); |
89ab3887 | 7420 | } |
7421 | ||
34f17811 | 7422 | /* Fold function call to builtin lround, lroundf or lroundl (or the |
ad52b9b7 | 7423 | corresponding long long versions) and other rounding functions. |
7424 | Return NULL_TREE if no simplification can be made. */ | |
34f17811 | 7425 | |
7426 | static tree | |
ad52b9b7 | 7427 | fold_builtin_int_roundingfn (tree fndecl, tree arglist) |
34f17811 | 7428 | { |
34f17811 | 7429 | tree arg; |
7430 | ||
7431 | if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
7432 | return 0; | |
7433 | ||
7434 | /* Optimize lround of constant value. */ | |
7435 | arg = TREE_VALUE (arglist); | |
7436 | if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg)) | |
7437 | { | |
7438 | const REAL_VALUE_TYPE x = TREE_REAL_CST (arg); | |
7439 | ||
7440 | if (! REAL_VALUE_ISNAN (x) && ! REAL_VALUE_ISINF (x)) | |
7441 | { | |
2426241c | 7442 | tree itype = TREE_TYPE (TREE_TYPE (fndecl)); |
7443 | tree ftype = TREE_TYPE (arg), result; | |
34f17811 | 7444 | HOST_WIDE_INT hi, lo; |
7445 | REAL_VALUE_TYPE r; | |
7446 | ||
ad52b9b7 | 7447 | switch (DECL_FUNCTION_CODE (fndecl)) |
7448 | { | |
4f35b1fc | 7449 | CASE_FLT_FN (BUILT_IN_LFLOOR): |
7450 | CASE_FLT_FN (BUILT_IN_LLFLOOR): | |
ad52b9b7 | 7451 | real_floor (&r, TYPE_MODE (ftype), &x); |
7452 | break; | |
7453 | ||
4f35b1fc | 7454 | CASE_FLT_FN (BUILT_IN_LCEIL): |
7455 | CASE_FLT_FN (BUILT_IN_LLCEIL): | |
ac148751 | 7456 | real_ceil (&r, TYPE_MODE (ftype), &x); |
7457 | break; | |
7458 | ||
4f35b1fc | 7459 | CASE_FLT_FN (BUILT_IN_LROUND): |
7460 | CASE_FLT_FN (BUILT_IN_LLROUND): | |
ad52b9b7 | 7461 | real_round (&r, TYPE_MODE (ftype), &x); |
7462 | break; | |
7463 | ||
7464 | default: | |
7465 | gcc_unreachable (); | |
7466 | } | |
7467 | ||
34f17811 | 7468 | REAL_VALUE_TO_INT (&lo, &hi, r); |
7016c612 | 7469 | result = build_int_cst_wide (NULL_TREE, lo, hi); |
34f17811 | 7470 | if (int_fits_type_p (result, itype)) |
7471 | return fold_convert (itype, result); | |
7472 | } | |
7473 | } | |
7474 | ||
7169af54 | 7475 | return fold_fixed_mathfn (fndecl, arglist); |
34f17811 | 7476 | } |
7477 | ||
70fb4c07 | 7478 | /* Fold function call to builtin ffs, clz, ctz, popcount and parity |
7479 | and their long and long long variants (i.e. ffsl and ffsll). | |
7480 | Return NULL_TREE if no simplification can be made. */ | |
7481 | ||
7482 | static tree | |
10b9666f | 7483 | fold_builtin_bitop (tree fndecl, tree arglist) |
70fb4c07 | 7484 | { |
70fb4c07 | 7485 | tree arg; |
7486 | ||
7487 | if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE)) | |
7488 | return NULL_TREE; | |
7489 | ||
7490 | /* Optimize for constant argument. */ | |
7491 | arg = TREE_VALUE (arglist); | |
7492 | if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg)) | |
7493 | { | |
7494 | HOST_WIDE_INT hi, width, result; | |
7495 | unsigned HOST_WIDE_INT lo; | |
7c446c95 | 7496 | tree type; |
70fb4c07 | 7497 | |
7498 | type = TREE_TYPE (arg); | |
7499 | width = TYPE_PRECISION (type); | |
7500 | lo = TREE_INT_CST_LOW (arg); | |
7501 | ||
7502 | /* Clear all the bits that are beyond the type's precision. */ | |
7503 | if (width > HOST_BITS_PER_WIDE_INT) | |
7504 | { | |
7505 | hi = TREE_INT_CST_HIGH (arg); | |
7506 | if (width < 2 * HOST_BITS_PER_WIDE_INT) | |
7507 | hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT)); | |
7508 | } | |
7509 | else | |
7510 | { | |
7511 | hi = 0; | |
7512 | if (width < HOST_BITS_PER_WIDE_INT) | |
7513 | lo &= ~((unsigned HOST_WIDE_INT) (-1) << width); | |
7514 | } | |
7515 | ||
7516 | switch (DECL_FUNCTION_CODE (fndecl)) | |
7517 | { | |
4f35b1fc | 7518 | CASE_INT_FN (BUILT_IN_FFS): |
70fb4c07 | 7519 | if (lo != 0) |
7520 | result = exact_log2 (lo & -lo) + 1; | |
7521 | else if (hi != 0) | |
7522 | result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1; | |
7523 | else | |
7524 | result = 0; | |
7525 | break; | |
7526 | ||
4f35b1fc | 7527 | CASE_INT_FN (BUILT_IN_CLZ): |
70fb4c07 | 7528 | if (hi != 0) |
7529 | result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT; | |
7530 | else if (lo != 0) | |
7531 | result = width - floor_log2 (lo) - 1; | |
7532 | else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result)) | |
7533 | result = width; | |
7534 | break; | |
7535 | ||
4f35b1fc | 7536 | CASE_INT_FN (BUILT_IN_CTZ): |
70fb4c07 | 7537 | if (lo != 0) |
7538 | result = exact_log2 (lo & -lo); | |
7539 | else if (hi != 0) | |
7540 | result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi); | |
7541 | else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result)) | |
7542 | result = width; | |
7543 | break; | |
7544 | ||
4f35b1fc | 7545 | CASE_INT_FN (BUILT_IN_POPCOUNT): |
70fb4c07 | 7546 | result = 0; |
7547 | while (lo) | |
7548 | result++, lo &= lo - 1; | |
7549 | while (hi) | |
7550 | result++, hi &= hi - 1; | |
7551 | break; | |
7552 | ||
4f35b1fc | 7553 | CASE_INT_FN (BUILT_IN_PARITY): |
70fb4c07 | 7554 | result = 0; |
7555 | while (lo) | |
7556 | result++, lo &= lo - 1; | |
7557 | while (hi) | |
7558 | result++, hi &= hi - 1; | |
7559 | result &= 1; | |
7560 | break; | |
7561 | ||
7562 | default: | |
64db345d | 7563 | gcc_unreachable (); |
70fb4c07 | 7564 | } |
7565 | ||
2426241c | 7566 | return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result); |
70fb4c07 | 7567 | } |
7568 | ||
7569 | return NULL_TREE; | |
7570 | } | |
7571 | ||
8918c507 | 7572 | /* Return true if EXPR is the real constant contained in VALUE. */ |
7573 | ||
7574 | static bool | |
7575 | real_dconstp (tree expr, const REAL_VALUE_TYPE *value) | |
7576 | { | |
7577 | STRIP_NOPS (expr); | |
7578 | ||
7579 | return ((TREE_CODE (expr) == REAL_CST | |
a0c938f0 | 7580 | && ! TREE_CONSTANT_OVERFLOW (expr) |
7581 | && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), *value)) | |
7582 | || (TREE_CODE (expr) == COMPLEX_CST | |
7583 | && real_dconstp (TREE_REALPART (expr), value) | |
7584 | && real_zerop (TREE_IMAGPART (expr)))); | |
8918c507 | 7585 | } |
7586 | ||
7587 | /* A subroutine of fold_builtin to fold the various logarithmic | |
ca273d4a | 7588 | functions. EXP is the CALL_EXPR of a call to a builtin logN |
7589 | function. VALUE is the base of the logN function. */ | |
8918c507 | 7590 | |
7591 | static tree | |
f27ba301 | 7592 | fold_builtin_logarithm (tree fndecl, tree arglist, |
7593 | const REAL_VALUE_TYPE *value) | |
8918c507 | 7594 | { |
8918c507 | 7595 | if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) |
7596 | { | |
8918c507 | 7597 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
7598 | tree arg = TREE_VALUE (arglist); | |
7599 | const enum built_in_function fcode = builtin_mathfn_code (arg); | |
0862b7e9 | 7600 | |
ca273d4a | 7601 | /* Optimize logN(1.0) = 0.0. */ |
8918c507 | 7602 | if (real_onep (arg)) |
7603 | return build_real (type, dconst0); | |
7604 | ||
1f5cb383 | 7605 | /* Optimize logN(N) = 1.0. If N can't be truncated to MODE |
a0c938f0 | 7606 | exactly, then only do this if flag_unsafe_math_optimizations. */ |
1f5cb383 | 7607 | if (exact_real_truncate (TYPE_MODE (type), value) |
7608 | || flag_unsafe_math_optimizations) | |
a0c938f0 | 7609 | { |
1f5cb383 | 7610 | const REAL_VALUE_TYPE value_truncate = |
7611 | real_value_truncate (TYPE_MODE (type), *value); | |
7612 | if (real_dconstp (arg, &value_truncate)) | |
7613 | return build_real (type, dconst1); | |
7614 | } | |
0862b7e9 | 7615 | |
8918c507 | 7616 | /* Special case, optimize logN(expN(x)) = x. */ |
7617 | if (flag_unsafe_math_optimizations | |
7618 | && ((value == &dconste | |
7619 | && (fcode == BUILT_IN_EXP | |
7620 | || fcode == BUILT_IN_EXPF | |
7621 | || fcode == BUILT_IN_EXPL)) | |
7622 | || (value == &dconst2 | |
7623 | && (fcode == BUILT_IN_EXP2 | |
7624 | || fcode == BUILT_IN_EXP2F | |
7625 | || fcode == BUILT_IN_EXP2L)) | |
852da3c3 | 7626 | || (value == &dconst10 && (BUILTIN_EXP10_P (fcode))))) |
d7aeca92 | 7627 | return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1))); |
8918c507 | 7628 | |
ca273d4a | 7629 | /* Optimize logN(func()) for various exponential functions. We |
a0c938f0 | 7630 | want to determine the value "x" and the power "exponent" in |
7631 | order to transform logN(x**exponent) into exponent*logN(x). */ | |
8918c507 | 7632 | if (flag_unsafe_math_optimizations) |
a0c938f0 | 7633 | { |
8918c507 | 7634 | tree exponent = 0, x = 0; |
0862b7e9 | 7635 | |
8918c507 | 7636 | switch (fcode) |
7637 | { | |
4f35b1fc | 7638 | CASE_FLT_FN (BUILT_IN_EXP): |
8918c507 | 7639 | /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */ |
8918c507 | 7640 | x = build_real (type, |
7641 | real_value_truncate (TYPE_MODE (type), dconste)); | |
7642 | exponent = TREE_VALUE (TREE_OPERAND (arg, 1)); | |
7643 | break; | |
4f35b1fc | 7644 | CASE_FLT_FN (BUILT_IN_EXP2): |
8918c507 | 7645 | /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */ |
7646 | x = build_real (type, dconst2); | |
7647 | exponent = TREE_VALUE (TREE_OPERAND (arg, 1)); | |
7648 | break; | |
4f35b1fc | 7649 | CASE_FLT_FN (BUILT_IN_EXP10): |
7650 | CASE_FLT_FN (BUILT_IN_POW10): | |
8918c507 | 7651 | /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */ |
7652 | x = build_real (type, dconst10); | |
7653 | exponent = TREE_VALUE (TREE_OPERAND (arg, 1)); | |
7654 | break; | |
4f35b1fc | 7655 | CASE_FLT_FN (BUILT_IN_SQRT): |
8918c507 | 7656 | /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */ |
7657 | x = TREE_VALUE (TREE_OPERAND (arg, 1)); | |
7658 | exponent = build_real (type, dconsthalf); | |
7659 | break; | |
4f35b1fc | 7660 | CASE_FLT_FN (BUILT_IN_CBRT): |
8918c507 | 7661 | /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */ |
7662 | x = TREE_VALUE (TREE_OPERAND (arg, 1)); | |
7663 | exponent = build_real (type, real_value_truncate (TYPE_MODE (type), | |
7664 | dconstthird)); | |
7665 | break; | |
4f35b1fc | 7666 | CASE_FLT_FN (BUILT_IN_POW): |
8918c507 | 7667 | /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */ |
7668 | x = TREE_VALUE (TREE_OPERAND (arg, 1)); | |
7669 | exponent = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1))); | |
7670 | break; | |
7671 | default: | |
7672 | break; | |
7673 | } | |
7674 | ||
7675 | /* Now perform the optimization. */ | |
7676 | if (x && exponent) | |
7677 | { | |
7678 | tree logfn; | |
7679 | arglist = build_tree_list (NULL_TREE, x); | |
7680 | logfn = build_function_call_expr (fndecl, arglist); | |
49d00087 | 7681 | return fold_build2 (MULT_EXPR, type, exponent, logfn); |
8918c507 | 7682 | } |
7683 | } | |
7684 | } | |
7685 | ||
7686 | return 0; | |
7687 | } | |
0862b7e9 | 7688 | |
e6e27594 | 7689 | /* Fold a builtin function call to pow, powf, or powl. Return |
7690 | NULL_TREE if no simplification can be made. */ | |
7691 | static tree | |
7692 | fold_builtin_pow (tree fndecl, tree arglist, tree type) | |
7693 | { | |
e6e27594 | 7694 | tree arg0 = TREE_VALUE (arglist); |
7695 | tree arg1 = TREE_VALUE (TREE_CHAIN (arglist)); | |
7696 | ||
7697 | if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE)) | |
7698 | return NULL_TREE; | |
7699 | ||
7700 | /* Optimize pow(1.0,y) = 1.0. */ | |
7701 | if (real_onep (arg0)) | |
7702 | return omit_one_operand (type, build_real (type, dconst1), arg1); | |
7703 | ||
7704 | if (TREE_CODE (arg1) == REAL_CST | |
7705 | && ! TREE_CONSTANT_OVERFLOW (arg1)) | |
7706 | { | |
198d9bbe | 7707 | REAL_VALUE_TYPE cint; |
e6e27594 | 7708 | REAL_VALUE_TYPE c; |
198d9bbe | 7709 | HOST_WIDE_INT n; |
7710 | ||
e6e27594 | 7711 | c = TREE_REAL_CST (arg1); |
7712 | ||
7713 | /* Optimize pow(x,0.0) = 1.0. */ | |
7714 | if (REAL_VALUES_EQUAL (c, dconst0)) | |
7715 | return omit_one_operand (type, build_real (type, dconst1), | |
7716 | arg0); | |
7717 | ||
7718 | /* Optimize pow(x,1.0) = x. */ | |
7719 | if (REAL_VALUES_EQUAL (c, dconst1)) | |
7720 | return arg0; | |
7721 | ||
7722 | /* Optimize pow(x,-1.0) = 1.0/x. */ | |
7723 | if (REAL_VALUES_EQUAL (c, dconstm1)) | |
49d00087 | 7724 | return fold_build2 (RDIV_EXPR, type, |
7725 | build_real (type, dconst1), arg0); | |
e6e27594 | 7726 | |
7727 | /* Optimize pow(x,0.5) = sqrt(x). */ | |
7728 | if (flag_unsafe_math_optimizations | |
7729 | && REAL_VALUES_EQUAL (c, dconsthalf)) | |
7730 | { | |
7731 | tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT); | |
7732 | ||
7733 | if (sqrtfn != NULL_TREE) | |
7734 | { | |
7735 | tree arglist = build_tree_list (NULL_TREE, arg0); | |
7736 | return build_function_call_expr (sqrtfn, arglist); | |
7737 | } | |
7738 | } | |
7739 | ||
feb5b3eb | 7740 | /* Optimize pow(x,1.0/3.0) = cbrt(x). */ |
7741 | if (flag_unsafe_math_optimizations) | |
7742 | { | |
7743 | const REAL_VALUE_TYPE dconstroot | |
7744 | = real_value_truncate (TYPE_MODE (type), dconstthird); | |
7745 | ||
7746 | if (REAL_VALUES_EQUAL (c, dconstroot)) | |
7747 | { | |
7748 | tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT); | |
7749 | if (cbrtfn != NULL_TREE) | |
7750 | { | |
7751 | tree arglist = build_tree_list (NULL_TREE, arg0); | |
7752 | return build_function_call_expr (cbrtfn, arglist); | |
7753 | } | |
7754 | } | |
7755 | } | |
7756 | ||
198d9bbe | 7757 | /* Check for an integer exponent. */ |
7758 | n = real_to_integer (&c); | |
7759 | real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0); | |
7760 | if (real_identical (&c, &cint)) | |
e6e27594 | 7761 | { |
198d9bbe | 7762 | /* Attempt to evaluate pow at compile-time. */ |
7763 | if (TREE_CODE (arg0) == REAL_CST | |
7764 | && ! TREE_CONSTANT_OVERFLOW (arg0)) | |
e6e27594 | 7765 | { |
7766 | REAL_VALUE_TYPE x; | |
7767 | bool inexact; | |
7768 | ||
7769 | x = TREE_REAL_CST (arg0); | |
7770 | inexact = real_powi (&x, TYPE_MODE (type), &x, n); | |
7771 | if (flag_unsafe_math_optimizations || !inexact) | |
7772 | return build_real (type, x); | |
7773 | } | |
198d9bbe | 7774 | |
7775 | /* Strip sign ops from even integer powers. */ | |
7776 | if ((n & 1) == 0 && flag_unsafe_math_optimizations) | |
7777 | { | |
7778 | tree narg0 = fold_strip_sign_ops (arg0); | |
7779 | if (narg0) | |
7780 | { | |
7781 | arglist = build_tree_list (NULL_TREE, arg1); | |
7782 | arglist = tree_cons (NULL_TREE, narg0, arglist); | |
7783 | return build_function_call_expr (fndecl, arglist); | |
7784 | } | |
7785 | } | |
e6e27594 | 7786 | } |
7787 | } | |
7788 | ||
cdfeb715 | 7789 | if (flag_unsafe_math_optimizations) |
e6e27594 | 7790 | { |
cdfeb715 | 7791 | const enum built_in_function fcode = builtin_mathfn_code (arg0); |
e6e27594 | 7792 | |
cdfeb715 | 7793 | /* Optimize pow(expN(x),y) = expN(x*y). */ |
7794 | if (BUILTIN_EXPONENT_P (fcode)) | |
a0c938f0 | 7795 | { |
cdfeb715 | 7796 | tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0); |
7797 | tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1)); | |
49d00087 | 7798 | arg = fold_build2 (MULT_EXPR, type, arg, arg1); |
cdfeb715 | 7799 | arglist = build_tree_list (NULL_TREE, arg); |
7800 | return build_function_call_expr (expfn, arglist); | |
7801 | } | |
e6e27594 | 7802 | |
cdfeb715 | 7803 | /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */ |
7804 | if (BUILTIN_SQRT_P (fcode)) | |
a0c938f0 | 7805 | { |
cdfeb715 | 7806 | tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1)); |
49d00087 | 7807 | tree narg1 = fold_build2 (MULT_EXPR, type, arg1, |
7808 | build_real (type, dconsthalf)); | |
e6e27594 | 7809 | |
cdfeb715 | 7810 | arglist = tree_cons (NULL_TREE, narg0, |
7811 | build_tree_list (NULL_TREE, narg1)); | |
7812 | return build_function_call_expr (fndecl, arglist); | |
7813 | } | |
7814 | ||
7815 | /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */ | |
7816 | if (BUILTIN_CBRT_P (fcode)) | |
a0c938f0 | 7817 | { |
cdfeb715 | 7818 | tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1)); |
7819 | if (tree_expr_nonnegative_p (arg)) | |
7820 | { | |
7821 | const REAL_VALUE_TYPE dconstroot | |
7822 | = real_value_truncate (TYPE_MODE (type), dconstthird); | |
49d00087 | 7823 | tree narg1 = fold_build2 (MULT_EXPR, type, arg1, |
7824 | build_real (type, dconstroot)); | |
cdfeb715 | 7825 | arglist = tree_cons (NULL_TREE, arg, |
7826 | build_tree_list (NULL_TREE, narg1)); | |
7827 | return build_function_call_expr (fndecl, arglist); | |
7828 | } | |
7829 | } | |
a0c938f0 | 7830 | |
cdfeb715 | 7831 | /* Optimize pow(pow(x,y),z) = pow(x,y*z). */ |
7832 | if (fcode == BUILT_IN_POW || fcode == BUILT_IN_POWF | |
7833 | || fcode == BUILT_IN_POWL) | |
a0c938f0 | 7834 | { |
cdfeb715 | 7835 | tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1)); |
7836 | tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1))); | |
49d00087 | 7837 | tree narg1 = fold_build2 (MULT_EXPR, type, arg01, arg1); |
cdfeb715 | 7838 | arglist = tree_cons (NULL_TREE, arg00, |
7839 | build_tree_list (NULL_TREE, narg1)); | |
7840 | return build_function_call_expr (fndecl, arglist); | |
7841 | } | |
e6e27594 | 7842 | } |
cdfeb715 | 7843 | |
e6e27594 | 7844 | return NULL_TREE; |
7845 | } | |
7846 | ||
b4d0c20c | 7847 | /* Fold a builtin function call to powi, powif, or powil. Return |
7848 | NULL_TREE if no simplification can be made. */ | |
7849 | static tree | |
7850 | fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED, tree arglist, tree type) | |
7851 | { | |
7852 | tree arg0 = TREE_VALUE (arglist); | |
7853 | tree arg1 = TREE_VALUE (TREE_CHAIN (arglist)); | |
7854 | ||
7855 | if (!validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
7856 | return NULL_TREE; | |
7857 | ||
7858 | /* Optimize pow(1.0,y) = 1.0. */ | |
7859 | if (real_onep (arg0)) | |
7860 | return omit_one_operand (type, build_real (type, dconst1), arg1); | |
7861 | ||
7862 | if (host_integerp (arg1, 0)) | |
7863 | { | |
7864 | HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1); | |
7865 | ||
7866 | /* Evaluate powi at compile-time. */ | |
7867 | if (TREE_CODE (arg0) == REAL_CST | |
7868 | && ! TREE_CONSTANT_OVERFLOW (arg0)) | |
7869 | { | |
7870 | REAL_VALUE_TYPE x; | |
7871 | x = TREE_REAL_CST (arg0); | |
7872 | real_powi (&x, TYPE_MODE (type), &x, c); | |
7873 | return build_real (type, x); | |
7874 | } | |
7875 | ||
7876 | /* Optimize pow(x,0) = 1.0. */ | |
7877 | if (c == 0) | |
7878 | return omit_one_operand (type, build_real (type, dconst1), | |
7879 | arg0); | |
7880 | ||
7881 | /* Optimize pow(x,1) = x. */ | |
7882 | if (c == 1) | |
7883 | return arg0; | |
7884 | ||
7885 | /* Optimize pow(x,-1) = 1.0/x. */ | |
7886 | if (c == -1) | |
49d00087 | 7887 | return fold_build2 (RDIV_EXPR, type, |
7888 | build_real (type, dconst1), arg0); | |
b4d0c20c | 7889 | } |
7890 | ||
7891 | return NULL_TREE; | |
7892 | } | |
7893 | ||
8918c507 | 7894 | /* A subroutine of fold_builtin to fold the various exponent |
7895 | functions. EXP is the CALL_EXPR of a call to a builtin function. | |
7896 | VALUE is the value which will be raised to a power. */ | |
7897 | ||
7898 | static tree | |
2d4553f0 | 7899 | fold_builtin_exponent (tree fndecl, tree arglist, |
7900 | const REAL_VALUE_TYPE *value) | |
8918c507 | 7901 | { |
8918c507 | 7902 | if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) |
7903 | { | |
8918c507 | 7904 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
7905 | tree arg = TREE_VALUE (arglist); | |
7906 | ||
7907 | /* Optimize exp*(0.0) = 1.0. */ | |
7908 | if (real_zerop (arg)) | |
7909 | return build_real (type, dconst1); | |
7910 | ||
7911 | /* Optimize expN(1.0) = N. */ | |
7912 | if (real_onep (arg)) | |
a0c938f0 | 7913 | { |
8918c507 | 7914 | REAL_VALUE_TYPE cst; |
7915 | ||
7916 | real_convert (&cst, TYPE_MODE (type), value); | |
7917 | return build_real (type, cst); | |
7918 | } | |
7919 | ||
7920 | /* Attempt to evaluate expN(integer) at compile-time. */ | |
7921 | if (flag_unsafe_math_optimizations | |
7922 | && TREE_CODE (arg) == REAL_CST | |
7923 | && ! TREE_CONSTANT_OVERFLOW (arg)) | |
a0c938f0 | 7924 | { |
8918c507 | 7925 | REAL_VALUE_TYPE cint; |
7926 | REAL_VALUE_TYPE c; | |
7927 | HOST_WIDE_INT n; | |
7928 | ||
7929 | c = TREE_REAL_CST (arg); | |
7930 | n = real_to_integer (&c); | |
7931 | real_from_integer (&cint, VOIDmode, n, | |
7932 | n < 0 ? -1 : 0, 0); | |
7933 | if (real_identical (&c, &cint)) | |
7934 | { | |
7935 | REAL_VALUE_TYPE x; | |
7936 | ||
7937 | real_powi (&x, TYPE_MODE (type), value, n); | |
7938 | return build_real (type, x); | |
7939 | } | |
7940 | } | |
7941 | ||
7942 | /* Optimize expN(logN(x)) = x. */ | |
7943 | if (flag_unsafe_math_optimizations) | |
a0c938f0 | 7944 | { |
8918c507 | 7945 | const enum built_in_function fcode = builtin_mathfn_code (arg); |
7946 | ||
7947 | if ((value == &dconste | |
7948 | && (fcode == BUILT_IN_LOG | |
7949 | || fcode == BUILT_IN_LOGF | |
7950 | || fcode == BUILT_IN_LOGL)) | |
7951 | || (value == &dconst2 | |
7952 | && (fcode == BUILT_IN_LOG2 | |
7953 | || fcode == BUILT_IN_LOG2F | |
7954 | || fcode == BUILT_IN_LOG2L)) | |
7955 | || (value == &dconst10 | |
7956 | && (fcode == BUILT_IN_LOG10 | |
7957 | || fcode == BUILT_IN_LOG10F | |
7958 | || fcode == BUILT_IN_LOG10L))) | |
d7aeca92 | 7959 | return fold_convert (type, TREE_VALUE (TREE_OPERAND (arg, 1))); |
8918c507 | 7960 | } |
7961 | } | |
7962 | ||
7963 | return 0; | |
7964 | } | |
7965 | ||
e973ffcc | 7966 | /* Return true if VAR is a VAR_DECL or a component thereof. */ |
7967 | ||
7968 | static bool | |
7969 | var_decl_component_p (tree var) | |
7970 | { | |
7971 | tree inner = var; | |
7972 | while (handled_component_p (inner)) | |
7973 | inner = TREE_OPERAND (inner, 0); | |
7974 | return SSA_VAR_P (inner); | |
7975 | } | |
7976 | ||
4f46f2b9 | 7977 | /* Fold function call to builtin memset. Return |
9c8a1629 | 7978 | NULL_TREE if no simplification can be made. */ |
7979 | ||
7980 | static tree | |
4f46f2b9 | 7981 | fold_builtin_memset (tree arglist, tree type, bool ignore) |
9c8a1629 | 7982 | { |
4f46f2b9 | 7983 | tree dest, c, len, var, ret; |
7984 | unsigned HOST_WIDE_INT length, cval; | |
9c8a1629 | 7985 | |
7986 | if (!validate_arglist (arglist, | |
4f46f2b9 | 7987 | POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
9c8a1629 | 7988 | return 0; |
7989 | ||
7990 | dest = TREE_VALUE (arglist); | |
4f46f2b9 | 7991 | c = TREE_VALUE (TREE_CHAIN (arglist)); |
9c8a1629 | 7992 | len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); |
7993 | ||
4f46f2b9 | 7994 | if (! host_integerp (len, 1)) |
7995 | return 0; | |
7996 | ||
9c8a1629 | 7997 | /* If the LEN parameter is zero, return DEST. */ |
7998 | if (integer_zerop (len)) | |
4f46f2b9 | 7999 | return omit_one_operand (type, dest, c); |
9c8a1629 | 8000 | |
4f46f2b9 | 8001 | if (! host_integerp (c, 1) || TREE_SIDE_EFFECTS (dest)) |
8002 | return 0; | |
9c8a1629 | 8003 | |
4f46f2b9 | 8004 | var = dest; |
8005 | STRIP_NOPS (var); | |
8006 | if (TREE_CODE (var) != ADDR_EXPR) | |
8007 | return 0; | |
8008 | ||
8009 | var = TREE_OPERAND (var, 0); | |
8010 | if (TREE_THIS_VOLATILE (var)) | |
8011 | return 0; | |
8012 | ||
8013 | if (!INTEGRAL_TYPE_P (TREE_TYPE (var)) | |
8014 | && !POINTER_TYPE_P (TREE_TYPE (var))) | |
8015 | return 0; | |
8016 | ||
e973ffcc | 8017 | if (! var_decl_component_p (var)) |
8018 | return 0; | |
8019 | ||
4f46f2b9 | 8020 | length = tree_low_cst (len, 1); |
8021 | if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (var))) != length | |
8022 | || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT | |
8023 | < (int) length) | |
8024 | return 0; | |
8025 | ||
8026 | if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) | |
8027 | return 0; | |
8028 | ||
8029 | if (integer_zerop (c)) | |
8030 | cval = 0; | |
8031 | else | |
8032 | { | |
8033 | if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64) | |
8034 | return 0; | |
8035 | ||
8036 | cval = tree_low_cst (c, 1); | |
8037 | cval &= 0xff; | |
8038 | cval |= cval << 8; | |
8039 | cval |= cval << 16; | |
8040 | cval |= (cval << 31) << 1; | |
8041 | } | |
8042 | ||
8043 | ret = build_int_cst_type (TREE_TYPE (var), cval); | |
8044 | ret = build2 (MODIFY_EXPR, TREE_TYPE (var), var, ret); | |
8045 | if (ignore) | |
8046 | return ret; | |
8047 | ||
8048 | return omit_one_operand (type, dest, ret); | |
9c8a1629 | 8049 | } |
8050 | ||
4f46f2b9 | 8051 | /* Fold function call to builtin memset. Return |
9c8a1629 | 8052 | NULL_TREE if no simplification can be made. */ |
8053 | ||
8054 | static tree | |
4f46f2b9 | 8055 | fold_builtin_bzero (tree arglist, bool ignore) |
9c8a1629 | 8056 | { |
4f46f2b9 | 8057 | tree dest, size, newarglist; |
9c8a1629 | 8058 | |
4f46f2b9 | 8059 | if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
8060 | return 0; | |
9c8a1629 | 8061 | |
4f46f2b9 | 8062 | if (!ignore) |
8063 | return 0; | |
9c8a1629 | 8064 | |
4f46f2b9 | 8065 | dest = TREE_VALUE (arglist); |
8066 | size = TREE_VALUE (TREE_CHAIN (arglist)); | |
a0c938f0 | 8067 | |
4f46f2b9 | 8068 | /* New argument list transforming bzero(ptr x, int y) to |
8069 | memset(ptr x, int 0, size_t y). This is done this way | |
8070 | so that if it isn't expanded inline, we fallback to | |
8071 | calling bzero instead of memset. */ | |
8072 | ||
8073 | newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size)); | |
8074 | newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist); | |
8075 | newarglist = tree_cons (NULL_TREE, dest, newarglist); | |
8076 | return fold_builtin_memset (newarglist, void_type_node, ignore); | |
9c8a1629 | 8077 | } |
8078 | ||
4f46f2b9 | 8079 | /* Fold function call to builtin mem{{,p}cpy,move}. Return |
8080 | NULL_TREE if no simplification can be made. | |
8081 | If ENDP is 0, return DEST (like memcpy). | |
8082 | If ENDP is 1, return DEST+LEN (like mempcpy). | |
8083 | If ENDP is 2, return DEST+LEN-1 (like stpcpy). | |
8084 | If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap | |
8085 | (memmove). */ | |
9c8a1629 | 8086 | |
8087 | static tree | |
4f46f2b9 | 8088 | fold_builtin_memory_op (tree arglist, tree type, bool ignore, int endp) |
9c8a1629 | 8089 | { |
4f46f2b9 | 8090 | tree dest, src, len, destvar, srcvar, expr; |
8091 | unsigned HOST_WIDE_INT length; | |
9c8a1629 | 8092 | |
4f46f2b9 | 8093 | if (! validate_arglist (arglist, |
8094 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
9c8a1629 | 8095 | return 0; |
8096 | ||
8097 | dest = TREE_VALUE (arglist); | |
8098 | src = TREE_VALUE (TREE_CHAIN (arglist)); | |
8099 | len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
8100 | ||
8101 | /* If the LEN parameter is zero, return DEST. */ | |
8102 | if (integer_zerop (len)) | |
7e72af53 | 8103 | return omit_one_operand (type, dest, src); |
9c8a1629 | 8104 | |
4f46f2b9 | 8105 | /* If SRC and DEST are the same (and not volatile), return |
8106 | DEST{,+LEN,+LEN-1}. */ | |
9c8a1629 | 8107 | if (operand_equal_p (src, dest, 0)) |
4f46f2b9 | 8108 | expr = len; |
8109 | else | |
8110 | { | |
3b1757a2 | 8111 | if (endp == 3) |
8112 | { | |
8113 | unsigned int src_align | |
8114 | = get_pointer_alignment (src, BIGGEST_ALIGNMENT); | |
8115 | unsigned int dest_align | |
8116 | = get_pointer_alignment (dest, BIGGEST_ALIGNMENT); | |
8117 | /* Both DEST and SRC must be pointer types. | |
8118 | ??? This is what old code did. Is the testing for pointer types | |
8119 | really mandatory? | |
8120 | ||
8121 | If either SRC is readonly or length is 1, we can use memcpy. */ | |
8122 | if (dest_align && src_align | |
8123 | && (readonly_data_expr (src) | |
8124 | || integer_onep (len))) | |
8125 | { | |
8126 | tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY]; | |
8127 | if (!fn) | |
8128 | return 0; | |
8129 | return build_function_call_expr (fn, arglist); | |
8130 | } | |
8131 | } | |
4f46f2b9 | 8132 | if (! host_integerp (len, 1)) |
8133 | return 0; | |
9c8a1629 | 8134 | |
4f46f2b9 | 8135 | if (TREE_SIDE_EFFECTS (dest) || TREE_SIDE_EFFECTS (src)) |
8136 | return 0; | |
8137 | ||
8138 | destvar = dest; | |
8139 | STRIP_NOPS (destvar); | |
8140 | if (TREE_CODE (destvar) != ADDR_EXPR) | |
8141 | return 0; | |
8142 | ||
8143 | destvar = TREE_OPERAND (destvar, 0); | |
8144 | if (TREE_THIS_VOLATILE (destvar)) | |
8145 | return 0; | |
8146 | ||
8147 | if (!INTEGRAL_TYPE_P (TREE_TYPE (destvar)) | |
8148 | && !POINTER_TYPE_P (TREE_TYPE (destvar)) | |
8149 | && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (destvar))) | |
8150 | return 0; | |
8151 | ||
e973ffcc | 8152 | if (! var_decl_component_p (destvar)) |
8153 | return 0; | |
8154 | ||
4f46f2b9 | 8155 | srcvar = src; |
8156 | STRIP_NOPS (srcvar); | |
8157 | if (TREE_CODE (srcvar) != ADDR_EXPR) | |
8158 | return 0; | |
8159 | ||
8160 | srcvar = TREE_OPERAND (srcvar, 0); | |
8161 | if (TREE_THIS_VOLATILE (srcvar)) | |
8162 | return 0; | |
8163 | ||
8164 | if (!INTEGRAL_TYPE_P (TREE_TYPE (srcvar)) | |
8165 | && !POINTER_TYPE_P (TREE_TYPE (srcvar)) | |
8166 | && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (srcvar))) | |
8167 | return 0; | |
8168 | ||
e973ffcc | 8169 | if (! var_decl_component_p (srcvar)) |
8170 | return 0; | |
8171 | ||
4f46f2b9 | 8172 | length = tree_low_cst (len, 1); |
8173 | if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (destvar))) != length | |
8174 | || get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT | |
8175 | < (int) length | |
8176 | || GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (srcvar))) != length | |
8177 | || get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT | |
8178 | < (int) length) | |
8179 | return 0; | |
8180 | ||
8181 | if ((INTEGRAL_TYPE_P (TREE_TYPE (srcvar)) | |
8182 | || POINTER_TYPE_P (TREE_TYPE (srcvar))) | |
8183 | && (INTEGRAL_TYPE_P (TREE_TYPE (destvar)) | |
8184 | || POINTER_TYPE_P (TREE_TYPE (destvar)))) | |
8185 | expr = fold_convert (TREE_TYPE (destvar), srcvar); | |
8186 | else | |
8187 | expr = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (destvar), srcvar); | |
8188 | expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, expr); | |
8189 | } | |
8190 | ||
8191 | if (ignore) | |
8192 | return expr; | |
8193 | ||
8194 | if (endp == 0 || endp == 3) | |
8195 | return omit_one_operand (type, dest, expr); | |
8196 | ||
8197 | if (expr == len) | |
8198 | expr = 0; | |
8199 | ||
8200 | if (endp == 2) | |
8201 | len = fold_build2 (MINUS_EXPR, TREE_TYPE (len), len, | |
8202 | ssize_int (1)); | |
8203 | ||
8204 | len = fold_convert (TREE_TYPE (dest), len); | |
8205 | dest = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len); | |
8206 | dest = fold_convert (type, dest); | |
8207 | if (expr) | |
8208 | dest = omit_one_operand (type, dest, expr); | |
8209 | return dest; | |
8210 | } | |
8211 | ||
8212 | /* Fold function call to builtin bcopy. Return NULL_TREE if no | |
8213 | simplification can be made. */ | |
8214 | ||
8215 | static tree | |
8216 | fold_builtin_bcopy (tree arglist, bool ignore) | |
8217 | { | |
8218 | tree src, dest, size, newarglist; | |
8219 | ||
8220 | if (!validate_arglist (arglist, | |
8221 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
8222 | return 0; | |
8223 | ||
8224 | if (! ignore) | |
8225 | return 0; | |
8226 | ||
8227 | src = TREE_VALUE (arglist); | |
8228 | dest = TREE_VALUE (TREE_CHAIN (arglist)); | |
8229 | size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
8230 | ||
8231 | /* New argument list transforming bcopy(ptr x, ptr y, int z) to | |
8232 | memmove(ptr y, ptr x, size_t z). This is done this way | |
8233 | so that if it isn't expanded inline, we fallback to | |
8234 | calling bcopy instead of memmove. */ | |
8235 | ||
8236 | newarglist = build_tree_list (NULL_TREE, fold_convert (sizetype, size)); | |
8237 | newarglist = tree_cons (NULL_TREE, src, newarglist); | |
8238 | newarglist = tree_cons (NULL_TREE, dest, newarglist); | |
8239 | ||
8240 | return fold_builtin_memory_op (newarglist, void_type_node, true, /*endp=*/3); | |
9c8a1629 | 8241 | } |
8242 | ||
f0613857 | 8243 | /* Fold function call to builtin strcpy. If LEN is not NULL, it represents |
8244 | the length of the string to be copied. Return NULL_TREE if no | |
8245 | simplification can be made. */ | |
9c8a1629 | 8246 | |
f0613857 | 8247 | tree |
ea726109 | 8248 | fold_builtin_strcpy (tree fndecl, tree arglist, tree len) |
9c8a1629 | 8249 | { |
f0613857 | 8250 | tree dest, src, fn; |
9c8a1629 | 8251 | |
8252 | if (!validate_arglist (arglist, | |
8253 | POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
8254 | return 0; | |
8255 | ||
8256 | dest = TREE_VALUE (arglist); | |
8257 | src = TREE_VALUE (TREE_CHAIN (arglist)); | |
8258 | ||
8259 | /* If SRC and DEST are the same (and not volatile), return DEST. */ | |
8260 | if (operand_equal_p (src, dest, 0)) | |
2426241c | 8261 | return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest); |
9c8a1629 | 8262 | |
f0613857 | 8263 | if (optimize_size) |
8264 | return 0; | |
8265 | ||
8266 | fn = implicit_built_in_decls[BUILT_IN_MEMCPY]; | |
8267 | if (!fn) | |
8268 | return 0; | |
8269 | ||
8270 | if (!len) | |
8271 | { | |
8272 | len = c_strlen (src, 1); | |
8273 | if (! len || TREE_SIDE_EFFECTS (len)) | |
8274 | return 0; | |
8275 | } | |
8276 | ||
8277 | len = size_binop (PLUS_EXPR, len, ssize_int (1)); | |
8278 | arglist = build_tree_list (NULL_TREE, len); | |
8279 | arglist = tree_cons (NULL_TREE, src, arglist); | |
8280 | arglist = tree_cons (NULL_TREE, dest, arglist); | |
2426241c | 8281 | return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), |
f0613857 | 8282 | build_function_call_expr (fn, arglist)); |
9c8a1629 | 8283 | } |
8284 | ||
f0613857 | 8285 | /* Fold function call to builtin strncpy. If SLEN is not NULL, it represents |
8286 | the length of the source string. Return NULL_TREE if no simplification | |
8287 | can be made. */ | |
9c8a1629 | 8288 | |
f0613857 | 8289 | tree |
ea726109 | 8290 | fold_builtin_strncpy (tree fndecl, tree arglist, tree slen) |
9c8a1629 | 8291 | { |
f0613857 | 8292 | tree dest, src, len, fn; |
9c8a1629 | 8293 | |
8294 | if (!validate_arglist (arglist, | |
8295 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
8296 | return 0; | |
8297 | ||
8298 | dest = TREE_VALUE (arglist); | |
8299 | src = TREE_VALUE (TREE_CHAIN (arglist)); | |
8300 | len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
8301 | ||
8302 | /* If the LEN parameter is zero, return DEST. */ | |
8303 | if (integer_zerop (len)) | |
2426241c | 8304 | return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src); |
9c8a1629 | 8305 | |
19226313 | 8306 | /* We can't compare slen with len as constants below if len is not a |
8307 | constant. */ | |
8308 | if (len == 0 || TREE_CODE (len) != INTEGER_CST) | |
8309 | return 0; | |
8310 | ||
f0613857 | 8311 | if (!slen) |
8312 | slen = c_strlen (src, 1); | |
8313 | ||
8314 | /* Now, we must be passed a constant src ptr parameter. */ | |
8315 | if (slen == 0 || TREE_CODE (slen) != INTEGER_CST) | |
8316 | return 0; | |
8317 | ||
8318 | slen = size_binop (PLUS_EXPR, slen, ssize_int (1)); | |
8319 | ||
8320 | /* We do not support simplification of this case, though we do | |
8321 | support it when expanding trees into RTL. */ | |
8322 | /* FIXME: generate a call to __builtin_memset. */ | |
8323 | if (tree_int_cst_lt (slen, len)) | |
8324 | return 0; | |
8325 | ||
8326 | /* OK transform into builtin memcpy. */ | |
8327 | fn = implicit_built_in_decls[BUILT_IN_MEMCPY]; | |
8328 | if (!fn) | |
8329 | return 0; | |
2426241c | 8330 | return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), |
f0613857 | 8331 | build_function_call_expr (fn, arglist)); |
9c8a1629 | 8332 | } |
8333 | ||
8334 | /* Fold function call to builtin memcmp. Return | |
8335 | NULL_TREE if no simplification can be made. */ | |
8336 | ||
8337 | static tree | |
c4fef134 | 8338 | fold_builtin_memcmp (tree arglist) |
9c8a1629 | 8339 | { |
9c8a1629 | 8340 | tree arg1, arg2, len; |
c4fef134 | 8341 | const char *p1, *p2; |
9c8a1629 | 8342 | |
8343 | if (!validate_arglist (arglist, | |
8344 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
8345 | return 0; | |
8346 | ||
8347 | arg1 = TREE_VALUE (arglist); | |
8348 | arg2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
8349 | len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
8350 | ||
8351 | /* If the LEN parameter is zero, return zero. */ | |
8352 | if (integer_zerop (len)) | |
c4fef134 | 8353 | return omit_two_operands (integer_type_node, integer_zero_node, |
8354 | arg1, arg2); | |
9c8a1629 | 8355 | |
8356 | /* If ARG1 and ARG2 are the same (and not volatile), return zero. */ | |
8357 | if (operand_equal_p (arg1, arg2, 0)) | |
c4fef134 | 8358 | return omit_one_operand (integer_type_node, integer_zero_node, len); |
8359 | ||
8360 | p1 = c_getstr (arg1); | |
8361 | p2 = c_getstr (arg2); | |
8362 | ||
8363 | /* If all arguments are constant, and the value of len is not greater | |
8364 | than the lengths of arg1 and arg2, evaluate at compile-time. */ | |
8365 | if (host_integerp (len, 1) && p1 && p2 | |
8366 | && compare_tree_int (len, strlen (p1) + 1) <= 0 | |
8367 | && compare_tree_int (len, strlen (p2) + 1) <= 0) | |
8368 | { | |
8369 | const int r = memcmp (p1, p2, tree_low_cst (len, 1)); | |
8370 | ||
8371 | if (r > 0) | |
8372 | return integer_one_node; | |
8373 | else if (r < 0) | |
8374 | return integer_minus_one_node; | |
8375 | else | |
8376 | return integer_zero_node; | |
8377 | } | |
8378 | ||
8379 | /* If len parameter is one, return an expression corresponding to | |
8380 | (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */ | |
8381 | if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1) | |
8382 | { | |
8383 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
4f1b71c0 | 8384 | tree cst_uchar_ptr_node |
8385 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
8386 | ||
c4fef134 | 8387 | tree ind1 = fold_convert (integer_type_node, |
8388 | build1 (INDIRECT_REF, cst_uchar_node, | |
8389 | fold_convert (cst_uchar_ptr_node, | |
8390 | arg1))); | |
8391 | tree ind2 = fold_convert (integer_type_node, | |
8392 | build1 (INDIRECT_REF, cst_uchar_node, | |
8393 | fold_convert (cst_uchar_ptr_node, | |
8394 | arg2))); | |
49d00087 | 8395 | return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2); |
c4fef134 | 8396 | } |
9c8a1629 | 8397 | |
8398 | return 0; | |
8399 | } | |
8400 | ||
8401 | /* Fold function call to builtin strcmp. Return | |
8402 | NULL_TREE if no simplification can be made. */ | |
8403 | ||
8404 | static tree | |
c4fef134 | 8405 | fold_builtin_strcmp (tree arglist) |
9c8a1629 | 8406 | { |
9c8a1629 | 8407 | tree arg1, arg2; |
8408 | const char *p1, *p2; | |
8409 | ||
c4fef134 | 8410 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
9c8a1629 | 8411 | return 0; |
8412 | ||
8413 | arg1 = TREE_VALUE (arglist); | |
8414 | arg2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
8415 | ||
8416 | /* If ARG1 and ARG2 are the same (and not volatile), return zero. */ | |
8417 | if (operand_equal_p (arg1, arg2, 0)) | |
c4fef134 | 8418 | return integer_zero_node; |
9c8a1629 | 8419 | |
8420 | p1 = c_getstr (arg1); | |
8421 | p2 = c_getstr (arg2); | |
8422 | ||
8423 | if (p1 && p2) | |
8424 | { | |
9c8a1629 | 8425 | const int i = strcmp (p1, p2); |
8426 | if (i < 0) | |
c4fef134 | 8427 | return integer_minus_one_node; |
9c8a1629 | 8428 | else if (i > 0) |
c4fef134 | 8429 | return integer_one_node; |
9c8a1629 | 8430 | else |
c4fef134 | 8431 | return integer_zero_node; |
8432 | } | |
8433 | ||
8434 | /* If the second arg is "", return *(const unsigned char*)arg1. */ | |
8435 | if (p2 && *p2 == '\0') | |
8436 | { | |
8437 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
4f1b71c0 | 8438 | tree cst_uchar_ptr_node |
8439 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
8440 | ||
c4fef134 | 8441 | return fold_convert (integer_type_node, |
8442 | build1 (INDIRECT_REF, cst_uchar_node, | |
8443 | fold_convert (cst_uchar_ptr_node, | |
8444 | arg1))); | |
8445 | } | |
8446 | ||
8447 | /* If the first arg is "", return -*(const unsigned char*)arg2. */ | |
8448 | if (p1 && *p1 == '\0') | |
8449 | { | |
8450 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
4f1b71c0 | 8451 | tree cst_uchar_ptr_node |
8452 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
8453 | ||
c4fef134 | 8454 | tree temp = fold_convert (integer_type_node, |
8455 | build1 (INDIRECT_REF, cst_uchar_node, | |
8456 | fold_convert (cst_uchar_ptr_node, | |
8457 | arg2))); | |
49d00087 | 8458 | return fold_build1 (NEGATE_EXPR, integer_type_node, temp); |
9c8a1629 | 8459 | } |
8460 | ||
8461 | return 0; | |
8462 | } | |
8463 | ||
8464 | /* Fold function call to builtin strncmp. Return | |
8465 | NULL_TREE if no simplification can be made. */ | |
8466 | ||
8467 | static tree | |
c4fef134 | 8468 | fold_builtin_strncmp (tree arglist) |
9c8a1629 | 8469 | { |
9c8a1629 | 8470 | tree arg1, arg2, len; |
8471 | const char *p1, *p2; | |
8472 | ||
8473 | if (!validate_arglist (arglist, | |
8474 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
8475 | return 0; | |
8476 | ||
8477 | arg1 = TREE_VALUE (arglist); | |
8478 | arg2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
8479 | len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
8480 | ||
8481 | /* If the LEN parameter is zero, return zero. */ | |
8482 | if (integer_zerop (len)) | |
c4fef134 | 8483 | return omit_two_operands (integer_type_node, integer_zero_node, |
8484 | arg1, arg2); | |
9c8a1629 | 8485 | |
8486 | /* If ARG1 and ARG2 are the same (and not volatile), return zero. */ | |
8487 | if (operand_equal_p (arg1, arg2, 0)) | |
c4fef134 | 8488 | return omit_one_operand (integer_type_node, integer_zero_node, len); |
9c8a1629 | 8489 | |
8490 | p1 = c_getstr (arg1); | |
8491 | p2 = c_getstr (arg2); | |
8492 | ||
8493 | if (host_integerp (len, 1) && p1 && p2) | |
8494 | { | |
9c8a1629 | 8495 | const int i = strncmp (p1, p2, tree_low_cst (len, 1)); |
c4fef134 | 8496 | if (i > 0) |
8497 | return integer_one_node; | |
8498 | else if (i < 0) | |
8499 | return integer_minus_one_node; | |
9c8a1629 | 8500 | else |
c4fef134 | 8501 | return integer_zero_node; |
8502 | } | |
8503 | ||
8504 | /* If the second arg is "", and the length is greater than zero, | |
8505 | return *(const unsigned char*)arg1. */ | |
8506 | if (p2 && *p2 == '\0' | |
8507 | && TREE_CODE (len) == INTEGER_CST | |
8508 | && tree_int_cst_sgn (len) == 1) | |
8509 | { | |
8510 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
4f1b71c0 | 8511 | tree cst_uchar_ptr_node |
8512 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
8513 | ||
c4fef134 | 8514 | return fold_convert (integer_type_node, |
8515 | build1 (INDIRECT_REF, cst_uchar_node, | |
8516 | fold_convert (cst_uchar_ptr_node, | |
8517 | arg1))); | |
8518 | } | |
8519 | ||
8520 | /* If the first arg is "", and the length is greater than zero, | |
8521 | return -*(const unsigned char*)arg2. */ | |
8522 | if (p1 && *p1 == '\0' | |
8523 | && TREE_CODE (len) == INTEGER_CST | |
8524 | && tree_int_cst_sgn (len) == 1) | |
8525 | { | |
8526 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
4f1b71c0 | 8527 | tree cst_uchar_ptr_node |
8528 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
8529 | ||
c4fef134 | 8530 | tree temp = fold_convert (integer_type_node, |
8531 | build1 (INDIRECT_REF, cst_uchar_node, | |
8532 | fold_convert (cst_uchar_ptr_node, | |
8533 | arg2))); | |
49d00087 | 8534 | return fold_build1 (NEGATE_EXPR, integer_type_node, temp); |
c4fef134 | 8535 | } |
8536 | ||
8537 | /* If len parameter is one, return an expression corresponding to | |
8538 | (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */ | |
8539 | if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1) | |
8540 | { | |
8541 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
4f1b71c0 | 8542 | tree cst_uchar_ptr_node |
8543 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
8544 | ||
c4fef134 | 8545 | tree ind1 = fold_convert (integer_type_node, |
8546 | build1 (INDIRECT_REF, cst_uchar_node, | |
8547 | fold_convert (cst_uchar_ptr_node, | |
8548 | arg1))); | |
8549 | tree ind2 = fold_convert (integer_type_node, | |
8550 | build1 (INDIRECT_REF, cst_uchar_node, | |
8551 | fold_convert (cst_uchar_ptr_node, | |
8552 | arg2))); | |
49d00087 | 8553 | return fold_build2 (MINUS_EXPR, integer_type_node, ind1, ind2); |
9c8a1629 | 8554 | } |
8555 | ||
8556 | return 0; | |
8557 | } | |
8558 | ||
27f261ef | 8559 | /* Fold function call to builtin signbit, signbitf or signbitl. Return |
8560 | NULL_TREE if no simplification can be made. */ | |
8561 | ||
8562 | static tree | |
64c18e8d | 8563 | fold_builtin_signbit (tree fndecl, tree arglist) |
27f261ef | 8564 | { |
2426241c | 8565 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
27f261ef | 8566 | tree arg, temp; |
8567 | ||
8568 | if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
8569 | return NULL_TREE; | |
8570 | ||
8571 | arg = TREE_VALUE (arglist); | |
8572 | ||
8573 | /* If ARG is a compile-time constant, determine the result. */ | |
8574 | if (TREE_CODE (arg) == REAL_CST | |
8575 | && !TREE_CONSTANT_OVERFLOW (arg)) | |
8576 | { | |
8577 | REAL_VALUE_TYPE c; | |
8578 | ||
8579 | c = TREE_REAL_CST (arg); | |
8580 | temp = REAL_VALUE_NEGATIVE (c) ? integer_one_node : integer_zero_node; | |
2426241c | 8581 | return fold_convert (type, temp); |
27f261ef | 8582 | } |
8583 | ||
8584 | /* If ARG is non-negative, the result is always zero. */ | |
8585 | if (tree_expr_nonnegative_p (arg)) | |
2426241c | 8586 | return omit_one_operand (type, integer_zero_node, arg); |
27f261ef | 8587 | |
8588 | /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */ | |
8589 | if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg)))) | |
49d00087 | 8590 | return fold_build2 (LT_EXPR, type, arg, |
8591 | build_real (TREE_TYPE (arg), dconst0)); | |
27f261ef | 8592 | |
8593 | return NULL_TREE; | |
8594 | } | |
8595 | ||
467214fd | 8596 | /* Fold function call to builtin copysign, copysignf or copysignl. |
8597 | Return NULL_TREE if no simplification can be made. */ | |
8598 | ||
8599 | static tree | |
198d9bbe | 8600 | fold_builtin_copysign (tree fndecl, tree arglist, tree type) |
467214fd | 8601 | { |
198d9bbe | 8602 | tree arg1, arg2, tem; |
467214fd | 8603 | |
8604 | if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE)) | |
8605 | return NULL_TREE; | |
8606 | ||
8607 | arg1 = TREE_VALUE (arglist); | |
8608 | arg2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
8609 | ||
8610 | /* copysign(X,X) is X. */ | |
8611 | if (operand_equal_p (arg1, arg2, 0)) | |
8612 | return fold_convert (type, arg1); | |
8613 | ||
8614 | /* If ARG1 and ARG2 are compile-time constants, determine the result. */ | |
8615 | if (TREE_CODE (arg1) == REAL_CST | |
8616 | && TREE_CODE (arg2) == REAL_CST | |
8617 | && !TREE_CONSTANT_OVERFLOW (arg1) | |
8618 | && !TREE_CONSTANT_OVERFLOW (arg2)) | |
8619 | { | |
8620 | REAL_VALUE_TYPE c1, c2; | |
8621 | ||
8622 | c1 = TREE_REAL_CST (arg1); | |
8623 | c2 = TREE_REAL_CST (arg2); | |
749680e2 | 8624 | /* c1.sign := c2.sign. */ |
467214fd | 8625 | real_copysign (&c1, &c2); |
8626 | return build_real (type, c1); | |
467214fd | 8627 | } |
8628 | ||
8629 | /* copysign(X, Y) is fabs(X) when Y is always non-negative. | |
8630 | Remember to evaluate Y for side-effects. */ | |
8631 | if (tree_expr_nonnegative_p (arg2)) | |
8632 | return omit_one_operand (type, | |
49d00087 | 8633 | fold_build1 (ABS_EXPR, type, arg1), |
467214fd | 8634 | arg2); |
8635 | ||
198d9bbe | 8636 | /* Strip sign changing operations for the first argument. */ |
8637 | tem = fold_strip_sign_ops (arg1); | |
8638 | if (tem) | |
8639 | { | |
8640 | arglist = tree_cons (NULL_TREE, tem, TREE_CHAIN (arglist)); | |
8641 | return build_function_call_expr (fndecl, arglist); | |
8642 | } | |
8643 | ||
467214fd | 8644 | return NULL_TREE; |
8645 | } | |
8646 | ||
d49367d4 | 8647 | /* Fold a call to builtin isascii. */ |
8648 | ||
8649 | static tree | |
8650 | fold_builtin_isascii (tree arglist) | |
8651 | { | |
8652 | if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE)) | |
8653 | return 0; | |
8654 | else | |
8655 | { | |
8656 | /* Transform isascii(c) -> ((c & ~0x7f) == 0). */ | |
8657 | tree arg = TREE_VALUE (arglist); | |
491e04ef | 8658 | |
7c446c95 | 8659 | arg = build2 (BIT_AND_EXPR, integer_type_node, arg, |
8660 | build_int_cst (NULL_TREE, | |
7016c612 | 8661 | ~ (unsigned HOST_WIDE_INT) 0x7f)); |
49d00087 | 8662 | arg = fold_build2 (EQ_EXPR, integer_type_node, |
8663 | arg, integer_zero_node); | |
491e04ef | 8664 | |
4ee9c684 | 8665 | if (in_gimple_form && !TREE_CONSTANT (arg)) |
a0c938f0 | 8666 | return NULL_TREE; |
4ee9c684 | 8667 | else |
a0c938f0 | 8668 | return arg; |
d49367d4 | 8669 | } |
8670 | } | |
8671 | ||
8672 | /* Fold a call to builtin toascii. */ | |
8673 | ||
8674 | static tree | |
8675 | fold_builtin_toascii (tree arglist) | |
8676 | { | |
8677 | if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE)) | |
8678 | return 0; | |
8679 | else | |
8680 | { | |
8681 | /* Transform toascii(c) -> (c & 0x7f). */ | |
8682 | tree arg = TREE_VALUE (arglist); | |
491e04ef | 8683 | |
49d00087 | 8684 | return fold_build2 (BIT_AND_EXPR, integer_type_node, arg, |
8685 | build_int_cst (NULL_TREE, 0x7f)); | |
d49367d4 | 8686 | } |
8687 | } | |
8688 | ||
df1cf42e | 8689 | /* Fold a call to builtin isdigit. */ |
8690 | ||
8691 | static tree | |
8692 | fold_builtin_isdigit (tree arglist) | |
8693 | { | |
8694 | if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE)) | |
8695 | return 0; | |
8696 | else | |
8697 | { | |
8698 | /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */ | |
624d37a6 | 8699 | /* According to the C standard, isdigit is unaffected by locale. |
8700 | However, it definitely is affected by the target character set. */ | |
8701 | tree arg; | |
8702 | unsigned HOST_WIDE_INT target_digit0 | |
8703 | = lang_hooks.to_target_charset ('0'); | |
8704 | ||
8705 | if (target_digit0 == 0) | |
8706 | return NULL_TREE; | |
8707 | ||
8708 | arg = fold_convert (unsigned_type_node, TREE_VALUE (arglist)); | |
05abc81b | 8709 | arg = build2 (MINUS_EXPR, unsigned_type_node, arg, |
624d37a6 | 8710 | build_int_cst (unsigned_type_node, target_digit0)); |
a1fbcbc1 | 8711 | arg = fold_build2 (LE_EXPR, integer_type_node, arg, |
8712 | build_int_cst (unsigned_type_node, 9)); | |
4ee9c684 | 8713 | if (in_gimple_form && !TREE_CONSTANT (arg)) |
a0c938f0 | 8714 | return NULL_TREE; |
4ee9c684 | 8715 | else |
a0c938f0 | 8716 | return arg; |
df1cf42e | 8717 | } |
8718 | } | |
27f261ef | 8719 | |
d1aade50 | 8720 | /* Fold a call to fabs, fabsf or fabsl. */ |
8721 | ||
8722 | static tree | |
8723 | fold_builtin_fabs (tree arglist, tree type) | |
8724 | { | |
8725 | tree arg; | |
8726 | ||
8727 | if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
8728 | return 0; | |
8729 | ||
8730 | arg = TREE_VALUE (arglist); | |
9829e110 | 8731 | arg = fold_convert (type, arg); |
d1aade50 | 8732 | if (TREE_CODE (arg) == REAL_CST) |
8733 | return fold_abs_const (arg, type); | |
49d00087 | 8734 | return fold_build1 (ABS_EXPR, type, arg); |
d1aade50 | 8735 | } |
8736 | ||
8737 | /* Fold a call to abs, labs, llabs or imaxabs. */ | |
8738 | ||
8739 | static tree | |
8740 | fold_builtin_abs (tree arglist, tree type) | |
8741 | { | |
8742 | tree arg; | |
8743 | ||
8744 | if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE)) | |
8745 | return 0; | |
8746 | ||
8747 | arg = TREE_VALUE (arglist); | |
9829e110 | 8748 | arg = fold_convert (type, arg); |
d1aade50 | 8749 | if (TREE_CODE (arg) == INTEGER_CST) |
8750 | return fold_abs_const (arg, type); | |
49d00087 | 8751 | return fold_build1 (ABS_EXPR, type, arg); |
d1aade50 | 8752 | } |
8753 | ||
726069ba | 8754 | /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite. |
8755 | EXP is the CALL_EXPR for the call. */ | |
8756 | ||
8757 | static tree | |
d008ac5f | 8758 | fold_builtin_classify (tree fndecl, tree arglist, int builtin_index) |
726069ba | 8759 | { |
726069ba | 8760 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
8761 | tree arg; | |
8762 | REAL_VALUE_TYPE r; | |
8763 | ||
8764 | if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
8765 | { | |
8766 | /* Check that we have exactly one argument. */ | |
8767 | if (arglist == 0) | |
8768 | { | |
eb586f2c | 8769 | error ("too few arguments to function %qs", |
726069ba | 8770 | IDENTIFIER_POINTER (DECL_NAME (fndecl))); |
8771 | return error_mark_node; | |
8772 | } | |
8773 | else if (TREE_CHAIN (arglist) != 0) | |
8774 | { | |
eb586f2c | 8775 | error ("too many arguments to function %qs", |
726069ba | 8776 | IDENTIFIER_POINTER (DECL_NAME (fndecl))); |
8777 | return error_mark_node; | |
8778 | } | |
8779 | else | |
8780 | { | |
eb586f2c | 8781 | error ("non-floating-point argument to function %qs", |
726069ba | 8782 | IDENTIFIER_POINTER (DECL_NAME (fndecl))); |
8783 | return error_mark_node; | |
8784 | } | |
8785 | } | |
8786 | ||
8787 | arg = TREE_VALUE (arglist); | |
8788 | switch (builtin_index) | |
8789 | { | |
8790 | case BUILT_IN_ISINF: | |
8791 | if (!MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg)))) | |
a0c938f0 | 8792 | return omit_one_operand (type, integer_zero_node, arg); |
726069ba | 8793 | |
8794 | if (TREE_CODE (arg) == REAL_CST) | |
8795 | { | |
8796 | r = TREE_REAL_CST (arg); | |
8797 | if (real_isinf (&r)) | |
8798 | return real_compare (GT_EXPR, &r, &dconst0) | |
8799 | ? integer_one_node : integer_minus_one_node; | |
8800 | else | |
8801 | return integer_zero_node; | |
8802 | } | |
8803 | ||
8804 | return NULL_TREE; | |
8805 | ||
f23e5f4e | 8806 | case BUILT_IN_FINITE: |
726069ba | 8807 | if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg))) |
a0c938f0 | 8808 | && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg)))) |
34709e02 | 8809 | return omit_one_operand (type, integer_one_node, arg); |
726069ba | 8810 | |
8811 | if (TREE_CODE (arg) == REAL_CST) | |
8812 | { | |
8813 | r = TREE_REAL_CST (arg); | |
8814 | return real_isinf (&r) || real_isnan (&r) | |
8815 | ? integer_zero_node : integer_one_node; | |
8816 | } | |
8817 | ||
8818 | return NULL_TREE; | |
8819 | ||
8820 | case BUILT_IN_ISNAN: | |
8821 | if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg)))) | |
a0c938f0 | 8822 | return omit_one_operand (type, integer_zero_node, arg); |
726069ba | 8823 | |
8824 | if (TREE_CODE (arg) == REAL_CST) | |
8825 | { | |
8826 | r = TREE_REAL_CST (arg); | |
8827 | return real_isnan (&r) ? integer_one_node : integer_zero_node; | |
8828 | } | |
8829 | ||
8830 | arg = builtin_save_expr (arg); | |
49d00087 | 8831 | return fold_build2 (UNORDERED_EXPR, type, arg, arg); |
726069ba | 8832 | |
8833 | default: | |
64db345d | 8834 | gcc_unreachable (); |
726069ba | 8835 | } |
8836 | } | |
8837 | ||
9bc9f15f | 8838 | /* Fold a call to an unordered comparison function such as |
d5019fe8 | 8839 | __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function |
8840 | being called and ARGLIST is the argument list for the call. | |
726069ba | 8841 | UNORDERED_CODE and ORDERED_CODE are comparison codes that give |
8842 | the opposite of the desired result. UNORDERED_CODE is used | |
8843 | for modes that can hold NaNs and ORDERED_CODE is used for | |
8844 | the rest. */ | |
9bc9f15f | 8845 | |
8846 | static tree | |
d5019fe8 | 8847 | fold_builtin_unordered_cmp (tree fndecl, tree arglist, |
9bc9f15f | 8848 | enum tree_code unordered_code, |
8849 | enum tree_code ordered_code) | |
8850 | { | |
859f903a | 8851 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
9bc9f15f | 8852 | enum tree_code code; |
8853 | tree arg0, arg1; | |
6978db0d | 8854 | tree type0, type1; |
8855 | enum tree_code code0, code1; | |
8856 | tree cmp_type = NULL_TREE; | |
9bc9f15f | 8857 | |
8858 | if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE)) | |
859f903a | 8859 | { |
859f903a | 8860 | /* Check that we have exactly two arguments. */ |
8861 | if (arglist == 0 || TREE_CHAIN (arglist) == 0) | |
8862 | { | |
eb586f2c | 8863 | error ("too few arguments to function %qs", |
859f903a | 8864 | IDENTIFIER_POINTER (DECL_NAME (fndecl))); |
8865 | return error_mark_node; | |
8866 | } | |
8867 | else if (TREE_CHAIN (TREE_CHAIN (arglist)) != 0) | |
8868 | { | |
eb586f2c | 8869 | error ("too many arguments to function %qs", |
859f903a | 8870 | IDENTIFIER_POINTER (DECL_NAME (fndecl))); |
8871 | return error_mark_node; | |
8872 | } | |
859f903a | 8873 | } |
6978db0d | 8874 | |
8875 | arg0 = TREE_VALUE (arglist); | |
8876 | arg1 = TREE_VALUE (TREE_CHAIN (arglist)); | |
a0c938f0 | 8877 | |
6978db0d | 8878 | type0 = TREE_TYPE (arg0); |
8879 | type1 = TREE_TYPE (arg1); | |
a0c938f0 | 8880 | |
6978db0d | 8881 | code0 = TREE_CODE (type0); |
8882 | code1 = TREE_CODE (type1); | |
a0c938f0 | 8883 | |
6978db0d | 8884 | if (code0 == REAL_TYPE && code1 == REAL_TYPE) |
8885 | /* Choose the wider of two real types. */ | |
8886 | cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1) | |
8887 | ? type0 : type1; | |
8888 | else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE) | |
8889 | cmp_type = type0; | |
8890 | else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE) | |
8891 | cmp_type = type1; | |
859f903a | 8892 | else |
8893 | { | |
6978db0d | 8894 | error ("non-floating-point argument to function %qs", |
8895 | IDENTIFIER_POINTER (DECL_NAME (fndecl))); | |
8896 | return error_mark_node; | |
859f903a | 8897 | } |
a0c938f0 | 8898 | |
6978db0d | 8899 | arg0 = fold_convert (cmp_type, arg0); |
8900 | arg1 = fold_convert (cmp_type, arg1); | |
859f903a | 8901 | |
8902 | if (unordered_code == UNORDERED_EXPR) | |
8903 | { | |
8904 | if (!MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0)))) | |
8905 | return omit_two_operands (type, integer_zero_node, arg0, arg1); | |
49d00087 | 8906 | return fold_build2 (UNORDERED_EXPR, type, arg0, arg1); |
859f903a | 8907 | } |
9bc9f15f | 8908 | |
8909 | code = MODE_HAS_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code | |
8910 | : ordered_code; | |
49d00087 | 8911 | return fold_build1 (TRUTH_NOT_EXPR, type, |
8912 | fold_build2 (code, type, arg0, arg1)); | |
9bc9f15f | 8913 | } |
8914 | ||
f0613857 | 8915 | /* Used by constant folding to simplify calls to builtin functions. EXP is |
8916 | the CALL_EXPR of a call to a builtin function. IGNORE is true if the | |
8917 | result of the function call is ignored. This function returns NULL_TREE | |
8918 | if no simplification was possible. */ | |
650e4c94 | 8919 | |
4ee9c684 | 8920 | static tree |
3d3d84b8 | 8921 | fold_builtin_1 (tree fndecl, tree arglist, bool ignore) |
650e4c94 | 8922 | { |
e9f80ff5 | 8923 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
189b3398 | 8924 | enum built_in_function fcode; |
650e4c94 | 8925 | |
8926 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) | |
0ab8af67 | 8927 | return targetm.fold_builtin (fndecl, arglist, ignore); |
650e4c94 | 8928 | |
189b3398 | 8929 | fcode = DECL_FUNCTION_CODE (fndecl); |
8930 | switch (fcode) | |
650e4c94 | 8931 | { |
7c2f0500 | 8932 | case BUILT_IN_FPUTS: |
8933 | return fold_builtin_fputs (arglist, ignore, false, NULL_TREE); | |
8934 | ||
8935 | case BUILT_IN_FPUTS_UNLOCKED: | |
8936 | return fold_builtin_fputs (arglist, ignore, true, NULL_TREE); | |
8937 | ||
8938 | case BUILT_IN_STRSTR: | |
daa1d5f5 | 8939 | return fold_builtin_strstr (arglist, type); |
7c2f0500 | 8940 | |
8941 | case BUILT_IN_STRCAT: | |
8942 | return fold_builtin_strcat (arglist); | |
8943 | ||
8944 | case BUILT_IN_STRNCAT: | |
8945 | return fold_builtin_strncat (arglist); | |
8946 | ||
8947 | case BUILT_IN_STRSPN: | |
8948 | return fold_builtin_strspn (arglist); | |
8949 | ||
8950 | case BUILT_IN_STRCSPN: | |
8951 | return fold_builtin_strcspn (arglist); | |
8952 | ||
8953 | case BUILT_IN_STRCHR: | |
8954 | case BUILT_IN_INDEX: | |
daa1d5f5 | 8955 | return fold_builtin_strchr (arglist, type); |
7c2f0500 | 8956 | |
8957 | case BUILT_IN_STRRCHR: | |
8958 | case BUILT_IN_RINDEX: | |
daa1d5f5 | 8959 | return fold_builtin_strrchr (arglist, type); |
7c2f0500 | 8960 | |
8961 | case BUILT_IN_STRCPY: | |
ea726109 | 8962 | return fold_builtin_strcpy (fndecl, arglist, NULL_TREE); |
7c2f0500 | 8963 | |
8964 | case BUILT_IN_STRNCPY: | |
ea726109 | 8965 | return fold_builtin_strncpy (fndecl, arglist, NULL_TREE); |
7c2f0500 | 8966 | |
8967 | case BUILT_IN_STRCMP: | |
8968 | return fold_builtin_strcmp (arglist); | |
8969 | ||
8970 | case BUILT_IN_STRNCMP: | |
8971 | return fold_builtin_strncmp (arglist); | |
8972 | ||
8973 | case BUILT_IN_STRPBRK: | |
daa1d5f5 | 8974 | return fold_builtin_strpbrk (arglist, type); |
7c2f0500 | 8975 | |
8976 | case BUILT_IN_BCMP: | |
8977 | case BUILT_IN_MEMCMP: | |
8978 | return fold_builtin_memcmp (arglist); | |
8979 | ||
8980 | case BUILT_IN_SPRINTF: | |
8981 | return fold_builtin_sprintf (arglist, ignore); | |
8982 | ||
650e4c94 | 8983 | case BUILT_IN_CONSTANT_P: |
7c2f0500 | 8984 | { |
8985 | tree val; | |
8986 | ||
8987 | val = fold_builtin_constant_p (arglist); | |
8988 | /* Gimplification will pull the CALL_EXPR for the builtin out of | |
8989 | an if condition. When not optimizing, we'll not CSE it back. | |
8990 | To avoid link error types of regressions, return false now. */ | |
8991 | if (!val && !optimize) | |
8992 | val = integer_zero_node; | |
8993 | ||
8994 | return val; | |
8995 | } | |
650e4c94 | 8996 | |
4ee9c684 | 8997 | case BUILT_IN_EXPECT: |
8998 | return fold_builtin_expect (arglist); | |
8999 | ||
539a3a92 | 9000 | case BUILT_IN_CLASSIFY_TYPE: |
9001 | return fold_builtin_classify_type (arglist); | |
9002 | ||
650e4c94 | 9003 | case BUILT_IN_STRLEN: |
e6e27594 | 9004 | return fold_builtin_strlen (arglist); |
650e4c94 | 9005 | |
4f35b1fc | 9006 | CASE_FLT_FN (BUILT_IN_FABS): |
d1aade50 | 9007 | return fold_builtin_fabs (arglist, type); |
9008 | ||
9009 | case BUILT_IN_ABS: | |
9010 | case BUILT_IN_LABS: | |
9011 | case BUILT_IN_LLABS: | |
9012 | case BUILT_IN_IMAXABS: | |
9013 | return fold_builtin_abs (arglist, type); | |
c63f4ad3 | 9014 | |
4f35b1fc | 9015 | CASE_FLT_FN (BUILT_IN_CONJ): |
36d3581d | 9016 | if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE)) |
49d00087 | 9017 | return fold_build1 (CONJ_EXPR, type, TREE_VALUE (arglist)); |
36d3581d | 9018 | break; |
9019 | ||
4f35b1fc | 9020 | CASE_FLT_FN (BUILT_IN_CREAL): |
36d3581d | 9021 | if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE)) |
a0c938f0 | 9022 | return non_lvalue (fold_build1 (REALPART_EXPR, type, |
49d00087 | 9023 | TREE_VALUE (arglist))); |
36d3581d | 9024 | break; |
9025 | ||
4f35b1fc | 9026 | CASE_FLT_FN (BUILT_IN_CIMAG): |
36d3581d | 9027 | if (validate_arglist (arglist, COMPLEX_TYPE, VOID_TYPE)) |
a0c938f0 | 9028 | return non_lvalue (fold_build1 (IMAGPART_EXPR, type, |
49d00087 | 9029 | TREE_VALUE (arglist))); |
36d3581d | 9030 | break; |
9031 | ||
4f35b1fc | 9032 | CASE_FLT_FN (BUILT_IN_CABS): |
749891b2 | 9033 | return fold_builtin_cabs (arglist, type, fndecl); |
c63f4ad3 | 9034 | |
4f35b1fc | 9035 | CASE_FLT_FN (BUILT_IN_SQRT): |
e6e27594 | 9036 | return fold_builtin_sqrt (arglist, type); |
805e22b2 | 9037 | |
4f35b1fc | 9038 | CASE_FLT_FN (BUILT_IN_CBRT): |
e6e27594 | 9039 | return fold_builtin_cbrt (arglist, type); |
3bc5c41b | 9040 | |
4f35b1fc | 9041 | CASE_FLT_FN (BUILT_IN_SIN): |
e6e27594 | 9042 | return fold_builtin_sin (arglist); |
77e89269 | 9043 | |
4f35b1fc | 9044 | CASE_FLT_FN (BUILT_IN_COS): |
e6e27594 | 9045 | return fold_builtin_cos (arglist, type, fndecl); |
77e89269 | 9046 | |
4f35b1fc | 9047 | CASE_FLT_FN (BUILT_IN_EXP): |
2d4553f0 | 9048 | return fold_builtin_exponent (fndecl, arglist, &dconste); |
467214fd | 9049 | |
4f35b1fc | 9050 | CASE_FLT_FN (BUILT_IN_EXP2): |
2d4553f0 | 9051 | return fold_builtin_exponent (fndecl, arglist, &dconst2); |
467214fd | 9052 | |
4f35b1fc | 9053 | CASE_FLT_FN (BUILT_IN_EXP10): |
9054 | CASE_FLT_FN (BUILT_IN_POW10): | |
2d4553f0 | 9055 | return fold_builtin_exponent (fndecl, arglist, &dconst10); |
467214fd | 9056 | |
4f35b1fc | 9057 | CASE_FLT_FN (BUILT_IN_LOG): |
f27ba301 | 9058 | return fold_builtin_logarithm (fndecl, arglist, &dconste); |
467214fd | 9059 | |
4f35b1fc | 9060 | CASE_FLT_FN (BUILT_IN_LOG2): |
f27ba301 | 9061 | return fold_builtin_logarithm (fndecl, arglist, &dconst2); |
467214fd | 9062 | |
4f35b1fc | 9063 | CASE_FLT_FN (BUILT_IN_LOG10): |
f27ba301 | 9064 | return fold_builtin_logarithm (fndecl, arglist, &dconst10); |
805e22b2 | 9065 | |
4f35b1fc | 9066 | CASE_FLT_FN (BUILT_IN_TAN): |
e6e27594 | 9067 | return fold_builtin_tan (arglist); |
98b40778 | 9068 | |
4f35b1fc | 9069 | CASE_FLT_FN (BUILT_IN_ATAN): |
e6e27594 | 9070 | return fold_builtin_atan (arglist, type); |
98b40778 | 9071 | |
4f35b1fc | 9072 | CASE_FLT_FN (BUILT_IN_POW): |
e6e27594 | 9073 | return fold_builtin_pow (fndecl, arglist, type); |
e9f80ff5 | 9074 | |
4f35b1fc | 9075 | CASE_FLT_FN (BUILT_IN_POWI): |
b4d0c20c | 9076 | return fold_builtin_powi (fndecl, arglist, type); |
9077 | ||
4f35b1fc | 9078 | CASE_FLT_FN (BUILT_IN_INF): |
c4503c0a | 9079 | case BUILT_IN_INFD32: |
9080 | case BUILT_IN_INFD64: | |
9081 | case BUILT_IN_INFD128: | |
e9f80ff5 | 9082 | return fold_builtin_inf (type, true); |
92c43e3c | 9083 | |
4f35b1fc | 9084 | CASE_FLT_FN (BUILT_IN_HUGE_VAL): |
e9f80ff5 | 9085 | return fold_builtin_inf (type, false); |
92c43e3c | 9086 | |
4f35b1fc | 9087 | CASE_FLT_FN (BUILT_IN_NAN): |
c4503c0a | 9088 | case BUILT_IN_NAND32: |
9089 | case BUILT_IN_NAND64: | |
9090 | case BUILT_IN_NAND128: | |
e9f80ff5 | 9091 | return fold_builtin_nan (arglist, type, true); |
b0db7939 | 9092 | |
4f35b1fc | 9093 | CASE_FLT_FN (BUILT_IN_NANS): |
e9f80ff5 | 9094 | return fold_builtin_nan (arglist, type, false); |
b0db7939 | 9095 | |
4f35b1fc | 9096 | CASE_FLT_FN (BUILT_IN_FLOOR): |
7169af54 | 9097 | return fold_builtin_floor (fndecl, arglist); |
277f8dd2 | 9098 | |
4f35b1fc | 9099 | CASE_FLT_FN (BUILT_IN_CEIL): |
7169af54 | 9100 | return fold_builtin_ceil (fndecl, arglist); |
277f8dd2 | 9101 | |
4f35b1fc | 9102 | CASE_FLT_FN (BUILT_IN_TRUNC): |
7169af54 | 9103 | return fold_builtin_trunc (fndecl, arglist); |
277f8dd2 | 9104 | |
4f35b1fc | 9105 | CASE_FLT_FN (BUILT_IN_ROUND): |
7169af54 | 9106 | return fold_builtin_round (fndecl, arglist); |
89ab3887 | 9107 | |
4f35b1fc | 9108 | CASE_FLT_FN (BUILT_IN_NEARBYINT): |
9109 | CASE_FLT_FN (BUILT_IN_RINT): | |
76b9b24b | 9110 | return fold_trunc_transparent_mathfn (fndecl, arglist); |
6528f4f4 | 9111 | |
4f35b1fc | 9112 | CASE_FLT_FN (BUILT_IN_LCEIL): |
9113 | CASE_FLT_FN (BUILT_IN_LLCEIL): | |
9114 | CASE_FLT_FN (BUILT_IN_LFLOOR): | |
9115 | CASE_FLT_FN (BUILT_IN_LLFLOOR): | |
a0c938f0 | 9116 | CASE_FLT_FN (BUILT_IN_LROUND): |
4f35b1fc | 9117 | CASE_FLT_FN (BUILT_IN_LLROUND): |
ad52b9b7 | 9118 | return fold_builtin_int_roundingfn (fndecl, arglist); |
34f17811 | 9119 | |
4f35b1fc | 9120 | CASE_FLT_FN (BUILT_IN_LRINT): |
9121 | CASE_FLT_FN (BUILT_IN_LLRINT): | |
7169af54 | 9122 | return fold_fixed_mathfn (fndecl, arglist); |
9ed65c7f | 9123 | |
4f35b1fc | 9124 | CASE_INT_FN (BUILT_IN_FFS): |
9125 | CASE_INT_FN (BUILT_IN_CLZ): | |
9126 | CASE_INT_FN (BUILT_IN_CTZ): | |
9127 | CASE_INT_FN (BUILT_IN_POPCOUNT): | |
9128 | CASE_INT_FN (BUILT_IN_PARITY): | |
10b9666f | 9129 | return fold_builtin_bitop (fndecl, arglist); |
70fb4c07 | 9130 | |
4f46f2b9 | 9131 | case BUILT_IN_MEMSET: |
9132 | return fold_builtin_memset (arglist, type, ignore); | |
9133 | ||
9c8a1629 | 9134 | case BUILT_IN_MEMCPY: |
4f46f2b9 | 9135 | return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/0); |
9c8a1629 | 9136 | |
9137 | case BUILT_IN_MEMPCPY: | |
4f46f2b9 | 9138 | return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/1); |
9c8a1629 | 9139 | |
9140 | case BUILT_IN_MEMMOVE: | |
4f46f2b9 | 9141 | return fold_builtin_memory_op (arglist, type, ignore, /*endp=*/3); |
9142 | ||
9143 | case BUILT_IN_BZERO: | |
9144 | return fold_builtin_bzero (arglist, ignore); | |
9145 | ||
9146 | case BUILT_IN_BCOPY: | |
9147 | return fold_builtin_bcopy (arglist, ignore); | |
9c8a1629 | 9148 | |
4f35b1fc | 9149 | CASE_FLT_FN (BUILT_IN_SIGNBIT): |
64c18e8d | 9150 | return fold_builtin_signbit (fndecl, arglist); |
27f261ef | 9151 | |
d49367d4 | 9152 | case BUILT_IN_ISASCII: |
9153 | return fold_builtin_isascii (arglist); | |
9154 | ||
9155 | case BUILT_IN_TOASCII: | |
9156 | return fold_builtin_toascii (arglist); | |
9157 | ||
df1cf42e | 9158 | case BUILT_IN_ISDIGIT: |
9159 | return fold_builtin_isdigit (arglist); | |
9160 | ||
4f35b1fc | 9161 | CASE_FLT_FN (BUILT_IN_COPYSIGN): |
198d9bbe | 9162 | return fold_builtin_copysign (fndecl, arglist, type); |
467214fd | 9163 | |
4f35b1fc | 9164 | CASE_FLT_FN (BUILT_IN_FINITE): |
c4503c0a | 9165 | case BUILT_IN_FINITED32: |
9166 | case BUILT_IN_FINITED64: | |
9167 | case BUILT_IN_FINITED128: | |
d008ac5f | 9168 | return fold_builtin_classify (fndecl, arglist, BUILT_IN_FINITE); |
726069ba | 9169 | |
4f35b1fc | 9170 | CASE_FLT_FN (BUILT_IN_ISINF): |
c4503c0a | 9171 | case BUILT_IN_ISINFD32: |
9172 | case BUILT_IN_ISINFD64: | |
9173 | case BUILT_IN_ISINFD128: | |
d008ac5f | 9174 | return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISINF); |
726069ba | 9175 | |
4f35b1fc | 9176 | CASE_FLT_FN (BUILT_IN_ISNAN): |
c4503c0a | 9177 | case BUILT_IN_ISNAND32: |
9178 | case BUILT_IN_ISNAND64: | |
9179 | case BUILT_IN_ISNAND128: | |
d008ac5f | 9180 | return fold_builtin_classify (fndecl, arglist, BUILT_IN_ISNAN); |
726069ba | 9181 | |
9bc9f15f | 9182 | case BUILT_IN_ISGREATER: |
d5019fe8 | 9183 | return fold_builtin_unordered_cmp (fndecl, arglist, UNLE_EXPR, LE_EXPR); |
9bc9f15f | 9184 | case BUILT_IN_ISGREATEREQUAL: |
d5019fe8 | 9185 | return fold_builtin_unordered_cmp (fndecl, arglist, UNLT_EXPR, LT_EXPR); |
9bc9f15f | 9186 | case BUILT_IN_ISLESS: |
d5019fe8 | 9187 | return fold_builtin_unordered_cmp (fndecl, arglist, UNGE_EXPR, GE_EXPR); |
9bc9f15f | 9188 | case BUILT_IN_ISLESSEQUAL: |
d5019fe8 | 9189 | return fold_builtin_unordered_cmp (fndecl, arglist, UNGT_EXPR, GT_EXPR); |
9bc9f15f | 9190 | case BUILT_IN_ISLESSGREATER: |
d5019fe8 | 9191 | return fold_builtin_unordered_cmp (fndecl, arglist, UNEQ_EXPR, EQ_EXPR); |
9bc9f15f | 9192 | case BUILT_IN_ISUNORDERED: |
d5019fe8 | 9193 | return fold_builtin_unordered_cmp (fndecl, arglist, UNORDERED_EXPR, |
9194 | NOP_EXPR); | |
9bc9f15f | 9195 | |
7c2f0500 | 9196 | /* We do the folding for va_start in the expander. */ |
9197 | case BUILT_IN_VA_START: | |
9198 | break; | |
f0613857 | 9199 | |
0a39fd54 | 9200 | case BUILT_IN_OBJECT_SIZE: |
9201 | return fold_builtin_object_size (arglist); | |
9202 | case BUILT_IN_MEMCPY_CHK: | |
9203 | case BUILT_IN_MEMPCPY_CHK: | |
9204 | case BUILT_IN_MEMMOVE_CHK: | |
9205 | case BUILT_IN_MEMSET_CHK: | |
9206 | return fold_builtin_memory_chk (fndecl, arglist, NULL_TREE, ignore, | |
9207 | DECL_FUNCTION_CODE (fndecl)); | |
9208 | case BUILT_IN_STRCPY_CHK: | |
9209 | case BUILT_IN_STPCPY_CHK: | |
9210 | return fold_builtin_stxcpy_chk (fndecl, arglist, NULL_TREE, ignore, | |
9211 | DECL_FUNCTION_CODE (fndecl)); | |
9212 | case BUILT_IN_STRNCPY_CHK: | |
9213 | return fold_builtin_strncpy_chk (arglist, NULL_TREE); | |
9214 | case BUILT_IN_STRCAT_CHK: | |
9215 | return fold_builtin_strcat_chk (fndecl, arglist); | |
9216 | case BUILT_IN_STRNCAT_CHK: | |
9217 | return fold_builtin_strncat_chk (fndecl, arglist); | |
9218 | case BUILT_IN_SPRINTF_CHK: | |
9219 | case BUILT_IN_VSPRINTF_CHK: | |
9220 | return fold_builtin_sprintf_chk (arglist, DECL_FUNCTION_CODE (fndecl)); | |
9221 | case BUILT_IN_SNPRINTF_CHK: | |
9222 | case BUILT_IN_VSNPRINTF_CHK: | |
9223 | return fold_builtin_snprintf_chk (arglist, NULL_TREE, | |
9224 | DECL_FUNCTION_CODE (fndecl)); | |
9225 | ||
9226 | case BUILT_IN_PRINTF: | |
9227 | case BUILT_IN_PRINTF_UNLOCKED: | |
9228 | case BUILT_IN_VPRINTF: | |
9229 | case BUILT_IN_PRINTF_CHK: | |
9230 | case BUILT_IN_VPRINTF_CHK: | |
9231 | return fold_builtin_printf (fndecl, arglist, ignore, | |
9232 | DECL_FUNCTION_CODE (fndecl)); | |
9233 | ||
9234 | case BUILT_IN_FPRINTF: | |
9235 | case BUILT_IN_FPRINTF_UNLOCKED: | |
9236 | case BUILT_IN_VFPRINTF: | |
9237 | case BUILT_IN_FPRINTF_CHK: | |
9238 | case BUILT_IN_VFPRINTF_CHK: | |
9239 | return fold_builtin_fprintf (fndecl, arglist, ignore, | |
9240 | DECL_FUNCTION_CODE (fndecl)); | |
9241 | ||
650e4c94 | 9242 | default: |
9243 | break; | |
9244 | } | |
9245 | ||
9246 | return 0; | |
9247 | } | |
7e15618b | 9248 | |
4ee9c684 | 9249 | /* A wrapper function for builtin folding that prevents warnings for |
491e04ef | 9250 | "statement without effect" and the like, caused by removing the |
4ee9c684 | 9251 | call node earlier than the warning is generated. */ |
9252 | ||
9253 | tree | |
4a4d48d5 | 9254 | fold_builtin (tree fndecl, tree arglist, bool ignore) |
4ee9c684 | 9255 | { |
4a4d48d5 | 9256 | tree exp = fold_builtin_1 (fndecl, arglist, ignore); |
4ee9c684 | 9257 | if (exp) |
9258 | { | |
134b69c9 | 9259 | exp = build1 (NOP_EXPR, TREE_TYPE (exp), exp); |
4ee9c684 | 9260 | TREE_NO_WARNING (exp) = 1; |
9261 | } | |
7c2f0500 | 9262 | |
4ee9c684 | 9263 | return exp; |
9264 | } | |
9265 | ||
805e22b2 | 9266 | /* Conveniently construct a function call expression. */ |
9267 | ||
9268 | tree | |
aecda0d6 | 9269 | build_function_call_expr (tree fn, tree arglist) |
7e15618b | 9270 | { |
9271 | tree call_expr; | |
9272 | ||
9273 | call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn); | |
a1fbcbc1 | 9274 | return fold_build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)), |
05abc81b | 9275 | call_expr, arglist, NULL_TREE); |
7e15618b | 9276 | } |
0eb671f7 | 9277 | |
9278 | /* This function validates the types of a function call argument list | |
9279 | represented as a tree chain of parameters against a specified list | |
9280 | of tree_codes. If the last specifier is a 0, that represents an | |
9281 | ellipses, otherwise the last specifier must be a VOID_TYPE. */ | |
27d0c333 | 9282 | |
0eb671f7 | 9283 | static int |
ee582a61 | 9284 | validate_arglist (tree arglist, ...) |
0eb671f7 | 9285 | { |
0eb671f7 | 9286 | enum tree_code code; |
0903457a | 9287 | int res = 0; |
ee582a61 | 9288 | va_list ap; |
aecda0d6 | 9289 | |
ee582a61 | 9290 | va_start (ap, arglist); |
0eb671f7 | 9291 | |
f0ce3b1f | 9292 | do |
0eb671f7 | 9293 | { |
f0ce3b1f | 9294 | code = va_arg (ap, enum tree_code); |
9295 | switch (code) | |
9296 | { | |
9297 | case 0: | |
9298 | /* This signifies an ellipses, any further arguments are all ok. */ | |
9299 | res = 1; | |
9300 | goto end; | |
9301 | case VOID_TYPE: | |
9302 | /* This signifies an endlink, if no arguments remain, return | |
9303 | true, otherwise return false. */ | |
9304 | res = arglist == 0; | |
9305 | goto end; | |
9306 | default: | |
9307 | /* If no parameters remain or the parameter's code does not | |
9308 | match the specified code, return false. Otherwise continue | |
9309 | checking any remaining arguments. */ | |
552752f7 | 9310 | if (arglist == 0) |
9311 | goto end; | |
9312 | if (code == POINTER_TYPE) | |
9313 | { | |
9314 | if (! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))) | |
9315 | goto end; | |
9316 | } | |
9317 | else if (code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist)))) | |
f0ce3b1f | 9318 | goto end; |
9319 | break; | |
9320 | } | |
9321 | arglist = TREE_CHAIN (arglist); | |
0eb671f7 | 9322 | } |
f0ce3b1f | 9323 | while (1); |
0903457a | 9324 | |
9325 | /* We need gotos here since we can only have one VA_CLOSE in a | |
9326 | function. */ | |
9327 | end: ; | |
ee582a61 | 9328 | va_end (ap); |
0903457a | 9329 | |
9330 | return res; | |
0eb671f7 | 9331 | } |
fc2a2dcb | 9332 | |
fc2a2dcb | 9333 | /* Default target-specific builtin expander that does nothing. */ |
9334 | ||
9335 | rtx | |
aecda0d6 | 9336 | default_expand_builtin (tree exp ATTRIBUTE_UNUSED, |
9337 | rtx target ATTRIBUTE_UNUSED, | |
9338 | rtx subtarget ATTRIBUTE_UNUSED, | |
9339 | enum machine_mode mode ATTRIBUTE_UNUSED, | |
9340 | int ignore ATTRIBUTE_UNUSED) | |
fc2a2dcb | 9341 | { |
9342 | return NULL_RTX; | |
9343 | } | |
c7926a82 | 9344 | |
01537105 | 9345 | /* Returns true is EXP represents data that would potentially reside |
9346 | in a readonly section. */ | |
9347 | ||
9348 | static bool | |
9349 | readonly_data_expr (tree exp) | |
9350 | { | |
9351 | STRIP_NOPS (exp); | |
9352 | ||
9ff0637e | 9353 | if (TREE_CODE (exp) != ADDR_EXPR) |
9354 | return false; | |
9355 | ||
9356 | exp = get_base_address (TREE_OPERAND (exp, 0)); | |
9357 | if (!exp) | |
9358 | return false; | |
9359 | ||
9360 | /* Make sure we call decl_readonly_section only for trees it | |
9361 | can handle (since it returns true for everything it doesn't | |
9362 | understand). */ | |
491e04ef | 9363 | if (TREE_CODE (exp) == STRING_CST |
9ff0637e | 9364 | || TREE_CODE (exp) == CONSTRUCTOR |
9365 | || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp))) | |
9366 | return decl_readonly_section (exp, 0); | |
01537105 | 9367 | else |
9368 | return false; | |
9369 | } | |
4ee9c684 | 9370 | |
4ee9c684 | 9371 | /* Simplify a call to the strstr builtin. |
9372 | ||
9373 | Return 0 if no simplification was possible, otherwise return the | |
9374 | simplified form of the call as a tree. | |
9375 | ||
9376 | The simplified form may be a constant or other expression which | |
9377 | computes the same value, but in a more efficient manner (including | |
9378 | calls to other builtin functions). | |
9379 | ||
9380 | The call may contain arguments which need to be evaluated, but | |
9381 | which are not useful to determine the result of the call. In | |
9382 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
9383 | COMPOUND_EXPR will be an argument which must be evaluated. | |
9384 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
9385 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
9386 | form of the builtin function call. */ | |
9387 | ||
9388 | static tree | |
daa1d5f5 | 9389 | fold_builtin_strstr (tree arglist, tree type) |
4ee9c684 | 9390 | { |
9391 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
9392 | return 0; | |
9393 | else | |
9394 | { | |
9395 | tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
9396 | tree fn; | |
9397 | const char *p1, *p2; | |
9398 | ||
9399 | p2 = c_getstr (s2); | |
9400 | if (p2 == NULL) | |
9401 | return 0; | |
9402 | ||
9403 | p1 = c_getstr (s1); | |
9404 | if (p1 != NULL) | |
9405 | { | |
9406 | const char *r = strstr (p1, p2); | |
daa1d5f5 | 9407 | tree tem; |
4ee9c684 | 9408 | |
4ee9c684 | 9409 | if (r == NULL) |
779b4c41 | 9410 | return build_int_cst (TREE_TYPE (s1), 0); |
c0c67e38 | 9411 | |
9412 | /* Return an offset into the constant string argument. */ | |
49d00087 | 9413 | tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1), |
9414 | s1, build_int_cst (TREE_TYPE (s1), r - p1)); | |
daa1d5f5 | 9415 | return fold_convert (type, tem); |
4ee9c684 | 9416 | } |
9417 | ||
7efa231c | 9418 | /* The argument is const char *, and the result is char *, so we need |
9419 | a type conversion here to avoid a warning. */ | |
4ee9c684 | 9420 | if (p2[0] == '\0') |
7efa231c | 9421 | return fold_convert (type, s1); |
4ee9c684 | 9422 | |
9423 | if (p2[1] != '\0') | |
9424 | return 0; | |
9425 | ||
9426 | fn = implicit_built_in_decls[BUILT_IN_STRCHR]; | |
9427 | if (!fn) | |
9428 | return 0; | |
9429 | ||
9430 | /* New argument list transforming strstr(s1, s2) to | |
9431 | strchr(s1, s2[0]). */ | |
7c446c95 | 9432 | arglist = build_tree_list (NULL_TREE, |
7016c612 | 9433 | build_int_cst (NULL_TREE, p2[0])); |
4ee9c684 | 9434 | arglist = tree_cons (NULL_TREE, s1, arglist); |
9435 | return build_function_call_expr (fn, arglist); | |
9436 | } | |
9437 | } | |
9438 | ||
b9741790 | 9439 | /* Simplify a call to the strchr builtin. |
4ee9c684 | 9440 | |
9441 | Return 0 if no simplification was possible, otherwise return the | |
9442 | simplified form of the call as a tree. | |
9443 | ||
9444 | The simplified form may be a constant or other expression which | |
9445 | computes the same value, but in a more efficient manner (including | |
9446 | calls to other builtin functions). | |
9447 | ||
9448 | The call may contain arguments which need to be evaluated, but | |
9449 | which are not useful to determine the result of the call. In | |
9450 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
9451 | COMPOUND_EXPR will be an argument which must be evaluated. | |
9452 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
9453 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
9454 | form of the builtin function call. */ | |
9455 | ||
9456 | static tree | |
daa1d5f5 | 9457 | fold_builtin_strchr (tree arglist, tree type) |
4ee9c684 | 9458 | { |
9459 | if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
9460 | return 0; | |
9461 | else | |
9462 | { | |
9463 | tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
9464 | const char *p1; | |
9465 | ||
9466 | if (TREE_CODE (s2) != INTEGER_CST) | |
9467 | return 0; | |
9468 | ||
9469 | p1 = c_getstr (s1); | |
9470 | if (p1 != NULL) | |
9471 | { | |
9472 | char c; | |
9473 | const char *r; | |
daa1d5f5 | 9474 | tree tem; |
4ee9c684 | 9475 | |
9476 | if (target_char_cast (s2, &c)) | |
9477 | return 0; | |
9478 | ||
9479 | r = strchr (p1, c); | |
9480 | ||
9481 | if (r == NULL) | |
779b4c41 | 9482 | return build_int_cst (TREE_TYPE (s1), 0); |
4ee9c684 | 9483 | |
9484 | /* Return an offset into the constant string argument. */ | |
49d00087 | 9485 | tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1), |
9486 | s1, build_int_cst (TREE_TYPE (s1), r - p1)); | |
daa1d5f5 | 9487 | return fold_convert (type, tem); |
4ee9c684 | 9488 | } |
4ee9c684 | 9489 | return 0; |
9490 | } | |
9491 | } | |
9492 | ||
9493 | /* Simplify a call to the strrchr builtin. | |
9494 | ||
9495 | Return 0 if no simplification was possible, otherwise return the | |
9496 | simplified form of the call as a tree. | |
9497 | ||
9498 | The simplified form may be a constant or other expression which | |
9499 | computes the same value, but in a more efficient manner (including | |
9500 | calls to other builtin functions). | |
9501 | ||
9502 | The call may contain arguments which need to be evaluated, but | |
9503 | which are not useful to determine the result of the call. In | |
9504 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
9505 | COMPOUND_EXPR will be an argument which must be evaluated. | |
9506 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
9507 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
9508 | form of the builtin function call. */ | |
9509 | ||
9510 | static tree | |
daa1d5f5 | 9511 | fold_builtin_strrchr (tree arglist, tree type) |
4ee9c684 | 9512 | { |
9513 | if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
9514 | return 0; | |
9515 | else | |
9516 | { | |
9517 | tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
9518 | tree fn; | |
9519 | const char *p1; | |
9520 | ||
9521 | if (TREE_CODE (s2) != INTEGER_CST) | |
9522 | return 0; | |
9523 | ||
9524 | p1 = c_getstr (s1); | |
9525 | if (p1 != NULL) | |
9526 | { | |
9527 | char c; | |
9528 | const char *r; | |
daa1d5f5 | 9529 | tree tem; |
4ee9c684 | 9530 | |
9531 | if (target_char_cast (s2, &c)) | |
9532 | return 0; | |
9533 | ||
9534 | r = strrchr (p1, c); | |
9535 | ||
9536 | if (r == NULL) | |
779b4c41 | 9537 | return build_int_cst (TREE_TYPE (s1), 0); |
4ee9c684 | 9538 | |
9539 | /* Return an offset into the constant string argument. */ | |
49d00087 | 9540 | tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1), |
9541 | s1, build_int_cst (TREE_TYPE (s1), r - p1)); | |
daa1d5f5 | 9542 | return fold_convert (type, tem); |
4ee9c684 | 9543 | } |
9544 | ||
9545 | if (! integer_zerop (s2)) | |
9546 | return 0; | |
9547 | ||
9548 | fn = implicit_built_in_decls[BUILT_IN_STRCHR]; | |
9549 | if (!fn) | |
9550 | return 0; | |
9551 | ||
9552 | /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */ | |
9553 | return build_function_call_expr (fn, arglist); | |
9554 | } | |
9555 | } | |
9556 | ||
9557 | /* Simplify a call to the strpbrk builtin. | |
9558 | ||
9559 | Return 0 if no simplification was possible, otherwise return the | |
9560 | simplified form of the call as a tree. | |
9561 | ||
9562 | The simplified form may be a constant or other expression which | |
9563 | computes the same value, but in a more efficient manner (including | |
9564 | calls to other builtin functions). | |
9565 | ||
9566 | The call may contain arguments which need to be evaluated, but | |
9567 | which are not useful to determine the result of the call. In | |
9568 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
9569 | COMPOUND_EXPR will be an argument which must be evaluated. | |
9570 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
9571 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
9572 | form of the builtin function call. */ | |
9573 | ||
9574 | static tree | |
daa1d5f5 | 9575 | fold_builtin_strpbrk (tree arglist, tree type) |
4ee9c684 | 9576 | { |
9577 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
9578 | return 0; | |
9579 | else | |
9580 | { | |
9581 | tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
9582 | tree fn; | |
9583 | const char *p1, *p2; | |
9584 | ||
9585 | p2 = c_getstr (s2); | |
9586 | if (p2 == NULL) | |
9587 | return 0; | |
9588 | ||
9589 | p1 = c_getstr (s1); | |
9590 | if (p1 != NULL) | |
9591 | { | |
9592 | const char *r = strpbrk (p1, p2); | |
daa1d5f5 | 9593 | tree tem; |
4ee9c684 | 9594 | |
9595 | if (r == NULL) | |
779b4c41 | 9596 | return build_int_cst (TREE_TYPE (s1), 0); |
4ee9c684 | 9597 | |
9598 | /* Return an offset into the constant string argument. */ | |
49d00087 | 9599 | tem = fold_build2 (PLUS_EXPR, TREE_TYPE (s1), |
9600 | s1, build_int_cst (TREE_TYPE (s1), r - p1)); | |
daa1d5f5 | 9601 | return fold_convert (type, tem); |
4ee9c684 | 9602 | } |
9603 | ||
9604 | if (p2[0] == '\0') | |
05abc81b | 9605 | /* strpbrk(x, "") == NULL. |
9606 | Evaluate and ignore s1 in case it had side-effects. */ | |
9607 | return omit_one_operand (TREE_TYPE (s1), integer_zero_node, s1); | |
4ee9c684 | 9608 | |
9609 | if (p2[1] != '\0') | |
9610 | return 0; /* Really call strpbrk. */ | |
9611 | ||
9612 | fn = implicit_built_in_decls[BUILT_IN_STRCHR]; | |
9613 | if (!fn) | |
9614 | return 0; | |
9615 | ||
9616 | /* New argument list transforming strpbrk(s1, s2) to | |
9617 | strchr(s1, s2[0]). */ | |
7c446c95 | 9618 | arglist = build_tree_list (NULL_TREE, |
7016c612 | 9619 | build_int_cst (NULL_TREE, p2[0])); |
4ee9c684 | 9620 | arglist = tree_cons (NULL_TREE, s1, arglist); |
9621 | return build_function_call_expr (fn, arglist); | |
9622 | } | |
9623 | } | |
9624 | ||
4ee9c684 | 9625 | /* Simplify a call to the strcat builtin. |
9626 | ||
9627 | Return 0 if no simplification was possible, otherwise return the | |
9628 | simplified form of the call as a tree. | |
9629 | ||
9630 | The simplified form may be a constant or other expression which | |
9631 | computes the same value, but in a more efficient manner (including | |
9632 | calls to other builtin functions). | |
9633 | ||
9634 | The call may contain arguments which need to be evaluated, but | |
9635 | which are not useful to determine the result of the call. In | |
9636 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
9637 | COMPOUND_EXPR will be an argument which must be evaluated. | |
9638 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
9639 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
9640 | form of the builtin function call. */ | |
9641 | ||
9642 | static tree | |
7c2f0500 | 9643 | fold_builtin_strcat (tree arglist) |
4ee9c684 | 9644 | { |
9645 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
9646 | return 0; | |
9647 | else | |
9648 | { | |
9649 | tree dst = TREE_VALUE (arglist), | |
9650 | src = TREE_VALUE (TREE_CHAIN (arglist)); | |
9651 | const char *p = c_getstr (src); | |
9652 | ||
9653 | /* If the string length is zero, return the dst parameter. */ | |
9654 | if (p && *p == '\0') | |
9655 | return dst; | |
9656 | ||
9657 | return 0; | |
9658 | } | |
9659 | } | |
9660 | ||
9661 | /* Simplify a call to the strncat builtin. | |
9662 | ||
9663 | Return 0 if no simplification was possible, otherwise return the | |
9664 | simplified form of the call as a tree. | |
9665 | ||
9666 | The simplified form may be a constant or other expression which | |
9667 | computes the same value, but in a more efficient manner (including | |
9668 | calls to other builtin functions). | |
9669 | ||
9670 | The call may contain arguments which need to be evaluated, but | |
9671 | which are not useful to determine the result of the call. In | |
9672 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
9673 | COMPOUND_EXPR will be an argument which must be evaluated. | |
9674 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
9675 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
9676 | form of the builtin function call. */ | |
9677 | ||
9678 | static tree | |
7c2f0500 | 9679 | fold_builtin_strncat (tree arglist) |
4ee9c684 | 9680 | { |
9681 | if (!validate_arglist (arglist, | |
9682 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
9683 | return 0; | |
9684 | else | |
9685 | { | |
9686 | tree dst = TREE_VALUE (arglist); | |
9687 | tree src = TREE_VALUE (TREE_CHAIN (arglist)); | |
9688 | tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
9689 | const char *p = c_getstr (src); | |
9690 | ||
9691 | /* If the requested length is zero, or the src parameter string | |
0a39fd54 | 9692 | length is zero, return the dst parameter. */ |
4ee9c684 | 9693 | if (integer_zerop (len) || (p && *p == '\0')) |
a0c938f0 | 9694 | return omit_two_operands (TREE_TYPE (dst), dst, src, len); |
4ee9c684 | 9695 | |
9696 | /* If the requested len is greater than or equal to the string | |
a0c938f0 | 9697 | length, call strcat. */ |
4ee9c684 | 9698 | if (TREE_CODE (len) == INTEGER_CST && p |
9699 | && compare_tree_int (len, strlen (p)) >= 0) | |
9700 | { | |
9701 | tree newarglist | |
9702 | = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src)); | |
9703 | tree fn = implicit_built_in_decls[BUILT_IN_STRCAT]; | |
9704 | ||
9705 | /* If the replacement _DECL isn't initialized, don't do the | |
9706 | transformation. */ | |
9707 | if (!fn) | |
9708 | return 0; | |
9709 | ||
9710 | return build_function_call_expr (fn, newarglist); | |
9711 | } | |
9712 | return 0; | |
9713 | } | |
9714 | } | |
9715 | ||
9716 | /* Simplify a call to the strspn builtin. | |
9717 | ||
9718 | Return 0 if no simplification was possible, otherwise return the | |
9719 | simplified form of the call as a tree. | |
9720 | ||
9721 | The simplified form may be a constant or other expression which | |
9722 | computes the same value, but in a more efficient manner (including | |
9723 | calls to other builtin functions). | |
9724 | ||
9725 | The call may contain arguments which need to be evaluated, but | |
9726 | which are not useful to determine the result of the call. In | |
9727 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
9728 | COMPOUND_EXPR will be an argument which must be evaluated. | |
9729 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
9730 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
9731 | form of the builtin function call. */ | |
9732 | ||
9733 | static tree | |
7c2f0500 | 9734 | fold_builtin_strspn (tree arglist) |
4ee9c684 | 9735 | { |
9736 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
9737 | return 0; | |
9738 | else | |
9739 | { | |
9740 | tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
9741 | const char *p1 = c_getstr (s1), *p2 = c_getstr (s2); | |
9742 | ||
9743 | /* If both arguments are constants, evaluate at compile-time. */ | |
9744 | if (p1 && p2) | |
9745 | { | |
9746 | const size_t r = strspn (p1, p2); | |
9747 | return size_int (r); | |
9748 | } | |
9749 | ||
9750 | /* If either argument is "", return 0. */ | |
9751 | if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0')) | |
9bc9f15f | 9752 | /* Evaluate and ignore both arguments in case either one has |
9753 | side-effects. */ | |
9754 | return omit_two_operands (integer_type_node, integer_zero_node, | |
9755 | s1, s2); | |
4ee9c684 | 9756 | return 0; |
9757 | } | |
9758 | } | |
9759 | ||
9760 | /* Simplify a call to the strcspn builtin. | |
9761 | ||
9762 | Return 0 if no simplification was possible, otherwise return the | |
9763 | simplified form of the call as a tree. | |
9764 | ||
9765 | The simplified form may be a constant or other expression which | |
9766 | computes the same value, but in a more efficient manner (including | |
9767 | calls to other builtin functions). | |
9768 | ||
9769 | The call may contain arguments which need to be evaluated, but | |
9770 | which are not useful to determine the result of the call. In | |
9771 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
9772 | COMPOUND_EXPR will be an argument which must be evaluated. | |
9773 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
9774 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
9775 | form of the builtin function call. */ | |
9776 | ||
9777 | static tree | |
7c2f0500 | 9778 | fold_builtin_strcspn (tree arglist) |
4ee9c684 | 9779 | { |
9780 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
9781 | return 0; | |
9782 | else | |
9783 | { | |
9784 | tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
9785 | const char *p1 = c_getstr (s1), *p2 = c_getstr (s2); | |
9786 | ||
9787 | /* If both arguments are constants, evaluate at compile-time. */ | |
9788 | if (p1 && p2) | |
9789 | { | |
9790 | const size_t r = strcspn (p1, p2); | |
9791 | return size_int (r); | |
9792 | } | |
9793 | ||
9794 | /* If the first argument is "", return 0. */ | |
9795 | if (p1 && *p1 == '\0') | |
9796 | { | |
9797 | /* Evaluate and ignore argument s2 in case it has | |
9798 | side-effects. */ | |
05abc81b | 9799 | return omit_one_operand (integer_type_node, |
9800 | integer_zero_node, s2); | |
4ee9c684 | 9801 | } |
9802 | ||
9803 | /* If the second argument is "", return __builtin_strlen(s1). */ | |
9804 | if (p2 && *p2 == '\0') | |
9805 | { | |
9806 | tree newarglist = build_tree_list (NULL_TREE, s1), | |
9807 | fn = implicit_built_in_decls[BUILT_IN_STRLEN]; | |
9808 | ||
9809 | /* If the replacement _DECL isn't initialized, don't do the | |
9810 | transformation. */ | |
9811 | if (!fn) | |
9812 | return 0; | |
9813 | ||
9814 | return build_function_call_expr (fn, newarglist); | |
9815 | } | |
9816 | return 0; | |
9817 | } | |
9818 | } | |
9819 | ||
f0613857 | 9820 | /* Fold a call to the fputs builtin. IGNORE is true if the value returned |
9821 | by the builtin will be ignored. UNLOCKED is true is true if this | |
9822 | actually a call to fputs_unlocked. If LEN in non-NULL, it represents | |
9823 | the known length of the string. Return NULL_TREE if no simplification | |
9824 | was possible. */ | |
4ee9c684 | 9825 | |
9826 | tree | |
f0613857 | 9827 | fold_builtin_fputs (tree arglist, bool ignore, bool unlocked, tree len) |
4ee9c684 | 9828 | { |
f0613857 | 9829 | tree fn; |
6d77ed92 | 9830 | /* If we're using an unlocked function, assume the other unlocked |
9831 | functions exist explicitly. */ | |
9832 | tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED] | |
4ee9c684 | 9833 | : implicit_built_in_decls[BUILT_IN_FPUTC]; |
6d77ed92 | 9834 | tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED] |
4ee9c684 | 9835 | : implicit_built_in_decls[BUILT_IN_FWRITE]; |
9836 | ||
c403edd4 | 9837 | /* If the return value is used, don't do the transformation. */ |
9838 | if (!ignore) | |
4ee9c684 | 9839 | return 0; |
9840 | ||
9841 | /* Verify the arguments in the original call. */ | |
9842 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
9843 | return 0; | |
9844 | ||
f0613857 | 9845 | if (! len) |
9846 | len = c_strlen (TREE_VALUE (arglist), 0); | |
4ee9c684 | 9847 | |
9848 | /* Get the length of the string passed to fputs. If the length | |
9849 | can't be determined, punt. */ | |
9850 | if (!len | |
9851 | || TREE_CODE (len) != INTEGER_CST) | |
9852 | return 0; | |
9853 | ||
9854 | switch (compare_tree_int (len, 1)) | |
9855 | { | |
9856 | case -1: /* length is 0, delete the call entirely . */ | |
05abc81b | 9857 | return omit_one_operand (integer_type_node, integer_zero_node, |
9858 | TREE_VALUE (TREE_CHAIN (arglist))); | |
9859 | ||
4ee9c684 | 9860 | case 0: /* length is 1, call fputc. */ |
9861 | { | |
9862 | const char *p = c_getstr (TREE_VALUE (arglist)); | |
9863 | ||
9864 | if (p != NULL) | |
9865 | { | |
9866 | /* New argument list transforming fputs(string, stream) to | |
9867 | fputc(string[0], stream). */ | |
7c446c95 | 9868 | arglist = build_tree_list (NULL_TREE, |
9869 | TREE_VALUE (TREE_CHAIN (arglist))); | |
9870 | arglist = tree_cons (NULL_TREE, | |
7016c612 | 9871 | build_int_cst (NULL_TREE, p[0]), |
7c446c95 | 9872 | arglist); |
4ee9c684 | 9873 | fn = fn_fputc; |
9874 | break; | |
9875 | } | |
9876 | } | |
9877 | /* FALLTHROUGH */ | |
9878 | case 1: /* length is greater than 1, call fwrite. */ | |
9879 | { | |
9880 | tree string_arg; | |
9881 | ||
0bed3869 | 9882 | /* If optimizing for size keep fputs. */ |
4ee9c684 | 9883 | if (optimize_size) |
9884 | return 0; | |
9885 | string_arg = TREE_VALUE (arglist); | |
9886 | /* New argument list transforming fputs(string, stream) to | |
9887 | fwrite(string, 1, len, stream). */ | |
f0613857 | 9888 | arglist = build_tree_list (NULL_TREE, |
9889 | TREE_VALUE (TREE_CHAIN (arglist))); | |
4ee9c684 | 9890 | arglist = tree_cons (NULL_TREE, len, arglist); |
9891 | arglist = tree_cons (NULL_TREE, size_one_node, arglist); | |
9892 | arglist = tree_cons (NULL_TREE, string_arg, arglist); | |
9893 | fn = fn_fwrite; | |
9894 | break; | |
9895 | } | |
9896 | default: | |
64db345d | 9897 | gcc_unreachable (); |
4ee9c684 | 9898 | } |
9899 | ||
c403edd4 | 9900 | /* If the replacement _DECL isn't initialized, don't do the |
9901 | transformation. */ | |
9902 | if (!fn) | |
9903 | return 0; | |
9904 | ||
db97ad41 | 9905 | /* These optimizations are only performed when the result is ignored, |
9906 | hence there's no need to cast the result to integer_type_node. */ | |
9907 | return build_function_call_expr (fn, arglist); | |
4ee9c684 | 9908 | } |
9909 | ||
b08cf617 | 9910 | /* Fold the new_arg's arguments (ARGLIST). Returns true if there was an error |
743b0c6a | 9911 | produced. False otherwise. This is done so that we don't output the error |
9912 | or warning twice or three times. */ | |
9913 | bool | |
7c2f0500 | 9914 | fold_builtin_next_arg (tree arglist) |
4ee9c684 | 9915 | { |
9916 | tree fntype = TREE_TYPE (current_function_decl); | |
9917 | ||
9918 | if (TYPE_ARG_TYPES (fntype) == 0 | |
9919 | || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype))) | |
9920 | == void_type_node)) | |
743b0c6a | 9921 | { |
9922 | error ("%<va_start%> used in function with fixed args"); | |
9923 | return true; | |
9924 | } | |
79012a9d | 9925 | else if (!arglist) |
9926 | { | |
9927 | /* Evidently an out of date version of <stdarg.h>; can't validate | |
9928 | va_start's second argument, but can still work as intended. */ | |
c3ceba8e | 9929 | warning (0, "%<__builtin_next_arg%> called without an argument"); |
79012a9d | 9930 | return true; |
9931 | } | |
9932 | /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0) | |
9933 | when we checked the arguments and if needed issued a warning. */ | |
9934 | else if (!TREE_CHAIN (arglist) | |
a0c938f0 | 9935 | || !integer_zerop (TREE_VALUE (arglist)) |
9936 | || !integer_zerop (TREE_VALUE (TREE_CHAIN (arglist))) | |
9937 | || TREE_CHAIN (TREE_CHAIN (arglist))) | |
4ee9c684 | 9938 | { |
9939 | tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl)); | |
9940 | tree arg = TREE_VALUE (arglist); | |
9941 | ||
79012a9d | 9942 | if (TREE_CHAIN (arglist)) |
a0c938f0 | 9943 | { |
9944 | error ("%<va_start%> used with too many arguments"); | |
9945 | return true; | |
9946 | } | |
79012a9d | 9947 | |
4ee9c684 | 9948 | /* Strip off all nops for the sake of the comparison. This |
9949 | is not quite the same as STRIP_NOPS. It does more. | |
9950 | We must also strip off INDIRECT_EXPR for C++ reference | |
9951 | parameters. */ | |
9952 | while (TREE_CODE (arg) == NOP_EXPR | |
9953 | || TREE_CODE (arg) == CONVERT_EXPR | |
9954 | || TREE_CODE (arg) == NON_LVALUE_EXPR | |
9955 | || TREE_CODE (arg) == INDIRECT_REF) | |
9956 | arg = TREE_OPERAND (arg, 0); | |
9957 | if (arg != last_parm) | |
a0c938f0 | 9958 | { |
b08cf617 | 9959 | /* FIXME: Sometimes with the tree optimizers we can get the |
9960 | not the last argument even though the user used the last | |
9961 | argument. We just warn and set the arg to be the last | |
9962 | argument so that we will get wrong-code because of | |
9963 | it. */ | |
c3ceba8e | 9964 | warning (0, "second parameter of %<va_start%> not last named argument"); |
743b0c6a | 9965 | } |
79012a9d | 9966 | /* We want to verify the second parameter just once before the tree |
a0c938f0 | 9967 | optimizers are run and then avoid keeping it in the tree, |
9968 | as otherwise we could warn even for correct code like: | |
9969 | void foo (int i, ...) | |
9970 | { va_list ap; i++; va_start (ap, i); va_end (ap); } */ | |
79012a9d | 9971 | TREE_VALUE (arglist) = integer_zero_node; |
9972 | TREE_CHAIN (arglist) = build_tree_list (NULL, integer_zero_node); | |
743b0c6a | 9973 | } |
9974 | return false; | |
4ee9c684 | 9975 | } |
9976 | ||
9977 | ||
9978 | /* Simplify a call to the sprintf builtin. | |
9979 | ||
9980 | Return 0 if no simplification was possible, otherwise return the | |
9981 | simplified form of the call as a tree. If IGNORED is true, it means that | |
9982 | the caller does not use the returned value of the function. */ | |
9983 | ||
9984 | static tree | |
7c2f0500 | 9985 | fold_builtin_sprintf (tree arglist, int ignored) |
4ee9c684 | 9986 | { |
9987 | tree call, retval, dest, fmt; | |
9988 | const char *fmt_str = NULL; | |
9989 | ||
9990 | /* Verify the required arguments in the original call. We deal with two | |
9991 | types of sprintf() calls: 'sprintf (str, fmt)' and | |
9992 | 'sprintf (dest, "%s", orig)'. */ | |
9993 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE) | |
9994 | && !validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE, | |
0a39fd54 | 9995 | VOID_TYPE)) |
4ee9c684 | 9996 | return NULL_TREE; |
9997 | ||
9998 | /* Get the destination string and the format specifier. */ | |
9999 | dest = TREE_VALUE (arglist); | |
10000 | fmt = TREE_VALUE (TREE_CHAIN (arglist)); | |
10001 | ||
10002 | /* Check whether the format is a literal string constant. */ | |
10003 | fmt_str = c_getstr (fmt); | |
10004 | if (fmt_str == NULL) | |
10005 | return NULL_TREE; | |
10006 | ||
10007 | call = NULL_TREE; | |
10008 | retval = NULL_TREE; | |
10009 | ||
99eabcc1 | 10010 | if (!init_target_chars()) |
10011 | return 0; | |
10012 | ||
4ee9c684 | 10013 | /* If the format doesn't contain % args or %%, use strcpy. */ |
99eabcc1 | 10014 | if (strchr (fmt_str, target_percent) == NULL) |
4ee9c684 | 10015 | { |
10016 | tree fn = implicit_built_in_decls[BUILT_IN_STRCPY]; | |
10017 | ||
10018 | if (!fn) | |
10019 | return NULL_TREE; | |
10020 | ||
10021 | /* Convert sprintf (str, fmt) into strcpy (str, fmt) when | |
10022 | 'format' is known to contain no % formats. */ | |
10023 | arglist = build_tree_list (NULL_TREE, fmt); | |
10024 | arglist = tree_cons (NULL_TREE, dest, arglist); | |
10025 | call = build_function_call_expr (fn, arglist); | |
10026 | if (!ignored) | |
7016c612 | 10027 | retval = build_int_cst (NULL_TREE, strlen (fmt_str)); |
4ee9c684 | 10028 | } |
10029 | ||
10030 | /* If the format is "%s", use strcpy if the result isn't used. */ | |
99eabcc1 | 10031 | else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0) |
4ee9c684 | 10032 | { |
10033 | tree fn, orig; | |
10034 | fn = implicit_built_in_decls[BUILT_IN_STRCPY]; | |
10035 | ||
10036 | if (!fn) | |
10037 | return NULL_TREE; | |
10038 | ||
10039 | /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */ | |
10040 | orig = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
10041 | arglist = build_tree_list (NULL_TREE, orig); | |
10042 | arglist = tree_cons (NULL_TREE, dest, arglist); | |
10043 | if (!ignored) | |
10044 | { | |
10045 | retval = c_strlen (orig, 1); | |
10046 | if (!retval || TREE_CODE (retval) != INTEGER_CST) | |
10047 | return NULL_TREE; | |
10048 | } | |
10049 | call = build_function_call_expr (fn, arglist); | |
10050 | } | |
10051 | ||
10052 | if (call && retval) | |
10053 | { | |
e3b560a6 | 10054 | retval = fold_convert |
4ee9c684 | 10055 | (TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])), |
10056 | retval); | |
05abc81b | 10057 | return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval); |
4ee9c684 | 10058 | } |
10059 | else | |
10060 | return call; | |
10061 | } | |
0a39fd54 | 10062 | |
10063 | /* Expand a call to __builtin_object_size. */ | |
10064 | ||
10065 | rtx | |
10066 | expand_builtin_object_size (tree exp) | |
10067 | { | |
10068 | tree ost; | |
10069 | int object_size_type; | |
10070 | tree fndecl = get_callee_fndecl (exp); | |
10071 | tree arglist = TREE_OPERAND (exp, 1); | |
10072 | location_t locus = EXPR_LOCATION (exp); | |
10073 | ||
10074 | if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
10075 | { | |
10076 | error ("%Hfirst argument of %D must be a pointer, second integer constant", | |
10077 | &locus, fndecl); | |
10078 | expand_builtin_trap (); | |
10079 | return const0_rtx; | |
10080 | } | |
10081 | ||
10082 | ost = TREE_VALUE (TREE_CHAIN (arglist)); | |
10083 | STRIP_NOPS (ost); | |
10084 | ||
10085 | if (TREE_CODE (ost) != INTEGER_CST | |
10086 | || tree_int_cst_sgn (ost) < 0 | |
10087 | || compare_tree_int (ost, 3) > 0) | |
10088 | { | |
10089 | error ("%Hlast argument of %D is not integer constant between 0 and 3", | |
10090 | &locus, fndecl); | |
10091 | expand_builtin_trap (); | |
10092 | return const0_rtx; | |
10093 | } | |
10094 | ||
10095 | object_size_type = tree_low_cst (ost, 0); | |
10096 | ||
10097 | return object_size_type < 2 ? constm1_rtx : const0_rtx; | |
10098 | } | |
10099 | ||
10100 | /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin. | |
10101 | FCODE is the BUILT_IN_* to use. | |
10102 | Return 0 if we failed; the caller should emit a normal call, | |
10103 | otherwise try to get the result in TARGET, if convenient (and in | |
10104 | mode MODE if that's convenient). */ | |
10105 | ||
10106 | static rtx | |
10107 | expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode, | |
10108 | enum built_in_function fcode) | |
10109 | { | |
10110 | tree arglist = TREE_OPERAND (exp, 1); | |
10111 | tree dest, src, len, size; | |
10112 | ||
10113 | if (!validate_arglist (arglist, | |
10114 | POINTER_TYPE, | |
10115 | fcode == BUILT_IN_MEMSET_CHK | |
10116 | ? INTEGER_TYPE : POINTER_TYPE, | |
10117 | INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
10118 | return 0; | |
10119 | ||
10120 | dest = TREE_VALUE (arglist); | |
10121 | src = TREE_VALUE (TREE_CHAIN (arglist)); | |
10122 | len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
10123 | size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist)))); | |
10124 | ||
10125 | if (! host_integerp (size, 1)) | |
10126 | return 0; | |
10127 | ||
10128 | if (host_integerp (len, 1) || integer_all_onesp (size)) | |
10129 | { | |
10130 | tree fn; | |
10131 | ||
10132 | if (! integer_all_onesp (size) && tree_int_cst_lt (size, len)) | |
10133 | { | |
10134 | location_t locus = EXPR_LOCATION (exp); | |
10135 | warning (0, "%Hcall to %D will always overflow destination buffer", | |
10136 | &locus, get_callee_fndecl (exp)); | |
10137 | return 0; | |
10138 | } | |
10139 | ||
10140 | arglist = build_tree_list (NULL_TREE, len); | |
10141 | arglist = tree_cons (NULL_TREE, src, arglist); | |
10142 | arglist = tree_cons (NULL_TREE, dest, arglist); | |
10143 | ||
10144 | fn = NULL_TREE; | |
10145 | /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume | |
10146 | mem{cpy,pcpy,move,set} is available. */ | |
10147 | switch (fcode) | |
10148 | { | |
10149 | case BUILT_IN_MEMCPY_CHK: | |
10150 | fn = built_in_decls[BUILT_IN_MEMCPY]; | |
10151 | break; | |
10152 | case BUILT_IN_MEMPCPY_CHK: | |
10153 | fn = built_in_decls[BUILT_IN_MEMPCPY]; | |
10154 | break; | |
10155 | case BUILT_IN_MEMMOVE_CHK: | |
10156 | fn = built_in_decls[BUILT_IN_MEMMOVE]; | |
10157 | break; | |
10158 | case BUILT_IN_MEMSET_CHK: | |
10159 | fn = built_in_decls[BUILT_IN_MEMSET]; | |
10160 | break; | |
10161 | default: | |
10162 | break; | |
10163 | } | |
10164 | ||
10165 | if (! fn) | |
10166 | return 0; | |
10167 | ||
10168 | fn = build_function_call_expr (fn, arglist); | |
10169 | if (TREE_CODE (fn) == CALL_EXPR) | |
10170 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
10171 | return expand_expr (fn, target, mode, EXPAND_NORMAL); | |
10172 | } | |
10173 | else if (fcode == BUILT_IN_MEMSET_CHK) | |
10174 | return 0; | |
10175 | else | |
10176 | { | |
10177 | unsigned int dest_align | |
10178 | = get_pointer_alignment (dest, BIGGEST_ALIGNMENT); | |
10179 | ||
10180 | /* If DEST is not a pointer type, call the normal function. */ | |
10181 | if (dest_align == 0) | |
10182 | return 0; | |
10183 | ||
10184 | /* If SRC and DEST are the same (and not volatile), do nothing. */ | |
10185 | if (operand_equal_p (src, dest, 0)) | |
10186 | { | |
10187 | tree expr; | |
10188 | ||
10189 | if (fcode != BUILT_IN_MEMPCPY_CHK) | |
10190 | { | |
10191 | /* Evaluate and ignore LEN in case it has side-effects. */ | |
10192 | expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
10193 | return expand_expr (dest, target, mode, EXPAND_NORMAL); | |
10194 | } | |
10195 | ||
10196 | len = fold_convert (TREE_TYPE (dest), len); | |
10197 | expr = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, len); | |
10198 | return expand_expr (expr, target, mode, EXPAND_NORMAL); | |
10199 | } | |
10200 | ||
10201 | /* __memmove_chk special case. */ | |
10202 | if (fcode == BUILT_IN_MEMMOVE_CHK) | |
10203 | { | |
10204 | unsigned int src_align | |
10205 | = get_pointer_alignment (src, BIGGEST_ALIGNMENT); | |
10206 | ||
10207 | if (src_align == 0) | |
10208 | return 0; | |
10209 | ||
10210 | /* If src is categorized for a readonly section we can use | |
10211 | normal __memcpy_chk. */ | |
10212 | if (readonly_data_expr (src)) | |
10213 | { | |
10214 | tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK]; | |
10215 | if (!fn) | |
10216 | return 0; | |
10217 | fn = build_function_call_expr (fn, arglist); | |
10218 | if (TREE_CODE (fn) == CALL_EXPR) | |
10219 | CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp); | |
10220 | return expand_expr (fn, target, mode, EXPAND_NORMAL); | |
10221 | } | |
10222 | } | |
10223 | return 0; | |
10224 | } | |
10225 | } | |
10226 | ||
10227 | /* Emit warning if a buffer overflow is detected at compile time. */ | |
10228 | ||
10229 | static void | |
10230 | maybe_emit_chk_warning (tree exp, enum built_in_function fcode) | |
10231 | { | |
10232 | int arg_mask, is_strlen = 0; | |
10233 | tree arglist = TREE_OPERAND (exp, 1), a; | |
10234 | tree len, size; | |
10235 | location_t locus; | |
10236 | ||
10237 | switch (fcode) | |
10238 | { | |
10239 | case BUILT_IN_STRCPY_CHK: | |
10240 | case BUILT_IN_STPCPY_CHK: | |
10241 | /* For __strcat_chk the warning will be emitted only if overflowing | |
10242 | by at least strlen (dest) + 1 bytes. */ | |
10243 | case BUILT_IN_STRCAT_CHK: | |
10244 | arg_mask = 6; | |
10245 | is_strlen = 1; | |
10246 | break; | |
b356dfef | 10247 | case BUILT_IN_STRNCAT_CHK: |
10248 | /* For __strncat_chk the warning will be emitted only if overflowing | |
10249 | by at least strlen (dest) + 1 bytes. */ | |
10250 | arg_mask = 12; | |
10251 | break; | |
0a39fd54 | 10252 | case BUILT_IN_STRNCPY_CHK: |
10253 | arg_mask = 12; | |
10254 | break; | |
10255 | case BUILT_IN_SNPRINTF_CHK: | |
10256 | case BUILT_IN_VSNPRINTF_CHK: | |
10257 | arg_mask = 10; | |
10258 | break; | |
10259 | default: | |
10260 | gcc_unreachable (); | |
10261 | } | |
10262 | ||
10263 | len = NULL_TREE; | |
10264 | size = NULL_TREE; | |
10265 | for (a = arglist; a && arg_mask; a = TREE_CHAIN (a), arg_mask >>= 1) | |
10266 | if (arg_mask & 1) | |
10267 | { | |
10268 | if (len) | |
10269 | size = a; | |
10270 | else | |
10271 | len = a; | |
10272 | } | |
10273 | ||
10274 | if (!len || !size) | |
10275 | return; | |
10276 | ||
10277 | len = TREE_VALUE (len); | |
10278 | size = TREE_VALUE (size); | |
10279 | ||
10280 | if (! host_integerp (size, 1) || integer_all_onesp (size)) | |
10281 | return; | |
10282 | ||
10283 | if (is_strlen) | |
10284 | { | |
10285 | len = c_strlen (len, 1); | |
10286 | if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size)) | |
10287 | return; | |
10288 | } | |
b356dfef | 10289 | else if (fcode == BUILT_IN_STRNCAT_CHK) |
10290 | { | |
10291 | tree src = TREE_VALUE (TREE_CHAIN (arglist)); | |
10292 | if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size)) | |
10293 | return; | |
10294 | src = c_strlen (src, 1); | |
10295 | if (! src || ! host_integerp (src, 1)) | |
10296 | { | |
10297 | locus = EXPR_LOCATION (exp); | |
10298 | warning (0, "%Hcall to %D might overflow destination buffer", | |
10299 | &locus, get_callee_fndecl (exp)); | |
10300 | return; | |
10301 | } | |
10302 | else if (tree_int_cst_lt (src, size)) | |
10303 | return; | |
10304 | } | |
0a39fd54 | 10305 | else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len)) |
10306 | return; | |
10307 | ||
10308 | locus = EXPR_LOCATION (exp); | |
10309 | warning (0, "%Hcall to %D will always overflow destination buffer", | |
10310 | &locus, get_callee_fndecl (exp)); | |
10311 | } | |
10312 | ||
10313 | /* Emit warning if a buffer overflow is detected at compile time | |
10314 | in __sprintf_chk/__vsprintf_chk calls. */ | |
10315 | ||
10316 | static void | |
10317 | maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode) | |
10318 | { | |
10319 | tree arglist = TREE_OPERAND (exp, 1); | |
10320 | tree dest, size, len, fmt, flag; | |
10321 | const char *fmt_str; | |
10322 | ||
10323 | /* Verify the required arguments in the original call. */ | |
10324 | if (! arglist) | |
10325 | return; | |
10326 | dest = TREE_VALUE (arglist); | |
10327 | arglist = TREE_CHAIN (arglist); | |
10328 | if (! arglist) | |
10329 | return; | |
10330 | flag = TREE_VALUE (arglist); | |
10331 | arglist = TREE_CHAIN (arglist); | |
10332 | if (! arglist) | |
10333 | return; | |
10334 | size = TREE_VALUE (arglist); | |
10335 | arglist = TREE_CHAIN (arglist); | |
10336 | if (! arglist) | |
10337 | return; | |
10338 | fmt = TREE_VALUE (arglist); | |
10339 | arglist = TREE_CHAIN (arglist); | |
10340 | ||
10341 | if (! host_integerp (size, 1) || integer_all_onesp (size)) | |
10342 | return; | |
10343 | ||
10344 | /* Check whether the format is a literal string constant. */ | |
10345 | fmt_str = c_getstr (fmt); | |
10346 | if (fmt_str == NULL) | |
10347 | return; | |
10348 | ||
99eabcc1 | 10349 | if (!init_target_chars()) |
10350 | return; | |
10351 | ||
0a39fd54 | 10352 | /* If the format doesn't contain % args or %%, we know its size. */ |
99eabcc1 | 10353 | if (strchr (fmt_str, target_percent) == 0) |
0a39fd54 | 10354 | len = build_int_cstu (size_type_node, strlen (fmt_str)); |
10355 | /* If the format is "%s" and first ... argument is a string literal, | |
10356 | we know it too. */ | |
99eabcc1 | 10357 | else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0) |
0a39fd54 | 10358 | { |
10359 | tree arg; | |
10360 | ||
10361 | if (! arglist) | |
10362 | return; | |
10363 | arg = TREE_VALUE (arglist); | |
10364 | if (! POINTER_TYPE_P (TREE_TYPE (arg))) | |
10365 | return; | |
10366 | ||
10367 | len = c_strlen (arg, 1); | |
10368 | if (!len || ! host_integerp (len, 1)) | |
10369 | return; | |
10370 | } | |
10371 | else | |
10372 | return; | |
10373 | ||
10374 | if (! tree_int_cst_lt (len, size)) | |
10375 | { | |
10376 | location_t locus = EXPR_LOCATION (exp); | |
10377 | warning (0, "%Hcall to %D will always overflow destination buffer", | |
10378 | &locus, get_callee_fndecl (exp)); | |
10379 | } | |
10380 | } | |
10381 | ||
10382 | /* Fold a call to __builtin_object_size, if possible. */ | |
10383 | ||
10384 | tree | |
10385 | fold_builtin_object_size (tree arglist) | |
10386 | { | |
10387 | tree ptr, ost, ret = 0; | |
10388 | int object_size_type; | |
10389 | ||
10390 | if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
10391 | return 0; | |
10392 | ||
10393 | ptr = TREE_VALUE (arglist); | |
10394 | ost = TREE_VALUE (TREE_CHAIN (arglist)); | |
10395 | STRIP_NOPS (ost); | |
10396 | ||
10397 | if (TREE_CODE (ost) != INTEGER_CST | |
10398 | || tree_int_cst_sgn (ost) < 0 | |
10399 | || compare_tree_int (ost, 3) > 0) | |
10400 | return 0; | |
10401 | ||
10402 | object_size_type = tree_low_cst (ost, 0); | |
10403 | ||
10404 | /* __builtin_object_size doesn't evaluate side-effects in its arguments; | |
10405 | if there are any side-effects, it returns (size_t) -1 for types 0 and 1 | |
10406 | and (size_t) 0 for types 2 and 3. */ | |
10407 | if (TREE_SIDE_EFFECTS (ptr)) | |
10408 | return fold_convert (size_type_node, | |
10409 | object_size_type < 2 | |
10410 | ? integer_minus_one_node : integer_zero_node); | |
10411 | ||
10412 | if (TREE_CODE (ptr) == ADDR_EXPR) | |
10413 | ret = build_int_cstu (size_type_node, | |
10414 | compute_builtin_object_size (ptr, object_size_type)); | |
10415 | ||
10416 | else if (TREE_CODE (ptr) == SSA_NAME) | |
10417 | { | |
10418 | unsigned HOST_WIDE_INT bytes; | |
10419 | ||
10420 | /* If object size is not known yet, delay folding until | |
10421 | later. Maybe subsequent passes will help determining | |
10422 | it. */ | |
10423 | bytes = compute_builtin_object_size (ptr, object_size_type); | |
10424 | if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 | |
10425 | ? -1 : 0)) | |
10426 | ret = build_int_cstu (size_type_node, bytes); | |
10427 | } | |
10428 | ||
10429 | if (ret) | |
10430 | { | |
10431 | ret = force_fit_type (ret, -1, false, false); | |
10432 | if (TREE_CONSTANT_OVERFLOW (ret)) | |
10433 | ret = 0; | |
10434 | } | |
10435 | ||
10436 | return ret; | |
10437 | } | |
10438 | ||
10439 | /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin. | |
10440 | IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_* | |
10441 | code of the builtin. If MAXLEN is not NULL, it is maximum length | |
10442 | passed as third argument. */ | |
10443 | ||
10444 | tree | |
10445 | fold_builtin_memory_chk (tree fndecl, tree arglist, tree maxlen, bool ignore, | |
10446 | enum built_in_function fcode) | |
10447 | { | |
10448 | tree dest, src, len, size, fn; | |
10449 | ||
10450 | if (!validate_arglist (arglist, | |
10451 | POINTER_TYPE, | |
10452 | fcode == BUILT_IN_MEMSET_CHK | |
10453 | ? INTEGER_TYPE : POINTER_TYPE, | |
10454 | INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
10455 | return 0; | |
10456 | ||
10457 | dest = TREE_VALUE (arglist); | |
10458 | /* Actually val for __memset_chk, but it doesn't matter. */ | |
10459 | src = TREE_VALUE (TREE_CHAIN (arglist)); | |
10460 | len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
10461 | size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist)))); | |
10462 | ||
10463 | /* If SRC and DEST are the same (and not volatile), return DEST | |
10464 | (resp. DEST+LEN for __mempcpy_chk). */ | |
10465 | if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0)) | |
10466 | { | |
10467 | if (fcode != BUILT_IN_MEMPCPY_CHK) | |
10468 | return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len); | |
10469 | else | |
10470 | { | |
10471 | tree temp = fold_convert (TREE_TYPE (dest), len); | |
10472 | temp = fold_build2 (PLUS_EXPR, TREE_TYPE (dest), dest, temp); | |
10473 | return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), temp); | |
10474 | } | |
10475 | } | |
10476 | ||
10477 | if (! host_integerp (size, 1)) | |
10478 | return 0; | |
10479 | ||
10480 | if (! integer_all_onesp (size)) | |
10481 | { | |
10482 | if (! host_integerp (len, 1)) | |
10483 | { | |
10484 | /* If LEN is not constant, try MAXLEN too. | |
10485 | For MAXLEN only allow optimizing into non-_ocs function | |
10486 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
10487 | if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1)) | |
10488 | { | |
10489 | if (fcode == BUILT_IN_MEMPCPY_CHK && ignore) | |
10490 | { | |
10491 | /* (void) __mempcpy_chk () can be optimized into | |
10492 | (void) __memcpy_chk (). */ | |
10493 | fn = built_in_decls[BUILT_IN_MEMCPY_CHK]; | |
10494 | if (!fn) | |
10495 | return 0; | |
10496 | ||
10497 | return build_function_call_expr (fn, arglist); | |
10498 | } | |
10499 | return 0; | |
10500 | } | |
0a39fd54 | 10501 | } |
ad89623c | 10502 | else |
10503 | maxlen = len; | |
0a39fd54 | 10504 | |
ad89623c | 10505 | if (tree_int_cst_lt (size, maxlen)) |
0a39fd54 | 10506 | return 0; |
10507 | } | |
10508 | ||
10509 | arglist = build_tree_list (NULL_TREE, len); | |
10510 | arglist = tree_cons (NULL_TREE, src, arglist); | |
10511 | arglist = tree_cons (NULL_TREE, dest, arglist); | |
10512 | ||
10513 | fn = NULL_TREE; | |
10514 | /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume | |
10515 | mem{cpy,pcpy,move,set} is available. */ | |
10516 | switch (fcode) | |
10517 | { | |
10518 | case BUILT_IN_MEMCPY_CHK: | |
10519 | fn = built_in_decls[BUILT_IN_MEMCPY]; | |
10520 | break; | |
10521 | case BUILT_IN_MEMPCPY_CHK: | |
10522 | fn = built_in_decls[BUILT_IN_MEMPCPY]; | |
10523 | break; | |
10524 | case BUILT_IN_MEMMOVE_CHK: | |
10525 | fn = built_in_decls[BUILT_IN_MEMMOVE]; | |
10526 | break; | |
10527 | case BUILT_IN_MEMSET_CHK: | |
10528 | fn = built_in_decls[BUILT_IN_MEMSET]; | |
10529 | break; | |
10530 | default: | |
10531 | break; | |
10532 | } | |
10533 | ||
10534 | if (!fn) | |
10535 | return 0; | |
10536 | ||
10537 | return build_function_call_expr (fn, arglist); | |
10538 | } | |
10539 | ||
10540 | /* Fold a call to the __st[rp]cpy_chk builtin. | |
10541 | IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_* | |
10542 | code of the builtin. If MAXLEN is not NULL, it is maximum length of | |
10543 | strings passed as second argument. */ | |
10544 | ||
10545 | tree | |
10546 | fold_builtin_stxcpy_chk (tree fndecl, tree arglist, tree maxlen, bool ignore, | |
10547 | enum built_in_function fcode) | |
10548 | { | |
10549 | tree dest, src, size, len, fn; | |
10550 | ||
10551 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, | |
10552 | VOID_TYPE)) | |
10553 | return 0; | |
10554 | ||
10555 | dest = TREE_VALUE (arglist); | |
10556 | src = TREE_VALUE (TREE_CHAIN (arglist)); | |
10557 | size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
10558 | ||
10559 | /* If SRC and DEST are the same (and not volatile), return DEST. */ | |
10560 | if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0)) | |
10561 | return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), dest); | |
a0c938f0 | 10562 | |
0a39fd54 | 10563 | if (! host_integerp (size, 1)) |
10564 | return 0; | |
10565 | ||
10566 | if (! integer_all_onesp (size)) | |
10567 | { | |
10568 | len = c_strlen (src, 1); | |
10569 | if (! len || ! host_integerp (len, 1)) | |
10570 | { | |
10571 | /* If LEN is not constant, try MAXLEN too. | |
10572 | For MAXLEN only allow optimizing into non-_ocs function | |
10573 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
10574 | if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1)) | |
10575 | { | |
10576 | if (fcode == BUILT_IN_STPCPY_CHK) | |
10577 | { | |
10578 | if (! ignore) | |
10579 | return 0; | |
10580 | ||
10581 | /* If return value of __stpcpy_chk is ignored, | |
10582 | optimize into __strcpy_chk. */ | |
10583 | fn = built_in_decls[BUILT_IN_STRCPY_CHK]; | |
10584 | if (!fn) | |
10585 | return 0; | |
10586 | ||
10587 | return build_function_call_expr (fn, arglist); | |
10588 | } | |
10589 | ||
10590 | if (! len || TREE_SIDE_EFFECTS (len)) | |
10591 | return 0; | |
10592 | ||
10593 | /* If c_strlen returned something, but not a constant, | |
10594 | transform __strcpy_chk into __memcpy_chk. */ | |
10595 | fn = built_in_decls[BUILT_IN_MEMCPY_CHK]; | |
10596 | if (!fn) | |
10597 | return 0; | |
10598 | ||
10599 | len = size_binop (PLUS_EXPR, len, ssize_int (1)); | |
10600 | arglist = build_tree_list (NULL_TREE, size); | |
10601 | arglist = tree_cons (NULL_TREE, len, arglist); | |
10602 | arglist = tree_cons (NULL_TREE, src, arglist); | |
10603 | arglist = tree_cons (NULL_TREE, dest, arglist); | |
10604 | return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), | |
10605 | build_function_call_expr (fn, arglist)); | |
10606 | } | |
0a39fd54 | 10607 | } |
ad89623c | 10608 | else |
10609 | maxlen = len; | |
10610 | ||
10611 | if (! tree_int_cst_lt (maxlen, size)) | |
0a39fd54 | 10612 | return 0; |
10613 | } | |
10614 | ||
10615 | arglist = build_tree_list (NULL_TREE, src); | |
10616 | arglist = tree_cons (NULL_TREE, dest, arglist); | |
10617 | ||
10618 | /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */ | |
10619 | fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK | |
10620 | ? BUILT_IN_STPCPY : BUILT_IN_STRCPY]; | |
10621 | if (!fn) | |
10622 | return 0; | |
10623 | ||
10624 | return build_function_call_expr (fn, arglist); | |
10625 | } | |
10626 | ||
10627 | /* Fold a call to the __strncpy_chk builtin. | |
10628 | If MAXLEN is not NULL, it is maximum length passed as third argument. */ | |
10629 | ||
10630 | tree | |
10631 | fold_builtin_strncpy_chk (tree arglist, tree maxlen) | |
10632 | { | |
10633 | tree dest, src, size, len, fn; | |
10634 | ||
10635 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, | |
10636 | INTEGER_TYPE, VOID_TYPE)) | |
10637 | return 0; | |
10638 | ||
10639 | dest = TREE_VALUE (arglist); | |
10640 | src = TREE_VALUE (TREE_CHAIN (arglist)); | |
10641 | len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
10642 | size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist)))); | |
10643 | ||
10644 | if (! host_integerp (size, 1)) | |
10645 | return 0; | |
10646 | ||
10647 | if (! integer_all_onesp (size)) | |
10648 | { | |
10649 | if (! host_integerp (len, 1)) | |
10650 | { | |
10651 | /* If LEN is not constant, try MAXLEN too. | |
10652 | For MAXLEN only allow optimizing into non-_ocs function | |
10653 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
10654 | if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1)) | |
10655 | return 0; | |
0a39fd54 | 10656 | } |
ad89623c | 10657 | else |
10658 | maxlen = len; | |
0a39fd54 | 10659 | |
ad89623c | 10660 | if (tree_int_cst_lt (size, maxlen)) |
0a39fd54 | 10661 | return 0; |
10662 | } | |
10663 | ||
10664 | arglist = build_tree_list (NULL_TREE, len); | |
10665 | arglist = tree_cons (NULL_TREE, src, arglist); | |
10666 | arglist = tree_cons (NULL_TREE, dest, arglist); | |
10667 | ||
10668 | /* If __builtin_strncpy_chk is used, assume strncpy is available. */ | |
10669 | fn = built_in_decls[BUILT_IN_STRNCPY]; | |
10670 | if (!fn) | |
10671 | return 0; | |
10672 | ||
10673 | return build_function_call_expr (fn, arglist); | |
10674 | } | |
10675 | ||
10676 | /* Fold a call to the __strcat_chk builtin FNDECL with ARGLIST. */ | |
10677 | ||
10678 | static tree | |
10679 | fold_builtin_strcat_chk (tree fndecl, tree arglist) | |
10680 | { | |
10681 | tree dest, src, size, fn; | |
10682 | const char *p; | |
10683 | ||
10684 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, | |
10685 | VOID_TYPE)) | |
10686 | return 0; | |
10687 | ||
10688 | dest = TREE_VALUE (arglist); | |
10689 | src = TREE_VALUE (TREE_CHAIN (arglist)); | |
10690 | size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
10691 | ||
10692 | p = c_getstr (src); | |
10693 | /* If the SRC parameter is "", return DEST. */ | |
10694 | if (p && *p == '\0') | |
10695 | return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src); | |
10696 | ||
10697 | if (! host_integerp (size, 1) || ! integer_all_onesp (size)) | |
10698 | return 0; | |
10699 | ||
10700 | arglist = build_tree_list (NULL_TREE, src); | |
10701 | arglist = tree_cons (NULL_TREE, dest, arglist); | |
10702 | ||
10703 | /* If __builtin_strcat_chk is used, assume strcat is available. */ | |
10704 | fn = built_in_decls[BUILT_IN_STRCAT]; | |
10705 | if (!fn) | |
10706 | return 0; | |
10707 | ||
10708 | return build_function_call_expr (fn, arglist); | |
10709 | } | |
10710 | ||
10711 | /* Fold a call to the __strncat_chk builtin EXP. */ | |
10712 | ||
10713 | static tree | |
10714 | fold_builtin_strncat_chk (tree fndecl, tree arglist) | |
10715 | { | |
10716 | tree dest, src, size, len, fn; | |
10717 | const char *p; | |
10718 | ||
10719 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, | |
10720 | INTEGER_TYPE, VOID_TYPE)) | |
10721 | return 0; | |
10722 | ||
10723 | dest = TREE_VALUE (arglist); | |
10724 | src = TREE_VALUE (TREE_CHAIN (arglist)); | |
10725 | len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
10726 | size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist)))); | |
10727 | ||
10728 | p = c_getstr (src); | |
10729 | /* If the SRC parameter is "" or if LEN is 0, return DEST. */ | |
10730 | if (p && *p == '\0') | |
10731 | return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, len); | |
10732 | else if (integer_zerop (len)) | |
10733 | return omit_one_operand (TREE_TYPE (TREE_TYPE (fndecl)), dest, src); | |
10734 | ||
10735 | if (! host_integerp (size, 1)) | |
10736 | return 0; | |
10737 | ||
10738 | if (! integer_all_onesp (size)) | |
10739 | { | |
10740 | tree src_len = c_strlen (src, 1); | |
10741 | if (src_len | |
10742 | && host_integerp (src_len, 1) | |
10743 | && host_integerp (len, 1) | |
10744 | && ! tree_int_cst_lt (len, src_len)) | |
10745 | { | |
10746 | /* If LEN >= strlen (SRC), optimize into __strcat_chk. */ | |
10747 | fn = built_in_decls[BUILT_IN_STRCAT_CHK]; | |
10748 | if (!fn) | |
10749 | return 0; | |
10750 | ||
10751 | arglist = build_tree_list (NULL_TREE, size); | |
10752 | arglist = tree_cons (NULL_TREE, src, arglist); | |
10753 | arglist = tree_cons (NULL_TREE, dest, arglist); | |
10754 | return build_function_call_expr (fn, arglist); | |
10755 | } | |
10756 | return 0; | |
10757 | } | |
10758 | ||
10759 | arglist = build_tree_list (NULL_TREE, len); | |
10760 | arglist = tree_cons (NULL_TREE, src, arglist); | |
10761 | arglist = tree_cons (NULL_TREE, dest, arglist); | |
10762 | ||
10763 | /* If __builtin_strncat_chk is used, assume strncat is available. */ | |
10764 | fn = built_in_decls[BUILT_IN_STRNCAT]; | |
10765 | if (!fn) | |
10766 | return 0; | |
10767 | ||
10768 | return build_function_call_expr (fn, arglist); | |
10769 | } | |
10770 | ||
10771 | /* Fold a call to __{,v}sprintf_chk with argument list ARGLIST. Return 0 if | |
10772 | a normal call should be emitted rather than expanding the function | |
10773 | inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */ | |
10774 | ||
10775 | static tree | |
10776 | fold_builtin_sprintf_chk (tree arglist, enum built_in_function fcode) | |
10777 | { | |
10778 | tree dest, size, len, fn, fmt, flag; | |
10779 | const char *fmt_str; | |
10780 | ||
10781 | /* Verify the required arguments in the original call. */ | |
10782 | if (! arglist) | |
10783 | return 0; | |
10784 | dest = TREE_VALUE (arglist); | |
10785 | if (! POINTER_TYPE_P (TREE_TYPE (dest))) | |
10786 | return 0; | |
10787 | arglist = TREE_CHAIN (arglist); | |
10788 | if (! arglist) | |
10789 | return 0; | |
10790 | flag = TREE_VALUE (arglist); | |
10791 | if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE) | |
10792 | return 0; | |
10793 | arglist = TREE_CHAIN (arglist); | |
10794 | if (! arglist) | |
10795 | return 0; | |
10796 | size = TREE_VALUE (arglist); | |
10797 | if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE) | |
10798 | return 0; | |
10799 | arglist = TREE_CHAIN (arglist); | |
10800 | if (! arglist) | |
10801 | return 0; | |
10802 | fmt = TREE_VALUE (arglist); | |
10803 | if (! POINTER_TYPE_P (TREE_TYPE (fmt))) | |
10804 | return 0; | |
10805 | arglist = TREE_CHAIN (arglist); | |
10806 | ||
10807 | if (! host_integerp (size, 1)) | |
10808 | return 0; | |
10809 | ||
10810 | len = NULL_TREE; | |
10811 | ||
99eabcc1 | 10812 | if (!init_target_chars()) |
10813 | return 0; | |
10814 | ||
0a39fd54 | 10815 | /* Check whether the format is a literal string constant. */ |
10816 | fmt_str = c_getstr (fmt); | |
10817 | if (fmt_str != NULL) | |
10818 | { | |
10819 | /* If the format doesn't contain % args or %%, we know the size. */ | |
99eabcc1 | 10820 | if (strchr (fmt_str, target_percent) == 0) |
0a39fd54 | 10821 | { |
10822 | if (fcode != BUILT_IN_SPRINTF_CHK || arglist == NULL_TREE) | |
10823 | len = build_int_cstu (size_type_node, strlen (fmt_str)); | |
10824 | } | |
10825 | /* If the format is "%s" and first ... argument is a string literal, | |
10826 | we know the size too. */ | |
99eabcc1 | 10827 | else if (fcode == BUILT_IN_SPRINTF_CHK && strcmp (fmt_str, target_percent_s) == 0) |
0a39fd54 | 10828 | { |
10829 | tree arg; | |
10830 | ||
10831 | if (arglist && !TREE_CHAIN (arglist)) | |
10832 | { | |
10833 | arg = TREE_VALUE (arglist); | |
10834 | if (POINTER_TYPE_P (TREE_TYPE (arg))) | |
10835 | { | |
10836 | len = c_strlen (arg, 1); | |
10837 | if (! len || ! host_integerp (len, 1)) | |
10838 | len = NULL_TREE; | |
10839 | } | |
10840 | } | |
10841 | } | |
10842 | } | |
10843 | ||
10844 | if (! integer_all_onesp (size)) | |
10845 | { | |
10846 | if (! len || ! tree_int_cst_lt (len, size)) | |
10847 | return 0; | |
10848 | } | |
10849 | ||
10850 | /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0 | |
10851 | or if format doesn't contain % chars or is "%s". */ | |
10852 | if (! integer_zerop (flag)) | |
10853 | { | |
10854 | if (fmt_str == NULL) | |
10855 | return 0; | |
99eabcc1 | 10856 | if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s)) |
0a39fd54 | 10857 | return 0; |
10858 | } | |
10859 | ||
10860 | arglist = tree_cons (NULL_TREE, fmt, arglist); | |
10861 | arglist = tree_cons (NULL_TREE, dest, arglist); | |
10862 | ||
10863 | /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */ | |
10864 | fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK | |
10865 | ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF]; | |
10866 | if (!fn) | |
10867 | return 0; | |
10868 | ||
10869 | return build_function_call_expr (fn, arglist); | |
10870 | } | |
10871 | ||
10872 | /* Fold a call to {,v}snprintf with argument list ARGLIST. Return 0 if | |
10873 | a normal call should be emitted rather than expanding the function | |
10874 | inline. FCODE is either BUILT_IN_SNPRINTF_CHK or | |
10875 | BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length | |
10876 | passed as second argument. */ | |
10877 | ||
10878 | tree | |
10879 | fold_builtin_snprintf_chk (tree arglist, tree maxlen, | |
10880 | enum built_in_function fcode) | |
10881 | { | |
10882 | tree dest, size, len, fn, fmt, flag; | |
10883 | const char *fmt_str; | |
10884 | ||
10885 | /* Verify the required arguments in the original call. */ | |
10886 | if (! arglist) | |
10887 | return 0; | |
10888 | dest = TREE_VALUE (arglist); | |
10889 | if (! POINTER_TYPE_P (TREE_TYPE (dest))) | |
10890 | return 0; | |
10891 | arglist = TREE_CHAIN (arglist); | |
10892 | if (! arglist) | |
10893 | return 0; | |
10894 | len = TREE_VALUE (arglist); | |
10895 | if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE) | |
10896 | return 0; | |
10897 | arglist = TREE_CHAIN (arglist); | |
10898 | if (! arglist) | |
10899 | return 0; | |
10900 | flag = TREE_VALUE (arglist); | |
10901 | if (TREE_CODE (TREE_TYPE (len)) != INTEGER_TYPE) | |
10902 | return 0; | |
10903 | arglist = TREE_CHAIN (arglist); | |
10904 | if (! arglist) | |
10905 | return 0; | |
10906 | size = TREE_VALUE (arglist); | |
10907 | if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE) | |
10908 | return 0; | |
10909 | arglist = TREE_CHAIN (arglist); | |
10910 | if (! arglist) | |
10911 | return 0; | |
10912 | fmt = TREE_VALUE (arglist); | |
10913 | if (! POINTER_TYPE_P (TREE_TYPE (fmt))) | |
10914 | return 0; | |
10915 | arglist = TREE_CHAIN (arglist); | |
10916 | ||
10917 | if (! host_integerp (size, 1)) | |
10918 | return 0; | |
10919 | ||
10920 | if (! integer_all_onesp (size)) | |
10921 | { | |
10922 | if (! host_integerp (len, 1)) | |
10923 | { | |
10924 | /* If LEN is not constant, try MAXLEN too. | |
10925 | For MAXLEN only allow optimizing into non-_ocs function | |
10926 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
10927 | if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1)) | |
10928 | return 0; | |
0a39fd54 | 10929 | } |
ad89623c | 10930 | else |
10931 | maxlen = len; | |
0a39fd54 | 10932 | |
ad89623c | 10933 | if (tree_int_cst_lt (size, maxlen)) |
0a39fd54 | 10934 | return 0; |
10935 | } | |
10936 | ||
99eabcc1 | 10937 | if (!init_target_chars()) |
10938 | return 0; | |
10939 | ||
0a39fd54 | 10940 | /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0 |
10941 | or if format doesn't contain % chars or is "%s". */ | |
10942 | if (! integer_zerop (flag)) | |
10943 | { | |
10944 | fmt_str = c_getstr (fmt); | |
10945 | if (fmt_str == NULL) | |
10946 | return 0; | |
99eabcc1 | 10947 | if (strchr (fmt_str, target_percent) != NULL && strcmp (fmt_str, target_percent_s)) |
0a39fd54 | 10948 | return 0; |
10949 | } | |
10950 | ||
10951 | arglist = tree_cons (NULL_TREE, fmt, arglist); | |
10952 | arglist = tree_cons (NULL_TREE, len, arglist); | |
10953 | arglist = tree_cons (NULL_TREE, dest, arglist); | |
10954 | ||
10955 | /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is | |
10956 | available. */ | |
10957 | fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK | |
10958 | ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF]; | |
10959 | if (!fn) | |
10960 | return 0; | |
10961 | ||
10962 | return build_function_call_expr (fn, arglist); | |
10963 | } | |
10964 | ||
10965 | /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins. | |
10966 | ||
10967 | Return 0 if no simplification was possible, otherwise return the | |
10968 | simplified form of the call as a tree. FCODE is the BUILT_IN_* | |
10969 | code of the function to be simplified. */ | |
10970 | ||
10971 | static tree | |
10972 | fold_builtin_printf (tree fndecl, tree arglist, bool ignore, | |
10973 | enum built_in_function fcode) | |
10974 | { | |
10975 | tree fmt, fn = NULL_TREE, fn_putchar, fn_puts, arg, call; | |
10976 | const char *fmt_str = NULL; | |
10977 | ||
10978 | /* If the return value is used, don't do the transformation. */ | |
10979 | if (! ignore) | |
10980 | return 0; | |
10981 | ||
10982 | /* Verify the required arguments in the original call. */ | |
10983 | if (fcode == BUILT_IN_PRINTF_CHK || fcode == BUILT_IN_VPRINTF_CHK) | |
10984 | { | |
10985 | tree flag; | |
10986 | ||
10987 | if (! arglist) | |
10988 | return 0; | |
10989 | flag = TREE_VALUE (arglist); | |
10990 | if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE | |
10991 | || TREE_SIDE_EFFECTS (flag)) | |
10992 | return 0; | |
10993 | arglist = TREE_CHAIN (arglist); | |
10994 | } | |
10995 | ||
10996 | if (! arglist) | |
10997 | return 0; | |
10998 | fmt = TREE_VALUE (arglist); | |
10999 | if (! POINTER_TYPE_P (TREE_TYPE (fmt))) | |
11000 | return 0; | |
11001 | arglist = TREE_CHAIN (arglist); | |
11002 | ||
11003 | /* Check whether the format is a literal string constant. */ | |
11004 | fmt_str = c_getstr (fmt); | |
11005 | if (fmt_str == NULL) | |
11006 | return NULL_TREE; | |
11007 | ||
11008 | if (fcode == BUILT_IN_PRINTF_UNLOCKED) | |
11009 | { | |
6d77ed92 | 11010 | /* If we're using an unlocked function, assume the other |
11011 | unlocked functions exist explicitly. */ | |
11012 | fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED]; | |
11013 | fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED]; | |
0a39fd54 | 11014 | } |
11015 | else | |
11016 | { | |
11017 | fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR]; | |
11018 | fn_puts = implicit_built_in_decls[BUILT_IN_PUTS]; | |
11019 | } | |
11020 | ||
99eabcc1 | 11021 | if (!init_target_chars()) |
11022 | return 0; | |
a0c938f0 | 11023 | |
99eabcc1 | 11024 | if (strcmp (fmt_str, target_percent_s) == 0 || strchr (fmt_str, target_percent) == NULL) |
0a39fd54 | 11025 | { |
11026 | const char *str; | |
11027 | ||
99eabcc1 | 11028 | if (strcmp (fmt_str, target_percent_s) == 0) |
0a39fd54 | 11029 | { |
11030 | if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK) | |
11031 | return 0; | |
11032 | ||
11033 | if (! arglist | |
11034 | || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))) | |
11035 | || TREE_CHAIN (arglist)) | |
11036 | return 0; | |
11037 | ||
11038 | str = c_getstr (TREE_VALUE (arglist)); | |
11039 | if (str == NULL) | |
11040 | return 0; | |
11041 | } | |
11042 | else | |
11043 | { | |
11044 | /* The format specifier doesn't contain any '%' characters. */ | |
11045 | if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK | |
11046 | && arglist) | |
11047 | return 0; | |
11048 | str = fmt_str; | |
11049 | } | |
11050 | ||
11051 | /* If the string was "", printf does nothing. */ | |
11052 | if (str[0] == '\0') | |
11053 | return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0); | |
11054 | ||
11055 | /* If the string has length of 1, call putchar. */ | |
11056 | if (str[1] == '\0') | |
11057 | { | |
11058 | /* Given printf("c"), (where c is any one character,) | |
11059 | convert "c"[0] to an int and pass that to the replacement | |
11060 | function. */ | |
11061 | arg = build_int_cst (NULL_TREE, str[0]); | |
11062 | arglist = build_tree_list (NULL_TREE, arg); | |
11063 | fn = fn_putchar; | |
11064 | } | |
11065 | else | |
11066 | { | |
11067 | /* If the string was "string\n", call puts("string"). */ | |
11068 | size_t len = strlen (str); | |
99eabcc1 | 11069 | if ((unsigned char)str[len - 1] == target_newline) |
0a39fd54 | 11070 | { |
11071 | /* Create a NUL-terminated string that's one char shorter | |
11072 | than the original, stripping off the trailing '\n'. */ | |
11073 | char *newstr = alloca (len); | |
11074 | memcpy (newstr, str, len - 1); | |
11075 | newstr[len - 1] = 0; | |
11076 | ||
11077 | arg = build_string_literal (len, newstr); | |
11078 | arglist = build_tree_list (NULL_TREE, arg); | |
11079 | fn = fn_puts; | |
11080 | } | |
11081 | else | |
11082 | /* We'd like to arrange to call fputs(string,stdout) here, | |
11083 | but we need stdout and don't have a way to get it yet. */ | |
11084 | return 0; | |
11085 | } | |
11086 | } | |
11087 | ||
11088 | /* The other optimizations can be done only on the non-va_list variants. */ | |
11089 | else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK) | |
11090 | return 0; | |
11091 | ||
11092 | /* If the format specifier was "%s\n", call __builtin_puts(arg). */ | |
99eabcc1 | 11093 | else if (strcmp (fmt_str, target_percent_s_newline) == 0) |
0a39fd54 | 11094 | { |
11095 | if (! arglist | |
11096 | || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))) | |
11097 | || TREE_CHAIN (arglist)) | |
11098 | return 0; | |
11099 | fn = fn_puts; | |
11100 | } | |
11101 | ||
11102 | /* If the format specifier was "%c", call __builtin_putchar(arg). */ | |
99eabcc1 | 11103 | else if (strcmp (fmt_str, target_percent_c) == 0) |
0a39fd54 | 11104 | { |
11105 | if (! arglist | |
11106 | || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE | |
11107 | || TREE_CHAIN (arglist)) | |
11108 | return 0; | |
11109 | fn = fn_putchar; | |
11110 | } | |
11111 | ||
11112 | if (!fn) | |
11113 | return 0; | |
11114 | ||
11115 | call = build_function_call_expr (fn, arglist); | |
11116 | return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call); | |
11117 | } | |
11118 | ||
11119 | /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins. | |
11120 | ||
11121 | Return 0 if no simplification was possible, otherwise return the | |
11122 | simplified form of the call as a tree. FCODE is the BUILT_IN_* | |
11123 | code of the function to be simplified. */ | |
11124 | ||
11125 | static tree | |
11126 | fold_builtin_fprintf (tree fndecl, tree arglist, bool ignore, | |
11127 | enum built_in_function fcode) | |
11128 | { | |
11129 | tree fp, fmt, fn = NULL_TREE, fn_fputc, fn_fputs, arg, call; | |
11130 | const char *fmt_str = NULL; | |
11131 | ||
11132 | /* If the return value is used, don't do the transformation. */ | |
11133 | if (! ignore) | |
11134 | return 0; | |
11135 | ||
11136 | /* Verify the required arguments in the original call. */ | |
11137 | if (! arglist) | |
11138 | return 0; | |
11139 | fp = TREE_VALUE (arglist); | |
11140 | if (! POINTER_TYPE_P (TREE_TYPE (fp))) | |
11141 | return 0; | |
11142 | arglist = TREE_CHAIN (arglist); | |
11143 | ||
11144 | if (fcode == BUILT_IN_FPRINTF_CHK || fcode == BUILT_IN_VFPRINTF_CHK) | |
11145 | { | |
11146 | tree flag; | |
11147 | ||
11148 | if (! arglist) | |
11149 | return 0; | |
11150 | flag = TREE_VALUE (arglist); | |
11151 | if (TREE_CODE (TREE_TYPE (flag)) != INTEGER_TYPE | |
11152 | || TREE_SIDE_EFFECTS (flag)) | |
11153 | return 0; | |
11154 | arglist = TREE_CHAIN (arglist); | |
11155 | } | |
11156 | ||
11157 | if (! arglist) | |
11158 | return 0; | |
11159 | fmt = TREE_VALUE (arglist); | |
11160 | if (! POINTER_TYPE_P (TREE_TYPE (fmt))) | |
11161 | return 0; | |
11162 | arglist = TREE_CHAIN (arglist); | |
11163 | ||
11164 | /* Check whether the format is a literal string constant. */ | |
11165 | fmt_str = c_getstr (fmt); | |
11166 | if (fmt_str == NULL) | |
11167 | return NULL_TREE; | |
11168 | ||
11169 | if (fcode == BUILT_IN_FPRINTF_UNLOCKED) | |
11170 | { | |
6d77ed92 | 11171 | /* If we're using an unlocked function, assume the other |
11172 | unlocked functions exist explicitly. */ | |
11173 | fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED]; | |
11174 | fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED]; | |
0a39fd54 | 11175 | } |
11176 | else | |
11177 | { | |
11178 | fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC]; | |
11179 | fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS]; | |
11180 | } | |
11181 | ||
99eabcc1 | 11182 | if (!init_target_chars()) |
11183 | return 0; | |
a0c938f0 | 11184 | |
0a39fd54 | 11185 | /* If the format doesn't contain % args or %%, use strcpy. */ |
99eabcc1 | 11186 | if (strchr (fmt_str, target_percent) == NULL) |
0a39fd54 | 11187 | { |
11188 | if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK | |
11189 | && arglist) | |
11190 | return 0; | |
11191 | ||
11192 | /* If the format specifier was "", fprintf does nothing. */ | |
11193 | if (fmt_str[0] == '\0') | |
11194 | { | |
11195 | /* If FP has side-effects, just wait until gimplification is | |
11196 | done. */ | |
11197 | if (TREE_SIDE_EFFECTS (fp)) | |
11198 | return 0; | |
11199 | ||
11200 | return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0); | |
11201 | } | |
11202 | ||
11203 | /* When "string" doesn't contain %, replace all cases of | |
11204 | fprintf (fp, string) with fputs (string, fp). The fputs | |
11205 | builtin will take care of special cases like length == 1. */ | |
11206 | arglist = build_tree_list (NULL_TREE, fp); | |
11207 | arglist = tree_cons (NULL_TREE, fmt, arglist); | |
11208 | fn = fn_fputs; | |
11209 | } | |
11210 | ||
11211 | /* The other optimizations can be done only on the non-va_list variants. */ | |
11212 | else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK) | |
11213 | return 0; | |
11214 | ||
11215 | /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */ | |
99eabcc1 | 11216 | else if (strcmp (fmt_str, target_percent_s) == 0) |
0a39fd54 | 11217 | { |
11218 | if (! arglist | |
11219 | || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist))) | |
11220 | || TREE_CHAIN (arglist)) | |
11221 | return 0; | |
11222 | arg = TREE_VALUE (arglist); | |
11223 | arglist = build_tree_list (NULL_TREE, fp); | |
11224 | arglist = tree_cons (NULL_TREE, arg, arglist); | |
11225 | fn = fn_fputs; | |
11226 | } | |
11227 | ||
11228 | /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */ | |
99eabcc1 | 11229 | else if (strcmp (fmt_str, target_percent_c) == 0) |
0a39fd54 | 11230 | { |
11231 | if (! arglist | |
11232 | || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE | |
11233 | || TREE_CHAIN (arglist)) | |
11234 | return 0; | |
11235 | arg = TREE_VALUE (arglist); | |
11236 | arglist = build_tree_list (NULL_TREE, fp); | |
11237 | arglist = tree_cons (NULL_TREE, arg, arglist); | |
11238 | fn = fn_fputc; | |
11239 | } | |
11240 | ||
11241 | if (!fn) | |
11242 | return 0; | |
11243 | ||
11244 | call = build_function_call_expr (fn, arglist); | |
11245 | return fold_convert (TREE_TYPE (TREE_TYPE (fndecl)), call); | |
11246 | } | |
99eabcc1 | 11247 | |
11248 | /* Initialize format string characters in the target charset. */ | |
11249 | ||
11250 | static bool | |
11251 | init_target_chars (void) | |
11252 | { | |
11253 | static bool init; | |
11254 | if (!init) | |
11255 | { | |
11256 | target_newline = lang_hooks.to_target_charset ('\n'); | |
11257 | target_percent = lang_hooks.to_target_charset ('%'); | |
11258 | target_c = lang_hooks.to_target_charset ('c'); | |
11259 | target_s = lang_hooks.to_target_charset ('s'); | |
11260 | if (target_newline == 0 || target_percent == 0 || target_c == 0 | |
11261 | || target_s == 0) | |
11262 | return false; | |
11263 | ||
11264 | target_percent_c[0] = target_percent; | |
11265 | target_percent_c[1] = target_c; | |
11266 | target_percent_c[2] = '\0'; | |
11267 | ||
11268 | target_percent_s[0] = target_percent; | |
11269 | target_percent_s[1] = target_s; | |
11270 | target_percent_s[2] = '\0'; | |
11271 | ||
11272 | target_percent_s_newline[0] = target_percent; | |
11273 | target_percent_s_newline[1] = target_s; | |
11274 | target_percent_s_newline[2] = target_newline; | |
11275 | target_percent_s_newline[3] = '\0'; | |
a0c938f0 | 11276 | |
99eabcc1 | 11277 | init = true; |
11278 | } | |
11279 | return true; | |
11280 | } |