[llvm] r183080 - When determining the new index for an insertelement, we may not assume that an

Nick Lewycky nicholas at mxc.ca
Sat Jun 1 13:51:31 PDT 2013


Author: nicholas
Date: Sat Jun  1 15:51:31 2013
New Revision: 183080

URL: http://llvm.org/viewvc/llvm-project?rev=183080&view=rev
Log:
When determining the new index for an insertelement, we may not assume that an
index greater than the size of the vector is invalid. The shuffle may be
shrinking the size of the vector. Fixes a crash!

Also drop the maximum recursion depth of the safety check for this
optimization to five.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
    llvm/trunk/test/Transforms/InstCombine/vec_shuffle.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp?rev=183080&r1=183079&r2=183080&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp Sat Jun  1 15:51:31 2013
@@ -497,7 +497,7 @@ Instruction *InstCombiner::visitInsertEl
 /// Return true if we can evaluate the specified expression tree if the vector
 /// elements were shuffled in a different order.
 static bool CanEvaluateShuffled(Value *V, ArrayRef<int> Mask,
-                                unsigned Depth = 100) {
+                                unsigned Depth = 5) {
   // We can always reorder the elements of a constant.
   if (isa<Constant>(V))
     return true;
@@ -718,19 +718,21 @@ InstCombiner::EvaluateInDifferentElement
     }
     case Instruction::InsertElement: {
       int Element = cast<ConstantInt>(I->getOperand(2))->getLimitedValue();
-      if (Element < 0 || Element >= (int)Mask.size()) {
-        // Such instructions are valid and exhibit undefined behaviour.
-        return UndefValue::get(I->getType());
-      }
 
       // The insertelement was inserting at Element. Figure out which element
       // that becomes after shuffling. The answer is guaranteed to be unique
       // by CanEvaluateShuffled.
+      bool Found = false;
       int Index = 0;
-      for (int e = Mask.size(); Index != e; ++Index)
-        if (Mask[Index] == Element)
+      for (int e = Mask.size(); Index != e; ++Index) {
+        if (Mask[Index] == Element) {
+          Found = true;
           break;
+        }
+      }
 
+      if (!Found)
+        return UndefValue::get(I->getType());
       Value *V = EvaluateInDifferentElementOrder(I->getOperand(0), Mask);
       return InsertElementInst::Create(V, I->getOperand(1),
                                        Builder->getInt32(Index), "", I);

Modified: llvm/trunk/test/Transforms/InstCombine/vec_shuffle.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/vec_shuffle.ll?rev=183080&r1=183079&r2=183080&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/vec_shuffle.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/vec_shuffle.ll Sat Jun  1 15:51:31 2013
@@ -174,3 +174,14 @@ define <2 x i8> @test13b(i8 %x) {
   %B = shufflevector <2 x i8> %A, <2 x i8> undef, <2 x i32> <i32 undef, i32 0>
   ret <2 x i8> %B
 }
+
+define <2 x i8> @test13c(i8 %x1, i8 %x2) {
+; CHECK: @test13c
+; CHECK-NEXT: insertelement <2 x i8> {{.*}}, i32 0
+; CHECK-NEXT: insertelement <2 x i8> {{.*}}, i32 1
+; CHECK-NEXT: ret
+  %A = insertelement <4 x i8> undef, i8 %x1, i32 0
+  %B = insertelement <4 x i8> %A, i8 %x2, i32 2
+  %C = shufflevector <4 x i8> %B, <4 x i8> undef, <2 x i32> <i32 0, i32 2>
+  ret <2 x i8> %C
+}





More information about the llvm-commits mailing list