[llvm-bugs] [Bug 44899] New: InstCombine rule for ICmp+Add causes extra instructions

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Feb 13 11:58:21 PST 2020


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

            Bug ID: 44899
           Summary: InstCombine rule for ICmp+Add causes extra
                    instructions
           Product: tools
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: opt
          Assignee: unassignedbugs at nondot.org
          Reporter: anna at azul.com
                CC: lebedev.ri at gmail.com, llvm-bugs at lists.llvm.org,
                    spatel+llvm at rotateright.com, t.p.northover at gmail.com

Recently, there was a change (0f22e783a038b6983f0fe161eef6cf2add3a4156) landed
in foldICmpAddConstant which was a revert of another change (checking for
multiple uses of Add much earlier).

With this change we are having regressions on an internal benchmark.

I've narrowed it down to this IR snippet:
define i1 @test(i64 %0) {  
  %c1 = ashr i64 %0, 1
  %c2 = add i64 %c1, 31083597720000
  %c3 = icmp eq i64 %c2, 0
  %c4 = icmp slt i64 %c2, 0
  %c6 = select i1 %c4, i32 -1, i32 1
  %result = select i1 %c3, i32 0, i32 %c6
  %c5 = icmp sge i32 %result, 0
  br i1 %c5, label %bci_28, label %unreached

bci_28:                                           ; preds = %bci_0
  ret i1 true

unreached:
  ret i1 false
}

Running with instCombine produces:
define i1 @test(i64 %0) {
  %c1.mask = and i64 %0, -2
  %c3 = icmp eq i64 %c1.mask, -62167195440000
  %c4 = icmp sgt i64 %0, -62167195440001
  %c5 = or i1 %c3, %c4
  br i1 %c5, label %bci_28, label %unreached

bci_28:                                           ; preds = %1
  ret i1 true

unreached:                                        ; preds = %1
  ret i1 false
}


We can clearly see from the original snippet, the whole sequence can be
narrowed to:
define i1 @test(i64 %0) {
  %2 = icmp sgt i64 %0, -62167195440001
  br i1 %2, label %bci_28, label %unreached

bci_28:                                           ; preds = %1
  ret i1 true

unreached:                                        ; preds = %1
  ret i1 false
}

This is the case if I run instCombine with the change
0f22e783a038b6983f0fe161eef6cf2add3a4156 reverted.

I noticed that this was done to fix PR44100. We will need a stronger check for
Adds that have more than one use. This is a proper case of more instructions
generated even if we break the dep chain between add and compare.

-- 
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/20200213/2a7f0a53/attachment.html>


More information about the llvm-bugs mailing list