2002-08-10 04:08:06 +08:00
|
|
|
//===-- MachineInstr.cpp --------------------------------------------------===//
|
2001-07-21 20:41:50 +08:00
|
|
|
//
|
2002-08-10 04:08:06 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-07-21 20:41:50 +08:00
|
|
|
|
2001-09-08 01:18:30 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2002-02-03 15:46:01 +08:00
|
|
|
#include "llvm/Value.h"
|
2002-10-28 10:28:39 +08:00
|
|
|
#include "llvm/Target/MachineInstrInfo.h" // FIXME: shouldn't need this!
|
2002-01-21 06:54:45 +08:00
|
|
|
using std::cerr;
|
2001-08-29 07:02:39 +08:00
|
|
|
|
2002-10-30 01:40:30 +08:00
|
|
|
// Global variable holding an array of descriptors for machine instructions.
|
|
|
|
// The actual object needs to be created separately for each target machine.
|
|
|
|
// This variable is initialized and reset by class MachineInstrInfo.
|
|
|
|
//
|
|
|
|
// FIXME: This should be a property of the target so that more than one target
|
|
|
|
// at a time can be active...
|
|
|
|
//
|
|
|
|
extern const MachineInstrDescriptor *TargetInstrDescriptors;
|
2001-10-19 06:40:02 +08:00
|
|
|
|
2001-08-01 05:49:28 +08:00
|
|
|
// Constructor for instructions with fixed #operands (nearly all)
|
2002-10-29 04:59:49 +08:00
|
|
|
MachineInstr::MachineInstr(MachineOpCode _opCode)
|
2002-10-29 05:17:20 +08:00
|
|
|
: opCode(_opCode),
|
2002-10-30 03:41:18 +08:00
|
|
|
operands(TargetInstrDescriptors[_opCode].numOperands, MachineOperand()),
|
|
|
|
numImplicitRefs(0)
|
|
|
|
{
|
2001-08-01 05:49:28 +08:00
|
|
|
assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constructor for instructions with variable #operands
|
2002-10-29 05:02:40 +08:00
|
|
|
MachineInstr::MachineInstr(MachineOpCode OpCode, unsigned numOperands)
|
2002-10-30 03:41:18 +08:00
|
|
|
: opCode(OpCode),
|
|
|
|
operands(numOperands, MachineOperand()),
|
|
|
|
numImplicitRefs(0)
|
|
|
|
{
|
2002-10-29 04:48:39 +08:00
|
|
|
}
|
|
|
|
|
2002-10-29 04:59:49 +08:00
|
|
|
MachineInstr::MachineInstr(MachineOpCode Opcode, unsigned numOperands,
|
2002-10-30 03:41:18 +08:00
|
|
|
bool XX, bool YY)
|
|
|
|
: opCode(Opcode),
|
|
|
|
numImplicitRefs(0)
|
|
|
|
{
|
2002-10-29 04:59:49 +08:00
|
|
|
operands.reserve(numOperands);
|
|
|
|
}
|
|
|
|
|
2002-10-29 04:48:39 +08:00
|
|
|
// OperandComplete - Return true if it's illegal to add a new operand
|
2002-10-30 03:41:18 +08:00
|
|
|
bool MachineInstr::OperandsComplete() const
|
|
|
|
{
|
2002-10-29 04:48:39 +08:00
|
|
|
int NumOperands = TargetInstrDescriptors[opCode].numOperands;
|
2002-10-30 03:41:18 +08:00
|
|
|
if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands)
|
2002-10-29 04:48:39 +08:00
|
|
|
return true; // Broken!
|
|
|
|
return false;
|
2001-07-21 20:41:50 +08:00
|
|
|
}
|
|
|
|
|
2002-10-29 04:48:39 +08:00
|
|
|
|
2002-09-20 08:47:49 +08:00
|
|
|
//
|
|
|
|
// Support for replacing opcode and operands of a MachineInstr in place.
|
|
|
|
// This only resets the size of the operand vector and initializes it.
|
|
|
|
// The new operands must be set explicitly later.
|
|
|
|
//
|
2002-10-30 03:41:18 +08:00
|
|
|
void MachineInstr::replace(MachineOpCode Opcode, unsigned numOperands)
|
|
|
|
{
|
|
|
|
assert(getNumImplicitRefs() == 0 &&
|
|
|
|
"This is probably broken because implicit refs are going to be lost.");
|
2002-10-29 04:48:39 +08:00
|
|
|
opCode = Opcode;
|
2002-09-20 08:47:49 +08:00
|
|
|
operands.clear();
|
2002-10-29 04:48:39 +08:00
|
|
|
operands.resize(numOperands, MachineOperand());
|
2002-09-20 08:47:49 +08:00
|
|
|
}
|
|
|
|
|
2001-07-21 20:41:50 +08:00
|
|
|
void
|
2002-10-29 04:48:39 +08:00
|
|
|
MachineInstr::SetMachineOperandVal(unsigned i,
|
2002-07-09 06:38:45 +08:00
|
|
|
MachineOperand::MachineOperandType opType,
|
2002-10-28 12:24:49 +08:00
|
|
|
Value* V,
|
2002-07-25 14:17:51 +08:00
|
|
|
bool isdef,
|
|
|
|
bool isDefAndUse)
|
2001-07-21 20:41:50 +08:00
|
|
|
{
|
2002-10-30 03:41:18 +08:00
|
|
|
assert(i < operands.size()); // may be explicit or implicit op
|
2002-10-28 12:24:49 +08:00
|
|
|
operands[i].opType = opType;
|
|
|
|
operands[i].value = V;
|
|
|
|
operands[i].regNum = -1;
|
|
|
|
operands[i].flags = 0;
|
|
|
|
|
2002-07-11 05:45:04 +08:00
|
|
|
if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
|
|
|
|
operands[i].markDef();
|
|
|
|
if (isDefAndUse)
|
|
|
|
operands[i].markDefAndUse();
|
2001-07-21 20:41:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-10-28 12:24:49 +08:00
|
|
|
MachineInstr::SetMachineOperandConst(unsigned i,
|
2001-07-21 20:41:50 +08:00
|
|
|
MachineOperand::MachineOperandType operandType,
|
2002-03-18 11:35:24 +08:00
|
|
|
int64_t intValue)
|
2001-07-21 20:41:50 +08:00
|
|
|
{
|
2002-10-30 03:41:18 +08:00
|
|
|
assert(i < getNumOperands()); // must be explicit op
|
2002-03-18 11:35:24 +08:00
|
|
|
assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
|
|
|
|
"immed. constant cannot be defined");
|
2002-10-28 12:24:49 +08:00
|
|
|
|
|
|
|
operands[i].opType = operandType;
|
|
|
|
operands[i].value = NULL;
|
|
|
|
operands[i].immedVal = intValue;
|
|
|
|
operands[i].regNum = -1;
|
|
|
|
operands[i].flags = 0;
|
2001-07-21 20:41:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-10-28 12:24:49 +08:00
|
|
|
MachineInstr::SetMachineOperandReg(unsigned i,
|
2002-03-18 11:35:24 +08:00
|
|
|
int regNum,
|
2002-10-29 03:46:59 +08:00
|
|
|
bool isdef) {
|
2002-10-30 03:41:18 +08:00
|
|
|
assert(i < getNumOperands()); // must be explicit op
|
2002-10-28 12:24:49 +08:00
|
|
|
|
2002-10-29 03:46:59 +08:00
|
|
|
operands[i].opType = MachineOperand::MO_MachineRegister;
|
2002-10-28 12:24:49 +08:00
|
|
|
operands[i].value = NULL;
|
|
|
|
operands[i].regNum = regNum;
|
|
|
|
operands[i].flags = 0;
|
|
|
|
|
2002-07-11 05:45:04 +08:00
|
|
|
if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
|
|
|
|
operands[i].markDef();
|
2002-10-23 07:16:21 +08:00
|
|
|
insertUsedReg(regNum);
|
2001-07-21 20:41:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-07-09 06:38:45 +08:00
|
|
|
MachineInstr::SetRegForOperand(unsigned i, int regNum)
|
2001-07-21 20:41:50 +08:00
|
|
|
{
|
2002-10-30 03:41:18 +08:00
|
|
|
assert(i < getNumOperands()); // must be explicit op
|
2002-07-09 06:38:45 +08:00
|
|
|
operands[i].setRegForValue(regNum);
|
2002-10-23 07:16:21 +08:00
|
|
|
insertUsedReg(regNum);
|
2002-07-09 06:38:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-15 00:52:58 +08:00
|
|
|
// Subsitute all occurrences of Value* oldVal with newVal in all operands
|
|
|
|
// and all implicit refs. If defsOnly == true, substitute defs only.
|
|
|
|
unsigned
|
|
|
|
MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
|
|
|
|
{
|
|
|
|
unsigned numSubst = 0;
|
|
|
|
|
|
|
|
// Subsitute operands
|
|
|
|
for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
|
|
|
|
if (*O == oldVal)
|
|
|
|
if (!defsOnly || O.isDef())
|
|
|
|
{
|
|
|
|
O.getMachineOperand().value = newVal;
|
|
|
|
++numSubst;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subsitute implicit refs
|
2002-10-30 03:41:18 +08:00
|
|
|
for (unsigned i=0, N=getNumImplicitRefs(); i < N; ++i)
|
2002-10-23 07:16:21 +08:00
|
|
|
if (getImplicitRef(i) == oldVal)
|
2002-08-15 00:52:58 +08:00
|
|
|
if (!defsOnly || implicitRefIsDefined(i))
|
|
|
|
{
|
2002-10-30 03:41:18 +08:00
|
|
|
getImplicitOp(i).value = newVal;
|
2002-08-15 00:52:58 +08:00
|
|
|
++numSubst;
|
|
|
|
}
|
|
|
|
|
|
|
|
return numSubst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-09 06:38:45 +08:00
|
|
|
void
|
|
|
|
MachineInstr::dump() const
|
|
|
|
{
|
|
|
|
cerr << " " << *this;
|
2001-07-21 20:41:50 +08:00
|
|
|
}
|
|
|
|
|
2002-09-16 23:18:53 +08:00
|
|
|
static inline std::ostream&
|
|
|
|
OutputValue(std::ostream &os, const Value* val)
|
2002-04-25 12:31:18 +08:00
|
|
|
{
|
|
|
|
os << "(val ";
|
|
|
|
if (val && val->hasName())
|
2002-07-11 05:45:04 +08:00
|
|
|
return os << val->getName() << ")";
|
2002-04-25 12:31:18 +08:00
|
|
|
else
|
2002-07-11 05:45:04 +08:00
|
|
|
return os << (void*) val << ")"; // print address only
|
2002-04-25 12:31:18 +08:00
|
|
|
}
|
|
|
|
|
2002-09-16 23:18:53 +08:00
|
|
|
static inline std::ostream&
|
|
|
|
OutputReg(std::ostream &os, unsigned int regNum)
|
|
|
|
{
|
|
|
|
return os << "%mreg(" << regNum << ")";
|
|
|
|
}
|
|
|
|
|
2002-01-21 06:54:45 +08:00
|
|
|
std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
|
2001-07-21 20:41:50 +08:00
|
|
|
{
|
2002-10-30 01:35:39 +08:00
|
|
|
os << TargetInstrDescriptors[minstr.opCode].Name;
|
2001-07-21 20:41:50 +08:00
|
|
|
|
2001-11-15 04:05:23 +08:00
|
|
|
for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
|
2001-07-21 20:41:50 +08:00
|
|
|
os << "\t" << minstr.getOperand(i);
|
2002-07-09 06:38:45 +08:00
|
|
|
if( minstr.operandIsDefined(i) )
|
|
|
|
os << "*";
|
|
|
|
if( minstr.operandIsDefinedAndUsed(i) )
|
2001-11-16 04:46:40 +08:00
|
|
|
os << "*";
|
2001-11-15 04:05:23 +08:00
|
|
|
}
|
2001-07-21 20:41:50 +08:00
|
|
|
|
2001-10-19 06:40:02 +08:00
|
|
|
// code for printing implict references
|
|
|
|
unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
|
|
|
|
if( NumOfImpRefs > 0 ) {
|
2002-04-25 12:31:18 +08:00
|
|
|
os << "\tImplicit: ";
|
2001-10-19 06:40:02 +08:00
|
|
|
for(unsigned z=0; z < NumOfImpRefs; z++) {
|
2002-04-25 12:31:18 +08:00
|
|
|
OutputValue(os, minstr.getImplicitRef(z));
|
2001-11-15 04:05:23 +08:00
|
|
|
if( minstr.implicitRefIsDefined(z)) os << "*";
|
2002-07-09 06:38:45 +08:00
|
|
|
if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
|
2001-11-16 04:46:40 +08:00
|
|
|
os << "\t";
|
2001-10-19 06:40:02 +08:00
|
|
|
}
|
|
|
|
}
|
2002-04-25 12:31:18 +08:00
|
|
|
|
2002-01-21 06:54:45 +08:00
|
|
|
return os << "\n";
|
2001-07-21 20:41:50 +08:00
|
|
|
}
|
|
|
|
|
2002-01-21 06:54:45 +08:00
|
|
|
std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
|
2001-09-18 20:56:28 +08:00
|
|
|
{
|
2002-07-11 05:45:04 +08:00
|
|
|
if (mop.opHiBits32())
|
|
|
|
os << "%lm(";
|
|
|
|
else if (mop.opLoBits32())
|
|
|
|
os << "%lo(";
|
|
|
|
else if (mop.opHiBits64())
|
|
|
|
os << "%hh(";
|
|
|
|
else if (mop.opLoBits64())
|
|
|
|
os << "%hm(";
|
|
|
|
|
2001-09-18 20:56:28 +08:00
|
|
|
switch(mop.opType)
|
|
|
|
{
|
|
|
|
case MachineOperand::MO_VirtualRegister:
|
|
|
|
os << "%reg";
|
2002-07-11 05:45:04 +08:00
|
|
|
OutputValue(os, mop.getVRegValue());
|
2002-09-16 23:18:53 +08:00
|
|
|
if (mop.hasAllocatedReg())
|
|
|
|
os << "==" << OutputReg(os, mop.getAllocatedRegNum());
|
2002-07-11 05:45:04 +08:00
|
|
|
break;
|
2001-09-18 20:56:28 +08:00
|
|
|
case MachineOperand::MO_CCRegister:
|
|
|
|
os << "%ccreg";
|
2002-07-11 05:45:04 +08:00
|
|
|
OutputValue(os, mop.getVRegValue());
|
2002-09-16 23:18:53 +08:00
|
|
|
if (mop.hasAllocatedReg())
|
|
|
|
os << "==" << OutputReg(os, mop.getAllocatedRegNum());
|
2002-07-11 05:45:04 +08:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_MachineRegister:
|
2002-09-16 23:18:53 +08:00
|
|
|
OutputReg(os, mop.getMachineRegNum());
|
2002-07-11 05:45:04 +08:00
|
|
|
break;
|
2001-09-18 20:56:28 +08:00
|
|
|
case MachineOperand::MO_SignExtendedImmed:
|
2002-07-11 05:45:04 +08:00
|
|
|
os << (long)mop.immedVal;
|
|
|
|
break;
|
2001-09-18 20:56:28 +08:00
|
|
|
case MachineOperand::MO_UnextendedImmed:
|
2002-07-11 05:45:04 +08:00
|
|
|
os << (long)mop.immedVal;
|
|
|
|
break;
|
2001-09-18 20:56:28 +08:00
|
|
|
case MachineOperand::MO_PCRelativeDisp:
|
2001-10-01 07:44:19 +08:00
|
|
|
{
|
|
|
|
const Value* opVal = mop.getVRegValue();
|
2002-04-09 06:01:15 +08:00
|
|
|
bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
|
2001-11-12 22:19:47 +08:00
|
|
|
os << "%disp(" << (isLabel? "label " : "addr-of-val ");
|
|
|
|
if (opVal->hasName())
|
2002-01-21 06:54:45 +08:00
|
|
|
os << opVal->getName();
|
2001-11-12 22:19:47 +08:00
|
|
|
else
|
2002-07-09 06:38:45 +08:00
|
|
|
os << (const void*) opVal;
|
2002-07-11 05:45:04 +08:00
|
|
|
os << ")";
|
|
|
|
break;
|
2001-10-01 07:44:19 +08:00
|
|
|
}
|
2001-09-18 20:56:28 +08:00
|
|
|
default:
|
|
|
|
assert(0 && "Unrecognized operand type");
|
|
|
|
break;
|
|
|
|
}
|
2001-09-10 06:26:29 +08:00
|
|
|
|
2002-07-11 05:45:04 +08:00
|
|
|
if (mop.flags &
|
|
|
|
(MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
|
|
|
|
MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))
|
|
|
|
os << ")";
|
|
|
|
|
2001-07-21 20:41:50 +08:00
|
|
|
return os;
|
|
|
|
}
|