blob: baab1203df05fa5520dad6a82fd561b66873736d [file] [log] [blame]
Chris Lattner85093632008-01-01 20:36:19 +00001//===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Methods common to all machine instructions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng2682ea02009-03-23 08:01:15 +000015#include "llvm/Constants.h"
16#include "llvm/InlineAsm.h"
Chris Lattner1b989192007-12-31 04:13:23 +000017#include "llvm/Value.h"
Dan Gohman915d8722009-09-23 01:33:16 +000018#include "llvm/Assembly/Writer.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnere45742f2008-01-01 01:12:31 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000021#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Target/TargetMachine.h"
Evan Cheng13d1c292008-01-31 09:59:15 +000023#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner8eaa5a92008-01-07 07:42:25 +000024#include "llvm/Target/TargetInstrDesc.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000025#include "llvm/Target/TargetRegisterInfo.h"
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +000026#include "llvm/Analysis/DebugInfo.h"
Edwin Török675d5622009-07-11 20:10:48 +000027#include "llvm/Support/ErrorHandling.h"
Dan Gohman8b3b5172008-07-17 23:49:46 +000028#include "llvm/Support/LeakDetector.h"
Dan Gohmanac6f8922008-07-07 20:32:02 +000029#include "llvm/Support/MathExtras.h"
Chris Lattner24ae2a92008-08-24 20:37:32 +000030#include "llvm/Support/raw_ostream.h"
Dan Gohman98beebe2008-08-20 15:58:01 +000031#include "llvm/ADT/FoldingSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032using namespace llvm;
33
Chris Lattner7f2d3b82007-12-30 21:56:09 +000034//===----------------------------------------------------------------------===//
35// MachineOperand Implementation
36//===----------------------------------------------------------------------===//
37
Chris Lattnere45742f2008-01-01 01:12:31 +000038/// AddRegOperandToRegInfo - Add this register operand to the specified
39/// MachineRegisterInfo. If it is null, then the next/prev fields should be
40/// explicitly nulled out.
41void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000042 assert(isReg() && "Can only add reg operand to use lists");
Chris Lattnere45742f2008-01-01 01:12:31 +000043
44 // If the reginfo pointer is null, just explicitly null out or next/prev
45 // pointers, to ensure they are not garbage.
46 if (RegInfo == 0) {
47 Contents.Reg.Prev = 0;
48 Contents.Reg.Next = 0;
49 return;
50 }
51
52 // Otherwise, add this operand to the head of the registers use/def list.
Chris Lattner6fc812d2008-01-01 21:08:22 +000053 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
Chris Lattnere45742f2008-01-01 01:12:31 +000054
Chris Lattner6fc812d2008-01-01 21:08:22 +000055 // For SSA values, we prefer to keep the definition at the start of the list.
56 // we do this by skipping over the definition if it is at the head of the
57 // list.
58 if (*Head && (*Head)->isDef())
59 Head = &(*Head)->Contents.Reg.Next;
60
61 Contents.Reg.Next = *Head;
Chris Lattnere45742f2008-01-01 01:12:31 +000062 if (Contents.Reg.Next) {
63 assert(getReg() == Contents.Reg.Next->getReg() &&
64 "Different regs on the same list!");
65 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
66 }
67
Chris Lattner6fc812d2008-01-01 21:08:22 +000068 Contents.Reg.Prev = Head;
69 *Head = this;
Chris Lattnere45742f2008-01-01 01:12:31 +000070}
71
Dan Gohman8ff914c2009-04-15 01:17:37 +000072/// RemoveRegOperandFromRegInfo - Remove this register operand from the
73/// MachineRegisterInfo it is linked with.
74void MachineOperand::RemoveRegOperandFromRegInfo() {
75 assert(isOnRegUseList() && "Reg operand is not on a use list");
76 // Unlink this from the doubly linked list of operands.
77 MachineOperand *NextOp = Contents.Reg.Next;
78 *Contents.Reg.Prev = NextOp;
79 if (NextOp) {
80 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
81 NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
82 }
83 Contents.Reg.Prev = 0;
84 Contents.Reg.Next = 0;
85}
86
Chris Lattnere45742f2008-01-01 01:12:31 +000087void MachineOperand::setReg(unsigned Reg) {
88 if (getReg() == Reg) return; // No change.
89
90 // Otherwise, we have to change the register. If this operand is embedded
91 // into a machine function, we need to update the old and new register's
92 // use/def lists.
93 if (MachineInstr *MI = getParent())
94 if (MachineBasicBlock *MBB = MI->getParent())
95 if (MachineFunction *MF = MBB->getParent()) {
96 RemoveRegOperandFromRegInfo();
97 Contents.Reg.RegNo = Reg;
98 AddRegOperandToRegInfo(&MF->getRegInfo());
99 return;
100 }
101
102 // Otherwise, just change the register, no problem. :)
103 Contents.Reg.RegNo = Reg;
104}
105
106/// ChangeToImmediate - Replace this operand with a new immediate operand of
107/// the specified value. If an operand is known to be an immediate already,
108/// the setImm method should be used.
109void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
110 // If this operand is currently a register operand, and if this is in a
111 // function, deregister the operand from the register's use/def list.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000112 if (isReg() && getParent() && getParent()->getParent() &&
Chris Lattnere45742f2008-01-01 01:12:31 +0000113 getParent()->getParent()->getParent())
114 RemoveRegOperandFromRegInfo();
115
116 OpKind = MO_Immediate;
117 Contents.ImmVal = ImmVal;
118}
119
120/// ChangeToRegister - Replace this operand with a new register operand of
121/// the specified value. If an operand is known to be an register already,
122/// the setReg method should be used.
123void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Evan Cheng9c73db12009-06-30 08:49:04 +0000124 bool isKill, bool isDead, bool isUndef) {
Chris Lattnere45742f2008-01-01 01:12:31 +0000125 // If this operand is already a register operand, use setReg to update the
126 // register's use/def lists.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000127 if (isReg()) {
Dale Johannesen1b0c5782008-09-14 01:44:36 +0000128 assert(!isEarlyClobber());
Chris Lattnere45742f2008-01-01 01:12:31 +0000129 setReg(Reg);
130 } else {
131 // Otherwise, change this to a register and set the reg#.
132 OpKind = MO_Register;
133 Contents.Reg.RegNo = Reg;
134
135 // If this operand is embedded in a function, add the operand to the
136 // register's use/def list.
137 if (MachineInstr *MI = getParent())
138 if (MachineBasicBlock *MBB = MI->getParent())
139 if (MachineFunction *MF = MBB->getParent())
140 AddRegOperandToRegInfo(&MF->getRegInfo());
141 }
142
143 IsDef = isDef;
144 IsImp = isImp;
145 IsKill = isKill;
146 IsDead = isDead;
Evan Cheng9c73db12009-06-30 08:49:04 +0000147 IsUndef = isUndef;
Dale Johannesen1b0c5782008-09-14 01:44:36 +0000148 IsEarlyClobber = false;
Chris Lattnere45742f2008-01-01 01:12:31 +0000149 SubReg = 0;
150}
151
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000152/// isIdenticalTo - Return true if this operand is identical to the specified
153/// operand.
154bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000155 if (getType() != Other.getType() ||
156 getTargetFlags() != Other.getTargetFlags())
157 return false;
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000158
159 switch (getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000160 default: llvm_unreachable("Unrecognized operand type");
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000161 case MachineOperand::MO_Register:
162 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
163 getSubReg() == Other.getSubReg();
164 case MachineOperand::MO_Immediate:
165 return getImm() == Other.getImm();
Nate Begeman6a38ec32008-02-14 07:39:30 +0000166 case MachineOperand::MO_FPImmediate:
167 return getFPImm() == Other.getFPImm();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000168 case MachineOperand::MO_MachineBasicBlock:
169 return getMBB() == Other.getMBB();
170 case MachineOperand::MO_FrameIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000171 return getIndex() == Other.getIndex();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000172 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000173 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000174 case MachineOperand::MO_JumpTableIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000175 return getIndex() == Other.getIndex();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000176 case MachineOperand::MO_GlobalAddress:
177 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
178 case MachineOperand::MO_ExternalSymbol:
179 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
180 getOffset() == Other.getOffset();
181 }
182}
183
184/// print - Print the specified machine operand.
185///
Mon P Wang2f2cd302008-10-10 01:43:55 +0000186void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000187 switch (getType()) {
188 case MachineOperand::MO_Register:
Dan Gohman1e57df32008-02-10 18:45:23 +0000189 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000190 OS << "%reg" << getReg();
191 } else {
192 // If the instruction is embedded into a basic block, we can find the
Chris Lattnere45742f2008-01-01 01:12:31 +0000193 // target info for the instruction.
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000194 if (TM == 0)
195 if (const MachineInstr *MI = getParent())
196 if (const MachineBasicBlock *MBB = MI->getParent())
197 if (const MachineFunction *MF = MBB->getParent())
198 TM = &MF->getTarget();
199
200 if (TM)
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000201 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000202 else
203 OS << "%mreg" << getReg();
204 }
Dan Gohman4849d102008-12-18 21:51:27 +0000205
Evan Cheng9c73db12009-06-30 08:49:04 +0000206 if (getSubReg() != 0)
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000207 OS << ':' << getSubReg();
Dan Gohman4849d102008-12-18 21:51:27 +0000208
Evan Cheng9c73db12009-06-30 08:49:04 +0000209 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
210 isEarlyClobber()) {
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000211 OS << '<';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000212 bool NeedComma = false;
213 if (isImplicit()) {
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000214 if (NeedComma) OS << ',';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000215 OS << (isDef() ? "imp-def" : "imp-use");
216 NeedComma = true;
217 } else if (isDef()) {
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000218 if (NeedComma) OS << ',';
Dale Johannesen38438f72008-09-12 17:49:03 +0000219 if (isEarlyClobber())
220 OS << "earlyclobber,";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000221 OS << "def";
222 NeedComma = true;
223 }
Evan Cheng9c73db12009-06-30 08:49:04 +0000224 if (isKill() || isDead() || isUndef()) {
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000225 if (NeedComma) OS << ',';
Bill Wendling733f0fd2008-02-24 00:56:13 +0000226 if (isKill()) OS << "kill";
227 if (isDead()) OS << "dead";
Evan Cheng9c73db12009-06-30 08:49:04 +0000228 if (isUndef()) {
229 if (isKill() || isDead())
230 OS << ',';
231 OS << "undef";
232 }
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000233 }
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000234 OS << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000235 }
236 break;
237 case MachineOperand::MO_Immediate:
238 OS << getImm();
239 break;
Nate Begeman6a38ec32008-02-14 07:39:30 +0000240 case MachineOperand::MO_FPImmediate:
Owen Anderson35b47072009-08-13 21:58:54 +0000241 if (getFPImm()->getType() == Type::getFloatTy(getFPImm()->getContext()))
Nate Begeman6a38ec32008-02-14 07:39:30 +0000242 OS << getFPImm()->getValueAPF().convertToFloat();
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000243 else
Nate Begeman6a38ec32008-02-14 07:39:30 +0000244 OS << getFPImm()->getValueAPF().convertToDouble();
Nate Begeman6a38ec32008-02-14 07:39:30 +0000245 break;
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000246 case MachineOperand::MO_MachineBasicBlock:
247 OS << "mbb<"
Chris Lattner6017d482007-12-30 23:10:15 +0000248 << ((Value*)getMBB()->getBasicBlock())->getName()
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000249 << "," << (void*)getMBB() << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000250 break;
251 case MachineOperand::MO_FrameIndex:
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000252 OS << "<fi#" << getIndex() << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000253 break;
254 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000255 OS << "<cp#" << getIndex();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000256 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000257 OS << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000258 break;
259 case MachineOperand::MO_JumpTableIndex:
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000260 OS << "<jt#" << getIndex() << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000261 break;
262 case MachineOperand::MO_GlobalAddress:
263 OS << "<ga:" << ((Value*)getGlobal())->getName();
264 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000265 OS << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000266 break;
267 case MachineOperand::MO_ExternalSymbol:
268 OS << "<es:" << getSymbolName();
269 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000270 OS << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000271 break;
272 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000273 llvm_unreachable("Unrecognized operand type");
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000274 }
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000275
276 if (unsigned TF = getTargetFlags())
277 OS << "[TF=" << TF << ']';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000278}
279
280//===----------------------------------------------------------------------===//
Dan Gohmanac6f8922008-07-07 20:32:02 +0000281// MachineMemOperand Implementation
282//===----------------------------------------------------------------------===//
283
284MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f,
285 int64_t o, uint64_t s, unsigned int a)
286 : Offset(o), Size(s), V(v),
287 Flags((f & 7) | ((Log2_32(a) + 1) << 3)) {
Dan Gohman169948d2009-09-21 19:47:04 +0000288 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
Dan Gohman78f9a462008-07-16 15:56:42 +0000289 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmanac6f8922008-07-07 20:32:02 +0000290}
291
Dan Gohman98beebe2008-08-20 15:58:01 +0000292/// Profile - Gather unique data for the object.
293///
294void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
295 ID.AddInteger(Offset);
296 ID.AddInteger(Size);
297 ID.AddPointer(V);
298 ID.AddInteger(Flags);
299}
300
Dan Gohman915d8722009-09-23 01:33:16 +0000301raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MRO) {
302 assert((MRO.isLoad() || MRO.isStore()) &&
303 "SV has to be a load, store or both.");
304
305 if (MRO.isVolatile())
306 OS << "Volatile ";
307
308 if (MRO.isLoad())
309 OS << "LD";
310 if (MRO.isStore())
311 OS << "ST";
312 OS << MRO.getSize();
313
314 // Print the address information.
315 OS << "[";
316 if (!MRO.getValue())
317 OS << "<unknown>";
318 else
319 WriteAsOperand(OS, MRO.getValue(), /*PrintType=*/false);
320
321 // If the alignment of the memory reference itself differs from the alignment
322 // of the base pointer, print the base alignment explicitly, next to the base
323 // pointer.
324 if (MRO.getBaseAlignment() != MRO.getAlignment())
325 OS << "(align=" << MRO.getBaseAlignment() << ")";
326
327 if (MRO.getOffset() != 0)
328 OS << "+" << MRO.getOffset();
329 OS << "]";
330
331 // Print the alignment of the reference.
332 if (MRO.getBaseAlignment() != MRO.getAlignment() ||
333 MRO.getBaseAlignment() != MRO.getSize())
334 OS << "(align=" << MRO.getAlignment() << ")";
335
336 return OS;
337}
338
Dan Gohmanac6f8922008-07-07 20:32:02 +0000339//===----------------------------------------------------------------------===//
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000340// MachineInstr Implementation
341//===----------------------------------------------------------------------===//
342
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
344/// TID NULL and no operands.
345MachineInstr::MachineInstr()
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000346 : TID(0), NumImplicitOps(0), Parent(0), debugLoc(DebugLoc::getUnknownLoc()) {
Dan Gohman8b3b5172008-07-17 23:49:46 +0000347 // Make sure that we get added to a machine basicblock
348 LeakDetector::addGarbageObject(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349}
350
351void MachineInstr::addImplicitDefUseOperands() {
352 if (TID->ImplicitDefs)
Chris Lattner720b6cf2007-12-30 00:12:25 +0000353 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner63ab1f22007-12-30 00:41:17 +0000354 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 if (TID->ImplicitUses)
Chris Lattner720b6cf2007-12-30 00:12:25 +0000356 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner63ab1f22007-12-30 00:41:17 +0000357 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358}
359
360/// MachineInstr ctor - This constructor create a MachineInstr and add the
361/// implicit operands. It reserves space for number of operands specified by
Chris Lattner5b930372008-01-07 07:27:27 +0000362/// TargetInstrDesc or the numOperands if it is not zero. (for
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363/// instructions with variable number of operands).
Chris Lattner5b930372008-01-07 07:27:27 +0000364MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000365 : TID(&tid), NumImplicitOps(0), Parent(0),
366 debugLoc(DebugLoc::getUnknownLoc()) {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000367 if (!NoImp && TID->getImplicitDefs())
368 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369 NumImplicitOps++;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000370 if (!NoImp && TID->getImplicitUses())
371 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 NumImplicitOps++;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000373 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Chengbdf72b42007-10-13 02:23:01 +0000374 if (!NoImp)
375 addImplicitDefUseOperands();
Dan Gohman8b3b5172008-07-17 23:49:46 +0000376 // Make sure that we get added to a machine basicblock
377 LeakDetector::addGarbageObject(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378}
379
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000380/// MachineInstr ctor - As above, but with a DebugLoc.
381MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl,
382 bool NoImp)
383 : TID(&tid), NumImplicitOps(0), Parent(0), debugLoc(dl) {
384 if (!NoImp && TID->getImplicitDefs())
385 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
386 NumImplicitOps++;
387 if (!NoImp && TID->getImplicitUses())
388 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
389 NumImplicitOps++;
390 Operands.reserve(NumImplicitOps + TID->getNumOperands());
391 if (!NoImp)
392 addImplicitDefUseOperands();
393 // Make sure that we get added to a machine basicblock
394 LeakDetector::addGarbageObject(this);
395}
396
397/// MachineInstr ctor - Work exactly the same as the ctor two above, except
398/// that the MachineInstr is created and added to the end of the specified
399/// basic block.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400///
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000401MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid)
402 : TID(&tid), NumImplicitOps(0), Parent(0),
403 debugLoc(DebugLoc::getUnknownLoc()) {
404 assert(MBB && "Cannot use inserting ctor with null basic block!");
405 if (TID->ImplicitDefs)
406 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
407 NumImplicitOps++;
408 if (TID->ImplicitUses)
409 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
410 NumImplicitOps++;
411 Operands.reserve(NumImplicitOps + TID->getNumOperands());
412 addImplicitDefUseOperands();
413 // Make sure that we get added to a machine basicblock
414 LeakDetector::addGarbageObject(this);
415 MBB->push_back(this); // Add instruction to end of basic block!
416}
417
418/// MachineInstr ctor - As above, but with a DebugLoc.
419///
420MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl,
Chris Lattner5b930372008-01-07 07:27:27 +0000421 const TargetInstrDesc &tid)
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000422 : TID(&tid), NumImplicitOps(0), Parent(0), debugLoc(dl) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423 assert(MBB && "Cannot use inserting ctor with null basic block!");
424 if (TID->ImplicitDefs)
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000425 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 NumImplicitOps++;
427 if (TID->ImplicitUses)
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000428 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000429 NumImplicitOps++;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000430 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431 addImplicitDefUseOperands();
Dan Gohman8b3b5172008-07-17 23:49:46 +0000432 // Make sure that we get added to a machine basicblock
433 LeakDetector::addGarbageObject(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 MBB->push_back(this); // Add instruction to end of basic block!
435}
436
437/// MachineInstr ctor - Copies MachineInstr arg exactly
438///
Evan Cheng4ce1a522008-07-19 00:37:25 +0000439MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000440 : TID(&MI.getDesc()), NumImplicitOps(0), Parent(0),
441 debugLoc(MI.getDebugLoc()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 Operands.reserve(MI.getNumOperands());
443
444 // Add operands
Evan Cheng4ce1a522008-07-19 00:37:25 +0000445 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
446 addOperand(MI.getOperand(i));
447 NumImplicitOps = MI.NumImplicitOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448
Dan Gohman221a4372008-07-07 23:14:23 +0000449 // Add memory operands.
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000450 for (std::list<MachineMemOperand>::const_iterator i = MI.memoperands_begin(),
Dan Gohman221a4372008-07-07 23:14:23 +0000451 j = MI.memoperands_end(); i != j; ++i)
452 addMemOperand(MF, *i);
453
454 // Set parent to null.
Chris Lattner7ce487f2007-12-31 04:56:33 +0000455 Parent = 0;
Dan Gohmance232952008-07-21 18:47:29 +0000456
457 LeakDetector::addGarbageObject(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458}
459
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460MachineInstr::~MachineInstr() {
Dan Gohman8b3b5172008-07-17 23:49:46 +0000461 LeakDetector::removeGarbageObject(this);
Dan Gohman221a4372008-07-07 23:14:23 +0000462 assert(MemOperands.empty() &&
463 "MachineInstr being deleted with live memoperands!");
Chris Lattnere722c3f2007-12-30 06:11:04 +0000464#ifndef NDEBUG
Chris Lattnere45742f2008-01-01 01:12:31 +0000465 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere722c3f2007-12-30 06:11:04 +0000466 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000467 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
Chris Lattnere45742f2008-01-01 01:12:31 +0000468 "Reg operand def/use list corrupted");
469 }
Chris Lattnere722c3f2007-12-30 06:11:04 +0000470#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471}
472
Chris Lattnere45742f2008-01-01 01:12:31 +0000473/// getRegInfo - If this instruction is embedded into a MachineFunction,
474/// return the MachineRegisterInfo object for the current function, otherwise
475/// return null.
476MachineRegisterInfo *MachineInstr::getRegInfo() {
477 if (MachineBasicBlock *MBB = getParent())
Dan Gohman07368822008-07-08 23:59:09 +0000478 return &MBB->getParent()->getRegInfo();
Chris Lattnere45742f2008-01-01 01:12:31 +0000479 return 0;
480}
481
482/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
483/// this instruction from their respective use lists. This requires that the
484/// operands already be on their use lists.
485void MachineInstr::RemoveRegOperandsFromUseLists() {
486 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000487 if (Operands[i].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000488 Operands[i].RemoveRegOperandFromRegInfo();
489 }
490}
491
492/// AddRegOperandsToUseLists - Add all of the register operands in
493/// this instruction from their respective use lists. This requires that the
494/// operands not be on their use lists yet.
495void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
496 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000497 if (Operands[i].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000498 Operands[i].AddRegOperandToRegInfo(&RegInfo);
499 }
500}
501
502
503/// addOperand - Add the specified operand to the instruction. If it is an
504/// implicit operand, it is added to the end of the operand list. If it is
505/// an explicit operand it is added at the end of the explicit operand list
506/// (before the first implicit operand).
507void MachineInstr::addOperand(const MachineOperand &Op) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000508 bool isImpReg = Op.isReg() && Op.isImplicit();
Chris Lattnere45742f2008-01-01 01:12:31 +0000509 assert((isImpReg || !OperandsComplete()) &&
510 "Trying to add an operand to a machine instr that is already done!");
511
Dan Gohmana0dff432008-12-09 22:45:08 +0000512 MachineRegisterInfo *RegInfo = getRegInfo();
513
Chris Lattnere45742f2008-01-01 01:12:31 +0000514 // If we are adding the operand to the end of the list, our job is simpler.
515 // This is true most of the time, so this is a reasonable optimization.
516 if (isImpReg || NumImplicitOps == 0) {
517 // We can only do this optimization if we know that the operand list won't
518 // reallocate.
519 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
520 Operands.push_back(Op);
521
522 // Set the parent of the operand.
523 Operands.back().ParentMI = this;
524
525 // If the operand is a register, update the operand's use list.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000526 if (Op.isReg())
Dan Gohmana0dff432008-12-09 22:45:08 +0000527 Operands.back().AddRegOperandToRegInfo(RegInfo);
Chris Lattnere45742f2008-01-01 01:12:31 +0000528 return;
529 }
530 }
531
532 // Otherwise, we have to insert a real operand before any implicit ones.
533 unsigned OpNo = Operands.size()-NumImplicitOps;
534
Chris Lattnere45742f2008-01-01 01:12:31 +0000535 // If this instruction isn't embedded into a function, then we don't need to
536 // update any operand lists.
537 if (RegInfo == 0) {
538 // Simple insertion, no reginfo update needed for other register operands.
539 Operands.insert(Operands.begin()+OpNo, Op);
540 Operands[OpNo].ParentMI = this;
541
542 // Do explicitly set the reginfo for this operand though, to ensure the
543 // next/prev fields are properly nulled out.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000544 if (Operands[OpNo].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000545 Operands[OpNo].AddRegOperandToRegInfo(0);
546
547 } else if (Operands.size()+1 <= Operands.capacity()) {
548 // Otherwise, we have to remove register operands from their register use
549 // list, add the operand, then add the register operands back to their use
550 // list. This also must handle the case when the operand list reallocates
551 // to somewhere else.
552
553 // If insertion of this operand won't cause reallocation of the operand
554 // list, just remove the implicit operands, add the operand, then re-add all
555 // the rest of the operands.
556 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000557 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattnere45742f2008-01-01 01:12:31 +0000558 Operands[i].RemoveRegOperandFromRegInfo();
559 }
560
561 // Add the operand. If it is a register, add it to the reg list.
562 Operands.insert(Operands.begin()+OpNo, Op);
563 Operands[OpNo].ParentMI = this;
564
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000565 if (Operands[OpNo].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000566 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
567
568 // Re-add all the implicit ops.
569 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000570 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattnere45742f2008-01-01 01:12:31 +0000571 Operands[i].AddRegOperandToRegInfo(RegInfo);
572 }
573 } else {
574 // Otherwise, we will be reallocating the operand list. Remove all reg
575 // operands from their list, then readd them after the operand list is
576 // reallocated.
577 RemoveRegOperandsFromUseLists();
578
579 Operands.insert(Operands.begin()+OpNo, Op);
580 Operands[OpNo].ParentMI = this;
581
582 // Re-add all the operands.
583 AddRegOperandsToUseLists(*RegInfo);
584 }
585}
586
587/// RemoveOperand - Erase an operand from an instruction, leaving it with one
588/// fewer operand than it started with.
589///
590void MachineInstr::RemoveOperand(unsigned OpNo) {
591 assert(OpNo < Operands.size() && "Invalid operand number");
592
593 // Special case removing the last one.
594 if (OpNo == Operands.size()-1) {
595 // If needed, remove from the reg def/use list.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000596 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
Chris Lattnere45742f2008-01-01 01:12:31 +0000597 Operands.back().RemoveRegOperandFromRegInfo();
598
599 Operands.pop_back();
600 return;
601 }
602
603 // Otherwise, we are removing an interior operand. If we have reginfo to
604 // update, remove all operands that will be shifted down from their reg lists,
605 // move everything down, then re-add them.
606 MachineRegisterInfo *RegInfo = getRegInfo();
607 if (RegInfo) {
608 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000609 if (Operands[i].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000610 Operands[i].RemoveRegOperandFromRegInfo();
611 }
612 }
613
614 Operands.erase(Operands.begin()+OpNo);
615
616 if (RegInfo) {
617 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000618 if (Operands[i].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000619 Operands[i].AddRegOperandToRegInfo(RegInfo);
620 }
621 }
622}
623
Dan Gohman221a4372008-07-07 23:14:23 +0000624/// addMemOperand - Add a MachineMemOperand to the machine instruction,
625/// referencing arbitrary storage.
626void MachineInstr::addMemOperand(MachineFunction &MF,
627 const MachineMemOperand &MO) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000628 MemOperands.push_back(MO);
Dan Gohman221a4372008-07-07 23:14:23 +0000629}
630
631/// clearMemOperands - Erase all of this MachineInstr's MachineMemOperands.
632void MachineInstr::clearMemOperands(MachineFunction &MF) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000633 MemOperands.clear();
Dan Gohman221a4372008-07-07 23:14:23 +0000634}
635
Chris Lattnere45742f2008-01-01 01:12:31 +0000636
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000637/// removeFromParent - This method unlinks 'this' from the containing basic
638/// block, and returns it, but does not delete it.
639MachineInstr *MachineInstr::removeFromParent() {
640 assert(getParent() && "Not embedded in a basic block!");
641 getParent()->remove(this);
642 return this;
643}
644
645
Dan Gohman221a4372008-07-07 23:14:23 +0000646/// eraseFromParent - This method unlinks 'this' from the containing basic
647/// block, and deletes it.
648void MachineInstr::eraseFromParent() {
649 assert(getParent() && "Not embedded in a basic block!");
650 getParent()->erase(this);
651}
652
653
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000654/// OperandComplete - Return true if it's illegal to add a new operand
655///
656bool MachineInstr::OperandsComplete() const {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000657 unsigned short NumOperands = TID->getNumOperands();
Chris Lattner2fb37c02008-01-07 05:19:29 +0000658 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659 return true; // Broken: we have all the operands of this instruction!
660 return false;
661}
662
663/// getNumExplicitOperands - Returns the number of non-implicit operands.
664///
665unsigned MachineInstr::getNumExplicitOperands() const {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000666 unsigned NumOperands = TID->getNumOperands();
Chris Lattner2fb37c02008-01-07 05:19:29 +0000667 if (!TID->isVariadic())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000668 return NumOperands;
669
Dan Gohman3d880012009-04-15 17:59:11 +0000670 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
671 const MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000672 if (!MO.isReg() || !MO.isImplicit())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000673 NumOperands++;
674 }
675 return NumOperands;
676}
677
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678
Dan Gohmanfa607c92008-07-01 00:05:16 +0000679/// isLabel - Returns true if the MachineInstr represents a label.
680///
681bool MachineInstr::isLabel() const {
682 return getOpcode() == TargetInstrInfo::DBG_LABEL ||
683 getOpcode() == TargetInstrInfo::EH_LABEL ||
684 getOpcode() == TargetInstrInfo::GC_LABEL;
685}
686
Evan Cheng13d1c292008-01-31 09:59:15 +0000687/// isDebugLabel - Returns true if the MachineInstr represents a debug label.
688///
689bool MachineInstr::isDebugLabel() const {
Dan Gohmanfa607c92008-07-01 00:05:16 +0000690 return getOpcode() == TargetInstrInfo::DBG_LABEL;
Evan Cheng13d1c292008-01-31 09:59:15 +0000691}
692
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbach47472be2009-09-17 17:57:26 +0000694/// the specific register or -1 if it is not found. It further tightens
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695/// the search criteria to a use that kills the register if isKill is true.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000696int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
697 const TargetRegisterInfo *TRI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
699 const MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000700 if (!MO.isReg() || !MO.isUse())
Evan Chengc7daf1f2008-03-05 00:59:57 +0000701 continue;
702 unsigned MOReg = MO.getReg();
703 if (!MOReg)
704 continue;
705 if (MOReg == Reg ||
706 (TRI &&
707 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
708 TargetRegisterInfo::isPhysicalRegister(Reg) &&
709 TRI->isSubRegister(MOReg, Reg)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710 if (!isKill || MO.isKill())
711 return i;
712 }
713 return -1;
714}
715
Evan Chengc7daf1f2008-03-05 00:59:57 +0000716/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman2f51e1f2008-05-06 00:20:10 +0000717/// the specified register or -1 if it is not found. If isDead is true, defs
718/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
719/// also checks if there is a def of a super-register.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000720int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead,
721 const TargetRegisterInfo *TRI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000722 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengc7daf1f2008-03-05 00:59:57 +0000723 const MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000724 if (!MO.isReg() || !MO.isDef())
Evan Chengc7daf1f2008-03-05 00:59:57 +0000725 continue;
726 unsigned MOReg = MO.getReg();
727 if (MOReg == Reg ||
728 (TRI &&
729 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
730 TargetRegisterInfo::isPhysicalRegister(Reg) &&
731 TRI->isSubRegister(MOReg, Reg)))
732 if (!isDead || MO.isDead())
733 return i;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734 }
Evan Chengc7daf1f2008-03-05 00:59:57 +0000735 return -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736}
737
738/// findFirstPredOperandIdx() - Find the index of the first operand in the
739/// operand list that is used to represent the predicate. It returns -1 if
740/// none is found.
741int MachineInstr::findFirstPredOperandIdx() const {
Chris Lattner5b930372008-01-07 07:27:27 +0000742 const TargetInstrDesc &TID = getDesc();
743 if (TID.isPredicable()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner5b930372008-01-07 07:27:27 +0000745 if (TID.OpInfo[i].isPredicate())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000746 return i;
747 }
748
749 return -1;
750}
751
Bob Wilsonaded9952009-04-09 17:16:43 +0000752/// isRegTiedToUseOperand - Given the index of a register def operand,
753/// check if the register def is tied to a source operand, due to either
754/// two-address elimination or inline assembly constraints. Returns the
755/// first tied use operand index by reference is UseOpIdx is not null.
Jakob Stoklund Olesencb9f5b52009-04-29 20:57:16 +0000756bool MachineInstr::
757isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const {
Evan Cheng2682ea02009-03-23 08:01:15 +0000758 if (getOpcode() == TargetInstrInfo::INLINEASM) {
Bob Wilsonaded9952009-04-09 17:16:43 +0000759 assert(DefOpIdx >= 2);
760 const MachineOperand &MO = getOperand(DefOpIdx);
Chris Lattnerb1e84232009-04-09 23:33:34 +0000761 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
Evan Cheng2682ea02009-03-23 08:01:15 +0000762 return false;
Evan Cheng3df52f72009-06-24 02:05:51 +0000763 // Determine the actual operand index that corresponds to this index.
Evan Cheng2682ea02009-03-23 08:01:15 +0000764 unsigned DefNo = 0;
Evan Cheng3df52f72009-06-24 02:05:51 +0000765 unsigned DefPart = 0;
Evan Cheng2682ea02009-03-23 08:01:15 +0000766 for (unsigned i = 1, e = getNumOperands(); i < e; ) {
767 const MachineOperand &FMO = getOperand(i);
Jakob Stoklund Olesen458bbfb2009-07-19 19:09:59 +0000768 // After the normal asm operands there may be additional imp-def regs.
769 if (!FMO.isImm())
770 return false;
Evan Cheng2682ea02009-03-23 08:01:15 +0000771 // Skip over this def.
Evan Cheng3df52f72009-06-24 02:05:51 +0000772 unsigned NumOps = InlineAsm::getNumOperandRegisters(FMO.getImm());
773 unsigned PrevDef = i + 1;
774 i = PrevDef + NumOps;
775 if (i > DefOpIdx) {
776 DefPart = DefOpIdx - PrevDef;
Evan Cheng2682ea02009-03-23 08:01:15 +0000777 break;
Evan Cheng3df52f72009-06-24 02:05:51 +0000778 }
Evan Cheng2682ea02009-03-23 08:01:15 +0000779 ++DefNo;
780 }
Evan Cheng3df52f72009-06-24 02:05:51 +0000781 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Evan Cheng2682ea02009-03-23 08:01:15 +0000782 const MachineOperand &FMO = getOperand(i);
783 if (!FMO.isImm())
784 continue;
785 if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse())
786 continue;
787 unsigned Idx;
Evan Cheng3df52f72009-06-24 02:05:51 +0000788 if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) &&
Bob Wilsonaded9952009-04-09 17:16:43 +0000789 Idx == DefNo) {
790 if (UseOpIdx)
Evan Cheng3df52f72009-06-24 02:05:51 +0000791 *UseOpIdx = (unsigned)i + 1 + DefPart;
Evan Cheng2682ea02009-03-23 08:01:15 +0000792 return true;
Bob Wilsonaded9952009-04-09 17:16:43 +0000793 }
Evan Cheng2682ea02009-03-23 08:01:15 +0000794 }
Evan Cheng3df52f72009-06-24 02:05:51 +0000795 return false;
Evan Cheng2682ea02009-03-23 08:01:15 +0000796 }
797
Bob Wilsonaded9952009-04-09 17:16:43 +0000798 assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!");
Chris Lattner5b930372008-01-07 07:27:27 +0000799 const TargetInstrDesc &TID = getDesc();
Evan Chengf1107fd2008-07-10 07:35:43 +0000800 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
801 const MachineOperand &MO = getOperand(i);
Dan Gohman4dbf8792008-12-05 05:45:42 +0000802 if (MO.isReg() && MO.isUse() &&
Bob Wilsonaded9952009-04-09 17:16:43 +0000803 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) {
804 if (UseOpIdx)
805 *UseOpIdx = (unsigned)i;
Evan Chengf1107fd2008-07-10 07:35:43 +0000806 return true;
Bob Wilsonaded9952009-04-09 17:16:43 +0000807 }
Evan Cheng687d1082007-10-12 08:50:34 +0000808 }
809 return false;
810}
811
Evan Cheng48555e82009-03-19 20:30:06 +0000812/// isRegTiedToDefOperand - Return true if the operand of the specified index
813/// is a register use and it is tied to an def operand. It also returns the def
814/// operand index by reference.
Jakob Stoklund Olesencb9f5b52009-04-29 20:57:16 +0000815bool MachineInstr::
816isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const {
Evan Cheng2682ea02009-03-23 08:01:15 +0000817 if (getOpcode() == TargetInstrInfo::INLINEASM) {
818 const MachineOperand &MO = getOperand(UseOpIdx);
Chris Lattner0d0e8a92009-04-09 16:50:43 +0000819 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0)
Evan Cheng2682ea02009-03-23 08:01:15 +0000820 return false;
Jakob Stoklund Olesen4c769c32009-07-16 20:58:34 +0000821
822 // Find the flag operand corresponding to UseOpIdx
823 unsigned FlagIdx, NumOps=0;
824 for (FlagIdx = 1; FlagIdx < UseOpIdx; FlagIdx += NumOps+1) {
825 const MachineOperand &UFMO = getOperand(FlagIdx);
Jakob Stoklund Olesen458bbfb2009-07-19 19:09:59 +0000826 // After the normal asm operands there may be additional imp-def regs.
827 if (!UFMO.isImm())
828 return false;
Jakob Stoklund Olesen4c769c32009-07-16 20:58:34 +0000829 NumOps = InlineAsm::getNumOperandRegisters(UFMO.getImm());
830 assert(NumOps < getNumOperands() && "Invalid inline asm flag");
831 if (UseOpIdx < FlagIdx+NumOps+1)
832 break;
Evan Cheng3df52f72009-06-24 02:05:51 +0000833 }
Jakob Stoklund Olesen4c769c32009-07-16 20:58:34 +0000834 if (FlagIdx >= UseOpIdx)
Evan Cheng3df52f72009-06-24 02:05:51 +0000835 return false;
Jakob Stoklund Olesen4c769c32009-07-16 20:58:34 +0000836 const MachineOperand &UFMO = getOperand(FlagIdx);
Evan Cheng2682ea02009-03-23 08:01:15 +0000837 unsigned DefNo;
838 if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) {
839 if (!DefOpIdx)
840 return true;
841
842 unsigned DefIdx = 1;
843 // Remember to adjust the index. First operand is asm string, then there
844 // is a flag for each.
845 while (DefNo) {
846 const MachineOperand &FMO = getOperand(DefIdx);
847 assert(FMO.isImm());
848 // Skip over this def.
849 DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
850 --DefNo;
851 }
Evan Cheng3df52f72009-06-24 02:05:51 +0000852 *DefOpIdx = DefIdx + UseOpIdx - FlagIdx;
Evan Cheng2682ea02009-03-23 08:01:15 +0000853 return true;
854 }
855 return false;
856 }
857
Evan Cheng48555e82009-03-19 20:30:06 +0000858 const TargetInstrDesc &TID = getDesc();
859 if (UseOpIdx >= TID.getNumOperands())
860 return false;
861 const MachineOperand &MO = getOperand(UseOpIdx);
862 if (!MO.isReg() || !MO.isUse())
863 return false;
864 int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO);
865 if (DefIdx == -1)
866 return false;
867 if (DefOpIdx)
868 *DefOpIdx = (unsigned)DefIdx;
869 return true;
870}
871
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
873///
874void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
875 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
876 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000877 if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878 continue;
879 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
880 MachineOperand &MOp = getOperand(j);
881 if (!MOp.isIdenticalTo(MO))
882 continue;
883 if (MO.isKill())
884 MOp.setIsKill();
885 else
886 MOp.setIsDead();
887 break;
888 }
889 }
890}
891
892/// copyPredicates - Copies predicate operand(s) from MI.
893void MachineInstr::copyPredicates(const MachineInstr *MI) {
Chris Lattner5b930372008-01-07 07:27:27 +0000894 const TargetInstrDesc &TID = MI->getDesc();
Evan Chengbe856622008-03-13 00:44:09 +0000895 if (!TID.isPredicable())
896 return;
897 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
898 if (TID.OpInfo[i].isPredicate()) {
899 // Predicated operands must be last operands.
900 addOperand(MI->getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000901 }
902 }
903}
904
Evan Chenge52c1912008-07-03 09:09:37 +0000905/// isSafeToMove - Return true if it is safe to move this instruction. If
906/// SawStore is set to true, it means that there is a store (or call) between
907/// the instruction's location and its intended destination.
Dan Gohman9ca19a32008-11-18 19:04:29 +0000908bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
909 bool &SawStore) const {
Evan Chengbe856622008-03-13 00:44:09 +0000910 // Ignore stuff that we obviously can't move.
911 if (TID->mayStore() || TID->isCall()) {
912 SawStore = true;
913 return false;
914 }
Dan Gohman64709cd2008-12-23 17:28:50 +0000915 if (TID->isTerminator() || TID->hasUnmodeledSideEffects())
Evan Chengbe856622008-03-13 00:44:09 +0000916 return false;
917
918 // See if this instruction does a load. If so, we have to guarantee that the
919 // loaded value doesn't change between the load and the its intended
920 // destination. The check for isInvariantLoad gives the targe the chance to
921 // classify the load as always returning a constant, e.g. a constant pool
922 // load.
Dan Gohman9ffbed82008-09-24 00:06:15 +0000923 if (TID->mayLoad() && !TII->isInvariantLoad(this))
Evan Chengbe856622008-03-13 00:44:09 +0000924 // Otherwise, this is a real load. If there is a store between the load and
Evan Cheng79d87ec2009-07-28 21:49:18 +0000925 // end of block, or if the load is volatile, we can't move it.
Dan Gohman0ce00b82008-10-02 15:04:30 +0000926 return !SawStore && !hasVolatileMemoryRef();
Dan Gohman9ffbed82008-09-24 00:06:15 +0000927
Evan Chengbe856622008-03-13 00:44:09 +0000928 return true;
929}
930
Evan Cheng75e2cee2008-08-27 20:33:50 +0000931/// isSafeToReMat - Return true if it's safe to rematerialize the specified
932/// instruction which defined the specified register instead of copying it.
Dan Gohman9ca19a32008-11-18 19:04:29 +0000933bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII,
934 unsigned DstReg) const {
Evan Cheng75e2cee2008-08-27 20:33:50 +0000935 bool SawStore = false;
Evan Chenga07a9a22008-08-30 09:07:18 +0000936 if (!getDesc().isRematerializable() ||
937 !TII->isTriviallyReMaterializable(this) ||
938 !isSafeToMove(TII, SawStore))
Evan Cheng75e2cee2008-08-27 20:33:50 +0000939 return false;
940 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohman90feee22008-11-18 19:49:32 +0000941 const MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000942 if (!MO.isReg())
Evan Cheng75e2cee2008-08-27 20:33:50 +0000943 continue;
944 // FIXME: For now, do not remat any instruction with register operands.
945 // Later on, we can loosen the restriction is the register operands have
946 // not been modified between the def and use. Note, this is different from
Evan Chenga02c6692008-08-27 20:58:54 +0000947 // MachineSink because the code is no longer in two-address form (at least
Evan Cheng75e2cee2008-08-27 20:33:50 +0000948 // partially).
949 if (MO.isUse())
950 return false;
951 else if (!MO.isDead() && MO.getReg() != DstReg)
952 return false;
953 }
954 return true;
955}
956
Dan Gohman9ffbed82008-09-24 00:06:15 +0000957/// hasVolatileMemoryRef - Return true if this instruction may have a
958/// volatile memory reference, or if the information describing the
959/// memory reference is not available. Return false if it is known to
960/// have no volatile memory references.
961bool MachineInstr::hasVolatileMemoryRef() const {
962 // An instruction known never to access memory won't have a volatile access.
963 if (!TID->mayStore() &&
964 !TID->mayLoad() &&
965 !TID->isCall() &&
966 !TID->hasUnmodeledSideEffects())
967 return false;
968
969 // Otherwise, if the instruction has no memory reference information,
970 // conservatively assume it wasn't preserved.
971 if (memoperands_empty())
972 return true;
973
974 // Check the memory reference information for volatile references.
975 for (std::list<MachineMemOperand>::const_iterator I = memoperands_begin(),
976 E = memoperands_end(); I != E; ++I)
977 if (I->isVolatile())
978 return true;
979
980 return false;
981}
982
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983void MachineInstr::dump() const {
Chris Lattnerd71b0b02009-08-23 03:41:05 +0000984 errs() << " " << *this;
Mon P Wang2f2cd302008-10-10 01:43:55 +0000985}
986
987void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const {
Chris Lattner9607bb82007-12-30 21:31:53 +0000988 // Specialize printing if op#0 is definition
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000989 unsigned StartOp = 0;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000990 if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000991 getOperand(0).print(OS, TM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000992 OS << " = ";
993 ++StartOp; // Don't print this operand again!
994 }
995
Chris Lattner5b930372008-01-07 07:27:27 +0000996 OS << getDesc().getName();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000997
998 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000999 if (i != StartOp)
1000 OS << ",";
1001 OS << " ";
Chris Lattner7f2d3b82007-12-30 21:56:09 +00001002 getOperand(i).print(OS, TM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001003 }
1004
Dan Gohman221a4372008-07-07 23:14:23 +00001005 if (!memoperands_empty()) {
Dan Gohmanf738b652008-02-07 16:18:00 +00001006 OS << ", Mem:";
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001007 for (std::list<MachineMemOperand>::const_iterator i = memoperands_begin(),
Dan Gohman221a4372008-07-07 23:14:23 +00001008 e = memoperands_end(); i != e; ++i) {
Dan Gohman915d8722009-09-23 01:33:16 +00001009 OS << *i;
1010 if (next(i) != e)
1011 OS << " ";
Dan Gohman12a9c082008-02-06 22:27:42 +00001012 }
1013 }
1014
Bill Wendlingb7596d22009-02-19 21:44:55 +00001015 if (!debugLoc.isUnknown()) {
1016 const MachineFunction *MF = getParent()->getParent();
1017 DebugLocTuple DLT = MF->getDebugLocTuple(debugLoc);
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +00001018 DICompileUnit CU(DLT.CompileUnit);
1019 std::string Dir, Fn;
Bill Wendlingb7596d22009-02-19 21:44:55 +00001020 OS << " [dbg: "
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +00001021 << CU.getDirectory(Dir) << '/' << CU.getFilename(Fn) << ","
Bill Wendlingb7596d22009-02-19 21:44:55 +00001022 << DLT.Line << ","
1023 << DLT.Col << "]";
1024 }
1025
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001026 OS << "\n";
1027}
1028
Owen Anderson58060792008-01-24 01:10:07 +00001029bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman1e57df32008-02-10 18:45:23 +00001030 const TargetRegisterInfo *RegInfo,
Owen Anderson58060792008-01-24 01:10:07 +00001031 bool AddIfNotFound) {
Evan Cheng794d0f72008-04-16 09:41:59 +00001032 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Dan Gohman9d90c632008-07-03 01:18:51 +00001033 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman244b86a2008-09-03 15:56:16 +00001034 bool Found = false;
Evan Cheng794d0f72008-04-16 09:41:59 +00001035 SmallVector<unsigned,4> DeadOps;
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001036 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1037 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenb409fff2009-08-04 20:09:25 +00001038 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng794d0f72008-04-16 09:41:59 +00001039 continue;
1040 unsigned Reg = MO.getReg();
1041 if (!Reg)
1042 continue;
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001043
Evan Cheng794d0f72008-04-16 09:41:59 +00001044 if (Reg == IncomingReg) {
Dan Gohman244b86a2008-09-03 15:56:16 +00001045 if (!Found) {
1046 if (MO.isKill())
1047 // The register is already marked kill.
1048 return true;
Jakob Stoklund Olesen7b8fe132009-08-02 19:13:03 +00001049 if (isPhysReg && isRegTiedToDefOperand(i))
1050 // Two-address uses of physregs must not be marked kill.
1051 return true;
Dan Gohman244b86a2008-09-03 15:56:16 +00001052 MO.setIsKill();
1053 Found = true;
1054 }
1055 } else if (hasAliases && MO.isKill() &&
1056 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng794d0f72008-04-16 09:41:59 +00001057 // A super-register kill already exists.
1058 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman9d90c632008-07-03 01:18:51 +00001059 return true;
1060 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng794d0f72008-04-16 09:41:59 +00001061 DeadOps.push_back(i);
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001062 }
1063 }
1064
Evan Cheng794d0f72008-04-16 09:41:59 +00001065 // Trim unneeded kill operands.
1066 while (!DeadOps.empty()) {
1067 unsigned OpIdx = DeadOps.back();
1068 if (getOperand(OpIdx).isImplicit())
1069 RemoveOperand(OpIdx);
1070 else
1071 getOperand(OpIdx).setIsKill(false);
1072 DeadOps.pop_back();
1073 }
1074
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001075 // If not found, this means an alias of one of the operands is killed. Add a
Owen Anderson58060792008-01-24 01:10:07 +00001076 // new implicit operand if required.
Dan Gohman244b86a2008-09-03 15:56:16 +00001077 if (!Found && AddIfNotFound) {
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001078 addOperand(MachineOperand::CreateReg(IncomingReg,
1079 false /*IsDef*/,
1080 true /*IsImp*/,
1081 true /*IsKill*/));
Owen Anderson58060792008-01-24 01:10:07 +00001082 return true;
1083 }
Dan Gohman244b86a2008-09-03 15:56:16 +00001084 return Found;
Owen Anderson58060792008-01-24 01:10:07 +00001085}
1086
1087bool MachineInstr::addRegisterDead(unsigned IncomingReg,
Dan Gohman1e57df32008-02-10 18:45:23 +00001088 const TargetRegisterInfo *RegInfo,
Owen Anderson58060792008-01-24 01:10:07 +00001089 bool AddIfNotFound) {
Evan Cheng794d0f72008-04-16 09:41:59 +00001090 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Evan Chengdd562a02008-06-27 22:11:49 +00001091 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman244b86a2008-09-03 15:56:16 +00001092 bool Found = false;
Evan Cheng794d0f72008-04-16 09:41:59 +00001093 SmallVector<unsigned,4> DeadOps;
Owen Anderson58060792008-01-24 01:10:07 +00001094 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1095 MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001096 if (!MO.isReg() || !MO.isDef())
Evan Cheng794d0f72008-04-16 09:41:59 +00001097 continue;
1098 unsigned Reg = MO.getReg();
Dan Gohman244b86a2008-09-03 15:56:16 +00001099 if (!Reg)
1100 continue;
1101
Evan Cheng794d0f72008-04-16 09:41:59 +00001102 if (Reg == IncomingReg) {
Dan Gohman244b86a2008-09-03 15:56:16 +00001103 if (!Found) {
1104 if (MO.isDead())
1105 // The register is already marked dead.
1106 return true;
1107 MO.setIsDead();
1108 Found = true;
1109 }
1110 } else if (hasAliases && MO.isDead() &&
1111 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng794d0f72008-04-16 09:41:59 +00001112 // There exists a super-register that's marked dead.
1113 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman9d90c632008-07-03 01:18:51 +00001114 return true;
Owen Andersonc11fa052008-08-14 18:34:18 +00001115 if (RegInfo->getSubRegisters(IncomingReg) &&
1116 RegInfo->getSuperRegisters(Reg) &&
1117 RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng794d0f72008-04-16 09:41:59 +00001118 DeadOps.push_back(i);
Owen Anderson58060792008-01-24 01:10:07 +00001119 }
1120 }
1121
Evan Cheng794d0f72008-04-16 09:41:59 +00001122 // Trim unneeded dead operands.
1123 while (!DeadOps.empty()) {
1124 unsigned OpIdx = DeadOps.back();
1125 if (getOperand(OpIdx).isImplicit())
1126 RemoveOperand(OpIdx);
1127 else
1128 getOperand(OpIdx).setIsDead(false);
1129 DeadOps.pop_back();
1130 }
1131
Dan Gohman244b86a2008-09-03 15:56:16 +00001132 // If not found, this means an alias of one of the operands is dead. Add a
1133 // new implicit operand if required.
Chris Lattnerf29b6dc2009-06-24 17:54:48 +00001134 if (Found || !AddIfNotFound)
1135 return Found;
1136
1137 addOperand(MachineOperand::CreateReg(IncomingReg,
1138 true /*IsDef*/,
1139 true /*IsImp*/,
1140 false /*IsKill*/,
1141 true /*IsDead*/));
1142 return true;
Owen Anderson58060792008-01-24 01:10:07 +00001143}