From 37508d3dd94b0154861a90b1909d17b01400df99 Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Thu, 15 Aug 2019 10:56:05 +0000 Subject: [PATCH] Replace llvm::integer_sequence and friends with the C++14 standard version The implementation in libc++ takes O(1) compile time, ours was O(n). llvm-svn: 368990 --- clang-tools-extra/clangd/Function.h | 4 +- clang-tools-extra/clangd/unittests/Matchers.h | 4 +- .../clang/ASTMatchers/ASTMatchersInternal.h | 4 +- clang/include/clang/Sema/Sema.h | 4 +- .../RefactoringActionRulesInternal.h | 8 +- clang/lib/CodeGen/EHScopeStack.h | 4 +- llvm/include/llvm/ADT/STLExtras.h | 107 ++++++++---------- .../ExecutionEngine/Orc/RPCSerialization.h | 8 +- .../llvm/ExecutionEngine/Orc/RPCUtils.h | 15 +-- llvm/include/llvm/IR/PassManager.h | 4 +- llvm/include/llvm/Support/Format.h | 5 +- 11 files changed, 75 insertions(+), 92 deletions(-) diff --git a/clang-tools-extra/clangd/Function.h b/clang-tools-extra/clangd/Function.h index 6d91136bf994..91aaa48a9fff 100644 --- a/clang-tools-extra/clangd/Function.h +++ b/clang-tools-extra/clangd/Function.h @@ -59,14 +59,14 @@ private: public: template auto operator()(RestArgs &&... Rest) - -> decltype(this->CallImpl(llvm::index_sequence_for(), + -> decltype(this->CallImpl(std::index_sequence_for(), std::forward(Rest)...)) { #ifndef NDEBUG assert(!WasCalled && "Can only call result of Bind once."); WasCalled = true; #endif - return CallImpl(llvm::index_sequence_for(), + return CallImpl(std::index_sequence_for(), std::forward(Rest)...); } }; diff --git a/clang-tools-extra/clangd/unittests/Matchers.h b/clang-tools-extra/clangd/unittests/Matchers.h index 0946398d8144..d477cf75e723 100644 --- a/clang-tools-extra/clangd/unittests/Matchers.h +++ b/clang-tools-extra/clangd/unittests/Matchers.h @@ -89,12 +89,12 @@ public: template operator Matcher &>() const { return ::testing::MakeMatcher(new SubsequenceMatcher( - TypedMatchers(llvm::index_sequence_for{}))); + TypedMatchers(std::index_sequence_for{}))); } private: template - std::vector> TypedMatchers(llvm::index_sequence) const { + std::vector> TypedMatchers(std::index_sequence) const { return {std::get(Matchers)...}; } }; diff --git a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h index 46e47a3100b5..ae7b9e7c3ee5 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h +++ b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h @@ -1334,14 +1334,14 @@ public: template operator Matcher() const { return DynTypedMatcher::constructVariadic( Op, ast_type_traits::ASTNodeKind::getFromNodeKind(), - getMatchers(llvm::index_sequence_for())) + getMatchers(std::index_sequence_for())) .template unconditionalConvertTo(); } private: // Helper method to unpack the tuple into a vector. template - std::vector getMatchers(llvm::index_sequence) const { + std::vector getMatchers(std::index_sequence) const { return {Matcher(std::get(Params))...}; } diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 4be2e5cdb96c..2dbb045be25c 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -1621,7 +1621,7 @@ public: template void emit(const SemaDiagnosticBuilder &DB, - llvm::index_sequence) const { + std::index_sequence) const { // Apply all tuple elements to the builder in order. bool Dummy[] = {false, (DB << getPrintable(std::get(Args)))...}; (void)Dummy; @@ -1635,7 +1635,7 @@ public: void diagnose(Sema &S, SourceLocation Loc, QualType T) override { const SemaDiagnosticBuilder &DB = S.Diag(Loc, DiagID); - emit(DB, llvm::index_sequence_for()); + emit(DB, std::index_sequence_for()); DB << T; } }; diff --git a/clang/include/clang/Tooling/Refactoring/RefactoringActionRulesInternal.h b/clang/include/clang/Tooling/Refactoring/RefactoringActionRulesInternal.h index aaa9e50faa9a..fb373fcf5029 100644 --- a/clang/include/clang/Tooling/Refactoring/RefactoringActionRulesInternal.h +++ b/clang/include/clang/Tooling/Refactoring/RefactoringActionRulesInternal.h @@ -47,7 +47,7 @@ template void invokeRuleAfterValidatingRequirements( RefactoringResultConsumer &Consumer, RefactoringRuleContext &Context, const std::tuple &Requirements, - llvm::index_sequence) { + std::index_sequence) { // Check if the requirements we're interested in can be evaluated. auto Values = std::make_tuple(std::get(Requirements).evaluate(Context)...); @@ -87,7 +87,7 @@ template void visitRefactoringOptions( RefactoringOptionVisitor &Visitor, const std::tuple &Requirements, - llvm::index_sequence) { + std::index_sequence) { visitRefactoringOptionsImpl(Visitor, std::get(Requirements)...); } @@ -131,7 +131,7 @@ createRefactoringActionRule(const RequirementTypes &... Requirements) { RefactoringRuleContext &Context) override { internal::invokeRuleAfterValidatingRequirements( Consumer, Context, Requirements, - llvm::index_sequence_for()); + std::index_sequence_for()); } bool hasSelectionRequirement() override { @@ -142,7 +142,7 @@ createRefactoringActionRule(const RequirementTypes &... Requirements) { void visitRefactoringOptions(RefactoringOptionVisitor &Visitor) override { internal::visitRefactoringOptions( Visitor, Requirements, - llvm::index_sequence_for()); + std::index_sequence_for()); } private: std::tuple Requirements; diff --git a/clang/lib/CodeGen/EHScopeStack.h b/clang/lib/CodeGen/EHScopeStack.h index 3b0db35d982b..0ed67aabcd62 100644 --- a/clang/lib/CodeGen/EHScopeStack.h +++ b/clang/lib/CodeGen/EHScopeStack.h @@ -199,14 +199,14 @@ public: SavedTuple Saved; template - T restore(CodeGenFunction &CGF, llvm::index_sequence) { + T restore(CodeGenFunction &CGF, std::index_sequence) { // It's important that the restores are emitted in order. The braced init // list guarantees that. return T{DominatingValue::restore(CGF, std::get(Saved))...}; } void Emit(CodeGenFunction &CGF, Flags flags) override { - restore(CGF, llvm::index_sequence_for()).Emit(CGF, flags); + restore(CGF, std::index_sequence_for()).Emit(CGF, flags); } public: diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h index 81dce0168c79..0e3911424a66 100644 --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -530,10 +530,6 @@ bool all_of(R &&range, UnaryPredicate P); template bool any_of(R &&range, UnaryPredicate P); -template struct index_sequence; - -template struct index_sequence_for; - namespace detail { using std::declval; @@ -568,38 +564,38 @@ struct zip_common : public zip_traits { std::tuple iterators; protected: - template value_type deref(index_sequence) const { + template value_type deref(std::index_sequence) const { return value_type(*std::get(iterators)...); } template - decltype(iterators) tup_inc(index_sequence) const { + decltype(iterators) tup_inc(std::index_sequence) const { return std::tuple(std::next(std::get(iterators))...); } template - decltype(iterators) tup_dec(index_sequence) const { + decltype(iterators) tup_dec(std::index_sequence) const { return std::tuple(std::prev(std::get(iterators))...); } public: zip_common(Iters &&... ts) : iterators(std::forward(ts)...) {} - value_type operator*() { return deref(index_sequence_for{}); } + value_type operator*() { return deref(std::index_sequence_for{}); } const value_type operator*() const { - return deref(index_sequence_for{}); + return deref(std::index_sequence_for{}); } ZipType &operator++() { - iterators = tup_inc(index_sequence_for{}); + iterators = tup_inc(std::index_sequence_for{}); return *reinterpret_cast(this); } ZipType &operator--() { static_assert(Base::IsBidirectional, "All inner iterators must be at least bidirectional."); - iterators = tup_dec(index_sequence_for{}); + iterators = tup_dec(std::index_sequence_for{}); return *reinterpret_cast(this); } }; @@ -618,7 +614,8 @@ struct zip_first : public zip_common, Iters...> { template class zip_shortest : public zip_common, Iters...> { template - bool test(const zip_shortest &other, index_sequence) const { + bool test(const zip_shortest &other, + std::index_sequence) const { return all_of(std::initializer_list{std::get(this->iterators) != std::get(other.iterators)...}, identity{}); @@ -630,7 +627,7 @@ public: zip_shortest(Iters &&... ts) : Base(std::forward(ts)...) {} bool operator==(const zip_shortest &other) const { - return !test(other, index_sequence_for{}); + return !test(other, std::index_sequence_for{}); } }; @@ -646,18 +643,21 @@ public: private: std::tuple ts; - template iterator begin_impl(index_sequence) const { + template + iterator begin_impl(std::index_sequence) const { return iterator(std::begin(std::get(ts))...); } - template iterator end_impl(index_sequence) const { + template iterator end_impl(std::index_sequence) const { return iterator(std::end(std::get(ts))...); } public: zippy(Args &&... ts_) : ts(std::forward(ts_)...) {} - iterator begin() const { return begin_impl(index_sequence_for{}); } - iterator end() const { return end_impl(index_sequence_for{}); } + iterator begin() const { + return begin_impl(std::index_sequence_for{}); + } + iterator end() const { return end_impl(std::index_sequence_for{}); } }; } // end namespace detail @@ -727,20 +727,20 @@ private: template bool test(const zip_longest_iterator &other, - index_sequence) const { + std::index_sequence) const { return llvm::any_of( std::initializer_list{std::get(this->iterators) != std::get(other.iterators)...}, identity{}); } - template value_type deref(index_sequence) const { + template value_type deref(std::index_sequence) const { return value_type( deref_or_none(std::get(iterators), std::get(end_iterators))...); } template - decltype(iterators) tup_inc(index_sequence) const { + decltype(iterators) tup_inc(std::index_sequence) const { return std::tuple( next_or_end(std::get(iterators), std::get(end_iterators))...); } @@ -750,17 +750,19 @@ public: : iterators(std::forward(ts.first)...), end_iterators(std::forward(ts.second)...) {} - value_type operator*() { return deref(index_sequence_for{}); } + value_type operator*() { return deref(std::index_sequence_for{}); } - value_type operator*() const { return deref(index_sequence_for{}); } + value_type operator*() const { + return deref(std::index_sequence_for{}); + } zip_longest_iterator &operator++() { - iterators = tup_inc(index_sequence_for{}); + iterators = tup_inc(std::index_sequence_for{}); return *this; } bool operator==(const zip_longest_iterator &other) const { - return !test(other, index_sequence_for{}); + return !test(other, std::index_sequence_for{}); } }; @@ -777,12 +779,13 @@ public: private: std::tuple ts; - template iterator begin_impl(index_sequence) const { + template + iterator begin_impl(std::index_sequence) const { return iterator(std::make_pair(adl_begin(std::get(ts)), adl_end(std::get(ts)))...); } - template iterator end_impl(index_sequence) const { + template iterator end_impl(std::index_sequence) const { return iterator(std::make_pair(adl_end(std::get(ts)), adl_end(std::get(ts)))...); } @@ -790,8 +793,10 @@ private: public: zip_longest_range(Args &&... ts_) : ts(std::forward(ts_)...) {} - iterator begin() const { return begin_impl(index_sequence_for{}); } - iterator end() const { return end_impl(index_sequence_for{}); } + iterator begin() const { + return begin_impl(std::index_sequence_for{}); + } + iterator end() const { return end_impl(std::index_sequence_for{}); } }; } // namespace detail @@ -847,7 +852,7 @@ class concat_iterator /// Increments the first non-end iterator. /// /// It is an error to call this with all iterators at the end. - template void increment(index_sequence) { + template void increment(std::index_sequence) { // Build a sequence of functions to increment each iterator if possible. bool (concat_iterator::*IncrementHelperFns[])() = { &concat_iterator::incrementHelper...}; @@ -876,7 +881,7 @@ class concat_iterator /// reference. /// /// It is an error to call this with all iterators at the end. - template ValueT &get(index_sequence) const { + template ValueT &get(std::index_sequence) const { // Build a sequence of functions to get from iterator if possible. ValueT *(concat_iterator::*GetHelperFns[])() const = { &concat_iterator::getHelper...}; @@ -901,11 +906,13 @@ public: using BaseT::operator++; concat_iterator &operator++() { - increment(index_sequence_for()); + increment(std::index_sequence_for()); return *this; } - ValueT &operator*() const { return get(index_sequence_for()); } + ValueT &operator*() const { + return get(std::index_sequence_for()); + } bool operator==(const concat_iterator &RHS) const { return Begins == RHS.Begins && Ends == RHS.Ends; @@ -928,10 +935,10 @@ public: private: std::tuple Ranges; - template iterator begin_impl(index_sequence) { + template iterator begin_impl(std::index_sequence) { return iterator(std::get(Ranges)...); } - template iterator end_impl(index_sequence) { + template iterator end_impl(std::index_sequence) { return iterator(make_range(std::end(std::get(Ranges)), std::end(std::get(Ranges)))...); } @@ -940,8 +947,8 @@ public: concat_range(RangeTs &&... Ranges) : Ranges(std::forward(Ranges)...) {} - iterator begin() { return begin_impl(index_sequence_for{}); } - iterator end() { return end_impl(index_sequence_for{}); } + iterator begin() { return begin_impl(std::index_sequence_for{}); } + iterator end() { return end_impl(std::index_sequence_for{}); } }; } // end namespace detail @@ -990,28 +997,6 @@ struct on_first { } }; -// A subset of N3658. More stuff can be added as-needed. - -/// Represents a compile-time sequence of integers. -template struct integer_sequence { - using value_type = T; - - static constexpr size_t size() { return sizeof...(I); } -}; - -/// Alias for the common case of a sequence of size_ts. -template -struct index_sequence : integer_sequence {}; - -template -struct build_index_impl : build_index_impl {}; -template -struct build_index_impl<0, I...> : index_sequence {}; - -/// Creates a compile-time integer sequence for a parameter pack. -template -struct index_sequence_for : build_index_impl {}; - /// Utility type to build an inheritance chain that makes it easy to rank /// overload candidates. template struct rank : rank {}; @@ -1580,7 +1565,7 @@ template detail::enumerator enumerate(R &&TheRange) { namespace detail { template -auto apply_tuple_impl(F &&f, Tuple &&t, index_sequence) +auto apply_tuple_impl(F &&f, Tuple &&t, std::index_sequence) -> decltype(std::forward(f)(std::get(std::forward(t))...)) { return std::forward(f)(std::get(std::forward(t))...); } @@ -1593,9 +1578,9 @@ auto apply_tuple_impl(F &&f, Tuple &&t, index_sequence) template auto apply_tuple(F &&f, Tuple &&t) -> decltype(detail::apply_tuple_impl( std::forward(f), std::forward(t), - build_index_impl< + std::make_index_sequence< std::tuple_size::type>::value>{})) { - using Indices = build_index_impl< + using Indices = std::make_index_sequence< std::tuple_size::type>::value>; return detail::apply_tuple_impl(std::forward(f), std::forward(t), diff --git a/llvm/include/llvm/ExecutionEngine/Orc/RPCSerialization.h b/llvm/include/llvm/ExecutionEngine/Orc/RPCSerialization.h index 07c7471afc6a..1892bf488251 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/RPCSerialization.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/RPCSerialization.h @@ -551,26 +551,26 @@ public: /// RPC channel serialization for std::tuple. static Error serialize(ChannelT &C, const std::tuple &V) { - return serializeTupleHelper(C, V, llvm::index_sequence_for()); + return serializeTupleHelper(C, V, std::index_sequence_for()); } /// RPC channel deserialization for std::tuple. static Error deserialize(ChannelT &C, std::tuple &V) { - return deserializeTupleHelper(C, V, llvm::index_sequence_for()); + return deserializeTupleHelper(C, V, std::index_sequence_for()); } private: // Serialization helper for std::tuple. template static Error serializeTupleHelper(ChannelT &C, const std::tuple &V, - llvm::index_sequence _) { + std::index_sequence _) { return serializeSeq(C, std::get(V)...); } // Serialization helper for std::tuple. template static Error deserializeTupleHelper(ChannelT &C, std::tuple &V, - llvm::index_sequence _) { + std::index_sequence _) { return deserializeSeq(C, std::get(V)...); } }; diff --git a/llvm/include/llvm/ExecutionEngine/Orc/RPCUtils.h b/llvm/include/llvm/ExecutionEngine/Orc/RPCUtils.h index 3b11e1b283de..1a8b2d14486c 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/RPCUtils.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/RPCUtils.h @@ -502,7 +502,7 @@ public: static typename WrappedHandlerReturn::Type unpackAndRun(HandlerT &Handler, std::tuple &Args) { return unpackAndRunHelper(Handler, Args, - llvm::index_sequence_for()); + std::index_sequence_for()); } // Call the given handler with the given arguments. @@ -510,7 +510,7 @@ public: static Error unpackAndRunAsync(HandlerT &Handler, ResponderT &Responder, std::tuple &Args) { return unpackAndRunAsyncHelper(Handler, Responder, Args, - llvm::index_sequence_for()); + std::index_sequence_for()); } // Call the given handler with the given arguments. @@ -540,14 +540,13 @@ public: // Deserialize arguments from the channel. template static Error deserializeArgs(ChannelT &C, std::tuple &Args) { - return deserializeArgsHelper(C, Args, - llvm::index_sequence_for()); + return deserializeArgsHelper(C, Args, std::index_sequence_for()); } private: template static Error deserializeArgsHelper(ChannelT &C, std::tuple &Args, - llvm::index_sequence _) { + std::index_sequence _) { return SequenceSerialization::deserialize( C, std::get(Args)...); } @@ -556,18 +555,16 @@ private: static typename WrappedHandlerReturn< typename HandlerTraits::ReturnType>::Type unpackAndRunHelper(HandlerT &Handler, ArgTuple &Args, - llvm::index_sequence) { + std::index_sequence) { return run(Handler, std::move(std::get(Args))...); } - template static typename WrappedHandlerReturn< typename HandlerTraits::ReturnType>::Type unpackAndRunAsyncHelper(HandlerT &Handler, ResponderT &Responder, - ArgTuple &Args, - llvm::index_sequence) { + ArgTuple &Args, std::index_sequence) { return run(Handler, Responder, std::move(std::get(Args))...); } }; diff --git a/llvm/include/llvm/IR/PassManager.h b/llvm/include/llvm/IR/PassManager.h index 37fe2a5b01ad..965c01b167cc 100644 --- a/llvm/include/llvm/IR/PassManager.h +++ b/llvm/include/llvm/IR/PassManager.h @@ -418,7 +418,7 @@ template Args, - llvm::index_sequence) { + std::index_sequence) { (void)Args; return AM.template getResult(IR, std::get(Args)...); } @@ -435,7 +435,7 @@ getAnalysisResult(AnalysisManager &AM, IRUnitT &IR, std::tuple Args) { return (getAnalysisResultUnpackTuple< PassT, IRUnitT>)(AM, IR, Args, - llvm::index_sequence_for{}); + std::index_sequence_for{}); } } // namespace detail diff --git a/llvm/include/llvm/Support/Format.h b/llvm/include/llvm/Support/Format.h index 77dcbaebf1a3..9dd7b401b46a 100644 --- a/llvm/include/llvm/Support/Format.h +++ b/llvm/include/llvm/Support/Format.h @@ -29,6 +29,7 @@ #include #include #include +#include namespace llvm { @@ -91,7 +92,7 @@ class format_object final : public format_object_base { template int snprint_tuple(char *Buffer, unsigned BufferSize, - index_sequence) const { + std::index_sequence) const { #ifdef _MSC_VER return _snprintf(Buffer, BufferSize, Fmt, std::get(Vals)...); #else @@ -106,7 +107,7 @@ public: } int snprint(char *Buffer, unsigned BufferSize) const override { - return snprint_tuple(Buffer, BufferSize, index_sequence_for()); + return snprint_tuple(Buffer, BufferSize, std::index_sequence_for()); } };