Index: llvm/trunk/tools/llvm-exegesis/lib/BenchmarkResult.cpp =================================================================== --- llvm/trunk/tools/llvm-exegesis/lib/BenchmarkResult.cpp +++ llvm/trunk/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,18 @@ // 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(State.getInstrInfo())) {} + + static llvm::StringMap + generateOpcodeNameToOpcodeIdxMapping(const llvm::MCInstrInfo &InstrInfo) { + llvm::StringMap Map(InstrInfo.getNumOpcodes()); + for (unsigned I = 0, E = InstrInfo.getNumOpcodes(); I < E; ++I) + Map[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 +148,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 +158,7 @@ const llvm::exegesis::LLVMState *State; std::string LastError; llvm::raw_string_ostream ErrorStream; + const llvm::StringMap OpcodeNameToOpcodeIdx; }; } // namespace