Skip to content

Commit 9aa2913

Browse files
committed
[InstCombine] Drop nsw in negation of select
(cherry-picked from 8d86a53)
1 parent d2d67d1 commit 9aa2913

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,17 @@ std::array<Value *, 2> Negator::getSortedOperandsOfBinOp(Instruction *I) {
334334
NewSelect->swapValues();
335335
// Don't swap prof metadata, we didn't change the branch behavior.
336336
NewSelect->setName(I->getName() + ".neg");
337+
// Poison-generating flags should be dropped
338+
Value *TV = NewSelect->getTrueValue();
339+
Value *FV = NewSelect->getFalseValue();
340+
if (match(TV, m_Neg(m_Specific(FV))))
341+
cast<Instruction>(TV)->dropPoisonGeneratingFlags();
342+
else if (match(FV, m_Neg(m_Specific(TV))))
343+
cast<Instruction>(FV)->dropPoisonGeneratingFlags();
344+
else {
345+
cast<Instruction>(TV)->dropPoisonGeneratingFlags();
346+
cast<Instruction>(FV)->dropPoisonGeneratingFlags();
347+
}
337348
Builder.Insert(NewSelect);
338349
return NewSelect;
339350
}

llvm/test/Transforms/InstCombine/sub-of-negatible.ll

+4-4
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,7 @@ define i8 @negate_select_of_op_vs_negated_op(i8 %x, i8 %y, i1 %c) {
13771377

13781378
define i8 @negate_select_of_op_vs_negated_op_nsw(i8 %x, i8 %y, i1 %c) {
13791379
; CHECK-LABEL: @negate_select_of_op_vs_negated_op_nsw(
1380-
; CHECK-NEXT: [[T0:%.*]] = sub nsw i8 0, [[X:%.*]]
1380+
; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[X:%.*]]
13811381
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C:%.*]], i8 [[X]], i8 [[T0]]
13821382
; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP1]], [[Y:%.*]]
13831383
; CHECK-NEXT: ret i8 [[T2]]
@@ -1390,7 +1390,7 @@ define i8 @negate_select_of_op_vs_negated_op_nsw(i8 %x, i8 %y, i1 %c) {
13901390

13911391
define i8 @negate_select_of_op_vs_negated_op_nsw_commuted(i8 %x, i8 %y, i1 %c) {
13921392
; CHECK-LABEL: @negate_select_of_op_vs_negated_op_nsw_commuted(
1393-
; CHECK-NEXT: [[T0:%.*]] = sub nsw i8 0, [[X:%.*]]
1393+
; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[X:%.*]]
13941394
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C:%.*]], i8 [[T0]], i8 [[X]]
13951395
; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP1]], [[Y:%.*]]
13961396
; CHECK-NEXT: ret i8 [[T2]]
@@ -1403,8 +1403,8 @@ define i8 @negate_select_of_op_vs_negated_op_nsw_commuted(i8 %x, i8 %y, i1 %c) {
14031403

14041404
define i8 @negate_select_of_op_vs_negated_op_nsw_xyyx(i8 %x, i8 %y, i8 %z, i1 %c) {
14051405
; CHECK-LABEL: @negate_select_of_op_vs_negated_op_nsw_xyyx(
1406-
; CHECK-NEXT: [[SUB1:%.*]] = sub nsw i8 [[X:%.*]], [[Y:%.*]]
1407-
; CHECK-NEXT: [[SUB2:%.*]] = sub nsw i8 [[Y]], [[X]]
1406+
; CHECK-NEXT: [[SUB1:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
1407+
; CHECK-NEXT: [[SUB2:%.*]] = sub i8 [[Y]], [[X]]
14081408
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C:%.*]], i8 [[SUB2]], i8 [[SUB1]]
14091409
; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP1]], [[Z:%.*]]
14101410
; CHECK-NEXT: ret i8 [[T2]]

0 commit comments

Comments
 (0)