1 //===-- llvm/CodeGen/MachineInstr.h - MachineInstr class --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declaration of the MachineInstr class, which is the
11 // basic representation for all target dependent machine instructions used by
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CODEGEN_MACHINEINSTR_H
17 #define LLVM_CODEGEN_MACHINEINSTR_H
19 #include "Support/Annotation.h"
20 #include "Support/iterator"
27 class MachineBasicBlock;
31 template <typename T> class ilist_traits;
32 template <typename T> class ilist;
34 typedef short MachineOpCode;
36 //===----------------------------------------------------------------------===//
37 /// MOTy - MachineOperandType - This namespace contains an enum that describes
38 /// how the machine operand is used by the instruction: is it read, defined, or
39 /// both? Note that the MachineInstr/Operator class currently uses bool
40 /// arguments to represent this information instead of an enum. Eventually this
41 /// should change over to use this _easier to read_ representation instead.
45 Use, /// This machine operand is only read by the instruction
46 Def, /// This machine operand is only written by the instruction
47 UseAndDef /// This machine operand is read AND written
51 //===----------------------------------------------------------------------===//
52 // class MachineOperand
55 // Representation of each machine instruction operand.
56 // This class is designed so that you can allocate a vector of operands
57 // first and initialize each one later.
59 // E.g, for this VM instruction:
60 // ptr = alloca type, numElements
61 // we generate 2 machine instructions on the SPARC:
63 // mul Constant, Numelements -> Reg
64 // add %sp, Reg -> Ptr
66 // Each instruction has 3 operands, listed above. Of those:
67 // - Reg, NumElements, and Ptr are of operand type MO_Register.
68 // - Constant is of operand type MO_SignExtendedImmed on the SPARC.
70 // For the register operands, the virtual register type is as follows:
72 // - Reg will be of virtual register type MO_MInstrVirtualReg. The field
73 // MachineInstr* minstr will point to the instruction that computes reg.
75 // - %sp will be of virtual register type MO_MachineReg.
76 // The field regNum identifies the machine register.
78 // - NumElements will be of virtual register type MO_VirtualReg.
79 // The field Value* value identifies the value.
81 // - Ptr will also be of virtual register type MO_VirtualReg.
82 // Again, the field Value* value identifies the value.
84 //===----------------------------------------------------------------------===//
86 struct MachineOperand {
87 enum MachineOperandType {
88 MO_VirtualRegister, // virtual register for *value
89 MO_MachineRegister, // pre-assigned machine register `regNum'
94 MO_MachineBasicBlock, // MachineBasicBlock reference
95 MO_FrameIndex, // Abstract Stack Frame Index
96 MO_ConstantPoolIndex, // Address of indexed Constant in Constant Pool
97 MO_ExternalSymbol, // Name of external global symbol
98 MO_GlobalAddress, // Address of a global value
102 // Bit fields of the flags variable used for different operand properties
104 DEFFLAG = 0x01, // this is a def of the operand
105 USEFLAG = 0x02, // this is a use of the operand
106 HIFLAG32 = 0x04, // operand is %hi32(value_or_immedVal)
107 LOFLAG32 = 0x08, // operand is %lo32(value_or_immedVal)
108 HIFLAG64 = 0x10, // operand is %hi64(value_or_immedVal)
109 LOFLAG64 = 0x20, // operand is %lo64(value_or_immedVal)
110 PCRELATIVE = 0x40, // Operand is relative to PC, not a global address
115 Value* value; // BasicBlockVal for a label operand.
116 // ConstantVal for a non-address immediate.
117 // Virtual register for an SSA operand,
118 // including hidden operands required for
119 // the generated machine code.
120 // LLVM global for MO_GlobalAddress.
122 int64_t immedVal; // Constant value for an explicit constant
124 MachineBasicBlock *MBB; // For MO_MachineBasicBlock type
125 std::string *SymbolName; // For MO_ExternalSymbol type
128 char flags; // see bit field definitions above
129 MachineOperandType opType:8; // Pack into 8 bits efficiently after flags.
130 int regNum; // register number for an explicit register
131 // will be set for a value after reg allocation
133 MachineOperand(int64_t ImmVal = 0, MachineOperandType OpTy = MO_VirtualRegister)
139 MachineOperand(int Reg, MachineOperandType OpTy, MOTy::UseType UseTy)
144 case MOTy::Use: flags = USEFLAG; break;
145 case MOTy::Def: flags = DEFFLAG; break;
146 case MOTy::UseAndDef: flags = DEFFLAG | USEFLAG; break;
147 default: assert(0 && "Invalid value for UseTy!");
151 MachineOperand(Value *V, MachineOperandType OpTy, MOTy::UseType UseTy,
152 bool isPCRelative = false)
153 : value(V), opType(OpTy), regNum(-1) {
155 case MOTy::Use: flags = USEFLAG; break;
156 case MOTy::Def: flags = DEFFLAG; break;
157 case MOTy::UseAndDef: flags = DEFFLAG | USEFLAG; break;
158 default: assert(0 && "Invalid value for UseTy!");
160 if (isPCRelative) flags |= PCRELATIVE;
163 MachineOperand(MachineBasicBlock *mbb)
164 : MBB(mbb), flags(0), opType(MO_MachineBasicBlock), regNum(-1) {}
166 MachineOperand(const std::string &SymName, bool isPCRelative)
167 : SymbolName(new std::string(SymName)), flags(isPCRelative ? PCRELATIVE :0),
168 opType(MO_ExternalSymbol), regNum(-1) {}
171 MachineOperand(const MachineOperand &M) : immedVal(M.immedVal),
175 if (isExternalSymbol())
176 SymbolName = new std::string(M.getSymbolName());
180 if (isExternalSymbol())
184 const MachineOperand &operator=(const MachineOperand &MO) {
185 if (isExternalSymbol()) // if old operand had a symbol name,
186 delete SymbolName; // release old memory
187 immedVal = MO.immedVal;
191 if (isExternalSymbol())
192 SymbolName = new std::string(MO.getSymbolName());
196 /// getType - Returns the MachineOperandType for this operand.
198 MachineOperandType getType() const { return opType; }
200 /// isPCRelative - This returns the value of the PCRELATIVE flag, which
201 /// indicates whether this operand should be emitted as a PC relative value
202 /// instead of a global address. This is used for operands of the forms:
203 /// MachineBasicBlock, GlobalAddress, ExternalSymbol
205 bool isPCRelative() const { return (flags & PCRELATIVE) != 0; }
207 /// isRegister - Return true if this operand is a register operand. The X86
208 /// backend currently can't decide whether to use MO_MR or MO_VR to represent
209 /// them, so we accept both.
211 /// Note: The sparc backend should not use this method.
213 bool isRegister() const {
214 return opType == MO_MachineRegister || opType == MO_VirtualRegister;
217 bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
218 bool isPCRelativeDisp() const { return opType == MO_PCRelativeDisp; }
219 bool isImmediate() const {
220 return opType == MO_SignExtendedImmed || opType == MO_UnextendedImmed;
222 bool isFrameIndex() const { return opType == MO_FrameIndex; }
223 bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
224 bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
225 bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
227 Value* getVRegValue() const {
228 assert(opType == MO_VirtualRegister || opType == MO_CCRegister ||
232 Value* getVRegValueOrNull() const {
233 return (opType == MO_VirtualRegister || opType == MO_CCRegister ||
234 isPCRelativeDisp()) ? value : NULL;
236 int getMachineRegNum() const {
237 assert(opType == MO_MachineRegister);
240 int64_t getImmedValue() const { assert(isImmediate()); return immedVal; }
241 void setImmedValue(int64_t ImmVal) { assert(isImmediate()); immedVal=ImmVal; }
243 MachineBasicBlock *getMachineBasicBlock() const {
244 assert(isMachineBasicBlock() && "Can't get MBB in non-MBB operand!");
247 int getFrameIndex() const { assert(isFrameIndex()); return immedVal; }
248 unsigned getConstantPoolIndex() const {
249 assert(isConstantPoolIndex());
253 GlobalValue *getGlobal() const {
254 assert(isGlobalAddress());
255 return (GlobalValue*)value;
258 const std::string &getSymbolName() const {
259 assert(isExternalSymbol());
263 bool isUse () const { return flags & USEFLAG; }
264 MachineOperand& setUse () { flags |= USEFLAG; return *this; }
265 bool isDef () const { return flags & DEFFLAG; }
266 MachineOperand& setDef () { flags |= DEFFLAG; return *this; }
267 bool isHiBits32 () const { return flags & HIFLAG32; }
268 bool isLoBits32 () const { return flags & LOFLAG32; }
269 bool isHiBits64 () const { return flags & HIFLAG64; }
270 bool isLoBits64 () const { return flags & LOFLAG64; }
272 // used to check if a machine register has been allocated to this operand
273 bool hasAllocatedReg() const {
274 return (regNum >= 0 &&
275 (opType == MO_VirtualRegister || opType == MO_CCRegister ||
276 opType == MO_MachineRegister));
279 // used to get the reg number if when one is allocated
280 unsigned getReg() const {
281 assert(hasAllocatedReg());
285 // ********** TODO: get rid of this duplicate code! ***********
286 void setReg(unsigned Reg) {
287 assert(hasAllocatedReg() && "This operand cannot have a register number!");
291 friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
295 // Construction methods needed for fine-grain control.
296 // These must be accessed via coresponding methods in MachineInstr.
297 void markHi32() { flags |= HIFLAG32; }
298 void markLo32() { flags |= LOFLAG32; }
299 void markHi64() { flags |= HIFLAG64; }
300 void markLo64() { flags |= LOFLAG64; }
302 // Replaces the Value with its corresponding physical register after
303 // register allocation is complete
304 void setRegForValue(int reg) {
305 assert(opType == MO_VirtualRegister || opType == MO_CCRegister ||
306 opType == MO_MachineRegister);
310 friend class MachineInstr;
314 //===----------------------------------------------------------------------===//
315 // class MachineInstr
318 // Representation of each machine instruction.
320 // MachineOpCode must be an enum, defined separately for each target.
321 // E.g., It is defined in SparcInstructionSelection.h for the SPARC.
323 // There are 2 kinds of operands:
325 // (1) Explicit operands of the machine instruction in vector operands[]
327 // (2) "Implicit operands" are values implicitly used or defined by the
328 // machine instruction, such as arguments to a CALL, return value of
329 // a CALL (if any), and return value of a RETURN.
330 //===----------------------------------------------------------------------===//
333 short Opcode; // the opcode
334 unsigned char numImplicitRefs; // number of implicit operands
335 std::vector<MachineOperand> operands; // the operands
336 MachineInstr* prev, *next; // links for our intrusive list
337 MachineBasicBlock* parent; // pointer to the owning basic block
338 // OperandComplete - Return true if it's illegal to add a new operand
339 bool OperandsComplete() const;
341 MachineInstr(const MachineInstr &); // DO NOT IMPLEMENT
342 void operator=(const MachineInstr&); // DO NOT IMPLEMENT
345 // Intrusive list support
347 friend class ilist_traits<MachineInstr>;
348 MachineInstr() : Opcode(0), numImplicitRefs(0) { /* used only by ilist */ }
351 MachineInstr(short Opcode, unsigned numOperands);
353 /// MachineInstr ctor - This constructor only does a _reserve_ of the
354 /// operands, not a resize for them. It is expected that if you use this that
355 /// you call add* methods below to fill up the operands, instead of the Set
356 /// methods. Eventually, the "resizing" ctors will be phased out.
358 MachineInstr(short Opcode, unsigned numOperands, bool XX, bool YY);
360 /// MachineInstr ctor - Work exactly the same as the ctor above, except that
361 /// the MachineInstr is created and added to the end of the specified basic
364 MachineInstr(MachineBasicBlock *MBB, short Opcode, unsigned numOps);
366 const MachineBasicBlock* getParent() const { return parent; }
367 MachineBasicBlock* getParent() { return parent; }
369 /// Accessors for opcode.
371 const int getOpcode() const { return Opcode; }
373 /// Access to explicit operands of the instruction.
375 unsigned getNumOperands() const { return operands.size() - numImplicitRefs; }
377 const MachineOperand& getOperand(unsigned i) const {
378 assert(i < getNumOperands() && "getOperand() out of range!");
381 MachineOperand& getOperand(unsigned i) {
382 assert(i < getNumOperands() && "getOperand() out of range!");
387 // Access to explicit or implicit operands of the instruction
388 // This returns the i'th entry in the operand vector.
389 // That represents the i'th explicit operand or the (i-N)'th implicit operand,
390 // depending on whether i < N or i >= N.
392 const MachineOperand& getExplOrImplOperand(unsigned i) const {
393 assert(i < operands.size() && "getExplOrImplOperand() out of range!");
394 return (i < getNumOperands()? getOperand(i)
395 : getImplicitOp(i - getNumOperands()));
399 // Access to implicit operands of the instruction
401 unsigned getNumImplicitRefs() const{ return numImplicitRefs; }
403 MachineOperand& getImplicitOp(unsigned i) {
404 assert(i < numImplicitRefs && "implicit ref# out of range!");
405 return operands[i + operands.size() - numImplicitRefs];
407 const MachineOperand& getImplicitOp(unsigned i) const {
408 assert(i < numImplicitRefs && "implicit ref# out of range!");
409 return operands[i + operands.size() - numImplicitRefs];
412 Value* getImplicitRef(unsigned i) {
413 return getImplicitOp(i).getVRegValue();
415 const Value* getImplicitRef(unsigned i) const {
416 return getImplicitOp(i).getVRegValue();
419 void addImplicitRef(Value* V, bool isDef = false, bool isDefAndUse = false) {
421 addRegOperand(V, isDef, isDefAndUse);
423 void setImplicitRef(unsigned i, Value* V) {
424 assert(i < getNumImplicitRefs() && "setImplicitRef() out of range!");
425 SetMachineOperandVal(i + getNumOperands(),
426 MachineOperand::MO_VirtualRegister, V);
432 void print(std::ostream &OS, const TargetMachine &TM) const;
434 friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
437 // Define iterators to access the Value operands of the Machine Instruction.
438 // Note that these iterators only enumerate the explicit operands.
439 // begin() and end() are defined to produce these iterators...
441 template<class _MI, class _V> class ValOpIterator;
442 typedef ValOpIterator<const MachineInstr*,const Value*> const_val_op_iterator;
443 typedef ValOpIterator< MachineInstr*, Value*> val_op_iterator;
446 //===--------------------------------------------------------------------===//
447 // Accessors to add operands when building up machine instructions
450 /// addRegOperand - Add a MO_VirtualRegister operand to the end of the
453 void addRegOperand(Value *V, bool isDef, bool isDefAndUse=false) {
454 assert(!OperandsComplete() &&
455 "Trying to add an operand to a machine instr that is already done!");
456 operands.push_back(MachineOperand(V, MachineOperand::MO_VirtualRegister,
457 !isDef ? MOTy::Use : (isDefAndUse ? MOTy::UseAndDef : MOTy::Def)));
460 void addRegOperand(Value *V, MOTy::UseType UTy = MOTy::Use,
461 bool isPCRelative = false) {
462 assert(!OperandsComplete() &&
463 "Trying to add an operand to a machine instr that is already done!");
464 operands.push_back(MachineOperand(V, MachineOperand::MO_VirtualRegister,
468 void addCCRegOperand(Value *V, MOTy::UseType UTy = MOTy::Use) {
469 assert(!OperandsComplete() &&
470 "Trying to add an operand to a machine instr that is already done!");
471 operands.push_back(MachineOperand(V, MachineOperand::MO_CCRegister, UTy,
476 /// addRegOperand - Add a symbolic virtual register reference...
478 void addRegOperand(int reg, bool isDef) {
479 assert(!OperandsComplete() &&
480 "Trying to add an operand to a machine instr that is already done!");
481 operands.push_back(MachineOperand(reg, MachineOperand::MO_VirtualRegister,
482 isDef ? MOTy::Def : MOTy::Use));
485 /// addRegOperand - Add a symbolic virtual register reference...
487 void addRegOperand(int reg, MOTy::UseType UTy = MOTy::Use) {
488 assert(!OperandsComplete() &&
489 "Trying to add an operand to a machine instr that is already done!");
490 operands.push_back(MachineOperand(reg, MachineOperand::MO_VirtualRegister,
494 /// addPCDispOperand - Add a PC relative displacement operand to the MI
496 void addPCDispOperand(Value *V) {
497 assert(!OperandsComplete() &&
498 "Trying to add an operand to a machine instr that is already done!");
499 operands.push_back(MachineOperand(V, MachineOperand::MO_PCRelativeDisp,
503 /// addMachineRegOperand - Add a virtual register operand to this MachineInstr
505 void addMachineRegOperand(int reg, bool isDef) {
506 assert(!OperandsComplete() &&
507 "Trying to add an operand to a machine instr that is already done!");
508 operands.push_back(MachineOperand(reg, MachineOperand::MO_MachineRegister,
509 isDef ? MOTy::Def : MOTy::Use));
512 /// addMachineRegOperand - Add a virtual register operand to this MachineInstr
514 void addMachineRegOperand(int reg, MOTy::UseType UTy = MOTy::Use) {
515 assert(!OperandsComplete() &&
516 "Trying to add an operand to a machine instr that is already done!");
517 operands.push_back(MachineOperand(reg, MachineOperand::MO_MachineRegister,
521 /// addZeroExtImmOperand - Add a zero extended constant argument to the
522 /// machine instruction.
524 void addZeroExtImmOperand(int64_t intValue) {
525 assert(!OperandsComplete() &&
526 "Trying to add an operand to a machine instr that is already done!");
527 operands.push_back(MachineOperand(intValue,
528 MachineOperand::MO_UnextendedImmed));
531 /// addSignExtImmOperand - Add a zero extended constant argument to the
532 /// machine instruction.
534 void addSignExtImmOperand(int64_t intValue) {
535 assert(!OperandsComplete() &&
536 "Trying to add an operand to a machine instr that is already done!");
537 operands.push_back(MachineOperand(intValue,
538 MachineOperand::MO_SignExtendedImmed));
541 void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
542 assert(!OperandsComplete() &&
543 "Trying to add an operand to a machine instr that is already done!");
544 operands.push_back(MachineOperand(MBB));
547 /// addFrameIndexOperand - Add an abstract frame index to the instruction
549 void addFrameIndexOperand(unsigned Idx) {
550 assert(!OperandsComplete() &&
551 "Trying to add an operand to a machine instr that is already done!");
552 operands.push_back(MachineOperand(Idx, MachineOperand::MO_FrameIndex));
555 /// addConstantPoolndexOperand - Add a constant pool object index to the
558 void addConstantPoolIndexOperand(unsigned I) {
559 assert(!OperandsComplete() &&
560 "Trying to add an operand to a machine instr that is already done!");
561 operands.push_back(MachineOperand(I, MachineOperand::MO_ConstantPoolIndex));
564 void addGlobalAddressOperand(GlobalValue *GV, bool isPCRelative) {
565 assert(!OperandsComplete() &&
566 "Trying to add an operand to a machine instr that is already done!");
567 operands.push_back(MachineOperand((Value*)GV,
568 MachineOperand::MO_GlobalAddress,
569 MOTy::Use, isPCRelative));
572 /// addExternalSymbolOperand - Add an external symbol operand to this instr
574 void addExternalSymbolOperand(const std::string &SymName, bool isPCRelative) {
575 operands.push_back(MachineOperand(SymName, isPCRelative));
578 //===--------------------------------------------------------------------===//
579 // Accessors used to modify instructions in place.
581 // FIXME: Move this stuff to MachineOperand itself!
583 /// replace - Support to rewrite a machine instruction in place: for now,
584 /// simply replace() and then set new operands with Set.*Operand methods
587 void replace(short Opcode, unsigned numOperands);
589 /// setOpcode - Replace the opcode of the current instruction with a new one.
591 void setOpcode(unsigned Op) { Opcode = Op; }
593 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
594 /// fewer operand than it started with.
596 void RemoveOperand(unsigned i) {
597 operands.erase(operands.begin()+i);
600 // Access to set the operands when building the machine instruction
602 void SetMachineOperandVal (unsigned i,
603 MachineOperand::MachineOperandType operandType,
606 void SetMachineOperandConst (unsigned i,
607 MachineOperand::MachineOperandType operandType,
610 void SetMachineOperandReg(unsigned i, int regNum);
613 unsigned substituteValue(const Value* oldVal, Value* newVal,
614 bool defsOnly, bool notDefsAndUses,
615 bool& someArgsWereIgnored);
617 void setOperandHi32(unsigned i) { operands[i].markHi32(); }
618 void setOperandLo32(unsigned i) { operands[i].markLo32(); }
619 void setOperandHi64(unsigned i) { operands[i].markHi64(); }
620 void setOperandLo64(unsigned i) { operands[i].markLo64(); }
623 // SetRegForOperand -
624 // SetRegForImplicitRef -
625 // Mark an explicit or implicit operand with its allocated physical register.
627 void SetRegForOperand(unsigned i, int regNum);
628 void SetRegForImplicitRef(unsigned i, int regNum);
631 // Iterator to enumerate machine operands.
633 template<class MITy, class VTy>
634 class ValOpIterator : public forward_iterator<VTy, ptrdiff_t> {
638 void skipToNextVal() {
639 while (i < MI->getNumOperands() &&
640 !( (MI->getOperand(i).getType() == MachineOperand::MO_VirtualRegister ||
641 MI->getOperand(i).getType() == MachineOperand::MO_CCRegister)
642 && MI->getOperand(i).getVRegValue() != 0))
646 inline ValOpIterator(MITy mi, unsigned I) : i(I), MI(mi) {
651 typedef ValOpIterator<MITy, VTy> _Self;
653 inline VTy operator*() const {
654 return MI->getOperand(i).getVRegValue();
657 const MachineOperand &getMachineOperand() const { return MI->getOperand(i);}
658 MachineOperand &getMachineOperand() { return MI->getOperand(i);}
660 inline VTy operator->() const { return operator*(); }
662 inline bool isUse() const { return MI->getOperand(i).isUse(); }
663 inline bool isDef() const { return MI->getOperand(i).isDef(); }
665 inline _Self& operator++() { i++; skipToNextVal(); return *this; }
666 inline _Self operator++(int) { _Self tmp = *this; ++*this; return tmp; }
668 inline bool operator==(const _Self &y) const {
671 inline bool operator!=(const _Self &y) const {
672 return !operator==(y);
675 static _Self begin(MITy MI) {
678 static _Self end(MITy MI) {
679 return _Self(MI, MI->getNumOperands());
683 // define begin() and end()
684 val_op_iterator begin() { return val_op_iterator::begin(this); }
685 val_op_iterator end() { return val_op_iterator::end(this); }
687 const_val_op_iterator begin() const {
688 return const_val_op_iterator::begin(this);
690 const_val_op_iterator end() const {
691 return const_val_op_iterator::end(this);
695 //===----------------------------------------------------------------------===//
698 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
699 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
700 void PrintMachineInstructions(const Function *F);
702 } // End llvm namespace