[PATCH] D128364: [InstCombine] Look through more casts when folding memchr and memcmp

Martin Sebor via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 22 11:00:24 PDT 2022


msebor created this revision.
msebor added reviewers: lattner, nikic, xbolva00.
Herald added subscribers: jsji, pengfei, hiraditya.
Herald added a project: All.
msebor requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The `memchr` and `memcmp` folders fail for the results of pointer addition involving fractional offsets into constants of types other than `i8`, such as in

  const int a[] = { 0x01010101, 0x02020202 };
  
  void* f (void)
  {
    return memchr((const char*)a + 1, 1, 7);
  }

and similar examples involving structs and unions.  This is due to what seems like an overly restrictive check in the `getConstantDataArrayInfo` that keeps the function from "looking through [all] bitcast instructions and geps" despite the comment documenting this intent.

In conjunction with the recent enhancement to let all libcall folders work with subobjects of constants of arbitrary types (D125114 <https://reviews.llvm.org/D125114>), this change removes the limitation above, bringing the `memchr` and `memcmp` folders up to par with GCC.  Tested on by running `make check-all` on x86_64-linux.

(The code in `getConstantDataArrayInfo` could stand to be simplified by letting the function take a `DataLayout` argument.  I stopped short of making that change in this patch to minimize the extent of the changes.)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128364

Files:
  llvm/lib/Analysis/ValueTracking.cpp
  llvm/test/Transforms/InstCombine/memchr-9.ll
  llvm/test/Transforms/InstCombine/memcmp-7.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128364.439083.patch
Type: text/x-patch
Size: 21175 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220622/99cbd1e1/attachment.bin>


More information about the llvm-commits mailing list