LLVM 19.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"
16#include "PPCSubtarget.h"
18#include "llvm/ADT/Twine.h"
23#include "llvm/IR/DataLayout.h"
24#include "llvm/IR/GlobalValue.h"
25#include "llvm/IR/Mangler.h"
26#include "llvm/MC/MCAsmInfo.h"
27#include "llvm/MC/MCExpr.h"
28#include "llvm/MC/MCInst.h"
30using namespace llvm;
33 AsmPrinter &AP) {
34 if (MO.isGlobal()) {
35 // Get the symbol from the global, accounting for XCOFF-specific
36 // intricacies (see TargetLoweringObjectFileXCOFF::getTargetSymbol).
37 const GlobalValue *GV = MO.getGlobal();
38 return AP.getSymbol(GV);
39 }
41 assert(MO.isSymbol() && "Isn't a symbol reference");
44 const DataLayout &DL = AP.getDataLayout();
47 MCContext &Ctx = AP.OutContext;
49 return Sym;
52static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
54 MCContext &Ctx = Printer.OutContext;
57 unsigned access = MO.getTargetFlags();
59 switch (access) {
62 break;
65 break;
68 break;
71 break;
74 break;
75 case PPCII::MO_TLS:
77 break;
80 break;
81 }
83 const TargetMachine &TM = Printer.TM;
85 if (MO.getTargetFlags() == PPCII::MO_PLT)
87 else if (MO.getTargetFlags() == PPCII::MO_PCREL_FLAG)
99 else if (MO.getTargetFlags() == PPCII::MO_TPREL_FLAG ||
101 assert(MO.isGlobal() && "Only expecting a global MachineOperand here!");
102 TLSModel::Model Model = TM.getTLSModel(MO.getGlobal());
103 // For the local-[exec|dynamic] TLS model, we may generate the offset from
104 // the TLS base as an immediate operand (instead of using a TOC entry). Set
105 // the relocation type in case the result is used for purposes other than a
106 // TOC reference. In TOC reference cases, this result is discarded.
107 if (Model == TLSModel::LocalExec)
109 else if (Model == TLSModel::LocalDynamic)
111 }
113 const MachineInstr *MI = MO.getParent();
114 const MachineFunction *MF = MI->getMF();
115 const Module *M = MF->getFunction().getParent();
116 const PPCSubtarget *Subtarget = &(MF->getSubtarget<PPCSubtarget>());
118 unsigned MIOpcode = MI->getOpcode();
119 assert((Subtarget->isUsingPCRelativeCalls() || MIOpcode != PPC::BL8_NOTOC) &&
120 "BL8_NOTOC is only valid when using PC Relative Calls.");
121 if (Subtarget->isUsingPCRelativeCalls()) {
122 if (MIOpcode == PPC::TAILB || MIOpcode == PPC::TAILB8 ||
123 MIOpcode == PPC::TCRETURNdi || MIOpcode == PPC::TCRETURNdi8 ||
124 MIOpcode == PPC::BL8_NOTOC || MIOpcode == PPC::BL8_NOTOC_RM) {
126 }
129 }
131 const MCExpr *Expr = MCSymbolRefExpr::create(Symbol, RefKind, Ctx);
132 // If -msecure-plt -fPIC, add 32768 to symbol.
133 if (Subtarget->isSecurePlt() && TM.isPositionIndependent() &&
134 M->getPICLevel() == PICLevel::BigPIC &&
136 Expr =
137 MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(32768, Ctx), Ctx);
139 if (!MO.isJTI() && MO.getOffset())
140 Expr = MCBinaryExpr::createAdd(Expr,
142 Ctx);
144 // Subtract off the PIC base if required.
148 const MachineFunction *MF = MO.getParent()->getParent()->getParent();
151 Expr = MCBinaryExpr::createSub(Expr, PB, Ctx);
152 }
154 // Add ha16() / lo16() markers if required.
155 switch (access) {
156 case PPCII::MO_LO:
158 Expr = PPCMCExpr::createLo(Expr, Ctx);
159 break;
160 case PPCII::MO_HA:
162 Expr = PPCMCExpr::createHa(Expr, Ctx);
163 break;
164 }
166 return MCOperand::createExpr(Expr);
170 AsmPrinter &AP) {
171 OutMI.setOpcode(MI->getOpcode());
173 for (const MachineOperand &MO : MI->operands()) {
174 MCOperand MCOp;
175 if (LowerPPCMachineOperandToMCOperand(MO, MCOp, AP))
176 OutMI.addOperand(MCOp);
177 }
181 MCOperand &OutMO, AsmPrinter &AP) {
182 switch (MO.getType()) {
183 default:
184 llvm_unreachable("unknown operand type");
186 assert(!MO.getSubReg() && "Subregs should be eliminated!");
187 assert(MO.getReg() > PPC::NoRegister &&
188 MO.getReg() < PPC::NUM_TARGET_REGS &&
189 "Invalid register for this target!");
190 // Ignore all implicit register operands.
191 if (MO.isImplicit())
192 return false;
193 OutMO = MCOperand::createReg(MO.getReg());
194 return true;
196 OutMO = MCOperand::createImm(MO.getImm());
197 return true;
199 OutMO = MCOperand::createExpr(
201 return true;
204 OutMO = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP);
205 return true;
207 OutMO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP);
208 return true;
210 OutMO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP);
211 return true;
213 OutMO =
215 return true;
217 OutMO = GetSymbolRef(MO, MO.getMCSymbol(), AP);
218 return true;
220 return false;
221 }
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
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:84
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:700
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:94
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:402
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:536
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:621
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
Context object for machine code objects.
Definition: MCContext.h:81
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:201
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
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:397
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
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.
Representation of each machine instruction.
Definition: MachineInstr.h:69
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:329
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
static const PPCMCExpr * createLo(const MCExpr *Expr, MCContext &Ctx)
Definition: PPCMCExpr.h:49
static const PPCMCExpr * createHa(const MCExpr *Expr, MCContext &Ctx)
Definition: PPCMCExpr.h:57
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:76
#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)