Add llvm::for_each as a range-based extensions to <algorithm> and make use of it in some cases where it is a more clear alternative to std::for_each.

llvm-svn: 317356
This commit is contained in:
Aaron Ballman 2017-11-03 20:01:25 +00:00
parent 189ebb6976
commit ecf0e95267
12 changed files with 92 additions and 89 deletions

View File

@ -813,6 +813,13 @@ void DeleteContainerSeconds(Container &C) {
C.clear();
}
/// Provide wrappers to std::for_each which take ranges instead of having to
/// pass begin/end explicitly.
template <typename R, typename UnaryPredicate>
UnaryPredicate for_each(R &&Range, UnaryPredicate P) {
return std::for_each(std::begin(Range), std::end(Range), P);
}
/// Provide wrappers to std::all_of which take ranges instead of having to pass
/// begin/end explicitly.
template <typename R, typename UnaryPredicate>

View File

@ -469,17 +469,15 @@ void LTOCodeGenerator::restoreLinkageForExternals() {
if (I == ExternalSymbols.end())
return;
GV.setLinkage(I->second);
};
std::for_each(MergedModule->begin(), MergedModule->end(), externalize);
std::for_each(MergedModule->global_begin(), MergedModule->global_end(),
externalize);
std::for_each(MergedModule->alias_begin(), MergedModule->alias_end(),
externalize);
}
void LTOCodeGenerator::verifyMergedModuleOnce() {
GV.setLinkage(I->second);
};
llvm::for_each(MergedModule->functions(), externalize);
llvm::for_each(MergedModule->globals(), externalize);
llvm::for_each(MergedModule->aliases(), externalize);
}
void LTOCodeGenerator::verifyMergedModuleOnce() {
// Only run on the first call.
if (HasVerifiedInput)
return;

View File

@ -548,14 +548,13 @@ bool HexagonVectorLoopCarriedReuse::doVLCR() {
findValueToReuse();
if (ReuseCandidate.isDefined()) {
reuseValue();
Changed = true;
Continue = true;
}
std::for_each(Dependences.begin(), Dependences.end(),
std::default_delete<DepChain>());
} while (Continue);
return Changed;
}
Changed = true;
Continue = true;
}
llvm::for_each(Dependences, std::default_delete<DepChain>());
} while (Continue);
return Changed;
}
void HexagonVectorLoopCarriedReuse::findDepChainFromPHI(Instruction *I,
DepChain &D) {

View File

@ -141,15 +141,15 @@ static void findPartitions(Module *M, ClusterIDMapType &ClusterIDMap,
}
if (GV.hasLocalLinkage())
addAllGlobalValueUsers(GVtoClusterMap, &GV, &GV);
};
std::for_each(M->begin(), M->end(), recordGVSet);
std::for_each(M->global_begin(), M->global_end(), recordGVSet);
std::for_each(M->alias_begin(), M->alias_end(), recordGVSet);
// Assigned all GVs to merged clusters while balancing number of objects in
// each.
addAllGlobalValueUsers(GVtoClusterMap, &GV, &GV);
};
llvm::for_each(M->functions(), recordGVSet);
llvm::for_each(M->globals(), recordGVSet);
llvm::for_each(M->aliases(), recordGVSet);
// Assigned all GVs to merged clusters while balancing number of objects in
// each.
auto CompareClusters = [](const std::pair<unsigned, unsigned> &a,
const std::pair<unsigned, unsigned> &b) {
if (a.second || b.second)

View File

@ -546,11 +546,10 @@ int main(int argc, const char *argv[]) {
cl::ParseCommandLineOptions(argc, argv, "LLVM C++ ABI Data Dumper\n");
// Default to stdin if no filename is specified.
if (opts::InputFilenames.size() == 0)
opts::InputFilenames.push_back("-");
std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
dumpInput);
return EXIT_SUCCESS;
}
if (opts::InputFilenames.size() == 0)
opts::InputFilenames.push_back("-");
llvm::for_each(opts::InputFilenames, dumpInput);
return EXIT_SUCCESS;
}

View File

@ -217,10 +217,9 @@ int main(int argc, char **argv) {
ToolName = argv[0];
// If no input files specified, read from stdin.
if (InputFilenames.size() == 0)
InputFilenames.push_back("-");
std::for_each(InputFilenames.begin(), InputFilenames.end(),
parseMCMarkup);
return 0;
}
if (InputFilenames.size() == 0)
InputFilenames.push_back("-");
llvm::for_each(InputFilenames, parseMCMarkup);
return 0;
}

View File

