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"
21 #include "llvm/Support/Streams.h"
30 class MachineBasicBlock;
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 llvm_ostream& operator<<(llvm_ostream& os, const MachineOperand& mop) {
289 if (os.stream()) *os.stream() << mop;
292 friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
294 friend class MachineInstr;
298 //===----------------------------------------------------------------------===//
299 /// MachineInstr - Representation of each machine instruction.
302 short Opcode; // the opcode
303 unsigned short NumImplicitOps; // Number of implicit operands (which
304 // are determined at construction time).
306 std::vector<MachineOperand> Operands; // the operands
307 MachineInstr* prev, *next; // links for our intrusive list
308 MachineBasicBlock* parent; // pointer to the owning basic block
310 // OperandComplete - Return true if it's illegal to add a new operand
311 bool OperandsComplete() const;
313 MachineInstr(const MachineInstr&);
314 void operator=(const MachineInstr&); // DO NOT IMPLEMENT
316 // Intrusive list support
318 friend struct ilist_traits<MachineInstr>;
321 /// MachineInstr ctor - This constructor creates a dummy MachineInstr with
322 /// opcode 0 and no operands.
325 /// MachineInstr ctor - This constructor create a MachineInstr and add the
326 /// implicit operands. It reserves space for number of operands specified by
327 /// TargetInstrDescriptor.
328 MachineInstr(const TargetInstrDescriptor &TID);
330 /// MachineInstr ctor - Work exactly the same as the ctor above, except that
331 /// the MachineInstr is created and added to the end of the specified basic
334 MachineInstr(MachineBasicBlock *MBB, const TargetInstrDescriptor &TID);
338 const MachineBasicBlock* getParent() const { return parent; }
339 MachineBasicBlock* getParent() { return parent; }
341 /// getOpcode - Returns the opcode of this MachineInstr.
343 const int getOpcode() const { return Opcode; }
345 /// Access to explicit operands of the instruction.
347 unsigned getNumOperands() const { return Operands.size(); }
349 const MachineOperand& getOperand(unsigned i) const {
350 assert(i < getNumOperands() && "getOperand() out of range!");
353 MachineOperand& getOperand(unsigned i) {
354 assert(i < getNumOperands() && "getOperand() out of range!");
359 /// isIdenticalTo - Return true if this instruction is identical to (same
360 /// opcode and same operands as) the specified instruction.
361 bool isIdenticalTo(const MachineInstr *Other) const {
362 if (Other->getOpcode() != getOpcode() ||
363 Other->getNumOperands() != getNumOperands())
365 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
366 if (!getOperand(i).isIdenticalTo(Other->getOperand(i)))
371 /// clone - Create a copy of 'this' instruction that is identical in
372 /// all ways except the the instruction has no parent, prev, or next.
373 MachineInstr* clone() const { return new MachineInstr(*this); }
375 /// removeFromParent - This method unlinks 'this' from the containing basic
376 /// block, and returns it, but does not delete it.
377 MachineInstr *removeFromParent();
379 /// eraseFromParent - This method unlinks 'this' from the containing basic
380 /// block and deletes it.
381 void eraseFromParent() {
382 delete removeFromParent();
385 /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
387 void copyKillDeadInfo(const MachineInstr *MI) {
388 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
389 const MachineOperand &MO = MI->getOperand(i);
390 if (MO.isReg() && (MO.isKill() || MO.isDead())) {
391 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
392 MachineOperand &MOp = getOperand(j);
393 if (MOp.isIdenticalTo(MO)) {
408 void print(llvm_ostream &OS, const TargetMachine *TM) const {
409 if (OS.stream()) print(*OS.stream(), TM);
411 void print(std::ostream &OS, const TargetMachine *TM) const;
413 friend llvm_ostream& operator<<(llvm_ostream& os, const MachineInstr& minstr){
414 if (os.stream()) *os.stream() << minstr;
417 friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
419 //===--------------------------------------------------------------------===//
420 // Accessors to add operands when building up machine instructions.
423 /// addRegOperand - Add a register operand.
425 void addRegOperand(unsigned Reg, bool IsDef, bool IsImp = false,
426 bool IsKill = false, bool IsDead = false) {
427 MachineOperand &Op = AddNewOperand(IsImp);
428 Op.opType = MachineOperand::MO_Register;
433 Op.contents.RegNo = Reg;
437 /// addImmOperand - Add a zero extended constant argument to the
438 /// machine instruction.
440 void addImmOperand(int64_t Val) {
441 MachineOperand &Op = AddNewOperand();
442 Op.opType = MachineOperand::MO_Immediate;
443 Op.contents.immedVal = Val;
447 void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
448 MachineOperand &Op = AddNewOperand();
449 Op.opType = MachineOperand::MO_MachineBasicBlock;
450 Op.contents.MBB = MBB;
454 /// addFrameIndexOperand - Add an abstract frame index to the instruction
456 void addFrameIndexOperand(unsigned Idx) {
457 MachineOperand &Op = AddNewOperand();
458 Op.opType = MachineOperand::MO_FrameIndex;
459 Op.contents.immedVal = Idx;
463 /// addConstantPoolndexOperand - Add a constant pool object index to the
466 void addConstantPoolIndexOperand(unsigned Idx, int Offset) {
467 MachineOperand &Op = AddNewOperand();
468 Op.opType = MachineOperand::MO_ConstantPoolIndex;
469 Op.contents.immedVal = Idx;
473 /// addJumpTableIndexOperand - Add a jump table object index to the
476 void addJumpTableIndexOperand(unsigned Idx) {
477 MachineOperand &Op = AddNewOperand();
478 Op.opType = MachineOperand::MO_JumpTableIndex;
479 Op.contents.immedVal = Idx;
483 void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
484 MachineOperand &Op = AddNewOperand();
485 Op.opType = MachineOperand::MO_GlobalAddress;
490 /// addExternalSymbolOperand - Add an external symbol operand to this instr
492 void addExternalSymbolOperand(const char *SymName) {
493 MachineOperand &Op = AddNewOperand();
494 Op.opType = MachineOperand::MO_ExternalSymbol;
495 Op.contents.SymbolName = SymName;
499 //===--------------------------------------------------------------------===//
500 // Accessors used to modify instructions in place.
503 /// setOpcode - Replace the opcode of the current instruction with a new one.
505 void setOpcode(unsigned Op) { Opcode = Op; }
507 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
508 /// fewer operand than it started with.
510 void RemoveOperand(unsigned i) {
511 Operands.erase(Operands.begin()+i);
514 MachineOperand &AddNewOperand(bool IsImp = false) {
515 assert((IsImp || !OperandsComplete()) &&
516 "Trying to add an operand to a machine instr that is already done!");
517 if (NumImplicitOps == 0) { // This is true most of the time.
518 Operands.push_back(MachineOperand());
519 return Operands.back();
521 return *Operands.insert(Operands.begin()+Operands.size()-NumImplicitOps,
526 /// addImplicitDefUseOperands - Add all implicit def and use operands to
527 /// this instruction.
528 void addImplicitDefUseOperands(const TargetInstrDescriptor &TID);
531 //===----------------------------------------------------------------------===//
534 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
535 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
537 } // End llvm namespace