From 285b85b96e177816167b880d670fc545e4d30a78 Mon Sep 17 00:00:00 2001 From: Sanjoy Das Date: Fri, 10 Jun 2016 21:18:39 +0000 Subject: [PATCH] [STLExtras] Introduce and use llvm::count_if; NFC (This is split out from was D21115) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@272435 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ADT/STLExtras.h | 8 ++++++++ lib/IR/Metadata.cpp | 2 +- lib/Support/CommandLine.cpp | 9 ++++---- lib/Target/X86/X86ISelLowering.cpp | 33 +++++++++++------------------- 4 files changed, 25 insertions(+), 27 deletions(-) diff --git a/include/llvm/ADT/STLExtras.h b/include/llvm/ADT/STLExtras.h index 72682f8ae1e..3d2ca9eb148 100644 --- a/include/llvm/ADT/STLExtras.h +++ b/include/llvm/ADT/STLExtras.h @@ -419,6 +419,14 @@ bool is_contained(R &&Range, const E &Element) { return std::find(Range.begin(), Range.end(), Element) != Range.end(); } +/// Wrapper function around std::count_if to count the number of times an +/// element satisfying a given predicate occurs in a range. +template +auto count_if(R &&Range, UnaryPredicate &&P) + -> typename std::iterator_traits::difference_type { + return std::count_if(Range.begin(), Range.end(), P); +} + //===----------------------------------------------------------------------===// // Extra additions to //===----------------------------------------------------------------------===// diff --git a/lib/IR/Metadata.cpp b/lib/IR/Metadata.cpp index 865c68a2ef1..5d84bb75cfb 100644 --- a/lib/IR/Metadata.cpp +++ b/lib/IR/Metadata.cpp @@ -502,7 +502,7 @@ static bool isOperandUnresolved(Metadata *Op) { void MDNode::countUnresolvedOperands() { assert(NumUnresolved == 0 && "Expected unresolved ops to be uncounted"); assert(isUniqued() && "Expected this to be uniqued"); - NumUnresolved = std::count_if(op_begin(), op_end(), isOperandUnresolved); + NumUnresolved = count_if(operands(), isOperandUnresolved); } void MDNode::makeUniqued() { diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp index aed42b25e64..9be0daada50 100644 --- a/lib/Support/CommandLine.cpp +++ b/lib/Support/CommandLine.cpp @@ -194,11 +194,10 @@ public: void printOptionValues(); void registerCategory(OptionCategory *cat) { - assert(std::count_if(RegisteredOptionCategories.begin(), - RegisteredOptionCategories.end(), - [cat](const OptionCategory *Category) { - return cat->getName() == Category->getName(); - }) == 0 && + assert(count_if(RegisteredOptionCategories, + [cat](const OptionCategory *Category) { + return cat->getName() == Category->getName(); + }) == 0 && "Duplicate option categories"); RegisteredOptionCategories.insert(cat); diff --git a/lib/Target/X86/X86ISelLowering.cpp b/lib/Target/X86/X86ISelLowering.cpp index c74731e0b33..d15cfecebfb 100644 --- a/lib/Target/X86/X86ISelLowering.cpp +++ b/lib/Target/X86/X86ISelLowering.cpp @@ -8684,11 +8684,10 @@ static SDValue lowerVectorShuffleAsPermuteAndUnpack(SDLoc DL, MVT VT, int Size = Mask.size(); - int NumLoInputs = std::count_if(Mask.begin(), Mask.end(), [Size](int M) { - return M >= 0 && M % Size < Size / 2; - }); - int NumHiInputs = std::count_if( - Mask.begin(), Mask.end(), [Size](int M) { return M % Size >= Size / 2; }); + int NumLoInputs = + count_if(Mask, [Size](int M) { return M >= 0 && M % Size < Size / 2; }); + int NumHiInputs = + count_if(Mask, [Size](int M) { return M % Size >= Size / 2; }); bool UnpackLo = NumLoInputs >= NumHiInputs; @@ -9000,8 +8999,7 @@ static SDValue lowerVectorShuffleWithSHUFPS(SDLoc DL, MVT VT, SDValue LowV = V1, HighV = V2; int NewMask[4] = {Mask[0], Mask[1], Mask[2], Mask[3]}; - int NumV2Elements = - std::count_if(Mask.begin(), Mask.end(), [](int M) { return M >= 4; }); + int NumV2Elements = count_if(Mask, [](int M) { return M >= 4; }); if (NumV2Elements == 1) { int V2Index = @@ -9095,8 +9093,7 @@ static SDValue lowerV4F32VectorShuffle(SDValue Op, SDValue V1, SDValue V2, ArrayRef Mask = SVOp->getMask(); assert(Mask.size() == 4 && "Unexpected mask size for v4 shuffle!"); - int NumV2Elements = - std::count_if(Mask.begin(), Mask.end(), [](int M) { return M >= 4; }); + int NumV2Elements = count_if(Mask, [](int M) { return M >= 4; }); if (NumV2Elements == 0) { // Check for being able to broadcast a single element. @@ -9187,8 +9184,7 @@ static SDValue lowerV4I32VectorShuffle(SDValue Op, SDValue V1, SDValue V2, Mask, Subtarget, DAG)) return ZExt; - int NumV2Elements = - std::count_if(Mask.begin(), Mask.end(), [](int M) { return M >= 4; }); + int NumV2Elements = count_if(Mask, [](int M) { return M >= 4; }); if (NumV2Elements == 0) { // Check for being able to broadcast a single element. @@ -9716,11 +9712,9 @@ static SDValue lowerV8I16GeneralSingleInputVectorShuffle( // At this point, each half should contain all its inputs, and we can then // just shuffle them into their final position. - assert(std::count_if(LoMask.begin(), LoMask.end(), - [](int M) { return M >= 4; }) == 0 && + assert(count_if(LoMask, [](int M) { return M >= 4; }) == 0 && "Failed to lift all the high half inputs to the low mask!"); - assert(std::count_if(HiMask.begin(), HiMask.end(), - [](int M) { return M >= 0 && M < 4; }) == 0 && + assert(count_if(HiMask, [](int M) { return M >= 0 && M < 4; }) == 0 && "Failed to lift all the low half inputs to the high mask!"); // Do a half shuffle for the low mask. @@ -9830,7 +9824,7 @@ static SDValue lowerV8I16VectorShuffle(SDValue Op, SDValue V1, SDValue V2, (void)isV1; auto isV2 = [](int M) { return M >= 8; }; - int NumV2Inputs = std::count_if(Mask.begin(), Mask.end(), isV2); + int NumV2Inputs = count_if(Mask, isV2); if (NumV2Inputs == 0) { // Check for being able to broadcast a single element. @@ -10027,8 +10021,7 @@ static SDValue lowerV16I8VectorShuffle(SDValue Op, SDValue V1, SDValue V2, if (SDValue V = lowerVectorShuffleWithSSE4A(DL, MVT::v16i8, V1, V2, Mask, DAG)) return V; - int NumV2Elements = - std::count_if(Mask.begin(), Mask.end(), [](int M) { return M >= 16; }); + int NumV2Elements = count_if(Mask, [](int M) { return M >= 16; }); // For single-input shuffles, there are some nicer lowering tricks we can use. if (NumV2Elements == 0) { @@ -11595,9 +11588,7 @@ static SDValue lower256BitVectorShuffle(SDValue Op, SDValue V1, SDValue V2, // If we have a single input to the zero element, insert that into V1 if we // can do so cheaply. int NumElts = VT.getVectorNumElements(); - int NumV2Elements = std::count_if(Mask.begin(), Mask.end(), [NumElts](int M) { - return M >= NumElts; - }); + int NumV2Elements = count_if(Mask, [NumElts](int M) { return M >= NumElts; }); if (NumV2Elements == 1 && Mask[0] >= NumElts) if (SDValue Insertion = lowerVectorShuffleAsElementInsertion( -- 2.50.1