blob: e1e44fd47e09e5abf8abb2a40e682ff7e9968294 [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- MachineInstr.cpp --------------------------------------------------===//
Vikram S. Adve70bc4b52001-07-21 12:41:50 +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.
7//
8//===----------------------------------------------------------------------===//
Brian Gaeke21326fc2004-02-13 04:39:32 +00009//
10// Methods common to all machine instructions.
11//
12// FIXME: Now that MachineInstrs have parent pointers, they should always
13// print themselves using their MachineFunction's TargetMachine.
14//
Chris Lattner035dfbe2002-08-09 20:08:06 +000015//===----------------------------------------------------------------------===//
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000016
Chris Lattner822b4fb2001-09-07 17:18:30 +000017#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner8517e1f2004-02-19 16:17:08 +000018#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner3801f6d2002-02-03 07:46:01 +000019#include "llvm/Value.h"
Chris Lattner10491642002-10-30 00:48:05 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000021#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner2a79a092002-10-30 00:58:19 +000022#include "llvm/Target/MRegisterInfo.h"
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000023#include "Support/LeakDetector.h"
Chris Lattner0742b592004-02-23 18:38:20 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattnerf1757c42002-10-29 17:40:30 +000026// Global variable holding an array of descriptors for machine instructions.
27// The actual object needs to be created separately for each target machine.
Chris Lattner3501fea2003-01-14 22:00:31 +000028// This variable is initialized and reset by class TargetInstrInfo.
Chris Lattnerf1757c42002-10-29 17:40:30 +000029//
30// FIXME: This should be a property of the target so that more than one target
31// at a time can be active...
32//
Chris Lattner11d1f212004-02-23 18:40:08 +000033namespace llvm {
Chris Lattner0742b592004-02-23 18:38:20 +000034 extern const TargetInstrDescriptor *TargetInstrDescriptors;
35}
Ruchira Sasanka69917e22001-10-18 22:40:02 +000036
Vikram S. Adve1885da42001-07-31 21:49:28 +000037// Constructor for instructions with variable #operands
Alkis Evlogimenosab8672c2004-02-12 18:49:07 +000038MachineInstr::MachineInstr(short opcode, unsigned numOperands)
39 : Opcode(opcode),
40 numImplicitRefs(0),
41 operands(numOperands, MachineOperand()),
42 parent(0) {
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000043 // Make sure that we get added to a machine basicblock
44 LeakDetector::addGarbageObject(this);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000045}
46
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000047/// MachineInstr ctor - This constructor only does a _reserve_ of the operands,
48/// not a resize for them. It is expected that if you use this that you call
49/// add* methods below to fill up the operands, instead of the Set methods.
50/// Eventually, the "resizing" ctors will be phased out.
51///
Brian Gaeke21326fc2004-02-13 04:39:32 +000052MachineInstr::MachineInstr(short opcode, unsigned numOperands, bool XX, bool YY)
53 : Opcode(opcode), numImplicitRefs(0), parent(0) {
Chris Lattner72791222002-10-28 20:59:49 +000054 operands.reserve(numOperands);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000055 // Make sure that we get added to a machine basicblock
56 LeakDetector::addGarbageObject(this);
Chris Lattner72791222002-10-28 20:59:49 +000057}
58
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000059/// MachineInstr ctor - Work exactly the same as the ctor above, except that the
60/// MachineInstr is created and added to the end of the specified basic block.
61///
Alkis Evlogimenosab8672c2004-02-12 18:49:07 +000062MachineInstr::MachineInstr(MachineBasicBlock *MBB, short opcode,
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000063 unsigned numOperands)
Brian Gaeke21326fc2004-02-13 04:39:32 +000064 : Opcode(opcode), numImplicitRefs(0), parent(0) {
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000065 assert(MBB && "Cannot use inserting ctor with null basic block!");
66 operands.reserve(numOperands);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000067 // Make sure that we get added to a machine basicblock
68 LeakDetector::addGarbageObject(this);
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000069 MBB->push_back(this); // Add instruction to end of basic block!
70}
71
Tanya Lattner466b5342004-05-23 19:35:12 +000072///MachineInstr ctor - Copies MachineInstr arg exactly
73MachineInstr::MachineInstr(const MachineInstr &MI) {
74 Opcode = MI.getOpcode();
75 numImplicitRefs = MI.getNumImplicitRefs();
76
77 //Add operands
78 for(unsigned i=0; i < MI.getNumOperands(); ++i)
79 operands.push_back(MachineOperand(MI.getOperand(i)));
80}
81
82
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000083MachineInstr::~MachineInstr()
84{
85 LeakDetector::removeGarbageObject(this);
86}
87
Tanya Lattner466b5342004-05-23 19:35:12 +000088///clone - Create a copy of 'this' instruction that is identical in
89///all ways except the following: The instruction has no parent The
90///instruction has no name
91MachineInstr* MachineInstr::clone() {
92 MachineInstr* newInst = new MachineInstr(*this);
93}
94
Brian Gaeke21326fc2004-02-13 04:39:32 +000095/// OperandComplete - Return true if it's illegal to add a new operand
96///
Chris Lattner2a90ba62004-02-12 16:09:53 +000097bool MachineInstr::OperandsComplete() const {
98 int NumOperands = TargetInstrDescriptors[Opcode].numOperands;
Vikram S. Advea2bae302002-10-29 19:41:18 +000099 if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000100 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000101 return false;
102}
103
Brian Gaeke21326fc2004-02-13 04:39:32 +0000104/// replace - Support for replacing opcode and operands of a MachineInstr in
105/// place. This only resets the size of the operand vector and initializes it.
106/// The new operands must be set explicitly later.
107///
Alkis Evlogimenosab8672c2004-02-12 18:49:07 +0000108void MachineInstr::replace(short opcode, unsigned numOperands) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000109 assert(getNumImplicitRefs() == 0 &&
110 "This is probably broken because implicit refs are going to be lost.");
Chris Lattner2a90ba62004-02-12 16:09:53 +0000111 Opcode = opcode;
Vikram S. Advee8b57ef2002-09-20 00:47:49 +0000112 operands.clear();
Chris Lattner413746e2002-10-28 20:48:39 +0000113 operands.resize(numOperands, MachineOperand());
Tanya Lattner466b5342004-05-23 19:35:12 +0000114
Vikram S. Advee8b57ef2002-09-20 00:47:49 +0000115}
116
Chris Lattnera2dd7452003-08-05 16:58:46 +0000117void MachineInstr::SetMachineOperandVal(unsigned i,
118 MachineOperand::MachineOperandType opTy,
119 Value* V) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000120 assert(i < operands.size()); // may be explicit or implicit op
Chris Lattnera2dd7452003-08-05 16:58:46 +0000121 operands[i].opType = opTy;
Brian Gaekec5483952004-03-03 19:07:27 +0000122 operands[i].contents.value = V;
Chris Lattner572f5c82002-10-28 04:24:49 +0000123 operands[i].regNum = -1;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000124}
125
126void
Chris Lattner572f5c82002-10-28 04:24:49 +0000127MachineInstr::SetMachineOperandConst(unsigned i,
Brian Gaeke21326fc2004-02-13 04:39:32 +0000128 MachineOperand::MachineOperandType opTy,
Chris Lattner561c0102004-02-29 05:07:02 +0000129 int intValue) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000130 assert(i < getNumOperands()); // must be explicit op
Chris Lattner2a90ba62004-02-12 16:09:53 +0000131 assert(TargetInstrDescriptors[Opcode].resultPos != (int) i &&
Vikram S. Advec356e562002-03-18 03:35:24 +0000132 "immed. constant cannot be defined");
Chris Lattner572f5c82002-10-28 04:24:49 +0000133
Brian Gaeke21326fc2004-02-13 04:39:32 +0000134 operands[i].opType = opTy;
Brian Gaekec5483952004-03-03 19:07:27 +0000135 operands[i].contents.value = NULL;
136 operands[i].contents.immedVal = intValue;
Chris Lattner572f5c82002-10-28 04:24:49 +0000137 operands[i].regNum = -1;
138 operands[i].flags = 0;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000139}
140
Chris Lattnera2dd7452003-08-05 16:58:46 +0000141void MachineInstr::SetMachineOperandReg(unsigned i, int regNum) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000142 assert(i < getNumOperands()); // must be explicit op
Chris Lattner572f5c82002-10-28 04:24:49 +0000143
Chris Lattner2f305982002-10-28 19:46:59 +0000144 operands[i].opType = MachineOperand::MO_MachineRegister;
Brian Gaekec5483952004-03-03 19:07:27 +0000145 operands[i].contents.value = NULL;
Chris Lattner572f5c82002-10-28 04:24:49 +0000146 operands[i].regNum = regNum;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000147}
148
Brian Gaeke21326fc2004-02-13 04:39:32 +0000149// Used only by the SPARC back-end.
Chris Lattner2a90ba62004-02-12 16:09:53 +0000150void MachineInstr::SetRegForOperand(unsigned i, int regNum) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000151 assert(i < getNumOperands()); // must be explicit op
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000152 operands[i].setRegForValue(regNum);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000153}
154
Brian Gaeke21326fc2004-02-13 04:39:32 +0000155// Used only by the SPARC back-end.
Chris Lattner2a90ba62004-02-12 16:09:53 +0000156void MachineInstr::SetRegForImplicitRef(unsigned i, int regNum) {
Vikram S. Adve34977822003-05-31 07:39:06 +0000157 getImplicitOp(i).setRegForValue(regNum);
Vikram S. Adve34977822003-05-31 07:39:06 +0000158}
159
Brian Gaeke21326fc2004-02-13 04:39:32 +0000160/// substituteValue - Substitute all occurrences of Value* oldVal with newVal
161/// in all operands and all implicit refs. If defsOnly == true, substitute defs
162/// only.
163///
164/// FIXME: Fold this into its single caller, at SparcInstrSelection.cpp:2865,
165/// or make it a static function in that file.
166///
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000167unsigned
Vikram S. Adve627eb312003-07-10 19:45:07 +0000168MachineInstr::substituteValue(const Value* oldVal, Value* newVal,
169 bool defsOnly, bool notDefsAndUses,
170 bool& someArgsWereIgnored)
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000171{
Vikram S. Adve2010f7b2003-08-07 15:01:48 +0000172 assert((!defsOnly || !notDefsAndUses) &&
173 "notDefsAndUses is irrelevant if defsOnly == true.");
Vikram S. Adve627eb312003-07-10 19:45:07 +0000174
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000175 unsigned numSubst = 0;
176
Misha Brukman6eba07a2003-09-17 21:34:23 +0000177 // Substitute operands
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000178 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
179 if (*O == oldVal)
Vikram S. Adve627eb312003-07-10 19:45:07 +0000180 if (!defsOnly ||
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000181 notDefsAndUses && (O.isDef() && !O.isUse()) ||
182 !notDefsAndUses && O.isDef())
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000183 {
Brian Gaekec5483952004-03-03 19:07:27 +0000184 O.getMachineOperand().contents.value = newVal;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000185 ++numSubst;
186 }
Vikram S. Adve627eb312003-07-10 19:45:07 +0000187 else
188 someArgsWereIgnored = true;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000189
Misha Brukman6eba07a2003-09-17 21:34:23 +0000190 // Substitute implicit refs
Vikram S. Advea2bae302002-10-29 19:41:18 +0000191 for (unsigned i=0, N=getNumImplicitRefs(); i < N; ++i)
Chris Lattner27a08932002-10-22 23:16:21 +0000192 if (getImplicitRef(i) == oldVal)
Vikram S. Adve627eb312003-07-10 19:45:07 +0000193 if (!defsOnly ||
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000194 notDefsAndUses && (getImplicitOp(i).isDef() && !getImplicitOp(i).isUse()) ||
195 !notDefsAndUses && getImplicitOp(i).isDef())
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000196 {
Brian Gaekec5483952004-03-03 19:07:27 +0000197 getImplicitOp(i).contents.value = newVal;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000198 ++numSubst;
199 }
Vikram S. Adve627eb312003-07-10 19:45:07 +0000200 else
201 someArgsWereIgnored = true;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000202
203 return numSubst;
204}
205
Brian Gaeke21326fc2004-02-13 04:39:32 +0000206void MachineInstr::dump() const {
Chris Lattner925b7712003-08-03 20:24:29 +0000207 std::cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000208}
209
Brian Gaeke21326fc2004-02-13 04:39:32 +0000210static inline std::ostream& OutputValue(std::ostream &os, const Value* val) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000211 os << "(val ";
Vikram S. Adve627eb312003-07-10 19:45:07 +0000212 os << (void*) val; // print address always
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000213 if (val && val->hasName())
Brian Gaeke21326fc2004-02-13 04:39:32 +0000214 os << " " << val->getName(); // print name also, if available
215 os << ")";
Vikram S. Adve627eb312003-07-10 19:45:07 +0000216 return os;
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000217}
218
Chris Lattner2a79a092002-10-30 00:58:19 +0000219static inline void OutputReg(std::ostream &os, unsigned RegNo,
220 const MRegisterInfo *MRI = 0) {
Alkis Evlogimenosddcfd9e2004-02-27 01:52:34 +0000221 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
Chris Lattner8517e1f2004-02-19 16:17:08 +0000222 if (MRI)
Chris Lattner2a79a092002-10-30 00:58:19 +0000223 os << "%" << MRI->get(RegNo).Name;
224 else
Chris Lattner8517e1f2004-02-19 16:17:08 +0000225 os << "%mreg(" << RegNo << ")";
Chris Lattner2a79a092002-10-30 00:58:19 +0000226 } else
Chris Lattner8517e1f2004-02-19 16:17:08 +0000227 os << "%reg" << RegNo;
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000228}
229
Chris Lattner10491642002-10-30 00:48:05 +0000230static void print(const MachineOperand &MO, std::ostream &OS,
231 const TargetMachine &TM) {
Chris Lattner2a79a092002-10-30 00:58:19 +0000232 const MRegisterInfo *MRI = TM.getRegisterInfo();
Chris Lattner10491642002-10-30 00:48:05 +0000233 bool CloseParen = true;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000234 if (MO.isHiBits32())
Chris Lattner10491642002-10-30 00:48:05 +0000235 OS << "%lm(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000236 else if (MO.isLoBits32())
Chris Lattner10491642002-10-30 00:48:05 +0000237 OS << "%lo(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000238 else if (MO.isHiBits64())
Chris Lattner10491642002-10-30 00:48:05 +0000239 OS << "%hh(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000240 else if (MO.isLoBits64())
Chris Lattner10491642002-10-30 00:48:05 +0000241 OS << "%hm(";
242 else
243 CloseParen = false;
244
245 switch (MO.getType()) {
246 case MachineOperand::MO_VirtualRegister:
247 if (MO.getVRegValue()) {
248 OS << "%reg";
249 OutputValue(OS, MO.getVRegValue());
250 if (MO.hasAllocatedReg())
251 OS << "==";
252 }
253 if (MO.hasAllocatedReg())
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000254 OutputReg(OS, MO.getReg(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000255 break;
256 case MachineOperand::MO_CCRegister:
257 OS << "%ccreg";
258 OutputValue(OS, MO.getVRegValue());
259 if (MO.hasAllocatedReg()) {
260 OS << "==";
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000261 OutputReg(OS, MO.getReg(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000262 }
263 break;
264 case MachineOperand::MO_MachineRegister:
Chris Lattner2a79a092002-10-30 00:58:19 +0000265 OutputReg(OS, MO.getMachineRegNum(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000266 break;
267 case MachineOperand::MO_SignExtendedImmed:
268 OS << (long)MO.getImmedValue();
269 break;
270 case MachineOperand::MO_UnextendedImmed:
271 OS << (long)MO.getImmedValue();
272 break;
273 case MachineOperand::MO_PCRelativeDisp: {
274 const Value* opVal = MO.getVRegValue();
275 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
276 OS << "%disp(" << (isLabel? "label " : "addr-of-val ");
277 if (opVal->hasName())
278 OS << opVal->getName();
279 else
280 OS << (const void*) opVal;
281 OS << ")";
282 break;
283 }
Chris Lattner2109f502002-12-15 20:35:25 +0000284 case MachineOperand::MO_MachineBasicBlock:
285 OS << "bb<"
286 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
287 << "," << (void*)MO.getMachineBasicBlock()->getBasicBlock() << ">";
288 break;
Chris Lattner10cb79b2002-12-28 20:37:37 +0000289 case MachineOperand::MO_FrameIndex:
290 OS << "<fi#" << MO.getFrameIndex() << ">";
291 break;
Chris Lattner8d95ef42003-01-13 00:23:24 +0000292 case MachineOperand::MO_ConstantPoolIndex:
293 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
294 break;
295 case MachineOperand::MO_GlobalAddress:
296 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
297 break;
298 case MachineOperand::MO_ExternalSymbol:
299 OS << "<es:" << MO.getSymbolName() << ">";
300 break;
Chris Lattner10491642002-10-30 00:48:05 +0000301 default:
302 assert(0 && "Unrecognized operand type");
303 }
304
305 if (CloseParen)
306 OS << ")";
307}
308
Chris Lattneraf55be12002-11-17 23:22:13 +0000309void MachineInstr::print(std::ostream &OS, const TargetMachine &TM) const {
Chris Lattner6a592272002-10-30 01:55:38 +0000310 unsigned StartOp = 0;
Chris Lattner10491642002-10-30 00:48:05 +0000311
Chris Lattner6a592272002-10-30 01:55:38 +0000312 // Specialize printing if op#0 is definition
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000313 if (getNumOperands() && getOperand(0).isDef() && !getOperand(0).isUse()) {
Chris Lattner0742b592004-02-23 18:38:20 +0000314 ::print(getOperand(0), OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +0000315 OS << " = ";
316 ++StartOp; // Don't print this operand again!
317 }
318 OS << TM.getInstrInfo().getName(getOpcode());
319
320 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000321 const MachineOperand& mop = getOperand(i);
Chris Lattner6a592272002-10-30 01:55:38 +0000322 if (i != StartOp)
323 OS << ",";
324 OS << " ";
Chris Lattner0742b592004-02-23 18:38:20 +0000325 ::print(mop, OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +0000326
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000327 if (mop.isDef())
328 if (mop.isUse())
329 OS << "<def&use>";
330 else
331 OS << "<def>";
Chris Lattner10491642002-10-30 00:48:05 +0000332 }
Chris Lattner6a592272002-10-30 01:55:38 +0000333
Misha Brukman6eba07a2003-09-17 21:34:23 +0000334 // code for printing implicit references
Chris Lattner10491642002-10-30 00:48:05 +0000335 if (getNumImplicitRefs()) {
336 OS << "\tImplicitRefs: ";
337 for(unsigned i = 0, e = getNumImplicitRefs(); i != e; ++i) {
338 OS << "\t";
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000339 OutputValue(OS, getImplicitRef(i));
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000340 if (getImplicitOp(i).isDef())
341 if (getImplicitOp(i).isUse())
342 OS << "<def&use>";
343 else
344 OS << "<def>";
Chris Lattner10491642002-10-30 00:48:05 +0000345 }
346 }
347
348 OS << "\n";
349}
350
Chris Lattner11d1f212004-02-23 18:40:08 +0000351namespace llvm {
Chris Lattner8517e1f2004-02-19 16:17:08 +0000352std::ostream &operator<<(std::ostream &os, const MachineInstr &MI) {
353 // If the instruction is embedded into a basic block, we can find the target
354 // info for the instruction.
355 if (const MachineBasicBlock *MBB = MI.getParent()) {
356 const MachineFunction *MF = MBB->getParent();
357 MI.print(os, MF->getTarget());
358 return os;
359 }
360
361 // Otherwise, print it out in the "raw" format without symbolic register names
362 // and such.
Chris Lattner2a90ba62004-02-12 16:09:53 +0000363 os << TargetInstrDescriptors[MI.getOpcode()].Name;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000364
Chris Lattner8d95ef42003-01-13 00:23:24 +0000365 for (unsigned i=0, N=MI.getNumOperands(); i < N; i++) {
366 os << "\t" << MI.getOperand(i);
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000367 if (MI.getOperand(i).isDef())
368 if (MI.getOperand(i).isUse())
369 os << "<d&u>";
370 else
371 os << "<d>";
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000372 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000373
Misha Brukman6eba07a2003-09-17 21:34:23 +0000374 // code for printing implicit references
Chris Lattner8d95ef42003-01-13 00:23:24 +0000375 unsigned NumOfImpRefs = MI.getNumImplicitRefs();
376 if (NumOfImpRefs > 0) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000377 os << "\tImplicit: ";
Chris Lattner8d95ef42003-01-13 00:23:24 +0000378 for (unsigned z=0; z < NumOfImpRefs; z++) {
379 OutputValue(os, MI.getImplicitRef(z));
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000380 if (MI.getImplicitOp(z).isDef())
381 if (MI.getImplicitOp(z).isUse())
382 os << "<d&u>";
383 else
384 os << "<d>";
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000385 os << "\t";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000386 }
387 }
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000388
Chris Lattner697954c2002-01-20 22:54:45 +0000389 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000390}
391
Brian Gaeke21326fc2004-02-13 04:39:32 +0000392std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) {
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000393 if (MO.isHiBits32())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000394 OS << "%lm(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000395 else if (MO.isLoBits32())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000396 OS << "%lo(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000397 else if (MO.isHiBits64())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000398 OS << "%hh(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000399 else if (MO.isLoBits64())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000400 OS << "%hm(";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000401
Chris Lattner2109f502002-12-15 20:35:25 +0000402 switch (MO.getType())
Vikram S. Adve6e447182001-09-18 12:56:28 +0000403 {
404 case MachineOperand::MO_VirtualRegister:
Chris Lattner8d95ef42003-01-13 00:23:24 +0000405 if (MO.hasAllocatedReg())
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000406 OutputReg(OS, MO.getReg());
Chris Lattner8d95ef42003-01-13 00:23:24 +0000407
408 if (MO.getVRegValue()) {
409 if (MO.hasAllocatedReg()) OS << "==";
410 OS << "%vreg";
411 OutputValue(OS, MO.getVRegValue());
Chris Lattner10491642002-10-30 00:48:05 +0000412 }
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000413 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000414 case MachineOperand::MO_CCRegister:
Chris Lattner10cb79b2002-12-28 20:37:37 +0000415 OS << "%ccreg";
416 OutputValue(OS, MO.getVRegValue());
Chris Lattner2109f502002-12-15 20:35:25 +0000417 if (MO.hasAllocatedReg()) {
Chris Lattner10cb79b2002-12-28 20:37:37 +0000418 OS << "==";
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000419 OutputReg(OS, MO.getReg());
Chris Lattner10491642002-10-30 00:48:05 +0000420 }
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000421 break;
422 case MachineOperand::MO_MachineRegister:
Chris Lattner10cb79b2002-12-28 20:37:37 +0000423 OutputReg(OS, MO.getMachineRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000424 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000425 case MachineOperand::MO_SignExtendedImmed:
Chris Lattner10cb79b2002-12-28 20:37:37 +0000426 OS << (long)MO.getImmedValue();
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000427 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000428 case MachineOperand::MO_UnextendedImmed:
Chris Lattner10cb79b2002-12-28 20:37:37 +0000429 OS << (long)MO.getImmedValue();
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000430 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000431 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000432 {
Chris Lattner2109f502002-12-15 20:35:25 +0000433 const Value* opVal = MO.getVRegValue();
Chris Lattner4d669b52002-04-08 22:01:15 +0000434 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
Chris Lattner10cb79b2002-12-28 20:37:37 +0000435 OS << "%disp(" << (isLabel? "label " : "addr-of-val ");
Vikram S. Adved9beb972001-11-12 14:19:47 +0000436 if (opVal->hasName())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000437 OS << opVal->getName();
Vikram S. Adved9beb972001-11-12 14:19:47 +0000438 else
Chris Lattner10cb79b2002-12-28 20:37:37 +0000439 OS << (const void*) opVal;
440 OS << ")";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000441 break;
Vikram S. Advee949da52001-09-30 23:44:19 +0000442 }
Chris Lattner2109f502002-12-15 20:35:25 +0000443 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner10cb79b2002-12-28 20:37:37 +0000444 OS << "bb<"
Chris Lattner2109f502002-12-15 20:35:25 +0000445 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
446 << "," << (void*)MO.getMachineBasicBlock()->getBasicBlock() << ">";
447 break;
Chris Lattner10cb79b2002-12-28 20:37:37 +0000448 case MachineOperand::MO_FrameIndex:
449 OS << "<fi#" << MO.getFrameIndex() << ">";
450 break;
Chris Lattner8d95ef42003-01-13 00:23:24 +0000451 case MachineOperand::MO_ConstantPoolIndex:
452 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
453 break;
454 case MachineOperand::MO_GlobalAddress:
455 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
456 break;
457 case MachineOperand::MO_ExternalSymbol:
458 OS << "<es:" << MO.getSymbolName() << ">";
459 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000460 default:
461 assert(0 && "Unrecognized operand type");
462 break;
463 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000464
Chris Lattner0742b592004-02-23 18:38:20 +0000465 if (MO.isHiBits32() || MO.isLoBits32() || MO.isHiBits64() || MO.isLoBits64())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000466 OS << ")";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000467
Chris Lattner10cb79b2002-12-28 20:37:37 +0000468 return OS;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000469}
Brian Gaeked0fde302003-11-11 22:41:34 +0000470
Chris Lattner11d1f212004-02-23 18:40:08 +0000471}