blob: 8d2cc932b2e8940288725111caec34cf20bf502b [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)));
Tanya Lattner0c63e032004-05-24 03:14:18 +000081
82 //Set parent, next, and prev to null
83 parent = 0;
84 prev = 0;
85 next = 0;
86
Tanya Lattner466b5342004-05-23 19:35:12 +000087}
88
89
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000090MachineInstr::~MachineInstr()
91{
92 LeakDetector::removeGarbageObject(this);
93}
94
Tanya Lattner466b5342004-05-23 19:35:12 +000095///clone - Create a copy of 'this' instruction that is identical in
96///all ways except the following: The instruction has no parent The
97///instruction has no name
Tanya Lattner0c63e032004-05-24 03:14:18 +000098MachineInstr* MachineInstr::clone() const {
Tanya Lattnerb5159ed2004-05-23 20:58:02 +000099 return new MachineInstr(*this);
Tanya Lattner466b5342004-05-23 19:35:12 +0000100}
101
Brian Gaeke21326fc2004-02-13 04:39:32 +0000102/// OperandComplete - Return true if it's illegal to add a new operand
103///
Chris Lattner2a90ba62004-02-12 16:09:53 +0000104bool MachineInstr::OperandsComplete() const {
105 int NumOperands = TargetInstrDescriptors[Opcode].numOperands;
Vikram S. Advea2bae302002-10-29 19:41:18 +0000106 if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000107 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000108 return false;
109}
110
Brian Gaeke21326fc2004-02-13 04:39:32 +0000111/// replace - Support for replacing opcode and operands of a MachineInstr in
112/// place. This only resets the size of the operand vector and initializes it.
113/// The new operands must be set explicitly later.
114///
Alkis Evlogimenosab8672c2004-02-12 18:49:07 +0000115void MachineInstr::replace(short opcode, unsigned numOperands) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000116 assert(getNumImplicitRefs() == 0 &&
117 "This is probably broken because implicit refs are going to be lost.");
Chris Lattner2a90ba62004-02-12 16:09:53 +0000118 Opcode = opcode;
Vikram S. Advee8b57ef2002-09-20 00:47:49 +0000119 operands.clear();
Chris Lattner413746e2002-10-28 20:48:39 +0000120 operands.resize(numOperands, MachineOperand());
Tanya Lattner466b5342004-05-23 19:35:12 +0000121
Vikram S. Advee8b57ef2002-09-20 00:47:49 +0000122}
123
Chris Lattnera2dd7452003-08-05 16:58:46 +0000124void MachineInstr::SetMachineOperandVal(unsigned i,
125 MachineOperand::MachineOperandType opTy,
126 Value* V) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000127 assert(i < operands.size()); // may be explicit or implicit op
Chris Lattnera2dd7452003-08-05 16:58:46 +0000128 operands[i].opType = opTy;
Brian Gaekec5483952004-03-03 19:07:27 +0000129 operands[i].contents.value = V;
Chris Lattner572f5c82002-10-28 04:24:49 +0000130 operands[i].regNum = -1;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000131}
132
133void
Chris Lattner572f5c82002-10-28 04:24:49 +0000134MachineInstr::SetMachineOperandConst(unsigned i,
Brian Gaeke21326fc2004-02-13 04:39:32 +0000135 MachineOperand::MachineOperandType opTy,
Chris Lattner561c0102004-02-29 05:07:02 +0000136 int intValue) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000137 assert(i < getNumOperands()); // must be explicit op
Chris Lattner2a90ba62004-02-12 16:09:53 +0000138 assert(TargetInstrDescriptors[Opcode].resultPos != (int) i &&
Vikram S. Advec356e562002-03-18 03:35:24 +0000139 "immed. constant cannot be defined");
Chris Lattner572f5c82002-10-28 04:24:49 +0000140
Brian Gaeke21326fc2004-02-13 04:39:32 +0000141 operands[i].opType = opTy;
Brian Gaekec5483952004-03-03 19:07:27 +0000142 operands[i].contents.value = NULL;
143 operands[i].contents.immedVal = intValue;
Chris Lattner572f5c82002-10-28 04:24:49 +0000144 operands[i].regNum = -1;
145 operands[i].flags = 0;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000146}
147
Chris Lattnera2dd7452003-08-05 16:58:46 +0000148void MachineInstr::SetMachineOperandReg(unsigned i, int regNum) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000149 assert(i < getNumOperands()); // must be explicit op
Chris Lattner572f5c82002-10-28 04:24:49 +0000150
Chris Lattner2f305982002-10-28 19:46:59 +0000151 operands[i].opType = MachineOperand::MO_MachineRegister;
Brian Gaekec5483952004-03-03 19:07:27 +0000152 operands[i].contents.value = NULL;
Chris Lattner572f5c82002-10-28 04:24:49 +0000153 operands[i].regNum = regNum;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +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::SetRegForOperand(unsigned i, int regNum) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000158 assert(i < getNumOperands()); // must be explicit op
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000159 operands[i].setRegForValue(regNum);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000160}
161
Brian Gaeke21326fc2004-02-13 04:39:32 +0000162// Used only by the SPARC back-end.
Chris Lattner2a90ba62004-02-12 16:09:53 +0000163void MachineInstr::SetRegForImplicitRef(unsigned i, int regNum) {
Vikram S. Adve34977822003-05-31 07:39:06 +0000164 getImplicitOp(i).setRegForValue(regNum);
Vikram S. Adve34977822003-05-31 07:39:06 +0000165}
166
Brian Gaeke21326fc2004-02-13 04:39:32 +0000167/// substituteValue - Substitute all occurrences of Value* oldVal with newVal
168/// in all operands and all implicit refs. If defsOnly == true, substitute defs
169/// only.
170///
171/// FIXME: Fold this into its single caller, at SparcInstrSelection.cpp:2865,
172/// or make it a static function in that file.
173///
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000174unsigned
Vikram S. Adve627eb312003-07-10 19:45:07 +0000175MachineInstr::substituteValue(const Value* oldVal, Value* newVal,
176 bool defsOnly, bool notDefsAndUses,
177 bool& someArgsWereIgnored)
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000178{
Vikram S. Adve2010f7b2003-08-07 15:01:48 +0000179 assert((!defsOnly || !notDefsAndUses) &&
180 "notDefsAndUses is irrelevant if defsOnly == true.");
Vikram S. Adve627eb312003-07-10 19:45:07 +0000181
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000182 unsigned numSubst = 0;
183
Misha Brukman6eba07a2003-09-17 21:34:23 +0000184 // Substitute operands
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000185 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
186 if (*O == oldVal)
Vikram S. Adve627eb312003-07-10 19:45:07 +0000187 if (!defsOnly ||
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000188 notDefsAndUses && (O.isDef() && !O.isUse()) ||
189 !notDefsAndUses && O.isDef())
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000190 {
Brian Gaekec5483952004-03-03 19:07:27 +0000191 O.getMachineOperand().contents.value = newVal;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000192 ++numSubst;
193 }
Vikram S. Adve627eb312003-07-10 19:45:07 +0000194 else
195 someArgsWereIgnored = true;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000196
Misha Brukman6eba07a2003-09-17 21:34:23 +0000197 // Substitute implicit refs
Vikram S. Advea2bae302002-10-29 19:41:18 +0000198 for (unsigned i=0, N=getNumImplicitRefs(); i < N; ++i)
Chris Lattner27a08932002-10-22 23:16:21 +0000199 if (getImplicitRef(i) == oldVal)
Vikram S. Adve627eb312003-07-10 19:45:07 +0000200 if (!defsOnly ||
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000201 notDefsAndUses && (getImplicitOp(i).isDef() && !getImplicitOp(i).isUse()) ||
202 !notDefsAndUses && getImplicitOp(i).isDef())
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000203 {
Brian Gaekec5483952004-03-03 19:07:27 +0000204 getImplicitOp(i).contents.value = newVal;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000205 ++numSubst;
206 }
Vikram S. Adve627eb312003-07-10 19:45:07 +0000207 else
208 someArgsWereIgnored = true;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000209
210 return numSubst;
211}
212
Brian Gaeke21326fc2004-02-13 04:39:32 +0000213void MachineInstr::dump() const {
Chris Lattner925b7712003-08-03 20:24:29 +0000214 std::cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000215}
216
Brian Gaeke21326fc2004-02-13 04:39:32 +0000217static inline std::ostream& OutputValue(std::ostream &os, const Value* val) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000218 os << "(val ";
Vikram S. Adve627eb312003-07-10 19:45:07 +0000219 os << (void*) val; // print address always
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000220 if (val && val->hasName())
Brian Gaeke21326fc2004-02-13 04:39:32 +0000221 os << " " << val->getName(); // print name also, if available
222 os << ")";
Vikram S. Adve627eb312003-07-10 19:45:07 +0000223 return os;
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000224}
225
Chris Lattner2a79a092002-10-30 00:58:19 +0000226static inline void OutputReg(std::ostream &os, unsigned RegNo,
227 const MRegisterInfo *MRI = 0) {
Alkis Evlogimenosddcfd9e2004-02-27 01:52:34 +0000228 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
Chris Lattner8517e1f2004-02-19 16:17:08 +0000229 if (MRI)
Chris Lattner2a79a092002-10-30 00:58:19 +0000230 os << "%" << MRI->get(RegNo).Name;
231 else
Chris Lattner8517e1f2004-02-19 16:17:08 +0000232 os << "%mreg(" << RegNo << ")";
Chris Lattner2a79a092002-10-30 00:58:19 +0000233 } else
Chris Lattner8517e1f2004-02-19 16:17:08 +0000234 os << "%reg" << RegNo;
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000235}
236
Chris Lattner10491642002-10-30 00:48:05 +0000237static void print(const MachineOperand &MO, std::ostream &OS,
238 const TargetMachine &TM) {
Chris Lattner2a79a092002-10-30 00:58:19 +0000239 const MRegisterInfo *MRI = TM.getRegisterInfo();
Chris Lattner10491642002-10-30 00:48:05 +0000240 bool CloseParen = true;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000241 if (MO.isHiBits32())
Chris Lattner10491642002-10-30 00:48:05 +0000242 OS << "%lm(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000243 else if (MO.isLoBits32())
Chris Lattner10491642002-10-30 00:48:05 +0000244 OS << "%lo(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000245 else if (MO.isHiBits64())
Chris Lattner10491642002-10-30 00:48:05 +0000246 OS << "%hh(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000247 else if (MO.isLoBits64())
Chris Lattner10491642002-10-30 00:48:05 +0000248 OS << "%hm(";
249 else
250 CloseParen = false;
251
252 switch (MO.getType()) {
253 case MachineOperand::MO_VirtualRegister:
254 if (MO.getVRegValue()) {
255 OS << "%reg";
256 OutputValue(OS, MO.getVRegValue());
257 if (MO.hasAllocatedReg())
258 OS << "==";
259 }
260 if (MO.hasAllocatedReg())
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000261 OutputReg(OS, MO.getReg(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000262 break;
263 case MachineOperand::MO_CCRegister:
264 OS << "%ccreg";
265 OutputValue(OS, MO.getVRegValue());
266 if (MO.hasAllocatedReg()) {
267 OS << "==";
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000268 OutputReg(OS, MO.getReg(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000269 }
270 break;
271 case MachineOperand::MO_MachineRegister:
Chris Lattner2a79a092002-10-30 00:58:19 +0000272 OutputReg(OS, MO.getMachineRegNum(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000273 break;
274 case MachineOperand::MO_SignExtendedImmed:
275 OS << (long)MO.getImmedValue();
276 break;
277 case MachineOperand::MO_UnextendedImmed:
278 OS << (long)MO.getImmedValue();
279 break;
280 case MachineOperand::MO_PCRelativeDisp: {
281 const Value* opVal = MO.getVRegValue();
282 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
283 OS << "%disp(" << (isLabel? "label " : "addr-of-val ");
284 if (opVal->hasName())
285 OS << opVal->getName();
286 else
287 OS << (const void*) opVal;
288 OS << ")";
289 break;
290 }
Chris Lattner2109f502002-12-15 20:35:25 +0000291 case MachineOperand::MO_MachineBasicBlock:
292 OS << "bb<"
293 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
294 << "," << (void*)MO.getMachineBasicBlock()->getBasicBlock() << ">";
295 break;
Chris Lattner10cb79b2002-12-28 20:37:37 +0000296 case MachineOperand::MO_FrameIndex:
297 OS << "<fi#" << MO.getFrameIndex() << ">";
298 break;
Chris Lattner8d95ef42003-01-13 00:23:24 +0000299 case MachineOperand::MO_ConstantPoolIndex:
300 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
301 break;
302 case MachineOperand::MO_GlobalAddress:
303 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
304 break;
305 case MachineOperand::MO_ExternalSymbol:
306 OS << "<es:" << MO.getSymbolName() << ">";
307 break;
Chris Lattner10491642002-10-30 00:48:05 +0000308 default:
309 assert(0 && "Unrecognized operand type");
310 }
311
312 if (CloseParen)
313 OS << ")";
314}
315
Chris Lattneraf55be12002-11-17 23:22:13 +0000316void MachineInstr::print(std::ostream &OS, const TargetMachine &TM) const {
Chris Lattner6a592272002-10-30 01:55:38 +0000317 unsigned StartOp = 0;
Chris Lattner10491642002-10-30 00:48:05 +0000318
Chris Lattner6a592272002-10-30 01:55:38 +0000319 // Specialize printing if op#0 is definition
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000320 if (getNumOperands() && getOperand(0).isDef() && !getOperand(0).isUse()) {
Chris Lattner0742b592004-02-23 18:38:20 +0000321 ::print(getOperand(0), OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +0000322 OS << " = ";
323 ++StartOp; // Don't print this operand again!
324 }
325 OS << TM.getInstrInfo().getName(getOpcode());
326
327 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000328 const MachineOperand& mop = getOperand(i);
Chris Lattner6a592272002-10-30 01:55:38 +0000329 if (i != StartOp)
330 OS << ",";
331 OS << " ";
Chris Lattner0742b592004-02-23 18:38:20 +0000332 ::print(mop, OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +0000333
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000334 if (mop.isDef())
335 if (mop.isUse())
336 OS << "<def&use>";
337 else
338 OS << "<def>";
Chris Lattner10491642002-10-30 00:48:05 +0000339 }
Chris Lattner6a592272002-10-30 01:55:38 +0000340
Misha Brukman6eba07a2003-09-17 21:34:23 +0000341 // code for printing implicit references
Chris Lattner10491642002-10-30 00:48:05 +0000342 if (getNumImplicitRefs()) {
343 OS << "\tImplicitRefs: ";
344 for(unsigned i = 0, e = getNumImplicitRefs(); i != e; ++i) {
345 OS << "\t";
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000346 OutputValue(OS, getImplicitRef(i));
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000347 if (getImplicitOp(i).isDef())
348 if (getImplicitOp(i).isUse())
349 OS << "<def&use>";
350 else
351 OS << "<def>";
Chris Lattner10491642002-10-30 00:48:05 +0000352 }
353 }
354
355 OS << "\n";
356}
357
Chris Lattner11d1f212004-02-23 18:40:08 +0000358namespace llvm {
Chris Lattner8517e1f2004-02-19 16:17:08 +0000359std::ostream &operator<<(std::ostream &os, const MachineInstr &MI) {
360 // If the instruction is embedded into a basic block, we can find the target
361 // info for the instruction.
362 if (const MachineBasicBlock *MBB = MI.getParent()) {
363 const MachineFunction *MF = MBB->getParent();
364 MI.print(os, MF->getTarget());
365 return os;
366 }
367
368 // Otherwise, print it out in the "raw" format without symbolic register names
369 // and such.
Chris Lattner2a90ba62004-02-12 16:09:53 +0000370 os << TargetInstrDescriptors[MI.getOpcode()].Name;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000371
Chris Lattner8d95ef42003-01-13 00:23:24 +0000372 for (unsigned i=0, N=MI.getNumOperands(); i < N; i++) {
373 os << "\t" << MI.getOperand(i);
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000374 if (MI.getOperand(i).isDef())
375 if (MI.getOperand(i).isUse())
376 os << "<d&u>";
377 else
378 os << "<d>";
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000379 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000380
Misha Brukman6eba07a2003-09-17 21:34:23 +0000381 // code for printing implicit references
Chris Lattner8d95ef42003-01-13 00:23:24 +0000382 unsigned NumOfImpRefs = MI.getNumImplicitRefs();
383 if (NumOfImpRefs > 0) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000384 os << "\tImplicit: ";
Chris Lattner8d95ef42003-01-13 00:23:24 +0000385 for (unsigned z=0; z < NumOfImpRefs; z++) {
386 OutputValue(os, MI.getImplicitRef(z));
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000387 if (MI.getImplicitOp(z).isDef())
388 if (MI.getImplicitOp(z).isUse())
389 os << "<d&u>";
390 else
391 os << "<d>";
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000392 os << "\t";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000393 }
394 }
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000395
Chris Lattner697954c2002-01-20 22:54:45 +0000396 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000397}
398
Brian Gaeke21326fc2004-02-13 04:39:32 +0000399std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) {
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000400 if (MO.isHiBits32())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000401 OS << "%lm(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000402 else if (MO.isLoBits32())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000403 OS << "%lo(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000404 else if (MO.isHiBits64())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000405 OS << "%hh(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000406 else if (MO.isLoBits64())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000407 OS << "%hm(";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000408
Chris Lattner2109f502002-12-15 20:35:25 +0000409 switch (MO.getType())
Vikram S. Adve6e447182001-09-18 12:56:28 +0000410 {
411 case MachineOperand::MO_VirtualRegister:
Chris Lattner8d95ef42003-01-13 00:23:24 +0000412 if (MO.hasAllocatedReg())
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000413 OutputReg(OS, MO.getReg());
Chris Lattner8d95ef42003-01-13 00:23:24 +0000414
415 if (MO.getVRegValue()) {
416 if (MO.hasAllocatedReg()) OS << "==";
417 OS << "%vreg";
418 OutputValue(OS, MO.getVRegValue());
Chris Lattner10491642002-10-30 00:48:05 +0000419 }
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000420 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000421 case MachineOperand::MO_CCRegister:
Chris Lattner10cb79b2002-12-28 20:37:37 +0000422 OS << "%ccreg";
423 OutputValue(OS, MO.getVRegValue());
Chris Lattner2109f502002-12-15 20:35:25 +0000424 if (MO.hasAllocatedReg()) {
Chris Lattner10cb79b2002-12-28 20:37:37 +0000425 OS << "==";
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000426 OutputReg(OS, MO.getReg());
Chris Lattner10491642002-10-30 00:48:05 +0000427 }
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000428 break;
429 case MachineOperand::MO_MachineRegister:
Chris Lattner10cb79b2002-12-28 20:37:37 +0000430 OutputReg(OS, MO.getMachineRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000431 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000432 case MachineOperand::MO_SignExtendedImmed:
Chris Lattner10cb79b2002-12-28 20:37:37 +0000433 OS << (long)MO.getImmedValue();
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000434 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000435 case MachineOperand::MO_UnextendedImmed:
Chris Lattner10cb79b2002-12-28 20:37:37 +0000436 OS << (long)MO.getImmedValue();
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000437 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000438 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000439 {
Chris Lattner2109f502002-12-15 20:35:25 +0000440 const Value* opVal = MO.getVRegValue();
Chris Lattner4d669b52002-04-08 22:01:15 +0000441 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
Chris Lattner10cb79b2002-12-28 20:37:37 +0000442 OS << "%disp(" << (isLabel? "label " : "addr-of-val ");
Vikram S. Adved9beb972001-11-12 14:19:47 +0000443 if (opVal->hasName())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000444 OS << opVal->getName();
Vikram S. Adved9beb972001-11-12 14:19:47 +0000445 else
Chris Lattner10cb79b2002-12-28 20:37:37 +0000446 OS << (const void*) opVal;
447 OS << ")";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000448 break;
Vikram S. Advee949da52001-09-30 23:44:19 +0000449 }
Chris Lattner2109f502002-12-15 20:35:25 +0000450 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner10cb79b2002-12-28 20:37:37 +0000451 OS << "bb<"
Chris Lattner2109f502002-12-15 20:35:25 +0000452 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
453 << "," << (void*)MO.getMachineBasicBlock()->getBasicBlock() << ">";
454 break;
Chris Lattner10cb79b2002-12-28 20:37:37 +0000455 case MachineOperand::MO_FrameIndex:
456 OS << "<fi#" << MO.getFrameIndex() << ">";
457 break;
Chris Lattner8d95ef42003-01-13 00:23:24 +0000458 case MachineOperand::MO_ConstantPoolIndex:
459 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
460 break;
461 case MachineOperand::MO_GlobalAddress:
462 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
463 break;
464 case MachineOperand::MO_ExternalSymbol:
465 OS << "<es:" << MO.getSymbolName() << ">";
466 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000467 default:
468 assert(0 && "Unrecognized operand type");
469 break;
470 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000471
Chris Lattner0742b592004-02-23 18:38:20 +0000472 if (MO.isHiBits32() || MO.isLoBits32() || MO.isHiBits64() || MO.isLoBits64())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000473 OS << ")";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000474
Chris Lattner10cb79b2002-12-28 20:37:37 +0000475 return OS;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000476}
Brian Gaeked0fde302003-11-11 22:41:34 +0000477
Chris Lattner11d1f212004-02-23 18:40:08 +0000478}