blob: 34a2a7d62842aafb2aa7d18b7755b957fe2fa276 [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
Chris Lattner48d7c062006-04-17 21:35:41 +0000103/// removeFromParent - This method unlinks 'this' from the containing basic
104/// block, and returns it, but does not delete it.
105MachineInstr *MachineInstr::removeFromParent() {
106 assert(getParent() && "Not embedded in a basic block!");
107 getParent()->remove(this);
108 return this;
109}
110
111
Brian Gaeke21326fc2004-02-13 04:39:32 +0000112/// OperandComplete - Return true if it's illegal to add a new operand
113///
Chris Lattner2a90ba62004-02-12 16:09:53 +0000114bool MachineInstr::OperandsComplete() const {
115 int NumOperands = TargetInstrDescriptors[Opcode].numOperands;
Vikram S. Advea2bae302002-10-29 19:41:18 +0000116 if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000117 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000118 return false;
119}
120
Brian Gaeke21326fc2004-02-13 04:39:32 +0000121/// replace - Support for replacing opcode and operands of a MachineInstr in
122/// place. This only resets the size of the operand vector and initializes it.
123/// The new operands must be set explicitly later.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000124///
Alkis Evlogimenosab8672c2004-02-12 18:49:07 +0000125void MachineInstr::replace(short opcode, unsigned numOperands) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000126 assert(getNumImplicitRefs() == 0 &&
127 "This is probably broken because implicit refs are going to be lost.");
Chris Lattner2a90ba62004-02-12 16:09:53 +0000128 Opcode = opcode;
Vikram S. Advee8b57ef2002-09-20 00:47:49 +0000129 operands.clear();
Chris Lattner413746e2002-10-28 20:48:39 +0000130 operands.resize(numOperands, MachineOperand());
Vikram S. Advee8b57ef2002-09-20 00:47:49 +0000131}
132
Chris Lattnera2dd7452003-08-05 16:58:46 +0000133void MachineInstr::SetMachineOperandVal(unsigned i,
134 MachineOperand::MachineOperandType opTy,
135 Value* V) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000136 assert(i < operands.size()); // may be explicit or implicit op
Chris Lattnera2dd7452003-08-05 16:58:46 +0000137 operands[i].opType = opTy;
Brian Gaekec5483952004-03-03 19:07:27 +0000138 operands[i].contents.value = V;
Chris Lattnerca4f6eb2004-10-15 04:38:41 +0000139 operands[i].extra.regNum = -1;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000140}
141
142void
Chris Lattner572f5c82002-10-28 04:24:49 +0000143MachineInstr::SetMachineOperandConst(unsigned i,
Brian Gaeke21326fc2004-02-13 04:39:32 +0000144 MachineOperand::MachineOperandType opTy,
Chris Lattner561c0102004-02-29 05:07:02 +0000145 int intValue) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000146 assert(i < getNumOperands()); // must be explicit op
Chris Lattner2a90ba62004-02-12 16:09:53 +0000147 assert(TargetInstrDescriptors[Opcode].resultPos != (int) i &&
Vikram S. Advec356e562002-03-18 03:35:24 +0000148 "immed. constant cannot be defined");
Chris Lattner572f5c82002-10-28 04:24:49 +0000149
Brian Gaeke21326fc2004-02-13 04:39:32 +0000150 operands[i].opType = opTy;
Brian Gaekec5483952004-03-03 19:07:27 +0000151 operands[i].contents.value = NULL;
152 operands[i].contents.immedVal = intValue;
Chris Lattnerca4f6eb2004-10-15 04:38:41 +0000153 operands[i].extra.regNum = -1;
Chris Lattner572f5c82002-10-28 04:24:49 +0000154 operands[i].flags = 0;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000155}
156
Chris Lattnera2dd7452003-08-05 16:58:46 +0000157void MachineInstr::SetMachineOperandReg(unsigned i, int regNum) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000158 assert(i < getNumOperands()); // must be explicit op
Chris Lattner572f5c82002-10-28 04:24:49 +0000159
Chris Lattner2f305982002-10-28 19:46:59 +0000160 operands[i].opType = MachineOperand::MO_MachineRegister;
Brian Gaekec5483952004-03-03 19:07:27 +0000161 operands[i].contents.value = NULL;
Chris Lattnerca4f6eb2004-10-15 04:38:41 +0000162 operands[i].extra.regNum = regNum;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000163}
164
Brian Gaeke21326fc2004-02-13 04:39:32 +0000165// Used only by the SPARC back-end.
Chris Lattner2a90ba62004-02-12 16:09:53 +0000166void MachineInstr::SetRegForOperand(unsigned i, int regNum) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000167 assert(i < getNumOperands()); // must be explicit op
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000168 operands[i].setRegForValue(regNum);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000169}
170
Brian Gaeke21326fc2004-02-13 04:39:32 +0000171// Used only by the SPARC back-end.
Chris Lattner2a90ba62004-02-12 16:09:53 +0000172void MachineInstr::SetRegForImplicitRef(unsigned i, int regNum) {
Vikram S. Adve34977822003-05-31 07:39:06 +0000173 getImplicitOp(i).setRegForValue(regNum);
Vikram S. Adve34977822003-05-31 07:39:06 +0000174}
175
Brian Gaeke21326fc2004-02-13 04:39:32 +0000176/// substituteValue - Substitute all occurrences of Value* oldVal with newVal
177/// in all operands and all implicit refs. If defsOnly == true, substitute defs
178/// only.
179///
180/// FIXME: Fold this into its single caller, at SparcInstrSelection.cpp:2865,
181/// or make it a static function in that file.
182///
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000183unsigned
Vikram S. Adve627eb312003-07-10 19:45:07 +0000184MachineInstr::substituteValue(const Value* oldVal, Value* newVal,
185 bool defsOnly, bool notDefsAndUses,
186 bool& someArgsWereIgnored)
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000187{
Vikram S. Adve2010f7b2003-08-07 15:01:48 +0000188 assert((!defsOnly || !notDefsAndUses) &&
189 "notDefsAndUses is irrelevant if defsOnly == true.");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000190
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000191 unsigned numSubst = 0;
192
Misha Brukman6eba07a2003-09-17 21:34:23 +0000193 // Substitute operands
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000194 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
195 if (*O == oldVal)
Vikram S. Adve627eb312003-07-10 19:45:07 +0000196 if (!defsOnly ||
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000197 notDefsAndUses && (O.isDef() && !O.isUse()) ||
198 !notDefsAndUses && O.isDef())
Misha Brukmance22e762004-07-09 14:45:17 +0000199 {
200 O.getMachineOperand().contents.value = newVal;
201 ++numSubst;
202 } else
Vikram S. Adve627eb312003-07-10 19:45:07 +0000203 someArgsWereIgnored = true;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000204
Misha Brukman6eba07a2003-09-17 21:34:23 +0000205 // Substitute implicit refs
Misha Brukmance22e762004-07-09 14:45:17 +0000206 for (unsigned i = 0, N = getNumImplicitRefs(); i < N; ++i)
207 if (getImplicitRef(i) == oldVal) {
208 MachineOperand Op = getImplicitOp(i);
Vikram S. Adve627eb312003-07-10 19:45:07 +0000209 if (!defsOnly ||
Misha Brukmance22e762004-07-09 14:45:17 +0000210 notDefsAndUses && (Op.isDef() && !Op.isUse()) ||
211 !notDefsAndUses && Op.isDef())
212 {
213 Op.contents.value = newVal;
214 ++numSubst;
215 } else
Vikram S. Adve627eb312003-07-10 19:45:07 +0000216 someArgsWereIgnored = true;
Misha Brukmance22e762004-07-09 14:45:17 +0000217 }
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000218 return numSubst;
219}
220
Brian Gaeke21326fc2004-02-13 04:39:32 +0000221void MachineInstr::dump() const {
Chris Lattner925b7712003-08-03 20:24:29 +0000222 std::cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000223}
224
Brian Gaeke21326fc2004-02-13 04:39:32 +0000225static inline std::ostream& OutputValue(std::ostream &os, const Value* val) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000226 os << "(val ";
Misha Brukmance22e762004-07-09 14:45:17 +0000227 os << (void*) val; // print address always
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000228 if (val && val->hasName())
Misha Brukmance22e762004-07-09 14:45:17 +0000229 os << " " << val->getName(); // print name also, if available
Brian Gaeke21326fc2004-02-13 04:39:32 +0000230 os << ")";
Vikram S. Adve627eb312003-07-10 19:45:07 +0000231 return os;
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000232}
233
Chris Lattner2a79a092002-10-30 00:58:19 +0000234static inline void OutputReg(std::ostream &os, unsigned RegNo,
235 const MRegisterInfo *MRI = 0) {
Alkis Evlogimenosddcfd9e2004-02-27 01:52:34 +0000236 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
Chris Lattner8517e1f2004-02-19 16:17:08 +0000237 if (MRI)
Chris Lattner2a79a092002-10-30 00:58:19 +0000238 os << "%" << MRI->get(RegNo).Name;
239 else
Chris Lattner8517e1f2004-02-19 16:17:08 +0000240 os << "%mreg(" << RegNo << ")";
Chris Lattner2a79a092002-10-30 00:58:19 +0000241 } else
Chris Lattner8517e1f2004-02-19 16:17:08 +0000242 os << "%reg" << RegNo;
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000243}
244
Chris Lattner10491642002-10-30 00:48:05 +0000245static void print(const MachineOperand &MO, std::ostream &OS,
Tanya Lattnerb1407622004-06-25 00:13:11 +0000246 const TargetMachine *TM) {
Misha Brukmance22e762004-07-09 14:45:17 +0000247 const MRegisterInfo *MRI = 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000248
Misha Brukmance22e762004-07-09 14:45:17 +0000249 if (TM) MRI = TM->getRegisterInfo();
Tanya Lattnerb1407622004-06-25 00:13:11 +0000250
Chris Lattner10491642002-10-30 00:48:05 +0000251 bool CloseParen = true;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000252 if (MO.isHiBits32())
Chris Lattner10491642002-10-30 00:48:05 +0000253 OS << "%lm(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000254 else if (MO.isLoBits32())
Chris Lattner10491642002-10-30 00:48:05 +0000255 OS << "%lo(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000256 else if (MO.isHiBits64())
Chris Lattner10491642002-10-30 00:48:05 +0000257 OS << "%hh(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000258 else if (MO.isLoBits64())
Chris Lattner10491642002-10-30 00:48:05 +0000259 OS << "%hm(";
260 else
261 CloseParen = false;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000262
Chris Lattner10491642002-10-30 00:48:05 +0000263 switch (MO.getType()) {
264 case MachineOperand::MO_VirtualRegister:
265 if (MO.getVRegValue()) {
266 OS << "%reg";
267 OutputValue(OS, MO.getVRegValue());
268 if (MO.hasAllocatedReg())
269 OS << "==";
270 }
271 if (MO.hasAllocatedReg())
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000272 OutputReg(OS, MO.getReg(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000273 break;
274 case MachineOperand::MO_CCRegister:
275 OS << "%ccreg";
276 OutputValue(OS, MO.getVRegValue());
277 if (MO.hasAllocatedReg()) {
278 OS << "==";
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000279 OutputReg(OS, MO.getReg(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000280 }
281 break;
282 case MachineOperand::MO_MachineRegister:
Chris Lattner2a79a092002-10-30 00:58:19 +0000283 OutputReg(OS, MO.getMachineRegNum(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000284 break;
285 case MachineOperand::MO_SignExtendedImmed:
286 OS << (long)MO.getImmedValue();
287 break;
288 case MachineOperand::MO_UnextendedImmed:
289 OS << (long)MO.getImmedValue();
290 break;
291 case MachineOperand::MO_PCRelativeDisp: {
292 const Value* opVal = MO.getVRegValue();
293 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
294 OS << "%disp(" << (isLabel? "label " : "addr-of-val ");
295 if (opVal->hasName())
296 OS << opVal->getName();
297 else
298 OS << (const void*) opVal;
299 OS << ")";
300 break;
301 }
Chris Lattner2109f502002-12-15 20:35:25 +0000302 case MachineOperand::MO_MachineBasicBlock:
Brian Gaeke988b7ba2004-06-17 22:26:53 +0000303 OS << "mbb<"
Chris Lattner2109f502002-12-15 20:35:25 +0000304 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
Brian Gaeke988b7ba2004-06-17 22:26:53 +0000305 << "," << (void*)MO.getMachineBasicBlock() << ">";
Chris Lattner2109f502002-12-15 20:35:25 +0000306 break;
Chris Lattner10cb79b2002-12-28 20:37:37 +0000307 case MachineOperand::MO_FrameIndex:
308 OS << "<fi#" << MO.getFrameIndex() << ">";
309 break;
Chris Lattner8d95ef42003-01-13 00:23:24 +0000310 case MachineOperand::MO_ConstantPoolIndex:
311 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
312 break;
313 case MachineOperand::MO_GlobalAddress:
Chris Lattnerca4f6eb2004-10-15 04:38:41 +0000314 OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
315 if (MO.getOffset()) OS << "+" << MO.getOffset();
316 OS << ">";
Chris Lattner8d95ef42003-01-13 00:23:24 +0000317 break;
318 case MachineOperand::MO_ExternalSymbol:
Chris Lattnerca4f6eb2004-10-15 04:38:41 +0000319 OS << "<es:" << MO.getSymbolName();
320 if (MO.getOffset()) OS << "+" << MO.getOffset();
321 OS << ">";
Chris Lattner8d95ef42003-01-13 00:23:24 +0000322 break;
Chris Lattner10491642002-10-30 00:48:05 +0000323 default:
324 assert(0 && "Unrecognized operand type");
325 }
326
327 if (CloseParen)
328 OS << ")";
329}
330
Tanya Lattnerb1407622004-06-25 00:13:11 +0000331void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
Chris Lattner6a592272002-10-30 01:55:38 +0000332 unsigned StartOp = 0;
Chris Lattner10491642002-10-30 00:48:05 +0000333
Chris Lattner6a592272002-10-30 01:55:38 +0000334 // Specialize printing if op#0 is definition
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000335 if (getNumOperands() && getOperand(0).isDef() && !getOperand(0).isUse()) {
Chris Lattner0742b592004-02-23 18:38:20 +0000336 ::print(getOperand(0), OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +0000337 OS << " = ";
338 ++StartOp; // Don't print this operand again!
339 }
Tanya Lattnerb1407622004-06-25 00:13:11 +0000340
Misha Brukmance22e762004-07-09 14:45:17 +0000341 // Must check if Target machine is not null because machine BB could not
342 // be attached to a Machine function yet
343 if (TM)
Tanya Lattnerb1407622004-06-25 00:13:11 +0000344 OS << TM->getInstrInfo()->getName(getOpcode());
Misha Brukmanedf128a2005-04-21 22:36:52 +0000345
Chris Lattner6a592272002-10-30 01:55:38 +0000346 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000347 const MachineOperand& mop = getOperand(i);
Chris Lattner6a592272002-10-30 01:55:38 +0000348 if (i != StartOp)
349 OS << ",";
350 OS << " ";
Chris Lattner0742b592004-02-23 18:38:20 +0000351 ::print(mop, OS, TM);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000352
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000353 if (mop.isDef())
354 if (mop.isUse())
355 OS << "<def&use>";
356 else
357 OS << "<def>";
Chris Lattner10491642002-10-30 00:48:05 +0000358 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000359
Misha Brukman6eba07a2003-09-17 21:34:23 +0000360 // code for printing implicit references
Chris Lattner10491642002-10-30 00:48:05 +0000361 if (getNumImplicitRefs()) {
362 OS << "\tImplicitRefs: ";
Misha Brukmance22e762004-07-09 14:45:17 +0000363 for (unsigned i = 0, e = getNumImplicitRefs(); i != e; ++i) {
Chris Lattner10491642002-10-30 00:48:05 +0000364 OS << "\t";
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000365 OutputValue(OS, getImplicitRef(i));
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000366 if (getImplicitOp(i).isDef())
Misha Brukmance22e762004-07-09 14:45:17 +0000367 if (getImplicitOp(i).isUse())
368 OS << "<def&use>";
369 else
370 OS << "<def>";
Chris Lattner10491642002-10-30 00:48:05 +0000371 }
372 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000373
Chris Lattner10491642002-10-30 00:48:05 +0000374 OS << "\n";
375}
376
Chris Lattner11d1f212004-02-23 18:40:08 +0000377namespace llvm {
Chris Lattner8517e1f2004-02-19 16:17:08 +0000378std::ostream &operator<<(std::ostream &os, const MachineInstr &MI) {
379 // If the instruction is embedded into a basic block, we can find the target
380 // info for the instruction.
381 if (const MachineBasicBlock *MBB = MI.getParent()) {
382 const MachineFunction *MF = MBB->getParent();
Misha Brukmance22e762004-07-09 14:45:17 +0000383 if (MF)
Tanya Lattnerb1407622004-06-25 00:13:11 +0000384 MI.print(os, &MF->getTarget());
385 else
386 MI.print(os, 0);
Chris Lattner8517e1f2004-02-19 16:17:08 +0000387 return os;
388 }
389
390 // Otherwise, print it out in the "raw" format without symbolic register names
391 // and such.
Chris Lattner2a90ba62004-02-12 16:09:53 +0000392 os << TargetInstrDescriptors[MI.getOpcode()].Name;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000393
Misha Brukmance22e762004-07-09 14:45:17 +0000394 for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
Chris Lattner8d95ef42003-01-13 00:23:24 +0000395 os << "\t" << MI.getOperand(i);
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000396 if (MI.getOperand(i).isDef())
397 if (MI.getOperand(i).isUse())
398 os << "<d&u>";
399 else
400 os << "<d>";
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000401 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000402
Misha Brukman6eba07a2003-09-17 21:34:23 +0000403 // code for printing implicit references
Chris Lattner8d95ef42003-01-13 00:23:24 +0000404 unsigned NumOfImpRefs = MI.getNumImplicitRefs();
405 if (NumOfImpRefs > 0) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000406 os << "\tImplicit: ";
Misha Brukmance22e762004-07-09 14:45:17 +0000407 for (unsigned z = 0; z < NumOfImpRefs; z++) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000408 OutputValue(os, MI.getImplicitRef(z));
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000409 if (MI.getImplicitOp(z).isDef())
410 if (MI.getImplicitOp(z).isUse())
411 os << "<d&u>";
412 else
413 os << "<d>";
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000414 os << "\t";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000415 }
416 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000417
Chris Lattner697954c2002-01-20 22:54:45 +0000418 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000419}
420
Brian Gaeke21326fc2004-02-13 04:39:32 +0000421std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) {
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000422 if (MO.isHiBits32())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000423 OS << "%lm(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000424 else if (MO.isLoBits32())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000425 OS << "%lo(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000426 else if (MO.isHiBits64())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000427 OS << "%hh(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000428 else if (MO.isLoBits64())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000429 OS << "%hm(";
Misha Brukmanedf128a2005-04-21 22:36:52 +0000430
Misha Brukmance22e762004-07-09 14:45:17 +0000431 switch (MO.getType()) {
432 case MachineOperand::MO_VirtualRegister:
433 if (MO.hasAllocatedReg())
434 OutputReg(OS, MO.getReg());
Chris Lattner8d95ef42003-01-13 00:23:24 +0000435
Misha Brukmance22e762004-07-09 14:45:17 +0000436 if (MO.getVRegValue()) {
437 if (MO.hasAllocatedReg()) OS << "==";
438 OS << "%vreg";
Chris Lattner10cb79b2002-12-28 20:37:37 +0000439 OutputValue(OS, MO.getVRegValue());
Vikram S. Adve6e447182001-09-18 12:56:28 +0000440 }
Misha Brukmance22e762004-07-09 14:45:17 +0000441 break;
442 case MachineOperand::MO_CCRegister:
443 OS << "%ccreg";
444 OutputValue(OS, MO.getVRegValue());
445 if (MO.hasAllocatedReg()) {
446 OS << "==";
447 OutputReg(OS, MO.getReg());
448 }
449 break;
450 case MachineOperand::MO_MachineRegister:
451 OutputReg(OS, MO.getMachineRegNum());
452 break;
453 case MachineOperand::MO_SignExtendedImmed:
454 OS << (long)MO.getImmedValue();
455 break;
456 case MachineOperand::MO_UnextendedImmed:
457 OS << (long)MO.getImmedValue();
458 break;
459 case MachineOperand::MO_PCRelativeDisp: {
460 const Value* opVal = MO.getVRegValue();
461 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
462 OS << "%disp(" << (isLabel? "label " : "addr-of-val ");
463 if (opVal->hasName())
464 OS << opVal->getName();
465 else
466 OS << (const void*) opVal;
467 OS << ")";
468 break;
469 }
470 case MachineOperand::MO_MachineBasicBlock:
471 OS << "<mbb:"
472 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
473 << "@" << (void*)MO.getMachineBasicBlock() << ">";
474 break;
475 case MachineOperand::MO_FrameIndex:
476 OS << "<fi#" << MO.getFrameIndex() << ">";
477 break;
478 case MachineOperand::MO_ConstantPoolIndex:
479 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
480 break;
481 case MachineOperand::MO_GlobalAddress:
482 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
483 break;
484 case MachineOperand::MO_ExternalSymbol:
485 OS << "<es:" << MO.getSymbolName() << ">";
486 break;
487 default:
488 assert(0 && "Unrecognized operand type");
489 break;
490 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000491
Chris Lattner0742b592004-02-23 18:38:20 +0000492 if (MO.isHiBits32() || MO.isLoBits32() || MO.isHiBits64() || MO.isLoBits64())
Chris Lattner10cb79b2002-12-28 20:37:37 +0000493 OS << ")";
Misha Brukmanedf128a2005-04-21 22:36:52 +0000494
Chris Lattner10cb79b2002-12-28 20:37:37 +0000495 return OS;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000496}
Brian Gaeked0fde302003-11-11 22:41:34 +0000497
Chris Lattner11d1f212004-02-23 18:40:08 +0000498}