[llvm] 62ce7e6 - [InstCombine] fix use check when canonicalizing abs/nabs
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 10 11:59:03 PST 2020
Author: Sanjay Patel
Date: 2020-02-10T14:57:37-05:00
New Revision: 62ce7e650a5a388540b5606d34bcce2fb5932498
URL: https://github.com/llvm/llvm-project/commit/62ce7e650a5a388540b5606d34bcce2fb5932498
DIFF: https://github.com/llvm/llvm-project/commit/62ce7e650a5a388540b5606d34bcce2fb5932498.diff
LOG: [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.
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
llvm/test/Transforms/InstCombine/abs-1.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index fe47ceec4131..c086b152c09b 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -1073,10 +1073,11 @@ 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->hasOneUse() || (RHS->hasNUses(2) && CmpUsesNegatedOp)))
- return nullptr;
+ // 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;
// Create the canonical compare: icmp slt LHS 0.
if (!CmpCanonicalized) {
diff --git a/llvm/test/Transforms/InstCombine/abs-1.ll b/llvm/test/Transforms/InstCombine/abs-1.ll
index f0d1fea25570..1ff8ee61cb19 100644
--- a/llvm/test/Transforms/InstCombine/abs-1.ll
+++ b/llvm/test/Transforms/InstCombine/abs-1.ll
@@ -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_
diff erent_constants(i8 %a) {
; CHECK-LABEL: @abs_
diff erent_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_
diff erent_constants(i8 %a) {
; CHECK-LABEL: @nabs_
diff erent_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
More information about the llvm-commits
mailing list