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"
29 class MachineBasicBlock;
30 class TargetInstrInfo;
31 class TargetInstrDescriptor;
35 template <typename T> struct ilist_traits;
36 template <typename T> struct ilist;
38 //===----------------------------------------------------------------------===//
39 // class MachineOperand
41 // Representation of each machine instruction operand.
43 struct MachineOperand {
44 enum MachineOperandType {
45 MO_Register, // Register operand.
46 MO_Immediate, // Immediate Operand
47 MO_MachineBasicBlock, // MachineBasicBlock reference
48 MO_FrameIndex, // Abstract Stack Frame Index
49 MO_ConstantPoolIndex, // Address of indexed Constant in Constant Pool
50 MO_JumpTableIndex, // Address of indexed Jump Table for switch
51 MO_ExternalSymbol, // Name of external global symbol
52 MO_GlobalAddress // Address of a global value
57 GlobalValue *GV; // For MO_GlobalAddress.
58 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
59 const char *SymbolName; // For MO_ExternalSymbol.
60 unsigned RegNo; // For MO_Register.
61 int64_t immedVal; // For MO_Immediate and MO_*Index.
64 MachineOperandType opType:8; // Discriminate the union.
65 bool IsDef : 1; // True if this is a def, false if this is a use.
66 bool IsImp : 1; // True if this is an implicit def or use.
68 bool IsKill : 1; // True if this is a reg use and the reg is dead
69 // immediately after the read.
70 bool IsDead : 1; // True if this is a reg def and the reg is dead
71 // immediately after the write. i.e. A register
72 // that is defined but never used.
74 /// offset - Offset to address of global or external, only valid for
75 /// MO_GlobalAddress, MO_ExternalSym and MO_ConstantPoolIndex
80 MachineOperand(const MachineOperand &M) {
86 static MachineOperand CreateImm(int64_t Val) {
88 Op.opType = MachineOperand::MO_Immediate;
89 Op.contents.immedVal = Val;
98 const MachineOperand &operator=(const MachineOperand &MO) {
99 contents = MO.contents;
109 /// getType - Returns the MachineOperandType for this operand.
111 MachineOperandType getType() const { return opType; }
113 /// Accessors that tell you what kind of MachineOperand you're looking at.
115 bool isReg() const { return opType == MO_Register; }
116 bool isImm() const { return opType == MO_Immediate; }
117 bool isMBB() const { return opType == MO_MachineBasicBlock; }
119 bool isRegister() const { return opType == MO_Register; }
120 bool isImmediate() const { return opType == MO_Immediate; }
121 bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
122 bool isFrameIndex() const { return opType == MO_FrameIndex; }
123 bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
124 bool isJumpTableIndex() const { return opType == MO_JumpTableIndex; }
125 bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
126 bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
128 int64_t getImm() const {
129 assert(isImm() && "Wrong MachineOperand accessor");
130 return contents.immedVal;
133 int64_t getImmedValue() const {
134 assert(isImm() && "Wrong MachineOperand accessor");
135 return contents.immedVal;
137 MachineBasicBlock *getMBB() const {
138 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
141 MachineBasicBlock *getMachineBasicBlock() const {
142 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
145 void setMachineBasicBlock(MachineBasicBlock *MBB) {
146 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
149 int getFrameIndex() const {
150 assert(isFrameIndex() && "Wrong MachineOperand accessor");
151 return (int)contents.immedVal;
153 unsigned getConstantPoolIndex() const {
154 assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
155 return (unsigned)contents.immedVal;
157 unsigned getJumpTableIndex() const {
158 assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
159 return (unsigned)contents.immedVal;
161 GlobalValue *getGlobal() const {
162 assert(isGlobalAddress() && "Wrong MachineOperand accessor");
165 int getOffset() const {
166 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
167 "Wrong MachineOperand accessor");
170 const char *getSymbolName() const {
171 assert(isExternalSymbol() && "Wrong MachineOperand accessor");
172 return contents.SymbolName;
176 assert(isRegister() && "Wrong MachineOperand accessor");
180 assert(isRegister() && "Wrong MachineOperand accessor");
184 assert(isRegister() && "Wrong MachineOperand accessor");
188 assert(isRegister() && "Wrong MachineOperand accessor");
192 bool isImplicit() const {
193 assert(isRegister() && "Wrong MachineOperand accessor");
197 assert(isRegister() && "Wrong MachineOperand accessor");
201 bool isKill() const {
202 assert(isRegister() && "Wrong MachineOperand accessor");
205 bool isDead() const {
206 assert(isRegister() && "Wrong MachineOperand accessor");
210 assert(isRegister() && !IsDef && "Wrong MachineOperand accessor");
214 assert(isRegister() && IsDef && "Wrong MachineOperand accessor");
218 assert(isRegister() && !IsDef && "Wrong MachineOperand accessor");
222 assert(isRegister() && IsDef && "Wrong MachineOperand accessor");
226 /// getReg - Returns the register number.
228 unsigned getReg() const {
229 assert(isRegister() && "This is not a register operand!");
230 return contents.RegNo;
233 /// MachineOperand mutators.
235 void setReg(unsigned Reg) {
236 assert(isRegister() && "This is not a register operand!");
237 contents.RegNo = Reg;
240 void setImmedValue(int64_t immVal) {
241 assert(isImm() && "Wrong MachineOperand mutator");
242 contents.immedVal = immVal;
244 void setImm(int64_t immVal) {
245 assert(isImm() && "Wrong MachineOperand mutator");
246 contents.immedVal = immVal;
249 void setOffset(int Offset) {
250 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex() ||
251 isJumpTableIndex()) &&
252 "Wrong MachineOperand accessor");
255 void setConstantPoolIndex(unsigned Idx) {
256 assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
257 contents.immedVal = Idx;
259 void setJumpTableIndex(unsigned Idx) {
260 assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
261 contents.immedVal = Idx;
264 /// isIdenticalTo - Return true if this operand is identical to the specified
265 /// operand. Note: This method ignores isKill and isDead properties.
266 bool isIdenticalTo(const MachineOperand &Other) const;
268 /// ChangeToImmediate - Replace this operand with a new immediate operand of
269 /// the specified value. If an operand is known to be an immediate already,
270 /// the setImmedValue method should be used.
271 void ChangeToImmediate(int64_t ImmVal) {
272 opType = MO_Immediate;
273 contents.immedVal = ImmVal;
276 /// ChangeToRegister - Replace this operand with a new register operand of
277 /// the specified value. If an operand is known to be an register already,
278 /// the setReg method should be used.
279 void ChangeToRegister(unsigned Reg, bool isDef) {
280 opType = MO_Register;
281 contents.RegNo = Reg;
288 friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
290 friend class MachineInstr;
294 //===----------------------------------------------------------------------===//
295 /// MachineInstr - Representation of each machine instruction.
298 short Opcode; // the opcode
299 short NumImplicitOps; // Number of implicit operands (which
300 // are determined at construction time).
302 std::vector<MachineOperand> Operands; // the operands
303 MachineInstr* prev, *next; // links for our intrusive list
304 MachineBasicBlock* parent; // pointer to the owning basic block
306 // OperandComplete - Return true if it's illegal to add a new operand
307 bool OperandsComplete() const;
309 MachineInstr(const MachineInstr&);
310 void operator=(const MachineInstr&); // DO NOT IMPLEMENT
312 // Intrusive list support
314 friend struct ilist_traits<MachineInstr>;
317 /// MachineInstr ctor - This constructor reserves space for numOperand
319 MachineInstr(short Opcode, unsigned numOperands);
321 /// MachineInstr ctor - This constructor create a MachineInstr and add the
322 /// implicit operands. It reserves space for numOperand operands.
323 MachineInstr(const TargetInstrInfo &TII, short Opcode, unsigned numOperands);
325 /// MachineInstr ctor - Work exactly the same as the ctor above, except that
326 /// the MachineInstr is created and added to the end of the specified basic
329 MachineInstr(MachineBasicBlock *MBB, short Opcode, unsigned numOps);
333 const MachineBasicBlock* getParent() const { return parent; }
334 MachineBasicBlock* getParent() { return parent; }
336 /// getOpcode - Returns the opcode of this MachineInstr.
338 const int getOpcode() const { return Opcode; }
340 /// Access to explicit operands of the instruction.
342 unsigned getNumOperands() const { return Operands.size(); }
344 const MachineOperand& getOperand(unsigned i) const {
345 assert(i < getNumOperands() && "getOperand() out of range!");
348 MachineOperand& getOperand(unsigned i) {
349 assert(i < getNumOperands() && "getOperand() out of range!");
354 /// isIdenticalTo - Return true if this instruction is identical to (same
355 /// opcode and same operands as) the specified instruction.
356 bool isIdenticalTo(const MachineInstr *Other) const {
357 if (Other->getOpcode() != getOpcode() ||
358 Other->getNumOperands() != getNumOperands())
360 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
361 if (!getOperand(i).isIdenticalTo(Other->getOperand(i)))
366 /// clone - Create a copy of 'this' instruction that is identical in
367 /// all ways except the the instruction has no parent, prev, or next.
368 MachineInstr* clone() const { return new MachineInstr(*this); }
370 /// removeFromParent - This method unlinks 'this' from the containing basic
371 /// block, and returns it, but does not delete it.
372 MachineInstr *removeFromParent();
374 /// eraseFromParent - This method unlinks 'this' from the containing basic
375 /// block and deletes it.
376 void eraseFromParent() {
377 delete removeFromParent();
380 /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
382 void copyKillDeadInfo(const MachineInstr *MI) {
383 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
384 const MachineOperand &MO = MI->getOperand(i);
385 if (MO.isReg() && (MO.isKill() || MO.isDead())) {
386 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
387 MachineOperand &MOp = getOperand(j);
388 if (MOp.isIdenticalTo(MO)) {
403 void print(std::ostream &OS, const TargetMachine *TM) const;
405 friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
407 //===--------------------------------------------------------------------===//
408 // Accessors to add operands when building up machine instructions.
411 /// addRegOperand - Add a register operand.
413 void addRegOperand(unsigned Reg, bool IsDef, bool IsImp = false,
414 bool IsKill = false, bool IsDead = false) {
415 MachineOperand &Op = AddNewOperand(IsImp);
416 Op.opType = MachineOperand::MO_Register;
421 Op.contents.RegNo = Reg;
425 /// addImmOperand - Add a zero extended constant argument to the
426 /// machine instruction.
428 void addImmOperand(int64_t Val) {
429 MachineOperand &Op = AddNewOperand();
430 Op.opType = MachineOperand::MO_Immediate;
431 Op.contents.immedVal = Val;
435 void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
436 MachineOperand &Op = AddNewOperand();
437 Op.opType = MachineOperand::MO_MachineBasicBlock;
438 Op.contents.MBB = MBB;
442 /// addFrameIndexOperand - Add an abstract frame index to the instruction
444 void addFrameIndexOperand(unsigned Idx) {
445 MachineOperand &Op = AddNewOperand();
446 Op.opType = MachineOperand::MO_FrameIndex;
447 Op.contents.immedVal = Idx;
451 /// addConstantPoolndexOperand - Add a constant pool object index to the
454 void addConstantPoolIndexOperand(unsigned Idx, int Offset) {
455 MachineOperand &Op = AddNewOperand();
456 Op.opType = MachineOperand::MO_ConstantPoolIndex;
457 Op.contents.immedVal = Idx;
461 /// addJumpTableIndexOperand - Add a jump table object index to the
464 void addJumpTableIndexOperand(unsigned Idx) {
465 MachineOperand &Op = AddNewOperand();
466 Op.opType = MachineOperand::MO_JumpTableIndex;
467 Op.contents.immedVal = Idx;
471 void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
472 MachineOperand &Op = AddNewOperand();
473 Op.opType = MachineOperand::MO_GlobalAddress;
478 /// addExternalSymbolOperand - Add an external symbol operand to this instr
480 void addExternalSymbolOperand(const char *SymName) {
481 MachineOperand &Op = AddNewOperand();
482 Op.opType = MachineOperand::MO_ExternalSymbol;
483 Op.contents.SymbolName = SymName;
487 //===--------------------------------------------------------------------===//
488 // Accessors used to modify instructions in place.
491 /// setOpcode - Replace the opcode of the current instruction with a new one.
493 void setOpcode(unsigned Op) { Opcode = Op; }
495 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
496 /// fewer operand than it started with.
498 void RemoveOperand(unsigned i) {
499 Operands.erase(Operands.begin()+i);
502 MachineOperand &AddNewOperand(bool IsImp = false) {
503 assert((IsImp || !OperandsComplete()) &&
504 "Trying to add an operand to a machine instr that is already done!");
505 if (NumImplicitOps == 0) { // This is true most of the time.
506 Operands.push_back(MachineOperand());
507 return Operands.back();
509 return *Operands.insert(Operands.begin()+Operands.size()-NumImplicitOps,
514 /// addImplicitDefUseOperands - Add all implicit def and use operands to
515 /// this instruction.
516 void addImplicitDefUseOperands(const TargetInstrDescriptor &TID);
519 //===----------------------------------------------------------------------===//
522 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
523 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
525 } // End llvm namespace