[PATCH] Fix a bug in Instruction Combine when optimizing shuffle vector and insert element

Hao Liu Hao.Liu at arm.com
Fri Jan 3 00:10:10 PST 2014


Hi t.p.northover,

Hi

This patch fix a bug about incorrectly generating undef instruction for combining shuffle vector.
For the following test case:
define <1 x i32> @test16a(i32 %ele) {
  %tmp0 = insertelement <2 x i32> <i32 1, i32 undef>, i32 %ele, i32 1
  %tmp1 = shl <2 x i32> %tmp0, <i32 1, i32 1>
  %tmp2 = shufflevector <2 x i32> %tmp1, <2 x i32> undef, <1 x i32> <i32 0>
  ret <1 x i32> %tmp2
}

Currently it will optimize shufflevector to shl <1 x i32 >undef, <i32 1>, and the final result will be ret <1 x i32> zero.

This is obviously wrong as it should return <1 x i32> 2.

This is caused by incorrectly return an undef value when it finds the inserted element in an insert element instruction is not in the mask of shuffle vector. But it should keep on finding all the values in mask in the operand 0 of insert element instruction.

Thanks,
-Hao

http://llvm-reviews.chandlerc.com/D2502

Files:
  lib/Transforms/InstCombine/InstCombineVectorOps.cpp
  test/Transforms/InstCombine/vec_shuffle.ll

Index: lib/Transforms/InstCombine/InstCombineVectorOps.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineVectorOps.cpp
+++ lib/Transforms/InstCombine/InstCombineVectorOps.cpp
@@ -763,9 +763,10 @@
         }
       }
 
+      // If element is not in Mask, no need to handle the operand 1 (element to
+      // be inserted). Just evaluate values in operand 0 according to Mask.
       if (!Found)
-        return UndefValue::get(
-            VectorType::get(V->getType()->getScalarType(), Mask.size()));
+        return EvaluateInDifferentElementOrder(I->getOperand(0), Mask);
 
       Value *V = EvaluateInDifferentElementOrder(I->getOperand(0), Mask);
       return InsertElementInst::Create(V, I->getOperand(1),
Index: test/Transforms/InstCombine/vec_shuffle.ll
===================================================================
--- test/Transforms/InstCombine/vec_shuffle.ll
+++ test/Transforms/InstCombine/vec_shuffle.ll
@@ -228,3 +228,20 @@
   ret <4 x float> %tmp5
 }
 
+define <1 x i32> @test16a(i32 %ele) {
+; CHECK-LABEL: @test16a(
+; CHECK-NEXT: ret <1 x i32> <i32 2>
+  %tmp0 = insertelement <2 x i32> <i32 1, i32 undef>, i32 %ele, i32 1
+  %tmp1 = shl <2 x i32> %tmp0, <i32 1, i32 1>
+  %tmp2 = shufflevector <2 x i32> %tmp1, <2 x i32> undef, <1 x i32> <i32 0>
+  ret <1 x i32> %tmp2
+}
+
+define <4 x i8> @test16b(i8 %ele) {
+; CHECK-LABEL: @test16b(
+; CHECK-NEXT: ret <4 x i8> <i8 2, i8 2, i8 2, i8 2>
+  %tmp0 = insertelement <8 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 undef, i8 1>, i8 %ele, i32 6
+  %tmp1 = shl <8 x i8> %tmp0, <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
+  %tmp2 = shufflevector <8 x i8> %tmp1, <8 x i8> undef, <4 x i32> <i32 1, i32 2, i32 3, i32 4>
+  ret <4 x i8> %tmp2
+}
\ No newline at end of file
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2502.1.patch
Type: text/x-patch
Size: 1814 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140103/fd834556/attachment.bin>


More information about the llvm-commits mailing list