Skip to content

Commit f6decfa

Browse files
committed
[InstCombine] Negator: freeze is freely negatible if it's operand is negatible
1 parent b899d13 commit f6decfa

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,13 @@ LLVM_NODISCARD Value *Negator::visitImpl(Value *V, unsigned Depth) {
244244
}
245245

246246
switch (I->getOpcode()) {
247+
case Instruction::Freeze: {
248+
// `freeze` is negatible if its operand is negatible.
249+
Value *NegOp = negate(I->getOperand(0), Depth + 1);
250+
if (!NegOp) // Early return.
251+
return nullptr;
252+
return Builder.CreateFreeze(NegOp, I->getName() + ".neg");
253+
}
247254
case Instruction::PHI: {
248255
// `phi` is negatible if all the incoming values are negatible.
249256
auto *PHI = cast<PHINode>(I);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,9 +1214,9 @@ define i8 @dont_negate_ordinary_select(i8 %x, i8 %y, i8 %z, i1 %c) {
12141214
; Freeze is transparent as far as negation is concerned
12151215
define i4 @negate_freeze(i4 %x, i4 %y, i4 %z) {
12161216
; CHECK-LABEL: @negate_freeze(
1217-
; CHECK-NEXT: [[T0:%.*]] = sub i4 [[X:%.*]], [[Y:%.*]]
1218-
; CHECK-NEXT: [[T1:%.*]] = freeze i4 [[T0]]
1219-
; CHECK-NEXT: [[T2:%.*]] = sub i4 [[Z:%.*]], [[T1]]
1217+
; CHECK-NEXT: [[T0_NEG:%.*]] = sub i4 [[Y:%.*]], [[X:%.*]]
1218+
; CHECK-NEXT: [[T1_NEG:%.*]] = freeze i4 [[T0_NEG]]
1219+
; CHECK-NEXT: [[T2:%.*]] = add i4 [[T1_NEG]], [[Z:%.*]]
12201220
; CHECK-NEXT: ret i4 [[T2]]
12211221
;
12221222
%t0 = sub i4 %x, %y

0 commit comments

Comments
 (0)