LLVM 23.0.0git
BPFAsmPrinter.cpp
Go to the documentation of this file.
1//===-- BPFAsmPrinter.cpp - BPF LLVM assembly writer ----------------------===//
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// This file contains a printer that converts from our internal representation
10// of machine-dependent LLVM code to the BPF assembly language.
11//
12//===----------------------------------------------------------------------===//
13
14#include "BPFAsmPrinter.h"
15#include "BPF.h"
16#include "BPFInstrInfo.h"
17#include "BPFMCInstLower.h"
18#include "BTFDebug.h"
29#include "llvm/IR/Module.h"
30#include "llvm/MC/MCAsmInfo.h"
31#include "llvm/MC/MCExpr.h"
32#include "llvm/MC/MCInst.h"
34#include "llvm/MC/MCStreamer.h"
35#include "llvm/MC/MCSymbol.h"
36#include "llvm/MC/MCSymbolELF.h"
41using namespace llvm;
42
43#define DEBUG_TYPE "asm-printer"
44
46 std::unique_ptr<MCStreamer> Streamer)
47 : AsmPrinter(TM, std::move(Streamer), ID), BTF(nullptr), TM(TM) {}
48
50
53
54 // Only emit BTF when debuginfo available.
55 if (MAI.doesSupportDebugInformation() && !M.debug_compile_units().empty()) {
56 BTF = new BTFDebug(this);
57 Handlers.push_back(std::unique_ptr<BTFDebug>(BTF));
58 }
59
60 return false;
61}
62
63const BPFTargetMachine &BPFAsmPrinter::getBTM() const {
64 return static_cast<const BPFTargetMachine &>(TM);
65}
66
68 // Remove unused globals which are previously used for jump table.
69 const BPFSubtarget *Subtarget = getBTM().getSubtargetImpl();
70 if (Subtarget->hasGotox()) {
71 std::vector<GlobalVariable *> Targets;
72 for (GlobalVariable &Global : M.globals()) {
73 if (Global.getLinkage() != GlobalValue::PrivateLinkage)
74 continue;
75 if (!Global.isConstant() || !Global.hasInitializer())
76 continue;
77
78 Constant *CV = dyn_cast<Constant>(Global.getInitializer());
79 if (!CV)
80 continue;
82 if (!CA)
83 continue;
84
85 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
87 continue;
88 }
89 Targets.push_back(&Global);
90 }
91
92 for (GlobalVariable *GV : Targets) {
93 GV->replaceAllUsesWith(PoisonValue::get(GV->getType()));
94 GV->dropAllReferences();
95 GV->eraseFromParent();
96 }
97 }
98
99 for (GlobalObject &GO : M.global_objects()) {
100 if (!GO.hasExternalWeakLinkage())
101 continue;
102
103 if (!SawTrapCall && GO.getName() == BPF_TRAP) {
104 GO.eraseFromParent();
105 break;
106 }
107 }
108
110}
111
113 raw_ostream &O) {
114 const MachineOperand &MO = MI->getOperand(OpNum);
115
116 switch (MO.getType()) {
119 break;
120
122 O << MO.getImm();
123 break;
124
126 O << *MO.getMBB()->getSymbol();
127 break;
128
130 O << *getSymbol(MO.getGlobal());
131 break;
132
135 O << BA->getName();
136 break;
137 }
138
141 break;
142
145 default:
146 llvm_unreachable("<unknown operand type>");
147 }
148}
149
151 const char *ExtraCode, raw_ostream &O) {
152 if (ExtraCode && ExtraCode[0])
153 return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
154
155 printOperand(MI, OpNo, O);
156 return false;
157}
158
160 unsigned OpNum, const char *ExtraCode,
161 raw_ostream &O) {
162 assert(OpNum + 1 < MI->getNumOperands() && "Insufficient operands");
163 const MachineOperand &BaseMO = MI->getOperand(OpNum);
164 const MachineOperand &OffsetMO = MI->getOperand(OpNum + 1);
165 assert(BaseMO.isReg() && "Unexpected base pointer for inline asm memory operand.");
166 assert(OffsetMO.isImm() && "Unexpected offset for inline asm memory operand.");
167 int Offset = OffsetMO.getImm();
168
169 if (ExtraCode)
170 return true; // Unknown modifier.
171
172 if (Offset < 0)
173 O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " - " << -Offset << ")";
174 else
175 O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " + " << Offset << ")";
176
177 return false;
178}
179
181 if (MI->isCall()) {
182 for (const MachineOperand &Op : MI->operands()) {
183 if (Op.isGlobal()) {
184 if (const GlobalValue *GV = Op.getGlobal())
185 if (GV->getName() == BPF_TRAP)
186 SawTrapCall = true;
187 }
188 }
189 }
190
191 BPF_MC::verifyInstructionPredicates(MI->getOpcode(),
192 getSubtargetInfo().getFeatureBits());
193
194 MCInst TmpInst;
195
196 if (!BTF || !BTF->InstLower(MI, TmpInst)) {
197 BPFMCInstLower MCInstLowering(OutContext, *this);
198 MCInstLowering.Lower(MI, TmpInst);
199 }
200 EmitToStreamer(*OutStreamer, TmpInst);
201}
202
204 // Emit .bpf_cleanup section with a flat table of
205 // (call_site, landing_pad) pairs.
206 const std::vector<LandingPadInfo> &LandingPads = MF->getLandingPads();
207 if (LandingPads.empty())
208 return;
209
210 MCContext &Ctx = OutContext;
211 auto *CleanupSec =
212 Ctx.getELFSection(".bpf_cleanup", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
213 OutStreamer->switchSection(CleanupSec);
214
215 const auto &TypeInfos = MF->getTypeInfos();
216 const Function &F = MF->getFunction();
217 LLVMContext &LLVMCtx = F.getContext();
218
219 // Each landing pad has BeginLabels/EndLabels marking the invoke
220 // call sites that unwind to it.
221 for (const LandingPadInfo &LP : LandingPads) {
222 // BPF treats all landing pads as catch-all: the kernel redirects to
223 // the landing pad regardless of exception type. Reject type-specific
224 // catches and filters which would silently misbehave.
225 for (int TId : LP.TypeIds) {
226 if (TId > 0 && TypeInfos[TId - 1] != nullptr) {
228 F, "BPF does not support type-specific exception catches yet"));
229 return;
230 }
231 if (TId < 0) {
233 F, "BPF does not support exception filters yet"));
234 return;
235 }
236 }
237
238 MCSymbol *LPLabel = LP.LandingPadLabel;
239 if (!LPLabel)
240 continue;
241 for (unsigned i = 0, e = LP.BeginLabels.size(); i != e; ++i) {
242 MCSymbol *Begin = LP.BeginLabels[i];
243 MCSymbol *End = LP.EndLabels[i];
244
245 // Each entry is 3 x 4 bytes: begin, end, landing_pad.
246 // The invoke region [begin, end) may include argument setup
247 // before the call. The runtime checks begin <= PC < end.
248 OutStreamer->emitSymbolValue(Begin, 4);
249 OutStreamer->emitSymbolValue(End, 4);
250 OutStreamer->emitSymbolValue(LPLabel, 4);
251 }
252 }
253
254 // Switch back to the function's section.
255 OutStreamer->switchSection(MF->getSection());
256}
257
259 SmallString<60> Name;
261 << "BPF.JT." << MF->getFunctionNumber() << '.' << JTI;
262 MCSymbol *S = OutContext.getOrCreateSymbol(Name);
263 if (auto *ES = static_cast<MCSymbolELF *>(S)) {
264 ES->setBinding(ELF::STB_GLOBAL);
265 ES->setType(ELF::STT_OBJECT);
266 }
267 return S;
268}
269
271 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
272 if (!MJTI)
273 return;
274
275 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
276 if (JT.empty())
277 return;
278
280 const Function &F = MF->getFunction();
281
282 MCSection *Sec = OutStreamer->getCurrentSectionOnly();
283 MCSymbol *SecStart = Sec->getBeginSymbol();
284
285 MCSection *JTS = TLOF.getSectionForJumpTable(F, TM);
287 unsigned EntrySize = MJTI->getEntrySize(getDataLayout());
288 OutStreamer->switchSection(JTS);
289 for (unsigned JTI = 0; JTI < JT.size(); JTI++) {
290 ArrayRef<MachineBasicBlock *> JTBBs = JT[JTI].MBBs;
291 if (JTBBs.empty())
292 continue;
293
294 MCSymbol *JTStart = getJTPublicSymbol(JTI);
295 OutStreamer->emitLabel(JTStart);
296 for (const MachineBasicBlock *MBB : JTBBs) {
297 const MCExpr *Diff = MCBinaryExpr::createSub(
300 OutStreamer->emitValue(Diff, EntrySize);
301 }
302 const MCExpr *JTSize =
303 MCConstantExpr::create(JTBBs.size() * EntrySize, OutContext);
304 OutStreamer->emitELFSize(JTStart, JTSize);
305 }
306}
307
308char BPFAsmPrinter::ID = 0;
309
310INITIALIZE_PASS(BPFAsmPrinter, "bpf-asm-printer", "BPF Assembly Printer", false,
311 false)
312
313// Force static initialization.
315LLVMInitializeBPFAsmPrinter() {
319}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
#define BPF_TRAP
Definition BPF.h:25
This file contains support for writing BTF debug info.
#define X(NUM, ENUM, NAME)
Definition ELF.h:853
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
This file describes how to lower LLVM code to machine code.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
MCSymbol * getSymbol(const GlobalValue *GV) const
void EmitToStreamer(MCStreamer &S, const MCInst &Inst)
TargetMachine & TM
Target machine description.
Definition AsmPrinter.h:94
SmallVector< std::unique_ptr< AsmPrinterHandler >, 2 > Handlers
Definition AsmPrinter.h:244
MachineFunction * MF
The current machine function.
Definition AsmPrinter.h:109
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
AsmPrinter(TargetMachine &TM, std::unique_ptr< MCStreamer > Streamer, char &ID=AsmPrinter::ID)
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition AsmPrinter.h:101
bool doFinalization(Module &M) override
Shut down the asmprinter.
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition AsmPrinter.h:106
const MCAsmInfo & MAI
Target Asm Printer information.
Definition AsmPrinter.h:97
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
const DataLayout & getDataLayout() const
Return information about data layout.
MCSymbol * GetExternalSymbolSymbol(const Twine &Sym) const
Return the MCSymbol for the specified ExternalSymbol.
const MCSubtargetInfo & getSubtargetInfo() const
Return information about subtarget.
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant.
MCSymbol * getJTPublicSymbol(unsigned JTI)
void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O)
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum, const char *ExtraCode, raw_ostream &O) override
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant as...
BPFAsmPrinter(TargetMachine &TM, std::unique_ptr< MCStreamer > Streamer)
bool doFinalization(Module &M) override
Shut down the asmprinter.
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &O) override
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant.
void emitFunctionBodyEnd() override
Targets can override this to emit stuff after the last basic block in the function.
~BPFAsmPrinter() override
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
void emitJumpTableInfo() override
Print assembly representations of the jump tables used by the current function to the current output ...
void emitInstruction(const MachineInstr *MI) override
Targets should implement this to emit instructions.
static const char * getRegisterName(MCRegister Reg)
void Lower(const MachineInstr *MI, MCInst &OutMI) const
bool hasGotox() const
Collect and emit BTF information.
Definition BTFDebug.h:289
ConstantArray - Constant Array Declarations.
Definition Constants.h:579
This is an important base class in LLVM.
Definition Constant.h:43
Diagnostic information for unsupported feature in backend.
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition GlobalValue.h:61
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition MCExpr.h:428
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
Context object for machine code objects.
Definition MCContext.h:83
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:573
MCSymbol * getBeginSymbol()
Definition MCSection.h:646
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:214
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
LLVM_ABI MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
Representation of each machine instruction.
LLVM_ABI unsigned getEntrySize(const DataLayout &TD) const
getEntrySize - Return the size of each entry in the jump table.
@ EK_BlockAddress
EK_BlockAddress - Each entry is a plain address of block, e.g.: .word LBB123.
const std::vector< MachineJumpTableEntry > & getJumpTables() const
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
const BlockAddress * getBlockAddress() const
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
const char * getSymbolName() const
Register getReg() const
getReg - Returns the register number.
@ MO_Immediate
Immediate operand.
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ MO_GlobalAddress
Address of a global value.
@ MO_BlockAddress
Address of a basic block.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_Register
Register operand.
@ MO_ExternalSymbol
Name of external global symbol.
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
virtual MCSection * getSectionForJumpTable(const Function &F, const TargetMachine &TM) const
Primary interface to the complete machine description for the target machine.
Value * getOperand(unsigned i) const
Definition User.h:207
unsigned getNumOperands() const
Definition User.h:229
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an SmallVector or SmallString.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ SHF_ALLOC
Definition ELF.h:1251
@ SHT_PROGBITS
Definition ELF.h:1150
@ STB_GLOBAL
Definition ELF.h:1408
@ STT_OBJECT
Definition ELF.h:1420
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:557
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
Target & getTheBPFleTarget()
Target & getTheBPFbeTarget()
Target & getTheBPFTarget()
@ Global
Append to llvm.global_dtors.
DWARFExpression::Operation Op
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1916
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:874
This structure is used to retain landing pad info for the current function.
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...