1 //===-- MachineInstr.cpp --------------------------------------------------===//
3 //===----------------------------------------------------------------------===//
5 #include "llvm/CodeGen/MachineInstr.h"
6 #include "llvm/Value.h"
10 // Constructor for instructions with fixed #operands (nearly all)
11 MachineInstr::MachineInstr(MachineOpCode _opCode,
12 OpCodeMask _opCodeMask)
14 opCodeMask(_opCodeMask),
15 operands(TargetInstrDescriptors[_opCode].numOperands)
17 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
20 // Constructor for instructions with variable #operands
21 MachineInstr::MachineInstr(MachineOpCode _opCode,
23 OpCodeMask _opCodeMask)
25 opCodeMask(_opCodeMask),
31 // Support for replacing opcode and operands of a MachineInstr in place.
32 // This only resets the size of the operand vector and initializes it.
33 // The new operands must be set explicitly later.
36 MachineInstr::replace(MachineOpCode _opCode,
38 OpCodeMask _opCodeMask)
41 opCodeMask = _opCodeMask;
43 operands.resize(numOperands);
47 MachineInstr::SetMachineOperandVal(unsigned int i,
48 MachineOperand::MachineOperandType opType,
53 assert(i < operands.size());
54 operands[i].Initialize(opType, _val);
55 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
56 operands[i].markDef();
58 operands[i].markDefAndUse();
62 MachineInstr::SetMachineOperandConst(unsigned int i,
63 MachineOperand::MachineOperandType operandType,
66 assert(i < operands.size());
67 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
68 "immed. constant cannot be defined");
69 operands[i].InitializeConst(operandType, intValue);
73 MachineInstr::SetMachineOperandReg(unsigned int i,
79 assert(i < operands.size());
80 operands[i].InitializeReg(regNum, isCCReg);
81 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
82 operands[i].markDef();
84 operands[i].markDefAndUse();
85 regsUsed.insert(regNum);
89 MachineInstr::SetRegForOperand(unsigned i, int regNum)
91 operands[i].setRegForValue(regNum);
92 regsUsed.insert(regNum);
96 // Subsitute all occurrences of Value* oldVal with newVal in all operands
97 // and all implicit refs. If defsOnly == true, substitute defs only.
99 MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
101 unsigned numSubst = 0;
103 // Subsitute operands
104 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
106 if (!defsOnly || O.isDef())
108 O.getMachineOperand().value = newVal;
112 // Subsitute implicit refs
113 for (unsigned i=0, N=implicitRefs.size(); i < N; ++i)
114 if (implicitRefs[i] == oldVal)
115 if (!defsOnly || implicitRefIsDefined(i))
117 implicitRefs[i] = newVal;
126 MachineInstr::dump() const
128 cerr << " " << *this;
131 static inline std::ostream&
132 OutputValue(std::ostream &os, const Value* val)
135 if (val && val->hasName())
136 return os << val->getName() << ")";
138 return os << (void*) val << ")"; // print address only
141 static inline std::ostream&
142 OutputReg(std::ostream &os, unsigned int regNum)
144 return os << "%mreg(" << regNum << ")";
147 std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
149 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
151 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
152 os << "\t" << minstr.getOperand(i);
153 if( minstr.operandIsDefined(i) )
155 if( minstr.operandIsDefinedAndUsed(i) )
159 // code for printing implict references
160 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
161 if( NumOfImpRefs > 0 ) {
162 os << "\tImplicit: ";
163 for(unsigned z=0; z < NumOfImpRefs; z++) {
164 OutputValue(os, minstr.getImplicitRef(z));
165 if( minstr.implicitRefIsDefined(z)) os << "*";
166 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
174 std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
176 if (mop.opHiBits32())
178 else if (mop.opLoBits32())
180 else if (mop.opHiBits64())
182 else if (mop.opLoBits64())
187 case MachineOperand::MO_VirtualRegister:
189 OutputValue(os, mop.getVRegValue());
190 if (mop.hasAllocatedReg())
191 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
193 case MachineOperand::MO_CCRegister:
195 OutputValue(os, mop.getVRegValue());
196 if (mop.hasAllocatedReg())
197 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
199 case MachineOperand::MO_MachineRegister:
200 OutputReg(os, mop.getMachineRegNum());
202 case MachineOperand::MO_SignExtendedImmed:
203 os << (long)mop.immedVal;
205 case MachineOperand::MO_UnextendedImmed:
206 os << (long)mop.immedVal;
208 case MachineOperand::MO_PCRelativeDisp:
210 const Value* opVal = mop.getVRegValue();
211 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
212 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
213 if (opVal->hasName())
214 os << opVal->getName();
216 os << (const void*) opVal;
221 assert(0 && "Unrecognized operand type");
226 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
227 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))