[InstCombine] Fold strncmp of constant arrays and variable size
authorMartin Sebor <msebor@redhat.com>
Tue, 28 Jun 2022 21:54:56 +0000 (15:54 -0600)
committerMartin Sebor <msebor@redhat.com>
Tue, 28 Jun 2022 21:59:14 +0000 (15:59 -0600)
commit8827679826b82753fcf6e8a827a127c384f1a93e
tree8276d4357c7e14e88c862be1d9149dc6f1822202
parente263a7670e28d880ec45971f91fa88de01fc51e1
[InstCombine] Fold strncmp of constant arrays and variable size

Extend the solution accepted in D127766 to strncmp and simplify
strncmp(A, B, N) calls with constant A and B and variable N to
the equivalent of

  N <= Pos ? 0 : (A < B ? -1 : B < A ? +1 : 0)

where Pos is the offset of either the first mismatch between A
and B or the terminating null character if both A and B are equal
strings.

Reviewed By: courbet

Differential Revision: https://reviews.llvm.org/D128089
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
llvm/test/Transforms/InstCombine/memcmp-4.ll
llvm/test/Transforms/InstCombine/strncmp-5.ll [new file with mode: 0644]
llvm/test/Transforms/InstCombine/strncmp-6.ll [new file with mode: 0644]