LLVM 20.0.0git
Go to the documentation of this file.
1//===-- PPCMCInstLower.cpp - Convert PPC MachineInstr to an MCInst --------===//
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
9// This file contains code to lower PPC MachineInstrs to their corresponding
10// MCInst records.
15#include "PPC.h"
17#include "PPCSubtarget.h"
19#include "llvm/ADT/Twine.h"
24#include "llvm/IR/DataLayout.h"
25#include "llvm/IR/GlobalValue.h"
26#include "llvm/IR/Mangler.h"
27#include "llvm/IR/Module.h"
28#include "llvm/MC/MCAsmInfo.h"
29#include "llvm/MC/MCExpr.h"
30#include "llvm/MC/MCInst.h"
32using namespace llvm;
35 AsmPrinter &AP) {
36 if (MO.isGlobal()) {
37 // Get the symbol from the global, accounting for XCOFF-specific
38 // intricacies (see TargetLoweringObjectFileXCOFF::getTargetSymbol).
39 const GlobalValue *GV = MO.getGlobal();
40 return AP.getSymbol(GV);
41 }
43 assert(MO.isSymbol() && "Isn't a symbol reference");
46 const DataLayout &DL = AP.getDataLayout();
49 MCContext &Ctx = AP.OutContext;
51 return Sym;
54static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
56 MCContext &Ctx = Printer.OutContext;
59 unsigned access = MO.getTargetFlags();
61 switch (access) {
64 break;
67 break;
70 break;
73 break;
76 break;
77 case PPCII::MO_TLS:
79 break;
82 break;
83 }
85 const TargetMachine &TM = Printer.TM;
86 const MachineInstr *MI = MO.getParent();
87 const MachineFunction *MF = MI->getMF();
89 if (MO.getTargetFlags() == PPCII::MO_PLT)
91 else if (MO.getTargetFlags() == PPCII::MO_PCREL_FLAG)
103 else if (MO.getTargetFlags() == PPCII::MO_TPREL_FLAG ||
105 assert(MO.isGlobal() && "Only expecting a global MachineOperand here!");
106 TLSModel::Model Model = TM.getTLSModel(MO.getGlobal());
107 const PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
108 // For the local-[exec|dynamic] TLS model, we may generate the offset from
109 // the TLS base as an immediate operand (instead of using a TOC entry). Set
110 // the relocation type in case the result is used for purposes other than a
111 // TOC reference. In TOC reference cases, this result is discarded.
112 if (Model == TLSModel::LocalExec)
114 else if (Model == TLSModel::LocalDynamic &&
115 FuncInfo->isAIXFuncUseTLSIEForLD())
116 // On AIX, TLS model opt may have turned local-dynamic accesses into
117 // initial-exec accesses.
119 else if (Model == TLSModel::LocalDynamic)
121 }
123 const Module *M = MF->getFunction().getParent();
124 const PPCSubtarget *Subtarget = &(MF->getSubtarget<PPCSubtarget>());
126 unsigned MIOpcode = MI->getOpcode();
127 assert((Subtarget->isUsingPCRelativeCalls() || MIOpcode != PPC::BL8_NOTOC) &&
128 "BL8_NOTOC is only valid when using PC Relative Calls.");
129 if (Subtarget->isUsingPCRelativeCalls()) {
130 if (MIOpcode == PPC::TAILB || MIOpcode == PPC::TAILB8 ||
131 MIOpcode == PPC::TCRETURNdi || MIOpcode == PPC::TCRETURNdi8 ||
132 MIOpcode == PPC::BL8_NOTOC || MIOpcode == PPC::BL8_NOTOC_RM) {
134 }
137 }
139 const MCExpr *Expr = MCSymbolRefExpr::create(Symbol, RefKind, Ctx);
140 // If -msecure-plt -fPIC, add 32768 to symbol.
141 if (Subtarget->isSecurePlt() && TM.isPositionIndependent() &&
142 M->getPICLevel() == PICLevel::BigPIC &&
144 Expr =
145 MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(32768, Ctx), Ctx);
147 if (!MO.isJTI() && MO.getOffset())
148 Expr = MCBinaryExpr::createAdd(Expr,
150 Ctx);
152 // Subtract off the PIC base if required.
156 const MachineFunction *MF = MO.getParent()->getParent()->getParent();
159 Expr = MCBinaryExpr::createSub(Expr, PB, Ctx);
160 }
162 // Add ha16() / lo16() markers if required.
163 switch (access) {
164 case PPCII::MO_LO:
166 Expr = PPCMCExpr::createLo(Expr, Ctx);
167 break;
168 case PPCII::MO_HA:
170 Expr = PPCMCExpr::createHa(Expr, Ctx);
171 break;
172 }
174 return MCOperand::createExpr(Expr);
178 AsmPrinter &AP) {
179 OutMI.setOpcode(MI->getOpcode());
181 for (const MachineOperand &MO : MI->operands()) {
182 MCOperand MCOp;
183 if (LowerPPCMachineOperandToMCOperand(MO, MCOp, AP))
184 OutMI.addOperand(MCOp);
185 }
189 MCOperand &OutMO, AsmPrinter &AP) {
190 switch (MO.getType()) {
191 default:
192 llvm_unreachable("unknown operand type");
194 assert(!MO.getSubReg() && "Subregs should be eliminated!");
195 assert(MO.getReg() > PPC::NoRegister &&
196 MO.getReg() < PPC::NUM_TARGET_REGS &&
197 "Invalid register for this target!");
198 // Ignore all implicit register operands.
199 if (MO.isImplicit())
200 return false;
201 OutMO = MCOperand::createReg(MO.getReg());
202 return true;
204 OutMO = MCOperand::createImm(MO.getImm());
205 return true;
207 OutMO = MCOperand::createExpr(
209 return true;
212 OutMO = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP);
213 return true;
215 OutMO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP);
216 return true;
218 OutMO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP);
219 return true;
221 OutMO =
223 return true;
225 OutMO = GetSymbolRef(MO, MO.getMCSymbol(), AP);
226 return true;
228 return false;
229 }
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
dxil pretty DXIL Metadata Pretty Printer
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol, AsmPrinter &Printer)
static MCSymbol * GetSymbolFromOperand(const MachineOperand &MO, AsmPrinter &AP)
const char LLVMTargetMachineRef TM
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallString class.
This file describes how to lower LLVM code to machine code.
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:86
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:676
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:96
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.
Definition: AsmPrinter.cpp:387
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:532
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:617
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:193
Context object for machine code objects.
Definition: MCContext.h:83
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:213
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:184
void addOperand(const MCOperand Op)
Definition: MCInst.h:210
void setOpcode(unsigned Op)
Definition: MCInst.h:197
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:134
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:162
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:141
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:393
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MCSymbol * getPICBaseSymbol() const
getPICBaseSymbol - Return a function-local symbol to represent the PIC base.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
Representation of each machine instruction.
Definition: MachineInstr.h:69
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:346
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
const GlobalValue * getGlobal() const
int64_t getImm() const
bool isImplicit() const
MachineBasicBlock * getMBB() const
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
const BlockAddress * getBlockAddress() const
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
unsigned getTargetFlags() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
const char * getSymbolName() const
Register getReg() const
getReg - Returns the register number.
MCSymbol * getMCSymbol() const
@ MO_Immediate
Immediate operand.
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ MO_MCSymbol
MCSymbol reference (for debug/eh info)
@ MO_GlobalAddress
Address of a global value.
@ MO_RegisterMask
Mask of preserved registers.
@ 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.
int64_t getOffset() const
Return the offset from the symbol in this operand.
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:120
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
PPCFunctionInfo - This class is derived from MachineFunction private PowerPC target-specific informat...
static const PPCMCExpr * createLo(const MCExpr *Expr, MCContext &Ctx)
Definition: PPCMCExpr.h:48
static const PPCMCExpr * createHa(const MCExpr *Expr, MCContext &Ctx)
Definition: PPCMCExpr.h:56
bool isUsingPCRelativeCalls() const
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: PPC.h:184
Definition: PPC.h:194
Definition: PPC.h:197
MO_GOT_TPREL_PCREL_FLAG - A combintaion of flags, if these bits are set they should produce the reloc...
Definition: PPC.h:172
Definition: PPC.h:203
MO_PCREL_FLAG - If this bit is set, the symbol reference is relative to the current instruction addre...
Definition: PPC.h:121
MO_TLSLD_FLAG - If this bit is set the symbol reference is relative to TLS Local Dynamic model.
Definition: PPC.h:150
Definition: PPC.h:200
Definition: PPC.h:179
On PPC, the 12 bits are not enough for all target operand flags.
Definition: PPC.h:113
These values identify relocations on immediates folded into memory operations.
Definition: PPC.h:183
Symbol for VK_PPC_TLS fixup attached to an ADD instruction.
Definition: PPC.h:188
MO_TPREL_FLAG - If this bit is set, the symbol reference is relative to the thread pointer and the sy...
Definition: PPC.h:140
Definition: PPC.h:178
MO_LO, MO_HA - lo16(symbol) and ha16(symbol)
Definition: PPC.h:175
MO_GOT_TLSLD_PCREL_FLAG - A combintaion of flags, if these bits are set they should produce the reloc...
Definition: PPC.h:166
Definition: PPC.h:191
Definition: PPC.h:185
MO_GOT_TLSGD_PCREL_FLAG - A combintaion of flags, if these bits are set they should produce the reloc...
Definition: PPC.h:160
Definition: PPC.h:176
MO_PIC_FLAG - If this bit is set, the symbol reference is relative to the function's picbase,...
Definition: PPC.h:117
MO_PCREL_OPT_FLAG - If this bit is set the operand is part of a PC Relative linker optimization.
Definition: PPC.h:130
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool LowerPPCMachineOperandToMCOperand(const MachineOperand &MO, MCOperand &OutMO, AsmPrinter &AP)
void LowerPPCMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI, AsmPrinter &AP)