blob: 3025af9be96394b6808b0907b30ee6eab7cae03d [file] [log] [blame]
Chris Lattner85093632008-01-01 20:36:19 +00001//===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Methods common to all machine instructions.
11//
12//===----------------------------------------------------------------------===//
13
Nate Begeman6a38ec32008-02-14 07:39:30 +000014#include "llvm/Constants.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner1b989192007-12-31 04:13:23 +000016#include "llvm/Value.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnere45742f2008-01-01 01:12:31 +000018#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000019#include "llvm/CodeGen/PseudoSourceValue.h"
20#include "llvm/CodeGen/SelectionDAGNodes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Target/TargetMachine.h"
Evan Cheng13d1c292008-01-31 09:59:15 +000022#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner8eaa5a92008-01-07 07:42:25 +000023#include "llvm/Target/TargetInstrDesc.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000024#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Support/LeakDetector.h"
Dan Gohmanac6f8922008-07-07 20:32:02 +000026#include "llvm/Support/MathExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/Support/Streams.h"
28#include <ostream>
29using namespace llvm;
30
Chris Lattner7f2d3b82007-12-30 21:56:09 +000031//===----------------------------------------------------------------------===//
32// MachineOperand Implementation
33//===----------------------------------------------------------------------===//
34
Chris Lattnere45742f2008-01-01 01:12:31 +000035/// AddRegOperandToRegInfo - Add this register operand to the specified
36/// MachineRegisterInfo. If it is null, then the next/prev fields should be
37/// explicitly nulled out.
38void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) {
39 assert(isReg() && "Can only add reg operand to use lists");
40
41 // If the reginfo pointer is null, just explicitly null out or next/prev
42 // pointers, to ensure they are not garbage.
43 if (RegInfo == 0) {
44 Contents.Reg.Prev = 0;
45 Contents.Reg.Next = 0;
46 return;
47 }
48
49 // Otherwise, add this operand to the head of the registers use/def list.
Chris Lattner6fc812d2008-01-01 21:08:22 +000050 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
Chris Lattnere45742f2008-01-01 01:12:31 +000051
Chris Lattner6fc812d2008-01-01 21:08:22 +000052 // For SSA values, we prefer to keep the definition at the start of the list.
53 // we do this by skipping over the definition if it is at the head of the
54 // list.
55 if (*Head && (*Head)->isDef())
56 Head = &(*Head)->Contents.Reg.Next;
57
58 Contents.Reg.Next = *Head;
Chris Lattnere45742f2008-01-01 01:12:31 +000059 if (Contents.Reg.Next) {
60 assert(getReg() == Contents.Reg.Next->getReg() &&
61 "Different regs on the same list!");
62 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
63 }
64
Chris Lattner6fc812d2008-01-01 21:08:22 +000065 Contents.Reg.Prev = Head;
66 *Head = this;
Chris Lattnere45742f2008-01-01 01:12:31 +000067}
68
69void MachineOperand::setReg(unsigned Reg) {
70 if (getReg() == Reg) return; // No change.
71
72 // Otherwise, we have to change the register. If this operand is embedded
73 // into a machine function, we need to update the old and new register's
74 // use/def lists.
75 if (MachineInstr *MI = getParent())
76 if (MachineBasicBlock *MBB = MI->getParent())
77 if (MachineFunction *MF = MBB->getParent()) {
78 RemoveRegOperandFromRegInfo();
79 Contents.Reg.RegNo = Reg;
80 AddRegOperandToRegInfo(&MF->getRegInfo());
81 return;
82 }
83
84 // Otherwise, just change the register, no problem. :)
85 Contents.Reg.RegNo = Reg;
86}
87
88/// ChangeToImmediate - Replace this operand with a new immediate operand of
89/// the specified value. If an operand is known to be an immediate already,
90/// the setImm method should be used.
91void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
92 // If this operand is currently a register operand, and if this is in a
93 // function, deregister the operand from the register's use/def list.
94 if (isReg() && getParent() && getParent()->getParent() &&
95 getParent()->getParent()->getParent())
96 RemoveRegOperandFromRegInfo();
97
98 OpKind = MO_Immediate;
99 Contents.ImmVal = ImmVal;
100}
101
102/// ChangeToRegister - Replace this operand with a new register operand of
103/// the specified value. If an operand is known to be an register already,
104/// the setReg method should be used.
105void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
106 bool isKill, bool isDead) {
107 // If this operand is already a register operand, use setReg to update the
108 // register's use/def lists.
109 if (isReg()) {
110 setReg(Reg);
111 } else {
112 // Otherwise, change this to a register and set the reg#.
113 OpKind = MO_Register;
114 Contents.Reg.RegNo = Reg;
115
116 // If this operand is embedded in a function, add the operand to the
117 // register's use/def list.
118 if (MachineInstr *MI = getParent())
119 if (MachineBasicBlock *MBB = MI->getParent())
120 if (MachineFunction *MF = MBB->getParent())
121 AddRegOperandToRegInfo(&MF->getRegInfo());
122 }
123
124 IsDef = isDef;
125 IsImp = isImp;
126 IsKill = isKill;
127 IsDead = isDead;
128 SubReg = 0;
129}
130
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000131/// isIdenticalTo - Return true if this operand is identical to the specified
132/// operand.
133bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
134 if (getType() != Other.getType()) return false;
135
136 switch (getType()) {
137 default: assert(0 && "Unrecognized operand type");
138 case MachineOperand::MO_Register:
139 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
140 getSubReg() == Other.getSubReg();
141 case MachineOperand::MO_Immediate:
142 return getImm() == Other.getImm();
Nate Begeman6a38ec32008-02-14 07:39:30 +0000143 case MachineOperand::MO_FPImmediate:
144 return getFPImm() == Other.getFPImm();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000145 case MachineOperand::MO_MachineBasicBlock:
146 return getMBB() == Other.getMBB();
147 case MachineOperand::MO_FrameIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000148 return getIndex() == Other.getIndex();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000149 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000150 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000151 case MachineOperand::MO_JumpTableIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000152 return getIndex() == Other.getIndex();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000153 case MachineOperand::MO_GlobalAddress:
154 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
155 case MachineOperand::MO_ExternalSymbol:
156 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
157 getOffset() == Other.getOffset();
158 }
159}
160
161/// print - Print the specified machine operand.
162///
163void MachineOperand::print(std::ostream &OS, const TargetMachine *TM) const {
164 switch (getType()) {
165 case MachineOperand::MO_Register:
Dan Gohman1e57df32008-02-10 18:45:23 +0000166 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000167 OS << "%reg" << getReg();
168 } else {
169 // If the instruction is embedded into a basic block, we can find the
Chris Lattnere45742f2008-01-01 01:12:31 +0000170 // target info for the instruction.
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000171 if (TM == 0)
172 if (const MachineInstr *MI = getParent())
173 if (const MachineBasicBlock *MBB = MI->getParent())
174 if (const MachineFunction *MF = MBB->getParent())
175 TM = &MF->getTarget();
176
177 if (TM)
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000178 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000179 else
180 OS << "%mreg" << getReg();
181 }
182
183 if (isDef() || isKill() || isDead() || isImplicit()) {
184 OS << "<";
185 bool NeedComma = false;
186 if (isImplicit()) {
187 OS << (isDef() ? "imp-def" : "imp-use");
188 NeedComma = true;
189 } else if (isDef()) {
190 OS << "def";
191 NeedComma = true;
192 }
193 if (isKill() || isDead()) {
Bill Wendling733f0fd2008-02-24 00:56:13 +0000194 if (NeedComma) OS << ",";
195 if (isKill()) OS << "kill";
196 if (isDead()) OS << "dead";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000197 }
198 OS << ">";
199 }
200 break;
201 case MachineOperand::MO_Immediate:
202 OS << getImm();
203 break;
Nate Begeman6a38ec32008-02-14 07:39:30 +0000204 case MachineOperand::MO_FPImmediate:
205 if (getFPImm()->getType() == Type::FloatTy) {
206 OS << getFPImm()->getValueAPF().convertToFloat();
207 } else {
208 OS << getFPImm()->getValueAPF().convertToDouble();
209 }
210 break;
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000211 case MachineOperand::MO_MachineBasicBlock:
212 OS << "mbb<"
Chris Lattner6017d482007-12-30 23:10:15 +0000213 << ((Value*)getMBB()->getBasicBlock())->getName()
214 << "," << (void*)getMBB() << ">";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000215 break;
216 case MachineOperand::MO_FrameIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000217 OS << "<fi#" << getIndex() << ">";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000218 break;
219 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000220 OS << "<cp#" << getIndex();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000221 if (getOffset()) OS << "+" << getOffset();
222 OS << ">";
223 break;
224 case MachineOperand::MO_JumpTableIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000225 OS << "<jt#" << getIndex() << ">";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000226 break;
227 case MachineOperand::MO_GlobalAddress:
228 OS << "<ga:" << ((Value*)getGlobal())->getName();
229 if (getOffset()) OS << "+" << getOffset();
230 OS << ">";
231 break;
232 case MachineOperand::MO_ExternalSymbol:
233 OS << "<es:" << getSymbolName();
234 if (getOffset()) OS << "+" << getOffset();
235 OS << ">";
236 break;
237 default:
238 assert(0 && "Unrecognized operand type");
239 }
240}
241
242//===----------------------------------------------------------------------===//
Dan Gohmanac6f8922008-07-07 20:32:02 +0000243// MachineMemOperand Implementation
244//===----------------------------------------------------------------------===//
245
246MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f,
247 int64_t o, uint64_t s, unsigned int a)
248 : Offset(o), Size(s), V(v),
249 Flags((f & 7) | ((Log2_32(a) + 1) << 3)) {
250}
251
252//===----------------------------------------------------------------------===//
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000253// MachineInstr Implementation
254//===----------------------------------------------------------------------===//
255
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
257/// TID NULL and no operands.
258MachineInstr::MachineInstr()
Chris Lattner7ce487f2007-12-31 04:56:33 +0000259 : TID(0), NumImplicitOps(0), Parent(0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 // Make sure that we get added to a machine basicblock
261 LeakDetector::addGarbageObject(this);
262}
263
264void MachineInstr::addImplicitDefUseOperands() {
265 if (TID->ImplicitDefs)
Chris Lattner720b6cf2007-12-30 00:12:25 +0000266 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner63ab1f22007-12-30 00:41:17 +0000267 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 if (TID->ImplicitUses)
Chris Lattner720b6cf2007-12-30 00:12:25 +0000269 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner63ab1f22007-12-30 00:41:17 +0000270 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271}
272
273/// MachineInstr ctor - This constructor create a MachineInstr and add the
274/// implicit operands. It reserves space for number of operands specified by
Chris Lattner5b930372008-01-07 07:27:27 +0000275/// TargetInstrDesc or the numOperands if it is not zero. (for
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276/// instructions with variable number of operands).
Chris Lattner5b930372008-01-07 07:27:27 +0000277MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
Chris Lattner7ce487f2007-12-31 04:56:33 +0000278 : TID(&tid), NumImplicitOps(0), Parent(0) {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000279 if (!NoImp && TID->getImplicitDefs())
280 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281 NumImplicitOps++;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000282 if (!NoImp && TID->getImplicitUses())
283 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 NumImplicitOps++;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000285 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Chengbdf72b42007-10-13 02:23:01 +0000286 if (!NoImp)
287 addImplicitDefUseOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 // Make sure that we get added to a machine basicblock
289 LeakDetector::addGarbageObject(this);
290}
291
292/// MachineInstr ctor - Work exactly the same as the ctor above, except that the
293/// MachineInstr is created and added to the end of the specified basic block.
294///
295MachineInstr::MachineInstr(MachineBasicBlock *MBB,
Chris Lattner5b930372008-01-07 07:27:27 +0000296 const TargetInstrDesc &tid)
Chris Lattner7ce487f2007-12-31 04:56:33 +0000297 : TID(&tid), NumImplicitOps(0), Parent(0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 assert(MBB && "Cannot use inserting ctor with null basic block!");
299 if (TID->ImplicitDefs)
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000300 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 NumImplicitOps++;
302 if (TID->ImplicitUses)
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000303 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304 NumImplicitOps++;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000305 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 addImplicitDefUseOperands();
307 // Make sure that we get added to a machine basicblock
308 LeakDetector::addGarbageObject(this);
309 MBB->push_back(this); // Add instruction to end of basic block!
310}
311
312/// MachineInstr ctor - Copies MachineInstr arg exactly
313///
314MachineInstr::MachineInstr(const MachineInstr &MI) {
Chris Lattner5b930372008-01-07 07:27:27 +0000315 TID = &MI.getDesc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 NumImplicitOps = MI.NumImplicitOps;
317 Operands.reserve(MI.getNumOperands());
Dan Gohman12a9c082008-02-06 22:27:42 +0000318 MemOperands = MI.MemOperands;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319
320 // Add operands
Chris Lattnere722c3f2007-12-30 06:11:04 +0000321 for (unsigned i = 0; i != MI.getNumOperands(); ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 Operands.push_back(MI.getOperand(i));
Chris Lattnere722c3f2007-12-30 06:11:04 +0000323 Operands.back().ParentMI = this;
324 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325
326 // Set parent, next, and prev to null
Chris Lattner7ce487f2007-12-31 04:56:33 +0000327 Parent = 0;
328 Prev = 0;
329 Next = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330}
331
332
333MachineInstr::~MachineInstr() {
334 LeakDetector::removeGarbageObject(this);
Chris Lattnere722c3f2007-12-30 06:11:04 +0000335#ifndef NDEBUG
Chris Lattnere45742f2008-01-01 01:12:31 +0000336 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere722c3f2007-12-30 06:11:04 +0000337 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Chris Lattnere45742f2008-01-01 01:12:31 +0000338 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
339 "Reg operand def/use list corrupted");
340 }
Chris Lattnere722c3f2007-12-30 06:11:04 +0000341#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342}
343
344/// getOpcode - Returns the opcode of this MachineInstr.
345///
Dan Gohman5f222be2007-09-14 20:08:19 +0000346int MachineInstr::getOpcode() const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347 return TID->Opcode;
348}
349
Chris Lattnere45742f2008-01-01 01:12:31 +0000350/// getRegInfo - If this instruction is embedded into a MachineFunction,
351/// return the MachineRegisterInfo object for the current function, otherwise
352/// return null.
353MachineRegisterInfo *MachineInstr::getRegInfo() {
354 if (MachineBasicBlock *MBB = getParent())
355 if (MachineFunction *MF = MBB->getParent())
356 return &MF->getRegInfo();
357 return 0;
358}
359
360/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
361/// this instruction from their respective use lists. This requires that the
362/// operands already be on their use lists.
363void MachineInstr::RemoveRegOperandsFromUseLists() {
364 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
365 if (Operands[i].isReg())
366 Operands[i].RemoveRegOperandFromRegInfo();
367 }
368}
369
370/// AddRegOperandsToUseLists - Add all of the register operands in
371/// this instruction from their respective use lists. This requires that the
372/// operands not be on their use lists yet.
373void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
374 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
375 if (Operands[i].isReg())
376 Operands[i].AddRegOperandToRegInfo(&RegInfo);
377 }
378}
379
380
381/// addOperand - Add the specified operand to the instruction. If it is an
382/// implicit operand, it is added to the end of the operand list. If it is
383/// an explicit operand it is added at the end of the explicit operand list
384/// (before the first implicit operand).
385void MachineInstr::addOperand(const MachineOperand &Op) {
386 bool isImpReg = Op.isReg() && Op.isImplicit();
387 assert((isImpReg || !OperandsComplete()) &&
388 "Trying to add an operand to a machine instr that is already done!");
389
390 // If we are adding the operand to the end of the list, our job is simpler.
391 // This is true most of the time, so this is a reasonable optimization.
392 if (isImpReg || NumImplicitOps == 0) {
393 // We can only do this optimization if we know that the operand list won't
394 // reallocate.
395 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
396 Operands.push_back(Op);
397
398 // Set the parent of the operand.
399 Operands.back().ParentMI = this;
400
401 // If the operand is a register, update the operand's use list.
402 if (Op.isReg())
403 Operands.back().AddRegOperandToRegInfo(getRegInfo());
404 return;
405 }
406 }
407
408 // Otherwise, we have to insert a real operand before any implicit ones.
409 unsigned OpNo = Operands.size()-NumImplicitOps;
410
411 MachineRegisterInfo *RegInfo = getRegInfo();
412
413 // If this instruction isn't embedded into a function, then we don't need to
414 // update any operand lists.
415 if (RegInfo == 0) {
416 // Simple insertion, no reginfo update needed for other register operands.
417 Operands.insert(Operands.begin()+OpNo, Op);
418 Operands[OpNo].ParentMI = this;
419
420 // Do explicitly set the reginfo for this operand though, to ensure the
421 // next/prev fields are properly nulled out.
422 if (Operands[OpNo].isReg())
423 Operands[OpNo].AddRegOperandToRegInfo(0);
424
425 } else if (Operands.size()+1 <= Operands.capacity()) {
426 // Otherwise, we have to remove register operands from their register use
427 // list, add the operand, then add the register operands back to their use
428 // list. This also must handle the case when the operand list reallocates
429 // to somewhere else.
430
431 // If insertion of this operand won't cause reallocation of the operand
432 // list, just remove the implicit operands, add the operand, then re-add all
433 // the rest of the operands.
434 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
435 assert(Operands[i].isReg() && "Should only be an implicit reg!");
436 Operands[i].RemoveRegOperandFromRegInfo();
437 }
438
439 // Add the operand. If it is a register, add it to the reg list.
440 Operands.insert(Operands.begin()+OpNo, Op);
441 Operands[OpNo].ParentMI = this;
442
443 if (Operands[OpNo].isReg())
444 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
445
446 // Re-add all the implicit ops.
447 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
448 assert(Operands[i].isReg() && "Should only be an implicit reg!");
449 Operands[i].AddRegOperandToRegInfo(RegInfo);
450 }
451 } else {
452 // Otherwise, we will be reallocating the operand list. Remove all reg
453 // operands from their list, then readd them after the operand list is
454 // reallocated.
455 RemoveRegOperandsFromUseLists();
456
457 Operands.insert(Operands.begin()+OpNo, Op);
458 Operands[OpNo].ParentMI = this;
459
460 // Re-add all the operands.
461 AddRegOperandsToUseLists(*RegInfo);
462 }
463}
464
465/// RemoveOperand - Erase an operand from an instruction, leaving it with one
466/// fewer operand than it started with.
467///
468void MachineInstr::RemoveOperand(unsigned OpNo) {
469 assert(OpNo < Operands.size() && "Invalid operand number");
470
471 // Special case removing the last one.
472 if (OpNo == Operands.size()-1) {
473 // If needed, remove from the reg def/use list.
474 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
475 Operands.back().RemoveRegOperandFromRegInfo();
476
477 Operands.pop_back();
478 return;
479 }
480
481 // Otherwise, we are removing an interior operand. If we have reginfo to
482 // update, remove all operands that will be shifted down from their reg lists,
483 // move everything down, then re-add them.
484 MachineRegisterInfo *RegInfo = getRegInfo();
485 if (RegInfo) {
486 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
487 if (Operands[i].isReg())
488 Operands[i].RemoveRegOperandFromRegInfo();
489 }
490 }
491
492 Operands.erase(Operands.begin()+OpNo);
493
494 if (RegInfo) {
495 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
496 if (Operands[i].isReg())
497 Operands[i].AddRegOperandToRegInfo(RegInfo);
498 }
499 }
500}
501
502
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503/// removeFromParent - This method unlinks 'this' from the containing basic
504/// block, and returns it, but does not delete it.
505MachineInstr *MachineInstr::removeFromParent() {
506 assert(getParent() && "Not embedded in a basic block!");
507 getParent()->remove(this);
508 return this;
509}
510
511
512/// OperandComplete - Return true if it's illegal to add a new operand
513///
514bool MachineInstr::OperandsComplete() const {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000515 unsigned short NumOperands = TID->getNumOperands();
Chris Lattner2fb37c02008-01-07 05:19:29 +0000516 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 return true; // Broken: we have all the operands of this instruction!
518 return false;
519}
520
521/// getNumExplicitOperands - Returns the number of non-implicit operands.
522///
523unsigned MachineInstr::getNumExplicitOperands() const {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000524 unsigned NumOperands = TID->getNumOperands();
Chris Lattner2fb37c02008-01-07 05:19:29 +0000525 if (!TID->isVariadic())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 return NumOperands;
527
528 for (unsigned e = getNumOperands(); NumOperands != e; ++NumOperands) {
529 const MachineOperand &MO = getOperand(NumOperands);
530 if (!MO.isRegister() || !MO.isImplicit())
531 NumOperands++;
532 }
533 return NumOperands;
534}
535
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536
Dan Gohmanfa607c92008-07-01 00:05:16 +0000537/// isLabel - Returns true if the MachineInstr represents a label.
538///
539bool MachineInstr::isLabel() const {
540 return getOpcode() == TargetInstrInfo::DBG_LABEL ||
541 getOpcode() == TargetInstrInfo::EH_LABEL ||
542 getOpcode() == TargetInstrInfo::GC_LABEL;
543}
544
Evan Cheng13d1c292008-01-31 09:59:15 +0000545/// isDebugLabel - Returns true if the MachineInstr represents a debug label.
546///
547bool MachineInstr::isDebugLabel() const {
Dan Gohmanfa607c92008-07-01 00:05:16 +0000548 return getOpcode() == TargetInstrInfo::DBG_LABEL;
Evan Cheng13d1c292008-01-31 09:59:15 +0000549}
550
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000551/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
552/// the specific register or -1 if it is not found. It further tightening
553/// the search criteria to a use that kills the register if isKill is true.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000554int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
555 const TargetRegisterInfo *TRI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
557 const MachineOperand &MO = getOperand(i);
Evan Chengc7daf1f2008-03-05 00:59:57 +0000558 if (!MO.isRegister() || !MO.isUse())
559 continue;
560 unsigned MOReg = MO.getReg();
561 if (!MOReg)
562 continue;
563 if (MOReg == Reg ||
564 (TRI &&
565 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
566 TargetRegisterInfo::isPhysicalRegister(Reg) &&
567 TRI->isSubRegister(MOReg, Reg)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 if (!isKill || MO.isKill())
569 return i;
570 }
571 return -1;
572}
573
Evan Chengc7daf1f2008-03-05 00:59:57 +0000574/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman2f51e1f2008-05-06 00:20:10 +0000575/// the specified register or -1 if it is not found. If isDead is true, defs
576/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
577/// also checks if there is a def of a super-register.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000578int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead,
579 const TargetRegisterInfo *TRI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengc7daf1f2008-03-05 00:59:57 +0000581 const MachineOperand &MO = getOperand(i);
582 if (!MO.isRegister() || !MO.isDef())
583 continue;
584 unsigned MOReg = MO.getReg();
585 if (MOReg == Reg ||
586 (TRI &&
587 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
588 TargetRegisterInfo::isPhysicalRegister(Reg) &&
589 TRI->isSubRegister(MOReg, Reg)))
590 if (!isDead || MO.isDead())
591 return i;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 }
Evan Chengc7daf1f2008-03-05 00:59:57 +0000593 return -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594}
595
596/// findFirstPredOperandIdx() - Find the index of the first operand in the
597/// operand list that is used to represent the predicate. It returns -1 if
598/// none is found.
599int MachineInstr::findFirstPredOperandIdx() const {
Chris Lattner5b930372008-01-07 07:27:27 +0000600 const TargetInstrDesc &TID = getDesc();
601 if (TID.isPredicable()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner5b930372008-01-07 07:27:27 +0000603 if (TID.OpInfo[i].isPredicate())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604 return i;
605 }
606
607 return -1;
608}
609
Evan Cheng687d1082007-10-12 08:50:34 +0000610/// isRegReDefinedByTwoAddr - Returns true if the Reg re-definition is due
611/// to two addr elimination.
612bool MachineInstr::isRegReDefinedByTwoAddr(unsigned Reg) const {
Chris Lattner5b930372008-01-07 07:27:27 +0000613 const TargetInstrDesc &TID = getDesc();
Evan Cheng687d1082007-10-12 08:50:34 +0000614 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
615 const MachineOperand &MO1 = getOperand(i);
616 if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) {
617 for (unsigned j = i+1; j < e; ++j) {
618 const MachineOperand &MO2 = getOperand(j);
619 if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg &&
Chris Lattner5b930372008-01-07 07:27:27 +0000620 TID.getOperandConstraint(j, TOI::TIED_TO) == (int)i)
Evan Cheng687d1082007-10-12 08:50:34 +0000621 return true;
622 }
623 }
624 }
625 return false;
626}
627
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000628/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
629///
630void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
631 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
632 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000633 if (!MO.isRegister() || (!MO.isKill() && !MO.isDead()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634 continue;
635 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
636 MachineOperand &MOp = getOperand(j);
637 if (!MOp.isIdenticalTo(MO))
638 continue;
639 if (MO.isKill())
640 MOp.setIsKill();
641 else
642 MOp.setIsDead();
643 break;
644 }
645 }
646}
647
648/// copyPredicates - Copies predicate operand(s) from MI.
649void MachineInstr::copyPredicates(const MachineInstr *MI) {
Chris Lattner5b930372008-01-07 07:27:27 +0000650 const TargetInstrDesc &TID = MI->getDesc();
Evan Chengbe856622008-03-13 00:44:09 +0000651 if (!TID.isPredicable())
652 return;
653 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
654 if (TID.OpInfo[i].isPredicate()) {
655 // Predicated operands must be last operands.
656 addOperand(MI->getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657 }
658 }
659}
660
Evan Chenge52c1912008-07-03 09:09:37 +0000661/// isSafeToMove - Return true if it is safe to move this instruction. If
662/// SawStore is set to true, it means that there is a store (or call) between
663/// the instruction's location and its intended destination.
Evan Chengbe856622008-03-13 00:44:09 +0000664bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII, bool &SawStore) {
665 // Ignore stuff that we obviously can't move.
666 if (TID->mayStore() || TID->isCall()) {
667 SawStore = true;
668 return false;
669 }
670 if (TID->isReturn() || TID->isBranch() || TID->hasUnmodeledSideEffects())
671 return false;
672
673 // See if this instruction does a load. If so, we have to guarantee that the
674 // loaded value doesn't change between the load and the its intended
675 // destination. The check for isInvariantLoad gives the targe the chance to
676 // classify the load as always returning a constant, e.g. a constant pool
677 // load.
678 if (TID->mayLoad() && !TII->isInvariantLoad(this)) {
679 // Otherwise, this is a real load. If there is a store between the load and
680 // end of block, we can't sink the load.
681 //
682 // FIXME: we can't do this transformation until we know that the load is
683 // not volatile, and machineinstrs don't keep this info. :(
684 //
685 //if (SawStore)
686 return false;
687 }
688 return true;
689}
690
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691void MachineInstr::dump() const {
692 cerr << " " << *this;
693}
694
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
Chris Lattner9607bb82007-12-30 21:31:53 +0000696 // Specialize printing if op#0 is definition
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697 unsigned StartOp = 0;
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000698 if (getNumOperands() && getOperand(0).isRegister() && getOperand(0).isDef()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000699 getOperand(0).print(OS, TM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 OS << " = ";
701 ++StartOp; // Don't print this operand again!
702 }
703
Chris Lattner5b930372008-01-07 07:27:27 +0000704 OS << getDesc().getName();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705
706 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000707 if (i != StartOp)
708 OS << ",";
709 OS << " ";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000710 getOperand(i).print(OS, TM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711 }
712
Dan Gohman12a9c082008-02-06 22:27:42 +0000713 if (getNumMemOperands() > 0) {
Dan Gohmanf738b652008-02-07 16:18:00 +0000714 OS << ", Mem:";
Dan Gohman12a9c082008-02-06 22:27:42 +0000715 for (unsigned i = 0; i < getNumMemOperands(); i++) {
Dan Gohman1fad9e62008-04-07 19:35:22 +0000716 const MachineMemOperand &MRO = getMemOperand(i);
Dan Gohman12a9c082008-02-06 22:27:42 +0000717 const Value *V = MRO.getValue();
718
Dan Gohman12a9c082008-02-06 22:27:42 +0000719 assert((MRO.isLoad() || MRO.isStore()) &&
720 "SV has to be a load, store or both.");
721
722 if (MRO.isVolatile())
723 OS << "Volatile ";
Dan Gohmanf738b652008-02-07 16:18:00 +0000724
Dan Gohman12a9c082008-02-06 22:27:42 +0000725 if (MRO.isLoad())
Dan Gohmanf738b652008-02-07 16:18:00 +0000726 OS << "LD";
Dan Gohman12a9c082008-02-06 22:27:42 +0000727 if (MRO.isStore())
Dan Gohmanf738b652008-02-07 16:18:00 +0000728 OS << "ST";
Dan Gohman12a9c082008-02-06 22:27:42 +0000729
Evan Cheng38dc79b2008-02-08 22:05:07 +0000730 OS << "(" << MRO.getSize() << "," << MRO.getAlignment() << ") [";
Dan Gohman12a9c082008-02-06 22:27:42 +0000731
Dan Gohmanf738b652008-02-07 16:18:00 +0000732 if (!V)
733 OS << "<unknown>";
734 else if (!V->getName().empty())
735 OS << V->getName();
Dan Gohman12a9c082008-02-06 22:27:42 +0000736 else if (isa<PseudoSourceValue>(V))
Dan Gohmanf738b652008-02-07 16:18:00 +0000737 OS << *V;
Dan Gohman12a9c082008-02-06 22:27:42 +0000738 else
Dan Gohmanf738b652008-02-07 16:18:00 +0000739 OS << V;
740
741 OS << " + " << MRO.getOffset() << "]";
Dan Gohman12a9c082008-02-06 22:27:42 +0000742 }
743 }
744
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000745 OS << "\n";
746}
747
Owen Anderson58060792008-01-24 01:10:07 +0000748bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman1e57df32008-02-10 18:45:23 +0000749 const TargetRegisterInfo *RegInfo,
Owen Anderson58060792008-01-24 01:10:07 +0000750 bool AddIfNotFound) {
Evan Cheng794d0f72008-04-16 09:41:59 +0000751 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Dan Gohman9d90c632008-07-03 01:18:51 +0000752 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Evan Cheng794d0f72008-04-16 09:41:59 +0000753 SmallVector<unsigned,4> DeadOps;
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +0000754 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
755 MachineOperand &MO = getOperand(i);
Evan Cheng794d0f72008-04-16 09:41:59 +0000756 if (!MO.isRegister() || !MO.isUse())
757 continue;
758 unsigned Reg = MO.getReg();
759 if (!Reg)
760 continue;
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +0000761
Evan Cheng794d0f72008-04-16 09:41:59 +0000762 if (Reg == IncomingReg) {
Dan Gohman9d90c632008-07-03 01:18:51 +0000763 MO.setIsKill();
764 return true;
765 }
766 if (hasAliases && MO.isKill() &&
767 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng794d0f72008-04-16 09:41:59 +0000768 // A super-register kill already exists.
769 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman9d90c632008-07-03 01:18:51 +0000770 return true;
771 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng794d0f72008-04-16 09:41:59 +0000772 DeadOps.push_back(i);
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +0000773 }
774 }
775
Evan Cheng794d0f72008-04-16 09:41:59 +0000776 // Trim unneeded kill operands.
777 while (!DeadOps.empty()) {
778 unsigned OpIdx = DeadOps.back();
779 if (getOperand(OpIdx).isImplicit())
780 RemoveOperand(OpIdx);
781 else
782 getOperand(OpIdx).setIsKill(false);
783 DeadOps.pop_back();
784 }
785
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +0000786 // If not found, this means an alias of one of the operands is killed. Add a
Owen Anderson58060792008-01-24 01:10:07 +0000787 // new implicit operand if required.
Dan Gohman9d90c632008-07-03 01:18:51 +0000788 if (AddIfNotFound) {
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +0000789 addOperand(MachineOperand::CreateReg(IncomingReg,
790 false /*IsDef*/,
791 true /*IsImp*/,
792 true /*IsKill*/));
Owen Anderson58060792008-01-24 01:10:07 +0000793 return true;
794 }
Dan Gohman9d90c632008-07-03 01:18:51 +0000795 return false;
Owen Anderson58060792008-01-24 01:10:07 +0000796}
797
798bool MachineInstr::addRegisterDead(unsigned IncomingReg,
Dan Gohman1e57df32008-02-10 18:45:23 +0000799 const TargetRegisterInfo *RegInfo,
Owen Anderson58060792008-01-24 01:10:07 +0000800 bool AddIfNotFound) {
Evan Cheng794d0f72008-04-16 09:41:59 +0000801 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Evan Chengdd562a02008-06-27 22:11:49 +0000802 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Evan Cheng794d0f72008-04-16 09:41:59 +0000803 SmallVector<unsigned,4> DeadOps;
Owen Anderson58060792008-01-24 01:10:07 +0000804 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
805 MachineOperand &MO = getOperand(i);
Evan Cheng794d0f72008-04-16 09:41:59 +0000806 if (!MO.isRegister() || !MO.isDef())
807 continue;
808 unsigned Reg = MO.getReg();
809 if (Reg == IncomingReg) {
810 MO.setIsDead();
Dan Gohman9d90c632008-07-03 01:18:51 +0000811 return true;
812 }
813 if (hasAliases && MO.isDead() &&
814 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng794d0f72008-04-16 09:41:59 +0000815 // There exists a super-register that's marked dead.
816 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman9d90c632008-07-03 01:18:51 +0000817 return true;
818 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng794d0f72008-04-16 09:41:59 +0000819 DeadOps.push_back(i);
Owen Anderson58060792008-01-24 01:10:07 +0000820 }
821 }
822
Evan Cheng794d0f72008-04-16 09:41:59 +0000823 // Trim unneeded dead operands.
824 while (!DeadOps.empty()) {
825 unsigned OpIdx = DeadOps.back();
826 if (getOperand(OpIdx).isImplicit())
827 RemoveOperand(OpIdx);
828 else
829 getOperand(OpIdx).setIsDead(false);
830 DeadOps.pop_back();
831 }
832
Owen Anderson58060792008-01-24 01:10:07 +0000833 // If not found, this means an alias of one of the operand is dead. Add a
834 // new implicit operand.
Dan Gohman9d90c632008-07-03 01:18:51 +0000835 if (AddIfNotFound) {
Owen Anderson58060792008-01-24 01:10:07 +0000836 addOperand(MachineOperand::CreateReg(IncomingReg, true/*IsDef*/,
837 true/*IsImp*/,false/*IsKill*/,
838 true/*IsDead*/));
839 return true;
840 }
Dan Gohman9d90c632008-07-03 01:18:51 +0000841 return false;
Owen Anderson58060792008-01-24 01:10:07 +0000842}