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_Register, // Register operand.
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) : flags(0), opType(MO_Immediate) {
97 contents.immedVal = ImmVal;
101 MachineOperand(unsigned Idx, MachineOperandType OpTy)
102 : flags(0), opType(OpTy) {
103 contents.immedVal = Idx;
107 MachineOperand(int Reg, MachineOperandType OpTy, UseType UseTy)
108 : flags(UseTy), opType(OpTy) {
113 MachineOperand(GlobalValue *V, int Offset = 0)
114 : flags(MachineOperand::Use), opType(MachineOperand::MO_GlobalAddress) {
116 extra.offset = Offset;
119 MachineOperand(MachineBasicBlock *mbb)
120 : flags(0), opType(MO_MachineBasicBlock) {
125 MachineOperand(const char *SymName, int Offset)
126 : flags(0), opType(MO_ExternalSymbol) {
128 contents.SymbolName = SymName;
129 extra.offset = Offset;
133 MachineOperand(const MachineOperand &M)
134 : flags(M.flags), opType(M.opType) {
136 contents = M.contents;
142 const MachineOperand &operator=(const MachineOperand &MO) {
143 contents = MO.contents;
150 /// getType - Returns the MachineOperandType for this operand.
152 MachineOperandType getType() const { return opType; }
154 /// getUseType - Returns the MachineOperandUseType of this operand.
156 UseType getUseType() const { return UseType(flags & (USEFLAG|DEFFLAG)); }
158 /// Accessors that tell you what kind of MachineOperand you're looking at.
160 bool isRegister() const { return opType == MO_Register; }
161 bool isImmediate() const { return opType == MO_Immediate; }
162 bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
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 /// getReg - Returns the register number.
217 unsigned getReg() const {
218 assert(isRegister() && "This is not a register operand!");
222 /// MachineOperand mutators.
224 void setReg(unsigned Reg) {
225 assert(isRegister() && "This is not a register operand!");
229 void setImmedValue(int64_t immVal) {
230 assert(isImmediate() && "Wrong MachineOperand mutator");
231 contents.immedVal = immVal;
234 void setOffset(int Offset) {
235 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex() ||
236 isJumpTableIndex()) &&
237 "Wrong MachineOperand accessor");
238 extra.offset = Offset;
241 /// ChangeToImmediate - Replace this operand with a new immediate operand of
242 /// the specified value. If an operand is known to be an immediate already,
243 /// the setImmedValue method should be used.
244 void ChangeToImmediate(int64_t ImmVal) {
245 opType = MO_Immediate;
246 contents.immedVal = ImmVal;
249 /// ChangeToRegister - Replace this operand with a new register operand of
250 /// the specified value. If an operand is known to be an register already,
251 /// the setReg method should be used.
252 void ChangeToRegister(unsigned Reg) {
253 opType = MO_Register;
257 friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
259 friend class MachineInstr;
263 //===----------------------------------------------------------------------===//
264 // class MachineInstr
267 // Representation of each machine instruction.
269 // MachineOpCode must be an enum, defined separately for each target.
270 // E.g., It is defined in SparcInstructionSelection.h for the SPARC.
272 // There are 2 kinds of operands:
274 // (1) Explicit operands of the machine instruction in vector operands[]
276 // (2) "Implicit operands" are values implicitly used or defined by the
277 // machine instruction, such as arguments to a CALL, return value of
278 // a CALL (if any), and return value of a RETURN.
279 //===----------------------------------------------------------------------===//
282 short Opcode; // the opcode
283 std::vector<MachineOperand> operands; // the operands
284 MachineInstr* prev, *next; // links for our intrusive list
285 MachineBasicBlock* parent; // pointer to the owning basic block
287 // OperandComplete - Return true if it's illegal to add a new operand
288 bool OperandsComplete() const;
290 //Constructor used by clone() method
291 MachineInstr(const MachineInstr&);
293 void operator=(const MachineInstr&); // DO NOT IMPLEMENT
295 // Intrusive list support
297 friend struct ilist_traits<MachineInstr>;
300 /// MachineInstr ctor - This constructor only does a _reserve_ of the
301 /// operands, not a resize for them. It is expected that if you use this that
302 /// you call add* methods below to fill up the operands, instead of the Set
303 /// methods. Eventually, the "resizing" ctors will be phased out.
305 MachineInstr(short Opcode, unsigned numOperands, bool XX, bool YY);
307 /// MachineInstr ctor - Work exactly the same as the ctor above, except that
308 /// the MachineInstr is created and added to the end of the specified basic
311 MachineInstr(MachineBasicBlock *MBB, short Opcode, unsigned numOps);
315 const MachineBasicBlock* getParent() const { return parent; }
316 MachineBasicBlock* getParent() { return parent; }
318 /// getOpcode - Returns the opcode of this MachineInstr.
320 const int getOpcode() const { return Opcode; }
322 /// Access to explicit operands of the instruction.
324 unsigned getNumOperands() const { return operands.size(); }
326 const MachineOperand& getOperand(unsigned i) const {
327 assert(i < getNumOperands() && "getOperand() out of range!");
330 MachineOperand& getOperand(unsigned i) {
331 assert(i < getNumOperands() && "getOperand() out of range!");
336 /// clone - Create a copy of 'this' instruction that is identical in
337 /// all ways except the the instruction has no parent, prev, or next.
338 MachineInstr* clone() const;
340 /// removeFromParent - This method unlinks 'this' from the containing basic
341 /// block, and returns it, but does not delete it.
342 MachineInstr *removeFromParent();
344 /// eraseFromParent - This method unlinks 'this' from the containing basic
345 /// block and deletes it.
346 void eraseFromParent() {
347 delete removeFromParent();
353 void print(std::ostream &OS, const TargetMachine *TM) const;
355 friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
357 //===--------------------------------------------------------------------===//
358 // Accessors to add operands when building up machine instructions
361 /// addRegOperand - Add a symbolic virtual register reference...
363 void addRegOperand(int reg,
364 MachineOperand::UseType UTy = MachineOperand::Use) {
365 assert(!OperandsComplete() &&
366 "Trying to add an operand to a machine instr that is already done!");
368 MachineOperand(reg, MachineOperand::MO_Register, UTy));
371 /// addImmOperand - Add a zero extended constant argument to the
372 /// machine instruction.
374 void addImmOperand(int64_t Val) {
375 assert(!OperandsComplete() &&
376 "Trying to add an operand to a machine instr that is already done!");
377 operands.push_back(MachineOperand(Val));
380 void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
381 assert(!OperandsComplete() &&
382 "Trying to add an operand to a machine instr that is already done!");
383 operands.push_back(MachineOperand(MBB));
386 /// addFrameIndexOperand - Add an abstract frame index to the instruction
388 void addFrameIndexOperand(unsigned Idx) {
389 assert(!OperandsComplete() &&
390 "Trying to add an operand to a machine instr that is already done!");
391 operands.push_back(MachineOperand(Idx, MachineOperand::MO_FrameIndex));
394 /// addConstantPoolndexOperand - Add a constant pool object index to the
397 void addConstantPoolIndexOperand(unsigned I, int Offset=0) {
398 assert(!OperandsComplete() &&
399 "Trying to add an operand to a machine instr that is already done!");
400 operands.push_back(MachineOperand(I, MachineOperand::MO_ConstantPoolIndex));
403 /// addJumpTableIndexOperand - Add a jump table object index to the
406 void addJumpTableIndexOperand(unsigned I) {
407 assert(!OperandsComplete() &&
408 "Trying to add an operand to a machine instr that is already done!");
409 operands.push_back(MachineOperand(I, MachineOperand::MO_JumpTableIndex));
412 void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
413 assert(!OperandsComplete() &&
414 "Trying to add an operand to a machine instr that is already done!");
415 operands.push_back(MachineOperand(GV, Offset));
418 /// addExternalSymbolOperand - Add an external symbol operand to this instr
420 void addExternalSymbolOperand(const char *SymName) {
421 operands.push_back(MachineOperand(SymName, 0));
424 //===--------------------------------------------------------------------===//
425 // Accessors used to modify instructions in place.
428 /// setOpcode - Replace the opcode of the current instruction with a new one.
430 void setOpcode(unsigned Op) { Opcode = Op; }
432 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
433 /// fewer operand than it started with.
435 void RemoveOperand(unsigned i) {
436 operands.erase(operands.begin()+i);
440 //===----------------------------------------------------------------------===//
443 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
444 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
446 } // End llvm namespace