From d2f27ead2d71afeee869cad8ae8a1c1dce7229cb Mon Sep 17 00:00:00 2001 From: Nadav Rotem Date: Fri, 11 Feb 2011 19:37:55 +0000 Subject: Fix 9173. Add more folding patterns to constant expressions of vector selects and vector bitcasts. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@125393 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/ConstantFold.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'lib/VMCore/ConstantFold.cpp') diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index f2129c08d2..0ec343f07d 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -42,6 +42,10 @@ using namespace llvm; /// input vector constant are all simple integer or FP values. static Constant *BitCastConstantVector(ConstantVector *CV, const VectorType *DstTy) { + + if (CV->isAllOnesValue()) return Constant::getAllOnesValue(DstTy); + if (CV->isNullValue()) return Constant::getNullValue(DstTy); + // If this cast changes element count then we can't handle it here: // doing so requires endianness information. This should be handled by // Analysis/ConstantFolding.cpp @@ -689,6 +693,42 @@ Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond, if (ConstantInt *CB = dyn_cast(Cond)) return CB->getZExtValue() ? V1 : V2; + // Check for zero aggregate and ConstantVector of zeros + if (Cond->isNullValue()) return V2; + + if (ConstantVector* CondV = dyn_cast(Cond)) { + + if (CondV->isAllOnesValue()) return V1; + + const VectorType *VTy = cast(V1->getType()); + ConstantVector *CP1 = dyn_cast(V1); + ConstantVector *CP2 = dyn_cast(V2); + + if ((CP1 || isa(V1)) && + (CP2 || isa(V2))) { + + // Find the element type of the returned vector + const Type *EltTy = VTy->getElementType(); + unsigned NumElem = VTy->getNumElements(); + std::vector Res(NumElem); + + bool Valid = true; + for (unsigned i = 0; i < NumElem; ++i) { + ConstantInt* c = dyn_cast(CondV->getOperand(i)); + if (!c) { + Valid = false; + break; + } + Constant *C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); + Constant *C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); + Res[i] = c->getZExtValue() ? C1 : C2; + } + // If we were able to build the vector, return it + if (Valid) return ConstantVector::get(Res); + } + } + + if (isa(V1)) return V2; if (isa(V2)) return V1; if (isa(Cond)) return V1; -- cgit v1.2.3