]>
Commit | Line | Data |
---|---|---|
53800dbe | 1 | /* Expand builtin functions. |
61f1dccc | 2 | Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
3 | 2000, 2001, 2002, 2003 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 |
19 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
20 | 02111-1307, 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" | |
53800dbe | 30 | #include "flags.h" |
31 | #include "regs.h" | |
32 | #include "hard-reg-set.h" | |
33 | #include "except.h" | |
34 | #include "function.h" | |
53800dbe | 35 | #include "insn-config.h" |
36 | #include "expr.h" | |
d8fc4d0b | 37 | #include "optabs.h" |
38 | #include "libfuncs.h" | |
53800dbe | 39 | #include "recog.h" |
40 | #include "output.h" | |
41 | #include "typeclass.h" | |
53800dbe | 42 | #include "toplev.h" |
689df48e | 43 | #include "predict.h" |
1dd6c958 | 44 | #include "tm_p.h" |
fc2a2dcb | 45 | #include "target.h" |
63c62881 | 46 | #include "langhooks.h" |
53800dbe | 47 | |
48 | #define CALLED_AS_BUILT_IN(NODE) \ | |
49 | (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10)) | |
50 | ||
53800dbe | 51 | /* Register mappings for target machines without register windows. */ |
52 | #ifndef INCOMING_REGNO | |
53 | #define INCOMING_REGNO(OUT) (OUT) | |
54 | #endif | |
55 | #ifndef OUTGOING_REGNO | |
56 | #define OUTGOING_REGNO(IN) (IN) | |
57 | #endif | |
58 | ||
726e2588 | 59 | #ifndef PAD_VARARGS_DOWN |
60 | #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN | |
61 | #endif | |
62 | ||
ab7943b9 | 63 | /* Define the names of the builtin function types and codes. */ |
8934cb0c | 64 | const char *const built_in_class_names[4] |
ab7943b9 | 65 | = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"}; |
66 | ||
18e43155 | 67 | #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM) #X, |
8934cb0c | 68 | const char *const built_in_names[(int) END_BUILTINS] = |
4e9d90c7 | 69 | { |
70 | #include "builtins.def" | |
71 | }; | |
72 | #undef DEF_BUILTIN | |
ab7943b9 | 73 | |
df94cd3b | 74 | /* Setup an array of _DECL trees, make sure each element is |
75 | initialized to NULL_TREE. */ | |
d2d4bdde | 76 | tree built_in_decls[(int) END_BUILTINS]; |
0a68165a | 77 | /* Declarations used when constructing the builtin implicitly in the compiler. |
78 | It may be NULL_TREE when this is invalid (for instance runtime is not | |
79 | required to implement the function call in all cases. */ | |
80 | tree implicit_built_in_decls[(int) END_BUILTINS]; | |
df94cd3b | 81 | |
98b40778 | 82 | /* Trigonometric and mathematical constants used in builtin folding. */ |
83 | static bool builtin_dconsts_init = 0; | |
84 | static REAL_VALUE_TYPE dconstpi; | |
85 | static REAL_VALUE_TYPE dconste; | |
86 | ||
aecda0d6 | 87 | static int get_pointer_alignment (tree, unsigned int); |
681fab1e | 88 | static tree c_strlen (tree, int); |
aecda0d6 | 89 | static const char *c_getstr (tree); |
90 | static rtx c_readstr (const char *, enum machine_mode); | |
91 | static int target_char_cast (tree, char *); | |
92 | static rtx get_memory_rtx (tree); | |
19bf118a | 93 | static tree build_string_literal (int, const char *); |
aecda0d6 | 94 | static int apply_args_size (void); |
95 | static int apply_result_size (void); | |
d8c9779c | 96 | #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return) |
aecda0d6 | 97 | static rtx result_vector (int, rtx); |
d8c9779c | 98 | #endif |
aecda0d6 | 99 | static rtx expand_builtin_setjmp (tree, rtx); |
100 | static void expand_builtin_prefetch (tree); | |
101 | static rtx expand_builtin_apply_args (void); | |
102 | static rtx expand_builtin_apply_args_1 (void); | |
103 | static rtx expand_builtin_apply (rtx, rtx, rtx); | |
104 | static void expand_builtin_return (rtx); | |
105 | static enum type_class type_to_class (tree); | |
106 | static rtx expand_builtin_classify_type (tree); | |
107 | static void expand_errno_check (tree, rtx); | |
108 | static rtx expand_builtin_mathfn (tree, rtx, rtx); | |
109 | static rtx expand_builtin_mathfn_2 (tree, rtx, rtx); | |
110 | static rtx expand_builtin_constant_p (tree, enum machine_mode); | |
111 | static rtx expand_builtin_args_info (tree); | |
112 | static rtx expand_builtin_next_arg (tree); | |
113 | static rtx expand_builtin_va_start (tree); | |
114 | static rtx expand_builtin_va_end (tree); | |
115 | static rtx expand_builtin_va_copy (tree); | |
116 | static rtx expand_builtin_memcmp (tree, tree, rtx, enum machine_mode); | |
117 | static rtx expand_builtin_strcmp (tree, rtx, enum machine_mode); | |
118 | static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode); | |
119 | static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode); | |
120 | static rtx expand_builtin_strcat (tree, rtx, enum machine_mode); | |
121 | static rtx expand_builtin_strncat (tree, rtx, enum machine_mode); | |
122 | static rtx expand_builtin_strspn (tree, rtx, enum machine_mode); | |
123 | static rtx expand_builtin_strcspn (tree, rtx, enum machine_mode); | |
124 | static rtx expand_builtin_memcpy (tree, rtx, enum machine_mode); | |
125 | static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode, int); | |
126 | static rtx expand_builtin_memmove (tree, rtx, enum machine_mode); | |
127 | static rtx expand_builtin_bcopy (tree); | |
128 | static rtx expand_builtin_strcpy (tree, rtx, enum machine_mode); | |
129 | static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode); | |
130 | static rtx builtin_strncpy_read_str (void *, HOST_WIDE_INT, enum machine_mode); | |
131 | static rtx expand_builtin_strncpy (tree, rtx, enum machine_mode); | |
132 | static rtx builtin_memset_read_str (void *, HOST_WIDE_INT, enum machine_mode); | |
133 | static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode); | |
134 | static rtx expand_builtin_memset (tree, rtx, enum machine_mode); | |
135 | static rtx expand_builtin_bzero (tree); | |
136 | static rtx expand_builtin_strlen (tree, rtx, enum machine_mode); | |
137 | static rtx expand_builtin_strstr (tree, rtx, enum machine_mode); | |
138 | static rtx expand_builtin_strpbrk (tree, rtx, enum machine_mode); | |
139 | static rtx expand_builtin_strchr (tree, rtx, enum machine_mode); | |
140 | static rtx expand_builtin_strrchr (tree, rtx, enum machine_mode); | |
141 | static rtx expand_builtin_alloca (tree, rtx); | |
142 | static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab); | |
143 | static rtx expand_builtin_frame_address (tree, tree); | |
19bf118a | 144 | static rtx expand_builtin_fputs (tree, rtx, bool); |
145 | static rtx expand_builtin_printf (tree, rtx, enum machine_mode, bool); | |
146 | static rtx expand_builtin_fprintf (tree, rtx, enum machine_mode, bool); | |
6411575e | 147 | static rtx expand_builtin_sprintf (tree, rtx, enum machine_mode); |
aecda0d6 | 148 | static tree stabilize_va_list (tree, int); |
149 | static rtx expand_builtin_expect (tree, rtx); | |
150 | static tree fold_builtin_constant_p (tree); | |
151 | static tree fold_builtin_classify_type (tree); | |
152 | static tree fold_builtin_inf (tree, int); | |
153 | static tree fold_builtin_nan (tree, tree, int); | |
154 | static int validate_arglist (tree, ...); | |
277f8dd2 | 155 | static bool integer_valued_real_p (tree); |
aecda0d6 | 156 | static tree fold_trunc_transparent_mathfn (tree); |
157 | static bool readonly_data_expr (tree); | |
158 | static rtx expand_builtin_fabs (tree, rtx, rtx); | |
159 | static rtx expand_builtin_cabs (tree, rtx); | |
160 | static void init_builtin_dconsts (void); | |
161 | static tree fold_builtin_cabs (tree, tree, tree); | |
277f8dd2 | 162 | static tree fold_builtin_trunc (tree); |
163 | static tree fold_builtin_floor (tree); | |
164 | static tree fold_builtin_ceil (tree); | |
70fb4c07 | 165 | static tree fold_builtin_bitop (tree); |
98b40778 | 166 | |
167 | /* Initialize mathematical constants for constant folding builtins. | |
dfcd8f35 | 168 | These constants need to be given to at least 160 bits precision. */ |
98b40778 | 169 | |
170 | static void | |
aecda0d6 | 171 | init_builtin_dconsts (void) |
98b40778 | 172 | { |
173 | real_from_string (&dconstpi, | |
174 | "3.1415926535897932384626433832795028841971693993751058209749445923078"); | |
175 | real_from_string (&dconste, | |
176 | "2.7182818284590452353602874713526624977572470936999595749669676277241"); | |
53800dbe | 177 | |
98b40778 | 178 | builtin_dconsts_init = true; |
179 | } | |
aecda0d6 | 180 | |
53800dbe | 181 | /* Return the alignment in bits of EXP, a pointer valued expression. |
182 | But don't return more than MAX_ALIGN no matter what. | |
183 | The alignment returned is, by default, the alignment of the thing that | |
27d0c333 | 184 | EXP points to. If it is not a POINTER_TYPE, 0 is returned. |
53800dbe | 185 | |
186 | Otherwise, look at the expression to see if we can do better, i.e., if the | |
187 | expression is actually pointing at an object whose alignment is tighter. */ | |
188 | ||
189 | static int | |
aecda0d6 | 190 | get_pointer_alignment (tree exp, unsigned int max_align) |
53800dbe | 191 | { |
27d0c333 | 192 | unsigned int align, inner; |
53800dbe | 193 | |
194 | if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE) | |
195 | return 0; | |
196 | ||
197 | align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp))); | |
198 | align = MIN (align, max_align); | |
199 | ||
200 | while (1) | |
201 | { | |
202 | switch (TREE_CODE (exp)) | |
203 | { | |
204 | case NOP_EXPR: | |
205 | case CONVERT_EXPR: | |
206 | case NON_LVALUE_EXPR: | |
207 | exp = TREE_OPERAND (exp, 0); | |
208 | if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE) | |
209 | return align; | |
325d1c45 | 210 | |
53800dbe | 211 | inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp))); |
212 | align = MIN (inner, max_align); | |
213 | break; | |
214 | ||
215 | case PLUS_EXPR: | |
216 | /* If sum of pointer + int, restrict our maximum alignment to that | |
217 | imposed by the integer. If not, we can't do any better than | |
218 | ALIGN. */ | |
325d1c45 | 219 | if (! host_integerp (TREE_OPERAND (exp, 1), 1)) |
53800dbe | 220 | return align; |
221 | ||
0994d2ed | 222 | while (((tree_low_cst (TREE_OPERAND (exp, 1), 1)) |
223 | & (max_align / BITS_PER_UNIT - 1)) | |
53800dbe | 224 | != 0) |
225 | max_align >>= 1; | |
226 | ||
227 | exp = TREE_OPERAND (exp, 0); | |
228 | break; | |
229 | ||
230 | case ADDR_EXPR: | |
231 | /* See what we are pointing at and look at its alignment. */ | |
232 | exp = TREE_OPERAND (exp, 0); | |
233 | if (TREE_CODE (exp) == FUNCTION_DECL) | |
234 | align = FUNCTION_BOUNDARY; | |
9308e976 | 235 | else if (DECL_P (exp)) |
53800dbe | 236 | align = DECL_ALIGN (exp); |
237 | #ifdef CONSTANT_ALIGNMENT | |
238 | else if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c') | |
239 | align = CONSTANT_ALIGNMENT (exp, align); | |
240 | #endif | |
241 | return MIN (align, max_align); | |
242 | ||
243 | default: | |
244 | return align; | |
245 | } | |
246 | } | |
247 | } | |
248 | ||
249 | /* Compute the length of a C string. TREE_STRING_LENGTH is not the right | |
250 | way, because it could contain a zero byte in the middle. | |
251 | TREE_STRING_LENGTH is the size of the character array, not the string. | |
252 | ||
4172d65e | 253 | ONLY_VALUE should be nonzero if the result is not going to be emitted |
c09841f6 | 254 | into the instruction stream and zero if it is going to be expanded. |
4172d65e | 255 | E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3 |
681fab1e | 256 | is returned, otherwise NULL, since |
257 | len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not | |
258 | evaluate the side-effects. | |
259 | ||
902de8ed | 260 | The value returned is of type `ssizetype'. |
261 | ||
53800dbe | 262 | Unfortunately, string_constant can't access the values of const char |
263 | arrays with initializers, so neither can we do so here. */ | |
264 | ||
265 | static tree | |
681fab1e | 266 | c_strlen (tree src, int only_value) |
53800dbe | 267 | { |
268 | tree offset_node; | |
27d0c333 | 269 | HOST_WIDE_INT offset; |
270 | int max; | |
44acf429 | 271 | const char *ptr; |
53800dbe | 272 | |
681fab1e | 273 | STRIP_NOPS (src); |
274 | if (TREE_CODE (src) == COND_EXPR | |
275 | && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0)))) | |
276 | { | |
277 | tree len1, len2; | |
278 | ||
279 | len1 = c_strlen (TREE_OPERAND (src, 1), only_value); | |
280 | len2 = c_strlen (TREE_OPERAND (src, 2), only_value); | |
281 | if (tree_int_cst_equal (len1, len2)) | |
282 | return len1; | |
283 | } | |
284 | ||
285 | if (TREE_CODE (src) == COMPOUND_EXPR | |
286 | && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0)))) | |
287 | return c_strlen (TREE_OPERAND (src, 1), only_value); | |
288 | ||
53800dbe | 289 | src = string_constant (src, &offset_node); |
290 | if (src == 0) | |
291 | return 0; | |
902de8ed | 292 | |
83d79705 | 293 | max = TREE_STRING_LENGTH (src) - 1; |
53800dbe | 294 | ptr = TREE_STRING_POINTER (src); |
902de8ed | 295 | |
53800dbe | 296 | if (offset_node && TREE_CODE (offset_node) != INTEGER_CST) |
297 | { | |
298 | /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't | |
299 | compute the offset to the following null if we don't know where to | |
300 | start searching for it. */ | |
301 | int i; | |
902de8ed | 302 | |
53800dbe | 303 | for (i = 0; i < max; i++) |
304 | if (ptr[i] == 0) | |
305 | return 0; | |
902de8ed | 306 | |
53800dbe | 307 | /* We don't know the starting offset, but we do know that the string |
308 | has no internal zero bytes. We can assume that the offset falls | |
309 | within the bounds of the string; otherwise, the programmer deserves | |
310 | what he gets. Subtract the offset from the length of the string, | |
902de8ed | 311 | and return that. This would perhaps not be valid if we were dealing |
312 | with named arrays in addition to literal string constants. */ | |
313 | ||
314 | return size_diffop (size_int (max), offset_node); | |
53800dbe | 315 | } |
316 | ||
317 | /* We have a known offset into the string. Start searching there for | |
27d0c333 | 318 | a null character if we can represent it as a single HOST_WIDE_INT. */ |
dabc4084 | 319 | if (offset_node == 0) |
53800dbe | 320 | offset = 0; |
dabc4084 | 321 | else if (! host_integerp (offset_node, 0)) |
322 | offset = -1; | |
53800dbe | 323 | else |
27d0c333 | 324 | offset = tree_low_cst (offset_node, 0); |
902de8ed | 325 | |
53800dbe | 326 | /* If the offset is known to be out of bounds, warn, and call strlen at |
327 | runtime. */ | |
328 | if (offset < 0 || offset > max) | |
329 | { | |
330 | warning ("offset outside bounds of constant string"); | |
331 | return 0; | |
332 | } | |
902de8ed | 333 | |
53800dbe | 334 | /* Use strlen to search for the first zero byte. Since any strings |
335 | constructed with build_string will have nulls appended, we win even | |
336 | if we get handed something like (char[4])"abcd". | |
337 | ||
338 | Since OFFSET is our starting index into the string, no further | |
339 | calculation is needed. */ | |
902de8ed | 340 | return ssize_int (strlen (ptr + offset)); |
53800dbe | 341 | } |
342 | ||
83d79705 | 343 | /* Return a char pointer for a C string if it is a string constant |
344 | or sum of string constant and integer constant. */ | |
345 | ||
346 | static const char * | |
aecda0d6 | 347 | c_getstr (tree src) |
83d79705 | 348 | { |
349 | tree offset_node; | |
83d79705 | 350 | |
351 | src = string_constant (src, &offset_node); | |
352 | if (src == 0) | |
353 | return 0; | |
354 | ||
8c85fcb7 | 355 | if (offset_node == 0) |
356 | return TREE_STRING_POINTER (src); | |
357 | else if (!host_integerp (offset_node, 1) | |
358 | || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0) | |
83d79705 | 359 | return 0; |
83d79705 | 360 | |
8c85fcb7 | 361 | return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1); |
83d79705 | 362 | } |
363 | ||
8c85fcb7 | 364 | /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading |
365 | GET_MODE_BITSIZE (MODE) bits from string constant STR. */ | |
ecc318ff | 366 | |
6840589f | 367 | static rtx |
aecda0d6 | 368 | c_readstr (const char *str, enum machine_mode mode) |
6840589f | 369 | { |
370 | HOST_WIDE_INT c[2]; | |
371 | HOST_WIDE_INT ch; | |
372 | unsigned int i, j; | |
373 | ||
374 | if (GET_MODE_CLASS (mode) != MODE_INT) | |
375 | abort (); | |
376 | c[0] = 0; | |
377 | c[1] = 0; | |
378 | ch = 1; | |
379 | for (i = 0; i < GET_MODE_SIZE (mode); i++) | |
380 | { | |
381 | j = i; | |
382 | if (WORDS_BIG_ENDIAN) | |
383 | j = GET_MODE_SIZE (mode) - i - 1; | |
384 | if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN | |
385 | && GET_MODE_SIZE (mode) > UNITS_PER_WORD) | |
386 | j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1; | |
387 | j *= BITS_PER_UNIT; | |
388 | if (j > 2 * HOST_BITS_PER_WIDE_INT) | |
389 | abort (); | |
390 | if (ch) | |
391 | ch = (unsigned char) str[i]; | |
392 | c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT); | |
393 | } | |
394 | return immed_double_const (c[0], c[1], mode); | |
395 | } | |
396 | ||
ecc318ff | 397 | /* Cast a target constant CST to target CHAR and if that value fits into |
398 | host char type, return zero and put that value into variable pointed by | |
399 | P. */ | |
400 | ||
401 | static int | |
aecda0d6 | 402 | target_char_cast (tree cst, char *p) |
ecc318ff | 403 | { |
404 | unsigned HOST_WIDE_INT val, hostval; | |
405 | ||
27d0c333 | 406 | if (!host_integerp (cst, 1) |
ecc318ff | 407 | || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT) |
408 | return 1; | |
409 | ||
27d0c333 | 410 | val = tree_low_cst (cst, 1); |
ecc318ff | 411 | if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT) |
412 | val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1; | |
413 | ||
414 | hostval = val; | |
415 | if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT) | |
416 | hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1; | |
417 | ||
418 | if (val != hostval) | |
419 | return 1; | |
420 | ||
421 | *p = hostval; | |
422 | return 0; | |
423 | } | |
424 | ||
53800dbe | 425 | /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT |
426 | times to get the address of either a higher stack frame, or a return | |
427 | address located within it (depending on FNDECL_CODE). */ | |
902de8ed | 428 | |
53800dbe | 429 | rtx |
aecda0d6 | 430 | expand_builtin_return_addr (enum built_in_function fndecl_code, int count, |
431 | rtx tem) | |
53800dbe | 432 | { |
433 | int i; | |
434 | ||
435 | /* Some machines need special handling before we can access | |
436 | arbitrary frames. For example, on the sparc, we must first flush | |
437 | all register windows to the stack. */ | |
438 | #ifdef SETUP_FRAME_ADDRESSES | |
439 | if (count > 0) | |
440 | SETUP_FRAME_ADDRESSES (); | |
441 | #endif | |
442 | ||
443 | /* On the sparc, the return address is not in the frame, it is in a | |
444 | register. There is no way to access it off of the current frame | |
445 | pointer, but it can be accessed off the previous frame pointer by | |
446 | reading the value from the register window save area. */ | |
447 | #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME | |
448 | if (fndecl_code == BUILT_IN_RETURN_ADDRESS) | |
449 | count--; | |
450 | #endif | |
451 | ||
452 | /* Scan back COUNT frames to the specified frame. */ | |
453 | for (i = 0; i < count; i++) | |
454 | { | |
455 | /* Assume the dynamic chain pointer is in the word that the | |
456 | frame address points to, unless otherwise specified. */ | |
457 | #ifdef DYNAMIC_CHAIN_ADDRESS | |
458 | tem = DYNAMIC_CHAIN_ADDRESS (tem); | |
459 | #endif | |
460 | tem = memory_address (Pmode, tem); | |
83fc1478 | 461 | tem = gen_rtx_MEM (Pmode, tem); |
ab6ab77e | 462 | set_mem_alias_set (tem, get_frame_alias_set ()); |
83fc1478 | 463 | tem = copy_to_reg (tem); |
53800dbe | 464 | } |
465 | ||
466 | /* For __builtin_frame_address, return what we've got. */ | |
467 | if (fndecl_code == BUILT_IN_FRAME_ADDRESS) | |
468 | return tem; | |
469 | ||
470 | /* For __builtin_return_address, Get the return address from that | |
471 | frame. */ | |
472 | #ifdef RETURN_ADDR_RTX | |
473 | tem = RETURN_ADDR_RTX (count, tem); | |
474 | #else | |
475 | tem = memory_address (Pmode, | |
476 | plus_constant (tem, GET_MODE_SIZE (Pmode))); | |
477 | tem = gen_rtx_MEM (Pmode, tem); | |
ab6ab77e | 478 | set_mem_alias_set (tem, get_frame_alias_set ()); |
53800dbe | 479 | #endif |
480 | return tem; | |
481 | } | |
482 | ||
f7c44134 | 483 | /* Alias set used for setjmp buffer. */ |
484 | static HOST_WIDE_INT setjmp_alias_set = -1; | |
485 | ||
6b7f6858 | 486 | /* Construct the leading half of a __builtin_setjmp call. Control will |
487 | return to RECEIVER_LABEL. This is used directly by sjlj exception | |
488 | handling code. */ | |
53800dbe | 489 | |
6b7f6858 | 490 | void |
aecda0d6 | 491 | expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label) |
53800dbe | 492 | { |
53800dbe | 493 | enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL); |
53800dbe | 494 | rtx stack_save; |
f7c44134 | 495 | rtx mem; |
53800dbe | 496 | |
f7c44134 | 497 | if (setjmp_alias_set == -1) |
498 | setjmp_alias_set = new_alias_set (); | |
499 | ||
53800dbe | 500 | #ifdef POINTERS_EXTEND_UNSIGNED |
479e4d5e | 501 | if (GET_MODE (buf_addr) != Pmode) |
502 | buf_addr = convert_memory_address (Pmode, buf_addr); | |
53800dbe | 503 | #endif |
504 | ||
37ae8504 | 505 | buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX)); |
53800dbe | 506 | |
53800dbe | 507 | emit_queue (); |
508 | ||
6b7f6858 | 509 | /* We store the frame pointer and the address of receiver_label in |
510 | the buffer and use the rest of it for the stack save area, which | |
511 | is machine-dependent. */ | |
53800dbe | 512 | |
513 | #ifndef BUILTIN_SETJMP_FRAME_VALUE | |
514 | #define BUILTIN_SETJMP_FRAME_VALUE virtual_stack_vars_rtx | |
515 | #endif | |
516 | ||
f7c44134 | 517 | mem = gen_rtx_MEM (Pmode, buf_addr); |
ab6ab77e | 518 | set_mem_alias_set (mem, setjmp_alias_set); |
f7c44134 | 519 | emit_move_insn (mem, BUILTIN_SETJMP_FRAME_VALUE); |
520 | ||
521 | mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))), | |
ab6ab77e | 522 | set_mem_alias_set (mem, setjmp_alias_set); |
f7c44134 | 523 | |
524 | emit_move_insn (validize_mem (mem), | |
6b7f6858 | 525 | force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label))); |
53800dbe | 526 | |
527 | stack_save = gen_rtx_MEM (sa_mode, | |
528 | plus_constant (buf_addr, | |
529 | 2 * GET_MODE_SIZE (Pmode))); | |
ab6ab77e | 530 | set_mem_alias_set (stack_save, setjmp_alias_set); |
53800dbe | 531 | emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX); |
532 | ||
533 | /* If there is further processing to do, do it. */ | |
534 | #ifdef HAVE_builtin_setjmp_setup | |
535 | if (HAVE_builtin_setjmp_setup) | |
536 | emit_insn (gen_builtin_setjmp_setup (buf_addr)); | |
537 | #endif | |
538 | ||
6b7f6858 | 539 | /* Tell optimize_save_area_alloca that extra work is going to |
540 | need to go on during alloca. */ | |
3b0fa6b6 | 541 | current_function_calls_setjmp = 1; |
80ab81b9 | 542 | |
543 | /* Set this so all the registers get saved in our frame; we need to be | |
2c0e001b | 544 | able to copy the saved values for any registers from frames we unwind. */ |
80ab81b9 | 545 | current_function_has_nonlocal_label = 1; |
6b7f6858 | 546 | } |
53800dbe | 547 | |
6b7f6858 | 548 | /* Construct the trailing part of a __builtin_setjmp call. |
549 | This is used directly by sjlj exception handling code. */ | |
550 | ||
551 | void | |
aecda0d6 | 552 | expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED) |
6b7f6858 | 553 | { |
53800dbe | 554 | /* Clobber the FP when we get here, so we have to make sure it's |
555 | marked as used by this function. */ | |
556 | emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx)); | |
557 | ||
558 | /* Mark the static chain as clobbered here so life information | |
559 | doesn't get messed up for it. */ | |
560 | emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx)); | |
561 | ||
562 | /* Now put in the code to restore the frame pointer, and argument | |
563 | pointer, if needed. The code below is from expand_end_bindings | |
564 | in stmt.c; see detailed documentation there. */ | |
565 | #ifdef HAVE_nonlocal_goto | |
566 | if (! HAVE_nonlocal_goto) | |
567 | #endif | |
568 | emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx); | |
569 | ||
570 | #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM | |
571 | if (fixed_regs[ARG_POINTER_REGNUM]) | |
572 | { | |
573 | #ifdef ELIMINABLE_REGS | |
574 | size_t i; | |
e99c3a1d | 575 | static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS; |
53800dbe | 576 | |
3098b2d3 | 577 | for (i = 0; i < ARRAY_SIZE (elim_regs); i++) |
53800dbe | 578 | if (elim_regs[i].from == ARG_POINTER_REGNUM |
579 | && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM) | |
580 | break; | |
581 | ||
3098b2d3 | 582 | if (i == ARRAY_SIZE (elim_regs)) |
53800dbe | 583 | #endif |
584 | { | |
585 | /* Now restore our arg pointer from the address at which it | |
05927e40 | 586 | was saved in our stack frame. */ |
53800dbe | 587 | emit_move_insn (virtual_incoming_args_rtx, |
05927e40 | 588 | copy_to_reg (get_arg_pointer_save_area (cfun))); |
53800dbe | 589 | } |
590 | } | |
591 | #endif | |
592 | ||
593 | #ifdef HAVE_builtin_setjmp_receiver | |
594 | if (HAVE_builtin_setjmp_receiver) | |
6b7f6858 | 595 | emit_insn (gen_builtin_setjmp_receiver (receiver_label)); |
53800dbe | 596 | else |
597 | #endif | |
598 | #ifdef HAVE_nonlocal_goto_receiver | |
599 | if (HAVE_nonlocal_goto_receiver) | |
600 | emit_insn (gen_nonlocal_goto_receiver ()); | |
601 | else | |
602 | #endif | |
6b7f6858 | 603 | { /* Nothing */ } |
57f6bb94 | 604 | |
605 | /* @@@ This is a kludge. Not all machine descriptions define a blockage | |
606 | insn, but we must not allow the code we just generated to be reordered | |
607 | by scheduling. Specifically, the update of the frame pointer must | |
608 | happen immediately, not later. So emit an ASM_INPUT to act as blockage | |
609 | insn. */ | |
610 | emit_insn (gen_rtx_ASM_INPUT (VOIDmode, "")); | |
6b7f6858 | 611 | } |
53800dbe | 612 | |
6b7f6858 | 613 | /* __builtin_setjmp is passed a pointer to an array of five words (not |
614 | all will be used on all machines). It operates similarly to the C | |
615 | library function of the same name, but is more efficient. Much of | |
616 | the code below (and for longjmp) is copied from the handling of | |
617 | non-local gotos. | |
618 | ||
619 | NOTE: This is intended for use by GNAT and the exception handling | |
620 | scheme in the compiler and will only work in the method used by | |
621 | them. */ | |
622 | ||
623 | static rtx | |
aecda0d6 | 624 | expand_builtin_setjmp (tree arglist, rtx target) |
6b7f6858 | 625 | { |
626 | rtx buf_addr, next_lab, cont_lab; | |
627 | ||
0eb671f7 | 628 | if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE)) |
6b7f6858 | 629 | return NULL_RTX; |
630 | ||
631 | if (target == 0 || GET_CODE (target) != REG | |
632 | || REGNO (target) < FIRST_PSEUDO_REGISTER) | |
633 | target = gen_reg_rtx (TYPE_MODE (integer_type_node)); | |
634 | ||
635 | buf_addr = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0); | |
636 | ||
637 | next_lab = gen_label_rtx (); | |
638 | cont_lab = gen_label_rtx (); | |
639 | ||
640 | expand_builtin_setjmp_setup (buf_addr, next_lab); | |
641 | ||
101a5a9d | 642 | /* Set TARGET to zero and branch to the continue label. Use emit_jump to |
643 | ensure that pending stack adjustments are flushed. */ | |
6b7f6858 | 644 | emit_move_insn (target, const0_rtx); |
101a5a9d | 645 | emit_jump (cont_lab); |
646 | ||
6b7f6858 | 647 | emit_label (next_lab); |
648 | ||
649 | expand_builtin_setjmp_receiver (next_lab); | |
650 | ||
651 | /* Set TARGET to one. */ | |
652 | emit_move_insn (target, const1_rtx); | |
653 | emit_label (cont_lab); | |
654 | ||
655 | /* Tell flow about the strange goings on. Putting `next_lab' on | |
656 | `nonlocal_goto_handler_labels' to indicates that function | |
657 | calls may traverse the arc back to this label. */ | |
658 | ||
659 | current_function_has_nonlocal_label = 1; | |
660 | nonlocal_goto_handler_labels | |
661 | = gen_rtx_EXPR_LIST (VOIDmode, next_lab, nonlocal_goto_handler_labels); | |
53800dbe | 662 | |
663 | return target; | |
664 | } | |
665 | ||
666 | /* __builtin_longjmp is passed a pointer to an array of five words (not | |
667 | all will be used on all machines). It operates similarly to the C | |
668 | library function of the same name, but is more efficient. Much of | |
669 | the code below is copied from the handling of non-local gotos. | |
670 | ||
671 | NOTE: This is intended for use by GNAT and the exception handling | |
672 | scheme in the compiler and will only work in the method used by | |
673 | them. */ | |
674 | ||
675 | void | |
aecda0d6 | 676 | expand_builtin_longjmp (rtx buf_addr, rtx value) |
53800dbe | 677 | { |
4712c7d6 | 678 | rtx fp, lab, stack, insn, last; |
53800dbe | 679 | enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL); |
680 | ||
f7c44134 | 681 | if (setjmp_alias_set == -1) |
682 | setjmp_alias_set = new_alias_set (); | |
683 | ||
53800dbe | 684 | #ifdef POINTERS_EXTEND_UNSIGNED |
479e4d5e | 685 | if (GET_MODE (buf_addr) != Pmode) |
686 | buf_addr = convert_memory_address (Pmode, buf_addr); | |
53800dbe | 687 | #endif |
479e4d5e | 688 | |
53800dbe | 689 | buf_addr = force_reg (Pmode, buf_addr); |
690 | ||
691 | /* We used to store value in static_chain_rtx, but that fails if pointers | |
692 | are smaller than integers. We instead require that the user must pass | |
693 | a second argument of 1, because that is what builtin_setjmp will | |
694 | return. This also makes EH slightly more efficient, since we are no | |
695 | longer copying around a value that we don't care about. */ | |
696 | if (value != const1_rtx) | |
697 | abort (); | |
698 | ||
3b0fa6b6 | 699 | current_function_calls_longjmp = 1; |
700 | ||
4712c7d6 | 701 | last = get_last_insn (); |
53800dbe | 702 | #ifdef HAVE_builtin_longjmp |
703 | if (HAVE_builtin_longjmp) | |
704 | emit_insn (gen_builtin_longjmp (buf_addr)); | |
705 | else | |
706 | #endif | |
707 | { | |
708 | fp = gen_rtx_MEM (Pmode, buf_addr); | |
709 | lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr, | |
710 | GET_MODE_SIZE (Pmode))); | |
711 | ||
712 | stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr, | |
713 | 2 * GET_MODE_SIZE (Pmode))); | |
ab6ab77e | 714 | set_mem_alias_set (fp, setjmp_alias_set); |
715 | set_mem_alias_set (lab, setjmp_alias_set); | |
716 | set_mem_alias_set (stack, setjmp_alias_set); | |
53800dbe | 717 | |
718 | /* Pick up FP, label, and SP from the block and jump. This code is | |
719 | from expand_goto in stmt.c; see there for detailed comments. */ | |
720 | #if HAVE_nonlocal_goto | |
721 | if (HAVE_nonlocal_goto) | |
722 | /* We have to pass a value to the nonlocal_goto pattern that will | |
723 | get copied into the static_chain pointer, but it does not matter | |
724 | what that value is, because builtin_setjmp does not use it. */ | |
28d202a8 | 725 | emit_insn (gen_nonlocal_goto (value, lab, stack, fp)); |
53800dbe | 726 | else |
727 | #endif | |
728 | { | |
729 | lab = copy_to_reg (lab); | |
730 | ||
731 | emit_move_insn (hard_frame_pointer_rtx, fp); | |
732 | emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX); | |
733 | ||
734 | emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx)); | |
735 | emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx)); | |
736 | emit_indirect_jump (lab); | |
737 | } | |
738 | } | |
615166bb | 739 | |
740 | /* Search backwards and mark the jump insn as a non-local goto. | |
741 | Note that this precludes the use of __builtin_longjmp to a | |
742 | __builtin_setjmp target in the same function. However, we've | |
743 | already cautioned the user that these functions are for | |
744 | internal exception handling use only. */ | |
449c0509 | 745 | for (insn = get_last_insn (); insn; insn = PREV_INSN (insn)) |
746 | { | |
4712c7d6 | 747 | if (insn == last) |
748 | abort (); | |
449c0509 | 749 | if (GET_CODE (insn) == JUMP_INSN) |
750 | { | |
751 | REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO, const0_rtx, | |
752 | REG_NOTES (insn)); | |
753 | break; | |
754 | } | |
755 | else if (GET_CODE (insn) == CALL_INSN) | |
9342ee68 | 756 | break; |
449c0509 | 757 | } |
53800dbe | 758 | } |
759 | ||
5e3608d8 | 760 | /* Expand a call to __builtin_prefetch. For a target that does not support |
761 | data prefetch, evaluate the memory address argument in case it has side | |
762 | effects. */ | |
763 | ||
764 | static void | |
aecda0d6 | 765 | expand_builtin_prefetch (tree arglist) |
5e3608d8 | 766 | { |
767 | tree arg0, arg1, arg2; | |
768 | rtx op0, op1, op2; | |
769 | ||
26a5cadb | 770 | if (!validate_arglist (arglist, POINTER_TYPE, 0)) |
771 | return; | |
772 | ||
5e3608d8 | 773 | arg0 = TREE_VALUE (arglist); |
26a5cadb | 774 | /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to |
775 | zero (read) and argument 2 (locality) defaults to 3 (high degree of | |
776 | locality). */ | |
777 | if (TREE_CHAIN (arglist)) | |
778 | { | |
779 | arg1 = TREE_VALUE (TREE_CHAIN (arglist)); | |
780 | if (TREE_CHAIN (TREE_CHAIN (arglist))) | |
9342ee68 | 781 | arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); |
26a5cadb | 782 | else |
783 | arg2 = build_int_2 (3, 0); | |
784 | } | |
785 | else | |
786 | { | |
787 | arg1 = integer_zero_node; | |
788 | arg2 = build_int_2 (3, 0); | |
789 | } | |
5e3608d8 | 790 | |
791 | /* Argument 0 is an address. */ | |
792 | op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL); | |
793 | ||
794 | /* Argument 1 (read/write flag) must be a compile-time constant int. */ | |
795 | if (TREE_CODE (arg1) != INTEGER_CST) | |
796 | { | |
9342ee68 | 797 | error ("second arg to `__builtin_prefetch' must be a constant"); |
798 | arg1 = integer_zero_node; | |
5e3608d8 | 799 | } |
bf8e3599 | 800 | op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0); |
5e3608d8 | 801 | /* Argument 1 must be either zero or one. */ |
802 | if (INTVAL (op1) != 0 && INTVAL (op1) != 1) | |
803 | { | |
804 | warning ("invalid second arg to __builtin_prefetch; using zero"); | |
805 | op1 = const0_rtx; | |
806 | } | |
807 | ||
808 | /* Argument 2 (locality) must be a compile-time constant int. */ | |
809 | if (TREE_CODE (arg2) != INTEGER_CST) | |
810 | { | |
811 | error ("third arg to `__builtin_prefetch' must be a constant"); | |
812 | arg2 = integer_zero_node; | |
813 | } | |
bf8e3599 | 814 | op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0); |
5e3608d8 | 815 | /* Argument 2 must be 0, 1, 2, or 3. */ |
816 | if (INTVAL (op2) < 0 || INTVAL (op2) > 3) | |
817 | { | |
818 | warning ("invalid third arg to __builtin_prefetch; using zero"); | |
819 | op2 = const0_rtx; | |
820 | } | |
821 | ||
822 | #ifdef HAVE_prefetch | |
823 | if (HAVE_prefetch) | |
824 | { | |
f0ce3b1f | 825 | if ((! (*insn_data[(int) CODE_FOR_prefetch].operand[0].predicate) |
4cd21437 | 826 | (op0, |
f0ce3b1f | 827 | insn_data[(int) CODE_FOR_prefetch].operand[0].mode)) |
19bf118a | 828 | || (GET_MODE (op0) != Pmode)) |
9342ee68 | 829 | { |
4cd21437 | 830 | #ifdef POINTERS_EXTEND_UNSIGNED |
19bf118a | 831 | if (GET_MODE (op0) != Pmode) |
4cd21437 | 832 | op0 = convert_memory_address (Pmode, op0); |
833 | #endif | |
9342ee68 | 834 | op0 = force_reg (Pmode, op0); |
835 | } | |
5e3608d8 | 836 | emit_insn (gen_prefetch (op0, op1, op2)); |
837 | } | |
838 | else | |
839 | #endif | |
840 | op0 = protect_from_queue (op0, 0); | |
f0ce3b1f | 841 | /* Don't do anything with direct references to volatile memory, but |
842 | generate code to handle other side effects. */ | |
843 | if (GET_CODE (op0) != MEM && side_effects_p (op0)) | |
844 | emit_insn (op0); | |
5e3608d8 | 845 | } |
846 | ||
f7c44134 | 847 | /* Get a MEM rtx for expression EXP which is the address of an operand |
848 | to be used to be used in a string instruction (cmpstrsi, movstrsi, ..). */ | |
849 | ||
53800dbe | 850 | static rtx |
aecda0d6 | 851 | get_memory_rtx (tree exp) |
53800dbe | 852 | { |
726ec87c | 853 | rtx addr = expand_expr (exp, NULL_RTX, ptr_mode, EXPAND_SUM); |
854 | rtx mem; | |
f7c44134 | 855 | |
726ec87c | 856 | #ifdef POINTERS_EXTEND_UNSIGNED |
857 | if (GET_MODE (addr) != Pmode) | |
858 | addr = convert_memory_address (Pmode, addr); | |
859 | #endif | |
860 | ||
861 | mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr)); | |
2a631e19 | 862 | |
f7c44134 | 863 | /* Get an expression we can use to find the attributes to assign to MEM. |
864 | If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if | |
865 | we can. First remove any nops. */ | |
866 | while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR | |
f0ce3b1f | 867 | || TREE_CODE (exp) == NON_LVALUE_EXPR) |
f7c44134 | 868 | && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0)))) |
869 | exp = TREE_OPERAND (exp, 0); | |
870 | ||
871 | if (TREE_CODE (exp) == ADDR_EXPR) | |
eec8e941 | 872 | { |
873 | exp = TREE_OPERAND (exp, 0); | |
874 | set_mem_attributes (mem, exp, 0); | |
875 | } | |
f7c44134 | 876 | else if (POINTER_TYPE_P (TREE_TYPE (exp))) |
eec8e941 | 877 | { |
878 | exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp); | |
879 | /* memcpy, memset and other builtin stringops can alias with anything. */ | |
880 | set_mem_alias_set (mem, 0); | |
881 | } | |
53800dbe | 882 | |
53800dbe | 883 | return mem; |
884 | } | |
885 | \f | |
886 | /* Built-in functions to perform an untyped call and return. */ | |
887 | ||
888 | /* For each register that may be used for calling a function, this | |
889 | gives a mode used to copy the register's value. VOIDmode indicates | |
890 | the register is not used for calling a function. If the machine | |
891 | has register windows, this gives only the outbound registers. | |
892 | INCOMING_REGNO gives the corresponding inbound register. */ | |
893 | static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER]; | |
894 | ||
895 | /* For each register that may be used for returning values, this gives | |
896 | a mode used to copy the register's value. VOIDmode indicates the | |
897 | register is not used for returning values. If the machine has | |
898 | register windows, this gives only the outbound registers. | |
899 | INCOMING_REGNO gives the corresponding inbound register. */ | |
900 | static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER]; | |
901 | ||
902 | /* For each register that may be used for calling a function, this | |
903 | gives the offset of that register into the block returned by | |
904 | __builtin_apply_args. 0 indicates that the register is not | |
905 | used for calling a function. */ | |
906 | static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER]; | |
907 | ||
bf8e3599 | 908 | /* Return the offset of register REGNO into the block returned by |
53800dbe | 909 | __builtin_apply_args. This is not declared static, since it is |
910 | needed in objc-act.c. */ | |
911 | ||
bf8e3599 | 912 | int |
aecda0d6 | 913 | apply_args_register_offset (int regno) |
53800dbe | 914 | { |
915 | apply_args_size (); | |
916 | ||
917 | /* Arguments are always put in outgoing registers (in the argument | |
918 | block) if such make sense. */ | |
919 | #ifdef OUTGOING_REGNO | |
f0ce3b1f | 920 | regno = OUTGOING_REGNO (regno); |
53800dbe | 921 | #endif |
922 | return apply_args_reg_offset[regno]; | |
923 | } | |
924 | ||
925 | /* Return the size required for the block returned by __builtin_apply_args, | |
926 | and initialize apply_args_mode. */ | |
927 | ||
928 | static int | |
aecda0d6 | 929 | apply_args_size (void) |
53800dbe | 930 | { |
931 | static int size = -1; | |
58e9ce8f | 932 | int align; |
933 | unsigned int regno; | |
53800dbe | 934 | enum machine_mode mode; |
935 | ||
936 | /* The values computed by this function never change. */ | |
937 | if (size < 0) | |
938 | { | |
939 | /* The first value is the incoming arg-pointer. */ | |
940 | size = GET_MODE_SIZE (Pmode); | |
941 | ||
942 | /* The second value is the structure value address unless this is | |
943 | passed as an "invisible" first argument. */ | |
944 | if (struct_value_rtx) | |
945 | size += GET_MODE_SIZE (Pmode); | |
946 | ||
947 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
948 | if (FUNCTION_ARG_REGNO_P (regno)) | |
949 | { | |
950 | /* Search for the proper mode for copying this register's | |
951 | value. I'm not sure this is right, but it works so far. */ | |
952 | enum machine_mode best_mode = VOIDmode; | |
953 | ||
954 | for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); | |
955 | mode != VOIDmode; | |
956 | mode = GET_MODE_WIDER_MODE (mode)) | |
957 | if (HARD_REGNO_MODE_OK (regno, mode) | |
958 | && HARD_REGNO_NREGS (regno, mode) == 1) | |
959 | best_mode = mode; | |
960 | ||
961 | if (best_mode == VOIDmode) | |
962 | for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); | |
963 | mode != VOIDmode; | |
964 | mode = GET_MODE_WIDER_MODE (mode)) | |
965 | if (HARD_REGNO_MODE_OK (regno, mode) | |
ad99e708 | 966 | && have_insn_for (SET, mode)) |
53800dbe | 967 | best_mode = mode; |
968 | ||
223097eb | 969 | if (best_mode == VOIDmode) |
970 | for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT); | |
971 | mode != VOIDmode; | |
972 | mode = GET_MODE_WIDER_MODE (mode)) | |
973 | if (HARD_REGNO_MODE_OK (regno, mode) | |
974 | && have_insn_for (SET, mode)) | |
975 | best_mode = mode; | |
976 | ||
977 | if (best_mode == VOIDmode) | |
978 | for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT); | |
979 | mode != VOIDmode; | |
980 | mode = GET_MODE_WIDER_MODE (mode)) | |
981 | if (HARD_REGNO_MODE_OK (regno, mode) | |
982 | && have_insn_for (SET, mode)) | |
983 | best_mode = mode; | |
984 | ||
53800dbe | 985 | mode = best_mode; |
986 | if (mode == VOIDmode) | |
987 | abort (); | |
988 | ||
989 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
990 | if (size % align != 0) | |
991 | size = CEIL (size, align) * align; | |
992 | apply_args_reg_offset[regno] = size; | |
993 | size += GET_MODE_SIZE (mode); | |
994 | apply_args_mode[regno] = mode; | |
995 | } | |
996 | else | |
997 | { | |
998 | apply_args_mode[regno] = VOIDmode; | |
999 | apply_args_reg_offset[regno] = 0; | |
1000 | } | |
1001 | } | |
1002 | return size; | |
1003 | } | |
1004 | ||
1005 | /* Return the size required for the block returned by __builtin_apply, | |
1006 | and initialize apply_result_mode. */ | |
1007 | ||
1008 | static int | |
aecda0d6 | 1009 | apply_result_size (void) |
53800dbe | 1010 | { |
1011 | static int size = -1; | |
1012 | int align, regno; | |
1013 | enum machine_mode mode; | |
1014 | ||
1015 | /* The values computed by this function never change. */ | |
1016 | if (size < 0) | |
1017 | { | |
1018 | size = 0; | |
1019 | ||
1020 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1021 | if (FUNCTION_VALUE_REGNO_P (regno)) | |
1022 | { | |
1023 | /* Search for the proper mode for copying this register's | |
1024 | value. I'm not sure this is right, but it works so far. */ | |
1025 | enum machine_mode best_mode = VOIDmode; | |
1026 | ||
1027 | for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); | |
1028 | mode != TImode; | |
1029 | mode = GET_MODE_WIDER_MODE (mode)) | |
1030 | if (HARD_REGNO_MODE_OK (regno, mode)) | |
1031 | best_mode = mode; | |
1032 | ||
1033 | if (best_mode == VOIDmode) | |
1034 | for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); | |
1035 | mode != VOIDmode; | |
1036 | mode = GET_MODE_WIDER_MODE (mode)) | |
1037 | if (HARD_REGNO_MODE_OK (regno, mode) | |
ad99e708 | 1038 | && have_insn_for (SET, mode)) |
53800dbe | 1039 | best_mode = mode; |
1040 | ||
223097eb | 1041 | if (best_mode == VOIDmode) |
1042 | for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_FLOAT); | |
1043 | mode != VOIDmode; | |
1044 | mode = GET_MODE_WIDER_MODE (mode)) | |
1045 | if (HARD_REGNO_MODE_OK (regno, mode) | |
1046 | && have_insn_for (SET, mode)) | |
f0ce3b1f | 1047 | best_mode = mode; |
223097eb | 1048 | |
1049 | if (best_mode == VOIDmode) | |
1050 | for (mode = GET_CLASS_NARROWEST_MODE (MODE_VECTOR_INT); | |
1051 | mode != VOIDmode; | |
1052 | mode = GET_MODE_WIDER_MODE (mode)) | |
1053 | if (HARD_REGNO_MODE_OK (regno, mode) | |
1054 | && have_insn_for (SET, mode)) | |
1055 | best_mode = mode; | |
1056 | ||
53800dbe | 1057 | mode = best_mode; |
1058 | if (mode == VOIDmode) | |
1059 | abort (); | |
1060 | ||
1061 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1062 | if (size % align != 0) | |
1063 | size = CEIL (size, align) * align; | |
1064 | size += GET_MODE_SIZE (mode); | |
1065 | apply_result_mode[regno] = mode; | |
1066 | } | |
1067 | else | |
1068 | apply_result_mode[regno] = VOIDmode; | |
1069 | ||
1070 | /* Allow targets that use untyped_call and untyped_return to override | |
1071 | the size so that machine-specific information can be stored here. */ | |
1072 | #ifdef APPLY_RESULT_SIZE | |
1073 | size = APPLY_RESULT_SIZE; | |
1074 | #endif | |
1075 | } | |
1076 | return size; | |
1077 | } | |
1078 | ||
1079 | #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return) | |
1080 | /* Create a vector describing the result block RESULT. If SAVEP is true, | |
1081 | the result block is used to save the values; otherwise it is used to | |
1082 | restore the values. */ | |
1083 | ||
1084 | static rtx | |
aecda0d6 | 1085 | result_vector (int savep, rtx result) |
53800dbe | 1086 | { |
1087 | int regno, size, align, nelts; | |
1088 | enum machine_mode mode; | |
1089 | rtx reg, mem; | |
f0af5a88 | 1090 | rtx *savevec = alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx)); |
bf8e3599 | 1091 | |
53800dbe | 1092 | size = nelts = 0; |
1093 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1094 | if ((mode = apply_result_mode[regno]) != VOIDmode) | |
1095 | { | |
1096 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1097 | if (size % align != 0) | |
1098 | size = CEIL (size, align) * align; | |
1099 | reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno)); | |
e513d163 | 1100 | mem = adjust_address (result, mode, size); |
53800dbe | 1101 | savevec[nelts++] = (savep |
1102 | ? gen_rtx_SET (VOIDmode, mem, reg) | |
1103 | : gen_rtx_SET (VOIDmode, reg, mem)); | |
1104 | size += GET_MODE_SIZE (mode); | |
1105 | } | |
1106 | return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec)); | |
1107 | } | |
1108 | #endif /* HAVE_untyped_call or HAVE_untyped_return */ | |
1109 | ||
1110 | /* Save the state required to perform an untyped call with the same | |
1111 | arguments as were passed to the current function. */ | |
1112 | ||
1113 | static rtx | |
aecda0d6 | 1114 | expand_builtin_apply_args_1 (void) |
53800dbe | 1115 | { |
1116 | rtx registers; | |
1117 | int size, align, regno; | |
1118 | enum machine_mode mode; | |
1119 | ||
1120 | /* Create a block where the arg-pointer, structure value address, | |
1121 | and argument registers can be saved. */ | |
1122 | registers = assign_stack_local (BLKmode, apply_args_size (), -1); | |
1123 | ||
1124 | /* Walk past the arg-pointer and structure value address. */ | |
1125 | size = GET_MODE_SIZE (Pmode); | |
1126 | if (struct_value_rtx) | |
1127 | size += GET_MODE_SIZE (Pmode); | |
1128 | ||
1129 | /* Save each register used in calling a function to the block. */ | |
1130 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1131 | if ((mode = apply_args_mode[regno]) != VOIDmode) | |
1132 | { | |
1133 | rtx tem; | |
1134 | ||
1135 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1136 | if (size % align != 0) | |
1137 | size = CEIL (size, align) * align; | |
1138 | ||
1139 | tem = gen_rtx_REG (mode, INCOMING_REGNO (regno)); | |
1140 | ||
e513d163 | 1141 | emit_move_insn (adjust_address (registers, mode, size), tem); |
53800dbe | 1142 | size += GET_MODE_SIZE (mode); |
1143 | } | |
1144 | ||
1145 | /* Save the arg pointer to the block. */ | |
e513d163 | 1146 | emit_move_insn (adjust_address (registers, Pmode, 0), |
53800dbe | 1147 | copy_to_reg (virtual_incoming_args_rtx)); |
1148 | size = GET_MODE_SIZE (Pmode); | |
1149 | ||
1150 | /* Save the structure value address unless this is passed as an | |
1151 | "invisible" first argument. */ | |
1152 | if (struct_value_incoming_rtx) | |
1153 | { | |
e513d163 | 1154 | emit_move_insn (adjust_address (registers, Pmode, size), |
53800dbe | 1155 | copy_to_reg (struct_value_incoming_rtx)); |
1156 | size += GET_MODE_SIZE (Pmode); | |
1157 | } | |
1158 | ||
1159 | /* Return the address of the block. */ | |
1160 | return copy_addr_to_reg (XEXP (registers, 0)); | |
1161 | } | |
1162 | ||
1163 | /* __builtin_apply_args returns block of memory allocated on | |
1164 | the stack into which is stored the arg pointer, structure | |
1165 | value address, static chain, and all the registers that might | |
1166 | possibly be used in performing a function call. The code is | |
1167 | moved to the start of the function so the incoming values are | |
1168 | saved. */ | |
27d0c333 | 1169 | |
53800dbe | 1170 | static rtx |
aecda0d6 | 1171 | expand_builtin_apply_args (void) |
53800dbe | 1172 | { |
1173 | /* Don't do __builtin_apply_args more than once in a function. | |
1174 | Save the result of the first call and reuse it. */ | |
1175 | if (apply_args_value != 0) | |
1176 | return apply_args_value; | |
1177 | { | |
1178 | /* When this function is called, it means that registers must be | |
1179 | saved on entry to this function. So we migrate the | |
1180 | call to the first insn of this function. */ | |
1181 | rtx temp; | |
1182 | rtx seq; | |
1183 | ||
1184 | start_sequence (); | |
1185 | temp = expand_builtin_apply_args_1 (); | |
1186 | seq = get_insns (); | |
1187 | end_sequence (); | |
1188 | ||
1189 | apply_args_value = temp; | |
1190 | ||
31d3e01c | 1191 | /* Put the insns after the NOTE that starts the function. |
1192 | If this is inside a start_sequence, make the outer-level insn | |
53800dbe | 1193 | chain current, so the code is placed at the start of the |
1194 | function. */ | |
1195 | push_topmost_sequence (); | |
31d3e01c | 1196 | emit_insn_before (seq, NEXT_INSN (get_insns ())); |
53800dbe | 1197 | pop_topmost_sequence (); |
1198 | return temp; | |
1199 | } | |
1200 | } | |
1201 | ||
1202 | /* Perform an untyped call and save the state required to perform an | |
1203 | untyped return of whatever value was returned by the given function. */ | |
1204 | ||
1205 | static rtx | |
aecda0d6 | 1206 | expand_builtin_apply (rtx function, rtx arguments, rtx argsize) |
53800dbe | 1207 | { |
1208 | int size, align, regno; | |
1209 | enum machine_mode mode; | |
2a631e19 | 1210 | rtx incoming_args, result, reg, dest, src, call_insn; |
53800dbe | 1211 | rtx old_stack_level = 0; |
1212 | rtx call_fusage = 0; | |
1213 | ||
726ec87c | 1214 | #ifdef POINTERS_EXTEND_UNSIGNED |
1215 | if (GET_MODE (arguments) != Pmode) | |
1216 | arguments = convert_memory_address (Pmode, arguments); | |
1217 | #endif | |
1218 | ||
53800dbe | 1219 | /* Create a block where the return registers can be saved. */ |
1220 | result = assign_stack_local (BLKmode, apply_result_size (), -1); | |
1221 | ||
53800dbe | 1222 | /* Fetch the arg pointer from the ARGUMENTS block. */ |
1223 | incoming_args = gen_reg_rtx (Pmode); | |
726ec87c | 1224 | emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments)); |
53800dbe | 1225 | #ifndef STACK_GROWS_DOWNWARD |
ad99e708 | 1226 | incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize, |
1227 | incoming_args, 0, OPTAB_LIB_WIDEN); | |
53800dbe | 1228 | #endif |
1229 | ||
1230 | /* Perform postincrements before actually calling the function. */ | |
1231 | emit_queue (); | |
1232 | ||
04a46d40 | 1233 | /* Push a new argument block and copy the arguments. Do not allow |
1234 | the (potential) memcpy call below to interfere with our stack | |
1235 | manipulations. */ | |
53800dbe | 1236 | do_pending_stack_adjust (); |
04a46d40 | 1237 | NO_DEFER_POP; |
53800dbe | 1238 | |
2358393e | 1239 | /* Save the stack with nonlocal if available. */ |
53800dbe | 1240 | #ifdef HAVE_save_stack_nonlocal |
1241 | if (HAVE_save_stack_nonlocal) | |
1242 | emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX); | |
1243 | else | |
1244 | #endif | |
1245 | emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX); | |
1246 | ||
1247 | /* Push a block of memory onto the stack to store the memory arguments. | |
1248 | Save the address in a register, and copy the memory arguments. ??? I | |
1249 | haven't figured out how the calling convention macros effect this, | |
1250 | but it's likely that the source and/or destination addresses in | |
1251 | the block copy will need updating in machine specific ways. */ | |
91b70175 | 1252 | dest = allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT); |
2a631e19 | 1253 | dest = gen_rtx_MEM (BLKmode, dest); |
1254 | set_mem_align (dest, PARM_BOUNDARY); | |
1255 | src = gen_rtx_MEM (BLKmode, incoming_args); | |
1256 | set_mem_align (src, PARM_BOUNDARY); | |
0378dbdc | 1257 | emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL); |
53800dbe | 1258 | |
1259 | /* Refer to the argument block. */ | |
1260 | apply_args_size (); | |
1261 | arguments = gen_rtx_MEM (BLKmode, arguments); | |
2a631e19 | 1262 | set_mem_align (arguments, PARM_BOUNDARY); |
53800dbe | 1263 | |
1264 | /* Walk past the arg-pointer and structure value address. */ | |
1265 | size = GET_MODE_SIZE (Pmode); | |
1266 | if (struct_value_rtx) | |
1267 | size += GET_MODE_SIZE (Pmode); | |
1268 | ||
1269 | /* Restore each of the registers previously saved. Make USE insns | |
1270 | for each of these registers for use in making the call. */ | |
1271 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1272 | if ((mode = apply_args_mode[regno]) != VOIDmode) | |
1273 | { | |
1274 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1275 | if (size % align != 0) | |
1276 | size = CEIL (size, align) * align; | |
1277 | reg = gen_rtx_REG (mode, regno); | |
e513d163 | 1278 | emit_move_insn (reg, adjust_address (arguments, mode, size)); |
53800dbe | 1279 | use_reg (&call_fusage, reg); |
1280 | size += GET_MODE_SIZE (mode); | |
1281 | } | |
1282 | ||
1283 | /* Restore the structure value address unless this is passed as an | |
1284 | "invisible" first argument. */ | |
1285 | size = GET_MODE_SIZE (Pmode); | |
1286 | if (struct_value_rtx) | |
1287 | { | |
1288 | rtx value = gen_reg_rtx (Pmode); | |
e513d163 | 1289 | emit_move_insn (value, adjust_address (arguments, Pmode, size)); |
53800dbe | 1290 | emit_move_insn (struct_value_rtx, value); |
1291 | if (GET_CODE (struct_value_rtx) == REG) | |
f0ce3b1f | 1292 | use_reg (&call_fusage, struct_value_rtx); |
53800dbe | 1293 | size += GET_MODE_SIZE (Pmode); |
1294 | } | |
1295 | ||
1296 | /* All arguments and registers used for the call are set up by now! */ | |
707ff8b1 | 1297 | function = prepare_call_address (function, NULL_TREE, &call_fusage, 0, 0); |
53800dbe | 1298 | |
1299 | /* Ensure address is valid. SYMBOL_REF is already valid, so no need, | |
1300 | and we don't want to load it into a register as an optimization, | |
1301 | because prepare_call_address already did it if it should be done. */ | |
1302 | if (GET_CODE (function) != SYMBOL_REF) | |
1303 | function = memory_address (FUNCTION_MODE, function); | |
1304 | ||
1305 | /* Generate the actual call instruction and save the return value. */ | |
1306 | #ifdef HAVE_untyped_call | |
1307 | if (HAVE_untyped_call) | |
1308 | emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function), | |
1309 | result, result_vector (1, result))); | |
1310 | else | |
1311 | #endif | |
1312 | #ifdef HAVE_call_value | |
1313 | if (HAVE_call_value) | |
1314 | { | |
1315 | rtx valreg = 0; | |
1316 | ||
1317 | /* Locate the unique return register. It is not possible to | |
1318 | express a call that sets more than one return register using | |
1319 | call_value; use untyped_call for that. In fact, untyped_call | |
1320 | only needs to save the return registers in the given block. */ | |
1321 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1322 | if ((mode = apply_result_mode[regno]) != VOIDmode) | |
1323 | { | |
1324 | if (valreg) | |
1325 | abort (); /* HAVE_untyped_call required. */ | |
1326 | valreg = gen_rtx_REG (mode, regno); | |
1327 | } | |
1328 | ||
2ed6c343 | 1329 | emit_call_insn (GEN_CALL_VALUE (valreg, |
53800dbe | 1330 | gen_rtx_MEM (FUNCTION_MODE, function), |
1331 | const0_rtx, NULL_RTX, const0_rtx)); | |
1332 | ||
e513d163 | 1333 | emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg); |
53800dbe | 1334 | } |
1335 | else | |
1336 | #endif | |
1337 | abort (); | |
1338 | ||
d5f9786f | 1339 | /* Find the CALL insn we just emitted, and attach the register usage |
1340 | information. */ | |
1341 | call_insn = last_call_insn (); | |
1342 | add_function_usage_to (call_insn, call_fusage); | |
53800dbe | 1343 | |
1344 | /* Restore the stack. */ | |
1345 | #ifdef HAVE_save_stack_nonlocal | |
1346 | if (HAVE_save_stack_nonlocal) | |
1347 | emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX); | |
1348 | else | |
1349 | #endif | |
1350 | emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX); | |
1351 | ||
04a46d40 | 1352 | OK_DEFER_POP; |
1353 | ||
53800dbe | 1354 | /* Return the address of the result block. */ |
1355 | return copy_addr_to_reg (XEXP (result, 0)); | |
1356 | } | |
1357 | ||
1358 | /* Perform an untyped return. */ | |
1359 | ||
1360 | static void | |
aecda0d6 | 1361 | expand_builtin_return (rtx result) |
53800dbe | 1362 | { |
1363 | int size, align, regno; | |
1364 | enum machine_mode mode; | |
1365 | rtx reg; | |
1366 | rtx call_fusage = 0; | |
1367 | ||
726ec87c | 1368 | #ifdef POINTERS_EXTEND_UNSIGNED |
1369 | if (GET_MODE (result) != Pmode) | |
1370 | result = convert_memory_address (Pmode, result); | |
1371 | #endif | |
1372 | ||
53800dbe | 1373 | apply_result_size (); |
1374 | result = gen_rtx_MEM (BLKmode, result); | |
1375 | ||
1376 | #ifdef HAVE_untyped_return | |
1377 | if (HAVE_untyped_return) | |
1378 | { | |
1379 | emit_jump_insn (gen_untyped_return (result, result_vector (0, result))); | |
1380 | emit_barrier (); | |
1381 | return; | |
1382 | } | |
1383 | #endif | |
1384 | ||
1385 | /* Restore the return value and note that each value is used. */ | |
1386 | size = 0; | |
1387 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
1388 | if ((mode = apply_result_mode[regno]) != VOIDmode) | |
1389 | { | |
1390 | align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT; | |
1391 | if (size % align != 0) | |
1392 | size = CEIL (size, align) * align; | |
1393 | reg = gen_rtx_REG (mode, INCOMING_REGNO (regno)); | |
e513d163 | 1394 | emit_move_insn (reg, adjust_address (result, mode, size)); |
53800dbe | 1395 | |
1396 | push_to_sequence (call_fusage); | |
1397 | emit_insn (gen_rtx_USE (VOIDmode, reg)); | |
1398 | call_fusage = get_insns (); | |
1399 | end_sequence (); | |
1400 | size += GET_MODE_SIZE (mode); | |
1401 | } | |
1402 | ||
1403 | /* Put the USE insns before the return. */ | |
31d3e01c | 1404 | emit_insn (call_fusage); |
53800dbe | 1405 | |
1406 | /* Return whatever values was restored by jumping directly to the end | |
1407 | of the function. */ | |
1408 | expand_null_return (); | |
1409 | } | |
1410 | ||
539a3a92 | 1411 | /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */ |
27d0c333 | 1412 | |
539a3a92 | 1413 | static enum type_class |
aecda0d6 | 1414 | type_to_class (tree type) |
539a3a92 | 1415 | { |
1416 | switch (TREE_CODE (type)) | |
1417 | { | |
1418 | case VOID_TYPE: return void_type_class; | |
1419 | case INTEGER_TYPE: return integer_type_class; | |
1420 | case CHAR_TYPE: return char_type_class; | |
1421 | case ENUMERAL_TYPE: return enumeral_type_class; | |
1422 | case BOOLEAN_TYPE: return boolean_type_class; | |
1423 | case POINTER_TYPE: return pointer_type_class; | |
1424 | case REFERENCE_TYPE: return reference_type_class; | |
1425 | case OFFSET_TYPE: return offset_type_class; | |
1426 | case REAL_TYPE: return real_type_class; | |
1427 | case COMPLEX_TYPE: return complex_type_class; | |
1428 | case FUNCTION_TYPE: return function_type_class; | |
1429 | case METHOD_TYPE: return method_type_class; | |
1430 | case RECORD_TYPE: return record_type_class; | |
1431 | case UNION_TYPE: | |
1432 | case QUAL_UNION_TYPE: return union_type_class; | |
1433 | case ARRAY_TYPE: return (TYPE_STRING_FLAG (type) | |
1434 | ? string_type_class : array_type_class); | |
1435 | case SET_TYPE: return set_type_class; | |
1436 | case FILE_TYPE: return file_type_class; | |
1437 | case LANG_TYPE: return lang_type_class; | |
1438 | default: return no_type_class; | |
1439 | } | |
1440 | } | |
bf8e3599 | 1441 | |
53800dbe | 1442 | /* Expand a call to __builtin_classify_type with arguments found in |
1443 | ARGLIST. */ | |
27d0c333 | 1444 | |
53800dbe | 1445 | static rtx |
aecda0d6 | 1446 | expand_builtin_classify_type (tree arglist) |
53800dbe | 1447 | { |
1448 | if (arglist != 0) | |
539a3a92 | 1449 | return GEN_INT (type_to_class (TREE_TYPE (TREE_VALUE (arglist)))); |
53800dbe | 1450 | return GEN_INT (no_type_class); |
1451 | } | |
1452 | ||
1453 | /* Expand expression EXP, which is a call to __builtin_constant_p. */ | |
27d0c333 | 1454 | |
53800dbe | 1455 | static rtx |
aecda0d6 | 1456 | expand_builtin_constant_p (tree arglist, enum machine_mode target_mode) |
53800dbe | 1457 | { |
650e4c94 | 1458 | rtx tmp; |
53800dbe | 1459 | |
1460 | if (arglist == 0) | |
1461 | return const0_rtx; | |
650e4c94 | 1462 | arglist = TREE_VALUE (arglist); |
53800dbe | 1463 | |
650e4c94 | 1464 | /* We have taken care of the easy cases during constant folding. This |
c7926a82 | 1465 | case is not obvious, so emit (constant_p_rtx (ARGLIST)) and let CSE |
1fb4300c | 1466 | get a chance to see if it can deduce whether ARGLIST is constant. |
1467 | If CSE isn't going to run, of course, don't bother waiting. */ | |
1468 | ||
1469 | if (cse_not_expected) | |
1470 | return const0_rtx; | |
53800dbe | 1471 | |
e6f295fb | 1472 | current_function_calls_constant_p = 1; |
1473 | ||
650e4c94 | 1474 | tmp = expand_expr (arglist, NULL_RTX, VOIDmode, 0); |
80cd7a5e | 1475 | tmp = gen_rtx_CONSTANT_P_RTX (target_mode, tmp); |
650e4c94 | 1476 | return tmp; |
53800dbe | 1477 | } |
1478 | ||
07976da7 | 1479 | /* This helper macro, meant to be used in mathfn_built_in below, |
1480 | determines which among a set of three builtin math functions is | |
1481 | appropriate for a given type mode. The `F' and `L' cases are | |
1482 | automatically generated from the `double' case. */ | |
1483 | #define CASE_MATHFN(BUILT_IN_MATHFN) \ | |
1484 | case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \ | |
1485 | fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \ | |
1486 | fcodel = BUILT_IN_MATHFN##L ; break; | |
1487 | ||
1488 | /* Return mathematic function equivalent to FN but operating directly | |
1489 | on TYPE, if available. If we can't do the conversion, return zero. */ | |
0a68165a | 1490 | tree |
aecda0d6 | 1491 | mathfn_built_in (tree type, enum built_in_function fn) |
0a68165a | 1492 | { |
07976da7 | 1493 | const enum machine_mode type_mode = TYPE_MODE (type); |
1494 | enum built_in_function fcode, fcodef, fcodel; | |
1495 | ||
1496 | switch (fn) | |
1497 | { | |
746114e8 | 1498 | CASE_MATHFN (BUILT_IN_ACOS) |
1499 | CASE_MATHFN (BUILT_IN_ACOSH) | |
1500 | CASE_MATHFN (BUILT_IN_ASIN) | |
1501 | CASE_MATHFN (BUILT_IN_ASINH) | |
07976da7 | 1502 | CASE_MATHFN (BUILT_IN_ATAN) |
746114e8 | 1503 | CASE_MATHFN (BUILT_IN_ATAN2) |
1504 | CASE_MATHFN (BUILT_IN_ATANH) | |
1505 | CASE_MATHFN (BUILT_IN_CBRT) | |
07976da7 | 1506 | CASE_MATHFN (BUILT_IN_CEIL) |
746114e8 | 1507 | CASE_MATHFN (BUILT_IN_COPYSIGN) |
07976da7 | 1508 | CASE_MATHFN (BUILT_IN_COS) |
746114e8 | 1509 | CASE_MATHFN (BUILT_IN_COSH) |
1510 | CASE_MATHFN (BUILT_IN_DREM) | |
1511 | CASE_MATHFN (BUILT_IN_ERF) | |
1512 | CASE_MATHFN (BUILT_IN_ERFC) | |
07976da7 | 1513 | CASE_MATHFN (BUILT_IN_EXP) |
746114e8 | 1514 | CASE_MATHFN (BUILT_IN_EXP10) |
1515 | CASE_MATHFN (BUILT_IN_EXP2) | |
1516 | CASE_MATHFN (BUILT_IN_EXPM1) | |
1517 | CASE_MATHFN (BUILT_IN_FABS) | |
1518 | CASE_MATHFN (BUILT_IN_FDIM) | |
07976da7 | 1519 | CASE_MATHFN (BUILT_IN_FLOOR) |
746114e8 | 1520 | CASE_MATHFN (BUILT_IN_FMA) |
1521 | CASE_MATHFN (BUILT_IN_FMAX) | |
1522 | CASE_MATHFN (BUILT_IN_FMIN) | |
1523 | CASE_MATHFN (BUILT_IN_FMOD) | |
1524 | CASE_MATHFN (BUILT_IN_FREXP) | |
1525 | CASE_MATHFN (BUILT_IN_GAMMA) | |
1526 | CASE_MATHFN (BUILT_IN_HUGE_VAL) | |
1527 | CASE_MATHFN (BUILT_IN_HYPOT) | |
1528 | CASE_MATHFN (BUILT_IN_ILOGB) | |
1529 | CASE_MATHFN (BUILT_IN_INF) | |
1530 | CASE_MATHFN (BUILT_IN_J0) | |
1531 | CASE_MATHFN (BUILT_IN_J1) | |
1532 | CASE_MATHFN (BUILT_IN_JN) | |
1533 | CASE_MATHFN (BUILT_IN_LDEXP) | |
1534 | CASE_MATHFN (BUILT_IN_LGAMMA) | |
1535 | CASE_MATHFN (BUILT_IN_LLRINT) | |
1536 | CASE_MATHFN (BUILT_IN_LLROUND) | |
07976da7 | 1537 | CASE_MATHFN (BUILT_IN_LOG) |
746114e8 | 1538 | CASE_MATHFN (BUILT_IN_LOG10) |
1539 | CASE_MATHFN (BUILT_IN_LOG1P) | |
1540 | CASE_MATHFN (BUILT_IN_LOG2) | |
1541 | CASE_MATHFN (BUILT_IN_LOGB) | |
1542 | CASE_MATHFN (BUILT_IN_LRINT) | |
1543 | CASE_MATHFN (BUILT_IN_LROUND) | |
1544 | CASE_MATHFN (BUILT_IN_MODF) | |
1545 | CASE_MATHFN (BUILT_IN_NAN) | |
1546 | CASE_MATHFN (BUILT_IN_NANS) | |
07976da7 | 1547 | CASE_MATHFN (BUILT_IN_NEARBYINT) |
746114e8 | 1548 | CASE_MATHFN (BUILT_IN_NEXTAFTER) |
1549 | CASE_MATHFN (BUILT_IN_NEXTTOWARD) | |
1550 | CASE_MATHFN (BUILT_IN_POW) | |
1551 | CASE_MATHFN (BUILT_IN_POW10) | |
1552 | CASE_MATHFN (BUILT_IN_REMAINDER) | |
1553 | CASE_MATHFN (BUILT_IN_REMQUO) | |
1554 | CASE_MATHFN (BUILT_IN_RINT) | |
07976da7 | 1555 | CASE_MATHFN (BUILT_IN_ROUND) |
746114e8 | 1556 | CASE_MATHFN (BUILT_IN_SCALB) |
1557 | CASE_MATHFN (BUILT_IN_SCALBLN) | |
1558 | CASE_MATHFN (BUILT_IN_SCALBN) | |
1559 | CASE_MATHFN (BUILT_IN_SIGNIFICAND) | |
07976da7 | 1560 | CASE_MATHFN (BUILT_IN_SIN) |
746114e8 | 1561 | CASE_MATHFN (BUILT_IN_SINCOS) |
1562 | CASE_MATHFN (BUILT_IN_SINH) | |
07976da7 | 1563 | CASE_MATHFN (BUILT_IN_SQRT) |
1564 | CASE_MATHFN (BUILT_IN_TAN) | |
746114e8 | 1565 | CASE_MATHFN (BUILT_IN_TANH) |
1566 | CASE_MATHFN (BUILT_IN_TGAMMA) | |
07976da7 | 1567 | CASE_MATHFN (BUILT_IN_TRUNC) |
746114e8 | 1568 | CASE_MATHFN (BUILT_IN_Y0) |
1569 | CASE_MATHFN (BUILT_IN_Y1) | |
1570 | CASE_MATHFN (BUILT_IN_YN) | |
07976da7 | 1571 | |
0a68165a | 1572 | default: |
07976da7 | 1573 | return 0; |
0a68165a | 1574 | } |
07976da7 | 1575 | |
1576 | if (type_mode == TYPE_MODE (double_type_node)) | |
1577 | return implicit_built_in_decls[fcode]; | |
1578 | else if (type_mode == TYPE_MODE (float_type_node)) | |
1579 | return implicit_built_in_decls[fcodef]; | |
1580 | else if (type_mode == TYPE_MODE (long_double_type_node)) | |
1581 | return implicit_built_in_decls[fcodel]; | |
1582 | else | |
1583 | return 0; | |
0a68165a | 1584 | } |
1585 | ||
0fd605a5 | 1586 | /* If errno must be maintained, expand the RTL to check if the result, |
1587 | TARGET, of a built-in function call, EXP, is NaN, and if so set | |
1588 | errno to EDOM. */ | |
1589 | ||
1590 | static void | |
aecda0d6 | 1591 | expand_errno_check (tree exp, rtx target) |
0fd605a5 | 1592 | { |
7f05340e | 1593 | rtx lab = gen_label_rtx (); |
0fd605a5 | 1594 | |
7f05340e | 1595 | /* Test the result; if it is NaN, set errno=EDOM because |
1596 | the argument was not in the domain. */ | |
1597 | emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target), | |
1598 | 0, lab); | |
0fd605a5 | 1599 | |
1600 | #ifdef TARGET_EDOM | |
7f05340e | 1601 | /* If this built-in doesn't throw an exception, set errno directly. */ |
1602 | if (TREE_NOTHROW (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))) | |
1603 | { | |
0fd605a5 | 1604 | #ifdef GEN_ERRNO_RTX |
7f05340e | 1605 | rtx errno_rtx = GEN_ERRNO_RTX; |
0fd605a5 | 1606 | #else |
7f05340e | 1607 | rtx errno_rtx |
0fd605a5 | 1608 | = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno")); |
1609 | #endif | |
7f05340e | 1610 | emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM)); |
0fd605a5 | 1611 | emit_label (lab); |
7f05340e | 1612 | return; |
0fd605a5 | 1613 | } |
7f05340e | 1614 | #endif |
1615 | ||
1616 | /* We can't set errno=EDOM directly; let the library call do it. | |
1617 | Pop the arguments right away in case the call gets deleted. */ | |
1618 | NO_DEFER_POP; | |
1619 | expand_call (exp, target, 0); | |
1620 | OK_DEFER_POP; | |
1621 | emit_label (lab); | |
0fd605a5 | 1622 | } |
1623 | ||
1624 | ||
53800dbe | 1625 | /* Expand a call to one of the builtin math functions (sin, cos, or sqrt). |
1626 | Return 0 if a normal call should be emitted rather than expanding the | |
1627 | function in-line. EXP is the expression that is a call to the builtin | |
1628 | function; if convenient, the result should be placed in TARGET. | |
1629 | SUBTARGET may be used as the target for computing one of EXP's operands. */ | |
27d0c333 | 1630 | |
53800dbe | 1631 | static rtx |
aecda0d6 | 1632 | expand_builtin_mathfn (tree exp, rtx target, rtx subtarget) |
53800dbe | 1633 | { |
bf8e3599 | 1634 | optab builtin_optab; |
bd421108 | 1635 | rtx op0, insns, before_call; |
c6e6ecb1 | 1636 | tree fndecl = get_callee_fndecl (exp); |
53800dbe | 1637 | tree arglist = TREE_OPERAND (exp, 1); |
7f05340e | 1638 | enum machine_mode mode; |
528ee710 | 1639 | bool errno_set = false; |
68e6cb9d | 1640 | tree arg, narg; |
53800dbe | 1641 | |
0eb671f7 | 1642 | if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) |
53800dbe | 1643 | return 0; |
1644 | ||
7f05340e | 1645 | arg = TREE_VALUE (arglist); |
53800dbe | 1646 | |
1647 | switch (DECL_FUNCTION_CODE (fndecl)) | |
1648 | { | |
d2d4bdde | 1649 | case BUILT_IN_SIN: |
1650 | case BUILT_IN_SINF: | |
1651 | case BUILT_IN_SINL: | |
53800dbe | 1652 | builtin_optab = sin_optab; break; |
d2d4bdde | 1653 | case BUILT_IN_COS: |
1654 | case BUILT_IN_COSF: | |
1655 | case BUILT_IN_COSL: | |
53800dbe | 1656 | builtin_optab = cos_optab; break; |
75357f66 | 1657 | case BUILT_IN_SQRT: |
d2d4bdde | 1658 | case BUILT_IN_SQRTF: |
1659 | case BUILT_IN_SQRTL: | |
7f05340e | 1660 | errno_set = ! tree_expr_nonnegative_p (arg); |
1661 | builtin_optab = sqrt_optab; | |
1662 | break; | |
42721db0 | 1663 | case BUILT_IN_EXP: |
1664 | case BUILT_IN_EXPF: | |
1665 | case BUILT_IN_EXPL: | |
528ee710 | 1666 | errno_set = true; builtin_optab = exp_optab; break; |
42721db0 | 1667 | case BUILT_IN_LOG: |
1668 | case BUILT_IN_LOGF: | |
1669 | case BUILT_IN_LOGL: | |
528ee710 | 1670 | errno_set = true; builtin_optab = log_optab; break; |
1671 | case BUILT_IN_TAN: | |
1672 | case BUILT_IN_TANF: | |
1673 | case BUILT_IN_TANL: | |
1674 | builtin_optab = tan_optab; break; | |
1675 | case BUILT_IN_ATAN: | |
1676 | case BUILT_IN_ATANF: | |
1677 | case BUILT_IN_ATANL: | |
1678 | builtin_optab = atan_optab; break; | |
805e22b2 | 1679 | case BUILT_IN_FLOOR: |
1680 | case BUILT_IN_FLOORF: | |
1681 | case BUILT_IN_FLOORL: | |
528ee710 | 1682 | builtin_optab = floor_optab; break; |
805e22b2 | 1683 | case BUILT_IN_CEIL: |
1684 | case BUILT_IN_CEILF: | |
1685 | case BUILT_IN_CEILL: | |
528ee710 | 1686 | builtin_optab = ceil_optab; break; |
805e22b2 | 1687 | case BUILT_IN_TRUNC: |
1688 | case BUILT_IN_TRUNCF: | |
1689 | case BUILT_IN_TRUNCL: | |
528ee710 | 1690 | builtin_optab = trunc_optab; break; |
805e22b2 | 1691 | case BUILT_IN_ROUND: |
1692 | case BUILT_IN_ROUNDF: | |
1693 | case BUILT_IN_ROUNDL: | |
528ee710 | 1694 | builtin_optab = round_optab; break; |
805e22b2 | 1695 | case BUILT_IN_NEARBYINT: |
1696 | case BUILT_IN_NEARBYINTF: | |
1697 | case BUILT_IN_NEARBYINTL: | |
528ee710 | 1698 | builtin_optab = nearbyint_optab; break; |
42721db0 | 1699 | default: |
53800dbe | 1700 | abort (); |
1701 | } | |
1702 | ||
7f05340e | 1703 | /* Make a suitable register to place result in. */ |
1704 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
fc4eef90 | 1705 | |
7f05340e | 1706 | if (! flag_errno_math || ! HONOR_NANS (mode)) |
1707 | errno_set = false; | |
1708 | ||
bd421108 | 1709 | /* Before working hard, check whether the instruction is available. */ |
1710 | if (builtin_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing) | |
68e6cb9d | 1711 | { |
bd421108 | 1712 | target = gen_reg_rtx (mode); |
7f05340e | 1713 | |
bd421108 | 1714 | /* Wrap the computation of the argument in a SAVE_EXPR, as we may |
1715 | need to expand the argument again. This way, we will not perform | |
1716 | side-effects more the once. */ | |
1717 | narg = save_expr (arg); | |
1718 | if (narg != arg) | |
1719 | { | |
1720 | arglist = build_tree_list (NULL_TREE, arg); | |
1721 | exp = build_function_call_expr (fndecl, arglist); | |
1722 | } | |
7f05340e | 1723 | |
bd421108 | 1724 | op0 = expand_expr (arg, subtarget, VOIDmode, 0); |
7f05340e | 1725 | |
bd421108 | 1726 | emit_queue (); |
1727 | start_sequence (); | |
53800dbe | 1728 | |
bd421108 | 1729 | /* Compute into TARGET. |
1730 | Set TARGET to wherever the result comes back. */ | |
1731 | target = expand_unop (mode, builtin_optab, op0, target, 0); | |
1732 | ||
1733 | if (target != 0) | |
1734 | { | |
1735 | if (errno_set) | |
1736 | expand_errno_check (exp, target); | |
1737 | ||
1738 | /* Output the entire sequence. */ | |
1739 | insns = get_insns (); | |
1740 | end_sequence (); | |
1741 | emit_insn (insns); | |
1742 | return target; | |
1743 | } | |
1744 | ||
1745 | /* If we were unable to expand via the builtin, stop the sequence | |
1746 | (without outputting the insns) and call to the library function | |
1747 | with the stabilized argument list. */ | |
53800dbe | 1748 | end_sequence (); |
53800dbe | 1749 | } |
1750 | ||
bd421108 | 1751 | before_call = get_last_insn (); |
1752 | ||
1753 | target = expand_call (exp, target, target == const0_rtx); | |
53800dbe | 1754 | |
bd421108 | 1755 | /* If this is a sqrt operation and we don't care about errno, try to |
1756 | attach a REG_EQUAL note with a SQRT rtx to the emitted libcall. | |
1757 | This allows the semantics of the libcall to be visible to the RTL | |
1758 | optimizers. */ | |
1759 | if (builtin_optab == sqrt_optab && !errno_set) | |
1760 | { | |
1761 | /* Search backwards through the insns emitted by expand_call looking | |
1762 | for the instruction with the REG_RETVAL note. */ | |
1763 | rtx last = get_last_insn (); | |
1764 | while (last != before_call) | |
1765 | { | |
1766 | if (find_reg_note (last, REG_RETVAL, NULL)) | |
1767 | { | |
1768 | rtx note = find_reg_note (last, REG_EQUAL, NULL); | |
1769 | /* Check that the REQ_EQUAL note is an EXPR_LIST with | |
1770 | two elements, i.e. symbol_ref(sqrt) and the operand. */ | |
1771 | if (note | |
1772 | && GET_CODE (note) == EXPR_LIST | |
1773 | && GET_CODE (XEXP (note, 0)) == EXPR_LIST | |
1774 | && XEXP (XEXP (note, 0), 1) != NULL_RTX | |
1775 | && XEXP (XEXP (XEXP (note, 0), 1), 1) == NULL_RTX) | |
1776 | { | |
1777 | rtx operand = XEXP (XEXP (XEXP (note, 0), 1), 0); | |
1778 | /* Check operand is a register with expected mode. */ | |
1779 | if (operand | |
1780 | && GET_CODE (operand) == REG | |
1781 | && GET_MODE (operand) == mode) | |
1782 | { | |
1783 | /* Replace the REG_EQUAL note with a SQRT rtx. */ | |
1784 | rtx equiv = gen_rtx_SQRT (mode, operand); | |
1785 | set_unique_reg_note (last, REG_EQUAL, equiv); | |
1786 | } | |
1787 | } | |
1788 | break; | |
1789 | } | |
1790 | last = PREV_INSN (last); | |
1791 | } | |
1792 | } | |
0fd605a5 | 1793 | |
1794 | return target; | |
1795 | } | |
1796 | ||
1797 | /* Expand a call to the builtin binary math functions (pow and atan2). | |
1798 | Return 0 if a normal call should be emitted rather than expanding the | |
1799 | function in-line. EXP is the expression that is a call to the builtin | |
1800 | function; if convenient, the result should be placed in TARGET. | |
1801 | SUBTARGET may be used as the target for computing one of EXP's | |
1802 | operands. */ | |
1803 | ||
1804 | static rtx | |
aecda0d6 | 1805 | expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget) |
0fd605a5 | 1806 | { |
1807 | optab builtin_optab; | |
1808 | rtx op0, op1, insns; | |
c6e6ecb1 | 1809 | tree fndecl = get_callee_fndecl (exp); |
0fd605a5 | 1810 | tree arglist = TREE_OPERAND (exp, 1); |
68e6cb9d | 1811 | tree arg0, arg1, temp, narg; |
7f05340e | 1812 | enum machine_mode mode; |
0fd605a5 | 1813 | bool errno_set = true; |
1814 | bool stable = true; | |
1815 | ||
1816 | if (!validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE)) | |
1817 | return 0; | |
1818 | ||
1819 | arg0 = TREE_VALUE (arglist); | |
1820 | arg1 = TREE_VALUE (TREE_CHAIN (arglist)); | |
1821 | ||
0fd605a5 | 1822 | switch (DECL_FUNCTION_CODE (fndecl)) |
1823 | { | |
1824 | case BUILT_IN_POW: | |
1825 | case BUILT_IN_POWF: | |
1826 | case BUILT_IN_POWL: | |
1827 | builtin_optab = pow_optab; break; | |
1828 | case BUILT_IN_ATAN2: | |
1829 | case BUILT_IN_ATAN2F: | |
1830 | case BUILT_IN_ATAN2L: | |
1831 | builtin_optab = atan2_optab; break; | |
1832 | default: | |
1833 | abort (); | |
1834 | } | |
1835 | ||
7f05340e | 1836 | /* Make a suitable register to place result in. */ |
1837 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
fc4eef90 | 1838 | |
1839 | /* Before working hard, check whether the instruction is available. */ | |
1840 | if (builtin_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing) | |
1841 | return 0; | |
1842 | ||
7f05340e | 1843 | target = gen_reg_rtx (mode); |
1844 | ||
1845 | if (! flag_errno_math || ! HONOR_NANS (mode)) | |
1846 | errno_set = false; | |
1847 | ||
68e6cb9d | 1848 | /* Alway stabilize the argument list. */ |
1849 | narg = save_expr (arg1); | |
1850 | if (narg != arg1) | |
7f05340e | 1851 | { |
68e6cb9d | 1852 | temp = build_tree_list (NULL_TREE, narg); |
1853 | stable = false; | |
1854 | } | |
1855 | else | |
1856 | temp = TREE_CHAIN (arglist); | |
7f05340e | 1857 | |
68e6cb9d | 1858 | narg = save_expr (arg0); |
1859 | if (narg != arg0) | |
1860 | { | |
1861 | arglist = tree_cons (NULL_TREE, narg, temp); | |
1862 | stable = false; | |
7f05340e | 1863 | } |
68e6cb9d | 1864 | else if (! stable) |
1865 | arglist = tree_cons (NULL_TREE, arg0, temp); | |
1866 | ||
1867 | if (! stable) | |
1868 | exp = build_function_call_expr (fndecl, arglist); | |
7f05340e | 1869 | |
1870 | op0 = expand_expr (arg0, subtarget, VOIDmode, 0); | |
1871 | op1 = expand_expr (arg1, 0, VOIDmode, 0); | |
1872 | ||
1873 | emit_queue (); | |
1874 | start_sequence (); | |
1875 | ||
0fd605a5 | 1876 | /* Compute into TARGET. |
1877 | Set TARGET to wherever the result comes back. */ | |
7f05340e | 1878 | target = expand_binop (mode, builtin_optab, op0, op1, |
0fd605a5 | 1879 | target, 0, OPTAB_DIRECT); |
53800dbe | 1880 | |
68e6cb9d | 1881 | /* If we were unable to expand via the builtin, stop the sequence |
1882 | (without outputting the insns) and call to the library function | |
1883 | with the stabilized argument list. */ | |
0fd605a5 | 1884 | if (target == 0) |
1885 | { | |
1886 | end_sequence (); | |
68e6cb9d | 1887 | return expand_call (exp, target, target == const0_rtx); |
53800dbe | 1888 | } |
1889 | ||
a4356fb9 | 1890 | if (errno_set) |
1891 | expand_errno_check (exp, target); | |
0fd605a5 | 1892 | |
53800dbe | 1893 | /* Output the entire sequence. */ |
1894 | insns = get_insns (); | |
1895 | end_sequence (); | |
31d3e01c | 1896 | emit_insn (insns); |
bf8e3599 | 1897 | |
53800dbe | 1898 | return target; |
1899 | } | |
1900 | ||
f1b844c6 | 1901 | /* To evaluate powi(x,n), the floating point value x raised to the |
1902 | constant integer exponent n, we use a hybrid algorithm that | |
1903 | combines the "window method" with look-up tables. For an | |
1904 | introduction to exponentiation algorithms and "addition chains", | |
1905 | see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth, | |
1906 | "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming", | |
1907 | 3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation | |
1908 | Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */ | |
1909 | ||
1910 | /* Provide a default value for POWI_MAX_MULTS, the maximum number of | |
1911 | multiplications to inline before calling the system library's pow | |
1912 | function. powi(x,n) requires at worst 2*bits(n)-2 multiplications, | |
1913 | so this default never requires calling pow, powf or powl. */ | |
1914 | ||
1915 | #ifndef POWI_MAX_MULTS | |
1916 | #define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2) | |
1917 | #endif | |
1918 | ||
1919 | /* The size of the "optimal power tree" lookup table. All | |
1920 | exponents less than this value are simply looked up in the | |
1921 | powi_table below. This threshold is also used to size the | |
1922 | cache of pseudo registers that hold intermediate results. */ | |
1923 | #define POWI_TABLE_SIZE 256 | |
1924 | ||
1925 | /* The size, in bits of the window, used in the "window method" | |
1926 | exponentiation algorithm. This is equivalent to a radix of | |
1927 | (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */ | |
1928 | #define POWI_WINDOW_SIZE 3 | |
1929 | ||
1930 | /* The following table is an efficient representation of an | |
1931 | "optimal power tree". For each value, i, the corresponding | |
1932 | value, j, in the table states than an optimal evaluation | |
1933 | sequence for calculating pow(x,i) can be found by evaluating | |
1934 | pow(x,j)*pow(x,i-j). An optimal power tree for the first | |
1935 | 100 integers is given in Knuth's "Seminumerical algorithms". */ | |
1936 | ||
1937 | static const unsigned char powi_table[POWI_TABLE_SIZE] = | |
1938 | { | |
1939 | 0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */ | |
1940 | 4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */ | |
1941 | 8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */ | |
1942 | 12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */ | |
1943 | 16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */ | |
1944 | 20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */ | |
1945 | 24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */ | |
1946 | 28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */ | |
1947 | 32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */ | |
1948 | 36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */ | |
1949 | 40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */ | |
1950 | 44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */ | |
1951 | 48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */ | |
1952 | 52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */ | |
1953 | 56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */ | |
1954 | 60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */ | |
1955 | 64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */ | |
1956 | 68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */ | |
1957 | 72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */ | |
1958 | 76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */ | |
1959 | 80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */ | |
1960 | 84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */ | |
1961 | 88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */ | |
1962 | 92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */ | |
1963 | 96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */ | |
1964 | 100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */ | |
1965 | 104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */ | |
1966 | 108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */ | |
1967 | 112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */ | |
1968 | 116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */ | |
1969 | 120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */ | |
1970 | 124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */ | |
1971 | }; | |
1972 | ||
1973 | ||
1974 | /* Return the number of multiplications required to calculate | |
1975 | powi(x,n) where n is less than POWI_TABLE_SIZE. This is a | |
1976 | subroutine of powi_cost. CACHE is an array indicating | |
1977 | which exponents have already been calculated. */ | |
1978 | ||
1979 | static int | |
1980 | powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache) | |
1981 | { | |
1982 | /* If we've already calculated this exponent, then this evaluation | |
1983 | doesn't require any additional multiplications. */ | |
1984 | if (cache[n]) | |
1985 | return 0; | |
1986 | ||
1987 | cache[n] = true; | |
1988 | return powi_lookup_cost (n - powi_table[n], cache) | |
1989 | + powi_lookup_cost (powi_table[n], cache) + 1; | |
1990 | } | |
1991 | ||
1992 | /* Return the number of multiplications required to calculate | |
1993 | powi(x,n) for an arbitrary x, given the exponent N. This | |
1994 | function needs to be kept in sync with expand_powi below. */ | |
1995 | ||
1996 | static int | |
1997 | powi_cost (HOST_WIDE_INT n) | |
1998 | { | |
1999 | bool cache[POWI_TABLE_SIZE]; | |
2000 | unsigned HOST_WIDE_INT digit; | |
2001 | unsigned HOST_WIDE_INT val; | |
2002 | int result; | |
2003 | ||
2004 | if (n == 0) | |
2005 | return 0; | |
2006 | ||
2007 | /* Ignore the reciprocal when calculating the cost. */ | |
2008 | val = (n < 0) ? -n : n; | |
2009 | ||
2010 | /* Initialize the exponent cache. */ | |
2011 | memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool)); | |
2012 | cache[1] = true; | |
2013 | ||
2014 | result = 0; | |
2015 | ||
2016 | while (val >= POWI_TABLE_SIZE) | |
2017 | { | |
2018 | if (val & 1) | |
2019 | { | |
2020 | digit = val & ((1 << POWI_WINDOW_SIZE) - 1); | |
2021 | result += powi_lookup_cost (digit, cache) | |
2022 | + POWI_WINDOW_SIZE + 1; | |
2023 | val >>= POWI_WINDOW_SIZE; | |
2024 | } | |
2025 | else | |
2026 | { | |
2027 | val >>= 1; | |
2028 | result++; | |
2029 | } | |
2030 | } | |
2031 | ||
6881dbab | 2032 | return result + powi_lookup_cost (val, cache); |
f1b844c6 | 2033 | } |
2034 | ||
2035 | /* Recursive subroutine of expand_powi. This function takes the array, | |
2036 | CACHE, of already calculated exponents and an exponent N and returns | |
2037 | an RTX that corresponds to CACHE[1]**N, as calculated in mode MODE. */ | |
2038 | ||
2039 | static rtx | |
2040 | expand_powi_1 (enum machine_mode mode, unsigned HOST_WIDE_INT n, rtx *cache) | |
2041 | { | |
2042 | unsigned HOST_WIDE_INT digit; | |
2043 | rtx target, result; | |
2044 | rtx op0, op1; | |
2045 | ||
2046 | if (n < POWI_TABLE_SIZE) | |
2047 | { | |
2048 | if (cache[n]) | |
2049 | return cache[n]; | |
2050 | ||
2051 | target = gen_reg_rtx (mode); | |
2052 | cache[n] = target; | |
2053 | ||
2054 | op0 = expand_powi_1 (mode, n - powi_table[n], cache); | |
2055 | op1 = expand_powi_1 (mode, powi_table[n], cache); | |
2056 | } | |
2057 | else if (n & 1) | |
2058 | { | |
2059 | target = gen_reg_rtx (mode); | |
2060 | digit = n & ((1 << POWI_WINDOW_SIZE) - 1); | |
2061 | op0 = expand_powi_1 (mode, n - digit, cache); | |
2062 | op1 = expand_powi_1 (mode, digit, cache); | |
2063 | } | |
2064 | else | |
2065 | { | |
2066 | target = gen_reg_rtx (mode); | |
2067 | op0 = expand_powi_1 (mode, n >> 1, cache); | |
2068 | op1 = op0; | |
2069 | } | |
2070 | ||
2071 | result = expand_mult (mode, op0, op1, target, 0); | |
2072 | if (result != target) | |
2073 | emit_move_insn (target, result); | |
2074 | return target; | |
2075 | } | |
2076 | ||
2077 | /* Expand the RTL to evaluate powi(x,n) in mode MODE. X is the | |
2078 | floating point operand in mode MODE, and N is the exponent. This | |
2079 | function needs to be kept in sync with powi_cost above. */ | |
2080 | ||
2081 | static rtx | |
2082 | expand_powi (rtx x, enum machine_mode mode, HOST_WIDE_INT n) | |
2083 | { | |
2084 | unsigned HOST_WIDE_INT val; | |
2085 | rtx cache[POWI_TABLE_SIZE]; | |
2086 | rtx result; | |
2087 | ||
2088 | if (n == 0) | |
2089 | return CONST1_RTX (mode); | |
2090 | ||
2091 | val = (n < 0) ? -n : n; | |
2092 | ||
19bf118a | 2093 | memset (cache, 0, sizeof (cache)); |
f1b844c6 | 2094 | cache[1] = x; |
2095 | ||
2096 | result = expand_powi_1 (mode, (n < 0) ? -n : n, cache); | |
2097 | ||
2098 | /* If the original exponent was negative, reciprocate the result. */ | |
2099 | if (n < 0) | |
2100 | result = expand_binop (mode, sdiv_optab, CONST1_RTX (mode), | |
2101 | result, NULL_RTX, 0, OPTAB_LIB_WIDEN); | |
2102 | ||
2103 | return result; | |
2104 | } | |
2105 | ||
2106 | /* Expand a call to the pow built-in mathematical function. Return 0 if | |
2107 | a normal call should be emitted rather than expanding the function | |
2108 | in-line. EXP is the expression that is a call to the builtin | |
2109 | function; if convenient, the result should be placed in TARGET. */ | |
2110 | ||
2111 | static rtx | |
2112 | expand_builtin_pow (tree exp, rtx target, rtx subtarget) | |
2113 | { | |
2114 | tree arglist = TREE_OPERAND (exp, 1); | |
2115 | tree arg0, arg1; | |
2116 | ||
2117 | if (! validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE)) | |
2118 | return 0; | |
2119 | ||
2120 | arg0 = TREE_VALUE (arglist); | |
2121 | arg1 = TREE_VALUE (TREE_CHAIN (arglist)); | |
2122 | ||
8c5cac78 | 2123 | if (TREE_CODE (arg1) == REAL_CST |
f1b844c6 | 2124 | && ! TREE_CONSTANT_OVERFLOW (arg1)) |
2125 | { | |
2126 | REAL_VALUE_TYPE cint; | |
2127 | REAL_VALUE_TYPE c; | |
2128 | HOST_WIDE_INT n; | |
2129 | ||
2130 | c = TREE_REAL_CST (arg1); | |
2131 | n = real_to_integer (&c); | |
2132 | real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0); | |
8c5cac78 | 2133 | if (real_identical (&c, &cint)) |
f1b844c6 | 2134 | { |
8c5cac78 | 2135 | /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact. |
2136 | Otherwise, check the number of multiplications required. | |
2137 | Note that pow never sets errno for an integer exponent. */ | |
2138 | if ((n >= -1 && n <= 2) | |
2139 | || (flag_unsafe_math_optimizations | |
2140 | && ! optimize_size | |
2141 | && powi_cost (n) <= POWI_MAX_MULTS)) | |
2142 | { | |
2143 | enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp)); | |
2144 | rtx op = expand_expr (arg0, subtarget, VOIDmode, 0); | |
2145 | op = force_reg (mode, op); | |
2146 | return expand_powi (op, mode, n); | |
2147 | } | |
f1b844c6 | 2148 | } |
2149 | } | |
2150 | return expand_builtin_mathfn_2 (exp, target, NULL_RTX); | |
2151 | } | |
2152 | ||
53800dbe | 2153 | /* Expand expression EXP which is a call to the strlen builtin. Return 0 |
2154 | if we failed the caller should emit a normal call, otherwise | |
aed0bd19 | 2155 | try to get the result in TARGET, if convenient. */ |
f7c44134 | 2156 | |
53800dbe | 2157 | static rtx |
aecda0d6 | 2158 | expand_builtin_strlen (tree arglist, rtx target, |
2159 | enum machine_mode target_mode) | |
53800dbe | 2160 | { |
0eb671f7 | 2161 | if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE)) |
53800dbe | 2162 | return 0; |
2163 | else | |
2164 | { | |
911c0150 | 2165 | rtx pat; |
6248e345 | 2166 | tree len, src = TREE_VALUE (arglist); |
911c0150 | 2167 | rtx result, src_reg, char_rtx, before_strlen; |
80cd7a5e | 2168 | enum machine_mode insn_mode = target_mode, char_mode; |
ef2c4a29 | 2169 | enum insn_code icode = CODE_FOR_nothing; |
6248e345 | 2170 | int align; |
2171 | ||
2172 | /* If the length can be computed at compile-time, return it. */ | |
681fab1e | 2173 | len = c_strlen (src, 0); |
6248e345 | 2174 | if (len) |
80cd7a5e | 2175 | return expand_expr (len, target, target_mode, EXPAND_NORMAL); |
6248e345 | 2176 | |
681fab1e | 2177 | /* If the length can be computed at compile-time and is constant |
2178 | integer, but there are side-effects in src, evaluate | |
2179 | src for side-effects, then return len. | |
2180 | E.g. x = strlen (i++ ? "xfoo" + 1 : "bar"); | |
2181 | can be optimized into: i++; x = 3; */ | |
2182 | len = c_strlen (src, 1); | |
2183 | if (len && TREE_CODE (len) == INTEGER_CST) | |
2184 | { | |
2185 | expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
2186 | return expand_expr (len, target, target_mode, EXPAND_NORMAL); | |
2187 | } | |
2188 | ||
6248e345 | 2189 | align = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; |
53800dbe | 2190 | |
53800dbe | 2191 | /* If SRC is not a pointer type, don't do this operation inline. */ |
2192 | if (align == 0) | |
2193 | return 0; | |
2194 | ||
911c0150 | 2195 | /* Bail out if we can't compute strlen in the right mode. */ |
53800dbe | 2196 | while (insn_mode != VOIDmode) |
2197 | { | |
2198 | icode = strlen_optab->handlers[(int) insn_mode].insn_code; | |
2199 | if (icode != CODE_FOR_nothing) | |
c28ae87f | 2200 | break; |
53800dbe | 2201 | |
2202 | insn_mode = GET_MODE_WIDER_MODE (insn_mode); | |
2203 | } | |
2204 | if (insn_mode == VOIDmode) | |
2205 | return 0; | |
2206 | ||
2207 | /* Make a place to write the result of the instruction. */ | |
2208 | result = target; | |
2209 | if (! (result != 0 | |
2210 | && GET_CODE (result) == REG | |
2211 | && GET_MODE (result) == insn_mode | |
2212 | && REGNO (result) >= FIRST_PSEUDO_REGISTER)) | |
2213 | result = gen_reg_rtx (insn_mode); | |
2214 | ||
911c0150 | 2215 | /* Make a place to hold the source address. We will not expand |
2216 | the actual source until we are sure that the expansion will | |
2217 | not fail -- there are trees that cannot be expanded twice. */ | |
2218 | src_reg = gen_reg_rtx (Pmode); | |
53800dbe | 2219 | |
911c0150 | 2220 | /* Mark the beginning of the strlen sequence so we can emit the |
2221 | source operand later. */ | |
f0ce3b1f | 2222 | before_strlen = get_last_insn (); |
53800dbe | 2223 | |
53800dbe | 2224 | char_rtx = const0_rtx; |
f7c44134 | 2225 | char_mode = insn_data[(int) icode].operand[2].mode; |
2226 | if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx, | |
2227 | char_mode)) | |
53800dbe | 2228 | char_rtx = copy_to_mode_reg (char_mode, char_rtx); |
2229 | ||
911c0150 | 2230 | pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg), |
2231 | char_rtx, GEN_INT (align)); | |
2232 | if (! pat) | |
2233 | return 0; | |
2234 | emit_insn (pat); | |
2235 | ||
2236 | /* Now that we are assured of success, expand the source. */ | |
2237 | start_sequence (); | |
bf8e3599 | 2238 | pat = memory_address (BLKmode, |
f7c44134 | 2239 | expand_expr (src, src_reg, ptr_mode, EXPAND_SUM)); |
911c0150 | 2240 | if (pat != src_reg) |
2241 | emit_move_insn (src_reg, pat); | |
31d3e01c | 2242 | pat = get_insns (); |
911c0150 | 2243 | end_sequence (); |
bceb0d1f | 2244 | |
2245 | if (before_strlen) | |
2246 | emit_insn_after (pat, before_strlen); | |
2247 | else | |
2248 | emit_insn_before (pat, get_insns ()); | |
53800dbe | 2249 | |
2250 | /* Return the value in the proper mode for this function. */ | |
80cd7a5e | 2251 | if (GET_MODE (result) == target_mode) |
911c0150 | 2252 | target = result; |
53800dbe | 2253 | else if (target != 0) |
911c0150 | 2254 | convert_move (target, result, 0); |
53800dbe | 2255 | else |
80cd7a5e | 2256 | target = convert_to_mode (target_mode, result, 0); |
911c0150 | 2257 | |
2258 | return target; | |
53800dbe | 2259 | } |
2260 | } | |
2261 | ||
17f5ea87 | 2262 | /* Expand a call to the strstr builtin. Return 0 if we failed the |
2263 | caller should emit a normal call, otherwise try to get the result | |
2264 | in TARGET, if convenient (and in mode MODE if that's convenient). */ | |
2265 | ||
2266 | static rtx | |
aecda0d6 | 2267 | expand_builtin_strstr (tree arglist, rtx target, enum machine_mode mode) |
17f5ea87 | 2268 | { |
8a06f2d4 | 2269 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
17f5ea87 | 2270 | return 0; |
2271 | else | |
2272 | { | |
2273 | tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
7e15618b | 2274 | tree fn; |
83d79705 | 2275 | const char *p1, *p2; |
17f5ea87 | 2276 | |
83d79705 | 2277 | p2 = c_getstr (s2); |
2278 | if (p2 == NULL) | |
17f5ea87 | 2279 | return 0; |
2280 | ||
83d79705 | 2281 | p1 = c_getstr (s1); |
2282 | if (p1 != NULL) | |
2283 | { | |
2284 | const char *r = strstr (p1, p2); | |
17f5ea87 | 2285 | |
83d79705 | 2286 | if (r == NULL) |
2287 | return const0_rtx; | |
17f5ea87 | 2288 | |
83d79705 | 2289 | /* Return an offset into the constant string argument. */ |
2290 | return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1), | |
2291 | s1, ssize_int (r - p1))), | |
2292 | target, mode, EXPAND_NORMAL); | |
17f5ea87 | 2293 | } |
83d79705 | 2294 | |
2295 | if (p2[0] == '\0') | |
2296 | return expand_expr (s1, target, mode, EXPAND_NORMAL); | |
2297 | ||
2298 | if (p2[1] != '\0') | |
2299 | return 0; | |
2300 | ||
0a68165a | 2301 | fn = implicit_built_in_decls[BUILT_IN_STRCHR]; |
83d79705 | 2302 | if (!fn) |
2303 | return 0; | |
2304 | ||
2305 | /* New argument list transforming strstr(s1, s2) to | |
2306 | strchr(s1, s2[0]). */ | |
2307 | arglist = | |
2308 | build_tree_list (NULL_TREE, build_int_2 (p2[0], 0)); | |
2309 | arglist = tree_cons (NULL_TREE, s1, arglist); | |
7e15618b | 2310 | return expand_expr (build_function_call_expr (fn, arglist), |
2311 | target, mode, EXPAND_NORMAL); | |
17f5ea87 | 2312 | } |
2313 | } | |
2314 | ||
83d79705 | 2315 | /* Expand a call to the strchr builtin. Return 0 if we failed the |
46f3a74a | 2316 | caller should emit a normal call, otherwise try to get the result |
2317 | in TARGET, if convenient (and in mode MODE if that's convenient). */ | |
2318 | ||
2319 | static rtx | |
aecda0d6 | 2320 | expand_builtin_strchr (tree arglist, rtx target, enum machine_mode mode) |
46f3a74a | 2321 | { |
8a06f2d4 | 2322 | if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
46f3a74a | 2323 | return 0; |
2324 | else | |
2325 | { | |
2326 | tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
83d79705 | 2327 | const char *p1; |
46f3a74a | 2328 | |
83d79705 | 2329 | if (TREE_CODE (s2) != INTEGER_CST) |
2330 | return 0; | |
2331 | ||
2332 | p1 = c_getstr (s1); | |
2333 | if (p1 != NULL) | |
2334 | { | |
ecc318ff | 2335 | char c; |
2336 | const char *r; | |
2337 | ||
2338 | if (target_char_cast (s2, &c)) | |
2339 | return 0; | |
2340 | ||
2341 | r = strchr (p1, c); | |
83d79705 | 2342 | |
2343 | if (r == NULL) | |
46f3a74a | 2344 | return const0_rtx; |
83d79705 | 2345 | |
2346 | /* Return an offset into the constant string argument. */ | |
2347 | return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1), | |
2348 | s1, ssize_int (r - p1))), | |
2349 | target, mode, EXPAND_NORMAL); | |
46f3a74a | 2350 | } |
2351 | ||
83d79705 | 2352 | /* FIXME: Should use here strchrM optab so that ports can optimize |
2353 | this. */ | |
2354 | return 0; | |
2355 | } | |
2356 | } | |
2357 | ||
2358 | /* Expand a call to the strrchr builtin. Return 0 if we failed the | |
2359 | caller should emit a normal call, otherwise try to get the result | |
2360 | in TARGET, if convenient (and in mode MODE if that's convenient). */ | |
2361 | ||
2362 | static rtx | |
aecda0d6 | 2363 | expand_builtin_strrchr (tree arglist, rtx target, enum machine_mode mode) |
83d79705 | 2364 | { |
8a06f2d4 | 2365 | if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
83d79705 | 2366 | return 0; |
2367 | else | |
2368 | { | |
2369 | tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
7e15618b | 2370 | tree fn; |
83d79705 | 2371 | const char *p1; |
2372 | ||
2373 | if (TREE_CODE (s2) != INTEGER_CST) | |
46f3a74a | 2374 | return 0; |
2375 | ||
83d79705 | 2376 | p1 = c_getstr (s1); |
2377 | if (p1 != NULL) | |
2378 | { | |
ecc318ff | 2379 | char c; |
2380 | const char *r; | |
2381 | ||
2382 | if (target_char_cast (s2, &c)) | |
2383 | return 0; | |
2384 | ||
2385 | r = strrchr (p1, c); | |
83d79705 | 2386 | |
2387 | if (r == NULL) | |
46f3a74a | 2388 | return const0_rtx; |
46f3a74a | 2389 | |
83d79705 | 2390 | /* Return an offset into the constant string argument. */ |
2391 | return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1), | |
2392 | s1, ssize_int (r - p1))), | |
2393 | target, mode, EXPAND_NORMAL); | |
2394 | } | |
46f3a74a | 2395 | |
83d79705 | 2396 | if (! integer_zerop (s2)) |
2397 | return 0; | |
2398 | ||
0a68165a | 2399 | fn = implicit_built_in_decls[BUILT_IN_STRCHR]; |
83d79705 | 2400 | if (!fn) |
2401 | return 0; | |
2402 | ||
2403 | /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */ | |
7e15618b | 2404 | return expand_expr (build_function_call_expr (fn, arglist), |
2405 | target, mode, EXPAND_NORMAL); | |
83d79705 | 2406 | } |
2407 | } | |
2408 | ||
2409 | /* Expand a call to the strpbrk builtin. Return 0 if we failed the | |
2410 | caller should emit a normal call, otherwise try to get the result | |
2411 | in TARGET, if convenient (and in mode MODE if that's convenient). */ | |
2412 | ||
2413 | static rtx | |
aecda0d6 | 2414 | expand_builtin_strpbrk (tree arglist, rtx target, enum machine_mode mode) |
83d79705 | 2415 | { |
8a06f2d4 | 2416 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
83d79705 | 2417 | return 0; |
2418 | else | |
2419 | { | |
2420 | tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
7e15618b | 2421 | tree fn; |
83d79705 | 2422 | const char *p1, *p2; |
2423 | ||
2424 | p2 = c_getstr (s2); | |
2425 | if (p2 == NULL) | |
2426 | return 0; | |
2427 | ||
2428 | p1 = c_getstr (s1); | |
2429 | if (p1 != NULL) | |
2430 | { | |
2431 | const char *r = strpbrk (p1, p2); | |
2432 | ||
2433 | if (r == NULL) | |
2434 | return const0_rtx; | |
2435 | ||
2436 | /* Return an offset into the constant string argument. */ | |
2437 | return expand_expr (fold (build (PLUS_EXPR, TREE_TYPE (s1), | |
2438 | s1, ssize_int (r - p1))), | |
2439 | target, mode, EXPAND_NORMAL); | |
46f3a74a | 2440 | } |
83d79705 | 2441 | |
2442 | if (p2[0] == '\0') | |
2443 | { | |
2444 | /* strpbrk(x, "") == NULL. | |
2445 | Evaluate and ignore the arguments in case they had | |
2446 | side-effects. */ | |
2447 | expand_expr (s1, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
2448 | return const0_rtx; | |
2449 | } | |
2450 | ||
2451 | if (p2[1] != '\0') | |
2452 | return 0; /* Really call strpbrk. */ | |
2453 | ||
0a68165a | 2454 | fn = implicit_built_in_decls[BUILT_IN_STRCHR]; |
83d79705 | 2455 | if (!fn) |
2456 | return 0; | |
2457 | ||
2458 | /* New argument list transforming strpbrk(s1, s2) to | |
2459 | strchr(s1, s2[0]). */ | |
2460 | arglist = | |
2461 | build_tree_list (NULL_TREE, build_int_2 (p2[0], 0)); | |
2462 | arglist = tree_cons (NULL_TREE, s1, arglist); | |
7e15618b | 2463 | return expand_expr (build_function_call_expr (fn, arglist), |
2464 | target, mode, EXPAND_NORMAL); | |
46f3a74a | 2465 | } |
2466 | } | |
2467 | ||
6840589f | 2468 | /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE) |
2469 | bytes from constant string DATA + OFFSET and return it as target | |
2470 | constant. */ | |
2471 | ||
2472 | static rtx | |
aecda0d6 | 2473 | builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset, |
2474 | enum machine_mode mode) | |
6840589f | 2475 | { |
2476 | const char *str = (const char *) data; | |
2477 | ||
27d0c333 | 2478 | if (offset < 0 |
2479 | || ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode) | |
2480 | > strlen (str) + 1)) | |
6840589f | 2481 | abort (); /* Attempt to read past the end of constant string. */ |
2482 | ||
2483 | return c_readstr (str + offset, mode); | |
2484 | } | |
2485 | ||
6f428e8b | 2486 | /* Expand a call to the memcpy builtin, with arguments in ARGLIST. |
3b824fa6 | 2487 | Return 0 if we failed, the caller should emit a normal call, |
2488 | otherwise try to get the result in TARGET, if convenient (and in | |
9fe0e1b8 | 2489 | mode MODE if that's convenient). */ |
53800dbe | 2490 | static rtx |
aecda0d6 | 2491 | expand_builtin_memcpy (tree arglist, rtx target, enum machine_mode mode) |
53800dbe | 2492 | { |
0eb671f7 | 2493 | if (!validate_arglist (arglist, |
2494 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
53800dbe | 2495 | return 0; |
2496 | else | |
2497 | { | |
2498 | tree dest = TREE_VALUE (arglist); | |
2499 | tree src = TREE_VALUE (TREE_CHAIN (arglist)); | |
2500 | tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
6840589f | 2501 | const char *src_str; |
27d0c333 | 2502 | unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT); |
2503 | unsigned int dest_align | |
2504 | = get_pointer_alignment (dest, BIGGEST_ALIGNMENT); | |
53800dbe | 2505 | rtx dest_mem, src_mem, dest_addr, len_rtx; |
2506 | ||
6f428e8b | 2507 | /* If DEST is not a pointer type, call the normal function. */ |
2508 | if (dest_align == 0) | |
9342ee68 | 2509 | return 0; |
6f428e8b | 2510 | |
2511 | /* If the LEN parameter is zero, return DEST. */ | |
2512 | if (host_integerp (len, 1) && tree_low_cst (len, 1) == 0) | |
9342ee68 | 2513 | { |
2514 | /* Evaluate and ignore SRC in case it has side-effects. */ | |
2515 | expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
2516 | return expand_expr (dest, target, mode, EXPAND_NORMAL); | |
2517 | } | |
6f428e8b | 2518 | |
2519 | /* If either SRC is not a pointer type, don't do this | |
2520 | operation in-line. */ | |
2521 | if (src_align == 0) | |
9342ee68 | 2522 | return 0; |
53800dbe | 2523 | |
2524 | dest_mem = get_memory_rtx (dest); | |
2a631e19 | 2525 | set_mem_align (dest_mem, dest_align); |
53800dbe | 2526 | len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0); |
6840589f | 2527 | src_str = c_getstr (src); |
2528 | ||
2529 | /* If SRC is a string constant and block move would be done | |
2530 | by pieces, we can avoid loading the string from memory | |
2531 | and only stored the computed constants. */ | |
2532 | if (src_str | |
6840589f | 2533 | && GET_CODE (len_rtx) == CONST_INT |
2534 | && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1 | |
2535 | && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str, | |
b9a7cc69 | 2536 | (void *) src_str, dest_align)) |
6840589f | 2537 | { |
9fe0e1b8 | 2538 | dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx), |
2539 | builtin_memcpy_read_str, | |
b9a7cc69 | 2540 | (void *) src_str, dest_align, 0); |
e5716f7e | 2541 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
2542 | #ifdef POINTERS_EXTEND_UNSIGNED | |
2543 | if (GET_MODE (dest_mem) != ptr_mode) | |
2544 | dest_mem = convert_memory_address (ptr_mode, dest_mem); | |
2545 | #endif | |
9fe0e1b8 | 2546 | return dest_mem; |
6840589f | 2547 | } |
2548 | ||
2549 | src_mem = get_memory_rtx (src); | |
2a631e19 | 2550 | set_mem_align (src_mem, src_align); |
53800dbe | 2551 | |
53800dbe | 2552 | /* Copy word part most expediently. */ |
0378dbdc | 2553 | dest_addr = emit_block_move (dest_mem, src_mem, len_rtx, |
2554 | BLOCK_OP_NORMAL); | |
53800dbe | 2555 | |
2556 | if (dest_addr == 0) | |
e5716f7e | 2557 | { |
2558 | dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX); | |
2559 | #ifdef POINTERS_EXTEND_UNSIGNED | |
2560 | if (GET_MODE (dest_addr) != ptr_mode) | |
2561 | dest_addr = convert_memory_address (ptr_mode, dest_addr); | |
2562 | #endif | |
2563 | } | |
9fe0e1b8 | 2564 | return dest_addr; |
53800dbe | 2565 | } |
2566 | } | |
2567 | ||
647661c6 | 2568 | /* Expand a call to the mempcpy builtin, with arguments in ARGLIST. |
2569 | Return 0 if we failed the caller should emit a normal call, | |
2570 | otherwise try to get the result in TARGET, if convenient (and in | |
9fe0e1b8 | 2571 | mode MODE if that's convenient). If ENDP is 0 return the |
2572 | destination pointer, if ENDP is 1 return the end pointer ala | |
2573 | mempcpy, and if ENDP is 2 return the end pointer minus one ala | |
2574 | stpcpy. */ | |
647661c6 | 2575 | |
2576 | static rtx | |
aecda0d6 | 2577 | expand_builtin_mempcpy (tree arglist, rtx target, enum machine_mode mode, |
2578 | int endp) | |
647661c6 | 2579 | { |
2580 | if (!validate_arglist (arglist, | |
2581 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
2582 | return 0; | |
9fe0e1b8 | 2583 | /* If return value is ignored, transform mempcpy into memcpy. */ |
2584 | else if (target == const0_rtx) | |
2585 | { | |
2586 | tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY]; | |
2587 | ||
2588 | if (!fn) | |
2589 | return 0; | |
2590 | ||
2591 | return expand_expr (build_function_call_expr (fn, arglist), | |
2592 | target, mode, EXPAND_NORMAL); | |
2593 | } | |
647661c6 | 2594 | else |
2595 | { | |
9fe0e1b8 | 2596 | tree dest = TREE_VALUE (arglist); |
2597 | tree src = TREE_VALUE (TREE_CHAIN (arglist)); | |
2598 | tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
2599 | const char *src_str; | |
2600 | unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT); | |
2601 | unsigned int dest_align | |
2602 | = get_pointer_alignment (dest, BIGGEST_ALIGNMENT); | |
2603 | rtx dest_mem, src_mem, len_rtx; | |
2604 | ||
2605 | /* If DEST is not a pointer type or LEN is not constant, | |
2606 | call the normal function. */ | |
2607 | if (dest_align == 0 || !host_integerp (len, 1)) | |
2608 | return 0; | |
2609 | ||
2610 | /* If the LEN parameter is zero, return DEST. */ | |
2611 | if (tree_low_cst (len, 1) == 0) | |
647661c6 | 2612 | { |
9fe0e1b8 | 2613 | /* Evaluate and ignore SRC in case it has side-effects. */ |
2614 | expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
2615 | return expand_expr (dest, target, mode, EXPAND_NORMAL); | |
2616 | } | |
647661c6 | 2617 | |
9fe0e1b8 | 2618 | /* If either SRC is not a pointer type, don't do this |
2619 | operation in-line. */ | |
2620 | if (src_align == 0) | |
2621 | return 0; | |
647661c6 | 2622 | |
9fe0e1b8 | 2623 | len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0); |
2624 | src_str = c_getstr (src); | |
647661c6 | 2625 | |
9fe0e1b8 | 2626 | /* If SRC is a string constant and block move would be done |
2627 | by pieces, we can avoid loading the string from memory | |
2628 | and only stored the computed constants. */ | |
2629 | if (src_str | |
2630 | && GET_CODE (len_rtx) == CONST_INT | |
2631 | && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1 | |
2632 | && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str, | |
b9a7cc69 | 2633 | (void *) src_str, dest_align)) |
9fe0e1b8 | 2634 | { |
2635 | dest_mem = get_memory_rtx (dest); | |
2636 | set_mem_align (dest_mem, dest_align); | |
2637 | dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx), | |
2638 | builtin_memcpy_read_str, | |
b9a7cc69 | 2639 | (void *) src_str, dest_align, endp); |
9fe0e1b8 | 2640 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
2641 | #ifdef POINTERS_EXTEND_UNSIGNED | |
2642 | if (GET_MODE (dest_mem) != ptr_mode) | |
2643 | dest_mem = convert_memory_address (ptr_mode, dest_mem); | |
2644 | #endif | |
2645 | return dest_mem; | |
647661c6 | 2646 | } |
2647 | ||
9fe0e1b8 | 2648 | if (GET_CODE (len_rtx) == CONST_INT |
2649 | && can_move_by_pieces (INTVAL (len_rtx), | |
2650 | MIN (dest_align, src_align))) | |
2651 | { | |
2652 | dest_mem = get_memory_rtx (dest); | |
2653 | set_mem_align (dest_mem, dest_align); | |
2654 | src_mem = get_memory_rtx (src); | |
2655 | set_mem_align (src_mem, src_align); | |
2656 | dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx), | |
2657 | MIN (dest_align, src_align), endp); | |
2658 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); | |
2659 | #ifdef POINTERS_EXTEND_UNSIGNED | |
2660 | if (GET_MODE (dest_mem) != ptr_mode) | |
2661 | dest_mem = convert_memory_address (ptr_mode, dest_mem); | |
2662 | #endif | |
2663 | return dest_mem; | |
2664 | } | |
2665 | ||
2666 | return 0; | |
647661c6 | 2667 | } |
2668 | } | |
2669 | ||
c4950093 | 2670 | /* Expand expression EXP, which is a call to the memmove builtin. Return 0 |
2671 | if we failed the caller should emit a normal call. */ | |
2672 | ||
2673 | static rtx | |
aecda0d6 | 2674 | expand_builtin_memmove (tree arglist, rtx target, enum machine_mode mode) |
c4950093 | 2675 | { |
2676 | if (!validate_arglist (arglist, | |
2677 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
2678 | return 0; | |
2679 | else | |
2680 | { | |
2681 | tree dest = TREE_VALUE (arglist); | |
2682 | tree src = TREE_VALUE (TREE_CHAIN (arglist)); | |
2683 | tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
2684 | ||
2685 | unsigned int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT); | |
2686 | unsigned int dest_align | |
2687 | = get_pointer_alignment (dest, BIGGEST_ALIGNMENT); | |
2688 | ||
2689 | /* If DEST is not a pointer type, call the normal function. */ | |
2690 | if (dest_align == 0) | |
2691 | return 0; | |
2692 | ||
2693 | /* If the LEN parameter is zero, return DEST. */ | |
2694 | if (host_integerp (len, 1) && tree_low_cst (len, 1) == 0) | |
2695 | { | |
2696 | /* Evaluate and ignore SRC in case it has side-effects. */ | |
2697 | expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
2698 | return expand_expr (dest, target, mode, EXPAND_NORMAL); | |
2699 | } | |
2700 | ||
2701 | /* If either SRC is not a pointer type, don't do this | |
2702 | operation in-line. */ | |
2703 | if (src_align == 0) | |
2704 | return 0; | |
2705 | ||
01537105 | 2706 | /* If src is categorized for a readonly section we can use |
2707 | normal memcpy. */ | |
2708 | if (readonly_data_expr (src)) | |
2709 | { | |
2710 | tree const fn = implicit_built_in_decls[BUILT_IN_MEMCPY]; | |
2711 | if (!fn) | |
2712 | return 0; | |
2713 | return expand_expr (build_function_call_expr (fn, arglist), | |
2714 | target, mode, EXPAND_NORMAL); | |
2715 | } | |
c4950093 | 2716 | |
2717 | /* Otherwise, call the normal function. */ | |
2718 | return 0; | |
2719 | } | |
2720 | } | |
2721 | ||
2722 | /* Expand expression EXP, which is a call to the bcopy builtin. Return 0 | |
2723 | if we failed the caller should emit a normal call. */ | |
2724 | ||
2725 | static rtx | |
aecda0d6 | 2726 | expand_builtin_bcopy (tree arglist) |
c4950093 | 2727 | { |
2728 | tree src, dest, size, newarglist; | |
2729 | ||
2730 | if (!validate_arglist (arglist, | |
2731 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
2732 | return NULL_RTX; | |
2733 | ||
2734 | src = TREE_VALUE (arglist); | |
2735 | dest = TREE_VALUE (TREE_CHAIN (arglist)); | |
2736 | size = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
2737 | ||
2738 | /* New argument list transforming bcopy(ptr x, ptr y, int z) to | |
2739 | memmove(ptr y, ptr x, size_t z). This is done this way | |
2740 | so that if it isn't expanded inline, we fallback to | |
2741 | calling bcopy instead of memmove. */ | |
2742 | ||
2743 | newarglist = build_tree_list (NULL_TREE, convert (sizetype, size)); | |
2744 | newarglist = tree_cons (NULL_TREE, src, newarglist); | |
2745 | newarglist = tree_cons (NULL_TREE, dest, newarglist); | |
2746 | ||
2747 | return expand_builtin_memmove (newarglist, const0_rtx, VOIDmode); | |
2748 | } | |
2749 | ||
53800dbe | 2750 | /* Expand expression EXP, which is a call to the strcpy builtin. Return 0 |
6f428e8b | 2751 | if we failed the caller should emit a normal call, otherwise try to get |
2752 | the result in TARGET, if convenient (and in mode MODE if that's | |
2753 | convenient). */ | |
902de8ed | 2754 | |
53800dbe | 2755 | static rtx |
aecda0d6 | 2756 | expand_builtin_strcpy (tree arglist, rtx target, enum machine_mode mode) |
53800dbe | 2757 | { |
6411575e | 2758 | tree fn, len, src, dst; |
53800dbe | 2759 | |
0eb671f7 | 2760 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
53800dbe | 2761 | return 0; |
53800dbe | 2762 | |
0a68165a | 2763 | fn = implicit_built_in_decls[BUILT_IN_MEMCPY]; |
6f428e8b | 2764 | if (!fn) |
2765 | return 0; | |
902de8ed | 2766 | |
6411575e | 2767 | src = TREE_VALUE (TREE_CHAIN (arglist)); |
681fab1e | 2768 | len = c_strlen (src, 1); |
5c23237b | 2769 | if (len == 0 || TREE_SIDE_EFFECTS (len)) |
6f428e8b | 2770 | return 0; |
902de8ed | 2771 | |
6411575e | 2772 | dst = TREE_VALUE (arglist); |
6f428e8b | 2773 | len = size_binop (PLUS_EXPR, len, ssize_int (1)); |
6411575e | 2774 | arglist = build_tree_list (NULL_TREE, len); |
2775 | arglist = tree_cons (NULL_TREE, src, arglist); | |
2776 | arglist = tree_cons (NULL_TREE, dst, arglist); | |
6f428e8b | 2777 | return expand_expr (build_function_call_expr (fn, arglist), |
9342ee68 | 2778 | target, mode, EXPAND_NORMAL); |
53800dbe | 2779 | } |
2780 | ||
3b824fa6 | 2781 | /* Expand a call to the stpcpy builtin, with arguments in ARGLIST. |
2782 | Return 0 if we failed the caller should emit a normal call, | |
2783 | otherwise try to get the result in TARGET, if convenient (and in | |
2784 | mode MODE if that's convenient). */ | |
2785 | ||
2786 | static rtx | |
aecda0d6 | 2787 | expand_builtin_stpcpy (tree arglist, rtx target, enum machine_mode mode) |
3b824fa6 | 2788 | { |
2789 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) | |
2790 | return 0; | |
2791 | else | |
2792 | { | |
6411575e | 2793 | tree dst, src, len; |
647661c6 | 2794 | |
2795 | /* If return value is ignored, transform stpcpy into strcpy. */ | |
2796 | if (target == const0_rtx) | |
2797 | { | |
9fe0e1b8 | 2798 | tree fn = implicit_built_in_decls[BUILT_IN_STRCPY]; |
647661c6 | 2799 | if (!fn) |
2800 | return 0; | |
2801 | ||
2802 | return expand_expr (build_function_call_expr (fn, arglist), | |
2803 | target, mode, EXPAND_NORMAL); | |
2804 | } | |
2805 | ||
9fe0e1b8 | 2806 | /* Ensure we get an actual string whose length can be evaluated at |
c04062e9 | 2807 | compile-time, not an expression containing a string. This is |
2808 | because the latter will potentially produce pessimized code | |
2809 | when used to produce the return value. */ | |
2810 | src = TREE_VALUE (TREE_CHAIN (arglist)); | |
681fab1e | 2811 | if (! c_getstr (src) || ! (len = c_strlen (src, 0))) |
3b824fa6 | 2812 | return 0; |
2813 | ||
6411575e | 2814 | dst = TREE_VALUE (arglist); |
3b824fa6 | 2815 | len = fold (size_binop (PLUS_EXPR, len, ssize_int (1))); |
6411575e | 2816 | arglist = build_tree_list (NULL_TREE, len); |
2817 | arglist = tree_cons (NULL_TREE, src, arglist); | |
2818 | arglist = tree_cons (NULL_TREE, dst, arglist); | |
2819 | return expand_builtin_mempcpy (arglist, target, mode, /*endp=*/2); | |
3b824fa6 | 2820 | } |
2821 | } | |
2822 | ||
6840589f | 2823 | /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE) |
2824 | bytes from constant string DATA + OFFSET and return it as target | |
2825 | constant. */ | |
2826 | ||
2827 | static rtx | |
aecda0d6 | 2828 | builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset, |
2829 | enum machine_mode mode) | |
6840589f | 2830 | { |
2831 | const char *str = (const char *) data; | |
2832 | ||
2833 | if ((unsigned HOST_WIDE_INT) offset > strlen (str)) | |
2834 | return const0_rtx; | |
2835 | ||
2836 | return c_readstr (str + offset, mode); | |
2837 | } | |
2838 | ||
ed09096d | 2839 | /* Expand expression EXP, which is a call to the strncpy builtin. Return 0 |
2840 | if we failed the caller should emit a normal call. */ | |
2841 | ||
2842 | static rtx | |
aecda0d6 | 2843 | expand_builtin_strncpy (tree arglist, rtx target, enum machine_mode mode) |
ed09096d | 2844 | { |
0eb671f7 | 2845 | if (!validate_arglist (arglist, |
2846 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
ed09096d | 2847 | return 0; |
2848 | else | |
2849 | { | |
681fab1e | 2850 | tree slen = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)), 1); |
ed09096d | 2851 | tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); |
6f428e8b | 2852 | tree fn; |
ed09096d | 2853 | |
2854 | /* We must be passed a constant len parameter. */ | |
2855 | if (TREE_CODE (len) != INTEGER_CST) | |
2856 | return 0; | |
2857 | ||
2858 | /* If the len parameter is zero, return the dst parameter. */ | |
df5c389e | 2859 | if (integer_zerop (len)) |
9342ee68 | 2860 | { |
2861 | /* Evaluate and ignore the src argument in case it has | |
2862 | side-effects. */ | |
ed09096d | 2863 | expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), const0_rtx, |
2864 | VOIDmode, EXPAND_NORMAL); | |
bf8e3599 | 2865 | /* Return the dst parameter. */ |
ed09096d | 2866 | return expand_expr (TREE_VALUE (arglist), target, mode, |
2867 | EXPAND_NORMAL); | |
2868 | } | |
6840589f | 2869 | |
ed09096d | 2870 | /* Now, we must be passed a constant src ptr parameter. */ |
6840589f | 2871 | if (slen == 0 || TREE_CODE (slen) != INTEGER_CST) |
ed09096d | 2872 | return 0; |
2873 | ||
2874 | slen = size_binop (PLUS_EXPR, slen, ssize_int (1)); | |
2875 | ||
2876 | /* We're required to pad with trailing zeros if the requested | |
6840589f | 2877 | len is greater than strlen(s2)+1. In that case try to |
2878 | use store_by_pieces, if it fails, punt. */ | |
ed09096d | 2879 | if (tree_int_cst_lt (slen, len)) |
6840589f | 2880 | { |
2881 | tree dest = TREE_VALUE (arglist); | |
27d0c333 | 2882 | unsigned int dest_align |
2883 | = get_pointer_alignment (dest, BIGGEST_ALIGNMENT); | |
6840589f | 2884 | const char *p = c_getstr (TREE_VALUE (TREE_CHAIN (arglist))); |
2885 | rtx dest_mem; | |
2886 | ||
27d0c333 | 2887 | if (!p || dest_align == 0 || !host_integerp (len, 1) |
2888 | || !can_store_by_pieces (tree_low_cst (len, 1), | |
6840589f | 2889 | builtin_strncpy_read_str, |
b9a7cc69 | 2890 | (void *) p, dest_align)) |
6840589f | 2891 | return 0; |
2892 | ||
2893 | dest_mem = get_memory_rtx (dest); | |
27d0c333 | 2894 | store_by_pieces (dest_mem, tree_low_cst (len, 1), |
6840589f | 2895 | builtin_strncpy_read_str, |
b9a7cc69 | 2896 | (void *) p, dest_align, 0); |
e5716f7e | 2897 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
2898 | #ifdef POINTERS_EXTEND_UNSIGNED | |
2899 | if (GET_MODE (dest_mem) != ptr_mode) | |
2900 | dest_mem = convert_memory_address (ptr_mode, dest_mem); | |
2901 | #endif | |
2902 | return dest_mem; | |
6840589f | 2903 | } |
bf8e3599 | 2904 | |
ed09096d | 2905 | /* OK transform into builtin memcpy. */ |
0a68165a | 2906 | fn = implicit_built_in_decls[BUILT_IN_MEMCPY]; |
6f428e8b | 2907 | if (!fn) |
9342ee68 | 2908 | return 0; |
6f428e8b | 2909 | return expand_expr (build_function_call_expr (fn, arglist), |
9342ee68 | 2910 | target, mode, EXPAND_NORMAL); |
ed09096d | 2911 | } |
2912 | } | |
2913 | ||
ecc318ff | 2914 | /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE) |
2915 | bytes from constant string DATA + OFFSET and return it as target | |
2916 | constant. */ | |
2917 | ||
2918 | static rtx | |
aecda0d6 | 2919 | builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED, |
2920 | enum machine_mode mode) | |
ecc318ff | 2921 | { |
2922 | const char *c = (const char *) data; | |
2923 | char *p = alloca (GET_MODE_SIZE (mode)); | |
2924 | ||
2925 | memset (p, *c, GET_MODE_SIZE (mode)); | |
2926 | ||
2927 | return c_readstr (p, mode); | |
2928 | } | |
2929 | ||
a7ec6974 | 2930 | /* Callback routine for store_by_pieces. Return the RTL of a register |
2931 | containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned | |
2932 | char value given in the RTL register data. For example, if mode is | |
2933 | 4 bytes wide, return the RTL for 0x01010101*data. */ | |
2934 | ||
2935 | static rtx | |
aecda0d6 | 2936 | builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED, |
2937 | enum machine_mode mode) | |
a7ec6974 | 2938 | { |
2939 | rtx target, coeff; | |
2940 | size_t size; | |
2941 | char *p; | |
2942 | ||
2943 | size = GET_MODE_SIZE (mode); | |
f0ce3b1f | 2944 | if (size == 1) |
2945 | return (rtx) data; | |
a7ec6974 | 2946 | |
2947 | p = alloca (size); | |
2948 | memset (p, 1, size); | |
2949 | coeff = c_readstr (p, mode); | |
2950 | ||
f0ce3b1f | 2951 | target = convert_to_mode (mode, (rtx) data, 1); |
a7ec6974 | 2952 | target = expand_mult (mode, target, coeff, NULL_RTX, 1); |
2953 | return force_reg (mode, target); | |
2954 | } | |
2955 | ||
53800dbe | 2956 | /* Expand expression EXP, which is a call to the memset builtin. Return 0 |
6f428e8b | 2957 | if we failed the caller should emit a normal call, otherwise try to get |
2958 | the result in TARGET, if convenient (and in mode MODE if that's | |
2959 | convenient). */ | |
902de8ed | 2960 | |
53800dbe | 2961 | static rtx |
aecda0d6 | 2962 | expand_builtin_memset (tree arglist, rtx target, enum machine_mode mode) |
53800dbe | 2963 | { |
0eb671f7 | 2964 | if (!validate_arglist (arglist, |
2965 | POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
53800dbe | 2966 | return 0; |
2967 | else | |
2968 | { | |
2969 | tree dest = TREE_VALUE (arglist); | |
2970 | tree val = TREE_VALUE (TREE_CHAIN (arglist)); | |
2971 | tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
ecc318ff | 2972 | char c; |
53800dbe | 2973 | |
27d0c333 | 2974 | unsigned int dest_align |
2975 | = get_pointer_alignment (dest, BIGGEST_ALIGNMENT); | |
53800dbe | 2976 | rtx dest_mem, dest_addr, len_rtx; |
2977 | ||
bf8e3599 | 2978 | /* If DEST is not a pointer type, don't do this |
53800dbe | 2979 | operation in-line. */ |
2980 | if (dest_align == 0) | |
2981 | return 0; | |
2982 | ||
6f428e8b | 2983 | /* If the LEN parameter is zero, return DEST. */ |
2984 | if (host_integerp (len, 1) && tree_low_cst (len, 1) == 0) | |
9342ee68 | 2985 | { |
2986 | /* Evaluate and ignore VAL in case it has side-effects. */ | |
2987 | expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
2988 | return expand_expr (dest, target, mode, EXPAND_NORMAL); | |
2989 | } | |
6f428e8b | 2990 | |
ecc318ff | 2991 | if (TREE_CODE (val) != INTEGER_CST) |
9342ee68 | 2992 | { |
2993 | rtx val_rtx; | |
a7ec6974 | 2994 | |
9342ee68 | 2995 | if (!host_integerp (len, 1)) |
2996 | return 0; | |
a7ec6974 | 2997 | |
9342ee68 | 2998 | if (optimize_size && tree_low_cst (len, 1) > 1) |
2999 | return 0; | |
a7ec6974 | 3000 | |
9342ee68 | 3001 | /* Assume that we can memset by pieces if we can store the |
3002 | * the coefficients by pieces (in the required modes). | |
3003 | * We can't pass builtin_memset_gen_str as that emits RTL. */ | |
3004 | c = 1; | |
a7ec6974 | 3005 | if (!can_store_by_pieces (tree_low_cst (len, 1), |
3006 | builtin_memset_read_str, | |
b9a7cc69 | 3007 | &c, dest_align)) |
a7ec6974 | 3008 | return 0; |
3009 | ||
9342ee68 | 3010 | val = fold (build1 (CONVERT_EXPR, unsigned_char_type_node, val)); |
3011 | val_rtx = expand_expr (val, NULL_RTX, VOIDmode, 0); | |
3012 | val_rtx = force_reg (TYPE_MODE (unsigned_char_type_node), | |
3013 | val_rtx); | |
a7ec6974 | 3014 | dest_mem = get_memory_rtx (dest); |
3015 | store_by_pieces (dest_mem, tree_low_cst (len, 1), | |
3016 | builtin_memset_gen_str, | |
b9a7cc69 | 3017 | val_rtx, dest_align, 0); |
e5716f7e | 3018 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
3019 | #ifdef POINTERS_EXTEND_UNSIGNED | |
3020 | if (GET_MODE (dest_mem) != ptr_mode) | |
3021 | dest_mem = convert_memory_address (ptr_mode, dest_mem); | |
3022 | #endif | |
3023 | return dest_mem; | |
9342ee68 | 3024 | } |
53800dbe | 3025 | |
ecc318ff | 3026 | if (target_char_cast (val, &c)) |
53800dbe | 3027 | return 0; |
3028 | ||
ecc318ff | 3029 | if (c) |
3030 | { | |
27d0c333 | 3031 | if (!host_integerp (len, 1)) |
ecc318ff | 3032 | return 0; |
8a06f2d4 | 3033 | if (!can_store_by_pieces (tree_low_cst (len, 1), |
b9a7cc69 | 3034 | builtin_memset_read_str, &c, |
8a06f2d4 | 3035 | dest_align)) |
ecc318ff | 3036 | return 0; |
3037 | ||
3038 | dest_mem = get_memory_rtx (dest); | |
27d0c333 | 3039 | store_by_pieces (dest_mem, tree_low_cst (len, 1), |
ecc318ff | 3040 | builtin_memset_read_str, |
b9a7cc69 | 3041 | &c, dest_align, 0); |
e5716f7e | 3042 | dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX); |
3043 | #ifdef POINTERS_EXTEND_UNSIGNED | |
3044 | if (GET_MODE (dest_mem) != ptr_mode) | |
3045 | dest_mem = convert_memory_address (ptr_mode, dest_mem); | |
3046 | #endif | |
3047 | return dest_mem; | |
ecc318ff | 3048 | } |
3049 | ||
53800dbe | 3050 | len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0); |
53800dbe | 3051 | |
3052 | dest_mem = get_memory_rtx (dest); | |
2a631e19 | 3053 | set_mem_align (dest_mem, dest_align); |
2a631e19 | 3054 | dest_addr = clear_storage (dest_mem, len_rtx); |
53800dbe | 3055 | |
3056 | if (dest_addr == 0) | |
e5716f7e | 3057 | { |
3058 | dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX); | |
3059 | #ifdef POINTERS_EXTEND_UNSIGNED | |
3060 | if (GET_MODE (dest_addr) != ptr_mode) | |
3061 | dest_addr = convert_memory_address (ptr_mode, dest_addr); | |
3062 | #endif | |
3063 | } | |
53800dbe | 3064 | |
3065 | return dest_addr; | |
3066 | } | |
3067 | } | |
3068 | ||
ffc83088 | 3069 | /* Expand expression EXP, which is a call to the bzero builtin. Return 0 |
3070 | if we failed the caller should emit a normal call. */ | |
27d0c333 | 3071 | |
ffc83088 | 3072 | static rtx |
aecda0d6 | 3073 | expand_builtin_bzero (tree arglist) |
ffc83088 | 3074 | { |
7369e7ba | 3075 | tree dest, size, newarglist; |
ffc83088 | 3076 | |
0eb671f7 | 3077 | if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
7369e7ba | 3078 | return NULL_RTX; |
ffc83088 | 3079 | |
0eb671f7 | 3080 | dest = TREE_VALUE (arglist); |
3081 | size = TREE_VALUE (TREE_CHAIN (arglist)); | |
bf8e3599 | 3082 | |
7369e7ba | 3083 | /* New argument list transforming bzero(ptr x, int y) to |
6f428e8b | 3084 | memset(ptr x, int 0, size_t y). This is done this way |
3085 | so that if it isn't expanded inline, we fallback to | |
3086 | calling bzero instead of memset. */ | |
bf8e3599 | 3087 | |
7369e7ba | 3088 | newarglist = build_tree_list (NULL_TREE, convert (sizetype, size)); |
3089 | newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist); | |
3090 | newarglist = tree_cons (NULL_TREE, dest, newarglist); | |
ffc83088 | 3091 | |
80cd7a5e | 3092 | return expand_builtin_memset (newarglist, const0_rtx, VOIDmode); |
ffc83088 | 3093 | } |
3094 | ||
7a3f89b5 | 3095 | /* Expand expression EXP, which is a call to the memcmp built-in function. |
53800dbe | 3096 | ARGLIST is the argument list for this call. Return 0 if we failed and the |
3097 | caller should emit a normal call, otherwise try to get the result in | |
6f428e8b | 3098 | TARGET, if convenient (and in mode MODE, if that's convenient). */ |
27d0c333 | 3099 | |
53800dbe | 3100 | static rtx |
aecda0d6 | 3101 | expand_builtin_memcmp (tree exp ATTRIBUTE_UNUSED, tree arglist, rtx target, |
3102 | enum machine_mode mode) | |
53800dbe | 3103 | { |
6f428e8b | 3104 | tree arg1, arg2, len; |
d470d782 | 3105 | const char *p1, *p2; |
6f428e8b | 3106 | |
0eb671f7 | 3107 | if (!validate_arglist (arglist, |
9342ee68 | 3108 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
53800dbe | 3109 | return 0; |
53800dbe | 3110 | |
6f428e8b | 3111 | arg1 = TREE_VALUE (arglist); |
3112 | arg2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
3113 | len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
3114 | ||
3115 | /* If the len parameter is zero, return zero. */ | |
3116 | if (host_integerp (len, 1) && tree_low_cst (len, 1) == 0) | |
3117 | { | |
3118 | /* Evaluate and ignore arg1 and arg2 in case they have | |
3119 | side-effects. */ | |
3120 | expand_expr (arg1, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
3121 | expand_expr (arg2, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
3122 | return const0_rtx; | |
3123 | } | |
3124 | ||
d470d782 | 3125 | p1 = c_getstr (arg1); |
3126 | p2 = c_getstr (arg2); | |
3127 | ||
3128 | /* If all arguments are constant, and the value of len is not greater | |
3129 | than the lengths of arg1 and arg2, evaluate at compile-time. */ | |
3130 | if (host_integerp (len, 1) && p1 && p2 | |
df5c389e | 3131 | && compare_tree_int (len, strlen (p1) + 1) <= 0 |
3132 | && compare_tree_int (len, strlen (p2) + 1) <= 0) | |
d470d782 | 3133 | { |
3134 | const int r = memcmp (p1, p2, tree_low_cst (len, 1)); | |
df5c389e | 3135 | |
d470d782 | 3136 | return (r < 0 ? constm1_rtx : (r > 0 ? const1_rtx : const0_rtx)); |
3137 | } | |
3138 | ||
6f428e8b | 3139 | /* If len parameter is one, return an expression corresponding to |
3140 | (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */ | |
3141 | if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1) | |
3142 | { | |
3143 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
3144 | tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node); | |
3145 | tree ind1 = | |
3146 | fold (build1 (CONVERT_EXPR, integer_type_node, | |
9342ee68 | 3147 | build1 (INDIRECT_REF, cst_uchar_node, |
3148 | build1 (NOP_EXPR, cst_uchar_ptr_node, arg1)))); | |
6f428e8b | 3149 | tree ind2 = |
3150 | fold (build1 (CONVERT_EXPR, integer_type_node, | |
9342ee68 | 3151 | build1 (INDIRECT_REF, cst_uchar_node, |
3152 | build1 (NOP_EXPR, cst_uchar_ptr_node, arg2)))); | |
6f428e8b | 3153 | tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2)); |
3154 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
3155 | } | |
3156 | ||
b428c0a5 | 3157 | #if defined HAVE_cmpmemsi || defined HAVE_cmpstrsi |
53800dbe | 3158 | { |
0cd832f0 | 3159 | rtx arg1_rtx, arg2_rtx, arg3_rtx; |
53800dbe | 3160 | rtx result; |
0cd832f0 | 3161 | rtx insn; |
53800dbe | 3162 | |
3163 | int arg1_align | |
3164 | = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; | |
3165 | int arg2_align | |
3166 | = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; | |
b428c0a5 | 3167 | enum machine_mode insn_mode; |
3168 | ||
3169 | #ifdef HAVE_cmpmemsi | |
3170 | if (HAVE_cmpmemsi) | |
3171 | insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode; | |
3172 | else | |
3173 | #endif | |
3174 | #ifdef HAVE_cmpstrsi | |
3175 | if (HAVE_cmpstrsi) | |
3176 | insn_mode = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode; | |
3177 | else | |
3178 | #endif | |
3179 | return 0; | |
53800dbe | 3180 | |
3181 | /* If we don't have POINTER_TYPE, call the function. */ | |
3182 | if (arg1_align == 0 || arg2_align == 0) | |
3183 | return 0; | |
3184 | ||
3185 | /* Make a place to write the result of the instruction. */ | |
3186 | result = target; | |
3187 | if (! (result != 0 | |
3188 | && GET_CODE (result) == REG && GET_MODE (result) == insn_mode | |
3189 | && REGNO (result) >= FIRST_PSEUDO_REGISTER)) | |
3190 | result = gen_reg_rtx (insn_mode); | |
3191 | ||
0cd832f0 | 3192 | arg1_rtx = get_memory_rtx (arg1); |
3193 | arg2_rtx = get_memory_rtx (arg2); | |
3194 | arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0); | |
b428c0a5 | 3195 | #ifdef HAVE_cmpmemsi |
3196 | if (HAVE_cmpmemsi) | |
3197 | insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx, | |
3198 | GEN_INT (MIN (arg1_align, arg2_align))); | |
0cd832f0 | 3199 | else |
b428c0a5 | 3200 | #endif |
3201 | #ifdef HAVE_cmpstrsi | |
3202 | if (HAVE_cmpstrsi) | |
0cd832f0 | 3203 | insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx, |
3204 | GEN_INT (MIN (arg1_align, arg2_align))); | |
b428c0a5 | 3205 | else |
3206 | #endif | |
3207 | abort (); | |
0cd832f0 | 3208 | |
3209 | if (insn) | |
3210 | emit_insn (insn); | |
3211 | else | |
2c5d421b | 3212 | emit_library_call_value (memcmp_libfunc, result, LCT_PURE_MAKE_BLOCK, |
0cd832f0 | 3213 | TYPE_MODE (integer_type_node), 3, |
3214 | XEXP (arg1_rtx, 0), Pmode, | |
3215 | XEXP (arg2_rtx, 0), Pmode, | |
3216 | convert_to_mode (TYPE_MODE (sizetype), arg3_rtx, | |
3217 | TREE_UNSIGNED (sizetype)), | |
3218 | TYPE_MODE (sizetype)); | |
53800dbe | 3219 | |
3220 | /* Return the value in the proper mode for this function. */ | |
3221 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
3222 | if (GET_MODE (result) == mode) | |
3223 | return result; | |
3224 | else if (target != 0) | |
3225 | { | |
3226 | convert_move (target, result, 0); | |
3227 | return target; | |
3228 | } | |
3229 | else | |
3230 | return convert_to_mode (mode, result, 0); | |
3231 | } | |
83d79705 | 3232 | #endif |
53800dbe | 3233 | |
6f428e8b | 3234 | return 0; |
3235 | } | |
3236 | ||
53800dbe | 3237 | /* Expand expression EXP, which is a call to the strcmp builtin. Return 0 |
3238 | if we failed the caller should emit a normal call, otherwise try to get | |
3239 | the result in TARGET, if convenient. */ | |
902de8ed | 3240 | |
53800dbe | 3241 | static rtx |
aecda0d6 | 3242 | expand_builtin_strcmp (tree exp, rtx target, enum machine_mode mode) |
53800dbe | 3243 | { |
3244 | tree arglist = TREE_OPERAND (exp, 1); | |
7a3f89b5 | 3245 | tree arg1, arg2; |
83d79705 | 3246 | const char *p1, *p2; |
53800dbe | 3247 | |
0eb671f7 | 3248 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
53800dbe | 3249 | return 0; |
902de8ed | 3250 | |
83d79705 | 3251 | arg1 = TREE_VALUE (arglist); |
3252 | arg2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
3253 | ||
3254 | p1 = c_getstr (arg1); | |
3255 | p2 = c_getstr (arg2); | |
3256 | ||
3257 | if (p1 && p2) | |
3258 | { | |
ef6c187e | 3259 | const int i = strcmp (p1, p2); |
3260 | return (i < 0 ? constm1_rtx : (i > 0 ? const1_rtx : const0_rtx)); | |
83d79705 | 3261 | } |
3262 | ||
ef6c187e | 3263 | /* If either arg is "", return an expression corresponding to |
3264 | (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */ | |
3265 | if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0')) | |
3266 | { | |
3267 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
3268 | tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node); | |
3269 | tree ind1 = | |
3270 | fold (build1 (CONVERT_EXPR, integer_type_node, | |
3271 | build1 (INDIRECT_REF, cst_uchar_node, | |
3272 | build1 (NOP_EXPR, cst_uchar_ptr_node, arg1)))); | |
3273 | tree ind2 = | |
3274 | fold (build1 (CONVERT_EXPR, integer_type_node, | |
3275 | build1 (INDIRECT_REF, cst_uchar_node, | |
3276 | build1 (NOP_EXPR, cst_uchar_ptr_node, arg2)))); | |
3277 | tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2)); | |
3278 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
3279 | } | |
bf8e3599 | 3280 | |
7a3f89b5 | 3281 | #ifdef HAVE_cmpstrsi |
3282 | if (HAVE_cmpstrsi) | |
3283 | { | |
3284 | tree len, len1, len2; | |
3285 | rtx arg1_rtx, arg2_rtx, arg3_rtx; | |
3286 | rtx result, insn; | |
902de8ed | 3287 | |
7a3f89b5 | 3288 | int arg1_align |
3289 | = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; | |
3290 | int arg2_align | |
3291 | = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; | |
3292 | enum machine_mode insn_mode | |
3293 | = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode; | |
902de8ed | 3294 | |
681fab1e | 3295 | len1 = c_strlen (arg1, 1); |
3296 | len2 = c_strlen (arg2, 1); | |
7a3f89b5 | 3297 | |
3298 | if (len1) | |
3299 | len1 = size_binop (PLUS_EXPR, ssize_int (1), len1); | |
3300 | if (len2) | |
3301 | len2 = size_binop (PLUS_EXPR, ssize_int (1), len2); | |
3302 | ||
3303 | /* If we don't have a constant length for the first, use the length | |
3304 | of the second, if we know it. We don't require a constant for | |
3305 | this case; some cost analysis could be done if both are available | |
3306 | but neither is constant. For now, assume they're equally cheap, | |
3307 | unless one has side effects. If both strings have constant lengths, | |
3308 | use the smaller. */ | |
3309 | ||
3310 | if (!len1) | |
3311 | len = len2; | |
3312 | else if (!len2) | |
3313 | len = len1; | |
3314 | else if (TREE_SIDE_EFFECTS (len1)) | |
3315 | len = len2; | |
3316 | else if (TREE_SIDE_EFFECTS (len2)) | |
3317 | len = len1; | |
3318 | else if (TREE_CODE (len1) != INTEGER_CST) | |
3319 | len = len2; | |
3320 | else if (TREE_CODE (len2) != INTEGER_CST) | |
3321 | len = len1; | |
3322 | else if (tree_int_cst_lt (len1, len2)) | |
3323 | len = len1; | |
3324 | else | |
3325 | len = len2; | |
53800dbe | 3326 | |
7a3f89b5 | 3327 | /* If both arguments have side effects, we cannot optimize. */ |
3328 | if (!len || TREE_SIDE_EFFECTS (len)) | |
3329 | return 0; | |
53800dbe | 3330 | |
7a3f89b5 | 3331 | /* If we don't have POINTER_TYPE, call the function. */ |
3332 | if (arg1_align == 0 || arg2_align == 0) | |
3333 | return 0; | |
902de8ed | 3334 | |
7a3f89b5 | 3335 | /* Make a place to write the result of the instruction. */ |
3336 | result = target; | |
3337 | if (! (result != 0 | |
3338 | && GET_CODE (result) == REG && GET_MODE (result) == insn_mode | |
3339 | && REGNO (result) >= FIRST_PSEUDO_REGISTER)) | |
3340 | result = gen_reg_rtx (insn_mode); | |
53800dbe | 3341 | |
7a3f89b5 | 3342 | arg1_rtx = get_memory_rtx (arg1); |
3343 | arg2_rtx = get_memory_rtx (arg2); | |
3344 | arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0); | |
3345 | insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx, | |
3346 | GEN_INT (MIN (arg1_align, arg2_align))); | |
3347 | if (!insn) | |
3348 | return 0; | |
6840589f | 3349 | |
7a3f89b5 | 3350 | emit_insn (insn); |
902de8ed | 3351 | |
7a3f89b5 | 3352 | /* Return the value in the proper mode for this function. */ |
3353 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
3354 | if (GET_MODE (result) == mode) | |
3355 | return result; | |
3356 | if (target == 0) | |
3357 | return convert_to_mode (mode, result, 0); | |
3358 | convert_move (target, result, 0); | |
3359 | return target; | |
3360 | } | |
3361 | #endif | |
3362 | return 0; | |
83d79705 | 3363 | } |
53800dbe | 3364 | |
ed09096d | 3365 | /* Expand expression EXP, which is a call to the strncmp builtin. Return 0 |
3366 | if we failed the caller should emit a normal call, otherwise try to get | |
3367 | the result in TARGET, if convenient. */ | |
27d0c333 | 3368 | |
ed09096d | 3369 | static rtx |
aecda0d6 | 3370 | expand_builtin_strncmp (tree exp, rtx target, enum machine_mode mode) |
ed09096d | 3371 | { |
3372 | tree arglist = TREE_OPERAND (exp, 1); | |
3373 | tree arg1, arg2, arg3; | |
3374 | const char *p1, *p2; | |
3375 | ||
0eb671f7 | 3376 | if (!validate_arglist (arglist, |
3377 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
ed09096d | 3378 | return 0; |
3379 | ||
3380 | arg1 = TREE_VALUE (arglist); | |
3381 | arg2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
3382 | arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
3383 | ||
ed09096d | 3384 | /* If the len parameter is zero, return zero. */ |
6e34e617 | 3385 | if (host_integerp (arg3, 1) && tree_low_cst (arg3, 1) == 0) |
9342ee68 | 3386 | { |
3387 | /* Evaluate and ignore arg1 and arg2 in case they have | |
3388 | side-effects. */ | |
3389 | expand_expr (arg1, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
3390 | expand_expr (arg2, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
3391 | return const0_rtx; | |
3392 | } | |
ed09096d | 3393 | |
3394 | p1 = c_getstr (arg1); | |
3395 | p2 = c_getstr (arg2); | |
3396 | ||
3397 | /* If all arguments are constant, evaluate at compile-time. */ | |
6e34e617 | 3398 | if (host_integerp (arg3, 1) && p1 && p2) |
9342ee68 | 3399 | { |
3400 | const int r = strncmp (p1, p2, tree_low_cst (arg3, 1)); | |
3401 | return (r < 0 ? constm1_rtx : (r > 0 ? const1_rtx : const0_rtx)); | |
3402 | } | |
ed09096d | 3403 | |
ef6c187e | 3404 | /* If len == 1 or (either string parameter is "" and (len >= 1)), |
6e34e617 | 3405 | return (*(const u_char*)arg1 - *(const u_char*)arg2). */ |
3406 | if (host_integerp (arg3, 1) | |
3407 | && (tree_low_cst (arg3, 1) == 1 | |
3408 | || (tree_low_cst (arg3, 1) > 1 | |
3409 | && ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))))) | |
ef6c187e | 3410 | { |
3411 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
3412 | tree cst_uchar_ptr_node = build_pointer_type (cst_uchar_node); | |
3413 | tree ind1 = | |
3414 | fold (build1 (CONVERT_EXPR, integer_type_node, | |
3415 | build1 (INDIRECT_REF, cst_uchar_node, | |
3416 | build1 (NOP_EXPR, cst_uchar_ptr_node, arg1)))); | |
3417 | tree ind2 = | |
3418 | fold (build1 (CONVERT_EXPR, integer_type_node, | |
3419 | build1 (INDIRECT_REF, cst_uchar_node, | |
3420 | build1 (NOP_EXPR, cst_uchar_ptr_node, arg2)))); | |
3421 | tree result = fold (build (MINUS_EXPR, integer_type_node, ind1, ind2)); | |
3422 | return expand_expr (result, target, mode, EXPAND_NORMAL); | |
3423 | } | |
ed09096d | 3424 | |
6e34e617 | 3425 | /* If c_strlen can determine an expression for one of the string |
7a3f89b5 | 3426 | lengths, and it doesn't have side effects, then emit cmpstrsi |
3427 | using length MIN(strlen(string)+1, arg3). */ | |
3428 | #ifdef HAVE_cmpstrsi | |
3429 | if (HAVE_cmpstrsi) | |
3430 | { | |
3431 | tree len, len1, len2; | |
3432 | rtx arg1_rtx, arg2_rtx, arg3_rtx; | |
3433 | rtx result, insn; | |
6f428e8b | 3434 | |
7a3f89b5 | 3435 | int arg1_align |
3436 | = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; | |
3437 | int arg2_align | |
3438 | = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT; | |
3439 | enum machine_mode insn_mode | |
3440 | = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode; | |
bf8e3599 | 3441 | |
681fab1e | 3442 | len1 = c_strlen (arg1, 1); |
3443 | len2 = c_strlen (arg2, 1); | |
7a3f89b5 | 3444 | |
3445 | if (len1) | |
3446 | len1 = size_binop (PLUS_EXPR, ssize_int (1), len1); | |
3447 | if (len2) | |
3448 | len2 = size_binop (PLUS_EXPR, ssize_int (1), len2); | |
3449 | ||
3450 | /* If we don't have a constant length for the first, use the length | |
3451 | of the second, if we know it. We don't require a constant for | |
3452 | this case; some cost analysis could be done if both are available | |
3453 | but neither is constant. For now, assume they're equally cheap, | |
3454 | unless one has side effects. If both strings have constant lengths, | |
3455 | use the smaller. */ | |
3456 | ||
3457 | if (!len1) | |
3458 | len = len2; | |
3459 | else if (!len2) | |
3460 | len = len1; | |
3461 | else if (TREE_SIDE_EFFECTS (len1)) | |
3462 | len = len2; | |
3463 | else if (TREE_SIDE_EFFECTS (len2)) | |
3464 | len = len1; | |
3465 | else if (TREE_CODE (len1) != INTEGER_CST) | |
3466 | len = len2; | |
3467 | else if (TREE_CODE (len2) != INTEGER_CST) | |
3468 | len = len1; | |
3469 | else if (tree_int_cst_lt (len1, len2)) | |
3470 | len = len1; | |
3471 | else | |
3472 | len = len2; | |
6e34e617 | 3473 | |
7a3f89b5 | 3474 | /* If both arguments have side effects, we cannot optimize. */ |
3475 | if (!len || TREE_SIDE_EFFECTS (len)) | |
3476 | return 0; | |
bf8e3599 | 3477 | |
7a3f89b5 | 3478 | /* The actual new length parameter is MIN(len,arg3). */ |
3479 | len = fold (build (MIN_EXPR, TREE_TYPE (len), len, arg3)); | |
3480 | ||
3481 | /* If we don't have POINTER_TYPE, call the function. */ | |
3482 | if (arg1_align == 0 || arg2_align == 0) | |
3483 | return 0; | |
3484 | ||
3485 | /* Make a place to write the result of the instruction. */ | |
3486 | result = target; | |
3487 | if (! (result != 0 | |
3488 | && GET_CODE (result) == REG && GET_MODE (result) == insn_mode | |
3489 | && REGNO (result) >= FIRST_PSEUDO_REGISTER)) | |
3490 | result = gen_reg_rtx (insn_mode); | |
3491 | ||
3492 | arg1_rtx = get_memory_rtx (arg1); | |
3493 | arg2_rtx = get_memory_rtx (arg2); | |
3494 | arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0); | |
3495 | insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx, | |
3496 | GEN_INT (MIN (arg1_align, arg2_align))); | |
3497 | if (!insn) | |
3498 | return 0; | |
3499 | ||
3500 | emit_insn (insn); | |
3501 | ||
3502 | /* Return the value in the proper mode for this function. */ | |
3503 | mode = TYPE_MODE (TREE_TYPE (exp)); | |
3504 | if (GET_MODE (result) == mode) | |
3505 | return result; | |
3506 | if (target == 0) | |
3507 | return convert_to_mode (mode, result, 0); | |
3508 | convert_move (target, result, 0); | |
3509 | return target; | |
3510 | } | |
3511 | #endif | |
3512 | return 0; | |
ed09096d | 3513 | } |
3514 | ||
49f0327b | 3515 | /* Expand expression EXP, which is a call to the strcat builtin. |
3516 | Return 0 if we failed the caller should emit a normal call, | |
3517 | otherwise try to get the result in TARGET, if convenient. */ | |
27d0c333 | 3518 | |
49f0327b | 3519 | static rtx |
aecda0d6 | 3520 | expand_builtin_strcat (tree arglist, rtx target, enum machine_mode mode) |
49f0327b | 3521 | { |
0eb671f7 | 3522 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
49f0327b | 3523 | return 0; |
3524 | else | |
3525 | { | |
3526 | tree dst = TREE_VALUE (arglist), | |
3527 | src = TREE_VALUE (TREE_CHAIN (arglist)); | |
3528 | const char *p = c_getstr (src); | |
3529 | ||
ca71f89b | 3530 | if (p) |
3531 | { | |
3532 | /* If the string length is zero, return the dst parameter. */ | |
3533 | if (*p == '\0') | |
3534 | return expand_expr (dst, target, mode, EXPAND_NORMAL); | |
3535 | else if (!optimize_size) | |
3536 | { | |
3537 | /* Otherwise if !optimize_size, see if we can store by | |
3538 | pieces into (dst + strlen(dst)). */ | |
3539 | tree newdst, arglist, | |
3540 | strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN]; | |
3541 | ||
3542 | /* This is the length argument. */ | |
3543 | arglist = build_tree_list (NULL_TREE, | |
3544 | fold (size_binop (PLUS_EXPR, | |
3545 | c_strlen (src, 0), | |
3546 | ssize_int (1)))); | |
3547 | /* Prepend src argument. */ | |
3548 | arglist = tree_cons (NULL_TREE, src, arglist); | |
3549 | ||
3550 | /* We're going to use dst more than once. */ | |
3551 | dst = save_expr (dst); | |
3552 | ||
3553 | /* Create strlen (dst). */ | |
3554 | newdst = | |
3555 | fold (build_function_call_expr (strlen_fn, | |
3556 | build_tree_list (NULL_TREE, | |
3557 | dst))); | |
3558 | /* Create (dst + strlen (dst)). */ | |
3559 | newdst = fold (build (PLUS_EXPR, TREE_TYPE (dst), dst, newdst)); | |
3560 | ||
3561 | /* Prepend the new dst argument. */ | |
3562 | arglist = tree_cons (NULL_TREE, newdst, arglist); | |
3563 | ||
3564 | /* We don't want to get turned into a memcpy if the | |
3565 | target is const0_rtx, i.e. when the return value | |
3566 | isn't used. That would produce pessimized code so | |
3567 | pass in a target of zero, it should never actually be | |
3568 | used. If this was successful return the original | |
3569 | dst, not the result of mempcpy. */ | |
3570 | if (expand_builtin_mempcpy (arglist, /*target=*/0, mode, /*endp=*/0)) | |
3571 | return expand_expr (dst, target, mode, EXPAND_NORMAL); | |
3572 | else | |
3573 | return 0; | |
3574 | } | |
3575 | } | |
49f0327b | 3576 | |
3577 | return 0; | |
3578 | } | |
3579 | } | |
3580 | ||
3581 | /* Expand expression EXP, which is a call to the strncat builtin. | |
3582 | Return 0 if we failed the caller should emit a normal call, | |
3583 | otherwise try to get the result in TARGET, if convenient. */ | |
27d0c333 | 3584 | |
49f0327b | 3585 | static rtx |
aecda0d6 | 3586 | expand_builtin_strncat (tree arglist, rtx target, enum machine_mode mode) |
49f0327b | 3587 | { |
0eb671f7 | 3588 | if (!validate_arglist (arglist, |
3589 | POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
49f0327b | 3590 | return 0; |
3591 | else | |
3592 | { | |
3593 | tree dst = TREE_VALUE (arglist), | |
3594 | src = TREE_VALUE (TREE_CHAIN (arglist)), | |
3595 | len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))); | |
3596 | const char *p = c_getstr (src); | |
3597 | ||
3598 | /* If the requested length is zero, or the src parameter string | |
3599 | length is zero, return the dst parameter. */ | |
b527e67e | 3600 | if (integer_zerop (len) || (p && *p == '\0')) |
9342ee68 | 3601 | { |
49f0327b | 3602 | /* Evaluate and ignore the src and len parameters in case |
3603 | they have side-effects. */ | |
3604 | expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
3605 | expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
3606 | return expand_expr (dst, target, mode, EXPAND_NORMAL); | |
3607 | } | |
3608 | ||
3609 | /* If the requested len is greater than or equal to the string | |
3610 | length, call strcat. */ | |
3611 | if (TREE_CODE (len) == INTEGER_CST && p | |
3612 | && compare_tree_int (len, strlen (p)) >= 0) | |
9342ee68 | 3613 | { |
df5c389e | 3614 | tree newarglist |
3615 | = tree_cons (NULL_TREE, dst, build_tree_list (NULL_TREE, src)); | |
0a68165a | 3616 | tree fn = implicit_built_in_decls[BUILT_IN_STRCAT]; |
bf8e3599 | 3617 | |
49f0327b | 3618 | /* If the replacement _DECL isn't initialized, don't do the |
2c0e001b | 3619 | transformation. */ |
49f0327b | 3620 | if (!fn) |
3621 | return 0; | |
3622 | ||
7e15618b | 3623 | return expand_expr (build_function_call_expr (fn, newarglist), |
3624 | target, mode, EXPAND_NORMAL); | |
49f0327b | 3625 | } |
3626 | return 0; | |
3627 | } | |
3628 | } | |
3629 | ||
3630 | /* Expand expression EXP, which is a call to the strspn builtin. | |
3631 | Return 0 if we failed the caller should emit a normal call, | |
3632 | otherwise try to get the result in TARGET, if convenient. */ | |
27d0c333 | 3633 | |
49f0327b | 3634 | static rtx |
aecda0d6 | 3635 | expand_builtin_strspn (tree arglist, rtx target, enum machine_mode mode) |
49f0327b | 3636 | { |
0eb671f7 | 3637 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
49f0327b | 3638 | return 0; |
3639 | else | |
3640 | { | |
3641 | tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
3642 | const char *p1 = c_getstr (s1), *p2 = c_getstr (s2); | |
bf8e3599 | 3643 | |
49f0327b | 3644 | /* If both arguments are constants, evaluate at compile-time. */ |
3645 | if (p1 && p2) | |
9342ee68 | 3646 | { |
49f0327b | 3647 | const size_t r = strspn (p1, p2); |
3648 | return expand_expr (size_int (r), target, mode, EXPAND_NORMAL); | |
3649 | } | |
bf8e3599 | 3650 | |
fb1f44c5 | 3651 | /* If either argument is "", return 0. */ |
3652 | if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0')) | |
9342ee68 | 3653 | { |
fb1f44c5 | 3654 | /* Evaluate and ignore both arguments in case either one has |
49f0327b | 3655 | side-effects. */ |
3656 | expand_expr (s1, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
fb1f44c5 | 3657 | expand_expr (s2, const0_rtx, VOIDmode, EXPAND_NORMAL); |
49f0327b | 3658 | return const0_rtx; |
3659 | } | |
3660 | return 0; | |
3661 | } | |
3662 | } | |
3663 | ||
3664 | /* Expand expression EXP, which is a call to the strcspn builtin. | |
3665 | Return 0 if we failed the caller should emit a normal call, | |
3666 | otherwise try to get the result in TARGET, if convenient. */ | |
27d0c333 | 3667 | |
49f0327b | 3668 | static rtx |
aecda0d6 | 3669 | expand_builtin_strcspn (tree arglist, rtx target, enum machine_mode mode) |
49f0327b | 3670 | { |
0eb671f7 | 3671 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
49f0327b | 3672 | return 0; |
3673 | else | |
3674 | { | |
3675 | tree s1 = TREE_VALUE (arglist), s2 = TREE_VALUE (TREE_CHAIN (arglist)); | |
3676 | const char *p1 = c_getstr (s1), *p2 = c_getstr (s2); | |
bf8e3599 | 3677 | |
49f0327b | 3678 | /* If both arguments are constants, evaluate at compile-time. */ |
3679 | if (p1 && p2) | |
9342ee68 | 3680 | { |
49f0327b | 3681 | const size_t r = strcspn (p1, p2); |
3682 | return expand_expr (size_int (r), target, mode, EXPAND_NORMAL); | |
3683 | } | |
bf8e3599 | 3684 | |
fb1f44c5 | 3685 | /* If the first argument is "", return 0. */ |
3686 | if (p1 && *p1 == '\0') | |
9342ee68 | 3687 | { |
fb1f44c5 | 3688 | /* Evaluate and ignore argument s2 in case it has |
3689 | side-effects. */ | |
3690 | expand_expr (s2, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
3691 | return const0_rtx; | |
3692 | } | |
3693 | ||
49f0327b | 3694 | /* If the second argument is "", return __builtin_strlen(s1). */ |
3695 | if (p2 && *p2 == '\0') | |
9342ee68 | 3696 | { |
7e15618b | 3697 | tree newarglist = build_tree_list (NULL_TREE, s1), |
0a68165a | 3698 | fn = implicit_built_in_decls[BUILT_IN_STRLEN]; |
bf8e3599 | 3699 | |
49f0327b | 3700 | /* If the replacement _DECL isn't initialized, don't do the |
2c0e001b | 3701 | transformation. */ |
49f0327b | 3702 | if (!fn) |
3703 | return 0; | |
3704 | ||
7e15618b | 3705 | return expand_expr (build_function_call_expr (fn, newarglist), |
3706 | target, mode, EXPAND_NORMAL); | |
49f0327b | 3707 | } |
3708 | return 0; | |
3709 | } | |
3710 | } | |
3711 | ||
a66c9326 | 3712 | /* Expand a call to __builtin_saveregs, generating the result in TARGET, |
3713 | if that's convenient. */ | |
902de8ed | 3714 | |
a66c9326 | 3715 | rtx |
aecda0d6 | 3716 | expand_builtin_saveregs (void) |
53800dbe | 3717 | { |
a66c9326 | 3718 | rtx val, seq; |
53800dbe | 3719 | |
3720 | /* Don't do __builtin_saveregs more than once in a function. | |
3721 | Save the result of the first call and reuse it. */ | |
3722 | if (saveregs_value != 0) | |
3723 | return saveregs_value; | |
53800dbe | 3724 | |
a66c9326 | 3725 | /* When this function is called, it means that registers must be |
3726 | saved on entry to this function. So we migrate the call to the | |
3727 | first insn of this function. */ | |
3728 | ||
3729 | start_sequence (); | |
53800dbe | 3730 | |
3731 | #ifdef EXPAND_BUILTIN_SAVEREGS | |
a66c9326 | 3732 | /* Do whatever the machine needs done in this case. */ |
3733 | val = EXPAND_BUILTIN_SAVEREGS (); | |
53800dbe | 3734 | #else |
a66c9326 | 3735 | /* ??? We used to try and build up a call to the out of line function, |
3736 | guessing about what registers needed saving etc. This became much | |
3737 | harder with __builtin_va_start, since we don't have a tree for a | |
3738 | call to __builtin_saveregs to fall back on. There was exactly one | |
3739 | port (i860) that used this code, and I'm unconvinced it could actually | |
3740 | handle the general case. So we no longer try to handle anything | |
3741 | weird and make the backend absorb the evil. */ | |
3742 | ||
3743 | error ("__builtin_saveregs not supported by this target"); | |
3744 | val = const0_rtx; | |
53800dbe | 3745 | #endif |
3746 | ||
a66c9326 | 3747 | seq = get_insns (); |
3748 | end_sequence (); | |
53800dbe | 3749 | |
a66c9326 | 3750 | saveregs_value = val; |
53800dbe | 3751 | |
31d3e01c | 3752 | /* Put the insns after the NOTE that starts the function. If this |
3753 | is inside a start_sequence, make the outer-level insn chain current, so | |
a66c9326 | 3754 | the code is placed at the start of the function. */ |
3755 | push_topmost_sequence (); | |
31d3e01c | 3756 | emit_insn_after (seq, get_insns ()); |
a66c9326 | 3757 | pop_topmost_sequence (); |
3758 | ||
3759 | return val; | |
53800dbe | 3760 | } |
3761 | ||
3762 | /* __builtin_args_info (N) returns word N of the arg space info | |
3763 | for the current function. The number and meanings of words | |
3764 | is controlled by the definition of CUMULATIVE_ARGS. */ | |
f7c44134 | 3765 | |
53800dbe | 3766 | static rtx |
aecda0d6 | 3767 | expand_builtin_args_info (tree arglist) |
53800dbe | 3768 | { |
53800dbe | 3769 | int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int); |
3770 | int *word_ptr = (int *) ¤t_function_args_info; | |
53800dbe | 3771 | |
3772 | if (sizeof (CUMULATIVE_ARGS) % sizeof (int) != 0) | |
dda90815 | 3773 | abort (); |
53800dbe | 3774 | |
3775 | if (arglist != 0) | |
3776 | { | |
27d0c333 | 3777 | if (!host_integerp (TREE_VALUE (arglist), 0)) |
53800dbe | 3778 | error ("argument of `__builtin_args_info' must be constant"); |
3779 | else | |
3780 | { | |
27d0c333 | 3781 | HOST_WIDE_INT wordnum = tree_low_cst (TREE_VALUE (arglist), 0); |
53800dbe | 3782 | |
27d0c333 | 3783 | if (wordnum < 0 || wordnum >= nwords) |
53800dbe | 3784 | error ("argument of `__builtin_args_info' out of range"); |
3785 | else | |
3786 | return GEN_INT (word_ptr[wordnum]); | |
3787 | } | |
3788 | } | |
3789 | else | |
3790 | error ("missing argument in `__builtin_args_info'"); | |
3791 | ||
3792 | return const0_rtx; | |
53800dbe | 3793 | } |
3794 | ||
a66c9326 | 3795 | /* Expand ARGLIST, from a call to __builtin_next_arg. */ |
27d0c333 | 3796 | |
53800dbe | 3797 | static rtx |
aecda0d6 | 3798 | expand_builtin_next_arg (tree arglist) |
53800dbe | 3799 | { |
53800dbe | 3800 | tree fntype = TREE_TYPE (current_function_decl); |
3801 | ||
7ccc713a | 3802 | if (TYPE_ARG_TYPES (fntype) == 0 |
3803 | || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype))) | |
3804 | == void_type_node)) | |
53800dbe | 3805 | { |
3806 | error ("`va_start' used in function with fixed args"); | |
3807 | return const0_rtx; | |
3808 | } | |
3809 | ||
3810 | if (arglist) | |
3811 | { | |
3812 | tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl)); | |
3813 | tree arg = TREE_VALUE (arglist); | |
3814 | ||
3815 | /* Strip off all nops for the sake of the comparison. This | |
bf8e3599 | 3816 | is not quite the same as STRIP_NOPS. It does more. |
53800dbe | 3817 | We must also strip off INDIRECT_EXPR for C++ reference |
3818 | parameters. */ | |
3819 | while (TREE_CODE (arg) == NOP_EXPR | |
3820 | || TREE_CODE (arg) == CONVERT_EXPR | |
3821 | || TREE_CODE (arg) == NON_LVALUE_EXPR | |
3822 | || TREE_CODE (arg) == INDIRECT_REF) | |
3823 | arg = TREE_OPERAND (arg, 0); | |
3824 | if (arg != last_parm) | |
3825 | warning ("second parameter of `va_start' not last named argument"); | |
3826 | } | |
7ccc713a | 3827 | else |
53800dbe | 3828 | /* Evidently an out of date version of <stdarg.h>; can't validate |
3829 | va_start's second argument, but can still work as intended. */ | |
3830 | warning ("`__builtin_next_arg' called without an argument"); | |
3831 | ||
3832 | return expand_binop (Pmode, add_optab, | |
3833 | current_function_internal_arg_pointer, | |
3834 | current_function_arg_offset_rtx, | |
3835 | NULL_RTX, 0, OPTAB_LIB_WIDEN); | |
3836 | } | |
3837 | ||
a66c9326 | 3838 | /* Make it easier for the backends by protecting the valist argument |
3839 | from multiple evaluations. */ | |
3840 | ||
3841 | static tree | |
aecda0d6 | 3842 | stabilize_va_list (tree valist, int needs_lvalue) |
a66c9326 | 3843 | { |
11a61dea | 3844 | if (TREE_CODE (va_list_type_node) == ARRAY_TYPE) |
a66c9326 | 3845 | { |
2d47cc32 | 3846 | if (TREE_SIDE_EFFECTS (valist)) |
3847 | valist = save_expr (valist); | |
11a61dea | 3848 | |
2d47cc32 | 3849 | /* For this case, the backends will be expecting a pointer to |
3850 | TREE_TYPE (va_list_type_node), but it's possible we've | |
3851 | actually been given an array (an actual va_list_type_node). | |
3852 | So fix it. */ | |
3853 | if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE) | |
8a15c04a | 3854 | { |
bf8e3599 | 3855 | tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node)); |
3856 | tree p2 = build_pointer_type (va_list_type_node); | |
325d1c45 | 3857 | |
bf8e3599 | 3858 | valist = build1 (ADDR_EXPR, p2, valist); |
2d47cc32 | 3859 | valist = fold (build1 (NOP_EXPR, p1, valist)); |
8a15c04a | 3860 | } |
a66c9326 | 3861 | } |
11a61dea | 3862 | else |
a66c9326 | 3863 | { |
2d47cc32 | 3864 | tree pt; |
11a61dea | 3865 | |
2d47cc32 | 3866 | if (! needs_lvalue) |
3867 | { | |
11a61dea | 3868 | if (! TREE_SIDE_EFFECTS (valist)) |
3869 | return valist; | |
bf8e3599 | 3870 | |
11a61dea | 3871 | pt = build_pointer_type (va_list_type_node); |
2d47cc32 | 3872 | valist = fold (build1 (ADDR_EXPR, pt, valist)); |
a66c9326 | 3873 | TREE_SIDE_EFFECTS (valist) = 1; |
a66c9326 | 3874 | } |
2d47cc32 | 3875 | |
11a61dea | 3876 | if (TREE_SIDE_EFFECTS (valist)) |
2d47cc32 | 3877 | valist = save_expr (valist); |
11a61dea | 3878 | valist = fold (build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), |
3879 | valist)); | |
a66c9326 | 3880 | } |
3881 | ||
3882 | return valist; | |
3883 | } | |
3884 | ||
3885 | /* The "standard" implementation of va_start: just assign `nextarg' to | |
3886 | the variable. */ | |
27d0c333 | 3887 | |
a66c9326 | 3888 | void |
aecda0d6 | 3889 | std_expand_builtin_va_start (tree valist, rtx nextarg) |
a66c9326 | 3890 | { |
3891 | tree t; | |
3892 | ||
3893 | t = build (MODIFY_EXPR, TREE_TYPE (valist), valist, | |
3894 | make_tree (ptr_type_node, nextarg)); | |
3895 | TREE_SIDE_EFFECTS (t) = 1; | |
3896 | ||
3897 | expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
3898 | } | |
3899 | ||
7ccc713a | 3900 | /* Expand ARGLIST, from a call to __builtin_va_start. */ |
27d0c333 | 3901 | |
a66c9326 | 3902 | static rtx |
aecda0d6 | 3903 | expand_builtin_va_start (tree arglist) |
a66c9326 | 3904 | { |
3905 | rtx nextarg; | |
7ccc713a | 3906 | tree chain, valist; |
a66c9326 | 3907 | |
7ccc713a | 3908 | chain = TREE_CHAIN (arglist); |
a66c9326 | 3909 | |
3910 | if (TREE_CHAIN (chain)) | |
3911 | error ("too many arguments to function `va_start'"); | |
3912 | ||
7ccc713a | 3913 | nextarg = expand_builtin_next_arg (chain); |
a66c9326 | 3914 | valist = stabilize_va_list (TREE_VALUE (arglist), 1); |
3915 | ||
3916 | #ifdef EXPAND_BUILTIN_VA_START | |
7df226a2 | 3917 | EXPAND_BUILTIN_VA_START (valist, nextarg); |
a66c9326 | 3918 | #else |
7df226a2 | 3919 | std_expand_builtin_va_start (valist, nextarg); |
a66c9326 | 3920 | #endif |
3921 | ||
3922 | return const0_rtx; | |
3923 | } | |
3924 | ||
a66c9326 | 3925 | /* The "standard" implementation of va_arg: read the value from the |
3926 | current (padded) address and increment by the (padded) size. */ | |
f7c44134 | 3927 | |
a66c9326 | 3928 | rtx |
aecda0d6 | 3929 | std_expand_builtin_va_arg (tree valist, tree type) |
a66c9326 | 3930 | { |
91f95e00 | 3931 | tree addr_tree, t, type_size = NULL; |
3932 | tree align, alignm1; | |
3933 | tree rounded_size; | |
a66c9326 | 3934 | rtx addr; |
3935 | ||
3936 | /* Compute the rounded size of the type. */ | |
91f95e00 | 3937 | align = size_int (PARM_BOUNDARY / BITS_PER_UNIT); |
3938 | alignm1 = size_int (PARM_BOUNDARY / BITS_PER_UNIT - 1); | |
3939 | if (type == error_mark_node | |
3940 | || (type_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (type))) == NULL | |
3941 | || TREE_OVERFLOW (type_size)) | |
3942 | rounded_size = size_zero_node; | |
3943 | else | |
3944 | rounded_size = fold (build (MULT_EXPR, sizetype, | |
3945 | fold (build (TRUNC_DIV_EXPR, sizetype, | |
3946 | fold (build (PLUS_EXPR, sizetype, | |
3947 | type_size, alignm1)), | |
3948 | align)), | |
3949 | align)); | |
a66c9326 | 3950 | |
3951 | /* Get AP. */ | |
3952 | addr_tree = valist; | |
91f95e00 | 3953 | if (PAD_VARARGS_DOWN && ! integer_zerop (rounded_size)) |
a66c9326 | 3954 | { |
3955 | /* Small args are padded downward. */ | |
91f95e00 | 3956 | addr_tree = fold (build (PLUS_EXPR, TREE_TYPE (addr_tree), addr_tree, |
3957 | fold (build (COND_EXPR, sizetype, | |
3958 | fold (build (GT_EXPR, sizetype, | |
3959 | rounded_size, | |
3960 | align)), | |
3961 | size_zero_node, | |
3962 | fold (build (MINUS_EXPR, sizetype, | |
3963 | rounded_size, | |
3964 | type_size)))))); | |
a66c9326 | 3965 | } |
3966 | ||
3967 | addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL); | |
3968 | addr = copy_to_reg (addr); | |
3969 | ||
3970 | /* Compute new value for AP. */ | |
91f95e00 | 3971 | if (! integer_zerop (rounded_size)) |
3972 | { | |
3973 | t = build (MODIFY_EXPR, TREE_TYPE (valist), valist, | |
3974 | build (PLUS_EXPR, TREE_TYPE (valist), valist, | |
3975 | rounded_size)); | |
3976 | TREE_SIDE_EFFECTS (t) = 1; | |
3977 | expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
3978 | } | |
a66c9326 | 3979 | |
3980 | return addr; | |
3981 | } | |
3982 | ||
3983 | /* Expand __builtin_va_arg, which is not really a builtin function, but | |
3984 | a very special sort of operator. */ | |
f7c44134 | 3985 | |
a66c9326 | 3986 | rtx |
aecda0d6 | 3987 | expand_builtin_va_arg (tree valist, tree type) |
a66c9326 | 3988 | { |
3989 | rtx addr, result; | |
6cd005c9 | 3990 | tree promoted_type, want_va_type, have_va_type; |
a66c9326 | 3991 | |
6cd005c9 | 3992 | /* Verify that valist is of the proper type. */ |
3993 | ||
3994 | want_va_type = va_list_type_node; | |
3995 | have_va_type = TREE_TYPE (valist); | |
3996 | if (TREE_CODE (want_va_type) == ARRAY_TYPE) | |
3997 | { | |
bf8e3599 | 3998 | /* If va_list is an array type, the argument may have decayed |
6cd005c9 | 3999 | to a pointer type, e.g. by being passed to another function. |
4000 | In that case, unwrap both types so that we can compare the | |
4001 | underlying records. */ | |
4002 | if (TREE_CODE (have_va_type) == ARRAY_TYPE | |
4003 | || TREE_CODE (have_va_type) == POINTER_TYPE) | |
4004 | { | |
4005 | want_va_type = TREE_TYPE (want_va_type); | |
4006 | have_va_type = TREE_TYPE (have_va_type); | |
4007 | } | |
4008 | } | |
4009 | if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type)) | |
a66c9326 | 4010 | { |
e94026da | 4011 | error ("first argument to `va_arg' not of type `va_list'"); |
4012 | addr = const0_rtx; | |
4013 | } | |
6cd005c9 | 4014 | |
4015 | /* Generate a diagnostic for requesting data of a type that cannot | |
4016 | be passed through `...' due to type promotion at the call site. */ | |
63c62881 | 4017 | else if ((promoted_type = (*lang_hooks.types.type_promotes_to) (type)) |
4018 | != type) | |
e94026da | 4019 | { |
01ce7a1b | 4020 | const char *name = "<anonymous type>", *pname = 0; |
a0ef1725 | 4021 | static bool gave_help; |
e94026da | 4022 | |
4023 | if (TYPE_NAME (type)) | |
4024 | { | |
4025 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
4026 | name = IDENTIFIER_POINTER (TYPE_NAME (type)); | |
4027 | else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL | |
4028 | && DECL_NAME (TYPE_NAME (type))) | |
4029 | name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))); | |
4030 | } | |
4031 | if (TYPE_NAME (promoted_type)) | |
4032 | { | |
4033 | if (TREE_CODE (TYPE_NAME (promoted_type)) == IDENTIFIER_NODE) | |
4034 | pname = IDENTIFIER_POINTER (TYPE_NAME (promoted_type)); | |
4035 | else if (TREE_CODE (TYPE_NAME (promoted_type)) == TYPE_DECL | |
4036 | && DECL_NAME (TYPE_NAME (promoted_type))) | |
4037 | pname = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (promoted_type))); | |
4038 | } | |
4039 | ||
a0ef1725 | 4040 | /* Unfortunately, this is merely undefined, rather than a constraint |
4041 | violation, so we cannot make this an error. If this call is never | |
4042 | executed, the program is still strictly conforming. */ | |
4043 | warning ("`%s' is promoted to `%s' when passed through `...'", | |
4044 | name, pname); | |
e94026da | 4045 | if (! gave_help) |
4046 | { | |
a0ef1725 | 4047 | gave_help = true; |
4048 | warning ("(so you should pass `%s' not `%s' to `va_arg')", | |
4049 | pname, name); | |
e94026da | 4050 | } |
4051 | ||
a0ef1725 | 4052 | /* We can, however, treat "undefined" any way we please. |
4053 | Call abort to encourage the user to fix the program. */ | |
4054 | expand_builtin_trap (); | |
4055 | ||
4056 | /* This is dead code, but go ahead and finish so that the | |
4057 | mode of the result comes out right. */ | |
a66c9326 | 4058 | addr = const0_rtx; |
4059 | } | |
4060 | else | |
4061 | { | |
4062 | /* Make it easier for the backends by protecting the valist argument | |
4063 | from multiple evaluations. */ | |
4064 | valist = stabilize_va_list (valist, 0); | |
4065 | ||
4066 | #ifdef EXPAND_BUILTIN_VA_ARG | |
4067 | addr = EXPAND_BUILTIN_VA_ARG (valist, type); | |
4068 | #else | |
4069 | addr = std_expand_builtin_va_arg (valist, type); | |
4070 | #endif | |
4071 | } | |
4072 | ||
726ec87c | 4073 | #ifdef POINTERS_EXTEND_UNSIGNED |
4074 | if (GET_MODE (addr) != Pmode) | |
4075 | addr = convert_memory_address (Pmode, addr); | |
4076 | #endif | |
4077 | ||
a66c9326 | 4078 | result = gen_rtx_MEM (TYPE_MODE (type), addr); |
ab6ab77e | 4079 | set_mem_alias_set (result, get_varargs_alias_set ()); |
a66c9326 | 4080 | |
4081 | return result; | |
4082 | } | |
4083 | ||
4084 | /* Expand ARGLIST, from a call to __builtin_va_end. */ | |
f7c44134 | 4085 | |
a66c9326 | 4086 | static rtx |
aecda0d6 | 4087 | expand_builtin_va_end (tree arglist) |
a66c9326 | 4088 | { |
8a15c04a | 4089 | tree valist = TREE_VALUE (arglist); |
4090 | ||
a66c9326 | 4091 | #ifdef EXPAND_BUILTIN_VA_END |
a66c9326 | 4092 | valist = stabilize_va_list (valist, 0); |
f0ce3b1f | 4093 | EXPAND_BUILTIN_VA_END (arglist); |
8a15c04a | 4094 | #else |
4095 | /* Evaluate for side effects, if needed. I hate macros that don't | |
4096 | do that. */ | |
4097 | if (TREE_SIDE_EFFECTS (valist)) | |
4098 | expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
a66c9326 | 4099 | #endif |
4100 | ||
4101 | return const0_rtx; | |
4102 | } | |
4103 | ||
bf8e3599 | 4104 | /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a |
a66c9326 | 4105 | builtin rather than just as an assignment in stdarg.h because of the |
4106 | nastiness of array-type va_list types. */ | |
f7c44134 | 4107 | |
a66c9326 | 4108 | static rtx |
aecda0d6 | 4109 | expand_builtin_va_copy (tree arglist) |
a66c9326 | 4110 | { |
4111 | tree dst, src, t; | |
4112 | ||
4113 | dst = TREE_VALUE (arglist); | |
4114 | src = TREE_VALUE (TREE_CHAIN (arglist)); | |
4115 | ||
4116 | dst = stabilize_va_list (dst, 1); | |
4117 | src = stabilize_va_list (src, 0); | |
4118 | ||
4119 | if (TREE_CODE (va_list_type_node) != ARRAY_TYPE) | |
4120 | { | |
4121 | t = build (MODIFY_EXPR, va_list_type_node, dst, src); | |
4122 | TREE_SIDE_EFFECTS (t) = 1; | |
4123 | expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
4124 | } | |
4125 | else | |
4126 | { | |
11a61dea | 4127 | rtx dstb, srcb, size; |
4128 | ||
4129 | /* Evaluate to pointers. */ | |
4130 | dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL); | |
4131 | srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL); | |
4132 | size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX, | |
4133 | VOIDmode, EXPAND_NORMAL); | |
4134 | ||
726ec87c | 4135 | #ifdef POINTERS_EXTEND_UNSIGNED |
4136 | if (GET_MODE (dstb) != Pmode) | |
4137 | dstb = convert_memory_address (Pmode, dstb); | |
4138 | ||
4139 | if (GET_MODE (srcb) != Pmode) | |
4140 | srcb = convert_memory_address (Pmode, srcb); | |
4141 | #endif | |
4142 | ||
11a61dea | 4143 | /* "Dereference" to BLKmode memories. */ |
4144 | dstb = gen_rtx_MEM (BLKmode, dstb); | |
ab6ab77e | 4145 | set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst)))); |
2a631e19 | 4146 | set_mem_align (dstb, TYPE_ALIGN (va_list_type_node)); |
11a61dea | 4147 | srcb = gen_rtx_MEM (BLKmode, srcb); |
ab6ab77e | 4148 | set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src)))); |
2a631e19 | 4149 | set_mem_align (srcb, TYPE_ALIGN (va_list_type_node)); |
11a61dea | 4150 | |
4151 | /* Copy. */ | |
0378dbdc | 4152 | emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL); |
a66c9326 | 4153 | } |
4154 | ||
4155 | return const0_rtx; | |
4156 | } | |
4157 | ||
53800dbe | 4158 | /* Expand a call to one of the builtin functions __builtin_frame_address or |
4159 | __builtin_return_address. */ | |
27d0c333 | 4160 | |
53800dbe | 4161 | static rtx |
aecda0d6 | 4162 | expand_builtin_frame_address (tree fndecl, tree arglist) |
53800dbe | 4163 | { |
53800dbe | 4164 | /* The argument must be a nonnegative integer constant. |
4165 | It counts the number of frames to scan up the stack. | |
4166 | The value is the return address saved in that frame. */ | |
4167 | if (arglist == 0) | |
4168 | /* Warning about missing arg was already issued. */ | |
4169 | return const0_rtx; | |
27d0c333 | 4170 | else if (! host_integerp (TREE_VALUE (arglist), 1)) |
53800dbe | 4171 | { |
4172 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS) | |
4173 | error ("invalid arg to `__builtin_frame_address'"); | |
4174 | else | |
4175 | error ("invalid arg to `__builtin_return_address'"); | |
4176 | return const0_rtx; | |
4177 | } | |
4178 | else | |
4179 | { | |
27d0c333 | 4180 | rtx tem |
4181 | = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), | |
4182 | tree_low_cst (TREE_VALUE (arglist), 1), | |
4183 | hard_frame_pointer_rtx); | |
53800dbe | 4184 | |
4185 | /* Some ports cannot access arbitrary stack frames. */ | |
4186 | if (tem == NULL) | |
4187 | { | |
4188 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS) | |
4189 | warning ("unsupported arg to `__builtin_frame_address'"); | |
4190 | else | |
4191 | warning ("unsupported arg to `__builtin_return_address'"); | |
4192 | return const0_rtx; | |
4193 | } | |
4194 | ||
4195 | /* For __builtin_frame_address, return what we've got. */ | |
4196 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS) | |
4197 | return tem; | |
4198 | ||
4199 | if (GET_CODE (tem) != REG | |
4200 | && ! CONSTANT_P (tem)) | |
4201 | tem = copy_to_mode_reg (Pmode, tem); | |
4202 | return tem; | |
4203 | } | |
4204 | } | |
4205 | ||
4206 | /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if | |
4207 | we failed and the caller should emit a normal call, otherwise try to get | |
4208 | the result in TARGET, if convenient. */ | |
15c6cf6b | 4209 | |
53800dbe | 4210 | static rtx |
aecda0d6 | 4211 | expand_builtin_alloca (tree arglist, rtx target) |
53800dbe | 4212 | { |
4213 | rtx op0; | |
15c6cf6b | 4214 | rtx result; |
53800dbe | 4215 | |
0eb671f7 | 4216 | if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE)) |
53800dbe | 4217 | return 0; |
4218 | ||
4219 | /* Compute the argument. */ | |
4220 | op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0); | |
4221 | ||
4222 | /* Allocate the desired space. */ | |
15c6cf6b | 4223 | result = allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT); |
4224 | ||
4225 | #ifdef POINTERS_EXTEND_UNSIGNED | |
479e4d5e | 4226 | if (GET_MODE (result) != ptr_mode) |
4227 | result = convert_memory_address (ptr_mode, result); | |
15c6cf6b | 4228 | #endif |
4229 | ||
4230 | return result; | |
53800dbe | 4231 | } |
4232 | ||
6a08d0ab | 4233 | /* Expand a call to a unary builtin. The arguments are in ARGLIST. |
53800dbe | 4234 | Return 0 if a normal call should be emitted rather than expanding the |
4235 | function in-line. If convenient, the result should be placed in TARGET. | |
4236 | SUBTARGET may be used as the target for computing one of EXP's operands. */ | |
15c6cf6b | 4237 | |
53800dbe | 4238 | static rtx |
aecda0d6 | 4239 | expand_builtin_unop (enum machine_mode target_mode, tree arglist, rtx target, |
4240 | rtx subtarget, optab op_optab) | |
53800dbe | 4241 | { |
4242 | rtx op0; | |
0eb671f7 | 4243 | if (!validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE)) |
53800dbe | 4244 | return 0; |
4245 | ||
4246 | /* Compute the argument. */ | |
4247 | op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0); | |
6a08d0ab | 4248 | /* Compute op, into TARGET if possible. |
53800dbe | 4249 | Set TARGET to wherever the result comes back. */ |
4250 | target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))), | |
6a08d0ab | 4251 | op_optab, op0, target, 1); |
53800dbe | 4252 | if (target == 0) |
4253 | abort (); | |
efb070c8 | 4254 | |
4255 | return convert_to_mode (target_mode, target, 0); | |
53800dbe | 4256 | } |
89cfe6e5 | 4257 | |
df94cd3b | 4258 | /* If the string passed to fputs is a constant and is one character |
2c0e001b | 4259 | long, we attempt to transform this call into __builtin_fputc(). */ |
15c6cf6b | 4260 | |
df94cd3b | 4261 | static rtx |
19bf118a | 4262 | expand_builtin_fputs (tree arglist, rtx target, bool unlocked) |
df94cd3b | 4263 | { |
c013a46e | 4264 | tree len, fn; |
0a68165a | 4265 | tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED] |
4266 | : implicit_built_in_decls[BUILT_IN_FPUTC]; | |
4267 | tree fn_fwrite = unlocked ? implicit_built_in_decls[BUILT_IN_FWRITE_UNLOCKED] | |
4268 | : implicit_built_in_decls[BUILT_IN_FWRITE]; | |
df94cd3b | 4269 | |
4270 | /* If the return value is used, or the replacement _DECL isn't | |
2c0e001b | 4271 | initialized, don't do the transformation. */ |
19bf118a | 4272 | if (target != const0_rtx || !fn_fputc || !fn_fwrite) |
df94cd3b | 4273 | return 0; |
4274 | ||
2c0e001b | 4275 | /* Verify the arguments in the original call. */ |
8a06f2d4 | 4276 | if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)) |
df94cd3b | 4277 | return 0; |
4278 | ||
ce1b14f4 | 4279 | /* Get the length of the string passed to fputs. If the length |
4280 | can't be determined, punt. */ | |
681fab1e | 4281 | if (!(len = c_strlen (TREE_VALUE (arglist), 1)) |
6840589f | 4282 | || TREE_CODE (len) != INTEGER_CST) |
df94cd3b | 4283 | return 0; |
4284 | ||
ce1b14f4 | 4285 | switch (compare_tree_int (len, 1)) |
4286 | { | |
4287 | case -1: /* length is 0, delete the call entirely . */ | |
6d42b7e4 | 4288 | { |
4289 | /* Evaluate and ignore the argument in case it has | |
4290 | side-effects. */ | |
4291 | expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), const0_rtx, | |
4292 | VOIDmode, EXPAND_NORMAL); | |
4293 | return const0_rtx; | |
4294 | } | |
ce1b14f4 | 4295 | case 0: /* length is 1, call fputc. */ |
4296 | { | |
83d79705 | 4297 | const char *p = c_getstr (TREE_VALUE (arglist)); |
df94cd3b | 4298 | |
83d79705 | 4299 | if (p != NULL) |
bf8e3599 | 4300 | { |
83d79705 | 4301 | /* New argument list transforming fputs(string, stream) to |
4302 | fputc(string[0], stream). */ | |
4303 | arglist = | |
4304 | build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist))); | |
4305 | arglist = | |
4306 | tree_cons (NULL_TREE, build_int_2 (p[0], 0), arglist); | |
4307 | fn = fn_fputc; | |
4308 | break; | |
4309 | } | |
ce1b14f4 | 4310 | } |
83d79705 | 4311 | /* FALLTHROUGH */ |
ce1b14f4 | 4312 | case 1: /* length is greater than 1, call fwrite. */ |
4313 | { | |
4ec25652 | 4314 | tree string_arg; |
bf8e3599 | 4315 | |
6473f3f4 | 4316 | /* If optimizing for size keep fputs. */ |
4ec25652 | 4317 | if (optimize_size) |
4318 | return 0; | |
4319 | string_arg = TREE_VALUE (arglist); | |
ce1b14f4 | 4320 | /* New argument list transforming fputs(string, stream) to |
4321 | fwrite(string, 1, len, stream). */ | |
4322 | arglist = build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist))); | |
4323 | arglist = tree_cons (NULL_TREE, len, arglist); | |
38d76e41 | 4324 | arglist = tree_cons (NULL_TREE, size_one_node, arglist); |
ce1b14f4 | 4325 | arglist = tree_cons (NULL_TREE, string_arg, arglist); |
4326 | fn = fn_fwrite; | |
4327 | break; | |
4328 | } | |
4329 | default: | |
e17f5b23 | 4330 | abort (); |
ce1b14f4 | 4331 | } |
bf8e3599 | 4332 | |
7e15618b | 4333 | return expand_expr (build_function_call_expr (fn, arglist), |
19bf118a | 4334 | const0_rtx, VOIDmode, EXPAND_NORMAL); |
3311f67b | 4335 | } |
4336 | ||
689df48e | 4337 | /* Expand a call to __builtin_expect. We return our argument and emit a |
4338 | NOTE_INSN_EXPECTED_VALUE note. This is the expansion of __builtin_expect in | |
4339 | a non-jump context. */ | |
89cfe6e5 | 4340 | |
4341 | static rtx | |
aecda0d6 | 4342 | expand_builtin_expect (tree arglist, rtx target) |
89cfe6e5 | 4343 | { |
4344 | tree exp, c; | |
4345 | rtx note, rtx_c; | |
4346 | ||
4347 | if (arglist == NULL_TREE | |
4348 | || TREE_CHAIN (arglist) == NULL_TREE) | |
4349 | return const0_rtx; | |
4350 | exp = TREE_VALUE (arglist); | |
4351 | c = TREE_VALUE (TREE_CHAIN (arglist)); | |
4352 | ||
4353 | if (TREE_CODE (c) != INTEGER_CST) | |
4354 | { | |
4355 | error ("second arg to `__builtin_expect' must be a constant"); | |
4356 | c = integer_zero_node; | |
4357 | } | |
4358 | ||
4359 | target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL); | |
4360 | ||
4361 | /* Don't bother with expected value notes for integral constants. */ | |
b28bedce | 4362 | if (flag_guess_branch_prob && GET_CODE (target) != CONST_INT) |
89cfe6e5 | 4363 | { |
4364 | /* We do need to force this into a register so that we can be | |
4365 | moderately sure to be able to correctly interpret the branch | |
4366 | condition later. */ | |
4367 | target = force_reg (GET_MODE (target), target); | |
bf8e3599 | 4368 | |
89cfe6e5 | 4369 | rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL); |
4370 | ||
31b97e8f | 4371 | note = emit_note (NOTE_INSN_EXPECTED_VALUE); |
89cfe6e5 | 4372 | NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c); |
4373 | } | |
4374 | ||
4375 | return target; | |
4376 | } | |
689df48e | 4377 | |
4378 | /* Like expand_builtin_expect, except do this in a jump context. This is | |
4379 | called from do_jump if the conditional is a __builtin_expect. Return either | |
31d3e01c | 4380 | a list of insns to emit the jump or NULL if we cannot optimize |
689df48e | 4381 | __builtin_expect. We need to optimize this at jump time so that machines |
4382 | like the PowerPC don't turn the test into a SCC operation, and then jump | |
4383 | based on the test being 0/1. */ | |
4384 | ||
4385 | rtx | |
aecda0d6 | 4386 | expand_builtin_expect_jump (tree exp, rtx if_false_label, rtx if_true_label) |
689df48e | 4387 | { |
4388 | tree arglist = TREE_OPERAND (exp, 1); | |
4389 | tree arg0 = TREE_VALUE (arglist); | |
4390 | tree arg1 = TREE_VALUE (TREE_CHAIN (arglist)); | |
4391 | rtx ret = NULL_RTX; | |
4392 | ||
4393 | /* Only handle __builtin_expect (test, 0) and | |
4394 | __builtin_expect (test, 1). */ | |
4395 | if (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE | |
27d0c333 | 4396 | && (integer_zerop (arg1) || integer_onep (arg1))) |
689df48e | 4397 | { |
689df48e | 4398 | int num_jumps = 0; |
0d21cbc7 | 4399 | int save_pending_stack_adjust = pending_stack_adjust; |
31d3e01c | 4400 | rtx insn; |
689df48e | 4401 | |
1dc55962 | 4402 | /* If we fail to locate an appropriate conditional jump, we'll |
4403 | fall back to normal evaluation. Ensure that the expression | |
4404 | can be re-evaluated. */ | |
4405 | switch (unsafe_for_reeval (arg0)) | |
4406 | { | |
4407 | case 0: /* Safe. */ | |
4408 | break; | |
4409 | ||
4410 | case 1: /* Mildly unsafe. */ | |
4411 | arg0 = unsave_expr (arg0); | |
4412 | break; | |
4413 | ||
4414 | case 2: /* Wildly unsafe. */ | |
4415 | return NULL_RTX; | |
4416 | } | |
4417 | ||
689df48e | 4418 | /* Expand the jump insns. */ |
4419 | start_sequence (); | |
4420 | do_jump (arg0, if_false_label, if_true_label); | |
31d3e01c | 4421 | ret = get_insns (); |
689df48e | 4422 | end_sequence (); |
4423 | ||
4424 | /* Now that the __builtin_expect has been validated, go through and add | |
4425 | the expect's to each of the conditional jumps. If we run into an | |
4426 | error, just give up and generate the 'safe' code of doing a SCC | |
4427 | operation and then doing a branch on that. */ | |
31d3e01c | 4428 | insn = ret; |
4429 | while (insn != NULL_RTX) | |
689df48e | 4430 | { |
31d3e01c | 4431 | rtx next = NEXT_INSN (insn); |
689df48e | 4432 | |
7caedf36 | 4433 | if (GET_CODE (insn) == JUMP_INSN && any_condjump_p (insn)) |
689df48e | 4434 | { |
7caedf36 | 4435 | rtx ifelse = SET_SRC (pc_set (insn)); |
689df48e | 4436 | rtx label; |
4437 | int taken; | |
4438 | ||
689df48e | 4439 | if (GET_CODE (XEXP (ifelse, 1)) == LABEL_REF) |
4440 | { | |
4441 | taken = 1; | |
4442 | label = XEXP (XEXP (ifelse, 1), 0); | |
4443 | } | |
4444 | /* An inverted jump reverses the probabilities. */ | |
4445 | else if (GET_CODE (XEXP (ifelse, 2)) == LABEL_REF) | |
4446 | { | |
4447 | taken = 0; | |
4448 | label = XEXP (XEXP (ifelse, 2), 0); | |
4449 | } | |
4450 | /* We shouldn't have to worry about conditional returns during | |
4451 | the expansion stage, but handle it gracefully anyway. */ | |
4452 | else if (GET_CODE (XEXP (ifelse, 1)) == RETURN) | |
4453 | { | |
4454 | taken = 1; | |
4455 | label = NULL_RTX; | |
4456 | } | |
4457 | /* An inverted return reverses the probabilities. */ | |
4458 | else if (GET_CODE (XEXP (ifelse, 2)) == RETURN) | |
4459 | { | |
4460 | taken = 0; | |
4461 | label = NULL_RTX; | |
4462 | } | |
4463 | else | |
31d3e01c | 4464 | goto do_next_insn; |
689df48e | 4465 | |
4466 | /* If the test is expected to fail, reverse the | |
4467 | probabilities. */ | |
27d0c333 | 4468 | if (integer_zerop (arg1)) |
689df48e | 4469 | taken = 1 - taken; |
4470 | ||
4471 | /* If we are jumping to the false label, reverse the | |
4472 | probabilities. */ | |
4473 | if (label == NULL_RTX) | |
4474 | ; /* conditional return */ | |
4475 | else if (label == if_false_label) | |
4476 | taken = 1 - taken; | |
4477 | else if (label != if_true_label) | |
31d3e01c | 4478 | goto do_next_insn; |
689df48e | 4479 | |
4480 | num_jumps++; | |
4481 | predict_insn_def (insn, PRED_BUILTIN_EXPECT, taken); | |
4482 | } | |
31d3e01c | 4483 | |
4484 | do_next_insn: | |
4485 | insn = next; | |
689df48e | 4486 | } |
4487 | ||
4488 | /* If no jumps were modified, fail and do __builtin_expect the normal | |
4489 | way. */ | |
4490 | if (num_jumps == 0) | |
0d21cbc7 | 4491 | { |
4492 | ret = NULL_RTX; | |
4493 | pending_stack_adjust = save_pending_stack_adjust; | |
4494 | } | |
689df48e | 4495 | } |
4496 | ||
4497 | return ret; | |
4498 | } | |
a0ef1725 | 4499 | |
4500 | void | |
aecda0d6 | 4501 | expand_builtin_trap (void) |
a0ef1725 | 4502 | { |
4503 | #ifdef HAVE_trap | |
4504 | if (HAVE_trap) | |
4505 | emit_insn (gen_trap ()); | |
4506 | else | |
4507 | #endif | |
4508 | emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0); | |
4509 | emit_barrier (); | |
4510 | } | |
78a74442 | 4511 | |
4512 | /* Expand a call to fabs, fabsf or fabsl with arguments ARGLIST. | |
4513 | Return 0 if a normal call should be emitted rather than expanding | |
4514 | the function inline. If convenient, the result should be placed | |
4515 | in TARGET. SUBTARGET may be used as the target for computing | |
4516 | the operand. */ | |
4517 | ||
4518 | static rtx | |
aecda0d6 | 4519 | expand_builtin_fabs (tree arglist, rtx target, rtx subtarget) |
78a74442 | 4520 | { |
4521 | enum machine_mode mode; | |
4522 | tree arg; | |
4523 | rtx op0; | |
4524 | ||
4525 | if (!validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
4526 | return 0; | |
4527 | ||
4528 | arg = TREE_VALUE (arglist); | |
4529 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
4530 | op0 = expand_expr (arg, subtarget, VOIDmode, 0); | |
4531 | return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1)); | |
4532 | } | |
4533 | ||
4534 | /* Expand a call to cabs, cabsf or cabsl with arguments ARGLIST. | |
4535 | Return 0 if a normal call should be emitted rather than expanding | |
4536 | the function inline. If convenient, the result should be placed | |
4537 | in target. */ | |
4538 | ||
4539 | static rtx | |
aecda0d6 | 4540 | expand_builtin_cabs (tree arglist, rtx target) |
78a74442 | 4541 | { |
4542 | enum machine_mode mode; | |
4543 | tree arg; | |
4544 | rtx op0; | |
4545 | ||
4546 | if (arglist == 0 || TREE_CHAIN (arglist)) | |
4547 | return 0; | |
4548 | arg = TREE_VALUE (arglist); | |
4549 | if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE | |
4550 | || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE) | |
4551 | return 0; | |
4552 | ||
4553 | mode = TYPE_MODE (TREE_TYPE (arg)); | |
4554 | op0 = expand_expr (arg, NULL_RTX, VOIDmode, 0); | |
4555 | return expand_complex_abs (mode, op0, target, 0); | |
4556 | } | |
4557 | ||
19bf118a | 4558 | /* Create a new constant string literal and return a char* pointer to it. |
4559 | The STRING_CST value is the LEN characters at STR. */ | |
4560 | static tree | |
4561 | build_string_literal (int len, const char *str) | |
4562 | { | |
4563 | tree t, elem, index, type; | |
4564 | ||
4565 | t = build_string (len, str); | |
4566 | elem = build_type_variant (char_type_node, 1, 0); | |
4567 | index = build_index_type (build_int_2 (len - 1, 0)); | |
4568 | type = build_array_type (elem, index); | |
4569 | TREE_TYPE (t) = type; | |
4570 | TREE_CONSTANT (t) = 1; | |
4571 | TREE_READONLY (t) = 1; | |
4572 | TREE_STATIC (t) = 1; | |
4573 | ||
4574 | type = build_pointer_type (type); | |
4575 | t = build1 (ADDR_EXPR, type, t); | |
4576 | ||
4577 | type = build_pointer_type (elem); | |
4578 | t = build1 (NOP_EXPR, type, t); | |
4579 | return t; | |
4580 | } | |
4581 | ||
4582 | /* Expand a call to printf or printf_unlocked with argument list ARGLIST. | |
4583 | Return 0 if a normal call should be emitted rather than transforming | |
4584 | the function inline. If convenient, the result should be placed in | |
4585 | TARGET with mode MODE. UNLOCKED indicates this is a printf_unlocked | |
4586 | call. */ | |
4587 | static rtx | |
4588 | expand_builtin_printf (tree arglist, rtx target, enum machine_mode mode, | |
4589 | bool unlocked) | |
4590 | { | |
4591 | tree fn_putchar = unlocked | |
4592 | ? implicit_built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED] | |
4593 | : implicit_built_in_decls[BUILT_IN_PUTCHAR]; | |
4594 | tree fn_puts = unlocked ? implicit_built_in_decls[BUILT_IN_PUTS_UNLOCKED] | |
4595 | : implicit_built_in_decls[BUILT_IN_PUTS]; | |
4596 | const char *fmt_str; | |
4597 | tree fn, fmt, arg; | |
4598 | ||
4599 | /* If the return value is used, don't do the transformation. */ | |
4600 | if (target != const0_rtx) | |
4601 | return 0; | |
4602 | ||
4603 | /* Verify the required arguments in the original call. */ | |
4604 | if (! arglist) | |
4605 | return 0; | |
4606 | fmt = TREE_VALUE (arglist); | |
4607 | if (TREE_CODE (TREE_TYPE (fmt)) != POINTER_TYPE) | |
4608 | return 0; | |
4609 | arglist = TREE_CHAIN (arglist); | |
4610 | ||
4611 | /* Check whether the format is a literal string constant. */ | |
4612 | fmt_str = c_getstr (fmt); | |
4613 | if (fmt_str == NULL) | |
4614 | return 0; | |
4615 | ||
4616 | /* If the format specifier was "%s\n", call __builtin_puts(arg). */ | |
4617 | if (strcmp (fmt_str, "%s\n") == 0) | |
4618 | { | |
4619 | if (! arglist | |
4620 | || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE | |
4621 | || TREE_CHAIN (arglist)) | |
4622 | return 0; | |
4623 | fn = fn_puts; | |
4624 | } | |
4625 | /* If the format specifier was "%c", call __builtin_putchar(arg). */ | |
4626 | else if (strcmp (fmt_str, "%c") == 0) | |
4627 | { | |
4628 | if (! arglist | |
4629 | || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE | |
4630 | || TREE_CHAIN (arglist)) | |
4631 | return 0; | |
4632 | fn = fn_putchar; | |
4633 | } | |
4634 | else | |
4635 | { | |
4636 | /* We can't handle anything else with % args or %% ... yet. */ | |
4637 | if (strchr (fmt_str, '%')) | |
4638 | return 0; | |
4639 | ||
4640 | if (arglist) | |
4641 | return 0; | |
4642 | ||
4643 | /* If the format specifier was "", printf does nothing. */ | |
4644 | if (fmt_str[0] == '\0') | |
4645 | return const0_rtx; | |
4646 | /* If the format specifier has length of 1, call putchar. */ | |
4647 | if (fmt_str[1] == '\0') | |
4648 | { | |
4649 | /* Given printf("c"), (where c is any one character,) | |
4650 | convert "c"[0] to an int and pass that to the replacement | |
4651 | function. */ | |
4652 | arg = build_int_2 (fmt_str[0], 0); | |
4653 | arglist = build_tree_list (NULL_TREE, arg); | |
4654 | fn = fn_putchar; | |
4655 | } | |
4656 | else | |
4657 | { | |
4658 | /* If the format specifier was "string\n", call puts("string"). */ | |
4659 | size_t len = strlen (fmt_str); | |
4660 | if (fmt_str[len - 1] == '\n') | |
4661 | { | |
91c82c20 | 4662 | /* Create a NUL-terminated string that's one char shorter |
19bf118a | 4663 | than the original, stripping off the trailing '\n'. */ |
4664 | char *newstr = (char *) alloca (len); | |
4665 | memcpy (newstr, fmt_str, len - 1); | |
4666 | newstr[len - 1] = 0; | |
4667 | ||
4668 | arg = build_string_literal (len, newstr); | |
4669 | arglist = build_tree_list (NULL_TREE, arg); | |
4670 | fn = fn_puts; | |
4671 | } | |
4672 | else | |
4673 | /* We'd like to arrange to call fputs(string,stdout) here, | |
4674 | but we need stdout and don't have a way to get it yet. */ | |
4675 | return 0; | |
4676 | } | |
4677 | } | |
4678 | ||
4679 | if (!fn) | |
4680 | return 0; | |
4681 | return expand_expr (build_function_call_expr (fn, arglist), | |
4682 | target, mode, EXPAND_NORMAL); | |
4683 | } | |
4684 | ||
4685 | /* Expand a call to fprintf or fprintf_unlocked with argument list ARGLIST. | |
4686 | Return 0 if a normal call should be emitted rather than transforming | |
4687 | the function inline. If convenient, the result should be placed in | |
4688 | TARGET with mode MODE. UNLOCKED indicates this is a fprintf_unlocked | |
4689 | call. */ | |
4690 | static rtx | |
4691 | expand_builtin_fprintf (tree arglist, rtx target, enum machine_mode mode, | |
4692 | bool unlocked) | |
4693 | { | |
4694 | tree fn_fputc = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTC_UNLOCKED] | |
4695 | : implicit_built_in_decls[BUILT_IN_FPUTC]; | |
4696 | tree fn_fputs = unlocked ? implicit_built_in_decls[BUILT_IN_FPUTS_UNLOCKED] | |
4697 | : implicit_built_in_decls[BUILT_IN_FPUTS]; | |
4698 | const char *fmt_str; | |
4699 | tree fn, fmt, fp, arg; | |
4700 | ||
4701 | /* If the return value is used, don't do the transformation. */ | |
4702 | if (target != const0_rtx) | |
4703 | return 0; | |
4704 | ||
4705 | /* Verify the required arguments in the original call. */ | |
4706 | if (! arglist) | |
4707 | return 0; | |
4708 | fp = TREE_VALUE (arglist); | |
4709 | if (TREE_CODE (TREE_TYPE (fp)) != POINTER_TYPE) | |
4710 | return 0; | |
4711 | arglist = TREE_CHAIN (arglist); | |
4712 | if (! arglist) | |
4713 | return 0; | |
4714 | fmt = TREE_VALUE (arglist); | |
4715 | if (TREE_CODE (TREE_TYPE (fmt)) != POINTER_TYPE) | |
4716 | return 0; | |
4717 | arglist = TREE_CHAIN (arglist); | |
4718 | ||
4719 | /* Check whether the format is a literal string constant. */ | |
4720 | fmt_str = c_getstr (fmt); | |
4721 | if (fmt_str == NULL) | |
4722 | return 0; | |
4723 | ||
4724 | /* If the format specifier was "%s", call __builtin_fputs(arg,fp). */ | |
4725 | if (strcmp (fmt_str, "%s") == 0) | |
4726 | { | |
4727 | if (! arglist | |
4728 | || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE | |
4729 | || TREE_CHAIN (arglist)) | |
4730 | return 0; | |
4731 | arg = TREE_VALUE (arglist); | |
4732 | arglist = build_tree_list (NULL_TREE, fp); | |
4733 | arglist = tree_cons (NULL_TREE, arg, arglist); | |
4734 | fn = fn_fputs; | |
4735 | } | |
4736 | /* If the format specifier was "%c", call __builtin_fputc(arg,fp). */ | |
4737 | else if (strcmp (fmt_str, "%c") == 0) | |
4738 | { | |
4739 | if (! arglist | |
4740 | || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE | |
4741 | || TREE_CHAIN (arglist)) | |
4742 | return 0; | |
4743 | arg = TREE_VALUE (arglist); | |
4744 | arglist = build_tree_list (NULL_TREE, fp); | |
4745 | arglist = tree_cons (NULL_TREE, arg, arglist); | |
4746 | fn = fn_fputc; | |
4747 | } | |
4748 | else | |
4749 | { | |
4750 | /* We can't handle anything else with % args or %% ... yet. */ | |
4751 | if (strchr (fmt_str, '%')) | |
4752 | return 0; | |
4753 | ||
4754 | if (arglist) | |
4755 | return 0; | |
4756 | ||
4757 | /* If the format specifier was "", fprintf does nothing. */ | |
4758 | if (fmt_str[0] == '\0') | |
4759 | { | |
4760 | /* Evaluate and ignore FILE* argument for side-effects. */ | |
4761 | expand_expr (fp, const0_rtx, VOIDmode, EXPAND_NORMAL); | |
4762 | return const0_rtx; | |
4763 | } | |
4764 | ||
4765 | /* When "string" doesn't contain %, replace all cases of | |
4766 | fprintf(stream,string) with fputs(string,stream). The fputs | |
4767 | builtin will take care of special cases like length == 1. */ | |
4768 | arglist = build_tree_list (NULL_TREE, fp); | |
4769 | arglist = tree_cons (NULL_TREE, fmt, arglist); | |
4770 | fn = fn_fputs; | |
4771 | } | |
4772 | ||
4773 | if (!fn) | |
4774 | return 0; | |
4775 | return expand_expr (build_function_call_expr (fn, arglist), | |
4776 | target, mode, EXPAND_NORMAL); | |
4777 | } | |
4778 | ||
6411575e | 4779 | /* Expand a call to sprintf with argument list ARGLIST. Return 0 if |
4780 | a normal call should be emitted rather than expanding the function | |
4781 | inline. If convenient, the result should be placed in TARGET with | |
4782 | mode MODE. */ | |
4783 | ||
4784 | static rtx | |
4785 | expand_builtin_sprintf (tree arglist, rtx target, enum machine_mode mode) | |
4786 | { | |
a7a723f0 | 4787 | tree orig_arglist, dest, fmt; |
4788 | const char *fmt_str; | |
6411575e | 4789 | |
4790 | orig_arglist = arglist; | |
4791 | ||
4792 | /* Verify the required arguments in the original call. */ | |
4793 | if (! arglist) | |
4794 | return 0; | |
4795 | dest = TREE_VALUE (arglist); | |
4796 | if (TREE_CODE (TREE_TYPE (dest)) != POINTER_TYPE) | |
4797 | return 0; | |
4798 | arglist = TREE_CHAIN (arglist); | |
4799 | if (! arglist) | |
4800 | return 0; | |
4801 | fmt = TREE_VALUE (arglist); | |
19bf118a | 4802 | if (TREE_CODE (TREE_TYPE (fmt)) != POINTER_TYPE) |
6411575e | 4803 | return 0; |
4804 | arglist = TREE_CHAIN (arglist); | |
4805 | ||
4806 | /* Check whether the format is a literal string constant. */ | |
a7a723f0 | 4807 | fmt_str = c_getstr (fmt); |
4808 | if (fmt_str == NULL) | |
6411575e | 4809 | return 0; |
4810 | ||
4811 | /* If the format doesn't contain % args or %%, use strcpy. */ | |
a7a723f0 | 4812 | if (strchr (fmt_str, '%') == 0) |
6411575e | 4813 | { |
4814 | tree fn = implicit_built_in_decls[BUILT_IN_STRCPY]; | |
4815 | tree exp; | |
4816 | ||
a7a723f0 | 4817 | if (arglist || ! fn) |
6411575e | 4818 | return 0; |
4819 | expand_expr (build_function_call_expr (fn, orig_arglist), | |
4820 | const0_rtx, VOIDmode, EXPAND_NORMAL); | |
4821 | if (target == const0_rtx) | |
4822 | return const0_rtx; | |
a7a723f0 | 4823 | exp = build_int_2 (strlen (fmt_str), 0); |
6411575e | 4824 | exp = fold (build1 (NOP_EXPR, integer_type_node, exp)); |
4825 | return expand_expr (exp, target, mode, EXPAND_NORMAL); | |
4826 | } | |
a7a723f0 | 4827 | /* If the format is "%s", use strcpy if the result isn't used. */ |
4828 | else if (strcmp (fmt_str, "%s") == 0) | |
6411575e | 4829 | { |
a7a723f0 | 4830 | tree fn, arg, len; |
4831 | fn = implicit_built_in_decls[BUILT_IN_STRCPY]; | |
6411575e | 4832 | |
a7a723f0 | 4833 | if (! fn) |
6411575e | 4834 | return 0; |
4835 | ||
4836 | if (! arglist || TREE_CHAIN (arglist)) | |
4837 | return 0; | |
4838 | arg = TREE_VALUE (arglist); | |
4839 | if (TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE) | |
4840 | return 0; | |
4841 | ||
4842 | if (target != const0_rtx) | |
4843 | { | |
681fab1e | 4844 | len = c_strlen (arg, 1); |
a7a723f0 | 4845 | if (! len || TREE_CODE (len) != INTEGER_CST) |
6411575e | 4846 | return 0; |
6411575e | 4847 | } |
4848 | else | |
a7a723f0 | 4849 | len = NULL_TREE; |
6411575e | 4850 | |
4851 | arglist = build_tree_list (NULL_TREE, arg); | |
4852 | arglist = tree_cons (NULL_TREE, dest, arglist); | |
a7a723f0 | 4853 | expand_expr (build_function_call_expr (fn, arglist), |
6411575e | 4854 | const0_rtx, VOIDmode, EXPAND_NORMAL); |
4855 | ||
4856 | if (target == const0_rtx) | |
4857 | return const0_rtx; | |
a7a723f0 | 4858 | return expand_expr (len, target, mode, EXPAND_NORMAL); |
6411575e | 4859 | } |
4860 | ||
4861 | return 0; | |
4862 | } | |
53800dbe | 4863 | \f |
4864 | /* Expand an expression EXP that calls a built-in function, | |
4865 | with result going to TARGET if that's convenient | |
4866 | (and in mode MODE if that's convenient). | |
4867 | SUBTARGET may be used as the target for computing one of EXP's operands. | |
4868 | IGNORE is nonzero if the value is to be ignored. */ | |
4869 | ||
4870 | rtx | |
aecda0d6 | 4871 | expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode, |
4872 | int ignore) | |
53800dbe | 4873 | { |
c6e6ecb1 | 4874 | tree fndecl = get_callee_fndecl (exp); |
53800dbe | 4875 | tree arglist = TREE_OPERAND (exp, 1); |
4876 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
efb070c8 | 4877 | enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp)); |
53800dbe | 4878 | |
cd9ff771 | 4879 | /* Perform postincrements before expanding builtin functions. */ |
313b27fa | 4880 | emit_queue (); |
4881 | ||
8305149e | 4882 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) |
fc2a2dcb | 4883 | return (*targetm.expand_builtin) (exp, target, subtarget, mode, ignore); |
bf8e3599 | 4884 | |
53800dbe | 4885 | /* When not optimizing, generate calls to library functions for a certain |
4886 | set of builtins. */ | |
cd9ff771 | 4887 | if (!optimize |
4888 | && !CALLED_AS_BUILT_IN (fndecl) | |
4889 | && DECL_ASSEMBLER_NAME_SET_P (fndecl) | |
4890 | && fcode != BUILT_IN_ALLOCA) | |
4891 | return expand_call (exp, target, ignore); | |
53800dbe | 4892 | |
8d6d7930 | 4893 | /* The built-in function expanders test for target == const0_rtx |
4894 | to determine whether the function's result will be ignored. */ | |
4895 | if (ignore) | |
4896 | target = const0_rtx; | |
4897 | ||
4898 | /* If the result of a pure or const built-in function is ignored, and | |
4899 | none of its arguments are volatile, we can avoid expanding the | |
4900 | built-in call and just evaluate the arguments for side-effects. */ | |
4901 | if (target == const0_rtx | |
4902 | && (DECL_IS_PURE (fndecl) || TREE_READONLY (fndecl))) | |
4903 | { | |
4904 | bool volatilep = false; | |
4905 | tree arg; | |
4906 | ||
4907 | for (arg = arglist; arg; arg = TREE_CHAIN (arg)) | |
4908 | if (TREE_THIS_VOLATILE (TREE_VALUE (arg))) | |
4909 | { | |
4910 | volatilep = true; | |
4911 | break; | |
4912 | } | |
4913 | ||
4914 | if (! volatilep) | |
4915 | { | |
4916 | for (arg = arglist; arg; arg = TREE_CHAIN (arg)) | |
4917 | expand_expr (TREE_VALUE (arg), const0_rtx, | |
4918 | VOIDmode, EXPAND_NORMAL); | |
4919 | return const0_rtx; | |
4920 | } | |
4921 | } | |
4922 | ||
53800dbe | 4923 | switch (fcode) |
4924 | { | |
4925 | case BUILT_IN_ABS: | |
d2d4bdde | 4926 | case BUILT_IN_LABS: |
4927 | case BUILT_IN_LLABS: | |
4928 | case BUILT_IN_IMAXABS: | |
78a74442 | 4929 | /* build_function_call changes these into ABS_EXPR. */ |
4930 | abort (); | |
4931 | ||
53800dbe | 4932 | case BUILT_IN_FABS: |
d2d4bdde | 4933 | case BUILT_IN_FABSF: |
4934 | case BUILT_IN_FABSL: | |
78a74442 | 4935 | target = expand_builtin_fabs (arglist, target, subtarget); |
4936 | if (target) | |
4937 | return target; | |
4938 | break; | |
4939 | ||
4940 | case BUILT_IN_CABS: | |
4941 | case BUILT_IN_CABSF: | |
4942 | case BUILT_IN_CABSL: | |
4943 | if (flag_unsafe_math_optimizations) | |
4944 | { | |
4945 | target = expand_builtin_cabs (arglist, target); | |
4946 | if (target) | |
4947 | return target; | |
4948 | } | |
4949 | break; | |
53800dbe | 4950 | |
d30e4d04 | 4951 | case BUILT_IN_CONJ: |
d2d4bdde | 4952 | case BUILT_IN_CONJF: |
4953 | case BUILT_IN_CONJL: | |
d30e4d04 | 4954 | case BUILT_IN_CREAL: |
d2d4bdde | 4955 | case BUILT_IN_CREALF: |
4956 | case BUILT_IN_CREALL: | |
d30e4d04 | 4957 | case BUILT_IN_CIMAG: |
d2d4bdde | 4958 | case BUILT_IN_CIMAGF: |
4959 | case BUILT_IN_CIMAGL: | |
d30e4d04 | 4960 | /* expand_tree_builtin changes these into CONJ_EXPR, REALPART_EXPR |
4961 | and IMAGPART_EXPR. */ | |
4962 | abort (); | |
4963 | ||
53800dbe | 4964 | case BUILT_IN_SIN: |
d2d4bdde | 4965 | case BUILT_IN_SINF: |
4966 | case BUILT_IN_SINL: | |
53800dbe | 4967 | case BUILT_IN_COS: |
d2d4bdde | 4968 | case BUILT_IN_COSF: |
4969 | case BUILT_IN_COSL: | |
42721db0 | 4970 | case BUILT_IN_EXP: |
4971 | case BUILT_IN_EXPF: | |
4972 | case BUILT_IN_EXPL: | |
4973 | case BUILT_IN_LOG: | |
4974 | case BUILT_IN_LOGF: | |
4975 | case BUILT_IN_LOGL: | |
528ee710 | 4976 | case BUILT_IN_TAN: |
4977 | case BUILT_IN_TANF: | |
4978 | case BUILT_IN_TANL: | |
4979 | case BUILT_IN_ATAN: | |
4980 | case BUILT_IN_ATANF: | |
4981 | case BUILT_IN_ATANL: | |
7f3be425 | 4982 | /* Treat these like sqrt only if unsafe math optimizations are allowed, |
4983 | because of possible accuracy problems. */ | |
4984 | if (! flag_unsafe_math_optimizations) | |
53800dbe | 4985 | break; |
75357f66 | 4986 | case BUILT_IN_SQRT: |
d2d4bdde | 4987 | case BUILT_IN_SQRTF: |
4988 | case BUILT_IN_SQRTL: | |
805e22b2 | 4989 | case BUILT_IN_FLOOR: |
4990 | case BUILT_IN_FLOORF: | |
4991 | case BUILT_IN_FLOORL: | |
4992 | case BUILT_IN_CEIL: | |
4993 | case BUILT_IN_CEILF: | |
4994 | case BUILT_IN_CEILL: | |
4995 | case BUILT_IN_TRUNC: | |
4996 | case BUILT_IN_TRUNCF: | |
4997 | case BUILT_IN_TRUNCL: | |
4998 | case BUILT_IN_ROUND: | |
4999 | case BUILT_IN_ROUNDF: | |
5000 | case BUILT_IN_ROUNDL: | |
5001 | case BUILT_IN_NEARBYINT: | |
5002 | case BUILT_IN_NEARBYINTF: | |
5003 | case BUILT_IN_NEARBYINTL: | |
53800dbe | 5004 | target = expand_builtin_mathfn (exp, target, subtarget); |
5005 | if (target) | |
5006 | return target; | |
5007 | break; | |
5008 | ||
0fd605a5 | 5009 | case BUILT_IN_POW: |
5010 | case BUILT_IN_POWF: | |
5011 | case BUILT_IN_POWL: | |
f1b844c6 | 5012 | if (! flag_unsafe_math_optimizations) |
5013 | break; | |
5014 | target = expand_builtin_pow (exp, target, subtarget); | |
5015 | if (target) | |
5016 | return target; | |
5017 | break; | |
5018 | ||
0fd605a5 | 5019 | case BUILT_IN_ATAN2: |
5020 | case BUILT_IN_ATAN2F: | |
5021 | case BUILT_IN_ATAN2L: | |
5022 | if (! flag_unsafe_math_optimizations) | |
5023 | break; | |
5024 | target = expand_builtin_mathfn_2 (exp, target, subtarget); | |
5025 | if (target) | |
5026 | return target; | |
5027 | break; | |
5028 | ||
53800dbe | 5029 | case BUILT_IN_APPLY_ARGS: |
5030 | return expand_builtin_apply_args (); | |
5031 | ||
5032 | /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes | |
5033 | FUNCTION with a copy of the parameters described by | |
5034 | ARGUMENTS, and ARGSIZE. It returns a block of memory | |
5035 | allocated on the stack into which is stored all the registers | |
5036 | that might possibly be used for returning the result of a | |
5037 | function. ARGUMENTS is the value returned by | |
5038 | __builtin_apply_args. ARGSIZE is the number of bytes of | |
5039 | arguments that must be copied. ??? How should this value be | |
5040 | computed? We'll also need a safe worst case value for varargs | |
5041 | functions. */ | |
5042 | case BUILT_IN_APPLY: | |
0eb671f7 | 5043 | if (!validate_arglist (arglist, POINTER_TYPE, |
5044 | POINTER_TYPE, INTEGER_TYPE, VOID_TYPE) | |
5045 | && !validate_arglist (arglist, REFERENCE_TYPE, | |
5046 | POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) | |
53800dbe | 5047 | return const0_rtx; |
5048 | else | |
5049 | { | |
5050 | int i; | |
5051 | tree t; | |
5052 | rtx ops[3]; | |
5053 | ||
5054 | for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++) | |
5055 | ops[i] = expand_expr (TREE_VALUE (t), NULL_RTX, VOIDmode, 0); | |
5056 | ||
5057 | return expand_builtin_apply (ops[0], ops[1], ops[2]); | |
5058 | } | |
5059 | ||
5060 | /* __builtin_return (RESULT) causes the function to return the | |
5061 | value described by RESULT. RESULT is address of the block of | |
5062 | memory returned by __builtin_apply. */ | |
5063 | case BUILT_IN_RETURN: | |
0eb671f7 | 5064 | if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE)) |
53800dbe | 5065 | expand_builtin_return (expand_expr (TREE_VALUE (arglist), |
5066 | NULL_RTX, VOIDmode, 0)); | |
5067 | return const0_rtx; | |
5068 | ||
5069 | case BUILT_IN_SAVEREGS: | |
a66c9326 | 5070 | return expand_builtin_saveregs (); |
53800dbe | 5071 | |
5072 | case BUILT_IN_ARGS_INFO: | |
80cd7a5e | 5073 | return expand_builtin_args_info (arglist); |
53800dbe | 5074 | |
5075 | /* Return the address of the first anonymous stack arg. */ | |
5076 | case BUILT_IN_NEXT_ARG: | |
a66c9326 | 5077 | return expand_builtin_next_arg (arglist); |
53800dbe | 5078 | |
5079 | case BUILT_IN_CLASSIFY_TYPE: | |
5080 | return expand_builtin_classify_type (arglist); | |
5081 | ||
5082 | case BUILT_IN_CONSTANT_P: | |
80cd7a5e | 5083 | return expand_builtin_constant_p (arglist, target_mode); |
53800dbe | 5084 | |
5085 | case BUILT_IN_FRAME_ADDRESS: | |
5086 | case BUILT_IN_RETURN_ADDRESS: | |
80cd7a5e | 5087 | return expand_builtin_frame_address (fndecl, arglist); |
53800dbe | 5088 | |
5089 | /* Returns the address of the area where the structure is returned. | |
5090 | 0 otherwise. */ | |
5091 | case BUILT_IN_AGGREGATE_INCOMING_ADDRESS: | |
5092 | if (arglist != 0 | |
9342ee68 | 5093 | || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))) |
5094 | || GET_CODE (DECL_RTL (DECL_RESULT (current_function_decl))) != MEM) | |
5095 | return const0_rtx; | |
53800dbe | 5096 | else |
9342ee68 | 5097 | return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0); |
53800dbe | 5098 | |
5099 | case BUILT_IN_ALLOCA: | |
5100 | target = expand_builtin_alloca (arglist, target); | |
5101 | if (target) | |
5102 | return target; | |
5103 | break; | |
5104 | ||
5105 | case BUILT_IN_FFS: | |
6a08d0ab | 5106 | case BUILT_IN_FFSL: |
5107 | case BUILT_IN_FFSLL: | |
efb070c8 | 5108 | target = expand_builtin_unop (target_mode, arglist, target, |
5109 | subtarget, ffs_optab); | |
6a08d0ab | 5110 | if (target) |
5111 | return target; | |
5112 | break; | |
5113 | ||
5114 | case BUILT_IN_CLZ: | |
5115 | case BUILT_IN_CLZL: | |
5116 | case BUILT_IN_CLZLL: | |
efb070c8 | 5117 | target = expand_builtin_unop (target_mode, arglist, target, |
5118 | subtarget, clz_optab); | |
6a08d0ab | 5119 | if (target) |
5120 | return target; | |
5121 | break; | |
5122 | ||
5123 | case BUILT_IN_CTZ: | |
5124 | case BUILT_IN_CTZL: | |
5125 | case BUILT_IN_CTZLL: | |
efb070c8 | 5126 | target = expand_builtin_unop (target_mode, arglist, target, |
5127 | subtarget, ctz_optab); | |
6a08d0ab | 5128 | if (target) |
5129 | return target; | |
5130 | break; | |
5131 | ||
5132 | case BUILT_IN_POPCOUNT: | |
5133 | case BUILT_IN_POPCOUNTL: | |
5134 | case BUILT_IN_POPCOUNTLL: | |
efb070c8 | 5135 | target = expand_builtin_unop (target_mode, arglist, target, |
5136 | subtarget, popcount_optab); | |
6a08d0ab | 5137 | if (target) |
5138 | return target; | |
5139 | break; | |
5140 | ||
5141 | case BUILT_IN_PARITY: | |
5142 | case BUILT_IN_PARITYL: | |
5143 | case BUILT_IN_PARITYLL: | |
efb070c8 | 5144 | target = expand_builtin_unop (target_mode, arglist, target, |
5145 | subtarget, parity_optab); | |
53800dbe | 5146 | if (target) |
5147 | return target; | |
5148 | break; | |
5149 | ||
5150 | case BUILT_IN_STRLEN: | |
80cd7a5e | 5151 | target = expand_builtin_strlen (arglist, target, target_mode); |
53800dbe | 5152 | if (target) |
5153 | return target; | |
5154 | break; | |
5155 | ||
5156 | case BUILT_IN_STRCPY: | |
80cd7a5e | 5157 | target = expand_builtin_strcpy (arglist, target, mode); |
53800dbe | 5158 | if (target) |
5159 | return target; | |
5160 | break; | |
bf8e3599 | 5161 | |
ed09096d | 5162 | case BUILT_IN_STRNCPY: |
5163 | target = expand_builtin_strncpy (arglist, target, mode); | |
5164 | if (target) | |
5165 | return target; | |
5166 | break; | |
bf8e3599 | 5167 | |
3b824fa6 | 5168 | case BUILT_IN_STPCPY: |
5169 | target = expand_builtin_stpcpy (arglist, target, mode); | |
5170 | if (target) | |
5171 | return target; | |
5172 | break; | |
5173 | ||
49f0327b | 5174 | case BUILT_IN_STRCAT: |
5175 | target = expand_builtin_strcat (arglist, target, mode); | |
5176 | if (target) | |
5177 | return target; | |
5178 | break; | |
bf8e3599 | 5179 | |
49f0327b | 5180 | case BUILT_IN_STRNCAT: |
5181 | target = expand_builtin_strncat (arglist, target, mode); | |
5182 | if (target) | |
5183 | return target; | |
5184 | break; | |
bf8e3599 | 5185 | |
49f0327b | 5186 | case BUILT_IN_STRSPN: |
5187 | target = expand_builtin_strspn (arglist, target, mode); | |
5188 | if (target) | |
5189 | return target; | |
5190 | break; | |
bf8e3599 | 5191 | |
49f0327b | 5192 | case BUILT_IN_STRCSPN: |
5193 | target = expand_builtin_strcspn (arglist, target, mode); | |
5194 | if (target) | |
5195 | return target; | |
5196 | break; | |
bf8e3599 | 5197 | |
17f5ea87 | 5198 | case BUILT_IN_STRSTR: |
5199 | target = expand_builtin_strstr (arglist, target, mode); | |
5200 | if (target) | |
5201 | return target; | |
5202 | break; | |
bf8e3599 | 5203 | |
46f3a74a | 5204 | case BUILT_IN_STRPBRK: |
5205 | target = expand_builtin_strpbrk (arglist, target, mode); | |
5206 | if (target) | |
5207 | return target; | |
5208 | break; | |
bf8e3599 | 5209 | |
398aae36 | 5210 | case BUILT_IN_INDEX: |
83d79705 | 5211 | case BUILT_IN_STRCHR: |
5212 | target = expand_builtin_strchr (arglist, target, mode); | |
5213 | if (target) | |
5214 | return target; | |
5215 | break; | |
5216 | ||
398aae36 | 5217 | case BUILT_IN_RINDEX: |
83d79705 | 5218 | case BUILT_IN_STRRCHR: |
5219 | target = expand_builtin_strrchr (arglist, target, mode); | |
5220 | if (target) | |
5221 | return target; | |
5222 | break; | |
5223 | ||
53800dbe | 5224 | case BUILT_IN_MEMCPY: |
9fe0e1b8 | 5225 | target = expand_builtin_memcpy (arglist, target, mode); |
3b824fa6 | 5226 | if (target) |
5227 | return target; | |
5228 | break; | |
5229 | ||
5230 | case BUILT_IN_MEMPCPY: | |
9fe0e1b8 | 5231 | target = expand_builtin_mempcpy (arglist, target, mode, /*endp=*/ 1); |
53800dbe | 5232 | if (target) |
5233 | return target; | |
5234 | break; | |
5235 | ||
c4950093 | 5236 | case BUILT_IN_MEMMOVE: |
5237 | target = expand_builtin_memmove (arglist, target, mode); | |
5238 | if (target) | |
5239 | return target; | |
5240 | break; | |
5241 | ||
5242 | case BUILT_IN_BCOPY: | |
5243 | target = expand_builtin_bcopy (arglist); | |
5244 | if (target) | |
5245 | return target; | |
5246 | break; | |
5247 | ||
53800dbe | 5248 | case BUILT_IN_MEMSET: |
80cd7a5e | 5249 | target = expand_builtin_memset (arglist, target, mode); |
53800dbe | 5250 | if (target) |
5251 | return target; | |
5252 | break; | |
5253 | ||
ffc83088 | 5254 | case BUILT_IN_BZERO: |
80cd7a5e | 5255 | target = expand_builtin_bzero (arglist); |
ffc83088 | 5256 | if (target) |
5257 | return target; | |
5258 | break; | |
5259 | ||
53800dbe | 5260 | case BUILT_IN_STRCMP: |
83d79705 | 5261 | target = expand_builtin_strcmp (exp, target, mode); |
53800dbe | 5262 | if (target) |
5263 | return target; | |
5264 | break; | |
5265 | ||
ed09096d | 5266 | case BUILT_IN_STRNCMP: |
5267 | target = expand_builtin_strncmp (exp, target, mode); | |
5268 | if (target) | |
5269 | return target; | |
5270 | break; | |
5271 | ||
071f1696 | 5272 | case BUILT_IN_BCMP: |
53800dbe | 5273 | case BUILT_IN_MEMCMP: |
6f428e8b | 5274 | target = expand_builtin_memcmp (exp, arglist, target, mode); |
53800dbe | 5275 | if (target) |
5276 | return target; | |
5277 | break; | |
53800dbe | 5278 | |
5279 | case BUILT_IN_SETJMP: | |
6b7f6858 | 5280 | target = expand_builtin_setjmp (arglist, target); |
5281 | if (target) | |
5282 | return target; | |
5283 | break; | |
53800dbe | 5284 | |
5285 | /* __builtin_longjmp is passed a pointer to an array of five words. | |
5286 | It's similar to the C library longjmp function but works with | |
5287 | __builtin_setjmp above. */ | |
5288 | case BUILT_IN_LONGJMP: | |
0eb671f7 | 5289 | if (!validate_arglist (arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) |
53800dbe | 5290 | break; |
5291 | else | |
5292 | { | |
5293 | rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget, | |
5294 | VOIDmode, 0); | |
5295 | rtx value = expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), | |
5296 | NULL_RTX, VOIDmode, 0); | |
5297 | ||
5298 | if (value != const1_rtx) | |
5299 | { | |
5300 | error ("__builtin_longjmp second argument must be 1"); | |
5301 | return const0_rtx; | |
5302 | } | |
5303 | ||
5304 | expand_builtin_longjmp (buf_addr, value); | |
5305 | return const0_rtx; | |
5306 | } | |
5307 | ||
5308 | case BUILT_IN_TRAP: | |
a0ef1725 | 5309 | expand_builtin_trap (); |
53800dbe | 5310 | return const0_rtx; |
5311 | ||
19bf118a | 5312 | case BUILT_IN_PRINTF: |
5313 | target = expand_builtin_printf (arglist, target, mode, false); | |
5314 | if (target) | |
5315 | return target; | |
5316 | break; | |
5317 | ||
5318 | case BUILT_IN_PRINTF_UNLOCKED: | |
5319 | target = expand_builtin_printf (arglist, target, mode, true); | |
5320 | if (target) | |
5321 | return target; | |
5322 | break; | |
5323 | ||
df94cd3b | 5324 | case BUILT_IN_FPUTS: |
19bf118a | 5325 | target = expand_builtin_fputs (arglist, target, false); |
c013a46e | 5326 | if (target) |
5327 | return target; | |
5328 | break; | |
19bf118a | 5329 | |
c013a46e | 5330 | case BUILT_IN_FPUTS_UNLOCKED: |
19bf118a | 5331 | target = expand_builtin_fputs (arglist, target, true); |
5332 | if (target) | |
5333 | return target; | |
5334 | break; | |
5335 | ||
5336 | case BUILT_IN_FPRINTF: | |
5337 | target = expand_builtin_fprintf (arglist, target, mode, false); | |
5338 | if (target) | |
5339 | return target; | |
5340 | break; | |
5341 | ||
5342 | case BUILT_IN_FPRINTF_UNLOCKED: | |
5343 | target = expand_builtin_fprintf (arglist, target, mode, true); | |
df94cd3b | 5344 | if (target) |
5345 | return target; | |
5346 | break; | |
bf8e3599 | 5347 | |
6411575e | 5348 | case BUILT_IN_SPRINTF: |
5349 | target = expand_builtin_sprintf (arglist, target, mode); | |
5350 | if (target) | |
5351 | return target; | |
5352 | break; | |
5353 | ||
53800dbe | 5354 | /* Various hooks for the DWARF 2 __throw routine. */ |
5355 | case BUILT_IN_UNWIND_INIT: | |
5356 | expand_builtin_unwind_init (); | |
5357 | return const0_rtx; | |
5358 | case BUILT_IN_DWARF_CFA: | |
5359 | return virtual_cfa_rtx; | |
5360 | #ifdef DWARF2_UNWIND_INFO | |
f8f023a5 | 5361 | case BUILT_IN_DWARF_SP_COLUMN: |
5362 | return expand_builtin_dwarf_sp_column (); | |
695e919b | 5363 | case BUILT_IN_INIT_DWARF_REG_SIZES: |
5364 | expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist)); | |
5365 | return const0_rtx; | |
53800dbe | 5366 | #endif |
5367 | case BUILT_IN_FROB_RETURN_ADDR: | |
5368 | return expand_builtin_frob_return_addr (TREE_VALUE (arglist)); | |
5369 | case BUILT_IN_EXTRACT_RETURN_ADDR: | |
5370 | return expand_builtin_extract_return_addr (TREE_VALUE (arglist)); | |
5371 | case BUILT_IN_EH_RETURN: | |
5372 | expand_builtin_eh_return (TREE_VALUE (arglist), | |
df4b504c | 5373 | TREE_VALUE (TREE_CHAIN (arglist))); |
53800dbe | 5374 | return const0_rtx; |
df4b504c | 5375 | #ifdef EH_RETURN_DATA_REGNO |
5376 | case BUILT_IN_EH_RETURN_DATA_REGNO: | |
5377 | return expand_builtin_eh_return_data_regno (arglist); | |
5378 | #endif | |
7ccc713a | 5379 | case BUILT_IN_VA_START: |
a66c9326 | 5380 | case BUILT_IN_STDARG_START: |
7ccc713a | 5381 | return expand_builtin_va_start (arglist); |
a66c9326 | 5382 | case BUILT_IN_VA_END: |
5383 | return expand_builtin_va_end (arglist); | |
5384 | case BUILT_IN_VA_COPY: | |
5385 | return expand_builtin_va_copy (arglist); | |
89cfe6e5 | 5386 | case BUILT_IN_EXPECT: |
5387 | return expand_builtin_expect (arglist, target); | |
5e3608d8 | 5388 | case BUILT_IN_PREFETCH: |
5389 | expand_builtin_prefetch (arglist); | |
5390 | return const0_rtx; | |
5391 | ||
53800dbe | 5392 | |
92482ee0 | 5393 | default: /* just do library call, if unknown builtin */ |
5394 | if (!DECL_ASSEMBLER_NAME_SET_P (fndecl)) | |
5395 | error ("built-in function `%s' not currently supported", | |
5396 | IDENTIFIER_POINTER (DECL_NAME (fndecl))); | |
53800dbe | 5397 | } |
5398 | ||
5399 | /* The switch statement above can drop through to cause the function | |
5400 | to be called normally. */ | |
5401 | return expand_call (exp, target, ignore); | |
5402 | } | |
650e4c94 | 5403 | |
805e22b2 | 5404 | /* Determine whether a tree node represents a call to a built-in |
5405 | math function. If the tree T is a call to a built-in function | |
5406 | taking a single real argument, then the return value is the | |
5407 | DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT. Otherwise | |
5408 | the return value is END_BUILTINS. */ | |
aecda0d6 | 5409 | |
805e22b2 | 5410 | enum built_in_function |
aecda0d6 | 5411 | builtin_mathfn_code (tree t) |
805e22b2 | 5412 | { |
5413 | tree fndecl, arglist; | |
5414 | ||
5415 | if (TREE_CODE (t) != CALL_EXPR | |
5416 | || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR) | |
5417 | return END_BUILTINS; | |
5418 | ||
c6e6ecb1 | 5419 | fndecl = get_callee_fndecl (t); |
5420 | if (fndecl == NULL_TREE | |
805e22b2 | 5421 | || ! DECL_BUILT_IN (fndecl) |
5422 | || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) | |
5423 | return END_BUILTINS; | |
5424 | ||
5425 | arglist = TREE_OPERAND (t, 1); | |
5426 | if (! arglist | |
e9f80ff5 | 5427 | || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != REAL_TYPE) |
805e22b2 | 5428 | return END_BUILTINS; |
5429 | ||
e9f80ff5 | 5430 | arglist = TREE_CHAIN (arglist); |
5431 | switch (DECL_FUNCTION_CODE (fndecl)) | |
5432 | { | |
5433 | case BUILT_IN_POW: | |
5434 | case BUILT_IN_POWF: | |
5435 | case BUILT_IN_POWL: | |
5436 | case BUILT_IN_ATAN2: | |
5437 | case BUILT_IN_ATAN2F: | |
5438 | case BUILT_IN_ATAN2L: | |
5439 | if (! arglist | |
5440 | || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != REAL_TYPE | |
5441 | || TREE_CHAIN (arglist)) | |
5442 | return END_BUILTINS; | |
5443 | break; | |
5444 | ||
5445 | default: | |
5446 | if (arglist) | |
5447 | return END_BUILTINS; | |
5448 | break; | |
5449 | } | |
5450 | ||
805e22b2 | 5451 | return DECL_FUNCTION_CODE (fndecl); |
5452 | } | |
5453 | ||
650e4c94 | 5454 | /* Fold a call to __builtin_constant_p, if we know it will evaluate to a |
5455 | constant. ARGLIST is the argument list of the call. */ | |
5456 | ||
5457 | static tree | |
aecda0d6 | 5458 | fold_builtin_constant_p (tree arglist) |
650e4c94 | 5459 | { |
5460 | if (arglist == 0) | |
5461 | return 0; | |
5462 | ||
5463 | arglist = TREE_VALUE (arglist); | |
5464 | ||
5465 | /* We return 1 for a numeric type that's known to be a constant | |
5466 | value at compile-time or for an aggregate type that's a | |
5467 | literal constant. */ | |
5468 | STRIP_NOPS (arglist); | |
5469 | ||
5470 | /* If we know this is a constant, emit the constant of one. */ | |
5471 | if (TREE_CODE_CLASS (TREE_CODE (arglist)) == 'c' | |
5472 | || (TREE_CODE (arglist) == CONSTRUCTOR | |
5473 | && TREE_CONSTANT (arglist)) | |
5474 | || (TREE_CODE (arglist) == ADDR_EXPR | |
5475 | && TREE_CODE (TREE_OPERAND (arglist, 0)) == STRING_CST)) | |
5476 | return integer_one_node; | |
5477 | ||
1fb4300c | 5478 | /* If this expression has side effects, show we don't know it to be a |
5479 | constant. Likewise if it's a pointer or aggregate type since in | |
5480 | those case we only want literals, since those are only optimized | |
f97c71a1 | 5481 | when generating RTL, not later. |
5482 | And finally, if we are compiling an initializer, not code, we | |
5483 | need to return a definite result now; there's not going to be any | |
5484 | more optimization done. */ | |
1fb4300c | 5485 | if (TREE_SIDE_EFFECTS (arglist) |
650e4c94 | 5486 | || AGGREGATE_TYPE_P (TREE_TYPE (arglist)) |
f97c71a1 | 5487 | || POINTER_TYPE_P (TREE_TYPE (arglist)) |
5488 | || cfun == 0) | |
650e4c94 | 5489 | return integer_zero_node; |
5490 | ||
5491 | return 0; | |
5492 | } | |
5493 | ||
539a3a92 | 5494 | /* Fold a call to __builtin_classify_type. */ |
27d0c333 | 5495 | |
539a3a92 | 5496 | static tree |
aecda0d6 | 5497 | fold_builtin_classify_type (tree arglist) |
539a3a92 | 5498 | { |
5499 | if (arglist == 0) | |
5500 | return build_int_2 (no_type_class, 0); | |
5501 | ||
5502 | return build_int_2 (type_to_class (TREE_TYPE (TREE_VALUE (arglist))), 0); | |
5503 | } | |
5504 | ||
92c43e3c | 5505 | /* Fold a call to __builtin_inf or __builtin_huge_val. */ |
5506 | ||
5507 | static tree | |
aecda0d6 | 5508 | fold_builtin_inf (tree type, int warn) |
92c43e3c | 5509 | { |
aa870c1b | 5510 | REAL_VALUE_TYPE real; |
5511 | ||
92c43e3c | 5512 | if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn) |
5513 | warning ("target format does not support infinity"); | |
5514 | ||
aa870c1b | 5515 | real_inf (&real); |
5516 | return build_real (type, real); | |
92c43e3c | 5517 | } |
5518 | ||
b0db7939 | 5519 | /* Fold a call to __builtin_nan or __builtin_nans. */ |
5520 | ||
5521 | static tree | |
aecda0d6 | 5522 | fold_builtin_nan (tree arglist, tree type, int quiet) |
b0db7939 | 5523 | { |
5524 | REAL_VALUE_TYPE real; | |
5525 | const char *str; | |
5526 | ||
5527 | if (!validate_arglist (arglist, POINTER_TYPE, VOID_TYPE)) | |
5528 | return 0; | |
5529 | str = c_getstr (TREE_VALUE (arglist)); | |
5530 | if (!str) | |
5531 | return 0; | |
5532 | ||
5533 | if (!real_nan (&real, str, quiet, TYPE_MODE (type))) | |
5534 | return 0; | |
5535 | ||
5536 | return build_real (type, real); | |
5537 | } | |
5538 | ||
277f8dd2 | 5539 | /* Return true if the floating point expression T has an integer value. |
5540 | We also allow +Inf, -Inf and NaN to be considered integer values. */ | |
5541 | ||
5542 | static bool | |
5543 | integer_valued_real_p (tree t) | |
5544 | { | |
5545 | switch (TREE_CODE (t)) | |
5546 | { | |
5547 | case FLOAT_EXPR: | |
5548 | return true; | |
5549 | ||
5550 | case ABS_EXPR: | |
5551 | case SAVE_EXPR: | |
5552 | case NON_LVALUE_EXPR: | |
5553 | return integer_valued_real_p (TREE_OPERAND (t, 0)); | |
5554 | ||
5555 | case COMPOUND_EXPR: | |
5556 | case MODIFY_EXPR: | |
5557 | case BIND_EXPR: | |
5558 | return integer_valued_real_p (TREE_OPERAND (t, 1)); | |
5559 | ||
5560 | case PLUS_EXPR: | |
5561 | case MINUS_EXPR: | |
5562 | case MULT_EXPR: | |
5563 | case MIN_EXPR: | |
5564 | case MAX_EXPR: | |
5565 | return integer_valued_real_p (TREE_OPERAND (t, 0)) | |
5566 | && integer_valued_real_p (TREE_OPERAND (t, 1)); | |
5567 | ||
5568 | case COND_EXPR: | |
5569 | return integer_valued_real_p (TREE_OPERAND (t, 1)) | |
5570 | && integer_valued_real_p (TREE_OPERAND (t, 2)); | |
5571 | ||
5572 | case REAL_CST: | |
5573 | if (! TREE_CONSTANT_OVERFLOW (t)) | |
5574 | { | |
5575 | REAL_VALUE_TYPE c, cint; | |
5576 | ||
5577 | c = TREE_REAL_CST (t); | |
5578 | real_trunc (&cint, TYPE_MODE (TREE_TYPE (t)), &c); | |
5579 | return real_identical (&c, &cint); | |
5580 | } | |
5581 | ||
5582 | case NOP_EXPR: | |
5583 | { | |
5584 | tree type = TREE_TYPE (TREE_OPERAND (t, 0)); | |
5585 | if (TREE_CODE (type) == INTEGER_TYPE) | |
5586 | return true; | |
5587 | if (TREE_CODE (type) == REAL_TYPE) | |
5588 | return integer_valued_real_p (TREE_OPERAND (t, 0)); | |
5589 | break; | |
5590 | } | |
5591 | ||
5592 | case CALL_EXPR: | |
5593 | switch (builtin_mathfn_code (t)) | |
5594 | { | |
5595 | case BUILT_IN_CEIL: | |
5596 | case BUILT_IN_CEILF: | |
5597 | case BUILT_IN_CEILL: | |
5598 | case BUILT_IN_FLOOR: | |
5599 | case BUILT_IN_FLOORF: | |
5600 | case BUILT_IN_FLOORL: | |
5601 | case BUILT_IN_NEARBYINT: | |
5602 | case BUILT_IN_NEARBYINTF: | |
5603 | case BUILT_IN_NEARBYINTL: | |
5604 | case BUILT_IN_ROUND: | |
5605 | case BUILT_IN_ROUNDF: | |
5606 | case BUILT_IN_ROUNDL: | |
5607 | case BUILT_IN_TRUNC: | |
5608 | case BUILT_IN_TRUNCF: | |
5609 | case BUILT_IN_TRUNCL: | |
5610 | return true; | |
5611 | ||
5612 | default: | |
5613 | break; | |
5614 | } | |
5615 | break; | |
5616 | ||
5617 | default: | |
5618 | break; | |
5619 | } | |
5620 | return false; | |
5621 | } | |
5622 | ||
5623 | /* EXP is assumed to be builtin call where truncation can be propagated | |
6528f4f4 | 5624 | across (for instance floor((double)f) == (double)floorf (f). |
5625 | Do the transformation. */ | |
277f8dd2 | 5626 | |
6528f4f4 | 5627 | static tree |
aecda0d6 | 5628 | fold_trunc_transparent_mathfn (tree exp) |
6528f4f4 | 5629 | { |
c6e6ecb1 | 5630 | tree fndecl = get_callee_fndecl (exp); |
6528f4f4 | 5631 | tree arglist = TREE_OPERAND (exp, 1); |
5632 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
277f8dd2 | 5633 | tree arg; |
5634 | ||
5635 | if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
5636 | return 0; | |
6528f4f4 | 5637 | |
277f8dd2 | 5638 | arg = TREE_VALUE (arglist); |
5639 | /* Integer rounding functions are idempotent. */ | |
5640 | if (fcode == builtin_mathfn_code (arg)) | |
5641 | return arg; | |
5642 | ||
5643 | /* If argument is already integer valued, and we don't need to worry | |
5644 | about setting errno, there's no need to perform rounding. */ | |
5645 | if (! flag_errno_math && integer_valued_real_p (arg)) | |
5646 | return arg; | |
5647 | ||
5648 | if (optimize) | |
6528f4f4 | 5649 | { |
277f8dd2 | 5650 | tree arg0 = strip_float_extensions (arg); |
6528f4f4 | 5651 | tree ftype = TREE_TYPE (exp); |
5652 | tree newtype = TREE_TYPE (arg0); | |
5653 | tree decl; | |
5654 | ||
5655 | if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype) | |
5656 | && (decl = mathfn_built_in (newtype, fcode))) | |
5657 | { | |
5658 | arglist = | |
5659 | build_tree_list (NULL_TREE, fold (convert (newtype, arg0))); | |
5660 | return convert (ftype, | |
5661 | build_function_call_expr (decl, arglist)); | |
5662 | } | |
5663 | } | |
5664 | return 0; | |
5665 | } | |
5666 | ||
c63f4ad3 | 5667 | /* Fold function call to builtin cabs, cabsf or cabsl. FNDECL is the |
5668 | function's DECL, ARGLIST is the argument list and TYPE is the return | |
5669 | type. Return NULL_TREE if no simplification can be made. */ | |
5670 | ||
5671 | static tree | |
aecda0d6 | 5672 | fold_builtin_cabs (tree fndecl, tree arglist, tree type) |
c63f4ad3 | 5673 | { |
5674 | tree arg; | |
5675 | ||
5676 | if (!arglist || TREE_CHAIN (arglist)) | |
5677 | return NULL_TREE; | |
5678 | ||
5679 | arg = TREE_VALUE (arglist); | |
5680 | if (TREE_CODE (TREE_TYPE (arg)) != COMPLEX_TYPE | |
5681 | || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE) | |
5682 | return NULL_TREE; | |
5683 | ||
5684 | /* Evaluate cabs of a constant at compile-time. */ | |
5685 | if (flag_unsafe_math_optimizations | |
5686 | && TREE_CODE (arg) == COMPLEX_CST | |
5687 | && TREE_CODE (TREE_REALPART (arg)) == REAL_CST | |
5688 | && TREE_CODE (TREE_IMAGPART (arg)) == REAL_CST | |
5689 | && ! TREE_CONSTANT_OVERFLOW (TREE_REALPART (arg)) | |
5690 | && ! TREE_CONSTANT_OVERFLOW (TREE_IMAGPART (arg))) | |
5691 | { | |
5692 | REAL_VALUE_TYPE r, i; | |
5693 | ||
5694 | r = TREE_REAL_CST (TREE_REALPART (arg)); | |
5695 | i = TREE_REAL_CST (TREE_IMAGPART (arg)); | |
5696 | ||
5697 | real_arithmetic (&r, MULT_EXPR, &r, &r); | |
5698 | real_arithmetic (&i, MULT_EXPR, &i, &i); | |
5699 | real_arithmetic (&r, PLUS_EXPR, &r, &i); | |
5700 | if (real_sqrt (&r, TYPE_MODE (type), &r) | |
5701 | || ! flag_trapping_math) | |
5702 | return build_real (type, r); | |
5703 | } | |
5704 | ||
5705 | /* If either part is zero, cabs is fabs of the other. */ | |
5706 | if (TREE_CODE (arg) == COMPLEX_EXPR | |
5707 | && real_zerop (TREE_OPERAND (arg, 0))) | |
5708 | return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 1))); | |
5709 | if (TREE_CODE (arg) == COMPLEX_EXPR | |
5710 | && real_zerop (TREE_OPERAND (arg, 1))) | |
5711 | return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg, 0))); | |
5712 | ||
5713 | if (flag_unsafe_math_optimizations) | |
5714 | { | |
5715 | enum built_in_function fcode; | |
5716 | tree sqrtfn; | |
5717 | ||
5718 | fcode = DECL_FUNCTION_CODE (fndecl); | |
5719 | if (fcode == BUILT_IN_CABS) | |
5720 | sqrtfn = implicit_built_in_decls[BUILT_IN_SQRT]; | |
5721 | else if (fcode == BUILT_IN_CABSF) | |
5722 | sqrtfn = implicit_built_in_decls[BUILT_IN_SQRTF]; | |
5723 | else if (fcode == BUILT_IN_CABSL) | |
5724 | sqrtfn = implicit_built_in_decls[BUILT_IN_SQRTL]; | |
5725 | else | |
5726 | sqrtfn = NULL_TREE; | |
5727 | ||
5728 | if (sqrtfn != NULL_TREE) | |
5729 | { | |
5730 | tree rpart, ipart, result, arglist; | |
5731 | ||
5732 | rpart = fold (build1 (REALPART_EXPR, type, arg)); | |
5733 | ipart = fold (build1 (IMAGPART_EXPR, type, arg)); | |
5734 | ||
5735 | rpart = save_expr (rpart); | |
5736 | ipart = save_expr (ipart); | |
5737 | ||
5738 | result = fold (build (PLUS_EXPR, type, | |
5739 | fold (build (MULT_EXPR, type, | |
5740 | rpart, rpart)), | |
5741 | fold (build (MULT_EXPR, type, | |
5742 | ipart, ipart)))); | |
5743 | ||
5744 | arglist = build_tree_list (NULL_TREE, result); | |
5745 | return build_function_call_expr (sqrtfn, arglist); | |
5746 | } | |
5747 | } | |
5748 | ||
5749 | return NULL_TREE; | |
5750 | } | |
5751 | ||
277f8dd2 | 5752 | /* Fold function call to builtin trunc, truncf or truncl. Return |
5753 | NULL_TREE if no simplification can be made. */ | |
5754 | ||
5755 | static tree | |
5756 | fold_builtin_trunc (tree exp) | |
5757 | { | |
5758 | tree arglist = TREE_OPERAND (exp, 1); | |
5759 | tree arg; | |
5760 | ||
5761 | if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
5762 | return 0; | |
5763 | ||
5764 | /* Optimize trunc of constant value. */ | |
5765 | arg = TREE_VALUE (arglist); | |
5766 | if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg)) | |
5767 | { | |
5768 | REAL_VALUE_TYPE r, x; | |
5769 | tree type = TREE_TYPE (exp); | |
5770 | ||
5771 | x = TREE_REAL_CST (arg); | |
5772 | real_trunc (&r, TYPE_MODE (type), &x); | |
5773 | return build_real (type, r); | |
5774 | } | |
5775 | ||
5776 | return fold_trunc_transparent_mathfn (exp); | |
5777 | } | |
5778 | ||
5779 | /* Fold function call to builtin floor, floorf or floorl. Return | |
5780 | NULL_TREE if no simplification can be made. */ | |
5781 | ||
5782 | static tree | |
5783 | fold_builtin_floor (tree exp) | |
5784 | { | |
5785 | tree arglist = TREE_OPERAND (exp, 1); | |
5786 | tree arg; | |
5787 | ||
5788 | if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
5789 | return 0; | |
5790 | ||
5791 | /* Optimize floor of constant value. */ | |
5792 | arg = TREE_VALUE (arglist); | |
5793 | if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg)) | |
5794 | { | |
5795 | REAL_VALUE_TYPE x; | |
5796 | ||
5797 | x = TREE_REAL_CST (arg); | |
5798 | if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math) | |
5799 | { | |
5800 | tree type = TREE_TYPE (exp); | |
5801 | REAL_VALUE_TYPE r; | |
5802 | ||
5803 | real_floor (&r, TYPE_MODE (type), &x); | |
5804 | return build_real (type, r); | |
5805 | } | |
5806 | } | |
5807 | ||
5808 | return fold_trunc_transparent_mathfn (exp); | |
5809 | } | |
5810 | ||
5811 | /* Fold function call to builtin ceil, ceilf or ceill. Return | |
5812 | NULL_TREE if no simplification can be made. */ | |
5813 | ||
5814 | static tree | |
5815 | fold_builtin_ceil (tree exp) | |
5816 | { | |
5817 | tree arglist = TREE_OPERAND (exp, 1); | |
5818 | tree arg; | |
5819 | ||
5820 | if (! validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
5821 | return 0; | |
5822 | ||
5823 | /* Optimize ceil of constant value. */ | |
5824 | arg = TREE_VALUE (arglist); | |
5825 | if (TREE_CODE (arg) == REAL_CST && ! TREE_CONSTANT_OVERFLOW (arg)) | |
5826 | { | |
5827 | REAL_VALUE_TYPE x; | |
5828 | ||
5829 | x = TREE_REAL_CST (arg); | |
5830 | if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math) | |
5831 | { | |
5832 | tree type = TREE_TYPE (exp); | |
5833 | REAL_VALUE_TYPE r; | |
5834 | ||
5835 | real_ceil (&r, TYPE_MODE (type), &x); | |
5836 | return build_real (type, r); | |
5837 | } | |
5838 | } | |
5839 | ||
5840 | return fold_trunc_transparent_mathfn (exp); | |
5841 | } | |
5842 | ||
70fb4c07 | 5843 | /* Fold function call to builtin ffs, clz, ctz, popcount and parity |
5844 | and their long and long long variants (i.e. ffsl and ffsll). | |
5845 | Return NULL_TREE if no simplification can be made. */ | |
5846 | ||
5847 | static tree | |
5848 | fold_builtin_bitop (tree exp) | |
5849 | { | |
5850 | tree fndecl = get_callee_fndecl (exp); | |
5851 | tree arglist = TREE_OPERAND (exp, 1); | |
5852 | tree arg; | |
5853 | ||
5854 | if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE)) | |
5855 | return NULL_TREE; | |
5856 | ||
5857 | /* Optimize for constant argument. */ | |
5858 | arg = TREE_VALUE (arglist); | |
5859 | if (TREE_CODE (arg) == INTEGER_CST && ! TREE_CONSTANT_OVERFLOW (arg)) | |
5860 | { | |
5861 | HOST_WIDE_INT hi, width, result; | |
5862 | unsigned HOST_WIDE_INT lo; | |
5863 | tree type, t; | |
5864 | ||
5865 | type = TREE_TYPE (arg); | |
5866 | width = TYPE_PRECISION (type); | |
5867 | lo = TREE_INT_CST_LOW (arg); | |
5868 | ||
5869 | /* Clear all the bits that are beyond the type's precision. */ | |
5870 | if (width > HOST_BITS_PER_WIDE_INT) | |
5871 | { | |
5872 | hi = TREE_INT_CST_HIGH (arg); | |
5873 | if (width < 2 * HOST_BITS_PER_WIDE_INT) | |
5874 | hi &= ~((HOST_WIDE_INT) (-1) >> (width - HOST_BITS_PER_WIDE_INT)); | |
5875 | } | |
5876 | else | |
5877 | { | |
5878 | hi = 0; | |
5879 | if (width < HOST_BITS_PER_WIDE_INT) | |
5880 | lo &= ~((unsigned HOST_WIDE_INT) (-1) << width); | |
5881 | } | |
5882 | ||
5883 | switch (DECL_FUNCTION_CODE (fndecl)) | |
5884 | { | |
5885 | case BUILT_IN_FFS: | |
5886 | case BUILT_IN_FFSL: | |
5887 | case BUILT_IN_FFSLL: | |
5888 | if (lo != 0) | |
5889 | result = exact_log2 (lo & -lo) + 1; | |
5890 | else if (hi != 0) | |
5891 | result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi) + 1; | |
5892 | else | |
5893 | result = 0; | |
5894 | break; | |
5895 | ||
5896 | case BUILT_IN_CLZ: | |
5897 | case BUILT_IN_CLZL: | |
5898 | case BUILT_IN_CLZLL: | |
5899 | if (hi != 0) | |
5900 | result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT; | |
5901 | else if (lo != 0) | |
5902 | result = width - floor_log2 (lo) - 1; | |
5903 | else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result)) | |
5904 | result = width; | |
5905 | break; | |
5906 | ||
5907 | case BUILT_IN_CTZ: | |
5908 | case BUILT_IN_CTZL: | |
5909 | case BUILT_IN_CTZLL: | |
5910 | if (lo != 0) | |
5911 | result = exact_log2 (lo & -lo); | |
5912 | else if (hi != 0) | |
5913 | result = HOST_BITS_PER_WIDE_INT + exact_log2 (hi & -hi); | |
5914 | else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result)) | |
5915 | result = width; | |
5916 | break; | |
5917 | ||
5918 | case BUILT_IN_POPCOUNT: | |
5919 | case BUILT_IN_POPCOUNTL: | |
5920 | case BUILT_IN_POPCOUNTLL: | |
5921 | result = 0; | |
5922 | while (lo) | |
5923 | result++, lo &= lo - 1; | |
5924 | while (hi) | |
5925 | result++, hi &= hi - 1; | |
5926 | break; | |
5927 | ||
5928 | case BUILT_IN_PARITY: | |
5929 | case BUILT_IN_PARITYL: | |
5930 | case BUILT_IN_PARITYLL: | |
5931 | result = 0; | |
5932 | while (lo) | |
5933 | result++, lo &= lo - 1; | |
5934 | while (hi) | |
5935 | result++, hi &= hi - 1; | |
5936 | result &= 1; | |
5937 | break; | |
5938 | ||
5939 | default: | |
5940 | abort(); | |
5941 | } | |
5942 | ||
5943 | t = build_int_2 (result, 0); | |
5944 | TREE_TYPE (t) = TREE_TYPE (exp); | |
5945 | return t; | |
5946 | } | |
5947 | ||
5948 | return NULL_TREE; | |
5949 | } | |
5950 | ||
650e4c94 | 5951 | /* Used by constant folding to eliminate some builtin calls early. EXP is |
5952 | the CALL_EXPR of a call to a builtin function. */ | |
5953 | ||
5954 | tree | |
aecda0d6 | 5955 | fold_builtin (tree exp) |
650e4c94 | 5956 | { |
c6e6ecb1 | 5957 | tree fndecl = get_callee_fndecl (exp); |
650e4c94 | 5958 | tree arglist = TREE_OPERAND (exp, 1); |
e9f80ff5 | 5959 | tree type = TREE_TYPE (TREE_TYPE (fndecl)); |
650e4c94 | 5960 | |
5961 | if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) | |
5962 | return 0; | |
5963 | ||
2643011a | 5964 | switch (DECL_FUNCTION_CODE (fndecl)) |
650e4c94 | 5965 | { |
5966 | case BUILT_IN_CONSTANT_P: | |
5967 | return fold_builtin_constant_p (arglist); | |
5968 | ||
539a3a92 | 5969 | case BUILT_IN_CLASSIFY_TYPE: |
5970 | return fold_builtin_classify_type (arglist); | |
5971 | ||
650e4c94 | 5972 | case BUILT_IN_STRLEN: |
0eb671f7 | 5973 | if (validate_arglist (arglist, POINTER_TYPE, VOID_TYPE)) |
650e4c94 | 5974 | { |
681fab1e | 5975 | tree len = c_strlen (TREE_VALUE (arglist), 0); |
654ef926 | 5976 | if (len) |
5977 | { | |
5978 | /* Convert from the internal "sizetype" type to "size_t". */ | |
5979 | if (size_type_node) | |
5980 | len = convert (size_type_node, len); | |
5981 | return len; | |
5982 | } | |
650e4c94 | 5983 | } |
5984 | break; | |
5985 | ||
c63f4ad3 | 5986 | case BUILT_IN_FABS: |
5987 | case BUILT_IN_FABSF: | |
5988 | case BUILT_IN_FABSL: | |
5989 | if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
5990 | return fold (build1 (ABS_EXPR, type, TREE_VALUE (arglist))); | |
5991 | break; | |
5992 | ||
5993 | case BUILT_IN_CABS: | |
5994 | case BUILT_IN_CABSF: | |
5995 | case BUILT_IN_CABSL: | |
5996 | return fold_builtin_cabs (fndecl, arglist, type); | |
5997 | ||
805e22b2 | 5998 | case BUILT_IN_SQRT: |
5999 | case BUILT_IN_SQRTF: | |
6000 | case BUILT_IN_SQRTL: | |
6001 | if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
6002 | { | |
6003 | enum built_in_function fcode; | |
6004 | tree arg = TREE_VALUE (arglist); | |
6005 | ||
6006 | /* Optimize sqrt of constant value. */ | |
6007 | if (TREE_CODE (arg) == REAL_CST | |
6008 | && ! TREE_CONSTANT_OVERFLOW (arg)) | |
6009 | { | |
805e22b2 | 6010 | REAL_VALUE_TYPE r, x; |
6011 | ||
6012 | x = TREE_REAL_CST (arg); | |
3be04962 | 6013 | if (real_sqrt (&r, TYPE_MODE (type), &x) |
61f1dccc | 6014 | || (!flag_trapping_math && !flag_errno_math)) |
e9f80ff5 | 6015 | return build_real (type, r); |
805e22b2 | 6016 | } |
6017 | ||
2643011a | 6018 | /* Optimize sqrt(exp(x)) = exp(x*0.5). */ |
805e22b2 | 6019 | fcode = builtin_mathfn_code (arg); |
6020 | if (flag_unsafe_math_optimizations | |
6021 | && (fcode == BUILT_IN_EXP | |
6022 | || fcode == BUILT_IN_EXPF | |
6023 | || fcode == BUILT_IN_EXPL)) | |
6024 | { | |
6025 | tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0); | |
2643011a | 6026 | arg = fold (build (MULT_EXPR, type, |
77e89269 | 6027 | TREE_VALUE (TREE_OPERAND (arg, 1)), |
2643011a | 6028 | build_real (type, dconsthalf))); |
805e22b2 | 6029 | arglist = build_tree_list (NULL_TREE, arg); |
6030 | return build_function_call_expr (expfn, arglist); | |
6031 | } | |
2643011a | 6032 | |
6033 | /* Optimize sqrt(pow(x,y)) = pow(x,y*0.5). */ | |
6034 | if (flag_unsafe_math_optimizations | |
6035 | && (fcode == BUILT_IN_POW | |
6036 | || fcode == BUILT_IN_POWF | |
6037 | || fcode == BUILT_IN_POWL)) | |
6038 | { | |
6039 | tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0); | |
6040 | tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1)); | |
6041 | tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1))); | |
6042 | tree narg1 = fold (build (MULT_EXPR, type, arg1, | |
6043 | build_real (type, dconsthalf))); | |
6044 | arglist = tree_cons (NULL_TREE, arg0, | |
6045 | build_tree_list (NULL_TREE, narg1)); | |
6046 | return build_function_call_expr (powfn, arglist); | |
6047 | } | |
805e22b2 | 6048 | } |
6049 | break; | |
6050 | ||
77e89269 | 6051 | case BUILT_IN_SIN: |
6052 | case BUILT_IN_SINF: | |
6053 | case BUILT_IN_SINL: | |
6054 | if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
6055 | { | |
6056 | tree arg = TREE_VALUE (arglist); | |
6057 | ||
6058 | /* Optimize sin(0.0) = 0.0. */ | |
6059 | if (real_zerop (arg)) | |
98b40778 | 6060 | return arg; |
77e89269 | 6061 | } |
6062 | break; | |
6063 | ||
6064 | case BUILT_IN_COS: | |
6065 | case BUILT_IN_COSF: | |
6066 | case BUILT_IN_COSL: | |
6067 | if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
6068 | { | |
6069 | tree arg = TREE_VALUE (arglist); | |
6070 | ||
6071 | /* Optimize cos(0.0) = 1.0. */ | |
6072 | if (real_zerop (arg)) | |
6073 | return build_real (type, dconst1); | |
a4a686a7 | 6074 | |
6075 | /* Optimize cos(-x) into cos(x). */ | |
6076 | if (TREE_CODE (arg) == NEGATE_EXPR) | |
6077 | { | |
6078 | tree arglist = build_tree_list (NULL_TREE, | |
6079 | TREE_OPERAND (arg, 0)); | |
6080 | return build_function_call_expr (fndecl, arglist); | |
6081 | } | |
77e89269 | 6082 | } |
6083 | break; | |
6084 | ||
805e22b2 | 6085 | case BUILT_IN_EXP: |
6086 | case BUILT_IN_EXPF: | |
6087 | case BUILT_IN_EXPL: | |
6088 | if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
6089 | { | |
6090 | enum built_in_function fcode; | |
6091 | tree arg = TREE_VALUE (arglist); | |
6092 | ||
6093 | /* Optimize exp(0.0) = 1.0. */ | |
6094 | if (real_zerop (arg)) | |
e9f80ff5 | 6095 | return build_real (type, dconst1); |
805e22b2 | 6096 | |
98b40778 | 6097 | /* Optimize exp(1.0) = e. */ |
6098 | if (real_onep (arg)) | |
6099 | { | |
6100 | REAL_VALUE_TYPE cst; | |
6101 | ||
6102 | if (! builtin_dconsts_init) | |
6103 | init_builtin_dconsts (); | |
6104 | real_convert (&cst, TYPE_MODE (type), &dconste); | |
6105 | return build_real (type, cst); | |
6106 | } | |
6107 | ||
6108 | /* Attempt to evaluate exp at compile-time. */ | |
6109 | if (flag_unsafe_math_optimizations | |
6110 | && TREE_CODE (arg) == REAL_CST | |
6111 | && ! TREE_CONSTANT_OVERFLOW (arg)) | |
6112 | { | |
6113 | REAL_VALUE_TYPE cint; | |
6114 | REAL_VALUE_TYPE c; | |
6115 | HOST_WIDE_INT n; | |
6116 | ||
6117 | c = TREE_REAL_CST (arg); | |
6118 | n = real_to_integer (&c); | |
6119 | real_from_integer (&cint, VOIDmode, n, | |
6120 | n < 0 ? -1 : 0, 0); | |
6121 | if (real_identical (&c, &cint)) | |
6122 | { | |
6123 | REAL_VALUE_TYPE x; | |
6124 | ||
6125 | if (! builtin_dconsts_init) | |
6126 | init_builtin_dconsts (); | |
6127 | real_powi (&x, TYPE_MODE (type), &dconste, n); | |
6128 | return build_real (type, x); | |
6129 | } | |
6130 | } | |
6131 | ||
805e22b2 | 6132 | /* Optimize exp(log(x)) = x. */ |
6133 | fcode = builtin_mathfn_code (arg); | |
6134 | if (flag_unsafe_math_optimizations | |
6135 | && (fcode == BUILT_IN_LOG | |
6136 | || fcode == BUILT_IN_LOGF | |
6137 | || fcode == BUILT_IN_LOGL)) | |
6138 | return TREE_VALUE (TREE_OPERAND (arg, 1)); | |
6139 | } | |
6140 | break; | |
6141 | ||
6142 | case BUILT_IN_LOG: | |
6143 | case BUILT_IN_LOGF: | |
6144 | case BUILT_IN_LOGL: | |
6145 | if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
6146 | { | |
6147 | enum built_in_function fcode; | |
6148 | tree arg = TREE_VALUE (arglist); | |
6149 | ||
6150 | /* Optimize log(1.0) = 0.0. */ | |
6151 | if (real_onep (arg)) | |
e9f80ff5 | 6152 | return build_real (type, dconst0); |
805e22b2 | 6153 | |
6154 | /* Optimize log(exp(x)) = x. */ | |
6155 | fcode = builtin_mathfn_code (arg); | |
6156 | if (flag_unsafe_math_optimizations | |
6157 | && (fcode == BUILT_IN_EXP | |
6158 | || fcode == BUILT_IN_EXPF | |
6159 | || fcode == BUILT_IN_EXPL)) | |
6160 | return TREE_VALUE (TREE_OPERAND (arg, 1)); | |
6161 | ||
2643011a | 6162 | /* Optimize log(sqrt(x)) = log(x)*0.5. */ |
805e22b2 | 6163 | if (flag_unsafe_math_optimizations |
6164 | && (fcode == BUILT_IN_SQRT | |
6165 | || fcode == BUILT_IN_SQRTF | |
6166 | || fcode == BUILT_IN_SQRTL)) | |
6167 | { | |
6168 | tree logfn = build_function_call_expr (fndecl, | |
6169 | TREE_OPERAND (arg, 1)); | |
2643011a | 6170 | return fold (build (MULT_EXPR, type, logfn, |
6171 | build_real (type, dconsthalf))); | |
6172 | } | |
6173 | ||
6174 | /* Optimize log(pow(x,y)) = y*log(x). */ | |
6175 | if (flag_unsafe_math_optimizations | |
6176 | && (fcode == BUILT_IN_POW | |
6177 | || fcode == BUILT_IN_POWF | |
6178 | || fcode == BUILT_IN_POWL)) | |
6179 | { | |
6180 | tree arg0, arg1, logfn; | |
6181 | ||
6182 | arg0 = TREE_VALUE (TREE_OPERAND (arg, 1)); | |
6183 | arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1))); | |
6184 | arglist = build_tree_list (NULL_TREE, arg0); | |
6185 | logfn = build_function_call_expr (fndecl, arglist); | |
6186 | return fold (build (MULT_EXPR, type, arg1, logfn)); | |
805e22b2 | 6187 | } |
6188 | } | |
6189 | break; | |
6190 | ||
98b40778 | 6191 | case BUILT_IN_TAN: |
6192 | case BUILT_IN_TANF: | |
6193 | case BUILT_IN_TANL: | |
6194 | if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
6195 | { | |
6196 | enum built_in_function fcode; | |
6197 | tree arg = TREE_VALUE (arglist); | |
6198 | ||
6199 | /* Optimize tan(0.0) = 0.0. */ | |
6200 | if (real_zerop (arg)) | |
6201 | return arg; | |
6202 | ||
6203 | /* Optimize tan(atan(x)) = x. */ | |
6204 | fcode = builtin_mathfn_code (arg); | |
6205 | if (flag_unsafe_math_optimizations | |
6206 | && (fcode == BUILT_IN_ATAN | |
6207 | || fcode == BUILT_IN_ATANF | |
6208 | || fcode == BUILT_IN_ATANL)) | |
6209 | return TREE_VALUE (TREE_OPERAND (arg, 1)); | |
6210 | } | |
6211 | break; | |
6212 | ||
6213 | case BUILT_IN_ATAN: | |
6214 | case BUILT_IN_ATANF: | |
6215 | case BUILT_IN_ATANL: | |
6216 | if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE)) | |
6217 | { | |
6218 | tree arg = TREE_VALUE (arglist); | |
6219 | ||
6220 | /* Optimize atan(0.0) = 0.0. */ | |
6221 | if (real_zerop (arg)) | |
6222 | return arg; | |
6223 | ||
6224 | /* Optimize atan(1.0) = pi/4. */ | |
6225 | if (real_onep (arg)) | |
6226 | { | |
6227 | REAL_VALUE_TYPE cst; | |
6228 | ||
6229 | if (! builtin_dconsts_init) | |
6230 | init_builtin_dconsts (); | |
6231 | real_convert (&cst, TYPE_MODE (type), &dconstpi); | |
6232 | cst.exp -= 2; | |
6233 | return build_real (type, cst); | |
6234 | } | |
6235 | } | |
6236 | break; | |
6237 | ||
e9f80ff5 | 6238 | case BUILT_IN_POW: |
6239 | case BUILT_IN_POWF: | |
6240 | case BUILT_IN_POWL: | |
6241 | if (validate_arglist (arglist, REAL_TYPE, REAL_TYPE, VOID_TYPE)) | |
6242 | { | |
2643011a | 6243 | enum built_in_function fcode; |
e9f80ff5 | 6244 | tree arg0 = TREE_VALUE (arglist); |
6245 | tree arg1 = TREE_VALUE (TREE_CHAIN (arglist)); | |
6246 | ||
e9f80ff5 | 6247 | /* Optimize pow(1.0,y) = 1.0. */ |
6248 | if (real_onep (arg0)) | |
6249 | return omit_one_operand (type, build_real (type, dconst1), arg1); | |
77e89269 | 6250 | |
6251 | if (TREE_CODE (arg1) == REAL_CST | |
6252 | && ! TREE_CONSTANT_OVERFLOW (arg1)) | |
6253 | { | |
6254 | REAL_VALUE_TYPE c; | |
6255 | c = TREE_REAL_CST (arg1); | |
6256 | ||
6257 | /* Optimize pow(x,0.0) = 1.0. */ | |
6258 | if (REAL_VALUES_EQUAL (c, dconst0)) | |
6259 | return omit_one_operand (type, build_real (type, dconst1), | |
6260 | arg0); | |
6261 | ||
6262 | /* Optimize pow(x,1.0) = x. */ | |
6263 | if (REAL_VALUES_EQUAL (c, dconst1)) | |
6264 | return arg0; | |
6265 | ||
6266 | /* Optimize pow(x,-1.0) = 1.0/x. */ | |
6267 | if (REAL_VALUES_EQUAL (c, dconstm1)) | |
6268 | return fold (build (RDIV_EXPR, type, | |
6269 | build_real (type, dconst1), | |
6270 | arg0)); | |
6271 | ||
77e89269 | 6272 | /* Optimize pow(x,0.5) = sqrt(x). */ |
6273 | if (flag_unsafe_math_optimizations | |
6274 | && REAL_VALUES_EQUAL (c, dconsthalf)) | |
6275 | { | |
6276 | tree sqrtfn; | |
6277 | ||
2643011a | 6278 | fcode = DECL_FUNCTION_CODE (fndecl); |
77e89269 | 6279 | if (fcode == BUILT_IN_POW) |
6280 | sqrtfn = implicit_built_in_decls[BUILT_IN_SQRT]; | |
6281 | else if (fcode == BUILT_IN_POWF) | |
6282 | sqrtfn = implicit_built_in_decls[BUILT_IN_SQRTF]; | |
6283 | else if (fcode == BUILT_IN_POWL) | |
6284 | sqrtfn = implicit_built_in_decls[BUILT_IN_SQRTL]; | |
6285 | else | |
6286 | sqrtfn = NULL_TREE; | |
6287 | ||
6288 | if (sqrtfn != NULL_TREE) | |
6289 | { | |
6290 | tree arglist = build_tree_list (NULL_TREE, arg0); | |
6291 | return build_function_call_expr (sqrtfn, arglist); | |
6292 | } | |
6293 | } | |
3be04962 | 6294 | |
6295 | /* Attempt to evaluate pow at compile-time. */ | |
6296 | if (TREE_CODE (arg0) == REAL_CST | |
6297 | && ! TREE_CONSTANT_OVERFLOW (arg0)) | |
6298 | { | |
6299 | REAL_VALUE_TYPE cint; | |
6300 | HOST_WIDE_INT n; | |
6301 | ||
98b40778 | 6302 | n = real_to_integer (&c); |
3be04962 | 6303 | real_from_integer (&cint, VOIDmode, n, |
6304 | n < 0 ? -1 : 0, 0); | |
6305 | if (real_identical (&c, &cint)) | |
6306 | { | |
6307 | REAL_VALUE_TYPE x; | |
6308 | bool inexact; | |
6309 | ||
6310 | x = TREE_REAL_CST (arg0); | |
6311 | inexact = real_powi (&x, TYPE_MODE (type), &x, n); | |
6312 | if (flag_unsafe_math_optimizations || !inexact) | |
6313 | return build_real (type, x); | |
6314 | } | |
6315 | } | |
77e89269 | 6316 | } |
2643011a | 6317 | |
6318 | /* Optimize pow(exp(x),y) = exp(x*y). */ | |
6319 | fcode = builtin_mathfn_code (arg0); | |
6320 | if (flag_unsafe_math_optimizations | |
6321 | && (fcode == BUILT_IN_EXP | |
6322 | || fcode == BUILT_IN_EXPF | |
6323 | || fcode == BUILT_IN_EXPL)) | |
6324 | { | |
6325 | tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0); | |
6326 | tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1)); | |
6327 | arg = fold (build (MULT_EXPR, type, arg, arg1)); | |
6328 | arglist = build_tree_list (NULL_TREE, arg); | |
6329 | return build_function_call_expr (expfn, arglist); | |
6330 | } | |
6331 | ||
6332 | /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */ | |
6333 | if (flag_unsafe_math_optimizations | |
6334 | && (fcode == BUILT_IN_SQRT | |
6335 | || fcode == BUILT_IN_SQRTF | |
6336 | || fcode == BUILT_IN_SQRTL)) | |
6337 | { | |
6338 | tree narg0 = TREE_VALUE (TREE_OPERAND (arg0, 1)); | |
6339 | tree narg1 = fold (build (MULT_EXPR, type, arg1, | |
6340 | build_real (type, dconsthalf))); | |
6341 | ||
6342 | arglist = tree_cons (NULL_TREE, narg0, | |
6343 | build_tree_list (NULL_TREE, narg1)); | |
6344 | return build_function_call_expr (fndecl, arglist); | |
6345 | } | |
6346 | ||
6347 | /* Optimize pow(pow(x,y),z) = pow(x,y*z). */ | |
6348 | if (flag_unsafe_math_optimizations | |
6349 | && (fcode == BUILT_IN_POW | |
6350 | || fcode == BUILT_IN_POWF | |
6351 | || fcode == BUILT_IN_POWL)) | |
6352 | { | |
6353 | tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1)); | |
6354 | tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1))); | |
6355 | tree narg1 = fold (build (MULT_EXPR, type, arg01, arg1)); | |
6356 | arglist = tree_cons (NULL_TREE, arg00, | |
6357 | build_tree_list (NULL_TREE, narg1)); | |
6358 | return build_function_call_expr (fndecl, arglist); | |
6359 | } | |
e9f80ff5 | 6360 | } |
6361 | break; | |
6362 | ||
92c43e3c | 6363 | case BUILT_IN_INF: |
6364 | case BUILT_IN_INFF: | |
6365 | case BUILT_IN_INFL: | |
e9f80ff5 | 6366 | return fold_builtin_inf (type, true); |
92c43e3c | 6367 | |
6368 | case BUILT_IN_HUGE_VAL: | |
6369 | case BUILT_IN_HUGE_VALF: | |
6370 | case BUILT_IN_HUGE_VALL: | |
e9f80ff5 | 6371 | return fold_builtin_inf (type, false); |
92c43e3c | 6372 | |
b0db7939 | 6373 | case BUILT_IN_NAN: |
6374 | case BUILT_IN_NANF: | |
6375 | case BUILT_IN_NANL: | |
e9f80ff5 | 6376 | return fold_builtin_nan (arglist, type, true); |
b0db7939 | 6377 | |
6378 | case BUILT_IN_NANS: | |
6379 | case BUILT_IN_NANSF: | |
6380 | case BUILT_IN_NANSL: | |
e9f80ff5 | 6381 | return fold_builtin_nan (arglist, type, false); |
b0db7939 | 6382 | |
6528f4f4 | 6383 | case BUILT_IN_FLOOR: |
6384 | case BUILT_IN_FLOORF: | |
6385 | case BUILT_IN_FLOORL: | |
277f8dd2 | 6386 | return fold_builtin_floor (exp); |
6387 | ||
6528f4f4 | 6388 | case BUILT_IN_CEIL: |
6389 | case BUILT_IN_CEILF: | |
6390 | case BUILT_IN_CEILL: | |
277f8dd2 | 6391 | return fold_builtin_ceil (exp); |
6392 | ||
6528f4f4 | 6393 | case BUILT_IN_TRUNC: |
6394 | case BUILT_IN_TRUNCF: | |
6395 | case BUILT_IN_TRUNCL: | |
277f8dd2 | 6396 | return fold_builtin_trunc (exp); |
6397 | ||
6528f4f4 | 6398 | case BUILT_IN_ROUND: |
6399 | case BUILT_IN_ROUNDF: | |
6400 | case BUILT_IN_ROUNDL: | |
6401 | case BUILT_IN_NEARBYINT: | |
6402 | case BUILT_IN_NEARBYINTF: | |
6403 | case BUILT_IN_NEARBYINTL: | |
6404 | return fold_trunc_transparent_mathfn (exp); | |
6405 | ||
70fb4c07 | 6406 | case BUILT_IN_FFS: |
6407 | case BUILT_IN_FFSL: | |
6408 | case BUILT_IN_FFSLL: | |
6409 | case BUILT_IN_CLZ: | |
6410 | case BUILT_IN_CLZL: | |
6411 | case BUILT_IN_CLZLL: | |
6412 | case BUILT_IN_CTZ: | |
6413 | case BUILT_IN_CTZL: | |
6414 | case BUILT_IN_CTZLL: | |
6415 | case BUILT_IN_POPCOUNT: | |
6416 | case BUILT_IN_POPCOUNTL: | |
6417 | case BUILT_IN_POPCOUNTLL: | |
6418 | case BUILT_IN_PARITY: | |
6419 | case BUILT_IN_PARITYL: | |
6420 | case BUILT_IN_PARITYLL: | |
6421 | return fold_builtin_bitop (exp); | |
6422 | ||
650e4c94 | 6423 | default: |
6424 | break; | |
6425 | } | |
6426 | ||
6427 | return 0; | |
6428 | } | |
7e15618b | 6429 | |
805e22b2 | 6430 | /* Conveniently construct a function call expression. */ |
6431 | ||
6432 | tree | |
aecda0d6 | 6433 | build_function_call_expr (tree fn, tree arglist) |
7e15618b | 6434 | { |
6435 | tree call_expr; | |
6436 | ||
6437 | call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn); | |
6438 | call_expr = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)), | |
6439 | call_expr, arglist); | |
7e15618b | 6440 | return fold (call_expr); |
6441 | } | |
0eb671f7 | 6442 | |
6443 | /* This function validates the types of a function call argument list | |
6444 | represented as a tree chain of parameters against a specified list | |
6445 | of tree_codes. If the last specifier is a 0, that represents an | |
6446 | ellipses, otherwise the last specifier must be a VOID_TYPE. */ | |
27d0c333 | 6447 | |
0eb671f7 | 6448 | static int |
ee582a61 | 6449 | validate_arglist (tree arglist, ...) |
0eb671f7 | 6450 | { |
0eb671f7 | 6451 | enum tree_code code; |
0903457a | 6452 | int res = 0; |
ee582a61 | 6453 | va_list ap; |
aecda0d6 | 6454 | |
ee582a61 | 6455 | va_start (ap, arglist); |
0eb671f7 | 6456 | |
f0ce3b1f | 6457 | do |
0eb671f7 | 6458 | { |
f0ce3b1f | 6459 | code = va_arg (ap, enum tree_code); |
6460 | switch (code) | |
6461 | { | |
6462 | case 0: | |
6463 | /* This signifies an ellipses, any further arguments are all ok. */ | |
6464 | res = 1; | |
6465 | goto end; | |
6466 | case VOID_TYPE: | |
6467 | /* This signifies an endlink, if no arguments remain, return | |
6468 | true, otherwise return false. */ | |
6469 | res = arglist == 0; | |
6470 | goto end; | |
6471 | default: | |
6472 | /* If no parameters remain or the parameter's code does not | |
6473 | match the specified code, return false. Otherwise continue | |
6474 | checking any remaining arguments. */ | |
6475 | if (arglist == 0 | |
6476 | || code != TREE_CODE (TREE_TYPE (TREE_VALUE (arglist)))) | |
6477 | goto end; | |
6478 | break; | |
6479 | } | |
6480 | arglist = TREE_CHAIN (arglist); | |
0eb671f7 | 6481 | } |
f0ce3b1f | 6482 | while (1); |
0903457a | 6483 | |
6484 | /* We need gotos here since we can only have one VA_CLOSE in a | |
6485 | function. */ | |
6486 | end: ; | |
ee582a61 | 6487 | va_end (ap); |
0903457a | 6488 | |
6489 | return res; | |
0eb671f7 | 6490 | } |
fc2a2dcb | 6491 | |
6492 | /* Default version of target-specific builtin setup that does nothing. */ | |
6493 | ||
6494 | void | |
aecda0d6 | 6495 | default_init_builtins (void) |
fc2a2dcb | 6496 | { |
6497 | } | |
6498 | ||
6499 | /* Default target-specific builtin expander that does nothing. */ | |
6500 | ||
6501 | rtx | |
aecda0d6 | 6502 | default_expand_builtin (tree exp ATTRIBUTE_UNUSED, |
6503 | rtx target ATTRIBUTE_UNUSED, | |
6504 | rtx subtarget ATTRIBUTE_UNUSED, | |
6505 | enum machine_mode mode ATTRIBUTE_UNUSED, | |
6506 | int ignore ATTRIBUTE_UNUSED) | |
fc2a2dcb | 6507 | { |
6508 | return NULL_RTX; | |
6509 | } | |
c7926a82 | 6510 | |
6511 | /* Instantiate all remaining CONSTANT_P_RTX nodes. */ | |
6512 | ||
6513 | void | |
aecda0d6 | 6514 | purge_builtin_constant_p (void) |
c7926a82 | 6515 | { |
ea06a334 | 6516 | rtx insn, set, arg, new, note; |
c7926a82 | 6517 | |
ea06a334 | 6518 | for (insn = get_insns (); insn; insn = NEXT_INSN (insn)) |
6519 | if (INSN_P (insn) | |
6520 | && (set = single_set (insn)) != NULL_RTX | |
26d64224 | 6521 | && (GET_CODE (arg = SET_SRC (set)) == CONSTANT_P_RTX |
6522 | || (GET_CODE (arg) == SUBREG | |
6523 | && (GET_CODE (arg = SUBREG_REG (arg)) | |
6524 | == CONSTANT_P_RTX)))) | |
ea06a334 | 6525 | { |
26d64224 | 6526 | arg = XEXP (arg, 0); |
ea06a334 | 6527 | new = CONSTANT_P (arg) ? const1_rtx : const0_rtx; |
6528 | validate_change (insn, &SET_SRC (set), new, 0); | |
c7926a82 | 6529 | |
ea06a334 | 6530 | /* Remove the REG_EQUAL note from the insn. */ |
6531 | if ((note = find_reg_note (insn, REG_EQUAL, NULL_RTX)) != 0) | |
6532 | remove_note (insn, note); | |
6533 | } | |
c7926a82 | 6534 | } |
6535 | ||
01537105 | 6536 | /* Returns true is EXP represents data that would potentially reside |
6537 | in a readonly section. */ | |
6538 | ||
6539 | static bool | |
6540 | readonly_data_expr (tree exp) | |
6541 | { | |
6542 | STRIP_NOPS (exp); | |
6543 | ||
6544 | if (TREE_CODE (exp) == ADDR_EXPR) | |
6545 | return decl_readonly_section (TREE_OPERAND (exp, 0), 0); | |
6546 | else | |
6547 | return false; | |
6548 | } |