[RISCV] Eliminate dead li after emitting VSETVLIs (#65934)
[llvm-project.git] / llvm / tools / llvm-exegesis / lib / Assembler.h
blobabc5aa7be8cfeef65be0274ff9f837c9c1455a63
1 //===-- Assembler.h ---------------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// Defines classes to assemble functions composed of a single basic block of
11 /// MCInsts.
12 ///
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H
16 #define LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H
18 #include <memory>
20 #include "BenchmarkCode.h"
21 #include "Error.h"
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/ADT/BitVector.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
27 #include "llvm/IR/LLVMContext.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/MC/MCInst.h"
30 #include "llvm/Object/Binary.h"
31 #include "llvm/Object/ObjectFile.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/Target/TargetMachine.h"
35 namespace llvm {
36 namespace exegesis {
38 class ExegesisTarget;
40 // Gather the set of reserved registers (depends on function's calling
41 // convention and target machine).
42 BitVector getFunctionReservedRegs(const TargetMachine &TM);
44 // Helper to fill in a basic block.
45 class BasicBlockFiller {
46 public:
47 BasicBlockFiller(MachineFunction &MF, MachineBasicBlock *MBB,
48 const MCInstrInfo *MCII);
50 void addInstruction(const MCInst &Inst, const DebugLoc &DL = DebugLoc());
51 void addInstructions(ArrayRef<MCInst> Insts, const DebugLoc &DL = DebugLoc());
53 void addReturn(const ExegesisTarget &ET, bool SubprocessCleanup,
54 const DebugLoc &DL = DebugLoc());
56 MachineFunction &MF;
57 MachineBasicBlock *const MBB;
58 const MCInstrInfo *const MCII;
61 // Helper to fill in a function.
62 class FunctionFiller {
63 public:
64 FunctionFiller(MachineFunction &MF, std::vector<unsigned> RegistersSetUp);
66 // Adds a basic block to the function.
67 BasicBlockFiller addBasicBlock();
69 // Returns the function entry point.
70 BasicBlockFiller getEntry() { return Entry; }
72 MachineFunction &MF;
73 const MCInstrInfo *const MCII;
75 // Returns the set of registers in the snippet setup code.
76 ArrayRef<unsigned> getRegistersSetUp() const;
78 private:
79 BasicBlockFiller Entry;
80 // The set of registers that are set up in the basic block.
81 std::vector<unsigned> RegistersSetUp;
84 // A callback that fills a function.
85 using FillFunction = std::function<void(FunctionFiller &)>;
87 // Creates a temporary `void foo(char*)` function containing the provided
88 // Instructions. Runs a set of llvm Passes to provide correct prologue and
89 // epilogue. Once the MachineFunction is ready, it is assembled for TM to
90 // AsmStream, the temporary function is eventually discarded.
91 Error assembleToStream(const ExegesisTarget &ET,
92 std::unique_ptr<LLVMTargetMachine> TM,
93 ArrayRef<unsigned> LiveIns,
94 ArrayRef<RegisterValue> RegisterInitialValues,
95 const FillFunction &Fill, raw_pwrite_stream &AsmStreamm,
96 const BenchmarkKey &Key,
97 bool GenerateMemoryInstructions);
99 // Creates an ObjectFile in the format understood by the host.
100 // Note: the resulting object keeps a copy of Buffer so it can be discarded once
101 // this function returns.
102 object::OwningBinary<object::ObjectFile> getObjectFromBuffer(StringRef Buffer);
104 // Loads the content of Filename as on ObjectFile and returns it.
105 object::OwningBinary<object::ObjectFile> getObjectFromFile(StringRef Filename);
107 // Consumes an ObjectFile containing a `void foo(char*)` function and make it
108 // executable.
109 class ExecutableFunction {
110 public:
111 static Expected<ExecutableFunction>
112 create(std::unique_ptr<LLVMTargetMachine> TM,
113 object::OwningBinary<object::ObjectFile> &&ObjectFileHolder);
115 // Retrieves the function as an array of bytes.
116 StringRef getFunctionBytes() const { return FunctionBytes; }
118 // Executes the function.
119 void operator()(char *Memory) const {
120 ((void (*)(char *))(intptr_t)FunctionBytes.data())(Memory);
123 StringRef FunctionBytes;
125 private:
126 ExecutableFunction(std::unique_ptr<LLVMContext> Ctx,
127 std::unique_ptr<orc::LLJIT> EJIT, StringRef FunctionBytes);
129 std::unique_ptr<LLVMContext> Context;
130 std::unique_ptr<orc::LLJIT> ExecJIT;
133 // Copies benchmark function's bytes from benchmark object.
134 Error getBenchmarkFunctionBytes(const StringRef InputData,
135 std::vector<uint8_t> &Bytes);
137 // Creates a void(int8*) MachineFunction.
138 MachineFunction &createVoidVoidPtrMachineFunction(StringRef FunctionID,
139 Module *Module,
140 MachineModuleInfo *MMI);
142 } // namespace exegesis
143 } // namespace llvm
145 #endif // LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H