blob: 43cc66d2370964ba55381af1c83e610af39ef4a1 [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//
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.
45 MachineOperand *&Head = RegInfo->getRegUseDefListHead(getReg());
46
47 Contents.Reg.Next = Head;
48 if (Contents.Reg.Next) {
49 assert(getReg() == Contents.Reg.Next->getReg() &&
50 "Different regs on the same list!");
51 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
52 }
53
54 Contents.Reg.Prev = &Head;
55 Head = this;
56}
57
58void MachineOperand::setReg(unsigned Reg) {
59 if (getReg() == Reg) return; // No change.
60
61 // Otherwise, we have to change the register. If this operand is embedded
62 // into a machine function, we need to update the old and new register's
63 // use/def lists.
64 if (MachineInstr *MI = getParent())
65 if (MachineBasicBlock *MBB = MI->getParent())
66 if (MachineFunction *MF = MBB->getParent()) {
67 RemoveRegOperandFromRegInfo();
68 Contents.Reg.RegNo = Reg;
69 AddRegOperandToRegInfo(&MF->getRegInfo());
70 return;
71 }
72
73 // Otherwise, just change the register, no problem. :)
74 Contents.Reg.RegNo = Reg;
75}
76
77/// ChangeToImmediate - Replace this operand with a new immediate operand of
78/// the specified value. If an operand is known to be an immediate already,
79/// the setImm method should be used.
80void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
81 // If this operand is currently a register operand, and if this is in a
82 // function, deregister the operand from the register's use/def list.
83 if (isReg() && getParent() && getParent()->getParent() &&
84 getParent()->getParent()->getParent())
85 RemoveRegOperandFromRegInfo();
86
87 OpKind = MO_Immediate;
88 Contents.ImmVal = ImmVal;
89}
90
91/// ChangeToRegister - Replace this operand with a new register operand of
92/// the specified value. If an operand is known to be an register already,
93/// the setReg method should be used.
94void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
95 bool isKill, bool isDead) {
96 // If this operand is already a register operand, use setReg to update the
97 // register's use/def lists.
98 if (isReg()) {
99 setReg(Reg);
100 } else {
101 // Otherwise, change this to a register and set the reg#.
102 OpKind = MO_Register;
103 Contents.Reg.RegNo = Reg;
104
105 // If this operand is embedded in a function, add the operand to the
106 // register's use/def list.
107 if (MachineInstr *MI = getParent())
108 if (MachineBasicBlock *MBB = MI->getParent())
109 if (MachineFunction *MF = MBB->getParent())
110 AddRegOperandToRegInfo(&MF->getRegInfo());
111 }
112
113 IsDef = isDef;
114 IsImp = isImp;
115 IsKill = isKill;
116 IsDead = isDead;
117 SubReg = 0;
118}
119
Chris Lattnerf7382302007-12-30 21:56:09 +0000120/// isIdenticalTo - Return true if this operand is identical to the specified
121/// operand.
122bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
123 if (getType() != Other.getType()) return false;
124
125 switch (getType()) {
126 default: assert(0 && "Unrecognized operand type");
127 case MachineOperand::MO_Register:
128 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
129 getSubReg() == Other.getSubReg();
130 case MachineOperand::MO_Immediate:
131 return getImm() == Other.getImm();
132 case MachineOperand::MO_MachineBasicBlock:
133 return getMBB() == Other.getMBB();
134 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000135 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000136 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000137 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattnerf7382302007-12-30 21:56:09 +0000138 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000139 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000140 case MachineOperand::MO_GlobalAddress:
141 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
142 case MachineOperand::MO_ExternalSymbol:
143 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
144 getOffset() == Other.getOffset();
145 }
146}
147
148/// print - Print the specified machine operand.
149///
150void MachineOperand::print(std::ostream &OS, const TargetMachine *TM) const {
151 switch (getType()) {
152 case MachineOperand::MO_Register:
153 if (getReg() == 0 || MRegisterInfo::isVirtualRegister(getReg())) {
154 OS << "%reg" << getReg();
155 } else {
156 // If the instruction is embedded into a basic block, we can find the
Chris Lattner62ed6b92008-01-01 01:12:31 +0000157 // target info for the instruction.
Chris Lattnerf7382302007-12-30 21:56:09 +0000158 if (TM == 0)
159 if (const MachineInstr *MI = getParent())
160 if (const MachineBasicBlock *MBB = MI->getParent())
161 if (const MachineFunction *MF = MBB->getParent())
162 TM = &MF->getTarget();
163
164 if (TM)
165 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
166 else
167 OS << "%mreg" << getReg();
168 }
169
170 if (isDef() || isKill() || isDead() || isImplicit()) {
171 OS << "<";
172 bool NeedComma = false;
173 if (isImplicit()) {
174 OS << (isDef() ? "imp-def" : "imp-use");
175 NeedComma = true;
176 } else if (isDef()) {
177 OS << "def";
178 NeedComma = true;
179 }
180 if (isKill() || isDead()) {
181 if (NeedComma) OS << ",";
182 if (isKill()) OS << "kill";
183 if (isDead()) OS << "dead";
184 }
185 OS << ">";
186 }
187 break;
188 case MachineOperand::MO_Immediate:
189 OS << getImm();
190 break;
191 case MachineOperand::MO_MachineBasicBlock:
192 OS << "mbb<"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000193 << ((Value*)getMBB()->getBasicBlock())->getName()
194 << "," << (void*)getMBB() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000195 break;
196 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000197 OS << "<fi#" << getIndex() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000198 break;
199 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000200 OS << "<cp#" << getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000201 if (getOffset()) OS << "+" << getOffset();
202 OS << ">";
203 break;
204 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000205 OS << "<jt#" << getIndex() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000206 break;
207 case MachineOperand::MO_GlobalAddress:
208 OS << "<ga:" << ((Value*)getGlobal())->getName();
209 if (getOffset()) OS << "+" << getOffset();
210 OS << ">";
211 break;
212 case MachineOperand::MO_ExternalSymbol:
213 OS << "<es:" << getSymbolName();
214 if (getOffset()) OS << "+" << getOffset();
215 OS << ">";
216 break;
217 default:
218 assert(0 && "Unrecognized operand type");
219 }
220}
221
222//===----------------------------------------------------------------------===//
223// MachineInstr Implementation
224//===----------------------------------------------------------------------===//
225
Evan Chengc0f64ff2006-11-27 23:37:22 +0000226/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
Evan Cheng67f660c2006-11-30 07:08:44 +0000227/// TID NULL and no operands.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000228MachineInstr::MachineInstr()
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000229 : TID(0), NumImplicitOps(0), Parent(0) {
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000230 // Make sure that we get added to a machine basicblock
231 LeakDetector::addGarbageObject(this);
Chris Lattner72791222002-10-28 20:59:49 +0000232}
233
Evan Cheng67f660c2006-11-30 07:08:44 +0000234void MachineInstr::addImplicitDefUseOperands() {
235 if (TID->ImplicitDefs)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000236 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner8019f412007-12-30 00:41:17 +0000237 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng67f660c2006-11-30 07:08:44 +0000238 if (TID->ImplicitUses)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000239 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner8019f412007-12-30 00:41:17 +0000240 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Evan Chengd7de4962006-11-13 23:34:06 +0000241}
242
243/// MachineInstr ctor - This constructor create a MachineInstr and add the
Evan Chengc0f64ff2006-11-27 23:37:22 +0000244/// implicit operands. It reserves space for number of operands specified by
245/// TargetInstrDescriptor or the numOperands if it is not zero. (for
246/// instructions with variable number of operands).
Evan Chengfa945722007-10-13 02:23:01 +0000247MachineInstr::MachineInstr(const TargetInstrDescriptor &tid, bool NoImp)
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000248 : TID(&tid), NumImplicitOps(0), Parent(0) {
Evan Chengfa945722007-10-13 02:23:01 +0000249 if (!NoImp && TID->ImplicitDefs)
Evan Cheng67f660c2006-11-30 07:08:44 +0000250 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000251 NumImplicitOps++;
Evan Chengfa945722007-10-13 02:23:01 +0000252 if (!NoImp && TID->ImplicitUses)
Evan Cheng67f660c2006-11-30 07:08:44 +0000253 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000254 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +0000255 Operands.reserve(NumImplicitOps + TID->numOperands);
Evan Chengfa945722007-10-13 02:23:01 +0000256 if (!NoImp)
257 addImplicitDefUseOperands();
Evan Chengd7de4962006-11-13 23:34:06 +0000258 // Make sure that we get added to a machine basicblock
259 LeakDetector::addGarbageObject(this);
260}
261
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000262/// MachineInstr ctor - Work exactly the same as the ctor above, except that the
263/// MachineInstr is created and added to the end of the specified basic block.
264///
Evan Chengc0f64ff2006-11-27 23:37:22 +0000265MachineInstr::MachineInstr(MachineBasicBlock *MBB,
Evan Cheng67f660c2006-11-30 07:08:44 +0000266 const TargetInstrDescriptor &tid)
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000267 : TID(&tid), NumImplicitOps(0), Parent(0) {
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000268 assert(MBB && "Cannot use inserting ctor with null basic block!");
Evan Cheng67f660c2006-11-30 07:08:44 +0000269 if (TID->ImplicitDefs)
270 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000271 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +0000272 if (TID->ImplicitUses)
273 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000274 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +0000275 Operands.reserve(NumImplicitOps + TID->numOperands);
276 addImplicitDefUseOperands();
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000277 // Make sure that we get added to a machine basicblock
278 LeakDetector::addGarbageObject(this);
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000279 MBB->push_back(this); // Add instruction to end of basic block!
280}
281
Misha Brukmance22e762004-07-09 14:45:17 +0000282/// MachineInstr ctor - Copies MachineInstr arg exactly
283///
Tanya Lattner466b5342004-05-23 19:35:12 +0000284MachineInstr::MachineInstr(const MachineInstr &MI) {
Evan Cheng67f660c2006-11-30 07:08:44 +0000285 TID = MI.getInstrDescriptor();
Evan Cheng6b2c05f2006-11-15 20:54:29 +0000286 NumImplicitOps = MI.NumImplicitOps;
Chris Lattner943b5e12006-05-04 19:14:44 +0000287 Operands.reserve(MI.getNumOperands());
Tanya Lattnerb5159ed2004-05-23 20:58:02 +0000288
Misha Brukmance22e762004-07-09 14:45:17 +0000289 // Add operands
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000290 for (unsigned i = 0; i != MI.getNumOperands(); ++i) {
Chris Lattner943b5e12006-05-04 19:14:44 +0000291 Operands.push_back(MI.getOperand(i));
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000292 Operands.back().ParentMI = this;
293 }
Tanya Lattner0c63e032004-05-24 03:14:18 +0000294
Misha Brukmance22e762004-07-09 14:45:17 +0000295 // Set parent, next, and prev to null
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000296 Parent = 0;
297 Prev = 0;
298 Next = 0;
Tanya Lattner466b5342004-05-23 19:35:12 +0000299}
300
301
Misha Brukmance22e762004-07-09 14:45:17 +0000302MachineInstr::~MachineInstr() {
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000303 LeakDetector::removeGarbageObject(this);
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000304#ifndef NDEBUG
Chris Lattner62ed6b92008-01-01 01:12:31 +0000305 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000306 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000307 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
308 "Reg operand def/use list corrupted");
309 }
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000310#endif
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000311}
312
Evan Cheng67f660c2006-11-30 07:08:44 +0000313/// getOpcode - Returns the opcode of this MachineInstr.
314///
Dan Gohmancb648f92007-09-14 20:08:19 +0000315int MachineInstr::getOpcode() const {
Evan Cheng67f660c2006-11-30 07:08:44 +0000316 return TID->Opcode;
317}
318
Chris Lattner62ed6b92008-01-01 01:12:31 +0000319/// getRegInfo - If this instruction is embedded into a MachineFunction,
320/// return the MachineRegisterInfo object for the current function, otherwise
321/// return null.
322MachineRegisterInfo *MachineInstr::getRegInfo() {
323 if (MachineBasicBlock *MBB = getParent())
324 if (MachineFunction *MF = MBB->getParent())
325 return &MF->getRegInfo();
326 return 0;
327}
328
329/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
330/// this instruction from their respective use lists. This requires that the
331/// operands already be on their use lists.
332void MachineInstr::RemoveRegOperandsFromUseLists() {
333 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
334 if (Operands[i].isReg())
335 Operands[i].RemoveRegOperandFromRegInfo();
336 }
337}
338
339/// AddRegOperandsToUseLists - Add all of the register operands in
340/// this instruction from their respective use lists. This requires that the
341/// operands not be on their use lists yet.
342void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
343 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
344 if (Operands[i].isReg())
345 Operands[i].AddRegOperandToRegInfo(&RegInfo);
346 }
347}
348
349
350/// addOperand - Add the specified operand to the instruction. If it is an
351/// implicit operand, it is added to the end of the operand list. If it is
352/// an explicit operand it is added at the end of the explicit operand list
353/// (before the first implicit operand).
354void MachineInstr::addOperand(const MachineOperand &Op) {
355 bool isImpReg = Op.isReg() && Op.isImplicit();
356 assert((isImpReg || !OperandsComplete()) &&
357 "Trying to add an operand to a machine instr that is already done!");
358
359 // If we are adding the operand to the end of the list, our job is simpler.
360 // This is true most of the time, so this is a reasonable optimization.
361 if (isImpReg || NumImplicitOps == 0) {
362 // We can only do this optimization if we know that the operand list won't
363 // reallocate.
364 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
365 Operands.push_back(Op);
366
367 // Set the parent of the operand.
368 Operands.back().ParentMI = this;
369
370 // If the operand is a register, update the operand's use list.
371 if (Op.isReg())
372 Operands.back().AddRegOperandToRegInfo(getRegInfo());
373 return;
374 }
375 }
376
377 // Otherwise, we have to insert a real operand before any implicit ones.
378 unsigned OpNo = Operands.size()-NumImplicitOps;
379
380 MachineRegisterInfo *RegInfo = getRegInfo();
381
382 // If this instruction isn't embedded into a function, then we don't need to
383 // update any operand lists.
384 if (RegInfo == 0) {
385 // Simple insertion, no reginfo update needed for other register operands.
386 Operands.insert(Operands.begin()+OpNo, Op);
387 Operands[OpNo].ParentMI = this;
388
389 // Do explicitly set the reginfo for this operand though, to ensure the
390 // next/prev fields are properly nulled out.
391 if (Operands[OpNo].isReg())
392 Operands[OpNo].AddRegOperandToRegInfo(0);
393
394 } else if (Operands.size()+1 <= Operands.capacity()) {
395 // Otherwise, we have to remove register operands from their register use
396 // list, add the operand, then add the register operands back to their use
397 // list. This also must handle the case when the operand list reallocates
398 // to somewhere else.
399
400 // If insertion of this operand won't cause reallocation of the operand
401 // list, just remove the implicit operands, add the operand, then re-add all
402 // the rest of the operands.
403 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
404 assert(Operands[i].isReg() && "Should only be an implicit reg!");
405 Operands[i].RemoveRegOperandFromRegInfo();
406 }
407
408 // Add the operand. If it is a register, add it to the reg list.
409 Operands.insert(Operands.begin()+OpNo, Op);
410 Operands[OpNo].ParentMI = this;
411
412 if (Operands[OpNo].isReg())
413 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
414
415 // Re-add all the implicit ops.
416 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
417 assert(Operands[i].isReg() && "Should only be an implicit reg!");
418 Operands[i].AddRegOperandToRegInfo(RegInfo);
419 }
420 } else {
421 // Otherwise, we will be reallocating the operand list. Remove all reg
422 // operands from their list, then readd them after the operand list is
423 // reallocated.
424 RemoveRegOperandsFromUseLists();
425
426 Operands.insert(Operands.begin()+OpNo, Op);
427 Operands[OpNo].ParentMI = this;
428
429 // Re-add all the operands.
430 AddRegOperandsToUseLists(*RegInfo);
431 }
432}
433
434/// RemoveOperand - Erase an operand from an instruction, leaving it with one
435/// fewer operand than it started with.
436///
437void MachineInstr::RemoveOperand(unsigned OpNo) {
438 assert(OpNo < Operands.size() && "Invalid operand number");
439
440 // Special case removing the last one.
441 if (OpNo == Operands.size()-1) {
442 // If needed, remove from the reg def/use list.
443 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
444 Operands.back().RemoveRegOperandFromRegInfo();
445
446 Operands.pop_back();
447 return;
448 }
449
450 // Otherwise, we are removing an interior operand. If we have reginfo to
451 // update, remove all operands that will be shifted down from their reg lists,
452 // move everything down, then re-add them.
453 MachineRegisterInfo *RegInfo = getRegInfo();
454 if (RegInfo) {
455 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
456 if (Operands[i].isReg())
457 Operands[i].RemoveRegOperandFromRegInfo();
458 }
459 }
460
461 Operands.erase(Operands.begin()+OpNo);
462
463 if (RegInfo) {
464 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
465 if (Operands[i].isReg())
466 Operands[i].AddRegOperandToRegInfo(RegInfo);
467 }
468 }
469}
470
471
Chris Lattner48d7c062006-04-17 21:35:41 +0000472/// removeFromParent - This method unlinks 'this' from the containing basic
473/// block, and returns it, but does not delete it.
474MachineInstr *MachineInstr::removeFromParent() {
475 assert(getParent() && "Not embedded in a basic block!");
476 getParent()->remove(this);
477 return this;
478}
479
480
Brian Gaeke21326fc2004-02-13 04:39:32 +0000481/// OperandComplete - Return true if it's illegal to add a new operand
482///
Chris Lattner2a90ba62004-02-12 16:09:53 +0000483bool MachineInstr::OperandsComplete() const {
Evan Cheng67f660c2006-11-30 07:08:44 +0000484 unsigned short NumOperands = TID->numOperands;
485 if ((TID->Flags & M_VARIABLE_OPS) == 0 &&
Evan Cheng8bcb0422006-11-28 02:25:34 +0000486 getNumOperands()-NumImplicitOps >= NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000487 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000488 return false;
489}
490
Evan Cheng19e3f312007-05-15 01:26:09 +0000491/// getNumExplicitOperands - Returns the number of non-implicit operands.
492///
493unsigned MachineInstr::getNumExplicitOperands() const {
494 unsigned NumOperands = TID->numOperands;
495 if ((TID->Flags & M_VARIABLE_OPS) == 0)
496 return NumOperands;
497
498 for (unsigned e = getNumOperands(); NumOperands != e; ++NumOperands) {
499 const MachineOperand &MO = getOperand(NumOperands);
500 if (!MO.isRegister() || !MO.isImplicit())
501 NumOperands++;
502 }
503 return NumOperands;
504}
505
Chris Lattner8ace2cd2006-10-20 22:39:59 +0000506
Evan Chengfaa51072007-04-26 19:00:32 +0000507/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Evan Cheng32eb1f12007-03-26 22:37:45 +0000508/// the specific register or -1 if it is not found. It further tightening
Evan Cheng76d7e762007-02-23 01:04:26 +0000509/// the search criteria to a use that kills the register if isKill is true.
Evan Chengf277ee42007-05-29 18:35:22 +0000510int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill) const {
Evan Cheng576d1232006-12-06 08:27:42 +0000511 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengf277ee42007-05-29 18:35:22 +0000512 const MachineOperand &MO = getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000513 if (MO.isRegister() && MO.isUse() && MO.getReg() == Reg)
Evan Cheng76d7e762007-02-23 01:04:26 +0000514 if (!isKill || MO.isKill())
Evan Cheng32eb1f12007-03-26 22:37:45 +0000515 return i;
Evan Cheng576d1232006-12-06 08:27:42 +0000516 }
Evan Cheng32eb1f12007-03-26 22:37:45 +0000517 return -1;
Evan Cheng576d1232006-12-06 08:27:42 +0000518}
519
Evan Chengb371f452007-02-19 21:49:54 +0000520/// findRegisterDefOperand() - Returns the MachineOperand that is a def of
521/// the specific register or NULL if it is not found.
522MachineOperand *MachineInstr::findRegisterDefOperand(unsigned Reg) {
523 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
524 MachineOperand &MO = getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000525 if (MO.isRegister() && MO.isDef() && MO.getReg() == Reg)
Evan Chengb371f452007-02-19 21:49:54 +0000526 return &MO;
527 }
528 return NULL;
529}
Evan Cheng19e3f312007-05-15 01:26:09 +0000530
Evan Chengf277ee42007-05-29 18:35:22 +0000531/// findFirstPredOperandIdx() - Find the index of the first operand in the
532/// operand list that is used to represent the predicate. It returns -1 if
533/// none is found.
534int MachineInstr::findFirstPredOperandIdx() const {
Evan Cheng19e3f312007-05-15 01:26:09 +0000535 const TargetInstrDescriptor *TID = getInstrDescriptor();
Evan Chengc3a289c2007-05-16 20:56:08 +0000536 if (TID->Flags & M_PREDICABLE) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000537 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
538 if ((TID->OpInfo[i].Flags & M_PREDICATE_OPERAND))
Evan Chengf277ee42007-05-29 18:35:22 +0000539 return i;
Evan Cheng19e3f312007-05-15 01:26:09 +0000540 }
541
Evan Chengf277ee42007-05-29 18:35:22 +0000542 return -1;
Evan Cheng19e3f312007-05-15 01:26:09 +0000543}
Evan Chengb371f452007-02-19 21:49:54 +0000544
Evan Cheng32dfbea2007-10-12 08:50:34 +0000545/// isRegReDefinedByTwoAddr - Returns true if the Reg re-definition is due
546/// to two addr elimination.
547bool MachineInstr::isRegReDefinedByTwoAddr(unsigned Reg) const {
548 const TargetInstrDescriptor *TID = getInstrDescriptor();
549 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
550 const MachineOperand &MO1 = getOperand(i);
551 if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) {
552 for (unsigned j = i+1; j < e; ++j) {
553 const MachineOperand &MO2 = getOperand(j);
554 if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg &&
555 TID->getOperandConstraint(j, TOI::TIED_TO) == (int)i)
556 return true;
557 }
558 }
559 }
560 return false;
561}
562
Evan Cheng576d1232006-12-06 08:27:42 +0000563/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
564///
565void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
566 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
567 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000568 if (!MO.isRegister() || (!MO.isKill() && !MO.isDead()))
Evan Cheng576d1232006-12-06 08:27:42 +0000569 continue;
570 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
571 MachineOperand &MOp = getOperand(j);
572 if (!MOp.isIdenticalTo(MO))
573 continue;
574 if (MO.isKill())
575 MOp.setIsKill();
576 else
577 MOp.setIsDead();
578 break;
579 }
580 }
581}
582
Evan Cheng19e3f312007-05-15 01:26:09 +0000583/// copyPredicates - Copies predicate operand(s) from MI.
584void MachineInstr::copyPredicates(const MachineInstr *MI) {
585 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
Evan Chengc3a289c2007-05-16 20:56:08 +0000586 if (TID->Flags & M_PREDICABLE) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000587 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
588 if ((TID->OpInfo[i].Flags & M_PREDICATE_OPERAND)) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000589 // Predicated operands must be last operands.
Chris Lattner8019f412007-12-30 00:41:17 +0000590 addOperand(MI->getOperand(i));
Evan Cheng19e3f312007-05-15 01:26:09 +0000591 }
592 }
593 }
594}
595
Brian Gaeke21326fc2004-02-13 04:39:32 +0000596void MachineInstr::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000597 cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000598}
599
Tanya Lattnerb1407622004-06-25 00:13:11 +0000600void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
Chris Lattnere3087892007-12-30 21:31:53 +0000601 // Specialize printing if op#0 is definition
Chris Lattner6a592272002-10-30 01:55:38 +0000602 unsigned StartOp = 0;
Dan Gohman92dfe202007-09-14 20:33:02 +0000603 if (getNumOperands() && getOperand(0).isRegister() && getOperand(0).isDef()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000604 getOperand(0).print(OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +0000605 OS << " = ";
606 ++StartOp; // Don't print this operand again!
607 }
Tanya Lattnerb1407622004-06-25 00:13:11 +0000608
Chris Lattnere3087892007-12-30 21:31:53 +0000609 OS << getInstrDescriptor()->Name;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000610
Chris Lattner6a592272002-10-30 01:55:38 +0000611 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
612 if (i != StartOp)
613 OS << ",";
614 OS << " ";
Chris Lattnerf7382302007-12-30 21:56:09 +0000615 getOperand(i).print(OS, TM);
Chris Lattner10491642002-10-30 00:48:05 +0000616 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000617
Chris Lattner10491642002-10-30 00:48:05 +0000618 OS << "\n";
619}
620