Skip to content

Commit ed05e1e

Browse files
dtcxzywnikic
authored andcommitted
[InstCombine] Drop nsw in negation of select (llvm#112893)
Closes llvm#112666 and llvm#114181. (cherry picked from commit ff07df6)
1 parent 1cd384f commit ed05e1e

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
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

+42
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,48 @@ define i8 @negate_select_of_op_vs_negated_op(i8 %x, i8 %y, i1 %c) {
13741374
%t2 = sub i8 %y, %t1
13751375
ret i8 %t2
13761376
}
1377+
1378+
define i8 @negate_select_of_op_vs_negated_op_nsw(i8 %x, i8 %y, i1 %c) {
1379+
; CHECK-LABEL: @negate_select_of_op_vs_negated_op_nsw(
1380+
; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[X:%.*]]
1381+
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C:%.*]], i8 [[X]], i8 [[T0]]
1382+
; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP1]], [[Y:%.*]]
1383+
; CHECK-NEXT: ret i8 [[T2]]
1384+
;
1385+
%t0 = sub nsw i8 0, %x
1386+
%t1 = select i1 %c, i8 %t0, i8 %x
1387+
%t2 = sub i8 %y, %t1
1388+
ret i8 %t2
1389+
}
1390+
1391+
define i8 @negate_select_of_op_vs_negated_op_nsw_commuted(i8 %x, i8 %y, i1 %c) {
1392+
; CHECK-LABEL: @negate_select_of_op_vs_negated_op_nsw_commuted(
1393+
; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[X:%.*]]
1394+
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C:%.*]], i8 [[T0]], i8 [[X]]
1395+
; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP1]], [[Y:%.*]]
1396+
; CHECK-NEXT: ret i8 [[T2]]
1397+
;
1398+
%t0 = sub nsw i8 0, %x
1399+
%t1 = select i1 %c, i8 %x, i8 %t0
1400+
%t2 = sub i8 %y, %t1
1401+
ret i8 %t2
1402+
}
1403+
1404+
define i8 @negate_select_of_op_vs_negated_op_nsw_xyyx(i8 %x, i8 %y, i8 %z, i1 %c) {
1405+
; CHECK-LABEL: @negate_select_of_op_vs_negated_op_nsw_xyyx(
1406+
; CHECK-NEXT: [[SUB1:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
1407+
; CHECK-NEXT: [[SUB2:%.*]] = sub i8 [[Y]], [[X]]
1408+
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C:%.*]], i8 [[SUB2]], i8 [[SUB1]]
1409+
; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP1]], [[Z:%.*]]
1410+
; CHECK-NEXT: ret i8 [[T2]]
1411+
;
1412+
%sub1 = sub nsw i8 %x, %y
1413+
%sub2 = sub nsw i8 %y, %x
1414+
%t1 = select i1 %c, i8 %sub1, i8 %sub2
1415+
%t2 = sub i8 %z, %t1
1416+
ret i8 %t2
1417+
}
1418+
13771419
define i8 @dont_negate_ordinary_select(i8 %x, i8 %y, i8 %z, i1 %c) {
13781420
; CHECK-LABEL: @dont_negate_ordinary_select(
13791421
; CHECK-NEXT: [[T0:%.*]] = select i1 [[C:%.*]], i8 [[X:%.*]], i8 [[Y:%.*]]

0 commit comments

Comments
 (0)