LLVM 19.0.0git
PPCMCInstLower.cpp
Go to the documentation of this file.
1//===-- PPCMCInstLower.cpp - Convert PPC MachineInstr to an MCInst --------===//
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 code to lower PPC MachineInstrs to their corresponding
10// MCInst records.
11//
12//===----------------------------------------------------------------------===//
13
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;
31
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 }
40
41 assert(MO.isSymbol() && "Isn't a symbol reference");
42
44 const DataLayout &DL = AP.getDataLayout();
46
47 MCContext &Ctx = AP.OutContext;
49 return Sym;
50}
51
52static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
54 MCContext &Ctx = Printer.OutContext;
56
57 unsigned access = MO.getTargetFlags();
58
59 switch (access) {
62 break;
65 break;
68 break;
71 break;
74 break;
75 case PPCII::MO_TLS:
77 break;
80 break;
81 }
82
83 const TargetMachine &TM = Printer.TM;
84
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) {
100 assert(MO.isGlobal() && "Only expecting a global MachineOperand here!");
101 TLSModel::Model Model = TM.getTLSModel(MO.getGlobal());
102 // For the local-exec TLS model, we may generate the offset from the TLS
103 // base as an immediate operand (instead of using a TOC entry).
104 // Set the relocation type in case the result is used for purposes other
105 // than a TOC reference. In TOC reference cases, this result is discarded.
106 if (Model == TLSModel::LocalExec)
108 }
109
110 const MachineInstr *MI = MO.getParent();
111 const MachineFunction *MF = MI->getMF();
112 const Module *M = MF->getFunction().getParent();
113 const PPCSubtarget *Subtarget = &(MF->getSubtarget<PPCSubtarget>());
114
115 unsigned MIOpcode = MI->getOpcode();
116 assert((Subtarget->isUsingPCRelativeCalls() || MIOpcode != PPC::BL8_NOTOC) &&
117 "BL8_NOTOC is only valid when using PC Relative Calls.");
118 if (Subtarget->isUsingPCRelativeCalls()) {
119 if (MIOpcode == PPC::TAILB || MIOpcode == PPC::TAILB8 ||
120 MIOpcode == PPC::TCRETURNdi || MIOpcode == PPC::TCRETURNdi8 ||
121 MIOpcode == PPC::BL8_NOTOC || MIOpcode == PPC::BL8_NOTOC_RM) {
123 }
126 }
127
128 const MCExpr *Expr = MCSymbolRefExpr::create(Symbol, RefKind, Ctx);
129 // If -msecure-plt -fPIC, add 32768 to symbol.
130 if (Subtarget->isSecurePlt() && TM.isPositionIndependent() &&
131 M->getPICLevel() == PICLevel::BigPIC &&
133 Expr =
134 MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(32768, Ctx), Ctx);
135
136 if (!MO.isJTI() && MO.getOffset())
137 Expr = MCBinaryExpr::createAdd(Expr,
139 Ctx);
140
141 // Subtract off the PIC base if required.
145 const MachineFunction *MF = MO.getParent()->getParent()->getParent();
146
148 Expr = MCBinaryExpr::createSub(Expr, PB, Ctx);
149 }
150
151 // Add ha16() / lo16() markers if required.
152 switch (access) {
153 case PPCII::MO_LO:
155 Expr = PPCMCExpr::createLo(Expr, Ctx);
156 break;
157 case PPCII::MO_HA:
159 Expr = PPCMCExpr::createHa(Expr, Ctx);
160 break;
161 }
162
163 return MCOperand::createExpr(Expr);
164}
165
167 AsmPrinter &AP) {
168 OutMI.setOpcode(MI->getOpcode());
169
170 for (const MachineOperand &MO : MI->operands()) {
171 MCOperand MCOp;
172 if (LowerPPCMachineOperandToMCOperand(MO, MCOp, AP))
173 OutMI.addOperand(MCOp);
174 }
175}
176
178 MCOperand &OutMO, AsmPrinter &AP) {
179 switch (MO.getType()) {
180 default:
181 llvm_unreachable("unknown operand type");
183 assert(!MO.getSubReg() && "Subregs should be eliminated!");
184 assert(MO.getReg() > PPC::NoRegister &&
185 MO.getReg() < PPC::NUM_TARGET_REGS &&
186 "Invalid register for this target!");
187 // Ignore all implicit register operands.
188 if (MO.isImplicit())
189 return false;
190 OutMO = MCOperand::createReg(MO.getReg());
191 return true;
193 OutMO = MCOperand::createImm(MO.getImm());
194 return true;
196 OutMO = MCOperand::createExpr(
198 return true;
201 OutMO = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP);
202 return true;
204 OutMO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP);
205 return true;
207 OutMO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP);
208 return true;
210 OutMO =
212 return true;
214 OutMO = GetSymbolRef(MO, MO.getMCSymbol(), AP);
215 return true;
217 return false;
218 }
219}
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:655
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:76
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:200
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:327
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:119
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.
@ MO_TLSLD_LO
Definition: PPC.h:184
@ MO_PIC_LO_FLAG
MO_PIC_LO_FLAG = MO_PIC_FLAG | MO_LO.
Definition: PPC.h:194
@ MO_TPREL_PCREL_FLAG
MO_TPREL_PCREL_FLAG = MO_PCREL_FLAG | MO_TPREL_FLAG.
Definition: PPC.h:197
@ MO_GOT_TPREL_PCREL_FLAG
MO_GOT_TPREL_PCREL_FLAG - A combintaion of flags, if these bits are set they should produce the reloc...
Definition: PPC.h:172
@ MO_GOT_PCREL_FLAG
MO_GOT_PCREL_FLAG = MO_PCREL_FLAG | MO_GOT_FLAG.
Definition: PPC.h:203
@ MO_PCREL_FLAG
MO_PCREL_FLAG - If this bit is set, the symbol reference is relative to the current instruction addre...
Definition: PPC.h:121
@ MO_TLS_PCREL_FLAG
MO_TPREL_PCREL_FLAG = MO_PCREL_FLAG | MO_TLS.
Definition: PPC.h:200
@ MO_TPREL_HA
Definition: PPC.h:179
@ MO_PLT
On PPC, the 12 bits are not enough for all target operand flags.
Definition: PPC.h:113
@ MO_DTPREL_LO
These values identify relocations on immediates folded into memory operations.
Definition: PPC.h:183
@ MO_TLS
Symbol for VK_PPC_TLS fixup attached to an ADD instruction.
Definition: PPC.h:188
@ MO_TPREL_FLAG
MO_TPREL_FLAG - If this bit is set, the symbol reference is relative to the thread pointer and the sy...
Definition: PPC.h:140
@ MO_TPREL_LO
Definition: PPC.h:178
@ MO_LO
MO_LO, MO_HA - lo16(symbol) and ha16(symbol)
Definition: PPC.h:175
@ MO_GOT_TLSLD_PCREL_FLAG
MO_GOT_TLSLD_PCREL_FLAG - A combintaion of flags, if these bits are set they should produce the reloc...
Definition: PPC.h:166
@ MO_PIC_HA_FLAG
MO_PIC_HA_FLAG = MO_PIC_FLAG | MO_HA.
Definition: PPC.h:191
@ MO_TOC_LO
Definition: PPC.h:185
@ MO_GOT_TLSGD_PCREL_FLAG
MO_GOT_TLSGD_PCREL_FLAG - A combintaion of flags, if these bits are set they should produce the reloc...
Definition: PPC.h:160
@ MO_HA
Definition: PPC.h:176
@ MO_PIC_FLAG
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
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)