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 void print(std::ostream &os) const;
81 void print(std::ostream *os) const { if (os) print(*os); }
84 MachineOperand(const MachineOperand &M) {
90 static MachineOperand CreateImm(int64_t Val) {
92 Op.opType = MachineOperand::MO_Immediate;
93 Op.contents.immedVal = Val;
102 const MachineOperand &operator=(const MachineOperand &MO) {
103 contents = MO.contents;
113 /// getType - Returns the MachineOperandType for this operand.
115 MachineOperandType getType() const { return opType; }
117 /// Accessors that tell you what kind of MachineOperand you're looking at.
119 bool isReg() const { return opType == MO_Register; }
120 bool isImm() const { return opType == MO_Immediate; }
121 bool isMBB() const { return opType == MO_MachineBasicBlock; }
123 bool isRegister() const { return opType == MO_Register; }
124 bool isImmediate() const { return opType == MO_Immediate; }
125 bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
126 bool isFrameIndex() const { return opType == MO_FrameIndex; }
127 bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
128 bool isJumpTableIndex() const { return opType == MO_JumpTableIndex; }
129 bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
130 bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
132 int64_t getImm() const {
133 assert(isImm() && "Wrong MachineOperand accessor");
134 return contents.immedVal;
137 int64_t getImmedValue() const {
138 assert(isImm() && "Wrong MachineOperand accessor");
139 return contents.immedVal;
141 MachineBasicBlock *getMBB() const {
142 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
145 MachineBasicBlock *getMachineBasicBlock() const {
146 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
149 void setMachineBasicBlock(MachineBasicBlock *MBB) {
150 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
153 int getFrameIndex() const {
154 assert(isFrameIndex() && "Wrong MachineOperand accessor");
155 return (int)contents.immedVal;
157 unsigned getConstantPoolIndex() const {
158 assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
159 return (unsigned)contents.immedVal;
161 unsigned getJumpTableIndex() const {
162 assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
163 return (unsigned)contents.immedVal;
165 GlobalValue *getGlobal() const {
166 assert(isGlobalAddress() && "Wrong MachineOperand accessor");
169 int getOffset() const {
170 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
171 "Wrong MachineOperand accessor");
174 const char *getSymbolName() const {
175 assert(isExternalSymbol() && "Wrong MachineOperand accessor");
176 return contents.SymbolName;
180 assert(isRegister() && "Wrong MachineOperand accessor");
184 assert(isRegister() && "Wrong MachineOperand accessor");
188 assert(isRegister() && "Wrong MachineOperand accessor");
192 assert(isRegister() && "Wrong MachineOperand accessor");
196 bool isImplicit() const {
197 assert(isRegister() && "Wrong MachineOperand accessor");
201 assert(isRegister() && "Wrong MachineOperand accessor");
205 bool isKill() const {
206 assert(isRegister() && "Wrong MachineOperand accessor");
209 bool isDead() const {
210 assert(isRegister() && "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 assert(isRegister() && IsDef && "Wrong MachineOperand accessor");
230 /// getReg - Returns the register number.
232 unsigned getReg() const {
233 assert(isRegister() && "This is not a register operand!");
234 return contents.RegNo;
237 /// MachineOperand mutators.
239 void setReg(unsigned Reg) {
240 assert(isRegister() && "This is not a register operand!");
241 contents.RegNo = Reg;
244 void setImmedValue(int64_t immVal) {
245 assert(isImm() && "Wrong MachineOperand mutator");
246 contents.immedVal = immVal;
248 void setImm(int64_t immVal) {
249 assert(isImm() && "Wrong MachineOperand mutator");
250 contents.immedVal = immVal;
253 void setOffset(int Offset) {
254 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex() ||
255 isJumpTableIndex()) &&
256 "Wrong MachineOperand accessor");
259 void setConstantPoolIndex(unsigned Idx) {
260 assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
261 contents.immedVal = Idx;
263 void setJumpTableIndex(unsigned Idx) {
264 assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
265 contents.immedVal = Idx;
268 /// isIdenticalTo - Return true if this operand is identical to the specified
269 /// operand. Note: This method ignores isKill and isDead properties.
270 bool isIdenticalTo(const MachineOperand &Other) const;
272 /// ChangeToImmediate - Replace this operand with a new immediate operand of
273 /// the specified value. If an operand is known to be an immediate already,
274 /// the setImmedValue method should be used.
275 void ChangeToImmediate(int64_t ImmVal) {
276 opType = MO_Immediate;
277 contents.immedVal = ImmVal;
280 /// ChangeToRegister - Replace this operand with a new register operand of
281 /// the specified value. If an operand is known to be an register already,
282 /// the setReg method should be used.
283 void ChangeToRegister(unsigned Reg, bool isDef) {
284 opType = MO_Register;
285 contents.RegNo = Reg;
292 friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop) {
297 friend class MachineInstr;
301 //===----------------------------------------------------------------------===//
302 /// MachineInstr - Representation of each machine instruction.
305 const TargetInstrDescriptor *TID; // Instruction descriptor.
306 unsigned short NumImplicitOps; // Number of implicit operands (which
307 // are determined at construction time).
309 std::vector<MachineOperand> Operands; // the operands
310 MachineInstr* prev, *next; // links for our intrusive list
311 MachineBasicBlock* parent; // pointer to the owning basic block
313 // OperandComplete - Return true if it's illegal to add a new operand
314 bool OperandsComplete() const;
316 MachineInstr(const MachineInstr&);
317 void operator=(const MachineInstr&); // DO NOT IMPLEMENT
319 // Intrusive list support
321 friend struct ilist_traits<MachineInstr>;
324 /// MachineInstr ctor - This constructor creates a dummy MachineInstr with
325 /// TID NULL and no operands.
328 /// MachineInstr ctor - This constructor create a MachineInstr and add the
329 /// implicit operands. It reserves space for number of operands specified by
330 /// TargetInstrDescriptor.
331 MachineInstr(const TargetInstrDescriptor &TID);
333 /// MachineInstr ctor - Work exactly the same as the ctor above, except that
334 /// the MachineInstr is created and added to the end of the specified basic
337 MachineInstr(MachineBasicBlock *MBB, const TargetInstrDescriptor &TID);
341 const MachineBasicBlock* getParent() const { return parent; }
342 MachineBasicBlock* getParent() { return parent; }
344 /// getInstrDescriptor - Returns the target instruction descriptor of this
346 const TargetInstrDescriptor *getInstrDescriptor() const { return TID; }
348 /// getOpcode - Returns the opcode of this MachineInstr.
350 const int getOpcode() const;
352 /// Access to explicit operands of the instruction.
354 unsigned getNumOperands() const { return Operands.size(); }
356 const MachineOperand& getOperand(unsigned i) const {
357 assert(i < getNumOperands() && "getOperand() out of range!");
360 MachineOperand& getOperand(unsigned i) {
361 assert(i < getNumOperands() && "getOperand() out of range!");
366 /// isIdenticalTo - Return true if this instruction is identical to (same
367 /// opcode and same operands as) the specified instruction.
368 bool isIdenticalTo(const MachineInstr *Other) const {
369 if (Other->getOpcode() != getOpcode() ||
370 Other->getNumOperands() != getNumOperands())
372 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
373 if (!getOperand(i).isIdenticalTo(Other->getOperand(i)))
378 /// clone - Create a copy of 'this' instruction that is identical in
379 /// all ways except the the instruction has no parent, prev, or next.
380 MachineInstr* clone() const { return new MachineInstr(*this); }
382 /// removeFromParent - This method unlinks 'this' from the containing basic
383 /// block, and returns it, but does not delete it.
384 MachineInstr *removeFromParent();
386 /// eraseFromParent - This method unlinks 'this' from the containing basic
387 /// block and deletes it.
388 void eraseFromParent() {
389 delete removeFromParent();
392 /// findRegisterUseOperand() - Returns the MachineOperand that is a use of
393 /// the specific register or NULL if it is not found.
394 MachineOperand *findRegisterUseOperand(unsigned Reg);
396 /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
398 void copyKillDeadInfo(const MachineInstr *MI);
403 void print(std::ostream *OS, const TargetMachine *TM) const {
404 if (OS) print(*OS, TM);
406 void print(std::ostream &OS, const TargetMachine *TM) const;
407 void print(std::ostream &OS) const;
408 void print(std::ostream *OS) const { if (OS) print(*OS); }
410 friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr){
415 //===--------------------------------------------------------------------===//
416 // Accessors to add operands when building up machine instructions.
419 /// addRegOperand - Add a register operand.
421 void addRegOperand(unsigned Reg, bool IsDef, bool IsImp = false,
422 bool IsKill = false, bool IsDead = false) {
423 MachineOperand &Op = AddNewOperand(IsImp);
424 Op.opType = MachineOperand::MO_Register;
429 Op.contents.RegNo = Reg;
433 /// addImmOperand - Add a zero extended constant argument to the
434 /// machine instruction.
436 void addImmOperand(int64_t Val) {
437 MachineOperand &Op = AddNewOperand();
438 Op.opType = MachineOperand::MO_Immediate;
439 Op.contents.immedVal = Val;
443 void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
444 MachineOperand &Op = AddNewOperand();
445 Op.opType = MachineOperand::MO_MachineBasicBlock;
446 Op.contents.MBB = MBB;
450 /// addFrameIndexOperand - Add an abstract frame index to the instruction
452 void addFrameIndexOperand(unsigned Idx) {
453 MachineOperand &Op = AddNewOperand();
454 Op.opType = MachineOperand::MO_FrameIndex;
455 Op.contents.immedVal = Idx;
459 /// addConstantPoolndexOperand - Add a constant pool object index to the
462 void addConstantPoolIndexOperand(unsigned Idx, int Offset) {
463 MachineOperand &Op = AddNewOperand();
464 Op.opType = MachineOperand::MO_ConstantPoolIndex;
465 Op.contents.immedVal = Idx;
469 /// addJumpTableIndexOperand - Add a jump table object index to the
472 void addJumpTableIndexOperand(unsigned Idx) {
473 MachineOperand &Op = AddNewOperand();
474 Op.opType = MachineOperand::MO_JumpTableIndex;
475 Op.contents.immedVal = Idx;
479 void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
480 MachineOperand &Op = AddNewOperand();
481 Op.opType = MachineOperand::MO_GlobalAddress;
486 /// addExternalSymbolOperand - Add an external symbol operand to this instr
488 void addExternalSymbolOperand(const char *SymName) {
489 MachineOperand &Op = AddNewOperand();
490 Op.opType = MachineOperand::MO_ExternalSymbol;
491 Op.contents.SymbolName = SymName;
495 //===--------------------------------------------------------------------===//
496 // Accessors used to modify instructions in place.
499 /// setInstrDescriptor - Replace the instruction descriptor (thus opcode) of
500 /// the current instruction with a new one.
502 void setInstrDescriptor(const TargetInstrDescriptor &tid) { TID = &tid; }
504 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
505 /// fewer operand than it started with.
507 void RemoveOperand(unsigned i) {
508 Operands.erase(Operands.begin()+i);
511 MachineOperand &AddNewOperand(bool IsImp = false) {
512 assert((IsImp || !OperandsComplete()) &&
513 "Trying to add an operand to a machine instr that is already done!");
514 if (NumImplicitOps == 0) { // This is true most of the time.
515 Operands.push_back(MachineOperand());
516 return Operands.back();
518 return *Operands.insert(Operands.begin()+Operands.size()-NumImplicitOps,
523 /// addImplicitDefUseOperands - Add all implicit def and use operands to
524 /// this instruction.
525 void addImplicitDefUseOperands();
528 //===----------------------------------------------------------------------===//
531 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
532 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
534 } // End llvm namespace