Simplify loading (un)signed constants to registers, patch by Nate Begeman.

llvm-svn: 15306
This commit is contained in:
Misha Brukman 2004-07-28 19:13:49 +00:00
parent db13b6edba
commit 862bb562cc
2 changed files with 106 additions and 102 deletions

View File

@ -542,61 +542,63 @@ void ISel::copyConstantToRegister(MachineBasicBlock *MBB,
unsigned Class = getClassB(C->getType()); unsigned Class = getClassB(C->getType());
if (Class == cLong) { if (Class == cLong) {
// Copy the value into the register pair. if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(C)) {
uint64_t Val = cast<ConstantInt>(C)->getRawValue(); uint64_t uval = CUI->getValue();
unsigned hiUVal = uval >> 32;
if (Val < (1ULL << 16)) { unsigned loUVal = uval;
BuildMI(*MBB, IP, PPC32::LI, 1, R).addSImm(0); ConstantUInt *CUHi = ConstantUInt::get(Type::UIntTy, hiUVal);
BuildMI(*MBB, IP, PPC32::LI, 1, R+1).addSImm(Val & 0xFFFF); ConstantUInt *CULo = ConstantUInt::get(Type::UIntTy, loUVal);
} else if (Val < (1ULL << 32)) { copyConstantToRegister(MBB, IP, CUHi, R);
unsigned Temp = makeAnotherReg(Type::IntTy); copyConstantToRegister(MBB, IP, CULo, R+1);
BuildMI(*MBB, IP, PPC32::LI, 1, R).addSImm(0); return;
BuildMI(*MBB, IP, PPC32::LIS, 1, Temp).addSImm((Val >> 16) & 0xFFFF); } else if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(C)) {
BuildMI(*MBB, IP, PPC32::ORI, 2, R+1).addReg(Temp).addImm(Val & 0xFFFF); int64_t sval = CSI->getValue();
} else if (Val < (1ULL << 48)) { int hiSVal = sval >> 32;
unsigned Temp = makeAnotherReg(Type::IntTy); int loSVal = sval;
int HiBits = (Val >> 32) & 0xFFFF; ConstantSInt *CSHi = ConstantSInt::get(Type::IntTy, hiSVal);
if (HiBits > 32767) { ConstantSInt *CSLo = ConstantSInt::get(Type::IntTy, loSVal);
BuildMI(*MBB, IP, PPC32::LI, 1, PPC32::R0).addImm(0); copyConstantToRegister(MBB, IP, CSHi, R);
BuildMI(*MBB, IP, PPC32::ORI, 2, R).addReg(PPC32::R0).addSImm(HiBits); copyConstantToRegister(MBB, IP, CSLo, R+1);
} else { return;
BuildMI(*MBB, IP, PPC32::LI, 1, R).addSImm(HiBits);
}
BuildMI(*MBB, IP, PPC32::LIS, 1, Temp).addSImm((Val >> 16) & 0xFFFF);
BuildMI(*MBB, IP, PPC32::ORI, 2, R+1).addReg(Temp).addImm(Val & 0xFFFF);
} else { } else {
unsigned TempLo = makeAnotherReg(Type::IntTy); std::cerr << "Unhandled long constant type!\n";
unsigned TempHi = makeAnotherReg(Type::IntTy); abort();
BuildMI(*MBB, IP, PPC32::LIS, 1, TempHi).addSImm((Val >> 48) & 0xFFFF); }
BuildMI(*MBB, IP, PPC32::ORI, 2, R).addReg(TempHi) }
.addImm((Val >> 32) & 0xFFFF);
BuildMI(*MBB, IP, PPC32::LIS, 1, TempLo).addSImm((Val >> 16) & 0xFFFF); assert(Class <= cInt && "Type not handled yet!");
BuildMI(*MBB, IP, PPC32::ORI, 2, R+1).addReg(TempLo)
.addImm(Val & 0xFFFF); // Handle bool
if (C->getType() == Type::BoolTy) {
BuildMI(*MBB, IP, PPC32::LI, 1, R).addSImm(C == ConstantBool::True);
return;
}
// Handle int
if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(C)) {
unsigned uval = CUI->getValue();
if (uval < 32768) {
BuildMI(*MBB, IP, PPC32::LI, 1, R).addSImm(uval);
} else {
unsigned Temp = makeAnotherReg(Type::IntTy);
BuildMI(*MBB, IP, PPC32::LIS, 1, Temp).addSImm(uval >> 16);
BuildMI(*MBB, IP, PPC32::ORI, 2, R).addReg(Temp).addImm(uval);
}
return;
} else if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(C)) {
int sval = CSI->getValue();
if (sval < 32768 && sval >= -32768) {
BuildMI(*MBB, IP, PPC32::LI, 1, R).addSImm(sval);
} else {
unsigned Temp = makeAnotherReg(Type::IntTy);
BuildMI(*MBB, IP, PPC32::LIS, 1, Temp).addSImm(sval >> 16);
BuildMI(*MBB, IP, PPC32::ORI, 2, R).addReg(Temp).addImm(sval);
} }
return; return;
} }
assert(Class <= cInt && "Type not handled yet!"); std::cerr << "Unhandled integer constant!\n";
abort();
if (C->getType() == Type::BoolTy) {
BuildMI(*MBB, IP, PPC32::LI, 1, R).addSImm(C == ConstantBool::True);
} else if (Class == cByte || Class == cShort) {
ConstantInt *CI = cast<ConstantInt>(C);
BuildMI(*MBB, IP, PPC32::LI, 1, R).addSImm(CI->getRawValue());
} else {
ConstantInt *CI = cast<ConstantInt>(C);
int TheVal = CI->getRawValue() & 0xFFFFFFFF;
if (TheVal < 32768 && TheVal >= -32768) {
BuildMI(*MBB, IP, PPC32::LI, 1, R).addSImm(CI->getRawValue());
} else {
unsigned TmpReg = makeAnotherReg(Type::IntTy);
BuildMI(*MBB, IP, PPC32::LIS, 1, TmpReg)
.addSImm(CI->getRawValue() >> 16);
BuildMI(*MBB, IP, PPC32::ORI, 2, R).addReg(TmpReg)
.addImm(CI->getRawValue() & 0xFFFF);
}
}
} else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
// We need to spill the constant to memory... // We need to spill the constant to memory...
MachineConstantPool *CP = F->getConstantPool(); MachineConstantPool *CP = F->getConstantPool();

