blob: b8c8563eab45d2f5330cbeb01c08f4618c46fafb [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
14#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng2682ea02009-03-23 08:01:15 +000015#include "llvm/Constants.h"
16#include "llvm/InlineAsm.h"
Chris Lattner1b989192007-12-31 04:13:23 +000017#include "llvm/Value.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnere45742f2008-01-01 01:12:31 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000020#include "llvm/CodeGen/PseudoSourceValue.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"
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +000025#include "llvm/Analysis/DebugInfo.h"
Dan Gohman8b3b5172008-07-17 23:49:46 +000026#include "llvm/Support/LeakDetector.h"
Dan Gohmanac6f8922008-07-07 20:32:02 +000027#include "llvm/Support/MathExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Support/Streams.h"
Chris Lattner24ae2a92008-08-24 20:37:32 +000029#include "llvm/Support/raw_ostream.h"
Dan Gohman98beebe2008-08-20 15:58:01 +000030#include "llvm/ADT/FoldingSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include <ostream>
32using namespace llvm;
33
Chris Lattner7f2d3b82007-12-30 21:56:09 +000034//===----------------------------------------------------------------------===//
35// MachineOperand Implementation
36//===----------------------------------------------------------------------===//
37
Chris Lattnere45742f2008-01-01 01:12:31 +000038/// AddRegOperandToRegInfo - Add this register operand to the specified
39/// MachineRegisterInfo. If it is null, then the next/prev fields should be
40/// explicitly nulled out.
41void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000042 assert(isReg() && "Can only add reg operand to use lists");
Chris Lattnere45742f2008-01-01 01:12:31 +000043
44 // If the reginfo pointer is null, just explicitly null out or next/prev
45 // pointers, to ensure they are not garbage.
46 if (RegInfo == 0) {
47 Contents.Reg.Prev = 0;
48 Contents.Reg.Next = 0;
49 return;
50 }
51
52 // Otherwise, add this operand to the head of the registers use/def list.
Chris Lattner6fc812d2008-01-01 21:08:22 +000053 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
Chris Lattnere45742f2008-01-01 01:12:31 +000054
Chris Lattner6fc812d2008-01-01 21:08:22 +000055 // For SSA values, we prefer to keep the definition at the start of the list.
56 // we do this by skipping over the definition if it is at the head of the
57 // list.
58 if (*Head && (*Head)->isDef())
59 Head = &(*Head)->Contents.Reg.Next;
60
61 Contents.Reg.Next = *Head;
Chris Lattnere45742f2008-01-01 01:12:31 +000062 if (Contents.Reg.Next) {
63 assert(getReg() == Contents.Reg.Next->getReg() &&
64 "Different regs on the same list!");
65 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
66 }
67
Chris Lattner6fc812d2008-01-01 21:08:22 +000068 Contents.Reg.Prev = Head;
69 *Head = this;
Chris Lattnere45742f2008-01-01 01:12:31 +000070}
71
Dan Gohman8ff914c2009-04-15 01:17:37 +000072/// RemoveRegOperandFromRegInfo - Remove this register operand from the
73/// MachineRegisterInfo it is linked with.
74void MachineOperand::RemoveRegOperandFromRegInfo() {
75 assert(isOnRegUseList() && "Reg operand is not on a use list");
76 // Unlink this from the doubly linked list of operands.
77 MachineOperand *NextOp = Contents.Reg.Next;
78 *Contents.Reg.Prev = NextOp;
79 if (NextOp) {
80 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
81 NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
82 }
83 Contents.Reg.Prev = 0;
84 Contents.Reg.Next = 0;
85}
86
Chris Lattnere45742f2008-01-01 01:12:31 +000087void MachineOperand::setReg(unsigned Reg) {
88 if (getReg() == Reg) return; // No change.
89
90 // Otherwise, we have to change the register. If this operand is embedded
91 // into a machine function, we need to update the old and new register's
92 // use/def lists.
93 if (MachineInstr *MI = getParent())
94 if (MachineBasicBlock *MBB = MI->getParent())
95 if (MachineFunction *MF = MBB->getParent()) {
96 RemoveRegOperandFromRegInfo();
97 Contents.Reg.RegNo = Reg;
98 AddRegOperandToRegInfo(&MF->getRegInfo());
99 return;
100 }
101
102 // Otherwise, just change the register, no problem. :)
103 Contents.Reg.RegNo = Reg;
104}
105
106/// ChangeToImmediate - Replace this operand with a new immediate operand of
107/// the specified value. If an operand is known to be an immediate already,
108/// the setImm method should be used.
109void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
110 // If this operand is currently a register operand, and if this is in a
111 // function, deregister the operand from the register's use/def list.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000112 if (isReg() && getParent() && getParent()->getParent() &&
Chris Lattnere45742f2008-01-01 01:12:31 +0000113 getParent()->getParent()->getParent())
114 RemoveRegOperandFromRegInfo();
115
116 OpKind = MO_Immediate;
117 Contents.ImmVal = ImmVal;
118}
119
120/// ChangeToRegister - Replace this operand with a new register operand of
121/// the specified value. If an operand is known to be an register already,
122/// the setReg method should be used.
123void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Dale Johannesen1b0c5782008-09-14 01:44:36 +0000124 bool isKill, bool isDead) {
Chris Lattnere45742f2008-01-01 01:12:31 +0000125 // If this operand is already a register operand, use setReg to update the
126 // register's use/def lists.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000127 if (isReg()) {
Dale Johannesen1b0c5782008-09-14 01:44:36 +0000128 assert(!isEarlyClobber());
Chris Lattnere45742f2008-01-01 01:12:31 +0000129 setReg(Reg);
130 } else {
131 // Otherwise, change this to a register and set the reg#.
132 OpKind = MO_Register;
133 Contents.Reg.RegNo = Reg;
134
135 // If this operand is embedded in a function, add the operand to the
136 // register's use/def list.
137 if (MachineInstr *MI = getParent())
138 if (MachineBasicBlock *MBB = MI->getParent())
139 if (MachineFunction *MF = MBB->getParent())
140 AddRegOperandToRegInfo(&MF->getRegInfo());
141 }
142
143 IsDef = isDef;
144 IsImp = isImp;
145 IsKill = isKill;
146 IsDead = isDead;
Dale Johannesen1b0c5782008-09-14 01:44:36 +0000147 IsEarlyClobber = false;
Chris Lattnere45742f2008-01-01 01:12:31 +0000148 SubReg = 0;
149}
150
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000151/// isIdenticalTo - Return true if this operand is identical to the specified
152/// operand.
153bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
154 if (getType() != Other.getType()) return false;
155
156 switch (getType()) {
157 default: assert(0 && "Unrecognized operand type");
158 case MachineOperand::MO_Register:
159 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
160 getSubReg() == Other.getSubReg();
161 case MachineOperand::MO_Immediate:
162 return getImm() == Other.getImm();
Nate Begeman6a38ec32008-02-14 07:39:30 +0000163 case MachineOperand::MO_FPImmediate:
164 return getFPImm() == Other.getFPImm();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000165 case MachineOperand::MO_MachineBasicBlock:
166 return getMBB() == Other.getMBB();
167 case MachineOperand::MO_FrameIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000168 return getIndex() == Other.getIndex();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000169 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000170 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000171 case MachineOperand::MO_JumpTableIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000172 return getIndex() == Other.getIndex();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000173 case MachineOperand::MO_GlobalAddress:
174 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
175 case MachineOperand::MO_ExternalSymbol:
176 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
177 getOffset() == Other.getOffset();
178 }
179}
180
181/// print - Print the specified machine operand.
182///
183void MachineOperand::print(std::ostream &OS, const TargetMachine *TM) const {
Mon P Wang2f2cd302008-10-10 01:43:55 +0000184 raw_os_ostream RawOS(OS);
185 print(RawOS, TM);
186}
187
188void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000189 switch (getType()) {
190 case MachineOperand::MO_Register:
Dan Gohman1e57df32008-02-10 18:45:23 +0000191 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000192 OS << "%reg" << getReg();
193 } else {
194 // If the instruction is embedded into a basic block, we can find the
Chris Lattnere45742f2008-01-01 01:12:31 +0000195 // target info for the instruction.
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000196 if (TM == 0)
197 if (const MachineInstr *MI = getParent())
198 if (const MachineBasicBlock *MBB = MI->getParent())
199 if (const MachineFunction *MF = MBB->getParent())
200 TM = &MF->getTarget();
201
202 if (TM)
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000203 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000204 else
205 OS << "%mreg" << getReg();
206 }
Dan Gohman4849d102008-12-18 21:51:27 +0000207
208 if (getSubReg() != 0) {
209 OS << ":" << getSubReg();
210 }
211
Dale Johannesen94464072008-09-24 01:07:17 +0000212 if (isDef() || isKill() || isDead() || isImplicit() || isEarlyClobber()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000213 OS << "<";
214 bool NeedComma = false;
215 if (isImplicit()) {
Dale Johannesenbac3c812008-09-17 21:13:11 +0000216 if (NeedComma) OS << ",";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000217 OS << (isDef() ? "imp-def" : "imp-use");
218 NeedComma = true;
219 } else if (isDef()) {
Dale Johannesenbac3c812008-09-17 21:13:11 +0000220 if (NeedComma) OS << ",";
Dale Johannesen38438f72008-09-12 17:49:03 +0000221 if (isEarlyClobber())
222 OS << "earlyclobber,";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000223 OS << "def";
224 NeedComma = true;
225 }
226 if (isKill() || isDead()) {
Bill Wendling733f0fd2008-02-24 00:56:13 +0000227 if (NeedComma) OS << ",";
228 if (isKill()) OS << "kill";
229 if (isDead()) OS << "dead";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000230 }
231 OS << ">";
232 }
233 break;
234 case MachineOperand::MO_Immediate:
235 OS << getImm();
236 break;
Nate Begeman6a38ec32008-02-14 07:39:30 +0000237 case MachineOperand::MO_FPImmediate:
238 if (getFPImm()->getType() == Type::FloatTy) {
239 OS << getFPImm()->getValueAPF().convertToFloat();
240 } else {
241 OS << getFPImm()->getValueAPF().convertToDouble();
242 }
243 break;
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000244 case MachineOperand::MO_MachineBasicBlock:
245 OS << "mbb<"
Chris Lattner6017d482007-12-30 23:10:15 +0000246 << ((Value*)getMBB()->getBasicBlock())->getName()
247 << "," << (void*)getMBB() << ">";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000248 break;
249 case MachineOperand::MO_FrameIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000250 OS << "<fi#" << getIndex() << ">";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000251 break;
252 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000253 OS << "<cp#" << getIndex();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000254 if (getOffset()) OS << "+" << getOffset();
255 OS << ">";
256 break;
257 case MachineOperand::MO_JumpTableIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000258 OS << "<jt#" << getIndex() << ">";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000259 break;
260 case MachineOperand::MO_GlobalAddress:
261 OS << "<ga:" << ((Value*)getGlobal())->getName();
262 if (getOffset()) OS << "+" << getOffset();
263 OS << ">";
264 break;
265 case MachineOperand::MO_ExternalSymbol:
266 OS << "<es:" << getSymbolName();
267 if (getOffset()) OS << "+" << getOffset();
268 OS << ">";
269 break;
270 default:
271 assert(0 && "Unrecognized operand type");
272 }
273}
274
275//===----------------------------------------------------------------------===//
Dan Gohmanac6f8922008-07-07 20:32:02 +0000276// MachineMemOperand Implementation
277//===----------------------------------------------------------------------===//
278
279MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f,
280 int64_t o, uint64_t s, unsigned int a)
281 : Offset(o), Size(s), V(v),
282 Flags((f & 7) | ((Log2_32(a) + 1) << 3)) {
Dan Gohman9b4f0792008-07-08 23:47:04 +0000283 assert(isPowerOf2_32(a) && "Alignment is not a power of 2!");
Dan Gohman78f9a462008-07-16 15:56:42 +0000284 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmanac6f8922008-07-07 20:32:02 +0000285}
286
Dan Gohman98beebe2008-08-20 15:58:01 +0000287/// Profile - Gather unique data for the object.
288///
289void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
290 ID.AddInteger(Offset);
291 ID.AddInteger(Size);
292 ID.AddPointer(V);
293 ID.AddInteger(Flags);
294}
295
Dan Gohmanac6f8922008-07-07 20:32:02 +0000296//===----------------------------------------------------------------------===//
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000297// MachineInstr Implementation
298//===----------------------------------------------------------------------===//
299
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
301/// TID NULL and no operands.
302MachineInstr::MachineInstr()
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000303 : TID(0), NumImplicitOps(0), Parent(0), debugLoc(DebugLoc::getUnknownLoc()) {
Dan Gohman8b3b5172008-07-17 23:49:46 +0000304 // Make sure that we get added to a machine basicblock
305 LeakDetector::addGarbageObject(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306}
307
308void MachineInstr::addImplicitDefUseOperands() {
309 if (TID->ImplicitDefs)
Chris Lattner720b6cf2007-12-30 00:12:25 +0000310 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner63ab1f22007-12-30 00:41:17 +0000311 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 if (TID->ImplicitUses)
Chris Lattner720b6cf2007-12-30 00:12:25 +0000313 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner63ab1f22007-12-30 00:41:17 +0000314 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315}
316
317/// MachineInstr ctor - This constructor create a MachineInstr and add the
318/// implicit operands. It reserves space for number of operands specified by
Chris Lattner5b930372008-01-07 07:27:27 +0000319/// TargetInstrDesc or the numOperands if it is not zero. (for
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320/// instructions with variable number of operands).
Chris Lattner5b930372008-01-07 07:27:27 +0000321MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000322 : TID(&tid), NumImplicitOps(0), Parent(0),
323 debugLoc(DebugLoc::getUnknownLoc()) {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000324 if (!NoImp && TID->getImplicitDefs())
325 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 NumImplicitOps++;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000327 if (!NoImp && TID->getImplicitUses())
328 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329 NumImplicitOps++;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000330 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Chengbdf72b42007-10-13 02:23:01 +0000331 if (!NoImp)
332 addImplicitDefUseOperands();
Dan Gohman8b3b5172008-07-17 23:49:46 +0000333 // Make sure that we get added to a machine basicblock
334 LeakDetector::addGarbageObject(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335}
336
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000337/// MachineInstr ctor - As above, but with a DebugLoc.
338MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl,
339 bool NoImp)
340 : TID(&tid), NumImplicitOps(0), Parent(0), debugLoc(dl) {
341 if (!NoImp && TID->getImplicitDefs())
342 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
343 NumImplicitOps++;
344 if (!NoImp && TID->getImplicitUses())
345 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
346 NumImplicitOps++;
347 Operands.reserve(NumImplicitOps + TID->getNumOperands());
348 if (!NoImp)
349 addImplicitDefUseOperands();
350 // Make sure that we get added to a machine basicblock
351 LeakDetector::addGarbageObject(this);
352}
353
354/// MachineInstr ctor - Work exactly the same as the ctor two above, except
355/// that the MachineInstr is created and added to the end of the specified
356/// basic block.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357///
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000358MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid)
359 : TID(&tid), NumImplicitOps(0), Parent(0),
360 debugLoc(DebugLoc::getUnknownLoc()) {
361 assert(MBB && "Cannot use inserting ctor with null basic block!");
362 if (TID->ImplicitDefs)
363 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
364 NumImplicitOps++;
365 if (TID->ImplicitUses)
366 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
367 NumImplicitOps++;
368 Operands.reserve(NumImplicitOps + TID->getNumOperands());
369 addImplicitDefUseOperands();
370 // Make sure that we get added to a machine basicblock
371 LeakDetector::addGarbageObject(this);
372 MBB->push_back(this); // Add instruction to end of basic block!
373}
374
375/// MachineInstr ctor - As above, but with a DebugLoc.
376///
377MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl,
Chris Lattner5b930372008-01-07 07:27:27 +0000378 const TargetInstrDesc &tid)
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000379 : TID(&tid), NumImplicitOps(0), Parent(0), debugLoc(dl) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 assert(MBB && "Cannot use inserting ctor with null basic block!");
381 if (TID->ImplicitDefs)
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000382 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 NumImplicitOps++;
384 if (TID->ImplicitUses)
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000385 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 NumImplicitOps++;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000387 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388 addImplicitDefUseOperands();
Dan Gohman8b3b5172008-07-17 23:49:46 +0000389 // Make sure that we get added to a machine basicblock
390 LeakDetector::addGarbageObject(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 MBB->push_back(this); // Add instruction to end of basic block!
392}
393
394/// MachineInstr ctor - Copies MachineInstr arg exactly
395///
Evan Cheng4ce1a522008-07-19 00:37:25 +0000396MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000397 : TID(&MI.getDesc()), NumImplicitOps(0), Parent(0),
398 debugLoc(MI.getDebugLoc()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 Operands.reserve(MI.getNumOperands());
400
401 // Add operands
Evan Cheng4ce1a522008-07-19 00:37:25 +0000402 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
403 addOperand(MI.getOperand(i));
404 NumImplicitOps = MI.NumImplicitOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405
Dan Gohman221a4372008-07-07 23:14:23 +0000406 // Add memory operands.
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000407 for (std::list<MachineMemOperand>::const_iterator i = MI.memoperands_begin(),
Dan Gohman221a4372008-07-07 23:14:23 +0000408 j = MI.memoperands_end(); i != j; ++i)
409 addMemOperand(MF, *i);
410
411 // Set parent to null.
Chris Lattner7ce487f2007-12-31 04:56:33 +0000412 Parent = 0;
Dan Gohmance232952008-07-21 18:47:29 +0000413
414 LeakDetector::addGarbageObject(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415}
416
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417MachineInstr::~MachineInstr() {
Dan Gohman8b3b5172008-07-17 23:49:46 +0000418 LeakDetector::removeGarbageObject(this);
Dan Gohman221a4372008-07-07 23:14:23 +0000419 assert(MemOperands.empty() &&
420 "MachineInstr being deleted with live memoperands!");
Chris Lattnere722c3f2007-12-30 06:11:04 +0000421#ifndef NDEBUG
Chris Lattnere45742f2008-01-01 01:12:31 +0000422 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere722c3f2007-12-30 06:11:04 +0000423 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000424 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
Chris Lattnere45742f2008-01-01 01:12:31 +0000425 "Reg operand def/use list corrupted");
426 }
Chris Lattnere722c3f2007-12-30 06:11:04 +0000427#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428}
429
Chris Lattnere45742f2008-01-01 01:12:31 +0000430/// getRegInfo - If this instruction is embedded into a MachineFunction,
431/// return the MachineRegisterInfo object for the current function, otherwise
432/// return null.
433MachineRegisterInfo *MachineInstr::getRegInfo() {
434 if (MachineBasicBlock *MBB = getParent())
Dan Gohman07368822008-07-08 23:59:09 +0000435 return &MBB->getParent()->getRegInfo();
Chris Lattnere45742f2008-01-01 01:12:31 +0000436 return 0;
437}
438
439/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
440/// this instruction from their respective use lists. This requires that the
441/// operands already be on their use lists.
442void MachineInstr::RemoveRegOperandsFromUseLists() {
443 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000444 if (Operands[i].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000445 Operands[i].RemoveRegOperandFromRegInfo();
446 }
447}
448
449/// AddRegOperandsToUseLists - Add all of the register operands in
450/// this instruction from their respective use lists. This requires that the
451/// operands not be on their use lists yet.
452void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
453 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000454 if (Operands[i].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000455 Operands[i].AddRegOperandToRegInfo(&RegInfo);
456 }
457}
458
459
460/// addOperand - Add the specified operand to the instruction. If it is an
461/// implicit operand, it is added to the end of the operand list. If it is
462/// an explicit operand it is added at the end of the explicit operand list
463/// (before the first implicit operand).
464void MachineInstr::addOperand(const MachineOperand &Op) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000465 bool isImpReg = Op.isReg() && Op.isImplicit();
Chris Lattnere45742f2008-01-01 01:12:31 +0000466 assert((isImpReg || !OperandsComplete()) &&
467 "Trying to add an operand to a machine instr that is already done!");
468
Dan Gohmana0dff432008-12-09 22:45:08 +0000469 MachineRegisterInfo *RegInfo = getRegInfo();
470
Chris Lattnere45742f2008-01-01 01:12:31 +0000471 // If we are adding the operand to the end of the list, our job is simpler.
472 // This is true most of the time, so this is a reasonable optimization.
473 if (isImpReg || NumImplicitOps == 0) {
474 // We can only do this optimization if we know that the operand list won't
475 // reallocate.
476 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
477 Operands.push_back(Op);
478
479 // Set the parent of the operand.
480 Operands.back().ParentMI = this;
481
482 // If the operand is a register, update the operand's use list.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000483 if (Op.isReg())
Dan Gohmana0dff432008-12-09 22:45:08 +0000484 Operands.back().AddRegOperandToRegInfo(RegInfo);
Chris Lattnere45742f2008-01-01 01:12:31 +0000485 return;
486 }
487 }
488
489 // Otherwise, we have to insert a real operand before any implicit ones.
490 unsigned OpNo = Operands.size()-NumImplicitOps;
491
Chris Lattnere45742f2008-01-01 01:12:31 +0000492 // If this instruction isn't embedded into a function, then we don't need to
493 // update any operand lists.
494 if (RegInfo == 0) {
495 // Simple insertion, no reginfo update needed for other register operands.
496 Operands.insert(Operands.begin()+OpNo, Op);
497 Operands[OpNo].ParentMI = this;
498
499 // Do explicitly set the reginfo for this operand though, to ensure the
500 // next/prev fields are properly nulled out.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000501 if (Operands[OpNo].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000502 Operands[OpNo].AddRegOperandToRegInfo(0);
503
504 } else if (Operands.size()+1 <= Operands.capacity()) {
505 // Otherwise, we have to remove register operands from their register use
506 // list, add the operand, then add the register operands back to their use
507 // list. This also must handle the case when the operand list reallocates
508 // to somewhere else.
509
510 // If insertion of this operand won't cause reallocation of the operand
511 // list, just remove the implicit operands, add the operand, then re-add all
512 // the rest of the operands.
513 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000514 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattnere45742f2008-01-01 01:12:31 +0000515 Operands[i].RemoveRegOperandFromRegInfo();
516 }
517
518 // Add the operand. If it is a register, add it to the reg list.
519 Operands.insert(Operands.begin()+OpNo, Op);
520 Operands[OpNo].ParentMI = this;
521
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000522 if (Operands[OpNo].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000523 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
524
525 // Re-add all the implicit ops.
526 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000527 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattnere45742f2008-01-01 01:12:31 +0000528 Operands[i].AddRegOperandToRegInfo(RegInfo);
529 }
530 } else {
531 // Otherwise, we will be reallocating the operand list. Remove all reg
532 // operands from their list, then readd them after the operand list is
533 // reallocated.
534 RemoveRegOperandsFromUseLists();
535
536 Operands.insert(Operands.begin()+OpNo, Op);
537 Operands[OpNo].ParentMI = this;
538
539 // Re-add all the operands.
540 AddRegOperandsToUseLists(*RegInfo);
541 }
542}
543
544/// RemoveOperand - Erase an operand from an instruction, leaving it with one
545/// fewer operand than it started with.
546///
547void MachineInstr::RemoveOperand(unsigned OpNo) {
548 assert(OpNo < Operands.size() && "Invalid operand number");
549
550 // Special case removing the last one.
551 if (OpNo == Operands.size()-1) {
552 // If needed, remove from the reg def/use list.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000553 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
Chris Lattnere45742f2008-01-01 01:12:31 +0000554 Operands.back().RemoveRegOperandFromRegInfo();
555
556 Operands.pop_back();
557 return;
558 }
559
560 // Otherwise, we are removing an interior operand. If we have reginfo to
561 // update, remove all operands that will be shifted down from their reg lists,
562 // move everything down, then re-add them.
563 MachineRegisterInfo *RegInfo = getRegInfo();
564 if (RegInfo) {
565 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000566 if (Operands[i].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000567 Operands[i].RemoveRegOperandFromRegInfo();
568 }
569 }
570
571 Operands.erase(Operands.begin()+OpNo);
572
573 if (RegInfo) {
574 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000575 if (Operands[i].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000576 Operands[i].AddRegOperandToRegInfo(RegInfo);
577 }
578 }
579}
580
Dan Gohman221a4372008-07-07 23:14:23 +0000581/// addMemOperand - Add a MachineMemOperand to the machine instruction,
582/// referencing arbitrary storage.
583void MachineInstr::addMemOperand(MachineFunction &MF,
584 const MachineMemOperand &MO) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000585 MemOperands.push_back(MO);
Dan Gohman221a4372008-07-07 23:14:23 +0000586}
587
588/// clearMemOperands - Erase all of this MachineInstr's MachineMemOperands.
589void MachineInstr::clearMemOperands(MachineFunction &MF) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000590 MemOperands.clear();
Dan Gohman221a4372008-07-07 23:14:23 +0000591}
592
Chris Lattnere45742f2008-01-01 01:12:31 +0000593
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594/// removeFromParent - This method unlinks 'this' from the containing basic
595/// block, and returns it, but does not delete it.
596MachineInstr *MachineInstr::removeFromParent() {
597 assert(getParent() && "Not embedded in a basic block!");
598 getParent()->remove(this);
599 return this;
600}
601
602
Dan Gohman221a4372008-07-07 23:14:23 +0000603/// eraseFromParent - This method unlinks 'this' from the containing basic
604/// block, and deletes it.
605void MachineInstr::eraseFromParent() {
606 assert(getParent() && "Not embedded in a basic block!");
607 getParent()->erase(this);
608}
609
610
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611/// OperandComplete - Return true if it's illegal to add a new operand
612///
613bool MachineInstr::OperandsComplete() const {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000614 unsigned short NumOperands = TID->getNumOperands();
Chris Lattner2fb37c02008-01-07 05:19:29 +0000615 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 return true; // Broken: we have all the operands of this instruction!
617 return false;
618}
619
620/// getNumExplicitOperands - Returns the number of non-implicit operands.
621///
622unsigned MachineInstr::getNumExplicitOperands() const {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000623 unsigned NumOperands = TID->getNumOperands();
Chris Lattner2fb37c02008-01-07 05:19:29 +0000624 if (!TID->isVariadic())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000625 return NumOperands;
626
Dan Gohman3d880012009-04-15 17:59:11 +0000627 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
628 const MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000629 if (!MO.isReg() || !MO.isImplicit())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630 NumOperands++;
631 }
632 return NumOperands;
633}
634
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635
Dan Gohmanfa607c92008-07-01 00:05:16 +0000636/// isLabel - Returns true if the MachineInstr represents a label.
637///
638bool MachineInstr::isLabel() const {
639 return getOpcode() == TargetInstrInfo::DBG_LABEL ||
640 getOpcode() == TargetInstrInfo::EH_LABEL ||
641 getOpcode() == TargetInstrInfo::GC_LABEL;
642}
643
Evan Cheng13d1c292008-01-31 09:59:15 +0000644/// isDebugLabel - Returns true if the MachineInstr represents a debug label.
645///
646bool MachineInstr::isDebugLabel() const {
Dan Gohmanfa607c92008-07-01 00:05:16 +0000647 return getOpcode() == TargetInstrInfo::DBG_LABEL;
Evan Cheng13d1c292008-01-31 09:59:15 +0000648}
649
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
651/// the specific register or -1 if it is not found. It further tightening
652/// the search criteria to a use that kills the register if isKill is true.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000653int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
654 const TargetRegisterInfo *TRI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
656 const MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000657 if (!MO.isReg() || !MO.isUse())
Evan Chengc7daf1f2008-03-05 00:59:57 +0000658 continue;
659 unsigned MOReg = MO.getReg();
660 if (!MOReg)
661 continue;
662 if (MOReg == Reg ||
663 (TRI &&
664 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
665 TargetRegisterInfo::isPhysicalRegister(Reg) &&
666 TRI->isSubRegister(MOReg, Reg)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000667 if (!isKill || MO.isKill())
668 return i;
669 }
670 return -1;
671}
672
Evan Chengc7daf1f2008-03-05 00:59:57 +0000673/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman2f51e1f2008-05-06 00:20:10 +0000674/// the specified register or -1 if it is not found. If isDead is true, defs
675/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
676/// also checks if there is a def of a super-register.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000677int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead,
678 const TargetRegisterInfo *TRI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000679 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengc7daf1f2008-03-05 00:59:57 +0000680 const MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000681 if (!MO.isReg() || !MO.isDef())
Evan Chengc7daf1f2008-03-05 00:59:57 +0000682 continue;
683 unsigned MOReg = MO.getReg();
684 if (MOReg == Reg ||
685 (TRI &&
686 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
687 TargetRegisterInfo::isPhysicalRegister(Reg) &&
688 TRI->isSubRegister(MOReg, Reg)))
689 if (!isDead || MO.isDead())
690 return i;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691 }
Evan Chengc7daf1f2008-03-05 00:59:57 +0000692 return -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693}
694
695/// findFirstPredOperandIdx() - Find the index of the first operand in the
696/// operand list that is used to represent the predicate. It returns -1 if
697/// none is found.
698int MachineInstr::findFirstPredOperandIdx() const {
Chris Lattner5b930372008-01-07 07:27:27 +0000699 const TargetInstrDesc &TID = getDesc();
700 if (TID.isPredicable()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner5b930372008-01-07 07:27:27 +0000702 if (TID.OpInfo[i].isPredicate())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703 return i;
704 }
705
706 return -1;
707}
708
Bob Wilsonaded9952009-04-09 17:16:43 +0000709/// isRegTiedToUseOperand - Given the index of a register def operand,
710/// check if the register def is tied to a source operand, due to either
711/// two-address elimination or inline assembly constraints. Returns the
712/// first tied use operand index by reference is UseOpIdx is not null.
Jakob Stoklund Olesencb9f5b52009-04-29 20:57:16 +0000713bool MachineInstr::
714isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const {
Evan Cheng2682ea02009-03-23 08:01:15 +0000715 if (getOpcode() == TargetInstrInfo::INLINEASM) {
Bob Wilsonaded9952009-04-09 17:16:43 +0000716 assert(DefOpIdx >= 2);
717 const MachineOperand &MO = getOperand(DefOpIdx);
Chris Lattnerb1e84232009-04-09 23:33:34 +0000718 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
Evan Cheng2682ea02009-03-23 08:01:15 +0000719 return false;
720 // Determine the actual operand no corresponding to this index.
721 unsigned DefNo = 0;
722 for (unsigned i = 1, e = getNumOperands(); i < e; ) {
723 const MachineOperand &FMO = getOperand(i);
724 assert(FMO.isImm());
725 // Skip over this def.
726 i += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
Bob Wilsonaded9952009-04-09 17:16:43 +0000727 if (i > DefOpIdx)
Evan Cheng2682ea02009-03-23 08:01:15 +0000728 break;
729 ++DefNo;
730 }
731 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
732 const MachineOperand &FMO = getOperand(i);
733 if (!FMO.isImm())
734 continue;
735 if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse())
736 continue;
737 unsigned Idx;
738 if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) &&
Bob Wilsonaded9952009-04-09 17:16:43 +0000739 Idx == DefNo) {
740 if (UseOpIdx)
741 *UseOpIdx = (unsigned)i + 1;
Evan Cheng2682ea02009-03-23 08:01:15 +0000742 return true;
Bob Wilsonaded9952009-04-09 17:16:43 +0000743 }
Evan Cheng2682ea02009-03-23 08:01:15 +0000744 }
745 }
746
Bob Wilsonaded9952009-04-09 17:16:43 +0000747 assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!");
Chris Lattner5b930372008-01-07 07:27:27 +0000748 const TargetInstrDesc &TID = getDesc();
Evan Chengf1107fd2008-07-10 07:35:43 +0000749 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
750 const MachineOperand &MO = getOperand(i);
Dan Gohman4dbf8792008-12-05 05:45:42 +0000751 if (MO.isReg() && MO.isUse() &&
Bob Wilsonaded9952009-04-09 17:16:43 +0000752 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) {
753 if (UseOpIdx)
754 *UseOpIdx = (unsigned)i;
Evan Chengf1107fd2008-07-10 07:35:43 +0000755 return true;
Bob Wilsonaded9952009-04-09 17:16:43 +0000756 }
Evan Cheng687d1082007-10-12 08:50:34 +0000757 }
758 return false;
759}
760
Evan Cheng48555e82009-03-19 20:30:06 +0000761/// isRegTiedToDefOperand - Return true if the operand of the specified index
762/// is a register use and it is tied to an def operand. It also returns the def
763/// operand index by reference.
Jakob Stoklund Olesencb9f5b52009-04-29 20:57:16 +0000764bool MachineInstr::
765isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const {
Evan Cheng2682ea02009-03-23 08:01:15 +0000766 if (getOpcode() == TargetInstrInfo::INLINEASM) {
767 const MachineOperand &MO = getOperand(UseOpIdx);
Chris Lattner0d0e8a92009-04-09 16:50:43 +0000768 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0)
Evan Cheng2682ea02009-03-23 08:01:15 +0000769 return false;
770 assert(UseOpIdx > 0);
771 const MachineOperand &UFMO = getOperand(UseOpIdx-1);
772 if (!UFMO.isImm())
773 return false; // Must be physreg uses.
774 unsigned DefNo;
775 if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) {
776 if (!DefOpIdx)
777 return true;
778
779 unsigned DefIdx = 1;
780 // Remember to adjust the index. First operand is asm string, then there
781 // is a flag for each.
782 while (DefNo) {
783 const MachineOperand &FMO = getOperand(DefIdx);
784 assert(FMO.isImm());
785 // Skip over this def.
786 DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
787 --DefNo;
788 }
789 *DefOpIdx = DefIdx+1;
790 return true;
791 }
792 return false;
793 }
794
Evan Cheng48555e82009-03-19 20:30:06 +0000795 const TargetInstrDesc &TID = getDesc();
796 if (UseOpIdx >= TID.getNumOperands())
797 return false;
798 const MachineOperand &MO = getOperand(UseOpIdx);
799 if (!MO.isReg() || !MO.isUse())
800 return false;
801 int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO);
802 if (DefIdx == -1)
803 return false;
804 if (DefOpIdx)
805 *DefOpIdx = (unsigned)DefIdx;
806 return true;
807}
808
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
810///
811void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
812 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
813 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000814 if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000815 continue;
816 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
817 MachineOperand &MOp = getOperand(j);
818 if (!MOp.isIdenticalTo(MO))
819 continue;
820 if (MO.isKill())
821 MOp.setIsKill();
822 else
823 MOp.setIsDead();
824 break;
825 }
826 }
827}
828
829/// copyPredicates - Copies predicate operand(s) from MI.
830void MachineInstr::copyPredicates(const MachineInstr *MI) {
Chris Lattner5b930372008-01-07 07:27:27 +0000831 const TargetInstrDesc &TID = MI->getDesc();
Evan Chengbe856622008-03-13 00:44:09 +0000832 if (!TID.isPredicable())
833 return;
834 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
835 if (TID.OpInfo[i].isPredicate()) {
836 // Predicated operands must be last operands.
837 addOperand(MI->getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838 }
839 }
840}
841
Evan Chenge52c1912008-07-03 09:09:37 +0000842/// isSafeToMove - Return true if it is safe to move this instruction. If
843/// SawStore is set to true, it means that there is a store (or call) between
844/// the instruction's location and its intended destination.
Dan Gohman9ca19a32008-11-18 19:04:29 +0000845bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
846 bool &SawStore) const {
Evan Chengbe856622008-03-13 00:44:09 +0000847 // Ignore stuff that we obviously can't move.
848 if (TID->mayStore() || TID->isCall()) {
849 SawStore = true;
850 return false;
851 }
Dan Gohman64709cd2008-12-23 17:28:50 +0000852 if (TID->isTerminator() || TID->hasUnmodeledSideEffects())
Evan Chengbe856622008-03-13 00:44:09 +0000853 return false;
854
855 // See if this instruction does a load. If so, we have to guarantee that the
856 // loaded value doesn't change between the load and the its intended
857 // destination. The check for isInvariantLoad gives the targe the chance to
858 // classify the load as always returning a constant, e.g. a constant pool
859 // load.
Dan Gohman9ffbed82008-09-24 00:06:15 +0000860 if (TID->mayLoad() && !TII->isInvariantLoad(this))
Evan Chengbe856622008-03-13 00:44:09 +0000861 // Otherwise, this is a real load. If there is a store between the load and
Dan Gohman9ffbed82008-09-24 00:06:15 +0000862 // end of block, or if the laod is volatile, we can't move it.
Dan Gohman0ce00b82008-10-02 15:04:30 +0000863 return !SawStore && !hasVolatileMemoryRef();
Dan Gohman9ffbed82008-09-24 00:06:15 +0000864
Evan Chengbe856622008-03-13 00:44:09 +0000865 return true;
866}
867
Evan Cheng75e2cee2008-08-27 20:33:50 +0000868/// isSafeToReMat - Return true if it's safe to rematerialize the specified
869/// instruction which defined the specified register instead of copying it.
Dan Gohman9ca19a32008-11-18 19:04:29 +0000870bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII,
871 unsigned DstReg) const {
Evan Cheng75e2cee2008-08-27 20:33:50 +0000872 bool SawStore = false;
Evan Chenga07a9a22008-08-30 09:07:18 +0000873 if (!getDesc().isRematerializable() ||
874 !TII->isTriviallyReMaterializable(this) ||
875 !isSafeToMove(TII, SawStore))
Evan Cheng75e2cee2008-08-27 20:33:50 +0000876 return false;
877 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohman90feee22008-11-18 19:49:32 +0000878 const MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000879 if (!MO.isReg())
Evan Cheng75e2cee2008-08-27 20:33:50 +0000880 continue;
881 // FIXME: For now, do not remat any instruction with register operands.
882 // Later on, we can loosen the restriction is the register operands have
883 // not been modified between the def and use. Note, this is different from
Evan Chenga02c6692008-08-27 20:58:54 +0000884 // MachineSink because the code is no longer in two-address form (at least
Evan Cheng75e2cee2008-08-27 20:33:50 +0000885 // partially).
886 if (MO.isUse())
887 return false;
888 else if (!MO.isDead() && MO.getReg() != DstReg)
889 return false;
890 }
891 return true;
892}
893
Dan Gohman9ffbed82008-09-24 00:06:15 +0000894/// hasVolatileMemoryRef - Return true if this instruction may have a
895/// volatile memory reference, or if the information describing the
896/// memory reference is not available. Return false if it is known to
897/// have no volatile memory references.
898bool MachineInstr::hasVolatileMemoryRef() const {
899 // An instruction known never to access memory won't have a volatile access.
900 if (!TID->mayStore() &&
901 !TID->mayLoad() &&
902 !TID->isCall() &&
903 !TID->hasUnmodeledSideEffects())
904 return false;
905
906 // Otherwise, if the instruction has no memory reference information,
907 // conservatively assume it wasn't preserved.
908 if (memoperands_empty())
909 return true;
910
911 // Check the memory reference information for volatile references.
912 for (std::list<MachineMemOperand>::const_iterator I = memoperands_begin(),
913 E = memoperands_end(); I != E; ++I)
914 if (I->isVolatile())
915 return true;
916
917 return false;
918}
919
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000920void MachineInstr::dump() const {
921 cerr << " " << *this;
922}
923
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000924void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
Mon P Wang2f2cd302008-10-10 01:43:55 +0000925 raw_os_ostream RawOS(OS);
926 print(RawOS, TM);
927}
928
929void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const {
Chris Lattner9607bb82007-12-30 21:31:53 +0000930 // Specialize printing if op#0 is definition
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000931 unsigned StartOp = 0;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000932 if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000933 getOperand(0).print(OS, TM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000934 OS << " = ";
935 ++StartOp; // Don't print this operand again!
936 }
937
Chris Lattner5b930372008-01-07 07:27:27 +0000938 OS << getDesc().getName();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000939
940 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000941 if (i != StartOp)
942 OS << ",";
943 OS << " ";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000944 getOperand(i).print(OS, TM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000945 }
946
Dan Gohman221a4372008-07-07 23:14:23 +0000947 if (!memoperands_empty()) {
Dan Gohmanf738b652008-02-07 16:18:00 +0000948 OS << ", Mem:";
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000949 for (std::list<MachineMemOperand>::const_iterator i = memoperands_begin(),
Dan Gohman221a4372008-07-07 23:14:23 +0000950 e = memoperands_end(); i != e; ++i) {
951 const MachineMemOperand &MRO = *i;
Dan Gohman12a9c082008-02-06 22:27:42 +0000952 const Value *V = MRO.getValue();
953
Dan Gohman12a9c082008-02-06 22:27:42 +0000954 assert((MRO.isLoad() || MRO.isStore()) &&
955 "SV has to be a load, store or both.");
956
957 if (MRO.isVolatile())
958 OS << "Volatile ";
Dan Gohmanf738b652008-02-07 16:18:00 +0000959
Dan Gohman12a9c082008-02-06 22:27:42 +0000960 if (MRO.isLoad())
Dan Gohmanf738b652008-02-07 16:18:00 +0000961 OS << "LD";
Dan Gohman12a9c082008-02-06 22:27:42 +0000962 if (MRO.isStore())
Dan Gohmanf738b652008-02-07 16:18:00 +0000963 OS << "ST";
Dan Gohman12a9c082008-02-06 22:27:42 +0000964
Evan Cheng38dc79b2008-02-08 22:05:07 +0000965 OS << "(" << MRO.getSize() << "," << MRO.getAlignment() << ") [";
Dan Gohman12a9c082008-02-06 22:27:42 +0000966
Dan Gohmanf738b652008-02-07 16:18:00 +0000967 if (!V)
968 OS << "<unknown>";
969 else if (!V->getName().empty())
970 OS << V->getName();
Chris Lattner24ae2a92008-08-24 20:37:32 +0000971 else if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
Mon P Wang2f2cd302008-10-10 01:43:55 +0000972 PSV->print(OS);
Chris Lattner24ae2a92008-08-24 20:37:32 +0000973 } else
Dan Gohmanf738b652008-02-07 16:18:00 +0000974 OS << V;
975
976 OS << " + " << MRO.getOffset() << "]";
Dan Gohman12a9c082008-02-06 22:27:42 +0000977 }
978 }
979
Bill Wendlingb7596d22009-02-19 21:44:55 +0000980 if (!debugLoc.isUnknown()) {
981 const MachineFunction *MF = getParent()->getParent();
982 DebugLocTuple DLT = MF->getDebugLocTuple(debugLoc);
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +0000983 DICompileUnit CU(DLT.CompileUnit);
984 std::string Dir, Fn;
Bill Wendlingb7596d22009-02-19 21:44:55 +0000985 OS << " [dbg: "
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +0000986 << CU.getDirectory(Dir) << '/' << CU.getFilename(Fn) << ","
Bill Wendlingb7596d22009-02-19 21:44:55 +0000987 << DLT.Line << ","
988 << DLT.Col << "]";
989 }
990
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 OS << "\n";
992}
993
Owen Anderson58060792008-01-24 01:10:07 +0000994bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman1e57df32008-02-10 18:45:23 +0000995 const TargetRegisterInfo *RegInfo,
Owen Anderson58060792008-01-24 01:10:07 +0000996 bool AddIfNotFound) {
Evan Cheng794d0f72008-04-16 09:41:59 +0000997 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Dan Gohman9d90c632008-07-03 01:18:51 +0000998 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman244b86a2008-09-03 15:56:16 +0000999 bool Found = false;
Evan Cheng794d0f72008-04-16 09:41:59 +00001000 SmallVector<unsigned,4> DeadOps;
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001001 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1002 MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001003 if (!MO.isReg() || !MO.isUse())
Evan Cheng794d0f72008-04-16 09:41:59 +00001004 continue;
1005 unsigned Reg = MO.getReg();
1006 if (!Reg)
1007 continue;
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001008
Evan Cheng794d0f72008-04-16 09:41:59 +00001009 if (Reg == IncomingReg) {
Dan Gohman244b86a2008-09-03 15:56:16 +00001010 if (!Found) {
1011 if (MO.isKill())
1012 // The register is already marked kill.
1013 return true;
1014 MO.setIsKill();
1015 Found = true;
1016 }
1017 } else if (hasAliases && MO.isKill() &&
1018 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng794d0f72008-04-16 09:41:59 +00001019 // A super-register kill already exists.
1020 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman9d90c632008-07-03 01:18:51 +00001021 return true;
1022 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng794d0f72008-04-16 09:41:59 +00001023 DeadOps.push_back(i);
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001024 }
1025 }
1026
Evan Cheng794d0f72008-04-16 09:41:59 +00001027 // Trim unneeded kill operands.
1028 while (!DeadOps.empty()) {
1029 unsigned OpIdx = DeadOps.back();
1030 if (getOperand(OpIdx).isImplicit())
1031 RemoveOperand(OpIdx);
1032 else
1033 getOperand(OpIdx).setIsKill(false);
1034 DeadOps.pop_back();
1035 }
1036
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001037 // If not found, this means an alias of one of the operands is killed. Add a
Owen Anderson58060792008-01-24 01:10:07 +00001038 // new implicit operand if required.
Dan Gohman244b86a2008-09-03 15:56:16 +00001039 if (!Found && AddIfNotFound) {
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001040 addOperand(MachineOperand::CreateReg(IncomingReg,
1041 false /*IsDef*/,
1042 true /*IsImp*/,
1043 true /*IsKill*/));
Owen Anderson58060792008-01-24 01:10:07 +00001044 return true;
1045 }
Dan Gohman244b86a2008-09-03 15:56:16 +00001046 return Found;
Owen Anderson58060792008-01-24 01:10:07 +00001047}
1048
1049bool MachineInstr::addRegisterDead(unsigned IncomingReg,
Dan Gohman1e57df32008-02-10 18:45:23 +00001050 const TargetRegisterInfo *RegInfo,
Owen Anderson58060792008-01-24 01:10:07 +00001051 bool AddIfNotFound) {
Evan Cheng794d0f72008-04-16 09:41:59 +00001052 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Evan Chengdd562a02008-06-27 22:11:49 +00001053 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman244b86a2008-09-03 15:56:16 +00001054 bool Found = false;
Evan Cheng794d0f72008-04-16 09:41:59 +00001055 SmallVector<unsigned,4> DeadOps;
Owen Anderson58060792008-01-24 01:10:07 +00001056 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1057 MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001058 if (!MO.isReg() || !MO.isDef())
Evan Cheng794d0f72008-04-16 09:41:59 +00001059 continue;
1060 unsigned Reg = MO.getReg();
Dan Gohman244b86a2008-09-03 15:56:16 +00001061 if (!Reg)
1062 continue;
1063
Evan Cheng794d0f72008-04-16 09:41:59 +00001064 if (Reg == IncomingReg) {
Dan Gohman244b86a2008-09-03 15:56:16 +00001065 if (!Found) {
1066 if (MO.isDead())
1067 // The register is already marked dead.
1068 return true;
1069 MO.setIsDead();
1070 Found = true;
1071 }
1072 } else if (hasAliases && MO.isDead() &&
1073 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng794d0f72008-04-16 09:41:59 +00001074 // There exists a super-register that's marked dead.
1075 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman9d90c632008-07-03 01:18:51 +00001076 return true;
Owen Andersonc11fa052008-08-14 18:34:18 +00001077 if (RegInfo->getSubRegisters(IncomingReg) &&
1078 RegInfo->getSuperRegisters(Reg) &&
1079 RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng794d0f72008-04-16 09:41:59 +00001080 DeadOps.push_back(i);
Owen Anderson58060792008-01-24 01:10:07 +00001081 }
1082 }
1083
Evan Cheng794d0f72008-04-16 09:41:59 +00001084 // Trim unneeded dead operands.
1085 while (!DeadOps.empty()) {
1086 unsigned OpIdx = DeadOps.back();
1087 if (getOperand(OpIdx).isImplicit())
1088 RemoveOperand(OpIdx);
1089 else
1090 getOperand(OpIdx).setIsDead(false);
1091 DeadOps.pop_back();
1092 }
1093
Dan Gohman244b86a2008-09-03 15:56:16 +00001094 // If not found, this means an alias of one of the operands is dead. Add a
1095 // new implicit operand if required.
1096 if (!Found && AddIfNotFound) {
1097 addOperand(MachineOperand::CreateReg(IncomingReg,
1098 true /*IsDef*/,
1099 true /*IsImp*/,
1100 false /*IsKill*/,
1101 true /*IsDead*/));
Owen Anderson58060792008-01-24 01:10:07 +00001102 return true;
1103 }
Dan Gohman244b86a2008-09-03 15:56:16 +00001104 return Found;
Owen Anderson58060792008-01-24 01:10:07 +00001105}