Index: tools/llvm-exegesis/lib/BenchmarkResult.cpp =================================================================== --- tools/llvm-exegesis/lib/BenchmarkResult.cpp +++ tools/llvm-exegesis/lib/BenchmarkResult.cpp @@ -9,8 +9,9 @@ #include "BenchmarkResult.h" #include "BenchmarkRunner.h" #include "llvm/ADT/STLExtras.h" -#include "llvm/ADT/bit.h" +#include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/bit.h" #include "llvm/ObjectYAML/YAML.h" #include "llvm/Support/FileOutputBuffer.h" #include "llvm/Support/FileSystem.h" @@ -29,7 +30,17 @@ // serialization process to encode/decode registers and instructions. struct YamlContext { YamlContext(const exegesis::LLVMState &State) - : State(&State), ErrorStream(LastError) {} + : State(&State), ErrorStream(LastError), + OpcodeNameToOpcodeIdx(generateOpcodeNameToOpcodeIdxMapping()) {} + + llvm::StringMap generateOpcodeNameToOpcodeIdxMapping() { + const llvm::MCInstrInfo &InstrInfo = State->getInstrInfo(); + llvm::StringMap Map(InstrInfo.getNumOpcodes()); + for (unsigned I = 0, E = InstrInfo.getNumOpcodes(); I < E; ++I) + Map.insert(std::make_pair(InstrInfo.getName(I), I)); + assert(Map.size() == InstrInfo.getNumOpcodes() && "Size prediction failed"); + return Map; + }; void serializeMCInst(const llvm::MCInst &MCInst, llvm::raw_ostream &OS) { OS << getInstrName(MCInst.getOpcode()); @@ -136,10 +147,9 @@ } unsigned getInstrOpcode(llvm::StringRef InstrName) { - const llvm::MCInstrInfo &InstrInfo = State->getInstrInfo(); - for (unsigned E = InstrInfo.getNumOpcodes(), I = 0; I < E; ++I) - if (InstrInfo.getName(I) == InstrName) - return I; + auto Iter = OpcodeNameToOpcodeIdx.find(InstrName); + if (Iter != OpcodeNameToOpcodeIdx.end()) + return Iter->second; ErrorStream << "No opcode with name " << InstrName; return 0; } @@ -147,6 +157,7 @@ const llvm::exegesis::LLVMState *State; std::string LastError; llvm::raw_string_ostream ErrorStream; + const llvm::StringMap OpcodeNameToOpcodeIdx; }; } // namespace