blob: e0444fa038103a5fe7ad477d40f84019137ceb5b [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- MachineInstr.cpp --------------------------------------------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
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.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaeke21326fc2004-02-13 04:39:32 +00009//
10// Methods common to all machine instructions.
11//
Chris Lattner035dfbe2002-08-09 20:08:06 +000012//===----------------------------------------------------------------------===//
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000013
Chris Lattner822b4fb2001-09-07 17:18:30 +000014#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner8517e1f2004-02-19 16:17:08 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner10491642002-10-30 00:48:05 +000016#include "llvm/Target/TargetMachine.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000017#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner2a79a092002-10-30 00:58:19 +000018#include "llvm/Target/MRegisterInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/Support/LeakDetector.h"
Bill Wendlinga09362e2006-11-28 22:48:48 +000020#include "llvm/Support/Streams.h"
Reid Spencer954da372004-07-04 12:19:56 +000021#include <iostream>
Chris Lattner0742b592004-02-23 18:38:20 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Evan Chengc0f64ff2006-11-27 23:37:22 +000024/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
Evan Cheng67f660c2006-11-30 07:08:44 +000025/// TID NULL and no operands.
Evan Chengc0f64ff2006-11-27 23:37:22 +000026MachineInstr::MachineInstr()
Evan Cheng67f660c2006-11-30 07:08:44 +000027 : TID(0), NumImplicitOps(0), parent(0) {
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000028 // Make sure that we get added to a machine basicblock
29 LeakDetector::addGarbageObject(this);
Chris Lattner72791222002-10-28 20:59:49 +000030}
31
Evan Cheng67f660c2006-11-30 07:08:44 +000032void MachineInstr::addImplicitDefUseOperands() {
33 if (TID->ImplicitDefs)
34 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) {
Evan Chengd7de4962006-11-13 23:34:06 +000035 MachineOperand Op;
36 Op.opType = MachineOperand::MO_Register;
37 Op.IsDef = true;
38 Op.IsImp = true;
39 Op.IsKill = false;
40 Op.IsDead = false;
41 Op.contents.RegNo = *ImpDefs;
42 Op.offset = 0;
43 Operands.push_back(Op);
44 }
Evan Cheng67f660c2006-11-30 07:08:44 +000045 if (TID->ImplicitUses)
46 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) {
Evan Chengd7de4962006-11-13 23:34:06 +000047 MachineOperand Op;
48 Op.opType = MachineOperand::MO_Register;
49 Op.IsDef = false;
50 Op.IsImp = true;
51 Op.IsKill = false;
52 Op.IsDead = false;
53 Op.contents.RegNo = *ImpUses;
54 Op.offset = 0;
55 Operands.push_back(Op);
56 }
57}
58
59/// MachineInstr ctor - This constructor create a MachineInstr and add the
Evan Chengc0f64ff2006-11-27 23:37:22 +000060/// implicit operands. It reserves space for number of operands specified by
61/// TargetInstrDescriptor or the numOperands if it is not zero. (for
62/// instructions with variable number of operands).
Evan Cheng67f660c2006-11-30 07:08:44 +000063MachineInstr::MachineInstr(const TargetInstrDescriptor &tid)
64 : TID(&tid), NumImplicitOps(0), parent(0) {
65 if (TID->ImplicitDefs)
66 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +000067 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +000068 if (TID->ImplicitUses)
69 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +000070 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +000071 Operands.reserve(NumImplicitOps + TID->numOperands);
72 addImplicitDefUseOperands();
Evan Chengd7de4962006-11-13 23:34:06 +000073 // Make sure that we get added to a machine basicblock
74 LeakDetector::addGarbageObject(this);
75}
76
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000077/// MachineInstr ctor - Work exactly the same as the ctor above, except that the
78/// MachineInstr is created and added to the end of the specified basic block.
79///
Evan Chengc0f64ff2006-11-27 23:37:22 +000080MachineInstr::MachineInstr(MachineBasicBlock *MBB,
Evan Cheng67f660c2006-11-30 07:08:44 +000081 const TargetInstrDescriptor &tid)
82 : TID(&tid), NumImplicitOps(0), parent(0) {
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000083 assert(MBB && "Cannot use inserting ctor with null basic block!");
Evan Cheng67f660c2006-11-30 07:08:44 +000084 if (TID->ImplicitDefs)
85 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +000086 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +000087 if (TID->ImplicitUses)
88 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +000089 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +000090 Operands.reserve(NumImplicitOps + TID->numOperands);
91 addImplicitDefUseOperands();
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000092 // Make sure that we get added to a machine basicblock
93 LeakDetector::addGarbageObject(this);
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000094 MBB->push_back(this); // Add instruction to end of basic block!
95}
96
Misha Brukmance22e762004-07-09 14:45:17 +000097/// MachineInstr ctor - Copies MachineInstr arg exactly
98///
Tanya Lattner466b5342004-05-23 19:35:12 +000099MachineInstr::MachineInstr(const MachineInstr &MI) {
Evan Cheng67f660c2006-11-30 07:08:44 +0000100 TID = MI.getInstrDescriptor();
Evan Cheng6b2c05f2006-11-15 20:54:29 +0000101 NumImplicitOps = MI.NumImplicitOps;
Chris Lattner943b5e12006-05-04 19:14:44 +0000102 Operands.reserve(MI.getNumOperands());
Tanya Lattnerb5159ed2004-05-23 20:58:02 +0000103
Misha Brukmance22e762004-07-09 14:45:17 +0000104 // Add operands
Chris Lattner943b5e12006-05-04 19:14:44 +0000105 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
106 Operands.push_back(MI.getOperand(i));
Tanya Lattner0c63e032004-05-24 03:14:18 +0000107
Misha Brukmance22e762004-07-09 14:45:17 +0000108 // Set parent, next, and prev to null
Tanya Lattner0c63e032004-05-24 03:14:18 +0000109 parent = 0;
110 prev = 0;
111 next = 0;
Tanya Lattner466b5342004-05-23 19:35:12 +0000112}
113
114
Misha Brukmance22e762004-07-09 14:45:17 +0000115MachineInstr::~MachineInstr() {
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000116 LeakDetector::removeGarbageObject(this);
117}
118
Evan Cheng67f660c2006-11-30 07:08:44 +0000119/// getOpcode - Returns the opcode of this MachineInstr.
120///
121const int MachineInstr::getOpcode() const {
122 return TID->Opcode;
123}
124
Chris Lattner48d7c062006-04-17 21:35:41 +0000125/// removeFromParent - This method unlinks 'this' from the containing basic
126/// block, and returns it, but does not delete it.
127MachineInstr *MachineInstr::removeFromParent() {
128 assert(getParent() && "Not embedded in a basic block!");
129 getParent()->remove(this);
130 return this;
131}
132
133
Brian Gaeke21326fc2004-02-13 04:39:32 +0000134/// OperandComplete - Return true if it's illegal to add a new operand
135///
Chris Lattner2a90ba62004-02-12 16:09:53 +0000136bool MachineInstr::OperandsComplete() const {
Evan Cheng67f660c2006-11-30 07:08:44 +0000137 unsigned short NumOperands = TID->numOperands;
138 if ((TID->Flags & M_VARIABLE_OPS) == 0 &&
Evan Cheng8bcb0422006-11-28 02:25:34 +0000139 getNumOperands()-NumImplicitOps >= NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000140 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000141 return false;
142}
143
Chris Lattner8ace2cd2006-10-20 22:39:59 +0000144/// isIdenticalTo - Return true if this operand is identical to the specified
145/// operand.
146bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
147 if (getType() != Other.getType()) return false;
148
149 switch (getType()) {
150 default: assert(0 && "Unrecognized operand type");
151 case MachineOperand::MO_Register:
152 return getReg() == Other.getReg() && isDef() == Other.isDef();
153 case MachineOperand::MO_Immediate:
154 return getImm() == Other.getImm();
155 case MachineOperand::MO_MachineBasicBlock:
156 return getMBB() == Other.getMBB();
157 case MachineOperand::MO_FrameIndex:
158 return getFrameIndex() == Other.getFrameIndex();
159 case MachineOperand::MO_ConstantPoolIndex:
160 return getConstantPoolIndex() == Other.getConstantPoolIndex() &&
161 getOffset() == Other.getOffset();
162 case MachineOperand::MO_JumpTableIndex:
163 return getJumpTableIndex() == Other.getJumpTableIndex();
164 case MachineOperand::MO_GlobalAddress:
165 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
166 case MachineOperand::MO_ExternalSymbol:
Chris Lattner13a04122006-10-25 18:08:14 +0000167 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
Chris Lattner8ace2cd2006-10-20 22:39:59 +0000168 getOffset() == Other.getOffset();
169 }
170}
171
Brian Gaeke21326fc2004-02-13 04:39:32 +0000172void MachineInstr::dump() const {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000173 llvm_cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000174}
175
Chris Lattner2a79a092002-10-30 00:58:19 +0000176static inline void OutputReg(std::ostream &os, unsigned RegNo,
177 const MRegisterInfo *MRI = 0) {
Alkis Evlogimenosddcfd9e2004-02-27 01:52:34 +0000178 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
Chris Lattner8517e1f2004-02-19 16:17:08 +0000179 if (MRI)
Chris Lattner2a79a092002-10-30 00:58:19 +0000180 os << "%" << MRI->get(RegNo).Name;
181 else
Chris Lattner8517e1f2004-02-19 16:17:08 +0000182 os << "%mreg(" << RegNo << ")";
Chris Lattner2a79a092002-10-30 00:58:19 +0000183 } else
Chris Lattner8517e1f2004-02-19 16:17:08 +0000184 os << "%reg" << RegNo;
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000185}
186
Chris Lattner10491642002-10-30 00:48:05 +0000187static void print(const MachineOperand &MO, std::ostream &OS,
Tanya Lattnerb1407622004-06-25 00:13:11 +0000188 const TargetMachine *TM) {
Misha Brukmance22e762004-07-09 14:45:17 +0000189 const MRegisterInfo *MRI = 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000190
Misha Brukmance22e762004-07-09 14:45:17 +0000191 if (TM) MRI = TM->getRegisterInfo();
Tanya Lattnerb1407622004-06-25 00:13:11 +0000192
Chris Lattner10491642002-10-30 00:48:05 +0000193 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000194 case MachineOperand::MO_Register:
Chris Lattner4efeab22006-05-04 01:26:39 +0000195 OutputReg(OS, MO.getReg(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000196 break;
Chris Lattner63b3d712006-05-04 17:21:20 +0000197 case MachineOperand::MO_Immediate:
Evan Cheng00aff7d2006-05-26 08:00:14 +0000198 OS << MO.getImmedValue();
Chris Lattner10491642002-10-30 00:48:05 +0000199 break;
Chris Lattner2109f502002-12-15 20:35:25 +0000200 case MachineOperand::MO_MachineBasicBlock:
Brian Gaeke988b7ba2004-06-17 22:26:53 +0000201 OS << "mbb<"
Chris Lattner2109f502002-12-15 20:35:25 +0000202 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
Brian Gaeke988b7ba2004-06-17 22:26:53 +0000203 << "," << (void*)MO.getMachineBasicBlock() << ">";
Chris Lattner2109f502002-12-15 20:35:25 +0000204 break;
Chris Lattner10cb79b2002-12-28 20:37:37 +0000205 case MachineOperand::MO_FrameIndex:
206 OS << "<fi#" << MO.getFrameIndex() << ">";
207 break;
Chris Lattner8d95ef42003-01-13 00:23:24 +0000208 case MachineOperand::MO_ConstantPoolIndex:
209 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
210 break;
Nate Begeman37efe672006-04-22 18:53:45 +0000211 case MachineOperand::MO_JumpTableIndex:
212 OS << "<jt#" << MO.getJumpTableIndex() << ">";
213 break;
Chris Lattner8d95ef42003-01-13 00:23:24 +0000214 case MachineOperand::MO_GlobalAddress:
Chris Lattnerca4f6eb2004-10-15 04:38:41 +0000215 OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
216 if (MO.getOffset()) OS << "+" << MO.getOffset();
217 OS << ">";
Chris Lattner8d95ef42003-01-13 00:23:24 +0000218 break;
219 case MachineOperand::MO_ExternalSymbol:
Chris Lattnerca4f6eb2004-10-15 04:38:41 +0000220 OS << "<es:" << MO.getSymbolName();
221 if (MO.getOffset()) OS << "+" << MO.getOffset();
222 OS << ">";
Chris Lattner8d95ef42003-01-13 00:23:24 +0000223 break;
Chris Lattner10491642002-10-30 00:48:05 +0000224 default:
225 assert(0 && "Unrecognized operand type");
226 }
Chris Lattner10491642002-10-30 00:48:05 +0000227}
228
Tanya Lattnerb1407622004-06-25 00:13:11 +0000229void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
Chris Lattner6a592272002-10-30 01:55:38 +0000230 unsigned StartOp = 0;
Chris Lattner10491642002-10-30 00:48:05 +0000231
Chris Lattner6a592272002-10-30 01:55:38 +0000232 // Specialize printing if op#0 is definition
Chris Lattnerd8f44e02006-09-05 20:19:27 +0000233 if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
Chris Lattner0742b592004-02-23 18:38:20 +0000234 ::print(getOperand(0), OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +0000235 OS << " = ";
236 ++StartOp; // Don't print this operand again!
237 }
Tanya Lattnerb1407622004-06-25 00:13:11 +0000238
Evan Cheng67f660c2006-11-30 07:08:44 +0000239 if (TID)
240 OS << TID->Name;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000241
Chris Lattner6a592272002-10-30 01:55:38 +0000242 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000243 const MachineOperand& mop = getOperand(i);
Chris Lattner6a592272002-10-30 01:55:38 +0000244 if (i != StartOp)
245 OS << ",";
246 OS << " ";
Chris Lattner0742b592004-02-23 18:38:20 +0000247 ::print(mop, OS, TM);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000248
Evan Cheng438f7bc2006-11-10 08:43:01 +0000249 if (mop.isReg()) {
Evan Chengd7de4962006-11-13 23:34:06 +0000250 if (mop.isDef() || mop.isKill() || mop.isDead() || mop.isImplicit()) {
251 OS << "<";
252 bool NeedComma = false;
253 if (mop.isImplicit()) {
254 OS << (mop.isDef() ? "imp-def" : "imp-use");
255 NeedComma = true;
256 } else if (mop.isDef()) {
257 OS << "def";
258 NeedComma = true;
259 }
260 if (mop.isKill() || mop.isDead()) {
261 if (NeedComma)
262 OS << ",";
263 if (mop.isKill())
264 OS << "kill";
265 if (mop.isDead())
266 OS << "dead";
267 }
268 OS << ">";
269 }
Evan Cheng438f7bc2006-11-10 08:43:01 +0000270 }
Chris Lattner10491642002-10-30 00:48:05 +0000271 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000272
Chris Lattner10491642002-10-30 00:48:05 +0000273 OS << "\n";
274}
275
Chris Lattner34fb2ca2006-05-04 00:49:59 +0000276std::ostream &llvm::operator<<(std::ostream &os, const MachineInstr &MI) {
Chris Lattner8517e1f2004-02-19 16:17:08 +0000277 // If the instruction is embedded into a basic block, we can find the target
278 // info for the instruction.
279 if (const MachineBasicBlock *MBB = MI.getParent()) {
280 const MachineFunction *MF = MBB->getParent();
Misha Brukmance22e762004-07-09 14:45:17 +0000281 if (MF)
Tanya Lattnerb1407622004-06-25 00:13:11 +0000282 MI.print(os, &MF->getTarget());
283 else
284 MI.print(os, 0);
Chris Lattner8517e1f2004-02-19 16:17:08 +0000285 return os;
286 }
287
288 // Otherwise, print it out in the "raw" format without symbolic register names
289 // and such.
Evan Cheng67f660c2006-11-30 07:08:44 +0000290 os << MI.getInstrDescriptor()->Name;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000291
Misha Brukmance22e762004-07-09 14:45:17 +0000292 for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
Chris Lattner8d95ef42003-01-13 00:23:24 +0000293 os << "\t" << MI.getOperand(i);
Chris Lattnerd8f44e02006-09-05 20:19:27 +0000294 if (MI.getOperand(i).isReg() && MI.getOperand(i).isDef())
295 os << "<d>";
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000296 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000297
Chris Lattner697954c2002-01-20 22:54:45 +0000298 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000299}
300
Chris Lattner34fb2ca2006-05-04 00:49:59 +0000301std::ostream &llvm::operator<<(std::ostream &OS, const MachineOperand &MO) {
Misha Brukmance22e762004-07-09 14:45:17 +0000302 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000303 case MachineOperand::MO_Register:
Chris Lattner4efeab22006-05-04 01:26:39 +0000304 OutputReg(OS, MO.getReg());
Misha Brukmance22e762004-07-09 14:45:17 +0000305 break;
Chris Lattner63b3d712006-05-04 17:21:20 +0000306 case MachineOperand::MO_Immediate:
Misha Brukmance22e762004-07-09 14:45:17 +0000307 OS << (long)MO.getImmedValue();
308 break;
Misha Brukmance22e762004-07-09 14:45:17 +0000309 case MachineOperand::MO_MachineBasicBlock:
310 OS << "<mbb:"
311 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
312 << "@" << (void*)MO.getMachineBasicBlock() << ">";
313 break;
314 case MachineOperand::MO_FrameIndex:
315 OS << "<fi#" << MO.getFrameIndex() << ">";
316 break;
317 case MachineOperand::MO_ConstantPoolIndex:
318 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
319 break;
Nate Begeman37efe672006-04-22 18:53:45 +0000320 case MachineOperand::MO_JumpTableIndex:
321 OS << "<jt#" << MO.getJumpTableIndex() << ">";
322 break;
Misha Brukmance22e762004-07-09 14:45:17 +0000323 case MachineOperand::MO_GlobalAddress:
324 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
325 break;
326 case MachineOperand::MO_ExternalSymbol:
327 OS << "<es:" << MO.getSymbolName() << ">";
328 break;
329 default:
330 assert(0 && "Unrecognized operand type");
331 break;
332 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000333
Chris Lattner10cb79b2002-12-28 20:37:37 +0000334 return OS;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000335}