blob: 716b40fbf1c1dcdadf5090636db37d8368fd9d2f [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"
Chris Lattner0742b592004-02-23 18:38:20 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Evan Chengc0f64ff2006-11-27 23:37:22 +000023/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
Evan Cheng67f660c2006-11-30 07:08:44 +000024/// TID NULL and no operands.
Evan Chengc0f64ff2006-11-27 23:37:22 +000025MachineInstr::MachineInstr()
Evan Cheng67f660c2006-11-30 07:08:44 +000026 : TID(0), NumImplicitOps(0), parent(0) {
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000027 // Make sure that we get added to a machine basicblock
28 LeakDetector::addGarbageObject(this);
Chris Lattner72791222002-10-28 20:59:49 +000029}
30
Evan Cheng67f660c2006-11-30 07:08:44 +000031void MachineInstr::addImplicitDefUseOperands() {
32 if (TID->ImplicitDefs)
33 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) {
Evan Chengd7de4962006-11-13 23:34:06 +000034 MachineOperand Op;
35 Op.opType = MachineOperand::MO_Register;
36 Op.IsDef = true;
37 Op.IsImp = true;
38 Op.IsKill = false;
39 Op.IsDead = false;
40 Op.contents.RegNo = *ImpDefs;
41 Op.offset = 0;
42 Operands.push_back(Op);
43 }
Evan Cheng67f660c2006-11-30 07:08:44 +000044 if (TID->ImplicitUses)
45 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) {
Evan Chengd7de4962006-11-13 23:34:06 +000046 MachineOperand Op;
47 Op.opType = MachineOperand::MO_Register;
48 Op.IsDef = false;
49 Op.IsImp = true;
50 Op.IsKill = false;
51 Op.IsDead = false;
52 Op.contents.RegNo = *ImpUses;
53 Op.offset = 0;
54 Operands.push_back(Op);
55 }
56}
57
58/// MachineInstr ctor - This constructor create a MachineInstr and add the
Evan Chengc0f64ff2006-11-27 23:37:22 +000059/// implicit operands. It reserves space for number of operands specified by
60/// TargetInstrDescriptor or the numOperands if it is not zero. (for
61/// instructions with variable number of operands).
Evan Cheng67f660c2006-11-30 07:08:44 +000062MachineInstr::MachineInstr(const TargetInstrDescriptor &tid)
63 : TID(&tid), NumImplicitOps(0), parent(0) {
64 if (TID->ImplicitDefs)
65 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +000066 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +000067 if (TID->ImplicitUses)
68 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +000069 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +000070 Operands.reserve(NumImplicitOps + TID->numOperands);
71 addImplicitDefUseOperands();
Evan Chengd7de4962006-11-13 23:34:06 +000072 // Make sure that we get added to a machine basicblock
73 LeakDetector::addGarbageObject(this);
74}
75
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000076/// MachineInstr ctor - Work exactly the same as the ctor above, except that the
77/// MachineInstr is created and added to the end of the specified basic block.
78///
Evan Chengc0f64ff2006-11-27 23:37:22 +000079MachineInstr::MachineInstr(MachineBasicBlock *MBB,
Evan Cheng67f660c2006-11-30 07:08:44 +000080 const TargetInstrDescriptor &tid)
81 : TID(&tid), NumImplicitOps(0), parent(0) {
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000082 assert(MBB && "Cannot use inserting ctor with null basic block!");
Evan Cheng67f660c2006-11-30 07:08:44 +000083 if (TID->ImplicitDefs)
84 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +000085 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +000086 if (TID->ImplicitUses)
87 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +000088 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +000089 Operands.reserve(NumImplicitOps + TID->numOperands);
90 addImplicitDefUseOperands();
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000091 // Make sure that we get added to a machine basicblock
92 LeakDetector::addGarbageObject(this);
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000093 MBB->push_back(this); // Add instruction to end of basic block!
94}
95
Misha Brukmance22e762004-07-09 14:45:17 +000096/// MachineInstr ctor - Copies MachineInstr arg exactly
97///
Tanya Lattner466b5342004-05-23 19:35:12 +000098MachineInstr::MachineInstr(const MachineInstr &MI) {
Evan Cheng67f660c2006-11-30 07:08:44 +000099 TID = MI.getInstrDescriptor();
Evan Cheng6b2c05f2006-11-15 20:54:29 +0000100 NumImplicitOps = MI.NumImplicitOps;
Chris Lattner943b5e12006-05-04 19:14:44 +0000101 Operands.reserve(MI.getNumOperands());
Tanya Lattnerb5159ed2004-05-23 20:58:02 +0000102
Misha Brukmance22e762004-07-09 14:45:17 +0000103 // Add operands
Chris Lattner943b5e12006-05-04 19:14:44 +0000104 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
105 Operands.push_back(MI.getOperand(i));
Tanya Lattner0c63e032004-05-24 03:14:18 +0000106
Misha Brukmance22e762004-07-09 14:45:17 +0000107 // Set parent, next, and prev to null
Tanya Lattner0c63e032004-05-24 03:14:18 +0000108 parent = 0;
109 prev = 0;
110 next = 0;
Tanya Lattner466b5342004-05-23 19:35:12 +0000111}
112
113
Misha Brukmance22e762004-07-09 14:45:17 +0000114MachineInstr::~MachineInstr() {
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000115 LeakDetector::removeGarbageObject(this);
116}
117
Evan Cheng67f660c2006-11-30 07:08:44 +0000118/// getOpcode - Returns the opcode of this MachineInstr.
119///
120const int MachineInstr::getOpcode() const {
121 return TID->Opcode;
122}
123
Chris Lattner48d7c062006-04-17 21:35:41 +0000124/// removeFromParent - This method unlinks 'this' from the containing basic
125/// block, and returns it, but does not delete it.
126MachineInstr *MachineInstr::removeFromParent() {
127 assert(getParent() && "Not embedded in a basic block!");
128 getParent()->remove(this);
129 return this;
130}
131
132
Brian Gaeke21326fc2004-02-13 04:39:32 +0000133/// OperandComplete - Return true if it's illegal to add a new operand
134///
Chris Lattner2a90ba62004-02-12 16:09:53 +0000135bool MachineInstr::OperandsComplete() const {
Evan Cheng67f660c2006-11-30 07:08:44 +0000136 unsigned short NumOperands = TID->numOperands;
137 if ((TID->Flags & M_VARIABLE_OPS) == 0 &&
Evan Cheng8bcb0422006-11-28 02:25:34 +0000138 getNumOperands()-NumImplicitOps >= NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000139 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000140 return false;
141}
142
Chris Lattner8ace2cd2006-10-20 22:39:59 +0000143/// isIdenticalTo - Return true if this operand is identical to the specified
144/// operand.
145bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
146 if (getType() != Other.getType()) return false;
147
148 switch (getType()) {
149 default: assert(0 && "Unrecognized operand type");
150 case MachineOperand::MO_Register:
151 return getReg() == Other.getReg() && isDef() == Other.isDef();
152 case MachineOperand::MO_Immediate:
153 return getImm() == Other.getImm();
154 case MachineOperand::MO_MachineBasicBlock:
155 return getMBB() == Other.getMBB();
156 case MachineOperand::MO_FrameIndex:
157 return getFrameIndex() == Other.getFrameIndex();
158 case MachineOperand::MO_ConstantPoolIndex:
159 return getConstantPoolIndex() == Other.getConstantPoolIndex() &&
160 getOffset() == Other.getOffset();
161 case MachineOperand::MO_JumpTableIndex:
162 return getJumpTableIndex() == Other.getJumpTableIndex();
163 case MachineOperand::MO_GlobalAddress:
164 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
165 case MachineOperand::MO_ExternalSymbol:
Chris Lattner13a04122006-10-25 18:08:14 +0000166 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
Chris Lattner8ace2cd2006-10-20 22:39:59 +0000167 getOffset() == Other.getOffset();
168 }
169}
170
Evan Cheng576d1232006-12-06 08:27:42 +0000171/// findRegisterUseOperand() - Returns the MachineOperand that is a use of
172/// the specific register or NULL if it is not found.
173MachineOperand *MachineInstr::findRegisterUseOperand(unsigned Reg) {
174 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
175 MachineOperand &MO = getOperand(i);
176 if (MO.isReg() && MO.isUse() && MO.getReg() == Reg)
177 return &MO;
178 }
179 return NULL;
180}
181
182/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
183///
184void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
185 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
186 const MachineOperand &MO = MI->getOperand(i);
187 if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
188 continue;
189 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
190 MachineOperand &MOp = getOperand(j);
191 if (!MOp.isIdenticalTo(MO))
192 continue;
193 if (MO.isKill())
194 MOp.setIsKill();
195 else
196 MOp.setIsDead();
197 break;
198 }
199 }
200}
201
Brian Gaeke21326fc2004-02-13 04:39:32 +0000202void MachineInstr::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000203 cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000204}
205
Chris Lattner2a79a092002-10-30 00:58:19 +0000206static inline void OutputReg(std::ostream &os, unsigned RegNo,
207 const MRegisterInfo *MRI = 0) {
Alkis Evlogimenosddcfd9e2004-02-27 01:52:34 +0000208 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
Chris Lattner8517e1f2004-02-19 16:17:08 +0000209 if (MRI)
Chris Lattner2a79a092002-10-30 00:58:19 +0000210 os << "%" << MRI->get(RegNo).Name;
211 else
Chris Lattner8517e1f2004-02-19 16:17:08 +0000212 os << "%mreg(" << RegNo << ")";
Chris Lattner2a79a092002-10-30 00:58:19 +0000213 } else
Chris Lattner8517e1f2004-02-19 16:17:08 +0000214 os << "%reg" << RegNo;
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000215}
216
Chris Lattner10491642002-10-30 00:48:05 +0000217static void print(const MachineOperand &MO, std::ostream &OS,
Tanya Lattnerb1407622004-06-25 00:13:11 +0000218 const TargetMachine *TM) {
Misha Brukmance22e762004-07-09 14:45:17 +0000219 const MRegisterInfo *MRI = 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000220
Misha Brukmance22e762004-07-09 14:45:17 +0000221 if (TM) MRI = TM->getRegisterInfo();
Tanya Lattnerb1407622004-06-25 00:13:11 +0000222
Chris Lattner10491642002-10-30 00:48:05 +0000223 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000224 case MachineOperand::MO_Register:
Chris Lattner4efeab22006-05-04 01:26:39 +0000225 OutputReg(OS, MO.getReg(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000226 break;
Chris Lattner63b3d712006-05-04 17:21:20 +0000227 case MachineOperand::MO_Immediate:
Evan Cheng00aff7d2006-05-26 08:00:14 +0000228 OS << MO.getImmedValue();
Chris Lattner10491642002-10-30 00:48:05 +0000229 break;
Chris Lattner2109f502002-12-15 20:35:25 +0000230 case MachineOperand::MO_MachineBasicBlock:
Brian Gaeke988b7ba2004-06-17 22:26:53 +0000231 OS << "mbb<"
Chris Lattner2109f502002-12-15 20:35:25 +0000232 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
Brian Gaeke988b7ba2004-06-17 22:26:53 +0000233 << "," << (void*)MO.getMachineBasicBlock() << ">";
Chris Lattner2109f502002-12-15 20:35:25 +0000234 break;
Chris Lattner10cb79b2002-12-28 20:37:37 +0000235 case MachineOperand::MO_FrameIndex:
236 OS << "<fi#" << MO.getFrameIndex() << ">";
237 break;
Chris Lattner8d95ef42003-01-13 00:23:24 +0000238 case MachineOperand::MO_ConstantPoolIndex:
239 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
240 break;
Nate Begeman37efe672006-04-22 18:53:45 +0000241 case MachineOperand::MO_JumpTableIndex:
242 OS << "<jt#" << MO.getJumpTableIndex() << ">";
243 break;
Chris Lattner8d95ef42003-01-13 00:23:24 +0000244 case MachineOperand::MO_GlobalAddress:
Chris Lattnerca4f6eb2004-10-15 04:38:41 +0000245 OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
246 if (MO.getOffset()) OS << "+" << MO.getOffset();
247 OS << ">";
Chris Lattner8d95ef42003-01-13 00:23:24 +0000248 break;
249 case MachineOperand::MO_ExternalSymbol:
Chris Lattnerca4f6eb2004-10-15 04:38:41 +0000250 OS << "<es:" << MO.getSymbolName();
251 if (MO.getOffset()) OS << "+" << MO.getOffset();
252 OS << ">";
Chris Lattner8d95ef42003-01-13 00:23:24 +0000253 break;
Chris Lattner10491642002-10-30 00:48:05 +0000254 default:
255 assert(0 && "Unrecognized operand type");
256 }
Chris Lattner10491642002-10-30 00:48:05 +0000257}
258
Tanya Lattnerb1407622004-06-25 00:13:11 +0000259void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
Chris Lattner6a592272002-10-30 01:55:38 +0000260 unsigned StartOp = 0;
Chris Lattner10491642002-10-30 00:48:05 +0000261
Chris Lattner6a592272002-10-30 01:55:38 +0000262 // Specialize printing if op#0 is definition
Chris Lattnerd8f44e02006-09-05 20:19:27 +0000263 if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
Chris Lattner0742b592004-02-23 18:38:20 +0000264 ::print(getOperand(0), OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +0000265 OS << " = ";
266 ++StartOp; // Don't print this operand again!
267 }
Tanya Lattnerb1407622004-06-25 00:13:11 +0000268
Evan Cheng67f660c2006-11-30 07:08:44 +0000269 if (TID)
270 OS << TID->Name;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000271
Chris Lattner6a592272002-10-30 01:55:38 +0000272 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000273 const MachineOperand& mop = getOperand(i);
Chris Lattner6a592272002-10-30 01:55:38 +0000274 if (i != StartOp)
275 OS << ",";
276 OS << " ";
Chris Lattner0742b592004-02-23 18:38:20 +0000277 ::print(mop, OS, TM);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000278
Evan Cheng438f7bc2006-11-10 08:43:01 +0000279 if (mop.isReg()) {
Evan Chengd7de4962006-11-13 23:34:06 +0000280 if (mop.isDef() || mop.isKill() || mop.isDead() || mop.isImplicit()) {
281 OS << "<";
282 bool NeedComma = false;
283 if (mop.isImplicit()) {
284 OS << (mop.isDef() ? "imp-def" : "imp-use");
285 NeedComma = true;
286 } else if (mop.isDef()) {
287 OS << "def";
288 NeedComma = true;
289 }
290 if (mop.isKill() || mop.isDead()) {
291 if (NeedComma)
292 OS << ",";
293 if (mop.isKill())
294 OS << "kill";
295 if (mop.isDead())
296 OS << "dead";
297 }
298 OS << ">";
299 }
Evan Cheng438f7bc2006-11-10 08:43:01 +0000300 }
Chris Lattner10491642002-10-30 00:48:05 +0000301 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000302
Chris Lattner10491642002-10-30 00:48:05 +0000303 OS << "\n";
304}
305
Chris Lattner34fb2ca2006-05-04 00:49:59 +0000306std::ostream &llvm::operator<<(std::ostream &os, const MachineInstr &MI) {
Chris Lattner8517e1f2004-02-19 16:17:08 +0000307 // If the instruction is embedded into a basic block, we can find the target
308 // info for the instruction.
309 if (const MachineBasicBlock *MBB = MI.getParent()) {
310 const MachineFunction *MF = MBB->getParent();
Misha Brukmance22e762004-07-09 14:45:17 +0000311 if (MF)
Tanya Lattnerb1407622004-06-25 00:13:11 +0000312 MI.print(os, &MF->getTarget());
313 else
314 MI.print(os, 0);
Chris Lattner8517e1f2004-02-19 16:17:08 +0000315 return os;
316 }
317
318 // Otherwise, print it out in the "raw" format without symbolic register names
319 // and such.
Evan Cheng67f660c2006-11-30 07:08:44 +0000320 os << MI.getInstrDescriptor()->Name;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000321
Misha Brukmance22e762004-07-09 14:45:17 +0000322 for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
Chris Lattner8d95ef42003-01-13 00:23:24 +0000323 os << "\t" << MI.getOperand(i);
Chris Lattnerd8f44e02006-09-05 20:19:27 +0000324 if (MI.getOperand(i).isReg() && MI.getOperand(i).isDef())
325 os << "<d>";
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000326 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000327
Chris Lattner697954c2002-01-20 22:54:45 +0000328 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000329}
330
Chris Lattner34fb2ca2006-05-04 00:49:59 +0000331std::ostream &llvm::operator<<(std::ostream &OS, const MachineOperand &MO) {
Misha Brukmance22e762004-07-09 14:45:17 +0000332 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000333 case MachineOperand::MO_Register:
Chris Lattner4efeab22006-05-04 01:26:39 +0000334 OutputReg(OS, MO.getReg());
Misha Brukmance22e762004-07-09 14:45:17 +0000335 break;
Chris Lattner63b3d712006-05-04 17:21:20 +0000336 case MachineOperand::MO_Immediate:
Misha Brukmance22e762004-07-09 14:45:17 +0000337 OS << (long)MO.getImmedValue();
338 break;
Misha Brukmance22e762004-07-09 14:45:17 +0000339 case MachineOperand::MO_MachineBasicBlock:
340 OS << "<mbb:"
341 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
342 << "@" << (void*)MO.getMachineBasicBlock() << ">";
343 break;
344 case MachineOperand::MO_FrameIndex:
345 OS << "<fi#" << MO.getFrameIndex() << ">";
346 break;
347 case MachineOperand::MO_ConstantPoolIndex:
348 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
349 break;
Nate Begeman37efe672006-04-22 18:53:45 +0000350 case MachineOperand::MO_JumpTableIndex:
351 OS << "<jt#" << MO.getJumpTableIndex() << ">";
352 break;
Misha Brukmance22e762004-07-09 14:45:17 +0000353 case MachineOperand::MO_GlobalAddress:
354 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
355 break;
356 case MachineOperand::MO_ExternalSymbol:
357 OS << "<es:" << MO.getSymbolName() << ">";
358 break;
359 default:
360 assert(0 && "Unrecognized operand type");
361 break;
362 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000363
Chris Lattner10cb79b2002-12-28 20:37:37 +0000364 return OS;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000365}