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 //===----------------------------------------------------------------------===//
36 // class MachineOperand
38 // Representation of each machine instruction operand.
40 struct MachineOperand {
41 enum MachineOperandType {
42 MO_Register, // Register operand.
43 MO_Immediate, // Immediate Operand
44 MO_MachineBasicBlock, // MachineBasicBlock reference
45 MO_FrameIndex, // Abstract Stack Frame Index
46 MO_ConstantPoolIndex, // Address of indexed Constant in Constant Pool
47 MO_JumpTableIndex, // Address of indexed Jump Table for switch
48 MO_ExternalSymbol, // Name of external global symbol
49 MO_GlobalAddress // Address of a global value
54 GlobalValue *GV; // For MO_GlobalAddress.
55 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
56 const char *SymbolName; // For MO_ExternalSymbol.
57 unsigned RegNo; // For MO_Register.
58 int64_t immedVal; // For MO_Immediate and MO_*Index.
61 MachineOperandType opType:8; // Discriminate the union.
62 bool IsDef : 1; // True if this is a def, false if this is a use.
64 /// offset - Offset to address of global or external, only valid for
65 /// MO_GlobalAddress, MO_ExternalSym and MO_ConstantPoolIndex
70 MachineOperand(const MachineOperand &M) {
76 static MachineOperand CreateImm(int64_t Val) {
78 Op.opType = MachineOperand::MO_Immediate;
79 Op.contents.immedVal = Val;
85 const MachineOperand &operator=(const MachineOperand &MO) {
86 contents = MO.contents;
93 /// getType - Returns the MachineOperandType for this operand.
95 MachineOperandType getType() const { return opType; }
97 /// Accessors that tell you what kind of MachineOperand you're looking at.
99 bool isReg() const { return opType == MO_Register; }
100 bool isImm() const { return opType == MO_Immediate; }
101 bool isMBB() const { return opType == MO_MachineBasicBlock; }
103 bool isRegister() const { return opType == MO_Register; }
104 bool isImmediate() const { return opType == MO_Immediate; }
105 bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
106 bool isFrameIndex() const { return opType == MO_FrameIndex; }
107 bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
108 bool isJumpTableIndex() const { return opType == MO_JumpTableIndex; }
109 bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
110 bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
112 int64_t getImm() const {
113 assert(isImm() && "Wrong MachineOperand accessor");
114 return contents.immedVal;
117 int64_t getImmedValue() const {
118 assert(isImm() && "Wrong MachineOperand accessor");
119 return contents.immedVal;
121 MachineBasicBlock *getMBB() const {
122 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
125 MachineBasicBlock *getMachineBasicBlock() const {
126 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
129 void setMachineBasicBlock(MachineBasicBlock *MBB) {
130 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
133 int getFrameIndex() const {
134 assert(isFrameIndex() && "Wrong MachineOperand accessor");
135 return (int)contents.immedVal;
137 unsigned getConstantPoolIndex() const {
138 assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
139 return (unsigned)contents.immedVal;
141 unsigned getJumpTableIndex() const {
142 assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
143 return (unsigned)contents.immedVal;
145 GlobalValue *getGlobal() const {
146 assert(isGlobalAddress() && "Wrong MachineOperand accessor");
149 int getOffset() const {
150 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
151 "Wrong MachineOperand accessor");
154 const char *getSymbolName() const {
155 assert(isExternalSymbol() && "Wrong MachineOperand accessor");
156 return contents.SymbolName;
160 assert(isRegister() && "Wrong MachineOperand accessor");
164 assert(isRegister() && "Wrong MachineOperand accessor");
168 assert(isRegister() && "Wrong MachineOperand accessor");
172 assert(isRegister() && "Wrong MachineOperand accessor");
176 /// getReg - Returns the register number.
178 unsigned getReg() const {
179 assert(isRegister() && "This is not a register operand!");
180 return contents.RegNo;
183 /// MachineOperand mutators.
185 void setReg(unsigned Reg) {
186 assert(isRegister() && "This is not a register operand!");
187 contents.RegNo = Reg;
190 void setImmedValue(int64_t immVal) {
191 assert(isImm() && "Wrong MachineOperand mutator");
192 contents.immedVal = immVal;
194 void setImm(int64_t immVal) {
195 assert(isImm() && "Wrong MachineOperand mutator");
196 contents.immedVal = immVal;
199 void setOffset(int Offset) {
200 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex() ||
201 isJumpTableIndex()) &&
202 "Wrong MachineOperand accessor");
205 void setConstantPoolIndex(unsigned Idx) {
206 assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
207 contents.immedVal = Idx;
210 /// isIdenticalTo - Return true if this operand is identical to the specified
212 bool isIdenticalTo(const MachineOperand &Other) const;
214 /// ChangeToImmediate - Replace this operand with a new immediate operand of
215 /// the specified value. If an operand is known to be an immediate already,
216 /// the setImmedValue method should be used.
217 void ChangeToImmediate(int64_t ImmVal) {
218 opType = MO_Immediate;
219 contents.immedVal = ImmVal;
222 /// ChangeToRegister - Replace this operand with a new register operand of
223 /// the specified value. If an operand is known to be an register already,
224 /// the setReg method should be used.
225 void ChangeToRegister(unsigned Reg, bool isDef) {
226 opType = MO_Register;
227 contents.RegNo = Reg;
231 friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
233 friend class MachineInstr;
237 //===----------------------------------------------------------------------===//
238 /// MachineInstr - Representation of each machine instruction.
241 short Opcode; // the opcode
242 std::vector<MachineOperand> Operands; // the operands
243 MachineInstr* prev, *next; // links for our intrusive list
244 MachineBasicBlock* parent; // pointer to the owning basic block
246 // OperandComplete - Return true if it's illegal to add a new operand
247 bool OperandsComplete() const;
249 MachineInstr(const MachineInstr&);
250 void operator=(const MachineInstr&); // DO NOT IMPLEMENT
252 // Intrusive list support
254 friend struct ilist_traits<MachineInstr>;
257 /// MachineInstr ctor - This constructor reserve's space for numOperand
259 MachineInstr(short Opcode, unsigned numOperands);
261 /// MachineInstr ctor - Work exactly the same as the ctor above, except that
262 /// the MachineInstr is created and added to the end of the specified basic
265 MachineInstr(MachineBasicBlock *MBB, short Opcode, unsigned numOps);
269 const MachineBasicBlock* getParent() const { return parent; }
270 MachineBasicBlock* getParent() { return parent; }
272 /// getOpcode - Returns the opcode of this MachineInstr.
274 const int getOpcode() const { return Opcode; }
276 /// Access to explicit operands of the instruction.
278 unsigned getNumOperands() const { return Operands.size(); }
280 const MachineOperand& getOperand(unsigned i) const {
281 assert(i < getNumOperands() && "getOperand() out of range!");
284 MachineOperand& getOperand(unsigned i) {
285 assert(i < getNumOperands() && "getOperand() out of range!");
290 /// isIdenticalTo - Return true if this instruction is identical to (same
291 /// opcode and same operands as) the specified instruction.
292 bool isIdenticalTo(const MachineInstr *Other) const {
293 if (Other->getOpcode() != getOpcode() ||
294 getNumOperands() != getNumOperands())
296 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
297 if (!getOperand(i).isIdenticalTo(Other->getOperand(i)))
302 /// clone - Create a copy of 'this' instruction that is identical in
303 /// all ways except the the instruction has no parent, prev, or next.
304 MachineInstr* clone() const { return new MachineInstr(*this); }
306 /// removeFromParent - This method unlinks 'this' from the containing basic
307 /// block, and returns it, but does not delete it.
308 MachineInstr *removeFromParent();
310 /// eraseFromParent - This method unlinks 'this' from the containing basic
311 /// block and deletes it.
312 void eraseFromParent() {
313 delete removeFromParent();
319 void print(std::ostream &OS, const TargetMachine *TM) const;
321 friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
323 //===--------------------------------------------------------------------===//
324 // Accessors to add operands when building up machine instructions.
327 /// addRegOperand - Add a register operand.
329 void addRegOperand(unsigned Reg, bool IsDef) {
330 MachineOperand &Op = AddNewOperand();
331 Op.opType = MachineOperand::MO_Register;
333 Op.contents.RegNo = Reg;
337 /// addImmOperand - Add a zero extended constant argument to the
338 /// machine instruction.
340 void addImmOperand(int64_t Val) {
341 MachineOperand &Op = AddNewOperand();
342 Op.opType = MachineOperand::MO_Immediate;
343 Op.contents.immedVal = Val;
347 void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
348 MachineOperand &Op = AddNewOperand();
349 Op.opType = MachineOperand::MO_MachineBasicBlock;
350 Op.contents.MBB = MBB;
354 /// addFrameIndexOperand - Add an abstract frame index to the instruction
356 void addFrameIndexOperand(unsigned Idx) {
357 MachineOperand &Op = AddNewOperand();
358 Op.opType = MachineOperand::MO_FrameIndex;
359 Op.contents.immedVal = Idx;
363 /// addConstantPoolndexOperand - Add a constant pool object index to the
366 void addConstantPoolIndexOperand(unsigned Idx, int Offset) {
367 MachineOperand &Op = AddNewOperand();
368 Op.opType = MachineOperand::MO_ConstantPoolIndex;
369 Op.contents.immedVal = Idx;
373 /// addJumpTableIndexOperand - Add a jump table object index to the
376 void addJumpTableIndexOperand(unsigned Idx) {
377 MachineOperand &Op = AddNewOperand();
378 Op.opType = MachineOperand::MO_JumpTableIndex;
379 Op.contents.immedVal = Idx;
383 void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
384 MachineOperand &Op = AddNewOperand();
385 Op.opType = MachineOperand::MO_GlobalAddress;
390 /// addExternalSymbolOperand - Add an external symbol operand to this instr
392 void addExternalSymbolOperand(const char *SymName) {
393 MachineOperand &Op = AddNewOperand();
394 Op.opType = MachineOperand::MO_ExternalSymbol;
395 Op.contents.SymbolName = SymName;
399 //===--------------------------------------------------------------------===//
400 // Accessors used to modify instructions in place.
403 /// setOpcode - Replace the opcode of the current instruction with a new one.
405 void setOpcode(unsigned Op) { Opcode = Op; }
407 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
408 /// fewer operand than it started with.
410 void RemoveOperand(unsigned i) {
411 Operands.erase(Operands.begin()+i);
414 MachineOperand &AddNewOperand() {
415 assert(!OperandsComplete() &&
416 "Trying to add an operand to a machine instr that is already done!");
417 Operands.push_back(MachineOperand());
418 return Operands.back();
422 //===----------------------------------------------------------------------===//
425 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
426 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
428 } // End llvm namespace