1 //===-- MachineInstr.cpp --------------------------------------------------===//
3 //===----------------------------------------------------------------------===//
5 #include "llvm/CodeGen/MachineInstr.h"
6 #include "llvm/CodeGen/MachineBasicBlock.h"
7 #include "llvm/Value.h"
8 #include "llvm/Target/TargetMachine.h"
9 #include "llvm/Target/TargetInstrInfo.h"
10 #include "llvm/Target/MRegisterInfo.h"
14 // Global variable holding an array of descriptors for machine instructions.
15 // The actual object needs to be created separately for each target machine.
16 // This variable is initialized and reset by class TargetInstrInfo.
18 // FIXME: This should be a property of the target so that more than one target
19 // at a time can be active...
21 extern const TargetInstrDescriptor *TargetInstrDescriptors;
23 // Constructor for instructions with variable #operands
24 MachineInstr::MachineInstr(MachineOpCode OpCode, unsigned numOperands)
26 operands(numOperands, MachineOperand()),
31 /// MachineInstr ctor - This constructor only does a _reserve_ of the operands,
32 /// not a resize for them. It is expected that if you use this that you call
33 /// add* methods below to fill up the operands, instead of the Set methods.
34 /// Eventually, the "resizing" ctors will be phased out.
36 MachineInstr::MachineInstr(MachineOpCode Opcode, unsigned numOperands,
41 operands.reserve(numOperands);
44 /// MachineInstr ctor - Work exactly the same as the ctor above, except that the
45 /// MachineInstr is created and added to the end of the specified basic block.
47 MachineInstr::MachineInstr(MachineBasicBlock *MBB, MachineOpCode Opcode,
52 assert(MBB && "Cannot use inserting ctor with null basic block!");
53 operands.reserve(numOperands);
54 MBB->push_back(this); // Add instruction to end of basic block!
58 // OperandComplete - Return true if it's illegal to add a new operand
59 bool MachineInstr::OperandsComplete() const
61 int NumOperands = TargetInstrDescriptors[opCode].numOperands;
62 if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands)
63 return true; // Broken!
69 // Support for replacing opcode and operands of a MachineInstr in place.
70 // This only resets the size of the operand vector and initializes it.
71 // The new operands must be set explicitly later.
73 void MachineInstr::replace(MachineOpCode Opcode, unsigned numOperands)
75 assert(getNumImplicitRefs() == 0 &&
76 "This is probably broken because implicit refs are going to be lost.");
79 operands.resize(numOperands, MachineOperand());
83 MachineInstr::SetMachineOperandVal(unsigned i,
84 MachineOperand::MachineOperandType opType,
89 assert(i < operands.size()); // may be explicit or implicit op
90 operands[i].opType = opType;
91 operands[i].value = V;
92 operands[i].regNum = -1;
95 operands[i].flags = MachineOperand::DEFUSEFLAG;
96 else if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
97 operands[i].flags = MachineOperand::DEFONLYFLAG;
99 operands[i].flags = 0;
103 MachineInstr::SetMachineOperandConst(unsigned i,
104 MachineOperand::MachineOperandType operandType,
107 assert(i < getNumOperands()); // must be explicit op
108 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
109 "immed. constant cannot be defined");
111 operands[i].opType = operandType;
112 operands[i].value = NULL;
113 operands[i].immedVal = intValue;
114 operands[i].regNum = -1;
115 operands[i].flags = 0;
119 MachineInstr::SetMachineOperandReg(unsigned i,
122 assert(i < getNumOperands()); // must be explicit op
124 operands[i].opType = MachineOperand::MO_MachineRegister;
125 operands[i].value = NULL;
126 operands[i].regNum = regNum;
128 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
129 operands[i].flags = MachineOperand::DEFONLYFLAG;
131 operands[i].flags = 0;
133 insertUsedReg(regNum);
137 MachineInstr::SetRegForOperand(unsigned i, int regNum)
139 assert(i < getNumOperands()); // must be explicit op
140 operands[i].setRegForValue(regNum);
141 insertUsedReg(regNum);
145 // Subsitute all occurrences of Value* oldVal with newVal in all operands
146 // and all implicit refs. If defsOnly == true, substitute defs only.
148 MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
150 unsigned numSubst = 0;
152 // Subsitute operands
153 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
155 if (!defsOnly || !O.isUseOnly())
157 O.getMachineOperand().value = newVal;
161 // Subsitute implicit refs
162 for (unsigned i=0, N=getNumImplicitRefs(); i < N; ++i)
163 if (getImplicitRef(i) == oldVal)
164 if (!defsOnly || !getImplicitOp(i).opIsUse())
166 getImplicitOp(i).value = newVal;
175 MachineInstr::dump() const
177 cerr << " " << *this;
180 static inline std::ostream&
181 OutputValue(std::ostream &os, const Value* val)
184 if (val && val->hasName())
185 return os << val->getName() << ")";
187 return os << (void*) val << ")"; // print address only
190 static inline void OutputReg(std::ostream &os, unsigned RegNo,
191 const MRegisterInfo *MRI = 0) {
193 if (RegNo < MRegisterInfo::FirstVirtualRegister)
194 os << "%" << MRI->get(RegNo).Name;
196 os << "%reg" << RegNo;
198 os << "%mreg(" << RegNo << ")";
201 static void print(const MachineOperand &MO, std::ostream &OS,
202 const TargetMachine &TM) {
203 const MRegisterInfo *MRI = TM.getRegisterInfo();
204 bool CloseParen = true;
207 else if (MO.opLoBits32())
209 else if (MO.opHiBits64())
211 else if (MO.opLoBits64())
216 switch (MO.getType()) {
217 case MachineOperand::MO_VirtualRegister:
218 if (MO.getVRegValue()) {
220 OutputValue(OS, MO.getVRegValue());
221 if (MO.hasAllocatedReg())
224 if (MO.hasAllocatedReg())
225 OutputReg(OS, MO.getAllocatedRegNum(), MRI);
227 case MachineOperand::MO_CCRegister:
229 OutputValue(OS, MO.getVRegValue());
230 if (MO.hasAllocatedReg()) {
232 OutputReg(OS, MO.getAllocatedRegNum(), MRI);
235 case MachineOperand::MO_MachineRegister:
236 OutputReg(OS, MO.getMachineRegNum(), MRI);
238 case MachineOperand::MO_SignExtendedImmed:
239 OS << (long)MO.getImmedValue();
241 case MachineOperand::MO_UnextendedImmed:
242 OS << (long)MO.getImmedValue();
244 case MachineOperand::MO_PCRelativeDisp: {
245 const Value* opVal = MO.getVRegValue();
246 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
247 OS << "%disp(" << (isLabel? "label " : "addr-of-val ");
248 if (opVal->hasName())
249 OS << opVal->getName();
251 OS << (const void*) opVal;
255 case MachineOperand::MO_MachineBasicBlock:
257 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
258 << "," << (void*)MO.getMachineBasicBlock()->getBasicBlock() << ">";
260 case MachineOperand::MO_FrameIndex:
261 OS << "<fi#" << MO.getFrameIndex() << ">";
263 case MachineOperand::MO_ConstantPoolIndex:
264 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
266 case MachineOperand::MO_GlobalAddress:
267 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
269 case MachineOperand::MO_ExternalSymbol:
270 OS << "<es:" << MO.getSymbolName() << ">";
273 assert(0 && "Unrecognized operand type");
280 void MachineInstr::print(std::ostream &OS, const TargetMachine &TM) const {
281 unsigned StartOp = 0;
283 // Specialize printing if op#0 is definition
284 if (getNumOperands() &&
285 (getOperand(0).opIsDefOnly() || getOperand(0).opIsDefAndUse())) {
286 ::print(getOperand(0), OS, TM);
288 ++StartOp; // Don't print this operand again!
290 OS << TM.getInstrInfo().getName(getOpcode());
292 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
293 const MachineOperand& mop = getOperand(i);
297 ::print(mop, OS, TM);
299 if (mop.opIsDefAndUse())
301 else if (mop.opIsDefOnly())
305 // code for printing implict references
306 if (getNumImplicitRefs()) {
307 OS << "\tImplicitRefs: ";
308 for(unsigned i = 0, e = getNumImplicitRefs(); i != e; ++i) {
310 OutputValue(OS, getImplicitRef(i));
311 if (getImplicitOp(i).opIsDefAndUse())
313 else if (getImplicitOp(i).opIsDefOnly())
322 std::ostream &operator<<(std::ostream& os, const MachineInstr& MI)
324 os << TargetInstrDescriptors[MI.opCode].Name;
326 for (unsigned i=0, N=MI.getNumOperands(); i < N; i++) {
327 os << "\t" << MI.getOperand(i);
328 if (MI.getOperand(i).opIsDefOnly())
330 if (MI.getOperand(i).opIsDefAndUse())
334 // code for printing implict references
335 unsigned NumOfImpRefs = MI.getNumImplicitRefs();
336 if (NumOfImpRefs > 0) {
337 os << "\tImplicit: ";
338 for (unsigned z=0; z < NumOfImpRefs; z++) {
339 OutputValue(os, MI.getImplicitRef(z));
340 if (MI.getImplicitOp(z).opIsDefOnly()) os << "<d>";
341 if (MI.getImplicitOp(z).opIsDefAndUse()) os << "<d&u>";
349 std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO)
353 else if (MO.opLoBits32())
355 else if (MO.opHiBits64())
357 else if (MO.opLoBits64())
360 switch (MO.getType())
362 case MachineOperand::MO_VirtualRegister:
363 if (MO.hasAllocatedReg())
364 OutputReg(OS, MO.getAllocatedRegNum());
366 if (MO.getVRegValue()) {
367 if (MO.hasAllocatedReg()) OS << "==";
369 OutputValue(OS, MO.getVRegValue());
372 case MachineOperand::MO_CCRegister:
374 OutputValue(OS, MO.getVRegValue());
375 if (MO.hasAllocatedReg()) {
377 OutputReg(OS, MO.getAllocatedRegNum());
380 case MachineOperand::MO_MachineRegister:
381 OutputReg(OS, MO.getMachineRegNum());
383 case MachineOperand::MO_SignExtendedImmed:
384 OS << (long)MO.getImmedValue();
386 case MachineOperand::MO_UnextendedImmed:
387 OS << (long)MO.getImmedValue();
389 case MachineOperand::MO_PCRelativeDisp:
391 const Value* opVal = MO.getVRegValue();
392 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
393 OS << "%disp(" << (isLabel? "label " : "addr-of-val ");
394 if (opVal->hasName())
395 OS << opVal->getName();
397 OS << (const void*) opVal;
401 case MachineOperand::MO_MachineBasicBlock:
403 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
404 << "," << (void*)MO.getMachineBasicBlock()->getBasicBlock() << ">";
406 case MachineOperand::MO_FrameIndex:
407 OS << "<fi#" << MO.getFrameIndex() << ">";
409 case MachineOperand::MO_ConstantPoolIndex:
410 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
412 case MachineOperand::MO_GlobalAddress:
413 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
415 case MachineOperand::MO_ExternalSymbol:
416 OS << "<es:" << MO.getSymbolName() << ">";
419 assert(0 && "Unrecognized operand type");
424 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
425 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))