Bug 21602

Summary: builtin memmove could be memcpy if src and dst don't alias
Product: gcc Reporter: Kaveh Ghazi <ghazi>
Component: middle-endAssignee: Richard Biener <rguenth>
Status: RESOLVED FIXED    
Severity: enhancement CC: gcc-bugs, rguenth, sjames
Priority: P2 Keywords: alias, missed-optimization
Version: 4.1.0   
Target Milestone: ---   
See Also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112653
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2010-05-09 16:54:18

Description Kaveh Ghazi 2005-05-16 02:08:56 UTC
The current builtin memmove optimizes to memcpy if it can prove that the source 
pointer is in readonly memory, under the assumption that the destination 
pointer must be writable and therefore couldn't overlap.  However we could 
generalize this such that if we can prove the source and dest don't alias, then 
we can do the transformation.  E.g. given the following code, we should 
optimize the memmove call in both foo() and bar(), however we only do foo() in 
mainline.

typedef __SIZE_TYPE__ size_t;
extern void *malloc (size_t);
extern void *memmove (void *, const void *, size_t);

void *foo (void)
{
  void *dst = malloc (13);
  return memmove (dst, "hello world\n", 13);
}

void *bar (void *src)
{
  void *dst = malloc(13);
  return memmove (dst, src, 13);
}
Comment 1 Andrew Pinski 2005-05-16 02:19:01 UTC
Confirmed.
Comment 2 Richard Biener 2010-05-09 16:54:18 UTC
Mine.
Comment 3 Richard Biener 2010-08-30 13:46:47 UTC
Fixed.
Comment 4 Richard Biener 2010-08-30 13:46:57 UTC
Subject: Bug 21602

Author: rguenth
Date: Mon Aug 30 13:46:31 2010
New Revision: 163646

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=163646
Log:
2010-08-30  Richard Guenther  <rguenther@suse.de>

	PR tree-optimization/21602
	* builtins.c (fold_builtin_memory_op): Fold memmove to memcpy
	using points-to information.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/builtins.c