blob: f919510525f4aa1cfd434953b70a2ebc81e8d067 [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"
Dan Gohman8c2b5252009-10-30 01:27:03 +000016#include "llvm/Function.h"
Evan Chengfb112882009-03-23 08:01:15 +000017#include "llvm/InlineAsm.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000018#include "llvm/Value.h"
Dan Gohmancd26ec52009-09-23 01:33:16 +000019#include "llvm/Assembly/Writer.h"
Chris Lattner8517e1f2004-02-19 16:17:08 +000020#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000021#include "llvm/CodeGen/MachineMemOperand.h"
Chris Lattner62ed6b92008-01-01 01:12:31 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000023#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner10491642002-10-30 00:48:05 +000024#include "llvm/Target/TargetMachine.h"
Evan Chengbb81d972008-01-31 09:59:15 +000025#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerf14cf852008-01-07 07:42:25 +000026#include "llvm/Target/TargetInstrDesc.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000027#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmane33f44c2009-10-07 17:38:06 +000028#include "llvm/Analysis/AliasAnalysis.h"
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +000029#include "llvm/Analysis/DebugInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000030#include "llvm/Support/ErrorHandling.h"
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000031#include "llvm/Support/LeakDetector.h"
Dan Gohmance42e402008-07-07 20:32:02 +000032#include "llvm/Support/MathExtras.h"
Chris Lattneredfb72c2008-08-24 20:37:32 +000033#include "llvm/Support/raw_ostream.h"
Dan Gohmanb8d2f552008-08-20 15:58:01 +000034#include "llvm/ADT/FoldingSet.h"
Chris Lattner0742b592004-02-23 18:38:20 +000035using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000036
Chris Lattnerf7382302007-12-30 21:56:09 +000037//===----------------------------------------------------------------------===//
38// MachineOperand Implementation
39//===----------------------------------------------------------------------===//
40
Chris Lattner62ed6b92008-01-01 01:12:31 +000041/// AddRegOperandToRegInfo - Add this register operand to the specified
42/// MachineRegisterInfo. If it is null, then the next/prev fields should be
43/// explicitly nulled out.
44void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) {
Dan Gohmand735b802008-10-03 15:45:36 +000045 assert(isReg() && "Can only add reg operand to use lists");
Chris Lattner62ed6b92008-01-01 01:12:31 +000046
47 // If the reginfo pointer is null, just explicitly null out or next/prev
48 // pointers, to ensure they are not garbage.
49 if (RegInfo == 0) {
50 Contents.Reg.Prev = 0;
51 Contents.Reg.Next = 0;
52 return;
53 }
54
55 // Otherwise, add this operand to the head of the registers use/def list.
Chris Lattner80fe5312008-01-01 21:08:22 +000056 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
Chris Lattner62ed6b92008-01-01 01:12:31 +000057
Chris Lattner80fe5312008-01-01 21:08:22 +000058 // For SSA values, we prefer to keep the definition at the start of the list.
59 // we do this by skipping over the definition if it is at the head of the
60 // list.
61 if (*Head && (*Head)->isDef())
62 Head = &(*Head)->Contents.Reg.Next;
63
64 Contents.Reg.Next = *Head;
Chris Lattner62ed6b92008-01-01 01:12:31 +000065 if (Contents.Reg.Next) {
66 assert(getReg() == Contents.Reg.Next->getReg() &&
67 "Different regs on the same list!");
68 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
69 }
70
Chris Lattner80fe5312008-01-01 21:08:22 +000071 Contents.Reg.Prev = Head;
72 *Head = this;
Chris Lattner62ed6b92008-01-01 01:12:31 +000073}
74
Dan Gohman3bc1a372009-04-15 01:17:37 +000075/// RemoveRegOperandFromRegInfo - Remove this register operand from the
76/// MachineRegisterInfo it is linked with.
77void MachineOperand::RemoveRegOperandFromRegInfo() {
78 assert(isOnRegUseList() && "Reg operand is not on a use list");
79 // Unlink this from the doubly linked list of operands.
80 MachineOperand *NextOp = Contents.Reg.Next;
81 *Contents.Reg.Prev = NextOp;
82 if (NextOp) {
83 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
84 NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
85 }
86 Contents.Reg.Prev = 0;
87 Contents.Reg.Next = 0;
88}
89
Chris Lattner62ed6b92008-01-01 01:12:31 +000090void MachineOperand::setReg(unsigned Reg) {
91 if (getReg() == Reg) return; // No change.
92
93 // Otherwise, we have to change the register. If this operand is embedded
94 // into a machine function, we need to update the old and new register's
95 // use/def lists.
96 if (MachineInstr *MI = getParent())
97 if (MachineBasicBlock *MBB = MI->getParent())
98 if (MachineFunction *MF = MBB->getParent()) {
99 RemoveRegOperandFromRegInfo();
100 Contents.Reg.RegNo = Reg;
101 AddRegOperandToRegInfo(&MF->getRegInfo());
102 return;
103 }
104
105 // Otherwise, just change the register, no problem. :)
106 Contents.Reg.RegNo = Reg;
107}
108
109/// ChangeToImmediate - Replace this operand with a new immediate operand of
110/// the specified value. If an operand is known to be an immediate already,
111/// the setImm method should be used.
112void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
113 // If this operand is currently a register operand, and if this is in a
114 // function, deregister the operand from the register's use/def list.
Dan Gohmand735b802008-10-03 15:45:36 +0000115 if (isReg() && getParent() && getParent()->getParent() &&
Chris Lattner62ed6b92008-01-01 01:12:31 +0000116 getParent()->getParent()->getParent())
117 RemoveRegOperandFromRegInfo();
118
119 OpKind = MO_Immediate;
120 Contents.ImmVal = ImmVal;
121}
122
123/// ChangeToRegister - Replace this operand with a new register operand of
124/// the specified value. If an operand is known to be an register already,
125/// the setReg method should be used.
126void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Evan Cheng4784f1f2009-06-30 08:49:04 +0000127 bool isKill, bool isDead, bool isUndef) {
Chris Lattner62ed6b92008-01-01 01:12:31 +0000128 // If this operand is already a register operand, use setReg to update the
129 // register's use/def lists.
Dan Gohmand735b802008-10-03 15:45:36 +0000130 if (isReg()) {
Dale Johannesene0091802008-09-14 01:44:36 +0000131 assert(!isEarlyClobber());
Chris Lattner62ed6b92008-01-01 01:12:31 +0000132 setReg(Reg);
133 } else {
134 // Otherwise, change this to a register and set the reg#.
135 OpKind = MO_Register;
136 Contents.Reg.RegNo = Reg;
137
138 // If this operand is embedded in a function, add the operand to the
139 // register's use/def list.
140 if (MachineInstr *MI = getParent())
141 if (MachineBasicBlock *MBB = MI->getParent())
142 if (MachineFunction *MF = MBB->getParent())
143 AddRegOperandToRegInfo(&MF->getRegInfo());
144 }
145
146 IsDef = isDef;
147 IsImp = isImp;
148 IsKill = isKill;
149 IsDead = isDead;
Evan Cheng4784f1f2009-06-30 08:49:04 +0000150 IsUndef = isUndef;
Dale Johannesene0091802008-09-14 01:44:36 +0000151 IsEarlyClobber = false;
Chris Lattner62ed6b92008-01-01 01:12:31 +0000152 SubReg = 0;
153}
154
Chris Lattnerf7382302007-12-30 21:56:09 +0000155/// isIdenticalTo - Return true if this operand is identical to the specified
156/// operand.
157bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
Chris Lattner31530612009-06-24 17:54:48 +0000158 if (getType() != Other.getType() ||
159 getTargetFlags() != Other.getTargetFlags())
160 return false;
Chris Lattnerf7382302007-12-30 21:56:09 +0000161
162 switch (getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000163 default: llvm_unreachable("Unrecognized operand type");
Chris Lattnerf7382302007-12-30 21:56:09 +0000164 case MachineOperand::MO_Register:
165 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
166 getSubReg() == Other.getSubReg();
167 case MachineOperand::MO_Immediate:
168 return getImm() == Other.getImm();
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000169 case MachineOperand::MO_FPImmediate:
170 return getFPImm() == Other.getFPImm();
Chris Lattnerf7382302007-12-30 21:56:09 +0000171 case MachineOperand::MO_MachineBasicBlock:
172 return getMBB() == Other.getMBB();
173 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000174 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000175 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000176 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattnerf7382302007-12-30 21:56:09 +0000177 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000178 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000179 case MachineOperand::MO_GlobalAddress:
180 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
181 case MachineOperand::MO_ExternalSymbol:
182 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
183 getOffset() == Other.getOffset();
Dan Gohman8c2b5252009-10-30 01:27:03 +0000184 case MachineOperand::MO_BlockAddress:
185 return getBlockAddress() == Other.getBlockAddress();
Chris Lattnerf7382302007-12-30 21:56:09 +0000186 }
187}
188
189/// print - Print the specified machine operand.
190///
Mon P Wang5ca6bd12008-10-10 01:43:55 +0000191void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
Dan Gohman80f6c582009-11-09 19:38:45 +0000192 // If the instruction is embedded into a basic block, we can find the
193 // target info for the instruction.
194 if (!TM)
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
Chris Lattnerf7382302007-12-30 21:56:09 +0000200 switch (getType()) {
201 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000202 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000203 OS << "%reg" << getReg();
204 } else {
Chris Lattnerf7382302007-12-30 21:56:09 +0000205 if (TM)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000206 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
Chris Lattnerf7382302007-12-30 21:56:09 +0000207 else
Dan Gohman0ba90f32009-10-31 20:19:03 +0000208 OS << "%physreg" << getReg();
Chris Lattnerf7382302007-12-30 21:56:09 +0000209 }
Dan Gohman2ccc8392008-12-18 21:51:27 +0000210
Evan Cheng4784f1f2009-06-30 08:49:04 +0000211 if (getSubReg() != 0)
Chris Lattner31530612009-06-24 17:54:48 +0000212 OS << ':' << getSubReg();
Dan Gohman2ccc8392008-12-18 21:51:27 +0000213
Evan Cheng4784f1f2009-06-30 08:49:04 +0000214 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
215 isEarlyClobber()) {
Chris Lattner31530612009-06-24 17:54:48 +0000216 OS << '<';
Chris Lattnerf7382302007-12-30 21:56:09 +0000217 bool NeedComma = false;
Evan Cheng07897072009-10-14 23:37:31 +0000218 if (isDef()) {
Chris Lattner31530612009-06-24 17:54:48 +0000219 if (NeedComma) OS << ',';
Dale Johannesen913d3df2008-09-12 17:49:03 +0000220 if (isEarlyClobber())
221 OS << "earlyclobber,";
Evan Cheng07897072009-10-14 23:37:31 +0000222 if (isImplicit())
223 OS << "imp-";
Chris Lattnerf7382302007-12-30 21:56:09 +0000224 OS << "def";
225 NeedComma = true;
Evan Cheng5affca02009-10-21 07:56:02 +0000226 } else if (isImplicit()) {
Evan Cheng07897072009-10-14 23:37:31 +0000227 OS << "imp-use";
Evan Cheng5affca02009-10-21 07:56:02 +0000228 NeedComma = true;
229 }
Evan Cheng07897072009-10-14 23:37:31 +0000230
Evan Cheng4784f1f2009-06-30 08:49:04 +0000231 if (isKill() || isDead() || isUndef()) {
Chris Lattner31530612009-06-24 17:54:48 +0000232 if (NeedComma) OS << ',';
Bill Wendling181eb732008-02-24 00:56:13 +0000233 if (isKill()) OS << "kill";
234 if (isDead()) OS << "dead";
Evan Cheng4784f1f2009-06-30 08:49:04 +0000235 if (isUndef()) {
236 if (isKill() || isDead())
237 OS << ',';
238 OS << "undef";
239 }
Chris Lattnerf7382302007-12-30 21:56:09 +0000240 }
Chris Lattner31530612009-06-24 17:54:48 +0000241 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000242 }
243 break;
244 case MachineOperand::MO_Immediate:
245 OS << getImm();
246 break;
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000247 case MachineOperand::MO_FPImmediate:
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000248 if (getFPImm()->getType()->isFloatTy())
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000249 OS << getFPImm()->getValueAPF().convertToFloat();
Chris Lattner31530612009-06-24 17:54:48 +0000250 else
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000251 OS << getFPImm()->getValueAPF().convertToDouble();
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000252 break;
Chris Lattnerf7382302007-12-30 21:56:09 +0000253 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman0ba90f32009-10-31 20:19:03 +0000254 OS << "<BB#" << getMBB()->getNumber() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000255 break;
256 case MachineOperand::MO_FrameIndex:
Chris Lattner31530612009-06-24 17:54:48 +0000257 OS << "<fi#" << getIndex() << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000258 break;
259 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000260 OS << "<cp#" << getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000261 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000262 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000263 break;
264 case MachineOperand::MO_JumpTableIndex:
Chris Lattner31530612009-06-24 17:54:48 +0000265 OS << "<jt#" << getIndex() << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000266 break;
267 case MachineOperand::MO_GlobalAddress:
Dan Gohman8d4e3b52009-11-06 18:03:10 +0000268 OS << "<ga:";
269 WriteAsOperand(OS, getGlobal(), /*PrintType=*/false);
Chris Lattnerf7382302007-12-30 21:56:09 +0000270 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000271 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000272 break;
273 case MachineOperand::MO_ExternalSymbol:
274 OS << "<es:" << getSymbolName();
275 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000276 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000277 break;
Dan Gohman8c2b5252009-10-30 01:27:03 +0000278 case MachineOperand::MO_BlockAddress:
Dan Gohman0ba90f32009-10-31 20:19:03 +0000279 OS << "<";
280 WriteAsOperand(OS, getBlockAddress(), /*PrintType=*/false);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000281 OS << '>';
282 break;
Chris Lattnerf7382302007-12-30 21:56:09 +0000283 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000284 llvm_unreachable("Unrecognized operand type");
Chris Lattnerf7382302007-12-30 21:56:09 +0000285 }
Chris Lattner31530612009-06-24 17:54:48 +0000286
287 if (unsigned TF = getTargetFlags())
288 OS << "[TF=" << TF << ']';
Chris Lattnerf7382302007-12-30 21:56:09 +0000289}
290
291//===----------------------------------------------------------------------===//
Dan Gohmance42e402008-07-07 20:32:02 +0000292// MachineMemOperand Implementation
293//===----------------------------------------------------------------------===//
294
295MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f,
296 int64_t o, uint64_t s, unsigned int a)
297 : Offset(o), Size(s), V(v),
298 Flags((f & 7) | ((Log2_32(a) + 1) << 3)) {
Dan Gohman28f02fd2009-09-21 19:47:04 +0000299 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
Dan Gohmanc5e1f982008-07-16 15:56:42 +0000300 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmance42e402008-07-07 20:32:02 +0000301}
302
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000303/// Profile - Gather unique data for the object.
304///
305void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
306 ID.AddInteger(Offset);
307 ID.AddInteger(Size);
308 ID.AddPointer(V);
309 ID.AddInteger(Flags);
310}
311
Dan Gohmanc76909a2009-09-25 20:36:54 +0000312void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
313 // The Value and Offset may differ due to CSE. But the flags and size
314 // should be the same.
315 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
316 assert(MMO->getSize() == getSize() && "Size mismatch!");
317
318 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
319 // Update the alignment value.
320 Flags = (Flags & 7) | ((Log2_32(MMO->getBaseAlignment()) + 1) << 3);
321 // Also update the base and offset, because the new alignment may
322 // not be applicable with the old ones.
323 V = MMO->getValue();
324 Offset = MMO->getOffset();
325 }
326}
327
Dan Gohman4b2ebc12009-09-25 23:33:20 +0000328/// getAlignment - Return the minimum known alignment in bytes of the
329/// actual memory reference.
330uint64_t MachineMemOperand::getAlignment() const {
331 return MinAlign(getBaseAlignment(), getOffset());
332}
333
Dan Gohmanc76909a2009-09-25 20:36:54 +0000334raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) {
335 assert((MMO.isLoad() || MMO.isStore()) &&
Dan Gohmancd26ec52009-09-23 01:33:16 +0000336 "SV has to be a load, store or both.");
337
Dan Gohmanc76909a2009-09-25 20:36:54 +0000338 if (MMO.isVolatile())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000339 OS << "Volatile ";
340
Dan Gohmanc76909a2009-09-25 20:36:54 +0000341 if (MMO.isLoad())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000342 OS << "LD";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000343 if (MMO.isStore())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000344 OS << "ST";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000345 OS << MMO.getSize();
Dan Gohmancd26ec52009-09-23 01:33:16 +0000346
347 // Print the address information.
348 OS << "[";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000349 if (!MMO.getValue())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000350 OS << "<unknown>";
351 else
Dan Gohmanc76909a2009-09-25 20:36:54 +0000352 WriteAsOperand(OS, MMO.getValue(), /*PrintType=*/false);
Dan Gohmancd26ec52009-09-23 01:33:16 +0000353
354 // If the alignment of the memory reference itself differs from the alignment
355 // of the base pointer, print the base alignment explicitly, next to the base
356 // pointer.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000357 if (MMO.getBaseAlignment() != MMO.getAlignment())
358 OS << "(align=" << MMO.getBaseAlignment() << ")";
Dan Gohmancd26ec52009-09-23 01:33:16 +0000359
Dan Gohmanc76909a2009-09-25 20:36:54 +0000360 if (MMO.getOffset() != 0)
361 OS << "+" << MMO.getOffset();
Dan Gohmancd26ec52009-09-23 01:33:16 +0000362 OS << "]";
363
364 // Print the alignment of the reference.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000365 if (MMO.getBaseAlignment() != MMO.getAlignment() ||
366 MMO.getBaseAlignment() != MMO.getSize())
367 OS << "(align=" << MMO.getAlignment() << ")";
Dan Gohmancd26ec52009-09-23 01:33:16 +0000368
369 return OS;
370}
371
Dan Gohmance42e402008-07-07 20:32:02 +0000372//===----------------------------------------------------------------------===//
Chris Lattnerf7382302007-12-30 21:56:09 +0000373// MachineInstr Implementation
374//===----------------------------------------------------------------------===//
375
Evan Chengc0f64ff2006-11-27 23:37:22 +0000376/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
Evan Cheng67f660c2006-11-30 07:08:44 +0000377/// TID NULL and no operands.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000378MachineInstr::MachineInstr()
Dan Gohman834651c2009-11-16 22:49:38 +0000379 : TID(0), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000380 Parent(0), debugLoc(DebugLoc::getUnknownLoc()) {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000381 // Make sure that we get added to a machine basicblock
382 LeakDetector::addGarbageObject(this);
Chris Lattner72791222002-10-28 20:59:49 +0000383}
384
Evan Cheng67f660c2006-11-30 07:08:44 +0000385void MachineInstr::addImplicitDefUseOperands() {
386 if (TID->ImplicitDefs)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000387 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner8019f412007-12-30 00:41:17 +0000388 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng67f660c2006-11-30 07:08:44 +0000389 if (TID->ImplicitUses)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000390 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner8019f412007-12-30 00:41:17 +0000391 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Evan Chengd7de4962006-11-13 23:34:06 +0000392}
393
394/// MachineInstr ctor - This constructor create a MachineInstr and add the
Evan Chengc0f64ff2006-11-27 23:37:22 +0000395/// implicit operands. It reserves space for number of operands specified by
Chris Lattner749c6f62008-01-07 07:27:27 +0000396/// TargetInstrDesc or the numOperands if it is not zero. (for
Evan Chengc0f64ff2006-11-27 23:37:22 +0000397/// instructions with variable number of operands).
Chris Lattner749c6f62008-01-07 07:27:27 +0000398MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
Dan Gohman834651c2009-11-16 22:49:38 +0000399 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0),
400 MemRefs(0), MemRefsEnd(0), Parent(0),
Dale Johannesen06efc022009-01-27 23:20:29 +0000401 debugLoc(DebugLoc::getUnknownLoc()) {
Chris Lattner349c4952008-01-07 03:13:06 +0000402 if (!NoImp && TID->getImplicitDefs())
403 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000404 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000405 if (!NoImp && TID->getImplicitUses())
406 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000407 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000408 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Chengfa945722007-10-13 02:23:01 +0000409 if (!NoImp)
410 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000411 // Make sure that we get added to a machine basicblock
412 LeakDetector::addGarbageObject(this);
Evan Chengd7de4962006-11-13 23:34:06 +0000413}
414
Dale Johannesen06efc022009-01-27 23:20:29 +0000415/// MachineInstr ctor - As above, but with a DebugLoc.
416MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl,
417 bool NoImp)
Dan Gohman834651c2009-11-16 22:49:38 +0000418 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000419 Parent(0), debugLoc(dl) {
Dale Johannesen06efc022009-01-27 23:20:29 +0000420 if (!NoImp && TID->getImplicitDefs())
421 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
422 NumImplicitOps++;
423 if (!NoImp && TID->getImplicitUses())
424 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
425 NumImplicitOps++;
426 Operands.reserve(NumImplicitOps + TID->getNumOperands());
427 if (!NoImp)
428 addImplicitDefUseOperands();
429 // Make sure that we get added to a machine basicblock
430 LeakDetector::addGarbageObject(this);
431}
432
433/// MachineInstr ctor - Work exactly the same as the ctor two above, except
434/// that the MachineInstr is created and added to the end of the specified
435/// basic block.
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000436///
Dale Johannesen06efc022009-01-27 23:20:29 +0000437MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid)
Dan Gohman834651c2009-11-16 22:49:38 +0000438 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0),
439 MemRefs(0), MemRefsEnd(0), Parent(0),
Dale Johannesen06efc022009-01-27 23:20:29 +0000440 debugLoc(DebugLoc::getUnknownLoc()) {
441 assert(MBB && "Cannot use inserting ctor with null basic block!");
442 if (TID->ImplicitDefs)
443 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
444 NumImplicitOps++;
445 if (TID->ImplicitUses)
446 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
447 NumImplicitOps++;
448 Operands.reserve(NumImplicitOps + TID->getNumOperands());
449 addImplicitDefUseOperands();
450 // Make sure that we get added to a machine basicblock
451 LeakDetector::addGarbageObject(this);
452 MBB->push_back(this); // Add instruction to end of basic block!
453}
454
455/// MachineInstr ctor - As above, but with a DebugLoc.
456///
457MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl,
Chris Lattner749c6f62008-01-07 07:27:27 +0000458 const TargetInstrDesc &tid)
Dan Gohman834651c2009-11-16 22:49:38 +0000459 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000460 Parent(0), debugLoc(dl) {
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000461 assert(MBB && "Cannot use inserting ctor with null basic block!");
Evan Cheng67f660c2006-11-30 07:08:44 +0000462 if (TID->ImplicitDefs)
Chris Lattner349c4952008-01-07 03:13:06 +0000463 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000464 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +0000465 if (TID->ImplicitUses)
Chris Lattner349c4952008-01-07 03:13:06 +0000466 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000467 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000468 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Cheng67f660c2006-11-30 07:08:44 +0000469 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000470 // Make sure that we get added to a machine basicblock
471 LeakDetector::addGarbageObject(this);
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000472 MBB->push_back(this); // Add instruction to end of basic block!
473}
474
Misha Brukmance22e762004-07-09 14:45:17 +0000475/// MachineInstr ctor - Copies MachineInstr arg exactly
476///
Evan Cheng1ed99222008-07-19 00:37:25 +0000477MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Dan Gohman834651c2009-11-16 22:49:38 +0000478 : TID(&MI.getDesc()), NumImplicitOps(0), AsmPrinterFlags(0),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000479 MemRefs(MI.MemRefs), MemRefsEnd(MI.MemRefsEnd),
480 Parent(0), debugLoc(MI.getDebugLoc()) {
Chris Lattner943b5e12006-05-04 19:14:44 +0000481 Operands.reserve(MI.getNumOperands());
Tanya Lattnerb5159ed2004-05-23 20:58:02 +0000482
Misha Brukmance22e762004-07-09 14:45:17 +0000483 // Add operands
Evan Cheng1ed99222008-07-19 00:37:25 +0000484 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
485 addOperand(MI.getOperand(i));
486 NumImplicitOps = MI.NumImplicitOps;
Tanya Lattner0c63e032004-05-24 03:14:18 +0000487
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000488 // Set parent to null.
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000489 Parent = 0;
Dan Gohman6116a732008-07-21 18:47:29 +0000490
491 LeakDetector::addGarbageObject(this);
Tanya Lattner466b5342004-05-23 19:35:12 +0000492}
493
Misha Brukmance22e762004-07-09 14:45:17 +0000494MachineInstr::~MachineInstr() {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000495 LeakDetector::removeGarbageObject(this);
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000496#ifndef NDEBUG
Chris Lattner62ed6b92008-01-01 01:12:31 +0000497 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000498 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Dan Gohmand735b802008-10-03 15:45:36 +0000499 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
Chris Lattner62ed6b92008-01-01 01:12:31 +0000500 "Reg operand def/use list corrupted");
501 }
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000502#endif
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000503}
504
Chris Lattner62ed6b92008-01-01 01:12:31 +0000505/// getRegInfo - If this instruction is embedded into a MachineFunction,
506/// return the MachineRegisterInfo object for the current function, otherwise
507/// return null.
508MachineRegisterInfo *MachineInstr::getRegInfo() {
509 if (MachineBasicBlock *MBB = getParent())
Dan Gohman4e526b92008-07-08 23:59:09 +0000510 return &MBB->getParent()->getRegInfo();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000511 return 0;
512}
513
514/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
515/// this instruction from their respective use lists. This requires that the
516/// operands already be on their use lists.
517void MachineInstr::RemoveRegOperandsFromUseLists() {
518 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000519 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000520 Operands[i].RemoveRegOperandFromRegInfo();
521 }
522}
523
524/// AddRegOperandsToUseLists - Add all of the register operands in
525/// this instruction from their respective use lists. This requires that the
526/// operands not be on their use lists yet.
527void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
528 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000529 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000530 Operands[i].AddRegOperandToRegInfo(&RegInfo);
531 }
532}
533
534
535/// addOperand - Add the specified operand to the instruction. If it is an
536/// implicit operand, it is added to the end of the operand list. If it is
537/// an explicit operand it is added at the end of the explicit operand list
538/// (before the first implicit operand).
539void MachineInstr::addOperand(const MachineOperand &Op) {
Dan Gohmand735b802008-10-03 15:45:36 +0000540 bool isImpReg = Op.isReg() && Op.isImplicit();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000541 assert((isImpReg || !OperandsComplete()) &&
542 "Trying to add an operand to a machine instr that is already done!");
543
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000544 MachineRegisterInfo *RegInfo = getRegInfo();
545
Chris Lattner62ed6b92008-01-01 01:12:31 +0000546 // If we are adding the operand to the end of the list, our job is simpler.
547 // This is true most of the time, so this is a reasonable optimization.
548 if (isImpReg || NumImplicitOps == 0) {
549 // We can only do this optimization if we know that the operand list won't
550 // reallocate.
551 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
552 Operands.push_back(Op);
553
554 // Set the parent of the operand.
555 Operands.back().ParentMI = this;
556
557 // If the operand is a register, update the operand's use list.
Jim Grosbach06801722009-12-16 19:43:02 +0000558 if (Op.isReg()) {
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000559 Operands.back().AddRegOperandToRegInfo(RegInfo);
Jim Grosbach06801722009-12-16 19:43:02 +0000560 // If the register operand is flagged as early, mark the operand as such
561 unsigned OpNo = Operands.size() - 1;
562 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
563 Operands[OpNo].setIsEarlyClobber(true);
564 }
Chris Lattner62ed6b92008-01-01 01:12:31 +0000565 return;
566 }
567 }
568
569 // Otherwise, we have to insert a real operand before any implicit ones.
570 unsigned OpNo = Operands.size()-NumImplicitOps;
571
Chris Lattner62ed6b92008-01-01 01:12:31 +0000572 // If this instruction isn't embedded into a function, then we don't need to
573 // update any operand lists.
574 if (RegInfo == 0) {
575 // Simple insertion, no reginfo update needed for other register operands.
576 Operands.insert(Operands.begin()+OpNo, Op);
577 Operands[OpNo].ParentMI = this;
578
579 // Do explicitly set the reginfo for this operand though, to ensure the
580 // next/prev fields are properly nulled out.
Jim Grosbach06801722009-12-16 19:43:02 +0000581 if (Operands[OpNo].isReg()) {
Chris Lattner62ed6b92008-01-01 01:12:31 +0000582 Operands[OpNo].AddRegOperandToRegInfo(0);
Jim Grosbach06801722009-12-16 19:43:02 +0000583 // If the register operand is flagged as early, mark the operand as such
584 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
585 Operands[OpNo].setIsEarlyClobber(true);
586 }
Chris Lattner62ed6b92008-01-01 01:12:31 +0000587
588 } else if (Operands.size()+1 <= Operands.capacity()) {
589 // Otherwise, we have to remove register operands from their register use
590 // list, add the operand, then add the register operands back to their use
591 // list. This also must handle the case when the operand list reallocates
592 // to somewhere else.
593
594 // If insertion of this operand won't cause reallocation of the operand
595 // list, just remove the implicit operands, add the operand, then re-add all
596 // the rest of the operands.
597 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000598 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000599 Operands[i].RemoveRegOperandFromRegInfo();
600 }
601
602 // Add the operand. If it is a register, add it to the reg list.
603 Operands.insert(Operands.begin()+OpNo, Op);
604 Operands[OpNo].ParentMI = this;
605
Jim Grosbach06801722009-12-16 19:43:02 +0000606 if (Operands[OpNo].isReg()) {
Chris Lattner62ed6b92008-01-01 01:12:31 +0000607 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
Jim Grosbach06801722009-12-16 19:43:02 +0000608 // If the register operand is flagged as early, mark the operand as such
609 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
610 Operands[OpNo].setIsEarlyClobber(true);
611 }
Chris Lattner62ed6b92008-01-01 01:12:31 +0000612
613 // Re-add all the implicit ops.
614 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000615 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000616 Operands[i].AddRegOperandToRegInfo(RegInfo);
617 }
618 } else {
619 // Otherwise, we will be reallocating the operand list. Remove all reg
620 // operands from their list, then readd them after the operand list is
621 // reallocated.
622 RemoveRegOperandsFromUseLists();
623
624 Operands.insert(Operands.begin()+OpNo, Op);
625 Operands[OpNo].ParentMI = this;
626
627 // Re-add all the operands.
628 AddRegOperandsToUseLists(*RegInfo);
Jim Grosbach06801722009-12-16 19:43:02 +0000629
630 // If the register operand is flagged as early, mark the operand as such
631 if (Operands[OpNo].isReg()
632 && TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
633 Operands[OpNo].setIsEarlyClobber(true);
Chris Lattner62ed6b92008-01-01 01:12:31 +0000634 }
635}
636
637/// RemoveOperand - Erase an operand from an instruction, leaving it with one
638/// fewer operand than it started with.
639///
640void MachineInstr::RemoveOperand(unsigned OpNo) {
641 assert(OpNo < Operands.size() && "Invalid operand number");
642
643 // Special case removing the last one.
644 if (OpNo == Operands.size()-1) {
645 // If needed, remove from the reg def/use list.
Dan Gohmand735b802008-10-03 15:45:36 +0000646 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000647 Operands.back().RemoveRegOperandFromRegInfo();
648
649 Operands.pop_back();
650 return;
651 }
652
653 // Otherwise, we are removing an interior operand. If we have reginfo to
654 // update, remove all operands that will be shifted down from their reg lists,
655 // move everything down, then re-add them.
656 MachineRegisterInfo *RegInfo = getRegInfo();
657 if (RegInfo) {
658 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000659 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000660 Operands[i].RemoveRegOperandFromRegInfo();
661 }
662 }
663
664 Operands.erase(Operands.begin()+OpNo);
665
666 if (RegInfo) {
667 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000668 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000669 Operands[i].AddRegOperandToRegInfo(RegInfo);
670 }
671 }
672}
673
Dan Gohmanc76909a2009-09-25 20:36:54 +0000674/// addMemOperand - Add a MachineMemOperand to the machine instruction.
675/// This function should be used only occasionally. The setMemRefs function
676/// is the primary method for setting up a MachineInstr's MemRefs list.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000677void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohmanc76909a2009-09-25 20:36:54 +0000678 MachineMemOperand *MO) {
679 mmo_iterator OldMemRefs = MemRefs;
680 mmo_iterator OldMemRefsEnd = MemRefsEnd;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000681
Dan Gohmanc76909a2009-09-25 20:36:54 +0000682 size_t NewNum = (MemRefsEnd - MemRefs) + 1;
683 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
684 mmo_iterator NewMemRefsEnd = NewMemRefs + NewNum;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000685
Dan Gohmanc76909a2009-09-25 20:36:54 +0000686 std::copy(OldMemRefs, OldMemRefsEnd, NewMemRefs);
687 NewMemRefs[NewNum - 1] = MO;
688
689 MemRefs = NewMemRefs;
690 MemRefsEnd = NewMemRefsEnd;
691}
Chris Lattner62ed6b92008-01-01 01:12:31 +0000692
Chris Lattner48d7c062006-04-17 21:35:41 +0000693/// removeFromParent - This method unlinks 'this' from the containing basic
694/// block, and returns it, but does not delete it.
695MachineInstr *MachineInstr::removeFromParent() {
696 assert(getParent() && "Not embedded in a basic block!");
697 getParent()->remove(this);
698 return this;
699}
700
701
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000702/// eraseFromParent - This method unlinks 'this' from the containing basic
703/// block, and deletes it.
704void MachineInstr::eraseFromParent() {
705 assert(getParent() && "Not embedded in a basic block!");
706 getParent()->erase(this);
707}
708
709
Brian Gaeke21326fc2004-02-13 04:39:32 +0000710/// OperandComplete - Return true if it's illegal to add a new operand
711///
Chris Lattner2a90ba62004-02-12 16:09:53 +0000712bool MachineInstr::OperandsComplete() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000713 unsigned short NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000714 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000715 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000716 return false;
717}
718
Evan Cheng19e3f312007-05-15 01:26:09 +0000719/// getNumExplicitOperands - Returns the number of non-implicit operands.
720///
721unsigned MachineInstr::getNumExplicitOperands() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000722 unsigned NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000723 if (!TID->isVariadic())
Evan Cheng19e3f312007-05-15 01:26:09 +0000724 return NumOperands;
725
Dan Gohman9407cd42009-04-15 17:59:11 +0000726 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
727 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000728 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng19e3f312007-05-15 01:26:09 +0000729 NumOperands++;
730 }
731 return NumOperands;
732}
733
Chris Lattner8ace2cd2006-10-20 22:39:59 +0000734
Dan Gohman44066042008-07-01 00:05:16 +0000735/// isLabel - Returns true if the MachineInstr represents a label.
736///
737bool MachineInstr::isLabel() const {
738 return getOpcode() == TargetInstrInfo::DBG_LABEL ||
739 getOpcode() == TargetInstrInfo::EH_LABEL ||
740 getOpcode() == TargetInstrInfo::GC_LABEL;
741}
742
Evan Chengbb81d972008-01-31 09:59:15 +0000743/// isDebugLabel - Returns true if the MachineInstr represents a debug label.
744///
745bool MachineInstr::isDebugLabel() const {
Dan Gohman44066042008-07-01 00:05:16 +0000746 return getOpcode() == TargetInstrInfo::DBG_LABEL;
Evan Chengbb81d972008-01-31 09:59:15 +0000747}
748
Evan Chengfaa51072007-04-26 19:00:32 +0000749/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbachf9ca50e2009-09-17 17:57:26 +0000750/// the specific register or -1 if it is not found. It further tightens
Evan Cheng76d7e762007-02-23 01:04:26 +0000751/// the search criteria to a use that kills the register if isKill is true.
Evan Cheng6130f662008-03-05 00:59:57 +0000752int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
753 const TargetRegisterInfo *TRI) const {
Evan Cheng576d1232006-12-06 08:27:42 +0000754 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengf277ee42007-05-29 18:35:22 +0000755 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000756 if (!MO.isReg() || !MO.isUse())
Evan Cheng6130f662008-03-05 00:59:57 +0000757 continue;
758 unsigned MOReg = MO.getReg();
759 if (!MOReg)
760 continue;
761 if (MOReg == Reg ||
762 (TRI &&
763 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
764 TargetRegisterInfo::isPhysicalRegister(Reg) &&
765 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng76d7e762007-02-23 01:04:26 +0000766 if (!isKill || MO.isKill())
Evan Cheng32eb1f12007-03-26 22:37:45 +0000767 return i;
Evan Cheng576d1232006-12-06 08:27:42 +0000768 }
Evan Cheng32eb1f12007-03-26 22:37:45 +0000769 return -1;
Evan Cheng576d1232006-12-06 08:27:42 +0000770}
771
Evan Cheng6130f662008-03-05 00:59:57 +0000772/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman703bfe62008-05-06 00:20:10 +0000773/// the specified register or -1 if it is not found. If isDead is true, defs
774/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
775/// also checks if there is a def of a super-register.
Evan Cheng6130f662008-03-05 00:59:57 +0000776int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead,
777 const TargetRegisterInfo *TRI) const {
Evan Chengb371f452007-02-19 21:49:54 +0000778 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng6130f662008-03-05 00:59:57 +0000779 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000780 if (!MO.isReg() || !MO.isDef())
Evan Cheng6130f662008-03-05 00:59:57 +0000781 continue;
782 unsigned MOReg = MO.getReg();
783 if (MOReg == Reg ||
784 (TRI &&
785 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
786 TargetRegisterInfo::isPhysicalRegister(Reg) &&
787 TRI->isSubRegister(MOReg, Reg)))
788 if (!isDead || MO.isDead())
789 return i;
Evan Chengb371f452007-02-19 21:49:54 +0000790 }
Evan Cheng6130f662008-03-05 00:59:57 +0000791 return -1;
Evan Chengb371f452007-02-19 21:49:54 +0000792}
Evan Cheng19e3f312007-05-15 01:26:09 +0000793
Evan Chengf277ee42007-05-29 18:35:22 +0000794/// findFirstPredOperandIdx() - Find the index of the first operand in the
795/// operand list that is used to represent the predicate. It returns -1 if
796/// none is found.
797int MachineInstr::findFirstPredOperandIdx() const {
Chris Lattner749c6f62008-01-07 07:27:27 +0000798 const TargetInstrDesc &TID = getDesc();
799 if (TID.isPredicable()) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000800 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner749c6f62008-01-07 07:27:27 +0000801 if (TID.OpInfo[i].isPredicate())
Evan Chengf277ee42007-05-29 18:35:22 +0000802 return i;
Evan Cheng19e3f312007-05-15 01:26:09 +0000803 }
804
Evan Chengf277ee42007-05-29 18:35:22 +0000805 return -1;
Evan Cheng19e3f312007-05-15 01:26:09 +0000806}
Evan Chengb371f452007-02-19 21:49:54 +0000807
Bob Wilsond9df5012009-04-09 17:16:43 +0000808/// isRegTiedToUseOperand - Given the index of a register def operand,
809/// check if the register def is tied to a source operand, due to either
810/// two-address elimination or inline assembly constraints. Returns the
811/// first tied use operand index by reference is UseOpIdx is not null.
Jakob Stoklund Olesence9be2c2009-04-29 20:57:16 +0000812bool MachineInstr::
813isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const {
Evan Chengfb112882009-03-23 08:01:15 +0000814 if (getOpcode() == TargetInstrInfo::INLINEASM) {
Bob Wilsond9df5012009-04-09 17:16:43 +0000815 assert(DefOpIdx >= 2);
816 const MachineOperand &MO = getOperand(DefOpIdx);
Chris Lattnerc30aa7b2009-04-09 23:33:34 +0000817 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
Evan Chengfb112882009-03-23 08:01:15 +0000818 return false;
Evan Chengef5d0702009-06-24 02:05:51 +0000819 // Determine the actual operand index that corresponds to this index.
Evan Chengfb112882009-03-23 08:01:15 +0000820 unsigned DefNo = 0;
Evan Chengef5d0702009-06-24 02:05:51 +0000821 unsigned DefPart = 0;
Evan Chengfb112882009-03-23 08:01:15 +0000822 for (unsigned i = 1, e = getNumOperands(); i < e; ) {
823 const MachineOperand &FMO = getOperand(i);
Jakob Stoklund Olesen45d34fe2009-07-19 19:09:59 +0000824 // After the normal asm operands there may be additional imp-def regs.
825 if (!FMO.isImm())
826 return false;
Evan Chengfb112882009-03-23 08:01:15 +0000827 // Skip over this def.
Evan Chengef5d0702009-06-24 02:05:51 +0000828 unsigned NumOps = InlineAsm::getNumOperandRegisters(FMO.getImm());
829 unsigned PrevDef = i + 1;
830 i = PrevDef + NumOps;
831 if (i > DefOpIdx) {
832 DefPart = DefOpIdx - PrevDef;
Evan Chengfb112882009-03-23 08:01:15 +0000833 break;
Evan Chengef5d0702009-06-24 02:05:51 +0000834 }
Evan Chengfb112882009-03-23 08:01:15 +0000835 ++DefNo;
836 }
Evan Chengef5d0702009-06-24 02:05:51 +0000837 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Evan Chengfb112882009-03-23 08:01:15 +0000838 const MachineOperand &FMO = getOperand(i);
839 if (!FMO.isImm())
840 continue;
841 if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse())
842 continue;
843 unsigned Idx;
Evan Chengef5d0702009-06-24 02:05:51 +0000844 if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000845 Idx == DefNo) {
846 if (UseOpIdx)
Evan Chengef5d0702009-06-24 02:05:51 +0000847 *UseOpIdx = (unsigned)i + 1 + DefPart;
Evan Chengfb112882009-03-23 08:01:15 +0000848 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000849 }
Evan Chengfb112882009-03-23 08:01:15 +0000850 }
Evan Chengef5d0702009-06-24 02:05:51 +0000851 return false;
Evan Chengfb112882009-03-23 08:01:15 +0000852 }
853
Bob Wilsond9df5012009-04-09 17:16:43 +0000854 assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!");
Chris Lattner749c6f62008-01-07 07:27:27 +0000855 const TargetInstrDesc &TID = getDesc();
Evan Chengef0732d2008-07-10 07:35:43 +0000856 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
857 const MachineOperand &MO = getOperand(i);
Dan Gohman2ce7f202008-12-05 05:45:42 +0000858 if (MO.isReg() && MO.isUse() &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000859 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) {
860 if (UseOpIdx)
861 *UseOpIdx = (unsigned)i;
Evan Chengef0732d2008-07-10 07:35:43 +0000862 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000863 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000864 }
865 return false;
866}
867
Evan Chenga24752f2009-03-19 20:30:06 +0000868/// isRegTiedToDefOperand - Return true if the operand of the specified index
869/// is a register use and it is tied to an def operand. It also returns the def
870/// operand index by reference.
Jakob Stoklund Olesence9be2c2009-04-29 20:57:16 +0000871bool MachineInstr::
872isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const {
Evan Chengfb112882009-03-23 08:01:15 +0000873 if (getOpcode() == TargetInstrInfo::INLINEASM) {
874 const MachineOperand &MO = getOperand(UseOpIdx);
Chris Lattner0c8382c2009-04-09 16:50:43 +0000875 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0)
Evan Chengfb112882009-03-23 08:01:15 +0000876 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000877
878 // Find the flag operand corresponding to UseOpIdx
879 unsigned FlagIdx, NumOps=0;
880 for (FlagIdx = 1; FlagIdx < UseOpIdx; FlagIdx += NumOps+1) {
881 const MachineOperand &UFMO = getOperand(FlagIdx);
Jakob Stoklund Olesen45d34fe2009-07-19 19:09:59 +0000882 // After the normal asm operands there may be additional imp-def regs.
883 if (!UFMO.isImm())
884 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000885 NumOps = InlineAsm::getNumOperandRegisters(UFMO.getImm());
886 assert(NumOps < getNumOperands() && "Invalid inline asm flag");
887 if (UseOpIdx < FlagIdx+NumOps+1)
888 break;
Evan Chengef5d0702009-06-24 02:05:51 +0000889 }
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000890 if (FlagIdx >= UseOpIdx)
Evan Chengef5d0702009-06-24 02:05:51 +0000891 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000892 const MachineOperand &UFMO = getOperand(FlagIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000893 unsigned DefNo;
894 if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) {
895 if (!DefOpIdx)
896 return true;
897
898 unsigned DefIdx = 1;
899 // Remember to adjust the index. First operand is asm string, then there
900 // is a flag for each.
901 while (DefNo) {
902 const MachineOperand &FMO = getOperand(DefIdx);
903 assert(FMO.isImm());
904 // Skip over this def.
905 DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
906 --DefNo;
907 }
Evan Chengef5d0702009-06-24 02:05:51 +0000908 *DefOpIdx = DefIdx + UseOpIdx - FlagIdx;
Evan Chengfb112882009-03-23 08:01:15 +0000909 return true;
910 }
911 return false;
912 }
913
Evan Chenga24752f2009-03-19 20:30:06 +0000914 const TargetInstrDesc &TID = getDesc();
915 if (UseOpIdx >= TID.getNumOperands())
916 return false;
917 const MachineOperand &MO = getOperand(UseOpIdx);
918 if (!MO.isReg() || !MO.isUse())
919 return false;
920 int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO);
921 if (DefIdx == -1)
922 return false;
923 if (DefOpIdx)
924 *DefOpIdx = (unsigned)DefIdx;
925 return true;
926}
927
Evan Cheng576d1232006-12-06 08:27:42 +0000928/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
929///
930void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
931 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
932 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000933 if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
Evan Cheng576d1232006-12-06 08:27:42 +0000934 continue;
935 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
936 MachineOperand &MOp = getOperand(j);
937 if (!MOp.isIdenticalTo(MO))
938 continue;
939 if (MO.isKill())
940 MOp.setIsKill();
941 else
942 MOp.setIsDead();
943 break;
944 }
945 }
946}
947
Evan Cheng19e3f312007-05-15 01:26:09 +0000948/// copyPredicates - Copies predicate operand(s) from MI.
949void MachineInstr::copyPredicates(const MachineInstr *MI) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000950 const TargetInstrDesc &TID = MI->getDesc();
Evan Chengb27087f2008-03-13 00:44:09 +0000951 if (!TID.isPredicable())
952 return;
953 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
954 if (TID.OpInfo[i].isPredicate()) {
955 // Predicated operands must be last operands.
956 addOperand(MI->getOperand(i));
Evan Cheng19e3f312007-05-15 01:26:09 +0000957 }
958 }
959}
960
Evan Cheng9f1c8312008-07-03 09:09:37 +0000961/// isSafeToMove - Return true if it is safe to move this instruction. If
962/// SawStore is set to true, it means that there is a store (or call) between
963/// the instruction's location and its intended destination.
Dan Gohmanb3b930a2008-11-18 19:04:29 +0000964bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
Dan Gohmana70dca12009-10-09 23:27:56 +0000965 bool &SawStore,
966 AliasAnalysis *AA) const {
Evan Chengb27087f2008-03-13 00:44:09 +0000967 // Ignore stuff that we obviously can't move.
968 if (TID->mayStore() || TID->isCall()) {
969 SawStore = true;
970 return false;
971 }
Dan Gohman237dee12008-12-23 17:28:50 +0000972 if (TID->isTerminator() || TID->hasUnmodeledSideEffects())
Evan Chengb27087f2008-03-13 00:44:09 +0000973 return false;
974
975 // See if this instruction does a load. If so, we have to guarantee that the
976 // loaded value doesn't change between the load and the its intended
977 // destination. The check for isInvariantLoad gives the targe the chance to
978 // classify the load as always returning a constant, e.g. a constant pool
979 // load.
Dan Gohmana70dca12009-10-09 23:27:56 +0000980 if (TID->mayLoad() && !isInvariantLoad(AA))
Evan Chengb27087f2008-03-13 00:44:09 +0000981 // Otherwise, this is a real load. If there is a store between the load and
Evan Cheng7cc2c402009-07-28 21:49:18 +0000982 // end of block, or if the load is volatile, we can't move it.
Dan Gohmand790a5c2008-10-02 15:04:30 +0000983 return !SawStore && !hasVolatileMemoryRef();
Dan Gohman3e4fb702008-09-24 00:06:15 +0000984
Evan Chengb27087f2008-03-13 00:44:09 +0000985 return true;
986}
987
Evan Chengdf3b9932008-08-27 20:33:50 +0000988/// isSafeToReMat - Return true if it's safe to rematerialize the specified
989/// instruction which defined the specified register instead of copying it.
Dan Gohmanb3b930a2008-11-18 19:04:29 +0000990bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII,
Dan Gohmana70dca12009-10-09 23:27:56 +0000991 unsigned DstReg,
992 AliasAnalysis *AA) const {
Evan Chengdf3b9932008-08-27 20:33:50 +0000993 bool SawStore = false;
Dan Gohmana70dca12009-10-09 23:27:56 +0000994 if (!TII->isTriviallyReMaterializable(this, AA) ||
995 !isSafeToMove(TII, SawStore, AA))
Evan Chengdf3b9932008-08-27 20:33:50 +0000996 return false;
997 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohmancbad42c2008-11-18 19:49:32 +0000998 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000999 if (!MO.isReg())
Evan Chengdf3b9932008-08-27 20:33:50 +00001000 continue;
1001 // FIXME: For now, do not remat any instruction with register operands.
1002 // Later on, we can loosen the restriction is the register operands have
1003 // not been modified between the def and use. Note, this is different from
Evan Cheng8763c1c2008-08-27 20:58:54 +00001004 // MachineSink because the code is no longer in two-address form (at least
Evan Chengdf3b9932008-08-27 20:33:50 +00001005 // partially).
1006 if (MO.isUse())
1007 return false;
1008 else if (!MO.isDead() && MO.getReg() != DstReg)
1009 return false;
1010 }
1011 return true;
1012}
1013
Dan Gohman3e4fb702008-09-24 00:06:15 +00001014/// hasVolatileMemoryRef - Return true if this instruction may have a
1015/// volatile memory reference, or if the information describing the
1016/// memory reference is not available. Return false if it is known to
1017/// have no volatile memory references.
1018bool MachineInstr::hasVolatileMemoryRef() const {
1019 // An instruction known never to access memory won't have a volatile access.
1020 if (!TID->mayStore() &&
1021 !TID->mayLoad() &&
1022 !TID->isCall() &&
1023 !TID->hasUnmodeledSideEffects())
1024 return false;
1025
1026 // Otherwise, if the instruction has no memory reference information,
1027 // conservatively assume it wasn't preserved.
1028 if (memoperands_empty())
1029 return true;
1030
1031 // Check the memory reference information for volatile references.
Dan Gohmanc76909a2009-09-25 20:36:54 +00001032 for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I)
1033 if ((*I)->isVolatile())
Dan Gohman3e4fb702008-09-24 00:06:15 +00001034 return true;
1035
1036 return false;
1037}
1038
Dan Gohmane33f44c2009-10-07 17:38:06 +00001039/// isInvariantLoad - Return true if this instruction is loading from a
1040/// location whose value is invariant across the function. For example,
1041/// loading a value from the constant pool or from from the argument area
1042/// of a function if it does not change. This should only return true of
1043/// *all* loads the instruction does are invariant (if it does multiple loads).
1044bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const {
1045 // If the instruction doesn't load at all, it isn't an invariant load.
1046 if (!TID->mayLoad())
1047 return false;
1048
1049 // If the instruction has lost its memoperands, conservatively assume that
1050 // it may not be an invariant load.
1051 if (memoperands_empty())
1052 return false;
1053
1054 const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo();
1055
1056 for (mmo_iterator I = memoperands_begin(),
1057 E = memoperands_end(); I != E; ++I) {
1058 if ((*I)->isVolatile()) return false;
1059 if ((*I)->isStore()) return false;
1060
1061 if (const Value *V = (*I)->getValue()) {
1062 // A load from a constant PseudoSourceValue is invariant.
1063 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V))
1064 if (PSV->isConstant(MFI))
1065 continue;
1066 // If we have an AliasAnalysis, ask it whether the memory is constant.
1067 if (AA && AA->pointsToConstantMemory(V))
1068 continue;
1069 }
1070
1071 // Otherwise assume conservatively.
1072 return false;
1073 }
1074
1075 // Everything checks out.
1076 return true;
1077}
1078
Evan Cheng229694f2009-12-03 02:31:43 +00001079/// isConstantValuePHI - If the specified instruction is a PHI that always
1080/// merges together the same virtual register, return the register, otherwise
1081/// return 0.
1082unsigned MachineInstr::isConstantValuePHI() const {
1083 if (getOpcode() != TargetInstrInfo::PHI)
1084 return 0;
Evan Chengd8f079c2009-12-07 23:10:34 +00001085 assert(getNumOperands() >= 3 &&
1086 "It's illegal to have a PHI without source operands");
Evan Cheng229694f2009-12-03 02:31:43 +00001087
1088 unsigned Reg = getOperand(1).getReg();
1089 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
1090 if (getOperand(i).getReg() != Reg)
1091 return 0;
1092 return Reg;
1093}
1094
Brian Gaeke21326fc2004-02-13 04:39:32 +00001095void MachineInstr::dump() const {
Chris Lattner705e07f2009-08-23 03:41:05 +00001096 errs() << " " << *this;
Mon P Wang5ca6bd12008-10-10 01:43:55 +00001097}
1098
1099void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const {
Dan Gohman80f6c582009-11-09 19:38:45 +00001100 // We can be a bit tidier if we know the TargetMachine and/or MachineFunction.
1101 const MachineFunction *MF = 0;
1102 if (const MachineBasicBlock *MBB = getParent()) {
1103 MF = MBB->getParent();
1104 if (!TM && MF)
1105 TM = &MF->getTarget();
1106 }
Dan Gohman0ba90f32009-10-31 20:19:03 +00001107
1108 // Print explicitly defined operands on the left of an assignment syntax.
Dan Gohman80f6c582009-11-09 19:38:45 +00001109 unsigned StartOp = 0, e = getNumOperands();
Dan Gohman0ba90f32009-10-31 20:19:03 +00001110 for (; StartOp < e && getOperand(StartOp).isReg() &&
1111 getOperand(StartOp).isDef() &&
1112 !getOperand(StartOp).isImplicit();
1113 ++StartOp) {
1114 if (StartOp != 0) OS << ", ";
1115 getOperand(StartOp).print(OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +00001116 }
Tanya Lattnerb1407622004-06-25 00:13:11 +00001117
Dan Gohman0ba90f32009-10-31 20:19:03 +00001118 if (StartOp != 0)
1119 OS << " = ";
1120
1121 // Print the opcode name.
Chris Lattner749c6f62008-01-07 07:27:27 +00001122 OS << getDesc().getName();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001123
Dan Gohman0ba90f32009-10-31 20:19:03 +00001124 // Print the rest of the operands.
Dan Gohman80f6c582009-11-09 19:38:45 +00001125 bool OmittedAnyCallClobbers = false;
1126 bool FirstOp = true;
Chris Lattner6a592272002-10-30 01:55:38 +00001127 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohman80f6c582009-11-09 19:38:45 +00001128 const MachineOperand &MO = getOperand(i);
1129
1130 // Omit call-clobbered registers which aren't used anywhere. This makes
1131 // call instructions much less noisy on targets where calls clobber lots
1132 // of registers. Don't rely on MO.isDead() because we may be called before
1133 // LiveVariables is run, or we may be looking at a non-allocatable reg.
1134 if (MF && getDesc().isCall() &&
1135 MO.isReg() && MO.isImplicit() && MO.isDef()) {
1136 unsigned Reg = MO.getReg();
1137 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) {
1138 const MachineRegisterInfo &MRI = MF->getRegInfo();
1139 if (MRI.use_empty(Reg) && !MRI.isLiveOut(Reg)) {
1140 bool HasAliasLive = false;
1141 for (const unsigned *Alias = TM->getRegisterInfo()->getAliasSet(Reg);
1142 unsigned AliasReg = *Alias; ++Alias)
1143 if (!MRI.use_empty(AliasReg) || MRI.isLiveOut(AliasReg)) {
1144 HasAliasLive = true;
1145 break;
1146 }
1147 if (!HasAliasLive) {
1148 OmittedAnyCallClobbers = true;
1149 continue;
1150 }
1151 }
1152 }
1153 }
1154
1155 if (FirstOp) FirstOp = false; else OS << ",";
Chris Lattner6a592272002-10-30 01:55:38 +00001156 OS << " ";
Dan Gohman80f6c582009-11-09 19:38:45 +00001157 MO.print(OS, TM);
1158 }
1159
1160 // Briefly indicate whether any call clobbers were omitted.
1161 if (OmittedAnyCallClobbers) {
Bill Wendling164558e2009-12-25 13:45:50 +00001162 if (!FirstOp) OS << ",";
Dan Gohman80f6c582009-11-09 19:38:45 +00001163 OS << " ...";
Chris Lattner10491642002-10-30 00:48:05 +00001164 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001165
Dan Gohman0ba90f32009-10-31 20:19:03 +00001166 bool HaveSemi = false;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001167 if (!memoperands_empty()) {
Dan Gohman0ba90f32009-10-31 20:19:03 +00001168 if (!HaveSemi) OS << ";"; HaveSemi = true;
1169
1170 OS << " mem:";
Dan Gohmanc76909a2009-09-25 20:36:54 +00001171 for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
1172 i != e; ++i) {
1173 OS << **i;
Dan Gohmancd26ec52009-09-23 01:33:16 +00001174 if (next(i) != e)
1175 OS << " ";
Dan Gohman69de1932008-02-06 22:27:42 +00001176 }
1177 }
1178
Dan Gohman80f6c582009-11-09 19:38:45 +00001179 if (!debugLoc.isUnknown() && MF) {
Bill Wendlingad2cf9d2009-12-25 13:44:36 +00001180 if (!HaveSemi) OS << ";";
Dan Gohman0ba90f32009-10-31 20:19:03 +00001181
1182 // TODO: print InlinedAtLoc information
1183
Bill Wendlingb5ef2732009-02-19 21:44:55 +00001184 DebugLocTuple DLT = MF->getDebugLocTuple(debugLoc);
Dan Gohman261a7d92009-12-01 00:45:56 +00001185 DIScope Scope(DLT.Scope);
Dan Gohman75ae5932009-11-23 21:29:08 +00001186 OS << " dbg:";
Dan Gohman4b808b02009-12-05 00:20:51 +00001187 // Omit the directory, since it's usually long and uninteresting.
Dan Gohman261a7d92009-12-01 00:45:56 +00001188 if (!Scope.isNull())
Dan Gohman4b808b02009-12-05 00:20:51 +00001189 OS << Scope.getFilename();
1190 else
1191 OS << "<unknown>";
1192 OS << ':' << DLT.Line;
1193 if (DLT.Col != 0)
1194 OS << ':' << DLT.Col;
Bill Wendlingb5ef2732009-02-19 21:44:55 +00001195 }
1196
Chris Lattner10491642002-10-30 00:48:05 +00001197 OS << "\n";
1198}
1199
Owen Andersonb487e722008-01-24 01:10:07 +00001200bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001201 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001202 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001203 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001204 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +00001205 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001206 SmallVector<unsigned,4> DeadOps;
Bill Wendling4a23d722008-03-03 22:14:33 +00001207 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1208 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenefb8e3e2009-08-04 20:09:25 +00001209 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001210 continue;
1211 unsigned Reg = MO.getReg();
1212 if (!Reg)
1213 continue;
Bill Wendling4a23d722008-03-03 22:14:33 +00001214
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001215 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001216 if (!Found) {
1217 if (MO.isKill())
1218 // The register is already marked kill.
1219 return true;
Jakob Stoklund Olesenece48182009-08-02 19:13:03 +00001220 if (isPhysReg && isRegTiedToDefOperand(i))
1221 // Two-address uses of physregs must not be marked kill.
1222 return true;
Dan Gohman3f629402008-09-03 15:56:16 +00001223 MO.setIsKill();
1224 Found = true;
1225 }
1226 } else if (hasAliases && MO.isKill() &&
1227 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001228 // A super-register kill already exists.
1229 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001230 return true;
1231 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001232 DeadOps.push_back(i);
Bill Wendling4a23d722008-03-03 22:14:33 +00001233 }
1234 }
1235
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001236 // Trim unneeded kill operands.
1237 while (!DeadOps.empty()) {
1238 unsigned OpIdx = DeadOps.back();
1239 if (getOperand(OpIdx).isImplicit())
1240 RemoveOperand(OpIdx);
1241 else
1242 getOperand(OpIdx).setIsKill(false);
1243 DeadOps.pop_back();
1244 }
1245
Bill Wendling4a23d722008-03-03 22:14:33 +00001246 // If not found, this means an alias of one of the operands is killed. Add a
Owen Andersonb487e722008-01-24 01:10:07 +00001247 // new implicit operand if required.
Dan Gohman3f629402008-09-03 15:56:16 +00001248 if (!Found && AddIfNotFound) {
Bill Wendling4a23d722008-03-03 22:14:33 +00001249 addOperand(MachineOperand::CreateReg(IncomingReg,
1250 false /*IsDef*/,
1251 true /*IsImp*/,
1252 true /*IsKill*/));
Owen Andersonb487e722008-01-24 01:10:07 +00001253 return true;
1254 }
Dan Gohman3f629402008-09-03 15:56:16 +00001255 return Found;
Owen Andersonb487e722008-01-24 01:10:07 +00001256}
1257
1258bool MachineInstr::addRegisterDead(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001259 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001260 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001261 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Evan Cheng01b2e232008-06-27 22:11:49 +00001262 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +00001263 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001264 SmallVector<unsigned,4> DeadOps;
Owen Andersonb487e722008-01-24 01:10:07 +00001265 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1266 MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001267 if (!MO.isReg() || !MO.isDef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001268 continue;
1269 unsigned Reg = MO.getReg();
Dan Gohman3f629402008-09-03 15:56:16 +00001270 if (!Reg)
1271 continue;
1272
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001273 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001274 if (!Found) {
1275 if (MO.isDead())
1276 // The register is already marked dead.
1277 return true;
1278 MO.setIsDead();
1279 Found = true;
1280 }
1281 } else if (hasAliases && MO.isDead() &&
1282 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001283 // There exists a super-register that's marked dead.
1284 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001285 return true;
Owen Anderson22ae9992008-08-14 18:34:18 +00001286 if (RegInfo->getSubRegisters(IncomingReg) &&
1287 RegInfo->getSuperRegisters(Reg) &&
1288 RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001289 DeadOps.push_back(i);
Owen Andersonb487e722008-01-24 01:10:07 +00001290 }
1291 }
1292
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001293 // Trim unneeded dead operands.
1294 while (!DeadOps.empty()) {
1295 unsigned OpIdx = DeadOps.back();
1296 if (getOperand(OpIdx).isImplicit())
1297 RemoveOperand(OpIdx);
1298 else
1299 getOperand(OpIdx).setIsDead(false);
1300 DeadOps.pop_back();
1301 }
1302
Dan Gohman3f629402008-09-03 15:56:16 +00001303 // If not found, this means an alias of one of the operands is dead. Add a
1304 // new implicit operand if required.
Chris Lattner31530612009-06-24 17:54:48 +00001305 if (Found || !AddIfNotFound)
1306 return Found;
1307
1308 addOperand(MachineOperand::CreateReg(IncomingReg,
1309 true /*IsDef*/,
1310 true /*IsImp*/,
1311 false /*IsKill*/,
1312 true /*IsDead*/));
1313 return true;
Owen Andersonb487e722008-01-24 01:10:07 +00001314}