Fix PR1304 and Transforms/InstCombine/2007-04-08-SingleEltVectorCrash.ll
authorChris Lattner <sabre@nondot.org>
Mon, 9 Apr 2007 01:37:55 +0000 (01:37 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 9 Apr 2007 01:37:55 +0000 (01:37 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@35792 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/InstructionCombining.cpp

index 47f649923364d4a68cb2f21f2d34af13dcebe25b..62f2b5afd02a7161c54bfad71c21d79e6d38a053 100644 (file)
@@ -8943,11 +8943,19 @@ Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) {
   // If extracting a specified index from the vector, see if we can recursively
   // find a previously computed scalar that was inserted into the vector.
   if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) {
+    unsigned IndexVal = IdxC->getZExtValue();
+    unsigned VectorWidth = 
+      cast<VectorType>(EI.getOperand(0)->getType())->getNumElements();
+      
+    // If this is extracting an invalid index, turn this into undef, to avoid
+    // crashing the code below.
+    if (IndexVal >= VectorWidth)
+      return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
+    
     // This instruction only demands the single element from the input vector.
     // If the input vector has a single use, simplify it based on this use
     // property.
-    uint64_t IndexVal = IdxC->getZExtValue();
-    if (EI.getOperand(0)->hasOneUse()) {
+    if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) {
       uint64_t UndefElts;
       if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0),
                                                 1 << IndexVal,