2002-08-09 20:08:06 +00:00
|
|
|
//===-- MachineInstr.cpp --------------------------------------------------===//
|
2001-07-21 12:41:50 +00:00
|
|
|
//
|
2002-08-09 20:08:06 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-07-21 12:41:50 +00:00
|
|
|
|
2001-09-07 17:18:30 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2002-02-03 07:46:01 +00:00
|
|
|
#include "llvm/Value.h"
|
2002-01-20 22:54:45 +00:00
|
|
|
using std::cerr;
|
2001-08-28 23:02:39 +00:00
|
|
|
|
2001-10-18 22:40:02 +00:00
|
|
|
|
2001-07-31 21:49:28 +00:00
|
|
|
// Constructor for instructions with fixed #operands (nearly all)
|
2001-07-21 12:41:50 +00:00
|
|
|
MachineInstr::MachineInstr(MachineOpCode _opCode,
|
|
|
|
OpCodeMask _opCodeMask)
|
|
|
|
: opCode(_opCode),
|
|
|
|
opCodeMask(_opCodeMask),
|
2001-07-28 04:06:37 +00:00
|
|
|
operands(TargetInstrDescriptors[_opCode].numOperands)
|
2001-07-31 21:49:28 +00:00
|
|
|
{
|
|
|
|
assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constructor for instructions with variable #operands
|
|
|
|
MachineInstr::MachineInstr(MachineOpCode _opCode,
|
|
|
|
unsigned numOperands,
|
|
|
|
OpCodeMask _opCodeMask)
|
|
|
|
: opCode(_opCode),
|
|
|
|
opCodeMask(_opCodeMask),
|
|
|
|
operands(numOperands)
|
2001-07-21 12:41:50 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-09-20 00:47:49 +00: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.
|
|
|
|
//
|
|
|
|
void
|
|
|
|
MachineInstr::replace(MachineOpCode _opCode,
|
|
|
|
unsigned numOperands,
|
|
|
|
OpCodeMask _opCodeMask)
|
|
|
|
{
|
|
|
|
opCode = _opCode;
|
|
|
|
opCodeMask = _opCodeMask;
|
|
|
|
operands.clear();
|
|
|
|
operands.resize(numOperands);
|
|
|
|
}
|
|
|
|
|
2001-07-21 12:41:50 +00:00
|
|
|
void
|
2002-03-18 03:35:24 +00:00
|
|
|
MachineInstr::SetMachineOperandVal(unsigned int i,
|
2002-07-08 22:38:45 +00:00
|
|
|
MachineOperand::MachineOperandType opType,
|
|
|
|
Value* _val,
|
2002-07-25 06:17:51 +00:00
|
|
|
bool isdef,
|
|
|
|
bool isDefAndUse)
|
2001-07-21 12:41:50 +00:00
|
|
|
{
|
2001-07-28 04:06:37 +00:00
|
|
|
assert(i < operands.size());
|
2002-07-08 22:38:45 +00:00
|
|
|
operands[i].Initialize(opType, _val);
|
2002-07-10 21:45:04 +00:00
|
|
|
if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
|
|
|
|
operands[i].markDef();
|
|
|
|
if (isDefAndUse)
|
|
|
|
operands[i].markDefAndUse();
|
2001-07-21 12:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-03-18 03:35:24 +00:00
|
|
|
MachineInstr::SetMachineOperandConst(unsigned int i,
|
2001-07-21 12:41:50 +00:00
|
|
|
MachineOperand::MachineOperandType operandType,
|
2002-03-18 03:35:24 +00:00
|
|
|
int64_t intValue)
|
2001-07-21 12:41:50 +00:00
|
|
|
{
|
2001-07-28 04:06:37 +00:00
|
|
|
assert(i < operands.size());
|
2002-03-18 03:35:24 +00:00
|
|
|
assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
|
|
|
|
"immed. constant cannot be defined");
|
2001-07-21 12:41:50 +00:00
|
|
|
operands[i].InitializeConst(operandType, intValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-03-18 03:35:24 +00:00
|
|
|
MachineInstr::SetMachineOperandReg(unsigned int i,
|
|
|
|
int regNum,
|
2002-07-25 06:17:51 +00:00
|
|
|
bool isdef,
|
|
|
|
bool isDefAndUse,
|
|
|
|
bool isCCReg)
|
2001-07-21 12:41:50 +00:00
|
|
|
{
|
2001-07-28 04:06:37 +00:00
|
|
|
assert(i < operands.size());
|
2002-03-18 03:35:24 +00:00
|
|
|
operands[i].InitializeReg(regNum, isCCReg);
|
2002-07-10 21:45:04 +00:00
|
|
|
if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
|
|
|
|
operands[i].markDef();
|
|
|
|
if (isDefAndUse)
|
|
|
|
operands[i].markDefAndUse();
|
2002-07-08 22:38:45 +00:00
|
|
|
regsUsed.insert(regNum);
|
2001-07-21 12:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-07-08 22:38:45 +00:00
|
|
|
MachineInstr::SetRegForOperand(unsigned i, int regNum)
|
2001-07-21 12:41:50 +00:00
|
|
|
{
|
2002-07-08 22:38:45 +00:00
|
|
|
operands[i].setRegForValue(regNum);
|
|
|
|
regsUsed.insert(regNum);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-14 16:52:58 +00: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
|
|
|
|
for (unsigned i=0, N=implicitRefs.size(); i < N; ++i)
|
|
|
|
if (implicitRefs[i] == oldVal)
|
|
|
|
if (!defsOnly || implicitRefIsDefined(i))
|
|
|
|
{
|
|
|
|
implicitRefs[i] = newVal;
|
|
|
|
++numSubst;
|
|
|
|
}
|
|
|
|
|
|
|
|
return numSubst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-08 22:38:45 +00:00
|
|
|
void
|
|
|
|
MachineInstr::dump() const
|
|
|
|
{
|
|
|
|
cerr << " " << *this;
|
2001-07-21 12:41:50 +00:00
|
|
|
}
|
|
|
|
|
2002-09-16 15:18:53 +00:00
|
|
|
static inline std::ostream&
|
|
|
|
OutputValue(std::ostream &os, const Value* val)
|
2002-04-25 04:31:18 +00:00
|
|
|
{
|
|
|
|
os << "(val ";
|
|
|
|
if (val && val->hasName())
|
2002-07-10 21:45:04 +00:00
|
|
|
return os << val->getName() << ")";
|
2002-04-25 04:31:18 +00:00
|
|
|
else
|
2002-07-10 21:45:04 +00:00
|
|
|
return os << (void*) val << ")"; // print address only
|
2002-04-25 04:31:18 +00:00
|
|
|
}
|
|
|
|
|
2002-09-16 15:18:53 +00:00
|
|
|
static inline std::ostream&
|
|
|
|
OutputReg(std::ostream &os, unsigned int regNum)
|
|
|
|
{
|
|
|
|
return os << "%mreg(" << regNum << ")";
|
|
|
|
}
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
|
2001-07-21 12:41:50 +00:00
|
|
|
{
|
2001-07-28 04:06:37 +00:00
|
|
|
os << TargetInstrDescriptors[minstr.opCode].opCodeString;
|
2001-07-21 12:41:50 +00:00
|
|
|
|
2001-11-14 20:05:23 +00:00
|
|
|
for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
|
2001-07-21 12:41:50 +00:00
|
|
|
os << "\t" << minstr.getOperand(i);
|
2002-07-08 22:38:45 +00:00
|
|
|
if( minstr.operandIsDefined(i) )
|
|
|
|
os << "*";
|
|
|
|
if( minstr.operandIsDefinedAndUsed(i) )
|
2001-11-15 20:46:40 +00:00
|
|
|
os << "*";
|
2001-11-14 20:05:23 +00:00
|
|
|
}
|
2001-07-21 12:41:50 +00:00
|
|
|
|
2001-10-18 22:40:02 +00:00
|
|
|
// code for printing implict references
|
|
|
|
unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
|
|
|
|
if( NumOfImpRefs > 0 ) {
|
2002-04-25 04:31:18 +00:00
|
|
|
os << "\tImplicit: ";
|
2001-10-18 22:40:02 +00:00
|
|
|
for(unsigned z=0; z < NumOfImpRefs; z++) {
|
2002-04-25 04:31:18 +00:00
|
|
|
OutputValue(os, minstr.getImplicitRef(z));
|
2001-11-14 20:05:23 +00:00
|
|
|
if( minstr.implicitRefIsDefined(z)) os << "*";
|
2002-07-08 22:38:45 +00:00
|
|
|
if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
|
2001-11-15 20:46:40 +00:00
|
|
|
os << "\t";
|
2001-10-18 22:40:02 +00:00
|
|
|
}
|
|
|
|
}
|
2002-04-25 04:31:18 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
return os << "\n";
|
2001-07-21 12:41:50 +00:00
|
|
|
}
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
|
2001-09-18 12:56:28 +00:00
|
|
|
{
|
2002-07-10 21:45:04 +00: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 12:56:28 +00:00
|
|
|
switch(mop.opType)
|
|
|
|
{
|
|
|
|
case MachineOperand::MO_VirtualRegister:
|
|
|
|
os << "%reg";
|
2002-07-10 21:45:04 +00:00
|
|
|
OutputValue(os, mop.getVRegValue());
|
2002-09-16 15:18:53 +00:00
|
|
|
if (mop.hasAllocatedReg())
|
|
|
|
os << "==" << OutputReg(os, mop.getAllocatedRegNum());
|
2002-07-10 21:45:04 +00:00
|
|
|
break;
|
2001-09-18 12:56:28 +00:00
|
|
|
case MachineOperand::MO_CCRegister:
|
|
|
|
os << "%ccreg";
|
2002-07-10 21:45:04 +00:00
|
|
|
OutputValue(os, mop.getVRegValue());
|
2002-09-16 15:18:53 +00:00
|
|
|
if (mop.hasAllocatedReg())
|
|
|
|
os << "==" << OutputReg(os, mop.getAllocatedRegNum());
|
2002-07-10 21:45:04 +00:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_MachineRegister:
|
2002-09-16 15:18:53 +00:00
|
|
|
OutputReg(os, mop.getMachineRegNum());
|
2002-07-10 21:45:04 +00:00
|
|
|
break;
|
2001-09-18 12:56:28 +00:00
|
|
|
case MachineOperand::MO_SignExtendedImmed:
|
2002-07-10 21:45:04 +00:00
|
|
|
os << (long)mop.immedVal;
|
|
|
|
break;
|
2001-09-18 12:56:28 +00:00
|
|
|
case MachineOperand::MO_UnextendedImmed:
|
2002-07-10 21:45:04 +00:00
|
|
|
os << (long)mop.immedVal;
|
|
|
|
break;
|
2001-09-18 12:56:28 +00:00
|
|
|
case MachineOperand::MO_PCRelativeDisp:
|
2001-09-30 23:44:19 +00:00
|
|
|
{
|
|
|
|
const Value* opVal = mop.getVRegValue();
|
2002-04-08 22:01:15 +00:00
|
|
|
bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
|
2001-11-12 14:19:47 +00:00
|
|
|
os << "%disp(" << (isLabel? "label " : "addr-of-val ");
|
|
|
|
if (opVal->hasName())
|
2002-01-20 22:54:45 +00:00
|
|
|
os << opVal->getName();
|
2001-11-12 14:19:47 +00:00
|
|
|
else
|
2002-07-08 22:38:45 +00:00
|
|
|
os << (const void*) opVal;
|
2002-07-10 21:45:04 +00:00
|
|
|
os << ")";
|
|
|
|
break;
|
2001-09-30 23:44:19 +00:00
|
|
|
}
|
2001-09-18 12:56:28 +00:00
|
|
|
default:
|
|
|
|
assert(0 && "Unrecognized operand type");
|
|
|
|
break;
|
|
|
|
}
|
2001-09-09 22:26:29 +00:00
|
|
|
|
2002-07-10 21:45:04 +00:00
|
|
|
if (mop.flags &
|
|
|
|
(MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
|
|
|
|
MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))
|
|
|
|
os << ")";
|
|
|
|
|
2001-07-21 12:41:50 +00:00
|
|
|
return os;
|
|
|
|
}
|