From 2ce8842641cc312628c4be836d34eb250e7c3f78 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 25 Jan 2012 05:34:41 +0000 Subject: [PATCH] reapply r148902: "use the new ConstantVector::getSplat method where it makes sense." Also simplify a bunch of code to use the Builder->getInt32 instead of doing it the hard and ugly way. Much more progress could be made here, but I don't plan to do it. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@148926 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGBuiltin.cpp | 18 +++++++----------- lib/CodeGen/CGExpr.cpp | 30 +++++++++++------------------- lib/CodeGen/CGExprScalar.cpp | 26 ++++++-------------------- 3 files changed, 24 insertions(+), 50 deletions(-) diff --git a/lib/CodeGen/CGBuiltin.cpp b/lib/CodeGen/CGBuiltin.cpp index a3e21dcab6..e63eeb3c76 100644 --- a/lib/CodeGen/CGBuiltin.cpp +++ b/lib/CodeGen/CGBuiltin.cpp @@ -1212,8 +1212,7 @@ static llvm::VectorType *GetNeonType(LLVMContext &C, NeonTypeFlags TypeFlags) { Value *CodeGenFunction::EmitNeonSplat(Value *V, Constant *C) { unsigned nElts = cast(V->getType())->getNumElements(); - SmallVector Indices(nElts, C); - Value* SV = llvm::ConstantVector::get(Indices); + Value* SV = llvm::ConstantVector::getSplat(nElts, C); return Builder.CreateShuffleVector(V, V, SV, "lane"); } @@ -1233,13 +1232,11 @@ Value *CodeGenFunction::EmitNeonCall(Function *F, SmallVectorImpl &Ops, Value *CodeGenFunction::EmitNeonShiftVector(Value *V, llvm::Type *Ty, bool neg) { - ConstantInt *CI = cast(V); - int SV = CI->getSExtValue(); + int SV = cast(V)->getSExtValue(); llvm::VectorType *VTy = cast(Ty); llvm::Constant *C = ConstantInt::get(VTy->getElementType(), neg ? -SV : SV); - SmallVector CV(VTy->getNumElements(), C); - return llvm::ConstantVector::get(CV); + return llvm::ConstantVector::getSplat(VTy->getNumElements(), C); } /// GetPointeeAlignment - Given an expression with a pointer type, find the @@ -1957,8 +1954,8 @@ Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID, for (unsigned vi = 0; vi != 2; ++vi) { SmallVector Indices; for (unsigned i = 0, e = VTy->getNumElements(); i != e; i += 2) { - Indices.push_back(ConstantInt::get(Int32Ty, i+vi)); - Indices.push_back(ConstantInt::get(Int32Ty, i+e+vi)); + Indices.push_back(Builder.getInt32(i+vi)); + Indices.push_back(Builder.getInt32(i+e+vi)); } Value *Addr = Builder.CreateConstInBoundsGEP1_32(Ops[0], vi); SV = llvm::ConstantVector::get(Indices); @@ -2019,7 +2016,7 @@ BuildVector(const SmallVectorImpl &Ops) { // If this is a constant vector, create a ConstantVector. if (AllConstants) { - std::vector CstOps; + SmallVector CstOps; for (unsigned i = 0, e = Ops.size(); i != e; ++i) CstOps.push_back(cast(Ops[i])); return llvm::ConstantVector::get(CstOps); @@ -2030,8 +2027,7 @@ BuildVector(const SmallVectorImpl &Ops) { llvm::UndefValue::get(llvm::VectorType::get(Ops[0]->getType(), Ops.size())); for (unsigned i = 0, e = Ops.size(); i != e; ++i) - Result = Builder.CreateInsertElement(Result, Ops[i], - llvm::ConstantInt::get(llvm::Type::getInt32Ty(getLLVMContext()), i)); + Result = Builder.CreateInsertElement(Result, Ops[i], Builder.getInt32(i)); return Result; } diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index 5c2061bed0..a9317f7e46 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -952,10 +952,8 @@ RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV) { unsigned NumResultElts = ExprVT->getNumElements(); SmallVector Mask; - for (unsigned i = 0; i != NumResultElts; ++i) { - unsigned InIdx = getAccessedFieldNo(i, Elts); - Mask.push_back(llvm::ConstantInt::get(Int32Ty, InIdx)); - } + for (unsigned i = 0; i != NumResultElts; ++i) + Mask.push_back(Builder.getInt32(getAccessedFieldNo(i, Elts))); llvm::Value *MaskV = llvm::ConstantVector::get(Mask); Vec = Builder.CreateShuffleVector(Vec, llvm::UndefValue::get(Vec->getType()), @@ -1175,10 +1173,8 @@ void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src, // elements and restore the vector mask since it is on the side it will be // stored. SmallVector Mask(NumDstElts); - for (unsigned i = 0; i != NumSrcElts; ++i) { - unsigned InIdx = getAccessedFieldNo(i, Elts); - Mask[InIdx] = llvm::ConstantInt::get(Int32Ty, i); - } + for (unsigned i = 0; i != NumSrcElts; ++i) + Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i); llvm::Value *MaskV = llvm::ConstantVector::get(Mask); Vec = Builder.CreateShuffleVector(SrcVal, @@ -1192,7 +1188,7 @@ void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src, SmallVector ExtMask; unsigned i; for (i = 0; i != NumSrcElts; ++i) - ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i)); + ExtMask.push_back(Builder.getInt32(i)); for (; i != NumDstElts; ++i) ExtMask.push_back(llvm::UndefValue::get(Int32Ty)); llvm::Value *ExtMaskV = llvm::ConstantVector::get(ExtMask); @@ -1203,13 +1199,11 @@ void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src, // build identity SmallVector Mask; for (unsigned i = 0; i != NumDstElts; ++i) - Mask.push_back(llvm::ConstantInt::get(Int32Ty, i)); + Mask.push_back(Builder.getInt32(i)); // modify when what gets shuffled in - for (unsigned i = 0; i != NumSrcElts; ++i) { - unsigned Idx = getAccessedFieldNo(i, Elts); - Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts); - } + for (unsigned i = 0; i != NumSrcElts; ++i) + Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i+NumDstElts); llvm::Value *MaskV = llvm::ConstantVector::get(Mask); Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV); } else { @@ -1734,13 +1728,11 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { } static -llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext, +llvm::Constant *GenerateConstantVector(llvm::IRBuilder<> &Builder, SmallVector &Elts) { SmallVector CElts; - - llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext); for (unsigned i = 0, e = Elts.size(); i != e; ++i) - CElts.push_back(llvm::ConstantInt::get(Int32Ty, Elts[i])); + CElts.push_back(Builder.getInt32(Elts[i])); return llvm::ConstantVector::get(CElts); } @@ -1783,7 +1775,7 @@ EmitExtVectorElementExpr(const ExtVectorElementExpr *E) { E->getEncodedElementAccess(Indices); if (Base.isSimple()) { - llvm::Constant *CV = GenerateConstantVector(getLLVMContext(), Indices); + llvm::Constant *CV = GenerateConstantVector(Builder, Indices); return LValue::MakeExtVectorElt(Base.getAddress(), CV, type); } assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!"); diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp index e1342d3ce5..1833a9083d 100644 --- a/lib/CodeGen/CGExprScalar.cpp +++ b/lib/CodeGen/CGExprScalar.cpp @@ -610,12 +610,9 @@ Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType, UnV = Builder.CreateInsertElement(UnV, Elt, Idx); // Splat the element across to all elements - SmallVector Args; unsigned NumElements = cast(DstTy)->getNumElements(); - for (unsigned i = 0; i != NumElements; ++i) - Args.push_back(Builder.getInt32(0)); - - llvm::Constant *Mask = llvm::ConstantVector::get(Args); + llvm::Constant *Mask = llvm::ConstantVector::getSplat(NumElements, + Builder.getInt32(0)); llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat"); return Yay; } @@ -749,11 +746,8 @@ Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { (1 << llvm::Log2_32(LHSElts))-1); // Mask off the high bits of each shuffle index. - SmallVector MaskV; - for (unsigned i = 0, e = MTy->getNumElements(); i != e; ++i) - MaskV.push_back(EltMask); - - Value* MaskBits = llvm::ConstantVector::get(MaskV); + Value *MaskBits = llvm::ConstantVector::getSplat(MTy->getNumElements(), + EltMask); Mask = Builder.CreateAnd(Mask, MaskBits, "mask"); // newv = undef @@ -1203,13 +1197,9 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) { UnV = Builder.CreateInsertElement(UnV, Elt, Idx); // Splat the element across to all elements - SmallVector Args; unsigned NumElements = cast(DstTy)->getNumElements(); llvm::Constant *Zero = Builder.getInt32(0); - for (unsigned i = 0; i < NumElements; i++) - Args.push_back(Zero); - - llvm::Constant *Mask = llvm::ConstantVector::get(Args); + llvm::Constant *Mask = llvm::ConstantVector::getSplat(NumElements, Zero); llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat"); return Yay; } @@ -2596,11 +2586,7 @@ VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { unsigned numElem = vecTy->getNumElements(); llvm::Type *elemType = vecTy->getElementType(); - std::vector Zvals; - for (unsigned i = 0; i < numElem; ++i) - Zvals.push_back(llvm::ConstantInt::get(elemType, 0)); - - llvm::Value *zeroVec = llvm::ConstantVector::get(Zvals); + llvm::Value *zeroVec = llvm::Constant::getNullValue(vecTy); llvm::Value *TestMSB = Builder.CreateICmpSLT(CondV, zeroVec); llvm::Value *tmp = Builder.CreateSExt(TestMSB, llvm::VectorType::get(elemType, -- 2.40.0