diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h --- a/llvm/include/llvm/ADT/SmallVector.h +++ b/llvm/include/llvm/ADT/SmallVector.h @@ -32,6 +32,8 @@ namespace llvm { +template class ArrayRef; + template class iterator_range; /// This is all the stuff common to all SmallVectors. @@ -1215,6 +1217,12 @@ this->assign(IL); } + template ::value>> + explicit SmallVector(ArrayRef A) : SmallVectorImpl(N) { + this->append(A.begin(), A.end()); + } + SmallVector(const SmallVector &RHS) : SmallVectorImpl(N) { if (!RHS.empty()) SmallVectorImpl::operator=(RHS); diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp --- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -19362,7 +19362,7 @@ } if (ElementOffset != -1) { - SmallVector NewMask(Mask.begin(), Mask.end()); + SmallVector NewMask(Mask); auto *ExtrIndex = cast(InsertVal.getOperand(1)); NewMask[InsIndex] = ElementOffset + ExtrIndex->getZExtValue(); @@ -22325,7 +22325,7 @@ // First, check if we are taking one element of a vector and shuffling that // element into another vector. ArrayRef Mask = Shuf->getMask(); - SmallVector CommutedMask(Mask.begin(), Mask.end()); + SmallVector CommutedMask(Mask); SDValue Op0 = Shuf->getOperand(0); SDValue Op1 = Shuf->getOperand(1); int ShufOp0Index = getShuffleMaskIndexOfOneElementFromOp0IntoOp1(Mask); @@ -22636,7 +22636,7 @@ if (SDValue InsertN1 = ShuffleToInsert(N0, N1, Mask)) return InsertN1; if (N0.getOpcode() == ISD::CONCAT_VECTORS) { - SmallVector CommuteMask(Mask.begin(), Mask.end()); + SmallVector CommuteMask(Mask); ShuffleVectorSDNode::commuteMask(CommuteMask); if (SDValue InsertN0 = ShuffleToInsert(N1, N0, CommuteMask)) return InsertN0; diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp --- a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp @@ -2286,7 +2286,7 @@ // If Lo or Hi uses elements from at most two of the four input vectors, then // express it as a vector shuffle of those two inputs. Otherwise extract the // input elements by hand and construct the Lo/Hi output using a BUILD_VECTOR. - SmallVector OrigMask(N->getMask().begin(), N->getMask().end()); + SmallVector OrigMask(N->getMask()); // Try to pack incoming shuffles/inputs. auto &&TryPeekThroughShufflesInputs = [&Inputs, &NewVT, this, NewElts, &DL](SmallVectorImpl &Mask) { diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -1893,7 +1893,7 @@ "Index out of range"); // Copy the mask so we can do any needed cleanup. - SmallVector MaskVec(Mask.begin(), Mask.end()); + SmallVector MaskVec(Mask); // Canonicalize shuffle v, v -> v, undef if (N1 == N2) { @@ -2049,7 +2049,7 @@ SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) { EVT VT = SV.getValueType(0); - SmallVector MaskVec(SV.getMask().begin(), SV.getMask().end()); + SmallVector MaskVec(SV.getMask()); ShuffleVectorSDNode::commuteMask(MaskVec); SDValue Op0 = SV.getOperand(0); diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -3710,7 +3710,7 @@ } // Calculate new mask. - SmallVector MappedOps(Mask.begin(), Mask.end()); + SmallVector MappedOps(Mask); for (int &Idx : MappedOps) { if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts + StartIdx[1] - MaskNumElts; diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp --- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp @@ -3118,7 +3118,7 @@ // Simplify mask using undef elements from LHS/RHS. bool Updated = false; bool IdentityLHS = true, IdentityRHS = true; - SmallVector NewMask(ShuffleMask.begin(), ShuffleMask.end()); + SmallVector NewMask(ShuffleMask); for (unsigned i = 0; i != NumElts; ++i) { int &M = NewMask[i]; if (M < 0) diff --git a/llvm/lib/Target/ARM/ARMISelLowering.cpp b/llvm/lib/Target/ARM/ARMISelLowering.cpp --- a/llvm/lib/Target/ARM/ARMISelLowering.cpp +++ b/llvm/lib/Target/ARM/ARMISelLowering.cpp @@ -18299,7 +18299,7 @@ if (S0->getOperand(0) == S1->getOperand(0) && S0->getOperand(1) == S1->getOperand(1)) { // Construct complete shuffle mask - SmallVector Mask(S0->getMask().begin(), S0->getMask().end()); + SmallVector Mask(S0->getMask()); Mask.append(S1->getMask().begin(), S1->getMask().end()); if (isVMOVNTruncMask(Mask, VT, false)) diff --git a/llvm/lib/Target/Hexagon/HexagonISelDAGToDAGHVX.cpp b/llvm/lib/Target/Hexagon/HexagonISelDAGToDAGHVX.cpp --- a/llvm/lib/Target/Hexagon/HexagonISelDAGToDAGHVX.cpp +++ b/llvm/lib/Target/Hexagon/HexagonISelDAGToDAGHVX.cpp @@ -1149,7 +1149,7 @@ // Check if we can shuffle vector halves around to get the used elements // into a single vector. - SmallVector MaskH(SM.Mask.begin(), SM.Mask.end()); + SmallVector MaskH(SM.Mask); SmallVector SegList = getInputSegmentList(SM.Mask, SegLen); unsigned SegCount = SegList.size(); SmallVector SegMap = getOutputSegmentMap(SM.Mask, SegLen); @@ -1271,11 +1271,11 @@ ShuffleMask SMH(MaskH); assert(SMH.Mask.size() == VecLen); - SmallVector MaskA(SMH.Mask.begin(), SMH.Mask.end()); + SmallVector MaskA(SMH.Mask); if (SMH.MaxSrc - SMH.MinSrc >= static_cast(HwLen)) { // valign(Lo=Va,Hi=Vb) won't work. Try swapping Va/Vb. - SmallVector Swapped(SMH.Mask.begin(), SMH.Mask.end()); + SmallVector Swapped(SMH.Mask); ShuffleVectorSDNode::commuteMask(Swapped); ShuffleMask SW(Swapped); if (SW.MaxSrc - SW.MinSrc < static_cast(HwLen)) { @@ -1998,7 +1998,7 @@ // a vector pair, but the two vectors in the pair are swapped. // The code below that identifies perfect shuffles will reject // it, unless the order is reversed. - SmallVector MaskStorage(SM.Mask.begin(), SM.Mask.end()); + SmallVector MaskStorage(SM.Mask); bool InvertedPair = false; if (HavePairs && SM.Mask[0] >= int(HwLen)) { for (int i = 0, e = SM.Mask.size(); i != e; ++i) { diff --git a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp --- a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp +++ b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp @@ -14885,7 +14885,7 @@ // Adjust the shuffle mask if either input vector comes from a // SCALAR_TO_VECTOR and keep the respective input vector in permuted // form (to prevent the need for a swap). - SmallVector ShuffV(Mask.begin(), Mask.end()); + SmallVector ShuffV(Mask); SDValue SToVLHS = isScalarToVec(LHS); SDValue SToVRHS = isScalarToVec(RHS); if (SToVLHS || SToVRHS) { diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp --- a/llvm/lib/Target/X86/X86ISelLowering.cpp +++ b/llvm/lib/Target/X86/X86ISelLowering.cpp @@ -6133,7 +6133,7 @@ SmallVectorImpl &WidenedMask) { // Create an alternative mask with info about zeroable elements. // Here we do not set undef elements as zeroable. - SmallVector ZeroableMask(Mask.begin(), Mask.end()); + SmallVector ZeroableMask(Mask); if (V2IsZero) { assert(!Zeroable.isZero() && "V2's non-undef elements are used?!"); for (int i = 0, Size = Mask.size(); i != Size; ++i) @@ -11883,7 +11883,7 @@ MVT VT = MVT::getVectorVT(EltVT, Mask.size()); // We can't assume a canonical shuffle mask, so try the commuted version too. - SmallVector CommutedMask(Mask.begin(), Mask.end()); + SmallVector CommutedMask(Mask); ShuffleVectorSDNode::commuteMask(CommutedMask); // Match any of unary/binary or low/high. @@ -12742,7 +12742,7 @@ SelectionDAG &DAG) { uint64_t BlendMask = 0; bool ForceV1Zero = false, ForceV2Zero = false; - SmallVector Mask(Original.begin(), Original.end()); + SmallVector Mask(Original); if (!matchShuffleAsBlend(V1, V2, Mask, Zeroable, ForceV1Zero, ForceV2Zero, BlendMask)) return SDValue(); @@ -14185,7 +14185,7 @@ assert(VT == ExtVT && "Cannot change extended type when non-zeroable!"); if (!VT.isFloatingPoint() || V2Index != 0) return SDValue(); - SmallVector V1Mask(Mask.begin(), Mask.end()); + SmallVector V1Mask(Mask); V1Mask[V2Index] = -1; if (!isNoopShuffleMask(V1Mask)) return SDValue(); @@ -14334,7 +14334,7 @@ // Match extracts of each half of the wide source vector. Commute the shuffle // if the extract of the low half is N1. unsigned NumElts = VT.getVectorNumElements(); - SmallVector NewMask(Mask.begin(), Mask.end()); + SmallVector NewMask(Mask); const APInt &ExtIndex0 = N0.getConstantOperandAPInt(1); const APInt &ExtIndex1 = N1.getConstantOperandAPInt(1); if (ExtIndex1 == 0 && ExtIndex0 == NumElts) @@ -14638,7 +14638,7 @@ return true; // Commute and try again. - SmallVector CommutedMask(Mask.begin(), Mask.end()); + SmallVector CommutedMask(Mask); ShuffleVectorSDNode::commuteMask(CommutedMask); if (matchAsInsertPS(V2, V1, CommutedMask)) return true; @@ -14973,7 +14973,7 @@ ArrayRef Mask, SDValue V1, SDValue V2, SelectionDAG &DAG) { SDValue LowV = V1, HighV = V2; - SmallVector NewMask(Mask.begin(), Mask.end()); + SmallVector NewMask(Mask); int NumV2Elements = count_if(Mask, [](int M) { return M >= 4; }); if (NumV2Elements == 1) { @@ -15894,7 +15894,7 @@ return Rotate; // Make a copy of the mask so it can be modified. - SmallVector MutableMask(Mask.begin(), Mask.end()); + SmallVector MutableMask(Mask); return lowerV8I16GeneralSingleInputShuffle(DL, MVT::v8i16, V1, MutableMask, Subtarget, DAG); } @@ -16076,7 +16076,7 @@ // Adjust mask to correct indices for the second input. int NumElts = VT.getVectorNumElements(); unsigned Scale = 512 / VT.getSizeInBits(); - SmallVector AdjustedMask(Mask.begin(), Mask.end()); + SmallVector AdjustedMask(Mask); for (int &M : AdjustedMask) if (NumElts <= M) M += (Scale - 1) * NumElts; @@ -16822,7 +16822,7 @@ assert(V2.isUndef() && "This last part of this routine only works on single input shuffles"); - SmallVector InLaneMask(Mask.begin(), Mask.end()); + SmallVector InLaneMask(Mask); for (int i = 0; i < Size; ++i) { int &M = InLaneMask[i]; if (M < 0) @@ -19310,7 +19310,7 @@ // the mask. if (V2IsUndef && any_of(OrigMask, [NumElements](int M) { return M >= NumElements; })) { - SmallVector NewMask(OrigMask.begin(), OrigMask.end()); + SmallVector NewMask(OrigMask); for (int &M : NewMask) if (M >= NumElements) M = -1; @@ -19383,7 +19383,7 @@ } SmallVector Ops = {V1, V2}; - SmallVector Mask(OrigMask.begin(), OrigMask.end()); + SmallVector Mask(OrigMask); // Canonicalize the shuffle with any horizontal ops inputs. // NOTE: This may update Ops and Mask. @@ -37542,7 +37542,7 @@ (MaskVT == MVT::v16i16 && Subtarget.hasAVX2())) { uint64_t BlendMask = 0; bool ForceV1Zero = false, ForceV2Zero = false; - SmallVector TargetMask(Mask.begin(), Mask.end()); + SmallVector TargetMask(Mask); if (matchShuffleAsBlend(V1, V2, TargetMask, Zeroable, ForceV1Zero, ForceV2Zero, BlendMask)) { if (MaskVT == MVT::v16i16) { @@ -37745,7 +37745,7 @@ return CanonicalizeShuffleInput(RootVT, V1); } - SmallVector Mask(BaseMask.begin(), BaseMask.end()); + SmallVector Mask(BaseMask); // See if the shuffle is a hidden identity shuffle - repeated args in HOPs // etc. can be simplified. @@ -38488,7 +38488,7 @@ for (unsigned i = 1; i != NumInputs; ++i) Offsets[i] += i * Scale * NumMaskElts; - SmallVector WideMask(BaseMask.begin(), BaseMask.end()); + SmallVector WideMask(BaseMask); for (int &M : WideMask) { if (M < 0) continue; diff --git a/llvm/lib/Target/X86/X86MCInstLower.cpp b/llvm/lib/Target/X86/X86MCInstLower.cpp --- a/llvm/lib/Target/X86/X86MCInstLower.cpp +++ b/llvm/lib/Target/X86/X86MCInstLower.cpp @@ -1857,7 +1857,7 @@ SrcOp2.isReg() ? GetRegisterName(SrcOp2.getReg()) : "mem"; // One source operand, fix the mask to print all elements in one span. - SmallVector ShuffleMask(Mask.begin(), Mask.end()); + SmallVector ShuffleMask(Mask); if (Src1Name == Src2Name) for (int i = 0, e = ShuffleMask.size(); i != e; ++i) if (ShuffleMask[i] >= e) diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp --- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp +++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp @@ -7653,7 +7653,7 @@ /// Functions adds masks, merging them into single one. void addMask(ArrayRef SubMask) { - SmallVector NewMask(SubMask.begin(), SubMask.end()); + SmallVector NewMask(SubMask); addMask(NewMask); } @@ -8749,7 +8749,7 @@ return PoisonValue::get(FixedVectorType::get( cast(V1->getType())->getElementType(), Mask.size())); Value *Op = V1; - SmallVector CombinedMask(Mask.begin(), Mask.end()); + SmallVector CombinedMask(Mask); PeekThroughShuffles(Op, CombinedMask); if (!isa(Op->getType()) || !IsIdentityMask(CombinedMask, cast(Op->getType()))) { diff --git a/llvm/unittests/ADT/SmallVectorTest.cpp b/llvm/unittests/ADT/SmallVectorTest.cpp --- a/llvm/unittests/ADT/SmallVectorTest.cpp +++ b/llvm/unittests/ADT/SmallVectorTest.cpp @@ -242,6 +242,16 @@ this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3); } +// Constructor test. +TYPED_TEST(SmallVectorTest, ConstructorFromArrayRefSimpleTest) { + SCOPED_TRACE("ConstructorTest"); + ArrayRef Array = {Constructable(1), Constructable(2), + Constructable(3)}; + this->theVector = SmallVector(Array); + this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3); + ASSERT_EQ(NumBuiltinElts(TypeParam{}), NumBuiltinElts(this->theVector)); +} + // New vector test. TYPED_TEST(SmallVectorTest, EmptyVectorTest) { SCOPED_TRACE("EmptyVectorTest"); @@ -1157,26 +1167,24 @@ } } -TEST(SmallVectorTest, ToVectorOf) { - struct Metadata { - int content; - bool operator==(const Metadata &RHS) const { - return content == RHS.content; - } - }; +struct Metadata { + int content; + bool operator==(const Metadata &RHS) const { return content == RHS.content; } +}; - class MDOperand { - public: - MDOperand() = default; - MDOperand(Metadata *M) { MD = M; } - operator Metadata *() const { return get(); } - Metadata &operator*() const { return *get(); } +class MDOperand { +public: + MDOperand() = default; + MDOperand(Metadata *M) { MD = M; } + operator Metadata *() const { return get(); } + Metadata &operator*() const { return *get(); } - private: - Metadata *get() const { return MD; } - Metadata *MD = nullptr; - }; +private: + Metadata *get() const { return MD; } + Metadata *MD = nullptr; +}; +TEST(SmallVectorTest, ToVectorOf) { Metadata m1{1}, m2{2}, m3{3}; std::vector Operands = {MDOperand(&m1), MDOperand(&m2), MDOperand(&m3)}; @@ -1224,6 +1232,32 @@ } } +TEST(SmallVectorTest, ConstructFromArrayRefOfConvertibleType) { + Metadata m1{1}, m2{2}, m3{3}; + std::vector Operands = {MDOperand(&m1), MDOperand(&m2), + MDOperand(&m3)}; + ArrayRef ArrayOperands = Operands; + { + llvm::SmallVector MetadataVector(ArrayOperands); + + ASSERT_EQ(ArrayOperands.size(), MetadataVector.size()); + for (size_t I = 0; I < ArrayOperands.size(); ++I) { + EXPECT_EQ(ArrayOperands[I], MetadataVector[I]); + EXPECT_EQ(*ArrayOperands[I], *MetadataVector[I]); + } + } + { + llvm::SmallVector MetadataVector(ArrayOperands); + + ASSERT_EQ(ArrayOperands.size(), MetadataVector.size()); + ASSERT_EQ(4u, NumBuiltinElts(MetadataVector)); + for (size_t I = 0; I < ArrayOperands.size(); ++I) { + EXPECT_EQ(ArrayOperands[I], MetadataVector[I]); + EXPECT_EQ(*ArrayOperands[I], *MetadataVector[I]); + } + } +} + template class SmallVectorReferenceInvalidationTest : public SmallVectorTestBase { protected: