blob: 08755238f9250d02c8c127352e37b43013efb7d8 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC -----------------===//
Misha Brukmanb4402432005-04-21 23:30:14 +00002//
Misha Brukmane05203f2004-06-21 16:55:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb4402432005-04-21 23:30:14 +00007//
Misha Brukmane05203f2004-06-21 16:55:25 +00008//===----------------------------------------------------------------------===//
Misha Brukmanb4402432005-04-21 23:30:14 +00009//
Misha Brukman8b1bf432004-10-14 06:07:25 +000010// This file defines the PowerPC 32-bit CodeEmitter and associated machinery to
Gabor Greife16561c2007-07-05 17:07:56 +000011// JIT-compile bitcode to native PowerPC.
Misha Brukmane05203f2004-06-21 16:55:25 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000015#include "PPC.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "PPCRelocations.h"
17#include "PPCTargetMachine.h"
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +000018#include "llvm/CodeGen/JITCodeEmitter.h"
Misha Brukman18922912004-08-09 23:03:59 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman9ce0da92004-10-23 23:47:34 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Nicolas Geoffray21ad4942008-02-13 18:39:37 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Module.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/PassManager.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000024#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/raw_ostream.h"
Evan Cheng5f997602006-02-18 00:08:58 +000026#include "llvm/Target/TargetOptions.h"
Chris Lattnerf2429792004-11-16 04:47:33 +000027using namespace llvm;
Misha Brukmane05203f2004-06-21 16:55:25 +000028
Misha Brukman18922912004-08-09 23:03:59 +000029namespace {
Chris Lattner308acc42010-02-02 21:55:58 +000030 class PPCCodeEmitter : public MachineFunctionPass {
Misha Brukman18922912004-08-09 23:03:59 +000031 TargetMachine &TM;
Chris Lattner308acc42010-02-02 21:55:58 +000032 JITCodeEmitter &MCE;
Chris Lattner34adc8d2010-03-14 01:41:15 +000033 MachineModuleInfo *MMI;
Chris Lattner308acc42010-02-02 21:55:58 +000034
Craig Topper0d3fa922014-04-29 07:57:37 +000035 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner308acc42010-02-02 21:55:58 +000036 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 Lopesa194c3a2009-05-30 20:51:52 +000045 public:
Chris Lattner308acc42010-02-02 21:55:58 +000046
47 PPCCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
Owen Andersona7aed182010-08-06 18:33:48 +000048 : MachineFunctionPass(ID), TM(tm), MCE(mce) {}
Bruno Cardoso Lopesa194c3a2009-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 Andersond845d9d2012-01-24 18:37:29 +000053 uint64_t getBinaryCodeForInstr(const MachineInstr &MI) const;
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +000054
Chris Lattner79fa3712010-11-15 05:57:53 +000055
56 MachineRelocation GetRelocation(const MachineOperand &MO,
57 unsigned RelocID) const;
58
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +000059 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +000060 unsigned getMachineOpValue(const MachineInstr &MI,
Jim Grosbacha7b6d582010-10-08 00:21:28 +000061 const MachineOperand &MO) const;
Misha Brukman18922912004-08-09 23:03:59 +000062
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000063 unsigned get_crbitm_encoding(const MachineInstr &MI, unsigned OpNo) const;
Chris Lattner0e3461e2010-11-15 06:09:35 +000064 unsigned getDirectBrEncoding(const MachineInstr &MI, unsigned OpNo) const;
65 unsigned getCondBrEncoding(const MachineInstr &MI, unsigned OpNo) const;
Ulrich Weigandb6a30d12013-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 Lattner65661122010-11-15 06:33:39 +000069
Ulrich Weigandfd3ad692013-06-26 13:49:15 +000070 unsigned getImm16Encoding(const MachineInstr &MI, unsigned OpNo) const;
Chris Lattnerefacb9e2010-11-15 08:22:03 +000071 unsigned getMemRIEncoding(const MachineInstr &MI, unsigned OpNo) const;
Chris Lattner8f4444d2010-11-15 08:02:41 +000072 unsigned getMemRIXEncoding(const MachineInstr &MI, unsigned OpNo) const;
Bill Schmidtca4a0c92012-12-04 16:18:08 +000073 unsigned getTLSRegEncoding(const MachineInstr &MI, unsigned OpNo) const;
Ulrich Weigand5143bab2013-07-02 21:31:04 +000074 unsigned getTLSCallEncoding(const MachineInstr &MI, unsigned OpNo) const;
Chris Lattner65661122010-11-15 06:33:39 +000075
Craig Topper0d3fa922014-04-29 07:57:37 +000076 const char *getPassName() const override {
77 return "PowerPC Machine Code Emitter";
78 }
Misha Brukman18922912004-08-09 23:03:59 +000079
80 /// runOnMachineFunction - emits the given MachineFunction to memory
81 ///
Craig Topper0d3fa922014-04-29 07:57:37 +000082 bool runOnMachineFunction(MachineFunction &MF) override;
Misha Brukman18922912004-08-09 23:03:59 +000083
84 /// emitBasicBlock - emits the given MachineBasicBlock to memory
85 ///
86 void emitBasicBlock(MachineBasicBlock &MBB);
Misha Brukman18922912004-08-09 23:03:59 +000087 };
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +000088}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000089
Chris Lattner308acc42010-02-02 21:55:58 +000090char PPCCodeEmitter::ID = 0;
91
Nate Begeman3cb39212006-08-23 21:08:52 +000092/// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
93/// to the specified MCE object.
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +000094FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +000095 JITCodeEmitter &JCE) {
Chris Lattner308acc42010-02-02 21:55:58 +000096 return new PPCCodeEmitter(TM, JCE);
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +000097}
98
Chris Lattner308acc42010-02-02 21:55:58 +000099bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng73136df2006-02-22 20:19:42 +0000100 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
101 MF.getTarget().getRelocationModel() != Reloc::Static) &&
102 "JIT relocation model must be set to static or default!");
Nicolas Geoffray21ad4942008-02-13 18:39:37 +0000103
Chris Lattner34adc8d2010-03-14 01:41:15 +0000104 MMI = &getAnalysis<MachineModuleInfo>();
105 MCE.setModuleInfo(MMI);
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000106 do {
Craig Topper062a2ba2014-04-25 05:30:21 +0000107 MovePCtoLROffset = nullptr;
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000108 MCE.startFunction(MF);
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000109 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
110 emitBasicBlock(*BB);
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000111 } while (MCE.finishFunction(MF));
Misha Brukman2beb63a2004-10-21 01:42:02 +0000112
Misha Brukman18922912004-08-09 23:03:59 +0000113 return false;
114}
115
Chris Lattner308acc42010-02-02 21:55:58 +0000116void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Chris Lattner1d8ee1f2006-05-03 17:10:41 +0000117 MCE.StartMachineBasicBlock(&MBB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000118
Misha Brukman421c3c12004-10-23 18:28:01 +0000119 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
Evan Cheng34f3a962008-09-02 06:51:36 +0000120 const MachineInstr &MI = *I;
Devang Patel051454a2009-10-06 02:19:11 +0000121 MCE.processDebugLoc(MI.getDebugLoc(), true);
Chris Lattner743a4342004-11-23 05:59:53 +0000122 switch (MI.getOpcode()) {
123 default:
Evan Cheng34f3a962008-09-02 06:51:36 +0000124 MCE.emitWordBE(getBinaryCodeForInstr(MI));
Chris Lattner743a4342004-11-23 05:59:53 +0000125 break;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000126 case TargetOpcode::CFI_INSTRUCTION:
127 break;
Chris Lattneree2fbbc2010-03-14 02:33:54 +0000128 case TargetOpcode::EH_LABEL:
129 MCE.emitLabel(MI.getOperand(0).getMCSymbol());
130 break;
Chris Lattnerb06015a2010-02-09 19:54:29 +0000131 case TargetOpcode::IMPLICIT_DEF:
132 case TargetOpcode::KILL:
Evan Cheng24bc1232008-03-17 06:56:52 +0000133 break; // pseudo opcode, no side effects
Chris Lattner743a4342004-11-23 05:59:53 +0000134 case PPC::MovePCtoLR:
Chris Lattner44dbdbe2006-11-14 18:44:47 +0000135 case PPC::MovePCtoLR8:
Chris Lattner09fecf92006-12-08 04:54:03 +0000136 assert(TM.getRelocationModel() == Reloc::PIC_);
137 MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
138 MCE.emitWordBE(0x48000005); // bl 1
Chris Lattner743a4342004-11-23 05:59:53 +0000139 break;
140 }
Devang Patel051454a2009-10-06 02:19:11 +0000141 MCE.processDebugLoc(MI.getDebugLoc(), false);
Misha Brukman421c3c12004-10-23 18:28:01 +0000142 }
Misha Brukman18922912004-08-09 23:03:59 +0000143}
144
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000145unsigned PPCCodeEmitter::get_crbitm_encoding(const MachineInstr &MI,
146 unsigned OpNo) const {
147 const MachineOperand &MO = MI.getOperand(OpNo);
Ulrich Weigand49f487e2013-07-03 17:59:07 +0000148 assert((MI.getOpcode() == PPC::MTOCRF || MI.getOpcode() == PPC::MTOCRF8 ||
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000149 MI.getOpcode() == PPC::MFOCRF || MI.getOpcode() == PPC::MFOCRF8) &&
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000150 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
Hal Finkelfeea6532013-03-26 20:08:20 +0000151 return 0x80 >> TM.getRegisterInfo()->getEncodingValue(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000152}
153
Chris Lattner79fa3712010-11-15 05:57:53 +0000154MachineRelocation PPCCodeEmitter::GetRelocation(const MachineOperand &MO,
155 unsigned RelocID) const {
Chris Lattner65661122010-11-15 06:33:39 +0000156 // If in PIC mode, we need to encode the negated address of the
157 // 'movepctolr' into the unrelocated field. After relocation, we'll have
158 // &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm
159 // field, we get &gv. This doesn't happen for branch relocations, which are
160 // always implicitly pc relative.
161 intptr_t Cst = 0;
162 if (TM.getRelocationModel() == Reloc::PIC_) {
163 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
164 Cst = -(intptr_t)MovePCtoLROffset - 4;
165 }
166
Chris Lattner79fa3712010-11-15 05:57:53 +0000167 if (MO.isGlobal())
168 return MachineRelocation::getGV(MCE.getCurrentPCOffset(), RelocID,
Chris Lattner65661122010-11-15 06:33:39 +0000169 const_cast<GlobalValue *>(MO.getGlobal()),
170 Cst, isa<Function>(MO.getGlobal()));
Chris Lattner79fa3712010-11-15 05:57:53 +0000171 if (MO.isSymbol())
172 return MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Chris Lattner65661122010-11-15 06:33:39 +0000173 RelocID, MO.getSymbolName(), Cst);
Chris Lattner79fa3712010-11-15 05:57:53 +0000174 if (MO.isCPI())
175 return MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Chris Lattner65661122010-11-15 06:33:39 +0000176 RelocID, MO.getIndex(), Cst);
Chris Lattner79fa3712010-11-15 05:57:53 +0000177
178 if (MO.isMBB())
Chris Lattnerbf9f2f22010-11-15 22:50:50 +0000179 return MachineRelocation::getBB(MCE.getCurrentPCOffset(),
180 RelocID, MO.getMBB());
Chris Lattner79fa3712010-11-15 05:57:53 +0000181
182 assert(MO.isJTI());
183 return MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Chris Lattner65661122010-11-15 06:33:39 +0000184 RelocID, MO.getIndex(), Cst);
Chris Lattner79fa3712010-11-15 05:57:53 +0000185}
186
Chris Lattner0e3461e2010-11-15 06:09:35 +0000187unsigned PPCCodeEmitter::getDirectBrEncoding(const MachineInstr &MI,
188 unsigned OpNo) const {
Chris Lattner79fa3712010-11-15 05:57:53 +0000189 const MachineOperand &MO = MI.getOperand(OpNo);
190 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
191
192 MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bx));
193 return 0;
194}
195
Chris Lattner0e3461e2010-11-15 06:09:35 +0000196unsigned PPCCodeEmitter::getCondBrEncoding(const MachineInstr &MI,
197 unsigned OpNo) const {
198 const MachineOperand &MO = MI.getOperand(OpNo);
199 MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bcx));
200 return 0;
201}
202
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000203unsigned PPCCodeEmitter::getAbsDirectBrEncoding(const MachineInstr &MI,
204 unsigned OpNo) const {
205 const MachineOperand &MO = MI.getOperand(OpNo);
206 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
207
208 llvm_unreachable("Absolute branch relocations unsupported on the old JIT.");
209}
210
211unsigned PPCCodeEmitter::getAbsCondBrEncoding(const MachineInstr &MI,
212 unsigned OpNo) const {
213 llvm_unreachable("Absolute branch relocations unsupported on the old JIT.");
214}
215
Ulrich Weigandfd3ad692013-06-26 13:49:15 +0000216unsigned PPCCodeEmitter::getImm16Encoding(const MachineInstr &MI,
217 unsigned OpNo) const {
Chris Lattner65661122010-11-15 06:33:39 +0000218 const MachineOperand &MO = MI.getOperand(OpNo);
219 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
220
Ulrich Weigand2dbe06a2013-05-17 14:14:12 +0000221 unsigned RelocID;
222 switch (MO.getTargetFlags() & PPCII::MO_ACCESS_MASK) {
223 default: llvm_unreachable("Unsupported target operand flags!");
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000224 case PPCII::MO_LO: RelocID = PPC::reloc_absolute_low; break;
225 case PPCII::MO_HA: RelocID = PPC::reloc_absolute_high; break;
Ulrich Weigand2dbe06a2013-05-17 14:14:12 +0000226 }
Chris Lattner65661122010-11-15 06:33:39 +0000227
Ulrich Weigand2dbe06a2013-05-17 14:14:12 +0000228 MCE.addRelocation(GetRelocation(MO, RelocID));
Chris Lattner65661122010-11-15 06:33:39 +0000229 return 0;
230}
231
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000232unsigned PPCCodeEmitter::getMemRIEncoding(const MachineInstr &MI,
233 unsigned OpNo) const {
234 // Encode (imm, reg) as a memri, which has the low 16-bits as the
235 // displacement and the next 5 bits as the register #.
236 assert(MI.getOperand(OpNo+1).isReg());
237 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1)) << 16;
238
239 const MachineOperand &MO = MI.getOperand(OpNo);
240 if (MO.isImm())
241 return (getMachineOpValue(MI, MO) & 0xFFFF) | RegBits;
242
243 // Add a fixup for the displacement field.
244 MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low));
245 return RegBits;
246}
247
Chris Lattner8f4444d2010-11-15 08:02:41 +0000248unsigned PPCCodeEmitter::getMemRIXEncoding(const MachineInstr &MI,
249 unsigned OpNo) const {
250 // Encode (imm, reg) as a memrix, which has the low 14-bits as the
251 // displacement and the next 5 bits as the register #.
252 assert(MI.getOperand(OpNo+1).isReg());
253 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1)) << 14;
254
Chris Lattner65661122010-11-15 06:33:39 +0000255 const MachineOperand &MO = MI.getOperand(OpNo);
Chris Lattner8f4444d2010-11-15 08:02:41 +0000256 if (MO.isImm())
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000257 return ((getMachineOpValue(MI, MO) >> 2) & 0x3FFF) | RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000258
259 MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low_ix));
Chris Lattner8f4444d2010-11-15 08:02:41 +0000260 return RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000261}
262
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000263
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000264unsigned PPCCodeEmitter::getTLSRegEncoding(const MachineInstr &MI,
265 unsigned OpNo) const {
266 llvm_unreachable("TLS not supported on the old JIT.");
267 return 0;
268}
269
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000270unsigned PPCCodeEmitter::getTLSCallEncoding(const MachineInstr &MI,
271 unsigned OpNo) const {
272 llvm_unreachable("TLS not supported on the old JIT.");
273 return 0;
274}
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000275
Evan Cheng34f3a962008-09-02 06:51:36 +0000276unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
Jim Grosbacha7b6d582010-10-08 00:21:28 +0000277 const MachineOperand &MO) const {
Misha Brukmanb4402432005-04-21 23:30:14 +0000278
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000279 if (MO.isReg()) {
Ulrich Weigand49f487e2013-07-03 17:59:07 +0000280 // MTOCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
Chris Lattner73716a62010-11-16 00:55:51 +0000281 // The GPR operand should come through here though.
Ulrich Weigand49f487e2013-07-03 17:59:07 +0000282 assert((MI.getOpcode() != PPC::MTOCRF && MI.getOpcode() != PPC::MTOCRF8 &&
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000283 MI.getOpcode() != PPC::MFOCRF && MI.getOpcode() != PPC::MFOCRF8) ||
Chris Lattner73716a62010-11-16 00:55:51 +0000284 MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
Hal Finkelfeea6532013-03-26 20:08:20 +0000285 return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000286 }
287
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000288 assert(MO.isImm() &&
289 "Relocation required in an instruction that we cannot encode!");
290 return MO.getImm();
Misha Brukmane05203f2004-06-21 16:55:25 +0000291}
292
Chris Lattner0921e3b2005-10-14 23:37:35 +0000293#include "PPCGenCodeEmitter.inc"