@ -1977,8 +1977,7 @@ int main(int argc, char **argv) {
if (NoDyldInfo && (AddDyldInfo || DyldInfoOnly))
error("-no-dyldinfo can't be used with -add-dyldinfo or -dyldinfo-only");
std::for_each(InputFilenames.begin(), InputFilenames.end(),
dumpSymbolNamesFromFile);
llvm::for_each(InputFilenames, dumpSymbolNamesFromFile);
if (HadError)
return 1;

View File

@ -2183,11 +2183,10 @@ int main(int argc, char **argv) {
&& !PrintFaultMaps
&& DwarfDumpType == DIDT_Null) {
cl::PrintHelpMessage();
return 2;
}
std::for_each(InputFilenames.begin(), InputFilenames.end(),
DumpInput);
return EXIT_SUCCESS;
}
return 2;
}
llvm::for_each(InputFilenames, DumpInput);
return EXIT_SUCCESS;
}

View File

@ -1199,20 +1199,17 @@ int main(int argc_, const char *argv_[]) {
opts::pretty::ExcludeCompilands.push_back(
"f:\\\\binaries\\\\Intermediate\\\\vctools\\\\crt_bld");
opts::pretty::ExcludeCompilands.push_back("f:\\\\dd\\\\vctools\\\\crt");
opts::pretty::ExcludeCompilands.push_back(
"d:\\\\th.obj.x86fre\\\\minkernel");
}
std::for_each(opts::pretty::InputFilenames.begin(),
opts::pretty::InputFilenames.end(), dumpPretty);
} else if (opts::DumpSubcommand) {
std::for_each(opts::dump::InputFilenames.begin(),
opts::dump::InputFilenames.end(), dumpRaw);
} else if (opts::BytesSubcommand) {
std::for_each(opts::bytes::InputFilenames.begin(),
opts::bytes::InputFilenames.end(), dumpBytes);
} else if (opts::DiffSubcommand) {
for (StringRef S : opts::diff::RawModiEquivalences) {
StringRef Left;
opts::pretty::ExcludeCompilands.push_back(
"d:\\\\th.obj.x86fre\\\\minkernel");
}
llvm::for_each(opts::pretty::InputFilenames, dumpPretty);
} else if (opts::DumpSubcommand) {
llvm::for_each(opts::dump::InputFilenames, dumpRaw);
} else if (opts::BytesSubcommand) {
llvm::for_each(opts::bytes::InputFilenames, dumpBytes);
} else if (opts::DiffSubcommand) {
for (StringRef S : opts::diff::RawModiEquivalences) {
StringRef Left;
StringRef Right;
std::tie(Left, Right) = S.split(',');
uint32_t X, Y;

View File

@ -566,14 +566,13 @@ int main(int argc, const char *argv[]) {
cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
// Default to stdin if no filename is specified.
if (opts::InputFilenames.size() == 0)
opts::InputFilenames.push_back("-");
std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
dumpInput);
if (opts::CodeViewMergedTypes) {
ScopedPrinter W(outs());
if (opts::InputFilenames.size() == 0)
opts::InputFilenames.push_back("-");
llvm::for_each(opts::InputFilenames, dumpInput);
if (opts::CodeViewMergedTypes) {
ScopedPrinter W(outs());
dumpCodeViewMergedTypes(W, CVTypes.IDTable, CVTypes.TypeTable);
}

View File

@ -880,14 +880,13 @@ int main(int argc, char **argv) {
}
if (InputFilenames.size() == 0)
InputFilenames.push_back("a.out");
MoreThanOneFile = InputFilenames.size() > 1;
std::for_each(InputFilenames.begin(), InputFilenames.end(),
printFileSectionSizes);
if (OutputFormat == berkeley && TotalSizes)
printBerkelyTotals();
InputFilenames.push_back("a.out");
MoreThanOneFile = InputFilenames.size() > 1;
llvm::for_each(InputFilenames, printFileSectionSizes);
if (OutputFormat == berkeley && TotalSizes)
printBerkelyTotals();
if (HadError)
return 1;
}

View File

@ -252,12 +252,20 @@ TEST(STLExtrasTest, CountAdaptor) {
EXPECT_EQ(3, count(v, 1));
EXPECT_EQ(2, count(v, 2));
EXPECT_EQ(1, count(v, 3));
EXPECT_EQ(1, count(v, 4));
}
TEST(STLExtrasTest, ToVector) {
std::vector<char> v = {'a', 'b', 'c'};
auto Enumerated = to_vector<4>(enumerate(v));
EXPECT_EQ(1, count(v, 4));
}
TEST(STLExtrasTest, for_each) {
std::vector<int> v{ 0, 1, 2, 3, 4 };
int count = 0;
llvm::for_each(v, [&count](int) { ++count; });
EXPECT_EQ(5, count);
}
TEST(STLExtrasTest, ToVector) {
std::vector<char> v = {'a', 'b', 'c'};
auto Enumerated = to_vector<4>(enumerate(v));
ASSERT_EQ(3u, Enumerated.size());
for (size_t I = 0; I < v.size(); ++I) {
EXPECT_EQ(I, Enumerated[I].index());