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!
10 // Global variable holding an array of descriptors for machine instructions.
11 // The actual object needs to be created separately for each target machine.
12 // This variable is initialized and reset by class MachineInstrInfo.
14 // FIXME: This should be a property of the target so that more than one target
15 // at a time can be active...
17 extern const MachineInstrDescriptor *TargetInstrDescriptors;
19 // Constructor for instructions with fixed #operands (nearly all)
20 MachineInstr::MachineInstr(MachineOpCode _opCode)
22 operands(TargetInstrDescriptors[_opCode].numOperands, MachineOperand()),
25 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
28 // Constructor for instructions with variable #operands
29 MachineInstr::MachineInstr(MachineOpCode OpCode, unsigned numOperands)
31 operands(numOperands, MachineOperand()),
36 MachineInstr::MachineInstr(MachineOpCode Opcode, unsigned numOperands,
41 operands.reserve(numOperands);
44 // OperandComplete - Return true if it's illegal to add a new operand
45 bool MachineInstr::OperandsComplete() const
47 int NumOperands = TargetInstrDescriptors[opCode].numOperands;
48 if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands)
49 return true; // Broken!
55 // Support for replacing opcode and operands of a MachineInstr in place.
56 // This only resets the size of the operand vector and initializes it.
57 // The new operands must be set explicitly later.
59 void MachineInstr::replace(MachineOpCode Opcode, unsigned numOperands)
61 assert(getNumImplicitRefs() == 0 &&
62 "This is probably broken because implicit refs are going to be lost.");
65 operands.resize(numOperands, MachineOperand());
69 MachineInstr::SetMachineOperandVal(unsigned i,
70 MachineOperand::MachineOperandType opType,
75 assert(i < operands.size()); // may be explicit or implicit op
76 operands[i].opType = opType;
77 operands[i].value = V;
78 operands[i].regNum = -1;
79 operands[i].flags = 0;
81 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
82 operands[i].markDef();
84 operands[i].markDefAndUse();
88 MachineInstr::SetMachineOperandConst(unsigned i,
89 MachineOperand::MachineOperandType operandType,
92 assert(i < getNumOperands()); // must be explicit op
93 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
94 "immed. constant cannot be defined");
96 operands[i].opType = operandType;
97 operands[i].value = NULL;
98 operands[i].immedVal = intValue;
99 operands[i].regNum = -1;
100 operands[i].flags = 0;
104 MachineInstr::SetMachineOperandReg(unsigned i,
107 assert(i < getNumOperands()); // must be explicit op
109 operands[i].opType = MachineOperand::MO_MachineRegister;
110 operands[i].value = NULL;
111 operands[i].regNum = regNum;
112 operands[i].flags = 0;
114 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
115 operands[i].markDef();
116 insertUsedReg(regNum);
120 MachineInstr::SetRegForOperand(unsigned i, int regNum)
122 assert(i < getNumOperands()); // must be explicit op
123 operands[i].setRegForValue(regNum);
124 insertUsedReg(regNum);
128 // Subsitute all occurrences of Value* oldVal with newVal in all operands
129 // and all implicit refs. If defsOnly == true, substitute defs only.
131 MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
133 unsigned numSubst = 0;
135 // Subsitute operands
136 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
138 if (!defsOnly || O.isDef())
140 O.getMachineOperand().value = newVal;
144 // Subsitute implicit refs
145 for (unsigned i=0, N=getNumImplicitRefs(); i < N; ++i)
146 if (getImplicitRef(i) == oldVal)
147 if (!defsOnly || implicitRefIsDefined(i))
149 getImplicitOp(i).value = newVal;
158 MachineInstr::dump() const
160 cerr << " " << *this;
163 static inline std::ostream&
164 OutputValue(std::ostream &os, const Value* val)
167 if (val && val->hasName())
168 return os << val->getName() << ")";
170 return os << (void*) val << ")"; // print address only
173 static inline std::ostream&
174 OutputReg(std::ostream &os, unsigned int regNum)
176 return os << "%mreg(" << regNum << ")";
179 std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
181 os << TargetInstrDescriptors[minstr.opCode].Name;
183 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
184 os << "\t" << minstr.getOperand(i);
185 if( minstr.operandIsDefined(i) )
187 if( minstr.operandIsDefinedAndUsed(i) )
191 // code for printing implict references
192 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
193 if( NumOfImpRefs > 0 ) {
194 os << "\tImplicit: ";
195 for(unsigned z=0; z < NumOfImpRefs; z++) {
196 OutputValue(os, minstr.getImplicitRef(z));
197 if( minstr.implicitRefIsDefined(z)) os << "*";
198 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
206 std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
208 if (mop.opHiBits32())
210 else if (mop.opLoBits32())
212 else if (mop.opHiBits64())
214 else if (mop.opLoBits64())
219 case MachineOperand::MO_VirtualRegister:
221 OutputValue(os, mop.getVRegValue());
222 if (mop.hasAllocatedReg())
223 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
225 case MachineOperand::MO_CCRegister:
227 OutputValue(os, mop.getVRegValue());
228 if (mop.hasAllocatedReg())
229 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
231 case MachineOperand::MO_MachineRegister:
232 OutputReg(os, mop.getMachineRegNum());
234 case MachineOperand::MO_SignExtendedImmed:
235 os << (long)mop.immedVal;
237 case MachineOperand::MO_UnextendedImmed:
238 os << (long)mop.immedVal;
240 case MachineOperand::MO_PCRelativeDisp:
242 const Value* opVal = mop.getVRegValue();
243 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
244 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
245 if (opVal->hasName())
246 os << opVal->getName();
248 os << (const void*) opVal;
253 assert(0 && "Unrecognized operand type");
258 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
259 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))