This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Fix PR optimize/3508
- From: Roger Sayle <roger at eyesopen dot com>
- To: <gcc-patches at gcc dot gnu dot org>
- Date: Thu, 27 Dec 2001 13:46:03 -0700 (MST)
- Subject: [PATCH] Fix PR optimize/3508
Ok, "Fix" may be a bit strong, and the word "Implement" may be much
more appropriate. Whilst checking GNATS for PRs I could fix, I came
across this one, which is more of a request than a problem report.
This optimizes builtin memcmp for the case where all the arguments
are known constants at compile time. The patch has been tested by
"make bootstrap" and "make -k check" on i686-pc-linux-gnu with no
new regressions. The patch also contains a new test case.
Ok for the mainline to close PR 3508?
2001-12-27 Roger Sayle <roger@eyesopen.com>
* builtins.c (expand_builtin_memcmp): Optimize memcmp built-in
when all arguments are known constant. Fixes PR opt/3508.
* gcc.c-torture/execute/string-opt-16.c: New testcase.
*** gcc/gcc/builtins.c Sun Dec 16 23:12:34 2001
--- patch6/gcc/builtins.c Thu Dec 27 11:14:18 2001
*************** expand_builtin_memcmp (exp, arglist, tar
*** 2252,2257 ****
--- 2252,2258 ----
enum machine_mode mode;
{
tree arg1, arg2, len;
+ const char *p1, *p2;
if (!validate_arglist (arglist,
POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
*************** expand_builtin_memcmp (exp, arglist, tar
*** 2269,2274 ****
--- 2270,2288 ----
expand_expr (arg1, const0_rtx, VOIDmode, EXPAND_NORMAL);
expand_expr (arg2, const0_rtx, VOIDmode, EXPAND_NORMAL);
return const0_rtx;
+ }
+
+ p1 = c_getstr (arg1);
+ p2 = c_getstr (arg2);
+
+ /* If all arguments are constant, and the value of len is not greater
+ than the lengths of arg1 and arg2, evaluate at compile-time. */
+ if (host_integerp (len, 1) && p1 && p2
+ && compare_tree_int (len, strlen (p1)+1) <= 0
+ && compare_tree_int (len, strlen (p2)+1) <= 0)
+ {
+ const int r = memcmp (p1, p2, tree_low_cst (len, 1));
+ return (r < 0 ? constm1_rtx : (r > 0 ? const1_rtx : const0_rtx));
}
/* If len parameter is one, return an expression corresponding to
*** /dev/null Tue May 5 14:32:27 1998
--- string-opt-16.c Thu Dec 27 11:07:26 2001
***************
*** 0 ****
--- 1,45 ----
+ /* Copyright (C) 2001 Free Software Foundation.
+
+ Ensure that builtin memcmp operations when all three arguments
+ are constant is optimized and performs correctly. Taken from
+ PR optimize/3508.
+
+ Written by Roger Sayle, 12/26/2001. */
+
+ extern void abort (void);
+ extern void link_error (void);
+
+ typedef __SIZE_TYPE__ size_t;
+ extern int memcmp (const void *, const void *, size_t);
+
+ int
+ main (int argc)
+ {
+ if (memcmp ("abcd", "efgh", 4) >= 0)
+ link_error ();
+ if (memcmp ("abcd", "abcd", 4) != 0)
+ link_error ();
+ if (memcmp ("efgh", "abcd", 4) <= 0)
+ link_error ();
+ return 0;
+ }
+
+ #ifdef __OPTIMIZE__
+ /* When optimizing, all the above cases should be transformed into
+ something else. So any remaining calls to the original function
+ should abort. */
+ static int
+ memcmp (const void *s1, const void *s2, size_t len)
+ {
+ abort ();
+ }
+ #else
+ /* When not optimizing, the above tests may generate references to
+ the function link_error, but should never actually call it. */
+ static void
+ link_error ()
+ {
+ abort ();
+ }
+ #endif
+
Roger
--
Roger Sayle, E-mail: roger@eyesopen.com
OpenEye Scientific Software, WWW: http://www.eyesopen.com/
Suite 1107, 3600 Cerrillos Road, Tel: (+1) 505-473-7385
Santa Fe, New Mexico, 87507. Fax: (+1) 505-438-3470