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 MachineInstr::SetMachineOperandVal(unsigned int i,
32 MachineOperand::MachineOperandType opType,
37 assert(i < operands.size());
38 operands[i].Initialize(opType, _val);
39 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
40 operands[i].markDef();
42 operands[i].markDefAndUse();
46 MachineInstr::SetMachineOperandConst(unsigned int i,
47 MachineOperand::MachineOperandType operandType,
50 assert(i < operands.size());
51 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
52 "immed. constant cannot be defined");
53 operands[i].InitializeConst(operandType, intValue);
57 MachineInstr::SetMachineOperandReg(unsigned int i,
63 assert(i < operands.size());
64 operands[i].InitializeReg(regNum, isCCReg);
65 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
66 operands[i].markDef();
68 operands[i].markDefAndUse();
69 regsUsed.insert(regNum);
73 MachineInstr::SetRegForOperand(unsigned i, int regNum)
75 operands[i].setRegForValue(regNum);
76 regsUsed.insert(regNum);
80 // Subsitute all occurrences of Value* oldVal with newVal in all operands
81 // and all implicit refs. If defsOnly == true, substitute defs only.
83 MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
85 unsigned numSubst = 0;
88 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
90 if (!defsOnly || O.isDef())
92 O.getMachineOperand().value = newVal;
96 // Subsitute implicit refs
97 for (unsigned i=0, N=implicitRefs.size(); i < N; ++i)
98 if (implicitRefs[i] == oldVal)
99 if (!defsOnly || implicitRefIsDefined(i))
101 implicitRefs[i] = newVal;
110 MachineInstr::dump() const
112 cerr << " " << *this;
115 static inline std::ostream&
116 OutputValue(std::ostream &os, const Value* val)
119 if (val && val->hasName())
120 return os << val->getName() << ")";
122 return os << (void*) val << ")"; // print address only
125 static inline std::ostream&
126 OutputReg(std::ostream &os, unsigned int regNum)
128 return os << "%mreg(" << regNum << ")";
131 std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
133 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
135 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
136 os << "\t" << minstr.getOperand(i);
137 if( minstr.operandIsDefined(i) )
139 if( minstr.operandIsDefinedAndUsed(i) )
143 // code for printing implict references
144 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
145 if( NumOfImpRefs > 0 ) {
146 os << "\tImplicit: ";
147 for(unsigned z=0; z < NumOfImpRefs; z++) {
148 OutputValue(os, minstr.getImplicitRef(z));
149 if( minstr.implicitRefIsDefined(z)) os << "*";
150 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
158 std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
160 if (mop.opHiBits32())
162 else if (mop.opLoBits32())
164 else if (mop.opHiBits64())
166 else if (mop.opLoBits64())
171 case MachineOperand::MO_VirtualRegister:
173 OutputValue(os, mop.getVRegValue());
174 if (mop.hasAllocatedReg())
175 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
177 case MachineOperand::MO_CCRegister:
179 OutputValue(os, mop.getVRegValue());
180 if (mop.hasAllocatedReg())
181 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
183 case MachineOperand::MO_MachineRegister:
184 OutputReg(os, mop.getMachineRegNum());
186 case MachineOperand::MO_SignExtendedImmed:
187 os << (long)mop.immedVal;
189 case MachineOperand::MO_UnextendedImmed:
190 os << (long)mop.immedVal;
192 case MachineOperand::MO_PCRelativeDisp:
194 const Value* opVal = mop.getVRegValue();
195 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
196 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
197 if (opVal->hasName())
198 os << opVal->getName();
200 os << (const void*) opVal;
205 assert(0 && "Unrecognized operand type");
210 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
211 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))