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
40 // Representation of each machine instruction operand.
42 struct MachineOperand {
44 // Bit fields of the flags variable used for different operand properties
46 DEFFLAG = 0x01, // this is a def of the operand
47 USEFLAG = 0x02, // this is a use of the operand
51 // UseType - This enum describes how the machine operand is used by
52 // the instruction. Note that the MachineInstr/Operator class
53 // currently uses bool arguments to represent this information
54 // instead of an enum. Eventually this should change over to use
55 // this _easier to read_ representation instead.
58 Use = USEFLAG, /// only read
59 Def = DEFFLAG, /// only written
60 UseAndDef = Use | Def /// read AND written
63 enum MachineOperandType {
64 MO_VirtualRegister, // virtual register for *value
67 MO_MachineBasicBlock, // MachineBasicBlock reference
68 MO_FrameIndex, // Abstract Stack Frame Index
69 MO_ConstantPoolIndex, // Address of indexed Constant in Constant Pool
70 MO_JumpTableIndex, // Address of indexed Jump Table for switch
71 MO_ExternalSymbol, // Name of external global symbol
72 MO_GlobalAddress // Address of a global value
77 Value* value; // BasicBlockVal for a label operand.
78 // ConstantVal for a non-address immediate.
79 // Virtual register for an SSA operand,
80 // including hidden operands required for
81 // the generated machine code.
82 // LLVM global for MO_GlobalAddress.
84 int64_t immedVal; // Constant value for an explicit constant
86 MachineBasicBlock *MBB; // For MO_MachineBasicBlock type
87 const char *SymbolName; // For MO_ExternalSymbol type
90 char flags; // see bit field definitions above
91 MachineOperandType opType:8; // Pack into 8 bits efficiently after flags.
93 int regNum; // register number for an explicit register
94 // will be set for a value after reg allocation
96 int offset; // Offset to address of global or external, only
97 // valid for MO_GlobalAddress, MO_ExternalSym
98 // and MO_ConstantPoolIndex
101 void zeroContents () {
102 memset (&contents, 0, sizeof (contents));
103 memset (&extra, 0, sizeof (extra));
106 MachineOperand(int64_t ImmVal, MachineOperandType OpTy, int Offset = 0)
107 : flags(0), opType(OpTy) {
109 contents.immedVal = ImmVal;
110 extra.offset = Offset;
113 MachineOperand(int Reg, MachineOperandType OpTy, UseType UseTy)
114 : flags(UseTy), opType(OpTy) {
119 MachineOperand(GlobalValue *V, int Offset = 0)
120 : flags(MachineOperand::Use), opType(MachineOperand::MO_GlobalAddress) {
122 contents.value = (Value*)V;
123 extra.offset = Offset;
126 MachineOperand(MachineBasicBlock *mbb)
127 : flags(0), opType(MO_MachineBasicBlock) {
132 MachineOperand(const char *SymName, int Offset)
133 : flags(0), opType(MO_ExternalSymbol) {
135 contents.SymbolName = SymName;
136 extra.offset = Offset;
140 MachineOperand(const MachineOperand &M)
141 : flags(M.flags), opType(M.opType) {
143 contents = M.contents;
149 const MachineOperand &operator=(const MachineOperand &MO) {
150 contents = MO.contents;
157 /// getType - Returns the MachineOperandType for this operand.
159 MachineOperandType getType() const { return opType; }
161 /// getUseType - Returns the MachineOperandUseType of this operand.
163 UseType getUseType() const { return UseType(flags & (USEFLAG|DEFFLAG)); }
165 /// isRegister - Return true if this operand is a register operand.
167 bool isRegister() const {
168 return opType == MO_VirtualRegister;
171 /// Accessors that tell you what kind of MachineOperand you're looking at.
173 bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
174 bool isImmediate() const {
175 return opType == MO_SignExtendedImmed || opType == MO_UnextendedImmed;
177 bool isFrameIndex() const { return opType == MO_FrameIndex; }
178 bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
179 bool isJumpTableIndex() const { return opType == MO_JumpTableIndex; }
180 bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
181 bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
183 int64_t getImmedValue() const {
184 assert(isImmediate() && "Wrong MachineOperand accessor");
185 return contents.immedVal;
187 MachineBasicBlock *getMachineBasicBlock() const {
188 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
191 void setMachineBasicBlock(MachineBasicBlock *MBB) {
192 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
195 int getFrameIndex() const {
196 assert(isFrameIndex() && "Wrong MachineOperand accessor");
197 return (int)contents.immedVal;
199 unsigned getConstantPoolIndex() const {
200 assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
201 return (unsigned)contents.immedVal;
203 unsigned getJumpTableIndex() const {
204 assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
205 return (unsigned)contents.immedVal;
207 GlobalValue *getGlobal() const {
208 assert(isGlobalAddress() && "Wrong MachineOperand accessor");
209 return (GlobalValue*)contents.value;
211 int getOffset() const {
212 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
213 "Wrong MachineOperand accessor");
216 const char *getSymbolName() const {
217 assert(isExternalSymbol() && "Wrong MachineOperand accessor");
218 return contents.SymbolName;
221 /// MachineOperand methods for testing that work on any kind of
222 /// MachineOperand...
224 bool isUse () const { return flags & USEFLAG; }
225 MachineOperand& setUse () { flags |= USEFLAG; return *this; }
226 bool isDef () const { return flags & DEFFLAG; }
227 MachineOperand& setDef () { flags |= DEFFLAG; return *this; }
229 /// hasAllocatedReg - Returns true iff a machine register has been
230 /// allocated to this operand.
232 bool hasAllocatedReg() const {
233 return extra.regNum >= 0 && opType == MO_VirtualRegister;
236 /// getReg - Returns the register number. It is a runtime error to call this
237 /// if a register is not allocated.
239 unsigned getReg() const {
240 assert(hasAllocatedReg());
244 /// MachineOperand mutators.
246 void setReg(unsigned Reg) {
247 assert(hasAllocatedReg() && "This operand cannot have a register number!");
251 void setImmedValue(int immVal) {
252 assert(isImmediate() && "Wrong MachineOperand mutator");
253 contents.immedVal = immVal;
256 void setOffset(int Offset) {
257 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex() ||
258 isJumpTableIndex()) &&
259 "Wrong MachineOperand accessor");
260 extra.offset = Offset;
263 friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
265 friend class MachineInstr;
269 //===----------------------------------------------------------------------===//
270 // class MachineInstr
273 // Representation of each machine instruction.
275 // MachineOpCode must be an enum, defined separately for each target.
276 // E.g., It is defined in SparcInstructionSelection.h for the SPARC.
278 // There are 2 kinds of operands:
280 // (1) Explicit operands of the machine instruction in vector operands[]
282 // (2) "Implicit operands" are values implicitly used or defined by the
283 // machine instruction, such as arguments to a CALL, return value of
284 // a CALL (if any), and return value of a RETURN.
285 //===----------------------------------------------------------------------===//
288 short Opcode; // the opcode
289 std::vector<MachineOperand> operands; // the operands
290 MachineInstr* prev, *next; // links for our intrusive list
291 MachineBasicBlock* parent; // pointer to the owning basic block
293 // OperandComplete - Return true if it's illegal to add a new operand
294 bool OperandsComplete() const;
296 //Constructor used by clone() method
297 MachineInstr(const MachineInstr&);
299 void operator=(const MachineInstr&); // DO NOT IMPLEMENT
301 // Intrusive list support
303 friend struct ilist_traits<MachineInstr>;
306 /// MachineInstr ctor - This constructor only does a _reserve_ of the
307 /// operands, not a resize for them. It is expected that if you use this that
308 /// you call add* methods below to fill up the operands, instead of the Set
309 /// methods. Eventually, the "resizing" ctors will be phased out.
311 MachineInstr(short Opcode, unsigned numOperands, bool XX, bool YY);
313 /// MachineInstr ctor - Work exactly the same as the ctor above, except that
314 /// the MachineInstr is created and added to the end of the specified basic
317 MachineInstr(MachineBasicBlock *MBB, short Opcode, unsigned numOps);
321 const MachineBasicBlock* getParent() const { return parent; }
322 MachineBasicBlock* getParent() { return parent; }
324 /// getOpcode - Returns the opcode of this MachineInstr.
326 const int getOpcode() const { return Opcode; }
328 /// Access to explicit operands of the instruction.
330 unsigned getNumOperands() const { return operands.size(); }
332 const MachineOperand& getOperand(unsigned i) const {
333 assert(i < getNumOperands() && "getOperand() out of range!");
336 MachineOperand& getOperand(unsigned i) {
337 assert(i < getNumOperands() && "getOperand() out of range!");
342 /// clone - Create a copy of 'this' instruction that is identical in
343 /// all ways except the the instruction has no parent, prev, or next.
344 MachineInstr* clone() const;
346 /// removeFromParent - This method unlinks 'this' from the containing basic
347 /// block, and returns it, but does not delete it.
348 MachineInstr *removeFromParent();
350 /// eraseFromParent - This method unlinks 'this' from the containing basic
351 /// block and deletes it.
352 void eraseFromParent() {
353 delete removeFromParent();
359 void print(std::ostream &OS, const TargetMachine *TM) const;
361 friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
363 //===--------------------------------------------------------------------===//
364 // Accessors to add operands when building up machine instructions
367 /// addRegOperand - Add a symbolic virtual register reference...
369 void addRegOperand(int reg, bool isDef) {
370 assert(!OperandsComplete() &&
371 "Trying to add an operand to a machine instr that is already done!");
373 MachineOperand(reg, MachineOperand::MO_VirtualRegister,
374 isDef ? MachineOperand::Def : MachineOperand::Use));
377 /// addRegOperand - Add a symbolic virtual register reference...
379 void addRegOperand(int reg,
380 MachineOperand::UseType UTy = MachineOperand::Use) {
381 assert(!OperandsComplete() &&
382 "Trying to add an operand to a machine instr that is already done!");
384 MachineOperand(reg, MachineOperand::MO_VirtualRegister, UTy));
387 /// addZeroExtImmOperand - Add a zero extended constant argument to the
388 /// machine instruction.
390 void addZeroExtImmOperand(int intValue) {
391 assert(!OperandsComplete() &&
392 "Trying to add an operand to a machine instr that is already done!");
394 MachineOperand(intValue, MachineOperand::MO_UnextendedImmed));
397 /// addZeroExtImm64Operand - Add a zero extended 64-bit constant argument
398 /// to the machine instruction.
400 void addZeroExtImm64Operand(uint64_t intValue) {
401 assert(!OperandsComplete() &&
402 "Trying to add an operand to a machine instr that is already done!");
404 MachineOperand(intValue, MachineOperand::MO_UnextendedImmed));
407 /// addSignExtImmOperand - Add a zero extended constant argument to the
408 /// machine instruction.
410 void addSignExtImmOperand(int intValue) {
411 assert(!OperandsComplete() &&
412 "Trying to add an operand to a machine instr that is already done!");
414 MachineOperand(intValue, MachineOperand::MO_SignExtendedImmed));
417 void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
418 assert(!OperandsComplete() &&
419 "Trying to add an operand to a machine instr that is already done!");
420 operands.push_back(MachineOperand(MBB));
423 /// addFrameIndexOperand - Add an abstract frame index to the instruction
425 void addFrameIndexOperand(unsigned Idx) {
426 assert(!OperandsComplete() &&
427 "Trying to add an operand to a machine instr that is already done!");
428 operands.push_back(MachineOperand(Idx, MachineOperand::MO_FrameIndex));
431 /// addConstantPoolndexOperand - Add a constant pool object index to the
434 void addConstantPoolIndexOperand(unsigned I, int Offset=0) {
435 assert(!OperandsComplete() &&
436 "Trying to add an operand to a machine instr that is already done!");
437 operands.push_back(MachineOperand(I, MachineOperand::MO_ConstantPoolIndex));
440 /// addJumpTableIndexOperand - Add a jump table object index to the
443 void addJumpTableIndexOperand(unsigned I) {
444 assert(!OperandsComplete() &&
445 "Trying to add an operand to a machine instr that is already done!");
446 operands.push_back(MachineOperand(I, MachineOperand::MO_JumpTableIndex));
449 void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
450 assert(!OperandsComplete() &&
451 "Trying to add an operand to a machine instr that is already done!");
452 operands.push_back(MachineOperand(GV, Offset));
455 /// addExternalSymbolOperand - Add an external symbol operand to this instr
457 void addExternalSymbolOperand(const char *SymName) {
458 operands.push_back(MachineOperand(SymName, 0));
461 //===--------------------------------------------------------------------===//
462 // Accessors used to modify instructions in place.
464 // FIXME: Move this stuff to MachineOperand itself!
466 /// setOpcode - Replace the opcode of the current instruction with a new one.
468 void setOpcode(unsigned Op) { Opcode = Op; }
470 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
471 /// fewer operand than it started with.
473 void RemoveOperand(unsigned i) {
474 operands.erase(operands.begin()+i);
477 // Access to set the operands when building the machine instruction
479 void SetMachineOperandVal(unsigned i,
480 MachineOperand::MachineOperandType operandType,
483 void SetMachineOperandConst(unsigned i,
484 MachineOperand::MachineOperandType operandType,
487 void SetMachineOperandReg(unsigned i, int regNum);
490 //===----------------------------------------------------------------------===//
493 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
494 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
496 } // End llvm namespace