LLVM 20.0.0git
PPCELFStreamer.cpp
Go to the documentation of this file.
1//===-------- PPCELFStreamer.cpp - ELF Object Output ---------------------===//
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 is a custom MCELFStreamer for PowerPC.
10//
11// The purpose of the custom ELF streamer is to allow us to intercept
12// instructions as they are being emitted and align all 8 byte instructions
13// to a 64 byte boundary if required (by adding a 4 byte nop). This is important
14// because 8 byte instructions are not allowed to cross 64 byte boundaries
15// and by aliging anything that is within 4 bytes of the boundary we can
16// guarantee that the 8 byte instructions do not cross that boundary.
17//
18//===----------------------------------------------------------------------===//
19
20#include "PPCELFStreamer.h"
21#include "PPCMCCodeEmitter.h"
22#include "PPCMCTargetDesc.h"
25#include "llvm/MC/MCAssembler.h"
27#include "llvm/MC/MCContext.h"
28#include "llvm/MC/MCInst.h"
29#include "llvm/MC/MCInstrDesc.h"
32
33using namespace llvm;
34
36 std::unique_ptr<MCAsmBackend> MAB,
37 std::unique_ptr<MCObjectWriter> OW,
38 std::unique_ptr<MCCodeEmitter> Emitter)
39 : MCELFStreamer(Context, std::move(MAB), std::move(OW), std::move(Emitter)),
40 LastLabel(nullptr) {}
41
42void PPCELFStreamer::emitPrefixedInstruction(const MCInst &Inst,
43 const MCSubtargetInfo &STI) {
44 // Prefixed instructions must not cross a 64-byte boundary (i.e. prefix is
45 // before the boundary and the remaining 4-bytes are after the boundary). In
46 // order to achieve this, a nop is added prior to any such boundary-crossing
47 // prefixed instruction. Align to 64 bytes if possible but add a maximum of 4
48 // bytes when trying to do that. If alignment requires adding more than 4
49 // bytes then the instruction won't be aligned. When emitting a code alignment
50 // a new fragment is created for this alignment. This fragment will contain
51 // all of the nops required as part of the alignment operation. In the cases
52 // when no nops are added then The fragment is still created but it remains
53 // empty.
54 emitCodeAlignment(Align(64), &STI, 4);
55
56 // Emit the instruction.
57 // Since the previous emit created a new fragment then adding this instruction
58 // also forces the addition of a new fragment. Inst is now the first
59 // instruction in that new fragment.
61
62 // The above instruction is forced to start a new fragment because it
63 // comes after a code alignment fragment. Get that new fragment.
64 MCFragment *InstructionFragment = getCurrentFragment();
65 SMLoc InstLoc = Inst.getLoc();
66 // Check if there was a last label emitted.
67 if (LastLabel && !LastLabel->isUnset() && LastLabelLoc.isValid() &&
68 InstLoc.isValid()) {
69 const SourceMgr *SourceManager = getContext().getSourceManager();
70 unsigned InstLine = SourceManager->FindLineNumber(InstLoc);
71 unsigned LabelLine = SourceManager->FindLineNumber(LastLabelLoc);
72 // If the Label and the Instruction are on the same line then move the
73 // label to the top of the fragment containing the aligned instruction that
74 // was just added.
75 if (InstLine == LabelLine) {
76 LastLabel->setFragment(InstructionFragment);
77 LastLabel->setOffset(0);
78 }
79 }
80}
81
83 const MCSubtargetInfo &STI) {
86
87 // If the instruction is a part of the GOT to PC-Rel link time optimization
88 // instruction pair, return a value, otherwise return std::nullopt. A true
89 // returned value means the instruction is the PLDpc and a false value means
90 // it is the user instruction.
91 std::optional<bool> IsPartOfGOTToPCRelPair =
92 isPartOfGOTToPCRelPair(Inst, STI);
93
94 // User of the GOT-indirect address.
95 // For example, the load that will get the relocation as follows:
96 // .reloc .Lpcrel1-8,R_PPC64_PCREL_OPT,.-(.Lpcrel1-8)
97 // lwa 3, 4(3)
98 if (IsPartOfGOTToPCRelPair && !*IsPartOfGOTToPCRelPair)
99 emitGOTToPCRelReloc(Inst);
100
101 // Special handling is only for prefixed instructions.
102 if (!Emitter->isPrefixedInstruction(Inst)) {
104 return;
105 }
106 emitPrefixedInstruction(Inst, STI);
107
108 // Producer of the GOT-indirect address.
109 // For example, the prefixed load from the got that will get the label as
110 // follows:
111 // pld 3, vec@got@pcrel(0), 1
112 // .Lpcrel1:
113 if (IsPartOfGOTToPCRelPair && *IsPartOfGOTToPCRelPair)
114 emitGOTToPCRelLabel(Inst);
115}
116
118 LastLabel = Symbol;
119 LastLabelLoc = Loc;
121}
122
123// This linker time GOT PC Relative optimization relocation will look like this:
124// pld <reg> symbol@got@pcrel
125// <Label###>:
126// .reloc Label###-8,R_PPC64_PCREL_OPT,.-(Label###-8)
127// load <loadedreg>, 0(<reg>)
128// The reason we place the label after the PLDpc instruction is that there
129// may be an alignment nop before it since prefixed instructions must not
130// cross a 64-byte boundary (please see
131// PPCELFStreamer::emitPrefixedInstruction()). When referring to the
132// label, we subtract the width of a prefixed instruction (8 bytes) to ensure
133// we refer to the PLDpc.
134void PPCELFStreamer::emitGOTToPCRelReloc(const MCInst &Inst) {
135 // Get the last operand which contains the symbol.
136 const MCOperand &Operand = Inst.getOperand(Inst.getNumOperands() - 1);
137 assert(Operand.isExpr() && "Expecting an MCExpr.");
138 // Cast the last operand to MCSymbolRefExpr to get the symbol.
139 const MCExpr *Expr = Operand.getExpr();
140 const MCSymbolRefExpr *SymExpr = static_cast<const MCSymbolRefExpr *>(Expr);
142 "Expecting a symbol of type VK_PPC_PCREL_OPT");
143 MCSymbol *LabelSym =
145 const MCExpr *LabelExpr = MCSymbolRefExpr::create(LabelSym, getContext());
146 const MCExpr *Eight = MCConstantExpr::create(8, getContext());
147 // SubExpr is just Label###-8
148 const MCExpr *SubExpr =
149 MCBinaryExpr::createSub(LabelExpr, Eight, getContext());
150 MCSymbol *CurrentLocation = getContext().createTempSymbol();
151 const MCExpr *CurrentLocationExpr =
152 MCSymbolRefExpr::create(CurrentLocation, getContext());
153 // SubExpr2 is .-(Label###-8)
154 const MCExpr *SubExpr2 =
155 MCBinaryExpr::createSub(CurrentLocationExpr, SubExpr, getContext());
156
157 MCDataFragment *DF = static_cast<MCDataFragment *>(LabelSym->getFragment());
158 assert(DF && "Expecting a valid data fragment.");
159 MCFixupKind FixupKind = static_cast<MCFixupKind>(FirstLiteralRelocationKind +
160 ELF::R_PPC64_PCREL_OPT);
161 DF->getFixups().push_back(
162 MCFixup::create(LabelSym->getOffset() - 8, SubExpr2,
163 FixupKind, Inst.getLoc()));
164 emitLabel(CurrentLocation, Inst.getLoc());
165}
166
167// Emit the label that immediately follows the PLDpc for a link time GOT PC Rel
168// optimization.
169void PPCELFStreamer::emitGOTToPCRelLabel(const MCInst &Inst) {
170 // Get the last operand which contains the symbol.
171 const MCOperand &Operand = Inst.getOperand(Inst.getNumOperands() - 1);
172 assert(Operand.isExpr() && "Expecting an MCExpr.");
173 // Cast the last operand to MCSymbolRefExpr to get the symbol.
174 const MCExpr *Expr = Operand.getExpr();
175 const MCSymbolRefExpr *SymExpr = static_cast<const MCSymbolRefExpr *>(Expr);
177 "Expecting a symbol of type VK_PPC_PCREL_OPT");
178 MCSymbol *LabelSym =
180 emitLabel(LabelSym, Inst.getLoc());
181}
182
183// This function checks if the parameter Inst is part of the setup for a link
184// time GOT PC Relative optimization. For example in this situation:
185// <MCInst PLDpc <MCOperand Reg:282> <MCOperand Expr:(glob_double@got@pcrel)>
186// <MCOperand Imm:0> <MCOperand Expr:(.Lpcrel@<<invalid>>)>>
187// <MCInst SOME_LOAD <MCOperand Reg:22> <MCOperand Imm:0> <MCOperand Reg:282>
188// <MCOperand Expr:(.Lpcrel@<<invalid>>)>>
189// The above is a pair of such instructions and this function will not return
190// std::nullopt for either one of them. In both cases we are looking for the
191// last operand <MCOperand Expr:(.Lpcrel@<<invalid>>)> which needs to be an
192// MCExpr and has the flag MCSymbolRefExpr::VK_PPC_PCREL_OPT. After that we just
193// look at the opcode and in the case of PLDpc we will return true. For the load
194// (or store) this function will return false indicating it has found the second
195// instruciton in the pair.
196std::optional<bool> llvm::isPartOfGOTToPCRelPair(const MCInst &Inst,
197 const MCSubtargetInfo &STI) {
198 // Need at least two operands.
199 if (Inst.getNumOperands() < 2)
200 return std::nullopt;
201
202 unsigned LastOp = Inst.getNumOperands() - 1;
203 // The last operand needs to be an MCExpr and it needs to have a variant kind
204 // of VK_PPC_PCREL_OPT. If it does not satisfy these conditions it is not a
205 // link time GOT PC Rel opt instruction and we can ignore it and return
206 // std::nullopt.
207 const MCOperand &Operand = Inst.getOperand(LastOp);
208 if (!Operand.isExpr())
209 return std::nullopt;
210
211 // Check for the variant kind VK_PPC_PCREL_OPT in this expression.
212 const MCExpr *Expr = Operand.getExpr();
213 const MCSymbolRefExpr *SymExpr = static_cast<const MCSymbolRefExpr *>(Expr);
214 if (!SymExpr || SymExpr->getKind() != MCSymbolRefExpr::VK_PPC_PCREL_OPT)
215 return std::nullopt;
216
217 return (Inst.getOpcode() == PPC::PLDpc);
218}
219
221 MCContext &Context, std::unique_ptr<MCAsmBackend> MAB,
222 std::unique_ptr<MCObjectWriter> OW,
223 std::unique_ptr<MCCodeEmitter> Emitter) {
224 return new PPCELFStreamer(Context, std::move(MAB), std::move(OW),
225 std::move(Emitter));
226}
dxil DXContainer Global Emitter
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MCCodeEmitter * getEmitterPtr() const
Definition: MCAssembler.h:186
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:622
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:222
Context object for machine code objects.
Definition: MCContext.h:83
MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
Definition: MCContext.cpp:345
const SourceMgr * getSourceManager() const
Definition: MCContext.h:401
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:212
Fragment for data and encoded instructions.
Definition: MCFragment.h:219
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Definition: MCFixup.h:87
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:185
unsigned getNumOperands() const
Definition: MCInst.h:209
SMLoc getLoc() const
Definition: MCInst.h:205
unsigned getOpcode() const
Definition: MCInst.h:199
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:207
MCAssembler & getAssembler()
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
void emitCodeAlignment(Align ByteAlignment, const MCSubtargetInfo *STI, unsigned MaxBytesToEmit=0) override
Emit nops until the byte alignment ByteAlignment is reached.
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:37
const MCExpr * getExpr() const
Definition: MCInst.h:115
bool isExpr() const
Definition: MCInst.h:66
MCFragment * getCurrentFragment() const
Definition: MCStreamer.h:411
MCContext & getContext() const
Definition: MCStreamer.h:300
Generic base class for all target subtargets.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
const MCSymbol & getSymbol() const
Definition: MCExpr.h:411
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:398
VariantKind getKind() const
Definition: MCExpr.h:413
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
void setFragment(MCFragment *F) const
Mark the symbol as defined in the fragment F.
Definition: MCSymbol.h:275
void setOffset(uint64_t Value)
Definition: MCSymbol.h:333
bool isUnset() const
Definition: MCSymbol.h:325
uint64_t getOffset() const
Definition: MCSymbol.h:327
MCFragment * getFragment(bool SetUsed=true) const
Definition: MCSymbol.h:397
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
PPCELFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > MAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter)
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
Represents a location in source code.
Definition: SMLoc.h:23
constexpr bool isValid() const
Definition: SMLoc.h:29
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
unsigned FindLineNumber(SMLoc Loc, unsigned BufferID=0) const
Find the line number for the specified location in the specified file.
Definition: SourceMgr.h:196
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
@ FirstLiteralRelocationKind
The range [FirstLiteralRelocationKind, MaxTargetFixupKind) is used for relocations coming from ....
Definition: MCFixup.h:50
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:1873
MCELFStreamer * createPPCELFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > MAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter)
std::optional< bool > isPartOfGOTToPCRelPair(const MCInst &Inst, const MCSubtargetInfo &STI)
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39