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"
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/MC/MCAsmInfo.h"
28#include "llvm/MC/MCExpr.h"
29#include "llvm/MC/MCInst.h"
31using namespace llvm;
32
34 AsmPrinter &AP) {
35 if (MO.isGlobal()) {
36 // Get the symbol from the global, accounting for XCOFF-specific
37 // intricacies (see TargetLoweringObjectFileXCOFF::getTargetSymbol).
38 const GlobalValue *GV = MO.getGlobal();
39 return AP.getSymbol(GV);
40 }
41
42 assert(MO.isSymbol() && "Isn't a symbol reference");
43
45 const DataLayout &DL = AP.getDataLayout();
47
48 MCContext &Ctx = AP.OutContext;
50 return Sym;
51}
52
53static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
55 MCContext &Ctx = Printer.OutContext;
57
58 unsigned access = MO.getTargetFlags();
59
60 switch (access) {
63 break;
66 break;
69 break;
72 break;
75 break;
76 case PPCII::MO_TLS:
78 break;
81 break;
82 }
83
84 const TargetMachine &TM = Printer.TM;
85 const MachineInstr *MI = MO.getParent();
86 const MachineFunction *MF = MI->getMF();
87
88 if (MO.getTargetFlags() == PPCII::MO_PLT)
90 else if (MO.getTargetFlags() == PPCII::MO_PCREL_FLAG)
102 else if (MO.getTargetFlags() == PPCII::MO_TPREL_FLAG ||
104 assert(MO.isGlobal() && "Only expecting a global MachineOperand here!");
105 TLSModel::Model Model = TM.getTLSModel(MO.getGlobal());
106 const PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
107 // For the local-[exec|dynamic] TLS model, we may generate the offset from
108 // the TLS base as an immediate operand (instead of using a TOC entry). Set
109 // the relocation type in case the result is used for purposes other than a
110 // TOC reference. In TOC reference cases, this result is discarded.
111 if (Model == TLSModel::LocalExec)
113 else if (Model == TLSModel::LocalDynamic &&
114 FuncInfo->isAIXFuncUseTLSIEForLD())
115 // On AIX, TLS model opt may have turned local-dynamic accesses into
116 // initial-exec accesses.
118 else if (Model == TLSModel::LocalDynamic)
120 }
121
122 const Module *M = MF->getFunction().getParent();
123 const PPCSubtarget *Subtarget = &(MF->getSubtarget<PPCSubtarget>());
124
125 unsigned MIOpcode = MI->getOpcode();
126 assert((Subtarget->isUsingPCRelativeCalls() || MIOpcode != PPC::BL8_NOTOC) &&
127 "BL8_NOTOC is only valid when using PC Relative Calls.");
128 if (Subtarget->isUsingPCRelativeCalls()) {
129 if (MIOpcode == PPC::TAILB || MIOpcode == PPC::TAILB8 ||
130 MIOpcode == PPC::TCRETURNdi || MIOpcode == PPC::TCRETURNdi8 ||
131 MIOpcode == PPC::BL8_NOTOC || MIOpcode == PPC::BL8_NOTOC_RM) {
133 }
136 }
137
138 const MCExpr *Expr = MCSymbolRefExpr::create(Symbol, RefKind, Ctx);
139 // If -msecure-plt -fPIC, add 32768 to symbol.
140 if (Subtarget->isSecurePlt() && TM.isPositionIndependent() &&
141 M->getPICLevel() == PICLevel::BigPIC &&
143 Expr =
144 MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(32768, Ctx), Ctx);
145
146 if (!MO.isJTI() && MO.getOffset())
147 Expr = MCBinaryExpr::createAdd(Expr,
149 Ctx);
150
151 // Subtract off the PIC base if required.
155 const MachineFunction *MF = MO.getParent()->getParent()->getParent();
156
158 Expr = MCBinaryExpr::createSub(Expr, PB, Ctx);
159 }
160
161 // Add ha16() / lo16() markers if required.
162 switch (access) {
163 case PPCII::MO_LO:
165 Expr = PPCMCExpr::createLo(Expr, Ctx);
166 break;
167 case PPCII::MO_HA:
169 Expr = PPCMCExpr::createHa(Expr, Ctx);
170 break;
171 }
172
173 return MCOperand::createExpr(Expr);
174}
175
177 AsmPrinter &AP) {
178 OutMI.setOpcode(MI->getOpcode());
179
180 for (const MachineOperand &MO : MI->operands()) {
181 MCOperand MCOp;
182 if (LowerPPCMachineOperandToMCOperand(MO, MCOp, AP))
183 OutMI.addOperand(MCOp);
184 }
185}
186
188 MCOperand &OutMO, AsmPrinter &AP) {
189 switch (MO.getType()) {
190 default:
191 llvm_unreachable("unknown operand type");
193 assert(!MO.getSubReg() && "Subregs should be eliminated!");
194 assert(MO.getReg() > PPC::NoRegister &&
195 MO.getReg() < PPC::NUM_TARGET_REGS &&
196 "Invalid register for this target!");
197 // Ignore all implicit register operands.
198 if (MO.isImplicit())
199 return false;
200 OutMO = MCOperand::createReg(MO.getReg());
201 return true;
203 OutMO = MCOperand::createImm(MO.getImm());
204 return true;
206 OutMO = MCOperand::createExpr(
208 return true;
211 OutMO = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP);
212 return true;
214 OutMO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP);
215 return true;
217 OutMO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP);
218 return true;
220 OutMO =
222 return true;
224 OutMO = GetSymbolRef(MO, MO.getMCSymbol(), AP);
225 return true;
227 return false;
228 }
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
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:704
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:403
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.
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:341
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: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_TLSLD_FLAG
MO_TLSLD_FLAG - If this bit is set the symbol reference is relative to TLS Local Dynamic model.
Definition: PPC.h:150
@ 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)