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;
29 class TargetInstrInfo;
30 class TargetInstrDescriptor;
34 template <typename T> struct ilist_traits;
35 template <typename T> struct ilist;
37 //===----------------------------------------------------------------------===//
38 // class MachineOperand
40 // Representation of each machine instruction operand.
42 struct MachineOperand {
43 enum MachineOperandType {
44 MO_Register, // Register operand.
45 MO_Immediate, // Immediate Operand
46 MO_MachineBasicBlock, // MachineBasicBlock reference
47 MO_FrameIndex, // Abstract Stack Frame Index
48 MO_ConstantPoolIndex, // Address of indexed Constant in Constant Pool
49 MO_JumpTableIndex, // Address of indexed Jump Table for switch
50 MO_ExternalSymbol, // Name of external global symbol
51 MO_GlobalAddress // Address of a global value
56 GlobalValue *GV; // For MO_GlobalAddress.
57 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
58 const char *SymbolName; // For MO_ExternalSymbol.
59 unsigned RegNo; // For MO_Register.
60 int64_t immedVal; // For MO_Immediate and MO_*Index.
63 MachineOperandType opType:8; // Discriminate the union.
64 bool IsDef : 1; // True if this is a def, false if this is a use.
65 bool IsImp : 1; // True if this is an implicit def or use.
67 bool IsKill : 1; // True if this is a reg use and the reg is dead
68 // immediately after the read.
69 bool IsDead : 1; // True if this is a reg def and the reg is dead
70 // immediately after the write. i.e. A register
71 // that is defined but never used.
73 /// offset - Offset to address of global or external, only valid for
74 /// MO_GlobalAddress, MO_ExternalSym and MO_ConstantPoolIndex
79 MachineOperand(const MachineOperand &M) {
85 static MachineOperand CreateImm(int64_t Val) {
87 Op.opType = MachineOperand::MO_Immediate;
88 Op.contents.immedVal = Val;
97 const MachineOperand &operator=(const MachineOperand &MO) {
98 contents = MO.contents;
108 /// getType - Returns the MachineOperandType for this operand.
110 MachineOperandType getType() const { return opType; }
112 /// Accessors that tell you what kind of MachineOperand you're looking at.
114 bool isReg() const { return opType == MO_Register; }
115 bool isImm() const { return opType == MO_Immediate; }
116 bool isMBB() const { return opType == MO_MachineBasicBlock; }
118 bool isRegister() const { return opType == MO_Register; }
119 bool isImmediate() const { return opType == MO_Immediate; }
120 bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
121 bool isFrameIndex() const { return opType == MO_FrameIndex; }
122 bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
123 bool isJumpTableIndex() const { return opType == MO_JumpTableIndex; }
124 bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
125 bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
127 int64_t getImm() const {
128 assert(isImm() && "Wrong MachineOperand accessor");
129 return contents.immedVal;
132 int64_t getImmedValue() const {
133 assert(isImm() && "Wrong MachineOperand accessor");
134 return contents.immedVal;
136 MachineBasicBlock *getMBB() const {
137 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
140 MachineBasicBlock *getMachineBasicBlock() const {
141 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
144 void setMachineBasicBlock(MachineBasicBlock *MBB) {
145 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
148 int getFrameIndex() const {
149 assert(isFrameIndex() && "Wrong MachineOperand accessor");
150 return (int)contents.immedVal;
152 unsigned getConstantPoolIndex() const {
153 assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
154 return (unsigned)contents.immedVal;
156 unsigned getJumpTableIndex() const {
157 assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
158 return (unsigned)contents.immedVal;
160 GlobalValue *getGlobal() const {
161 assert(isGlobalAddress() && "Wrong MachineOperand accessor");
164 int getOffset() const {
165 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
166 "Wrong MachineOperand accessor");
169 const char *getSymbolName() const {
170 assert(isExternalSymbol() && "Wrong MachineOperand accessor");
171 return contents.SymbolName;
175 assert(isRegister() && "Wrong MachineOperand accessor");
179 assert(isRegister() && "Wrong MachineOperand accessor");
183 assert(isRegister() && "Wrong MachineOperand accessor");
187 assert(isRegister() && "Wrong MachineOperand accessor");
191 bool isImplicit() const {
192 assert(isRegister() && "Wrong MachineOperand accessor");
196 assert(isRegister() && "Wrong MachineOperand accessor");
200 bool isKill() const {
201 assert(isRegister() && "Wrong MachineOperand accessor");
204 bool isDead() const {
205 assert(isRegister() && "Wrong MachineOperand accessor");
209 assert(isRegister() && !IsDef && "Wrong MachineOperand accessor");
213 assert(isRegister() && IsDef && "Wrong MachineOperand accessor");
217 assert(isRegister() && !IsDef && "Wrong MachineOperand accessor");
221 assert(isRegister() && IsDef && "Wrong MachineOperand accessor");
225 /// getReg - Returns the register number.
227 unsigned getReg() const {
228 assert(isRegister() && "This is not a register operand!");
229 return contents.RegNo;
232 /// MachineOperand mutators.
234 void setReg(unsigned Reg) {
235 assert(isRegister() && "This is not a register operand!");
236 contents.RegNo = Reg;
239 void setImmedValue(int64_t immVal) {
240 assert(isImm() && "Wrong MachineOperand mutator");
241 contents.immedVal = immVal;
243 void setImm(int64_t immVal) {
244 assert(isImm() && "Wrong MachineOperand mutator");
245 contents.immedVal = immVal;
248 void setOffset(int Offset) {
249 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex() ||
250 isJumpTableIndex()) &&
251 "Wrong MachineOperand accessor");
254 void setConstantPoolIndex(unsigned Idx) {
255 assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
256 contents.immedVal = Idx;
258 void setJumpTableIndex(unsigned Idx) {
259 assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
260 contents.immedVal = Idx;
263 /// isIdenticalTo - Return true if this operand is identical to the specified
264 /// operand. Note: This method ignores isKill and isDead properties.
265 bool isIdenticalTo(const MachineOperand &Other) const;
267 /// ChangeToImmediate - Replace this operand with a new immediate operand of
268 /// the specified value. If an operand is known to be an immediate already,
269 /// the setImmedValue method should be used.
270 void ChangeToImmediate(int64_t ImmVal) {
271 opType = MO_Immediate;
272 contents.immedVal = ImmVal;
275 /// ChangeToRegister - Replace this operand with a new register operand of
276 /// the specified value. If an operand is known to be an register already,
277 /// the setReg method should be used.
278 void ChangeToRegister(unsigned Reg, bool isDef) {
279 opType = MO_Register;
280 contents.RegNo = Reg;
287 friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
289 friend class MachineInstr;
293 //===----------------------------------------------------------------------===//
294 /// MachineInstr - Representation of each machine instruction.
297 short Opcode; // the opcode
298 short NumImplicitOps; // Number of implicit operands (which
299 // are determined at construction time).
301 std::vector<MachineOperand> Operands; // the operands
302 MachineInstr* prev, *next; // links for our intrusive list
303 MachineBasicBlock* parent; // pointer to the owning basic block
305 // OperandComplete - Return true if it's illegal to add a new operand
306 bool OperandsComplete() const;
308 MachineInstr(const MachineInstr&);
309 void operator=(const MachineInstr&); // DO NOT IMPLEMENT
311 // Intrusive list support
313 friend struct ilist_traits<MachineInstr>;
316 /// MachineInstr ctor - This constructor reserves space for numOperand
318 MachineInstr(short Opcode, unsigned numOperands);
320 /// MachineInstr ctor - This constructor create a MachineInstr and add the
321 /// implicit operands. It reserves space for numOperand operands.
322 MachineInstr(const TargetInstrInfo &TII, short Opcode, unsigned numOperands);
324 /// MachineInstr ctor - Work exactly the same as the ctor above, except that
325 /// the MachineInstr is created and added to the end of the specified basic
328 MachineInstr(MachineBasicBlock *MBB, short Opcode, unsigned numOps);
332 const MachineBasicBlock* getParent() const { return parent; }
333 MachineBasicBlock* getParent() { return parent; }
335 /// getOpcode - Returns the opcode of this MachineInstr.
337 const int getOpcode() const { return Opcode; }
339 /// Access to explicit operands of the instruction.
341 unsigned getNumOperands() const { return Operands.size(); }
343 const MachineOperand& getOperand(unsigned i) const {
344 assert(i < getNumOperands() && "getOperand() out of range!");
347 MachineOperand& getOperand(unsigned i) {
348 assert(i < getNumOperands() && "getOperand() out of range!");
353 /// isIdenticalTo - Return true if this instruction is identical to (same
354 /// opcode and same operands as) the specified instruction.
355 bool isIdenticalTo(const MachineInstr *Other) const {
356 if (Other->getOpcode() != getOpcode() ||
357 Other->getNumOperands() != getNumOperands())
359 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
360 if (!getOperand(i).isIdenticalTo(Other->getOperand(i)))
365 /// clone - Create a copy of 'this' instruction that is identical in
366 /// all ways except the the instruction has no parent, prev, or next.
367 MachineInstr* clone() const { return new MachineInstr(*this); }
369 /// removeFromParent - This method unlinks 'this' from the containing basic
370 /// block, and returns it, but does not delete it.
371 MachineInstr *removeFromParent();
373 /// eraseFromParent - This method unlinks 'this' from the containing basic
374 /// block and deletes it.
375 void eraseFromParent() {
376 delete removeFromParent();
379 /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
381 void copyKillDeadInfo(const MachineInstr *MI) {
382 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
383 const MachineOperand &MO = MI->getOperand(i);
384 if (MO.isReg() && (MO.isKill() || MO.isDead())) {
385 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
386 MachineOperand &MOp = getOperand(j);
387 if (MOp.isIdenticalTo(MO)) {
402 void print(std::ostream &OS, const TargetMachine *TM) const;
404 friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
406 //===--------------------------------------------------------------------===//
407 // Accessors to add operands when building up machine instructions.
410 /// addRegOperand - Add a register operand.
412 void addRegOperand(unsigned Reg, bool IsDef, bool IsImp = false,
413 bool IsKill = false, bool IsDead = false) {
414 MachineOperand &Op = AddNewOperand(IsImp);
415 Op.opType = MachineOperand::MO_Register;
420 Op.contents.RegNo = Reg;
424 /// addImmOperand - Add a zero extended constant argument to the
425 /// machine instruction.
427 void addImmOperand(int64_t Val) {
428 MachineOperand &Op = AddNewOperand();
429 Op.opType = MachineOperand::MO_Immediate;
430 Op.contents.immedVal = Val;
434 void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
435 MachineOperand &Op = AddNewOperand();
436 Op.opType = MachineOperand::MO_MachineBasicBlock;
437 Op.contents.MBB = MBB;
441 /// addFrameIndexOperand - Add an abstract frame index to the instruction
443 void addFrameIndexOperand(unsigned Idx) {
444 MachineOperand &Op = AddNewOperand();
445 Op.opType = MachineOperand::MO_FrameIndex;
446 Op.contents.immedVal = Idx;
450 /// addConstantPoolndexOperand - Add a constant pool object index to the
453 void addConstantPoolIndexOperand(unsigned Idx, int Offset) {
454 MachineOperand &Op = AddNewOperand();
455 Op.opType = MachineOperand::MO_ConstantPoolIndex;
456 Op.contents.immedVal = Idx;
460 /// addJumpTableIndexOperand - Add a jump table object index to the
463 void addJumpTableIndexOperand(unsigned Idx) {
464 MachineOperand &Op = AddNewOperand();
465 Op.opType = MachineOperand::MO_JumpTableIndex;
466 Op.contents.immedVal = Idx;
470 void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
471 MachineOperand &Op = AddNewOperand();
472 Op.opType = MachineOperand::MO_GlobalAddress;
477 /// addExternalSymbolOperand - Add an external symbol operand to this instr
479 void addExternalSymbolOperand(const char *SymName) {
480 MachineOperand &Op = AddNewOperand();
481 Op.opType = MachineOperand::MO_ExternalSymbol;
482 Op.contents.SymbolName = SymName;
486 //===--------------------------------------------------------------------===//
487 // Accessors used to modify instructions in place.
490 /// setOpcode - Replace the opcode of the current instruction with a new one.
492 void setOpcode(unsigned Op);
494 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
495 /// fewer operand than it started with.
497 void RemoveOperand(unsigned i) {
498 Operands.erase(Operands.begin()+i);
501 MachineOperand &AddNewOperand(bool IsImp = false) {
502 assert((IsImp || !OperandsComplete()) &&
503 "Trying to add an operand to a machine instr that is already done!");
504 if (NumImplicitOps == 0) { // This is true most of the time.
505 Operands.push_back(MachineOperand());
506 return Operands.back();
508 return *Operands.insert(Operands.begin()+Operands.size()-NumImplicitOps,
513 /// addImplicitDefUseOperands - Add all implicit def and use operands to
514 /// this instruction.
515 void addImplicitDefUseOperands(const TargetInstrDescriptor &TID);
518 //===----------------------------------------------------------------------===//
521 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
522 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
524 } // End llvm namespace