[llvm-bugs] [Bug 47997] New: InstCombine misses C0-(C1-X) optimization

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Oct 28 05:56:01 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=47997

            Bug ID: 47997
           Summary: InstCombine misses C0-(C1-X) optimization
           Product: libraries
           Version: 11.0
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: bruno-llvm at defraine.net
                CC: llvm-bugs at lists.llvm.org

We are seeing a performance regression in LLVM-11. The optimizer leaves
redundant C0-(C1-X) instructions, where previously this was combined to
X+(C0-C1).

Reduced C++ case:

extern int x;
extern int y;

bool test(int a) {
    x = a - 1;
    y = 1 - a;
    return x == -y;
}

clang-10 output with -O2:

define dso_local zeroext i1 @_Z4testi(i32 %0) local_unnamed_addr #0 {
  %2 = add nsw i32 %0, -1
  store i32 %2, i32* @x, align 4, !tbaa !2
  %3 = sub nsw i32 1, %0
  store i32 %3, i32* @y, align 4, !tbaa !2
  ret i1 true
}

clang-11 output with -O2:

define dso_local zeroext i1 @_Z4testi(i32 %0) local_unnamed_addr #0 {
  %2 = add nsw i32 %0, -1
  store i32 %2, i32* @x, align 4, !tbaa !2
  %3 = sub nsw i32 1, %0
  store i32 %3, i32* @y, align 4, !tbaa !2
  %4 = sub nsw i32 0, %3
  %5 = icmp eq i32 %2, %4
  ret i1 %5
}

Godbolt link: https://godbolt.org/z/Y94rnE

In the LLVM-11 outut, %4 should be optimized to (add i32 %0, -1) such that %5
can be optimized to true.

Analysis:

This regression was introduced by https://reviews.llvm.org/D68408

The issue seems to be in InstCombiner::visitSub; an expression C0-(C1-X)
matches the IsNegation path, but is not handled by Negator::Negate.

Because this path ends with a return:

  if (IsNegation)
    return TryToNarrowDeduceFlags(); // Should have been handled in Negator!

Further optimizations from InstCombiner::visitSub are not attempted.

Either this should be fixed in Negator::Negate, or the code should leave
opportunity for other optimizations by doing:

  if (IsNegation)
    if (auto *I = TryToNarrowDeduceFlags())
       return I; // Should have been handled in Negator!

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20201028/0e127a5e/attachment.html>


More information about the llvm-bugs mailing list