blob: c455b024e3f14ce993ead2b34d6db6c5100601ad [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"
Dan Gohman69de1932008-02-06 22:27:42 +000018#include "llvm/CodeGen/PseudoSourceValue.h"
19#include "llvm/CodeGen/SelectionDAGNodes.h"
Chris Lattner10491642002-10-30 00:48:05 +000020#include "llvm/Target/TargetMachine.h"
Evan Chengbb81d972008-01-31 09:59:15 +000021#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerf14cf852008-01-07 07:42:25 +000022#include "llvm/Target/TargetInstrDesc.h"
Chris Lattner2a79a092002-10-30 00:58:19 +000023#include "llvm/Target/MRegisterInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/LeakDetector.h"
Bill Wendlinga09362e2006-11-28 22:48:48 +000025#include "llvm/Support/Streams.h"
Jeff Cohenc21c5ee2006-12-15 22:57:14 +000026#include <ostream>
Chris Lattner0742b592004-02-23 18:38:20 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattnerf7382302007-12-30 21:56:09 +000029//===----------------------------------------------------------------------===//
30// MachineOperand Implementation
31//===----------------------------------------------------------------------===//
32
Chris Lattner62ed6b92008-01-01 01:12:31 +000033/// AddRegOperandToRegInfo - Add this register operand to the specified
34/// MachineRegisterInfo. If it is null, then the next/prev fields should be
35/// explicitly nulled out.
36void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) {
37 assert(isReg() && "Can only add reg operand to use lists");
38
39 // If the reginfo pointer is null, just explicitly null out or next/prev
40 // pointers, to ensure they are not garbage.
41 if (RegInfo == 0) {
42 Contents.Reg.Prev = 0;
43 Contents.Reg.Next = 0;
44 return;
45 }
46
47 // Otherwise, add this operand to the head of the registers use/def list.
Chris Lattner80fe5312008-01-01 21:08:22 +000048 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
Chris Lattner62ed6b92008-01-01 01:12:31 +000049
Chris Lattner80fe5312008-01-01 21:08:22 +000050 // For SSA values, we prefer to keep the definition at the start of the list.
51 // we do this by skipping over the definition if it is at the head of the
52 // list.
53 if (*Head && (*Head)->isDef())
54 Head = &(*Head)->Contents.Reg.Next;
55
56 Contents.Reg.Next = *Head;
Chris Lattner62ed6b92008-01-01 01:12:31 +000057 if (Contents.Reg.Next) {
58 assert(getReg() == Contents.Reg.Next->getReg() &&
59 "Different regs on the same list!");
60 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
61 }
62
Chris Lattner80fe5312008-01-01 21:08:22 +000063 Contents.Reg.Prev = Head;
64 *Head = this;
Chris Lattner62ed6b92008-01-01 01:12:31 +000065}
66
67void MachineOperand::setReg(unsigned Reg) {
68 if (getReg() == Reg) return; // No change.
69
70 // Otherwise, we have to change the register. If this operand is embedded
71 // into a machine function, we need to update the old and new register's
72 // use/def lists.
73 if (MachineInstr *MI = getParent())
74 if (MachineBasicBlock *MBB = MI->getParent())
75 if (MachineFunction *MF = MBB->getParent()) {
76 RemoveRegOperandFromRegInfo();
77 Contents.Reg.RegNo = Reg;
78 AddRegOperandToRegInfo(&MF->getRegInfo());
79 return;
80 }
81
82 // Otherwise, just change the register, no problem. :)
83 Contents.Reg.RegNo = Reg;
84}
85
86/// ChangeToImmediate - Replace this operand with a new immediate operand of
87/// the specified value. If an operand is known to be an immediate already,
88/// the setImm method should be used.
89void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
90 // If this operand is currently a register operand, and if this is in a
91 // function, deregister the operand from the register's use/def list.
92 if (isReg() && getParent() && getParent()->getParent() &&
93 getParent()->getParent()->getParent())
94 RemoveRegOperandFromRegInfo();
95
96 OpKind = MO_Immediate;
97 Contents.ImmVal = ImmVal;
98}
99
100/// ChangeToRegister - Replace this operand with a new register operand of
101/// the specified value. If an operand is known to be an register already,
102/// the setReg method should be used.
103void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
104 bool isKill, bool isDead) {
105 // If this operand is already a register operand, use setReg to update the
106 // register's use/def lists.
107 if (isReg()) {
108 setReg(Reg);
109 } else {
110 // Otherwise, change this to a register and set the reg#.
111 OpKind = MO_Register;
112 Contents.Reg.RegNo = Reg;
113
114 // If this operand is embedded in a function, add the operand to the
115 // register's use/def list.
116 if (MachineInstr *MI = getParent())
117 if (MachineBasicBlock *MBB = MI->getParent())
118 if (MachineFunction *MF = MBB->getParent())
119 AddRegOperandToRegInfo(&MF->getRegInfo());
120 }
121
122 IsDef = isDef;
123 IsImp = isImp;
124 IsKill = isKill;
125 IsDead = isDead;
126 SubReg = 0;
127}
128
Chris Lattnerf7382302007-12-30 21:56:09 +0000129/// isIdenticalTo - Return true if this operand is identical to the specified
130/// operand.
131bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
132 if (getType() != Other.getType()) return false;
133
134 switch (getType()) {
135 default: assert(0 && "Unrecognized operand type");
136 case MachineOperand::MO_Register:
137 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
138 getSubReg() == Other.getSubReg();
139 case MachineOperand::MO_Immediate:
140 return getImm() == Other.getImm();
141 case MachineOperand::MO_MachineBasicBlock:
142 return getMBB() == Other.getMBB();
143 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000144 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000145 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000146 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattnerf7382302007-12-30 21:56:09 +0000147 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000148 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000149 case MachineOperand::MO_GlobalAddress:
150 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
151 case MachineOperand::MO_ExternalSymbol:
152 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
153 getOffset() == Other.getOffset();
154 }
155}
156
157/// print - Print the specified machine operand.
158///
159void MachineOperand::print(std::ostream &OS, const TargetMachine *TM) const {
160 switch (getType()) {
161 case MachineOperand::MO_Register:
162 if (getReg() == 0 || MRegisterInfo::isVirtualRegister(getReg())) {
163 OS << "%reg" << getReg();
164 } else {
165 // If the instruction is embedded into a basic block, we can find the
Chris Lattner62ed6b92008-01-01 01:12:31 +0000166 // target info for the instruction.
Chris Lattnerf7382302007-12-30 21:56:09 +0000167 if (TM == 0)
168 if (const MachineInstr *MI = getParent())
169 if (const MachineBasicBlock *MBB = MI->getParent())
170 if (const MachineFunction *MF = MBB->getParent())
171 TM = &MF->getTarget();
172
173 if (TM)
174 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
175 else
176 OS << "%mreg" << getReg();
177 }
178
179 if (isDef() || isKill() || isDead() || isImplicit()) {
180 OS << "<";
181 bool NeedComma = false;
182 if (isImplicit()) {
183 OS << (isDef() ? "imp-def" : "imp-use");
184 NeedComma = true;
185 } else if (isDef()) {
186 OS << "def";
187 NeedComma = true;
188 }
189 if (isKill() || isDead()) {
190 if (NeedComma) OS << ",";
191 if (isKill()) OS << "kill";
192 if (isDead()) OS << "dead";
193 }
194 OS << ">";
195 }
196 break;
197 case MachineOperand::MO_Immediate:
198 OS << getImm();
199 break;
200 case MachineOperand::MO_MachineBasicBlock:
201 OS << "mbb<"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000202 << ((Value*)getMBB()->getBasicBlock())->getName()
203 << "," << (void*)getMBB() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000204 break;
205 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000206 OS << "<fi#" << getIndex() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000207 break;
208 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000209 OS << "<cp#" << getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000210 if (getOffset()) OS << "+" << getOffset();
211 OS << ">";
212 break;
213 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000214 OS << "<jt#" << getIndex() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000215 break;
216 case MachineOperand::MO_GlobalAddress:
217 OS << "<ga:" << ((Value*)getGlobal())->getName();
218 if (getOffset()) OS << "+" << getOffset();
219 OS << ">";
220 break;
221 case MachineOperand::MO_ExternalSymbol:
222 OS << "<es:" << getSymbolName();
223 if (getOffset()) OS << "+" << getOffset();
224 OS << ">";
225 break;
226 default:
227 assert(0 && "Unrecognized operand type");
228 }
229}
230
231//===----------------------------------------------------------------------===//
232// MachineInstr Implementation
233//===----------------------------------------------------------------------===//
234
Evan Chengc0f64ff2006-11-27 23:37:22 +0000235/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
Evan Cheng67f660c2006-11-30 07:08:44 +0000236/// TID NULL and no operands.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000237MachineInstr::MachineInstr()
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000238 : TID(0), NumImplicitOps(0), Parent(0) {
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000239 // Make sure that we get added to a machine basicblock
240 LeakDetector::addGarbageObject(this);
Chris Lattner72791222002-10-28 20:59:49 +0000241}
242
Evan Cheng67f660c2006-11-30 07:08:44 +0000243void MachineInstr::addImplicitDefUseOperands() {
244 if (TID->ImplicitDefs)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000245 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner8019f412007-12-30 00:41:17 +0000246 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng67f660c2006-11-30 07:08:44 +0000247 if (TID->ImplicitUses)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000248 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner8019f412007-12-30 00:41:17 +0000249 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Evan Chengd7de4962006-11-13 23:34:06 +0000250}
251
252/// MachineInstr ctor - This constructor create a MachineInstr and add the
Evan Chengc0f64ff2006-11-27 23:37:22 +0000253/// implicit operands. It reserves space for number of operands specified by
Chris Lattner749c6f62008-01-07 07:27:27 +0000254/// TargetInstrDesc or the numOperands if it is not zero. (for
Evan Chengc0f64ff2006-11-27 23:37:22 +0000255/// instructions with variable number of operands).
Chris Lattner749c6f62008-01-07 07:27:27 +0000256MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000257 : TID(&tid), NumImplicitOps(0), Parent(0) {
Chris Lattner349c4952008-01-07 03:13:06 +0000258 if (!NoImp && TID->getImplicitDefs())
259 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000260 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000261 if (!NoImp && TID->getImplicitUses())
262 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000263 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000264 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Chengfa945722007-10-13 02:23:01 +0000265 if (!NoImp)
266 addImplicitDefUseOperands();
Evan Chengd7de4962006-11-13 23:34:06 +0000267 // Make sure that we get added to a machine basicblock
268 LeakDetector::addGarbageObject(this);
269}
270
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000271/// MachineInstr ctor - Work exactly the same as the ctor above, except that the
272/// MachineInstr is created and added to the end of the specified basic block.
273///
Evan Chengc0f64ff2006-11-27 23:37:22 +0000274MachineInstr::MachineInstr(MachineBasicBlock *MBB,
Chris Lattner749c6f62008-01-07 07:27:27 +0000275 const TargetInstrDesc &tid)
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000276 : TID(&tid), NumImplicitOps(0), Parent(0) {
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000277 assert(MBB && "Cannot use inserting ctor with null basic block!");
Evan Cheng67f660c2006-11-30 07:08:44 +0000278 if (TID->ImplicitDefs)
Chris Lattner349c4952008-01-07 03:13:06 +0000279 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000280 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +0000281 if (TID->ImplicitUses)
Chris Lattner349c4952008-01-07 03:13:06 +0000282 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000283 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000284 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Cheng67f660c2006-11-30 07:08:44 +0000285 addImplicitDefUseOperands();
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000286 // Make sure that we get added to a machine basicblock
287 LeakDetector::addGarbageObject(this);
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000288 MBB->push_back(this); // Add instruction to end of basic block!
289}
290
Misha Brukmance22e762004-07-09 14:45:17 +0000291/// MachineInstr ctor - Copies MachineInstr arg exactly
292///
Tanya Lattner466b5342004-05-23 19:35:12 +0000293MachineInstr::MachineInstr(const MachineInstr &MI) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000294 TID = &MI.getDesc();
Evan Cheng6b2c05f2006-11-15 20:54:29 +0000295 NumImplicitOps = MI.NumImplicitOps;
Chris Lattner943b5e12006-05-04 19:14:44 +0000296 Operands.reserve(MI.getNumOperands());
Dan Gohman69de1932008-02-06 22:27:42 +0000297 MemOperands = MI.MemOperands;
Tanya Lattnerb5159ed2004-05-23 20:58:02 +0000298
Misha Brukmance22e762004-07-09 14:45:17 +0000299 // Add operands
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000300 for (unsigned i = 0; i != MI.getNumOperands(); ++i) {
Chris Lattner943b5e12006-05-04 19:14:44 +0000301 Operands.push_back(MI.getOperand(i));
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000302 Operands.back().ParentMI = this;
303 }
Tanya Lattner0c63e032004-05-24 03:14:18 +0000304
Misha Brukmance22e762004-07-09 14:45:17 +0000305 // Set parent, next, and prev to null
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000306 Parent = 0;
307 Prev = 0;
308 Next = 0;
Tanya Lattner466b5342004-05-23 19:35:12 +0000309}
310
311
Misha Brukmance22e762004-07-09 14:45:17 +0000312MachineInstr::~MachineInstr() {
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000313 LeakDetector::removeGarbageObject(this);
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000314#ifndef NDEBUG
Chris Lattner62ed6b92008-01-01 01:12:31 +0000315 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000316 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000317 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
318 "Reg operand def/use list corrupted");
319 }
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000320#endif
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000321}
322
Evan Cheng67f660c2006-11-30 07:08:44 +0000323/// getOpcode - Returns the opcode of this MachineInstr.
324///
Dan Gohmancb648f92007-09-14 20:08:19 +0000325int MachineInstr::getOpcode() const {
Evan Cheng67f660c2006-11-30 07:08:44 +0000326 return TID->Opcode;
327}
328
Chris Lattner62ed6b92008-01-01 01:12:31 +0000329/// getRegInfo - If this instruction is embedded into a MachineFunction,
330/// return the MachineRegisterInfo object for the current function, otherwise
331/// return null.
332MachineRegisterInfo *MachineInstr::getRegInfo() {
333 if (MachineBasicBlock *MBB = getParent())
334 if (MachineFunction *MF = MBB->getParent())
335 return &MF->getRegInfo();
336 return 0;
337}
338
339/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
340/// this instruction from their respective use lists. This requires that the
341/// operands already be on their use lists.
342void MachineInstr::RemoveRegOperandsFromUseLists() {
343 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
344 if (Operands[i].isReg())
345 Operands[i].RemoveRegOperandFromRegInfo();
346 }
347}
348
349/// AddRegOperandsToUseLists - Add all of the register operands in
350/// this instruction from their respective use lists. This requires that the
351/// operands not be on their use lists yet.
352void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
353 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
354 if (Operands[i].isReg())
355 Operands[i].AddRegOperandToRegInfo(&RegInfo);
356 }
357}
358
359
360/// addOperand - Add the specified operand to the instruction. If it is an
361/// implicit operand, it is added to the end of the operand list. If it is
362/// an explicit operand it is added at the end of the explicit operand list
363/// (before the first implicit operand).
364void MachineInstr::addOperand(const MachineOperand &Op) {
365 bool isImpReg = Op.isReg() && Op.isImplicit();
366 assert((isImpReg || !OperandsComplete()) &&
367 "Trying to add an operand to a machine instr that is already done!");
368
369 // If we are adding the operand to the end of the list, our job is simpler.
370 // This is true most of the time, so this is a reasonable optimization.
371 if (isImpReg || NumImplicitOps == 0) {
372 // We can only do this optimization if we know that the operand list won't
373 // reallocate.
374 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
375 Operands.push_back(Op);
376
377 // Set the parent of the operand.
378 Operands.back().ParentMI = this;
379
380 // If the operand is a register, update the operand's use list.
381 if (Op.isReg())
382 Operands.back().AddRegOperandToRegInfo(getRegInfo());
383 return;
384 }
385 }
386
387 // Otherwise, we have to insert a real operand before any implicit ones.
388 unsigned OpNo = Operands.size()-NumImplicitOps;
389
390 MachineRegisterInfo *RegInfo = getRegInfo();
391
392 // If this instruction isn't embedded into a function, then we don't need to
393 // update any operand lists.
394 if (RegInfo == 0) {
395 // Simple insertion, no reginfo update needed for other register operands.
396 Operands.insert(Operands.begin()+OpNo, Op);
397 Operands[OpNo].ParentMI = this;
398
399 // Do explicitly set the reginfo for this operand though, to ensure the
400 // next/prev fields are properly nulled out.
401 if (Operands[OpNo].isReg())
402 Operands[OpNo].AddRegOperandToRegInfo(0);
403
404 } else if (Operands.size()+1 <= Operands.capacity()) {
405 // Otherwise, we have to remove register operands from their register use
406 // list, add the operand, then add the register operands back to their use
407 // list. This also must handle the case when the operand list reallocates
408 // to somewhere else.
409
410 // If insertion of this operand won't cause reallocation of the operand
411 // list, just remove the implicit operands, add the operand, then re-add all
412 // the rest of the operands.
413 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
414 assert(Operands[i].isReg() && "Should only be an implicit reg!");
415 Operands[i].RemoveRegOperandFromRegInfo();
416 }
417
418 // Add the operand. If it is a register, add it to the reg list.
419 Operands.insert(Operands.begin()+OpNo, Op);
420 Operands[OpNo].ParentMI = this;
421
422 if (Operands[OpNo].isReg())
423 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
424
425 // Re-add all the implicit ops.
426 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
427 assert(Operands[i].isReg() && "Should only be an implicit reg!");
428 Operands[i].AddRegOperandToRegInfo(RegInfo);
429 }
430 } else {
431 // Otherwise, we will be reallocating the operand list. Remove all reg
432 // operands from their list, then readd them after the operand list is
433 // reallocated.
434 RemoveRegOperandsFromUseLists();
435
436 Operands.insert(Operands.begin()+OpNo, Op);
437 Operands[OpNo].ParentMI = this;
438
439 // Re-add all the operands.
440 AddRegOperandsToUseLists(*RegInfo);
441 }
442}
443
444/// RemoveOperand - Erase an operand from an instruction, leaving it with one
445/// fewer operand than it started with.
446///
447void MachineInstr::RemoveOperand(unsigned OpNo) {
448 assert(OpNo < Operands.size() && "Invalid operand number");
449
450 // Special case removing the last one.
451 if (OpNo == Operands.size()-1) {
452 // If needed, remove from the reg def/use list.
453 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
454 Operands.back().RemoveRegOperandFromRegInfo();
455
456 Operands.pop_back();
457 return;
458 }
459
460 // Otherwise, we are removing an interior operand. If we have reginfo to
461 // update, remove all operands that will be shifted down from their reg lists,
462 // move everything down, then re-add them.
463 MachineRegisterInfo *RegInfo = getRegInfo();
464 if (RegInfo) {
465 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
466 if (Operands[i].isReg())
467 Operands[i].RemoveRegOperandFromRegInfo();
468 }
469 }
470
471 Operands.erase(Operands.begin()+OpNo);
472
473 if (RegInfo) {
474 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
475 if (Operands[i].isReg())
476 Operands[i].AddRegOperandToRegInfo(RegInfo);
477 }
478 }
479}
480
481
Chris Lattner48d7c062006-04-17 21:35:41 +0000482/// removeFromParent - This method unlinks 'this' from the containing basic
483/// block, and returns it, but does not delete it.
484MachineInstr *MachineInstr::removeFromParent() {
485 assert(getParent() && "Not embedded in a basic block!");
486 getParent()->remove(this);
487 return this;
488}
489
490
Brian Gaeke21326fc2004-02-13 04:39:32 +0000491/// OperandComplete - Return true if it's illegal to add a new operand
492///
Chris Lattner2a90ba62004-02-12 16:09:53 +0000493bool MachineInstr::OperandsComplete() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000494 unsigned short NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000495 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000496 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000497 return false;
498}
499
Evan Cheng19e3f312007-05-15 01:26:09 +0000500/// getNumExplicitOperands - Returns the number of non-implicit operands.
501///
502unsigned MachineInstr::getNumExplicitOperands() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000503 unsigned NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000504 if (!TID->isVariadic())
Evan Cheng19e3f312007-05-15 01:26:09 +0000505 return NumOperands;
506
507 for (unsigned e = getNumOperands(); NumOperands != e; ++NumOperands) {
508 const MachineOperand &MO = getOperand(NumOperands);
509 if (!MO.isRegister() || !MO.isImplicit())
510 NumOperands++;
511 }
512 return NumOperands;
513}
514
Chris Lattner8ace2cd2006-10-20 22:39:59 +0000515
Evan Chengbb81d972008-01-31 09:59:15 +0000516/// isDebugLabel - Returns true if the MachineInstr represents a debug label.
517///
518bool MachineInstr::isDebugLabel() const {
519 return getOpcode() == TargetInstrInfo::LABEL && getOperand(1).getImm() == 0;
520}
521
Evan Chengfaa51072007-04-26 19:00:32 +0000522/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Evan Cheng32eb1f12007-03-26 22:37:45 +0000523/// the specific register or -1 if it is not found. It further tightening
Evan Cheng76d7e762007-02-23 01:04:26 +0000524/// the search criteria to a use that kills the register if isKill is true.
Evan Chengf277ee42007-05-29 18:35:22 +0000525int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill) const {
Evan Cheng576d1232006-12-06 08:27:42 +0000526 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengf277ee42007-05-29 18:35:22 +0000527 const MachineOperand &MO = getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000528 if (MO.isRegister() && MO.isUse() && MO.getReg() == Reg)
Evan Cheng76d7e762007-02-23 01:04:26 +0000529 if (!isKill || MO.isKill())
Evan Cheng32eb1f12007-03-26 22:37:45 +0000530 return i;
Evan Cheng576d1232006-12-06 08:27:42 +0000531 }
Evan Cheng32eb1f12007-03-26 22:37:45 +0000532 return -1;
Evan Cheng576d1232006-12-06 08:27:42 +0000533}
534
Evan Chengb371f452007-02-19 21:49:54 +0000535/// findRegisterDefOperand() - Returns the MachineOperand that is a def of
536/// the specific register or NULL if it is not found.
537MachineOperand *MachineInstr::findRegisterDefOperand(unsigned Reg) {
538 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
539 MachineOperand &MO = getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000540 if (MO.isRegister() && MO.isDef() && MO.getReg() == Reg)
Evan Chengb371f452007-02-19 21:49:54 +0000541 return &MO;
542 }
543 return NULL;
544}
Evan Cheng19e3f312007-05-15 01:26:09 +0000545
Evan Chengf277ee42007-05-29 18:35:22 +0000546/// findFirstPredOperandIdx() - Find the index of the first operand in the
547/// operand list that is used to represent the predicate. It returns -1 if
548/// none is found.
549int MachineInstr::findFirstPredOperandIdx() const {
Chris Lattner749c6f62008-01-07 07:27:27 +0000550 const TargetInstrDesc &TID = getDesc();
551 if (TID.isPredicable()) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000552 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner749c6f62008-01-07 07:27:27 +0000553 if (TID.OpInfo[i].isPredicate())
Evan Chengf277ee42007-05-29 18:35:22 +0000554 return i;
Evan Cheng19e3f312007-05-15 01:26:09 +0000555 }
556
Evan Chengf277ee42007-05-29 18:35:22 +0000557 return -1;
Evan Cheng19e3f312007-05-15 01:26:09 +0000558}
Evan Chengb371f452007-02-19 21:49:54 +0000559
Evan Cheng32dfbea2007-10-12 08:50:34 +0000560/// isRegReDefinedByTwoAddr - Returns true if the Reg re-definition is due
561/// to two addr elimination.
562bool MachineInstr::isRegReDefinedByTwoAddr(unsigned Reg) const {
Chris Lattner749c6f62008-01-07 07:27:27 +0000563 const TargetInstrDesc &TID = getDesc();
Evan Cheng32dfbea2007-10-12 08:50:34 +0000564 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
565 const MachineOperand &MO1 = getOperand(i);
566 if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) {
567 for (unsigned j = i+1; j < e; ++j) {
568 const MachineOperand &MO2 = getOperand(j);
569 if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg &&
Chris Lattner749c6f62008-01-07 07:27:27 +0000570 TID.getOperandConstraint(j, TOI::TIED_TO) == (int)i)
Evan Cheng32dfbea2007-10-12 08:50:34 +0000571 return true;
572 }
573 }
574 }
575 return false;
576}
577
Evan Cheng576d1232006-12-06 08:27:42 +0000578/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
579///
580void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
581 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
582 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000583 if (!MO.isRegister() || (!MO.isKill() && !MO.isDead()))
Evan Cheng576d1232006-12-06 08:27:42 +0000584 continue;
585 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
586 MachineOperand &MOp = getOperand(j);
587 if (!MOp.isIdenticalTo(MO))
588 continue;
589 if (MO.isKill())
590 MOp.setIsKill();
591 else
592 MOp.setIsDead();
593 break;
594 }
595 }
596}
597
Evan Cheng19e3f312007-05-15 01:26:09 +0000598/// copyPredicates - Copies predicate operand(s) from MI.
599void MachineInstr::copyPredicates(const MachineInstr *MI) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000600 const TargetInstrDesc &TID = MI->getDesc();
601 if (TID.isPredicable()) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000602 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000603 if (TID.OpInfo[i].isPredicate()) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000604 // Predicated operands must be last operands.
Chris Lattner8019f412007-12-30 00:41:17 +0000605 addOperand(MI->getOperand(i));
Evan Cheng19e3f312007-05-15 01:26:09 +0000606 }
607 }
608 }
609}
610
Brian Gaeke21326fc2004-02-13 04:39:32 +0000611void MachineInstr::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000612 cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000613}
614
Tanya Lattnerb1407622004-06-25 00:13:11 +0000615void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
Chris Lattnere3087892007-12-30 21:31:53 +0000616 // Specialize printing if op#0 is definition
Chris Lattner6a592272002-10-30 01:55:38 +0000617 unsigned StartOp = 0;
Dan Gohman92dfe202007-09-14 20:33:02 +0000618 if (getNumOperands() && getOperand(0).isRegister() && getOperand(0).isDef()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000619 getOperand(0).print(OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +0000620 OS << " = ";
621 ++StartOp; // Don't print this operand again!
622 }
Tanya Lattnerb1407622004-06-25 00:13:11 +0000623
Chris Lattner749c6f62008-01-07 07:27:27 +0000624 OS << getDesc().getName();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000625
Chris Lattner6a592272002-10-30 01:55:38 +0000626 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
627 if (i != StartOp)
628 OS << ",";
629 OS << " ";
Chris Lattnerf7382302007-12-30 21:56:09 +0000630 getOperand(i).print(OS, TM);
Chris Lattner10491642002-10-30 00:48:05 +0000631 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000632
Dan Gohman69de1932008-02-06 22:27:42 +0000633 if (getNumMemOperands() > 0) {
634 OS << ", SV:";
635 for (unsigned i = 0; i < getNumMemOperands(); i++) {
636 const MemOperand &MRO = getMemOperand(i);
637 const Value *V = MRO.getValue();
638
639 assert(V && "SV missing.");
640 assert((MRO.isLoad() || MRO.isStore()) &&
641 "SV has to be a load, store or both.");
642
643 if (MRO.isVolatile())
644 OS << "Volatile ";
645 if (MRO.isLoad())
646 OS << "LD ";
647 if (MRO.isStore())
648 OS << "ST ";
649
650 OS << MRO.getSize();
651
652 if (!V->getName().empty())
653 OS << "[" << V->getName() << " + " << MRO.getOffset() << "]";
654 else if (isa<PseudoSourceValue>(V))
655 OS << "[" << *V << " + " << MRO.getOffset() << "]";
656 else
657 OS << "[" << V << " + " << MRO.getOffset() << "]";
658 }
659 }
660
Chris Lattner10491642002-10-30 00:48:05 +0000661 OS << "\n";
662}
663
Owen Andersonb487e722008-01-24 01:10:07 +0000664bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
665 const MRegisterInfo *RegInfo,
666 bool AddIfNotFound) {
667 bool Found = false;
668 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
669 MachineOperand &MO = getOperand(i);
670 if (MO.isRegister() && MO.isUse()) {
671 unsigned Reg = MO.getReg();
672 if (!Reg)
673 continue;
674 if (Reg == IncomingReg) {
675 MO.setIsKill();
676 Found = true;
677 break;
678 } else if (MRegisterInfo::isPhysicalRegister(Reg) &&
679 MRegisterInfo::isPhysicalRegister(IncomingReg) &&
680 RegInfo->isSuperRegister(IncomingReg, Reg) &&
681 MO.isKill())
682 // A super-register kill already exists.
683 Found = true;
684 }
685 }
686
687 // If not found, this means an alias of one of the operand is killed. Add a
688 // new implicit operand if required.
689 if (!Found && AddIfNotFound) {
690 addOperand(MachineOperand::CreateReg(IncomingReg, false/*IsDef*/,
691 true/*IsImp*/,true/*IsKill*/));
692 return true;
693 }
694 return Found;
695}
696
697bool MachineInstr::addRegisterDead(unsigned IncomingReg,
698 const MRegisterInfo *RegInfo,
699 bool AddIfNotFound) {
700 bool Found = false;
701 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
702 MachineOperand &MO = getOperand(i);
703 if (MO.isRegister() && MO.isDef()) {
704 unsigned Reg = MO.getReg();
705 if (!Reg)
706 continue;
707 if (Reg == IncomingReg) {
708 MO.setIsDead();
709 Found = true;
710 break;
711 } else if (MRegisterInfo::isPhysicalRegister(Reg) &&
712 MRegisterInfo::isPhysicalRegister(IncomingReg) &&
713 RegInfo->isSuperRegister(IncomingReg, Reg) &&
714 MO.isDead())
715 // There exists a super-register that's marked dead.
716 return true;
717 }
718 }
719
720 // If not found, this means an alias of one of the operand is dead. Add a
721 // new implicit operand.
722 if (!Found && AddIfNotFound) {
723 addOperand(MachineOperand::CreateReg(IncomingReg, true/*IsDef*/,
724 true/*IsImp*/,false/*IsKill*/,
725 true/*IsDead*/));
726 return true;
727 }
728 return Found;
729}
730
731/// copyKillDeadInfo - copies killed/dead information from one instr to another
732void MachineInstr::copyKillDeadInfo(MachineInstr *OldMI,
733 const MRegisterInfo *RegInfo) {
734 // If the instruction defines any virtual registers, update the VarInfo,
735 // kill and dead information for the instruction.
736 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
737 MachineOperand &MO = OldMI->getOperand(i);
738 if (MO.isRegister() && MO.getReg() &&
739 MRegisterInfo::isVirtualRegister(MO.getReg())) {
740 unsigned Reg = MO.getReg();
741 if (MO.isDef()) {
742 if (MO.isDead()) {
743 MO.setIsDead(false);
744 addRegisterDead(Reg, RegInfo);
745 }
746 }
747 if (MO.isKill()) {
748 MO.setIsKill(false);
749 addRegisterKilled(Reg, RegInfo);
750 }
751 }
752 }
753}