blob: df877b65484800167d026c07a1fd5b9ce74b9730 [file] [log] [blame]
Jia Liuc5707112012-02-17 08:55:11 +00001//===-- Mips/MipsCodeEmitter.cpp - Convert Mips Code to Machine Code ------===//
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +00002//
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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "MCTargetDesc/MipsBaseInfo.h"
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000018#include "MipsInstrInfo.h"
19#include "MipsRelocations.h"
20#include "MipsSubtarget.h"
21#include "MipsTargetMachine.h"
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +000022#include "llvm/ADT/Statistic.h"
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000023#include "llvm/CodeGen/JITCodeEmitter.h"
24#include "llvm/CodeGen/MachineConstantPool.h"
25#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachineInstr.h"
27#include "llvm/CodeGen/MachineJumpTableInfo.h"
Akira Hatanakab4b4fa82013-02-11 22:35:40 +000028#include "llvm/CodeGen/MachineInstrBuilder.h"
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000029#include "llvm/CodeGen/MachineModuleInfo.h"
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +000030#include "llvm/CodeGen/MachineOperand.h"
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000031#include "llvm/CodeGen/Passes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000032#include "llvm/IR/Constants.h"
33#include "llvm/IR/DerivedTypes.h"
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +000034#include "llvm/PassManager.h"
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000035#include "llvm/Support/Debug.h"
36#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/raw_ostream.h"
38#ifndef NDEBUG
39#include <iomanip>
40#endif
41
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000042using namespace llvm;
43
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +000044STATISTIC(NumEmitted, "Number of machine instructions emitted");
45
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000046namespace {
47
48class MipsCodeEmitter : public MachineFunctionPass {
49 MipsJITInfo *JTI;
50 const MipsInstrInfo *II;
Micah Villmow3574eca2012-10-08 16:38:25 +000051 const DataLayout *TD;
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000052 const MipsSubtarget *Subtarget;
53 TargetMachine &TM;
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 {
60 AU.addRequired<MachineModuleInfo> ();
61 MachineFunctionPass::getAnalysisUsage(AU);
62 }
63
64 static char ID;
65
Akira Hatanaka1b235a22013-02-11 22:03:52 +000066public:
67 MipsCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
68 : MachineFunctionPass(ID), JTI(0),
69 II((const MipsInstrInfo *) tm.getInstrInfo()), TD(tm.getDataLayout()),
70 TM(tm), MCE(mce), MCPEs(0), MJTEs(0),
71 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {}
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000072
Akira Hatanaka1b235a22013-02-11 22:03:52 +000073 bool runOnMachineFunction(MachineFunction &MF);
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000074
Akira Hatanaka1b235a22013-02-11 22:03:52 +000075 virtual const char *getPassName() const {
76 return "Mips Machine Code Emitter";
77 }
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000078
Akira Hatanaka1b235a22013-02-11 22:03:52 +000079 /// 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;
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +000083
Akira Hatanakab4b4fa82013-02-11 22:35:40 +000084 void emitInstruction(MachineBasicBlock::instr_iterator MI,
85 MachineBasicBlock &MBB);
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000086
Akira Hatanaka1b235a22013-02-11 22:03:52 +000087private:
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +000088
Akira Hatanaka1b235a22013-02-11 22:03:52 +000089 void emitWord(unsigned Word);
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +000090
Akira Hatanaka1b235a22013-02-11 22:03:52 +000091 /// 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;
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +000099
Akira Hatanaka1b235a22013-02-11 22:03:52 +0000100 /// 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;
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000104
Akira Hatanaka1b235a22013-02-11 22:03:52 +0000105 unsigned getRelocation(const MachineInstr &MI,
106 const MachineOperand &MO) const;
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000107
Akira Hatanaka1b235a22013-02-11 22:03:52 +0000108 unsigned getJumpTargetOpValue(const MachineInstr &MI, unsigned OpNo) const;
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000109
Akira Hatanaka1b235a22013-02-11 22:03:52 +0000110 unsigned getBranchTargetOpValue(const MachineInstr &MI, unsigned OpNo) const;
111 unsigned getMemEncoding(const MachineInstr &MI, unsigned OpNo) const;
112 unsigned getSizeExtEncoding(const MachineInstr &MI, unsigned OpNo) const;
113 unsigned getSizeInsEncoding(const MachineInstr &MI, unsigned OpNo) const;
Bruno Cardoso Lopesad6eef42011-11-08 12:47:11 +0000114
Akira Hatanaka1b235a22013-02-11 22:03:52 +0000115 void emitGlobalAddressUnaligned(const GlobalValue *GV, unsigned Reloc,
116 int Offset) const;
Akira Hatanakab4b4fa82013-02-11 22:35:40 +0000117
118 /// \brief Expand pseudo instruction. Return true if MI was expanded.
119 bool expandPseudos(MachineBasicBlock::instr_iterator &MI,
120 MachineBasicBlock &MBB) const;
Akira Hatanaka1b235a22013-02-11 22:03:52 +0000121};
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000122}
123
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000124char MipsCodeEmitter::ID = 0;
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000125
126bool MipsCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Dmitri Gribenko953cbfc2013-01-14 22:08:37 +0000127 MipsTargetMachine &Target = static_cast<MipsTargetMachine &>(
128 const_cast<TargetMachine &>(MF.getTarget()));
129
130 JTI = Target.getJITInfo();
131 II = Target.getInstrInfo();
132 TD = Target.getDataLayout();
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000133 Subtarget = &TM.getSubtarget<MipsSubtarget> ();
134 MCPEs = &MF.getConstantPool()->getConstants();
135 MJTEs = 0;
136 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
Akira Hatanakaff0b2cf2012-12-03 23:11:12 +0000137 JTI->Initialize(MF, IsPIC, Subtarget->isLittle());
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000138 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
139
140 do {
141 DEBUG(errs() << "JITTing function '"
Craig Topper96601ca2012-08-22 06:07:19 +0000142 << MF.getName() << "'\n");
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000143 MCE.startFunction(MF);
144
145 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
146 MBB != E; ++MBB){
147 MCE.StartMachineBasicBlock(MBB);
Akira Hatanaka226ae402012-06-19 03:39:45 +0000148 for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(),
Akira Hatanakab4b4fa82013-02-11 22:35:40 +0000149 E = MBB->instr_end(); I != E;)
150 emitInstruction(*I++, *MBB);
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000151 }
152 } while (MCE.finishFunction(MF));
153
154 return false;
155}
156
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000157unsigned MipsCodeEmitter::getRelocation(const MachineInstr &MI,
158 const MachineOperand &MO) const {
159 // NOTE: This relocations are for static.
160 uint64_t TSFlags = MI.getDesc().TSFlags;
161 uint64_t Form = TSFlags & MipsII::FormMask;
162 if (Form == MipsII::FrmJ)
163 return Mips::reloc_mips_26;
164 if ((Form == MipsII::FrmI || Form == MipsII::FrmFI)
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000165 && MI.isBranch())
Bruno Cardoso Lopes3aa035f2011-12-30 21:04:30 +0000166 return Mips::reloc_mips_pc16;
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000167 if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi)
168 return Mips::reloc_mips_hi;
169 return Mips::reloc_mips_lo;
Bruno Cardoso Lopesdca6cdd2011-07-21 16:28:51 +0000170}
171
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000172unsigned MipsCodeEmitter::getJumpTargetOpValue(const MachineInstr &MI,
173 unsigned OpNo) const {
Bruno Cardoso Lopes3aa035f2011-12-30 21:04:30 +0000174 MachineOperand MO = MI.getOperand(OpNo);
175 if (MO.isGlobal())
176 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
177 else if (MO.isSymbol())
178 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
179 else if (MO.isMBB())
180 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
181 else
182 llvm_unreachable("Unexpected jump target operand kind.");
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000183 return 0;
184}
185
186unsigned MipsCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI,
187 unsigned OpNo) const {
Bruno Cardoso Lopes3aa035f2011-12-30 21:04:30 +0000188 MachineOperand MO = MI.getOperand(OpNo);
189 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000190 return 0;
191}
192
Bruno Cardoso Lopesc3f16b32011-10-18 17:50:36 +0000193unsigned MipsCodeEmitter::getMemEncoding(const MachineInstr &MI,
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000194 unsigned OpNo) const {
Bruno Cardoso Lopesc3f16b32011-10-18 17:50:36 +0000195 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
196 assert(MI.getOperand(OpNo).isReg());
197 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo)) << 16;
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000198 return (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits;
Bruno Cardoso Lopesc3f16b32011-10-18 17:50:36 +0000199}
200
201unsigned MipsCodeEmitter::getSizeExtEncoding(const MachineInstr &MI,
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000202 unsigned OpNo) const {
Bruno Cardoso Lopesc3f16b32011-10-18 17:50:36 +0000203 // size is encoded as size-1.
204 return getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
205}
206
207unsigned MipsCodeEmitter::getSizeInsEncoding(const MachineInstr &MI,
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000208 unsigned OpNo) const {
Bruno Cardoso Lopesc3f16b32011-10-18 17:50:36 +0000209 // size is encoded as pos+size-1.
210 return getMachineOpValue(MI, MI.getOperand(OpNo-1)) +
211 getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
212}
213
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000214/// getMachineOpValue - Return binary encoding of operand. If the machine
215/// operand requires relocation, record the relocation and return zero.
216unsigned MipsCodeEmitter::getMachineOpValue(const MachineInstr &MI,
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000217 const MachineOperand &MO) const {
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000218 if (MO.isReg())
Akira Hatanakae8068692012-12-10 20:04:40 +0000219 return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000220 else if (MO.isImm())
221 return static_cast<unsigned>(MO.getImm());
Akira Hatanaka5a7dd432012-09-15 01:52:08 +0000222 else if (MO.isGlobal())
223 emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
224 else if (MO.isSymbol())
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000225 emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
226 else if (MO.isCPI())
227 emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
228 else if (MO.isJTI())
229 emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO));
230 else if (MO.isMBB())
231 emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
232 else
233 llvm_unreachable("Unable to encode MachineOperand!");
234 return 0;
235}
236
237void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000238 bool MayNeedFarStub) const {
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000239 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000240 const_cast<GlobalValue *>(GV), 0,
241 MayNeedFarStub));
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000242}
243
Bruno Cardoso Lopesad6eef42011-11-08 12:47:11 +0000244void MipsCodeEmitter::emitGlobalAddressUnaligned(const GlobalValue *GV,
245 unsigned Reloc, int Offset) const {
246 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
247 const_cast<GlobalValue *>(GV), 0, false));
248 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset() + Offset,
249 Reloc, const_cast<GlobalValue *>(GV), 0, false));
250}
251
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000252void MipsCodeEmitter::
253emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
254 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Akira Hatanakab5713452012-07-24 00:08:26 +0000255 Reloc, ES, 0, 0));
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000256}
257
258void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
259 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
260 Reloc, CPI, 0, false));
261}
262
263void MipsCodeEmitter::
264emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
265 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
266 Reloc, JTIndex, 0, false));
267}
268
269void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000270 unsigned Reloc) const {
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000271 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
272 Reloc, BB));
273}
274
Akira Hatanakab4b4fa82013-02-11 22:35:40 +0000275void MipsCodeEmitter::emitInstruction(MachineBasicBlock::instr_iterator MI,
276 MachineBasicBlock &MBB) {
277 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << *MI);
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000278
Akira Hatanakab4b4fa82013-02-11 22:35:40 +0000279 // Expand pseudo instruction. Skip if MI was not expanded.
280 if (((MI->getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo) &&
281 !expandPseudos(MI, MBB))
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000282 return;
283
Akira Hatanakab4b4fa82013-02-11 22:35:40 +0000284 MCE.processDebugLoc(MI->getDebugLoc(), true);
285
286 emitWord(getBinaryCodeForInstr(*MI));
Akira Hatanaka5a7dd432012-09-15 01:52:08 +0000287 ++NumEmitted; // Keep track of the # of mi's emitted
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000288
Akira Hatanakab4b4fa82013-02-11 22:35:40 +0000289 MCE.processDebugLoc(MI->getDebugLoc(), false);
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000290}
291
Akira Hatanakaff0b2cf2012-12-03 23:11:12 +0000292void MipsCodeEmitter::emitWord(unsigned Word) {
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000293 DEBUG(errs() << " 0x";
294 errs().write_hex(Word) << "\n");
Akira Hatanakaff0b2cf2012-12-03 23:11:12 +0000295 if (Subtarget->isLittle())
296 MCE.emitWordLE(Word);
297 else
298 MCE.emitWordBE(Word);
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000299}
300
Akira Hatanakab4b4fa82013-02-11 22:35:40 +0000301bool MipsCodeEmitter::expandPseudos(MachineBasicBlock::instr_iterator &MI,
302 MachineBasicBlock &MBB) const {
303 switch (MI->getOpcode()) {
304 case Mips::NOP:
305 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::SLL), Mips::ZERO)
306 .addReg(Mips::ZERO).addImm(0);
307 break;
308 case Mips::JALRPseudo:
309 BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::JALR), Mips::RA)
310 .addReg(MI->getOperand(0).getReg());
311 break;
312 default:
313 return false;
314 }
315
316 (MI--)->eraseFromBundle();
317 return true;
318}
319
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000320/// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips
321/// code to the specified MCE object.
322FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM,
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000323 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesc4cc40c2011-09-14 03:00:41 +0000324 return new MipsCodeEmitter(TM, JCE);
325}
326
Bruno Cardoso Lopesc3f16b32011-10-18 17:50:36 +0000327#include "MipsGenCodeEmitter.inc"