LLVM 17.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 const TargetMachine &TM = AP.TM;
35 Mangler &Mang = TM.getObjFileLowering()->getMangler();
36 const DataLayout &DL = AP.getDataLayout();
37 MCContext &Ctx = AP.OutContext;
40 if (!MO.isGlobal()) {
41 assert(MO.isSymbol() && "Isn't a symbol reference");
43 } else {
44 const GlobalValue *GV = MO.getGlobal();
45 TM.getNameWithPrefix(Name, GV, Mang);
46 }
50 return Sym;
53static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
54 AsmPrinter &Printer) {
55 MCContext &Ctx = Printer.OutContext;
58 unsigned access = MO.getTargetFlags() & PPCII::MO_ACCESS_MASK;
60 switch (access) {
63 break;
66 break;
69 break;
72 break;
75 break;
76 case PPCII::MO_TLS:
77 bool IsPCRel = (MO.getTargetFlags() & ~access) == PPCII::MO_PCREL_FLAG;
78 RefKind = IsPCRel ? MCSymbolRefExpr::VK_PPC_TLS_PCREL
80 break;
81 }
83 if (MO.getTargetFlags() == PPCII::MO_PLT)
85 else if (MO.getTargetFlags() == PPCII::MO_PCREL_FLAG)
98 const MachineInstr *MI = MO.getParent();
99 const MachineFunction *MF = MI->getMF();
100 const Module *M = MF->getFunction().getParent();
101 const PPCSubtarget *Subtarget = &(MF->getSubtarget<PPCSubtarget>());
102 const TargetMachine &TM = Printer.TM;
104 unsigned MIOpcode = MI->getOpcode();
105 assert((Subtarget->isUsingPCRelativeCalls() || MIOpcode != PPC::BL8_NOTOC) &&
106 "BL8_NOTOC is only valid when using PC Relative Calls.");
107 if (Subtarget->isUsingPCRelativeCalls()) {
108 if (MIOpcode == PPC::TAILB || MIOpcode == PPC::TAILB8 ||
109 MIOpcode == PPC::TCRETURNdi || MIOpcode == PPC::TCRETURNdi8 ||
110 MIOpcode == PPC::BL8_NOTOC || MIOpcode == PPC::BL8_NOTOC_RM) {
112 }
115 }
117 const MCExpr *Expr = MCSymbolRefExpr::create(Symbol, RefKind, Ctx);
118 // If -msecure-plt -fPIC, add 32768 to symbol.
119 if (Subtarget->isSecurePlt() && TM.isPositionIndependent() &&
120 M->getPICLevel() == PICLevel::BigPIC &&
122 Expr =
123 MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(32768, Ctx), Ctx);
125 if (!MO.isJTI() && MO.getOffset())
126 Expr = MCBinaryExpr::createAdd(Expr,
128 Ctx);
130 // Subtract off the PIC base if required.
132 const MachineFunction *MF = MO.getParent()->getParent()->getParent();
135 Expr = MCBinaryExpr::createSub(Expr, PB, Ctx);
136 }
138 // Add ha16() / lo16() markers if required.
139 switch (access) {
140 case PPCII::MO_LO:
141 Expr = PPCMCExpr::createLo(Expr, Ctx);
142 break;
143 case PPCII::MO_HA:
144 Expr = PPCMCExpr::createHa(Expr, Ctx);
145 break;
146 }
148 return MCOperand::createExpr(Expr);
152 AsmPrinter &AP) {
153 OutMI.setOpcode(MI->getOpcode());
155 for (const MachineOperand &MO : MI->operands()) {
156 MCOperand MCOp;
157 if (LowerPPCMachineOperandToMCOperand(MO, MCOp, AP))
158 OutMI.addOperand(MCOp);
159 }
163 MCOperand &OutMO, AsmPrinter &AP) {
164 switch (MO.getType()) {
165 default:
166 llvm_unreachable("unknown operand type");
168 assert(!MO.getSubReg() && "Subregs should be eliminated!");
169 assert(MO.getReg() > PPC::NoRegister &&
170 MO.getReg() < PPC::NUM_TARGET_REGS &&
171 "Invalid register for this target!");
172 // Ignore all implicit register operands.
173 if (MO.isImplicit())
174 return false;
175 OutMO = MCOperand::createReg(MO.getReg());
176 return true;
178 OutMO = MCOperand::createImm(MO.getImm());
179 return true;
181 OutMO = MCOperand::createExpr(
183 return true;
186 OutMO = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP);
187 return true;
189 OutMO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP);
190 return true;
192 OutMO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP);
193 return true;
195 OutMO =
197 return true;
199 OutMO = GetSymbolRef(MO, MO.getMCSymbol(), AP);
200 return true;
202 return false;
203 }
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
std::string Name
IRTranslator LLVM IR MI
Memory true print Memory SSA Printer
Definition: MemorySSA.cpp:78
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
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:87
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:383
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:652
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:525
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:610
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: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:386
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.
Representation of each machine instruction.
Definition: MachineInstr.h:68
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:313
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:78
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: PPC.h:170
MO_GOT_TPREL_PCREL_FLAG - A combintaion of flags, if these bits are set they should produce the reloc...
Definition: PPC.h:155
The next are not flags but distinct values.
Definition: PPC.h:158
MO_PCREL_FLAG - If this bit is set, the symbol reference is relative to the current instruction addre...
Definition: PPC.h:114
MO_GOT_FLAG - If this bit is set the symbol reference is to be computed via the GOT.
Definition: PPC.h:119
Definition: PPC.h:165
On a symbol operand "FOO", this indicates that the reference is actually to "FOO@plt".
Definition: PPC.h:106
These values identify relocations on immediates folded into memory operations.
Definition: PPC.h:169
Definition: PPC.h:174
MO_TPREL_FLAG - If this bit is set the symbol reference is relative to TLS Initial Exec model.
Definition: PPC.h:132
Definition: PPC.h:164
MO_LO, MO_HA - lo16(symbol) and ha16(symbol)
Definition: PPC.h:161
MO_GOT_TLSLD_PCREL_FLAG - A combintaion of flags, if these bits are set they should produce the reloc...
Definition: PPC.h:150
Definition: PPC.h:171
MO_GOT_TLSGD_PCREL_FLAG - A combintaion of flags, if these bits are set they should produce the reloc...
Definition: PPC.h:145
Definition: PPC.h:162
MO_PIC_FLAG - If this bit is set, the symbol reference is relative to the function's picbase,...
Definition: PPC.h:110
Definition: PPC.h:123
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)