Skip to content

Commit

Permalink
[InstCombine] Drop nsw in negation of select (llvm#112893)
Browse files Browse the repository at this point in the history
Closes llvm#112666 and
llvm#114181.

(cherry picked from commit ff07df6)
  • Loading branch information
dtcxzyw authored and nikic committed Nov 20, 2024
1 parent 1cd384f commit ed05e1e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
11 changes: 11 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,17 @@ std::array<Value *, 2> Negator::getSortedOperandsOfBinOp(Instruction *I) {
NewSelect->swapValues();
// Don't swap prof metadata, we didn't change the branch behavior.
NewSelect->setName(I->getName() + ".neg");
// Poison-generating flags should be dropped
Value *TV = NewSelect->getTrueValue();
Value *FV = NewSelect->getFalseValue();
if (match(TV, m_Neg(m_Specific(FV))))
cast<Instruction>(TV)->dropPoisonGeneratingFlags();
else if (match(FV, m_Neg(m_Specific(TV))))
cast<Instruction>(FV)->dropPoisonGeneratingFlags();
else {
cast<Instruction>(TV)->dropPoisonGeneratingFlags();
cast<Instruction>(FV)->dropPoisonGeneratingFlags();
}
Builder.Insert(NewSelect);
return NewSelect;
}
Expand Down
42 changes: 42 additions & 0 deletions llvm/test/Transforms/InstCombine/sub-of-negatible.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,48 @@ define i8 @negate_select_of_op_vs_negated_op(i8 %x, i8 %y, i1 %c) {
%t2 = sub i8 %y, %t1
ret i8 %t2
}

define i8 @negate_select_of_op_vs_negated_op_nsw(i8 %x, i8 %y, i1 %c) {
; CHECK-LABEL: @negate_select_of_op_vs_negated_op_nsw(
; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[X:%.*]]
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C:%.*]], i8 [[X]], i8 [[T0]]
; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP1]], [[Y:%.*]]
; CHECK-NEXT: ret i8 [[T2]]
;
%t0 = sub nsw i8 0, %x
%t1 = select i1 %c, i8 %t0, i8 %x
%t2 = sub i8 %y, %t1
ret i8 %t2
}

define i8 @negate_select_of_op_vs_negated_op_nsw_commuted(i8 %x, i8 %y, i1 %c) {
; CHECK-LABEL: @negate_select_of_op_vs_negated_op_nsw_commuted(
; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[X:%.*]]
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C:%.*]], i8 [[T0]], i8 [[X]]
; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP1]], [[Y:%.*]]
; CHECK-NEXT: ret i8 [[T2]]
;
%t0 = sub nsw i8 0, %x
%t1 = select i1 %c, i8 %x, i8 %t0
%t2 = sub i8 %y, %t1
ret i8 %t2
}

define i8 @negate_select_of_op_vs_negated_op_nsw_xyyx(i8 %x, i8 %y, i8 %z, i1 %c) {
; CHECK-LABEL: @negate_select_of_op_vs_negated_op_nsw_xyyx(
; CHECK-NEXT: [[SUB1:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[SUB2:%.*]] = sub i8 [[Y]], [[X]]
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C:%.*]], i8 [[SUB2]], i8 [[SUB1]]
; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP1]], [[Z:%.*]]
; CHECK-NEXT: ret i8 [[T2]]
;
%sub1 = sub nsw i8 %x, %y
%sub2 = sub nsw i8 %y, %x
%t1 = select i1 %c, i8 %sub1, i8 %sub2
%t2 = sub i8 %z, %t1
ret i8 %t2
}

define i8 @dont_negate_ordinary_select(i8 %x, i8 %y, i8 %z, i1 %c) {
; CHECK-LABEL: @dont_negate_ordinary_select(
; CHECK-NEXT: [[T0:%.*]] = select i1 [[C:%.*]], i8 [[X:%.*]], i8 [[Y:%.*]]
Expand Down

0 comments on commit ed05e1e

Please sign in to comment.