2012-02-18 20:03:15 +08:00
|
|
|
//===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC -----------------===//
|
2005-04-22 07:30:14 +08:00
|
|
|
//
|
2004-06-22 00:55:25 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:30:14 +08:00
|
|
|
//
|
2004-06-22 00:55:25 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2005-04-22 07:30:14 +08:00
|
|
|
//
|
2004-10-14 14:07:25 +08:00
|
|
|
// This file defines the PowerPC 32-bit CodeEmitter and associated machinery to
|
2007-07-06 01:07:56 +08:00
|
|
|
// JIT-compile bitcode to native PowerPC.
|
2004-06-22 00:55:25 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-10-15 07:51:18 +08:00
|
|
|
#include "PPC.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "PPCRelocations.h"
|
|
|
|
#include "PPCTargetMachine.h"
|
2009-05-31 04:51:52 +08:00
|
|
|
#include "llvm/CodeGen/JITCodeEmitter.h"
|
2004-08-10 07:03:59 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2004-10-24 07:47:34 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2008-02-14 02:39:37 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/PassManager.h"
|
2009-07-09 04:53:28 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2006-02-18 08:08:58 +08:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2004-11-16 12:47:33 +08:00
|
|
|
using namespace llvm;
|
2004-06-22 00:55:25 +08:00
|
|
|
|
2004-08-10 07:03:59 +08:00
|
|
|
namespace {
|
2010-02-03 05:55:58 +08:00
|
|
|
class PPCCodeEmitter : public MachineFunctionPass {
|
2004-08-10 07:03:59 +08:00
|
|
|
TargetMachine &TM;
|
2010-02-03 05:55:58 +08:00
|
|
|
JITCodeEmitter &MCE;
|
2010-03-14 09:41:15 +08:00
|
|
|
MachineModuleInfo *MMI;
|
2010-02-03 05:55:58 +08: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-31 04:51:52 +08:00
|
|
|
public:
|
2010-02-03 05:55:58 +08:00
|
|
|
|
|
|
|
PPCCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
|
2010-08-07 02:33:48 +08:00
|
|
|
: MachineFunctionPass(ID), TM(tm), MCE(mce) {}
|
2009-05-31 04:51:52 +08:00
|
|
|
|
|
|
|
/// getBinaryCodeForInstr - This function, generated by the
|
|
|
|
/// CodeEmitterGenerator using TableGen, produces the binary encoding for
|
|
|
|
/// machine instructions.
|
2012-01-25 02:37:29 +08:00
|
|
|
uint64_t getBinaryCodeForInstr(const MachineInstr &MI) const;
|
2009-05-31 04:51:52 +08:00
|
|
|
|
2010-11-15 13:57:53 +08:00
|
|
|
|
|
|
|
MachineRelocation GetRelocation(const MachineOperand &MO,
|
|
|
|
unsigned RelocID) const;
|
|
|
|
|
2009-05-31 04:51:52 +08:00
|
|
|
/// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
|
2009-06-02 03:57:37 +08:00
|
|
|
unsigned getMachineOpValue(const MachineInstr &MI,
|
2010-10-08 08:21:28 +08:00
|
|
|
const MachineOperand &MO) const;
|
2004-08-10 07:03:59 +08:00
|
|
|
|
2010-11-15 13:19:25 +08:00
|
|
|
unsigned get_crbitm_encoding(const MachineInstr &MI, unsigned OpNo) const;
|
2010-11-15 14:09:35 +08:00
|
|
|
unsigned getDirectBrEncoding(const MachineInstr &MI, unsigned OpNo) const;
|
|
|
|
unsigned getCondBrEncoding(const MachineInstr &MI, unsigned OpNo) const;
|
2010-11-15 14:33:39 +08:00
|
|
|
|
|
|
|
unsigned getHA16Encoding(const MachineInstr &MI, unsigned OpNo) const;
|
|
|
|
unsigned getLO16Encoding(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.
llvm-svn: 119134
2010-11-15 16:22:03 +08:00
|
|
|
unsigned getMemRIEncoding(const MachineInstr &MI, unsigned OpNo) const;
|
2010-11-15 16:02:41 +08:00
|
|
|
unsigned getMemRIXEncoding(const MachineInstr &MI, unsigned OpNo) const;
|
2012-12-05 00:18:08 +08:00
|
|
|
unsigned getTLSRegEncoding(const MachineInstr &MI, unsigned OpNo) const;
|
2010-11-15 14:33:39 +08:00
|
|
|
|
2004-08-10 07:03:59 +08: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-31 04:51:52 +08:00
|
|
|
}
|
2009-09-20 10:20:51 +08:00
|
|
|
|
2010-02-03 05:55:58 +08:00
|
|
|
char PPCCodeEmitter::ID = 0;
|
|
|
|
|
2006-08-24 05:08:52 +08:00
|
|
|
/// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
|
|
|
|
/// to the specified MCE object.
|
2009-05-31 04:51:52 +08:00
|
|
|
FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
|
2009-06-02 03:57:37 +08:00
|
|
|
JITCodeEmitter &JCE) {
|
2010-02-03 05:55:58 +08:00
|
|
|
return new PPCCodeEmitter(TM, JCE);
|
2004-08-10 07:03:59 +08:00
|
|
|
}
|
|
|
|
|
2010-02-03 05:55:58 +08:00
|
|
|
bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
|
2006-02-23 04:19:42 +08:00
|
|
|
assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
|
|
|
|
MF.getTarget().getRelocationModel() != Reloc::Static) &&
|
|
|
|
"JIT relocation model must be set to static or default!");
|
2008-02-14 02:39:37 +08:00
|
|
|
|
2010-03-14 09:41:15 +08:00
|
|
|
MMI = &getAnalysis<MachineModuleInfo>();
|
|
|
|
MCE.setModuleInfo(MMI);
|
2006-05-03 02:27:26 +08:00
|
|
|
do {
|
2006-12-08 12:54:03 +08:00
|
|
|
MovePCtoLROffset = 0;
|
2006-05-03 02:27:26 +08: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 09:42:02 +08:00
|
|
|
|
2004-08-10 07:03:59 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-03 05:55:58 +08:00
|
|
|
void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
|
2006-05-04 01:10:41 +08:00
|
|
|
MCE.StartMachineBasicBlock(&MBB);
|
2009-09-20 10:20:51 +08:00
|
|
|
|
2004-10-24 02:28:01 +08:00
|
|
|
for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
|
2008-09-02 14:51:36 +08:00
|
|
|
const MachineInstr &MI = *I;
|
2009-10-06 10:19:11 +08:00
|
|
|
MCE.processDebugLoc(MI.getDebugLoc(), true);
|
2004-11-23 13:59:53 +08:00
|
|
|
switch (MI.getOpcode()) {
|
|
|
|
default:
|
2008-09-02 14:51:36 +08:00
|
|
|
MCE.emitWordBE(getBinaryCodeForInstr(MI));
|
2004-11-23 13:59:53 +08:00
|
|
|
break;
|
2010-07-17 06:20:36 +08:00
|
|
|
case TargetOpcode::PROLOG_LABEL:
|
2010-03-14 10:33:54 +08:00
|
|
|
case TargetOpcode::EH_LABEL:
|
|
|
|
MCE.emitLabel(MI.getOperand(0).getMCSymbol());
|
|
|
|
break;
|
2010-02-10 03:54:29 +08:00
|
|
|
case TargetOpcode::IMPLICIT_DEF:
|
|
|
|
case TargetOpcode::KILL:
|
2008-03-17 14:56:52 +08:00
|
|
|
break; // pseudo opcode, no side effects
|
2004-11-23 13:59:53 +08:00
|
|
|
case PPC::MovePCtoLR:
|
2006-11-15 02:44:47 +08:00
|
|
|
case PPC::MovePCtoLR8:
|
2006-12-08 12:54:03 +08:00
|
|
|
assert(TM.getRelocationModel() == Reloc::PIC_);
|
|
|
|
MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
|
|
|
|
MCE.emitWordBE(0x48000005); // bl 1
|
2004-11-23 13:59:53 +08:00
|
|
|
break;
|
|
|
|
}
|
2009-10-06 10:19:11 +08:00
|
|
|
MCE.processDebugLoc(MI.getDebugLoc(), false);
|
2004-10-24 02:28:01 +08:00
|
|
|
}
|
2004-08-10 07:03:59 +08:00
|
|
|
}
|
|
|
|
|
2010-11-15 13:19:25 +08:00
|
|
|
unsigned PPCCodeEmitter::get_crbitm_encoding(const MachineInstr &MI,
|
|
|
|
unsigned OpNo) const {
|
|
|
|
const MachineOperand &MO = MI.getOperand(OpNo);
|
2011-12-07 14:34:06 +08:00
|
|
|
assert((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MTCRF8 ||
|
|
|
|
MI.getOpcode() == PPC::MFOCRF) &&
|
2010-11-15 13:19:25 +08:00
|
|
|
(MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
|
2011-07-26 03:53:23 +08:00
|
|
|
return 0x80 >> getPPCRegisterNumbering(MO.getReg());
|
2010-11-15 13:19:25 +08:00
|
|
|
}
|
|
|
|
|
2010-11-15 13:57:53 +08:00
|
|
|
MachineRelocation PPCCodeEmitter::GetRelocation(const MachineOperand &MO,
|
|
|
|
unsigned RelocID) const {
|
2010-11-15 14:33:39 +08: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 13:57:53 +08:00
|
|
|
if (MO.isGlobal())
|
|
|
|
return MachineRelocation::getGV(MCE.getCurrentPCOffset(), RelocID,
|
2010-11-15 14:33:39 +08:00
|
|
|
const_cast<GlobalValue *>(MO.getGlobal()),
|
|
|
|
Cst, isa<Function>(MO.getGlobal()));
|
2010-11-15 13:57:53 +08:00
|
|
|
if (MO.isSymbol())
|
|
|
|
return MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
|
2010-11-15 14:33:39 +08:00
|
|
|
RelocID, MO.getSymbolName(), Cst);
|
2010-11-15 13:57:53 +08:00
|
|
|
if (MO.isCPI())
|
|
|
|
return MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
|
2010-11-15 14:33:39 +08:00
|
|
|
RelocID, MO.getIndex(), Cst);
|
2010-11-15 13:57:53 +08:00
|
|
|
|
|
|
|
if (MO.isMBB())
|
2010-11-16 06:50:50 +08:00
|
|
|
return MachineRelocation::getBB(MCE.getCurrentPCOffset(),
|
|
|
|
RelocID, MO.getMBB());
|
2010-11-15 13:57:53 +08:00
|
|
|
|
|
|
|
assert(MO.isJTI());
|
|
|
|
return MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
|
2010-11-15 14:33:39 +08:00
|
|
|
RelocID, MO.getIndex(), Cst);
|
2010-11-15 13:57:53 +08:00
|
|
|
}
|
|
|
|
|
2010-11-15 14:09:35 +08:00
|
|
|
unsigned PPCCodeEmitter::getDirectBrEncoding(const MachineInstr &MI,
|
|
|
|
unsigned OpNo) const {
|
2010-11-15 13:57:53 +08: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 14:09:35 +08: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;
|
|
|
|
}
|
|
|
|
|
2010-11-15 14:33:39 +08:00
|
|
|
unsigned PPCCodeEmitter::getHA16Encoding(const MachineInstr &MI,
|
|
|
|
unsigned OpNo) const {
|
|
|
|
const MachineOperand &MO = MI.getOperand(OpNo);
|
|
|
|
if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
|
|
|
|
|
|
|
|
MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_high));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned PPCCodeEmitter::getLO16Encoding(const MachineInstr &MI,
|
|
|
|
unsigned OpNo) const {
|
|
|
|
const MachineOperand &MO = MI.getOperand(OpNo);
|
|
|
|
if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
|
|
|
|
|
|
|
|
MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low));
|
|
|
|
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.
llvm-svn: 119134
2010-11-15 16:22:03 +08: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 16:02:41 +08: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 14:33:39 +08:00
|
|
|
const MachineOperand &MO = MI.getOperand(OpNo);
|
2010-11-15 16:02:41 +08:00
|
|
|
if (MO.isImm())
|
|
|
|
return (getMachineOpValue(MI, MO) & 0x3FFF) | RegBits;
|
2010-11-15 14:33:39 +08:00
|
|
|
|
|
|
|
MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low_ix));
|
2010-11-15 16:02:41 +08:00
|
|
|
return RegBits;
|
2010-11-15 14:33:39 +08:00
|
|
|
}
|
|
|
|
|
2010-11-15 13:19:25 +08:00
|
|
|
|
2012-12-05 00:18:08 +08:00
|
|
|
unsigned PPCCodeEmitter::getTLSRegEncoding(const MachineInstr &MI,
|
|
|
|
unsigned OpNo) const {
|
|
|
|
llvm_unreachable("TLS not supported on the old JIT.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-02 14:51:36 +08:00
|
|
|
unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
|
2010-10-08 08:21:28 +08:00
|
|
|
const MachineOperand &MO) const {
|
2005-04-22 07:30:14 +08:00
|
|
|
|
2008-10-03 23:45:36 +08:00
|
|
|
if (MO.isReg()) {
|
2010-11-16 08:55:51 +08:00
|
|
|
// MTCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
|
|
|
|
// The GPR operand should come through here though.
|
2011-12-07 14:34:06 +08:00
|
|
|
assert((MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MTCRF8 &&
|
|
|
|
MI.getOpcode() != PPC::MFOCRF) ||
|
2010-11-16 08:55:51 +08:00
|
|
|
MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
|
2011-07-26 03:53:23 +08:00
|
|
|
return getPPCRegisterNumbering(MO.getReg());
|
2010-11-15 13:19:25 +08: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.
llvm-svn: 119134
2010-11-15 16:22:03 +08:00
|
|
|
assert(MO.isImm() &&
|
|
|
|
"Relocation required in an instruction that we cannot encode!");
|
|
|
|
return MO.getImm();
|
2004-06-22 00:55:25 +08:00
|
|
|
}
|
|
|
|
|
2005-10-15 07:37:35 +08:00
|
|
|
#include "PPCGenCodeEmitter.inc"
|