LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MipsCodeEmitter.cpp
Go to the documentation of this file.
1 //===-- Mips/MipsCodeEmitter.cpp - Convert Mips 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 contains the pass that transforms the Mips machine instructions
11 // into relocatable machine code.
12 //
13 //===---------------------------------------------------------------------===//
14 
15 #define DEBUG_TYPE "jit"
16 #include "Mips.h"
18 #include "MipsInstrInfo.h"
19 #include "MipsRelocations.h"
20 #include "MipsSubtarget.h"
21 #include "MipsTargetMachine.h"
22 #include "llvm/ADT/Statistic.h"
31 #include "llvm/CodeGen/Passes.h"
32 #include "llvm/IR/Constants.h"
33 #include "llvm/IR/DerivedTypes.h"
34 #include "llvm/PassManager.h"
35 #include "llvm/Support/Debug.h"
38 #ifndef NDEBUG
39 #include <iomanip>
40 #endif
41 
42 using namespace llvm;
43 
44 STATISTIC(NumEmitted, "Number of machine instructions emitted");
45 
46 namespace {
47 
48 class MipsCodeEmitter : public MachineFunctionPass {
49  MipsJITInfo *JTI;
50  const MipsInstrInfo *II;
51  const DataLayout *TD;
52  const MipsSubtarget *Subtarget;
54  JITCodeEmitter &MCE;
55  const std::vector<MachineConstantPoolEntry> *MCPEs;
56  const std::vector<MachineJumpTableEntry> *MJTEs;
57  bool IsPIC;
58 
59  void getAnalysisUsage(AnalysisUsage &AU) const {
62  }
63 
64  static char ID;
65 
66 public:
67  MipsCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
68  : MachineFunctionPass(ID), JTI(0), II(0), TD(0),
69  TM(tm), MCE(mce), MCPEs(0), MJTEs(0),
70  IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
71 
72  bool runOnMachineFunction(MachineFunction &MF);
73 
74  virtual const char *getPassName() const {
75  return "Mips Machine Code Emitter";
76  }
77 
78  /// getBinaryCodeForInstr - This function, generated by the
79  /// CodeEmitterGenerator using TableGen, produces the binary encoding for
80  /// machine instructions.
81  uint64_t getBinaryCodeForInstr(const MachineInstr &MI) const;
82 
83  void emitInstruction(MachineBasicBlock::instr_iterator MI,
84  MachineBasicBlock &MBB);
85 
86 private:
87 
88  void emitWord(unsigned Word);
89 
90  /// Routines that handle operands which add machine relocations which are
91  /// fixed up by the relocation stage.
92  void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
93  bool MayNeedFarStub) const;
94  void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const;
95  void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const;
96  void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const;
97  void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc) const;
98 
99  /// getMachineOpValue - Return binary encoding of operand. If the machine
100  /// operand requires relocation, record the relocation and return zero.
101  unsigned getMachineOpValue(const MachineInstr &MI,
102  const MachineOperand &MO) const;
103 
104  unsigned getRelocation(const MachineInstr &MI,
105  const MachineOperand &MO) const;
106 
107  unsigned getJumpTargetOpValue(const MachineInstr &MI, unsigned OpNo) const;
108  unsigned getJumpTargetOpValueMM(const MachineInstr &MI, unsigned OpNo) const;
109  unsigned getBranchTargetOpValueMM(const MachineInstr &MI,
110  unsigned OpNo) const;
111 
112  unsigned getBranchTargetOpValue(const MachineInstr &MI, unsigned OpNo) const;
113  unsigned getMemEncoding(const MachineInstr &MI, unsigned OpNo) const;
114  unsigned getMemEncodingMMImm12(const MachineInstr &MI, unsigned OpNo) const;
115  unsigned getSizeExtEncoding(const MachineInstr &MI, unsigned OpNo) const;
116  unsigned getSizeInsEncoding(const MachineInstr &MI, unsigned OpNo) const;
117  unsigned getLSAImmEncoding(const MachineInstr &MI, unsigned OpNo) const;
118 
119  void emitGlobalAddressUnaligned(const GlobalValue *GV, unsigned Reloc,
120  int Offset) const;
121 
122  /// Expand pseudo instructions with accumulator register operands.
123  void expandACCInstr(MachineBasicBlock::instr_iterator MI,
124  MachineBasicBlock &MBB, unsigned Opc) const;
125 
126  /// \brief Expand pseudo instruction. Return true if MI was expanded.
127  bool expandPseudos(MachineBasicBlock::instr_iterator &MI,
128  MachineBasicBlock &MBB) const;
129 };
130 }
131 
132 char MipsCodeEmitter::ID = 0;
133 
134 bool MipsCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
135  MipsTargetMachine &Target = static_cast<MipsTargetMachine &>(
136  const_cast<TargetMachine &>(MF.getTarget()));
137 
138  JTI = Target.getJITInfo();
139  II = Target.getInstrInfo();
140  TD = Target.getDataLayout();
141  Subtarget = &TM.getSubtarget<MipsSubtarget> ();
142  MCPEs = &MF.getConstantPool()->getConstants();
143  MJTEs = 0;
144  if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
145  JTI->Initialize(MF, IsPIC, Subtarget->isLittle());
146  MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
147 
148  do {
149  DEBUG(errs() << "JITTing function '"
150  << MF.getName() << "'\n");
151  MCE.startFunction(MF);
152 
153  for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
154  MBB != E; ++MBB){
155  MCE.StartMachineBasicBlock(MBB);
156  for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(),
157  E = MBB->instr_end(); I != E;)
158  emitInstruction(*I++, *MBB);
159  }
160  } while (MCE.finishFunction(MF));
161 
162  return false;
163 }
164 
165 unsigned MipsCodeEmitter::getRelocation(const MachineInstr &MI,
166  const MachineOperand &MO) const {
167  // NOTE: This relocations are for static.
168  uint64_t TSFlags = MI.getDesc().TSFlags;
169  uint64_t Form = TSFlags & MipsII::FormMask;
170  if (Form == MipsII::FrmJ)
171  return Mips::reloc_mips_26;
172  if ((Form == MipsII::FrmI || Form == MipsII::FrmFI)
173  && MI.isBranch())
174  return Mips::reloc_mips_pc16;
175  if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi)
176  return Mips::reloc_mips_hi;
177  return Mips::reloc_mips_lo;
178 }
179 
180 unsigned MipsCodeEmitter::getJumpTargetOpValue(const MachineInstr &MI,
181  unsigned OpNo) const {
182  MachineOperand MO = MI.getOperand(OpNo);
183  if (MO.isGlobal())
184  emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
185  else if (MO.isSymbol())
186  emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
187  else if (MO.isMBB())
188  emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
189  else
190  llvm_unreachable("Unexpected jump target operand kind.");
191  return 0;
192 }
193 
194 unsigned MipsCodeEmitter::getJumpTargetOpValueMM(const MachineInstr &MI,
195  unsigned OpNo) const {
196  llvm_unreachable("Unimplemented function.");
197  return 0;
198 }
199 
200 unsigned MipsCodeEmitter::getBranchTargetOpValueMM(const MachineInstr &MI,
201  unsigned OpNo) const {
202  llvm_unreachable("Unimplemented function.");
203  return 0;
204 }
205 
207  unsigned OpNo) const {
208  MachineOperand MO = MI.getOperand(OpNo);
209  emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
210  return 0;
211 }
212 
213 unsigned MipsCodeEmitter::getMemEncoding(const MachineInstr &MI,
214  unsigned OpNo) const {
215  // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
216  assert(MI.getOperand(OpNo).isReg());
217  unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo)) << 16;
218  return (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits;
219 }
220 
221 unsigned MipsCodeEmitter::getMemEncodingMMImm12(const MachineInstr &MI,
222  unsigned OpNo) const {
223  llvm_unreachable("Unimplemented function.");
224  return 0;
225 }
226 
227 unsigned MipsCodeEmitter::getSizeExtEncoding(const MachineInstr &MI,
228  unsigned OpNo) const {
229  // size is encoded as size-1.
230  return getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
231 }
232 
233 unsigned MipsCodeEmitter::getSizeInsEncoding(const MachineInstr &MI,
234  unsigned OpNo) const {
235  // size is encoded as pos+size-1.
236  return getMachineOpValue(MI, MI.getOperand(OpNo-1)) +
237  getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
238 }
239 
240 unsigned MipsCodeEmitter::getLSAImmEncoding(const MachineInstr &MI,
241  unsigned OpNo) const {
242  llvm_unreachable("Unimplemented function.");
243  return 0;
244 }
245 
246 /// getMachineOpValue - Return binary encoding of operand. If the machine
247 /// operand requires relocation, record the relocation and return zero.
248 unsigned MipsCodeEmitter::getMachineOpValue(const MachineInstr &MI,
249  const MachineOperand &MO) const {
250  if (MO.isReg())
251  return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
252  else if (MO.isImm())
253  return static_cast<unsigned>(MO.getImm());
254  else if (MO.isGlobal())
255  emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
256  else if (MO.isSymbol())
257  emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
258  else if (MO.isCPI())
259  emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
260  else if (MO.isJTI())
261  emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO));
262  else if (MO.isMBB())
263  emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
264  else
265  llvm_unreachable("Unable to encode MachineOperand!");
266  return 0;
267 }
268 
269 void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
270  bool MayNeedFarStub) const {
271  MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
272  const_cast<GlobalValue *>(GV), 0,
273  MayNeedFarStub));
274 }
275 
276 void MipsCodeEmitter::emitGlobalAddressUnaligned(const GlobalValue *GV,
277  unsigned Reloc, int Offset) const {
278  MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
279  const_cast<GlobalValue *>(GV), 0, false));
280  MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset() + Offset,
281  Reloc, const_cast<GlobalValue *>(GV), 0, false));
282 }
283 
284 void MipsCodeEmitter::
285 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
286  MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
287  Reloc, ES, 0, 0));
288 }
289 
290 void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
291  MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
292  Reloc, CPI, 0, false));
293 }
294 
295 void MipsCodeEmitter::
296 emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
297  MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
298  Reloc, JTIndex, 0, false));
299 }
300 
301 void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
302  unsigned Reloc) const {
303  MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
304  Reloc, BB));
305 }
306 
307 void MipsCodeEmitter::emitInstruction(MachineBasicBlock::instr_iterator MI,
308  MachineBasicBlock &MBB) {
309  DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << *MI);
310 
311  // Expand pseudo instruction. Skip if MI was not expanded.
312  if (((MI->getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo) &&
313  !expandPseudos(MI, MBB))
314  return;
315 
316  MCE.processDebugLoc(MI->getDebugLoc(), true);
317 
318  emitWord(getBinaryCodeForInstr(*MI));
319  ++NumEmitted; // Keep track of the # of mi's emitted
320 
321  MCE.processDebugLoc(MI->getDebugLoc(), false);
322 }
323 
324 void MipsCodeEmitter::emitWord(unsigned Word) {
325  DEBUG(errs() << " 0x";
326  errs().write_hex(Word) << "\n");
327  if (Subtarget->isLittle())
328  MCE.emitWordLE(Word);
329  else
330  MCE.emitWordBE(Word);
331 }
332 
333 void MipsCodeEmitter::expandACCInstr(MachineBasicBlock::instr_iterator MI,
334  MachineBasicBlock &MBB,
335  unsigned Opc) const {
336  // Expand "pseudomult $ac0, $t0, $t1" to "mult $t0, $t1".
337  BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Opc))
338  .addReg(MI->getOperand(1).getReg()).addReg(MI->getOperand(2).getReg());
339 }
340 
341 bool MipsCodeEmitter::expandPseudos(MachineBasicBlock::instr_iterator &MI,
342  MachineBasicBlock &MBB) const {
343  switch (MI->getOpcode()) {
344  case Mips::NOP:
345  BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::SLL), Mips::ZERO)
346  .addReg(Mips::ZERO).addImm(0);
347  break;
348  case Mips::B:
349  BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::BEQ)).addReg(Mips::ZERO)
350  .addReg(Mips::ZERO).addOperand(MI->getOperand(0));
351  break;
352  case Mips::TRAP:
353  BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::BREAK)).addImm(0)
354  .addImm(0);
355  break;
356  case Mips::JALRPseudo:
357  BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::JALR), Mips::RA)
358  .addReg(MI->getOperand(0).getReg());
359  break;
360  case Mips::PseudoMULT:
361  expandACCInstr(MI, MBB, Mips::MULT);
362  break;
363  case Mips::PseudoMULTu:
364  expandACCInstr(MI, MBB, Mips::MULTu);
365  break;
366  case Mips::PseudoSDIV:
367  expandACCInstr(MI, MBB, Mips::SDIV);
368  break;
369  case Mips::PseudoUDIV:
370  expandACCInstr(MI, MBB, Mips::UDIV);
371  break;
372  case Mips::PseudoMADD:
373  expandACCInstr(MI, MBB, Mips::MADD);
374  break;
375  case Mips::PseudoMADDU:
376  expandACCInstr(MI, MBB, Mips::MADDU);
377  break;
378  case Mips::PseudoMSUB:
379  expandACCInstr(MI, MBB, Mips::MSUB);
380  break;
381  case Mips::PseudoMSUBU:
382  expandACCInstr(MI, MBB, Mips::MSUBU);
383  break;
384  default:
385  return false;
386  }
387 
388  (MI--)->eraseFromBundle();
389  return true;
390 }
391 
392 /// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips
393 /// code to the specified MCE object.
395  JITCodeEmitter &JCE) {
396  return new MipsCodeEmitter(TM, JCE);
397 }
398 
399 #include "MipsGenCodeEmitter.inc"
raw_ostream & errs()
const GlobalValue * getGlobal() const
bool isBranch(QueryType Type=AnyInBundle) const
Definition: MachineInstr.h:374
virtual const MipsInstrInfo * getInstrInfo() const
MachineBasicBlock * getMBB() const
FrmFI - This form is for instructions of the format FI.
Definition: MipsBaseInfo.h:116
const MCInstrDesc & getDesc() const
Definition: MachineInstr.h:257
const char * getSymbolName() const
virtual MipsJITInfo * getJITInfo()
Instructions::iterator instr_iterator
FrmJ - This form is for instructions of the format J.
Definition: MipsBaseInfo.h:112
const std::vector< MachineJumpTableEntry > & getJumpTables() const
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
AnalysisUsage & addRequired()
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
#define llvm_unreachable(msg)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
const MachineInstrBuilder & addImm(int64_t Val) const
static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx, unsigned FixupKind, SmallVectorImpl< MCFixup > &Fixups)
bool isCPI() const
isCPI - Tests if this is a MO_ConstantPoolIndex operand.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
int getOpcode() const
Definition: MachineInstr.h:261
raw_ostream & write_hex(unsigned long long N)
write_hex - Output N in hexadecimal, without any prefix or padding.
const MachineJumpTableInfo * getJumpTableInfo() const
int64_t getImm() const
static MachineRelocation getBB(uintptr_t offset, unsigned RelocationType, MachineBasicBlock *MBB, intptr_t cst=0)
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:267
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
TRAP - Trapping instruction.
Definition: ISDOpcodes.h:586
MachineConstantPool * getConstantPool()
MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL, const MCInstrDesc &MCID)
FunctionPass * createMipsJITCodeEmitterPass(MipsTargetMachine &TM, JITCodeEmitter &JCE)
virtual const DataLayout * getDataLayout() const
bool isMBB() const
isMBB - Tests if this is a MO_MachineBasicBlock operand.
static MachineRelocation getGV(uintptr_t offset, unsigned RelocationType, GlobalValue *GV, intptr_t cst=0, bool MayNeedFarStub=0, bool GOTrelative=0)
STATISTIC(NumEmitted,"Number of machine instructions emitted")
FrmI - This form is for instructions of the format I.
Definition: MipsBaseInfo.h:110
virtual void getAnalysisUsage(AnalysisUsage &AU) const
#define I(x, y, z)
Definition: MD5.cpp:54
const TargetMachine & getTarget() const
static MachineRelocation getJumpTable(uintptr_t offset, unsigned RelocationType, unsigned JTI, intptr_t cst=0, bool letTargetResolve=false)
unsigned getReg() const
getReg - Returns the register number.
static MachineRelocation getConstPool(uintptr_t offset, unsigned RelocationType, unsigned CPI, intptr_t cst=0, bool letTargetResolve=false)
const MachineInstrBuilder & addOperand(const MachineOperand &MO) const
BasicBlockListType::iterator iterator
#define DEBUG(X)
Definition: Debug.h:97
const std::vector< MachineConstantPoolEntry > & getConstants() const
StringRef getName() const
static MachineRelocation getExtSym(uintptr_t offset, unsigned RelocationType, const char *ES, intptr_t cst=0, bool GOTrelative=0, bool NeedStub=true)
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
INITIALIZE_PASS(GlobalMerge,"global-merge","Global Merge", false, false) bool GlobalMerge const DataLayout * TD