2002-08-09 20:08:06 +00:00
|
|
|
//===-- InstrSelectionSupport.cpp -----------------------------------------===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-08-09 20:08:06 +00:00
|
|
|
//
|
|
|
|
// 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"
|
2004-01-09 06:22:34 +00:00
|
|
|
#include "../SparcInstrSelectionSupport.h"
|
2001-10-10 20:50:43 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
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-10-23 17:39:37 +00:00
|
|
|
target.getInstrInfo().constantFitsInImmedField(opCode, intValue)) {
|
2002-09-16 15:15:57 +00:00
|
|
|
opType = isSigned? MachineOperand::MO_SignExtendedImmed
|
|
|
|
: MachineOperand::MO_UnextendedImmed;
|
|
|
|
getImmedValue = intValue;
|
2003-10-23 17:39:37 +00:00
|
|
|
} else if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0) {
|
|
|
|
opType = MachineOperand::MO_MachineRegister;
|
|
|
|
getMachineRegNum = target.getRegInfo().getZeroRegNum();
|
|
|
|
}
|
2002-09-16 15:15:57 +00:00
|
|
|
|
|
|
|
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
|
2003-07-29 19:50:12 +00:00
|
|
|
// TargetInstrInfo::ConvertConstantToIntType() does the right conversions:
|
|
|
|
bool isValidConstant;
|
|
|
|
uint64_t valueToUse =
|
|
|
|
target.getInstrInfo().ConvertConstantToIntType(target, val, val->getType(),
|
|
|
|
isValidConstant);
|
|
|
|
if (! isValidConstant)
|
2002-09-16 15:15:57 +00:00
|
|
|
return MachineOperand::MO_VirtualRegister;
|
|
|
|
|
2003-07-29 19:50:12 +00:00
|
|
|
// Now check if the constant value fits in the IMMED field.
|
2001-10-10 20:50:43 +00:00
|
|
|
//
|
2003-07-29 19:50:12 +00:00
|
|
|
return ChooseRegOrImmed((int64_t) valueToUse, val->getType()->isSigned(),
|
2002-09-16 15:15:57 +00:00
|
|
|
opCode, target, canUseImmed,
|
|
|
|
getMachineRegNum, getImmedValue);
|
2001-10-10 20:50:43 +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
|
2003-10-23 17:39:37 +00:00
|
|
|
if (mop.getType() == MachineOperand::MO_VirtualRegister) {
|
|
|
|
assert(mop.getVRegValue() != NULL);
|
|
|
|
opValue = mop.getVRegValue();
|
|
|
|
if (Constant *opConst = dyn_cast<Constant>(opValue)) {
|
|
|
|
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
|
|
|
}
|
2003-10-23 17:39:37 +00:00
|
|
|
} else {
|
2003-12-10 22:51:41 +00:00
|
|
|
//
|
|
|
|
// If the operand is from the constant pool, don't try to change it.
|
|
|
|
//
|
|
|
|
if (mop.getType() == MachineOperand::MO_ConstantPoolIndex) {
|
|
|
|
continue;
|
|
|
|
}
|
2003-10-23 17:39:37 +00:00
|
|
|
assert(mop.isImmediate());
|
|
|
|
bool isSigned = mop.getType() == MachineOperand::MO_SignExtendedImmed;
|
2002-09-16 15:15:57 +00:00
|
|
|
|
2003-10-23 17:39:37 +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.
|
2003-12-14 13:24:17 +00:00
|
|
|
if (mop.isHiBits32() || mop.isLoBits32() ||
|
|
|
|
mop.isHiBits64() || mop.isLoBits64())
|
2003-10-23 17:39:37 +00:00
|
|
|
continue;
|
2002-09-16 15:15:57 +00:00
|
|
|
|
2003-10-23 17:39:37 +00:00
|
|
|
opType = ChooseRegOrImmed(mop.getImmedValue(), isSigned,
|
|
|
|
opCode, target, (immedPos == (int)op),
|
|
|
|
machineRegNum, immedValue);
|
2002-09-16 15:15:57 +00:00
|
|
|
|
2003-10-23 17:39:37 +00:00
|
|
|
if (opType == MachineOperand::MO_SignExtendedImmed ||
|
|
|
|
opType == MachineOperand::MO_UnextendedImmed) {
|
|
|
|
// 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);
|
|
|
|
}
|
2003-06-04 04:54:06 +00:00
|
|
|
|
2003-10-23 17:39:37 +00:00
|
|
|
if (opType == mop.getType())
|
|
|
|
continue; // no change: this is the most common case
|
2002-09-16 15:15:57 +00:00
|
|
|
|
2003-10-23 17:39:37 +00:00
|
|
|
if (opType == MachineOperand::MO_VirtualRegister) {
|
|
|
|
constantThatMustBeLoaded = true;
|
|
|
|
opValue = isSigned
|
|
|
|
? (Value*)ConstantSInt::get(Type::LongTy, immedValue)
|
|
|
|
: (Value*)ConstantUInt::get(Type::ULongTy,(uint64_t)immedValue);
|
2001-10-28 21:46:23 +00:00
|
|
|
}
|
2003-10-23 17:39:37 +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
|
|
|
|
2003-10-23 17:39:37 +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
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|
|
|
|
} // End llvm namespace
|