From 3ebfc88637bc34dc6ae292057203b31133b376c6 Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Sun, 18 Dec 2022 19:54:42 +0300 Subject: [PATCH] [NFC][llvm-exegesis] Improve `getOpcodesOrDie()` We already have opcode name -> opcode index map, use it. Reserve memory where appropriate. --- llvm/tools/llvm-exegesis/llvm-exegesis.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/llvm/tools/llvm-exegesis/llvm-exegesis.cpp b/llvm/tools/llvm-exegesis/llvm-exegesis.cpp index 6a9e475..d13abd1 100644 --- a/llvm/tools/llvm-exegesis/llvm-exegesis.cpp +++ b/llvm/tools/llvm-exegesis/llvm-exegesis.cpp @@ -240,7 +240,7 @@ T ExitOnFileError(const Twine &FileName, Expected &&E) { // Checks that only one of OpcodeNames, OpcodeIndex or SnippetsFile is provided, // and returns the opcode indices or {} if snippets should be read from // `SnippetsFile`. -static std::vector getOpcodesOrDie(const MCInstrInfo &MCInstrInfo) { +static std::vector getOpcodesOrDie(const LLVMState &State) { const size_t NumSetFlags = (OpcodeNames.empty() ? 0 : 1) + (OpcodeIndex == 0 ? 0 : 1) + (SnippetsFile.empty() ? 0 : 1); @@ -255,21 +255,25 @@ static std::vector getOpcodesOrDie(const MCInstrInfo &MCInstrInfo) { return {static_cast(OpcodeIndex)}; if (OpcodeIndex < 0) { std::vector Result; - for (unsigned I = 1, E = MCInstrInfo.getNumOpcodes(); I < E; ++I) + unsigned NumOpcodes = State.getInstrInfo().getNumOpcodes(); + Result.reserve(NumOpcodes); + for (unsigned I = 0, E = NumOpcodes; I < E; ++I) Result.push_back(I); return Result; } // Resolve opcode name -> opcode. - const auto ResolveName = [&MCInstrInfo](StringRef OpcodeName) -> unsigned { - for (unsigned I = 1, E = MCInstrInfo.getNumOpcodes(); I < E; ++I) - if (MCInstrInfo.getName(I) == OpcodeName) - return I; + const auto ResolveName = [&State](StringRef OpcodeName) -> unsigned { + const auto &Map = State.getOpcodeNameToOpcodeIdxMapping(); + auto I = Map.find(OpcodeName); + if (I != Map.end()) + return I->getSecond(); return 0u; }; SmallVector Pieces; StringRef(OpcodeNames.getValue()) .split(Pieces, ",", /* MaxSplit */ -1, /* KeepEmpty */ false); std::vector Result; + Result.reserve(Pieces.size()); for (const StringRef &OpcodeName : Pieces) { if (unsigned Opcode = ResolveName(OpcodeName)) Result.push_back(Opcode); @@ -415,7 +419,7 @@ void benchmarkMain() { ExitWithError("cannot create benchmark runner"); } - const auto Opcodes = getOpcodesOrDie(State.getInstrInfo()); + const auto Opcodes = getOpcodesOrDie(State); SmallVector, 2> Repetitors; if (RepetitionMode != InstructionBenchmark::RepetitionModeE::AggregateMin) -- 2.7.4