1 //===-- Mips/MipsCodeEmitter.cpp - Convert Mips Code to Machine Code ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===---------------------------------------------------------------------===//
10 // This file contains the pass that transforms the Mips machine instructions
11 // into relocatable machine code.
13 //===---------------------------------------------------------------------===//
16 #include "MCTargetDesc/MipsBaseInfo.h"
17 #include "MipsInstrInfo.h"
18 #include "MipsRelocations.h"
19 #include "MipsSubtarget.h"
20 #include "MipsTargetMachine.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/CodeGen/JITCodeEmitter.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineJumpTableInfo.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/CodeGen/MachineOperand.h"
30 #include "llvm/CodeGen/Passes.h"
31 #include "llvm/IR/Constants.h"
32 #include "llvm/IR/DerivedTypes.h"
33 #include "llvm/PassManager.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/raw_ostream.h"
43 #define DEBUG_TYPE "jit"
45 STATISTIC(NumEmitted, "Number of machine instructions emitted");
49 class MipsCodeEmitter : public MachineFunctionPass {
51 const MipsInstrInfo *II;
53 const MipsSubtarget *Subtarget;
56 const std::vector<MachineConstantPoolEntry> *MCPEs;
57 const std::vector<MachineJumpTableEntry> *MJTEs;
60 void getAnalysisUsage(AnalysisUsage &AU) const override {
61 AU.addRequired<MachineModuleInfo> ();
62 MachineFunctionPass::getAnalysisUsage(AU);
68 MipsCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
69 : MachineFunctionPass(ID), JTI(nullptr), II(nullptr), TD(nullptr),
70 TM(tm), MCE(mce), MCPEs(nullptr), MJTEs(nullptr),
71 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
73 bool runOnMachineFunction(MachineFunction &MF) override;
75 const char *getPassName() const override {
76 return "Mips Machine Code Emitter";
79 /// getBinaryCodeForInstr - This function, generated by the
80 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
81 /// machine instructions.
82 uint64_t getBinaryCodeForInstr(const MachineInstr &MI) const;
84 void emitInstruction(MachineBasicBlock::instr_iterator MI,
85 MachineBasicBlock &MBB);
89 void emitWord(unsigned Word);
91 /// Routines that handle operands which add machine relocations which are
92 /// fixed up by the relocation stage.
93 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
94 bool MayNeedFarStub) const;
95 void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const;
96 void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const;
97 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const;
98 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc) const;
100 /// getMachineOpValue - Return binary encoding of operand. If the machine
101 /// operand requires relocation, record the relocation and return zero.
102 unsigned getMachineOpValue(const MachineInstr &MI,
103 const MachineOperand &MO) const;
105 unsigned getRelocation(const MachineInstr &MI,
106 const MachineOperand &MO) const;
108 unsigned getJumpTargetOpValue(const MachineInstr &MI, unsigned OpNo) const;
109 unsigned getJumpTargetOpValueMM(const MachineInstr &MI, unsigned OpNo) const;
110 unsigned getBranchTargetOpValueMM(const MachineInstr &MI,
111 unsigned OpNo) const;
113 unsigned getBranchTarget21OpValue(const MachineInstr &MI,
114 unsigned OpNo) const;
115 unsigned getBranchTarget26OpValue(const MachineInstr &MI,
116 unsigned OpNo) const;
117 unsigned getJumpOffset16OpValue(const MachineInstr &MI, unsigned OpNo) const;
119 unsigned getBranchTargetOpValue(const MachineInstr &MI, unsigned OpNo) const;
120 unsigned getMemEncoding(const MachineInstr &MI, unsigned OpNo) const;
121 unsigned getMemEncodingMMImm12(const MachineInstr &MI, unsigned OpNo) const;
122 unsigned getMSAMemEncoding(const MachineInstr &MI, unsigned OpNo) const;
123 unsigned getSizeExtEncoding(const MachineInstr &MI, unsigned OpNo) const;
124 unsigned getSizeInsEncoding(const MachineInstr &MI, unsigned OpNo) const;
125 unsigned getLSAImmEncoding(const MachineInstr &MI, unsigned OpNo) const;
126 unsigned getSimm19Lsl2Encoding(const MachineInstr &MI, unsigned OpNo) const;
128 /// Expand pseudo instructions with accumulator register operands.
129 void expandACCInstr(MachineBasicBlock::instr_iterator MI,
130 MachineBasicBlock &MBB, unsigned Opc) const;
132 /// \brief Expand pseudo instruction. Return true if MI was expanded.
133 bool expandPseudos(MachineBasicBlock::instr_iterator &MI,
134 MachineBasicBlock &MBB) const;
138 char MipsCodeEmitter::ID = 0;
140 bool MipsCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
141 MipsTargetMachine &Target = static_cast<MipsTargetMachine &>(
142 const_cast<TargetMachine &>(MF.getTarget()));
144 JTI = Target.getJITInfo();
145 II = Target.getInstrInfo();
146 TD = Target.getDataLayout();
147 Subtarget = &TM.getSubtarget<MipsSubtarget> ();
148 MCPEs = &MF.getConstantPool()->getConstants();
150 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
151 JTI->Initialize(MF, IsPIC, Subtarget->isLittle());
152 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
155 DEBUG(errs() << "JITTing function '"
156 << MF.getName() << "'\n");
157 MCE.startFunction(MF);
159 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
161 MCE.StartMachineBasicBlock(MBB);
162 for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(),
163 E = MBB->instr_end(); I != E;)
164 emitInstruction(*I++, *MBB);
166 } while (MCE.finishFunction(MF));
171 unsigned MipsCodeEmitter::getRelocation(const MachineInstr &MI,
172 const MachineOperand &MO) const {
173 // NOTE: This relocations are for static.
174 uint64_t TSFlags = MI.getDesc().TSFlags;
175 uint64_t Form = TSFlags & MipsII::FormMask;
176 if (Form == MipsII::FrmJ)
177 return Mips::reloc_mips_26;
178 if ((Form == MipsII::FrmI || Form == MipsII::FrmFI)
180 return Mips::reloc_mips_pc16;
181 if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi)
182 return Mips::reloc_mips_hi;
183 return Mips::reloc_mips_lo;
186 unsigned MipsCodeEmitter::getJumpTargetOpValue(const MachineInstr &MI,
187 unsigned OpNo) const {
188 MachineOperand MO = MI.getOperand(OpNo);
190 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
191 else if (MO.isSymbol())
192 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
194 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
196 llvm_unreachable("Unexpected jump target operand kind.");
200 unsigned MipsCodeEmitter::getJumpTargetOpValueMM(const MachineInstr &MI,
201 unsigned OpNo) const {
202 llvm_unreachable("Unimplemented function.");
206 unsigned MipsCodeEmitter::getBranchTargetOpValueMM(const MachineInstr &MI,
207 unsigned OpNo) const {
208 llvm_unreachable("Unimplemented function.");
212 unsigned MipsCodeEmitter::getBranchTarget21OpValue(const MachineInstr &MI,
213 unsigned OpNo) const {
214 llvm_unreachable("Unimplemented function.");
218 unsigned MipsCodeEmitter::getBranchTarget26OpValue(const MachineInstr &MI,
219 unsigned OpNo) const {
220 llvm_unreachable("Unimplemented function.");
224 unsigned MipsCodeEmitter::getJumpOffset16OpValue(const MachineInstr &MI,
225 unsigned OpNo) const {
226 llvm_unreachable("Unimplemented function.");
230 unsigned MipsCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI,
231 unsigned OpNo) const {
232 MachineOperand MO = MI.getOperand(OpNo);
233 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
237 unsigned MipsCodeEmitter::getMemEncoding(const MachineInstr &MI,
238 unsigned OpNo) const {
239 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
240 assert(MI.getOperand(OpNo).isReg());
241 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo)) << 16;
242 return (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits;
245 unsigned MipsCodeEmitter::getMemEncodingMMImm12(const MachineInstr &MI,
246 unsigned OpNo) const {
247 llvm_unreachable("Unimplemented function.");
251 unsigned MipsCodeEmitter::getMSAMemEncoding(const MachineInstr &MI,
252 unsigned OpNo) const {
253 llvm_unreachable("Unimplemented function.");
257 unsigned MipsCodeEmitter::getSizeExtEncoding(const MachineInstr &MI,
258 unsigned OpNo) const {
259 // size is encoded as size-1.
260 return getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
263 unsigned MipsCodeEmitter::getSizeInsEncoding(const MachineInstr &MI,
264 unsigned OpNo) const {
265 // size is encoded as pos+size-1.
266 return getMachineOpValue(MI, MI.getOperand(OpNo-1)) +
267 getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
270 unsigned MipsCodeEmitter::getLSAImmEncoding(const MachineInstr &MI,
271 unsigned OpNo) const {
272 llvm_unreachable("Unimplemented function.");
276 unsigned MipsCodeEmitter::getSimm19Lsl2Encoding(const MachineInstr &MI,
277 unsigned OpNo) const {
278 llvm_unreachable("Unimplemented function.");
282 /// getMachineOpValue - Return binary encoding of operand. If the machine
283 /// operand requires relocation, record the relocation and return zero.
284 unsigned MipsCodeEmitter::getMachineOpValue(const MachineInstr &MI,
285 const MachineOperand &MO) const {
287 return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
289 return static_cast<unsigned>(MO.getImm());
290 else if (MO.isGlobal())
291 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
292 else if (MO.isSymbol())
293 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
295 emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
297 emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO));
299 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
301 llvm_unreachable("Unable to encode MachineOperand!");
305 void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
306 bool MayNeedFarStub) const {
307 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
308 const_cast<GlobalValue *>(GV), 0,
312 void MipsCodeEmitter::
313 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
314 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
318 void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
319 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
320 Reloc, CPI, 0, false));
323 void MipsCodeEmitter::
324 emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
325 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
326 Reloc, JTIndex, 0, false));
329 void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
330 unsigned Reloc) const {
331 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
335 void MipsCodeEmitter::emitInstruction(MachineBasicBlock::instr_iterator MI,
336 MachineBasicBlock &MBB) {
337 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << *MI);
339 // Expand pseudo instruction. Skip if MI was not expanded.
340 if (((MI->getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo) &&
341 !expandPseudos(MI, MBB))
344 MCE.processDebugLoc(MI->getDebugLoc(), true);
346 emitWord(getBinaryCodeForInstr(*MI));
347 ++NumEmitted; // Keep track of the # of mi's emitted
349 MCE.processDebugLoc(MI->getDebugLoc(), false);
352 void MipsCodeEmitter::emitWord(unsigned Word) {
353 DEBUG(errs() << " 0x";
354 errs().write_hex(Word) << "\n");
355 if (Subtarget->isLittle())
356 MCE.emitWordLE(Word);
358 MCE.emitWordBE(Word);
361 void MipsCodeEmitter::expandACCInstr(MachineBasicBlock::instr_iterator MI,
362 MachineBasicBlock &MBB,
363 unsigned Opc) const {
364 // Expand "pseudomult $ac0, $t0, $t1" to "mult $t0, $t1".
365 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Opc))
366 .addReg(MI->getOperand(1).getReg()).addReg(MI->getOperand(2).getReg());
369 bool MipsCodeEmitter::expandPseudos(MachineBasicBlock::instr_iterator &MI,
370 MachineBasicBlock &MBB) const {
371 switch (MI->getOpcode()) {
373 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::SLL), Mips::ZERO)
374 .addReg(Mips::ZERO).addImm(0);
377 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::BEQ)).addReg(Mips::ZERO)
378 .addReg(Mips::ZERO).addOperand(MI->getOperand(0));
381 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::BREAK)).addImm(0)
384 case Mips::JALRPseudo:
385 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::JALR), Mips::RA)
386 .addReg(MI->getOperand(0).getReg());
388 case Mips::PseudoMULT:
389 expandACCInstr(MI, MBB, Mips::MULT);
391 case Mips::PseudoMULTu:
392 expandACCInstr(MI, MBB, Mips::MULTu);
394 case Mips::PseudoSDIV:
395 expandACCInstr(MI, MBB, Mips::SDIV);
397 case Mips::PseudoUDIV:
398 expandACCInstr(MI, MBB, Mips::UDIV);
400 case Mips::PseudoMADD:
401 expandACCInstr(MI, MBB, Mips::MADD);
403 case Mips::PseudoMADDU:
404 expandACCInstr(MI, MBB, Mips::MADDU);
406 case Mips::PseudoMSUB:
407 expandACCInstr(MI, MBB, Mips::MSUB);
409 case Mips::PseudoMSUBU:
410 expandACCInstr(MI, MBB, Mips::MSUBU);
416 (MI--)->eraseFromBundle();
420 /// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips
421 /// code to the specified MCE object.
422 FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM,
423 JITCodeEmitter &JCE) {
424 return new MipsCodeEmitter(TM, JCE);
427 #include "MipsGenCodeEmitter.inc"