blob: 9cfff82233d3b7f4dea352055e7b6f5e1f6c15e6 [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"
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000025#include "llvm/Support/LeakDetector.h"
Dan Gohmance42e402008-07-07 20:32:02 +000026#include "llvm/Support/MathExtras.h"
Bill Wendlinga09362e2006-11-28 22:48:48 +000027#include "llvm/Support/Streams.h"
Chris Lattneredfb72c2008-08-24 20:37:32 +000028#include "llvm/Support/raw_ostream.h"
Dan Gohmanb8d2f552008-08-20 15:58:01 +000029#include "llvm/ADT/FoldingSet.h"
Jeff Cohenc21c5ee2006-12-15 22:57:14 +000030#include <ostream>
Chris Lattner0742b592004-02-23 18:38:20 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattnerf7382302007-12-30 21:56:09 +000033//===----------------------------------------------------------------------===//
34// MachineOperand Implementation
35//===----------------------------------------------------------------------===//
36
Chris Lattner62ed6b92008-01-01 01:12:31 +000037/// AddRegOperandToRegInfo - Add this register operand to the specified
38/// MachineRegisterInfo. If it is null, then the next/prev fields should be
39/// explicitly nulled out.
40void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) {
Dan Gohmand735b802008-10-03 15:45:36 +000041 assert(isReg() && "Can only add reg operand to use lists");
Chris Lattner62ed6b92008-01-01 01:12:31 +000042
43 // If the reginfo pointer is null, just explicitly null out or next/prev
44 // pointers, to ensure they are not garbage.
45 if (RegInfo == 0) {
46 Contents.Reg.Prev = 0;
47 Contents.Reg.Next = 0;
48 return;
49 }
50
51 // Otherwise, add this operand to the head of the registers use/def list.
Chris Lattner80fe5312008-01-01 21:08:22 +000052 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
Chris Lattner62ed6b92008-01-01 01:12:31 +000053
Chris Lattner80fe5312008-01-01 21:08:22 +000054 // For SSA values, we prefer to keep the definition at the start of the list.
55 // we do this by skipping over the definition if it is at the head of the
56 // list.
57 if (*Head && (*Head)->isDef())
58 Head = &(*Head)->Contents.Reg.Next;
59
60 Contents.Reg.Next = *Head;
Chris Lattner62ed6b92008-01-01 01:12:31 +000061 if (Contents.Reg.Next) {
62 assert(getReg() == Contents.Reg.Next->getReg() &&
63 "Different regs on the same list!");
64 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
65 }
66
Chris Lattner80fe5312008-01-01 21:08:22 +000067 Contents.Reg.Prev = Head;
68 *Head = this;
Chris Lattner62ed6b92008-01-01 01:12:31 +000069}
70
Dan Gohman3bc1a372009-04-15 01:17:37 +000071/// RemoveRegOperandFromRegInfo - Remove this register operand from the
72/// MachineRegisterInfo it is linked with.
73void MachineOperand::RemoveRegOperandFromRegInfo() {
74 assert(isOnRegUseList() && "Reg operand is not on a use list");
75 // Unlink this from the doubly linked list of operands.
76 MachineOperand *NextOp = Contents.Reg.Next;
77 *Contents.Reg.Prev = NextOp;
78 if (NextOp) {
79 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
80 NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
81 }
82 Contents.Reg.Prev = 0;
83 Contents.Reg.Next = 0;
84}
85
Chris Lattner62ed6b92008-01-01 01:12:31 +000086void MachineOperand::setReg(unsigned Reg) {
87 if (getReg() == Reg) return; // No change.
88
89 // Otherwise, we have to change the register. If this operand is embedded
90 // into a machine function, we need to update the old and new register's
91 // use/def lists.
92 if (MachineInstr *MI = getParent())
93 if (MachineBasicBlock *MBB = MI->getParent())
94 if (MachineFunction *MF = MBB->getParent()) {
95 RemoveRegOperandFromRegInfo();
96 Contents.Reg.RegNo = Reg;
97 AddRegOperandToRegInfo(&MF->getRegInfo());
98 return;
99 }
100
101 // Otherwise, just change the register, no problem. :)
102 Contents.Reg.RegNo = Reg;
103}
104
105/// ChangeToImmediate - Replace this operand with a new immediate operand of
106/// the specified value. If an operand is known to be an immediate already,
107/// the setImm method should be used.
108void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
109 // If this operand is currently a register operand, and if this is in a
110 // function, deregister the operand from the register's use/def list.
Dan Gohmand735b802008-10-03 15:45:36 +0000111 if (isReg() && getParent() && getParent()->getParent() &&
Chris Lattner62ed6b92008-01-01 01:12:31 +0000112 getParent()->getParent()->getParent())
113 RemoveRegOperandFromRegInfo();
114
115 OpKind = MO_Immediate;
116 Contents.ImmVal = ImmVal;
117}
118
119/// ChangeToRegister - Replace this operand with a new register operand of
120/// the specified value. If an operand is known to be an register already,
121/// the setReg method should be used.
122void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Dale Johannesene0091802008-09-14 01:44:36 +0000123 bool isKill, bool isDead) {
Chris Lattner62ed6b92008-01-01 01:12:31 +0000124 // If this operand is already a register operand, use setReg to update the
125 // register's use/def lists.
Dan Gohmand735b802008-10-03 15:45:36 +0000126 if (isReg()) {
Dale Johannesene0091802008-09-14 01:44:36 +0000127 assert(!isEarlyClobber());
Chris Lattner62ed6b92008-01-01 01:12:31 +0000128 setReg(Reg);
129 } else {
130 // Otherwise, change this to a register and set the reg#.
131 OpKind = MO_Register;
132 Contents.Reg.RegNo = Reg;
133
134 // If this operand is embedded in a function, add the operand to the
135 // register's use/def list.
136 if (MachineInstr *MI = getParent())
137 if (MachineBasicBlock *MBB = MI->getParent())
138 if (MachineFunction *MF = MBB->getParent())
139 AddRegOperandToRegInfo(&MF->getRegInfo());
140 }
141
142 IsDef = isDef;
143 IsImp = isImp;
144 IsKill = isKill;
145 IsDead = isDead;
Dale Johannesene0091802008-09-14 01:44:36 +0000146 IsEarlyClobber = false;
Chris Lattner62ed6b92008-01-01 01:12:31 +0000147 SubReg = 0;
148}
149
Chris Lattnerf7382302007-12-30 21:56:09 +0000150/// isIdenticalTo - Return true if this operand is identical to the specified
151/// operand.
152bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
153 if (getType() != Other.getType()) return false;
154
155 switch (getType()) {
156 default: assert(0 && "Unrecognized operand type");
157 case MachineOperand::MO_Register:
158 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
159 getSubReg() == Other.getSubReg();
160 case MachineOperand::MO_Immediate:
161 return getImm() == Other.getImm();
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000162 case MachineOperand::MO_FPImmediate:
163 return getFPImm() == Other.getFPImm();
Chris Lattnerf7382302007-12-30 21:56:09 +0000164 case MachineOperand::MO_MachineBasicBlock:
165 return getMBB() == Other.getMBB();
166 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000167 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000168 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000169 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattnerf7382302007-12-30 21:56:09 +0000170 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000171 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000172 case MachineOperand::MO_GlobalAddress:
173 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
174 case MachineOperand::MO_ExternalSymbol:
175 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
176 getOffset() == Other.getOffset();
177 }
178}
179
180/// print - Print the specified machine operand.
181///
182void MachineOperand::print(std::ostream &OS, const TargetMachine *TM) const {
Mon P Wang5ca6bd12008-10-10 01:43:55 +0000183 raw_os_ostream RawOS(OS);
184 print(RawOS, TM);
185}
186
187void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
Chris Lattnerf7382302007-12-30 21:56:09 +0000188 switch (getType()) {
189 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000190 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000191 OS << "%reg" << getReg();
192 } else {
193 // If the instruction is embedded into a basic block, we can find the
Chris Lattner62ed6b92008-01-01 01:12:31 +0000194 // target info for the instruction.
Chris Lattnerf7382302007-12-30 21:56:09 +0000195 if (TM == 0)
196 if (const MachineInstr *MI = getParent())
197 if (const MachineBasicBlock *MBB = MI->getParent())
198 if (const MachineFunction *MF = MBB->getParent())
199 TM = &MF->getTarget();
200
201 if (TM)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000202 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
Chris Lattnerf7382302007-12-30 21:56:09 +0000203 else
204 OS << "%mreg" << getReg();
205 }
Dan Gohman2ccc8392008-12-18 21:51:27 +0000206
207 if (getSubReg() != 0) {
208 OS << ":" << getSubReg();
209 }
210
Dale Johannesen86b49f82008-09-24 01:07:17 +0000211 if (isDef() || isKill() || isDead() || isImplicit() || isEarlyClobber()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000212 OS << "<";
213 bool NeedComma = false;
214 if (isImplicit()) {
Dale Johannesen91aac102008-09-17 21:13:11 +0000215 if (NeedComma) OS << ",";
Chris Lattnerf7382302007-12-30 21:56:09 +0000216 OS << (isDef() ? "imp-def" : "imp-use");
217 NeedComma = true;
218 } else if (isDef()) {
Dale Johannesen91aac102008-09-17 21:13:11 +0000219 if (NeedComma) OS << ",";
Dale Johannesen913d3df2008-09-12 17:49:03 +0000220 if (isEarlyClobber())
221 OS << "earlyclobber,";
Chris Lattnerf7382302007-12-30 21:56:09 +0000222 OS << "def";
223 NeedComma = true;
224 }
225 if (isKill() || isDead()) {
Bill Wendling181eb732008-02-24 00:56:13 +0000226 if (NeedComma) OS << ",";
227 if (isKill()) OS << "kill";
228 if (isDead()) OS << "dead";
Chris Lattnerf7382302007-12-30 21:56:09 +0000229 }
230 OS << ">";
231 }
232 break;
233 case MachineOperand::MO_Immediate:
234 OS << getImm();
235 break;
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000236 case MachineOperand::MO_FPImmediate:
237 if (getFPImm()->getType() == Type::FloatTy) {
238 OS << getFPImm()->getValueAPF().convertToFloat();
239 } else {
240 OS << getFPImm()->getValueAPF().convertToDouble();
241 }
242 break;
Chris Lattnerf7382302007-12-30 21:56:09 +0000243 case MachineOperand::MO_MachineBasicBlock:
244 OS << "mbb<"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000245 << ((Value*)getMBB()->getBasicBlock())->getName()
246 << "," << (void*)getMBB() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000247 break;
248 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000249 OS << "<fi#" << getIndex() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000250 break;
251 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000252 OS << "<cp#" << getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000253 if (getOffset()) OS << "+" << getOffset();
254 OS << ">";
255 break;
256 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000257 OS << "<jt#" << getIndex() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000258 break;
259 case MachineOperand::MO_GlobalAddress:
260 OS << "<ga:" << ((Value*)getGlobal())->getName();
261 if (getOffset()) OS << "+" << getOffset();
262 OS << ">";
263 break;
264 case MachineOperand::MO_ExternalSymbol:
265 OS << "<es:" << getSymbolName();
266 if (getOffset()) OS << "+" << getOffset();
267 OS << ">";
268 break;
269 default:
270 assert(0 && "Unrecognized operand type");
271 }
272}
273
274//===----------------------------------------------------------------------===//
Dan Gohmance42e402008-07-07 20:32:02 +0000275// MachineMemOperand Implementation
276//===----------------------------------------------------------------------===//
277
278MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f,
279 int64_t o, uint64_t s, unsigned int a)
280 : Offset(o), Size(s), V(v),
281 Flags((f & 7) | ((Log2_32(a) + 1) << 3)) {
Dan Gohmanf1bf29e2008-07-08 23:47:04 +0000282 assert(isPowerOf2_32(a) && "Alignment is not a power of 2!");
Dan Gohmanc5e1f982008-07-16 15:56:42 +0000283 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmance42e402008-07-07 20:32:02 +0000284}
285
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000286/// Profile - Gather unique data for the object.
287///
288void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
289 ID.AddInteger(Offset);
290 ID.AddInteger(Size);
291 ID.AddPointer(V);
292 ID.AddInteger(Flags);
293}
294
Dan Gohmance42e402008-07-07 20:32:02 +0000295//===----------------------------------------------------------------------===//
Chris Lattnerf7382302007-12-30 21:56:09 +0000296// MachineInstr Implementation
297//===----------------------------------------------------------------------===//
298
Evan Chengc0f64ff2006-11-27 23:37:22 +0000299/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
Evan Cheng67f660c2006-11-30 07:08:44 +0000300/// TID NULL and no operands.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000301MachineInstr::MachineInstr()
Dale Johannesen06efc022009-01-27 23:20:29 +0000302 : TID(0), NumImplicitOps(0), Parent(0), debugLoc(DebugLoc::getUnknownLoc()) {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000303 // Make sure that we get added to a machine basicblock
304 LeakDetector::addGarbageObject(this);
Chris Lattner72791222002-10-28 20:59:49 +0000305}
306
Evan Cheng67f660c2006-11-30 07:08:44 +0000307void MachineInstr::addImplicitDefUseOperands() {
308 if (TID->ImplicitDefs)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000309 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner8019f412007-12-30 00:41:17 +0000310 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng67f660c2006-11-30 07:08:44 +0000311 if (TID->ImplicitUses)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000312 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner8019f412007-12-30 00:41:17 +0000313 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Evan Chengd7de4962006-11-13 23:34:06 +0000314}
315
316/// MachineInstr ctor - This constructor create a MachineInstr and add the
Evan Chengc0f64ff2006-11-27 23:37:22 +0000317/// implicit operands. It reserves space for number of operands specified by
Chris Lattner749c6f62008-01-07 07:27:27 +0000318/// TargetInstrDesc or the numOperands if it is not zero. (for
Evan Chengc0f64ff2006-11-27 23:37:22 +0000319/// instructions with variable number of operands).
Chris Lattner749c6f62008-01-07 07:27:27 +0000320MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
Dale Johannesen06efc022009-01-27 23:20:29 +0000321 : TID(&tid), NumImplicitOps(0), Parent(0),
322 debugLoc(DebugLoc::getUnknownLoc()) {
Chris Lattner349c4952008-01-07 03:13:06 +0000323 if (!NoImp && TID->getImplicitDefs())
324 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000325 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000326 if (!NoImp && TID->getImplicitUses())
327 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000328 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000329 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Chengfa945722007-10-13 02:23:01 +0000330 if (!NoImp)
331 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000332 // Make sure that we get added to a machine basicblock
333 LeakDetector::addGarbageObject(this);
Evan Chengd7de4962006-11-13 23:34:06 +0000334}
335
Dale Johannesen06efc022009-01-27 23:20:29 +0000336/// MachineInstr ctor - As above, but with a DebugLoc.
337MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl,
338 bool NoImp)
339 : TID(&tid), NumImplicitOps(0), Parent(0), debugLoc(dl) {
340 if (!NoImp && TID->getImplicitDefs())
341 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
342 NumImplicitOps++;
343 if (!NoImp && TID->getImplicitUses())
344 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
345 NumImplicitOps++;
346 Operands.reserve(NumImplicitOps + TID->getNumOperands());
347 if (!NoImp)
348 addImplicitDefUseOperands();
349 // Make sure that we get added to a machine basicblock
350 LeakDetector::addGarbageObject(this);
351}
352
353/// MachineInstr ctor - Work exactly the same as the ctor two above, except
354/// that the MachineInstr is created and added to the end of the specified
355/// basic block.
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000356///
Dale Johannesen06efc022009-01-27 23:20:29 +0000357MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid)
358 : TID(&tid), NumImplicitOps(0), Parent(0),
359 debugLoc(DebugLoc::getUnknownLoc()) {
360 assert(MBB && "Cannot use inserting ctor with null basic block!");
361 if (TID->ImplicitDefs)
362 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
363 NumImplicitOps++;
364 if (TID->ImplicitUses)
365 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
366 NumImplicitOps++;
367 Operands.reserve(NumImplicitOps + TID->getNumOperands());
368 addImplicitDefUseOperands();
369 // Make sure that we get added to a machine basicblock
370 LeakDetector::addGarbageObject(this);
371 MBB->push_back(this); // Add instruction to end of basic block!
372}
373
374/// MachineInstr ctor - As above, but with a DebugLoc.
375///
376MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl,
Chris Lattner749c6f62008-01-07 07:27:27 +0000377 const TargetInstrDesc &tid)
Dale Johannesen06efc022009-01-27 23:20:29 +0000378 : TID(&tid), NumImplicitOps(0), Parent(0), debugLoc(dl) {
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000379 assert(MBB && "Cannot use inserting ctor with null basic block!");
Evan Cheng67f660c2006-11-30 07:08:44 +0000380 if (TID->ImplicitDefs)
Chris Lattner349c4952008-01-07 03:13:06 +0000381 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000382 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +0000383 if (TID->ImplicitUses)
Chris Lattner349c4952008-01-07 03:13:06 +0000384 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000385 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000386 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Cheng67f660c2006-11-30 07:08:44 +0000387 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000388 // Make sure that we get added to a machine basicblock
389 LeakDetector::addGarbageObject(this);
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000390 MBB->push_back(this); // Add instruction to end of basic block!
391}
392
Misha Brukmance22e762004-07-09 14:45:17 +0000393/// MachineInstr ctor - Copies MachineInstr arg exactly
394///
Evan Cheng1ed99222008-07-19 00:37:25 +0000395MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Dale Johannesen06efc022009-01-27 23:20:29 +0000396 : TID(&MI.getDesc()), NumImplicitOps(0), Parent(0),
397 debugLoc(MI.getDebugLoc()) {
Chris Lattner943b5e12006-05-04 19:14:44 +0000398 Operands.reserve(MI.getNumOperands());
Tanya Lattnerb5159ed2004-05-23 20:58:02 +0000399
Misha Brukmance22e762004-07-09 14:45:17 +0000400 // Add operands
Evan Cheng1ed99222008-07-19 00:37:25 +0000401 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
402 addOperand(MI.getOperand(i));
403 NumImplicitOps = MI.NumImplicitOps;
Tanya Lattner0c63e032004-05-24 03:14:18 +0000404
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000405 // Add memory operands.
Dan Gohmanfed90b62008-07-28 21:51:04 +0000406 for (std::list<MachineMemOperand>::const_iterator i = MI.memoperands_begin(),
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000407 j = MI.memoperands_end(); i != j; ++i)
408 addMemOperand(MF, *i);
409
410 // Set parent to null.
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000411 Parent = 0;
Dan Gohman6116a732008-07-21 18:47:29 +0000412
413 LeakDetector::addGarbageObject(this);
Tanya Lattner466b5342004-05-23 19:35:12 +0000414}
415
Misha Brukmance22e762004-07-09 14:45:17 +0000416MachineInstr::~MachineInstr() {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000417 LeakDetector::removeGarbageObject(this);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000418 assert(MemOperands.empty() &&
419 "MachineInstr being deleted with live memoperands!");
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000420#ifndef NDEBUG
Chris Lattner62ed6b92008-01-01 01:12:31 +0000421 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000422 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Dan Gohmand735b802008-10-03 15:45:36 +0000423 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
Chris Lattner62ed6b92008-01-01 01:12:31 +0000424 "Reg operand def/use list corrupted");
425 }
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000426#endif
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000427}
428
Chris Lattner62ed6b92008-01-01 01:12:31 +0000429/// getRegInfo - If this instruction is embedded into a MachineFunction,
430/// return the MachineRegisterInfo object for the current function, otherwise
431/// return null.
432MachineRegisterInfo *MachineInstr::getRegInfo() {
433 if (MachineBasicBlock *MBB = getParent())
Dan Gohman4e526b92008-07-08 23:59:09 +0000434 return &MBB->getParent()->getRegInfo();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000435 return 0;
436}
437
438/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
439/// this instruction from their respective use lists. This requires that the
440/// operands already be on their use lists.
441void MachineInstr::RemoveRegOperandsFromUseLists() {
442 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000443 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000444 Operands[i].RemoveRegOperandFromRegInfo();
445 }
446}
447
448/// AddRegOperandsToUseLists - Add all of the register operands in
449/// this instruction from their respective use lists. This requires that the
450/// operands not be on their use lists yet.
451void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
452 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000453 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000454 Operands[i].AddRegOperandToRegInfo(&RegInfo);
455 }
456}
457
458
459/// addOperand - Add the specified operand to the instruction. If it is an
460/// implicit operand, it is added to the end of the operand list. If it is
461/// an explicit operand it is added at the end of the explicit operand list
462/// (before the first implicit operand).
463void MachineInstr::addOperand(const MachineOperand &Op) {
Dan Gohmand735b802008-10-03 15:45:36 +0000464 bool isImpReg = Op.isReg() && Op.isImplicit();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000465 assert((isImpReg || !OperandsComplete()) &&
466 "Trying to add an operand to a machine instr that is already done!");
467
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000468 MachineRegisterInfo *RegInfo = getRegInfo();
469
Chris Lattner62ed6b92008-01-01 01:12:31 +0000470 // If we are adding the operand to the end of the list, our job is simpler.
471 // This is true most of the time, so this is a reasonable optimization.
472 if (isImpReg || NumImplicitOps == 0) {
473 // We can only do this optimization if we know that the operand list won't
474 // reallocate.
475 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
476 Operands.push_back(Op);
477
478 // Set the parent of the operand.
479 Operands.back().ParentMI = this;
480
481 // If the operand is a register, update the operand's use list.
Dan Gohmand735b802008-10-03 15:45:36 +0000482 if (Op.isReg())
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000483 Operands.back().AddRegOperandToRegInfo(RegInfo);
Chris Lattner62ed6b92008-01-01 01:12:31 +0000484 return;
485 }
486 }
487
488 // Otherwise, we have to insert a real operand before any implicit ones.
489 unsigned OpNo = Operands.size()-NumImplicitOps;
490
Chris Lattner62ed6b92008-01-01 01:12:31 +0000491 // If this instruction isn't embedded into a function, then we don't need to
492 // update any operand lists.
493 if (RegInfo == 0) {
494 // Simple insertion, no reginfo update needed for other register operands.
495 Operands.insert(Operands.begin()+OpNo, Op);
496 Operands[OpNo].ParentMI = this;
497
498 // Do explicitly set the reginfo for this operand though, to ensure the
499 // next/prev fields are properly nulled out.
Dan Gohmand735b802008-10-03 15:45:36 +0000500 if (Operands[OpNo].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000501 Operands[OpNo].AddRegOperandToRegInfo(0);
502
503 } else if (Operands.size()+1 <= Operands.capacity()) {
504 // Otherwise, we have to remove register operands from their register use
505 // list, add the operand, then add the register operands back to their use
506 // list. This also must handle the case when the operand list reallocates
507 // to somewhere else.
508
509 // If insertion of this operand won't cause reallocation of the operand
510 // list, just remove the implicit operands, add the operand, then re-add all
511 // the rest of the operands.
512 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000513 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000514 Operands[i].RemoveRegOperandFromRegInfo();
515 }
516
517 // Add the operand. If it is a register, add it to the reg list.
518 Operands.insert(Operands.begin()+OpNo, Op);
519 Operands[OpNo].ParentMI = this;
520
Dan Gohmand735b802008-10-03 15:45:36 +0000521 if (Operands[OpNo].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000522 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
523
524 // Re-add all the implicit ops.
525 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000526 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000527 Operands[i].AddRegOperandToRegInfo(RegInfo);
528 }
529 } else {
530 // Otherwise, we will be reallocating the operand list. Remove all reg
531 // operands from their list, then readd them after the operand list is
532 // reallocated.
533 RemoveRegOperandsFromUseLists();
534
535 Operands.insert(Operands.begin()+OpNo, Op);
536 Operands[OpNo].ParentMI = this;
537
538 // Re-add all the operands.
539 AddRegOperandsToUseLists(*RegInfo);
540 }
541}
542
543/// RemoveOperand - Erase an operand from an instruction, leaving it with one
544/// fewer operand than it started with.
545///
546void MachineInstr::RemoveOperand(unsigned OpNo) {
547 assert(OpNo < Operands.size() && "Invalid operand number");
548
549 // Special case removing the last one.
550 if (OpNo == Operands.size()-1) {
551 // If needed, remove from the reg def/use list.
Dan Gohmand735b802008-10-03 15:45:36 +0000552 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000553 Operands.back().RemoveRegOperandFromRegInfo();
554
555 Operands.pop_back();
556 return;
557 }
558
559 // Otherwise, we are removing an interior operand. If we have reginfo to
560 // update, remove all operands that will be shifted down from their reg lists,
561 // move everything down, then re-add them.
562 MachineRegisterInfo *RegInfo = getRegInfo();
563 if (RegInfo) {
564 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000565 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000566 Operands[i].RemoveRegOperandFromRegInfo();
567 }
568 }
569
570 Operands.erase(Operands.begin()+OpNo);
571
572 if (RegInfo) {
573 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000574 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000575 Operands[i].AddRegOperandToRegInfo(RegInfo);
576 }
577 }
578}
579
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000580/// addMemOperand - Add a MachineMemOperand to the machine instruction,
581/// referencing arbitrary storage.
582void MachineInstr::addMemOperand(MachineFunction &MF,
583 const MachineMemOperand &MO) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000584 MemOperands.push_back(MO);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000585}
586
587/// clearMemOperands - Erase all of this MachineInstr's MachineMemOperands.
588void MachineInstr::clearMemOperands(MachineFunction &MF) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000589 MemOperands.clear();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000590}
591
Chris Lattner62ed6b92008-01-01 01:12:31 +0000592
Chris Lattner48d7c062006-04-17 21:35:41 +0000593/// removeFromParent - This method unlinks 'this' from the containing basic
594/// block, and returns it, but does not delete it.
595MachineInstr *MachineInstr::removeFromParent() {
596 assert(getParent() && "Not embedded in a basic block!");
597 getParent()->remove(this);
598 return this;
599}
600
601
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000602/// eraseFromParent - This method unlinks 'this' from the containing basic
603/// block, and deletes it.
604void MachineInstr::eraseFromParent() {
605 assert(getParent() && "Not embedded in a basic block!");
606 getParent()->erase(this);
607}
608
609
Brian Gaeke21326fc2004-02-13 04:39:32 +0000610/// OperandComplete - Return true if it's illegal to add a new operand
611///
Chris Lattner2a90ba62004-02-12 16:09:53 +0000612bool MachineInstr::OperandsComplete() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000613 unsigned short NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000614 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000615 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000616 return false;
617}
618
Evan Cheng19e3f312007-05-15 01:26:09 +0000619/// getNumExplicitOperands - Returns the number of non-implicit operands.
620///
621unsigned MachineInstr::getNumExplicitOperands() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000622 unsigned NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000623 if (!TID->isVariadic())
Evan Cheng19e3f312007-05-15 01:26:09 +0000624 return NumOperands;
625
Dan Gohman9407cd42009-04-15 17:59:11 +0000626 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
627 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000628 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng19e3f312007-05-15 01:26:09 +0000629 NumOperands++;
630 }
631 return NumOperands;
632}
633
Chris Lattner8ace2cd2006-10-20 22:39:59 +0000634
Dan Gohman44066042008-07-01 00:05:16 +0000635/// isLabel - Returns true if the MachineInstr represents a label.
636///
637bool MachineInstr::isLabel() const {
638 return getOpcode() == TargetInstrInfo::DBG_LABEL ||
639 getOpcode() == TargetInstrInfo::EH_LABEL ||
640 getOpcode() == TargetInstrInfo::GC_LABEL;
641}
642
Evan Chengbb81d972008-01-31 09:59:15 +0000643/// isDebugLabel - Returns true if the MachineInstr represents a debug label.
644///
645bool MachineInstr::isDebugLabel() const {
Dan Gohman44066042008-07-01 00:05:16 +0000646 return getOpcode() == TargetInstrInfo::DBG_LABEL;
Evan Chengbb81d972008-01-31 09:59:15 +0000647}
648
Evan Chengfaa51072007-04-26 19:00:32 +0000649/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Evan Cheng32eb1f12007-03-26 22:37:45 +0000650/// the specific register or -1 if it is not found. It further tightening
Evan Cheng76d7e762007-02-23 01:04:26 +0000651/// the search criteria to a use that kills the register if isKill is true.
Evan Cheng6130f662008-03-05 00:59:57 +0000652int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
653 const TargetRegisterInfo *TRI) const {
Evan Cheng576d1232006-12-06 08:27:42 +0000654 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengf277ee42007-05-29 18:35:22 +0000655 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000656 if (!MO.isReg() || !MO.isUse())
Evan Cheng6130f662008-03-05 00:59:57 +0000657 continue;
658 unsigned MOReg = MO.getReg();
659 if (!MOReg)
660 continue;
661 if (MOReg == Reg ||
662 (TRI &&
663 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
664 TargetRegisterInfo::isPhysicalRegister(Reg) &&
665 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng76d7e762007-02-23 01:04:26 +0000666 if (!isKill || MO.isKill())
Evan Cheng32eb1f12007-03-26 22:37:45 +0000667 return i;
Evan Cheng576d1232006-12-06 08:27:42 +0000668 }
Evan Cheng32eb1f12007-03-26 22:37:45 +0000669 return -1;
Evan Cheng576d1232006-12-06 08:27:42 +0000670}
671
Evan Cheng6130f662008-03-05 00:59:57 +0000672/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman703bfe62008-05-06 00:20:10 +0000673/// the specified register or -1 if it is not found. If isDead is true, defs
674/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
675/// also checks if there is a def of a super-register.
Evan Cheng6130f662008-03-05 00:59:57 +0000676int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead,
677 const TargetRegisterInfo *TRI) const {
Evan Chengb371f452007-02-19 21:49:54 +0000678 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng6130f662008-03-05 00:59:57 +0000679 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000680 if (!MO.isReg() || !MO.isDef())
Evan Cheng6130f662008-03-05 00:59:57 +0000681 continue;
682 unsigned MOReg = MO.getReg();
683 if (MOReg == Reg ||
684 (TRI &&
685 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
686 TargetRegisterInfo::isPhysicalRegister(Reg) &&
687 TRI->isSubRegister(MOReg, Reg)))
688 if (!isDead || MO.isDead())
689 return i;
Evan Chengb371f452007-02-19 21:49:54 +0000690 }
Evan Cheng6130f662008-03-05 00:59:57 +0000691 return -1;
Evan Chengb371f452007-02-19 21:49:54 +0000692}
Evan Cheng19e3f312007-05-15 01:26:09 +0000693
Evan Chengf277ee42007-05-29 18:35:22 +0000694/// findFirstPredOperandIdx() - Find the index of the first operand in the
695/// operand list that is used to represent the predicate. It returns -1 if
696/// none is found.
697int MachineInstr::findFirstPredOperandIdx() const {
Chris Lattner749c6f62008-01-07 07:27:27 +0000698 const TargetInstrDesc &TID = getDesc();
699 if (TID.isPredicable()) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000700 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner749c6f62008-01-07 07:27:27 +0000701 if (TID.OpInfo[i].isPredicate())
Evan Chengf277ee42007-05-29 18:35:22 +0000702 return i;
Evan Cheng19e3f312007-05-15 01:26:09 +0000703 }
704
Evan Chengf277ee42007-05-29 18:35:22 +0000705 return -1;
Evan Cheng19e3f312007-05-15 01:26:09 +0000706}
Evan Chengb371f452007-02-19 21:49:54 +0000707
Bob Wilsond9df5012009-04-09 17:16:43 +0000708/// isRegTiedToUseOperand - Given the index of a register def operand,
709/// check if the register def is tied to a source operand, due to either
710/// two-address elimination or inline assembly constraints. Returns the
711/// first tied use operand index by reference is UseOpIdx is not null.
Jakob Stoklund Olesence9be2c2009-04-29 20:57:16 +0000712bool MachineInstr::
713isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const {
Evan Chengfb112882009-03-23 08:01:15 +0000714 if (getOpcode() == TargetInstrInfo::INLINEASM) {
Bob Wilsond9df5012009-04-09 17:16:43 +0000715 assert(DefOpIdx >= 2);
716 const MachineOperand &MO = getOperand(DefOpIdx);
Chris Lattnerc30aa7b2009-04-09 23:33:34 +0000717 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
Evan Chengfb112882009-03-23 08:01:15 +0000718 return false;
719 // Determine the actual operand no corresponding to this index.
720 unsigned DefNo = 0;
721 for (unsigned i = 1, e = getNumOperands(); i < e; ) {
722 const MachineOperand &FMO = getOperand(i);
723 assert(FMO.isImm());
724 // Skip over this def.
725 i += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
Bob Wilsond9df5012009-04-09 17:16:43 +0000726 if (i > DefOpIdx)
Evan Chengfb112882009-03-23 08:01:15 +0000727 break;
728 ++DefNo;
729 }
730 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
731 const MachineOperand &FMO = getOperand(i);
732 if (!FMO.isImm())
733 continue;
734 if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse())
735 continue;
736 unsigned Idx;
737 if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000738 Idx == DefNo) {
739 if (UseOpIdx)
740 *UseOpIdx = (unsigned)i + 1;
Evan Chengfb112882009-03-23 08:01:15 +0000741 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000742 }
Evan Chengfb112882009-03-23 08:01:15 +0000743 }
744 }
745
Bob Wilsond9df5012009-04-09 17:16:43 +0000746 assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!");
Chris Lattner749c6f62008-01-07 07:27:27 +0000747 const TargetInstrDesc &TID = getDesc();
Evan Chengef0732d2008-07-10 07:35:43 +0000748 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
749 const MachineOperand &MO = getOperand(i);
Dan Gohman2ce7f202008-12-05 05:45:42 +0000750 if (MO.isReg() && MO.isUse() &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000751 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) {
752 if (UseOpIdx)
753 *UseOpIdx = (unsigned)i;
Evan Chengef0732d2008-07-10 07:35:43 +0000754 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000755 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000756 }
757 return false;
758}
759
Evan Chenga24752f2009-03-19 20:30:06 +0000760/// isRegTiedToDefOperand - Return true if the operand of the specified index
761/// is a register use and it is tied to an def operand. It also returns the def
762/// operand index by reference.
Jakob Stoklund Olesence9be2c2009-04-29 20:57:16 +0000763bool MachineInstr::
764isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const {
Evan Chengfb112882009-03-23 08:01:15 +0000765 if (getOpcode() == TargetInstrInfo::INLINEASM) {
766 const MachineOperand &MO = getOperand(UseOpIdx);
Chris Lattner0c8382c2009-04-09 16:50:43 +0000767 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0)
Evan Chengfb112882009-03-23 08:01:15 +0000768 return false;
769 assert(UseOpIdx > 0);
770 const MachineOperand &UFMO = getOperand(UseOpIdx-1);
771 if (!UFMO.isImm())
772 return false; // Must be physreg uses.
773 unsigned DefNo;
774 if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) {
775 if (!DefOpIdx)
776 return true;
777
778 unsigned DefIdx = 1;
779 // Remember to adjust the index. First operand is asm string, then there
780 // is a flag for each.
781 while (DefNo) {
782 const MachineOperand &FMO = getOperand(DefIdx);
783 assert(FMO.isImm());
784 // Skip over this def.
785 DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
786 --DefNo;
787 }
788 *DefOpIdx = DefIdx+1;
789 return true;
790 }
791 return false;
792 }
793
Evan Chenga24752f2009-03-19 20:30:06 +0000794 const TargetInstrDesc &TID = getDesc();
795 if (UseOpIdx >= TID.getNumOperands())
796 return false;
797 const MachineOperand &MO = getOperand(UseOpIdx);
798 if (!MO.isReg() || !MO.isUse())
799 return false;
800 int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO);
801 if (DefIdx == -1)
802 return false;
803 if (DefOpIdx)
804 *DefOpIdx = (unsigned)DefIdx;
805 return true;
806}
807
Evan Cheng576d1232006-12-06 08:27:42 +0000808/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
809///
810void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
811 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
812 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000813 if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
Evan Cheng576d1232006-12-06 08:27:42 +0000814 continue;
815 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
816 MachineOperand &MOp = getOperand(j);
817 if (!MOp.isIdenticalTo(MO))
818 continue;
819 if (MO.isKill())
820 MOp.setIsKill();
821 else
822 MOp.setIsDead();
823 break;
824 }
825 }
826}
827
Evan Cheng19e3f312007-05-15 01:26:09 +0000828/// copyPredicates - Copies predicate operand(s) from MI.
829void MachineInstr::copyPredicates(const MachineInstr *MI) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000830 const TargetInstrDesc &TID = MI->getDesc();
Evan Chengb27087f2008-03-13 00:44:09 +0000831 if (!TID.isPredicable())
832 return;
833 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
834 if (TID.OpInfo[i].isPredicate()) {
835 // Predicated operands must be last operands.
836 addOperand(MI->getOperand(i));
Evan Cheng19e3f312007-05-15 01:26:09 +0000837 }
838 }
839}
840
Evan Cheng9f1c8312008-07-03 09:09:37 +0000841/// isSafeToMove - Return true if it is safe to move this instruction. If
842/// SawStore is set to true, it means that there is a store (or call) between
843/// the instruction's location and its intended destination.
Dan Gohmanb3b930a2008-11-18 19:04:29 +0000844bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
845 bool &SawStore) const {
Evan Chengb27087f2008-03-13 00:44:09 +0000846 // Ignore stuff that we obviously can't move.
847 if (TID->mayStore() || TID->isCall()) {
848 SawStore = true;
849 return false;
850 }
Dan Gohman237dee12008-12-23 17:28:50 +0000851 if (TID->isTerminator() || TID->hasUnmodeledSideEffects())
Evan Chengb27087f2008-03-13 00:44:09 +0000852 return false;
853
854 // See if this instruction does a load. If so, we have to guarantee that the
855 // loaded value doesn't change between the load and the its intended
856 // destination. The check for isInvariantLoad gives the targe the chance to
857 // classify the load as always returning a constant, e.g. a constant pool
858 // load.
Dan Gohman3e4fb702008-09-24 00:06:15 +0000859 if (TID->mayLoad() && !TII->isInvariantLoad(this))
Evan Chengb27087f2008-03-13 00:44:09 +0000860 // Otherwise, this is a real load. If there is a store between the load and
Dan Gohman3e4fb702008-09-24 00:06:15 +0000861 // end of block, or if the laod is volatile, we can't move it.
Dan Gohmand790a5c2008-10-02 15:04:30 +0000862 return !SawStore && !hasVolatileMemoryRef();
Dan Gohman3e4fb702008-09-24 00:06:15 +0000863
Evan Chengb27087f2008-03-13 00:44:09 +0000864 return true;
865}
866
Evan Chengdf3b9932008-08-27 20:33:50 +0000867/// isSafeToReMat - Return true if it's safe to rematerialize the specified
868/// instruction which defined the specified register instead of copying it.
Dan Gohmanb3b930a2008-11-18 19:04:29 +0000869bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII,
870 unsigned DstReg) const {
Evan Chengdf3b9932008-08-27 20:33:50 +0000871 bool SawStore = false;
Evan Cheng3689ff42008-08-30 09:07:18 +0000872 if (!getDesc().isRematerializable() ||
873 !TII->isTriviallyReMaterializable(this) ||
874 !isSafeToMove(TII, SawStore))
Evan Chengdf3b9932008-08-27 20:33:50 +0000875 return false;
876 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohmancbad42c2008-11-18 19:49:32 +0000877 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000878 if (!MO.isReg())
Evan Chengdf3b9932008-08-27 20:33:50 +0000879 continue;
880 // FIXME: For now, do not remat any instruction with register operands.
881 // Later on, we can loosen the restriction is the register operands have
882 // not been modified between the def and use. Note, this is different from
Evan Cheng8763c1c2008-08-27 20:58:54 +0000883 // MachineSink because the code is no longer in two-address form (at least
Evan Chengdf3b9932008-08-27 20:33:50 +0000884 // partially).
885 if (MO.isUse())
886 return false;
887 else if (!MO.isDead() && MO.getReg() != DstReg)
888 return false;
889 }
890 return true;
891}
892
Dan Gohman3e4fb702008-09-24 00:06:15 +0000893/// hasVolatileMemoryRef - Return true if this instruction may have a
894/// volatile memory reference, or if the information describing the
895/// memory reference is not available. Return false if it is known to
896/// have no volatile memory references.
897bool MachineInstr::hasVolatileMemoryRef() const {
898 // An instruction known never to access memory won't have a volatile access.
899 if (!TID->mayStore() &&
900 !TID->mayLoad() &&
901 !TID->isCall() &&
902 !TID->hasUnmodeledSideEffects())
903 return false;
904
905 // Otherwise, if the instruction has no memory reference information,
906 // conservatively assume it wasn't preserved.
907 if (memoperands_empty())
908 return true;
909
910 // Check the memory reference information for volatile references.
911 for (std::list<MachineMemOperand>::const_iterator I = memoperands_begin(),
912 E = memoperands_end(); I != E; ++I)
913 if (I->isVolatile())
914 return true;
915
916 return false;
917}
918
Brian Gaeke21326fc2004-02-13 04:39:32 +0000919void MachineInstr::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000920 cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000921}
922
Tanya Lattnerb1407622004-06-25 00:13:11 +0000923void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
Mon P Wang5ca6bd12008-10-10 01:43:55 +0000924 raw_os_ostream RawOS(OS);
925 print(RawOS, TM);
926}
927
928void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const {
Chris Lattnere3087892007-12-30 21:31:53 +0000929 // Specialize printing if op#0 is definition
Chris Lattner6a592272002-10-30 01:55:38 +0000930 unsigned StartOp = 0;
Dan Gohmand735b802008-10-03 15:45:36 +0000931 if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000932 getOperand(0).print(OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +0000933 OS << " = ";
934 ++StartOp; // Don't print this operand again!
935 }
Tanya Lattnerb1407622004-06-25 00:13:11 +0000936
Chris Lattner749c6f62008-01-07 07:27:27 +0000937 OS << getDesc().getName();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000938
Chris Lattner6a592272002-10-30 01:55:38 +0000939 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
940 if (i != StartOp)
941 OS << ",";
942 OS << " ";
Chris Lattnerf7382302007-12-30 21:56:09 +0000943 getOperand(i).print(OS, TM);
Chris Lattner10491642002-10-30 00:48:05 +0000944 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000945
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000946 if (!memoperands_empty()) {
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000947 OS << ", Mem:";
Dan Gohmanfed90b62008-07-28 21:51:04 +0000948 for (std::list<MachineMemOperand>::const_iterator i = memoperands_begin(),
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000949 e = memoperands_end(); i != e; ++i) {
950 const MachineMemOperand &MRO = *i;
Dan Gohman69de1932008-02-06 22:27:42 +0000951 const Value *V = MRO.getValue();
952
Dan Gohman69de1932008-02-06 22:27:42 +0000953 assert((MRO.isLoad() || MRO.isStore()) &&
954 "SV has to be a load, store or both.");
955
956 if (MRO.isVolatile())
957 OS << "Volatile ";
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000958
Dan Gohman69de1932008-02-06 22:27:42 +0000959 if (MRO.isLoad())
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000960 OS << "LD";
Dan Gohman69de1932008-02-06 22:27:42 +0000961 if (MRO.isStore())
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000962 OS << "ST";
Dan Gohman69de1932008-02-06 22:27:42 +0000963
Evan Chengbbd83222008-02-08 22:05:07 +0000964 OS << "(" << MRO.getSize() << "," << MRO.getAlignment() << ") [";
Dan Gohman69de1932008-02-06 22:27:42 +0000965
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000966 if (!V)
967 OS << "<unknown>";
968 else if (!V->getName().empty())
969 OS << V->getName();
Chris Lattneredfb72c2008-08-24 20:37:32 +0000970 else if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
Mon P Wang5ca6bd12008-10-10 01:43:55 +0000971 PSV->print(OS);
Chris Lattneredfb72c2008-08-24 20:37:32 +0000972 } else
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000973 OS << V;
974
975 OS << " + " << MRO.getOffset() << "]";
Dan Gohman69de1932008-02-06 22:27:42 +0000976 }
977 }
978
Bill Wendlingb5ef2732009-02-19 21:44:55 +0000979 if (!debugLoc.isUnknown()) {
980 const MachineFunction *MF = getParent()->getParent();
981 DebugLocTuple DLT = MF->getDebugLocTuple(debugLoc);
982 OS << " [dbg: "
983 << DLT.Src << ","
984 << DLT.Line << ","
985 << DLT.Col << "]";
986 }
987
Chris Lattner10491642002-10-30 00:48:05 +0000988 OS << "\n";
989}
990
Owen Andersonb487e722008-01-24 01:10:07 +0000991bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +0000992 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +0000993 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +0000994 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Dan Gohman2ebc11a2008-07-03 01:18:51 +0000995 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +0000996 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +0000997 SmallVector<unsigned,4> DeadOps;
Bill Wendling4a23d722008-03-03 22:14:33 +0000998 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
999 MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001000 if (!MO.isReg() || !MO.isUse())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001001 continue;
1002 unsigned Reg = MO.getReg();
1003 if (!Reg)
1004 continue;
Bill Wendling4a23d722008-03-03 22:14:33 +00001005
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001006 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001007 if (!Found) {
1008 if (MO.isKill())
1009 // The register is already marked kill.
1010 return true;
1011 MO.setIsKill();
1012 Found = true;
1013 }
1014 } else if (hasAliases && MO.isKill() &&
1015 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001016 // A super-register kill already exists.
1017 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001018 return true;
1019 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001020 DeadOps.push_back(i);
Bill Wendling4a23d722008-03-03 22:14:33 +00001021 }
1022 }
1023
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001024 // Trim unneeded kill operands.
1025 while (!DeadOps.empty()) {
1026 unsigned OpIdx = DeadOps.back();
1027 if (getOperand(OpIdx).isImplicit())
1028 RemoveOperand(OpIdx);
1029 else
1030 getOperand(OpIdx).setIsKill(false);
1031 DeadOps.pop_back();
1032 }
1033
Bill Wendling4a23d722008-03-03 22:14:33 +00001034 // If not found, this means an alias of one of the operands is killed. Add a
Owen Andersonb487e722008-01-24 01:10:07 +00001035 // new implicit operand if required.
Dan Gohman3f629402008-09-03 15:56:16 +00001036 if (!Found && AddIfNotFound) {
Bill Wendling4a23d722008-03-03 22:14:33 +00001037 addOperand(MachineOperand::CreateReg(IncomingReg,
1038 false /*IsDef*/,
1039 true /*IsImp*/,
1040 true /*IsKill*/));
Owen Andersonb487e722008-01-24 01:10:07 +00001041 return true;
1042 }
Dan Gohman3f629402008-09-03 15:56:16 +00001043 return Found;
Owen Andersonb487e722008-01-24 01:10:07 +00001044}
1045
1046bool MachineInstr::addRegisterDead(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001047 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001048 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001049 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Evan Cheng01b2e232008-06-27 22:11:49 +00001050 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +00001051 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001052 SmallVector<unsigned,4> DeadOps;
Owen Andersonb487e722008-01-24 01:10:07 +00001053 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1054 MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001055 if (!MO.isReg() || !MO.isDef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001056 continue;
1057 unsigned Reg = MO.getReg();
Dan Gohman3f629402008-09-03 15:56:16 +00001058 if (!Reg)
1059 continue;
1060
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001061 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001062 if (!Found) {
1063 if (MO.isDead())
1064 // The register is already marked dead.
1065 return true;
1066 MO.setIsDead();
1067 Found = true;
1068 }
1069 } else if (hasAliases && MO.isDead() &&
1070 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001071 // There exists a super-register that's marked dead.
1072 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001073 return true;
Owen Anderson22ae9992008-08-14 18:34:18 +00001074 if (RegInfo->getSubRegisters(IncomingReg) &&
1075 RegInfo->getSuperRegisters(Reg) &&
1076 RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001077 DeadOps.push_back(i);
Owen Andersonb487e722008-01-24 01:10:07 +00001078 }
1079 }
1080
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001081 // Trim unneeded dead operands.
1082 while (!DeadOps.empty()) {
1083 unsigned OpIdx = DeadOps.back();
1084 if (getOperand(OpIdx).isImplicit())
1085 RemoveOperand(OpIdx);
1086 else
1087 getOperand(OpIdx).setIsDead(false);
1088 DeadOps.pop_back();
1089 }
1090
Dan Gohman3f629402008-09-03 15:56:16 +00001091 // If not found, this means an alias of one of the operands is dead. Add a
1092 // new implicit operand if required.
1093 if (!Found && AddIfNotFound) {
1094 addOperand(MachineOperand::CreateReg(IncomingReg,
1095 true /*IsDef*/,
1096 true /*IsImp*/,
1097 false /*IsKill*/,
1098 true /*IsDead*/));
Owen Andersonb487e722008-01-24 01:10:07 +00001099 return true;
1100 }
Dan Gohman3f629402008-09-03 15:56:16 +00001101 return Found;
Owen Andersonb487e722008-01-24 01:10:07 +00001102}