Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 1 | //===-- Assembler.cpp -------------------------------------------*- C++ -*-===// |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 9 | #include "Assembler.h" |
| 10 | |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 11 | #include "SnippetRepetitor.h" |
Clement Courbet | 6fd00e3 | 2018-06-20 11:54:35 +0000 | [diff] [blame] | 12 | #include "Target.h" |
Clement Courbet | 559d1e3 | 2018-05-15 07:40:21 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/GlobalISel/CallLowering.h" |
| 14 | #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 16 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 17 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 18 | #include "llvm/CodeGen/TargetInstrInfo.h" |
| 19 | #include "llvm/CodeGen/TargetPassConfig.h" |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 21 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 22 | #include "llvm/IR/LegacyPassManager.h" |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCInstrInfo.h" |
Clement Courbet | 04a9a0e | 2019-10-09 14:25:08 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Alignment.h" |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 26 | |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 27 | namespace llvm { |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 28 | namespace exegesis { |
| 29 | |
| 30 | static constexpr const char ModuleID[] = "ExegesisInfoTest"; |
| 31 | static constexpr const char FunctionID[] = "foo"; |
Clement Courbet | 04a9a0e | 2019-10-09 14:25:08 +0000 | [diff] [blame] | 32 | static const Align kFunctionAlignment(4096); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 33 | |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 34 | // Fills the given basic block with register setup code, and returns true if |
| 35 | // all registers could be setup correctly. |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 36 | static bool generateSnippetSetupCode( |
| 37 | const ExegesisTarget &ET, const MCSubtargetInfo *const MSI, |
| 38 | ArrayRef<RegisterValue> RegisterInitialValues, BasicBlockFiller &BBF) { |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 39 | bool IsSnippetSetupComplete = true; |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 40 | for (const RegisterValue &RV : RegisterInitialValues) { |
Simon Pilgrim | f652ef3 | 2018-09-18 15:38:16 +0000 | [diff] [blame] | 41 | // Load a constant in the register. |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 42 | const auto SetRegisterCode = ET.setRegTo(*MSI, RV.Register, RV.Value); |
| 43 | if (SetRegisterCode.empty()) |
| 44 | IsSnippetSetupComplete = false; |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 45 | BBF.addInstructions(SetRegisterCode); |
Simon Pilgrim | f652ef3 | 2018-09-18 15:38:16 +0000 | [diff] [blame] | 46 | } |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 47 | return IsSnippetSetupComplete; |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 50 | // Small utility function to add named passes. |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 51 | static bool addPass(PassManagerBase &PM, StringRef PassName, |
| 52 | TargetPassConfig &TPC) { |
| 53 | const PassRegistry *PR = PassRegistry::getPassRegistry(); |
| 54 | const PassInfo *PI = PR->getPassInfo(PassName); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 55 | if (!PI) { |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 56 | errs() << " run-pass " << PassName << " is not registered.\n"; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 57 | return true; |
| 58 | } |
| 59 | |
| 60 | if (!PI->getNormalCtor()) { |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 61 | errs() << " cannot create pass: " << PI->getPassName() << "\n"; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 62 | return true; |
| 63 | } |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 64 | Pass *P = PI->getNormalCtor()(); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 65 | std::string Banner = std::string("After ") + std::string(P->getPassName()); |
| 66 | PM.add(P); |
| 67 | TPC.printAndVerify(Banner); |
| 68 | |
| 69 | return false; |
| 70 | } |
| 71 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 72 | MachineFunction &createVoidVoidPtrMachineFunction(StringRef FunctionID, |
| 73 | Module *Module, |
| 74 | MachineModuleInfo *MMI) { |
| 75 | Type *const ReturnType = Type::getInt32Ty(Module->getContext()); |
| 76 | Type *const MemParamType = PointerType::get( |
| 77 | Type::getInt8Ty(Module->getContext()), 0 /*default address space*/); |
| 78 | FunctionType *FunctionType = |
| 79 | FunctionType::get(ReturnType, {MemParamType}, false); |
| 80 | Function *const F = Function::Create( |
| 81 | FunctionType, GlobalValue::InternalLinkage, FunctionID, Module); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 82 | // Making sure we can create a MachineFunction out of this Function even if it |
| 83 | // contains no IR. |
| 84 | F->setIsMaterializable(true); |
| 85 | return MMI->getOrCreateMachineFunction(*F); |
| 86 | } |
| 87 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 88 | BasicBlockFiller::BasicBlockFiller(MachineFunction &MF, MachineBasicBlock *MBB, |
| 89 | const MCInstrInfo *MCII) |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 90 | : MF(MF), MBB(MBB), MCII(MCII) {} |
| 91 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 92 | void BasicBlockFiller::addInstruction(const MCInst &Inst, const DebugLoc &DL) { |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 93 | const unsigned Opcode = Inst.getOpcode(); |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 94 | const MCInstrDesc &MCID = MCII->get(Opcode); |
| 95 | MachineInstrBuilder Builder = BuildMI(MBB, DL, MCID); |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 96 | for (unsigned OpIndex = 0, E = Inst.getNumOperands(); OpIndex < E; |
| 97 | ++OpIndex) { |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 98 | const MCOperand &Op = Inst.getOperand(OpIndex); |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 99 | if (Op.isReg()) { |
| 100 | const bool IsDef = OpIndex < MCID.getNumDefs(); |
| 101 | unsigned Flags = 0; |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 102 | const MCOperandInfo &OpInfo = MCID.operands().begin()[OpIndex]; |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 103 | if (IsDef && !OpInfo.isOptionalDef()) |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 104 | Flags |= RegState::Define; |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 105 | Builder.addReg(Op.getReg(), Flags); |
| 106 | } else if (Op.isImm()) { |
| 107 | Builder.addImm(Op.getImm()); |
| 108 | } else if (!Op.isValid()) { |
| 109 | llvm_unreachable("Operand is not set"); |
| 110 | } else { |
| 111 | llvm_unreachable("Not yet implemented"); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 116 | void BasicBlockFiller::addInstructions(ArrayRef<MCInst> Insts, |
| 117 | const DebugLoc &DL) { |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 118 | for (const MCInst &Inst : Insts) |
| 119 | addInstruction(Inst, DL); |
| 120 | } |
| 121 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 122 | void BasicBlockFiller::addReturn(const DebugLoc &DL) { |
Clement Courbet | 559d1e3 | 2018-05-15 07:40:21 +0000 | [diff] [blame] | 123 | // Insert the return code. |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 124 | const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); |
Clement Courbet | 559d1e3 | 2018-05-15 07:40:21 +0000 | [diff] [blame] | 125 | if (TII->getReturnOpcode() < TII->getNumOpcodes()) { |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 126 | BuildMI(MBB, DL, TII->get(TII->getReturnOpcode())); |
Clement Courbet | 559d1e3 | 2018-05-15 07:40:21 +0000 | [diff] [blame] | 127 | } else { |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 128 | MachineIRBuilder MIB(MF); |
Clement Courbet | 559d1e3 | 2018-05-15 07:40:21 +0000 | [diff] [blame] | 129 | MIB.setMBB(*MBB); |
John Brawn | c616a72 | 2018-10-10 13:03:23 +0000 | [diff] [blame] | 130 | MF.getSubtarget().getCallLowering()->lowerReturn(MIB, nullptr, {}); |
Clement Courbet | 559d1e3 | 2018-05-15 07:40:21 +0000 | [diff] [blame] | 131 | } |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 134 | FunctionFiller::FunctionFiller(MachineFunction &MF, |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 135 | std::vector<unsigned> RegistersSetUp) |
| 136 | : MF(MF), MCII(MF.getTarget().getMCInstrInfo()), Entry(addBasicBlock()), |
| 137 | RegistersSetUp(std::move(RegistersSetUp)) {} |
| 138 | |
| 139 | BasicBlockFiller FunctionFiller::addBasicBlock() { |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 140 | MachineBasicBlock *MBB = MF.CreateMachineBasicBlock(); |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 141 | MF.push_back(MBB); |
| 142 | return BasicBlockFiller(MF, MBB, MCII); |
| 143 | } |
| 144 | |
| 145 | ArrayRef<unsigned> FunctionFiller::getRegistersSetUp() const { |
| 146 | return RegistersSetUp; |
| 147 | } |
| 148 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 149 | static std::unique_ptr<Module> |
| 150 | createModule(const std::unique_ptr<LLVMContext> &Context, const DataLayout DL) { |
| 151 | auto Mod = std::make_unique<Module>(ModuleID, *Context); |
| 152 | Mod->setDataLayout(DL); |
| 153 | return Mod; |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 156 | BitVector getFunctionReservedRegs(const TargetMachine &TM) { |
| 157 | std::unique_ptr<LLVMContext> Context = std::make_unique<LLVMContext>(); |
| 158 | std::unique_ptr<Module> Module = createModule(Context, TM.createDataLayout()); |
Matthias Braun | 3d849f6 | 2018-11-05 23:49:13 +0000 | [diff] [blame] | 159 | // TODO: This only works for targets implementing LLVMTargetMachine. |
Yuanfang Chen | cc382cf | 2019-09-30 17:54:50 +0000 | [diff] [blame] | 160 | const LLVMTargetMachine &LLVMTM = static_cast<const LLVMTargetMachine &>(TM); |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 161 | std::unique_ptr<MachineModuleInfoWrapperPass> MMIWP = |
| 162 | std::make_unique<MachineModuleInfoWrapperPass>(&LLVMTM); |
| 163 | MachineFunction &MF = createVoidVoidPtrMachineFunction( |
Yuanfang Chen | cc382cf | 2019-09-30 17:54:50 +0000 | [diff] [blame] | 164 | FunctionID, Module.get(), &MMIWP.get()->getMMI()); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 165 | // Saving reserved registers for client. |
| 166 | return MF.getSubtarget().getRegisterInfo()->getReservedRegs(MF); |
| 167 | } |
| 168 | |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 169 | void assembleToStream(const ExegesisTarget &ET, |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 170 | std::unique_ptr<LLVMTargetMachine> TM, |
| 171 | ArrayRef<unsigned> LiveIns, |
| 172 | ArrayRef<RegisterValue> RegisterInitialValues, |
| 173 | const FillFunction &Fill, raw_pwrite_stream &AsmStream) { |
Clement Courbet | 04a9a0e | 2019-10-09 14:25:08 +0000 | [diff] [blame] | 174 | auto Context = std::make_unique<LLVMContext>(); |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 175 | std::unique_ptr<Module> Module = |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 176 | createModule(Context, TM->createDataLayout()); |
Clement Courbet | 04a9a0e | 2019-10-09 14:25:08 +0000 | [diff] [blame] | 177 | auto MMIWP = std::make_unique<MachineModuleInfoWrapperPass>(TM.get()); |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 178 | MachineFunction &MF = createVoidVoidPtrMachineFunction( |
Yuanfang Chen | cc382cf | 2019-09-30 17:54:50 +0000 | [diff] [blame] | 179 | FunctionID, Module.get(), &MMIWP.get()->getMMI()); |
Clement Courbet | 04a9a0e | 2019-10-09 14:25:08 +0000 | [diff] [blame] | 180 | MF.ensureAlignment(kFunctionAlignment); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 181 | |
| 182 | // We need to instruct the passes that we're done with SSA and virtual |
| 183 | // registers. |
| 184 | auto &Properties = MF.getProperties(); |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 185 | Properties.set(MachineFunctionProperties::Property::NoVRegs); |
| 186 | Properties.reset(MachineFunctionProperties::Property::IsSSA); |
| 187 | Properties.set(MachineFunctionProperties::Property::NoPHIs); |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 188 | |
| 189 | for (const unsigned Reg : LiveIns) |
| 190 | MF.getRegInfo().addLiveIn(Reg); |
| 191 | |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 192 | std::vector<unsigned> RegistersSetUp; |
| 193 | for (const auto &InitValue : RegisterInitialValues) { |
| 194 | RegistersSetUp.push_back(InitValue.Register); |
| 195 | } |
| 196 | FunctionFiller Sink(MF, std::move(RegistersSetUp)); |
| 197 | auto Entry = Sink.getEntry(); |
| 198 | for (const unsigned Reg : LiveIns) |
| 199 | Entry.MBB->addLiveIn(Reg); |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 200 | |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 201 | const bool IsSnippetSetupComplete = generateSnippetSetupCode( |
| 202 | ET, TM->getMCSubtargetInfo(), RegisterInitialValues, Entry); |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 203 | |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 204 | // If the snippet setup is not complete, we disable liveliness tracking. This |
| 205 | // means that we won't know what values are in the registers. |
| 206 | if (!IsSnippetSetupComplete) |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 207 | Properties.reset(MachineFunctionProperties::Property::TracksLiveness); |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 208 | |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 209 | Fill(Sink); |
| 210 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 211 | // prologue/epilogue pass needs the reserved registers to be frozen, this |
| 212 | // is usually done by the SelectionDAGISel pass. |
| 213 | MF.getRegInfo().freezeReservedRegs(MF); |
| 214 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 215 | // We create the pass manager, run the passes to populate AsmBuffer. |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 216 | MCContext &MCContext = MMIWP->getMMI().getContext(); |
| 217 | legacy::PassManager PM; |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 218 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 219 | TargetLibraryInfoImpl TLII(Triple(Module->getTargetTriple())); |
| 220 | PM.add(new TargetLibraryInfoWrapperPass(TLII)); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 221 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 222 | TargetPassConfig *TPC = TM->createPassConfig(PM); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 223 | PM.add(TPC); |
Yuanfang Chen | cc382cf | 2019-09-30 17:54:50 +0000 | [diff] [blame] | 224 | PM.add(MMIWP.release()); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 225 | TPC->printAndVerify("MachineFunctionGenerator::assemble"); |
Clement Courbet | 6fd00e3 | 2018-06-20 11:54:35 +0000 | [diff] [blame] | 226 | // Add target-specific passes. |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 227 | ET.addTargetSpecificPasses(PM); |
| 228 | TPC->printAndVerify("After ExegesisTarget::addTargetSpecificPasses"); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 229 | // Adding the following passes: |
Simon Atanasyan | cf1ba23 | 2019-10-11 20:26:08 +0000 | [diff] [blame^] | 230 | // - postrapseudos: expands pseudo return instructions used on some targets. |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 231 | // - machineverifier: checks that the MachineFunction is well formed. |
| 232 | // - prologepilog: saves and restore callee saved registers. |
Simon Atanasyan | cf1ba23 | 2019-10-11 20:26:08 +0000 | [diff] [blame^] | 233 | for (const char *PassName : |
| 234 | {"postrapseudos", "machineverifier", "prologepilog"}) |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 235 | if (addPass(PM, PassName, *TPC)) |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 236 | report_fatal_error("Unable to add a mandatory pass"); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 237 | TPC->setInitialized(); |
| 238 | |
| 239 | // AsmPrinter is responsible for generating the assembly into AsmBuffer. |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 240 | if (TM->addAsmPrinter(PM, AsmStream, nullptr, TargetMachine::CGFT_ObjectFile, |
| 241 | MCContext)) |
| 242 | report_fatal_error("Cannot add AsmPrinter passes"); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 243 | |
| 244 | PM.run(*Module); // Run all the passes |
| 245 | } |
| 246 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 247 | object::OwningBinary<object::ObjectFile> |
| 248 | getObjectFromBuffer(StringRef InputData) { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 249 | // Storing the generated assembly into a MemoryBuffer that owns the memory. |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 250 | std::unique_ptr<MemoryBuffer> Buffer = |
| 251 | MemoryBuffer::getMemBufferCopy(InputData); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 252 | // Create the ObjectFile from the MemoryBuffer. |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 253 | std::unique_ptr<object::ObjectFile> Obj = |
| 254 | cantFail(object::ObjectFile::createObjectFile(Buffer->getMemBufferRef())); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 255 | // Returning both the MemoryBuffer and the ObjectFile. |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 256 | return object::OwningBinary<object::ObjectFile>(std::move(Obj), |
| 257 | std::move(Buffer)); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 260 | object::OwningBinary<object::ObjectFile> getObjectFromFile(StringRef Filename) { |
| 261 | return cantFail(object::ObjectFile::createObjectFile(Filename)); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 264 | namespace { |
| 265 | |
| 266 | // Implementation of this class relies on the fact that a single object with a |
| 267 | // single function will be loaded into memory. |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 268 | class TrackingSectionMemoryManager : public SectionMemoryManager { |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 269 | public: |
| 270 | explicit TrackingSectionMemoryManager(uintptr_t *CodeSize) |
| 271 | : CodeSize(CodeSize) {} |
| 272 | |
| 273 | uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, |
| 274 | unsigned SectionID, |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 275 | StringRef SectionName) override { |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 276 | *CodeSize = Size; |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 277 | return SectionMemoryManager::allocateCodeSection(Size, Alignment, SectionID, |
| 278 | SectionName); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | private: |
| 282 | uintptr_t *const CodeSize = nullptr; |
| 283 | }; |
| 284 | |
| 285 | } // namespace |
| 286 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 287 | ExecutableFunction::ExecutableFunction( |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 288 | std::unique_ptr<LLVMTargetMachine> TM, |
| 289 | object::OwningBinary<object::ObjectFile> &&ObjectFileHolder) |
| 290 | : Context(std::make_unique<LLVMContext>()) { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 291 | assert(ObjectFileHolder.getBinary() && "cannot create object file"); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 292 | // Initializing the execution engine. |
| 293 | // We need to use the JIT EngineKind to be able to add an object file. |
| 294 | LLVMLinkInMCJIT(); |
| 295 | uintptr_t CodeSize = 0; |
| 296 | std::string Error; |
| 297 | ExecEngine.reset( |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 298 | EngineBuilder(createModule(Context, TM->createDataLayout())) |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 299 | .setErrorStr(&Error) |
Simon Pilgrim | 01e8c4e | 2018-04-18 14:22:33 +0000 | [diff] [blame] | 300 | .setMCPU(TM->getTargetCPU()) |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 301 | .setEngineKind(EngineKind::JIT) |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 302 | .setMCJITMemoryManager( |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 303 | std::make_unique<TrackingSectionMemoryManager>(&CodeSize)) |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 304 | .create(TM.release())); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 305 | if (!ExecEngine) |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 306 | report_fatal_error(Error); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 307 | // Adding the generated object file containing the assembled function. |
| 308 | // The ExecutionEngine makes sure the object file is copied into an |
| 309 | // executable page. |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 310 | ExecEngine->addObjectFile(std::move(ObjectFileHolder)); |
| 311 | // Fetching function bytes. |
Clement Courbet | 04a9a0e | 2019-10-09 14:25:08 +0000 | [diff] [blame] | 312 | const uint64_t FunctionAddress = ExecEngine->getFunctionAddress(FunctionID); |
| 313 | assert(isAligned(kFunctionAlignment, FunctionAddress) && |
| 314 | "function is not properly aligned"); |
| 315 | FunctionBytes = |
| 316 | StringRef(reinterpret_cast<const char *>(FunctionAddress), CodeSize); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | } // namespace exegesis |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 320 | } // namespace llvm |