LLVM  10.0.0svn
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 
14 #include "MCTargetDesc/PPCMCExpr.h"
15 #include "PPC.h"
16 #include "PPCSubtarget.h"
17 #include "llvm/ADT/SmallString.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"
30 using namespace llvm;
31 
34 }
35 
37  AsmPrinter &AP) {
38  const TargetMachine &TM = AP.TM;
39  Mangler &Mang = TM.getObjFileLowering()->getMangler();
40  const DataLayout &DL = AP.getDataLayout();
41  MCContext &Ctx = AP.OutContext;
42 
44  StringRef Suffix;
46  Suffix = "$non_lazy_ptr";
47 
48  if (!Suffix.empty())
49  Name += DL.getPrivateGlobalPrefix();
50 
51  if (!MO.isGlobal()) {
52  assert(MO.isSymbol() && "Isn't a symbol reference");
54  } else {
55  const GlobalValue *GV = MO.getGlobal();
56  TM.getNameWithPrefix(Name, GV, Mang);
57  }
58 
59  Name += Suffix;
60  MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
61 
62  // If the symbol reference is actually to a non_lazy_ptr, not to the symbol,
63  // then add the suffix.
66 
68 
69  if (!StubSym.getPointer()) {
70  assert(MO.isGlobal() && "Extern symbol not handled yet");
71  StubSym = MachineModuleInfoImpl::
73  !MO.getGlobal()->hasInternalLinkage());
74  }
75  return Sym;
76  }
77 
78  return Sym;
79 }
80 
82  AsmPrinter &Printer, bool IsDarwin) {
83  MCContext &Ctx = Printer.OutContext;
85 
87 
88  switch (access) {
89  case PPCII::MO_TPREL_LO:
91  break;
92  case PPCII::MO_TPREL_HA:
94  break;
97  break;
98  case PPCII::MO_TLSLD_LO:
100  break;
101  case PPCII::MO_TOC_LO:
103  break;
104  case PPCII::MO_TLS:
105  RefKind = MCSymbolRefExpr::VK_PPC_TLS;
106  break;
107  }
108 
109  if (MO.getTargetFlags() == PPCII::MO_PLT)
110  RefKind = MCSymbolRefExpr::VK_PLT;
111 
112  const MachineFunction *MF = MO.getParent()->getParent()->getParent();
113  const Module *M = MF->getFunction().getParent();
114  const PPCSubtarget *Subtarget = &(MF->getSubtarget<PPCSubtarget>());
115  const TargetMachine &TM = Printer.TM;
116  const MCExpr *Expr = MCSymbolRefExpr::create(Symbol, RefKind, Ctx);
117  // If -msecure-plt -fPIC, add 32768 to symbol.
118  if (Subtarget->isSecurePlt() && TM.isPositionIndependent() &&
119  M->getPICLevel() == PICLevel::BigPIC &&
121  Expr =
122  MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(32768, Ctx), Ctx);
123 
124  if (!MO.isJTI() && MO.getOffset())
125  Expr = MCBinaryExpr::createAdd(Expr,
127  Ctx);
128 
129  // Subtract off the PIC base if required.
130  if (MO.getTargetFlags() & PPCII::MO_PIC_FLAG) {
131  const MachineFunction *MF = MO.getParent()->getParent()->getParent();
132 
133  const MCExpr *PB = MCSymbolRefExpr::create(MF->getPICBaseSymbol(), Ctx);
134  Expr = MCBinaryExpr::createSub(Expr, PB, Ctx);
135  }
136 
137  // Add ha16() / lo16() markers if required.
138  switch (access) {
139  case PPCII::MO_LO:
140  Expr = PPCMCExpr::createLo(Expr, IsDarwin, Ctx);
141  break;
142  case PPCII::MO_HA:
143  Expr = PPCMCExpr::createHa(Expr, IsDarwin, Ctx);
144  break;
145  }
146 
147  return MCOperand::createExpr(Expr);
148 }
149 
151  AsmPrinter &AP, bool IsDarwin) {
152  OutMI.setOpcode(MI->getOpcode());
153 
154  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
155  MCOperand MCOp;
156  if (LowerPPCMachineOperandToMCOperand(MI->getOperand(i), MCOp, AP,
157  IsDarwin))
158  OutMI.addOperand(MCOp);
159  }
160 }
161 
163  MCOperand &OutMO, AsmPrinter &AP,
164  bool IsDarwin) {
165  switch (MO.getType()) {
166  default:
167  llvm_unreachable("unknown operand type");
169  assert(!MO.getSubReg() && "Subregs should be eliminated!");
170  assert(MO.getReg() > PPC::NoRegister &&
171  MO.getReg() < PPC::NUM_TARGET_REGS &&
172  "Invalid register for this target!");
173  OutMO = MCOperand::createReg(MO.getReg());
174  return true;
176  OutMO = MCOperand::createImm(MO.getImm());
177  return true;
179  OutMO = MCOperand::createExpr(
181  return true;
184  OutMO = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP, IsDarwin);
185  return true;
187  OutMO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, IsDarwin);
188  return true;
190  OutMO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, IsDarwin);
191  return true;
193  OutMO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP,
194  IsDarwin);
195  return true;
197  OutMO = GetSymbolRef(MO, MO.getMCSymbol(), AP, IsDarwin);
198  return true;
200  return false;
201  }
202 }
unsigned getTargetFlags() const
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:112
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
MachineBasicBlock * getMBB() const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:329
This class represents lattice values for constants.
Definition: AllocatorList.h:23
PointerTy getPointer() const
StringRef getPrivateGlobalPrefix() const
Definition: DataLayout.h:321
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:88
static MachineModuleInfoMachO & getMachOMMI(AsmPrinter &AP)
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:136
MO_LO, MO_HA - lo16(symbol) and ha16(symbol)
Definition: PPC.h:110
Address of indexed Jump Table for switch.
unsigned getSubReg() const
PointerIntPair< MCSymbol *, 1, bool > StubValueTy
MachineBasicBlock reference.
print alias Alias Set Printer
static const PPCMCExpr * createLo(const MCExpr *Expr, bool IsDarwin, MCContext &Ctx)
Definition: PPCMCExpr.h:50
Mask of preserved registers.
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:115
unsigned getNumOperands() const
Retuns the total number of operands.
Definition: MachineInstr.h:413
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Name of external global symbol.
void getNameWithPrefix(SmallVectorImpl< char > &Name, const GlobalValue *GV, Mangler &Mang, bool MayAlwaysUsePrivate=false) const
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:410
const char * getSymbolName() const
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:140
Context object for machine code objects.
Definition: MCContext.h:65
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:552
void LowerPPCMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI, AsmPrinter &AP, bool IsDarwin)
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:467
MachineModuleInfo * MMI
This is a pointer to the current MachineModuleInfo.
Definition: AsmPrinter.h:99
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false)
Definition: MCExpr.cpp:169
Address of a global value.
bool isSecurePlt() const
Definition: PPCSubtarget.h:269
PointerIntPair - This class implements a pair of a pointer and small integer.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MO_NLP_FLAG - If this bit is set, the symbol reference is actually to the non_lazy_ptr for the global...
Definition: PPC.h:99
bool LowerPPCMachineOperandToMCOperand(const MachineOperand &MO, MCOperand &OutMO, AsmPrinter &AP, bool IsDarwin)
const GlobalValue * getGlobal() const
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:81
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
bool hasInternalLinkage() const
Definition: GlobalValue.h:443
Address of a basic block.
static const PPCMCExpr * createHa(const MCExpr *Expr, bool IsDarwin, MCContext &Ctx)
Definition: PPCMCExpr.h:60
constexpr double e
Definition: MathExtras.h:57
Ty & getObjFileInfo()
Keep track of various per-function pieces of information for backends that would like to do so...
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
StubValueTy & getGVStubEntry(MCSymbol *Sym)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
The next are not flags but distinct values.
Definition: PPC.h:107
void setOpcode(unsigned Op)
Definition: MCInst.h:170
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
virtual TargetLoweringObjectFile * getObjFileLowering() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:449
MachineOperand class - Representation of each machine instruction operand.
static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol, AsmPrinter &Printer, bool IsDarwin)
int64_t getImm() const
These values identify relocations on immediates folded into memory operations.
Definition: PPC.h:118
const Function & getFunction() const
Return the LLVM function that this machine code represents.
MCSymbol reference (for debug/eh info)
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:255
Representation of each machine instruction.
Definition: MachineInstr.h:63
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:129
bool isPositionIndependent() const
int64_t getOffset() const
Return the offset from the symbol in this operand.
const BlockAddress * getBlockAddress() const
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
MachineModuleInfoMachO - This is a MachineModuleInfoImpl implementation for MachO targets...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MCSymbol * getMCSymbol() const
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:575
MCSymbol * getPICBaseSymbol() const
getPICBaseSymbol - Return a function-local symbol to represent the PIC base.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:65
std::error_code access(const Twine &Path, AccessMode Mode)
Can the file be accessed?
PICLevel::Level getPICLevel() const
Returns the PIC level (small or large model)
Definition: Module.cpp:488
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable&#39;s name.
Definition: Mangler.cpp:111
const DataLayout & getDataLayout() const
Return information about data layout.
Definition: AsmPrinter.cpp:220
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:183
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Address of indexed Constant in Constant Pool.
static MCSymbol * GetSymbolFromOperand(const MachineOperand &MO, AsmPrinter &AP)
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:415
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:34
On a symbol operand "FOO", this indicates that the reference is actually to "FOO@plt".
Definition: PPC.h:91
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:122
MO_PIC_FLAG - If this bit is set, the symbol reference is relative to the function&#39;s picbase...
Definition: PPC.h:95
This file describes how to lower LLVM code to machine code.