2012-02-18 12:03:15 +00:00
|
|
|
//===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC -----------------===//
|
2005-04-21 23:30:14 +00:00
|
|
|
//
|
2004-06-21 16:55:25 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:30:14 +00:00
|
|
|
//
|
2004-06-21 16:55:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2005-04-21 23:30:14 +00:00
|
|
|
//
|
2004-10-14 06:07:25 +00:00
|
|
|
// This file defines the PowerPC 32-bit CodeEmitter and associated machinery to
|
2007-07-05 17:07:56 +00:00
|
|
|
// JIT-compile bitcode to native PowerPC.
|
2004-06-21 16:55:25 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-10-14 23:51:18 +00:00
|
|
|
#include "PPC.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "PPCRelocations.h"
|
|
|
|
#include "PPCTargetMachine.h"
|
2009-05-30 20:51:52 +00:00
|
|
|
#include "llvm/CodeGen/JITCodeEmitter.h"
|
2004-08-09 23:03:59 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2004-10-23 23:47:34 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2008-02-13 18:39:37 +00:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/PassManager.h"
|
2009-07-08 20:53:28 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2006-02-18 00:08:58 +00:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2004-11-16 04:47:33 +00:00
|
|
|
using namespace llvm;
|
2004-06-21 16:55:25 +00:00
|
|
|
|
2004-08-09 23:03:59 +00:00
|
|
|
namespace {
|
2010-02-02 21:55:58 +00:00
|
|
|
class PPCCodeEmitter : public MachineFunctionPass {
|
2004-08-09 23:03:59 +00:00
|
|
|
TargetMachine &TM;
|
2010-02-02 21:55:58 +00:00
|
|
|
JITCodeEmitter &MCE;
|
2010-03-14 01:41:15 +00:00
|
|
|
MachineModuleInfo *MMI;
|
2010-02-02 21:55:58 +00:00
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<MachineModuleInfo>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
/// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
|
|
|
|
/// its address in the function into this pointer.
|
|
|
|
void *MovePCtoLROffset;
|
2009-05-30 20:51:52 +00:00
|
|
|
public:
|
2010-02-02 21:55:58 +00:00
|
|
|
|
|
|
|
PPCCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
|
2010-08-06 18:33:48 +00:00
|
|
|
: MachineFunctionPass(ID), TM(tm), MCE(mce) {}
|
2009-05-30 20:51:52 +00:00
|
|
|
|
|
|
|
/// getBinaryCodeForInstr - This function, generated by the
|
|
|
|
/// CodeEmitterGenerator using TableGen, produces the binary encoding for
|
|
|
|
/// machine instructions.
|
2012-01-24 18:37:29 +00:00
|
|
|
uint64_t getBinaryCodeForInstr(const MachineInstr &MI) const;
|
2009-05-30 20:51:52 +00:00
|
|
|
|
2010-11-15 05:57:53 +00:00
|
|
|
|
|
|
|
MachineRelocation GetRelocation(const MachineOperand &MO,
|
|
|
|
unsigned RelocID) const;
|
|
|
|
|
2009-05-30 20:51:52 +00:00
|
|
|
/// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
|
2009-06-01 19:57:37 +00:00
|
|
|
unsigned getMachineOpValue(const MachineInstr &MI,
|
2010-10-08 00:21:28 +00:00
|
|
|
const MachineOperand &MO) const;
|
2004-08-09 23:03:59 +00:00
|
|
|
|
2010-11-15 05:19:25 +00:00
|
|
|
unsigned get_crbitm_encoding(const MachineInstr &MI, unsigned OpNo) const;
|
2010-11-15 06:09:35 +00:00
|
|
|
unsigned getDirectBrEncoding(const MachineInstr &MI, unsigned OpNo) const;
|
|
|
|
unsigned getCondBrEncoding(const MachineInstr &MI, unsigned OpNo) const;
|
2010-11-15 06:33:39 +00:00
|
|
|
|
2013-05-17 14:14:12 +00:00
|
|
|
unsigned getS16ImmEncoding(const MachineInstr &MI, unsigned OpNo) const;
|
split out an encoder for memri operands, allowing a relocation to be plopped
into the immediate field. This allows us to encode stuff like this:
lbz r3, lo16(__ZL4init)(r4) ; globalopt.cpp:5
; encoding: [0x88,0x64,A,A]
; fixup A - offset: 0, value: lo16(__ZL4init), kind: fixup_ppc_lo16
stw r3, lo16(__ZL1s)(r5) ; globalopt.cpp:6
; encoding: [0x90,0x65,A,A]
; fixup A - offset: 0, value: lo16(__ZL1s), kind: fixup_ppc_lo16
With this, we should have a completely function MCCodeEmitter for PPC, wewt.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@119134 91177308-0d34-0410-b5e6-96231b3b80d8
2010-11-15 08:22:03 +00:00
|
|
|
unsigned getMemRIEncoding(const MachineInstr &MI, unsigned OpNo) const;
|
2010-11-15 08:02:41 +00:00
|
|
|
unsigned getMemRIXEncoding(const MachineInstr &MI, unsigned OpNo) const;
|
2012-12-04 16:18:08 +00:00
|
|
|
unsigned getTLSRegEncoding(const MachineInstr &MI, unsigned OpNo) const;
|
2010-11-15 06:33:39 +00:00
|
|
|
|
2004-08-09 23:03:59 +00:00
|
|
|
const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
|
|
|
|
|
|
|
|
/// runOnMachineFunction - emits the given MachineFunction to memory
|
|
|
|
///
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF);
|
|
|
|
|
|
|
|
/// emitBasicBlock - emits the given MachineBasicBlock to memory
|
|
|
|
///
|
|
|
|
void emitBasicBlock(MachineBasicBlock &MBB);
|
|
|
|
};
|
2009-05-30 20:51:52 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2010-02-02 21:55:58 +00:00
|
|
|
char PPCCodeEmitter::ID = 0;
|
|
|
|
|
2006-08-23 21:08:52 +00:00
|
|
|
/// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
|
|
|
|
/// to the specified MCE object.
|
2009-05-30 20:51:52 +00:00
|
|
|
FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
|
2009-06-01 19:57:37 +00:00
|
|
|
JITCodeEmitter &JCE) {
|
2010-02-02 21:55:58 +00:00
|
|
|
return new PPCCodeEmitter(TM, JCE);
|
2004-08-09 23:03:59 +00:00
|
|
|
}
|
|
|
|
|
2010-02-02 21:55:58 +00:00
|
|
|
bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
|
2006-02-22 20:19:42 +00:00
|
|
|
assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
|
|
|
|
MF.getTarget().getRelocationModel() != Reloc::Static) &&
|
|
|
|
"JIT relocation model must be set to static or default!");
|
2008-02-13 18:39:37 +00:00
|
|
|
|
2010-03-14 01:41:15 +00:00
|
|
|
MMI = &getAnalysis<MachineModuleInfo>();
|
|
|
|
MCE.setModuleInfo(MMI);
|
2006-05-02 18:27:26 +00:00
|
|
|
do {
|
2006-12-08 04:54:03 +00:00
|
|
|
MovePCtoLROffset = 0;
|
2006-05-02 18:27:26 +00:00
|
|
|
MCE.startFunction(MF);
|
|
|
|
for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
|
|
|
|
emitBasicBlock(*BB);
|
|
|
|
} while (MCE.finishFunction(MF));
|
2004-10-21 01:42:02 +00:00
|
|
|
|
2004-08-09 23:03:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-02 21:55:58 +00:00
|
|
|
void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
|
2006-05-03 17:10:41 +00:00
|
|
|
MCE.StartMachineBasicBlock(&MBB);
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2004-10-23 18:28:01 +00:00
|
|
|
for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
|
2008-09-02 06:51:36 +00:00
|
|
|
const MachineInstr &MI = *I;
|
2009-10-06 02:19:11 +00:00
|
|
|
MCE.processDebugLoc(MI.getDebugLoc(), true);
|
2004-11-23 05:59:53 +00:00
|
|
|
switch (MI.getOpcode()) {
|
|
|
|
default:
|
2008-09-02 06:51:36 +00:00
|
|
|
MCE.emitWordBE(getBinaryCodeForInstr(MI));
|
2004-11-23 05:59:53 +00:00
|
|
|
break;
|
2010-07-16 22:20:36 +00:00
|
|
|
case TargetOpcode::PROLOG_LABEL:
|
2010-03-14 02:33:54 +00:00
|
|
|
case TargetOpcode::EH_LABEL:
|
|
|
|
MCE.emitLabel(MI.getOperand(0).getMCSymbol());
|
|
|
|
break;
|
2010-02-09 19:54:29 +00:00
|
|
|
case TargetOpcode::IMPLICIT_DEF:
|
|
|
|
case TargetOpcode::KILL:
|
2008-03-17 06:56:52 +00:00
|
|
|
break; // pseudo opcode, no side effects
|
2004-11-23 05:59:53 +00:00
|
|
|
case PPC::MovePCtoLR:
|
2006-11-14 18:44:47 +00:00
|
|
|
case PPC::MovePCtoLR8:
|
2006-12-08 04:54:03 +00:00
|
|
|
assert(TM.getRelocationModel() == Reloc::PIC_);
|
|
|
|
MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
|
|
|
|
MCE.emitWordBE(0x48000005); // bl 1
|
2004-11-23 05:59:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-10-06 02:19:11 +00:00
|
|
|
MCE.processDebugLoc(MI.getDebugLoc(), false);
|
2004-10-23 18:28:01 +00:00
|
|
|
}
|
2004-08-09 23:03:59 +00:00
|
|
|
}
|
|
|
|
|
2010-11-15 05:19:25 +00:00
|
|
|
unsigned PPCCodeEmitter::get_crbitm_encoding(const MachineInstr &MI,
|
|
|
|
unsigned OpNo) const {
|
|
|
|
const MachineOperand &MO = MI.getOperand(OpNo);
|
2011-12-07 06:34:06 +00:00
|
|
|
assert((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MTCRF8 ||
|
|
|
|
MI.getOpcode() == PPC::MFOCRF) &&
|
2010-11-15 05:19:25 +00:00
|
|
|
(MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
|
2013-03-26 20:08:20 +00:00
|
|
|
return 0x80 >> TM.getRegisterInfo()->getEncodingValue(MO.getReg());
|
2010-11-15 05:19:25 +00:00
|
|
|
}
|
|
|
|
|
2010-11-15 05:57:53 +00:00
|
|
|
MachineRelocation PPCCodeEmitter::GetRelocation(const MachineOperand &MO,
|
|
|
|
unsigned RelocID) const {
|
2010-11-15 06:33:39 +00:00
|
|
|
// If in PIC mode, we need to encode the negated address of the
|
|
|
|
// 'movepctolr' into the unrelocated field. After relocation, we'll have
|
|
|
|
// &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm
|
|
|
|
// field, we get &gv. This doesn't happen for branch relocations, which are
|
|
|
|
// always implicitly pc relative.
|
|
|
|
intptr_t Cst = 0;
|
|
|
|
if (TM.getRelocationModel() == Reloc::PIC_) {
|
|
|
|
assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
|
|
|
|
Cst = -(intptr_t)MovePCtoLROffset - 4;
|
|
|
|
}
|
|
|
|
|
2010-11-15 05:57:53 +00:00
|
|
|
if (MO.isGlobal())
|
|
|
|
return MachineRelocation::getGV(MCE.getCurrentPCOffset(), RelocID,
|
2010-11-15 06:33:39 +00:00
|
|
|
const_cast<GlobalValue *>(MO.getGlobal()),
|
|
|
|
Cst, isa<Function>(MO.getGlobal()));
|
2010-11-15 05:57:53 +00:00
|
|
|
if (MO.isSymbol())
|
|
|
|
return MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
|
2010-11-15 06:33:39 +00:00
|
|
|
RelocID, MO.getSymbolName(), Cst);
|
2010-11-15 05:57:53 +00:00
|
|
|
if (MO.isCPI())
|
|
|
|
return MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
|
2010-11-15 06:33:39 +00:00
|
|
|
RelocID, MO.getIndex(), Cst);
|
2010-11-15 05:57:53 +00:00
|
|
|
|
|
|
|
if (MO.isMBB())
|
2010-11-15 22:50:50 +00:00
|
|
|
return MachineRelocation::getBB(MCE.getCurrentPCOffset(),
|
|
|
|
RelocID, MO.getMBB());
|
2010-11-15 05:57:53 +00:00
|
|
|
|
|
|
|
assert(MO.isJTI());
|
|
|
|
return MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
|
2010-11-15 06:33:39 +00:00
|
|
|
RelocID, MO.getIndex(), Cst);
|
2010-11-15 05:57:53 +00:00
|
|
|
}
|
|
|
|
|
2010-11-15 06:09:35 +00:00
|
|
|
unsigned PPCCodeEmitter::getDirectBrEncoding(const MachineInstr &MI,
|
|
|
|
unsigned OpNo) const {
|
2010-11-15 05:57:53 +00:00
|
|
|
const MachineOperand &MO = MI.getOperand(OpNo);
|
|
|
|
if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
|
|
|
|
|
|
|
|
MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bx));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-15 06:09:35 +00:00
|
|
|
unsigned PPCCodeEmitter::getCondBrEncoding(const MachineInstr &MI,
|
|
|
|
unsigned OpNo) const {
|
|
|
|
const MachineOperand &MO = MI.getOperand(OpNo);
|
|
|
|
MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bcx));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-17 14:14:12 +00:00
|
|
|
unsigned PPCCodeEmitter::getS16ImmEncoding(const MachineInstr &MI,
|
|
|
|
unsigned OpNo) const {
|
2010-11-15 06:33:39 +00:00
|
|
|
const MachineOperand &MO = MI.getOperand(OpNo);
|
|
|
|
if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
|
|
|
|
|
2013-05-17 14:14:12 +00:00
|
|
|
unsigned RelocID;
|
|
|
|
switch (MO.getTargetFlags() & PPCII::MO_ACCESS_MASK) {
|
|
|
|
default: llvm_unreachable("Unsupported target operand flags!");
|
2013-06-21 14:42:20 +00:00
|
|
|
case PPCII::MO_LO: RelocID = PPC::reloc_absolute_low; break;
|
|
|
|
case PPCII::MO_HA: RelocID = PPC::reloc_absolute_high; break;
|
2013-05-17 14:14:12 +00:00
|
|
|
}
|
2010-11-15 06:33:39 +00:00
|
|
|
|
2013-05-17 14:14:12 +00:00
|
|
|
MCE.addRelocation(GetRelocation(MO, RelocID));
|
2010-11-15 06:33:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
split out an encoder for memri operands, allowing a relocation to be plopped
into the immediate field. This allows us to encode stuff like this:
lbz r3, lo16(__ZL4init)(r4) ; globalopt.cpp:5
; encoding: [0x88,0x64,A,A]
; fixup A - offset: 0, value: lo16(__ZL4init), kind: fixup_ppc_lo16
stw r3, lo16(__ZL1s)(r5) ; globalopt.cpp:6
; encoding: [0x90,0x65,A,A]
; fixup A - offset: 0, value: lo16(__ZL1s), kind: fixup_ppc_lo16
With this, we should have a completely function MCCodeEmitter for PPC, wewt.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@119134 91177308-0d34-0410-b5e6-96231b3b80d8
2010-11-15 08:22:03 +00:00
|
|
|
unsigned PPCCodeEmitter::getMemRIEncoding(const MachineInstr &MI,
|
|
|
|
unsigned OpNo) const {
|
|
|
|
// Encode (imm, reg) as a memri, which has the low 16-bits as the
|
|
|
|
// displacement and the next 5 bits as the register #.
|
|
|
|
assert(MI.getOperand(OpNo+1).isReg());
|
|
|
|
unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1)) << 16;
|
|
|
|
|
|
|
|
const MachineOperand &MO = MI.getOperand(OpNo);
|
|
|
|
if (MO.isImm())
|
|
|
|
return (getMachineOpValue(MI, MO) & 0xFFFF) | RegBits;
|
|
|
|
|
|
|
|
// Add a fixup for the displacement field.
|
|
|
|
MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low));
|
|
|
|
return RegBits;
|
|
|
|
}
|
|
|
|
|
2010-11-15 08:02:41 +00:00
|
|
|
unsigned PPCCodeEmitter::getMemRIXEncoding(const MachineInstr &MI,
|
|
|
|
unsigned OpNo) const {
|
|
|
|
// Encode (imm, reg) as a memrix, which has the low 14-bits as the
|
|
|
|
// displacement and the next 5 bits as the register #.
|
|
|
|
assert(MI.getOperand(OpNo+1).isReg());
|
|
|
|
unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1)) << 14;
|
|
|
|
|
2010-11-15 06:33:39 +00:00
|
|
|
const MachineOperand &MO = MI.getOperand(OpNo);
|
2010-11-15 08:02:41 +00:00
|
|
|
if (MO.isImm())
|
2013-05-16 17:58:02 +00:00
|
|
|
return ((getMachineOpValue(MI, MO) >> 2) & 0x3FFF) | RegBits;
|
2010-11-15 06:33:39 +00:00
|
|
|
|
|
|
|
MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low_ix));
|
2010-11-15 08:02:41 +00:00
|
|
|
return RegBits;
|
2010-11-15 06:33:39 +00:00
|
|
|
}
|
|
|
|
|
2010-11-15 05:19:25 +00:00
|
|
|
|
2012-12-04 16:18:08 +00:00
|
|
|
unsigned PPCCodeEmitter::getTLSRegEncoding(const MachineInstr &MI,
|
|
|
|
unsigned OpNo) const {
|
|
|
|
llvm_unreachable("TLS not supported on the old JIT.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-02 06:51:36 +00:00
|
|
|
unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
|
2010-10-08 00:21:28 +00:00
|
|
|
const MachineOperand &MO) const {
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2008-10-03 15:45:36 +00:00
|
|
|
if (MO.isReg()) {
|
2010-11-16 00:55:51 +00:00
|
|
|
// MTCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
|
|
|
|
// The GPR operand should come through here though.
|
2011-12-07 06:34:06 +00:00
|
|
|
assert((MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MTCRF8 &&
|
|
|
|
MI.getOpcode() != PPC::MFOCRF) ||
|
2010-11-16 00:55:51 +00:00
|
|
|
MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
|
2013-03-26 20:08:20 +00:00
|
|
|
return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
|
2010-11-15 05:19:25 +00:00
|
|
|
}
|
|
|
|
|
split out an encoder for memri operands, allowing a relocation to be plopped
into the immediate field. This allows us to encode stuff like this:
lbz r3, lo16(__ZL4init)(r4) ; globalopt.cpp:5
; encoding: [0x88,0x64,A,A]
; fixup A - offset: 0, value: lo16(__ZL4init), kind: fixup_ppc_lo16
stw r3, lo16(__ZL1s)(r5) ; globalopt.cpp:6
; encoding: [0x90,0x65,A,A]
; fixup A - offset: 0, value: lo16(__ZL1s), kind: fixup_ppc_lo16
With this, we should have a completely function MCCodeEmitter for PPC, wewt.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@119134 91177308-0d34-0410-b5e6-96231b3b80d8
2010-11-15 08:22:03 +00:00
|
|
|
assert(MO.isImm() &&
|
|
|
|
"Relocation required in an instruction that we cannot encode!");
|
|
|
|
return MO.getImm();
|
2004-06-21 16:55:25 +00:00
|
|
|
}
|
|
|
|
|
2005-10-14 23:37:35 +00:00
|
|
|
#include "PPCGenCodeEmitter.inc"
|