[llvm] r361058 - [InstCombine] move bitcast after insertelement-with-bitcasted-operands

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Fri May 17 11:06:12 PDT 2019


Author: spatel
Date: Fri May 17 11:06:12 2019
New Revision: 361058

URL: http://llvm.org/viewvc/llvm-project?rev=361058&view=rev
Log:
[InstCombine] move bitcast after insertelement-with-bitcasted-operands

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
    llvm/trunk/test/Transforms/InstCombine/insertelement-bitcast.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp?rev=361058&r1=361057&r2=361058&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp Fri May 17 11:06:12 2019
@@ -867,6 +867,20 @@ Instruction *InstCombiner::visitInsertEl
   if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp))
     replaceInstUsesWith(IE, VecOp);
 
+  // If the vector and scalar are both bitcast from the same element type, do
+  // the insert in that source type followed by bitcast.
+  Value *VecSrc, *ScalarSrc;
+  if (match(VecOp, m_BitCast(m_Value(VecSrc))) &&
+      match(ScalarOp, m_BitCast(m_Value(ScalarSrc))) &&
+      (VecOp->hasOneUse() || ScalarOp->hasOneUse()) &&
+      VecSrc->getType()->isVectorTy() && !ScalarSrc->getType()->isVectorTy() &&
+      VecSrc->getType()->getVectorElementType() == ScalarSrc->getType()) {
+    // inselt (bitcast VecSrc), (bitcast ScalarSrc), IdxOp -->
+    //   bitcast (inselt VecSrc, ScalarSrc, IdxOp)
+    Value *NewInsElt = Builder.CreateInsertElement(VecSrc, ScalarSrc, IdxOp);
+    return new BitCastInst(NewInsElt, IE.getType());
+  }
+
   // If the inserted element was extracted from some other vector and both
   // indexes are constant, try to turn this into a shuffle.
   uint64_t InsertedIdx, ExtractedIdx;

Modified: llvm/trunk/test/Transforms/InstCombine/insertelement-bitcast.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/insertelement-bitcast.ll?rev=361058&r1=361057&r2=361058&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/insertelement-bitcast.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/insertelement-bitcast.ll Fri May 17 11:06:12 2019
@@ -6,9 +6,8 @@ declare void @use_v4f32(<4 x float>)
 
 define <4 x float> @bitcast_inselt(i32 %x, <4 x i32> %v) {
 ; CHECK-LABEL: @bitcast_inselt(
-; CHECK-NEXT:    [[XF:%.*]] = bitcast i32 [[X:%.*]] to float
-; CHECK-NEXT:    [[VF:%.*]] = bitcast <4 x i32> [[V:%.*]] to <4 x float>
-; CHECK-NEXT:    [[R:%.*]] = insertelement <4 x float> [[VF]], float [[XF]], i32 0
+; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <4 x i32> [[V:%.*]], i32 [[X:%.*]], i32 0
+; CHECK-NEXT:    [[R:%.*]] = bitcast <4 x i32> [[TMP1]] to <4 x float>
 ; CHECK-NEXT:    ret <4 x float> [[R]]
 ;
   %xf = bitcast i32 %x to float
@@ -21,8 +20,8 @@ define <4 x float> @bitcast_inselt_use1(
 ; CHECK-LABEL: @bitcast_inselt_use1(
 ; CHECK-NEXT:    [[XF:%.*]] = bitcast i32 [[X:%.*]] to float
 ; CHECK-NEXT:    call void @use_f32(float [[XF]])
-; CHECK-NEXT:    [[VF:%.*]] = bitcast <4 x i32> [[V:%.*]] to <4 x float>
-; CHECK-NEXT:    [[R:%.*]] = insertelement <4 x float> [[VF]], float [[XF]], i32 1
+; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <4 x i32> [[V:%.*]], i32 [[X]], i32 1
+; CHECK-NEXT:    [[R:%.*]] = bitcast <4 x i32> [[TMP1]] to <4 x float>
 ; CHECK-NEXT:    ret <4 x float> [[R]]
 ;
   %xf = bitcast i32 %x to float
@@ -34,10 +33,10 @@ define <4 x float> @bitcast_inselt_use1(
 
 define <4 x float> @bitcast_inselt_use2(i32 %x, <4 x i32> %v, i32 %index) {
 ; CHECK-LABEL: @bitcast_inselt_use2(
-; CHECK-NEXT:    [[XF:%.*]] = bitcast i32 [[X:%.*]] to float
 ; CHECK-NEXT:    [[VF:%.*]] = bitcast <4 x i32> [[V:%.*]] to <4 x float>
 ; CHECK-NEXT:    call void @use_v4f32(<4 x float> [[VF]])
-; CHECK-NEXT:    [[R:%.*]] = insertelement <4 x float> [[VF]], float [[XF]], i32 [[INDEX:%.*]]
+; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <4 x i32> [[V]], i32 [[X:%.*]], i32 [[INDEX:%.*]]
+; CHECK-NEXT:    [[R:%.*]] = bitcast <4 x i32> [[TMP1]] to <4 x float>
 ; CHECK-NEXT:    ret <4 x float> [[R]]
 ;
   %xf = bitcast i32 %x to float
@@ -47,6 +46,8 @@ define <4 x float> @bitcast_inselt_use2(
   ret <4 x float> %r
 }
 
+; Negative test - 2 extra uses would require an extra instruction.
+
 define <4 x float> @bitcast_inselt_use3(i32 %x, <4 x i32> %v) {
 ; CHECK-LABEL: @bitcast_inselt_use3(
 ; CHECK-NEXT:    [[XF:%.*]] = bitcast i32 [[X:%.*]] to float
@@ -64,6 +65,8 @@ define <4 x float> @bitcast_inselt_use3(
   ret <4 x float> %r
 }
 
+; Negative test - wrong source type for vector.
+
 define <2 x float> @bitcast_inselt_wrong_bitcast1(i32 %x, i64 %notv) {
 ; CHECK-LABEL: @bitcast_inselt_wrong_bitcast1(
 ; CHECK-NEXT:    [[XF:%.*]] = bitcast i32 [[X:%.*]] to float
@@ -77,6 +80,8 @@ define <2 x float> @bitcast_inselt_wrong
   ret <2 x float> %r
 }
 
+; Negative test - wrong source type for scalar.
+
 define <2 x float> @bitcast_inselt_wrong_bitcast2(<2 x i16> %notscalar, <2 x i32> %v) {
 ; CHECK-LABEL: @bitcast_inselt_wrong_bitcast2(
 ; CHECK-NEXT:    [[XF:%.*]] = bitcast <2 x i16> [[NOTSCALAR:%.*]] to float
@@ -89,4 +94,3 @@ define <2 x float> @bitcast_inselt_wrong
   %r = insertelement <2 x float> %vf, float %xf, i32 0
   ret <2 x float> %r
 }
-




More information about the llvm-commits mailing list