2001-10-10 20:50:43 +00:00
|
|
|
// $Id$ -*-c++-*-
|
|
|
|
//***************************************************************************
|
|
|
|
// File:
|
|
|
|
// InstrSelectionSupport.h
|
|
|
|
//
|
|
|
|
// Purpose:
|
|
|
|
// Target-independent instruction selection code.
|
|
|
|
// See SparcInstrSelection.cpp for usage.
|
|
|
|
//
|
|
|
|
// History:
|
|
|
|
// 10/10/01 - Vikram Adve - Created
|
|
|
|
//**************************************************************************/
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/InstrSelectionSupport.h"
|
|
|
|
#include "llvm/CodeGen/InstrSelection.h"
|
2002-05-19 15:34:29 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrAnnot.h"
|
2002-02-03 07:39:06 +00:00
|
|
|
#include "llvm/CodeGen/MachineCodeForInstruction.h"
|
|
|
|
#include "llvm/CodeGen/MachineCodeForMethod.h"
|
|
|
|
#include "llvm/CodeGen/InstrForest.h"
|
2001-10-10 20:50:43 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/MachineRegInfo.h"
|
2002-04-28 19:55:58 +00:00
|
|
|
#include "llvm/Constants.h"
|
2002-04-07 20:49:59 +00:00
|
|
|
#include "llvm/Function.h"
|
2001-10-28 21:46:23 +00:00
|
|
|
#include "llvm/BasicBlock.h"
|
2001-10-10 20:50:43 +00:00
|
|
|
#include "llvm/Type.h"
|
|
|
|
#include "llvm/iMemory.h"
|
2002-01-20 22:54:45 +00:00
|
|
|
using std::vector;
|
2001-10-10 20:50:43 +00:00
|
|
|
|
|
|
|
//*************************** Local Functions ******************************/
|
|
|
|
|
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,
|
|
|
|
vector<MachineInstr*>& loadConstVec,
|
|
|
|
TargetMachine& target)
|
2001-10-10 20:50:43 +00:00
|
|
|
{
|
2001-10-17 23:57:50 +00:00
|
|
|
vector<TmpInstruction*> tempVec;
|
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-02-03 07:39:06 +00:00
|
|
|
TmpInstruction* tmpReg = new TmpInstruction(opValue);
|
2002-05-19 15:34:29 +00:00
|
|
|
MachineCodeForInstruction &mcfi = MachineCodeForInstruction::get(vmInstr);
|
|
|
|
mcfi.addTemp(tmpReg);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-17 23:57:50 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Function GetConstantValueAsSignedInt
|
|
|
|
//
|
|
|
|
// Convenience function to get the value of an integer constant, for an
|
|
|
|
// appropriate integer or non-integer type that can be held in an integer.
|
|
|
|
// The type of the argument must be the following:
|
|
|
|
// Signed or unsigned integer
|
|
|
|
// Boolean
|
|
|
|
// Pointer
|
|
|
|
//
|
|
|
|
// isValidConstant is set to true if a valid constant was found.
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
int64_t
|
|
|
|
GetConstantValueAsSignedInt(const Value *V,
|
|
|
|
bool &isValidConstant)
|
2001-10-10 20:50:43 +00:00
|
|
|
{
|
2001-12-03 22:26:30 +00:00
|
|
|
if (!isa<Constant>(V))
|
2001-10-10 20:50:43 +00:00
|
|
|
{
|
2001-10-17 23:57:50 +00:00
|
|
|
isValidConstant = false;
|
|
|
|
return 0;
|
2001-10-10 20:50:43 +00:00
|
|
|
}
|
2001-10-17 23:57:50 +00:00
|
|
|
|
|
|
|
isValidConstant = true;
|
|
|
|
|
|
|
|
if (V->getType() == Type::BoolTy)
|
2001-12-03 22:26:30 +00:00
|
|
|
return (int64_t) cast<ConstantBool>(V)->getValue();
|
2001-10-17 23:57:50 +00:00
|
|
|
|
|
|
|
if (V->getType()->isIntegral())
|
2001-10-10 20:50:43 +00:00
|
|
|
{
|
2001-10-17 23:57:50 +00:00
|
|
|
if (V->getType()->isSigned())
|
2001-12-03 22:26:30 +00:00
|
|
|
return cast<ConstantSInt>(V)->getValue();
|
2001-10-17 23:57:50 +00:00
|
|
|
|
|
|
|
assert(V->getType()->isUnsigned());
|
2001-12-03 22:26:30 +00:00
|
|
|
uint64_t Val = cast<ConstantUInt>(V)->getValue();
|
2001-10-17 23:57:50 +00:00
|
|
|
if (Val < INT64_MAX) // then safe to cast to signed
|
|
|
|
return (int64_t)Val;
|
2001-10-10 20:50:43 +00:00
|
|
|
}
|
2002-03-31 18:56:51 +00:00
|
|
|
|
2001-10-17 23:57:50 +00:00
|
|
|
isValidConstant = false;
|
|
|
|
return 0;
|
2001-10-10 20:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Function: FoldGetElemChain
|
|
|
|
//
|
|
|
|
// Purpose:
|
2002-03-24 03:37:53 +00:00
|
|
|
// Fold a chain of GetElementPtr instructions containing only
|
2002-05-19 15:34:29 +00:00
|
|
|
// constant offsets into an equivalent (Pointer, IndexVector) pair.
|
2002-03-24 03:37:53 +00:00
|
|
|
// Returns the pointer Value, and stores the resulting IndexVector
|
|
|
|
// in argument chainIdxVec.
|
2001-10-10 20:50:43 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Value*
|
|
|
|
FoldGetElemChain(const InstructionNode* getElemInstrNode,
|
2001-12-15 00:36:32 +00:00
|
|
|
vector<Value*>& chainIdxVec)
|
2001-10-10 20:50:43 +00:00
|
|
|
{
|
|
|
|
MemAccessInst* getElemInst = (MemAccessInst*)
|
|
|
|
getElemInstrNode->getInstruction();
|
|
|
|
|
2002-03-31 18:56:51 +00:00
|
|
|
// Return NULL if we don't fold any instructions in.
|
2002-03-24 03:37:53 +00:00
|
|
|
Value* ptrVal = NULL;
|
2002-05-19 15:34:29 +00:00
|
|
|
|
|
|
|
// Remember if the last instruction had a leading [0] index.
|
|
|
|
bool hasLeadingZero = false;
|
2001-10-10 20:50:43 +00:00
|
|
|
|
2002-03-24 03:37:53 +00:00
|
|
|
// Now chase the chain of getElementInstr instructions, if any.
|
2002-05-19 15:34:29 +00:00
|
|
|
// Check for any non-constant indices and stop there.
|
2002-03-24 03:37:53 +00:00
|
|
|
//
|
|
|
|
const InstrTreeNode* ptrChild = getElemInstrNode;
|
2001-10-10 20:50:43 +00:00
|
|
|
while (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
|
|
|
|
ptrChild->getOpLabel() == GetElemPtrIdx)
|
|
|
|
{
|
|
|
|
// Child is a GetElemPtr instruction
|
|
|
|
getElemInst = (MemAccessInst*)
|
|
|
|
((InstructionNode*) ptrChild)->getInstruction();
|
2001-12-15 00:36:32 +00:00
|
|
|
const vector<Value*>& idxVec = getElemInst->copyIndices();
|
2002-05-19 15:34:29 +00:00
|
|
|
bool allConstantOffsets = true;
|
2001-10-10 20:50:43 +00:00
|
|
|
|
2002-05-19 15:34:29 +00:00
|
|
|
// Check for a leading [0] index, if any. It will be discarded later.
|
|
|
|
ConstantUInt* CV = dyn_cast<ConstantUInt>(idxVec[0]);
|
|
|
|
hasLeadingZero = bool(CV && CV->getType() == Type::UIntTy &&
|
|
|
|
(CV->getValue() == 0));
|
2002-03-24 03:37:53 +00:00
|
|
|
|
2002-05-19 15:34:29 +00:00
|
|
|
// Check that all offsets are constant for this instruction
|
|
|
|
for (unsigned int i=0; i < idxVec.size(); i++)
|
|
|
|
if (! isa<ConstantUInt>(idxVec[i]))
|
|
|
|
{
|
|
|
|
allConstantOffsets = false;
|
|
|
|
break;
|
|
|
|
}
|
2002-03-24 03:37:53 +00:00
|
|
|
|
2002-05-19 15:34:29 +00:00
|
|
|
if (allConstantOffsets)
|
2002-03-31 18:56:51 +00:00
|
|
|
{ // Get pointer value out of ptrChild.
|
2002-03-24 03:37:53 +00:00
|
|
|
ptrVal = getElemInst->getPointerOperand();
|
2002-05-19 15:34:29 +00:00
|
|
|
|
|
|
|
// Insert its index vector at the start.
|
|
|
|
chainIdxVec.insert(chainIdxVec.begin(),
|
|
|
|
idxVec.begin() + (hasLeadingZero? 1:0),
|
|
|
|
idxVec.end());
|
2002-03-31 18:56:51 +00:00
|
|
|
|
|
|
|
// Mark the folded node so no code is generated for it.
|
2002-03-24 03:37:53 +00:00
|
|
|
((InstructionNode*) ptrChild)->markFoldedIntoParent();
|
|
|
|
}
|
|
|
|
else // cannot fold this getElementPtr instr. or any further ones
|
|
|
|
break;
|
2001-10-10 20:50:43 +00:00
|
|
|
|
|
|
|
ptrChild = ptrChild->leftChild();
|
|
|
|
}
|
|
|
|
|
2002-05-19 15:34:29 +00:00
|
|
|
// If the first getElementPtr instruction had a leading [0], add it back.
|
|
|
|
// Note that this instruction is the *last* one handled above.
|
|
|
|
if (hasLeadingZero)
|
|
|
|
chainIdxVec.insert(chainIdxVec.begin(), ConstantUInt::get(Type::UIntTy,0));
|
|
|
|
|
2001-10-10 20:50:43 +00:00
|
|
|
return ptrVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Function Set2OperandsFromInstr
|
|
|
|
// Function Set3OperandsFromInstr
|
|
|
|
//
|
|
|
|
// For the common case of 2- and 3-operand arithmetic/logical instructions,
|
|
|
|
// set the m/c instr. operands directly from the VM instruction's operands.
|
|
|
|
// Check whether the first or second operand is 0 and can use a dedicated "0"
|
|
|
|
// register.
|
|
|
|
// Check whether the second operand should use an immediate field or register.
|
|
|
|
// (First and third operands are never immediates for such instructions.)
|
|
|
|
//
|
|
|
|
// Arguments:
|
|
|
|
// canDiscardResult: Specifies that the result operand can be discarded
|
|
|
|
// by using the dedicated "0"
|
|
|
|
//
|
|
|
|
// op1position, op2position and resultPosition: Specify in which position
|
|
|
|
// in the machine instruction the 3 operands (arg1, arg2
|
|
|
|
// and result) should go.
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
Set2OperandsFromInstr(MachineInstr* minstr,
|
|
|
|
InstructionNode* vmInstrNode,
|
|
|
|
const TargetMachine& target,
|
|
|
|
bool canDiscardResult,
|
|
|
|
int op1Position,
|
|
|
|
int resultPosition)
|
|
|
|
{
|
|
|
|
Set3OperandsFromInstr(minstr, vmInstrNode, target,
|
|
|
|
canDiscardResult, op1Position,
|
|
|
|
/*op2Position*/ -1, resultPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Set3OperandsFromInstr(MachineInstr* minstr,
|
|
|
|
InstructionNode* vmInstrNode,
|
|
|
|
const TargetMachine& target,
|
|
|
|
bool canDiscardResult,
|
|
|
|
int op1Position,
|
|
|
|
int op2Position,
|
|
|
|
int resultPosition)
|
|
|
|
{
|
|
|
|
assert(op1Position >= 0);
|
|
|
|
assert(resultPosition >= 0);
|
|
|
|
|
|
|
|
// operand 1
|
2002-03-18 03:33:43 +00:00
|
|
|
minstr->SetMachineOperandVal(op1Position, MachineOperand::MO_VirtualRegister,
|
2001-10-10 20:50:43 +00:00
|
|
|
vmInstrNode->leftChild()->getValue());
|
|
|
|
|
|
|
|
// operand 2 (if any)
|
|
|
|
if (op2Position >= 0)
|
2002-03-18 03:33:43 +00:00
|
|
|
minstr->SetMachineOperandVal(op2Position, MachineOperand::MO_VirtualRegister,
|
2001-10-10 20:50:43 +00:00
|
|
|
vmInstrNode->rightChild()->getValue());
|
|
|
|
|
|
|
|
// result operand: if it can be discarded, use a dead register if one exists
|
|
|
|
if (canDiscardResult && target.getRegInfo().getZeroRegNum() >= 0)
|
2002-03-18 03:33:43 +00:00
|
|
|
minstr->SetMachineOperandReg(resultPosition,
|
2001-10-10 20:50:43 +00:00
|
|
|
target.getRegInfo().getZeroRegNum());
|
|
|
|
else
|
2002-03-18 03:33:43 +00:00
|
|
|
minstr->SetMachineOperandVal(resultPosition,
|
2001-10-10 20:50:43 +00:00
|
|
|
MachineOperand::MO_VirtualRegister, vmInstrNode->getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MachineOperand::MachineOperandType
|
|
|
|
ChooseRegOrImmed(Value* val,
|
|
|
|
MachineOpCode opCode,
|
|
|
|
const TargetMachine& target,
|
|
|
|
bool canUseImmed,
|
|
|
|
unsigned int& getMachineRegNum,
|
|
|
|
int64_t& getImmedValue)
|
|
|
|
{
|
|
|
|
MachineOperand::MachineOperandType opType =
|
|
|
|
MachineOperand::MO_VirtualRegister;
|
|
|
|
getMachineRegNum = 0;
|
|
|
|
getImmedValue = 0;
|
|
|
|
|
|
|
|
// Check for the common case first: argument is not constant
|
|
|
|
//
|
2001-12-03 22:26:30 +00:00
|
|
|
Constant *CPV = dyn_cast<Constant>(val);
|
2001-10-10 20:50:43 +00:00
|
|
|
if (!CPV) return opType;
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
if (ConstantBool *CPB = dyn_cast<ConstantBool>(CPV))
|
2001-10-10 20:50:43 +00:00
|
|
|
{
|
|
|
|
if (!CPB->getValue() && target.getRegInfo().getZeroRegNum() >= 0)
|
|
|
|
{
|
|
|
|
getMachineRegNum = target.getRegInfo().getZeroRegNum();
|
|
|
|
return MachineOperand::MO_MachineRegister;
|
|
|
|
}
|
|
|
|
|
|
|
|
getImmedValue = 1;
|
|
|
|
return MachineOperand::MO_SignExtendedImmed;
|
|
|
|
}
|
|
|
|
|
2001-11-14 17:24:49 +00:00
|
|
|
// Otherwise it needs to be an integer or a NULL pointer
|
|
|
|
if (! CPV->getType()->isIntegral() &&
|
2002-05-06 16:15:30 +00:00
|
|
|
! (isa<PointerType>(CPV->getType()) &&
|
2001-11-14 17:24:49 +00:00
|
|
|
CPV->isNullValue()))
|
|
|
|
return opType;
|
|
|
|
|
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()))
|
2001-11-14 17:24:49 +00:00
|
|
|
{
|
|
|
|
intValue = 0;
|
|
|
|
}
|
2001-11-14 17:55:02 +00:00
|
|
|
else if (CPV->getType()->isSigned())
|
2001-10-10 20:50:43 +00:00
|
|
|
{
|
2001-12-03 22:26:30 +00:00
|
|
|
intValue = cast<ConstantSInt>(CPV)->getValue();
|
2001-10-10 20:50:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-12-03 22:26:30 +00:00
|
|
|
uint64_t V = cast<ConstantUInt>(CPV)->getValue();
|
2001-10-10 20:50:43 +00:00
|
|
|
if (V >= INT64_MAX) return opType;
|
|
|
|
intValue = (int64_t)V;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0)
|
|
|
|
{
|
|
|
|
opType = MachineOperand::MO_MachineRegister;
|
|
|
|
getMachineRegNum = target.getRegInfo().getZeroRegNum();
|
|
|
|
}
|
|
|
|
else if (canUseImmed &&
|
|
|
|
target.getInstrInfo().constantFitsInImmedField(opCode, intValue))
|
|
|
|
{
|
2002-05-19 15:34:29 +00:00
|
|
|
opType = CPV->getType()->isSigned()
|
|
|
|
? MachineOperand::MO_SignExtendedImmed
|
|
|
|
: MachineOperand::MO_UnextendedImmed;
|
2001-10-10 20:50:43 +00:00
|
|
|
getImmedValue = intValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return opType;
|
|
|
|
}
|
|
|
|
|
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'.
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
vector<MachineInstr*>
|
|
|
|
FixConstantOperandsForInstr(Instruction* vmInstr,
|
|
|
|
MachineInstr* minstr,
|
|
|
|
TargetMachine& target)
|
|
|
|
{
|
|
|
|
vector<MachineInstr*> loadConstVec;
|
|
|
|
|
|
|
|
const MachineInstrDescriptor& instrDesc =
|
|
|
|
target.getInstrInfo().getDescriptor(minstr->getOpCode());
|
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
Function *F = vmInstr->getParent()->getParent();
|
2001-10-28 21:46:23 +00:00
|
|
|
|
2001-10-17 23:57:50 +00:00
|
|
|
for (unsigned op=0; op < minstr->getNumOperands(); op++)
|
|
|
|
{
|
|
|
|
const MachineOperand& mop = minstr->getOperand(op);
|
|
|
|
|
|
|
|
// skip the result position (for efficiency below) and any other
|
|
|
|
// positions already marked as not a virtual register
|
|
|
|
if (instrDesc.resultPos == (int) op ||
|
|
|
|
mop.getOperandType() != MachineOperand::MO_VirtualRegister ||
|
|
|
|
mop.getVRegValue() == NULL)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value* opValue = mop.getVRegValue();
|
|
|
|
bool constantThatMustBeLoaded = false;
|
|
|
|
|
2002-03-18 03:33:43 +00:00
|
|
|
if (Constant *opConst = dyn_cast<Constant>(opValue))
|
|
|
|
{
|
2001-10-17 23:57:50 +00:00
|
|
|
unsigned int machineRegNum;
|
|
|
|
int64_t immedValue;
|
|
|
|
MachineOperand::MachineOperandType opType =
|
|
|
|
ChooseRegOrImmed(opValue, minstr->getOpCode(), target,
|
2002-03-18 03:33:43 +00:00
|
|
|
(target.getInstrInfo().getImmedConstantPos(minstr->getOpCode()) == (int) op),
|
2001-10-17 23:57:50 +00:00
|
|
|
machineRegNum, immedValue);
|
2001-11-14 18:49:45 +00:00
|
|
|
|
2001-10-17 23:57:50 +00:00
|
|
|
if (opType == MachineOperand::MO_MachineRegister)
|
2002-03-18 03:33:43 +00:00
|
|
|
minstr->SetMachineOperandReg(op, machineRegNum);
|
2001-10-17 23:57:50 +00:00
|
|
|
else if (opType == MachineOperand::MO_VirtualRegister)
|
|
|
|
constantThatMustBeLoaded = true; // load is generated below
|
|
|
|
else
|
2002-03-18 03:33:43 +00:00
|
|
|
minstr->SetMachineOperandConst(op, opType, immedValue);
|
2001-10-28 21:46:23 +00:00
|
|
|
}
|
|
|
|
|
2001-10-17 23:57:50 +00:00
|
|
|
if (constantThatMustBeLoaded || isa<GlobalValue>(opValue))
|
|
|
|
{ // opValue is a constant that must be explicitly loaded into a reg.
|
2002-05-19 15:34:29 +00:00
|
|
|
TmpInstruction* tmpReg = InsertCodeToLoadConstant(F, opValue,vmInstr,
|
2002-04-07 20:49:59 +00:00
|
|
|
loadConstVec,
|
|
|
|
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-05-19 15:34:29 +00:00
|
|
|
bool isCall = target.getInstrInfo().isCall(minstr->getOpCode());
|
|
|
|
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 =
|
2002-04-07 20:49:59 +00:00
|
|
|
InsertCodeToLoadConstant(F, oldVal, vmInstr, loadConstVec, 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
|
|
|
}
|
|
|
|
|
|
|
|
return loadConstVec;
|
|
|
|
}
|
|
|
|
|
|
|
|
|