blob: 99bc22230c30348b5af845bc0286114e5f20aa50 [file] [log] [blame]
Chris Lattnere138b3d2008-01-01 20:36:19 +00001//===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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//
Chris Lattner035dfbe2002-08-09 20:08:06 +000012//===----------------------------------------------------------------------===//
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000013
Chris Lattner822b4fb2001-09-07 17:18:30 +000014#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000015#include "llvm/Value.h"
Chris Lattner8517e1f2004-02-19 16:17:08 +000016#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner62ed6b92008-01-01 01:12:31 +000017#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner10491642002-10-30 00:48:05 +000018#include "llvm/Target/TargetMachine.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000019#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner2a79a092002-10-30 00:58:19 +000020#include "llvm/Target/MRegisterInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Support/LeakDetector.h"
Bill Wendlinga09362e2006-11-28 22:48:48 +000022#include "llvm/Support/Streams.h"
Jeff Cohenc21c5ee2006-12-15 22:57:14 +000023#include <ostream>
Chris Lattner0742b592004-02-23 18:38:20 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattnerf7382302007-12-30 21:56:09 +000026//===----------------------------------------------------------------------===//
27// MachineOperand Implementation
28//===----------------------------------------------------------------------===//
29
Chris Lattner62ed6b92008-01-01 01:12:31 +000030/// AddRegOperandToRegInfo - Add this register operand to the specified
31/// MachineRegisterInfo. If it is null, then the next/prev fields should be
32/// explicitly nulled out.
33void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) {
34 assert(isReg() && "Can only add reg operand to use lists");
35
36 // If the reginfo pointer is null, just explicitly null out or next/prev
37 // pointers, to ensure they are not garbage.
38 if (RegInfo == 0) {
39 Contents.Reg.Prev = 0;
40 Contents.Reg.Next = 0;
41 return;
42 }
43
44 // Otherwise, add this operand to the head of the registers use/def list.
Chris Lattner80fe5312008-01-01 21:08:22 +000045 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
Chris Lattner62ed6b92008-01-01 01:12:31 +000046
Chris Lattner80fe5312008-01-01 21:08:22 +000047 // For SSA values, we prefer to keep the definition at the start of the list.
48 // we do this by skipping over the definition if it is at the head of the
49 // list.
50 if (*Head && (*Head)->isDef())
51 Head = &(*Head)->Contents.Reg.Next;
52
53 Contents.Reg.Next = *Head;
Chris Lattner62ed6b92008-01-01 01:12:31 +000054 if (Contents.Reg.Next) {
55 assert(getReg() == Contents.Reg.Next->getReg() &&
56 "Different regs on the same list!");
57 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
58 }
59
Chris Lattner80fe5312008-01-01 21:08:22 +000060 Contents.Reg.Prev = Head;
61 *Head = this;
Chris Lattner62ed6b92008-01-01 01:12:31 +000062}
63
64void MachineOperand::setReg(unsigned Reg) {
65 if (getReg() == Reg) return; // No change.
66
67 // Otherwise, we have to change the register. If this operand is embedded
68 // into a machine function, we need to update the old and new register's
69 // use/def lists.
70 if (MachineInstr *MI = getParent())
71 if (MachineBasicBlock *MBB = MI->getParent())
72 if (MachineFunction *MF = MBB->getParent()) {
73 RemoveRegOperandFromRegInfo();
74 Contents.Reg.RegNo = Reg;
75 AddRegOperandToRegInfo(&MF->getRegInfo());
76 return;
77 }
78
79 // Otherwise, just change the register, no problem. :)
80 Contents.Reg.RegNo = Reg;
81}
82
83/// ChangeToImmediate - Replace this operand with a new immediate operand of
84/// the specified value. If an operand is known to be an immediate already,
85/// the setImm method should be used.
86void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
87 // If this operand is currently a register operand, and if this is in a
88 // function, deregister the operand from the register's use/def list.
89 if (isReg() && getParent() && getParent()->getParent() &&
90 getParent()->getParent()->getParent())
91 RemoveRegOperandFromRegInfo();
92
93 OpKind = MO_Immediate;
94 Contents.ImmVal = ImmVal;
95}
96
97/// ChangeToRegister - Replace this operand with a new register operand of
98/// the specified value. If an operand is known to be an register already,
99/// the setReg method should be used.
100void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
101 bool isKill, bool isDead) {
102 // If this operand is already a register operand, use setReg to update the
103 // register's use/def lists.
104 if (isReg()) {
105 setReg(Reg);
106 } else {
107 // Otherwise, change this to a register and set the reg#.
108 OpKind = MO_Register;
109 Contents.Reg.RegNo = Reg;
110
111 // If this operand is embedded in a function, add the operand to the
112 // register's use/def list.
113 if (MachineInstr *MI = getParent())
114 if (MachineBasicBlock *MBB = MI->getParent())
115 if (MachineFunction *MF = MBB->getParent())
116 AddRegOperandToRegInfo(&MF->getRegInfo());
117 }
118
119 IsDef = isDef;
120 IsImp = isImp;
121 IsKill = isKill;
122 IsDead = isDead;
123 SubReg = 0;
124}
125
Chris Lattnerf7382302007-12-30 21:56:09 +0000126/// isIdenticalTo - Return true if this operand is identical to the specified
127/// operand.
128bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
129 if (getType() != Other.getType()) return false;
130
131 switch (getType()) {
132 default: assert(0 && "Unrecognized operand type");
133 case MachineOperand::MO_Register:
134 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
135 getSubReg() == Other.getSubReg();
136 case MachineOperand::MO_Immediate:
137 return getImm() == Other.getImm();
138 case MachineOperand::MO_MachineBasicBlock:
139 return getMBB() == Other.getMBB();
140 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000141 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000142 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000143 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattnerf7382302007-12-30 21:56:09 +0000144 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000145 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000146 case MachineOperand::MO_GlobalAddress:
147 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
148 case MachineOperand::MO_ExternalSymbol:
149 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
150 getOffset() == Other.getOffset();
151 }
152}
153
154/// print - Print the specified machine operand.
155///
156void MachineOperand::print(std::ostream &OS, const TargetMachine *TM) const {
157 switch (getType()) {
158 case MachineOperand::MO_Register:
159 if (getReg() == 0 || MRegisterInfo::isVirtualRegister(getReg())) {
160 OS << "%reg" << getReg();
161 } else {
162 // If the instruction is embedded into a basic block, we can find the
Chris Lattner62ed6b92008-01-01 01:12:31 +0000163 // target info for the instruction.
Chris Lattnerf7382302007-12-30 21:56:09 +0000164 if (TM == 0)
165 if (const MachineInstr *MI = getParent())
166 if (const MachineBasicBlock *MBB = MI->getParent())
167 if (const MachineFunction *MF = MBB->getParent())
168 TM = &MF->getTarget();
169
170 if (TM)
171 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
172 else
173 OS << "%mreg" << getReg();
174 }
175
176 if (isDef() || isKill() || isDead() || isImplicit()) {
177 OS << "<";
178 bool NeedComma = false;
179 if (isImplicit()) {
180 OS << (isDef() ? "imp-def" : "imp-use");
181 NeedComma = true;
182 } else if (isDef()) {
183 OS << "def";
184 NeedComma = true;
185 }
186 if (isKill() || isDead()) {
187 if (NeedComma) OS << ",";
188 if (isKill()) OS << "kill";
189 if (isDead()) OS << "dead";
190 }
191 OS << ">";
192 }
193 break;
194 case MachineOperand::MO_Immediate:
195 OS << getImm();
196 break;
197 case MachineOperand::MO_MachineBasicBlock:
198 OS << "mbb<"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000199 << ((Value*)getMBB()->getBasicBlock())->getName()
200 << "," << (void*)getMBB() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000201 break;
202 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000203 OS << "<fi#" << getIndex() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000204 break;
205 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000206 OS << "<cp#" << getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000207 if (getOffset()) OS << "+" << getOffset();
208 OS << ">";
209 break;
210 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000211 OS << "<jt#" << getIndex() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000212 break;
213 case MachineOperand::MO_GlobalAddress:
214 OS << "<ga:" << ((Value*)getGlobal())->getName();
215 if (getOffset()) OS << "+" << getOffset();
216 OS << ">";
217 break;
218 case MachineOperand::MO_ExternalSymbol:
219 OS << "<es:" << getSymbolName();
220 if (getOffset()) OS << "+" << getOffset();
221 OS << ">";
222 break;
223 default:
224 assert(0 && "Unrecognized operand type");
225 }
226}
227
228//===----------------------------------------------------------------------===//
229// MachineInstr Implementation
230//===----------------------------------------------------------------------===//
231
Evan Chengc0f64ff2006-11-27 23:37:22 +0000232/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
Evan Cheng67f660c2006-11-30 07:08:44 +0000233/// TID NULL and no operands.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000234MachineInstr::MachineInstr()
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000235 : TID(0), NumImplicitOps(0), Parent(0) {
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000236 // Make sure that we get added to a machine basicblock
237 LeakDetector::addGarbageObject(this);
Chris Lattner72791222002-10-28 20:59:49 +0000238}
239
Evan Cheng67f660c2006-11-30 07:08:44 +0000240void MachineInstr::addImplicitDefUseOperands() {
241 if (TID->ImplicitDefs)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000242 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner8019f412007-12-30 00:41:17 +0000243 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng67f660c2006-11-30 07:08:44 +0000244 if (TID->ImplicitUses)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000245 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner8019f412007-12-30 00:41:17 +0000246 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Evan Chengd7de4962006-11-13 23:34:06 +0000247}
248
249/// MachineInstr ctor - This constructor create a MachineInstr and add the
Evan Chengc0f64ff2006-11-27 23:37:22 +0000250/// implicit operands. It reserves space for number of operands specified by
251/// TargetInstrDescriptor or the numOperands if it is not zero. (for
252/// instructions with variable number of operands).
Evan Chengfa945722007-10-13 02:23:01 +0000253MachineInstr::MachineInstr(const TargetInstrDescriptor &tid, bool NoImp)
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000254 : TID(&tid), NumImplicitOps(0), Parent(0) {
Chris Lattner349c4952008-01-07 03:13:06 +0000255 if (!NoImp && TID->getImplicitDefs())
256 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000257 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000258 if (!NoImp && TID->getImplicitUses())
259 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000260 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000261 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Chengfa945722007-10-13 02:23:01 +0000262 if (!NoImp)
263 addImplicitDefUseOperands();
Evan Chengd7de4962006-11-13 23:34:06 +0000264 // Make sure that we get added to a machine basicblock
265 LeakDetector::addGarbageObject(this);
266}
267
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000268/// MachineInstr ctor - Work exactly the same as the ctor above, except that the
269/// MachineInstr is created and added to the end of the specified basic block.
270///
Evan Chengc0f64ff2006-11-27 23:37:22 +0000271MachineInstr::MachineInstr(MachineBasicBlock *MBB,
Evan Cheng67f660c2006-11-30 07:08:44 +0000272 const TargetInstrDescriptor &tid)
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000273 : TID(&tid), NumImplicitOps(0), Parent(0) {
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000274 assert(MBB && "Cannot use inserting ctor with null basic block!");
Evan Cheng67f660c2006-11-30 07:08:44 +0000275 if (TID->ImplicitDefs)
Chris Lattner349c4952008-01-07 03:13:06 +0000276 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000277 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +0000278 if (TID->ImplicitUses)
Chris Lattner349c4952008-01-07 03:13:06 +0000279 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000280 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000281 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Cheng67f660c2006-11-30 07:08:44 +0000282 addImplicitDefUseOperands();
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000283 // Make sure that we get added to a machine basicblock
284 LeakDetector::addGarbageObject(this);
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000285 MBB->push_back(this); // Add instruction to end of basic block!
286}
287
Misha Brukmance22e762004-07-09 14:45:17 +0000288/// MachineInstr ctor - Copies MachineInstr arg exactly
289///
Tanya Lattner466b5342004-05-23 19:35:12 +0000290MachineInstr::MachineInstr(const MachineInstr &MI) {
Chris Lattner69244302008-01-07 01:56:04 +0000291 TID = MI.getDesc();
Evan Cheng6b2c05f2006-11-15 20:54:29 +0000292 NumImplicitOps = MI.NumImplicitOps;
Chris Lattner943b5e12006-05-04 19:14:44 +0000293 Operands.reserve(MI.getNumOperands());
Tanya Lattnerb5159ed2004-05-23 20:58:02 +0000294
Misha Brukmance22e762004-07-09 14:45:17 +0000295 // Add operands
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000296 for (unsigned i = 0; i != MI.getNumOperands(); ++i) {
Chris Lattner943b5e12006-05-04 19:14:44 +0000297 Operands.push_back(MI.getOperand(i));
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000298 Operands.back().ParentMI = this;
299 }
Tanya Lattner0c63e032004-05-24 03:14:18 +0000300
Misha Brukmance22e762004-07-09 14:45:17 +0000301 // Set parent, next, and prev to null
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000302 Parent = 0;
303 Prev = 0;
304 Next = 0;
Tanya Lattner466b5342004-05-23 19:35:12 +0000305}
306
307
Misha Brukmance22e762004-07-09 14:45:17 +0000308MachineInstr::~MachineInstr() {
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000309 LeakDetector::removeGarbageObject(this);
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000310#ifndef NDEBUG
Chris Lattner62ed6b92008-01-01 01:12:31 +0000311 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000312 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000313 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
314 "Reg operand def/use list corrupted");
315 }
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000316#endif
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000317}
318
Evan Cheng67f660c2006-11-30 07:08:44 +0000319/// getOpcode - Returns the opcode of this MachineInstr.
320///
Dan Gohmancb648f92007-09-14 20:08:19 +0000321int MachineInstr::getOpcode() const {
Evan Cheng67f660c2006-11-30 07:08:44 +0000322 return TID->Opcode;
323}
324
Chris Lattner62ed6b92008-01-01 01:12:31 +0000325/// getRegInfo - If this instruction is embedded into a MachineFunction,
326/// return the MachineRegisterInfo object for the current function, otherwise
327/// return null.
328MachineRegisterInfo *MachineInstr::getRegInfo() {
329 if (MachineBasicBlock *MBB = getParent())
330 if (MachineFunction *MF = MBB->getParent())
331 return &MF->getRegInfo();
332 return 0;
333}
334
335/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
336/// this instruction from their respective use lists. This requires that the
337/// operands already be on their use lists.
338void MachineInstr::RemoveRegOperandsFromUseLists() {
339 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
340 if (Operands[i].isReg())
341 Operands[i].RemoveRegOperandFromRegInfo();
342 }
343}
344
345/// AddRegOperandsToUseLists - Add all of the register operands in
346/// this instruction from their respective use lists. This requires that the
347/// operands not be on their use lists yet.
348void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
349 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
350 if (Operands[i].isReg())
351 Operands[i].AddRegOperandToRegInfo(&RegInfo);
352 }
353}
354
355
356/// addOperand - Add the specified operand to the instruction. If it is an
357/// implicit operand, it is added to the end of the operand list. If it is
358/// an explicit operand it is added at the end of the explicit operand list
359/// (before the first implicit operand).
360void MachineInstr::addOperand(const MachineOperand &Op) {
361 bool isImpReg = Op.isReg() && Op.isImplicit();
362 assert((isImpReg || !OperandsComplete()) &&
363 "Trying to add an operand to a machine instr that is already done!");
364
365 // If we are adding the operand to the end of the list, our job is simpler.
366 // This is true most of the time, so this is a reasonable optimization.
367 if (isImpReg || NumImplicitOps == 0) {
368 // We can only do this optimization if we know that the operand list won't
369 // reallocate.
370 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
371 Operands.push_back(Op);
372
373 // Set the parent of the operand.
374 Operands.back().ParentMI = this;
375
376 // If the operand is a register, update the operand's use list.
377 if (Op.isReg())
378 Operands.back().AddRegOperandToRegInfo(getRegInfo());
379 return;
380 }
381 }
382
383 // Otherwise, we have to insert a real operand before any implicit ones.
384 unsigned OpNo = Operands.size()-NumImplicitOps;
385
386 MachineRegisterInfo *RegInfo = getRegInfo();
387
388 // If this instruction isn't embedded into a function, then we don't need to
389 // update any operand lists.
390 if (RegInfo == 0) {
391 // Simple insertion, no reginfo update needed for other register operands.
392 Operands.insert(Operands.begin()+OpNo, Op);
393 Operands[OpNo].ParentMI = this;
394
395 // Do explicitly set the reginfo for this operand though, to ensure the
396 // next/prev fields are properly nulled out.
397 if (Operands[OpNo].isReg())
398 Operands[OpNo].AddRegOperandToRegInfo(0);
399
400 } else if (Operands.size()+1 <= Operands.capacity()) {
401 // Otherwise, we have to remove register operands from their register use
402 // list, add the operand, then add the register operands back to their use
403 // list. This also must handle the case when the operand list reallocates
404 // to somewhere else.
405
406 // If insertion of this operand won't cause reallocation of the operand
407 // list, just remove the implicit operands, add the operand, then re-add all
408 // the rest of the operands.
409 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
410 assert(Operands[i].isReg() && "Should only be an implicit reg!");
411 Operands[i].RemoveRegOperandFromRegInfo();
412 }
413
414 // Add the operand. If it is a register, add it to the reg list.
415 Operands.insert(Operands.begin()+OpNo, Op);
416 Operands[OpNo].ParentMI = this;
417
418 if (Operands[OpNo].isReg())
419 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
420
421 // Re-add all the implicit ops.
422 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
423 assert(Operands[i].isReg() && "Should only be an implicit reg!");
424 Operands[i].AddRegOperandToRegInfo(RegInfo);
425 }
426 } else {
427 // Otherwise, we will be reallocating the operand list. Remove all reg
428 // operands from their list, then readd them after the operand list is
429 // reallocated.
430 RemoveRegOperandsFromUseLists();
431
432 Operands.insert(Operands.begin()+OpNo, Op);
433 Operands[OpNo].ParentMI = this;
434
435 // Re-add all the operands.
436 AddRegOperandsToUseLists(*RegInfo);
437 }
438}
439
440/// RemoveOperand - Erase an operand from an instruction, leaving it with one
441/// fewer operand than it started with.
442///
443void MachineInstr::RemoveOperand(unsigned OpNo) {
444 assert(OpNo < Operands.size() && "Invalid operand number");
445
446 // Special case removing the last one.
447 if (OpNo == Operands.size()-1) {
448 // If needed, remove from the reg def/use list.
449 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
450 Operands.back().RemoveRegOperandFromRegInfo();
451
452 Operands.pop_back();
453 return;
454 }
455
456 // Otherwise, we are removing an interior operand. If we have reginfo to
457 // update, remove all operands that will be shifted down from their reg lists,
458 // move everything down, then re-add them.
459 MachineRegisterInfo *RegInfo = getRegInfo();
460 if (RegInfo) {
461 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
462 if (Operands[i].isReg())
463 Operands[i].RemoveRegOperandFromRegInfo();
464 }
465 }
466
467 Operands.erase(Operands.begin()+OpNo);
468
469 if (RegInfo) {
470 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
471 if (Operands[i].isReg())
472 Operands[i].AddRegOperandToRegInfo(RegInfo);
473 }
474 }
475}
476
477
Chris Lattner48d7c062006-04-17 21:35:41 +0000478/// removeFromParent - This method unlinks 'this' from the containing basic
479/// block, and returns it, but does not delete it.
480MachineInstr *MachineInstr::removeFromParent() {
481 assert(getParent() && "Not embedded in a basic block!");
482 getParent()->remove(this);
483 return this;
484}
485
486
Brian Gaeke21326fc2004-02-13 04:39:32 +0000487/// OperandComplete - Return true if it's illegal to add a new operand
488///
Chris Lattner2a90ba62004-02-12 16:09:53 +0000489bool MachineInstr::OperandsComplete() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000490 unsigned short NumOperands = TID->getNumOperands();
491 if (TID->hasVariableOperands() == 0 &&
Evan Cheng8bcb0422006-11-28 02:25:34 +0000492 getNumOperands()-NumImplicitOps >= NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000493 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000494 return false;
495}
496
Evan Cheng19e3f312007-05-15 01:26:09 +0000497/// getNumExplicitOperands - Returns the number of non-implicit operands.
498///
499unsigned MachineInstr::getNumExplicitOperands() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000500 unsigned NumOperands = TID->getNumOperands();
501 if (TID->hasVariableOperands() == 0)
Evan Cheng19e3f312007-05-15 01:26:09 +0000502 return NumOperands;
503
504 for (unsigned e = getNumOperands(); NumOperands != e; ++NumOperands) {
505 const MachineOperand &MO = getOperand(NumOperands);
506 if (!MO.isRegister() || !MO.isImplicit())
507 NumOperands++;
508 }
509 return NumOperands;
510}
511
Chris Lattner8ace2cd2006-10-20 22:39:59 +0000512
Evan Chengfaa51072007-04-26 19:00:32 +0000513/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Evan Cheng32eb1f12007-03-26 22:37:45 +0000514/// the specific register or -1 if it is not found. It further tightening
Evan Cheng76d7e762007-02-23 01:04:26 +0000515/// the search criteria to a use that kills the register if isKill is true.
Evan Chengf277ee42007-05-29 18:35:22 +0000516int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill) const {
Evan Cheng576d1232006-12-06 08:27:42 +0000517 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengf277ee42007-05-29 18:35:22 +0000518 const MachineOperand &MO = getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000519 if (MO.isRegister() && MO.isUse() && MO.getReg() == Reg)
Evan Cheng76d7e762007-02-23 01:04:26 +0000520 if (!isKill || MO.isKill())
Evan Cheng32eb1f12007-03-26 22:37:45 +0000521 return i;
Evan Cheng576d1232006-12-06 08:27:42 +0000522 }
Evan Cheng32eb1f12007-03-26 22:37:45 +0000523 return -1;
Evan Cheng576d1232006-12-06 08:27:42 +0000524}
525
Evan Chengb371f452007-02-19 21:49:54 +0000526/// findRegisterDefOperand() - Returns the MachineOperand that is a def of
527/// the specific register or NULL if it is not found.
528MachineOperand *MachineInstr::findRegisterDefOperand(unsigned Reg) {
529 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
530 MachineOperand &MO = getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000531 if (MO.isRegister() && MO.isDef() && MO.getReg() == Reg)
Evan Chengb371f452007-02-19 21:49:54 +0000532 return &MO;
533 }
534 return NULL;
535}
Evan Cheng19e3f312007-05-15 01:26:09 +0000536
Evan Chengf277ee42007-05-29 18:35:22 +0000537/// findFirstPredOperandIdx() - Find the index of the first operand in the
538/// operand list that is used to represent the predicate. It returns -1 if
539/// none is found.
540int MachineInstr::findFirstPredOperandIdx() const {
Chris Lattner69244302008-01-07 01:56:04 +0000541 const TargetInstrDescriptor *TID = getDesc();
542 if (TID->isPredicable()) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000543 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner8ca5c672008-01-07 02:39:19 +0000544 if (TID->OpInfo[i].isPredicate())
Evan Chengf277ee42007-05-29 18:35:22 +0000545 return i;
Evan Cheng19e3f312007-05-15 01:26:09 +0000546 }
547
Evan Chengf277ee42007-05-29 18:35:22 +0000548 return -1;
Evan Cheng19e3f312007-05-15 01:26:09 +0000549}
Evan Chengb371f452007-02-19 21:49:54 +0000550
Evan Cheng32dfbea2007-10-12 08:50:34 +0000551/// isRegReDefinedByTwoAddr - Returns true if the Reg re-definition is due
552/// to two addr elimination.
553bool MachineInstr::isRegReDefinedByTwoAddr(unsigned Reg) const {
Chris Lattner69244302008-01-07 01:56:04 +0000554 const TargetInstrDescriptor *TID = getDesc();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000555 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
556 const MachineOperand &MO1 = getOperand(i);
557 if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) {
558 for (unsigned j = i+1; j < e; ++j) {
559 const MachineOperand &MO2 = getOperand(j);
560 if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg &&
561 TID->getOperandConstraint(j, TOI::TIED_TO) == (int)i)
562 return true;
563 }
564 }
565 }
566 return false;
567}
568
Evan Cheng576d1232006-12-06 08:27:42 +0000569/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
570///
571void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
572 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
573 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000574 if (!MO.isRegister() || (!MO.isKill() && !MO.isDead()))
Evan Cheng576d1232006-12-06 08:27:42 +0000575 continue;
576 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
577 MachineOperand &MOp = getOperand(j);
578 if (!MOp.isIdenticalTo(MO))
579 continue;
580 if (MO.isKill())
581 MOp.setIsKill();
582 else
583 MOp.setIsDead();
584 break;
585 }
586 }
587}
588
Evan Cheng19e3f312007-05-15 01:26:09 +0000589/// copyPredicates - Copies predicate operand(s) from MI.
590void MachineInstr::copyPredicates(const MachineInstr *MI) {
Chris Lattner69244302008-01-07 01:56:04 +0000591 const TargetInstrDescriptor *TID = MI->getDesc();
592 if (TID->isPredicable()) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000593 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner8ca5c672008-01-07 02:39:19 +0000594 if (TID->OpInfo[i].isPredicate()) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000595 // Predicated operands must be last operands.
Chris Lattner8019f412007-12-30 00:41:17 +0000596 addOperand(MI->getOperand(i));
Evan Cheng19e3f312007-05-15 01:26:09 +0000597 }
598 }
599 }
600}
601
Brian Gaeke21326fc2004-02-13 04:39:32 +0000602void MachineInstr::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000603 cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000604}
605
Tanya Lattnerb1407622004-06-25 00:13:11 +0000606void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
Chris Lattnere3087892007-12-30 21:31:53 +0000607 // Specialize printing if op#0 is definition
Chris Lattner6a592272002-10-30 01:55:38 +0000608 unsigned StartOp = 0;
Dan Gohman92dfe202007-09-14 20:33:02 +0000609 if (getNumOperands() && getOperand(0).isRegister() && getOperand(0).isDef()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000610 getOperand(0).print(OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +0000611 OS << " = ";
612 ++StartOp; // Don't print this operand again!
613 }
Tanya Lattnerb1407622004-06-25 00:13:11 +0000614
Chris Lattner69244302008-01-07 01:56:04 +0000615 OS << getDesc()->Name;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000616
Chris Lattner6a592272002-10-30 01:55:38 +0000617 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
618 if (i != StartOp)
619 OS << ",";
620 OS << " ";
Chris Lattnerf7382302007-12-30 21:56:09 +0000621 getOperand(i).print(OS, TM);
Chris Lattner10491642002-10-30 00:48:05 +0000622 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000623
Chris Lattner10491642002-10-30 00:48:05 +0000624 OS << "\n";
625}
626