blob: 46332158fb5c710c9a99ebf42314462b590b86b8 [file] [log] [blame]
Nate Begeman21e463b2005-10-16 05:39:50 +00001//===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC32 -------*- C++ -*-=//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00008//===----------------------------------------------------------------------===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00009//
Misha Brukmand37faba2004-10-14 06:07:25 +000010// This file defines the PowerPC 32-bit CodeEmitter and associated machinery to
Gabor Greifa99be512007-07-05 17:07:56 +000011// JIT-compile bitcode to native PowerPC.
Misha Brukman5dfe3a92004-06-21 16:55:25 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner16e71f22005-10-14 23:59:06 +000015#include "PPCTargetMachine.h"
16#include "PPCRelocations.h"
Chris Lattner26689592005-10-14 23:51:18 +000017#include "PPC.h"
Misha Brukman3070e2f2004-10-21 01:42:02 +000018#include "llvm/Module.h"
Chris Lattnerde123822005-10-15 21:58:54 +000019#include "llvm/PassManager.h"
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000020#include "llvm/CodeGen/JITCodeEmitter.h"
Misha Brukmanb05daff2004-08-09 23:03:59 +000021#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukmand4b4a992004-10-23 23:47:34 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Torok Edwindac237e2009-07-08 20:53:28 +000024#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/raw_ostream.h"
Evan Chengd2ee2182006-02-18 00:08:58 +000026#include "llvm/Target/TargetOptions.h"
Chris Lattnereea9b132004-11-16 04:47:33 +000027using namespace llvm;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000028
Misha Brukmanb05daff2004-08-09 23:03:59 +000029namespace {
Chris Lattnerfc89bc92010-02-02 21:55:58 +000030 class PPCCodeEmitter : public MachineFunctionPass {
Misha Brukmanb05daff2004-08-09 23:03:59 +000031 TargetMachine &TM;
Chris Lattnerfc89bc92010-02-02 21:55:58 +000032 JITCodeEmitter &MCE;
Chris Lattner16112732010-03-14 01:41:15 +000033 MachineModuleInfo *MMI;
Chris Lattnerfc89bc92010-02-02 21:55:58 +000034
35 void getAnalysisUsage(AnalysisUsage &AU) const {
36 AU.addRequired<MachineModuleInfo>();
37 MachineFunctionPass::getAnalysisUsage(AU);
38 }
39
40 static char ID;
41
42 /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
43 /// its address in the function into this pointer.
44 void *MovePCtoLROffset;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000045 public:
Chris Lattnerfc89bc92010-02-02 21:55:58 +000046
47 PPCCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
Owen Anderson90c579d2010-08-06 18:33:48 +000048 : MachineFunctionPass(ID), TM(tm), MCE(mce) {}
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000049
50 /// getBinaryCodeForInstr - This function, generated by the
51 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
52 /// machine instructions.
Jim Grosbachbade37b2010-10-08 00:21:28 +000053 unsigned getBinaryCodeForInstr(const MachineInstr &MI) const;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000054
Chris Lattnera9d9ab92010-11-15 05:57:53 +000055
56 MachineRelocation GetRelocation(const MachineOperand &MO,
57 unsigned RelocID) const;
58
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000059 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000060 unsigned getMachineOpValue(const MachineInstr &MI,
Jim Grosbachbade37b2010-10-08 00:21:28 +000061 const MachineOperand &MO) const;
Misha Brukmanb05daff2004-08-09 23:03:59 +000062
Chris Lattner7192eb82010-11-15 05:19:25 +000063 unsigned get_crbitm_encoding(const MachineInstr &MI, unsigned OpNo) const;
Chris Lattner8d704112010-11-15 06:09:35 +000064 unsigned getDirectBrEncoding(const MachineInstr &MI, unsigned OpNo) const;
65 unsigned getCondBrEncoding(const MachineInstr &MI, unsigned OpNo) const;
Chris Lattner85cf7d72010-11-15 06:33:39 +000066
67 unsigned getHA16Encoding(const MachineInstr &MI, unsigned OpNo) const;
68 unsigned getLO16Encoding(const MachineInstr &MI, unsigned OpNo) const;
Chris Lattnerb7035d02010-11-15 08:22:03 +000069 unsigned getMemRIEncoding(const MachineInstr &MI, unsigned OpNo) const;
Chris Lattner17e2c182010-11-15 08:02:41 +000070 unsigned getMemRIXEncoding(const MachineInstr &MI, unsigned OpNo) const;
Chris Lattner85cf7d72010-11-15 06:33:39 +000071
Misha Brukmanb05daff2004-08-09 23:03:59 +000072 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
73
74 /// runOnMachineFunction - emits the given MachineFunction to memory
75 ///
76 bool runOnMachineFunction(MachineFunction &MF);
77
78 /// emitBasicBlock - emits the given MachineBasicBlock to memory
79 ///
80 void emitBasicBlock(MachineBasicBlock &MBB);
Misha Brukmanb05daff2004-08-09 23:03:59 +000081 };
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000082}
Daniel Dunbara279bc32009-09-20 02:20:51 +000083
Chris Lattnerfc89bc92010-02-02 21:55:58 +000084char PPCCodeEmitter::ID = 0;
85
Nate Begemaneb883af2006-08-23 21:08:52 +000086/// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
87/// to the specified MCE object.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000088FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000089 JITCodeEmitter &JCE) {
Chris Lattnerfc89bc92010-02-02 21:55:58 +000090 return new PPCCodeEmitter(TM, JCE);
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000091}
92
Chris Lattnerfc89bc92010-02-02 21:55:58 +000093bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng4c1aa862006-02-22 20:19:42 +000094 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
95 MF.getTarget().getRelocationModel() != Reloc::Static) &&
96 "JIT relocation model must be set to static or default!");
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000097
Chris Lattner16112732010-03-14 01:41:15 +000098 MMI = &getAnalysis<MachineModuleInfo>();
99 MCE.setModuleInfo(MMI);
Chris Lattner43b429b2006-05-02 18:27:26 +0000100 do {
Chris Lattnere150b8e2006-12-08 04:54:03 +0000101 MovePCtoLROffset = 0;
Chris Lattner43b429b2006-05-02 18:27:26 +0000102 MCE.startFunction(MF);
Chris Lattner43b429b2006-05-02 18:27:26 +0000103 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
104 emitBasicBlock(*BB);
Chris Lattner43b429b2006-05-02 18:27:26 +0000105 } while (MCE.finishFunction(MF));
Misha Brukman3070e2f2004-10-21 01:42:02 +0000106
Misha Brukmanb05daff2004-08-09 23:03:59 +0000107 return false;
108}
109
Chris Lattnerfc89bc92010-02-02 21:55:58 +0000110void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerb4432f32006-05-03 17:10:41 +0000111 MCE.StartMachineBasicBlock(&MBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000112
Misha Brukmana4df3502004-10-23 18:28:01 +0000113 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
Evan Chengacff3392008-09-02 06:51:36 +0000114 const MachineInstr &MI = *I;
Devang Patelaf0e2722009-10-06 02:19:11 +0000115 MCE.processDebugLoc(MI.getDebugLoc(), true);
Chris Lattnere94c5172004-11-23 05:59:53 +0000116 switch (MI.getOpcode()) {
117 default:
Evan Chengacff3392008-09-02 06:51:36 +0000118 MCE.emitWordBE(getBinaryCodeForInstr(MI));
Chris Lattnere94c5172004-11-23 05:59:53 +0000119 break;
Bill Wendling7431bea2010-07-16 22:20:36 +0000120 case TargetOpcode::PROLOG_LABEL:
Chris Lattner7561d482010-03-14 02:33:54 +0000121 case TargetOpcode::EH_LABEL:
122 MCE.emitLabel(MI.getOperand(0).getMCSymbol());
123 break;
Chris Lattner518bb532010-02-09 19:54:29 +0000124 case TargetOpcode::IMPLICIT_DEF:
125 case TargetOpcode::KILL:
Evan Chengd1833072008-03-17 06:56:52 +0000126 break; // pseudo opcode, no side effects
Chris Lattnere94c5172004-11-23 05:59:53 +0000127 case PPC::MovePCtoLR:
Chris Lattner6a5339b2006-11-14 18:44:47 +0000128 case PPC::MovePCtoLR8:
Chris Lattnere150b8e2006-12-08 04:54:03 +0000129 assert(TM.getRelocationModel() == Reloc::PIC_);
130 MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
131 MCE.emitWordBE(0x48000005); // bl 1
Chris Lattnere94c5172004-11-23 05:59:53 +0000132 break;
133 }
Devang Patelaf0e2722009-10-06 02:19:11 +0000134 MCE.processDebugLoc(MI.getDebugLoc(), false);
Misha Brukmana4df3502004-10-23 18:28:01 +0000135 }
Misha Brukmanb05daff2004-08-09 23:03:59 +0000136}
137
Chris Lattner7192eb82010-11-15 05:19:25 +0000138unsigned PPCCodeEmitter::get_crbitm_encoding(const MachineInstr &MI,
139 unsigned OpNo) const {
140 const MachineOperand &MO = MI.getOperand(OpNo);
141 assert((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
142 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
143 return 0x80 >> PPCRegisterInfo::getRegisterNumbering(MO.getReg());
144}
145
Chris Lattnera9d9ab92010-11-15 05:57:53 +0000146MachineRelocation PPCCodeEmitter::GetRelocation(const MachineOperand &MO,
147 unsigned RelocID) const {
Chris Lattner85cf7d72010-11-15 06:33:39 +0000148 // If in PIC mode, we need to encode the negated address of the
149 // 'movepctolr' into the unrelocated field. After relocation, we'll have
150 // &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm
151 // field, we get &gv. This doesn't happen for branch relocations, which are
152 // always implicitly pc relative.
153 intptr_t Cst = 0;
154 if (TM.getRelocationModel() == Reloc::PIC_) {
155 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
156 Cst = -(intptr_t)MovePCtoLROffset - 4;
157 }
158
Chris Lattnera9d9ab92010-11-15 05:57:53 +0000159 if (MO.isGlobal())
160 return MachineRelocation::getGV(MCE.getCurrentPCOffset(), RelocID,
Chris Lattner85cf7d72010-11-15 06:33:39 +0000161 const_cast<GlobalValue *>(MO.getGlobal()),
162 Cst, isa<Function>(MO.getGlobal()));
Chris Lattnera9d9ab92010-11-15 05:57:53 +0000163 if (MO.isSymbol())
164 return MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Chris Lattner85cf7d72010-11-15 06:33:39 +0000165 RelocID, MO.getSymbolName(), Cst);
Chris Lattnera9d9ab92010-11-15 05:57:53 +0000166 if (MO.isCPI())
167 return MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Chris Lattner85cf7d72010-11-15 06:33:39 +0000168 RelocID, MO.getIndex(), Cst);
Chris Lattnera9d9ab92010-11-15 05:57:53 +0000169
170 if (MO.isMBB())
171 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
172 RelocID, MO.getMBB()));
173
174 assert(MO.isJTI());
175 return MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Chris Lattner85cf7d72010-11-15 06:33:39 +0000176 RelocID, MO.getIndex(), Cst);
Chris Lattnera9d9ab92010-11-15 05:57:53 +0000177}
178
Chris Lattner8d704112010-11-15 06:09:35 +0000179unsigned PPCCodeEmitter::getDirectBrEncoding(const MachineInstr &MI,
180 unsigned OpNo) const {
Chris Lattnera9d9ab92010-11-15 05:57:53 +0000181 const MachineOperand &MO = MI.getOperand(OpNo);
182 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
183
184 MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bx));
185 return 0;
186}
187
Chris Lattner8d704112010-11-15 06:09:35 +0000188unsigned PPCCodeEmitter::getCondBrEncoding(const MachineInstr &MI,
189 unsigned OpNo) const {
190 const MachineOperand &MO = MI.getOperand(OpNo);
191 MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bcx));
192 return 0;
193}
194
Chris Lattner85cf7d72010-11-15 06:33:39 +0000195unsigned PPCCodeEmitter::getHA16Encoding(const MachineInstr &MI,
196 unsigned OpNo) const {
197 const MachineOperand &MO = MI.getOperand(OpNo);
198 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
199
200 MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_high));
201 return 0;
202}
203
204unsigned PPCCodeEmitter::getLO16Encoding(const MachineInstr &MI,
205 unsigned OpNo) const {
206 const MachineOperand &MO = MI.getOperand(OpNo);
207 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
208
209 MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low));
210 return 0;
211}
212
Chris Lattnerb7035d02010-11-15 08:22:03 +0000213unsigned PPCCodeEmitter::getMemRIEncoding(const MachineInstr &MI,
214 unsigned OpNo) const {
215 // Encode (imm, reg) as a memri, which has the low 16-bits as the
216 // displacement and the next 5 bits as the register #.
217 assert(MI.getOperand(OpNo+1).isReg());
218 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1)) << 16;
219
220 const MachineOperand &MO = MI.getOperand(OpNo);
221 if (MO.isImm())
222 return (getMachineOpValue(MI, MO) & 0xFFFF) | RegBits;
223
224 // Add a fixup for the displacement field.
225 MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low));
226 return RegBits;
227}
228
Chris Lattner17e2c182010-11-15 08:02:41 +0000229unsigned PPCCodeEmitter::getMemRIXEncoding(const MachineInstr &MI,
230 unsigned OpNo) const {
231 // Encode (imm, reg) as a memrix, which has the low 14-bits as the
232 // displacement and the next 5 bits as the register #.
233 assert(MI.getOperand(OpNo+1).isReg());
234 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1)) << 14;
235
Chris Lattner85cf7d72010-11-15 06:33:39 +0000236 const MachineOperand &MO = MI.getOperand(OpNo);
Chris Lattner17e2c182010-11-15 08:02:41 +0000237 if (MO.isImm())
238 return (getMachineOpValue(MI, MO) & 0x3FFF) | RegBits;
Chris Lattner85cf7d72010-11-15 06:33:39 +0000239
240 MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low_ix));
Chris Lattner17e2c182010-11-15 08:02:41 +0000241 return RegBits;
Chris Lattner85cf7d72010-11-15 06:33:39 +0000242}
243
Chris Lattner7192eb82010-11-15 05:19:25 +0000244
Evan Chengacff3392008-09-02 06:51:36 +0000245unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
Jim Grosbachbade37b2010-10-08 00:21:28 +0000246 const MachineOperand &MO) const {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000247
Dan Gohmand735b802008-10-03 15:45:36 +0000248 if (MO.isReg()) {
Chris Lattner7192eb82010-11-15 05:19:25 +0000249 assert(MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF);
250 return PPCRegisterInfo::getRegisterNumbering(MO.getReg());
251 }
252
Chris Lattnerb7035d02010-11-15 08:22:03 +0000253 assert(MO.isImm() &&
254 "Relocation required in an instruction that we cannot encode!");
255 return MO.getImm();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000256}
257
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000258#include "PPCGenCodeEmitter.inc"