Give the X86 JIT the ability to encode global+disp constants. Patch

contributed by Jeff Cohen!

llvm-svn: 17010
This commit is contained in:
Chris Lattner 2004-10-15 04:53:13 +00:00
parent 19025d5ad0
commit 3b78938b9e
1 changed files with 54 additions and 27 deletions

View File

@ -193,7 +193,7 @@ namespace {
void emitPCRelativeBlockAddress(const MachineBasicBlock *BB); void emitPCRelativeBlockAddress(const MachineBasicBlock *BB);
void emitMaybePCRelativeValue(unsigned Address, bool isPCRelative); void emitMaybePCRelativeValue(unsigned Address, bool isPCRelative);
void emitGlobalAddressForCall(GlobalValue *GV); void emitGlobalAddressForCall(GlobalValue *GV);
void emitGlobalAddressForPtr(GlobalValue *GV); void emitGlobalAddressForPtr(GlobalValue *GV, int Disp = 0);
void emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeField); void emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeField);
void emitSIBByte(unsigned SS, unsigned Index, unsigned Base); void emitSIBByte(unsigned SS, unsigned Index, unsigned Base);
@ -294,7 +294,7 @@ void Emitter::emitGlobalAddressForCall(GlobalValue *GV) {
/// this is part of a "take the address of a global" instruction, which is not /// this is part of a "take the address of a global" instruction, which is not
/// PC relative. /// PC relative.
/// ///
void Emitter::emitGlobalAddressForPtr(GlobalValue *GV) { void Emitter::emitGlobalAddressForPtr(GlobalValue *GV, int Disp /* = 0 */) {
// Get the address from the backend... // Get the address from the backend...
unsigned Address = MCE.getGlobalValueAddress(GV); unsigned Address = MCE.getGlobalValueAddress(GV);
@ -306,7 +306,7 @@ void Emitter::emitGlobalAddressForPtr(GlobalValue *GV) {
Address = getResolver(MCE).getLazyResolver((Function*)GV); Address = getResolver(MCE).getLazyResolver((Function*)GV);
} }
emitMaybePCRelativeValue(Address, false); emitMaybePCRelativeValue(Address + Disp, false);
} }
@ -374,14 +374,25 @@ static bool isDisp8(int Value) {
void Emitter::emitMemModRMByte(const MachineInstr &MI, void Emitter::emitMemModRMByte(const MachineInstr &MI,
unsigned Op, unsigned RegOpcodeField) { unsigned Op, unsigned RegOpcodeField) {
const MachineOperand &Disp = MI.getOperand(Op+3); const MachineOperand &Op3 = MI.getOperand(Op+3);
GlobalValue *GV = 0;
int DispVal = 0;
if (Op3.isGlobalAddress()) {
GV = Op3.getGlobal();
DispVal = Op3.getOffset();
} else {
DispVal = Op3.getImmedValue();
}
if (MI.getOperand(Op).isConstantPoolIndex()) { if (MI.getOperand(Op).isConstantPoolIndex()) {
// Emit a direct address reference [disp32] where the displacement of the // Emit a direct address reference [disp32] where the displacement of the
// constant pool entry is controlled by the MCE. // constant pool entry is controlled by the MCE.
assert(!GV && "Constant Pool reference cannot be relative to global!");
MCE.emitByte(ModRMByte(0, RegOpcodeField, 5)); MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
unsigned Index = MI.getOperand(Op).getConstantPoolIndex(); unsigned Index = MI.getOperand(Op).getConstantPoolIndex();
unsigned Address = MCE.getConstantPoolEntryAddress(Index); unsigned Address = MCE.getConstantPoolEntryAddress(Index);
MCE.emitWord(Address+Disp.getImmedValue()); MCE.emitWord(Address+DispVal);
return; return;
} }
@ -394,20 +405,27 @@ void Emitter::emitMemModRMByte(const MachineInstr &MI,
if (BaseReg.getReg() == 0) { // Just a displacement? if (BaseReg.getReg() == 0) { // Just a displacement?
// Emit special case [disp32] encoding // Emit special case [disp32] encoding
MCE.emitByte(ModRMByte(0, RegOpcodeField, 5)); MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
emitConstant(Disp.getImmedValue(), 4); if (GV)
emitGlobalAddressForPtr(GV, DispVal);
else
emitConstant(DispVal, 4);
} else { } else {
unsigned BaseRegNo = getX86RegNum(BaseReg.getReg()); unsigned BaseRegNo = getX86RegNum(BaseReg.getReg());
if (Disp.getImmedValue() == 0 && BaseRegNo != N86::EBP) { if (GV) {
// Emit the most general non-SIB encoding: [REG+disp32]
MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
emitGlobalAddressForPtr(GV, DispVal);
} else if (DispVal == 0 && BaseRegNo != N86::EBP) {
// Emit simple indirect register encoding... [EAX] f.e. // Emit simple indirect register encoding... [EAX] f.e.
MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo)); MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
} else if (isDisp8(Disp.getImmedValue())) { } else if (isDisp8(DispVal)) {
// Emit the disp8 encoding... [REG+disp8] // Emit the disp8 encoding... [REG+disp8]
MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo)); MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
emitConstant(Disp.getImmedValue(), 1); emitConstant(DispVal, 1);
} else { } else {
// Emit the most general non-SIB encoding: [REG+disp32] // Emit the most general non-SIB encoding: [REG+disp32]
MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo)); MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
emitConstant(Disp.getImmedValue(), 4); emitConstant(DispVal, 4);
} }
} }
@ -421,10 +439,14 @@ void Emitter::emitMemModRMByte(const MachineInstr &MI,
// MOD=0, BASE=5, to JUST get the index, scale, and displacement. // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
MCE.emitByte(ModRMByte(0, RegOpcodeField, 4)); MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
ForceDisp32 = true; ForceDisp32 = true;
} else if (Disp.getImmedValue() == 0 && BaseReg.getReg() != X86::EBP) { } else if (GV) {
// Emit the normal disp32 encoding...
MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
ForceDisp32 = true;
} else if (DispVal == 0 && BaseReg.getReg() != X86::EBP) {
// Emit no displacement ModR/M byte // Emit no displacement ModR/M byte
MCE.emitByte(ModRMByte(0, RegOpcodeField, 4)); MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
} else if (isDisp8(Disp.getImmedValue())) { } else if (isDisp8(DispVal)) {
// Emit the disp8 encoding... // Emit the disp8 encoding...
MCE.emitByte(ModRMByte(1, RegOpcodeField, 4)); MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
@ -446,18 +468,20 @@ void Emitter::emitMemModRMByte(const MachineInstr &MI,
unsigned BaseRegNo = getX86RegNum(BaseReg.getReg()); unsigned BaseRegNo = getX86RegNum(BaseReg.getReg());
unsigned IndexRegNo; unsigned IndexRegNo;
if (IndexReg.getReg()) if (IndexReg.getReg())
IndexRegNo = getX86RegNum(IndexReg.getReg()); IndexRegNo = getX86RegNum(IndexReg.getReg());
else else
IndexRegNo = 4; // For example [ESP+1*<noreg>+4] IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
emitSIBByte(SS, IndexRegNo, BaseRegNo); emitSIBByte(SS, IndexRegNo, BaseRegNo);
} }
// Do we need to output a displacement? // Do we need to output a displacement?
if (Disp.getImmedValue() != 0 || ForceDisp32 || ForceDisp8) { if (DispVal != 0 || ForceDisp32 || ForceDisp8) {
if (!ForceDisp32 && isDisp8(Disp.getImmedValue())) if (!ForceDisp32 && isDisp8(DispVal))
emitConstant(Disp.getImmedValue(), 1); emitConstant(DispVal, 1);
else if (GV)
emitGlobalAddressForPtr(GV, DispVal);
else else
emitConstant(Disp.getImmedValue(), 4); emitConstant(DispVal, 4);
} }
} }
} }
@ -492,8 +516,8 @@ void Emitter::emitInstruction(const MachineInstr &MI) {
case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB: case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF: case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
MCE.emitByte(0xD8+ MCE.emitByte(0xD8+
(((Desc.TSFlags & X86II::Op0Mask)-X86II::D8) (((Desc.TSFlags & X86II::Op0Mask)-X86II::D8)
>> X86II::Op0Shift)); >> X86II::Op0Shift));
break; // Two-byte opcode prefix break; // Two-byte opcode prefix
default: assert(0 && "Invalid prefix!"); default: assert(0 && "Invalid prefix!");
case 0: break; // No prefix! case 0: break; // No prefix!
@ -525,7 +549,7 @@ void Emitter::emitInstruction(const MachineInstr &MI) {
} else if (MO.isImmediate()) { } else if (MO.isImmediate()) {
emitConstant(MO.getImmedValue(), sizeOfImm(Desc)); emitConstant(MO.getImmedValue(), sizeOfImm(Desc));
} else { } else {
assert(0 && "Unknown RawFrm operand!"); assert(0 && "Unknown RawFrm operand!");
} }
} }
break; break;
@ -535,15 +559,17 @@ void Emitter::emitInstruction(const MachineInstr &MI) {
if (MI.getNumOperands() == 2) { if (MI.getNumOperands() == 2) {
const MachineOperand &MO1 = MI.getOperand(1); const MachineOperand &MO1 = MI.getOperand(1);
if (Value *V = MO1.getVRegValueOrNull()) { if (Value *V = MO1.getVRegValueOrNull()) {
assert(sizeOfImm(Desc) == 4 && "Don't know how to emit non-pointer values!"); assert(sizeOfImm(Desc) == 4 &&
"Don't know how to emit non-pointer values!");
emitGlobalAddressForPtr(cast<GlobalValue>(V)); emitGlobalAddressForPtr(cast<GlobalValue>(V));
} else if (MO1.isGlobalAddress()) { } else if (MO1.isGlobalAddress()) {
assert(sizeOfImm(Desc) == 4 && "Don't know how to emit non-pointer values!"); assert(sizeOfImm(Desc) == 4 &&
"Don't know how to emit non-pointer values!");
assert(!MO1.isPCRelative() && "Function pointer ref is PC relative?"); assert(!MO1.isPCRelative() && "Function pointer ref is PC relative?");
emitGlobalAddressForPtr(MO1.getGlobal()); emitGlobalAddressForPtr(MO1.getGlobal(), MO1.getOffset());
} else if (MO1.isExternalSymbol()) { } else if (MO1.isExternalSymbol()) {
assert(sizeOfImm(Desc) == 4 && "Don't know how to emit non-pointer values!"); assert(sizeOfImm(Desc) == 4 &&
"Don't know how to emit non-pointer values!");
unsigned Address = MCE.getGlobalValueAddress(MO1.getSymbolName()); unsigned Address = MCE.getGlobalValueAddress(MO1.getSymbolName());
assert(Address && "Unknown external symbol!"); assert(Address && "Unknown external symbol!");
emitMaybePCRelativeValue(Address, MO1.isPCRelative()); emitMaybePCRelativeValue(Address, MO1.isPCRelative());
@ -608,7 +634,8 @@ void Emitter::emitInstruction(const MachineInstr &MI) {
if (MI.getOperand(4).isImmediate()) if (MI.getOperand(4).isImmediate())
emitConstant(MI.getOperand(4).getImmedValue(), sizeOfImm(Desc)); emitConstant(MI.getOperand(4).getImmedValue(), sizeOfImm(Desc));
else if (MI.getOperand(4).isGlobalAddress()) else if (MI.getOperand(4).isGlobalAddress())
emitGlobalAddressForPtr(MI.getOperand(4).getGlobal()); emitGlobalAddressForPtr(MI.getOperand(4).getGlobal(),
MI.getOperand(4).getOffset());
else else
assert(0 && "Unknown operand!"); assert(0 && "Unknown operand!");
} }