[all-commits] [llvm/llvm-project] 160559: [SimplifyLibCalls] Fix memchr opt to use CreateLog...

Juneyoung Lee via All-commits all-commits at lists.llvm.org
Fri Jun 25 13:59:53 PDT 2021


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 160559344026824ee0b510741c7906c0e165f9a7
      https://github.com/llvm/llvm-project/commit/160559344026824ee0b510741c7906c0e165f9a7
  Author: Juneyoung Lee <aqjune at gmail.com>
  Date:   2021-06-26 (Sat, 26 Jun 2021)

  Changed paths:
    M llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
    M llvm/test/Transforms/InstCombine/memchr.ll
    M llvm/test/Transforms/InstCombine/strchr-1.ll

  Log Message:
  -----------
  [SimplifyLibCalls] Fix memchr opt to use CreateLogicalAnd

This fixes a bug at LibCallSimplifier::optimizeMemChr which does the following transformation:

```
// memchr("\r\n", C, 2) != nullptr -> (1 << C & ((1 << '\r') | (1 << '\n')))
// != 0
//   after bounds check.
```

As written above, a bounds check on C (whether it is less than integer bitwidth) is done before doing `1 << C` otherwise 1 << C will overflow.
If the bounds check is false, the result of (1 << C & ...) must not be used at all, otherwise the result of shift (which is poison) will contaminate the whole results.
A correct way to encode this is `select i1 (bounds check), (1 << C & ...), false`  because select does not allow the unused operand to contaminate the result.
However, this optimization was introducing `and (bounds check), (1 << C & ...)` which cannot do that.

The bug was found from compilation of this C++ code: https://reviews.llvm.org/rG2fd3037ac615#1007197

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D104901




More information about the All-commits mailing list