blob: 382d709afa8f67592df257581bc286d4f18abdb4 [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC -----------------===//
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 Lattner26689592005-10-14 23:51:18 +000015#include "PPC.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "PPCRelocations.h"
17#include "PPCTargetMachine.h"
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000018#include "llvm/CodeGen/JITCodeEmitter.h"
Misha Brukmanb05daff2004-08-09 23:03:59 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukmand4b4a992004-10-23 23:47:34 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000022#include "llvm/IR/Module.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/PassManager.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.
Owen Anderson4f8dc7b2012-01-24 18:37:29 +000053 uint64_t 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;
Ulrich Weigand9679c472013-06-24 11:03:33 +000066 unsigned getAbsDirectBrEncoding(const MachineInstr &MI,
67 unsigned OpNo) const;
68 unsigned getAbsCondBrEncoding(const MachineInstr &MI, unsigned OpNo) const;
Chris Lattner85cf7d72010-11-15 06:33:39 +000069
Ulrich Weigand0b859422013-06-26 13:49:15 +000070 unsigned getImm16Encoding(const MachineInstr &MI, unsigned OpNo) const;
Chris Lattnerb7035d02010-11-15 08:22:03 +000071 unsigned getMemRIEncoding(const MachineInstr &MI, unsigned OpNo) const;
Chris Lattner17e2c182010-11-15 08:02:41 +000072 unsigned getMemRIXEncoding(const MachineInstr &MI, unsigned OpNo) const;
Bill Schmidtd7802bf2012-12-04 16:18:08 +000073 unsigned getTLSRegEncoding(const MachineInstr &MI, unsigned OpNo) const;
Chris Lattner85cf7d72010-11-15 06:33:39 +000074
Misha Brukmanb05daff2004-08-09 23:03:59 +000075 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
76
77 /// runOnMachineFunction - emits the given MachineFunction to memory
78 ///
79 bool runOnMachineFunction(MachineFunction &MF);
80
81 /// emitBasicBlock - emits the given MachineBasicBlock to memory
82 ///
83 void emitBasicBlock(MachineBasicBlock &MBB);
Misha Brukmanb05daff2004-08-09 23:03:59 +000084 };
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000085}
Daniel Dunbara279bc32009-09-20 02:20:51 +000086
Chris Lattnerfc89bc92010-02-02 21:55:58 +000087char PPCCodeEmitter::ID = 0;
88
Nate Begemaneb883af2006-08-23 21:08:52 +000089/// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
90/// to the specified MCE object.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000091FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000092 JITCodeEmitter &JCE) {
Chris Lattnerfc89bc92010-02-02 21:55:58 +000093 return new PPCCodeEmitter(TM, JCE);
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000094}
95
Chris Lattnerfc89bc92010-02-02 21:55:58 +000096bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng4c1aa862006-02-22 20:19:42 +000097 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
98 MF.getTarget().getRelocationModel() != Reloc::Static) &&
99 "JIT relocation model must be set to static or default!");
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000100
Chris Lattner16112732010-03-14 01:41:15 +0000101 MMI = &getAnalysis<MachineModuleInfo>();
102 MCE.setModuleInfo(MMI);
Chris Lattner43b429b2006-05-02 18:27:26 +0000103 do {
Chris Lattnere150b8e2006-12-08 04:54:03 +0000104 MovePCtoLROffset = 0;
Chris Lattner43b429b2006-05-02 18:27:26 +0000105 MCE.startFunction(MF);
Chris Lattner43b429b2006-05-02 18:27:26 +0000106 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
107 emitBasicBlock(*BB);
Chris Lattner43b429b2006-05-02 18:27:26 +0000108 } while (MCE.finishFunction(MF));
Misha Brukman3070e2f2004-10-21 01:42:02 +0000109
Misha Brukmanb05daff2004-08-09 23:03:59 +0000110 return false;
111}
112
Chris Lattnerfc89bc92010-02-02 21:55:58 +0000113void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerb4432f32006-05-03 17:10:41 +0000114 MCE.StartMachineBasicBlock(&MBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000115
Misha Brukmana4df3502004-10-23 18:28:01 +0000116 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
Evan Chengacff3392008-09-02 06:51:36 +0000117 const MachineInstr &MI = *I;
Devang Patelaf0e2722009-10-06 02:19:11 +0000118 MCE.processDebugLoc(MI.getDebugLoc(), true);
Chris Lattnere94c5172004-11-23 05:59:53 +0000119 switch (MI.getOpcode()) {
120 default:
Evan Chengacff3392008-09-02 06:51:36 +0000121 MCE.emitWordBE(getBinaryCodeForInstr(MI));
Chris Lattnere94c5172004-11-23 05:59:53 +0000122 break;
Bill Wendling7431bea2010-07-16 22:20:36 +0000123 case TargetOpcode::PROLOG_LABEL:
Chris Lattner7561d482010-03-14 02:33:54 +0000124 case TargetOpcode::EH_LABEL:
125 MCE.emitLabel(MI.getOperand(0).getMCSymbol());
126 break;
Chris Lattner518bb532010-02-09 19:54:29 +0000127 case TargetOpcode::IMPLICIT_DEF:
128 case TargetOpcode::KILL:
Evan Chengd1833072008-03-17 06:56:52 +0000129 break; // pseudo opcode, no side effects
Chris Lattnere94c5172004-11-23 05:59:53 +0000130 case PPC::MovePCtoLR:
Chris Lattner6a5339b2006-11-14 18:44:47 +0000131 case PPC::MovePCtoLR8:
Chris Lattnere150b8e2006-12-08 04:54:03 +0000132 assert(TM.getRelocationModel() == Reloc::PIC_);
133 MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
134 MCE.emitWordBE(0x48000005); // bl 1
Chris Lattnere94c5172004-11-23 05:59:53 +0000135 break;
136 }
Devang Patelaf0e2722009-10-06 02:19:11 +0000137 MCE.processDebugLoc(MI.getDebugLoc(), false);
Misha Brukmana4df3502004-10-23 18:28:01 +0000138 }
Misha Brukmanb05daff2004-08-09 23:03:59 +0000139}
140
Chris Lattner7192eb82010-11-15 05:19:25 +0000141unsigned PPCCodeEmitter::get_crbitm_encoding(const MachineInstr &MI,
142 unsigned OpNo) const {
143 const MachineOperand &MO = MI.getOperand(OpNo);
Hal Finkel234bb382011-12-07 06:34:06 +0000144 assert((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MTCRF8 ||
145 MI.getOpcode() == PPC::MFOCRF) &&
Chris Lattner7192eb82010-11-15 05:19:25 +0000146 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
Hal Finkelaa6047d2013-03-26 20:08:20 +0000147 return 0x80 >> TM.getRegisterInfo()->getEncodingValue(MO.getReg());
Chris Lattner7192eb82010-11-15 05:19:25 +0000148}
149
Chris Lattnera9d9ab92010-11-15 05:57:53 +0000150MachineRelocation PPCCodeEmitter::GetRelocation(const MachineOperand &MO,
151 unsigned RelocID) const {
Chris Lattner85cf7d72010-11-15 06:33:39 +0000152 // If in PIC mode, we need to encode the negated address of the
153 // 'movepctolr' into the unrelocated field. After relocation, we'll have
154 // &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm
155 // field, we get &gv. This doesn't happen for branch relocations, which are
156 // always implicitly pc relative.
157 intptr_t Cst = 0;
158 if (TM.getRelocationModel() == Reloc::PIC_) {
159 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
160 Cst = -(intptr_t)MovePCtoLROffset - 4;
161 }
162
Chris Lattnera9d9ab92010-11-15 05:57:53 +0000163 if (MO.isGlobal())
164 return MachineRelocation::getGV(MCE.getCurrentPCOffset(), RelocID,
Chris Lattner85cf7d72010-11-15 06:33:39 +0000165 const_cast<GlobalValue *>(MO.getGlobal()),
166 Cst, isa<Function>(MO.getGlobal()));
Chris Lattnera9d9ab92010-11-15 05:57:53 +0000167 if (MO.isSymbol())
168 return MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Chris Lattner85cf7d72010-11-15 06:33:39 +0000169 RelocID, MO.getSymbolName(), Cst);
Chris Lattnera9d9ab92010-11-15 05:57:53 +0000170 if (MO.isCPI())
171 return MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Chris Lattner85cf7d72010-11-15 06:33:39 +0000172 RelocID, MO.getIndex(), Cst);
Chris Lattnera9d9ab92010-11-15 05:57:53 +0000173
174 if (MO.isMBB())
Chris Lattnerbc443412010-11-15 22:50:50 +0000175 return MachineRelocation::getBB(MCE.getCurrentPCOffset(),
176 RelocID, MO.getMBB());
Chris Lattnera9d9ab92010-11-15 05:57:53 +0000177
178 assert(MO.isJTI());
179 return MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Chris Lattner85cf7d72010-11-15 06:33:39 +0000180 RelocID, MO.getIndex(), Cst);
Chris Lattnera9d9ab92010-11-15 05:57:53 +0000181}
182
Chris Lattner8d704112010-11-15 06:09:35 +0000183unsigned PPCCodeEmitter::getDirectBrEncoding(const MachineInstr &MI,
184 unsigned OpNo) const {
Chris Lattnera9d9ab92010-11-15 05:57:53 +0000185 const MachineOperand &MO = MI.getOperand(OpNo);
186 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
187
188 MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bx));
189 return 0;
190}
191
Chris Lattner8d704112010-11-15 06:09:35 +0000192unsigned PPCCodeEmitter::getCondBrEncoding(const MachineInstr &MI,
193 unsigned OpNo) const {
194 const MachineOperand &MO = MI.getOperand(OpNo);
195 MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bcx));
196 return 0;
197}
198
Ulrich Weigand9679c472013-06-24 11:03:33 +0000199unsigned PPCCodeEmitter::getAbsDirectBrEncoding(const MachineInstr &MI,
200 unsigned OpNo) const {
201 const MachineOperand &MO = MI.getOperand(OpNo);
202 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
203
204 llvm_unreachable("Absolute branch relocations unsupported on the old JIT.");
205}
206
207unsigned PPCCodeEmitter::getAbsCondBrEncoding(const MachineInstr &MI,
208 unsigned OpNo) const {
209 llvm_unreachable("Absolute branch relocations unsupported on the old JIT.");
210}
211
Ulrich Weigand0b859422013-06-26 13:49:15 +0000212unsigned PPCCodeEmitter::getImm16Encoding(const MachineInstr &MI,
213 unsigned OpNo) const {
Chris Lattner85cf7d72010-11-15 06:33:39 +0000214 const MachineOperand &MO = MI.getOperand(OpNo);
215 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
216
Ulrich Weigand4456a8e2013-05-17 14:14:12 +0000217 unsigned RelocID;
218 switch (MO.getTargetFlags() & PPCII::MO_ACCESS_MASK) {
219 default: llvm_unreachable("Unsupported target operand flags!");
Ulrich Weigand92cfa612013-06-21 14:42:20 +0000220 case PPCII::MO_LO: RelocID = PPC::reloc_absolute_low; break;
221 case PPCII::MO_HA: RelocID = PPC::reloc_absolute_high; break;
Ulrich Weigand4456a8e2013-05-17 14:14:12 +0000222 }
Chris Lattner85cf7d72010-11-15 06:33:39 +0000223
Ulrich Weigand4456a8e2013-05-17 14:14:12 +0000224 MCE.addRelocation(GetRelocation(MO, RelocID));
Chris Lattner85cf7d72010-11-15 06:33:39 +0000225 return 0;
226}
227
Chris Lattnerb7035d02010-11-15 08:22:03 +0000228unsigned PPCCodeEmitter::getMemRIEncoding(const MachineInstr &MI,
229 unsigned OpNo) const {
230 // Encode (imm, reg) as a memri, which has the low 16-bits as the
231 // displacement and the next 5 bits as the register #.
232 assert(MI.getOperand(OpNo+1).isReg());
233 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1)) << 16;
234
235 const MachineOperand &MO = MI.getOperand(OpNo);
236 if (MO.isImm())
237 return (getMachineOpValue(MI, MO) & 0xFFFF) | RegBits;
238
239 // Add a fixup for the displacement field.
240 MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low));
241 return RegBits;
242}
243
Chris Lattner17e2c182010-11-15 08:02:41 +0000244unsigned PPCCodeEmitter::getMemRIXEncoding(const MachineInstr &MI,
245 unsigned OpNo) const {
246 // Encode (imm, reg) as a memrix, which has the low 14-bits as the
247 // displacement and the next 5 bits as the register #.
248 assert(MI.getOperand(OpNo+1).isReg());
249 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1)) << 14;
250
Chris Lattner85cf7d72010-11-15 06:33:39 +0000251 const MachineOperand &MO = MI.getOperand(OpNo);
Chris Lattner17e2c182010-11-15 08:02:41 +0000252 if (MO.isImm())
Ulrich Weigand347a5072013-05-16 17:58:02 +0000253 return ((getMachineOpValue(MI, MO) >> 2) & 0x3FFF) | RegBits;
Chris Lattner85cf7d72010-11-15 06:33:39 +0000254
255 MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low_ix));
Chris Lattner17e2c182010-11-15 08:02:41 +0000256 return RegBits;
Chris Lattner85cf7d72010-11-15 06:33:39 +0000257}
258
Chris Lattner7192eb82010-11-15 05:19:25 +0000259
Bill Schmidtd7802bf2012-12-04 16:18:08 +0000260unsigned PPCCodeEmitter::getTLSRegEncoding(const MachineInstr &MI,
261 unsigned OpNo) const {
262 llvm_unreachable("TLS not supported on the old JIT.");
263 return 0;
264}
265
266
Evan Chengacff3392008-09-02 06:51:36 +0000267unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
Jim Grosbachbade37b2010-10-08 00:21:28 +0000268 const MachineOperand &MO) const {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000269
Dan Gohmand735b802008-10-03 15:45:36 +0000270 if (MO.isReg()) {
Chris Lattnerb69cdfa2010-11-16 00:55:51 +0000271 // MTCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
272 // The GPR operand should come through here though.
Hal Finkel234bb382011-12-07 06:34:06 +0000273 assert((MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MTCRF8 &&
274 MI.getOpcode() != PPC::MFOCRF) ||
Chris Lattnerb69cdfa2010-11-16 00:55:51 +0000275 MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
Hal Finkelaa6047d2013-03-26 20:08:20 +0000276 return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
Chris Lattner7192eb82010-11-15 05:19:25 +0000277 }
278
Chris Lattnerb7035d02010-11-15 08:22:03 +0000279 assert(MO.isImm() &&
280 "Relocation required in an instruction that we cannot encode!");
281 return MO.getImm();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000282}
283
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000284#include "PPCGenCodeEmitter.inc"