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
65 MO_Immediate, // Immediate Operand
66 MO_MachineBasicBlock, // MachineBasicBlock reference
67 MO_FrameIndex, // Abstract Stack Frame Index
68 MO_ConstantPoolIndex, // Address of indexed Constant in Constant Pool
69 MO_JumpTableIndex, // Address of indexed Jump Table for switch
70 MO_ExternalSymbol, // Name of external global symbol
71 MO_GlobalAddress // Address of a global value
76 GlobalValue *GV; // LLVM global for MO_GlobalAddress.
77 int64_t immedVal; // Constant value for an explicit constant
78 MachineBasicBlock *MBB; // For MO_MachineBasicBlock type
79 const char *SymbolName; // For MO_ExternalSymbol type
82 char flags; // see bit field definitions above
83 MachineOperandType opType:8; // Pack into 8 bits efficiently after flags.
85 int regNum; // register number for an explicit register
86 int offset; // Offset to address of global or external, only
87 // valid for MO_GlobalAddress, MO_ExternalSym
88 // and MO_ConstantPoolIndex
92 contents.immedVal = 0;
96 MachineOperand(int64_t ImmVal, MachineOperandType OpTy, int Offset = 0)
97 : flags(0), opType(OpTy) {
98 contents.immedVal = ImmVal;
99 extra.offset = Offset;
102 MachineOperand(int Reg, MachineOperandType OpTy, UseType UseTy)
103 : flags(UseTy), opType(OpTy) {
108 MachineOperand(GlobalValue *V, int Offset = 0)
109 : flags(MachineOperand::Use), opType(MachineOperand::MO_GlobalAddress) {
111 extra.offset = Offset;
114 MachineOperand(MachineBasicBlock *mbb)
115 : flags(0), opType(MO_MachineBasicBlock) {
120 MachineOperand(const char *SymName, int Offset)
121 : flags(0), opType(MO_ExternalSymbol) {
123 contents.SymbolName = SymName;
124 extra.offset = Offset;
128 MachineOperand(const MachineOperand &M)
129 : flags(M.flags), opType(M.opType) {
131 contents = M.contents;
137 const MachineOperand &operator=(const MachineOperand &MO) {
138 contents = MO.contents;
145 /// getType - Returns the MachineOperandType for this operand.
147 MachineOperandType getType() const { return opType; }
149 /// getUseType - Returns the MachineOperandUseType of this operand.
151 UseType getUseType() const { return UseType(flags & (USEFLAG|DEFFLAG)); }
153 /// isRegister - Return true if this operand is a register operand.
155 bool isRegister() const {
156 return opType == MO_VirtualRegister;
159 /// Accessors that tell you what kind of MachineOperand you're looking at.
161 bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
162 bool isImmediate() const { return opType == MO_Immediate; }
163 bool isFrameIndex() const { return opType == MO_FrameIndex; }
164 bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
165 bool isJumpTableIndex() const { return opType == MO_JumpTableIndex; }
166 bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
167 bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
169 int64_t getImmedValue() const {
170 assert(isImmediate() && "Wrong MachineOperand accessor");
171 return contents.immedVal;
173 MachineBasicBlock *getMachineBasicBlock() const {
174 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
177 void setMachineBasicBlock(MachineBasicBlock *MBB) {
178 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
181 int getFrameIndex() const {
182 assert(isFrameIndex() && "Wrong MachineOperand accessor");
183 return (int)contents.immedVal;
185 unsigned getConstantPoolIndex() const {
186 assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
187 return (unsigned)contents.immedVal;
189 unsigned getJumpTableIndex() const {
190 assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
191 return (unsigned)contents.immedVal;
193 GlobalValue *getGlobal() const {
194 assert(isGlobalAddress() && "Wrong MachineOperand accessor");
197 int getOffset() const {
198 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
199 "Wrong MachineOperand accessor");
202 const char *getSymbolName() const {
203 assert(isExternalSymbol() && "Wrong MachineOperand accessor");
204 return contents.SymbolName;
207 /// MachineOperand methods for testing that work on any kind of
208 /// MachineOperand...
210 bool isUse () const { return flags & USEFLAG; }
211 MachineOperand& setUse () { flags |= USEFLAG; return *this; }
212 bool isDef () const { return flags & DEFFLAG; }
213 MachineOperand& setDef () { flags |= DEFFLAG; return *this; }
215 /// hasAllocatedReg - Returns true iff a machine register has been
216 /// allocated to this operand.
218 bool hasAllocatedReg() const {
219 return extra.regNum >= 0 && opType == MO_VirtualRegister;
222 /// getReg - Returns the register number. It is a runtime error to call this
223 /// if a register is not allocated.
225 unsigned getReg() const {
226 assert(hasAllocatedReg());
230 /// MachineOperand mutators.
232 void setReg(unsigned Reg) {
233 assert(hasAllocatedReg() && "This operand cannot have a register number!");
237 void setImmedValue(int64_t immVal) {
238 assert(isImmediate() && "Wrong MachineOperand mutator");
239 contents.immedVal = immVal;
242 void setOffset(int Offset) {
243 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex() ||
244 isJumpTableIndex()) &&
245 "Wrong MachineOperand accessor");
246 extra.offset = Offset;
249 /// ChangeToImmediate - Replace this operand with a new immediate operand of
250 /// the specified value. If an operand is known to be an immediate already,
251 /// the setImmedValue method should be used.
252 void ChangeToImmediate(int64_t ImmVal) {
253 opType = MO_Immediate;
254 contents.immedVal = ImmVal;
257 /// ChangeToRegister - Replace this operand with a new register operand of
258 /// the specified value. If an operand is known to be an register already,
259 /// the setReg method should be used.
260 void ChangeToRegister(unsigned Reg) {
261 opType = MO_VirtualRegister;
265 friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
267 friend class MachineInstr;
271 //===----------------------------------------------------------------------===//
272 // class MachineInstr
275 // Representation of each machine instruction.
277 // MachineOpCode must be an enum, defined separately for each target.
278 // E.g., It is defined in SparcInstructionSelection.h for the SPARC.
280 // There are 2 kinds of operands:
282 // (1) Explicit operands of the machine instruction in vector operands[]
284 // (2) "Implicit operands" are values implicitly used or defined by the
285 // machine instruction, such as arguments to a CALL, return value of
286 // a CALL (if any), and return value of a RETURN.
287 //===----------------------------------------------------------------------===//
290 short Opcode; // the opcode
291 std::vector<MachineOperand> operands; // the operands
292 MachineInstr* prev, *next; // links for our intrusive list
293 MachineBasicBlock* parent; // pointer to the owning basic block
295 // OperandComplete - Return true if it's illegal to add a new operand
296 bool OperandsComplete() const;
298 //Constructor used by clone() method
299 MachineInstr(const MachineInstr&);
301 void operator=(const MachineInstr&); // DO NOT IMPLEMENT
303 // Intrusive list support
305 friend struct ilist_traits<MachineInstr>;
308 /// MachineInstr ctor - This constructor only does a _reserve_ of the
309 /// operands, not a resize for them. It is expected that if you use this that
310 /// you call add* methods below to fill up the operands, instead of the Set
311 /// methods. Eventually, the "resizing" ctors will be phased out.
313 MachineInstr(short Opcode, unsigned numOperands, bool XX, bool YY);
315 /// MachineInstr ctor - Work exactly the same as the ctor above, except that
316 /// the MachineInstr is created and added to the end of the specified basic
319 MachineInstr(MachineBasicBlock *MBB, short Opcode, unsigned numOps);
323 const MachineBasicBlock* getParent() const { return parent; }
324 MachineBasicBlock* getParent() { return parent; }
326 /// getOpcode - Returns the opcode of this MachineInstr.
328 const int getOpcode() const { return Opcode; }
330 /// Access to explicit operands of the instruction.
332 unsigned getNumOperands() const { return operands.size(); }
334 const MachineOperand& getOperand(unsigned i) const {
335 assert(i < getNumOperands() && "getOperand() out of range!");
338 MachineOperand& getOperand(unsigned i) {
339 assert(i < getNumOperands() && "getOperand() out of range!");
344 /// clone - Create a copy of 'this' instruction that is identical in
345 /// all ways except the the instruction has no parent, prev, or next.
346 MachineInstr* clone() const;
348 /// removeFromParent - This method unlinks 'this' from the containing basic
349 /// block, and returns it, but does not delete it.
350 MachineInstr *removeFromParent();
352 /// eraseFromParent - This method unlinks 'this' from the containing basic
353 /// block and deletes it.
354 void eraseFromParent() {
355 delete removeFromParent();
361 void print(std::ostream &OS, const TargetMachine *TM) const;
363 friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
365 //===--------------------------------------------------------------------===//
366 // Accessors to add operands when building up machine instructions
369 /// addRegOperand - Add a symbolic virtual register reference...
371 void addRegOperand(int reg, bool isDef) {
372 assert(!OperandsComplete() &&
373 "Trying to add an operand to a machine instr that is already done!");
375 MachineOperand(reg, MachineOperand::MO_VirtualRegister,
376 isDef ? MachineOperand::Def : MachineOperand::Use));
379 /// addRegOperand - Add a symbolic virtual register reference...
381 void addRegOperand(int reg,
382 MachineOperand::UseType UTy = MachineOperand::Use) {
383 assert(!OperandsComplete() &&
384 "Trying to add an operand to a machine instr that is already done!");
386 MachineOperand(reg, MachineOperand::MO_VirtualRegister, UTy));
389 /// addZeroExtImmOperand - Add a zero extended constant argument to the
390 /// machine instruction.
392 void addZeroExtImmOperand(int intValue) {
393 assert(!OperandsComplete() &&
394 "Trying to add an operand to a machine instr that is already done!");
396 MachineOperand(intValue, MachineOperand::MO_Immediate));
399 /// addZeroExtImm64Operand - Add a zero extended 64-bit constant argument
400 /// to the machine instruction.
402 void addZeroExtImm64Operand(uint64_t intValue) {
403 assert(!OperandsComplete() &&
404 "Trying to add an operand to a machine instr that is already done!");
405 operands.push_back(MachineOperand(intValue, MachineOperand::MO_Immediate));
408 void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
409 assert(!OperandsComplete() &&
410 "Trying to add an operand to a machine instr that is already done!");
411 operands.push_back(MachineOperand(MBB));
414 /// addFrameIndexOperand - Add an abstract frame index to the instruction
416 void addFrameIndexOperand(unsigned Idx) {
417 assert(!OperandsComplete() &&
418 "Trying to add an operand to a machine instr that is already done!");
419 operands.push_back(MachineOperand(Idx, MachineOperand::MO_FrameIndex));
422 /// addConstantPoolndexOperand - Add a constant pool object index to the
425 void addConstantPoolIndexOperand(unsigned I, int Offset=0) {
426 assert(!OperandsComplete() &&
427 "Trying to add an operand to a machine instr that is already done!");
428 operands.push_back(MachineOperand(I, MachineOperand::MO_ConstantPoolIndex));
431 /// addJumpTableIndexOperand - Add a jump table object index to the
434 void addJumpTableIndexOperand(unsigned I) {
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_JumpTableIndex));
440 void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
441 assert(!OperandsComplete() &&
442 "Trying to add an operand to a machine instr that is already done!");
443 operands.push_back(MachineOperand(GV, Offset));
446 /// addExternalSymbolOperand - Add an external symbol operand to this instr
448 void addExternalSymbolOperand(const char *SymName) {
449 operands.push_back(MachineOperand(SymName, 0));
452 //===--------------------------------------------------------------------===//
453 // Accessors used to modify instructions in place.
456 /// setOpcode - Replace the opcode of the current instruction with a new one.
458 void setOpcode(unsigned Op) { Opcode = Op; }
460 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
461 /// fewer operand than it started with.
463 void RemoveOperand(unsigned i) {
464 operands.erase(operands.begin()+i);
468 //===----------------------------------------------------------------------===//
471 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
472 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
474 } // End llvm namespace