[SimplifyLibCalls] Don't confuse strcpy_chk for stpcpy_chk.
authorAhmed Bougacha <ahmed.bougacha@gmail.com>
Tue, 27 Jan 2015 21:52:16 +0000 (21:52 +0000)
committerAhmed Bougacha <ahmed.bougacha@gmail.com>
Tue, 27 Jan 2015 21:52:16 +0000 (21:52 +0000)
commit37be0d7c434bc5f44ec01f6f1ad2152721b66fbc
treedf005ee4077766d512d8ceb6039ab27421737f4b
parentfdefc694cd20958bbeb10bd66767fda371452c7c
[SimplifyLibCalls] Don't confuse strcpy_chk for stpcpy_chk.

This was introduced in a faulty refactoring (r225640, mea culpa):
the tests weren't testing the return values, so, for both
__strcpy_chk and __stpcpy_chk, we would return the end of the
buffer (matching stpcpy) instead of the beginning (for strcpy).

The root cause was the prefix "__" being ignored when comparing,
which made us always pick LibFunc::stpcpy_chk.
Pass the LibFunc::Func directly to avoid this kind of error.
Also, make the testcases as explicit as possible to prevent this.

The now-useful testcases expose another, entangled, stpcpy problem,
with the further simplification.  This was introduced in a
refactoring (r225640) to match the original behavior.

However, this leads to problems when successive simplifications
generate several similar instructions, none of which are removed
by the custom replaceAllUsesWith.

For instance, InstCombine (the main user) doesn't erase the
instruction in its custom RAUW.  When trying to simplify say
__stpcpy_chk:
- first, an stpcpy is created (fortified simplifier),
- second, a memcpy is created (normal simplifier), but the
  stpcpy call isn't removed.
- third, InstCombine later revisits the instructions,
  and simplifies the first stpcpy to a memcpy.  We now have
  two memcpys.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@227250 91177308-0d34-0410-b5e6-96231b3b80d8
include/llvm/Transforms/Utils/SimplifyLibCalls.h
lib/Transforms/Utils/SimplifyLibCalls.cpp
test/Transforms/InstCombine/memcpy_chk-1.ll
test/Transforms/InstCombine/memmove_chk-1.ll
test/Transforms/InstCombine/memset_chk-1.ll
test/Transforms/InstCombine/stpcpy_chk-1.ll
test/Transforms/InstCombine/strcpy_chk-1.ll
test/Transforms/InstCombine/strncpy_chk-1.ll