blob: d44305f33338774339bd312d08493acc8edb8b00 [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"
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,
Evan Cheng4784f1f2009-06-30 08:49:04 +0000123 bool isKill, bool isDead, bool isUndef) {
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;
Evan Cheng4784f1f2009-06-30 08:49:04 +0000146 IsUndef = isUndef;
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 {
Chris Lattner31530612009-06-24 17:54:48 +0000154 if (getType() != Other.getType() ||
155 getTargetFlags() != Other.getTargetFlags())
156 return false;
Chris Lattnerf7382302007-12-30 21:56:09 +0000157
158 switch (getType()) {
159 default: assert(0 && "Unrecognized operand type");
160 case MachineOperand::MO_Register:
161 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
162 getSubReg() == Other.getSubReg();
163 case MachineOperand::MO_Immediate:
164 return getImm() == Other.getImm();
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000165 case MachineOperand::MO_FPImmediate:
166 return getFPImm() == Other.getFPImm();
Chris Lattnerf7382302007-12-30 21:56:09 +0000167 case MachineOperand::MO_MachineBasicBlock:
168 return getMBB() == Other.getMBB();
169 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000170 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000171 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000172 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattnerf7382302007-12-30 21:56:09 +0000173 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000174 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000175 case MachineOperand::MO_GlobalAddress:
176 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
177 case MachineOperand::MO_ExternalSymbol:
178 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
179 getOffset() == Other.getOffset();
180 }
181}
182
183/// print - Print the specified machine operand.
184///
185void MachineOperand::print(std::ostream &OS, const TargetMachine *TM) const {
Mon P Wang5ca6bd12008-10-10 01:43:55 +0000186 raw_os_ostream RawOS(OS);
187 print(RawOS, TM);
188}
189
190void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
Chris Lattnerf7382302007-12-30 21:56:09 +0000191 switch (getType()) {
192 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000193 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000194 OS << "%reg" << getReg();
195 } else {
196 // If the instruction is embedded into a basic block, we can find the
Chris Lattner62ed6b92008-01-01 01:12:31 +0000197 // target info for the instruction.
Chris Lattnerf7382302007-12-30 21:56:09 +0000198 if (TM == 0)
199 if (const MachineInstr *MI = getParent())
200 if (const MachineBasicBlock *MBB = MI->getParent())
201 if (const MachineFunction *MF = MBB->getParent())
202 TM = &MF->getTarget();
203
204 if (TM)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000205 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
Chris Lattnerf7382302007-12-30 21:56:09 +0000206 else
207 OS << "%mreg" << getReg();
208 }
Dan Gohman2ccc8392008-12-18 21:51:27 +0000209
Evan Cheng4784f1f2009-06-30 08:49:04 +0000210 if (getSubReg() != 0)
Chris Lattner31530612009-06-24 17:54:48 +0000211 OS << ':' << getSubReg();
Dan Gohman2ccc8392008-12-18 21:51:27 +0000212
Evan Cheng4784f1f2009-06-30 08:49:04 +0000213 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
214 isEarlyClobber()) {
Chris Lattner31530612009-06-24 17:54:48 +0000215 OS << '<';
Chris Lattnerf7382302007-12-30 21:56:09 +0000216 bool NeedComma = false;
217 if (isImplicit()) {
Chris Lattner31530612009-06-24 17:54:48 +0000218 if (NeedComma) OS << ',';
Chris Lattnerf7382302007-12-30 21:56:09 +0000219 OS << (isDef() ? "imp-def" : "imp-use");
220 NeedComma = true;
221 } else if (isDef()) {
Chris Lattner31530612009-06-24 17:54:48 +0000222 if (NeedComma) OS << ',';
Dale Johannesen913d3df2008-09-12 17:49:03 +0000223 if (isEarlyClobber())
224 OS << "earlyclobber,";
Chris Lattnerf7382302007-12-30 21:56:09 +0000225 OS << "def";
226 NeedComma = true;
227 }
Evan Cheng4784f1f2009-06-30 08:49:04 +0000228 if (isKill() || isDead() || isUndef()) {
Chris Lattner31530612009-06-24 17:54:48 +0000229 if (NeedComma) OS << ',';
Bill Wendling181eb732008-02-24 00:56:13 +0000230 if (isKill()) OS << "kill";
231 if (isDead()) OS << "dead";
Evan Cheng4784f1f2009-06-30 08:49:04 +0000232 if (isUndef()) {
233 if (isKill() || isDead())
234 OS << ',';
235 OS << "undef";
236 }
Chris Lattnerf7382302007-12-30 21:56:09 +0000237 }
Chris Lattner31530612009-06-24 17:54:48 +0000238 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000239 }
240 break;
241 case MachineOperand::MO_Immediate:
242 OS << getImm();
243 break;
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000244 case MachineOperand::MO_FPImmediate:
Chris Lattner31530612009-06-24 17:54:48 +0000245 if (getFPImm()->getType() == Type::FloatTy)
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000246 OS << getFPImm()->getValueAPF().convertToFloat();
Chris Lattner31530612009-06-24 17:54:48 +0000247 else
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000248 OS << getFPImm()->getValueAPF().convertToDouble();
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000249 break;
Chris Lattnerf7382302007-12-30 21:56:09 +0000250 case MachineOperand::MO_MachineBasicBlock:
251 OS << "mbb<"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000252 << ((Value*)getMBB()->getBasicBlock())->getName()
Chris Lattner31530612009-06-24 17:54:48 +0000253 << "," << (void*)getMBB() << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000254 break;
255 case MachineOperand::MO_FrameIndex:
Chris Lattner31530612009-06-24 17:54:48 +0000256 OS << "<fi#" << getIndex() << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000257 break;
258 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000259 OS << "<cp#" << getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000260 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000261 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000262 break;
263 case MachineOperand::MO_JumpTableIndex:
Chris Lattner31530612009-06-24 17:54:48 +0000264 OS << "<jt#" << getIndex() << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000265 break;
266 case MachineOperand::MO_GlobalAddress:
267 OS << "<ga:" << ((Value*)getGlobal())->getName();
268 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000269 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000270 break;
271 case MachineOperand::MO_ExternalSymbol:
272 OS << "<es:" << getSymbolName();
273 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000274 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000275 break;
276 default:
277 assert(0 && "Unrecognized operand type");
278 }
Chris Lattner31530612009-06-24 17:54:48 +0000279
280 if (unsigned TF = getTargetFlags())
281 OS << "[TF=" << TF << ']';
Chris Lattnerf7382302007-12-30 21:56:09 +0000282}
283
284//===----------------------------------------------------------------------===//
Dan Gohmance42e402008-07-07 20:32:02 +0000285// MachineMemOperand Implementation
286//===----------------------------------------------------------------------===//
287
288MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f,
289 int64_t o, uint64_t s, unsigned int a)
290 : Offset(o), Size(s), V(v),
291 Flags((f & 7) | ((Log2_32(a) + 1) << 3)) {
Dan Gohmanf1bf29e2008-07-08 23:47:04 +0000292 assert(isPowerOf2_32(a) && "Alignment is not a power of 2!");
Dan Gohmanc5e1f982008-07-16 15:56:42 +0000293 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmance42e402008-07-07 20:32:02 +0000294}
295
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000296/// Profile - Gather unique data for the object.
297///
298void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
299 ID.AddInteger(Offset);
300 ID.AddInteger(Size);
301 ID.AddPointer(V);
302 ID.AddInteger(Flags);
303}
304
Dan Gohmance42e402008-07-07 20:32:02 +0000305//===----------------------------------------------------------------------===//
Chris Lattnerf7382302007-12-30 21:56:09 +0000306// MachineInstr Implementation
307//===----------------------------------------------------------------------===//
308
Evan Chengc0f64ff2006-11-27 23:37:22 +0000309/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
Evan Cheng67f660c2006-11-30 07:08:44 +0000310/// TID NULL and no operands.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000311MachineInstr::MachineInstr()
Dale Johannesen06efc022009-01-27 23:20:29 +0000312 : TID(0), NumImplicitOps(0), Parent(0), debugLoc(DebugLoc::getUnknownLoc()) {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000313 // Make sure that we get added to a machine basicblock
314 LeakDetector::addGarbageObject(this);
Chris Lattner72791222002-10-28 20:59:49 +0000315}
316
Evan Cheng67f660c2006-11-30 07:08:44 +0000317void MachineInstr::addImplicitDefUseOperands() {
318 if (TID->ImplicitDefs)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000319 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner8019f412007-12-30 00:41:17 +0000320 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng67f660c2006-11-30 07:08:44 +0000321 if (TID->ImplicitUses)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000322 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner8019f412007-12-30 00:41:17 +0000323 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Evan Chengd7de4962006-11-13 23:34:06 +0000324}
325
326/// MachineInstr ctor - This constructor create a MachineInstr and add the
Evan Chengc0f64ff2006-11-27 23:37:22 +0000327/// implicit operands. It reserves space for number of operands specified by
Chris Lattner749c6f62008-01-07 07:27:27 +0000328/// TargetInstrDesc or the numOperands if it is not zero. (for
Evan Chengc0f64ff2006-11-27 23:37:22 +0000329/// instructions with variable number of operands).
Chris Lattner749c6f62008-01-07 07:27:27 +0000330MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
Dale Johannesen06efc022009-01-27 23:20:29 +0000331 : TID(&tid), NumImplicitOps(0), Parent(0),
332 debugLoc(DebugLoc::getUnknownLoc()) {
Chris Lattner349c4952008-01-07 03:13:06 +0000333 if (!NoImp && TID->getImplicitDefs())
334 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000335 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000336 if (!NoImp && TID->getImplicitUses())
337 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000338 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000339 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Chengfa945722007-10-13 02:23:01 +0000340 if (!NoImp)
341 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000342 // Make sure that we get added to a machine basicblock
343 LeakDetector::addGarbageObject(this);
Evan Chengd7de4962006-11-13 23:34:06 +0000344}
345
Dale Johannesen06efc022009-01-27 23:20:29 +0000346/// MachineInstr ctor - As above, but with a DebugLoc.
347MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl,
348 bool NoImp)
349 : TID(&tid), NumImplicitOps(0), Parent(0), debugLoc(dl) {
350 if (!NoImp && TID->getImplicitDefs())
351 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
352 NumImplicitOps++;
353 if (!NoImp && TID->getImplicitUses())
354 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
355 NumImplicitOps++;
356 Operands.reserve(NumImplicitOps + TID->getNumOperands());
357 if (!NoImp)
358 addImplicitDefUseOperands();
359 // Make sure that we get added to a machine basicblock
360 LeakDetector::addGarbageObject(this);
361}
362
363/// MachineInstr ctor - Work exactly the same as the ctor two above, except
364/// that the MachineInstr is created and added to the end of the specified
365/// basic block.
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000366///
Dale Johannesen06efc022009-01-27 23:20:29 +0000367MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid)
368 : TID(&tid), NumImplicitOps(0), Parent(0),
369 debugLoc(DebugLoc::getUnknownLoc()) {
370 assert(MBB && "Cannot use inserting ctor with null basic block!");
371 if (TID->ImplicitDefs)
372 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
373 NumImplicitOps++;
374 if (TID->ImplicitUses)
375 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
376 NumImplicitOps++;
377 Operands.reserve(NumImplicitOps + TID->getNumOperands());
378 addImplicitDefUseOperands();
379 // Make sure that we get added to a machine basicblock
380 LeakDetector::addGarbageObject(this);
381 MBB->push_back(this); // Add instruction to end of basic block!
382}
383
384/// MachineInstr ctor - As above, but with a DebugLoc.
385///
386MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl,
Chris Lattner749c6f62008-01-07 07:27:27 +0000387 const TargetInstrDesc &tid)
Dale Johannesen06efc022009-01-27 23:20:29 +0000388 : TID(&tid), NumImplicitOps(0), Parent(0), debugLoc(dl) {
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000389 assert(MBB && "Cannot use inserting ctor with null basic block!");
Evan Cheng67f660c2006-11-30 07:08:44 +0000390 if (TID->ImplicitDefs)
Chris Lattner349c4952008-01-07 03:13:06 +0000391 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000392 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +0000393 if (TID->ImplicitUses)
Chris Lattner349c4952008-01-07 03:13:06 +0000394 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000395 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000396 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Cheng67f660c2006-11-30 07:08:44 +0000397 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000398 // Make sure that we get added to a machine basicblock
399 LeakDetector::addGarbageObject(this);
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000400 MBB->push_back(this); // Add instruction to end of basic block!
401}
402
Misha Brukmance22e762004-07-09 14:45:17 +0000403/// MachineInstr ctor - Copies MachineInstr arg exactly
404///
Evan Cheng1ed99222008-07-19 00:37:25 +0000405MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Dale Johannesen06efc022009-01-27 23:20:29 +0000406 : TID(&MI.getDesc()), NumImplicitOps(0), Parent(0),
407 debugLoc(MI.getDebugLoc()) {
Chris Lattner943b5e12006-05-04 19:14:44 +0000408 Operands.reserve(MI.getNumOperands());
Tanya Lattnerb5159ed2004-05-23 20:58:02 +0000409
Misha Brukmance22e762004-07-09 14:45:17 +0000410 // Add operands
Evan Cheng1ed99222008-07-19 00:37:25 +0000411 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
412 addOperand(MI.getOperand(i));
413 NumImplicitOps = MI.NumImplicitOps;
Tanya Lattner0c63e032004-05-24 03:14:18 +0000414
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000415 // Add memory operands.
Dan Gohmanfed90b62008-07-28 21:51:04 +0000416 for (std::list<MachineMemOperand>::const_iterator i = MI.memoperands_begin(),
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000417 j = MI.memoperands_end(); i != j; ++i)
418 addMemOperand(MF, *i);
419
420 // Set parent to null.
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000421 Parent = 0;
Dan Gohman6116a732008-07-21 18:47:29 +0000422
423 LeakDetector::addGarbageObject(this);
Tanya Lattner466b5342004-05-23 19:35:12 +0000424}
425
Misha Brukmance22e762004-07-09 14:45:17 +0000426MachineInstr::~MachineInstr() {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000427 LeakDetector::removeGarbageObject(this);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000428 assert(MemOperands.empty() &&
429 "MachineInstr being deleted with live memoperands!");
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000430#ifndef NDEBUG
Chris Lattner62ed6b92008-01-01 01:12:31 +0000431 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000432 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Dan Gohmand735b802008-10-03 15:45:36 +0000433 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
Chris Lattner62ed6b92008-01-01 01:12:31 +0000434 "Reg operand def/use list corrupted");
435 }
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000436#endif
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000437}
438
Chris Lattner62ed6b92008-01-01 01:12:31 +0000439/// getRegInfo - If this instruction is embedded into a MachineFunction,
440/// return the MachineRegisterInfo object for the current function, otherwise
441/// return null.
442MachineRegisterInfo *MachineInstr::getRegInfo() {
443 if (MachineBasicBlock *MBB = getParent())
Dan Gohman4e526b92008-07-08 23:59:09 +0000444 return &MBB->getParent()->getRegInfo();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000445 return 0;
446}
447
448/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
449/// this instruction from their respective use lists. This requires that the
450/// operands already be on their use lists.
451void MachineInstr::RemoveRegOperandsFromUseLists() {
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].RemoveRegOperandFromRegInfo();
455 }
456}
457
458/// AddRegOperandsToUseLists - Add all of the register operands in
459/// this instruction from their respective use lists. This requires that the
460/// operands not be on their use lists yet.
461void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
462 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000463 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000464 Operands[i].AddRegOperandToRegInfo(&RegInfo);
465 }
466}
467
468
469/// addOperand - Add the specified operand to the instruction. If it is an
470/// implicit operand, it is added to the end of the operand list. If it is
471/// an explicit operand it is added at the end of the explicit operand list
472/// (before the first implicit operand).
473void MachineInstr::addOperand(const MachineOperand &Op) {
Dan Gohmand735b802008-10-03 15:45:36 +0000474 bool isImpReg = Op.isReg() && Op.isImplicit();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000475 assert((isImpReg || !OperandsComplete()) &&
476 "Trying to add an operand to a machine instr that is already done!");
477
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000478 MachineRegisterInfo *RegInfo = getRegInfo();
479
Chris Lattner62ed6b92008-01-01 01:12:31 +0000480 // If we are adding the operand to the end of the list, our job is simpler.
481 // This is true most of the time, so this is a reasonable optimization.
482 if (isImpReg || NumImplicitOps == 0) {
483 // We can only do this optimization if we know that the operand list won't
484 // reallocate.
485 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
486 Operands.push_back(Op);
487
488 // Set the parent of the operand.
489 Operands.back().ParentMI = this;
490
491 // If the operand is a register, update the operand's use list.
Dan Gohmand735b802008-10-03 15:45:36 +0000492 if (Op.isReg())
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000493 Operands.back().AddRegOperandToRegInfo(RegInfo);
Chris Lattner62ed6b92008-01-01 01:12:31 +0000494 return;
495 }
496 }
497
498 // Otherwise, we have to insert a real operand before any implicit ones.
499 unsigned OpNo = Operands.size()-NumImplicitOps;
500
Chris Lattner62ed6b92008-01-01 01:12:31 +0000501 // If this instruction isn't embedded into a function, then we don't need to
502 // update any operand lists.
503 if (RegInfo == 0) {
504 // Simple insertion, no reginfo update needed for other register operands.
505 Operands.insert(Operands.begin()+OpNo, Op);
506 Operands[OpNo].ParentMI = this;
507
508 // Do explicitly set the reginfo for this operand though, to ensure the
509 // next/prev fields are properly nulled out.
Dan Gohmand735b802008-10-03 15:45:36 +0000510 if (Operands[OpNo].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000511 Operands[OpNo].AddRegOperandToRegInfo(0);
512
513 } else if (Operands.size()+1 <= Operands.capacity()) {
514 // Otherwise, we have to remove register operands from their register use
515 // list, add the operand, then add the register operands back to their use
516 // list. This also must handle the case when the operand list reallocates
517 // to somewhere else.
518
519 // If insertion of this operand won't cause reallocation of the operand
520 // list, just remove the implicit operands, add the operand, then re-add all
521 // the rest of the operands.
522 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000523 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000524 Operands[i].RemoveRegOperandFromRegInfo();
525 }
526
527 // Add the operand. If it is a register, add it to the reg list.
528 Operands.insert(Operands.begin()+OpNo, Op);
529 Operands[OpNo].ParentMI = this;
530
Dan Gohmand735b802008-10-03 15:45:36 +0000531 if (Operands[OpNo].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000532 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
533
534 // Re-add all the implicit ops.
535 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000536 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000537 Operands[i].AddRegOperandToRegInfo(RegInfo);
538 }
539 } else {
540 // Otherwise, we will be reallocating the operand list. Remove all reg
541 // operands from their list, then readd them after the operand list is
542 // reallocated.
543 RemoveRegOperandsFromUseLists();
544
545 Operands.insert(Operands.begin()+OpNo, Op);
546 Operands[OpNo].ParentMI = this;
547
548 // Re-add all the operands.
549 AddRegOperandsToUseLists(*RegInfo);
550 }
551}
552
553/// RemoveOperand - Erase an operand from an instruction, leaving it with one
554/// fewer operand than it started with.
555///
556void MachineInstr::RemoveOperand(unsigned OpNo) {
557 assert(OpNo < Operands.size() && "Invalid operand number");
558
559 // Special case removing the last one.
560 if (OpNo == Operands.size()-1) {
561 // If needed, remove from the reg def/use list.
Dan Gohmand735b802008-10-03 15:45:36 +0000562 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000563 Operands.back().RemoveRegOperandFromRegInfo();
564
565 Operands.pop_back();
566 return;
567 }
568
569 // Otherwise, we are removing an interior operand. If we have reginfo to
570 // update, remove all operands that will be shifted down from their reg lists,
571 // move everything down, then re-add them.
572 MachineRegisterInfo *RegInfo = getRegInfo();
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].RemoveRegOperandFromRegInfo();
577 }
578 }
579
580 Operands.erase(Operands.begin()+OpNo);
581
582 if (RegInfo) {
583 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000584 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000585 Operands[i].AddRegOperandToRegInfo(RegInfo);
586 }
587 }
588}
589
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000590/// addMemOperand - Add a MachineMemOperand to the machine instruction,
591/// referencing arbitrary storage.
592void MachineInstr::addMemOperand(MachineFunction &MF,
593 const MachineMemOperand &MO) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000594 MemOperands.push_back(MO);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000595}
596
597/// clearMemOperands - Erase all of this MachineInstr's MachineMemOperands.
598void MachineInstr::clearMemOperands(MachineFunction &MF) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000599 MemOperands.clear();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000600}
601
Chris Lattner62ed6b92008-01-01 01:12:31 +0000602
Chris Lattner48d7c062006-04-17 21:35:41 +0000603/// removeFromParent - This method unlinks 'this' from the containing basic
604/// block, and returns it, but does not delete it.
605MachineInstr *MachineInstr::removeFromParent() {
606 assert(getParent() && "Not embedded in a basic block!");
607 getParent()->remove(this);
608 return this;
609}
610
611
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000612/// eraseFromParent - This method unlinks 'this' from the containing basic
613/// block, and deletes it.
614void MachineInstr::eraseFromParent() {
615 assert(getParent() && "Not embedded in a basic block!");
616 getParent()->erase(this);
617}
618
619
Brian Gaeke21326fc2004-02-13 04:39:32 +0000620/// OperandComplete - Return true if it's illegal to add a new operand
621///
Chris Lattner2a90ba62004-02-12 16:09:53 +0000622bool MachineInstr::OperandsComplete() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000623 unsigned short NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000624 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000625 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000626 return false;
627}
628
Evan Cheng19e3f312007-05-15 01:26:09 +0000629/// getNumExplicitOperands - Returns the number of non-implicit operands.
630///
631unsigned MachineInstr::getNumExplicitOperands() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000632 unsigned NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000633 if (!TID->isVariadic())
Evan Cheng19e3f312007-05-15 01:26:09 +0000634 return NumOperands;
635
Dan Gohman9407cd42009-04-15 17:59:11 +0000636 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
637 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000638 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng19e3f312007-05-15 01:26:09 +0000639 NumOperands++;
640 }
641 return NumOperands;
642}
643
Chris Lattner8ace2cd2006-10-20 22:39:59 +0000644
Dan Gohman44066042008-07-01 00:05:16 +0000645/// isLabel - Returns true if the MachineInstr represents a label.
646///
647bool MachineInstr::isLabel() const {
648 return getOpcode() == TargetInstrInfo::DBG_LABEL ||
649 getOpcode() == TargetInstrInfo::EH_LABEL ||
650 getOpcode() == TargetInstrInfo::GC_LABEL;
651}
652
Evan Chengbb81d972008-01-31 09:59:15 +0000653/// isDebugLabel - Returns true if the MachineInstr represents a debug label.
654///
655bool MachineInstr::isDebugLabel() const {
Dan Gohman44066042008-07-01 00:05:16 +0000656 return getOpcode() == TargetInstrInfo::DBG_LABEL;
Evan Chengbb81d972008-01-31 09:59:15 +0000657}
658
Evan Chengfaa51072007-04-26 19:00:32 +0000659/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Evan Cheng32eb1f12007-03-26 22:37:45 +0000660/// the specific register or -1 if it is not found. It further tightening
Evan Cheng76d7e762007-02-23 01:04:26 +0000661/// the search criteria to a use that kills the register if isKill is true.
Evan Cheng6130f662008-03-05 00:59:57 +0000662int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
663 const TargetRegisterInfo *TRI) const {
Evan Cheng576d1232006-12-06 08:27:42 +0000664 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengf277ee42007-05-29 18:35:22 +0000665 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000666 if (!MO.isReg() || !MO.isUse())
Evan Cheng6130f662008-03-05 00:59:57 +0000667 continue;
668 unsigned MOReg = MO.getReg();
669 if (!MOReg)
670 continue;
671 if (MOReg == Reg ||
672 (TRI &&
673 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
674 TargetRegisterInfo::isPhysicalRegister(Reg) &&
675 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng76d7e762007-02-23 01:04:26 +0000676 if (!isKill || MO.isKill())
Evan Cheng32eb1f12007-03-26 22:37:45 +0000677 return i;
Evan Cheng576d1232006-12-06 08:27:42 +0000678 }
Evan Cheng32eb1f12007-03-26 22:37:45 +0000679 return -1;
Evan Cheng576d1232006-12-06 08:27:42 +0000680}
681
Evan Cheng6130f662008-03-05 00:59:57 +0000682/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman703bfe62008-05-06 00:20:10 +0000683/// the specified register or -1 if it is not found. If isDead is true, defs
684/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
685/// also checks if there is a def of a super-register.
Evan Cheng6130f662008-03-05 00:59:57 +0000686int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead,
687 const TargetRegisterInfo *TRI) const {
Evan Chengb371f452007-02-19 21:49:54 +0000688 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng6130f662008-03-05 00:59:57 +0000689 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000690 if (!MO.isReg() || !MO.isDef())
Evan Cheng6130f662008-03-05 00:59:57 +0000691 continue;
692 unsigned MOReg = MO.getReg();
693 if (MOReg == Reg ||
694 (TRI &&
695 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
696 TargetRegisterInfo::isPhysicalRegister(Reg) &&
697 TRI->isSubRegister(MOReg, Reg)))
698 if (!isDead || MO.isDead())
699 return i;
Evan Chengb371f452007-02-19 21:49:54 +0000700 }
Evan Cheng6130f662008-03-05 00:59:57 +0000701 return -1;
Evan Chengb371f452007-02-19 21:49:54 +0000702}
Evan Cheng19e3f312007-05-15 01:26:09 +0000703
Evan Chengf277ee42007-05-29 18:35:22 +0000704/// findFirstPredOperandIdx() - Find the index of the first operand in the
705/// operand list that is used to represent the predicate. It returns -1 if
706/// none is found.
707int MachineInstr::findFirstPredOperandIdx() const {
Chris Lattner749c6f62008-01-07 07:27:27 +0000708 const TargetInstrDesc &TID = getDesc();
709 if (TID.isPredicable()) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000710 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner749c6f62008-01-07 07:27:27 +0000711 if (TID.OpInfo[i].isPredicate())
Evan Chengf277ee42007-05-29 18:35:22 +0000712 return i;
Evan Cheng19e3f312007-05-15 01:26:09 +0000713 }
714
Evan Chengf277ee42007-05-29 18:35:22 +0000715 return -1;
Evan Cheng19e3f312007-05-15 01:26:09 +0000716}
Evan Chengb371f452007-02-19 21:49:54 +0000717
Bob Wilsond9df5012009-04-09 17:16:43 +0000718/// isRegTiedToUseOperand - Given the index of a register def operand,
719/// check if the register def is tied to a source operand, due to either
720/// two-address elimination or inline assembly constraints. Returns the
721/// first tied use operand index by reference is UseOpIdx is not null.
Jakob Stoklund Olesence9be2c2009-04-29 20:57:16 +0000722bool MachineInstr::
723isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const {
Evan Chengfb112882009-03-23 08:01:15 +0000724 if (getOpcode() == TargetInstrInfo::INLINEASM) {
Bob Wilsond9df5012009-04-09 17:16:43 +0000725 assert(DefOpIdx >= 2);
726 const MachineOperand &MO = getOperand(DefOpIdx);
Chris Lattnerc30aa7b2009-04-09 23:33:34 +0000727 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
Evan Chengfb112882009-03-23 08:01:15 +0000728 return false;
Evan Chengef5d0702009-06-24 02:05:51 +0000729 // Determine the actual operand index that corresponds to this index.
Evan Chengfb112882009-03-23 08:01:15 +0000730 unsigned DefNo = 0;
Evan Chengef5d0702009-06-24 02:05:51 +0000731 unsigned DefPart = 0;
Evan Chengfb112882009-03-23 08:01:15 +0000732 for (unsigned i = 1, e = getNumOperands(); i < e; ) {
733 const MachineOperand &FMO = getOperand(i);
734 assert(FMO.isImm());
735 // Skip over this def.
Evan Chengef5d0702009-06-24 02:05:51 +0000736 unsigned NumOps = InlineAsm::getNumOperandRegisters(FMO.getImm());
737 unsigned PrevDef = i + 1;
738 i = PrevDef + NumOps;
739 if (i > DefOpIdx) {
740 DefPart = DefOpIdx - PrevDef;
Evan Chengfb112882009-03-23 08:01:15 +0000741 break;
Evan Chengef5d0702009-06-24 02:05:51 +0000742 }
Evan Chengfb112882009-03-23 08:01:15 +0000743 ++DefNo;
744 }
Evan Chengef5d0702009-06-24 02:05:51 +0000745 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Evan Chengfb112882009-03-23 08:01:15 +0000746 const MachineOperand &FMO = getOperand(i);
747 if (!FMO.isImm())
748 continue;
749 if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse())
750 continue;
751 unsigned Idx;
Evan Chengef5d0702009-06-24 02:05:51 +0000752 if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000753 Idx == DefNo) {
754 if (UseOpIdx)
Evan Chengef5d0702009-06-24 02:05:51 +0000755 *UseOpIdx = (unsigned)i + 1 + DefPart;
Evan Chengfb112882009-03-23 08:01:15 +0000756 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000757 }
Evan Chengfb112882009-03-23 08:01:15 +0000758 }
Evan Chengef5d0702009-06-24 02:05:51 +0000759 return false;
Evan Chengfb112882009-03-23 08:01:15 +0000760 }
761
Bob Wilsond9df5012009-04-09 17:16:43 +0000762 assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!");
Chris Lattner749c6f62008-01-07 07:27:27 +0000763 const TargetInstrDesc &TID = getDesc();
Evan Chengef0732d2008-07-10 07:35:43 +0000764 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
765 const MachineOperand &MO = getOperand(i);
Dan Gohman2ce7f202008-12-05 05:45:42 +0000766 if (MO.isReg() && MO.isUse() &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000767 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) {
768 if (UseOpIdx)
769 *UseOpIdx = (unsigned)i;
Evan Chengef0732d2008-07-10 07:35:43 +0000770 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000771 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000772 }
773 return false;
774}
775
Evan Chenga24752f2009-03-19 20:30:06 +0000776/// isRegTiedToDefOperand - Return true if the operand of the specified index
777/// is a register use and it is tied to an def operand. It also returns the def
778/// operand index by reference.
Jakob Stoklund Olesence9be2c2009-04-29 20:57:16 +0000779bool MachineInstr::
780isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const {
Evan Chengfb112882009-03-23 08:01:15 +0000781 if (getOpcode() == TargetInstrInfo::INLINEASM) {
782 const MachineOperand &MO = getOperand(UseOpIdx);
Chris Lattner0c8382c2009-04-09 16:50:43 +0000783 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0)
Evan Chengfb112882009-03-23 08:01:15 +0000784 return false;
Evan Chengef5d0702009-06-24 02:05:51 +0000785 int FlagIdx = UseOpIdx - 1;
786 if (FlagIdx < 1)
787 return false;
788 while (!getOperand(FlagIdx).isImm()) {
789 if (--FlagIdx == 0)
790 return false;
791 }
792 const MachineOperand &UFMO = getOperand(FlagIdx);
793 if (FlagIdx + InlineAsm::getNumOperandRegisters(UFMO.getImm()) < UseOpIdx)
794 return false;
Evan Chengfb112882009-03-23 08:01:15 +0000795 unsigned DefNo;
796 if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) {
797 if (!DefOpIdx)
798 return true;
799
800 unsigned DefIdx = 1;
801 // Remember to adjust the index. First operand is asm string, then there
802 // is a flag for each.
803 while (DefNo) {
804 const MachineOperand &FMO = getOperand(DefIdx);
805 assert(FMO.isImm());
806 // Skip over this def.
807 DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
808 --DefNo;
809 }
Evan Chengef5d0702009-06-24 02:05:51 +0000810 *DefOpIdx = DefIdx + UseOpIdx - FlagIdx;
Evan Chengfb112882009-03-23 08:01:15 +0000811 return true;
812 }
813 return false;
814 }
815
Evan Chenga24752f2009-03-19 20:30:06 +0000816 const TargetInstrDesc &TID = getDesc();
817 if (UseOpIdx >= TID.getNumOperands())
818 return false;
819 const MachineOperand &MO = getOperand(UseOpIdx);
820 if (!MO.isReg() || !MO.isUse())
821 return false;
822 int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO);
823 if (DefIdx == -1)
824 return false;
825 if (DefOpIdx)
826 *DefOpIdx = (unsigned)DefIdx;
827 return true;
828}
829
Evan Cheng576d1232006-12-06 08:27:42 +0000830/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
831///
832void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
833 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
834 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000835 if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
Evan Cheng576d1232006-12-06 08:27:42 +0000836 continue;
837 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
838 MachineOperand &MOp = getOperand(j);
839 if (!MOp.isIdenticalTo(MO))
840 continue;
841 if (MO.isKill())
842 MOp.setIsKill();
843 else
844 MOp.setIsDead();
845 break;
846 }
847 }
848}
849
Evan Cheng19e3f312007-05-15 01:26:09 +0000850/// copyPredicates - Copies predicate operand(s) from MI.
851void MachineInstr::copyPredicates(const MachineInstr *MI) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000852 const TargetInstrDesc &TID = MI->getDesc();
Evan Chengb27087f2008-03-13 00:44:09 +0000853 if (!TID.isPredicable())
854 return;
855 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
856 if (TID.OpInfo[i].isPredicate()) {
857 // Predicated operands must be last operands.
858 addOperand(MI->getOperand(i));
Evan Cheng19e3f312007-05-15 01:26:09 +0000859 }
860 }
861}
862
Evan Cheng9f1c8312008-07-03 09:09:37 +0000863/// isSafeToMove - Return true if it is safe to move this instruction. If
864/// SawStore is set to true, it means that there is a store (or call) between
865/// the instruction's location and its intended destination.
Dan Gohmanb3b930a2008-11-18 19:04:29 +0000866bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
867 bool &SawStore) const {
Evan Chengb27087f2008-03-13 00:44:09 +0000868 // Ignore stuff that we obviously can't move.
869 if (TID->mayStore() || TID->isCall()) {
870 SawStore = true;
871 return false;
872 }
Dan Gohman237dee12008-12-23 17:28:50 +0000873 if (TID->isTerminator() || TID->hasUnmodeledSideEffects())
Evan Chengb27087f2008-03-13 00:44:09 +0000874 return false;
875
876 // See if this instruction does a load. If so, we have to guarantee that the
877 // loaded value doesn't change between the load and the its intended
878 // destination. The check for isInvariantLoad gives the targe the chance to
879 // classify the load as always returning a constant, e.g. a constant pool
880 // load.
Dan Gohman3e4fb702008-09-24 00:06:15 +0000881 if (TID->mayLoad() && !TII->isInvariantLoad(this))
Evan Chengb27087f2008-03-13 00:44:09 +0000882 // Otherwise, this is a real load. If there is a store between the load and
Dan Gohman3e4fb702008-09-24 00:06:15 +0000883 // end of block, or if the laod is volatile, we can't move it.
Dan Gohmand790a5c2008-10-02 15:04:30 +0000884 return !SawStore && !hasVolatileMemoryRef();
Dan Gohman3e4fb702008-09-24 00:06:15 +0000885
Evan Chengb27087f2008-03-13 00:44:09 +0000886 return true;
887}
888
Evan Chengdf3b9932008-08-27 20:33:50 +0000889/// isSafeToReMat - Return true if it's safe to rematerialize the specified
890/// instruction which defined the specified register instead of copying it.
Dan Gohmanb3b930a2008-11-18 19:04:29 +0000891bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII,
892 unsigned DstReg) const {
Evan Chengdf3b9932008-08-27 20:33:50 +0000893 bool SawStore = false;
Evan Cheng3689ff42008-08-30 09:07:18 +0000894 if (!getDesc().isRematerializable() ||
895 !TII->isTriviallyReMaterializable(this) ||
896 !isSafeToMove(TII, SawStore))
Evan Chengdf3b9932008-08-27 20:33:50 +0000897 return false;
898 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohmancbad42c2008-11-18 19:49:32 +0000899 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000900 if (!MO.isReg())
Evan Chengdf3b9932008-08-27 20:33:50 +0000901 continue;
902 // FIXME: For now, do not remat any instruction with register operands.
903 // Later on, we can loosen the restriction is the register operands have
904 // not been modified between the def and use. Note, this is different from
Evan Cheng8763c1c2008-08-27 20:58:54 +0000905 // MachineSink because the code is no longer in two-address form (at least
Evan Chengdf3b9932008-08-27 20:33:50 +0000906 // partially).
907 if (MO.isUse())
908 return false;
909 else if (!MO.isDead() && MO.getReg() != DstReg)
910 return false;
911 }
912 return true;
913}
914
Dan Gohman3e4fb702008-09-24 00:06:15 +0000915/// hasVolatileMemoryRef - Return true if this instruction may have a
916/// volatile memory reference, or if the information describing the
917/// memory reference is not available. Return false if it is known to
918/// have no volatile memory references.
919bool MachineInstr::hasVolatileMemoryRef() const {
920 // An instruction known never to access memory won't have a volatile access.
921 if (!TID->mayStore() &&
922 !TID->mayLoad() &&
923 !TID->isCall() &&
924 !TID->hasUnmodeledSideEffects())
925 return false;
926
927 // Otherwise, if the instruction has no memory reference information,
928 // conservatively assume it wasn't preserved.
929 if (memoperands_empty())
930 return true;
931
932 // Check the memory reference information for volatile references.
933 for (std::list<MachineMemOperand>::const_iterator I = memoperands_begin(),
934 E = memoperands_end(); I != E; ++I)
935 if (I->isVolatile())
936 return true;
937
938 return false;
939}
940
Brian Gaeke21326fc2004-02-13 04:39:32 +0000941void MachineInstr::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000942 cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000943}
944
Tanya Lattnerb1407622004-06-25 00:13:11 +0000945void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
Mon P Wang5ca6bd12008-10-10 01:43:55 +0000946 raw_os_ostream RawOS(OS);
947 print(RawOS, TM);
948}
949
950void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const {
Chris Lattnere3087892007-12-30 21:31:53 +0000951 // Specialize printing if op#0 is definition
Chris Lattner6a592272002-10-30 01:55:38 +0000952 unsigned StartOp = 0;
Dan Gohmand735b802008-10-03 15:45:36 +0000953 if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000954 getOperand(0).print(OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +0000955 OS << " = ";
956 ++StartOp; // Don't print this operand again!
957 }
Tanya Lattnerb1407622004-06-25 00:13:11 +0000958
Chris Lattner749c6f62008-01-07 07:27:27 +0000959 OS << getDesc().getName();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000960
Chris Lattner6a592272002-10-30 01:55:38 +0000961 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
962 if (i != StartOp)
963 OS << ",";
964 OS << " ";
Chris Lattnerf7382302007-12-30 21:56:09 +0000965 getOperand(i).print(OS, TM);
Chris Lattner10491642002-10-30 00:48:05 +0000966 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000967
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000968 if (!memoperands_empty()) {
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000969 OS << ", Mem:";
Dan Gohmanfed90b62008-07-28 21:51:04 +0000970 for (std::list<MachineMemOperand>::const_iterator i = memoperands_begin(),
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000971 e = memoperands_end(); i != e; ++i) {
972 const MachineMemOperand &MRO = *i;
Dan Gohman69de1932008-02-06 22:27:42 +0000973 const Value *V = MRO.getValue();
974
Dan Gohman69de1932008-02-06 22:27:42 +0000975 assert((MRO.isLoad() || MRO.isStore()) &&
976 "SV has to be a load, store or both.");
977
978 if (MRO.isVolatile())
979 OS << "Volatile ";
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000980
Dan Gohman69de1932008-02-06 22:27:42 +0000981 if (MRO.isLoad())
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000982 OS << "LD";
Dan Gohman69de1932008-02-06 22:27:42 +0000983 if (MRO.isStore())
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000984 OS << "ST";
Dan Gohman69de1932008-02-06 22:27:42 +0000985
Evan Chengbbd83222008-02-08 22:05:07 +0000986 OS << "(" << MRO.getSize() << "," << MRO.getAlignment() << ") [";
Dan Gohman69de1932008-02-06 22:27:42 +0000987
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000988 if (!V)
989 OS << "<unknown>";
990 else if (!V->getName().empty())
991 OS << V->getName();
Chris Lattneredfb72c2008-08-24 20:37:32 +0000992 else if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
Mon P Wang5ca6bd12008-10-10 01:43:55 +0000993 PSV->print(OS);
Chris Lattneredfb72c2008-08-24 20:37:32 +0000994 } else
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000995 OS << V;
996
997 OS << " + " << MRO.getOffset() << "]";
Dan Gohman69de1932008-02-06 22:27:42 +0000998 }
999 }
1000
Bill Wendlingb5ef2732009-02-19 21:44:55 +00001001 if (!debugLoc.isUnknown()) {
1002 const MachineFunction *MF = getParent()->getParent();
1003 DebugLocTuple DLT = MF->getDebugLocTuple(debugLoc);
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00001004 DICompileUnit CU(DLT.CompileUnit);
1005 std::string Dir, Fn;
Bill Wendlingb5ef2732009-02-19 21:44:55 +00001006 OS << " [dbg: "
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00001007 << CU.getDirectory(Dir) << '/' << CU.getFilename(Fn) << ","
Bill Wendlingb5ef2732009-02-19 21:44:55 +00001008 << DLT.Line << ","
1009 << DLT.Col << "]";
1010 }
1011
Chris Lattner10491642002-10-30 00:48:05 +00001012 OS << "\n";
1013}
1014
Owen Andersonb487e722008-01-24 01:10:07 +00001015bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001016 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001017 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001018 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001019 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +00001020 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001021 SmallVector<unsigned,4> DeadOps;
Bill Wendling4a23d722008-03-03 22:14:33 +00001022 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1023 MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001024 if (!MO.isReg() || !MO.isUse())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001025 continue;
1026 unsigned Reg = MO.getReg();
1027 if (!Reg)
1028 continue;
Bill Wendling4a23d722008-03-03 22:14:33 +00001029
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001030 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001031 if (!Found) {
1032 if (MO.isKill())
1033 // The register is already marked kill.
1034 return true;
1035 MO.setIsKill();
1036 Found = true;
1037 }
1038 } else if (hasAliases && MO.isKill() &&
1039 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001040 // A super-register kill already exists.
1041 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001042 return true;
1043 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001044 DeadOps.push_back(i);
Bill Wendling4a23d722008-03-03 22:14:33 +00001045 }
1046 }
1047
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001048 // Trim unneeded kill operands.
1049 while (!DeadOps.empty()) {
1050 unsigned OpIdx = DeadOps.back();
1051 if (getOperand(OpIdx).isImplicit())
1052 RemoveOperand(OpIdx);
1053 else
1054 getOperand(OpIdx).setIsKill(false);
1055 DeadOps.pop_back();
1056 }
1057
Bill Wendling4a23d722008-03-03 22:14:33 +00001058 // If not found, this means an alias of one of the operands is killed. Add a
Owen Andersonb487e722008-01-24 01:10:07 +00001059 // new implicit operand if required.
Dan Gohman3f629402008-09-03 15:56:16 +00001060 if (!Found && AddIfNotFound) {
Bill Wendling4a23d722008-03-03 22:14:33 +00001061 addOperand(MachineOperand::CreateReg(IncomingReg,
1062 false /*IsDef*/,
1063 true /*IsImp*/,
1064 true /*IsKill*/));
Owen Andersonb487e722008-01-24 01:10:07 +00001065 return true;
1066 }
Dan Gohman3f629402008-09-03 15:56:16 +00001067 return Found;
Owen Andersonb487e722008-01-24 01:10:07 +00001068}
1069
1070bool MachineInstr::addRegisterDead(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001071 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001072 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001073 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Evan Cheng01b2e232008-06-27 22:11:49 +00001074 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +00001075 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001076 SmallVector<unsigned,4> DeadOps;
Owen Andersonb487e722008-01-24 01:10:07 +00001077 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1078 MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001079 if (!MO.isReg() || !MO.isDef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001080 continue;
1081 unsigned Reg = MO.getReg();
Dan Gohman3f629402008-09-03 15:56:16 +00001082 if (!Reg)
1083 continue;
1084
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001085 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001086 if (!Found) {
1087 if (MO.isDead())
1088 // The register is already marked dead.
1089 return true;
1090 MO.setIsDead();
1091 Found = true;
1092 }
1093 } else if (hasAliases && MO.isDead() &&
1094 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001095 // There exists a super-register that's marked dead.
1096 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001097 return true;
Owen Anderson22ae9992008-08-14 18:34:18 +00001098 if (RegInfo->getSubRegisters(IncomingReg) &&
1099 RegInfo->getSuperRegisters(Reg) &&
1100 RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001101 DeadOps.push_back(i);
Owen Andersonb487e722008-01-24 01:10:07 +00001102 }
1103 }
1104
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001105 // Trim unneeded dead operands.
1106 while (!DeadOps.empty()) {
1107 unsigned OpIdx = DeadOps.back();
1108 if (getOperand(OpIdx).isImplicit())
1109 RemoveOperand(OpIdx);
1110 else
1111 getOperand(OpIdx).setIsDead(false);
1112 DeadOps.pop_back();
1113 }
1114
Dan Gohman3f629402008-09-03 15:56:16 +00001115 // If not found, this means an alias of one of the operands is dead. Add a
1116 // new implicit operand if required.
Chris Lattner31530612009-06-24 17:54:48 +00001117 if (Found || !AddIfNotFound)
1118 return Found;
1119
1120 addOperand(MachineOperand::CreateReg(IncomingReg,
1121 true /*IsDef*/,
1122 true /*IsImp*/,
1123 false /*IsKill*/,
1124 true /*IsDead*/));
1125 return true;
Owen Andersonb487e722008-01-24 01:10:07 +00001126}