Commit 62ce7e65 authored by Sanjay Patel's avatar Sanjay Patel
Browse files

[InstCombine] fix use check when canonicalizing abs/nabs

We were checking for extra uses of the negated operand even
if we were not going to create it as part of this canonicalization.

This was showing up as a regression when we limit EarlyCSE as
proposed in D74285.
parent 93073e52
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -1073,8 +1073,9 @@ static Instruction *canonicalizeAbsNabs(SelectInst &Sel, ICmpInst &Cmp,
  if (CmpCanonicalized && RHSCanonicalized)
    return nullptr;

  // If RHS is used by other instructions except compare and select, don't
  // canonicalize it to not increase the instruction count.
  // If RHS is not canonical but is used by other instructions, don't
  // canonicalize it and potentially increase the instruction count.
  if (!RHSCanonicalized)
    if (!(RHS->hasOneUse() || (RHS->hasNUses(2) && CmpUsesNegatedOp)))
      return nullptr;

+8 −8
Original line number Diff line number Diff line
@@ -573,8 +573,8 @@ define i8 @abs_swapped(i8 %a) {
; CHECK-LABEL: @abs_swapped(
; CHECK-NEXT:    [[NEG:%.*]] = sub i8 0, [[A:%.*]]
; CHECK-NEXT:    call void @extra_use(i8 [[NEG]])
; CHECK-NEXT:    [[CMP1:%.*]] = icmp sgt i8 [[A]], 0
; CHECK-NEXT:    [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[NEG]]
; CHECK-NEXT:    [[CMP1:%.*]] = icmp slt i8 [[A]], 0
; CHECK-NEXT:    [[M1:%.*]] = select i1 [[CMP1]], i8 [[NEG]], i8 [[A]]
; CHECK-NEXT:    ret i8 [[M1]]
;
  %neg = sub i8 0, %a
@@ -588,8 +588,8 @@ define i8 @nabs_swapped(i8 %a) {
; CHECK-LABEL: @nabs_swapped(
; CHECK-NEXT:    [[NEG:%.*]] = sub i8 0, [[A:%.*]]
; CHECK-NEXT:    call void @extra_use(i8 [[NEG]])
; CHECK-NEXT:    [[CMP2:%.*]] = icmp sgt i8 [[A]], 0
; CHECK-NEXT:    [[M2:%.*]] = select i1 [[CMP2]], i8 [[NEG]], i8 [[A]]
; CHECK-NEXT:    [[CMP2:%.*]] = icmp slt i8 [[A]], 0
; CHECK-NEXT:    [[M2:%.*]] = select i1 [[CMP2]], i8 [[A]], i8 [[NEG]]
; CHECK-NEXT:    ret i8 [[M2]]
;
  %neg = sub i8 0, %a
@@ -603,8 +603,8 @@ define i8 @abs_different_constants(i8 %a) {
; CHECK-LABEL: @abs_different_constants(
; CHECK-NEXT:    [[NEG:%.*]] = sub i8 0, [[A:%.*]]
; CHECK-NEXT:    call void @extra_use(i8 [[NEG]])
; CHECK-NEXT:    [[CMP1:%.*]] = icmp sgt i8 [[A]], -1
; CHECK-NEXT:    [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[NEG]]
; CHECK-NEXT:    [[CMP1:%.*]] = icmp slt i8 [[A]], 0
; CHECK-NEXT:    [[M1:%.*]] = select i1 [[CMP1]], i8 [[NEG]], i8 [[A]]
; CHECK-NEXT:    ret i8 [[M1]]
;
  %neg = sub i8 0, %a
@@ -618,8 +618,8 @@ define i8 @nabs_different_constants(i8 %a) {
; CHECK-LABEL: @nabs_different_constants(
; CHECK-NEXT:    [[NEG:%.*]] = sub i8 0, [[A:%.*]]
; CHECK-NEXT:    call void @extra_use(i8 [[NEG]])
; CHECK-NEXT:    [[CMP2:%.*]] = icmp sgt i8 [[A]], -1
; CHECK-NEXT:    [[M2:%.*]] = select i1 [[CMP2]], i8 [[NEG]], i8 [[A]]
; CHECK-NEXT:    [[CMP2:%.*]] = icmp slt i8 [[A]], 0
; CHECK-NEXT:    [[M2:%.*]] = select i1 [[CMP2]], i8 [[A]], i8 [[NEG]]
; CHECK-NEXT:    ret i8 [[M2]]
;
  %neg = sub i8 0, %a