Generate mfocrf when targeting g5. Generate fsqrt/fsqrts when targetin g5.
[oota-llvm.git] / lib / Target / PowerPC / PPCCodeEmitter.cpp
1 //===-- PPC32CodeEmitter.cpp - JIT Code Emitter for PowerPC32 -----*- C++ -*-=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the PowerPC 32-bit CodeEmitter and associated machinery to
11 // JIT-compile bytecode to native PowerPC.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "PPC32TargetMachine.h"
16 #include "PPC32Relocations.h"
17 #include "PowerPC.h"
18 #include "llvm/Module.h"
19 #include "llvm/CodeGen/MachineCodeEmitter.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/Passes.h"
23 #include "llvm/Support/Debug.h"
24 using namespace llvm;
25
26 namespace {
27   class PPC32CodeEmitter : public MachineFunctionPass {
28     TargetMachine &TM;
29     MachineCodeEmitter &MCE;
30
31     /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
32     /// its address in the function into this pointer.
33     void *MovePCtoLROffset;
34
35     // Tracks which instruction references which BasicBlock
36     std::vector<std::pair<MachineBasicBlock*, unsigned*> > BBRefs;
37     // Tracks where each BasicBlock starts
38     std::map<MachineBasicBlock*, long> BBLocations;
39
40     /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
41     ///
42     int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
43
44   public:
45     PPC32CodeEmitter(TargetMachine &T, MachineCodeEmitter &M)
46       : TM(T), MCE(M) {}
47
48     const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
49
50     /// runOnMachineFunction - emits the given MachineFunction to memory
51     ///
52     bool runOnMachineFunction(MachineFunction &MF);
53
54     /// emitBasicBlock - emits the given MachineBasicBlock to memory
55     ///
56     void emitBasicBlock(MachineBasicBlock &MBB);
57
58     /// emitWord - write a 32-bit word to memory at the current PC
59     ///
60     void emitWord(unsigned w) { MCE.emitWord(w); }
61
62     /// getValueBit - return the particular bit of Val
63     ///
64     unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
65
66     /// getBinaryCodeForInstr - This function, generated by the
67     /// CodeEmitterGenerator using TableGen, produces the binary encoding for
68     /// machine instructions.
69     ///
70     unsigned getBinaryCodeForInstr(MachineInstr &MI);
71   };
72 }
73
74 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to get
75 /// machine code emitted.  This uses a MachineCodeEmitter object to handle
76 /// actually outputting the machine code and resolving things like the address
77 /// of functions.  This method should returns true if machine code emission is
78 /// not supported.
79 ///
80 bool PPC32TargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
81                                                     MachineCodeEmitter &MCE) {
82   // Machine code emitter pass for PowerPC
83   PM.add(new PPC32CodeEmitter(*this, MCE));
84   // Delete machine code for this function after emitting it
85   PM.add(createMachineCodeDeleter());
86   return false;
87 }
88
89 bool PPC32CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
90   MovePCtoLROffset = 0;
91   MCE.startFunction(MF);
92   MCE.emitConstantPool(MF.getConstantPool());
93   for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
94     emitBasicBlock(*BB);
95   MCE.finishFunction(MF);
96
97   // Resolve branches to BasicBlocks for the entire function
98   for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
99     intptr_t Location = BBLocations[BBRefs[i].first];
100     unsigned *Ref = BBRefs[i].second;
101     DEBUG(std::cerr << "Fixup @ " << (void*)Ref << " to " << (void*)Location
102                     << "\n");
103     unsigned Instr = *Ref;
104     intptr_t BranchTargetDisp = (Location - (intptr_t)Ref) >> 2;
105
106     switch (Instr >> 26) {
107     default: assert(0 && "Unknown branch user!");
108     case 18:  // This is B or BL
109       *Ref |= (BranchTargetDisp & ((1 << 24)-1)) << 2;
110       break;
111     case 16:  // This is BLT,BLE,BEQ,BGE,BGT,BNE, or other bcx instruction
112       *Ref |= (BranchTargetDisp & ((1 << 14)-1)) << 2;
113       break;
114     }
115   }
116   BBRefs.clear();
117   BBLocations.clear();
118
119   return false;
120 }
121
122 void PPC32CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
123   BBLocations[&MBB] = MCE.getCurrentPCValue();
124   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
125     MachineInstr &MI = *I;
126     unsigned Opcode = MI.getOpcode();
127     switch (MI.getOpcode()) {
128     default:
129       emitWord(getBinaryCodeForInstr(*I));
130       break;
131     case PPC::IMPLICIT_DEF:
132       break; // pseudo opcode, no side effects
133     case PPC::MovePCtoLR:
134       assert(MovePCtoLROffset == 0 &&
135              "Multiple MovePCtoLR instructions in the function?");
136       MovePCtoLROffset = (void*)(intptr_t)MCE.getCurrentPCValue();
137       emitWord(0x48000005);    // bl 1
138       break;
139     }
140   }
141 }
142
143 static unsigned enumRegToMachineReg(unsigned enumReg) {
144   switch (enumReg) {
145   case PPC::R0 :  case PPC::F0 :  case PPC::CR0:  return  0;
146   case PPC::R1 :  case PPC::F1 :  case PPC::CR1:  return  1;
147   case PPC::R2 :  case PPC::F2 :  case PPC::CR2:  return  2;
148   case PPC::R3 :  case PPC::F3 :  case PPC::CR3:  return  3;
149   case PPC::R4 :  case PPC::F4 :  case PPC::CR4:  return  4;
150   case PPC::R5 :  case PPC::F5 :  case PPC::CR5:  return  5;
151   case PPC::R6 :  case PPC::F6 :  case PPC::CR6:  return  6;
152   case PPC::R7 :  case PPC::F7 :  case PPC::CR7:  return  7;
153   case PPC::R8 :  case PPC::F8 :  return  8;
154   case PPC::R9 :  case PPC::F9 :  return  9;
155   case PPC::R10:  case PPC::F10:  return 10;
156   case PPC::R11:  case PPC::F11:  return 11;
157   case PPC::R12:  case PPC::F12:  return 12;
158   case PPC::R13:  case PPC::F13:  return 13;
159   case PPC::R14:  case PPC::F14:  return 14;
160   case PPC::R15:  case PPC::F15:  return 15;
161   case PPC::R16:  case PPC::F16:  return 16;
162   case PPC::R17:  case PPC::F17:  return 17;
163   case PPC::R18:  case PPC::F18:  return 18;
164   case PPC::R19:  case PPC::F19:  return 19;
165   case PPC::R20:  case PPC::F20:  return 20;
166   case PPC::R21:  case PPC::F21:  return 21;
167   case PPC::R22:  case PPC::F22:  return 22;
168   case PPC::R23:  case PPC::F23:  return 23;
169   case PPC::R24:  case PPC::F24:  return 24;
170   case PPC::R25:  case PPC::F25:  return 25;
171   case PPC::R26:  case PPC::F26:  return 26;
172   case PPC::R27:  case PPC::F27:  return 27;
173   case PPC::R28:  case PPC::F28:  return 28;
174   case PPC::R29:  case PPC::F29:  return 29;
175   case PPC::R30:  case PPC::F30:  return 30;
176   case PPC::R31:  case PPC::F31:  return 31;
177   default:
178     std::cerr << "Unhandled reg in enumRegToRealReg!\n";
179     abort();
180   }
181 }
182
183 int PPC32CodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
184
185   int rv = 0; // Return value; defaults to 0 for unhandled cases
186                   // or things that get fixed up later by the JIT.
187   if (MO.isRegister()) {
188     rv = enumRegToMachineReg(MO.getReg());
189
190     // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
191     // register, not the register number directly.
192     if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
193         (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
194       rv = 0x80 >> rv;
195     }
196   } else if (MO.isImmediate()) {
197     rv = MO.getImmedValue();
198   } else if (MO.isGlobalAddress() || MO.isExternalSymbol()) {
199     bool isExternal = MO.isExternalSymbol() ||
200                       MO.getGlobal()->hasWeakLinkage() ||
201                       MO.getGlobal()->isExternal();
202     unsigned Reloc = 0;
203     int Offset = 0;
204     if (MI.getOpcode() == PPC::CALLpcrel)
205       Reloc = PPC::reloc_pcrel_bx;
206     else {
207       assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
208       Offset = -((intptr_t)MovePCtoLROffset+4);
209
210       if (MI.getOpcode() == PPC::LOADHiAddr) {
211         if (isExternal)
212           Reloc = PPC::reloc_absolute_ptr_high;   // Pointer to stub
213         else
214           Reloc = PPC::reloc_absolute_high;       // Pointer to symbol
215
216       } else if (MI.getOpcode() == PPC::LA) {
217         assert(!isExternal && "Something in the ISEL changed\n");
218
219         Reloc = PPC::reloc_absolute_low;
220       } else if (MI.getOpcode() == PPC::LWZ) {
221         Reloc = PPC::reloc_absolute_ptr_low;
222
223         assert(isExternal && "Something in the ISEL changed\n");
224       } else {
225         // These don't show up for global value references AFAIK, only for
226         // constant pool refs: PPC::LFS, PPC::LFD
227         assert(0 && "Unknown instruction for relocation!");
228       }
229     }
230     if (MO.isGlobalAddress())
231       MCE.addRelocation(MachineRelocation(MCE.getCurrentPCOffset(),
232                                           Reloc, MO.getGlobal(), Offset));
233     else
234       MCE.addRelocation(MachineRelocation(MCE.getCurrentPCOffset(),
235                                           Reloc, MO.getSymbolName(), Offset));
236   } else if (MO.isMachineBasicBlock()) {
237     unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
238     BBRefs.push_back(std::make_pair(MO.getMachineBasicBlock(), CurrPC));
239   } else if (MO.isConstantPoolIndex()) {
240     unsigned index = MO.getConstantPoolIndex();
241     assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
242     rv = MCE.getConstantPoolEntryAddress(index) - (intptr_t)MovePCtoLROffset-4;
243
244     unsigned Opcode = MI.getOpcode();
245     if (Opcode == PPC::LOADHiAddr) {
246       // LoadHiAddr wants hi16(addr - &MovePCtoLR)
247       if ((short)rv < 0) rv += 1 << 16;
248       rv >>= 16;
249     } else if (Opcode == PPC::LWZ || Opcode == PPC::LA ||
250                Opcode == PPC::LFS || Opcode == PPC::LFD) {
251       // These load opcodes want lo16(addr - &MovePCtoLR)
252       rv &= 0xffff;
253     } else {
254       assert(0 && "Unknown constant pool using instruction!");
255     }
256   } else {
257     std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
258     abort();
259   }
260
261   return rv;
262 }
263
264 #include "PPC32GenCodeEmitter.inc"
265