1 //===-- MachineInstr.cpp --------------------------------------------------===//
3 //===----------------------------------------------------------------------===//
5 #include "llvm/CodeGen/MachineInstr.h"
6 #include "llvm/Value.h"
7 #include "llvm/Target/MachineInstrInfo.h" // FIXME: shouldn't need this!
11 // Constructor for instructions with fixed #operands (nearly all)
12 MachineInstr::MachineInstr(MachineOpCode _opCode)
13 : opCode(_opCode), opCodeMask(0),
14 operands(TargetInstrDescriptors[_opCode].numOperands, MachineOperand()) {
15 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
18 // Constructor for instructions with variable #operands
19 MachineInstr::MachineInstr(MachineOpCode OpCode, unsigned numOperands/*,
20 OpCodeMask OpCodeMask*/)
21 : opCode(OpCode), opCodeMask(0/*OpCodeMask*/),
22 operands(numOperands, MachineOperand()) {
25 MachineInstr::MachineInstr(MachineOpCode Opcode, unsigned numOperands,
26 bool XX, bool YY) : opCode(Opcode), opCodeMask(0) {
27 operands.reserve(numOperands);
30 // OperandComplete - Return true if it's illegal to add a new operand
31 bool MachineInstr::OperandsComplete() const {
32 int NumOperands = TargetInstrDescriptors[opCode].numOperands;
33 if (NumOperands >= 0 && operands.size() >= (unsigned)NumOperands)
34 return true; // Broken!
40 // Support for replacing opcode and operands of a MachineInstr in place.
41 // This only resets the size of the operand vector and initializes it.
42 // The new operands must be set explicitly later.
44 void MachineInstr::replace(MachineOpCode Opcode, unsigned numOperands,
49 operands.resize(numOperands, MachineOperand());
53 MachineInstr::SetMachineOperandVal(unsigned i,
54 MachineOperand::MachineOperandType opType,
59 assert(i < operands.size());
60 operands[i].opType = opType;
61 operands[i].value = V;
62 operands[i].regNum = -1;
63 operands[i].flags = 0;
65 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
66 operands[i].markDef();
68 operands[i].markDefAndUse();
72 MachineInstr::SetMachineOperandConst(unsigned i,
73 MachineOperand::MachineOperandType operandType,
76 assert(i < operands.size());
77 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
78 "immed. constant cannot be defined");
80 operands[i].opType = operandType;
81 operands[i].value = NULL;
82 operands[i].immedVal = intValue;
83 operands[i].regNum = -1;
84 operands[i].flags = 0;
88 MachineInstr::SetMachineOperandReg(unsigned i,
91 assert(i < operands.size());
93 operands[i].opType = MachineOperand::MO_MachineRegister;
94 operands[i].value = NULL;
95 operands[i].regNum = regNum;
96 operands[i].flags = 0;
98 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
99 operands[i].markDef();
100 insertUsedReg(regNum);
104 MachineInstr::SetRegForOperand(unsigned i, int regNum)
106 operands[i].setRegForValue(regNum);
107 insertUsedReg(regNum);
111 // Subsitute all occurrences of Value* oldVal with newVal in all operands
112 // and all implicit refs. If defsOnly == true, substitute defs only.
114 MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
116 unsigned numSubst = 0;
118 // Subsitute operands
119 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
121 if (!defsOnly || O.isDef())
123 O.getMachineOperand().value = newVal;
127 // Subsitute implicit refs
128 for (unsigned i=0, N=implicitRefs.size(); i < N; ++i)
129 if (getImplicitRef(i) == oldVal)
130 if (!defsOnly || implicitRefIsDefined(i))
132 implicitRefs[i].Val = newVal;
141 MachineInstr::dump() const
143 cerr << " " << *this;
146 static inline std::ostream&
147 OutputValue(std::ostream &os, const Value* val)
150 if (val && val->hasName())
151 return os << val->getName() << ")";
153 return os << (void*) val << ")"; // print address only
156 static inline std::ostream&
157 OutputReg(std::ostream &os, unsigned int regNum)
159 return os << "%mreg(" << regNum << ")";
162 std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
164 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
166 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
167 os << "\t" << minstr.getOperand(i);
168 if( minstr.operandIsDefined(i) )
170 if( minstr.operandIsDefinedAndUsed(i) )
174 // code for printing implict references
175 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
176 if( NumOfImpRefs > 0 ) {
177 os << "\tImplicit: ";
178 for(unsigned z=0; z < NumOfImpRefs; z++) {
179 OutputValue(os, minstr.getImplicitRef(z));
180 if( minstr.implicitRefIsDefined(z)) os << "*";
181 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
189 std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
191 if (mop.opHiBits32())
193 else if (mop.opLoBits32())
195 else if (mop.opHiBits64())
197 else if (mop.opLoBits64())
202 case MachineOperand::MO_VirtualRegister:
204 OutputValue(os, mop.getVRegValue());
205 if (mop.hasAllocatedReg())
206 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
208 case MachineOperand::MO_CCRegister:
210 OutputValue(os, mop.getVRegValue());
211 if (mop.hasAllocatedReg())
212 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
214 case MachineOperand::MO_MachineRegister:
215 OutputReg(os, mop.getMachineRegNum());
217 case MachineOperand::MO_SignExtendedImmed:
218 os << (long)mop.immedVal;
220 case MachineOperand::MO_UnextendedImmed:
221 os << (long)mop.immedVal;
223 case MachineOperand::MO_PCRelativeDisp:
225 const Value* opVal = mop.getVRegValue();
226 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
227 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
228 if (opVal->hasName())
229 os << opVal->getName();
231 os << (const void*) opVal;
236 assert(0 && "Unrecognized operand type");
241 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
242 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))