[llvm] 55430f5 - [InstCombine] `insertelement` is negatible if both sources are negatible

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Wed May 20 11:48:12 PDT 2020


Author: Roman Lebedev
Date: 2020-05-20T21:44:31+03:00
New Revision: 55430f53f397e942b91d8379b5dbae5d2c852986

URL: https://github.com/llvm/llvm-project/commit/55430f53f397e942b91d8379b5dbae5d2c852986
DIFF: https://github.com/llvm/llvm-project/commit/55430f53f397e942b91d8379b5dbae5d2c852986.diff

LOG: [InstCombine] `insertelement` is negatible if both sources are negatible

----------------------------------------
define <2 x i4> @negate_insertelement(<2 x i4> %src, i4 %a, i32 %x, <2 x i4> %b) {
%0:
  %t0 = sub <2 x i4> { 0, 0 }, %src
  %t1 = sub i4 0, %a
  %t2 = insertelement <2 x i4> %t0, i4 %t1, i32 %x
  %t3 = sub <2 x i4> %b, %t2
  ret <2 x i4> %t3
}
=>
define <2 x i4> @negate_insertelement(<2 x i4> %src, i4 %a, i32 %x, <2 x i4> %b) {
%0:
  %t2.neg = insertelement <2 x i4> %src, i4 %a, i32 %x
  %t3 = add <2 x i4> %t2.neg, %b
  ret <2 x i4> %t3
}
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 1d26edb5f728..6194396c49e6 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
@@ -304,6 +304,19 @@ LLVM_NODISCARD Value *Negator::visit(Value *V, unsigned Depth) {
     return Builder.CreateExtractElement(NegVector, EEI->getIndexOperand(),
                                         I->getName() + ".neg");
   }
+  case Instruction::InsertElement: {
+    // `insertelement` is negatible if both the source vector and
+    // element-to-be-inserted are negatible.
+    auto *IEI = cast<InsertElementInst>(I);
+    Value *NegVector = visit(IEI->getOperand(0), Depth + 1);
+    if (!NegVector) // Early return.
+      return nullptr;
+    Value *NegNewElt = visit(IEI->getOperand(1), Depth + 1);
+    if (!NegNewElt) // Early return.
+      return nullptr;
+    return Builder.CreateInsertElement(NegVector, NegNewElt, IEI->getOperand(2),
+                                       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 7ff52c7f9e73..79d1e584fe19 100644
--- a/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
+++ b/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
@@ -951,10 +951,8 @@ define i4 @negate_extractelement_extrause(<2 x i4> %x, i32 %y, i4 %z) {
 ; `insertelement` is negatible if both source vector and element-to-be-inserted are negatible.
 define <2 x i4> @negate_insertelement(<2 x i4> %src, i4 %a, i32 %x, <2 x i4> %b) {
 ; CHECK-LABEL: @negate_insertelement(
-; CHECK-NEXT:    [[T0:%.*]] = sub <2 x i4> zeroinitializer, [[SRC:%.*]]
-; CHECK-NEXT:    [[T1:%.*]] = sub i4 0, [[A:%.*]]
-; CHECK-NEXT:    [[T2:%.*]] = insertelement <2 x i4> [[T0]], i4 [[T1]], i32 [[X:%.*]]
-; CHECK-NEXT:    [[T3:%.*]] = sub <2 x i4> [[B:%.*]], [[T2]]
+; CHECK-NEXT:    [[T2_NEG:%.*]] = insertelement <2 x i4> [[SRC:%.*]], i4 [[A:%.*]], i32 [[X:%.*]]
+; CHECK-NEXT:    [[T3:%.*]] = add <2 x i4> [[T2_NEG]], [[B:%.*]]
 ; CHECK-NEXT:    ret <2 x i4> [[T3]]
 ;
   %t0 = sub <2 x i4> zeroinitializer, %src


        


More information about the llvm-commits mailing list