Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

builtin memcpy behaviour compared to gcc when destinatio​n unknown... #13018

Open Quuxplusone opened 12 years ago

Quuxplusone commented 12 years ago
Bugzilla Link PR12909
Status NEW
Importance P enhancement
Reported by viswabramana.rajesh@gmail.com
Reported on 2012-05-22 03:57:38 -0700
Last modified on 2012-05-24 09:45:56 -0700
Version 3.0
Hardware PC Windows XP
CC geek4civic@gmail.com, llvm-bugs@lists.llvm.org
Fixed by commit(s)
Attachments libcalls.c (643 bytes, text/plain)
Blocks
Blocked by
See also
Created attachment 8615
library calls file

__builtin_memcpy_chk function optimization O123 behaviour in clang.

I have noticed in clang for builtin __memcpy_chk function when destination
buffer unknown than, builtin calls optimized to direct memcpy.
Where as if destination has known pointer at compilation time than it will call
builtin mem_chk function which in turn calls memcpy.

Test code:

typedef __SIZE_TYPE__ size_t;
extern void abort (void);
extern void *memcpy (void *, const void *, size_t);
#define memcpy(dst, src, len) \
  __builtin___memcpy_chk (dst, src, len, __builtin_object_size (dst, 0))

int l1 = 1;
int chk_calls;
int main ()
{
    char *s3 = "FGH";
    char *r;
    char buf1[10], buf2[10];
    chk_calls = 0;
    memcpy(buf1, s3, l1); // Destination is known(buf1), length is not known calls memcpy_chk
    r = l1 == 1 ? buf1 : buf2;
    memcpy(r, s3, l1 + 1); // Destination not known, clang optimizing call to memcpy
    if (chk_calls != 2)
        abort ();
}
Above test code along with attached libcalls.c,when run in gcc with
optimizaiton O123 test code passes.
Same set of code when run in Clang(O123) its aborted, since second builtin
memcpy call optimized to direct memcpy function and chk_calls not incremented
to 2

Optimizing builtin memcpy function when destination buffer is unknown, Is it
Accepted behaviour?

-viswa
Quuxplusone commented 12 years ago

Attached libcalls.c (643 bytes, text/plain): library calls file