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;
209 void setJumpTableIndex(unsigned Idx) {
210 assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
211 contents.immedVal = Idx;
214 /// isIdenticalTo - Return true if this operand is identical to the specified
216 bool isIdenticalTo(const MachineOperand &Other) const;
218 /// ChangeToImmediate - Replace this operand with a new immediate operand of
219 /// the specified value. If an operand is known to be an immediate already,
220 /// the setImmedValue method should be used.
221 void ChangeToImmediate(int64_t ImmVal) {
222 opType = MO_Immediate;
223 contents.immedVal = ImmVal;
226 /// ChangeToRegister - Replace this operand with a new register operand of
227 /// the specified value. If an operand is known to be an register already,
228 /// the setReg method should be used.
229 void ChangeToRegister(unsigned Reg, bool isDef) {
230 opType = MO_Register;
231 contents.RegNo = Reg;
235 friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
237 friend class MachineInstr;
241 //===----------------------------------------------------------------------===//
242 /// MachineInstr - Representation of each machine instruction.
245 short Opcode; // the opcode
246 std::vector<MachineOperand> Operands; // the operands
247 MachineInstr* prev, *next; // links for our intrusive list
248 MachineBasicBlock* parent; // pointer to the owning basic block
250 // OperandComplete - Return true if it's illegal to add a new operand
251 bool OperandsComplete() const;
253 MachineInstr(const MachineInstr&);
254 void operator=(const MachineInstr&); // DO NOT IMPLEMENT
256 // Intrusive list support
258 friend struct ilist_traits<MachineInstr>;
261 /// MachineInstr ctor - This constructor reserve's space for numOperand
263 MachineInstr(short Opcode, unsigned numOperands);
265 /// MachineInstr ctor - Work exactly the same as the ctor above, except that
266 /// the MachineInstr is created and added to the end of the specified basic
269 MachineInstr(MachineBasicBlock *MBB, short Opcode, unsigned numOps);
273 const MachineBasicBlock* getParent() const { return parent; }
274 MachineBasicBlock* getParent() { return parent; }
276 /// getOpcode - Returns the opcode of this MachineInstr.
278 const int getOpcode() const { return Opcode; }
280 /// Access to explicit operands of the instruction.
282 unsigned getNumOperands() const { return Operands.size(); }
284 const MachineOperand& getOperand(unsigned i) const {
285 assert(i < getNumOperands() && "getOperand() out of range!");
288 MachineOperand& getOperand(unsigned i) {
289 assert(i < getNumOperands() && "getOperand() out of range!");
294 /// isIdenticalTo - Return true if this instruction is identical to (same
295 /// opcode and same operands as) the specified instruction.
296 bool isIdenticalTo(const MachineInstr *Other) const {
297 if (Other->getOpcode() != getOpcode() ||
298 Other->getNumOperands() != getNumOperands())
300 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
301 if (!getOperand(i).isIdenticalTo(Other->getOperand(i)))
306 /// clone - Create a copy of 'this' instruction that is identical in
307 /// all ways except the the instruction has no parent, prev, or next.
308 MachineInstr* clone() const { return new MachineInstr(*this); }
310 /// removeFromParent - This method unlinks 'this' from the containing basic
311 /// block, and returns it, but does not delete it.
312 MachineInstr *removeFromParent();
314 /// eraseFromParent - This method unlinks 'this' from the containing basic
315 /// block and deletes it.
316 void eraseFromParent() {
317 delete removeFromParent();
323 void print(std::ostream &OS, const TargetMachine *TM) const;
325 friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
327 //===--------------------------------------------------------------------===//
328 // Accessors to add operands when building up machine instructions.
331 /// addRegOperand - Add a register operand.
333 void addRegOperand(unsigned Reg, bool IsDef) {
334 MachineOperand &Op = AddNewOperand();
335 Op.opType = MachineOperand::MO_Register;
337 Op.contents.RegNo = Reg;
341 /// addImmOperand - Add a zero extended constant argument to the
342 /// machine instruction.
344 void addImmOperand(int64_t Val) {
345 MachineOperand &Op = AddNewOperand();
346 Op.opType = MachineOperand::MO_Immediate;
347 Op.contents.immedVal = Val;
351 void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
352 MachineOperand &Op = AddNewOperand();
353 Op.opType = MachineOperand::MO_MachineBasicBlock;
354 Op.contents.MBB = MBB;
358 /// addFrameIndexOperand - Add an abstract frame index to the instruction
360 void addFrameIndexOperand(unsigned Idx) {
361 MachineOperand &Op = AddNewOperand();
362 Op.opType = MachineOperand::MO_FrameIndex;
363 Op.contents.immedVal = Idx;
367 /// addConstantPoolndexOperand - Add a constant pool object index to the
370 void addConstantPoolIndexOperand(unsigned Idx, int Offset) {
371 MachineOperand &Op = AddNewOperand();
372 Op.opType = MachineOperand::MO_ConstantPoolIndex;
373 Op.contents.immedVal = Idx;
377 /// addJumpTableIndexOperand - Add a jump table object index to the
380 void addJumpTableIndexOperand(unsigned Idx) {
381 MachineOperand &Op = AddNewOperand();
382 Op.opType = MachineOperand::MO_JumpTableIndex;
383 Op.contents.immedVal = Idx;
387 void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
388 MachineOperand &Op = AddNewOperand();
389 Op.opType = MachineOperand::MO_GlobalAddress;
394 /// addExternalSymbolOperand - Add an external symbol operand to this instr
396 void addExternalSymbolOperand(const char *SymName) {
397 MachineOperand &Op = AddNewOperand();
398 Op.opType = MachineOperand::MO_ExternalSymbol;
399 Op.contents.SymbolName = SymName;
403 //===--------------------------------------------------------------------===//
404 // Accessors used to modify instructions in place.
407 /// setOpcode - Replace the opcode of the current instruction with a new one.
409 void setOpcode(unsigned Op) { Opcode = Op; }
411 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
412 /// fewer operand than it started with.
414 void RemoveOperand(unsigned i) {
415 Operands.erase(Operands.begin()+i);
418 MachineOperand &AddNewOperand() {
419 assert(!OperandsComplete() &&
420 "Trying to add an operand to a machine instr that is already done!");
421 Operands.push_back(MachineOperand());
422 return Operands.back();
426 //===----------------------------------------------------------------------===//
429 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
430 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
432 } // End llvm namespace