[llvm] ebed96f - [InstCombine] Negator: `extractelement` is negatible if src is negatible
Roman Lebedev via llvm-commits
llvm-commits at lists.llvm.org
Wed May 20 11:48:08 PDT 2020
Author: Roman Lebedev
Date: 2020-05-20T21:44:31+03:00
New Revision: ebed96fdbf26c081ecbff990fc7f5ae35e9ad2c7
URL: https://github.com/llvm/llvm-project/commit/ebed96fdbf26c081ecbff990fc7f5ae35e9ad2c7
DIFF: https://github.com/llvm/llvm-project/commit/ebed96fdbf26c081ecbff990fc7f5ae35e9ad2c7.diff
LOG: [InstCombine] Negator: `extractelement` is negatible if src is negatible
----------------------------------------
define i4 @negate_extractelement(<2 x i4> %x, i32 %y, i4 %z) {
%0:
%t0 = sub <2 x i4> { 0, 0 }, %x
call void @use_v2i4(<2 x i4> %t0)
%t1 = extractelement <2 x i4> %t0, i32 %y
%t2 = sub i4 %z, %t1
ret i4 %t2
}
=>
define i4 @negate_extractelement(<2 x i4> %x, i32 %y, i4 %z) {
%0:
%t0 = sub <2 x i4> { 0, 0 }, %x
call void @use_v2i4(<2 x i4> %t0)
%t1.neg = extractelement <2 x i4> %x, i32 %y
%t2 = add i4 %t1.neg, %z
ret i4 %t2
}
Transformation seems to be correct!
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
llvm/test/Transforms/InstCombine/sub-of-negatible.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
index c393a6373f7a..1d26edb5f728 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
@@ -295,6 +295,15 @@ LLVM_NODISCARD Value *Negator::visit(Value *V, unsigned Depth) {
return Builder.CreateShuffleVector(NegOp0, NegOp1, Shuf->getShuffleMask(),
I->getName() + ".neg");
}
+ case Instruction::ExtractElement: {
+ // `extractelement` is negatible if source operand is negatible.
+ auto *EEI = cast<ExtractElementInst>(I);
+ Value *NegVector = visit(EEI->getVectorOperand(), Depth + 1);
+ if (!NegVector) // Early return.
+ return nullptr;
+ return Builder.CreateExtractElement(NegVector, EEI->getIndexOperand(),
+ I->getName() + ".neg");
+ }
case Instruction::Trunc: {
// `trunc` is negatible if its operand is negatible.
Value *NegOp = visit(I->getOperand(0), Depth + 1);
diff --git a/llvm/test/Transforms/InstCombine/sub-of-negatible.ll b/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
index 95269460b344..d18e49be8581 100644
--- a/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
+++ b/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
@@ -921,8 +921,8 @@ define i4 @negate_extractelement(<2 x i4> %x, i32 %y, i4 %z) {
; CHECK-LABEL: @negate_extractelement(
; CHECK-NEXT: [[T0:%.*]] = sub <2 x i4> zeroinitializer, [[X:%.*]]
; CHECK-NEXT: call void @use_v2i4(<2 x i4> [[T0]])
-; CHECK-NEXT: [[T1:%.*]] = extractelement <2 x i4> [[T0]], i32 [[Y:%.*]]
-; CHECK-NEXT: [[T2:%.*]] = sub i4 [[Z:%.*]], [[T1]]
+; CHECK-NEXT: [[T1_NEG:%.*]] = extractelement <2 x i4> [[X]], i32 [[Y:%.*]]
+; CHECK-NEXT: [[T2:%.*]] = add i4 [[T1_NEG]], [[Z:%.*]]
; CHECK-NEXT: ret i4 [[T2]]
;
%t0 = sub <2 x i4> zeroinitializer, %x
More information about the llvm-commits
mailing list