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 GlobalValue *GV; // LLVM global for MO_GlobalAddress.
78 int64_t immedVal; // Constant value for an explicit constant
79 MachineBasicBlock *MBB; // For MO_MachineBasicBlock type
80 const char *SymbolName; // For MO_ExternalSymbol type
83 char flags; // see bit field definitions above
84 MachineOperandType opType:8; // Pack into 8 bits efficiently after flags.
86 int regNum; // register number for an explicit register
87 int offset; // Offset to address of global or external, only
88 // valid for MO_GlobalAddress, MO_ExternalSym
89 // and MO_ConstantPoolIndex
93 contents.immedVal = 0;
97 MachineOperand(int64_t ImmVal, MachineOperandType OpTy, int Offset = 0)
98 : flags(0), opType(OpTy) {
99 contents.immedVal = ImmVal;
100 extra.offset = Offset;
103 MachineOperand(int Reg, MachineOperandType OpTy, UseType UseTy)
104 : flags(UseTy), opType(OpTy) {
109 MachineOperand(GlobalValue *V, int Offset = 0)
110 : flags(MachineOperand::Use), opType(MachineOperand::MO_GlobalAddress) {
112 extra.offset = Offset;
115 MachineOperand(MachineBasicBlock *mbb)
116 : flags(0), opType(MO_MachineBasicBlock) {
121 MachineOperand(const char *SymName, int Offset)
122 : flags(0), opType(MO_ExternalSymbol) {
124 contents.SymbolName = SymName;
125 extra.offset = Offset;
129 MachineOperand(const MachineOperand &M)
130 : flags(M.flags), opType(M.opType) {
132 contents = M.contents;
138 const MachineOperand &operator=(const MachineOperand &MO) {
139 contents = MO.contents;
146 /// getType - Returns the MachineOperandType for this operand.
148 MachineOperandType getType() const { return opType; }
150 /// getUseType - Returns the MachineOperandUseType of this operand.
152 UseType getUseType() const { return UseType(flags & (USEFLAG|DEFFLAG)); }
154 /// isRegister - Return true if this operand is a register operand.
156 bool isRegister() const {
157 return opType == MO_VirtualRegister;
160 /// Accessors that tell you what kind of MachineOperand you're looking at.
162 bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
163 bool isImmediate() const {
164 return opType == MO_SignExtendedImmed || opType == MO_UnextendedImmed;
166 bool isFrameIndex() const { return opType == MO_FrameIndex; }
167 bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
168 bool isJumpTableIndex() const { return opType == MO_JumpTableIndex; }
169 bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
170 bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
172 int64_t getImmedValue() const {
173 assert(isImmediate() && "Wrong MachineOperand accessor");
174 return contents.immedVal;
176 MachineBasicBlock *getMachineBasicBlock() const {
177 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
180 void setMachineBasicBlock(MachineBasicBlock *MBB) {
181 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
184 int getFrameIndex() const {
185 assert(isFrameIndex() && "Wrong MachineOperand accessor");
186 return (int)contents.immedVal;
188 unsigned getConstantPoolIndex() const {
189 assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
190 return (unsigned)contents.immedVal;
192 unsigned getJumpTableIndex() const {
193 assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
194 return (unsigned)contents.immedVal;
196 GlobalValue *getGlobal() const {
197 assert(isGlobalAddress() && "Wrong MachineOperand accessor");
200 int getOffset() const {
201 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
202 "Wrong MachineOperand accessor");
205 const char *getSymbolName() const {
206 assert(isExternalSymbol() && "Wrong MachineOperand accessor");
207 return contents.SymbolName;
210 /// MachineOperand methods for testing that work on any kind of
211 /// MachineOperand...
213 bool isUse () const { return flags & USEFLAG; }
214 MachineOperand& setUse () { flags |= USEFLAG; return *this; }
215 bool isDef () const { return flags & DEFFLAG; }
216 MachineOperand& setDef () { flags |= DEFFLAG; return *this; }
218 /// hasAllocatedReg - Returns true iff a machine register has been
219 /// allocated to this operand.
221 bool hasAllocatedReg() const {
222 return extra.regNum >= 0 && opType == MO_VirtualRegister;
225 /// getReg - Returns the register number. It is a runtime error to call this
226 /// if a register is not allocated.
228 unsigned getReg() const {
229 assert(hasAllocatedReg());
233 /// MachineOperand mutators.
235 void setReg(unsigned Reg) {
236 assert(hasAllocatedReg() && "This operand cannot have a register number!");
240 void setImmedValue(int immVal) {
241 assert(isImmediate() && "Wrong MachineOperand mutator");
242 contents.immedVal = immVal;
245 void setOffset(int Offset) {
246 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex() ||
247 isJumpTableIndex()) &&
248 "Wrong MachineOperand accessor");
249 extra.offset = Offset;
252 friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
254 friend class MachineInstr;
258 //===----------------------------------------------------------------------===//
259 // class MachineInstr
262 // Representation of each machine instruction.
264 // MachineOpCode must be an enum, defined separately for each target.
265 // E.g., It is defined in SparcInstructionSelection.h for the SPARC.
267 // There are 2 kinds of operands:
269 // (1) Explicit operands of the machine instruction in vector operands[]
271 // (2) "Implicit operands" are values implicitly used or defined by the
272 // machine instruction, such as arguments to a CALL, return value of
273 // a CALL (if any), and return value of a RETURN.
274 //===----------------------------------------------------------------------===//
277 short Opcode; // the opcode
278 std::vector<MachineOperand> operands; // the operands
279 MachineInstr* prev, *next; // links for our intrusive list
280 MachineBasicBlock* parent; // pointer to the owning basic block
282 // OperandComplete - Return true if it's illegal to add a new operand
283 bool OperandsComplete() const;
285 //Constructor used by clone() method
286 MachineInstr(const MachineInstr&);
288 void operator=(const MachineInstr&); // DO NOT IMPLEMENT
290 // Intrusive list support
292 friend struct ilist_traits<MachineInstr>;
295 /// MachineInstr ctor - This constructor only does a _reserve_ of the
296 /// operands, not a resize for them. It is expected that if you use this that
297 /// you call add* methods below to fill up the operands, instead of the Set
298 /// methods. Eventually, the "resizing" ctors will be phased out.
300 MachineInstr(short Opcode, unsigned numOperands, bool XX, bool YY);
302 /// MachineInstr ctor - Work exactly the same as the ctor above, except that
303 /// the MachineInstr is created and added to the end of the specified basic
306 MachineInstr(MachineBasicBlock *MBB, short Opcode, unsigned numOps);
310 const MachineBasicBlock* getParent() const { return parent; }
311 MachineBasicBlock* getParent() { return parent; }
313 /// getOpcode - Returns the opcode of this MachineInstr.
315 const int getOpcode() const { return Opcode; }
317 /// Access to explicit operands of the instruction.
319 unsigned getNumOperands() const { return operands.size(); }
321 const MachineOperand& getOperand(unsigned i) const {
322 assert(i < getNumOperands() && "getOperand() out of range!");
325 MachineOperand& getOperand(unsigned i) {
326 assert(i < getNumOperands() && "getOperand() out of range!");
331 /// clone - Create a copy of 'this' instruction that is identical in
332 /// all ways except the the instruction has no parent, prev, or next.
333 MachineInstr* clone() const;
335 /// removeFromParent - This method unlinks 'this' from the containing basic
336 /// block, and returns it, but does not delete it.
337 MachineInstr *removeFromParent();
339 /// eraseFromParent - This method unlinks 'this' from the containing basic
340 /// block and deletes it.
341 void eraseFromParent() {
342 delete removeFromParent();
348 void print(std::ostream &OS, const TargetMachine *TM) const;
350 friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
352 //===--------------------------------------------------------------------===//
353 // Accessors to add operands when building up machine instructions
356 /// addRegOperand - Add a symbolic virtual register reference...
358 void addRegOperand(int reg, bool isDef) {
359 assert(!OperandsComplete() &&
360 "Trying to add an operand to a machine instr that is already done!");
362 MachineOperand(reg, MachineOperand::MO_VirtualRegister,
363 isDef ? MachineOperand::Def : MachineOperand::Use));
366 /// addRegOperand - Add a symbolic virtual register reference...
368 void addRegOperand(int reg,
369 MachineOperand::UseType UTy = MachineOperand::Use) {
370 assert(!OperandsComplete() &&
371 "Trying to add an operand to a machine instr that is already done!");
373 MachineOperand(reg, MachineOperand::MO_VirtualRegister, UTy));
376 /// addZeroExtImmOperand - Add a zero extended constant argument to the
377 /// machine instruction.
379 void addZeroExtImmOperand(int intValue) {
380 assert(!OperandsComplete() &&
381 "Trying to add an operand to a machine instr that is already done!");
383 MachineOperand(intValue, MachineOperand::MO_UnextendedImmed));
386 /// addZeroExtImm64Operand - Add a zero extended 64-bit constant argument
387 /// to the machine instruction.
389 void addZeroExtImm64Operand(uint64_t intValue) {
390 assert(!OperandsComplete() &&
391 "Trying to add an operand to a machine instr that is already done!");
393 MachineOperand(intValue, MachineOperand::MO_UnextendedImmed));
396 /// addSignExtImmOperand - Add a zero extended constant argument to the
397 /// machine instruction.
399 void addSignExtImmOperand(int intValue) {
400 assert(!OperandsComplete() &&
401 "Trying to add an operand to a machine instr that is already done!");
403 MachineOperand(intValue, MachineOperand::MO_SignExtendedImmed));
406 void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
407 assert(!OperandsComplete() &&
408 "Trying to add an operand to a machine instr that is already done!");
409 operands.push_back(MachineOperand(MBB));
412 /// addFrameIndexOperand - Add an abstract frame index to the instruction
414 void addFrameIndexOperand(unsigned Idx) {
415 assert(!OperandsComplete() &&
416 "Trying to add an operand to a machine instr that is already done!");
417 operands.push_back(MachineOperand(Idx, MachineOperand::MO_FrameIndex));
420 /// addConstantPoolndexOperand - Add a constant pool object index to the
423 void addConstantPoolIndexOperand(unsigned I, int Offset=0) {
424 assert(!OperandsComplete() &&
425 "Trying to add an operand to a machine instr that is already done!");
426 operands.push_back(MachineOperand(I, MachineOperand::MO_ConstantPoolIndex));
429 /// addJumpTableIndexOperand - Add a jump table object index to the
432 void addJumpTableIndexOperand(unsigned I) {
433 assert(!OperandsComplete() &&
434 "Trying to add an operand to a machine instr that is already done!");
435 operands.push_back(MachineOperand(I, MachineOperand::MO_JumpTableIndex));
438 void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
439 assert(!OperandsComplete() &&
440 "Trying to add an operand to a machine instr that is already done!");
441 operands.push_back(MachineOperand(GV, Offset));
444 /// addExternalSymbolOperand - Add an external symbol operand to this instr
446 void addExternalSymbolOperand(const char *SymName) {
447 operands.push_back(MachineOperand(SymName, 0));
450 //===--------------------------------------------------------------------===//
451 // Accessors used to modify instructions in place.
453 // FIXME: Move this stuff to MachineOperand itself!
455 /// setOpcode - Replace the opcode of the current instruction with a new one.
457 void setOpcode(unsigned Op) { Opcode = Op; }
459 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
460 /// fewer operand than it started with.
462 void RemoveOperand(unsigned i) {
463 operands.erase(operands.begin()+i);
466 // Access to set the operands when building the machine instruction
468 void SetMachineOperandConst(unsigned i,
469 MachineOperand::MachineOperandType operandType,
472 void SetMachineOperandReg(unsigned i, int regNum);
475 //===----------------------------------------------------------------------===//
478 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
479 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
481 } // End llvm namespace