View File

@ -542,61 +542,63 @@ void ISel::copyConstantToRegister(MachineBasicBlock *MBB,
unsigned Class = getClassB(C->getType()); unsigned Class = getClassB(C->getType());
if (Class == cLong) { if (Class == cLong) {
// Copy the value into the register pair. if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(C)) {
uint64_t Val = cast<ConstantInt>(C)->getRawValue(); uint64_t uval = CUI->getValue();
unsigned hiUVal = uval >> 32;
if (Val < (1ULL << 16)) { unsigned loUVal = uval;
BuildMI(*MBB, IP, PPC32::LI, 1, R).addSImm(0); ConstantUInt *CUHi = ConstantUInt::get(Type::UIntTy, hiUVal);
BuildMI(*MBB, IP, PPC32::LI, 1, R+1).addSImm(Val & 0xFFFF); ConstantUInt *CULo = ConstantUInt::get(Type::UIntTy, loUVal);
} else if (Val < (1ULL << 32)) { copyConstantToRegister(MBB, IP, CUHi, R);
unsigned Temp = makeAnotherReg(Type::IntTy); copyConstantToRegister(MBB, IP, CULo, R+1);
BuildMI(*MBB, IP, PPC32::LI, 1, R).addSImm(0); return;
BuildMI(*MBB, IP, PPC32::LIS, 1, Temp).addSImm((Val >> 16) & 0xFFFF); } else if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(C)) {
BuildMI(*MBB, IP, PPC32::ORI, 2, R+1).addReg(Temp).addImm(Val & 0xFFFF); int64_t sval = CSI->getValue();
} else if (Val < (1ULL << 48)) { int hiSVal = sval >> 32;
unsigned Temp = makeAnotherReg(Type::IntTy); int loSVal = sval;
int HiBits = (Val >> 32) & 0xFFFF; ConstantSInt *CSHi = ConstantSInt::get(Type::IntTy, hiSVal);
if (HiBits > 32767) { ConstantSInt *CSLo = ConstantSInt::get(Type::IntTy, loSVal);
BuildMI(*MBB, IP, PPC32::LI, 1, PPC32::R0).addImm(0); copyConstantToRegister(MBB, IP, CSHi, R);
BuildMI(*MBB, IP, PPC32::ORI, 2, R).addReg(PPC32::R0).addSImm(HiBits); copyConstantToRegister(MBB, IP, CSLo, R+1);
} else { return;
BuildMI(*MBB, IP, PPC32::LI, 1, R).addSImm(HiBits);
}
BuildMI(*MBB, IP, PPC32::LIS, 1, Temp).addSImm((Val >> 16) & 0xFFFF);
BuildMI(*MBB, IP, PPC32::ORI, 2, R+1).addReg(Temp).addImm(Val & 0xFFFF);
} else { } else {
unsigned TempLo = makeAnotherReg(Type::IntTy); std::cerr << "Unhandled long constant type!\n";
unsigned TempHi = makeAnotherReg(Type::IntTy); abort();
BuildMI(*MBB, IP, PPC32::LIS, 1, TempHi).addSImm((Val >> 48) & 0xFFFF); }
BuildMI(*MBB, IP, PPC32::ORI, 2, R).addReg(TempHi) }
.addImm((Val >> 32) & 0xFFFF);
BuildMI(*MBB, IP, PPC32::LIS, 1, TempLo).addSImm((Val >> 16) & 0xFFFF); assert(Class <= cInt && "Type not handled yet!");
BuildMI(*MBB, IP, PPC32::ORI, 2, R+1).addReg(TempLo)
.addImm(Val & 0xFFFF); // Handle bool
if (C->getType() == Type::BoolTy) {
BuildMI(*MBB, IP, PPC32::LI, 1, R).addSImm(C == ConstantBool::True);
return;
}
// Handle int
if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(C)) {
unsigned uval = CUI->getValue();
if (uval < 32768) {
BuildMI(*MBB, IP, PPC32::LI, 1, R).addSImm(uval);
} else {
unsigned Temp = makeAnotherReg(Type::IntTy);
BuildMI(*MBB, IP, PPC32::LIS, 1, Temp).addSImm(uval >> 16);
BuildMI(*MBB, IP, PPC32::ORI, 2, R).addReg(Temp).addImm(uval);
}
return;
} else if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(C)) {
int sval = CSI->getValue();
if (sval < 32768 && sval >= -32768) {
BuildMI(*MBB, IP, PPC32::LI, 1, R).addSImm(sval);
} else {
unsigned Temp = makeAnotherReg(Type::IntTy);
BuildMI(*MBB, IP, PPC32::LIS, 1, Temp).addSImm(sval >> 16);
BuildMI(*MBB, IP, PPC32::ORI, 2, R).addReg(Temp).addImm(sval);
} }
return; return;
} }
assert(Class <= cInt && "Type not handled yet!"); std::cerr << "Unhandled integer constant!\n";
abort();
if (C->getType() == Type::BoolTy) {
BuildMI(*MBB, IP, PPC32::LI, 1, R).addSImm(C == ConstantBool::True);
} else if (Class == cByte || Class == cShort) {
ConstantInt *CI = cast<ConstantInt>(C);
BuildMI(*MBB, IP, PPC32::LI, 1, R).addSImm(CI->getRawValue());
} else {
ConstantInt *CI = cast<ConstantInt>(C);
int TheVal = CI->getRawValue() & 0xFFFFFFFF;
if (TheVal < 32768 && TheVal >= -32768) {
BuildMI(*MBB, IP, PPC32::LI, 1, R).addSImm(CI->getRawValue());
} else {
unsigned TmpReg = makeAnotherReg(Type::IntTy);
BuildMI(*MBB, IP, PPC32::LIS, 1, TmpReg)
.addSImm(CI->getRawValue() >> 16);
BuildMI(*MBB, IP, PPC32::ORI, 2, R).addReg(TmpReg)
.addImm(CI->getRawValue() & 0xFFFF);
}
}
} else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
// We need to spill the constant to memory... // We need to spill the constant to memory...
MachineConstantPool *CP = F->getConstantPool(); MachineConstantPool *CP = F->getConstantPool();