diff options
Diffstat (limited to 'llvm/tools/llvm-exegesis/lib/Assembler.cpp')
-rw-r--r-- | llvm/tools/llvm-exegesis/lib/Assembler.cpp | 196 |
1 files changed, 92 insertions, 104 deletions
diff --git a/llvm/tools/llvm-exegesis/lib/Assembler.cpp b/llvm/tools/llvm-exegesis/lib/Assembler.cpp index 229bf36da6b..c19cac834ba 100644 --- a/llvm/tools/llvm-exegesis/lib/Assembler.cpp +++ b/llvm/tools/llvm-exegesis/lib/Assembler.cpp @@ -31,11 +31,9 @@ static constexpr const char FunctionID[] = "foo"; // Fills the given basic block with register setup code, and returns true if // all registers could be setup correctly. -static bool -generateSnippetSetupCode(const ExegesisTarget &ET, - const llvm::MCSubtargetInfo *const MSI, - llvm::ArrayRef<RegisterValue> RegisterInitialValues, - BasicBlockFiller &BBF) { +static bool generateSnippetSetupCode( + const ExegesisTarget &ET, const MCSubtargetInfo *const MSI, + ArrayRef<RegisterValue> RegisterInitialValues, BasicBlockFiller &BBF) { bool IsSnippetSetupComplete = true; for (const RegisterValue &RV : RegisterInitialValues) { // Load a constant in the register. @@ -48,20 +46,20 @@ generateSnippetSetupCode(const ExegesisTarget &ET, } // Small utility function to add named passes. -static bool addPass(llvm::PassManagerBase &PM, llvm::StringRef PassName, - llvm::TargetPassConfig &TPC) { - const llvm::PassRegistry *PR = llvm::PassRegistry::getPassRegistry(); - const llvm::PassInfo *PI = PR->getPassInfo(PassName); +static bool addPass(PassManagerBase &PM, StringRef PassName, + TargetPassConfig &TPC) { + const PassRegistry *PR = PassRegistry::getPassRegistry(); + const PassInfo *PI = PR->getPassInfo(PassName); if (!PI) { - llvm::errs() << " run-pass " << PassName << " is not registered.\n"; + errs() << " run-pass " << PassName << " is not registered.\n"; return true; } if (!PI->getNormalCtor()) { - llvm::errs() << " cannot create pass: " << PI->getPassName() << "\n"; + errs() << " cannot create pass: " << PI->getPassName() << "\n"; return true; } - llvm::Pass *P = PI->getNormalCtor()(); + Pass *P = PI->getNormalCtor()(); std::string Banner = std::string("After ") + std::string(P->getPassName()); PM.add(P); TPC.printAndVerify(Banner); @@ -69,42 +67,39 @@ static bool addPass(llvm::PassManagerBase &PM, llvm::StringRef PassName, return false; } -llvm::MachineFunction & -createVoidVoidPtrMachineFunction(llvm::StringRef FunctionID, - llvm::Module *Module, - llvm::MachineModuleInfo *MMI) { - llvm::Type *const ReturnType = llvm::Type::getInt32Ty(Module->getContext()); - llvm::Type *const MemParamType = llvm::PointerType::get( - llvm::Type::getInt8Ty(Module->getContext()), 0 /*default address space*/); - llvm::FunctionType *FunctionType = - llvm::FunctionType::get(ReturnType, {MemParamType}, false); - llvm::Function *const F = llvm::Function::Create( - FunctionType, llvm::GlobalValue::InternalLinkage, FunctionID, Module); +MachineFunction &createVoidVoidPtrMachineFunction(StringRef FunctionID, + Module *Module, + MachineModuleInfo *MMI) { + Type *const ReturnType = Type::getInt32Ty(Module->getContext()); + Type *const MemParamType = PointerType::get( + Type::getInt8Ty(Module->getContext()), 0 /*default address space*/); + FunctionType *FunctionType = + FunctionType::get(ReturnType, {MemParamType}, false); + Function *const F = Function::Create( + FunctionType, GlobalValue::InternalLinkage, FunctionID, Module); // Making sure we can create a MachineFunction out of this Function even if it // contains no IR. F->setIsMaterializable(true); return MMI->getOrCreateMachineFunction(*F); } -BasicBlockFiller::BasicBlockFiller(llvm::MachineFunction &MF, - llvm::MachineBasicBlock *MBB, - const llvm::MCInstrInfo *MCII) +BasicBlockFiller::BasicBlockFiller(MachineFunction &MF, MachineBasicBlock *MBB, + const MCInstrInfo *MCII) : MF(MF), MBB(MBB), MCII(MCII) {} -void BasicBlockFiller::addInstruction(const llvm::MCInst &Inst, - const llvm::DebugLoc &DL) { +void BasicBlockFiller::addInstruction(const MCInst &Inst, const DebugLoc &DL) { const unsigned Opcode = Inst.getOpcode(); - const llvm::MCInstrDesc &MCID = MCII->get(Opcode); - llvm::MachineInstrBuilder Builder = llvm::BuildMI(MBB, DL, MCID); + const MCInstrDesc &MCID = MCII->get(Opcode); + MachineInstrBuilder Builder = BuildMI(MBB, DL, MCID); for (unsigned OpIndex = 0, E = Inst.getNumOperands(); OpIndex < E; ++OpIndex) { - const llvm::MCOperand &Op = Inst.getOperand(OpIndex); + const MCOperand &Op = Inst.getOperand(OpIndex); if (Op.isReg()) { const bool IsDef = OpIndex < MCID.getNumDefs(); unsigned Flags = 0; - const llvm::MCOperandInfo &OpInfo = MCID.operands().begin()[OpIndex]; + const MCOperandInfo &OpInfo = MCID.operands().begin()[OpIndex]; if (IsDef && !OpInfo.isOptionalDef()) - Flags |= llvm::RegState::Define; + Flags |= RegState::Define; Builder.addReg(Op.getReg(), Flags); } else if (Op.isImm()) { Builder.addImm(Op.getImm()); @@ -116,31 +111,31 @@ void BasicBlockFiller::addInstruction(const llvm::MCInst &Inst, } } -void BasicBlockFiller::addInstructions(ArrayRef<llvm::MCInst> Insts, - const llvm::DebugLoc &DL) { +void BasicBlockFiller::addInstructions(ArrayRef<MCInst> Insts, + const DebugLoc &DL) { for (const MCInst &Inst : Insts) addInstruction(Inst, DL); } -void BasicBlockFiller::addReturn(const llvm::DebugLoc &DL) { +void BasicBlockFiller::addReturn(const DebugLoc &DL) { // Insert the return code. - const llvm::TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); + const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); if (TII->getReturnOpcode() < TII->getNumOpcodes()) { - llvm::BuildMI(MBB, DL, TII->get(TII->getReturnOpcode())); + BuildMI(MBB, DL, TII->get(TII->getReturnOpcode())); } else { - llvm::MachineIRBuilder MIB(MF); + MachineIRBuilder MIB(MF); MIB.setMBB(*MBB); MF.getSubtarget().getCallLowering()->lowerReturn(MIB, nullptr, {}); } } -FunctionFiller::FunctionFiller(llvm::MachineFunction &MF, +FunctionFiller::FunctionFiller(MachineFunction &MF, std::vector<unsigned> RegistersSetUp) : MF(MF), MCII(MF.getTarget().getMCInstrInfo()), Entry(addBasicBlock()), RegistersSetUp(std::move(RegistersSetUp)) {} BasicBlockFiller FunctionFiller::addBasicBlock() { - llvm::MachineBasicBlock *MBB = MF.CreateMachineBasicBlock(); + MachineBasicBlock *MBB = MF.CreateMachineBasicBlock(); MF.push_back(MBB); return BasicBlockFiller(MF, MBB, MCII); } @@ -149,50 +144,45 @@ ArrayRef<unsigned> FunctionFiller::getRegistersSetUp() const { return RegistersSetUp; } -static std::unique_ptr<llvm::Module> -createModule(const std::unique_ptr<llvm::LLVMContext> &Context, - const llvm::DataLayout DL) { - auto Module = std::make_unique<llvm::Module>(ModuleID, *Context); - Module->setDataLayout(DL); - return Module; +static std::unique_ptr<Module> +createModule(const std::unique_ptr<LLVMContext> &Context, const DataLayout DL) { + auto Mod = std::make_unique<Module>(ModuleID, *Context); + Mod->setDataLayout(DL); + return Mod; } -llvm::BitVector getFunctionReservedRegs(const llvm::TargetMachine &TM) { - std::unique_ptr<llvm::LLVMContext> Context = - std::make_unique<llvm::LLVMContext>(); - std::unique_ptr<llvm::Module> Module = - createModule(Context, TM.createDataLayout()); +BitVector getFunctionReservedRegs(const TargetMachine &TM) { + std::unique_ptr<LLVMContext> Context = std::make_unique<LLVMContext>(); + std::unique_ptr<Module> Module = createModule(Context, TM.createDataLayout()); // TODO: This only works for targets implementing LLVMTargetMachine. const LLVMTargetMachine &LLVMTM = static_cast<const LLVMTargetMachine &>(TM); - std::unique_ptr<llvm::MachineModuleInfoWrapperPass> MMIWP = - std::make_unique<llvm::MachineModuleInfoWrapperPass>(&LLVMTM); - llvm::MachineFunction &MF = createVoidVoidPtrMachineFunction( + std::unique_ptr<MachineModuleInfoWrapperPass> MMIWP = + std::make_unique<MachineModuleInfoWrapperPass>(&LLVMTM); + MachineFunction &MF = createVoidVoidPtrMachineFunction( FunctionID, Module.get(), &MMIWP.get()->getMMI()); // Saving reserved registers for client. return MF.getSubtarget().getRegisterInfo()->getReservedRegs(MF); } void assembleToStream(const ExegesisTarget &ET, - std::unique_ptr<llvm::LLVMTargetMachine> TM, - llvm::ArrayRef<unsigned> LiveIns, - llvm::ArrayRef<RegisterValue> RegisterInitialValues, - const FillFunction &Fill, - llvm::raw_pwrite_stream &AsmStream) { - std::unique_ptr<llvm::LLVMContext> Context = - std::make_unique<llvm::LLVMContext>(); - std::unique_ptr<llvm::Module> Module = + std::unique_ptr<LLVMTargetMachine> TM, + ArrayRef<unsigned> LiveIns, + ArrayRef<RegisterValue> RegisterInitialValues, + const FillFunction &Fill, raw_pwrite_stream &AsmStream) { + std::unique_ptr<LLVMContext> Context = std::make_unique<LLVMContext>(); + std::unique_ptr<Module> Module = createModule(Context, TM->createDataLayout()); - std::unique_ptr<llvm::MachineModuleInfoWrapperPass> MMIWP = - std::make_unique<llvm::MachineModuleInfoWrapperPass>(TM.get()); - llvm::MachineFunction &MF = createVoidVoidPtrMachineFunction( + std::unique_ptr<MachineModuleInfoWrapperPass> MMIWP = + std::make_unique<MachineModuleInfoWrapperPass>(TM.get()); + MachineFunction &MF = createVoidVoidPtrMachineFunction( FunctionID, Module.get(), &MMIWP.get()->getMMI()); // We need to instruct the passes that we're done with SSA and virtual // registers. auto &Properties = MF.getProperties(); - Properties.set(llvm::MachineFunctionProperties::Property::NoVRegs); - Properties.reset(llvm::MachineFunctionProperties::Property::IsSSA); - Properties.set(llvm::MachineFunctionProperties::Property::NoPHIs); + Properties.set(MachineFunctionProperties::Property::NoVRegs); + Properties.reset(MachineFunctionProperties::Property::IsSSA); + Properties.set(MachineFunctionProperties::Property::NoPHIs); for (const unsigned Reg : LiveIns) MF.getRegInfo().addLiveIn(Reg); @@ -212,7 +202,7 @@ void assembleToStream(const ExegesisTarget &ET, // If the snippet setup is not complete, we disable liveliness tracking. This // means that we won't know what values are in the registers. if (!IsSnippetSetupComplete) - Properties.reset(llvm::MachineFunctionProperties::Property::TracksLiveness); + Properties.reset(MachineFunctionProperties::Property::TracksLiveness); Fill(Sink); @@ -221,13 +211,13 @@ void assembleToStream(const ExegesisTarget &ET, MF.getRegInfo().freezeReservedRegs(MF); // We create the pass manager, run the passes to populate AsmBuffer. - llvm::MCContext &MCContext = MMIWP->getMMI().getContext(); - llvm::legacy::PassManager PM; + MCContext &MCContext = MMIWP->getMMI().getContext(); + legacy::PassManager PM; - llvm::TargetLibraryInfoImpl TLII(llvm::Triple(Module->getTargetTriple())); - PM.add(new llvm::TargetLibraryInfoWrapperPass(TLII)); + TargetLibraryInfoImpl TLII(Triple(Module->getTargetTriple())); + PM.add(new TargetLibraryInfoWrapperPass(TLII)); - llvm::TargetPassConfig *TPC = TM->createPassConfig(PM); + TargetPassConfig *TPC = TM->createPassConfig(PM); PM.add(TPC); PM.add(MMIWP.release()); TPC->printAndVerify("MachineFunctionGenerator::assemble"); @@ -239,50 +229,49 @@ void assembleToStream(const ExegesisTarget &ET, // - prologepilog: saves and restore callee saved registers. for (const char *PassName : {"machineverifier", "prologepilog"}) if (addPass(PM, PassName, *TPC)) - llvm::report_fatal_error("Unable to add a mandatory pass"); + report_fatal_error("Unable to add a mandatory pass"); TPC->setInitialized(); // AsmPrinter is responsible for generating the assembly into AsmBuffer. - if (TM->addAsmPrinter(PM, AsmStream, nullptr, - llvm::TargetMachine::CGFT_ObjectFile, MCContext)) - llvm::report_fatal_error("Cannot add AsmPrinter passes"); + if (TM->addAsmPrinter(PM, AsmStream, nullptr, TargetMachine::CGFT_ObjectFile, + MCContext)) + report_fatal_error("Cannot add AsmPrinter passes"); PM.run(*Module); // Run all the passes } -llvm::object::OwningBinary<llvm::object::ObjectFile> -getObjectFromBuffer(llvm::StringRef InputData) { +object::OwningBinary<object::ObjectFile> +getObjectFromBuffer(StringRef InputData) { // Storing the generated assembly into a MemoryBuffer that owns the memory. - std::unique_ptr<llvm::MemoryBuffer> Buffer = - llvm::MemoryBuffer::getMemBufferCopy(InputData); + std::unique_ptr<MemoryBuffer> Buffer = + MemoryBuffer::getMemBufferCopy(InputData); // Create the ObjectFile from the MemoryBuffer. - std::unique_ptr<llvm::object::ObjectFile> Obj = llvm::cantFail( - llvm::object::ObjectFile::createObjectFile(Buffer->getMemBufferRef())); + std::unique_ptr<object::ObjectFile> Obj = + cantFail(object::ObjectFile::createObjectFile(Buffer->getMemBufferRef())); // Returning both the MemoryBuffer and the ObjectFile. - return llvm::object::OwningBinary<llvm::object::ObjectFile>( - std::move(Obj), std::move(Buffer)); + return object::OwningBinary<object::ObjectFile>(std::move(Obj), + std::move(Buffer)); } -llvm::object::OwningBinary<llvm::object::ObjectFile> -getObjectFromFile(llvm::StringRef Filename) { - return llvm::cantFail(llvm::object::ObjectFile::createObjectFile(Filename)); +object::OwningBinary<object::ObjectFile> getObjectFromFile(StringRef Filename) { + return cantFail(object::ObjectFile::createObjectFile(Filename)); } namespace { // Implementation of this class relies on the fact that a single object with a // single function will be loaded into memory. -class TrackingSectionMemoryManager : public llvm::SectionMemoryManager { +class TrackingSectionMemoryManager : public SectionMemoryManager { public: explicit TrackingSectionMemoryManager(uintptr_t *CodeSize) : CodeSize(CodeSize) {} uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID, - llvm::StringRef SectionName) override { + StringRef SectionName) override { *CodeSize = Size; - return llvm::SectionMemoryManager::allocateCodeSection( - Size, Alignment, SectionID, SectionName); + return SectionMemoryManager::allocateCodeSection(Size, Alignment, SectionID, + SectionName); } private: @@ -292,9 +281,9 @@ private: } // namespace ExecutableFunction::ExecutableFunction( - std::unique_ptr<llvm::LLVMTargetMachine> TM, - llvm::object::OwningBinary<llvm::object::ObjectFile> &&ObjectFileHolder) - : Context(std::make_unique<llvm::LLVMContext>()) { + std::unique_ptr<LLVMTargetMachine> TM, + object::OwningBinary<object::ObjectFile> &&ObjectFileHolder) + : Context(std::make_unique<LLVMContext>()) { assert(ObjectFileHolder.getBinary() && "cannot create object file"); // Initializing the execution engine. // We need to use the JIT EngineKind to be able to add an object file. @@ -302,24 +291,23 @@ ExecutableFunction::ExecutableFunction( uintptr_t CodeSize = 0; std::string Error; ExecEngine.reset( - llvm::EngineBuilder(createModule(Context, TM->createDataLayout())) + EngineBuilder(createModule(Context, TM->createDataLayout())) .setErrorStr(&Error) .setMCPU(TM->getTargetCPU()) - .setEngineKind(llvm::EngineKind::JIT) + .setEngineKind(EngineKind::JIT) .setMCJITMemoryManager( std::make_unique<TrackingSectionMemoryManager>(&CodeSize)) .create(TM.release())); if (!ExecEngine) - llvm::report_fatal_error(Error); + report_fatal_error(Error); // Adding the generated object file containing the assembled function. // The ExecutionEngine makes sure the object file is copied into an // executable page. ExecEngine->addObjectFile(std::move(ObjectFileHolder)); // Fetching function bytes. - FunctionBytes = - llvm::StringRef(reinterpret_cast<const char *>( - ExecEngine->getFunctionAddress(FunctionID)), - CodeSize); + FunctionBytes = StringRef(reinterpret_cast<const char *>( + ExecEngine->getFunctionAddress(FunctionID)), + CodeSize); } } // namespace exegesis |