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 "llvm/ADT/iterator"
20 #include "llvm/Support/DataTypes.h"
28 class MachineBasicBlock;
32 template <typename T> struct ilist_traits;
33 template <typename T> struct ilist;
35 typedef short MachineOpCode;
37 //===----------------------------------------------------------------------===//
38 // class MachineOperand
41 // Representation of each machine instruction operand.
42 // This class is designed so that you can allocate a vector of operands
43 // first and initialize each one later.
45 // E.g, for this VM instruction:
46 // ptr = alloca type, numElements
47 // we generate 2 machine instructions on the SPARC:
49 // mul Constant, Numelements -> Reg
50 // add %sp, Reg -> Ptr
52 // Each instruction has 3 operands, listed above. Of those:
53 // - Reg, NumElements, and Ptr are of operand type MO_Register.
54 // - Constant is of operand type MO_SignExtendedImmed on the SPARC.
56 // For the register operands, the virtual register type is as follows:
58 // - Reg will be of virtual register type MO_MInstrVirtualReg. The field
59 // MachineInstr* minstr will point to the instruction that computes reg.
61 // - %sp will be of virtual register type MO_MachineReg.
62 // The field regNum identifies the machine register.
64 // - NumElements will be of virtual register type MO_VirtualReg.
65 // The field Value* value identifies the value.
67 // - Ptr will also be of virtual register type MO_VirtualReg.
68 // Again, the field Value* value identifies the value.
70 //===----------------------------------------------------------------------===//
72 struct MachineOperand {
74 // Bit fields of the flags variable used for different operand properties
76 DEFFLAG = 0x01, // this is a def of the operand
77 USEFLAG = 0x02, // this is a use of the operand
78 HIFLAG32 = 0x04, // operand is %hi32(value_or_immedVal)
79 LOFLAG32 = 0x08, // operand is %lo32(value_or_immedVal)
80 HIFLAG64 = 0x10, // operand is %hi64(value_or_immedVal)
81 LOFLAG64 = 0x20, // operand is %lo64(value_or_immedVal)
82 PCRELATIVE = 0x40 // Operand is relative to PC, not a global address
86 // UseType - This enum describes how the machine operand is used by
87 // the instruction. Note that the MachineInstr/Operator class
88 // currently uses bool arguments to represent this information
89 // instead of an enum. Eventually this should change over to use
90 // this _easier to read_ representation instead.
93 Use = USEFLAG, /// only read
94 Def = DEFFLAG, /// only written
95 UseAndDef = Use | Def /// read AND written
98 enum MachineOperandType {
99 MO_VirtualRegister, // virtual register for *value
100 MO_MachineRegister, // pre-assigned machine register `regNum'
102 MO_SignExtendedImmed,
105 MO_MachineBasicBlock, // MachineBasicBlock reference
106 MO_FrameIndex, // Abstract Stack Frame Index
107 MO_ConstantPoolIndex, // Address of indexed Constant in Constant Pool
108 MO_ExternalSymbol, // Name of external global symbol
109 MO_GlobalAddress // Address of a global value
114 Value* value; // BasicBlockVal for a label operand.
115 // ConstantVal for a non-address immediate.
116 // Virtual register for an SSA operand,
117 // including hidden operands required for
118 // the generated machine code.
119 // LLVM global for MO_GlobalAddress.
121 int64_t immedVal; // Constant value for an explicit constant
123 MachineBasicBlock *MBB; // For MO_MachineBasicBlock type
124 const char *SymbolName; // For MO_ExternalSymbol type
127 char flags; // see bit field definitions above
128 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 int offset; // Offset to address of global or external, only
134 // valid for MO_GlobalAddress, MO_ExternalSym
135 // and MO_ConstantPoolIndex
138 void zeroContents () {
139 memset (&contents, 0, sizeof (contents));
140 memset (&extra, 0, sizeof (extra));
143 MachineOperand(int64_t ImmVal = 0,
144 MachineOperandType OpTy = MO_VirtualRegister, int Offset = 0)
145 : flags(0), opType(OpTy) {
147 contents.immedVal = ImmVal;
148 if (OpTy == MachineOperand::MO_ConstantPoolIndex)
149 extra.offset = Offset;
154 MachineOperand(int Reg, MachineOperandType OpTy, UseType UseTy)
155 : flags(UseTy), opType(OpTy) {
160 MachineOperand(Value *V, MachineOperandType OpTy, UseType UseTy,
161 bool isPCRelative = false)
162 : flags(UseTy | (isPCRelative?PCRELATIVE:0)), opType(OpTy) {
163 assert(OpTy != MachineOperand::MO_GlobalAddress);
169 MachineOperand(GlobalValue *V, MachineOperandType OpTy, UseType UseTy,
170 bool isPCRelative = false, int Offset = 0)
171 : flags(UseTy | (isPCRelative?PCRELATIVE:0)), opType(OpTy) {
172 assert(OpTy == MachineOperand::MO_GlobalAddress);
174 contents.value = (Value*)V;
175 extra.offset = Offset;
178 MachineOperand(MachineBasicBlock *mbb)
179 : flags(0), opType(MO_MachineBasicBlock) {
185 MachineOperand(const char *SymName, bool isPCRelative, int Offset)
186 : flags(isPCRelative?PCRELATIVE:0), opType(MO_ExternalSymbol) {
188 contents.SymbolName = SymName;
189 extra.offset = Offset;
193 MachineOperand(const MachineOperand &M)
194 : flags(M.flags), opType(M.opType) {
196 contents = M.contents;
203 const MachineOperand &operator=(const MachineOperand &MO) {
204 contents = MO.contents;
211 /// getType - Returns the MachineOperandType for this operand.
213 MachineOperandType getType() const { return opType; }
215 /// getUseType - Returns the MachineOperandUseType of this operand.
217 UseType getUseType() const { return UseType(flags & (USEFLAG|DEFFLAG)); }
219 /// isPCRelative - This returns the value of the PCRELATIVE flag, which
220 /// indicates whether this operand should be emitted as a PC relative value
221 /// instead of a global address. This is used for operands of the forms:
222 /// MachineBasicBlock, GlobalAddress, ExternalSymbol
224 bool isPCRelative() const { return (flags & PCRELATIVE) != 0; }
226 /// isRegister - Return true if this operand is a register operand. The X86
227 /// backend currently can't decide whether to use MO_MR or MO_VR to represent
228 /// them, so we accept both.
230 /// Note: The sparc backend should not use this method.
232 bool isRegister() const {
233 return opType == MO_MachineRegister || opType == MO_VirtualRegister;
236 /// Accessors that tell you what kind of MachineOperand you're looking at.
238 bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
239 bool isPCRelativeDisp() const { return opType == MO_PCRelativeDisp; }
240 bool isImmediate() const {
241 return opType == MO_SignExtendedImmed || opType == MO_UnextendedImmed;
243 bool isFrameIndex() const { return opType == MO_FrameIndex; }
244 bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
245 bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
246 bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
248 /// getVRegValueOrNull - Get the Value* out of a MachineOperand if it
249 /// has one. This is deprecated and only used by the SPARC v9 backend.
251 Value* getVRegValueOrNull() const {
252 return (opType == MO_VirtualRegister || opType == MO_CCRegister ||
253 isPCRelativeDisp()) ? contents.value : NULL;
256 /// MachineOperand accessors that only work on certain types of
257 /// MachineOperand...
259 Value* getVRegValue() const {
260 assert ((opType == MO_VirtualRegister || opType == MO_CCRegister
261 || isPCRelativeDisp()) && "Wrong MachineOperand accessor");
262 return contents.value;
264 int getMachineRegNum() const {
265 assert(opType == MO_MachineRegister && "Wrong MachineOperand accessor");
268 int64_t getImmedValue() const {
269 assert(isImmediate() && "Wrong MachineOperand accessor");
270 return contents.immedVal;
272 MachineBasicBlock *getMachineBasicBlock() const {
273 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
276 void setMachineBasicBlock(MachineBasicBlock *MBB) {
277 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
280 int getFrameIndex() const {
281 assert(isFrameIndex() && "Wrong MachineOperand accessor");
282 return (int)contents.immedVal;
284 unsigned getConstantPoolIndex() const {
285 assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
286 return (unsigned)contents.immedVal;
288 GlobalValue *getGlobal() const {
289 assert(isGlobalAddress() && "Wrong MachineOperand accessor");
290 return (GlobalValue*)contents.value;
292 int getOffset() const {
293 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
294 "Wrong MachineOperand accessor");
297 const char *getSymbolName() const {
298 assert(isExternalSymbol() && "Wrong MachineOperand accessor");
299 return contents.SymbolName;
302 /// MachineOperand methods for testing that work on any kind of
303 /// MachineOperand...
305 bool isUse () const { return flags & USEFLAG; }
306 MachineOperand& setUse () { flags |= USEFLAG; return *this; }
307 bool isDef () const { return flags & DEFFLAG; }
308 MachineOperand& setDef () { flags |= DEFFLAG; return *this; }
309 bool isHiBits32 () const { return flags & HIFLAG32; }
310 bool isLoBits32 () const { return flags & LOFLAG32; }
311 bool isHiBits64 () const { return flags & HIFLAG64; }
312 bool isLoBits64 () const { return flags & LOFLAG64; }
314 /// hasAllocatedReg - Returns true iff a machine register has been
315 /// allocated to this operand.
317 bool hasAllocatedReg() const {
318 return (extra.regNum >= 0 &&
319 (opType == MO_VirtualRegister || opType == MO_CCRegister ||
320 opType == MO_MachineRegister));
323 /// getReg - Returns the register number. It is a runtime error to call this
324 /// if a register is not allocated.
326 unsigned getReg() const {
327 assert(hasAllocatedReg());
331 /// MachineOperand mutators...
333 void setReg(unsigned Reg) {
334 // This method's comment used to say: 'TODO: get rid of this duplicate
335 // code.' It's not clear where the duplication is.
336 assert(hasAllocatedReg() && "This operand cannot have a register number!");
340 void setValueReg(Value *val) {
341 assert(getVRegValueOrNull() != 0 && "Original operand must of type Value*");
342 contents.value = val;
345 void setImmedValue(int immVal) {
346 assert(isImmediate() && "Wrong MachineOperand mutator");
347 contents.immedVal = immVal;
350 void setOffset(int Offset) {
351 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
352 "Wrong MachineOperand accessor");
353 extra.offset = Offset;
356 friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
358 /// markHi32, markLo32, etc. - These methods are deprecated and only used by
359 /// the SPARC v9 back-end.
361 void markHi32() { flags |= HIFLAG32; }
362 void markLo32() { flags |= LOFLAG32; }
363 void markHi64() { flags |= HIFLAG64; }
364 void markLo64() { flags |= LOFLAG64; }
367 /// setRegForValue - Replaces the Value with its corresponding physical
368 /// register after register allocation is complete. This is deprecated
369 /// and only used by the SPARC v9 back-end.
371 void setRegForValue(int reg) {
372 assert(opType == MO_VirtualRegister || opType == MO_CCRegister ||
373 opType == MO_MachineRegister);
377 friend class MachineInstr;
381 //===----------------------------------------------------------------------===//
382 // class MachineInstr
385 // Representation of each machine instruction.
387 // MachineOpCode must be an enum, defined separately for each target.
388 // E.g., It is defined in SparcInstructionSelection.h for the SPARC.
390 // There are 2 kinds of operands:
392 // (1) Explicit operands of the machine instruction in vector operands[]
394 // (2) "Implicit operands" are values implicitly used or defined by the
395 // machine instruction, such as arguments to a CALL, return value of
396 // a CALL (if any), and return value of a RETURN.
397 //===----------------------------------------------------------------------===//
400 short Opcode; // the opcode
401 std::vector<MachineOperand> operands; // the operands
402 MachineInstr* prev, *next; // links for our intrusive list
403 MachineBasicBlock* parent; // pointer to the owning basic block
405 // OperandComplete - Return true if it's illegal to add a new operand
406 bool OperandsComplete() const;
408 //Constructor used by clone() method
409 MachineInstr(const MachineInstr&);
411 void operator=(const MachineInstr&); // DO NOT IMPLEMENT
413 // Intrusive list support
415 friend struct ilist_traits<MachineInstr>;
418 MachineInstr(short Opcode, unsigned numOperands);
420 /// MachineInstr ctor - This constructor only does a _reserve_ of the
421 /// operands, not a resize for them. It is expected that if you use this that
422 /// you call add* methods below to fill up the operands, instead of the Set
423 /// methods. Eventually, the "resizing" ctors will be phased out.
425 MachineInstr(short Opcode, unsigned numOperands, bool XX, bool YY);
427 /// MachineInstr ctor - Work exactly the same as the ctor above, except that
428 /// the MachineInstr is created and added to the end of the specified basic
431 MachineInstr(MachineBasicBlock *MBB, short Opcode, unsigned numOps);
435 const MachineBasicBlock* getParent() const { return parent; }
436 MachineBasicBlock* getParent() { return parent; }
438 /// getOpcode - Returns the opcode of this MachineInstr.
440 const int getOpcode() const { return Opcode; }
442 /// Access to explicit operands of the instruction.
444 unsigned getNumOperands() const { return operands.size(); }
446 const MachineOperand& getOperand(unsigned i) const {
447 assert(i < getNumOperands() && "getOperand() out of range!");
450 MachineOperand& getOperand(unsigned i) {
451 assert(i < getNumOperands() && "getOperand() out of range!");
456 /// clone - Create a copy of 'this' instruction that is identical in
457 /// all ways except the the instruction has no parent, prev, or next.
458 MachineInstr* clone() const;
460 /// removeFromParent - This method unlinks 'this' from the containing basic
461 /// block, and returns it, but does not delete it.
462 MachineInstr *removeFromParent();
464 /// eraseFromParent - This method unlinks 'this' from the containing basic
465 /// block and deletes it.
466 void eraseFromParent() {
467 delete removeFromParent();
473 void print(std::ostream &OS, const TargetMachine *TM) const;
475 friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
477 //===--------------------------------------------------------------------===//
478 // Accessors to add operands when building up machine instructions
481 /// addRegOperand - Add a MO_VirtualRegister operand to the end of the
484 void addRegOperand(Value *V, bool isDef, bool isDefAndUse=false) {
485 assert(!OperandsComplete() &&
486 "Trying to add an operand to a machine instr that is already done!");
488 MachineOperand(V, MachineOperand::MO_VirtualRegister,
489 !isDef ? MachineOperand::Use :
490 (isDefAndUse ? MachineOperand::UseAndDef :
491 MachineOperand::Def)));
494 void addRegOperand(Value *V,
495 MachineOperand::UseType UTy = MachineOperand::Use,
496 bool isPCRelative = false) {
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_VirtualRegister,
503 void addCCRegOperand(Value *V,
504 MachineOperand::UseType UTy = MachineOperand::Use) {
505 assert(!OperandsComplete() &&
506 "Trying to add an operand to a machine instr that is already done!");
507 operands.push_back(MachineOperand(V, MachineOperand::MO_CCRegister, UTy,
512 /// addRegOperand - Add a symbolic virtual register reference...
514 void addRegOperand(int reg, bool isDef) {
515 assert(!OperandsComplete() &&
516 "Trying to add an operand to a machine instr that is already done!");
518 MachineOperand(reg, MachineOperand::MO_VirtualRegister,
519 isDef ? MachineOperand::Def : MachineOperand::Use));
522 /// addRegOperand - Add a symbolic virtual register reference...
524 void addRegOperand(int reg,
525 MachineOperand::UseType UTy = MachineOperand::Use) {
526 assert(!OperandsComplete() &&
527 "Trying to add an operand to a machine instr that is already done!");
529 MachineOperand(reg, MachineOperand::MO_VirtualRegister, UTy));
532 /// addPCDispOperand - Add a PC relative displacement operand to the MI
534 void addPCDispOperand(Value *V) {
535 assert(!OperandsComplete() &&
536 "Trying to add an operand to a machine instr that is already done!");
538 MachineOperand(V, MachineOperand::MO_PCRelativeDisp,MachineOperand::Use));
541 /// addMachineRegOperand - Add a virtual register operand to this MachineInstr
543 void addMachineRegOperand(int reg, bool isDef) {
544 assert(!OperandsComplete() &&
545 "Trying to add an operand to a machine instr that is already done!");
547 MachineOperand(reg, MachineOperand::MO_MachineRegister,
548 isDef ? MachineOperand::Def : MachineOperand::Use));
551 /// addMachineRegOperand - Add a virtual register operand to this MachineInstr
553 void addMachineRegOperand(int reg,
554 MachineOperand::UseType UTy = MachineOperand::Use) {
555 assert(!OperandsComplete() &&
556 "Trying to add an operand to a machine instr that is already done!");
558 MachineOperand(reg, MachineOperand::MO_MachineRegister, UTy));
561 /// addZeroExtImmOperand - Add a zero extended constant argument to the
562 /// machine instruction.
564 void addZeroExtImmOperand(int intValue) {
565 assert(!OperandsComplete() &&
566 "Trying to add an operand to a machine instr that is already done!");
568 MachineOperand(intValue, MachineOperand::MO_UnextendedImmed));
571 /// addZeroExtImm64Operand - Add a zero extended 64-bit constant argument
572 /// to the machine instruction.
574 void addZeroExtImm64Operand(uint64_t intValue) {
575 assert(!OperandsComplete() &&
576 "Trying to add an operand to a machine instr that is already done!");
578 MachineOperand(intValue, MachineOperand::MO_UnextendedImmed));
581 /// addSignExtImmOperand - Add a zero extended constant argument to the
582 /// machine instruction.
584 void addSignExtImmOperand(int intValue) {
585 assert(!OperandsComplete() &&
586 "Trying to add an operand to a machine instr that is already done!");
588 MachineOperand(intValue, MachineOperand::MO_SignExtendedImmed));
591 void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
592 assert(!OperandsComplete() &&
593 "Trying to add an operand to a machine instr that is already done!");
594 operands.push_back(MachineOperand(MBB));
597 /// addFrameIndexOperand - Add an abstract frame index to the instruction
599 void addFrameIndexOperand(unsigned Idx) {
600 assert(!OperandsComplete() &&
601 "Trying to add an operand to a machine instr that is already done!");
602 operands.push_back(MachineOperand(Idx, MachineOperand::MO_FrameIndex));
605 /// addConstantPoolndexOperand - Add a constant pool object index to the
608 void addConstantPoolIndexOperand(unsigned I, int Offset=0) {
609 assert(!OperandsComplete() &&
610 "Trying to add an operand to a machine instr that is already done!");
611 operands.push_back(MachineOperand(I, MachineOperand::MO_ConstantPoolIndex));
614 void addGlobalAddressOperand(GlobalValue *GV, bool isPCRelative, int Offset) {
615 assert(!OperandsComplete() &&
616 "Trying to add an operand to a machine instr that is already done!");
618 MachineOperand(GV, MachineOperand::MO_GlobalAddress,
619 MachineOperand::Use, isPCRelative, Offset));
622 /// addExternalSymbolOperand - Add an external symbol operand to this instr
624 void addExternalSymbolOperand(const char *SymName, bool isPCRelative) {
625 operands.push_back(MachineOperand(SymName, isPCRelative, 0));
628 //===--------------------------------------------------------------------===//
629 // Accessors used to modify instructions in place.
631 // FIXME: Move this stuff to MachineOperand itself!
633 /// setOpcode - Replace the opcode of the current instruction with a new one.
635 void setOpcode(unsigned Op) { Opcode = Op; }
637 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
638 /// fewer operand than it started with.
640 void RemoveOperand(unsigned i) {
641 operands.erase(operands.begin()+i);
644 // Access to set the operands when building the machine instruction
646 void SetMachineOperandVal(unsigned i,
647 MachineOperand::MachineOperandType operandType,
650 void SetMachineOperandConst(unsigned i,
651 MachineOperand::MachineOperandType operandType,
654 void SetMachineOperandReg(unsigned i, int regNum);
657 //===----------------------------------------------------------------------===//
660 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
661 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
663 } // End llvm namespace