1 //===-- MachineInstr.cpp --------------------------------------------------===//
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 // Methods common to all machine instructions.
12 // FIXME: Now that MachineInstrs have parent pointers, they should always
13 // print themselves using their MachineFunction's TargetMachine.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/Value.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/Target/TargetInstrInfo.h"
22 #include "llvm/Target/MRegisterInfo.h"
23 #include "llvm/Support/LeakDetector.h"
28 // Global variable holding an array of descriptors for machine instructions.
29 // The actual object needs to be created separately for each target machine.
30 // This variable is initialized and reset by class TargetInstrInfo.
32 // FIXME: This should be a property of the target so that more than one target
33 // at a time can be active...
36 extern const TargetInstrDescriptor *TargetInstrDescriptors;
39 /// MachineInstr ctor - This constructor only does a _reserve_ of the operands,
40 /// not a resize for them. It is expected that if you use this that you call
41 /// add* methods below to fill up the operands, instead of the Set methods.
42 /// Eventually, the "resizing" ctors will be phased out.
44 MachineInstr::MachineInstr(short opcode, unsigned numOperands, bool XX, bool YY)
45 : Opcode(opcode), parent(0) {
46 operands.reserve(numOperands);
47 // Make sure that we get added to a machine basicblock
48 LeakDetector::addGarbageObject(this);
51 /// MachineInstr ctor - Work exactly the same as the ctor above, except that the
52 /// MachineInstr is created and added to the end of the specified basic block.
54 MachineInstr::MachineInstr(MachineBasicBlock *MBB, short opcode,
56 : Opcode(opcode), parent(0) {
57 assert(MBB && "Cannot use inserting ctor with null basic block!");
58 operands.reserve(numOperands);
59 // Make sure that we get added to a machine basicblock
60 LeakDetector::addGarbageObject(this);
61 MBB->push_back(this); // Add instruction to end of basic block!
64 /// MachineInstr ctor - Copies MachineInstr arg exactly
66 MachineInstr::MachineInstr(const MachineInstr &MI) {
67 Opcode = MI.getOpcode();
68 operands.reserve(MI.getNumOperands());
71 for (unsigned i = 0; i < MI.getNumOperands(); ++i)
72 operands.push_back(MachineOperand(MI.getOperand(i)));
74 // Set parent, next, and prev to null
81 MachineInstr::~MachineInstr() {
82 LeakDetector::removeGarbageObject(this);
85 /// clone - Create a copy of 'this' instruction that is identical in all ways
86 /// except the following: the new instruction has no parent and it has no name
88 MachineInstr* MachineInstr::clone() const {
89 return new MachineInstr(*this);
92 /// removeFromParent - This method unlinks 'this' from the containing basic
93 /// block, and returns it, but does not delete it.
94 MachineInstr *MachineInstr::removeFromParent() {
95 assert(getParent() && "Not embedded in a basic block!");
96 getParent()->remove(this);
101 /// OperandComplete - Return true if it's illegal to add a new operand
103 bool MachineInstr::OperandsComplete() const {
104 int NumOperands = TargetInstrDescriptors[Opcode].numOperands;
105 if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands)
106 return true; // Broken: we have all the operands of this instruction!
110 void MachineInstr::SetMachineOperandVal(unsigned i,
111 MachineOperand::MachineOperandType opTy,
113 assert(i < operands.size()); // may be explicit or implicit op
114 operands[i].opType = opTy;
115 operands[i].contents.value = V;
116 operands[i].extra.regNum = -1;
120 MachineInstr::SetMachineOperandConst(unsigned i,
121 MachineOperand::MachineOperandType opTy,
123 assert(i < getNumOperands()); // must be explicit op
125 operands[i].opType = opTy;
126 operands[i].contents.value = NULL;
127 operands[i].contents.immedVal = intValue;
128 operands[i].extra.regNum = -1;
129 operands[i].flags = 0;
132 void MachineInstr::SetMachineOperandReg(unsigned i, int regNum) {
133 assert(i < getNumOperands()); // must be explicit op
135 operands[i].opType = MachineOperand::MO_VirtualRegister;
136 operands[i].contents.value = NULL;
137 operands[i].extra.regNum = regNum;
140 void MachineInstr::dump() const {
141 std::cerr << " " << *this;
144 static inline std::ostream& OutputValue(std::ostream &os, const Value* val) {
146 os << (void*) val; // print address always
147 if (val && val->hasName())
148 os << " " << val->getName(); // print name also, if available
153 static inline void OutputReg(std::ostream &os, unsigned RegNo,
154 const MRegisterInfo *MRI = 0) {
155 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
157 os << "%" << MRI->get(RegNo).Name;
159 os << "%mreg(" << RegNo << ")";
161 os << "%reg" << RegNo;
164 static void print(const MachineOperand &MO, std::ostream &OS,
165 const TargetMachine *TM) {
166 const MRegisterInfo *MRI = 0;
168 if (TM) MRI = TM->getRegisterInfo();
170 switch (MO.getType()) {
171 case MachineOperand::MO_VirtualRegister:
172 OutputReg(OS, MO.getReg(), MRI);
174 case MachineOperand::MO_SignExtendedImmed:
175 OS << (long)MO.getImmedValue();
177 case MachineOperand::MO_UnextendedImmed:
178 OS << (long)MO.getImmedValue();
180 case MachineOperand::MO_MachineBasicBlock:
182 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
183 << "," << (void*)MO.getMachineBasicBlock() << ">";
185 case MachineOperand::MO_FrameIndex:
186 OS << "<fi#" << MO.getFrameIndex() << ">";
188 case MachineOperand::MO_ConstantPoolIndex:
189 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
191 case MachineOperand::MO_JumpTableIndex:
192 OS << "<jt#" << MO.getJumpTableIndex() << ">";
194 case MachineOperand::MO_GlobalAddress:
195 OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
196 if (MO.getOffset()) OS << "+" << MO.getOffset();
199 case MachineOperand::MO_ExternalSymbol:
200 OS << "<es:" << MO.getSymbolName();
201 if (MO.getOffset()) OS << "+" << MO.getOffset();
205 assert(0 && "Unrecognized operand type");
209 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
210 unsigned StartOp = 0;
212 // Specialize printing if op#0 is definition
213 if (getNumOperands() && getOperand(0).isDef() && !getOperand(0).isUse()) {
214 ::print(getOperand(0), OS, TM);
216 ++StartOp; // Don't print this operand again!
219 // Must check if Target machine is not null because machine BB could not
220 // be attached to a Machine function yet
222 OS << TM->getInstrInfo()->getName(getOpcode());
224 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
225 const MachineOperand& mop = getOperand(i);
229 ::print(mop, OS, TM);
241 std::ostream &llvm::operator<<(std::ostream &os, const MachineInstr &MI) {
242 // If the instruction is embedded into a basic block, we can find the target
243 // info for the instruction.
244 if (const MachineBasicBlock *MBB = MI.getParent()) {
245 const MachineFunction *MF = MBB->getParent();
247 MI.print(os, &MF->getTarget());
253 // Otherwise, print it out in the "raw" format without symbolic register names
255 os << TargetInstrDescriptors[MI.getOpcode()].Name;
257 for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
258 os << "\t" << MI.getOperand(i);
259 if (MI.getOperand(i).isDef())
260 if (MI.getOperand(i).isUse())
269 std::ostream &llvm::operator<<(std::ostream &OS, const MachineOperand &MO) {
270 switch (MO.getType()) {
271 case MachineOperand::MO_VirtualRegister:
272 OutputReg(OS, MO.getReg());
274 case MachineOperand::MO_SignExtendedImmed:
275 OS << (long)MO.getImmedValue();
277 case MachineOperand::MO_UnextendedImmed:
278 OS << (long)MO.getImmedValue();
280 case MachineOperand::MO_MachineBasicBlock:
282 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
283 << "@" << (void*)MO.getMachineBasicBlock() << ">";
285 case MachineOperand::MO_FrameIndex:
286 OS << "<fi#" << MO.getFrameIndex() << ">";
288 case MachineOperand::MO_ConstantPoolIndex:
289 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
291 case MachineOperand::MO_JumpTableIndex:
292 OS << "<jt#" << MO.getJumpTableIndex() << ">";
294 case MachineOperand::MO_GlobalAddress:
295 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
297 case MachineOperand::MO_ExternalSymbol:
298 OS << "<es:" << MO.getSymbolName() << ">";
301 assert(0 && "Unrecognized operand type");