1 //===-- llvm/CodeGen/MachineInstr.h - MachineInstr class ---------*- C++ -*--=//
3 // This file contains the declaration of the MachineInstr class, which is the
4 // basic representation for all target dependant machine instructions used by
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CODEGEN_MACHINEINSTR_H
10 #define LLVM_CODEGEN_MACHINEINSTR_H
12 #include "llvm/Annotation.h"
13 #include "Support/iterator"
14 #include "Support/NonCopyable.h"
19 typedef int MachineOpCode;
20 typedef int OpCodeMask;
22 //---------------------------------------------------------------------------
23 // class MachineOperand
26 // Representation of each machine instruction operand.
27 // This class is designed so that you can allocate a vector of operands
28 // first and initialize each one later.
30 // E.g, for this VM instruction:
31 // ptr = alloca type, numElements
32 // we generate 2 machine instructions on the SPARC:
34 // mul Constant, Numelements -> Reg
35 // add %sp, Reg -> Ptr
37 // Each instruction has 3 operands, listed above. Of those:
38 // - Reg, NumElements, and Ptr are of operand type MO_Register.
39 // - Constant is of operand type MO_SignExtendedImmed on the SPARC.
41 // For the register operands, the virtual register type is as follows:
43 // - Reg will be of virtual register type MO_MInstrVirtualReg. The field
44 // MachineInstr* minstr will point to the instruction that computes reg.
46 // - %sp will be of virtual register type MO_MachineReg.
47 // The field regNum identifies the machine register.
49 // - NumElements will be of virtual register type MO_VirtualReg.
50 // The field Value* value identifies the value.
52 // - Ptr will also be of virtual register type MO_VirtualReg.
53 // Again, the field Value* value identifies the value.
55 //---------------------------------------------------------------------------
57 class MachineOperand {
59 enum MachineOperandType {
60 MO_VirtualRegister, // virtual register for *value
61 MO_MachineRegister, // pre-assigned machine register `regNum'
69 // Bit fields of the flags variable used for different operand properties
70 static const char DEFFLAG = 0x1; // this is a def of the operand
71 static const char DEFUSEFLAG = 0x2; // this is both a def and a use
72 static const char HIFLAG32 = 0x4; // operand is %hi32(value_or_immedVal)
73 static const char LOFLAG32 = 0x8; // operand is %lo32(value_or_immedVal)
74 static const char HIFLAG64 = 0x10; // operand is %hi64(value_or_immedVal)
75 static const char LOFLAG64 = 0x20; // operand is %lo64(value_or_immedVal)
79 Value* value; // BasicBlockVal for a label operand.
80 // ConstantVal for a non-address immediate.
81 // Virtual register for an SSA operand,
82 // including hidden operands required for
83 // the generated machine code.
84 int64_t immedVal; // constant value for an explicit constant
87 MachineOperandType opType:8; // Pack into 8 bits efficiently after flags.
88 char flags; // see bit field definitions above
89 int regNum; // register number for an explicit register
90 // will be set for a value after reg allocation
93 : immedVal(0), opType(MO_VirtualRegister), flags(0), regNum(-1) {}
94 MachineOperand(int64_t ImmVal, MachineOperandType OpTy)
95 : immedVal(ImmVal), opType(OpTy), flags(0), regNum(-1) {}
96 MachineOperand(int Reg, MachineOperandType OpTy, bool isDef = false)
97 : immedVal(0), opType(OpTy), flags(isDef ? DEFFLAG : 0), regNum(Reg) {}
98 MachineOperand(Value *V, MachineOperandType OpTy,
99 bool isDef = false, bool isDNU = false)
100 : value(V), opType(OpTy), regNum(-1) {
101 flags = (isDef ? DEFFLAG : 0) | (isDNU ? DEFUSEFLAG : 0);
105 MachineOperand(const MachineOperand &M)
106 : immedVal(M.immedVal), opType(M.opType), flags(M.flags), regNum(M.regNum) {
110 // Accessor methods. Caller is responsible for checking the
111 // operand type before invoking the corresponding accessor.
113 MachineOperandType getType() const { return opType; }
115 inline Value* getVRegValue () const {
116 assert(opType == MO_VirtualRegister || opType == MO_CCRegister ||
117 opType == MO_PCRelativeDisp);
120 inline Value* getVRegValueOrNull() const {
121 return (opType == MO_VirtualRegister || opType == MO_CCRegister ||
122 opType == MO_PCRelativeDisp)? value : NULL;
124 inline int getMachineRegNum() const {
125 assert(opType == MO_MachineRegister);
128 inline int64_t getImmedValue () const {
129 assert(opType == MO_SignExtendedImmed || opType == MO_UnextendedImmed);
132 bool opIsDef () const { return flags & DEFFLAG; }
133 bool opIsDefAndUse () const { return flags & DEFUSEFLAG; }
134 bool opHiBits32 () const { return flags & HIFLAG32; }
135 bool opLoBits32 () const { return flags & LOFLAG32; }
136 bool opHiBits64 () const { return flags & HIFLAG64; }
137 bool opLoBits64 () const { return flags & LOFLAG64; }
139 // used to check if a machine register has been allocated to this operand
140 inline bool hasAllocatedReg() const {
141 return (regNum >= 0 &&
142 (opType == MO_VirtualRegister || opType == MO_CCRegister ||
143 opType == MO_MachineRegister));
146 // used to get the reg number if when one is allocated
147 inline int getAllocatedRegNum() const {
148 assert(opType == MO_VirtualRegister || opType == MO_CCRegister ||
149 opType == MO_MachineRegister);
154 friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
158 // Construction methods needed for fine-grain control.
159 // These must be accessed via coresponding methods in MachineInstr.
160 void markDef() { flags |= DEFFLAG; }
161 void markDefAndUse() { flags |= DEFUSEFLAG; }
162 void markHi32() { flags |= HIFLAG32; }
163 void markLo32() { flags |= LOFLAG32; }
164 void markHi64() { flags |= HIFLAG64; }
165 void markLo64() { flags |= LOFLAG64; }
167 // Replaces the Value with its corresponding physical register after
168 // register allocation is complete
169 void setRegForValue(int reg) {
170 assert(opType == MO_VirtualRegister || opType == MO_CCRegister ||
171 opType == MO_MachineRegister);
175 friend class MachineInstr;
179 //---------------------------------------------------------------------------
180 // class MachineInstr
183 // Representation of each machine instruction.
185 // MachineOpCode must be an enum, defined separately for each target.
186 // E.g., It is defined in SparcInstructionSelection.h for the SPARC.
188 // opCodeMask is used to record variants of an instruction.
189 // E.g., each branch instruction on SPARC has 2 flags (i.e., 4 variants):
190 // ANNUL: if 1: Annul delay slot instruction.
191 // PREDICT-NOT-TAKEN: if 1: predict branch not taken.
192 // Instead of creating 4 different opcodes for BNZ, we create a single
193 // opcode and set bits in opCodeMask for each of these flags.
195 // There are 2 kinds of operands:
197 // (1) Explicit operands of the machine instruction in vector operands[]
199 // (2) "Implicit operands" are values implicitly used or defined by the
200 // machine instruction, such as arguments to a CALL, return value of
201 // a CALL (if any), and return value of a RETURN.
202 //---------------------------------------------------------------------------
204 class MachineInstr : public Annotable, // MachineInstrs are annotable
205 public NonCopyable { // Disable copy operations
206 MachineOpCode opCode; // the opcode
207 OpCodeMask opCodeMask; // extra bits for variants of an opcode
208 std::vector<MachineOperand> operands; // the operands
212 bool isDef, isDefAndUse;
214 ImplicitRef(Value *V, bool D, bool DU) : Val(V), isDef(D), isDefAndUse(DU){}
217 // implicitRefs - Values implicitly referenced by this machine instruction
219 std::vector<ImplicitRef> implicitRefs;
221 // regsUsed - all machine registers used for this instruction, including regs
222 // used to save values across the instruction. This is a bitset of registers.
223 std::vector<bool> regsUsed;
225 // OperandComplete - Return true if it's illegal to add a new operand
226 bool OperandsComplete() const;
228 MachineInstr(MachineOpCode Opcode, OpCodeMask OpcodeMask = 0);
229 MachineInstr(MachineOpCode Opcode, unsigned numOperands, OpCodeMask Mask = 0);
231 /// MachineInstr ctor - This constructor only does a _reserve_ of the
232 /// operands, not a resize for them. It is expected that if you use this that
233 /// you call add* methods below to fill up the operands, instead of the Set
236 MachineInstr(MachineOpCode Opcode, unsigned numOperands, bool XX, bool YY);
239 // Support to rewrite a machine instruction in place: for now, simply
240 // replace() and then set new operands with Set.*Operand methods below.
242 void replace(MachineOpCode Opcode, unsigned numOperands,
243 OpCodeMask Mask = 0x0);
248 const MachineOpCode getOpCode() const { return opCode; }
251 // Information about explicit operands of the instruction
253 unsigned getNumOperands() const { return operands.size(); }
255 const MachineOperand& getOperand(unsigned i) const {
256 assert(i < operands.size() && "getOperand() out of range!");
259 MachineOperand& getOperand(unsigned i) {
260 assert(i < operands.size() && "getOperand() out of range!");
264 MachineOperand::MachineOperandType getOperandType(unsigned i) const {
265 return getOperand(i).getType();
268 bool operandIsDefined(unsigned i) const {
269 return getOperand(i).opIsDef();
272 bool operandIsDefinedAndUsed(unsigned i) const {
273 return getOperand(i).opIsDefAndUse();
277 // Information about implicit operands of the instruction
279 unsigned getNumImplicitRefs() const{ return implicitRefs.size();}
281 const Value* getImplicitRef(unsigned i) const {
282 assert(i < implicitRefs.size() && "getImplicitRef() out of range!");
283 return implicitRefs[i].Val;
285 Value* getImplicitRef(unsigned i) {
286 assert(i < implicitRefs.size() && "getImplicitRef() out of range!");
287 return implicitRefs[i].Val;
290 bool implicitRefIsDefined(unsigned i) const {
291 assert(i < implicitRefs.size() && "implicitRefIsDefined() out of range!");
292 return implicitRefs[i].isDef;
294 bool implicitRefIsDefinedAndUsed(unsigned i) const {
295 assert(i < implicitRefs.size() && "implicitRefIsDef&Used() out of range!");
296 return implicitRefs[i].isDefAndUse;
299 void addImplicitRef(Value* V, bool isDef=false, bool isDefAndUse=false) {
300 implicitRefs.push_back(ImplicitRef(V, isDef, isDefAndUse));
303 void setImplicitRef(unsigned i, Value* V, bool isDef=false,
304 bool isDefAndUse=false) {
305 assert(i < implicitRefs.size() && "setImplicitRef() out of range!");
306 implicitRefs[i] = ImplicitRef(V, isDef, isDefAndUse);
310 // Information about registers used in this instruction
312 const std::vector<bool> &getRegsUsed() const { return regsUsed; }
314 // insertUsedReg - Add a register to the Used registers set...
315 void insertUsedReg(unsigned Reg) {
316 if (Reg >= regsUsed.size())
317 regsUsed.resize(Reg+1);
318 regsUsed[Reg] = true;
325 friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
328 // Define iterators to access the Value operands of the Machine Instruction.
329 // begin() and end() are defined to produce these iterators...
331 template<class _MI, class _V> class ValOpIterator;
332 typedef ValOpIterator<const MachineInstr*,const Value*> const_val_op_iterator;
333 typedef ValOpIterator< MachineInstr*, Value*> val_op_iterator;
336 // Access to set the operands when building the machine instruction
338 void SetMachineOperandVal(unsigned i,
339 MachineOperand::MachineOperandType operandType,
340 Value* V, bool isDef=false, bool isDefAndUse=false);
341 void SetMachineOperandConst(unsigned i,
342 MachineOperand::MachineOperandType operandType,
344 void SetMachineOperandReg(unsigned i, int regNum, bool isDef=false);
346 //===--------------------------------------------------------------------===//
347 // Accessors to add operands when building up machine instructions
350 /// addRegOperand - Add a MO_VirtualRegister operand to the end of the
353 void addRegOperand(Value *V, bool isDef=false, bool isDefAndUse=false) {
354 assert(!OperandsComplete() &&
355 "Trying to add an operand to a machine instr that is already done!");
356 operands.push_back(MachineOperand(V, MachineOperand::MO_VirtualRegister,
357 isDef, isDefAndUse));
360 /// addRegOperand - Add a symbolic virtual register reference...
362 void addRegOperand(int reg) {
363 assert(!OperandsComplete() &&
364 "Trying to add an operand to a machine instr that is already done!");
365 operands.push_back(MachineOperand(reg, MachineOperand::MO_VirtualRegister));
368 /// addPCDispOperand - Add a PC relative displacement operand to the MI
370 void addPCDispOperand(Value *V) {
371 assert(!OperandsComplete() &&
372 "Trying to add an operand to a machine instr that is already done!");
373 operands.push_back(MachineOperand(V, MachineOperand::MO_PCRelativeDisp));
376 /// addMachineRegOperand - Add a virtual register operand to this MachineInstr
378 void addMachineRegOperand(int reg, bool isDef=false) {
379 assert(!OperandsComplete() &&
380 "Trying to add an operand to a machine instr that is already done!");
381 operands.push_back(MachineOperand(reg, MachineOperand::MO_MachineRegister,
386 /// addZeroExtImmOperand - Add a zero extended constant argument to the
387 /// machine instruction.
389 void addZeroExtImmOperand(int64_t intValue) {
390 assert(!OperandsComplete() &&
391 "Trying to add an operand to a machine instr that is already done!");
392 operands.push_back(MachineOperand(intValue,
393 MachineOperand::MO_UnextendedImmed));
396 /// addSignExtImmOperand - Add a zero extended constant argument to the
397 /// machine instruction.
399 void addSignExtImmOperand(int64_t intValue) {
400 assert(!OperandsComplete() &&
401 "Trying to add an operand to a machine instr that is already done!");
402 operands.push_back(MachineOperand(intValue,
403 MachineOperand::MO_SignExtendedImmed));
407 unsigned substituteValue(const Value* oldVal, Value* newVal,
408 bool defsOnly = true);
410 void setOperandHi32(unsigned i) { operands[i].markHi32(); }
411 void setOperandLo32(unsigned i) { operands[i].markLo32(); }
412 void setOperandHi64(unsigned i) { operands[i].markHi64(); }
413 void setOperandLo64(unsigned i) { operands[i].markLo64(); }
416 // SetRegForOperand - Replaces the Value for the operand with its allocated
417 // physical register after register allocation is complete.
419 void SetRegForOperand(unsigned i, int regNum);
422 // Iterator to enumerate machine operands.
424 template<class MITy, class VTy>
425 class ValOpIterator : public forward_iterator<VTy, ptrdiff_t> {
429 void skipToNextVal() {
430 while (i < MI->getNumOperands() &&
431 !((MI->getOperandType(i) == MachineOperand::MO_VirtualRegister ||
432 MI->getOperandType(i) == MachineOperand::MO_CCRegister)
433 && MI->getOperand(i).getVRegValue() != 0))
437 inline ValOpIterator(MITy mi, unsigned I) : i(I), MI(mi) {
442 typedef ValOpIterator<MITy, VTy> _Self;
444 inline VTy operator*() const {
445 return MI->getOperand(i).getVRegValue();
448 const MachineOperand &getMachineOperand() const { return MI->getOperand(i);}
449 MachineOperand &getMachineOperand() { return MI->getOperand(i);}
451 inline VTy operator->() const { return operator*(); }
453 inline bool isDef() const { return MI->getOperand(i).opIsDef(); }
454 inline bool isDefAndUse() const { return MI->getOperand(i).opIsDefAndUse();}
456 inline _Self& operator++() { i++; skipToNextVal(); return *this; }
457 inline _Self operator++(int) { _Self tmp = *this; ++*this; return tmp; }
459 inline bool operator==(const _Self &y) const {
462 inline bool operator!=(const _Self &y) const {
463 return !operator==(y);
466 static _Self begin(MITy MI) {
469 static _Self end(MITy MI) {
470 return _Self(MI, MI->getNumOperands());
474 // define begin() and end()
475 val_op_iterator begin() { return val_op_iterator::begin(this); }
476 val_op_iterator end() { return val_op_iterator::end(this); }
478 const_val_op_iterator begin() const {
479 return const_val_op_iterator::begin(this);
481 const_val_op_iterator end() const {
482 return const_val_op_iterator::end(this);
486 //---------------------------------------------------------------------------
488 //---------------------------------------------------------------------------
490 std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
492 std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
494 void PrintMachineInstructions(const Function *F);