blob: 495da5edfcfb353cfb17c72cd6288a2a616bd65e [file] [log] [blame]
Chris Lattner959a5fb2002-08-09 20:08:06 +00001//===-- MachineInstr.cpp --------------------------------------------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-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 Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaekee8f7c2f2004-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 Lattner959a5fb2002-08-09 20:08:06 +000015//===----------------------------------------------------------------------===//
Vikram S. Adveab9e5572001-07-21 12:41:50 +000016
Chris Lattner23fcc082001-09-07 17:18:30 +000017#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner63f41ab2004-02-19 16:17:08 +000018#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner4cec1472002-02-03 07:46:01 +000019#include "llvm/Value.h"
Chris Lattner214808f2002-10-30 00:48:05 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattnerb4d58d72003-01-14 22:00:31 +000021#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner4e9fb1f2002-10-30 00:58:19 +000022#include "llvm/Target/MRegisterInfo.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/Support/LeakDetector.h"
Reid Spencereb04d9b2004-07-04 12:19:56 +000024#include <iostream>
25
Chris Lattner43df6c22004-02-23 18:38:20 +000026using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000027
Chris Lattner07e26832002-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 Lattnerb4d58d72003-01-14 22:00:31 +000030// This variable is initialized and reset by class TargetInstrInfo.
Misha Brukman835702a2005-04-21 22:36:52 +000031//
Chris Lattner07e26832002-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 Lattner91a7dc02004-02-23 18:40:08 +000035namespace llvm {
Chris Lattner43df6c22004-02-23 18:38:20 +000036 extern const TargetInstrDescriptor *TargetInstrDescriptors;
37}
Ruchira Sasanka59e864e2001-10-18 22:40:02 +000038
Chris Lattner27ccb702002-10-29 23:19:00 +000039/// MachineInstr ctor - This constructor only does a _reserve_ of the operands,
40/// not a resize for them. It is expected that if you use this that you call
41/// add* methods below to fill up the operands, instead of the Set methods.
42/// Eventually, the "resizing" ctors will be phased out.
43///
Chris Lattner469647b2006-05-04 18:16:01 +000044MachineInstr::MachineInstr(short opcode, unsigned numOperands)
Chris Lattnera38c3582006-04-20 18:08:53 +000045 : Opcode(opcode), parent(0) {
Chris Lattner53af9da2006-05-04 19:14:44 +000046 Operands.reserve(numOperands);
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +000047 // Make sure that we get added to a machine basicblock
48 LeakDetector::addGarbageObject(this);
Chris Lattner307fb1a2002-10-28 20:59:49 +000049}
50
Chris Lattner27ccb702002-10-29 23:19:00 +000051/// MachineInstr ctor - Work exactly the same as the ctor above, except that the
52/// MachineInstr is created and added to the end of the specified basic block.
53///
Alkis Evlogimenosde8ac742004-02-12 18:49:07 +000054MachineInstr::MachineInstr(MachineBasicBlock *MBB, short opcode,
Chris Lattner27ccb702002-10-29 23:19:00 +000055 unsigned numOperands)
Chris Lattnera38c3582006-04-20 18:08:53 +000056 : Opcode(opcode), parent(0) {
Chris Lattner27ccb702002-10-29 23:19:00 +000057 assert(MBB && "Cannot use inserting ctor with null basic block!");
Chris Lattner53af9da2006-05-04 19:14:44 +000058 Operands.reserve(numOperands);
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +000059 // Make sure that we get added to a machine basicblock
60 LeakDetector::addGarbageObject(this);
Chris Lattner27ccb702002-10-29 23:19:00 +000061 MBB->push_back(this); // Add instruction to end of basic block!
62}
63
Misha Brukmanb47ab7a2004-07-09 14:45:17 +000064/// MachineInstr ctor - Copies MachineInstr arg exactly
65///
Tanya Lattnere6a4a7d2004-05-23 19:35:12 +000066MachineInstr::MachineInstr(const MachineInstr &MI) {
67 Opcode = MI.getOpcode();
Chris Lattner53af9da2006-05-04 19:14:44 +000068 Operands.reserve(MI.getNumOperands());
Tanya Lattner9953d862004-05-23 20:58:02 +000069
Misha Brukmanb47ab7a2004-07-09 14:45:17 +000070 // Add operands
Chris Lattner53af9da2006-05-04 19:14:44 +000071 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
72 Operands.push_back(MI.getOperand(i));
Tanya Lattnerbcee21b2004-05-24 03:14:18 +000073
Misha Brukmanb47ab7a2004-07-09 14:45:17 +000074 // Set parent, next, and prev to null
Tanya Lattnerbcee21b2004-05-24 03:14:18 +000075 parent = 0;
76 prev = 0;
77 next = 0;
Tanya Lattnere6a4a7d2004-05-23 19:35:12 +000078}
79
80
Misha Brukmanb47ab7a2004-07-09 14:45:17 +000081MachineInstr::~MachineInstr() {
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +000082 LeakDetector::removeGarbageObject(this);
83}
84
Chris Lattnerbec79b42006-04-17 21:35:41 +000085/// removeFromParent - This method unlinks 'this' from the containing basic
86/// block, and returns it, but does not delete it.
87MachineInstr *MachineInstr::removeFromParent() {
88 assert(getParent() && "Not embedded in a basic block!");
89 getParent()->remove(this);
90 return this;
91}
92
93
Brian Gaekee8f7c2f2004-02-13 04:39:32 +000094/// OperandComplete - Return true if it's illegal to add a new operand
95///
Chris Lattner6a597d62004-02-12 16:09:53 +000096bool MachineInstr::OperandsComplete() const {
97 int NumOperands = TargetInstrDescriptors[Opcode].numOperands;
Vikram S. Advea1b4f0f2002-10-29 19:41:18 +000098 if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands)
Vikram S. Advec4688822003-05-31 07:39:06 +000099 return true; // Broken: we have all the operands of this instruction!
Chris Lattnerca4a9d22002-10-28 20:48:39 +0000100 return false;
101}
102
Brian Gaekee8f7c2f2004-02-13 04:39:32 +0000103void MachineInstr::dump() const {
Chris Lattner1789d232003-08-03 20:24:29 +0000104 std::cerr << " " << *this;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000105}
106
Chris Lattner4e9fb1f2002-10-30 00:58:19 +0000107static inline void OutputReg(std::ostream &os, unsigned RegNo,
108 const MRegisterInfo *MRI = 0) {
Alkis Evlogimenos519e1e82004-02-27 01:52:34 +0000109 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
Chris Lattner63f41ab2004-02-19 16:17:08 +0000110 if (MRI)
Chris Lattner4e9fb1f2002-10-30 00:58:19 +0000111 os << "%" << MRI->get(RegNo).Name;
112 else
Chris Lattner63f41ab2004-02-19 16:17:08 +0000113 os << "%mreg(" << RegNo << ")";
Chris Lattner4e9fb1f2002-10-30 00:58:19 +0000114 } else
Chris Lattner63f41ab2004-02-19 16:17:08 +0000115 os << "%reg" << RegNo;
Vikram S. Adve75113022002-09-16 15:18:53 +0000116}
117
Chris Lattner214808f2002-10-30 00:48:05 +0000118static void print(const MachineOperand &MO, std::ostream &OS,
Tanya Lattner23dbc812004-06-25 00:13:11 +0000119 const TargetMachine *TM) {
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000120 const MRegisterInfo *MRI = 0;
Misha Brukman835702a2005-04-21 22:36:52 +0000121
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000122 if (TM) MRI = TM->getRegisterInfo();
Tanya Lattner23dbc812004-06-25 00:13:11 +0000123
Chris Lattner214808f2002-10-30 00:48:05 +0000124 switch (MO.getType()) {
Chris Lattner10b71c02006-05-04 18:05:43 +0000125 case MachineOperand::MO_Register:
Chris Lattneree64b6b2006-05-04 01:26:39 +0000126 OutputReg(OS, MO.getReg(), MRI);
Chris Lattner214808f2002-10-30 00:48:05 +0000127 break;
Chris Lattnerfef7a2d2006-05-04 17:21:20 +0000128 case MachineOperand::MO_Immediate:
Chris Lattner214808f2002-10-30 00:48:05 +0000129 OS << (long)MO.getImmedValue();
130 break;
Chris Lattnerf8954182002-12-15 20:35:25 +0000131 case MachineOperand::MO_MachineBasicBlock:
Brian Gaeke4300ca92004-06-17 22:26:53 +0000132 OS << "mbb<"
Chris Lattnerf8954182002-12-15 20:35:25 +0000133 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
Brian Gaeke4300ca92004-06-17 22:26:53 +0000134 << "," << (void*)MO.getMachineBasicBlock() << ">";
Chris Lattnerf8954182002-12-15 20:35:25 +0000135 break;
Chris Lattnerfd1ecad2002-12-28 20:37:37 +0000136 case MachineOperand::MO_FrameIndex:
137 OS << "<fi#" << MO.getFrameIndex() << ">";
138 break;
Chris Lattnera2ad8742003-01-13 00:23:24 +0000139 case MachineOperand::MO_ConstantPoolIndex:
140 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
141 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000142 case MachineOperand::MO_JumpTableIndex:
143 OS << "<jt#" << MO.getJumpTableIndex() << ">";
144 break;
Chris Lattnera2ad8742003-01-13 00:23:24 +0000145 case MachineOperand::MO_GlobalAddress:
Chris Lattner30652202004-10-15 04:38:41 +0000146 OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
147 if (MO.getOffset()) OS << "+" << MO.getOffset();
148 OS << ">";
Chris Lattnera2ad8742003-01-13 00:23:24 +0000149 break;
150 case MachineOperand::MO_ExternalSymbol:
Chris Lattner30652202004-10-15 04:38:41 +0000151 OS << "<es:" << MO.getSymbolName();
152 if (MO.getOffset()) OS << "+" << MO.getOffset();
153 OS << ">";
Chris Lattnera2ad8742003-01-13 00:23:24 +0000154 break;
Chris Lattner214808f2002-10-30 00:48:05 +0000155 default:
156 assert(0 && "Unrecognized operand type");
157 }
Chris Lattner214808f2002-10-30 00:48:05 +0000158}
159
Tanya Lattner23dbc812004-06-25 00:13:11 +0000160void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
Chris Lattnerac6e9742002-10-30 01:55:38 +0000161 unsigned StartOp = 0;
Chris Lattner214808f2002-10-30 00:48:05 +0000162
Chris Lattnerac6e9742002-10-30 01:55:38 +0000163 // Specialize printing if op#0 is definition
Alkis Evlogimenosaaba4632003-12-14 13:24:17 +0000164 if (getNumOperands() && getOperand(0).isDef() && !getOperand(0).isUse()) {
Chris Lattner43df6c22004-02-23 18:38:20 +0000165 ::print(getOperand(0), OS, TM);
Chris Lattnerac6e9742002-10-30 01:55:38 +0000166 OS << " = ";
167 ++StartOp; // Don't print this operand again!
168 }
Tanya Lattner23dbc812004-06-25 00:13:11 +0000169
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000170 // Must check if Target machine is not null because machine BB could not
171 // be attached to a Machine function yet
172 if (TM)
Tanya Lattner23dbc812004-06-25 00:13:11 +0000173 OS << TM->getInstrInfo()->getName(getOpcode());
Misha Brukman835702a2005-04-21 22:36:52 +0000174
Chris Lattnerac6e9742002-10-30 01:55:38 +0000175 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Vikram S. Adve7366fa12003-05-27 00:05:23 +0000176 const MachineOperand& mop = getOperand(i);
Chris Lattnerac6e9742002-10-30 01:55:38 +0000177 if (i != StartOp)
178 OS << ",";
179 OS << " ";
Chris Lattner43df6c22004-02-23 18:38:20 +0000180 ::print(mop, OS, TM);
Misha Brukman835702a2005-04-21 22:36:52 +0000181
Alkis Evlogimenosaaba4632003-12-14 13:24:17 +0000182 if (mop.isDef())
183 if (mop.isUse())
184 OS << "<def&use>";
185 else
186 OS << "<def>";
Chris Lattner214808f2002-10-30 00:48:05 +0000187 }
Misha Brukman835702a2005-04-21 22:36:52 +0000188
Chris Lattner214808f2002-10-30 00:48:05 +0000189 OS << "\n";
190}
191
Chris Lattner6e663f12006-05-04 00:49:59 +0000192std::ostream &llvm::operator<<(std::ostream &os, const MachineInstr &MI) {
Chris Lattner63f41ab2004-02-19 16:17:08 +0000193 // If the instruction is embedded into a basic block, we can find the target
194 // info for the instruction.
195 if (const MachineBasicBlock *MBB = MI.getParent()) {
196 const MachineFunction *MF = MBB->getParent();
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000197 if (MF)
Tanya Lattner23dbc812004-06-25 00:13:11 +0000198 MI.print(os, &MF->getTarget());
199 else
200 MI.print(os, 0);
Chris Lattner63f41ab2004-02-19 16:17:08 +0000201 return os;
202 }
203
204 // Otherwise, print it out in the "raw" format without symbolic register names
205 // and such.
Chris Lattner6a597d62004-02-12 16:09:53 +0000206 os << TargetInstrDescriptors[MI.getOpcode()].Name;
Misha Brukman835702a2005-04-21 22:36:52 +0000207
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000208 for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
Chris Lattnera2ad8742003-01-13 00:23:24 +0000209 os << "\t" << MI.getOperand(i);
Alkis Evlogimenosaaba4632003-12-14 13:24:17 +0000210 if (MI.getOperand(i).isDef())
211 if (MI.getOperand(i).isUse())
212 os << "<d&u>";
213 else
214 os << "<d>";
Ruchira Sasanka64f75672001-11-14 20:05:23 +0000215 }
Misha Brukman835702a2005-04-21 22:36:52 +0000216
Chris Lattner7f74a562002-01-20 22:54:45 +0000217 return os << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000218}
219
Chris Lattner6e663f12006-05-04 00:49:59 +0000220std::ostream &llvm::operator<<(std::ostream &OS, const MachineOperand &MO) {
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000221 switch (MO.getType()) {
Chris Lattner10b71c02006-05-04 18:05:43 +0000222 case MachineOperand::MO_Register:
Chris Lattneree64b6b2006-05-04 01:26:39 +0000223 OutputReg(OS, MO.getReg());
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000224 break;
Chris Lattnerfef7a2d2006-05-04 17:21:20 +0000225 case MachineOperand::MO_Immediate:
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000226 OS << (long)MO.getImmedValue();
227 break;
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000228 case MachineOperand::MO_MachineBasicBlock:
229 OS << "<mbb:"
230 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
231 << "@" << (void*)MO.getMachineBasicBlock() << ">";
232 break;
233 case MachineOperand::MO_FrameIndex:
234 OS << "<fi#" << MO.getFrameIndex() << ">";
235 break;
236 case MachineOperand::MO_ConstantPoolIndex:
237 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
238 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000239 case MachineOperand::MO_JumpTableIndex:
240 OS << "<jt#" << MO.getJumpTableIndex() << ">";
241 break;
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000242 case MachineOperand::MO_GlobalAddress:
243 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
244 break;
245 case MachineOperand::MO_ExternalSymbol:
246 OS << "<es:" << MO.getSymbolName() << ">";
247 break;
248 default:
249 assert(0 && "Unrecognized operand type");
250 break;
251 }
Misha Brukman835702a2005-04-21 22:36:52 +0000252
Chris Lattnerfd1ecad2002-12-28 20:37:37 +0000253 return OS;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000254}