LLVM  8.0.1
PPCMCCodeEmitter.cpp
Go to the documentation of this file.
1 //===-- PPCMCCodeEmitter.cpp - Convert PPC code to machine code -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the PPCMCCodeEmitter class.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "PPCInstrInfo.h"
16 #include "PPCMCCodeEmitter.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/MC/MCFixup.h"
21 #include "llvm/MC/MCInstrDesc.h"
22 #include "llvm/MC/MCRegisterInfo.h"
23 #include "llvm/Support/Endian.h"
28 #include <cassert>
29 #include <cstdint>
30 
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "mccodeemitter"
34 
35 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
36 
38  const MCRegisterInfo &MRI,
39  MCContext &Ctx) {
40  return new PPCMCCodeEmitter(MCII, Ctx);
41 }
42 
43 unsigned PPCMCCodeEmitter::
44 getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
46  const MCSubtargetInfo &STI) const {
47  const MCOperand &MO = MI.getOperand(OpNo);
48  if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
49 
50  // Add a fixup for the branch target.
51  Fixups.push_back(MCFixup::create(0, MO.getExpr(),
53  return 0;
54 }
55 
56 unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
58  const MCSubtargetInfo &STI) const {
59  const MCOperand &MO = MI.getOperand(OpNo);
60  if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
61 
62  // Add a fixup for the branch target.
63  Fixups.push_back(MCFixup::create(0, MO.getExpr(),
65  return 0;
66 }
67 
68 unsigned PPCMCCodeEmitter::
69 getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo,
71  const MCSubtargetInfo &STI) const {
72  const MCOperand &MO = MI.getOperand(OpNo);
73  if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
74 
75  // Add a fixup for the branch target.
76  Fixups.push_back(MCFixup::create(0, MO.getExpr(),
78  return 0;
79 }
80 
81 unsigned PPCMCCodeEmitter::
82 getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo,
84  const MCSubtargetInfo &STI) const {
85  const MCOperand &MO = MI.getOperand(OpNo);
86  if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
87 
88  // Add a fixup for the branch target.
89  Fixups.push_back(MCFixup::create(0, MO.getExpr(),
91  return 0;
92 }
93 
94 unsigned PPCMCCodeEmitter::getImm16Encoding(const MCInst &MI, unsigned OpNo,
96  const MCSubtargetInfo &STI) const {
97  const MCOperand &MO = MI.getOperand(OpNo);
98  if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
99 
100  // Add a fixup for the immediate field.
101  Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(),
103  return 0;
104 }
105 
106 unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
108  const MCSubtargetInfo &STI) const {
109  // Encode (imm, reg) as a memri, which has the low 16-bits as the
110  // displacement and the next 5 bits as the register #.
111  assert(MI.getOperand(OpNo+1).isReg());
112  unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 16;
113 
114  const MCOperand &MO = MI.getOperand(OpNo);
115  if (MO.isImm())
116  return (getMachineOpValue(MI, MO, Fixups, STI) & 0xFFFF) | RegBits;
117 
118  // Add a fixup for the displacement field.
119  Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(),
121  return RegBits;
122 }
123 
124 unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
126  const MCSubtargetInfo &STI) const {
127  // Encode (imm, reg) as a memrix, which has the low 14-bits as the
128  // displacement and the next 5 bits as the register #.
129  assert(MI.getOperand(OpNo+1).isReg());
130  unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 14;
131 
132  const MCOperand &MO = MI.getOperand(OpNo);
133  if (MO.isImm())
134  return ((getMachineOpValue(MI, MO, Fixups, STI) >> 2) & 0x3FFF) | RegBits;
135 
136  // Add a fixup for the displacement field.
137  Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(),
139  return RegBits;
140 }
141 
142 unsigned PPCMCCodeEmitter::getMemRIX16Encoding(const MCInst &MI, unsigned OpNo,
144  const MCSubtargetInfo &STI) const {
145  // Encode (imm, reg) as a memrix16, which has the low 12-bits as the
146  // displacement and the next 5 bits as the register #.
147  assert(MI.getOperand(OpNo+1).isReg());
148  unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 12;
149 
150  const MCOperand &MO = MI.getOperand(OpNo);
151  if (MO.isImm()) {
152  assert(!(MO.getImm() % 16) &&
153  "Expecting an immediate that is a multiple of 16");
154  return ((getMachineOpValue(MI, MO, Fixups, STI) >> 4) & 0xFFF) | RegBits;
155  }
156 
157  // Otherwise add a fixup for the displacement field.
158  Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(),
160  return RegBits;
161 }
162 
163 unsigned PPCMCCodeEmitter::getSPE8DisEncoding(const MCInst &MI, unsigned OpNo,
165  const MCSubtargetInfo &STI)
166  const {
167  // Encode (imm, reg) as a spe8dis, which has the low 5-bits of (imm / 8)
168  // as the displacement and the next 5 bits as the register #.
169  assert(MI.getOperand(OpNo+1).isReg());
170  uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
171 
172  const MCOperand &MO = MI.getOperand(OpNo);
173  assert(MO.isImm());
174  uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 3;
175  return reverseBits(Imm | RegBits) >> 22;
176 }
177 
178 unsigned PPCMCCodeEmitter::getSPE4DisEncoding(const MCInst &MI, unsigned OpNo,
180  const MCSubtargetInfo &STI)
181  const {
182  // Encode (imm, reg) as a spe4dis, which has the low 5-bits of (imm / 4)
183  // as the displacement and the next 5 bits as the register #.
184  assert(MI.getOperand(OpNo+1).isReg());
185  uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
186 
187  const MCOperand &MO = MI.getOperand(OpNo);
188  assert(MO.isImm());
189  uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 2;
190  return reverseBits(Imm | RegBits) >> 22;
191 }
192 
193 unsigned PPCMCCodeEmitter::getSPE2DisEncoding(const MCInst &MI, unsigned OpNo,
195  const MCSubtargetInfo &STI)
196  const {
197  // Encode (imm, reg) as a spe2dis, which has the low 5-bits of (imm / 2)
198  // as the displacement and the next 5 bits as the register #.
199  assert(MI.getOperand(OpNo+1).isReg());
200  uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
201 
202  const MCOperand &MO = MI.getOperand(OpNo);
203  assert(MO.isImm());
204  uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 1;
205  return reverseBits(Imm | RegBits) >> 22;
206 }
207 
208 unsigned PPCMCCodeEmitter::getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
210  const MCSubtargetInfo &STI) const {
211  const MCOperand &MO = MI.getOperand(OpNo);
212  if (MO.isReg()) return getMachineOpValue(MI, MO, Fixups, STI);
213 
214  // Add a fixup for the TLS register, which simply provides a relocation
215  // hint to the linker that this statement is part of a relocation sequence.
216  // Return the thread-pointer register's encoding.
217  Fixups.push_back(MCFixup::create(0, MO.getExpr(),
219  const Triple &TT = STI.getTargetTriple();
220  bool isPPC64 = TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le;
221  return CTX.getRegisterInfo()->getEncodingValue(isPPC64 ? PPC::X13 : PPC::R2);
222 }
223 
224 unsigned PPCMCCodeEmitter::getTLSCallEncoding(const MCInst &MI, unsigned OpNo,
226  const MCSubtargetInfo &STI) const {
227  // For special TLS calls, we need two fixups; one for the branch target
228  // (__tls_get_addr), which we create via getDirectBrEncoding as usual,
229  // and one for the TLSGD or TLSLD symbol, which is emitted here.
230  const MCOperand &MO = MI.getOperand(OpNo+1);
231  Fixups.push_back(MCFixup::create(0, MO.getExpr(),
233  return getDirectBrEncoding(MI, OpNo, Fixups, STI);
234 }
235 
236 unsigned PPCMCCodeEmitter::
237 get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
239  const MCSubtargetInfo &STI) const {
240  const MCOperand &MO = MI.getOperand(OpNo);
241  assert((MI.getOpcode() == PPC::MTOCRF || MI.getOpcode() == PPC::MTOCRF8 ||
242  MI.getOpcode() == PPC::MFOCRF || MI.getOpcode() == PPC::MFOCRF8) &&
243  (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
244  return 0x80 >> CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
245 }
246 
247 // Get the index for this operand in this instruction. This is needed for
248 // computing the register number in PPCInstrInfo::getRegNumForOperand() for
249 // any instructions that use a different numbering scheme for registers in
250 // different operands.
251 static unsigned getOpIdxForMO(const MCInst &MI, const MCOperand &MO) {
252  for (unsigned i = 0; i < MI.getNumOperands(); i++) {
253  const MCOperand &Op = MI.getOperand(i);
254  if (&Op == &MO)
255  return i;
256  }
257  llvm_unreachable("This operand is not part of this instruction");
258  return ~0U; // Silence any warnings about no return.
259 }
260 
261 unsigned PPCMCCodeEmitter::
264  const MCSubtargetInfo &STI) const {
265  if (MO.isReg()) {
266  // MTOCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
267  // The GPR operand should come through here though.
268  assert((MI.getOpcode() != PPC::MTOCRF && MI.getOpcode() != PPC::MTOCRF8 &&
269  MI.getOpcode() != PPC::MFOCRF && MI.getOpcode() != PPC::MFOCRF8) ||
270  MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
271  unsigned OpNo = getOpIdxForMO(MI, MO);
272  unsigned Reg =
274  MO.getReg(), OpNo);
275  return CTX.getRegisterInfo()->getEncodingValue(Reg);
276  }
277 
278  assert(MO.isImm() &&
279  "Relocation required in an instruction that we cannot encode!");
280  return MO.getImm();
281 }
282 
285  const MCSubtargetInfo &STI) const {
286  verifyInstructionPredicates(MI,
287  computeAvailableFeatures(STI.getFeatureBits()));
288 
289  uint64_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
290 
291  // Output the constant in big/little endian byte order.
292  unsigned Size = getInstSizeInBytes(MI);
293  support::endianness E = IsLittleEndian ? support::little : support::big;
294  switch (Size) {
295  case 0:
296  break;
297  case 4:
298  support::endian::write<uint32_t>(OS, Bits, E);
299  break;
300  case 8:
301  // If we emit a pair of instructions, the first one is
302  // always in the top 32 bits, even on little-endian.
303  support::endian::write<uint32_t>(OS, Bits >> 32, E);
304  support::endian::write<uint32_t>(OS, Bits, E);
305  break;
306  default:
307  llvm_unreachable("Invalid instruction size");
308  }
309 
310  ++MCNumEmitted; // Keep track of the # of mi's emitted.
311 }
312 
313 // Get the number of bytes used to encode the given MCInst.
315  unsigned Opcode = MI.getOpcode();
316  const MCInstrDesc &Desc = MCII.get(Opcode);
317  return Desc.getSize();
318 }
319 
320 #define ENABLE_INSTR_PREDICATE_VERIFIER
321 #include "PPCGenMCCodeEmitter.inc"
unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getTLSRegEncoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
bool isImm() const
Definition: MCInst.h:59
This class represents lattice values for constants.
Definition: AllocatorList.h:24
void encodeInstruction(const MCInst &MI, raw_ostream &OS, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const override
EncodeInstruction - Encode the given Inst to bytes on the output stream OS.
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:164
unsigned Reg
bool isReg() const
Definition: MCInst.h:58
STATISTIC(NumFunctions, "Total number of functions")
14-bit absolute relocation for conditional branches.
Definition: PPCFixupKinds.h:30
#define R2(n)
unsigned getImm16Encoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
const Triple & getTargetTriple() const
A 16-bit fixup corresponding to lo16(_foo) or ha16(_foo) for instrs like &#39;li&#39; or &#39;addis&#39;.
Definition: PPCFixupKinds.h:34
24-bit absolute relocation for direct branches like &#39;ba&#39; and &#39;bla&#39;.
Definition: PPCFixupKinds.h:27
const FeatureBitset & getFeatureBits() const
unsigned getMemRIX16Encoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
Not a true fixup, but ties a symbol to a call to __tls_get_addr for the TLS general and local dynamic...
Definition: PPCFixupKinds.h:43
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:65
Context object for machine code objects.
Definition: MCContext.h:63
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:290
unsigned getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getSPE4DisEncoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
R32 = MFOCRF(CRREG, INFLAG) - Represents the MFOCRF instruction.
const MCExpr * getExpr() const
Definition: MCInst.h:96
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
int64_t getImm() const
Definition: MCInst.h:76
unsigned const MachineRegisterInfo * MRI
unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
getMachineOpValue - Return binary encoding of operand.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:22
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:24
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:23
unsigned getInstSizeInBytes(const MCInst &MI) const
unsigned getNumOperands() const
Definition: MCInst.h:184
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Definition: MCFixup.h:90
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned getSPE2DisEncoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
uint64_t getBinaryCodeForInstr(const MCInst &MI, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
static unsigned getRegNumForOperand(const MCInstrDesc &Desc, unsigned Reg, unsigned OpNo)
getRegNumForOperand - some operands use different numbering schemes for the same registers.
Definition: PPCInstrInfo.h:430
static unsigned getOpIdxForMO(const MCInst &MI, const MCOperand &MO)
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:182
unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getTLSCallEncoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
A 14-bit fixup corresponding to lo16(_foo) with implied 2 zero bits for instrs like &#39;std&#39;...
Definition: PPCFixupKinds.h:38
uint16_t getEncodingValue(unsigned RegNo) const
Returns the encoding for RegNo.
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
Definition: MCInstrInfo.h:45
Generic base class for all target subtargets.
unsigned getSPE8DisEncoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
uint32_t Size
Definition: Profile.cpp:47
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCRegisterInfo * getRegisterInfo() const
Definition: MCContext.h:295
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
IRTranslator LLVM IR MI
14-bit PC relative relocation for conditional branches.
Definition: PPCFixupKinds.h:24
unsigned getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getOpcode() const
Definition: MCInst.h:174
T reverseBits(T Val)
Reverse the bits in Val.
Definition: MathExtras.h:269
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:35
MCCodeEmitter * createPPCMCCodeEmitter(const MCInstrInfo &MCII, const MCRegisterInfo &MRI, MCContext &Ctx)
unsigned getSize() const
Return the number of bytes in the encoding of this instruction, or zero if the encoding size cannot b...
Definition: MCInstrDesc.h:581