diff options
author | Nick Lewycky <nicholas@mxc.ca> | 2008-09-03 05:54:33 +0000 |
---|---|---|
committer | Nick Lewycky <nicholas@mxc.ca> | 2008-09-03 05:54:33 +0000 |
commit | 83c05e5d30366d7c7e3d08e886dee89b1320ae7a (patch) | |
tree | 159bca772cbcb500a1fe373010f02b1181b5e51c | |
parent | b388eb82fb4a95e2f6d54163dfcf962b8032bae8 (diff) |
Don't crash when trying to constant fold a vector with some elements that can't
be folded. Instead, fail to fold the entire vector.
We could also return a vector with some elements folded and some not. If anyone
thinks that's a better approach, please speak up!
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@55689 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/VMCore/ConstantFold.cpp | 18 | ||||
-rw-r--r-- | test/Transforms/InstCombine/2008-09-02-VectorCrash.ll | 27 |
2 files changed, 39 insertions, 6 deletions
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 5167c12d49..25c756ab6e 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -238,9 +238,12 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, std::vector<Constant*> res; const VectorType *DestVecTy = cast<VectorType>(DestTy); const Type *DstEltTy = DestVecTy->getElementType(); - for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i) - res.push_back(ConstantFoldCastInstruction(opc, V->getOperand(i), - DstEltTy)); + for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i) { + Constant *C = ConstantFoldCastInstruction(opc, CV->getOperand(i), + DstEltTy); + if (!C) return 0; // Can't fold operand. + res.push_back(C); + } return ConstantVector::get(DestVecTy, res); } return 0; // Can't fold. @@ -268,9 +271,12 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, std::vector<Constant*> res; const VectorType *DestVecTy = cast<VectorType>(DestTy); const Type *DstEltTy = DestVecTy->getElementType(); - for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i) - res.push_back(ConstantFoldCastInstruction(opc, V->getOperand(i), - DstEltTy)); + for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i) { + Constant *C = ConstantFoldCastInstruction(opc, CV->getOperand(i), + DstEltTy); + if (!C) return 0; // Can't fold operand. + res.push_back(C); + } return ConstantVector::get(DestVecTy, res); } return 0; diff --git a/test/Transforms/InstCombine/2008-09-02-VectorCrash.ll b/test/Transforms/InstCombine/2008-09-02-VectorCrash.ll new file mode 100644 index 0000000000..4b5dc6a5df --- /dev/null +++ b/test/Transforms/InstCombine/2008-09-02-VectorCrash.ll @@ -0,0 +1,27 @@ +; RUN: llvm-as < %s | opt -instcombine + +define void @entry(i32 %m_task_id, i32 %start_x, i32 %end_x, i32 %start_y, i32 %end_y) { + br label %1 + +; <label>:1 ; preds = %4, %0 + %2 = icmp slt i32 0, %end_y ; <i1> [#uses=1] + br i1 %2, label %4, label %3 + +; <label>:3 ; preds = %1 + ret void + +; <label>:4 ; preds = %6, %1 + %5 = icmp slt i32 0, %end_x ; <i1> [#uses=1] + br i1 %5, label %6, label %1 + +; <label>:6 ; preds = %4 + %7 = srem <2 x i32> zeroinitializer, zeroinitializer ; <<2 x i32>> [#uses=1] + %8 = extractelement <2 x i32> %7, i32 1 ; <i32> [#uses=1] + %9 = select i1 false, i32 0, i32 %8 ; <i32> [#uses=1] + %10 = insertelement <2 x i32> zeroinitializer, i32 %9, i32 1 ; <<2 x i32>> [#uses=1] + %11 = extractelement <2 x i32> %10, i32 1 ; <i32> [#uses=1] + %12 = insertelement <4 x i32> zeroinitializer, i32 %11, i32 3 ; <<4 x i32>> [#uses=1] + %13 = sitofp <4 x i32> %12 to <4 x float> ; <<4 x float>> [#uses=1] + store <4 x float> %13, <4 x float>* null + br label %4 +} |