= get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
rtx dest_mem, src_mem, len_rtx;
- /* If DEST is not a pointer type or LEN is not constant,
- call the normal function. */
- if (dest_align == 0 || !host_integerp (len, 1))
+ /* If DEST is not a pointer type, call the normal function. */
+ if (dest_align == 0)
return 0;
+ /* If SRC and DEST are the same (and not volatile), do nothing. */
+ if (operand_equal_p (src, dest, 0))
+ {
+ tree expr;
+
+ if (endp == 0)
+ {
+ /* Evaluate and ignore LEN in case it has side-effects. */
+ expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
+ return expand_expr (dest, target, mode, EXPAND_NORMAL);
+ }
+
+ if (endp == 2)
+ len = fold (build (MINUS_EXPR, TREE_TYPE (len), dest,
+ integer_one_node));
+ len = convert (TREE_TYPE (dest), len);
+ expr = fold (build (PLUS_EXPR, TREE_TYPE (dest), dest, len));
+ return expand_expr (expr, target, mode, EXPAND_NORMAL);
+ }
+
+ /* If LEN is not constant, call the normal function. */
+ if (! host_integerp (len, 1))
+ return 0;
+
/* If the LEN parameter is zero, return DEST. */
if (tree_low_cst (len, 1) == 0)
{
return expand_expr (dest, target, mode, EXPAND_NORMAL);
}
+ /* If SRC and DEST are the same (and not volatile), return DEST. */
+ if (operand_equal_p (src, dest, 0))
+ {
+ /* Evaluate and ignore LEN in case it has side-effects. */
+ expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
+ return expand_expr (dest, target, mode, EXPAND_NORMAL);
+ }
+
/* If either SRC is not a pointer type, don't do this
operation in-line. */
if (src_align == 0)
return expand_expr (dest, target, mode, EXPAND_NORMAL);
}
+ /* If SRC and DEST are the same (and not volatile), return DEST. */
+ if (operand_equal_p (src, dest, 0))
+ {
+ /* Evaluate and ignore LEN in case it has side-effects. */
+ expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
+ return expand_expr (dest, target, mode, EXPAND_NORMAL);
+ }
+
/* If either SRC is not a pointer type, don't do this
operation in-line. */
if (src_align == 0)
if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
return 0;
+ src = TREE_VALUE (TREE_CHAIN (arglist));
+ dst = TREE_VALUE (arglist);
+
+ /* If SRC and DST are equal (and not volatile), return DST. */
+ if (operand_equal_p (src, dst, 0))
+ return expand_expr (dst, target, mode, EXPAND_NORMAL);
+
fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
if (!fn)
return 0;
- src = TREE_VALUE (TREE_CHAIN (arglist));
len = c_strlen (src, 1);
if (len == 0 || TREE_SIDE_EFFECTS (len))
return 0;
- dst = TREE_VALUE (arglist);
len = size_binop (PLUS_EXPR, len, ssize_int (1));
arglist = build_tree_list (NULL_TREE, len);
arglist = tree_cons (NULL_TREE, src, arglist);
return const0_rtx;
}
+ /* If both arguments are equal (and not volatile), return zero. */
+ if (operand_equal_p (arg1, arg2, 0))
+ {
+ /* Evaluate and ignore len in case it has side-effects. */
+ expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
+ return const0_rtx;
+ }
+
p1 = c_getstr (arg1);
p2 = c_getstr (arg2);
arg1 = TREE_VALUE (arglist);
arg2 = TREE_VALUE (TREE_CHAIN (arglist));
+ /* If both arguments are equal (and not volatile), return zero. */
+ if (operand_equal_p (arg1, arg2, 0))
+ return const0_rtx;
+
p1 = c_getstr (arg1);
p2 = c_getstr (arg2);
return const0_rtx;
}
+ /* If arg1 and arg2 are equal (and not volatile), return zero. */
+ if (operand_equal_p (arg1, arg2, 0))
+ {
+ /* Evaluate and ignore arg3 in case it has side-effects. */
+ expand_expr (arg3, const0_rtx, VOIDmode, EXPAND_NORMAL);
+ return const0_rtx;
+ }
+
p1 = c_getstr (arg1);
p2 = c_getstr (arg2);
--- /dev/null
+/* Copyright (C) 2003 Free Software Foundation.
+
+ Test equal pointer optimizations don't break anything.
+
+ Written by Roger Sayle, July 14, 2003. */
+
+extern void abort ();
+typedef __SIZE_TYPE__ size_t;
+
+extern void *memcpy(void*, const void*, size_t);
+extern void *mempcpy(void*, const void*, size_t);
+extern void *memmove(void*, const void*, size_t);
+extern char *strcpy(char*, const char*);
+extern int memcmp(const void*, const void*, size_t);
+extern int strcmp(const char*, const char*);
+extern int strncmp(const char*, const char*, size_t);
+
+
+void test1 (void *ptr)
+{
+ if (memcpy(ptr,ptr,8) != ptr)
+ abort ();
+}
+
+void test2 (char *ptr)
+{
+ if (mempcpy(ptr,ptr,8) != ptr+8)
+ abort ();
+}
+
+void test3 (void *ptr)
+{
+ if (memmove(ptr,ptr,8) != ptr)
+ abort ();
+}
+
+void test4 (char *ptr)
+{
+ if (strcpy(ptr,ptr) != ptr)
+ abort ();
+}
+
+void test5 (void *ptr)
+{
+ if (memcmp(ptr,ptr,8) != 0)
+ abort ();
+}
+
+void test6 (const char *ptr)
+{
+ if (strcmp(ptr,ptr) != 0)
+ abort ();
+}
+
+void test7 (const char *ptr)
+{
+ if (strncmp(ptr,ptr,8) != 0)
+ abort ();
+}
+
+
+int main ()
+{
+ char buf[10];
+
+ test1 (buf);
+ test2 (buf);
+ test3 (buf);
+ test4 (buf);
+ test5 (buf);
+ test6 (buf);
+ test7 (buf);
+
+ return 0;
+}
+