2002-08-09 20:08:06 +00:00
|
|
|
//===-- InstrSelectionSupport.cpp -----------------------------------------===//
|
|
|
|
//
|
|
|
|
// Target-independent instruction selection code. See SparcInstrSelection.cpp
|
|
|
|
// for usage.
|
2001-10-10 20:50:43 +00:00
|
|
|
//
|
2002-08-09 20:08:06 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-10 20:50:43 +00:00
|
|
|
|
|
|
|
#include "llvm/CodeGen/InstrSelectionSupport.h"
|
|
|
|
#include "llvm/CodeGen/InstrSelection.h"
|
2002-05-19 15:34:29 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstrAnnot.h"
|
2002-02-03 07:39:06 +00:00
|
|
|
#include "llvm/CodeGen/MachineCodeForInstruction.h"
|
|
|
|
#include "llvm/CodeGen/InstrForest.h"
|
2001-10-10 20:50:43 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2002-12-29 03:13:05 +00:00
|
|
|
#include "llvm/Target/TargetRegInfo.h"
|
2003-01-14 22:00:31 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2002-04-28 19:55:58 +00:00
|
|
|
#include "llvm/Constants.h"
|
2003-01-15 21:36:50 +00:00
|
|
|
#include "llvm/BasicBlock.h"
|
2002-10-28 23:54:47 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2003-07-23 14:55:59 +00:00
|
|
|
#include "../../Target/Sparc/SparcInstrSelectionSupport.h" // FIXME!
|
2001-10-10 20:50:43 +00:00
|
|
|
|
2001-10-17 23:57:50 +00:00
|
|
|
|
2002-05-19 15:34:29 +00:00
|
|
|
// Generate code to load the constant into a TmpInstruction (virtual reg) and
|
|
|
|
// returns the virtual register.
|
|
|
|
//
|
2001-10-17 23:57:50 +00:00
|
|
|
static TmpInstruction*
|
2002-04-07 20:49:59 +00:00
|
|
|
InsertCodeToLoadConstant(Function *F,
|
2002-03-18 03:33:43 +00:00
|
|
|
Value* opValue,
|
2001-10-17 23:57:50 +00:00
|
|
|
Instruction* vmInstr,
|
2003-07-23 14:55:59 +00:00
|
|
|
std::vector<MachineInstr*>& loadConstVec,
|
2001-10-17 23:57:50 +00:00
|
|
|
TargetMachine& target)
|
2001-10-10 20:50:43 +00:00
|
|
|
{
|
2001-10-17 23:57:50 +00:00
|
|
|
// Create a tmp virtual register to hold the constant.
|
2002-05-19 15:34:29 +00:00
|
|
|
MachineCodeForInstruction &mcfi = MachineCodeForInstruction::get(vmInstr);
|
2003-05-31 07:41:24 +00:00
|
|
|
TmpInstruction* tmpReg = new TmpInstruction(mcfi, opValue);
|
2001-10-10 20:50:43 +00:00
|
|
|
|
2002-05-19 15:34:29 +00:00
|
|
|
target.getInstrInfo().CreateCodeToLoadConst(target, F, opValue, tmpReg,
|
|
|
|
loadConstVec, mcfi);
|
2001-10-17 23:57:50 +00:00
|
|
|
|
|
|
|
// Record the mapping from the tmp VM instruction to machine instruction.
|
|
|
|
// Do this for all machine instructions that were not mapped to any
|
|
|
|
// other temp values created by
|
|
|
|
// tmpReg->addMachineInstruction(loadConstVec.back());
|
2001-10-10 20:50:43 +00:00
|
|
|
|
2001-10-17 23:57:50 +00:00
|
|
|
return tmpReg;
|
2001-10-10 20:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MachineOperand::MachineOperandType
|
2002-09-16 15:15:57 +00:00
|
|
|
ChooseRegOrImmed(int64_t intValue,
|
|
|
|
bool isSigned,
|
2001-10-10 20:50:43 +00:00
|
|
|
MachineOpCode opCode,
|
|
|
|
const TargetMachine& target,
|
|
|
|
bool canUseImmed,
|
|
|
|
unsigned int& getMachineRegNum,
|
|
|
|
int64_t& getImmedValue)
|
|
|
|
{
|
2002-09-16 15:15:57 +00:00
|
|
|
MachineOperand::MachineOperandType opType=MachineOperand::MO_VirtualRegister;
|
2001-10-10 20:50:43 +00:00
|
|
|
getMachineRegNum = 0;
|
|
|
|
getImmedValue = 0;
|
|
|
|
|
2002-09-16 15:15:57 +00:00
|
|
|
if (canUseImmed &&
|
2003-01-15 21:36:50 +00:00
|
|
|
target.getInstrInfo().constantFitsInImmedField(opCode, intValue))
|
2001-10-10 20:50:43 +00:00
|
|
|
{
|
2002-09-16 15:15:57 +00:00
|
|
|
opType = isSigned? MachineOperand::MO_SignExtendedImmed
|
|
|
|
: MachineOperand::MO_UnextendedImmed;
|
|
|
|
getImmedValue = intValue;
|
2001-10-10 20:50:43 +00:00
|
|
|
}
|
2002-09-16 15:15:57 +00:00
|
|
|
else if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0)
|
|
|
|
{
|
|
|
|
opType = MachineOperand::MO_MachineRegister;
|
|
|
|
getMachineRegNum = target.getRegInfo().getZeroRegNum();
|
|
|
|
}
|
|
|
|
|
|
|
|
return opType;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MachineOperand::MachineOperandType
|
|
|
|
ChooseRegOrImmed(Value* val,
|
|
|
|
MachineOpCode opCode,
|
|
|
|
const TargetMachine& target,
|
|
|
|
bool canUseImmed,
|
|
|
|
unsigned int& getMachineRegNum,
|
|
|
|
int64_t& getImmedValue)
|
|
|
|
{
|
|
|
|
getMachineRegNum = 0;
|
|
|
|
getImmedValue = 0;
|
|
|
|
|
|
|
|
// To use reg or immed, constant needs to be integer, bool, or a NULL pointer
|
|
|
|
Constant *CPV = dyn_cast<Constant>(val);
|
2003-07-06 20:33:21 +00:00
|
|
|
if (CPV == NULL
|
|
|
|
|| CPV->isConstantExpr()
|
|
|
|
|| (! CPV->getType()->isIntegral() &&
|
|
|
|
! (isa<PointerType>(CPV->getType()) && CPV->isNullValue())))
|
2002-09-16 15:15:57 +00:00
|
|
|
return MachineOperand::MO_VirtualRegister;
|
|
|
|
|
2001-10-10 20:50:43 +00:00
|
|
|
// Now get the constant value and check if it fits in the IMMED field.
|
|
|
|
// Take advantage of the fact that the max unsigned value will rarely
|
|
|
|
// fit into any IMMED field and ignore that case (i.e., cast smaller
|
|
|
|
// unsigned constants to signed).
|
|
|
|
//
|
|
|
|
int64_t intValue;
|
2002-05-06 16:15:30 +00:00
|
|
|
if (isa<PointerType>(CPV->getType()))
|
2002-09-16 15:15:57 +00:00
|
|
|
intValue = 0; // We checked above that it is NULL
|
|
|
|
else if (ConstantBool* CB = dyn_cast<ConstantBool>(CPV))
|
|
|
|
intValue = (int64_t) CB->getValue();
|
2001-11-14 17:55:02 +00:00
|
|
|
else if (CPV->getType()->isSigned())
|
2002-09-16 15:15:57 +00:00
|
|
|
intValue = cast<ConstantSInt>(CPV)->getValue();
|
2001-10-10 20:50:43 +00:00
|
|
|
else
|
2002-09-27 14:26:20 +00:00
|
|
|
{ // get the int value and sign-extend if original was less than 64 bits
|
|
|
|
intValue = (int64_t) cast<ConstantUInt>(CPV)->getValue();
|
|
|
|
switch(CPV->getType()->getPrimitiveID())
|
|
|
|
{
|
|
|
|
case Type::UByteTyID: intValue = (int64_t) (int8_t) intValue; break;
|
|
|
|
case Type::UShortTyID: intValue = (int64_t) (short) intValue; break;
|
|
|
|
case Type::UIntTyID: intValue = (int64_t) (int) intValue; break;
|
|
|
|
default: break;
|
|
|
|
}
|
2001-10-10 20:50:43 +00:00
|
|
|
}
|
|
|
|
|
2002-09-16 15:15:57 +00:00
|
|
|
return ChooseRegOrImmed(intValue, CPV->getType()->isSigned(),
|
|
|
|
opCode, target, canUseImmed,
|
|
|
|
getMachineRegNum, getImmedValue);
|
2001-10-10 20:50:43 +00:00
|
|
|
}
|
|
|
|
|
2001-10-17 23:57:50 +00:00
|
|
|
|
2003-01-15 19:47:53 +00:00
|
|
|
|
2001-10-17 23:57:50 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Function: FixConstantOperandsForInstr
|
|
|
|
//
|
|
|
|
// Purpose:
|
|
|
|
// Special handling for constant operands of a machine instruction
|
|
|
|
// -- if the constant is 0, use the hardwired 0 register, if any;
|
|
|
|
// -- if the constant fits in the IMMEDIATE field, use that field;
|
|
|
|
// -- else create instructions to put the constant into a register, either
|
|
|
|
// directly or by loading explicitly from the constant pool.
|
|
|
|
//
|
|
|
|
// In the first 2 cases, the operand of `minstr' is modified in place.
|
|
|
|
// Returns a vector of machine instructions generated for operands that
|
|
|
|
// fall under case 3; these must be inserted before `minstr'.
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2003-07-23 14:55:59 +00:00
|
|
|
std::vector<MachineInstr*>
|
2001-10-17 23:57:50 +00:00
|
|
|
FixConstantOperandsForInstr(Instruction* vmInstr,
|
|
|
|
MachineInstr* minstr,
|
|
|
|
TargetMachine& target)
|
|
|
|
{
|
2003-07-23 14:55:59 +00:00
|
|
|
std::vector<MachineInstr*> MVec;
|
2001-10-17 23:57:50 +00:00
|
|
|
|
2002-09-16 15:15:57 +00:00
|
|
|
MachineOpCode opCode = minstr->getOpCode();
|
2003-01-14 22:00:31 +00:00
|
|
|
const TargetInstrInfo& instrInfo = target.getInstrInfo();
|
2002-10-29 17:25:41 +00:00
|
|
|
int resultPos = instrInfo.getResultPos(opCode);
|
2002-09-16 15:15:57 +00:00
|
|
|
int immedPos = instrInfo.getImmedConstantPos(opCode);
|
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
Function *F = vmInstr->getParent()->getParent();
|
2002-09-16 15:15:57 +00:00
|
|
|
|
2001-10-17 23:57:50 +00:00
|
|
|
for (unsigned op=0; op < minstr->getNumOperands(); op++)
|
|
|
|
{
|
|
|
|
const MachineOperand& mop = minstr->getOperand(op);
|
|
|
|
|
2002-09-16 15:15:57 +00:00
|
|
|
// Skip the result position, preallocated machine registers, or operands
|
|
|
|
// that cannot be constants (CC regs or PC-relative displacements)
|
2002-10-29 17:25:41 +00:00
|
|
|
if (resultPos == (int)op ||
|
2002-10-28 04:45:29 +00:00
|
|
|
mop.getType() == MachineOperand::MO_MachineRegister ||
|
|
|
|
mop.getType() == MachineOperand::MO_CCRegister ||
|
|
|
|
mop.getType() == MachineOperand::MO_PCRelativeDisp)
|
2002-09-16 15:15:57 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
bool constantThatMustBeLoaded = false;
|
|
|
|
unsigned int machineRegNum = 0;
|
|
|
|
int64_t immedValue = 0;
|
|
|
|
Value* opValue = NULL;
|
|
|
|
MachineOperand::MachineOperandType opType =
|
|
|
|
MachineOperand::MO_VirtualRegister;
|
|
|
|
|
|
|
|
// Operand may be a virtual register or a compile-time constant
|
2002-10-28 04:45:29 +00:00
|
|
|
if (mop.getType() == MachineOperand::MO_VirtualRegister)
|
2001-10-17 23:57:50 +00:00
|
|
|
{
|
2002-09-16 15:15:57 +00:00
|
|
|
assert(mop.getVRegValue() != NULL);
|
2003-01-15 20:32:15 +00:00
|
|
|
opValue = mop.getVRegValue();
|
|
|
|
if (Constant *opConst = dyn_cast<Constant>(opValue)) {
|
2003-01-15 19:47:53 +00:00
|
|
|
opType = ChooseRegOrImmed(opConst, opCode, target,
|
|
|
|
(immedPos == (int)op), machineRegNum,
|
|
|
|
immedValue);
|
|
|
|
if (opType == MachineOperand::MO_VirtualRegister)
|
|
|
|
constantThatMustBeLoaded = true;
|
|
|
|
}
|
2001-10-17 23:57:50 +00:00
|
|
|
}
|
2002-09-16 15:15:57 +00:00
|
|
|
else
|
2002-03-18 03:33:43 +00:00
|
|
|
{
|
2003-01-15 19:47:53 +00:00
|
|
|
assert(mop.isImmediate());
|
|
|
|
bool isSigned = mop.getType() == MachineOperand::MO_SignExtendedImmed;
|
2002-09-16 15:15:57 +00:00
|
|
|
|
|
|
|
// Bit-selection flags indicate an instruction that is extracting
|
|
|
|
// bits from its operand so ignore this even if it is a big constant.
|
|
|
|
if (mop.opHiBits32() || mop.opLoBits32() ||
|
|
|
|
mop.opHiBits64() || mop.opLoBits64())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
opType = ChooseRegOrImmed(mop.getImmedValue(), isSigned,
|
|
|
|
opCode, target, (immedPos == (int)op),
|
|
|
|
machineRegNum, immedValue);
|
|
|
|
|
2003-06-07 02:34:43 +00:00
|
|
|
if (opType == MachineOperand::MO_SignExtendedImmed ||
|
|
|
|
opType == MachineOperand::MO_UnextendedImmed) {
|
2003-06-04 04:54:06 +00:00
|
|
|
// The optype is an immediate value
|
|
|
|
// This means we need to change the opcode, e.g. ADDr -> ADDi
|
|
|
|
unsigned newOpcode = convertOpcodeFromRegToImm(opCode);
|
|
|
|
minstr->setOpcode(newOpcode);
|
|
|
|
}
|
|
|
|
|
2002-10-28 04:45:29 +00:00
|
|
|
if (opType == mop.getType())
|
2002-09-16 15:15:57 +00:00
|
|
|
continue; // no change: this is the most common case
|
|
|
|
|
|
|
|
if (opType == MachineOperand::MO_VirtualRegister)
|
|
|
|
{
|
|
|
|
constantThatMustBeLoaded = true;
|
|
|
|
opValue = isSigned
|
2002-09-17 17:23:09 +00:00
|
|
|
? (Value*)ConstantSInt::get(Type::LongTy, immedValue)
|
|
|
|
: (Value*)ConstantUInt::get(Type::ULongTy,(uint64_t)immedValue);
|
2002-09-16 15:15:57 +00:00
|
|
|
}
|
2001-10-28 21:46:23 +00:00
|
|
|
}
|
2002-09-16 15:15:57 +00:00
|
|
|
|
|
|
|
if (opType == MachineOperand::MO_MachineRegister)
|
|
|
|
minstr->SetMachineOperandReg(op, machineRegNum);
|
|
|
|
else if (opType == MachineOperand::MO_SignExtendedImmed ||
|
2003-06-03 03:18:20 +00:00
|
|
|
opType == MachineOperand::MO_UnextendedImmed) {
|
2002-09-16 15:15:57 +00:00
|
|
|
minstr->SetMachineOperandConst(op, opType, immedValue);
|
2003-06-07 02:34:43 +00:00
|
|
|
// The optype is or has become an immediate
|
|
|
|
// This means we need to change the opcode, e.g. ADDr -> ADDi
|
|
|
|
unsigned newOpcode = convertOpcodeFromRegToImm(opCode);
|
|
|
|
minstr->setOpcode(newOpcode);
|
2003-06-03 03:18:20 +00:00
|
|
|
} else if (constantThatMustBeLoaded ||
|
2002-09-16 15:15:57 +00:00
|
|
|
(opValue && isa<GlobalValue>(opValue)))
|
|
|
|
{ // opValue is a constant that must be explicitly loaded into a reg
|
|
|
|
assert(opValue);
|
|
|
|
TmpInstruction* tmpReg = InsertCodeToLoadConstant(F, opValue, vmInstr,
|
2003-01-15 19:47:53 +00:00
|
|
|
MVec, target);
|
2002-03-18 03:33:43 +00:00
|
|
|
minstr->SetMachineOperandVal(op, MachineOperand::MO_VirtualRegister,
|
|
|
|
tmpReg);
|
2001-10-17 23:57:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-19 15:34:29 +00:00
|
|
|
// Also, check for implicit operands used by the machine instruction
|
|
|
|
// (no need to check those defined since they cannot be constants).
|
|
|
|
// These include:
|
2001-10-17 23:57:50 +00:00
|
|
|
// -- arguments to a Call
|
|
|
|
// -- return value of a Return
|
|
|
|
// Any such operand that is a constant value needs to be fixed also.
|
|
|
|
// The current instructions with implicit refs (viz., Call and Return)
|
|
|
|
// have no immediate fields, so the constant always needs to be loaded
|
|
|
|
// into a register.
|
|
|
|
//
|
2002-09-16 15:15:57 +00:00
|
|
|
bool isCall = instrInfo.isCall(opCode);
|
2002-05-19 15:34:29 +00:00
|
|
|
unsigned lastCallArgNum = 0; // unused if not a call
|
|
|
|
CallArgsDescriptor* argDesc = NULL; // unused if not a call
|
|
|
|
if (isCall)
|
|
|
|
argDesc = CallArgsDescriptor::get(minstr);
|
|
|
|
|
2001-10-17 23:57:50 +00:00
|
|
|
for (unsigned i=0, N=minstr->getNumImplicitRefs(); i < N; ++i)
|
2001-12-03 22:26:30 +00:00
|
|
|
if (isa<Constant>(minstr->getImplicitRef(i)) ||
|
2001-10-17 23:57:50 +00:00
|
|
|
isa<GlobalValue>(minstr->getImplicitRef(i)))
|
|
|
|
{
|
2001-10-28 21:46:23 +00:00
|
|
|
Value* oldVal = minstr->getImplicitRef(i);
|
2001-10-17 23:57:50 +00:00
|
|
|
TmpInstruction* tmpReg =
|
2003-01-15 19:47:53 +00:00
|
|
|
InsertCodeToLoadConstant(F, oldVal, vmInstr, MVec, target);
|
2001-10-17 23:57:50 +00:00
|
|
|
minstr->setImplicitRef(i, tmpReg);
|
2002-05-19 15:34:29 +00:00
|
|
|
|
|
|
|
if (isCall)
|
|
|
|
{ // find and replace the argument in the CallArgsDescriptor
|
|
|
|
unsigned i=lastCallArgNum;
|
|
|
|
while (argDesc->getArgInfo(i).getArgVal() != oldVal)
|
|
|
|
++i;
|
|
|
|
assert(i < argDesc->getNumArgs() &&
|
|
|
|
"Constant operands to a call *must* be in the arg list");
|
|
|
|
lastCallArgNum = i;
|
|
|
|
argDesc->getArgInfo(i).replaceArgVal(tmpReg);
|
|
|
|
}
|
2001-10-17 23:57:50 +00:00
|
|
|
}
|
|
|
|
|
2003-01-15 19:47:53 +00:00
|
|
|
return MVec;
|
2001-10-17 23:57:50 +00:00
|
|
|
}
|