blob: 401cd8b417238b7ea419239b4778f931f182647c [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();
Tanya Lattnerb5159ed2004-05-23 20:58:02 +000076 operands.reserve(MI.getNumOperands());
77
Tanya Lattner466b5342004-05-23 19:35:12 +000078 //Add operands
79 for(unsigned i=0; i < MI.getNumOperands(); ++i)
80 operands.push_back(MachineOperand(MI.getOperand(i)));
81}
82
83
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000084MachineInstr::~MachineInstr()
85{
86 LeakDetector::removeGarbageObject(this);
87}
88
Tanya Lattner466b5342004-05-23 19:35:12 +000089///clone - Create a copy of 'this' instruction that is identical in
90///all ways except the following: The instruction has no parent The
91///instruction has no name
92MachineInstr* MachineInstr::clone() {
Tanya Lattnerb5159ed2004-05-23 20:58:02 +000093 return new MachineInstr(*this);
Tanya Lattner466b5342004-05-23 19:35:12 +000094}
95
Brian Gaeke21326fc2004-02-13 04:39:32 +000096/// OperandComplete - Return true if it's illegal to add a new operand
97///
Chris Lattner2a90ba62004-02-12 16:09:53 +000098bool MachineInstr::OperandsComplete() const {
99 int NumOperands = TargetInstrDescriptors[Opcode].numOperands;
Vikram S. Advea2bae302002-10-29 19:41:18 +0000100 if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000101 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000102 return false;
103}
104
Brian Gaeke21326fc2004-02-13 04:39:32 +0000105/// replace - Support for replacing opcode and operands of a MachineInstr in
106/// place. This only resets the size of the operand vector and initializes it.
107/// The new operands must be set explicitly later.
108///
Alkis Evlogimenosab8672c2004-02-12 18:49:07 +0000109void MachineInstr::replace(short opcode, unsigned numOperands) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000110 assert(getNumImplicitRefs() == 0 &&
111 "This is probably broken because implicit refs are going to be lost.");
Chris Lattner2a90ba62004-02-12 16:09:53 +0000112 Opcode = opcode;
Vikram S. Advee8b57ef2002-09-20 00:47:49 +0000113 operands.clear();
Chris Lattner413746e2002-10-28 20:48:39 +0000114 operands.resize(numOperands, MachineOperand());
Tanya Lattner466b5342004-05-23 19:35:12 +0000115
Vikram S. Advee8b57ef2002-09-20 00:47:49 +0000116}
117
Chris Lattnera2dd7452003-08-05 16:58:46 +0000118void MachineInstr::SetMachineOperandVal(unsigned i,
119 MachineOperand::MachineOperandType opTy,
120 Value* V) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000121 assert(i < operands.size()); // may be explicit or implicit op
Chris Lattnera2dd7452003-08-05 16:58:46 +0000122 operands[i].opType = opTy;
Brian Gaekec5483952004-03-03 19:07:27 +0000123 operands[i].contents.value = V;
Chris Lattner572f5c82002-10-28 04:24:49 +0000124 operands[i].regNum = -1;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000125}
126
127void
Chris Lattner572f5c82002-10-28 04:24:49 +0000128MachineInstr::SetMachineOperandConst(unsigned i,
Brian Gaeke21326fc2004-02-13 04:39:32 +0000129 MachineOperand::MachineOperandType opTy,
Chris Lattner561c0102004-02-29 05:07:02 +0000130 int intValue) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000131 assert(i < getNumOperands()); // must be explicit op
Chris Lattner2a90ba62004-02-12 16:09:53 +0000132 assert(TargetInstrDescriptors[Opcode].resultPos != (int) i &&
Vikram S. Advec356e562002-03-18 03:35:24 +0000133 "immed. constant cannot be defined");
Chris Lattner572f5c82002-10-28 04:24:49 +0000134
Brian Gaeke21326fc2004-02-13 04:39:32 +0000135 operands[i].opType = opTy;
Brian Gaekec5483952004-03-03 19:07:27 +0000136 operands[i].contents.value = NULL;
137 operands[i].contents.immedVal = intValue;
Chris Lattner572f5c82002-10-28 04:24:49 +0000138 operands[i].regNum = -1;
139 operands[i].flags = 0;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000140}
141
Chris Lattnera2dd7452003-08-05 16:58:46 +0000142void MachineInstr::SetMachineOperandReg(unsigned i, int regNum) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000143 assert(i < getNumOperands()); // must be explicit op
Chris Lattner572f5c82002-10-28 04:24:49 +0000144
Chris Lattner2f305982002-10-28 19:46:59 +0000145 operands[i].opType = MachineOperand::MO_MachineRegister;
Brian Gaekec5483952004-03-03 19:07:27 +0000146 operands[i].contents.value = NULL;
Chris Lattner572f5c82002-10-28 04:24:49 +0000147 operands[i].regNum = regNum;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000148}
149
Brian Gaeke21326fc2004-02-13 04:39:32 +0000150// Used only by the SPARC back-end.
Chris Lattner2a90ba62004-02-12 16:09:53 +0000151void MachineInstr::SetRegForOperand(unsigned i, int regNum) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000152 assert(i < getNumOperands()); // must be explicit op
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000153 operands[i].setRegForValue(regNum);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000154}
155
Brian Gaeke21326fc2004-02-13 04:39:32 +0000156// Used only by the SPARC back-end.
Chris Lattner2a90ba62004-02-12 16:09:53 +0000157void MachineInstr::SetRegForImplicitRef(unsigned i, int regNum) {
Vikram S. Adve34977822003-05-31 07:39:06 +0000158 getImplicitOp(i).setRegForValue(regNum);
Vikram S. Adve34977822003-05-31 07:39:06 +0000159}
160
Brian Gaeke21326fc2004-02-13 04:39:32 +0000161/// substituteValue - Substitute all occurrences of Value* oldVal with newVal
162/// in all operands and all implicit refs. If defsOnly == true, substitute defs
163/// only.
164///
165/// FIXME: Fold this into its single caller, at SparcInstrSelection.cpp:2865,
166/// or make it a static function in that file.
167///
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000168unsigned
Vikram S. Adve627eb312003-07-10 19:45:07 +0000169MachineInstr::substituteValue(const Value* oldVal, Value* newVal,
170 bool defsOnly, bool notDefsAndUses,
171 bool& someArgsWereIgnored)
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000172{
Vikram S. Adve2010f7b2003-08-07 15:01:48 +0000173 assert((!defsOnly || !notDefsAndUses) &&
174 "notDefsAndUses is irrelevant if defsOnly == true.");
Vikram S. Adve627eb312003-07-10 19:45:07 +0000175
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000176 unsigned numSubst = 0;
177
Misha Brukman6eba07a2003-09-17 21:34:23 +0000178 // Substitute operands
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000179 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
180 if (*O == oldVal)
Vikram S. Adve627eb312003-07-10 19:45:07 +0000181 if (!defsOnly ||
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000182 notDefsAndUses && (O.isDef() && !O.isUse()) ||
183 !notDefsAndUses && O.isDef())
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000184 {
Brian Gaekec5483952004-03-03 19:07:27 +0000185 O.getMachineOperand().contents.value = newVal;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000186 ++numSubst;
187 }
Vikram S. Adve627eb312003-07-10 19:45:07 +0000188 else
189 someArgsWereIgnored = true;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000190
Misha Brukman6eba07a2003-09-17 21:34:23 +0000191 // Substitute implicit refs
Vikram S. Advea2bae302002-10-29 19:41:18 +0000192 for (unsigned i=0, N=getNumImplicitRefs(); i < N; ++i)
Chris Lattner27a08932002-10-22 23:16:21 +0000193 if (getImplicitRef(i) == oldVal)
Vikram S. Adve627eb312003-07-10 19:45:07 +0000194 if (!defsOnly ||
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000195 notDefsAndUses && (getImplicitOp(i).isDef() && !getImplicitOp(i).isUse()) ||
196 !notDefsAndUses && getImplicitOp(i).isDef())
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000197 {
Brian Gaekec5483952004-03-03 19:07:27 +0000198 getImplicitOp(i).contents.value = newVal;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000199 ++numSubst;
200 }
Vikram S. Adve627eb312003-07-10 19:45:07 +0000201 else
202 someArgsWereIgnored = true;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000203
204 return numSubst;
205}
206
Brian Gaeke21326fc2004-02-13 04:39:32 +0000207void MachineInstr::dump() const {
Chris Lattner925b7712003-08-03 20:24:29 +0000208 std::cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000209}
210
Brian Gaeke21326fc2004-02-13 04:39:32 +0000211static inline std::ostream& OutputValue(std::ostream &os, const Value* val) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000212 os << "(val ";
Vikram S. Adve627eb312003-07-10 19:45:07 +0000213 os << (void*) val; // print address always
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000214 if (val && val->hasName())
Brian Gaeke21326fc2004-02-13 04:39:32 +0000215 os << " " << val->getName(); // print name also, if available
216 os << ")";
Vikram S. Adve627eb312003-07-10 19:45:07 +0000217 return os;
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000218}
219
Chris Lattner2a79a092002-10-30 00:58:19 +0000220static inline void OutputReg(std::ostream &os, unsigned RegNo,
221 const MRegisterInfo *MRI = 0) {
Alkis Evlogimenosddcfd9e2004-02-27 01:52:34 +0000222 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
Chris Lattner8517e1f2004-02-19 16:17:08 +0000223 if (MRI)
Chris Lattner2a79a092002-10-30 00:58:19 +0000224 os << "%" << MRI->get(RegNo).Name;
225 else
Chris Lattner8517e1f2004-02-19 16:17:08 +0000226 os << "%mreg(" << RegNo << ")";
Chris Lattner2a79a092002-10-30 00:58:19 +0000227 } else
Chris Lattner8517e1f2004-02-19 16:17:08 +0000228 os << "%reg" << RegNo;
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000229}
230
Chris Lattner10491642002-10-30 00:48:05 +0000231static void print(const MachineOperand &MO, std::ostream &OS,
232 const TargetMachine &TM) {
Chris Lattner2a79a092002-10-30 00:58:19 +0000233 const MRegisterInfo *MRI = TM.getRegisterInfo();
Chris Lattner10491642002-10-30 00:48:05 +0000234 bool CloseParen = true;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000235 if (MO.isHiBits32())
Chris Lattner10491642002-10-30 00:48:05 +0000236 OS << "%lm(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000237 else if (MO.isLoBits32())
Chris Lattner10491642002-10-30 00:48:05 +0000238 OS << "%lo(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000239 else if (MO.isHiBits64())
Chris Lattner10491642002-10-30 00:48:05 +0000240 OS << "%hh(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000241 else if (MO.isLoBits64())
Chris Lattner10491642002-10-30 00:48:05 +0000242 OS << "%hm(";
243 else
244 CloseParen = false;
245
246 switch (MO.getType()) {
247 case MachineOperand::MO_VirtualRegister:
248 if (MO.getVRegValue()) {
249 OS << "%reg";
250 OutputValue(OS, MO.getVRegValue());
251 if (MO.hasAllocatedReg())
252 OS << "==";
253 }
254 if (MO.hasAllocatedReg())
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000255 OutputReg(OS, MO.getReg(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000256 break;
257 case MachineOperand::MO_CCRegister:
258 OS << "%ccreg";
259 OutputValue(OS, MO.getVRegValue());
260 if (MO.hasAllocatedReg()) {
261 OS << "==";
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000262 OutputReg(OS, MO.getReg(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000263 }
264 break;
265 case MachineOperand::MO_MachineRegister:
Chris Lattner2a79a092002-10-30 00:58:19 +0000266 OutputReg(OS, MO.getMachineRegNum(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000267 break;
268 case MachineOperand::MO_SignExtendedImmed:
269 OS << (long)MO.getImmedValue();
270 break;
271 case MachineOperand::MO_UnextendedImmed:
272 OS << (long)MO.getImmedValue();
273 break;
274 case MachineOperand::MO_PCRelativeDisp: {
275 const Value* opVal = MO.getVRegValue();
276 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
277 OS << "%disp(" << (isLabel? "label " : "addr-of-val ");
278 if (opVal->hasName())
279 OS << opVal->getName();
280 else
281 OS << (const void*) opVal;
282 OS << ")";
283 break;
284 }
Chris Lattner2109f502002-12-15 20:35:25 +0000285 case MachineOperand::MO_MachineBasicBlock:
286 OS << "bb<"
287 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
288 << "," << (void*)MO.getMachineBasicBlock()->getBasicBlock() << ">";
289 break;
Chris Lattner10cb79b2002-12-28 20:37:37 +0000290 case MachineOperand::MO_FrameIndex:
291 OS << "<fi#" << MO.getFrameIndex() << ">";
292 break;
Chris Lattner8d95ef42003-01-13 00:23:24 +0000293 case MachineOperand::MO_ConstantPoolIndex:
294 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
295 break;
296 case MachineOperand::MO_GlobalAddress:
297 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
298 break;
299 case MachineOperand::MO_ExternalSymbol:
300 OS << "<es:" << MO.getSymbolName() << ">";
301 break;
Chris Lattner10491642002-10-30 00:48:05 +0000302 default:
303 assert(0 && "Unrecognized operand type");
304 }
305
306 if (CloseParen)
307 OS << ")";
308}
309
Chris Lattneraf55be12002-11-17 23:22:13 +0000310void MachineInstr::print(std::ostream &OS, const TargetMachine &TM) const {
Chris Lattner6a592272002-10-30 01:55:38 +0000311 unsigned StartOp = 0;
Chris Lattner10491642002-10-30 00:48:05 +0000312
Chris Lattner6a592272002-10-30 01:55:38 +0000313 // Specialize printing if op#0 is definition
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000314 if (getNumOperands() && getOperand(0).isDef() && !getOperand(0).isUse()) {
Chris Lattner0742b592004-02-23 18:38:20 +0000315 ::print(getOperand(0), OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +0000316 OS << " = ";
317 ++StartOp; // Don't print this operand again!
318 }
319 OS << TM.getInstrInfo().getName(getOpcode());
320
321 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000322 const MachineOperand& mop = getOperand(i);
Chris Lattner6a592272002-10-30 01:55:38 +0000323 if (i != StartOp)
324 OS << ",";
325 OS << " ";
Chris Lattner0742b592004-02-23 18:38:20 +0000326 ::print(mop, OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +0000327
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000328 if (mop.isDef())
329 if (mop.isUse())
330 OS << "<def&use>";
331 else
332 OS << "<def>";
Chris Lattner10491642002-10-30 00:48:05 +0000333 }
Chris Lattner6a592272002-10-30 01:55:38 +0000334
Misha Brukman6eba07a2003-09-17 21:34:23 +0000335 // code for printing implicit references
Chris Lattner10491642002-10-30 00:48:05 +0000336 if (getNumImplicitRefs()) {
337 OS << "\tImplicitRefs: ";
338 for(unsigned i = 0, e = getNumImplicitRefs(); i != e; ++i) {
339 OS << "\t";
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000340 OutputValue(OS, getImplicitRef(i));
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000341 if (getImplicitOp(i).isDef())
342 if (getImplicitOp(i).isUse())
343 OS << "<def&use>";
344 else
345 OS << "<def>";
Chris Lattner10491642002-10-30 00:48:05 +0000346 }
347 }
348
349 OS << "\n";
350}
351
Chris Lattner11d1f212004-02-23 18:40:08 +0000352namespace llvm {
Chris Lattner8517e1f2004-02-19 16:17:08 +0000353std::ostream &operator<<(std::ostream &os, const MachineInstr &MI) {
354 // If the instruction is embedded into a basic block, we can find the target
355 // info for the instruction.
356 if (const MachineBasicBlock *MBB = MI.getParent()) {
357 const MachineFunction *MF = MBB->getParent();
358 MI.print(os, MF->getTarget());
359 return os;
360 }
361
362 // Otherwise, print it out in the "raw" format without symbolic register names
363 // and such.
Chris Lattner2a90ba62004-02-12 16:09:53 +0000364 os << TargetInstrDescriptors[MI.getOpcode()].Name;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000365
Chris Lattner8d95ef42003-01-13 00:23:24 +0000366 for (unsigned i=0, N=MI.getNumOperands(); i < N; i++) {
367 os << "\t" << MI.getOperand(i);
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000368 if (MI.getOperand(i).isDef())
369 if (MI.getOperand(i).isUse())
370 os << "<d&u>";
371 else
372 os << "<d>";
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000373 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000374
Misha Brukman6eba07a2003-09-17 21:34:23 +0000375 // code for printing implicit references
Chris Lattner8d95ef42003-01-13 00:23:24 +0000376 unsigned NumOfImpRefs = MI.getNumImplicitRefs();
377 if (NumOfImpRefs > 0) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000378 os << "\tImplicit: ";
Chris Lattner8d95ef42003-01-13 00:23:24 +0000379 for (unsigned z=0; z < NumOfImpRefs; z++) {
380 OutputValue(os, MI.getImplicitRef(z));
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000381 if (MI.getImplicitOp(z).isDef())
382 if (MI.getImplicitOp(z).isUse())
383 os << "<d&u>";
384 else
385 os << "<d>";
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000386 os << "\t";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000387 }
388 }
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000389
Chris Lattner697954c2002-01-20 22:54:45 +0000390 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000391}
392
Brian Gaeke21326fc2004-02-13 04:39:32 +0000393std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) {
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000394 if (MO.isHiBits32())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000395 OS << "%lm(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000396 else if (MO.isLoBits32())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000397 OS << "%lo(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000398 else if (MO.isHiBits64())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000399 OS << "%hh(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000400 else if (MO.isLoBits64())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000401 OS << "%hm(";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000402
Chris Lattner2109f502002-12-15 20:35:25 +0000403 switch (MO.getType())
Vikram S. Adve6e447182001-09-18 12:56:28 +0000404 {
405 case MachineOperand::MO_VirtualRegister:
Chris Lattner8d95ef42003-01-13 00:23:24 +0000406 if (MO.hasAllocatedReg())
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000407 OutputReg(OS, MO.getReg());
Chris Lattner8d95ef42003-01-13 00:23:24 +0000408
409 if (MO.getVRegValue()) {
410 if (MO.hasAllocatedReg()) OS << "==";
411 OS << "%vreg";
412 OutputValue(OS, MO.getVRegValue());
Chris Lattner10491642002-10-30 00:48:05 +0000413 }
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000414 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000415 case MachineOperand::MO_CCRegister:
Chris Lattner10cb79b2002-12-28 20:37:37 +0000416 OS << "%ccreg";
417 OutputValue(OS, MO.getVRegValue());
Chris Lattner2109f502002-12-15 20:35:25 +0000418 if (MO.hasAllocatedReg()) {
Chris Lattner10cb79b2002-12-28 20:37:37 +0000419 OS << "==";
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000420 OutputReg(OS, MO.getReg());
Chris Lattner10491642002-10-30 00:48:05 +0000421 }
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000422 break;
423 case MachineOperand::MO_MachineRegister:
Chris Lattner10cb79b2002-12-28 20:37:37 +0000424 OutputReg(OS, MO.getMachineRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000425 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000426 case MachineOperand::MO_SignExtendedImmed:
Chris Lattner10cb79b2002-12-28 20:37:37 +0000427 OS << (long)MO.getImmedValue();
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000428 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000429 case MachineOperand::MO_UnextendedImmed:
Chris Lattner10cb79b2002-12-28 20:37:37 +0000430 OS << (long)MO.getImmedValue();
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000431 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000432 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000433 {
Chris Lattner2109f502002-12-15 20:35:25 +0000434 const Value* opVal = MO.getVRegValue();
Chris Lattner4d669b52002-04-08 22:01:15 +0000435 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
Chris Lattner10cb79b2002-12-28 20:37:37 +0000436 OS << "%disp(" << (isLabel? "label " : "addr-of-val ");
Vikram S. Adved9beb972001-11-12 14:19:47 +0000437 if (opVal->hasName())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000438 OS << opVal->getName();
Vikram S. Adved9beb972001-11-12 14:19:47 +0000439 else
Chris Lattner10cb79b2002-12-28 20:37:37 +0000440 OS << (const void*) opVal;
441 OS << ")";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000442 break;
Vikram S. Advee949da52001-09-30 23:44:19 +0000443 }
Chris Lattner2109f502002-12-15 20:35:25 +0000444 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner10cb79b2002-12-28 20:37:37 +0000445 OS << "bb<"
Chris Lattner2109f502002-12-15 20:35:25 +0000446 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
447 << "," << (void*)MO.getMachineBasicBlock()->getBasicBlock() << ">";
448 break;
Chris Lattner10cb79b2002-12-28 20:37:37 +0000449 case MachineOperand::MO_FrameIndex:
450 OS << "<fi#" << MO.getFrameIndex() << ">";
451 break;
Chris Lattner8d95ef42003-01-13 00:23:24 +0000452 case MachineOperand::MO_ConstantPoolIndex:
453 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
454 break;
455 case MachineOperand::MO_GlobalAddress:
456 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
457 break;
458 case MachineOperand::MO_ExternalSymbol:
459 OS << "<es:" << MO.getSymbolName() << ">";
460 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000461 default:
462 assert(0 && "Unrecognized operand type");
463 break;
464 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000465
Chris Lattner0742b592004-02-23 18:38:20 +0000466 if (MO.isHiBits32() || MO.isLoBits32() || MO.isHiBits64() || MO.isLoBits64())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000467 OS << ")";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000468
Chris Lattner10cb79b2002-12-28 20:37:37 +0000469 return OS;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000470}
Brian Gaeked0fde302003-11-11 22:41:34 +0000471
Chris Lattner11d1f212004-02-23 18:40:08 +0000472}