2001-10-18 00:01:48 +00:00
|
|
|
// $Id$
|
|
|
|
//***************************************************************************
|
|
|
|
// File:
|
|
|
|
// SparcInstrInfo.cpp
|
|
|
|
//
|
|
|
|
// Purpose:
|
|
|
|
//
|
|
|
|
// History:
|
|
|
|
// 10/15/01 - Vikram Adve - Created
|
|
|
|
//**************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#include "SparcInternals.h"
|
|
|
|
#include "SparcInstrSelectionSupport.h"
|
|
|
|
#include "llvm/Target/Sparc.h"
|
|
|
|
#include "llvm/CodeGen/InstrSelection.h"
|
|
|
|
#include "llvm/CodeGen/InstrSelectionSupport.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2002-02-03 07:49:49 +00:00
|
|
|
#include "llvm/CodeGen/MachineCodeForMethod.h"
|
2002-04-07 20:49:59 +00:00
|
|
|
#include "llvm/Function.h"
|
2001-12-03 22:26:30 +00:00
|
|
|
#include "llvm/ConstantVals.h"
|
2001-11-08 04:57:53 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2001-10-18 00:01:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
//************************ Internal Functions ******************************/
|
|
|
|
|
|
|
|
|
|
|
|
static inline MachineInstr*
|
2001-12-15 00:33:36 +00:00
|
|
|
CreateIntSetInstruction(int64_t C, Value* dest,
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<TmpInstruction*>& tempVec)
|
2001-10-18 00:01:48 +00:00
|
|
|
{
|
|
|
|
MachineInstr* minstr;
|
2001-10-28 21:41:46 +00:00
|
|
|
uint64_t absC = (C >= 0)? C : -C;
|
|
|
|
if (absC > (unsigned int) ~0)
|
|
|
|
{ // C does not fit in 32 bits
|
2002-02-03 07:49:49 +00:00
|
|
|
TmpInstruction* tmpReg = new TmpInstruction(Type::IntTy);
|
2001-10-28 21:41:46 +00:00
|
|
|
tempVec.push_back(tmpReg);
|
|
|
|
|
|
|
|
minstr = new MachineInstr(SETX);
|
2002-03-18 03:09:15 +00:00
|
|
|
minstr->SetMachineOperandConst(0, MachineOperand::MO_SignExtendedImmed, C);
|
|
|
|
minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpReg,
|
2001-10-28 21:41:46 +00:00
|
|
|
/*isdef*/ true);
|
2002-03-18 03:09:15 +00:00
|
|
|
minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,dest);
|
2001-10-28 21:41:46 +00:00
|
|
|
}
|
2001-12-15 00:33:36 +00:00
|
|
|
else
|
2001-10-18 00:01:48 +00:00
|
|
|
{
|
|
|
|
minstr = new MachineInstr(SETSW);
|
2002-03-31 00:13:12 +00:00
|
|
|
minstr->SetMachineOperandConst(0,MachineOperand::MO_SignExtendedImmed,C);
|
|
|
|
minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,dest);
|
2001-10-18 00:01:48 +00:00
|
|
|
}
|
2001-12-15 00:33:36 +00:00
|
|
|
|
|
|
|
return minstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline MachineInstr*
|
|
|
|
CreateUIntSetInstruction(uint64_t C, Value* dest,
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<TmpInstruction*>& tempVec)
|
2001-12-15 00:33:36 +00:00
|
|
|
{
|
|
|
|
MachineInstr* minstr;
|
|
|
|
if (C > (unsigned int) ~0)
|
|
|
|
{ // C does not fit in 32 bits
|
2002-03-31 00:13:12 +00:00
|
|
|
assert(dest->getType() == Type::ULongTy && "Sign extension problems");
|
2002-02-03 07:49:49 +00:00
|
|
|
TmpInstruction *tmpReg = new TmpInstruction(Type::IntTy);
|
2001-12-15 00:33:36 +00:00
|
|
|
tempVec.push_back(tmpReg);
|
|
|
|
|
|
|
|
minstr = new MachineInstr(SETX);
|
2002-03-31 00:13:12 +00:00
|
|
|
minstr->SetMachineOperandConst(0,MachineOperand::MO_SignExtendedImmed,C);
|
|
|
|
minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
|
|
|
|
tmpReg, /*isdef*/ true);
|
2002-03-18 03:09:15 +00:00
|
|
|
minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,dest);
|
2001-12-15 00:33:36 +00:00
|
|
|
}
|
2002-03-31 00:13:12 +00:00
|
|
|
else if (dest->getType() == Type::ULongTy)
|
2001-10-18 00:01:48 +00:00
|
|
|
{
|
|
|
|
minstr = new MachineInstr(SETUW);
|
2002-03-18 03:09:15 +00:00
|
|
|
minstr->SetMachineOperandConst(0, MachineOperand::MO_UnextendedImmed, C);
|
2002-03-31 00:13:12 +00:00
|
|
|
minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,dest);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // cast to signed type of the right length and use signed op (SETSW)
|
|
|
|
// to get correct sign extension
|
|
|
|
//
|
|
|
|
minstr = new MachineInstr(SETSW);
|
|
|
|
minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,dest);
|
|
|
|
|
|
|
|
switch (dest->getType()->getPrimitiveID())
|
|
|
|
{
|
|
|
|
case Type::UIntTyID:
|
|
|
|
minstr->SetMachineOperandConst(0,
|
|
|
|
MachineOperand::MO_SignExtendedImmed,
|
|
|
|
(int) C);
|
|
|
|
break;
|
|
|
|
case Type::UShortTyID:
|
|
|
|
minstr->SetMachineOperandConst(0,
|
|
|
|
MachineOperand::MO_SignExtendedImmed,
|
|
|
|
(short) C);
|
|
|
|
break;
|
|
|
|
case Type::UByteTyID:
|
|
|
|
minstr->SetMachineOperandConst(0,
|
|
|
|
MachineOperand::MO_SignExtendedImmed,
|
|
|
|
(char) C);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0 && "Unexpected unsigned type");
|
|
|
|
break;
|
|
|
|
}
|
2001-10-18 00:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return minstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//************************* External Classes *******************************/
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// class UltraSparcInstrInfo
|
|
|
|
//
|
|
|
|
// Purpose:
|
|
|
|
// Information about individual instructions.
|
|
|
|
// Most information is stored in the SparcMachineInstrDesc array above.
|
|
|
|
// Other information is computed on demand, and most such functions
|
|
|
|
// default to member functions in base class MachineInstrInfo.
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/*ctor*/
|
2001-11-08 04:57:53 +00:00
|
|
|
UltraSparcInstrInfo::UltraSparcInstrInfo(const TargetMachine& tgt)
|
|
|
|
: MachineInstrInfo(tgt, SparcMachineInstrDesc,
|
2001-10-18 00:01:48 +00:00
|
|
|
/*descSize = */ NUM_TOTAL_OPCODES,
|
|
|
|
/*numRealOpCodes = */ NUM_REAL_OPCODES)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-03-18 03:09:15 +00:00
|
|
|
//
|
2001-10-18 00:01:48 +00:00
|
|
|
// Create an instruction sequence to put the constant `val' into
|
2001-12-03 22:26:30 +00:00
|
|
|
// the virtual register `dest'. `val' may be a Constant or a
|
2001-10-18 00:01:48 +00:00
|
|
|
// GlobalValue, viz., the constant address of a global variable or function.
|
|
|
|
// The generated instructions are returned in `minstrVec'.
|
|
|
|
// Any temp. registers (TmpInstruction) created are returned in `tempVec'.
|
|
|
|
//
|
|
|
|
void
|
2002-04-07 20:49:59 +00:00
|
|
|
UltraSparcInstrInfo::CreateCodeToLoadConst(Function *F, Value* val,
|
2002-03-18 03:09:15 +00:00
|
|
|
Instruction* dest,
|
2002-04-07 20:49:59 +00:00
|
|
|
std::vector<MachineInstr*>&minstrVec,
|
|
|
|
std::vector<TmpInstruction*>& tempVec) const
|
2001-10-18 00:01:48 +00:00
|
|
|
{
|
|
|
|
MachineInstr* minstr;
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
|
2001-10-18 00:01:48 +00:00
|
|
|
"I only know about constant values and global addresses");
|
|
|
|
|
|
|
|
// Use a "set" instruction for known constants that can go in an integer reg.
|
|
|
|
// Use a "load" instruction for all other constants, in particular,
|
|
|
|
// floating point constants and addresses of globals.
|
|
|
|
//
|
|
|
|
const Type* valType = val->getType();
|
|
|
|
|
|
|
|
if (valType->isIntegral() || valType == Type::BoolTy)
|
|
|
|
{
|
2001-12-15 00:33:36 +00:00
|
|
|
if (ConstantUInt* uval = dyn_cast<ConstantUInt>(val))
|
|
|
|
{
|
|
|
|
uint64_t C = uval->getValue();
|
|
|
|
minstr = CreateUIntSetInstruction(C, dest, tempVec);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool isValidConstant;
|
|
|
|
int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
|
|
|
|
assert(isValidConstant && "Unrecognized constant");
|
|
|
|
minstr = CreateIntSetInstruction(C, dest, tempVec);
|
|
|
|
}
|
2001-10-18 00:01:48 +00:00
|
|
|
minstrVec.push_back(minstr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Make an instruction sequence to load the constant, viz:
|
2001-10-28 21:41:46 +00:00
|
|
|
// SETX <addr-of-constant>, tmpReg, addrReg
|
2001-10-18 00:01:48 +00:00
|
|
|
// LOAD /*addr*/ addrReg, /*offset*/ 0, dest
|
2001-10-28 21:41:46 +00:00
|
|
|
// Only the SETX is needed if `val' is a GlobalValue, i.e,. it is
|
2001-10-18 00:01:48 +00:00
|
|
|
// itself a constant address. Otherwise, both are needed.
|
|
|
|
|
|
|
|
Value* addrVal;
|
|
|
|
int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
|
|
|
|
|
2001-10-28 21:41:46 +00:00
|
|
|
TmpInstruction* tmpReg =
|
2002-02-03 07:49:49 +00:00
|
|
|
new TmpInstruction(PointerType::get(val->getType()), val);
|
2001-10-28 21:41:46 +00:00
|
|
|
tempVec.push_back(tmpReg);
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
if (isa<Constant>(val))
|
2001-10-18 00:01:48 +00:00
|
|
|
{
|
|
|
|
// Create another TmpInstruction for the hidden integer register
|
|
|
|
TmpInstruction* addrReg =
|
2002-02-03 07:49:49 +00:00
|
|
|
new TmpInstruction(PointerType::get(val->getType()), val);
|
2001-10-18 00:01:48 +00:00
|
|
|
tempVec.push_back(addrReg);
|
|
|
|
addrVal = addrReg;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
addrVal = dest;
|
|
|
|
|
2001-10-28 21:41:46 +00:00
|
|
|
minstr = new MachineInstr(SETX);
|
2002-03-18 03:09:15 +00:00
|
|
|
minstr->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp, val);
|
2002-04-07 20:49:59 +00:00
|
|
|
minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,tmpReg,
|
2001-10-28 21:41:46 +00:00
|
|
|
/*isdef*/ true);
|
2002-04-07 20:49:59 +00:00
|
|
|
minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
|
|
|
|
addrVal);
|
2001-10-18 00:01:48 +00:00
|
|
|
minstrVec.push_back(minstr);
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
if (isa<Constant>(val))
|
2001-10-18 00:01:48 +00:00
|
|
|
{
|
2002-03-18 03:09:15 +00:00
|
|
|
// Make sure constant is emitted to constant pool in assembly code.
|
2002-04-07 20:49:59 +00:00
|
|
|
MachineCodeForMethod& mcinfo = MachineCodeForMethod::get(F);
|
2002-03-18 03:09:15 +00:00
|
|
|
mcinfo.addToConstantPool(cast<Constant>(val));
|
|
|
|
|
|
|
|
// Generate the load instruction
|
2001-10-18 00:01:48 +00:00
|
|
|
minstr = new MachineInstr(ChooseLoadInstruction(val->getType()));
|
2002-03-18 03:09:15 +00:00
|
|
|
minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
|
2001-10-18 00:01:48 +00:00
|
|
|
addrVal);
|
2002-04-07 20:49:59 +00:00
|
|
|
minstr->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
|
2001-10-18 00:01:48 +00:00
|
|
|
zeroOffset);
|
2002-03-18 03:09:15 +00:00
|
|
|
minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
|
2001-10-18 00:01:48 +00:00
|
|
|
dest);
|
|
|
|
minstrVec.push_back(minstr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-09 02:16:40 +00:00
|
|
|
// Create an instruction sequence to copy an integer value `val'
|
|
|
|
// to a floating point value `dest' by copying to memory and back.
|
|
|
|
// val must be an integral type. dest must be a Float or Double.
|
2001-11-08 04:57:53 +00:00
|
|
|
// The generated instructions are returned in `minstrVec'.
|
|
|
|
// Any temp. registers (TmpInstruction) created are returned in `tempVec'.
|
|
|
|
//
|
|
|
|
void
|
2002-04-07 20:49:59 +00:00
|
|
|
UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(Function *F,
|
2002-01-20 22:54:45 +00:00
|
|
|
Value* val,
|
|
|
|
Instruction* dest,
|
|
|
|
std::vector<MachineInstr*>& minstrVec,
|
|
|
|
std::vector<TmpInstruction*>& tempVec,
|
|
|
|
TargetMachine& target) const
|
2001-11-08 04:57:53 +00:00
|
|
|
{
|
2001-11-09 02:16:40 +00:00
|
|
|
assert((val->getType()->isIntegral() || val->getType()->isPointerType())
|
|
|
|
&& "Source type must be integral");
|
2002-04-07 20:49:59 +00:00
|
|
|
assert((dest->getType() == Type::FloatTy || dest->getType() == Type::DoubleTy)
|
2001-11-08 04:57:53 +00:00
|
|
|
&& "Dest type must be float/double");
|
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
MachineCodeForMethod& mcinfo = MachineCodeForMethod::get(F);
|
2001-11-08 04:57:53 +00:00
|
|
|
int offset = mcinfo.allocateLocalVar(target, val);
|
|
|
|
|
|
|
|
// Store instruction stores `val' to [%fp+offset].
|
2001-11-09 02:16:40 +00:00
|
|
|
// The store and load opCodes are based on the value being copied, and
|
2001-11-15 14:59:56 +00:00
|
|
|
// they use integer and float types that accomodate the
|
|
|
|
// larger of the source type and the destination type:
|
2001-11-08 04:57:53 +00:00
|
|
|
// On SparcV9: int for float, long for double.
|
2001-11-09 02:16:40 +00:00
|
|
|
//
|
2001-11-08 04:57:53 +00:00
|
|
|
Type* tmpType = (dest->getType() == Type::FloatTy)? Type::IntTy
|
|
|
|
: Type::LongTy;
|
|
|
|
MachineInstr* store = new MachineInstr(ChooseStoreInstruction(tmpType));
|
2002-03-18 03:09:15 +00:00
|
|
|
store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, val);
|
|
|
|
store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer());
|
2002-04-07 20:49:59 +00:00
|
|
|
store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed, offset);
|
2001-11-08 04:57:53 +00:00
|
|
|
minstrVec.push_back(store);
|
2001-10-18 00:01:48 +00:00
|
|
|
|
2001-11-08 04:57:53 +00:00
|
|
|
// Load instruction loads [%fp+offset] to `dest'.
|
2001-11-09 02:16:40 +00:00
|
|
|
//
|
2001-11-15 14:59:56 +00:00
|
|
|
MachineInstr* load =new MachineInstr(ChooseLoadInstruction(dest->getType()));
|
2002-03-18 03:09:15 +00:00
|
|
|
load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
|
|
|
|
load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset);
|
|
|
|
load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest);
|
2001-11-09 02:16:40 +00:00
|
|
|
minstrVec.push_back(load);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Similarly, create an instruction sequence to copy an FP value
|
|
|
|
// `val' to an integer value `dest' by copying to memory and back.
|
|
|
|
// See the previous function for information about return values.
|
|
|
|
//
|
|
|
|
void
|
2002-04-07 20:49:59 +00:00
|
|
|
UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(Function *F,
|
2002-01-20 22:54:45 +00:00
|
|
|
Value* val,
|
|
|
|
Instruction* dest,
|
|
|
|
std::vector<MachineInstr*>& minstrVec,
|
|
|
|
std::vector<TmpInstruction*>& tempVec,
|
|
|
|
TargetMachine& target) const
|
2001-11-09 02:16:40 +00:00
|
|
|
{
|
|
|
|
assert((val->getType() ==Type::FloatTy || val->getType() ==Type::DoubleTy)
|
|
|
|
&& "Source type must be float/double");
|
|
|
|
assert((dest->getType()->isIntegral() || dest->getType()->isPointerType())
|
|
|
|
&& "Dest type must be integral");
|
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
MachineCodeForMethod& mcinfo = MachineCodeForMethod::get(F);
|
2001-11-09 02:16:40 +00:00
|
|
|
int offset = mcinfo.allocateLocalVar(target, val);
|
|
|
|
|
|
|
|
// Store instruction stores `val' to [%fp+offset].
|
|
|
|
// The store and load opCodes are based on the value being copied, and
|
|
|
|
// they use the integer type that matches the source type in size:
|
|
|
|
// On SparcV9: int for float, long for double.
|
|
|
|
//
|
|
|
|
Type* tmpType = (val->getType() == Type::FloatTy)? Type::IntTy
|
|
|
|
: Type::LongTy;
|
2001-11-15 14:59:56 +00:00
|
|
|
MachineInstr* store=new MachineInstr(ChooseStoreInstruction(val->getType()));
|
2002-03-18 03:09:15 +00:00
|
|
|
store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, val);
|
|
|
|
store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer());
|
|
|
|
store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset);
|
2001-11-09 02:16:40 +00:00
|
|
|
minstrVec.push_back(store);
|
|
|
|
|
|
|
|
// Load instruction loads [%fp+offset] to `dest'.
|
2001-11-08 04:57:53 +00:00
|
|
|
//
|
|
|
|
MachineInstr* load = new MachineInstr(ChooseLoadInstruction(tmpType));
|
2002-03-18 03:09:15 +00:00
|
|
|
load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
|
|
|
|
load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed, offset);
|
|
|
|
load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest);
|
2001-11-08 04:57:53 +00:00
|
|
|
minstrVec.push_back(load);
|
|
|
|
}
|