blob: b8c8563eab45d2f5330cbeb01c08f4618c46fafb [file] [log] [blame]
Chris Lattnere138b3d2008-01-01 20:36:19 +00001//===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaeke21326fc2004-02-13 04:39:32 +00009//
10// Methods common to all machine instructions.
11//
Chris Lattner035dfbe2002-08-09 20:08:06 +000012//===----------------------------------------------------------------------===//
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000013
Chris Lattner822b4fb2001-09-07 17:18:30 +000014#include "llvm/CodeGen/MachineInstr.h"
Evan Chengfb112882009-03-23 08:01:15 +000015#include "llvm/Constants.h"
16#include "llvm/InlineAsm.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000017#include "llvm/Value.h"
Chris Lattner8517e1f2004-02-19 16:17:08 +000018#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner62ed6b92008-01-01 01:12:31 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000020#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner10491642002-10-30 00:48:05 +000021#include "llvm/Target/TargetMachine.h"
Evan Chengbb81d972008-01-31 09:59:15 +000022#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerf14cf852008-01-07 07:42:25 +000023#include "llvm/Target/TargetInstrDesc.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000024#include "llvm/Target/TargetRegisterInfo.h"
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +000025#include "llvm/Analysis/DebugInfo.h"
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000026#include "llvm/Support/LeakDetector.h"
Dan Gohmance42e402008-07-07 20:32:02 +000027#include "llvm/Support/MathExtras.h"
Bill Wendlinga09362e2006-11-28 22:48:48 +000028#include "llvm/Support/Streams.h"
Chris Lattneredfb72c2008-08-24 20:37:32 +000029#include "llvm/Support/raw_ostream.h"
Dan Gohmanb8d2f552008-08-20 15:58:01 +000030#include "llvm/ADT/FoldingSet.h"
Jeff Cohenc21c5ee2006-12-15 22:57:14 +000031#include <ostream>
Chris Lattner0742b592004-02-23 18:38:20 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattnerf7382302007-12-30 21:56:09 +000034//===----------------------------------------------------------------------===//
35// MachineOperand Implementation
36//===----------------------------------------------------------------------===//
37
Chris Lattner62ed6b92008-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 Gohmand735b802008-10-03 15:45:36 +000042 assert(isReg() && "Can only add reg operand to use lists");
Chris Lattner62ed6b92008-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 Lattner80fe5312008-01-01 21:08:22 +000053 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
Chris Lattner62ed6b92008-01-01 01:12:31 +000054
Chris Lattner80fe5312008-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 Lattner62ed6b92008-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 Lattner80fe5312008-01-01 21:08:22 +000068 Contents.Reg.Prev = Head;
69 *Head = this;
Chris Lattner62ed6b92008-01-01 01:12:31 +000070}
71
Dan Gohman3bc1a372009-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 Lattner62ed6b92008-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 Gohmand735b802008-10-03 15:45:36 +0000112 if (isReg() && getParent() && getParent()->getParent() &&
Chris Lattner62ed6b92008-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 Johannesene0091802008-09-14 01:44:36 +0000124 bool isKill, bool isDead) {
Chris Lattner62ed6b92008-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 Gohmand735b802008-10-03 15:45:36 +0000127 if (isReg()) {
Dale Johannesene0091802008-09-14 01:44:36 +0000128 assert(!isEarlyClobber());
Chris Lattner62ed6b92008-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 Johannesene0091802008-09-14 01:44:36 +0000147 IsEarlyClobber = false;
Chris Lattner62ed6b92008-01-01 01:12:31 +0000148 SubReg = 0;
149}
150
Chris Lattnerf7382302007-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 Begemane8b7ccf2008-02-14 07:39:30 +0000163 case MachineOperand::MO_FPImmediate:
164 return getFPImm() == Other.getFPImm();
Chris Lattnerf7382302007-12-30 21:56:09 +0000165 case MachineOperand::MO_MachineBasicBlock:
166 return getMBB() == Other.getMBB();
167 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000168 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000169 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000170 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattnerf7382302007-12-30 21:56:09 +0000171 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000172 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-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 Wang5ca6bd12008-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 Lattnerf7382302007-12-30 21:56:09 +0000189 switch (getType()) {
190 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000191 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) {
Chris Lattnerf7382302007-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 Lattner62ed6b92008-01-01 01:12:31 +0000195 // target info for the instruction.
Chris Lattnerf7382302007-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 Wendlinge6d088a2008-02-26 21:47:57 +0000203 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
Chris Lattnerf7382302007-12-30 21:56:09 +0000204 else
205 OS << "%mreg" << getReg();
206 }
Dan Gohman2ccc8392008-12-18 21:51:27 +0000207
208 if (getSubReg() != 0) {
209 OS << ":" << getSubReg();
210 }
211
Dale Johannesen86b49f82008-09-24 01:07:17 +0000212 if (isDef() || isKill() || isDead() || isImplicit() || isEarlyClobber()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000213 OS << "<";
214 bool NeedComma = false;
215 if (isImplicit()) {
Dale Johannesen91aac102008-09-17 21:13:11 +0000216 if (NeedComma) OS << ",";
Chris Lattnerf7382302007-12-30 21:56:09 +0000217 OS << (isDef() ? "imp-def" : "imp-use");
218 NeedComma = true;
219 } else if (isDef()) {
Dale Johannesen91aac102008-09-17 21:13:11 +0000220 if (NeedComma) OS << ",";
Dale Johannesen913d3df2008-09-12 17:49:03 +0000221 if (isEarlyClobber())
222 OS << "earlyclobber,";
Chris Lattnerf7382302007-12-30 21:56:09 +0000223 OS << "def";
224 NeedComma = true;
225 }
226 if (isKill() || isDead()) {
Bill Wendling181eb732008-02-24 00:56:13 +0000227 if (NeedComma) OS << ",";
228 if (isKill()) OS << "kill";
229 if (isDead()) OS << "dead";
Chris Lattnerf7382302007-12-30 21:56:09 +0000230 }
231 OS << ">";
232 }
233 break;
234 case MachineOperand::MO_Immediate:
235 OS << getImm();
236 break;
Nate Begemane8b7ccf2008-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 Lattnerf7382302007-12-30 21:56:09 +0000244 case MachineOperand::MO_MachineBasicBlock:
245 OS << "mbb<"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000246 << ((Value*)getMBB()->getBasicBlock())->getName()
247 << "," << (void*)getMBB() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000248 break;
249 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000250 OS << "<fi#" << getIndex() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000251 break;
252 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000253 OS << "<cp#" << getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000254 if (getOffset()) OS << "+" << getOffset();
255 OS << ">";
256 break;
257 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000258 OS << "<jt#" << getIndex() << ">";
Chris Lattnerf7382302007-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 Gohmance42e402008-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 Gohmanf1bf29e2008-07-08 23:47:04 +0000283 assert(isPowerOf2_32(a) && "Alignment is not a power of 2!");
Dan Gohmanc5e1f982008-07-16 15:56:42 +0000284 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmance42e402008-07-07 20:32:02 +0000285}
286
Dan Gohmanb8d2f552008-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 Gohmance42e402008-07-07 20:32:02 +0000296//===----------------------------------------------------------------------===//
Chris Lattnerf7382302007-12-30 21:56:09 +0000297// MachineInstr Implementation
298//===----------------------------------------------------------------------===//
299
Evan Chengc0f64ff2006-11-27 23:37:22 +0000300/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
Evan Cheng67f660c2006-11-30 07:08:44 +0000301/// TID NULL and no operands.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000302MachineInstr::MachineInstr()
Dale Johannesen06efc022009-01-27 23:20:29 +0000303 : TID(0), NumImplicitOps(0), Parent(0), debugLoc(DebugLoc::getUnknownLoc()) {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000304 // Make sure that we get added to a machine basicblock
305 LeakDetector::addGarbageObject(this);
Chris Lattner72791222002-10-28 20:59:49 +0000306}
307
Evan Cheng67f660c2006-11-30 07:08:44 +0000308void MachineInstr::addImplicitDefUseOperands() {
309 if (TID->ImplicitDefs)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000310 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner8019f412007-12-30 00:41:17 +0000311 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng67f660c2006-11-30 07:08:44 +0000312 if (TID->ImplicitUses)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000313 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner8019f412007-12-30 00:41:17 +0000314 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Evan Chengd7de4962006-11-13 23:34:06 +0000315}
316
317/// MachineInstr ctor - This constructor create a MachineInstr and add the
Evan Chengc0f64ff2006-11-27 23:37:22 +0000318/// implicit operands. It reserves space for number of operands specified by
Chris Lattner749c6f62008-01-07 07:27:27 +0000319/// TargetInstrDesc or the numOperands if it is not zero. (for
Evan Chengc0f64ff2006-11-27 23:37:22 +0000320/// instructions with variable number of operands).
Chris Lattner749c6f62008-01-07 07:27:27 +0000321MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
Dale Johannesen06efc022009-01-27 23:20:29 +0000322 : TID(&tid), NumImplicitOps(0), Parent(0),
323 debugLoc(DebugLoc::getUnknownLoc()) {
Chris Lattner349c4952008-01-07 03:13:06 +0000324 if (!NoImp && TID->getImplicitDefs())
325 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000326 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000327 if (!NoImp && TID->getImplicitUses())
328 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000329 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000330 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Chengfa945722007-10-13 02:23:01 +0000331 if (!NoImp)
332 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000333 // Make sure that we get added to a machine basicblock
334 LeakDetector::addGarbageObject(this);
Evan Chengd7de4962006-11-13 23:34:06 +0000335}
336
Dale Johannesen06efc022009-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.
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000357///
Dale Johannesen06efc022009-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 Lattner749c6f62008-01-07 07:27:27 +0000378 const TargetInstrDesc &tid)
Dale Johannesen06efc022009-01-27 23:20:29 +0000379 : TID(&tid), NumImplicitOps(0), Parent(0), debugLoc(dl) {
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000380 assert(MBB && "Cannot use inserting ctor with null basic block!");
Evan Cheng67f660c2006-11-30 07:08:44 +0000381 if (TID->ImplicitDefs)
Chris Lattner349c4952008-01-07 03:13:06 +0000382 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000383 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +0000384 if (TID->ImplicitUses)
Chris Lattner349c4952008-01-07 03:13:06 +0000385 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000386 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000387 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Cheng67f660c2006-11-30 07:08:44 +0000388 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000389 // Make sure that we get added to a machine basicblock
390 LeakDetector::addGarbageObject(this);
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000391 MBB->push_back(this); // Add instruction to end of basic block!
392}
393
Misha Brukmance22e762004-07-09 14:45:17 +0000394/// MachineInstr ctor - Copies MachineInstr arg exactly
395///
Evan Cheng1ed99222008-07-19 00:37:25 +0000396MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Dale Johannesen06efc022009-01-27 23:20:29 +0000397 : TID(&MI.getDesc()), NumImplicitOps(0), Parent(0),
398 debugLoc(MI.getDebugLoc()) {
Chris Lattner943b5e12006-05-04 19:14:44 +0000399 Operands.reserve(MI.getNumOperands());
Tanya Lattnerb5159ed2004-05-23 20:58:02 +0000400
Misha Brukmance22e762004-07-09 14:45:17 +0000401 // Add operands
Evan Cheng1ed99222008-07-19 00:37:25 +0000402 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
403 addOperand(MI.getOperand(i));
404 NumImplicitOps = MI.NumImplicitOps;
Tanya Lattner0c63e032004-05-24 03:14:18 +0000405
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000406 // Add memory operands.
Dan Gohmanfed90b62008-07-28 21:51:04 +0000407 for (std::list<MachineMemOperand>::const_iterator i = MI.memoperands_begin(),
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000408 j = MI.memoperands_end(); i != j; ++i)
409 addMemOperand(MF, *i);
410
411 // Set parent to null.
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000412 Parent = 0;
Dan Gohman6116a732008-07-21 18:47:29 +0000413
414 LeakDetector::addGarbageObject(this);
Tanya Lattner466b5342004-05-23 19:35:12 +0000415}
416
Misha Brukmance22e762004-07-09 14:45:17 +0000417MachineInstr::~MachineInstr() {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000418 LeakDetector::removeGarbageObject(this);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000419 assert(MemOperands.empty() &&
420 "MachineInstr being deleted with live memoperands!");
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000421#ifndef NDEBUG
Chris Lattner62ed6b92008-01-01 01:12:31 +0000422 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000423 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Dan Gohmand735b802008-10-03 15:45:36 +0000424 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
Chris Lattner62ed6b92008-01-01 01:12:31 +0000425 "Reg operand def/use list corrupted");
426 }
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000427#endif
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000428}
429
Chris Lattner62ed6b92008-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 Gohman4e526b92008-07-08 23:59:09 +0000435 return &MBB->getParent()->getRegInfo();
Chris Lattner62ed6b92008-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 Gohmand735b802008-10-03 15:45:36 +0000444 if (Operands[i].isReg())
Chris Lattner62ed6b92008-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 Gohmand735b802008-10-03 15:45:36 +0000454 if (Operands[i].isReg())
Chris Lattner62ed6b92008-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 Gohmand735b802008-10-03 15:45:36 +0000465 bool isImpReg = Op.isReg() && Op.isImplicit();
Chris Lattner62ed6b92008-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 Gohmanbcf28c02008-12-09 22:45:08 +0000469 MachineRegisterInfo *RegInfo = getRegInfo();
470
Chris Lattner62ed6b92008-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 Gohmand735b802008-10-03 15:45:36 +0000483 if (Op.isReg())
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000484 Operands.back().AddRegOperandToRegInfo(RegInfo);
Chris Lattner62ed6b92008-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 Lattner62ed6b92008-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 Gohmand735b802008-10-03 15:45:36 +0000501 if (Operands[OpNo].isReg())
Chris Lattner62ed6b92008-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 Gohmand735b802008-10-03 15:45:36 +0000514 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-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 Gohmand735b802008-10-03 15:45:36 +0000522 if (Operands[OpNo].isReg())
Chris Lattner62ed6b92008-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 Gohmand735b802008-10-03 15:45:36 +0000527 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-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 Gohmand735b802008-10-03 15:45:36 +0000553 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
Chris Lattner62ed6b92008-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 Gohmand735b802008-10-03 15:45:36 +0000566 if (Operands[i].isReg())
Chris Lattner62ed6b92008-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 Gohmand735b802008-10-03 15:45:36 +0000575 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000576 Operands[i].AddRegOperandToRegInfo(RegInfo);
577 }
578 }
579}
580
Dan Gohman8e5f2c62008-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 Gohmanfed90b62008-07-28 21:51:04 +0000585 MemOperands.push_back(MO);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000586}
587
588/// clearMemOperands - Erase all of this MachineInstr's MachineMemOperands.
589void MachineInstr::clearMemOperands(MachineFunction &MF) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000590 MemOperands.clear();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000591}
592
Chris Lattner62ed6b92008-01-01 01:12:31 +0000593
Chris Lattner48d7c062006-04-17 21:35:41 +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 Gohman8e5f2c62008-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
Brian Gaeke21326fc2004-02-13 04:39:32 +0000611/// OperandComplete - Return true if it's illegal to add a new operand
612///
Chris Lattner2a90ba62004-02-12 16:09:53 +0000613bool MachineInstr::OperandsComplete() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000614 unsigned short NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000615 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000616 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000617 return false;
618}
619
Evan Cheng19e3f312007-05-15 01:26:09 +0000620/// getNumExplicitOperands - Returns the number of non-implicit operands.
621///
622unsigned MachineInstr::getNumExplicitOperands() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000623 unsigned NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000624 if (!TID->isVariadic())
Evan Cheng19e3f312007-05-15 01:26:09 +0000625 return NumOperands;
626
Dan Gohman9407cd42009-04-15 17:59:11 +0000627 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
628 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000629 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng19e3f312007-05-15 01:26:09 +0000630 NumOperands++;
631 }
632 return NumOperands;
633}
634
Chris Lattner8ace2cd2006-10-20 22:39:59 +0000635
Dan Gohman44066042008-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 Chengbb81d972008-01-31 09:59:15 +0000644/// isDebugLabel - Returns true if the MachineInstr represents a debug label.
645///
646bool MachineInstr::isDebugLabel() const {
Dan Gohman44066042008-07-01 00:05:16 +0000647 return getOpcode() == TargetInstrInfo::DBG_LABEL;
Evan Chengbb81d972008-01-31 09:59:15 +0000648}
649
Evan Chengfaa51072007-04-26 19:00:32 +0000650/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Evan Cheng32eb1f12007-03-26 22:37:45 +0000651/// the specific register or -1 if it is not found. It further tightening
Evan Cheng76d7e762007-02-23 01:04:26 +0000652/// the search criteria to a use that kills the register if isKill is true.
Evan Cheng6130f662008-03-05 00:59:57 +0000653int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
654 const TargetRegisterInfo *TRI) const {
Evan Cheng576d1232006-12-06 08:27:42 +0000655 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengf277ee42007-05-29 18:35:22 +0000656 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000657 if (!MO.isReg() || !MO.isUse())
Evan Cheng6130f662008-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)))
Evan Cheng76d7e762007-02-23 01:04:26 +0000667 if (!isKill || MO.isKill())
Evan Cheng32eb1f12007-03-26 22:37:45 +0000668 return i;
Evan Cheng576d1232006-12-06 08:27:42 +0000669 }
Evan Cheng32eb1f12007-03-26 22:37:45 +0000670 return -1;
Evan Cheng576d1232006-12-06 08:27:42 +0000671}
672
Evan Cheng6130f662008-03-05 00:59:57 +0000673/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman703bfe62008-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 Cheng6130f662008-03-05 00:59:57 +0000677int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead,
678 const TargetRegisterInfo *TRI) const {
Evan Chengb371f452007-02-19 21:49:54 +0000679 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng6130f662008-03-05 00:59:57 +0000680 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000681 if (!MO.isReg() || !MO.isDef())
Evan Cheng6130f662008-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;
Evan Chengb371f452007-02-19 21:49:54 +0000691 }
Evan Cheng6130f662008-03-05 00:59:57 +0000692 return -1;
Evan Chengb371f452007-02-19 21:49:54 +0000693}
Evan Cheng19e3f312007-05-15 01:26:09 +0000694
Evan Chengf277ee42007-05-29 18:35:22 +0000695/// 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 Lattner749c6f62008-01-07 07:27:27 +0000699 const TargetInstrDesc &TID = getDesc();
700 if (TID.isPredicable()) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000701 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner749c6f62008-01-07 07:27:27 +0000702 if (TID.OpInfo[i].isPredicate())
Evan Chengf277ee42007-05-29 18:35:22 +0000703 return i;
Evan Cheng19e3f312007-05-15 01:26:09 +0000704 }
705
Evan Chengf277ee42007-05-29 18:35:22 +0000706 return -1;
Evan Cheng19e3f312007-05-15 01:26:09 +0000707}
Evan Chengb371f452007-02-19 21:49:54 +0000708
Bob Wilsond9df5012009-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 Olesence9be2c2009-04-29 20:57:16 +0000713bool MachineInstr::
714isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const {
Evan Chengfb112882009-03-23 08:01:15 +0000715 if (getOpcode() == TargetInstrInfo::INLINEASM) {
Bob Wilsond9df5012009-04-09 17:16:43 +0000716 assert(DefOpIdx >= 2);
717 const MachineOperand &MO = getOperand(DefOpIdx);
Chris Lattnerc30aa7b2009-04-09 23:33:34 +0000718 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
Evan Chengfb112882009-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 Wilsond9df5012009-04-09 17:16:43 +0000727 if (i > DefOpIdx)
Evan Chengfb112882009-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 Wilsond9df5012009-04-09 17:16:43 +0000739 Idx == DefNo) {
740 if (UseOpIdx)
741 *UseOpIdx = (unsigned)i + 1;
Evan Chengfb112882009-03-23 08:01:15 +0000742 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000743 }
Evan Chengfb112882009-03-23 08:01:15 +0000744 }
745 }
746
Bob Wilsond9df5012009-04-09 17:16:43 +0000747 assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!");
Chris Lattner749c6f62008-01-07 07:27:27 +0000748 const TargetInstrDesc &TID = getDesc();
Evan Chengef0732d2008-07-10 07:35:43 +0000749 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
750 const MachineOperand &MO = getOperand(i);
Dan Gohman2ce7f202008-12-05 05:45:42 +0000751 if (MO.isReg() && MO.isUse() &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000752 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) {
753 if (UseOpIdx)
754 *UseOpIdx = (unsigned)i;
Evan Chengef0732d2008-07-10 07:35:43 +0000755 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000756 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000757 }
758 return false;
759}
760
Evan Chenga24752f2009-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 Olesence9be2c2009-04-29 20:57:16 +0000764bool MachineInstr::
765isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const {
Evan Chengfb112882009-03-23 08:01:15 +0000766 if (getOpcode() == TargetInstrInfo::INLINEASM) {
767 const MachineOperand &MO = getOperand(UseOpIdx);
Chris Lattner0c8382c2009-04-09 16:50:43 +0000768 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0)
Evan Chengfb112882009-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 Chenga24752f2009-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
Evan Cheng576d1232006-12-06 08:27:42 +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 Gohmand735b802008-10-03 15:45:36 +0000814 if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
Evan Cheng576d1232006-12-06 08:27:42 +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
Evan Cheng19e3f312007-05-15 01:26:09 +0000829/// copyPredicates - Copies predicate operand(s) from MI.
830void MachineInstr::copyPredicates(const MachineInstr *MI) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000831 const TargetInstrDesc &TID = MI->getDesc();
Evan Chengb27087f2008-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));
Evan Cheng19e3f312007-05-15 01:26:09 +0000838 }
839 }
840}
841
Evan Cheng9f1c8312008-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 Gohmanb3b930a2008-11-18 19:04:29 +0000845bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
846 bool &SawStore) const {
Evan Chengb27087f2008-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 Gohman237dee12008-12-23 17:28:50 +0000852 if (TID->isTerminator() || TID->hasUnmodeledSideEffects())
Evan Chengb27087f2008-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 Gohman3e4fb702008-09-24 00:06:15 +0000860 if (TID->mayLoad() && !TII->isInvariantLoad(this))
Evan Chengb27087f2008-03-13 00:44:09 +0000861 // Otherwise, this is a real load. If there is a store between the load and
Dan Gohman3e4fb702008-09-24 00:06:15 +0000862 // end of block, or if the laod is volatile, we can't move it.
Dan Gohmand790a5c2008-10-02 15:04:30 +0000863 return !SawStore && !hasVolatileMemoryRef();
Dan Gohman3e4fb702008-09-24 00:06:15 +0000864
Evan Chengb27087f2008-03-13 00:44:09 +0000865 return true;
866}
867
Evan Chengdf3b9932008-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 Gohmanb3b930a2008-11-18 19:04:29 +0000870bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII,
871 unsigned DstReg) const {
Evan Chengdf3b9932008-08-27 20:33:50 +0000872 bool SawStore = false;
Evan Cheng3689ff42008-08-30 09:07:18 +0000873 if (!getDesc().isRematerializable() ||
874 !TII->isTriviallyReMaterializable(this) ||
875 !isSafeToMove(TII, SawStore))
Evan Chengdf3b9932008-08-27 20:33:50 +0000876 return false;
877 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohmancbad42c2008-11-18 19:49:32 +0000878 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000879 if (!MO.isReg())
Evan Chengdf3b9932008-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 Cheng8763c1c2008-08-27 20:58:54 +0000884 // MachineSink because the code is no longer in two-address form (at least
Evan Chengdf3b9932008-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 Gohman3e4fb702008-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
Brian Gaeke21326fc2004-02-13 04:39:32 +0000920void MachineInstr::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000921 cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000922}
923
Tanya Lattnerb1407622004-06-25 00:13:11 +0000924void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
Mon P Wang5ca6bd12008-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 Lattnere3087892007-12-30 21:31:53 +0000930 // Specialize printing if op#0 is definition
Chris Lattner6a592272002-10-30 01:55:38 +0000931 unsigned StartOp = 0;
Dan Gohmand735b802008-10-03 15:45:36 +0000932 if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000933 getOperand(0).print(OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +0000934 OS << " = ";
935 ++StartOp; // Don't print this operand again!
936 }
Tanya Lattnerb1407622004-06-25 00:13:11 +0000937
Chris Lattner749c6f62008-01-07 07:27:27 +0000938 OS << getDesc().getName();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000939
Chris Lattner6a592272002-10-30 01:55:38 +0000940 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
941 if (i != StartOp)
942 OS << ",";
943 OS << " ";
Chris Lattnerf7382302007-12-30 21:56:09 +0000944 getOperand(i).print(OS, TM);
Chris Lattner10491642002-10-30 00:48:05 +0000945 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000946
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000947 if (!memoperands_empty()) {
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000948 OS << ", Mem:";
Dan Gohmanfed90b62008-07-28 21:51:04 +0000949 for (std::list<MachineMemOperand>::const_iterator i = memoperands_begin(),
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000950 e = memoperands_end(); i != e; ++i) {
951 const MachineMemOperand &MRO = *i;
Dan Gohman69de1932008-02-06 22:27:42 +0000952 const Value *V = MRO.getValue();
953
Dan Gohman69de1932008-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 Gohman2bfe6ff2008-02-07 16:18:00 +0000959
Dan Gohman69de1932008-02-06 22:27:42 +0000960 if (MRO.isLoad())
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000961 OS << "LD";
Dan Gohman69de1932008-02-06 22:27:42 +0000962 if (MRO.isStore())
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000963 OS << "ST";
Dan Gohman69de1932008-02-06 22:27:42 +0000964
Evan Chengbbd83222008-02-08 22:05:07 +0000965 OS << "(" << MRO.getSize() << "," << MRO.getAlignment() << ") [";
Dan Gohman69de1932008-02-06 22:27:42 +0000966
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000967 if (!V)
968 OS << "<unknown>";
969 else if (!V->getName().empty())
970 OS << V->getName();
Chris Lattneredfb72c2008-08-24 20:37:32 +0000971 else if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
Mon P Wang5ca6bd12008-10-10 01:43:55 +0000972 PSV->print(OS);
Chris Lattneredfb72c2008-08-24 20:37:32 +0000973 } else
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000974 OS << V;
975
976 OS << " + " << MRO.getOffset() << "]";
Dan Gohman69de1932008-02-06 22:27:42 +0000977 }
978 }
979
Bill Wendlingb5ef2732009-02-19 21:44:55 +0000980 if (!debugLoc.isUnknown()) {
981 const MachineFunction *MF = getParent()->getParent();
982 DebugLocTuple DLT = MF->getDebugLocTuple(debugLoc);
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +0000983 DICompileUnit CU(DLT.CompileUnit);
984 std::string Dir, Fn;
Bill Wendlingb5ef2732009-02-19 21:44:55 +0000985 OS << " [dbg: "
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +0000986 << CU.getDirectory(Dir) << '/' << CU.getFilename(Fn) << ","
Bill Wendlingb5ef2732009-02-19 21:44:55 +0000987 << DLT.Line << ","
988 << DLT.Col << "]";
989 }
990
Chris Lattner10491642002-10-30 00:48:05 +0000991 OS << "\n";
992}
993
Owen Andersonb487e722008-01-24 01:10:07 +0000994bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +0000995 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +0000996 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +0000997 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Dan Gohman2ebc11a2008-07-03 01:18:51 +0000998 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +0000999 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001000 SmallVector<unsigned,4> DeadOps;
Bill Wendling4a23d722008-03-03 22:14:33 +00001001 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1002 MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001003 if (!MO.isReg() || !MO.isUse())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001004 continue;
1005 unsigned Reg = MO.getReg();
1006 if (!Reg)
1007 continue;
Bill Wendling4a23d722008-03-03 22:14:33 +00001008
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001009 if (Reg == IncomingReg) {
Dan Gohman3f629402008-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 Cheng9b6d7b92008-04-16 09:41:59 +00001019 // A super-register kill already exists.
1020 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001021 return true;
1022 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001023 DeadOps.push_back(i);
Bill Wendling4a23d722008-03-03 22:14:33 +00001024 }
1025 }
1026
Evan Cheng9b6d7b92008-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 Wendling4a23d722008-03-03 22:14:33 +00001037 // If not found, this means an alias of one of the operands is killed. Add a
Owen Andersonb487e722008-01-24 01:10:07 +00001038 // new implicit operand if required.
Dan Gohman3f629402008-09-03 15:56:16 +00001039 if (!Found && AddIfNotFound) {
Bill Wendling4a23d722008-03-03 22:14:33 +00001040 addOperand(MachineOperand::CreateReg(IncomingReg,
1041 false /*IsDef*/,
1042 true /*IsImp*/,
1043 true /*IsKill*/));
Owen Andersonb487e722008-01-24 01:10:07 +00001044 return true;
1045 }
Dan Gohman3f629402008-09-03 15:56:16 +00001046 return Found;
Owen Andersonb487e722008-01-24 01:10:07 +00001047}
1048
1049bool MachineInstr::addRegisterDead(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001050 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001051 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001052 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Evan Cheng01b2e232008-06-27 22:11:49 +00001053 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +00001054 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001055 SmallVector<unsigned,4> DeadOps;
Owen Andersonb487e722008-01-24 01:10:07 +00001056 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1057 MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001058 if (!MO.isReg() || !MO.isDef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001059 continue;
1060 unsigned Reg = MO.getReg();
Dan Gohman3f629402008-09-03 15:56:16 +00001061 if (!Reg)
1062 continue;
1063
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001064 if (Reg == IncomingReg) {
Dan Gohman3f629402008-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 Cheng9b6d7b92008-04-16 09:41:59 +00001074 // There exists a super-register that's marked dead.
1075 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001076 return true;
Owen Anderson22ae9992008-08-14 18:34:18 +00001077 if (RegInfo->getSubRegisters(IncomingReg) &&
1078 RegInfo->getSuperRegisters(Reg) &&
1079 RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001080 DeadOps.push_back(i);
Owen Andersonb487e722008-01-24 01:10:07 +00001081 }
1082 }
1083
Evan Cheng9b6d7b92008-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 Gohman3f629402008-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 Andersonb487e722008-01-24 01:10:07 +00001102 return true;
1103 }
Dan Gohman3f629402008-09-03 15:56:16 +00001104 return Found;
Owen Andersonb487e722008-01-24 01:10:07 +00001105}