remove a bunch of unused private methods
[oota-llvm.git] / lib / Target / Mips / MipsCodeEmitter.cpp
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"
17 #include "MCTargetDesc/MipsBaseInfo.h"
18 #include "MipsInstrInfo.h"
19 #include "MipsRelocations.h"
20 #include "MipsSubtarget.h"
21 #include "MipsTargetMachine.h"
22 #include "llvm/ADT/Statistic.h"
23 #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/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineJumpTableInfo.h"
29 #include "llvm/CodeGen/MachineModuleInfo.h"
30 #include "llvm/CodeGen/MachineOperand.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"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/raw_ostream.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;
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
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 getMSAMemEncoding(const MachineInstr &MI, unsigned OpNo) const;
116   unsigned getSizeExtEncoding(const MachineInstr &MI, unsigned OpNo) const;
117   unsigned getSizeInsEncoding(const MachineInstr &MI, unsigned OpNo) const;
118   unsigned getLSAImmEncoding(const MachineInstr &MI, unsigned OpNo) const;
119
120   /// Expand pseudo instructions with accumulator register operands.
121   void expandACCInstr(MachineBasicBlock::instr_iterator MI,
122                       MachineBasicBlock &MBB, unsigned Opc) const;
123
124   /// \brief Expand pseudo instruction. Return true if MI was expanded.
125   bool expandPseudos(MachineBasicBlock::instr_iterator &MI,
126                      MachineBasicBlock &MBB) const;
127 };
128 }
129
130 char MipsCodeEmitter::ID = 0;
131
132 bool MipsCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
133   MipsTargetMachine &Target = static_cast<MipsTargetMachine &>(
134                                 const_cast<TargetMachine &>(MF.getTarget()));
135
136   JTI = Target.getJITInfo();
137   II = Target.getInstrInfo();
138   TD = Target.getDataLayout();
139   Subtarget = &TM.getSubtarget<MipsSubtarget> ();
140   MCPEs = &MF.getConstantPool()->getConstants();
141   MJTEs = 0;
142   if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables();
143   JTI->Initialize(MF, IsPIC, Subtarget->isLittle());
144   MCE.setModuleInfo(&getAnalysis<MachineModuleInfo> ());
145
146   do {
147     DEBUG(errs() << "JITTing function '"
148         << MF.getName() << "'\n");
149     MCE.startFunction(MF);
150
151     for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
152         MBB != E; ++MBB){
153       MCE.StartMachineBasicBlock(MBB);
154       for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(),
155            E = MBB->instr_end(); I != E;)
156         emitInstruction(*I++, *MBB);
157     }
158   } while (MCE.finishFunction(MF));
159
160   return false;
161 }
162
163 unsigned MipsCodeEmitter::getRelocation(const MachineInstr &MI,
164                                         const MachineOperand &MO) const {
165   // NOTE: This relocations are for static.
166   uint64_t TSFlags = MI.getDesc().TSFlags;
167   uint64_t Form = TSFlags & MipsII::FormMask;
168   if (Form == MipsII::FrmJ)
169     return Mips::reloc_mips_26;
170   if ((Form == MipsII::FrmI || Form == MipsII::FrmFI)
171        && MI.isBranch())
172     return Mips::reloc_mips_pc16;
173   if (Form == MipsII::FrmI && MI.getOpcode() == Mips::LUi)
174     return Mips::reloc_mips_hi;
175   return Mips::reloc_mips_lo;
176 }
177
178 unsigned MipsCodeEmitter::getJumpTargetOpValue(const MachineInstr &MI,
179                                                unsigned OpNo) const {
180   MachineOperand MO = MI.getOperand(OpNo);
181   if (MO.isGlobal())
182     emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
183   else if (MO.isSymbol())
184     emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
185   else if (MO.isMBB())
186     emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
187   else
188     llvm_unreachable("Unexpected jump target operand kind.");
189   return 0;
190 }
191
192 unsigned MipsCodeEmitter::getJumpTargetOpValueMM(const MachineInstr &MI,
193                                                  unsigned OpNo) const {
194   llvm_unreachable("Unimplemented function.");
195   return 0;
196 }
197
198 unsigned MipsCodeEmitter::getBranchTargetOpValueMM(const MachineInstr &MI,
199                                                    unsigned OpNo) const {
200   llvm_unreachable("Unimplemented function.");
201   return 0;
202 }
203
204 unsigned MipsCodeEmitter::getBranchTargetOpValue(const MachineInstr &MI,
205                                                  unsigned OpNo) const {
206   MachineOperand MO = MI.getOperand(OpNo);
207   emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
208   return 0;
209 }
210
211 unsigned MipsCodeEmitter::getMemEncoding(const MachineInstr &MI,
212                                          unsigned OpNo) const {
213   // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
214   assert(MI.getOperand(OpNo).isReg());
215   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo)) << 16;
216   return (getMachineOpValue(MI, MI.getOperand(OpNo+1)) & 0xFFFF) | RegBits;
217 }
218
219 unsigned MipsCodeEmitter::getMemEncodingMMImm12(const MachineInstr &MI,
220                                                 unsigned OpNo) const {
221   llvm_unreachable("Unimplemented function.");
222   return 0;
223 }
224
225 unsigned MipsCodeEmitter::getMSAMemEncoding(const MachineInstr &MI,
226                                             unsigned OpNo) const {
227   llvm_unreachable("Unimplemented function.");
228   return 0;
229 }
230
231 unsigned MipsCodeEmitter::getSizeExtEncoding(const MachineInstr &MI,
232                                              unsigned OpNo) const {
233   // size is encoded as size-1.
234   return getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
235 }
236
237 unsigned MipsCodeEmitter::getSizeInsEncoding(const MachineInstr &MI,
238                                              unsigned OpNo) const {
239   // size is encoded as pos+size-1.
240   return getMachineOpValue(MI, MI.getOperand(OpNo-1)) +
241          getMachineOpValue(MI, MI.getOperand(OpNo)) - 1;
242 }
243
244 unsigned MipsCodeEmitter::getLSAImmEncoding(const MachineInstr &MI,
245                                             unsigned OpNo) const {
246   llvm_unreachable("Unimplemented function.");
247   return 0;
248 }
249
250 /// getMachineOpValue - Return binary encoding of operand. If the machine
251 /// operand requires relocation, record the relocation and return zero.
252 unsigned MipsCodeEmitter::getMachineOpValue(const MachineInstr &MI,
253                                             const MachineOperand &MO) const {
254   if (MO.isReg())
255     return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
256   else if (MO.isImm())
257     return static_cast<unsigned>(MO.getImm());
258   else if (MO.isGlobal())
259     emitGlobalAddress(MO.getGlobal(), getRelocation(MI, MO), true);
260   else if (MO.isSymbol())
261     emitExternalSymbolAddress(MO.getSymbolName(), getRelocation(MI, MO));
262   else if (MO.isCPI())
263     emitConstPoolAddress(MO.getIndex(), getRelocation(MI, MO));
264   else if (MO.isJTI())
265     emitJumpTableAddress(MO.getIndex(), getRelocation(MI, MO));
266   else if (MO.isMBB())
267     emitMachineBasicBlock(MO.getMBB(), getRelocation(MI, MO));
268   else
269     llvm_unreachable("Unable to encode MachineOperand!");
270   return 0;
271 }
272
273 void MipsCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc,
274                                         bool MayNeedFarStub) const {
275   MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
276                                              const_cast<GlobalValue *>(GV), 0,
277                                              MayNeedFarStub));
278 }
279
280 void MipsCodeEmitter::
281 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const {
282   MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
283                                                  Reloc, ES, 0, 0));
284 }
285
286 void MipsCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const {
287   MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
288                                                     Reloc, CPI, 0, false));
289 }
290
291 void MipsCodeEmitter::
292 emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const {
293   MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
294                                                     Reloc, JTIndex, 0, false));
295 }
296
297 void MipsCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB,
298                                             unsigned Reloc) const {
299   MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
300                                              Reloc, BB));
301 }
302
303 void MipsCodeEmitter::emitInstruction(MachineBasicBlock::instr_iterator MI,
304                                       MachineBasicBlock &MBB) {
305   DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << *MI);
306
307   // Expand pseudo instruction. Skip if MI was not expanded.
308   if (((MI->getDesc().TSFlags & MipsII::FormMask) == MipsII::Pseudo) &&
309       !expandPseudos(MI, MBB))
310     return;
311
312   MCE.processDebugLoc(MI->getDebugLoc(), true);
313
314   emitWord(getBinaryCodeForInstr(*MI));
315   ++NumEmitted;  // Keep track of the # of mi's emitted
316
317   MCE.processDebugLoc(MI->getDebugLoc(), false);
318 }
319
320 void MipsCodeEmitter::emitWord(unsigned Word) {
321   DEBUG(errs() << "  0x";
322         errs().write_hex(Word) << "\n");
323   if (Subtarget->isLittle())
324     MCE.emitWordLE(Word);
325   else
326     MCE.emitWordBE(Word);
327 }
328
329 void MipsCodeEmitter::expandACCInstr(MachineBasicBlock::instr_iterator MI,
330                                      MachineBasicBlock &MBB,
331                                      unsigned Opc) const {
332   // Expand "pseudomult $ac0, $t0, $t1" to "mult $t0, $t1".
333   BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Opc))
334     .addReg(MI->getOperand(1).getReg()).addReg(MI->getOperand(2).getReg());
335 }
336
337 bool MipsCodeEmitter::expandPseudos(MachineBasicBlock::instr_iterator &MI,
338                                     MachineBasicBlock &MBB) const {
339   switch (MI->getOpcode()) {
340   case Mips::NOP:
341     BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::SLL), Mips::ZERO)
342       .addReg(Mips::ZERO).addImm(0);
343     break;
344   case Mips::B:
345     BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::BEQ)).addReg(Mips::ZERO)
346       .addReg(Mips::ZERO).addOperand(MI->getOperand(0));
347     break;
348   case Mips::TRAP:
349     BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::BREAK)).addImm(0)
350       .addImm(0);
351     break;
352   case Mips::JALRPseudo:
353     BuildMI(MBB, &*MI, MI->getDebugLoc(), II->get(Mips::JALR), Mips::RA)
354       .addReg(MI->getOperand(0).getReg());
355     break;
356   case Mips::PseudoMULT:
357     expandACCInstr(MI, MBB, Mips::MULT);
358     break;
359   case Mips::PseudoMULTu:
360     expandACCInstr(MI, MBB, Mips::MULTu);
361     break;
362   case Mips::PseudoSDIV:
363     expandACCInstr(MI, MBB, Mips::SDIV);
364     break;
365   case Mips::PseudoUDIV:
366     expandACCInstr(MI, MBB, Mips::UDIV);
367     break;
368   case Mips::PseudoMADD:
369     expandACCInstr(MI, MBB, Mips::MADD);
370     break;
371   case Mips::PseudoMADDU:
372     expandACCInstr(MI, MBB, Mips::MADDU);
373     break;
374   case Mips::PseudoMSUB:
375     expandACCInstr(MI, MBB, Mips::MSUB);
376     break;
377   case Mips::PseudoMSUBU:
378     expandACCInstr(MI, MBB, Mips::MSUBU);
379     break;
380   default:
381     return false;
382   }
383
384   (MI--)->eraseFromBundle();
385   return true;
386 }
387
388 /// createMipsJITCodeEmitterPass - Return a pass that emits the collected Mips
389 /// code to the specified MCE object.
390 FunctionPass *llvm::createMipsJITCodeEmitterPass(MipsTargetMachine &TM,
391                                                  JITCodeEmitter &JCE) {
392   return new MipsCodeEmitter(TM, JCE);
393 }
394
395 #include "MipsGenCodeEmitter.inc"