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 &OutputValue(std::ostream &os,
119 if (val && val->hasName())
120 return os << val->getName() << ")";
122 return os << (void*) val << ")"; // print address only
125 std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
127 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
129 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
130 os << "\t" << minstr.getOperand(i);
131 if( minstr.operandIsDefined(i) )
133 if( minstr.operandIsDefinedAndUsed(i) )
137 // code for printing implict references
138 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
139 if( NumOfImpRefs > 0 ) {
140 os << "\tImplicit: ";
141 for(unsigned z=0; z < NumOfImpRefs; z++) {
142 OutputValue(os, minstr.getImplicitRef(z));
143 if( minstr.implicitRefIsDefined(z)) os << "*";
144 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
152 std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
154 if (mop.opHiBits32())
156 else if (mop.opLoBits32())
158 else if (mop.opHiBits64())
160 else if (mop.opLoBits64())
165 case MachineOperand::MO_VirtualRegister:
167 OutputValue(os, mop.getVRegValue());
169 case MachineOperand::MO_CCRegister:
171 OutputValue(os, mop.getVRegValue());
173 case MachineOperand::MO_MachineRegister:
175 os << "(" << mop.getMachineRegNum() << ")";
177 case MachineOperand::MO_SignExtendedImmed:
178 os << (long)mop.immedVal;
180 case MachineOperand::MO_UnextendedImmed:
181 os << (long)mop.immedVal;
183 case MachineOperand::MO_PCRelativeDisp:
185 const Value* opVal = mop.getVRegValue();
186 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
187 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
188 if (opVal->hasName())
189 os << opVal->getName();
191 os << (const void*) opVal;
196 assert(0 && "Unrecognized operand type");
201 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
202 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))