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 const MachineOperand &operator=(const MachineOperand &MO) {
77 contents = MO.contents;
84 /// getType - Returns the MachineOperandType for this operand.
86 MachineOperandType getType() const { return opType; }
88 /// Accessors that tell you what kind of MachineOperand you're looking at.
90 bool isReg() const { return opType == MO_Register; }
91 bool isImm() const { return opType == MO_Immediate; }
92 bool isMBB() const { return opType == MO_MachineBasicBlock; }
94 bool isRegister() const { return opType == MO_Register; }
95 bool isImmediate() const { return opType == MO_Immediate; }
96 bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
97 bool isFrameIndex() const { return opType == MO_FrameIndex; }
98 bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
99 bool isJumpTableIndex() const { return opType == MO_JumpTableIndex; }
100 bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
101 bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
103 int64_t getImm() const {
104 assert(isImm() && "Wrong MachineOperand accessor");
105 return contents.immedVal;
108 int64_t getImmedValue() const {
109 assert(isImm() && "Wrong MachineOperand accessor");
110 return contents.immedVal;
112 MachineBasicBlock *getMachineBasicBlock() const {
113 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
116 void setMachineBasicBlock(MachineBasicBlock *MBB) {
117 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
120 int getFrameIndex() const {
121 assert(isFrameIndex() && "Wrong MachineOperand accessor");
122 return (int)contents.immedVal;
124 unsigned getConstantPoolIndex() const {
125 assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
126 return (unsigned)contents.immedVal;
128 unsigned getJumpTableIndex() const {
129 assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
130 return (unsigned)contents.immedVal;
132 GlobalValue *getGlobal() const {
133 assert(isGlobalAddress() && "Wrong MachineOperand accessor");
136 int getOffset() const {
137 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
138 "Wrong MachineOperand accessor");
141 const char *getSymbolName() const {
142 assert(isExternalSymbol() && "Wrong MachineOperand accessor");
143 return contents.SymbolName;
147 assert(isRegister() && "Wrong MachineOperand accessor");
151 assert(isRegister() && "Wrong MachineOperand accessor");
155 assert(isRegister() && "Wrong MachineOperand accessor");
159 assert(isRegister() && "Wrong MachineOperand accessor");
163 /// getReg - Returns the register number.
165 unsigned getReg() const {
166 assert(isRegister() && "This is not a register operand!");
167 return contents.RegNo;
170 /// MachineOperand mutators.
172 void setReg(unsigned Reg) {
173 assert(isRegister() && "This is not a register operand!");
174 contents.RegNo = Reg;
177 void setImmedValue(int64_t immVal) {
178 assert(isImm() && "Wrong MachineOperand mutator");
179 contents.immedVal = immVal;
181 void setImm(int64_t immVal) {
182 assert(isImm() && "Wrong MachineOperand mutator");
183 contents.immedVal = immVal;
186 void setOffset(int Offset) {
187 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex() ||
188 isJumpTableIndex()) &&
189 "Wrong MachineOperand accessor");
192 void setConstantPoolIndex(unsigned Idx) {
193 assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
194 contents.immedVal = Idx;
198 /// ChangeToImmediate - Replace this operand with a new immediate operand of
199 /// the specified value. If an operand is known to be an immediate already,
200 /// the setImmedValue method should be used.
201 void ChangeToImmediate(int64_t ImmVal) {
202 opType = MO_Immediate;
203 contents.immedVal = ImmVal;
206 /// ChangeToRegister - Replace this operand with a new register operand of
207 /// the specified value. If an operand is known to be an register already,
208 /// the setReg method should be used.
209 void ChangeToRegister(unsigned Reg, bool isDef) {
210 opType = MO_Register;
211 contents.RegNo = Reg;
215 friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
217 friend class MachineInstr;
221 //===----------------------------------------------------------------------===//
222 /// MachineInstr - Representation of each machine instruction.
225 short Opcode; // the opcode
226 std::vector<MachineOperand> Operands; // the operands
227 MachineInstr* prev, *next; // links for our intrusive list
228 MachineBasicBlock* parent; // pointer to the owning basic block
230 // OperandComplete - Return true if it's illegal to add a new operand
231 bool OperandsComplete() const;
233 MachineInstr(const MachineInstr&);
234 void operator=(const MachineInstr&); // DO NOT IMPLEMENT
236 // Intrusive list support
238 friend struct ilist_traits<MachineInstr>;
241 /// MachineInstr ctor - This constructor reserve's space for numOperand
243 MachineInstr(short Opcode, unsigned numOperands);
245 /// MachineInstr ctor - Work exactly the same as the ctor above, except that
246 /// the MachineInstr is created and added to the end of the specified basic
249 MachineInstr(MachineBasicBlock *MBB, short Opcode, unsigned numOps);
253 const MachineBasicBlock* getParent() const { return parent; }
254 MachineBasicBlock* getParent() { return parent; }
256 /// getOpcode - Returns the opcode of this MachineInstr.
258 const int getOpcode() const { return Opcode; }
260 /// Access to explicit operands of the instruction.
262 unsigned getNumOperands() const { return Operands.size(); }
264 const MachineOperand& getOperand(unsigned i) const {
265 assert(i < getNumOperands() && "getOperand() out of range!");
268 MachineOperand& getOperand(unsigned i) {
269 assert(i < getNumOperands() && "getOperand() out of range!");
274 /// clone - Create a copy of 'this' instruction that is identical in
275 /// all ways except the the instruction has no parent, prev, or next.
276 MachineInstr* clone() const { return new MachineInstr(*this); }
278 /// removeFromParent - This method unlinks 'this' from the containing basic
279 /// block, and returns it, but does not delete it.
280 MachineInstr *removeFromParent();
282 /// eraseFromParent - This method unlinks 'this' from the containing basic
283 /// block and deletes it.
284 void eraseFromParent() {
285 delete removeFromParent();
291 void print(std::ostream &OS, const TargetMachine *TM) const;
293 friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
295 //===--------------------------------------------------------------------===//
296 // Accessors to add operands when building up machine instructions.
299 /// addRegOperand - Add a register operand.
301 void addRegOperand(unsigned Reg, bool IsDef) {
302 MachineOperand &Op = AddNewOperand();
303 Op.opType = MachineOperand::MO_Register;
305 Op.contents.RegNo = Reg;
309 /// addImmOperand - Add a zero extended constant argument to the
310 /// machine instruction.
312 void addImmOperand(int64_t Val) {
313 MachineOperand &Op = AddNewOperand();
314 Op.opType = MachineOperand::MO_Immediate;
315 Op.contents.immedVal = Val;
319 void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
320 MachineOperand &Op = AddNewOperand();
321 Op.opType = MachineOperand::MO_MachineBasicBlock;
322 Op.contents.MBB = MBB;
326 /// addFrameIndexOperand - Add an abstract frame index to the instruction
328 void addFrameIndexOperand(unsigned Idx) {
329 MachineOperand &Op = AddNewOperand();
330 Op.opType = MachineOperand::MO_FrameIndex;
331 Op.contents.immedVal = Idx;
335 /// addConstantPoolndexOperand - Add a constant pool object index to the
338 void addConstantPoolIndexOperand(unsigned Idx, int Offset) {
339 MachineOperand &Op = AddNewOperand();
340 Op.opType = MachineOperand::MO_ConstantPoolIndex;
341 Op.contents.immedVal = Idx;
345 /// addJumpTableIndexOperand - Add a jump table object index to the
348 void addJumpTableIndexOperand(unsigned Idx) {
349 MachineOperand &Op = AddNewOperand();
350 Op.opType = MachineOperand::MO_JumpTableIndex;
351 Op.contents.immedVal = Idx;
355 void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
356 MachineOperand &Op = AddNewOperand();
357 Op.opType = MachineOperand::MO_GlobalAddress;
362 /// addExternalSymbolOperand - Add an external symbol operand to this instr
364 void addExternalSymbolOperand(const char *SymName) {
365 MachineOperand &Op = AddNewOperand();
366 Op.opType = MachineOperand::MO_ExternalSymbol;
367 Op.contents.SymbolName = SymName;
371 //===--------------------------------------------------------------------===//
372 // Accessors used to modify instructions in place.
375 /// setOpcode - Replace the opcode of the current instruction with a new one.
377 void setOpcode(unsigned Op) { Opcode = Op; }
379 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
380 /// fewer operand than it started with.
382 void RemoveOperand(unsigned i) {
383 Operands.erase(Operands.begin()+i);
386 MachineOperand &AddNewOperand() {
387 assert(!OperandsComplete() &&
388 "Trying to add an operand to a machine instr that is already done!");
389 Operands.push_back(MachineOperand());
390 return Operands.back();
394 //===----------------------------------------------------------------------===//
397 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
398 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
400 } // End llvm namespace