blob: 0351cc2783e2286fef7c148d7b46ef414fd1f9bc [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//
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"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/LeakDetector.h"
Reid Spencer954da372004-07-04 12:19:56 +000024#include <iostream>
25
Chris Lattner0742b592004-02-23 18:38:20 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattnerf1757c42002-10-29 17:40:30 +000028// Global variable holding an array of descriptors for machine instructions.
29// The actual object needs to be created separately for each target machine.
Chris Lattner3501fea2003-01-14 22:00:31 +000030// This variable is initialized and reset by class TargetInstrInfo.
Misha Brukmanedf128a2005-04-21 22:36:52 +000031//
Chris Lattnerf1757c42002-10-29 17:40:30 +000032// FIXME: This should be a property of the target so that more than one target
33// at a time can be active...
34//
Chris Lattner11d1f212004-02-23 18:40:08 +000035namespace llvm {
Chris Lattner0742b592004-02-23 18:38:20 +000036 extern const TargetInstrDescriptor *TargetInstrDescriptors;
37}
Ruchira Sasanka69917e22001-10-18 22:40:02 +000038
Vikram S. Adve1885da42001-07-31 21:49:28 +000039// Constructor for instructions with variable #operands
Alkis Evlogimenosab8672c2004-02-12 18:49:07 +000040MachineInstr::MachineInstr(short opcode, unsigned numOperands)
41 : Opcode(opcode),
42 numImplicitRefs(0),
43 operands(numOperands, MachineOperand()),
44 parent(0) {
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000045 // Make sure that we get added to a machine basicblock
46 LeakDetector::addGarbageObject(this);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000047}
48
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000049/// MachineInstr ctor - This constructor only does a _reserve_ of the operands,
50/// not a resize for them. It is expected that if you use this that you call
51/// add* methods below to fill up the operands, instead of the Set methods.
52/// Eventually, the "resizing" ctors will be phased out.
53///
Brian Gaeke21326fc2004-02-13 04:39:32 +000054MachineInstr::MachineInstr(short opcode, unsigned numOperands, bool XX, bool YY)
55 : Opcode(opcode), numImplicitRefs(0), parent(0) {
Chris Lattner72791222002-10-28 20:59:49 +000056 operands.reserve(numOperands);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000057 // Make sure that we get added to a machine basicblock
58 LeakDetector::addGarbageObject(this);
Chris Lattner72791222002-10-28 20:59:49 +000059}
60
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000061/// MachineInstr ctor - Work exactly the same as the ctor above, except that the
62/// MachineInstr is created and added to the end of the specified basic block.
63///
Alkis Evlogimenosab8672c2004-02-12 18:49:07 +000064MachineInstr::MachineInstr(MachineBasicBlock *MBB, short opcode,
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000065 unsigned numOperands)
Brian Gaeke21326fc2004-02-13 04:39:32 +000066 : Opcode(opcode), numImplicitRefs(0), parent(0) {
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000067 assert(MBB && "Cannot use inserting ctor with null basic block!");
68 operands.reserve(numOperands);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000069 // Make sure that we get added to a machine basicblock
70 LeakDetector::addGarbageObject(this);
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000071 MBB->push_back(this); // Add instruction to end of basic block!
72}
73
Misha Brukmance22e762004-07-09 14:45:17 +000074/// MachineInstr ctor - Copies MachineInstr arg exactly
75///
Tanya Lattner466b5342004-05-23 19:35:12 +000076MachineInstr::MachineInstr(const MachineInstr &MI) {
77 Opcode = MI.getOpcode();
78 numImplicitRefs = MI.getNumImplicitRefs();
Tanya Lattnerb5159ed2004-05-23 20:58:02 +000079 operands.reserve(MI.getNumOperands());
80
Misha Brukmance22e762004-07-09 14:45:17 +000081 // Add operands
82 for (unsigned i = 0; i < MI.getNumOperands(); ++i)
Tanya Lattner466b5342004-05-23 19:35:12 +000083 operands.push_back(MachineOperand(MI.getOperand(i)));
Tanya Lattner0c63e032004-05-24 03:14:18 +000084
Misha Brukmance22e762004-07-09 14:45:17 +000085 // Set parent, next, and prev to null
Tanya Lattner0c63e032004-05-24 03:14:18 +000086 parent = 0;
87 prev = 0;
88 next = 0;
Tanya Lattner466b5342004-05-23 19:35:12 +000089}
90
91
Misha Brukmance22e762004-07-09 14:45:17 +000092MachineInstr::~MachineInstr() {
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000093 LeakDetector::removeGarbageObject(this);
94}
95
Misha Brukmance22e762004-07-09 14:45:17 +000096/// clone - Create a copy of 'this' instruction that is identical in all ways
97/// except the following: the new instruction has no parent and it has no name
98///
Tanya Lattner0c63e032004-05-24 03:14:18 +000099MachineInstr* MachineInstr::clone() const {
Tanya Lattnerb5159ed2004-05-23 20:58:02 +0000100 return new MachineInstr(*this);
Tanya Lattner466b5342004-05-23 19:35:12 +0000101}
102
Brian Gaeke21326fc2004-02-13 04:39:32 +0000103/// OperandComplete - Return true if it's illegal to add a new operand
104///
Chris Lattner2a90ba62004-02-12 16:09:53 +0000105bool MachineInstr::OperandsComplete() const {
106 int NumOperands = TargetInstrDescriptors[Opcode].numOperands;
Vikram S. Advea2bae302002-10-29 19:41:18 +0000107 if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000108 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000109 return false;
110}
111
Brian Gaeke21326fc2004-02-13 04:39:32 +0000112/// replace - Support for replacing opcode and operands of a MachineInstr in
113/// place. This only resets the size of the operand vector and initializes it.
114/// The new operands must be set explicitly later.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000115///
Alkis Evlogimenosab8672c2004-02-12 18:49:07 +0000116void MachineInstr::replace(short opcode, unsigned numOperands) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000117 assert(getNumImplicitRefs() == 0 &&
118 "This is probably broken because implicit refs are going to be lost.");
Chris Lattner2a90ba62004-02-12 16:09:53 +0000119 Opcode = opcode;
Vikram S. Advee8b57ef2002-09-20 00:47:49 +0000120 operands.clear();
Chris Lattner413746e2002-10-28 20:48:39 +0000121 operands.resize(numOperands, MachineOperand());
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 Lattnerca4f6eb2004-10-15 04:38:41 +0000130 operands[i].extra.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 Lattnerca4f6eb2004-10-15 04:38:41 +0000144 operands[i].extra.regNum = -1;
Chris Lattner572f5c82002-10-28 04:24:49 +0000145 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 Lattnerca4f6eb2004-10-15 04:38:41 +0000153 operands[i].extra.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.");
Misha Brukmanedf128a2005-04-21 22:36:52 +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())
Misha Brukmance22e762004-07-09 14:45:17 +0000190 {
191 O.getMachineOperand().contents.value = newVal;
192 ++numSubst;
193 } else
Vikram S. Adve627eb312003-07-10 19:45:07 +0000194 someArgsWereIgnored = true;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000195
Misha Brukman6eba07a2003-09-17 21:34:23 +0000196 // Substitute implicit refs
Misha Brukmance22e762004-07-09 14:45:17 +0000197 for (unsigned i = 0, N = getNumImplicitRefs(); i < N; ++i)
198 if (getImplicitRef(i) == oldVal) {
199 MachineOperand Op = getImplicitOp(i);
Vikram S. Adve627eb312003-07-10 19:45:07 +0000200 if (!defsOnly ||
Misha Brukmance22e762004-07-09 14:45:17 +0000201 notDefsAndUses && (Op.isDef() && !Op.isUse()) ||
202 !notDefsAndUses && Op.isDef())
203 {
204 Op.contents.value = newVal;
205 ++numSubst;
206 } else
Vikram S. Adve627eb312003-07-10 19:45:07 +0000207 someArgsWereIgnored = true;
Misha Brukmance22e762004-07-09 14:45:17 +0000208 }
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000209 return numSubst;
210}
211
Brian Gaeke21326fc2004-02-13 04:39:32 +0000212void MachineInstr::dump() const {
Chris Lattner925b7712003-08-03 20:24:29 +0000213 std::cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000214}
215
Brian Gaeke21326fc2004-02-13 04:39:32 +0000216static inline std::ostream& OutputValue(std::ostream &os, const Value* val) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000217 os << "(val ";
Misha Brukmance22e762004-07-09 14:45:17 +0000218 os << (void*) val; // print address always
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000219 if (val && val->hasName())
Misha Brukmance22e762004-07-09 14:45:17 +0000220 os << " " << val->getName(); // print name also, if available
Brian Gaeke21326fc2004-02-13 04:39:32 +0000221 os << ")";
Vikram S. Adve627eb312003-07-10 19:45:07 +0000222 return os;
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000223}
224
Chris Lattner2a79a092002-10-30 00:58:19 +0000225static inline void OutputReg(std::ostream &os, unsigned RegNo,
226 const MRegisterInfo *MRI = 0) {
Alkis Evlogimenosddcfd9e2004-02-27 01:52:34 +0000227 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
Chris Lattner8517e1f2004-02-19 16:17:08 +0000228 if (MRI)
Chris Lattner2a79a092002-10-30 00:58:19 +0000229 os << "%" << MRI->get(RegNo).Name;
230 else
Chris Lattner8517e1f2004-02-19 16:17:08 +0000231 os << "%mreg(" << RegNo << ")";
Chris Lattner2a79a092002-10-30 00:58:19 +0000232 } else
Chris Lattner8517e1f2004-02-19 16:17:08 +0000233 os << "%reg" << RegNo;
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000234}
235
Chris Lattner10491642002-10-30 00:48:05 +0000236static void print(const MachineOperand &MO, std::ostream &OS,
Tanya Lattnerb1407622004-06-25 00:13:11 +0000237 const TargetMachine *TM) {
Misha Brukmance22e762004-07-09 14:45:17 +0000238 const MRegisterInfo *MRI = 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000239
Misha Brukmance22e762004-07-09 14:45:17 +0000240 if (TM) MRI = TM->getRegisterInfo();
Tanya Lattnerb1407622004-06-25 00:13:11 +0000241
Chris Lattner10491642002-10-30 00:48:05 +0000242 bool CloseParen = true;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000243 if (MO.isHiBits32())
Chris Lattner10491642002-10-30 00:48:05 +0000244 OS << "%lm(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000245 else if (MO.isLoBits32())
Chris Lattner10491642002-10-30 00:48:05 +0000246 OS << "%lo(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000247 else if (MO.isHiBits64())
Chris Lattner10491642002-10-30 00:48:05 +0000248 OS << "%hh(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000249 else if (MO.isLoBits64())
Chris Lattner10491642002-10-30 00:48:05 +0000250 OS << "%hm(";
251 else
252 CloseParen = false;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000253
Chris Lattner10491642002-10-30 00:48:05 +0000254 switch (MO.getType()) {
255 case MachineOperand::MO_VirtualRegister:
256 if (MO.getVRegValue()) {
257 OS << "%reg";
258 OutputValue(OS, MO.getVRegValue());
259 if (MO.hasAllocatedReg())
260 OS << "==";
261 }
262 if (MO.hasAllocatedReg())
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000263 OutputReg(OS, MO.getReg(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000264 break;
265 case MachineOperand::MO_CCRegister:
266 OS << "%ccreg";
267 OutputValue(OS, MO.getVRegValue());
268 if (MO.hasAllocatedReg()) {
269 OS << "==";
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000270 OutputReg(OS, MO.getReg(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000271 }
272 break;
273 case MachineOperand::MO_MachineRegister:
Chris Lattner2a79a092002-10-30 00:58:19 +0000274 OutputReg(OS, MO.getMachineRegNum(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000275 break;
276 case MachineOperand::MO_SignExtendedImmed:
277 OS << (long)MO.getImmedValue();
278 break;
279 case MachineOperand::MO_UnextendedImmed:
280 OS << (long)MO.getImmedValue();
281 break;
282 case MachineOperand::MO_PCRelativeDisp: {
283 const Value* opVal = MO.getVRegValue();
284 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
285 OS << "%disp(" << (isLabel? "label " : "addr-of-val ");
286 if (opVal->hasName())
287 OS << opVal->getName();
288 else
289 OS << (const void*) opVal;
290 OS << ")";
291 break;
292 }
Chris Lattner2109f502002-12-15 20:35:25 +0000293 case MachineOperand::MO_MachineBasicBlock:
Brian Gaeke988b7ba2004-06-17 22:26:53 +0000294 OS << "mbb<"
Chris Lattner2109f502002-12-15 20:35:25 +0000295 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
Brian Gaeke988b7ba2004-06-17 22:26:53 +0000296 << "," << (void*)MO.getMachineBasicBlock() << ">";
Chris Lattner2109f502002-12-15 20:35:25 +0000297 break;
Chris Lattner10cb79b2002-12-28 20:37:37 +0000298 case MachineOperand::MO_FrameIndex:
299 OS << "<fi#" << MO.getFrameIndex() << ">";
300 break;
Chris Lattner8d95ef42003-01-13 00:23:24 +0000301 case MachineOperand::MO_ConstantPoolIndex:
302 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
303 break;
304 case MachineOperand::MO_GlobalAddress:
Chris Lattnerca4f6eb2004-10-15 04:38:41 +0000305 OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
306 if (MO.getOffset()) OS << "+" << MO.getOffset();
307 OS << ">";
Chris Lattner8d95ef42003-01-13 00:23:24 +0000308 break;
309 case MachineOperand::MO_ExternalSymbol:
Chris Lattnerca4f6eb2004-10-15 04:38:41 +0000310 OS << "<es:" << MO.getSymbolName();
311 if (MO.getOffset()) OS << "+" << MO.getOffset();
312 OS << ">";
Chris Lattner8d95ef42003-01-13 00:23:24 +0000313 break;
Chris Lattner10491642002-10-30 00:48:05 +0000314 default:
315 assert(0 && "Unrecognized operand type");
316 }
317
318 if (CloseParen)
319 OS << ")";
320}
321
Tanya Lattnerb1407622004-06-25 00:13:11 +0000322void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
Chris Lattner6a592272002-10-30 01:55:38 +0000323 unsigned StartOp = 0;
Chris Lattner10491642002-10-30 00:48:05 +0000324
Chris Lattner6a592272002-10-30 01:55:38 +0000325 // Specialize printing if op#0 is definition
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000326 if (getNumOperands() && getOperand(0).isDef() && !getOperand(0).isUse()) {
Chris Lattner0742b592004-02-23 18:38:20 +0000327 ::print(getOperand(0), OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +0000328 OS << " = ";
329 ++StartOp; // Don't print this operand again!
330 }
Tanya Lattnerb1407622004-06-25 00:13:11 +0000331
Misha Brukmance22e762004-07-09 14:45:17 +0000332 // Must check if Target machine is not null because machine BB could not
333 // be attached to a Machine function yet
334 if (TM)
Tanya Lattnerb1407622004-06-25 00:13:11 +0000335 OS << TM->getInstrInfo()->getName(getOpcode());
Misha Brukmanedf128a2005-04-21 22:36:52 +0000336
Chris Lattner6a592272002-10-30 01:55:38 +0000337 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000338 const MachineOperand& mop = getOperand(i);
Chris Lattner6a592272002-10-30 01:55:38 +0000339 if (i != StartOp)
340 OS << ",";
341 OS << " ";
Chris Lattner0742b592004-02-23 18:38:20 +0000342 ::print(mop, OS, TM);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000343
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000344 if (mop.isDef())
345 if (mop.isUse())
346 OS << "<def&use>";
347 else
348 OS << "<def>";
Chris Lattner10491642002-10-30 00:48:05 +0000349 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000350
Misha Brukman6eba07a2003-09-17 21:34:23 +0000351 // code for printing implicit references
Chris Lattner10491642002-10-30 00:48:05 +0000352 if (getNumImplicitRefs()) {
353 OS << "\tImplicitRefs: ";
Misha Brukmance22e762004-07-09 14:45:17 +0000354 for (unsigned i = 0, e = getNumImplicitRefs(); i != e; ++i) {
Chris Lattner10491642002-10-30 00:48:05 +0000355 OS << "\t";
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000356 OutputValue(OS, getImplicitRef(i));
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000357 if (getImplicitOp(i).isDef())
Misha Brukmance22e762004-07-09 14:45:17 +0000358 if (getImplicitOp(i).isUse())
359 OS << "<def&use>";
360 else
361 OS << "<def>";
Chris Lattner10491642002-10-30 00:48:05 +0000362 }
363 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000364
Chris Lattner10491642002-10-30 00:48:05 +0000365 OS << "\n";
366}
367
Chris Lattner11d1f212004-02-23 18:40:08 +0000368namespace llvm {
Chris Lattner8517e1f2004-02-19 16:17:08 +0000369std::ostream &operator<<(std::ostream &os, const MachineInstr &MI) {
370 // If the instruction is embedded into a basic block, we can find the target
371 // info for the instruction.
372 if (const MachineBasicBlock *MBB = MI.getParent()) {
373 const MachineFunction *MF = MBB->getParent();
Misha Brukmance22e762004-07-09 14:45:17 +0000374 if (MF)
Tanya Lattnerb1407622004-06-25 00:13:11 +0000375 MI.print(os, &MF->getTarget());
376 else
377 MI.print(os, 0);
Chris Lattner8517e1f2004-02-19 16:17:08 +0000378 return os;
379 }
380
381 // Otherwise, print it out in the "raw" format without symbolic register names
382 // and such.
Chris Lattner2a90ba62004-02-12 16:09:53 +0000383 os << TargetInstrDescriptors[MI.getOpcode()].Name;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000384
Misha Brukmance22e762004-07-09 14:45:17 +0000385 for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
Chris Lattner8d95ef42003-01-13 00:23:24 +0000386 os << "\t" << MI.getOperand(i);
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000387 if (MI.getOperand(i).isDef())
388 if (MI.getOperand(i).isUse())
389 os << "<d&u>";
390 else
391 os << "<d>";
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000392 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000393
Misha Brukman6eba07a2003-09-17 21:34:23 +0000394 // code for printing implicit references
Chris Lattner8d95ef42003-01-13 00:23:24 +0000395 unsigned NumOfImpRefs = MI.getNumImplicitRefs();
396 if (NumOfImpRefs > 0) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000397 os << "\tImplicit: ";
Misha Brukmance22e762004-07-09 14:45:17 +0000398 for (unsigned z = 0; z < NumOfImpRefs; z++) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000399 OutputValue(os, MI.getImplicitRef(z));
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000400 if (MI.getImplicitOp(z).isDef())
401 if (MI.getImplicitOp(z).isUse())
402 os << "<d&u>";
403 else
404 os << "<d>";
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000405 os << "\t";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000406 }
407 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000408
Chris Lattner697954c2002-01-20 22:54:45 +0000409 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000410}
411
Brian Gaeke21326fc2004-02-13 04:39:32 +0000412std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) {
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000413 if (MO.isHiBits32())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000414 OS << "%lm(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000415 else if (MO.isLoBits32())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000416 OS << "%lo(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000417 else if (MO.isHiBits64())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000418 OS << "%hh(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000419 else if (MO.isLoBits64())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000420 OS << "%hm(";
Misha Brukmanedf128a2005-04-21 22:36:52 +0000421
Misha Brukmance22e762004-07-09 14:45:17 +0000422 switch (MO.getType()) {
423 case MachineOperand::MO_VirtualRegister:
424 if (MO.hasAllocatedReg())
425 OutputReg(OS, MO.getReg());
Chris Lattner8d95ef42003-01-13 00:23:24 +0000426
Misha Brukmance22e762004-07-09 14:45:17 +0000427 if (MO.getVRegValue()) {
428 if (MO.hasAllocatedReg()) OS << "==";
429 OS << "%vreg";
Chris Lattner10cb79b2002-12-28 20:37:37 +0000430 OutputValue(OS, MO.getVRegValue());
Vikram S. Adve6e447182001-09-18 12:56:28 +0000431 }
Misha Brukmance22e762004-07-09 14:45:17 +0000432 break;
433 case MachineOperand::MO_CCRegister:
434 OS << "%ccreg";
435 OutputValue(OS, MO.getVRegValue());
436 if (MO.hasAllocatedReg()) {
437 OS << "==";
438 OutputReg(OS, MO.getReg());
439 }
440 break;
441 case MachineOperand::MO_MachineRegister:
442 OutputReg(OS, MO.getMachineRegNum());
443 break;
444 case MachineOperand::MO_SignExtendedImmed:
445 OS << (long)MO.getImmedValue();
446 break;
447 case MachineOperand::MO_UnextendedImmed:
448 OS << (long)MO.getImmedValue();
449 break;
450 case MachineOperand::MO_PCRelativeDisp: {
451 const Value* opVal = MO.getVRegValue();
452 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
453 OS << "%disp(" << (isLabel? "label " : "addr-of-val ");
454 if (opVal->hasName())
455 OS << opVal->getName();
456 else
457 OS << (const void*) opVal;
458 OS << ")";
459 break;
460 }
461 case MachineOperand::MO_MachineBasicBlock:
462 OS << "<mbb:"
463 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
464 << "@" << (void*)MO.getMachineBasicBlock() << ">";
465 break;
466 case MachineOperand::MO_FrameIndex:
467 OS << "<fi#" << MO.getFrameIndex() << ">";
468 break;
469 case MachineOperand::MO_ConstantPoolIndex:
470 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
471 break;
472 case MachineOperand::MO_GlobalAddress:
473 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
474 break;
475 case MachineOperand::MO_ExternalSymbol:
476 OS << "<es:" << MO.getSymbolName() << ">";
477 break;
478 default:
479 assert(0 && "Unrecognized operand type");
480 break;
481 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000482
Chris Lattner0742b592004-02-23 18:38:20 +0000483 if (MO.isHiBits32() || MO.isLoBits32() || MO.isHiBits64() || MO.isLoBits64())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000484 OS << ")";
Misha Brukmanedf128a2005-04-21 22:36:52 +0000485
Chris Lattner10cb79b2002-12-28 20:37:37 +0000486 return OS;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000487}
Brian Gaeked0fde302003-11-11 22:41:34 +0000488
Chris Lattner11d1f212004-02-23 18:40:08 +0000489}