blob: 5f1bfff1c0c9942dc1701fb4d423597554e02b1b [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 Lattner5e9cd432009-12-28 08:30:43 +000018#include "llvm/Type.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000019#include "llvm/Value.h"
Dan Gohmancd26ec52009-09-23 01:33:16 +000020#include "llvm/Assembly/Writer.h"
Chris Lattner8517e1f2004-02-19 16:17:08 +000021#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000022#include "llvm/CodeGen/MachineMemOperand.h"
Chris Lattner62ed6b92008-01-01 01:12:31 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000024#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner10491642002-10-30 00:48:05 +000025#include "llvm/Target/TargetMachine.h"
Evan Chengbb81d972008-01-31 09:59:15 +000026#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerf14cf852008-01-07 07:42:25 +000027#include "llvm/Target/TargetInstrDesc.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmane33f44c2009-10-07 17:38:06 +000029#include "llvm/Analysis/AliasAnalysis.h"
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +000030#include "llvm/Analysis/DebugInfo.h"
David Greene3b325332010-01-04 23:48:20 +000031#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000032#include "llvm/Support/ErrorHandling.h"
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000033#include "llvm/Support/LeakDetector.h"
Dan Gohmance42e402008-07-07 20:32:02 +000034#include "llvm/Support/MathExtras.h"
Chris Lattneredfb72c2008-08-24 20:37:32 +000035#include "llvm/Support/raw_ostream.h"
Dan Gohmanb8d2f552008-08-20 15:58:01 +000036#include "llvm/ADT/FoldingSet.h"
Dale Johannesen5f72a5e2010-01-13 00:00:24 +000037#include "llvm/Metadata.h"
Chris Lattner0742b592004-02-23 18:38:20 +000038using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000039
Chris Lattnerf7382302007-12-30 21:56:09 +000040//===----------------------------------------------------------------------===//
41// MachineOperand Implementation
42//===----------------------------------------------------------------------===//
43
Chris Lattner62ed6b92008-01-01 01:12:31 +000044/// AddRegOperandToRegInfo - Add this register operand to the specified
45/// MachineRegisterInfo. If it is null, then the next/prev fields should be
46/// explicitly nulled out.
47void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) {
Dan Gohmand735b802008-10-03 15:45:36 +000048 assert(isReg() && "Can only add reg operand to use lists");
Chris Lattner62ed6b92008-01-01 01:12:31 +000049
50 // If the reginfo pointer is null, just explicitly null out or next/prev
51 // pointers, to ensure they are not garbage.
52 if (RegInfo == 0) {
53 Contents.Reg.Prev = 0;
54 Contents.Reg.Next = 0;
55 return;
56 }
57
58 // Otherwise, add this operand to the head of the registers use/def list.
Chris Lattner80fe5312008-01-01 21:08:22 +000059 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
Chris Lattner62ed6b92008-01-01 01:12:31 +000060
Chris Lattner80fe5312008-01-01 21:08:22 +000061 // For SSA values, we prefer to keep the definition at the start of the list.
62 // we do this by skipping over the definition if it is at the head of the
63 // list.
64 if (*Head && (*Head)->isDef())
65 Head = &(*Head)->Contents.Reg.Next;
66
67 Contents.Reg.Next = *Head;
Chris Lattner62ed6b92008-01-01 01:12:31 +000068 if (Contents.Reg.Next) {
69 assert(getReg() == Contents.Reg.Next->getReg() &&
70 "Different regs on the same list!");
71 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
72 }
73
Chris Lattner80fe5312008-01-01 21:08:22 +000074 Contents.Reg.Prev = Head;
75 *Head = this;
Chris Lattner62ed6b92008-01-01 01:12:31 +000076}
77
Dan Gohman3bc1a372009-04-15 01:17:37 +000078/// RemoveRegOperandFromRegInfo - Remove this register operand from the
79/// MachineRegisterInfo it is linked with.
80void MachineOperand::RemoveRegOperandFromRegInfo() {
81 assert(isOnRegUseList() && "Reg operand is not on a use list");
82 // Unlink this from the doubly linked list of operands.
83 MachineOperand *NextOp = Contents.Reg.Next;
84 *Contents.Reg.Prev = NextOp;
85 if (NextOp) {
86 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
87 NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
88 }
89 Contents.Reg.Prev = 0;
90 Contents.Reg.Next = 0;
91}
92
Chris Lattner62ed6b92008-01-01 01:12:31 +000093void MachineOperand::setReg(unsigned Reg) {
94 if (getReg() == Reg) return; // No change.
95
96 // Otherwise, we have to change the register. If this operand is embedded
97 // into a machine function, we need to update the old and new register's
98 // use/def lists.
99 if (MachineInstr *MI = getParent())
100 if (MachineBasicBlock *MBB = MI->getParent())
101 if (MachineFunction *MF = MBB->getParent()) {
102 RemoveRegOperandFromRegInfo();
103 Contents.Reg.RegNo = Reg;
104 AddRegOperandToRegInfo(&MF->getRegInfo());
105 return;
106 }
107
108 // Otherwise, just change the register, no problem. :)
109 Contents.Reg.RegNo = Reg;
110}
111
112/// ChangeToImmediate - Replace this operand with a new immediate operand of
113/// the specified value. If an operand is known to be an immediate already,
114/// the setImm method should be used.
115void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
116 // If this operand is currently a register operand, and if this is in a
117 // function, deregister the operand from the register's use/def list.
Dan Gohmand735b802008-10-03 15:45:36 +0000118 if (isReg() && getParent() && getParent()->getParent() &&
Chris Lattner62ed6b92008-01-01 01:12:31 +0000119 getParent()->getParent()->getParent())
120 RemoveRegOperandFromRegInfo();
121
122 OpKind = MO_Immediate;
123 Contents.ImmVal = ImmVal;
124}
125
126/// ChangeToRegister - Replace this operand with a new register operand of
127/// the specified value. If an operand is known to be an register already,
128/// the setReg method should be used.
129void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Dale Johannesen9653f9e2010-02-10 00:41:49 +0000130 bool isKill, bool isDead, bool isUndef,
131 bool isDebug) {
Chris Lattner62ed6b92008-01-01 01:12:31 +0000132 // If this operand is already a register operand, use setReg to update the
133 // register's use/def lists.
Dan Gohmand735b802008-10-03 15:45:36 +0000134 if (isReg()) {
Dale Johannesene0091802008-09-14 01:44:36 +0000135 assert(!isEarlyClobber());
Chris Lattner62ed6b92008-01-01 01:12:31 +0000136 setReg(Reg);
137 } else {
138 // Otherwise, change this to a register and set the reg#.
139 OpKind = MO_Register;
140 Contents.Reg.RegNo = Reg;
141
142 // If this operand is embedded in a function, add the operand to the
143 // register's use/def list.
144 if (MachineInstr *MI = getParent())
145 if (MachineBasicBlock *MBB = MI->getParent())
146 if (MachineFunction *MF = MBB->getParent())
147 AddRegOperandToRegInfo(&MF->getRegInfo());
148 }
149
150 IsDef = isDef;
151 IsImp = isImp;
152 IsKill = isKill;
153 IsDead = isDead;
Evan Cheng4784f1f2009-06-30 08:49:04 +0000154 IsUndef = isUndef;
Dale Johannesene0091802008-09-14 01:44:36 +0000155 IsEarlyClobber = false;
Dale Johannesen9653f9e2010-02-10 00:41:49 +0000156 IsDebug = isDebug;
Chris Lattner62ed6b92008-01-01 01:12:31 +0000157 SubReg = 0;
158}
159
Chris Lattnerf7382302007-12-30 21:56:09 +0000160/// isIdenticalTo - Return true if this operand is identical to the specified
161/// operand.
162bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
Chris Lattner31530612009-06-24 17:54:48 +0000163 if (getType() != Other.getType() ||
164 getTargetFlags() != Other.getTargetFlags())
165 return false;
Chris Lattnerf7382302007-12-30 21:56:09 +0000166
167 switch (getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000168 default: llvm_unreachable("Unrecognized operand type");
Chris Lattnerf7382302007-12-30 21:56:09 +0000169 case MachineOperand::MO_Register:
170 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
171 getSubReg() == Other.getSubReg();
172 case MachineOperand::MO_Immediate:
173 return getImm() == Other.getImm();
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000174 case MachineOperand::MO_FPImmediate:
175 return getFPImm() == Other.getFPImm();
Chris Lattnerf7382302007-12-30 21:56:09 +0000176 case MachineOperand::MO_MachineBasicBlock:
177 return getMBB() == Other.getMBB();
178 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000179 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000180 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000181 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattnerf7382302007-12-30 21:56:09 +0000182 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000183 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000184 case MachineOperand::MO_GlobalAddress:
185 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
186 case MachineOperand::MO_ExternalSymbol:
187 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
188 getOffset() == Other.getOffset();
Dan Gohman8c2b5252009-10-30 01:27:03 +0000189 case MachineOperand::MO_BlockAddress:
190 return getBlockAddress() == Other.getBlockAddress();
Chris Lattnerf7382302007-12-30 21:56:09 +0000191 }
192}
193
194/// print - Print the specified machine operand.
195///
Mon P Wang5ca6bd12008-10-10 01:43:55 +0000196void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
Dan Gohman80f6c582009-11-09 19:38:45 +0000197 // If the instruction is embedded into a basic block, we can find the
198 // target info for the instruction.
199 if (!TM)
200 if (const MachineInstr *MI = getParent())
201 if (const MachineBasicBlock *MBB = MI->getParent())
202 if (const MachineFunction *MF = MBB->getParent())
203 TM = &MF->getTarget();
204
Chris Lattnerf7382302007-12-30 21:56:09 +0000205 switch (getType()) {
206 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000207 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000208 OS << "%reg" << getReg();
209 } else {
Chris Lattnerf7382302007-12-30 21:56:09 +0000210 if (TM)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000211 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
Chris Lattnerf7382302007-12-30 21:56:09 +0000212 else
Dan Gohman0ba90f32009-10-31 20:19:03 +0000213 OS << "%physreg" << getReg();
Chris Lattnerf7382302007-12-30 21:56:09 +0000214 }
Dan Gohman2ccc8392008-12-18 21:51:27 +0000215
Evan Cheng4784f1f2009-06-30 08:49:04 +0000216 if (getSubReg() != 0)
Chris Lattner31530612009-06-24 17:54:48 +0000217 OS << ':' << getSubReg();
Dan Gohman2ccc8392008-12-18 21:51:27 +0000218
Evan Cheng4784f1f2009-06-30 08:49:04 +0000219 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
220 isEarlyClobber()) {
Chris Lattner31530612009-06-24 17:54:48 +0000221 OS << '<';
Chris Lattnerf7382302007-12-30 21:56:09 +0000222 bool NeedComma = false;
Evan Cheng07897072009-10-14 23:37:31 +0000223 if (isDef()) {
Chris Lattner31530612009-06-24 17:54:48 +0000224 if (NeedComma) OS << ',';
Dale Johannesen913d3df2008-09-12 17:49:03 +0000225 if (isEarlyClobber())
226 OS << "earlyclobber,";
Evan Cheng07897072009-10-14 23:37:31 +0000227 if (isImplicit())
228 OS << "imp-";
Chris Lattnerf7382302007-12-30 21:56:09 +0000229 OS << "def";
230 NeedComma = true;
Evan Cheng5affca02009-10-21 07:56:02 +0000231 } else if (isImplicit()) {
Evan Cheng07897072009-10-14 23:37:31 +0000232 OS << "imp-use";
Evan Cheng5affca02009-10-21 07:56:02 +0000233 NeedComma = true;
234 }
Evan Cheng07897072009-10-14 23:37:31 +0000235
Evan Cheng4784f1f2009-06-30 08:49:04 +0000236 if (isKill() || isDead() || isUndef()) {
Chris Lattner31530612009-06-24 17:54:48 +0000237 if (NeedComma) OS << ',';
Bill Wendling181eb732008-02-24 00:56:13 +0000238 if (isKill()) OS << "kill";
239 if (isDead()) OS << "dead";
Evan Cheng4784f1f2009-06-30 08:49:04 +0000240 if (isUndef()) {
241 if (isKill() || isDead())
242 OS << ',';
243 OS << "undef";
244 }
Chris Lattnerf7382302007-12-30 21:56:09 +0000245 }
Chris Lattner31530612009-06-24 17:54:48 +0000246 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000247 }
248 break;
249 case MachineOperand::MO_Immediate:
250 OS << getImm();
251 break;
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000252 case MachineOperand::MO_FPImmediate:
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000253 if (getFPImm()->getType()->isFloatTy())
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000254 OS << getFPImm()->getValueAPF().convertToFloat();
Chris Lattner31530612009-06-24 17:54:48 +0000255 else
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000256 OS << getFPImm()->getValueAPF().convertToDouble();
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000257 break;
Chris Lattnerf7382302007-12-30 21:56:09 +0000258 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman0ba90f32009-10-31 20:19:03 +0000259 OS << "<BB#" << getMBB()->getNumber() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000260 break;
261 case MachineOperand::MO_FrameIndex:
Chris Lattner31530612009-06-24 17:54:48 +0000262 OS << "<fi#" << getIndex() << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000263 break;
264 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000265 OS << "<cp#" << getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000266 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000267 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000268 break;
269 case MachineOperand::MO_JumpTableIndex:
Chris Lattner31530612009-06-24 17:54:48 +0000270 OS << "<jt#" << getIndex() << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000271 break;
272 case MachineOperand::MO_GlobalAddress:
Dan Gohman8d4e3b52009-11-06 18:03:10 +0000273 OS << "<ga:";
274 WriteAsOperand(OS, getGlobal(), /*PrintType=*/false);
Chris Lattnerf7382302007-12-30 21:56:09 +0000275 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000276 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000277 break;
278 case MachineOperand::MO_ExternalSymbol:
279 OS << "<es:" << getSymbolName();
280 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000281 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000282 break;
Dan Gohman8c2b5252009-10-30 01:27:03 +0000283 case MachineOperand::MO_BlockAddress:
Dale Johannesen5f72a5e2010-01-13 00:00:24 +0000284 OS << '<';
Dan Gohman0ba90f32009-10-31 20:19:03 +0000285 WriteAsOperand(OS, getBlockAddress(), /*PrintType=*/false);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000286 OS << '>';
287 break;
Dale Johannesen5f72a5e2010-01-13 00:00:24 +0000288 case MachineOperand::MO_Metadata:
289 OS << '<';
290 WriteAsOperand(OS, getMetadata(), /*PrintType=*/false);
291 OS << '>';
292 break;
Chris Lattnerf7382302007-12-30 21:56:09 +0000293 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000294 llvm_unreachable("Unrecognized operand type");
Chris Lattnerf7382302007-12-30 21:56:09 +0000295 }
Chris Lattner31530612009-06-24 17:54:48 +0000296
297 if (unsigned TF = getTargetFlags())
298 OS << "[TF=" << TF << ']';
Chris Lattnerf7382302007-12-30 21:56:09 +0000299}
300
301//===----------------------------------------------------------------------===//
Dan Gohmance42e402008-07-07 20:32:02 +0000302// MachineMemOperand Implementation
303//===----------------------------------------------------------------------===//
304
305MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f,
306 int64_t o, uint64_t s, unsigned int a)
307 : Offset(o), Size(s), V(v),
David Greeneba2b2972010-02-15 16:48:31 +0000308 Flags((f & ((1 << MOMaxBits) - 1)) | ((Log2_32(a) + 1) << MOMaxBits)) {
Dan Gohman28f02fd2009-09-21 19:47:04 +0000309 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
Dan Gohmanc5e1f982008-07-16 15:56:42 +0000310 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmance42e402008-07-07 20:32:02 +0000311}
312
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000313/// Profile - Gather unique data for the object.
314///
315void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
316 ID.AddInteger(Offset);
317 ID.AddInteger(Size);
318 ID.AddPointer(V);
319 ID.AddInteger(Flags);
320}
321
Dan Gohmanc76909a2009-09-25 20:36:54 +0000322void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
323 // The Value and Offset may differ due to CSE. But the flags and size
324 // should be the same.
325 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
326 assert(MMO->getSize() == getSize() && "Size mismatch!");
327
328 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
329 // Update the alignment value.
David Greeneba2b2972010-02-15 16:48:31 +0000330 Flags = (Flags & ((1 << MOMaxBits) - 1)) |
331 ((Log2_32(MMO->getBaseAlignment()) + 1) << MOMaxBits);
Dan Gohmanc76909a2009-09-25 20:36:54 +0000332 // Also update the base and offset, because the new alignment may
333 // not be applicable with the old ones.
334 V = MMO->getValue();
335 Offset = MMO->getOffset();
336 }
337}
338
Dan Gohman4b2ebc12009-09-25 23:33:20 +0000339/// getAlignment - Return the minimum known alignment in bytes of the
340/// actual memory reference.
341uint64_t MachineMemOperand::getAlignment() const {
342 return MinAlign(getBaseAlignment(), getOffset());
343}
344
Dan Gohmanc76909a2009-09-25 20:36:54 +0000345raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) {
346 assert((MMO.isLoad() || MMO.isStore()) &&
Dan Gohmancd26ec52009-09-23 01:33:16 +0000347 "SV has to be a load, store or both.");
348
Dan Gohmanc76909a2009-09-25 20:36:54 +0000349 if (MMO.isVolatile())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000350 OS << "Volatile ";
351
Dan Gohmanc76909a2009-09-25 20:36:54 +0000352 if (MMO.isLoad())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000353 OS << "LD";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000354 if (MMO.isStore())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000355 OS << "ST";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000356 OS << MMO.getSize();
Dan Gohmancd26ec52009-09-23 01:33:16 +0000357
358 // Print the address information.
359 OS << "[";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000360 if (!MMO.getValue())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000361 OS << "<unknown>";
362 else
Dan Gohmanc76909a2009-09-25 20:36:54 +0000363 WriteAsOperand(OS, MMO.getValue(), /*PrintType=*/false);
Dan Gohmancd26ec52009-09-23 01:33:16 +0000364
365 // If the alignment of the memory reference itself differs from the alignment
366 // of the base pointer, print the base alignment explicitly, next to the base
367 // pointer.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000368 if (MMO.getBaseAlignment() != MMO.getAlignment())
369 OS << "(align=" << MMO.getBaseAlignment() << ")";
Dan Gohmancd26ec52009-09-23 01:33:16 +0000370
Dan Gohmanc76909a2009-09-25 20:36:54 +0000371 if (MMO.getOffset() != 0)
372 OS << "+" << MMO.getOffset();
Dan Gohmancd26ec52009-09-23 01:33:16 +0000373 OS << "]";
374
375 // Print the alignment of the reference.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000376 if (MMO.getBaseAlignment() != MMO.getAlignment() ||
377 MMO.getBaseAlignment() != MMO.getSize())
378 OS << "(align=" << MMO.getAlignment() << ")";
Dan Gohmancd26ec52009-09-23 01:33:16 +0000379
380 return OS;
381}
382
Dan Gohmance42e402008-07-07 20:32:02 +0000383//===----------------------------------------------------------------------===//
Chris Lattnerf7382302007-12-30 21:56:09 +0000384// MachineInstr Implementation
385//===----------------------------------------------------------------------===//
386
Evan Chengc0f64ff2006-11-27 23:37:22 +0000387/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
Evan Cheng67f660c2006-11-30 07:08:44 +0000388/// TID NULL and no operands.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000389MachineInstr::MachineInstr()
Dan Gohman834651c2009-11-16 22:49:38 +0000390 : TID(0), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000391 Parent(0), debugLoc(DebugLoc::getUnknownLoc()) {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000392 // Make sure that we get added to a machine basicblock
393 LeakDetector::addGarbageObject(this);
Chris Lattner72791222002-10-28 20:59:49 +0000394}
395
Evan Cheng67f660c2006-11-30 07:08:44 +0000396void MachineInstr::addImplicitDefUseOperands() {
397 if (TID->ImplicitDefs)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000398 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner8019f412007-12-30 00:41:17 +0000399 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng67f660c2006-11-30 07:08:44 +0000400 if (TID->ImplicitUses)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000401 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner8019f412007-12-30 00:41:17 +0000402 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Evan Chengd7de4962006-11-13 23:34:06 +0000403}
404
405/// MachineInstr ctor - This constructor create a MachineInstr and add the
Evan Chengc0f64ff2006-11-27 23:37:22 +0000406/// implicit operands. It reserves space for number of operands specified by
Chris Lattner749c6f62008-01-07 07:27:27 +0000407/// TargetInstrDesc or the numOperands if it is not zero. (for
Evan Chengc0f64ff2006-11-27 23:37:22 +0000408/// instructions with variable number of operands).
Chris Lattner749c6f62008-01-07 07:27:27 +0000409MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
Dan Gohman834651c2009-11-16 22:49:38 +0000410 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0),
411 MemRefs(0), MemRefsEnd(0), Parent(0),
Dale Johannesen06efc022009-01-27 23:20:29 +0000412 debugLoc(DebugLoc::getUnknownLoc()) {
Chris Lattner349c4952008-01-07 03:13:06 +0000413 if (!NoImp && TID->getImplicitDefs())
414 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000415 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000416 if (!NoImp && TID->getImplicitUses())
417 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000418 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000419 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Chengfa945722007-10-13 02:23:01 +0000420 if (!NoImp)
421 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000422 // Make sure that we get added to a machine basicblock
423 LeakDetector::addGarbageObject(this);
Evan Chengd7de4962006-11-13 23:34:06 +0000424}
425
Dale Johannesen06efc022009-01-27 23:20:29 +0000426/// MachineInstr ctor - As above, but with a DebugLoc.
427MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl,
428 bool NoImp)
Dan Gohman834651c2009-11-16 22:49:38 +0000429 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000430 Parent(0), debugLoc(dl) {
Dale Johannesen06efc022009-01-27 23:20:29 +0000431 if (!NoImp && TID->getImplicitDefs())
432 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
433 NumImplicitOps++;
434 if (!NoImp && TID->getImplicitUses())
435 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
436 NumImplicitOps++;
437 Operands.reserve(NumImplicitOps + TID->getNumOperands());
438 if (!NoImp)
439 addImplicitDefUseOperands();
440 // Make sure that we get added to a machine basicblock
441 LeakDetector::addGarbageObject(this);
442}
443
444/// MachineInstr ctor - Work exactly the same as the ctor two above, except
445/// that the MachineInstr is created and added to the end of the specified
446/// basic block.
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000447///
Dale Johannesen06efc022009-01-27 23:20:29 +0000448MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid)
Dan Gohman834651c2009-11-16 22:49:38 +0000449 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0),
450 MemRefs(0), MemRefsEnd(0), Parent(0),
Dale Johannesen06efc022009-01-27 23:20:29 +0000451 debugLoc(DebugLoc::getUnknownLoc()) {
452 assert(MBB && "Cannot use inserting ctor with null basic block!");
453 if (TID->ImplicitDefs)
454 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
455 NumImplicitOps++;
456 if (TID->ImplicitUses)
457 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
458 NumImplicitOps++;
459 Operands.reserve(NumImplicitOps + TID->getNumOperands());
460 addImplicitDefUseOperands();
461 // Make sure that we get added to a machine basicblock
462 LeakDetector::addGarbageObject(this);
463 MBB->push_back(this); // Add instruction to end of basic block!
464}
465
466/// MachineInstr ctor - As above, but with a DebugLoc.
467///
468MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl,
Chris Lattner749c6f62008-01-07 07:27:27 +0000469 const TargetInstrDesc &tid)
Dan Gohman834651c2009-11-16 22:49:38 +0000470 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000471 Parent(0), debugLoc(dl) {
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000472 assert(MBB && "Cannot use inserting ctor with null basic block!");
Evan Cheng67f660c2006-11-30 07:08:44 +0000473 if (TID->ImplicitDefs)
Chris Lattner349c4952008-01-07 03:13:06 +0000474 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000475 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +0000476 if (TID->ImplicitUses)
Chris Lattner349c4952008-01-07 03:13:06 +0000477 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000478 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000479 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Cheng67f660c2006-11-30 07:08:44 +0000480 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000481 // Make sure that we get added to a machine basicblock
482 LeakDetector::addGarbageObject(this);
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000483 MBB->push_back(this); // Add instruction to end of basic block!
484}
485
Misha Brukmance22e762004-07-09 14:45:17 +0000486/// MachineInstr ctor - Copies MachineInstr arg exactly
487///
Evan Cheng1ed99222008-07-19 00:37:25 +0000488MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Dan Gohman834651c2009-11-16 22:49:38 +0000489 : TID(&MI.getDesc()), NumImplicitOps(0), AsmPrinterFlags(0),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000490 MemRefs(MI.MemRefs), MemRefsEnd(MI.MemRefsEnd),
491 Parent(0), debugLoc(MI.getDebugLoc()) {
Chris Lattner943b5e12006-05-04 19:14:44 +0000492 Operands.reserve(MI.getNumOperands());
Tanya Lattnerb5159ed2004-05-23 20:58:02 +0000493
Misha Brukmance22e762004-07-09 14:45:17 +0000494 // Add operands
Evan Cheng1ed99222008-07-19 00:37:25 +0000495 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
496 addOperand(MI.getOperand(i));
497 NumImplicitOps = MI.NumImplicitOps;
Tanya Lattner0c63e032004-05-24 03:14:18 +0000498
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000499 // Set parent to null.
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000500 Parent = 0;
Dan Gohman6116a732008-07-21 18:47:29 +0000501
502 LeakDetector::addGarbageObject(this);
Tanya Lattner466b5342004-05-23 19:35:12 +0000503}
504
Misha Brukmance22e762004-07-09 14:45:17 +0000505MachineInstr::~MachineInstr() {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000506 LeakDetector::removeGarbageObject(this);
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000507#ifndef NDEBUG
Chris Lattner62ed6b92008-01-01 01:12:31 +0000508 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000509 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Dan Gohmand735b802008-10-03 15:45:36 +0000510 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
Chris Lattner62ed6b92008-01-01 01:12:31 +0000511 "Reg operand def/use list corrupted");
512 }
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000513#endif
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000514}
515
Chris Lattner62ed6b92008-01-01 01:12:31 +0000516/// getRegInfo - If this instruction is embedded into a MachineFunction,
517/// return the MachineRegisterInfo object for the current function, otherwise
518/// return null.
519MachineRegisterInfo *MachineInstr::getRegInfo() {
520 if (MachineBasicBlock *MBB = getParent())
Dan Gohman4e526b92008-07-08 23:59:09 +0000521 return &MBB->getParent()->getRegInfo();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000522 return 0;
523}
524
525/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
526/// this instruction from their respective use lists. This requires that the
527/// operands already be on their use lists.
528void MachineInstr::RemoveRegOperandsFromUseLists() {
529 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000530 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000531 Operands[i].RemoveRegOperandFromRegInfo();
532 }
533}
534
535/// AddRegOperandsToUseLists - Add all of the register operands in
536/// this instruction from their respective use lists. This requires that the
537/// operands not be on their use lists yet.
538void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
539 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000540 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000541 Operands[i].AddRegOperandToRegInfo(&RegInfo);
542 }
543}
544
545
546/// addOperand - Add the specified operand to the instruction. If it is an
547/// implicit operand, it is added to the end of the operand list. If it is
548/// an explicit operand it is added at the end of the explicit operand list
549/// (before the first implicit operand).
550void MachineInstr::addOperand(const MachineOperand &Op) {
Dan Gohmand735b802008-10-03 15:45:36 +0000551 bool isImpReg = Op.isReg() && Op.isImplicit();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000552 assert((isImpReg || !OperandsComplete()) &&
553 "Trying to add an operand to a machine instr that is already done!");
554
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000555 MachineRegisterInfo *RegInfo = getRegInfo();
556
Chris Lattner62ed6b92008-01-01 01:12:31 +0000557 // If we are adding the operand to the end of the list, our job is simpler.
558 // This is true most of the time, so this is a reasonable optimization.
559 if (isImpReg || NumImplicitOps == 0) {
560 // We can only do this optimization if we know that the operand list won't
561 // reallocate.
562 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
563 Operands.push_back(Op);
564
565 // Set the parent of the operand.
566 Operands.back().ParentMI = this;
567
568 // If the operand is a register, update the operand's use list.
Jim Grosbach06801722009-12-16 19:43:02 +0000569 if (Op.isReg()) {
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000570 Operands.back().AddRegOperandToRegInfo(RegInfo);
Jim Grosbach06801722009-12-16 19:43:02 +0000571 // If the register operand is flagged as early, mark the operand as such
572 unsigned OpNo = Operands.size() - 1;
573 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
574 Operands[OpNo].setIsEarlyClobber(true);
575 }
Chris Lattner62ed6b92008-01-01 01:12:31 +0000576 return;
577 }
578 }
579
580 // Otherwise, we have to insert a real operand before any implicit ones.
581 unsigned OpNo = Operands.size()-NumImplicitOps;
582
Chris Lattner62ed6b92008-01-01 01:12:31 +0000583 // If this instruction isn't embedded into a function, then we don't need to
584 // update any operand lists.
585 if (RegInfo == 0) {
586 // Simple insertion, no reginfo update needed for other register operands.
587 Operands.insert(Operands.begin()+OpNo, Op);
588 Operands[OpNo].ParentMI = this;
589
590 // Do explicitly set the reginfo for this operand though, to ensure the
591 // next/prev fields are properly nulled out.
Jim Grosbach06801722009-12-16 19:43:02 +0000592 if (Operands[OpNo].isReg()) {
Chris Lattner62ed6b92008-01-01 01:12:31 +0000593 Operands[OpNo].AddRegOperandToRegInfo(0);
Jim Grosbach06801722009-12-16 19:43:02 +0000594 // If the register operand is flagged as early, mark the operand as such
595 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
596 Operands[OpNo].setIsEarlyClobber(true);
597 }
Chris Lattner62ed6b92008-01-01 01:12:31 +0000598
599 } else if (Operands.size()+1 <= Operands.capacity()) {
600 // Otherwise, we have to remove register operands from their register use
601 // list, add the operand, then add the register operands back to their use
602 // list. This also must handle the case when the operand list reallocates
603 // to somewhere else.
604
605 // If insertion of this operand won't cause reallocation of the operand
606 // list, just remove the implicit operands, add the operand, then re-add all
607 // the rest of the operands.
608 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000609 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000610 Operands[i].RemoveRegOperandFromRegInfo();
611 }
612
613 // Add the operand. If it is a register, add it to the reg list.
614 Operands.insert(Operands.begin()+OpNo, Op);
615 Operands[OpNo].ParentMI = this;
616
Jim Grosbach06801722009-12-16 19:43:02 +0000617 if (Operands[OpNo].isReg()) {
Chris Lattner62ed6b92008-01-01 01:12:31 +0000618 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
Jim Grosbach06801722009-12-16 19:43:02 +0000619 // If the register operand is flagged as early, mark the operand as such
620 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
621 Operands[OpNo].setIsEarlyClobber(true);
622 }
Chris Lattner62ed6b92008-01-01 01:12:31 +0000623
624 // Re-add all the implicit ops.
625 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000626 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000627 Operands[i].AddRegOperandToRegInfo(RegInfo);
628 }
629 } else {
630 // Otherwise, we will be reallocating the operand list. Remove all reg
631 // operands from their list, then readd them after the operand list is
632 // reallocated.
633 RemoveRegOperandsFromUseLists();
634
635 Operands.insert(Operands.begin()+OpNo, Op);
636 Operands[OpNo].ParentMI = this;
637
638 // Re-add all the operands.
639 AddRegOperandsToUseLists(*RegInfo);
Jim Grosbach06801722009-12-16 19:43:02 +0000640
641 // If the register operand is flagged as early, mark the operand as such
642 if (Operands[OpNo].isReg()
643 && TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
644 Operands[OpNo].setIsEarlyClobber(true);
Chris Lattner62ed6b92008-01-01 01:12:31 +0000645 }
646}
647
648/// RemoveOperand - Erase an operand from an instruction, leaving it with one
649/// fewer operand than it started with.
650///
651void MachineInstr::RemoveOperand(unsigned OpNo) {
652 assert(OpNo < Operands.size() && "Invalid operand number");
653
654 // Special case removing the last one.
655 if (OpNo == Operands.size()-1) {
656 // If needed, remove from the reg def/use list.
Dan Gohmand735b802008-10-03 15:45:36 +0000657 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000658 Operands.back().RemoveRegOperandFromRegInfo();
659
660 Operands.pop_back();
661 return;
662 }
663
664 // Otherwise, we are removing an interior operand. If we have reginfo to
665 // update, remove all operands that will be shifted down from their reg lists,
666 // move everything down, then re-add them.
667 MachineRegisterInfo *RegInfo = getRegInfo();
668 if (RegInfo) {
669 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000670 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000671 Operands[i].RemoveRegOperandFromRegInfo();
672 }
673 }
674
675 Operands.erase(Operands.begin()+OpNo);
676
677 if (RegInfo) {
678 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000679 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000680 Operands[i].AddRegOperandToRegInfo(RegInfo);
681 }
682 }
683}
684
Dan Gohmanc76909a2009-09-25 20:36:54 +0000685/// addMemOperand - Add a MachineMemOperand to the machine instruction.
686/// This function should be used only occasionally. The setMemRefs function
687/// is the primary method for setting up a MachineInstr's MemRefs list.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000688void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohmanc76909a2009-09-25 20:36:54 +0000689 MachineMemOperand *MO) {
690 mmo_iterator OldMemRefs = MemRefs;
691 mmo_iterator OldMemRefsEnd = MemRefsEnd;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000692
Dan Gohmanc76909a2009-09-25 20:36:54 +0000693 size_t NewNum = (MemRefsEnd - MemRefs) + 1;
694 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
695 mmo_iterator NewMemRefsEnd = NewMemRefs + NewNum;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000696
Dan Gohmanc76909a2009-09-25 20:36:54 +0000697 std::copy(OldMemRefs, OldMemRefsEnd, NewMemRefs);
698 NewMemRefs[NewNum - 1] = MO;
699
700 MemRefs = NewMemRefs;
701 MemRefsEnd = NewMemRefsEnd;
702}
Chris Lattner62ed6b92008-01-01 01:12:31 +0000703
Chris Lattner48d7c062006-04-17 21:35:41 +0000704/// removeFromParent - This method unlinks 'this' from the containing basic
705/// block, and returns it, but does not delete it.
706MachineInstr *MachineInstr::removeFromParent() {
707 assert(getParent() && "Not embedded in a basic block!");
708 getParent()->remove(this);
709 return this;
710}
711
712
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000713/// eraseFromParent - This method unlinks 'this' from the containing basic
714/// block, and deletes it.
715void MachineInstr::eraseFromParent() {
716 assert(getParent() && "Not embedded in a basic block!");
717 getParent()->erase(this);
718}
719
720
Brian Gaeke21326fc2004-02-13 04:39:32 +0000721/// OperandComplete - Return true if it's illegal to add a new operand
722///
Chris Lattner2a90ba62004-02-12 16:09:53 +0000723bool MachineInstr::OperandsComplete() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000724 unsigned short NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000725 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000726 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000727 return false;
728}
729
Evan Cheng19e3f312007-05-15 01:26:09 +0000730/// getNumExplicitOperands - Returns the number of non-implicit operands.
731///
732unsigned MachineInstr::getNumExplicitOperands() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000733 unsigned NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000734 if (!TID->isVariadic())
Evan Cheng19e3f312007-05-15 01:26:09 +0000735 return NumOperands;
736
Dan Gohman9407cd42009-04-15 17:59:11 +0000737 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
738 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000739 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng19e3f312007-05-15 01:26:09 +0000740 NumOperands++;
741 }
742 return NumOperands;
743}
744
Chris Lattner8ace2cd2006-10-20 22:39:59 +0000745
Evan Chengfaa51072007-04-26 19:00:32 +0000746/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbachf9ca50e2009-09-17 17:57:26 +0000747/// the specific register or -1 if it is not found. It further tightens
Evan Cheng76d7e762007-02-23 01:04:26 +0000748/// the search criteria to a use that kills the register if isKill is true.
Evan Cheng6130f662008-03-05 00:59:57 +0000749int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
750 const TargetRegisterInfo *TRI) const {
Evan Cheng576d1232006-12-06 08:27:42 +0000751 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengf277ee42007-05-29 18:35:22 +0000752 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000753 if (!MO.isReg() || !MO.isUse())
Evan Cheng6130f662008-03-05 00:59:57 +0000754 continue;
755 unsigned MOReg = MO.getReg();
756 if (!MOReg)
757 continue;
758 if (MOReg == Reg ||
759 (TRI &&
760 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
761 TargetRegisterInfo::isPhysicalRegister(Reg) &&
762 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng76d7e762007-02-23 01:04:26 +0000763 if (!isKill || MO.isKill())
Evan Cheng32eb1f12007-03-26 22:37:45 +0000764 return i;
Evan Cheng576d1232006-12-06 08:27:42 +0000765 }
Evan Cheng32eb1f12007-03-26 22:37:45 +0000766 return -1;
Evan Cheng576d1232006-12-06 08:27:42 +0000767}
768
Evan Cheng6130f662008-03-05 00:59:57 +0000769/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman703bfe62008-05-06 00:20:10 +0000770/// the specified register or -1 if it is not found. If isDead is true, defs
771/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
772/// also checks if there is a def of a super-register.
Evan Cheng6130f662008-03-05 00:59:57 +0000773int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead,
774 const TargetRegisterInfo *TRI) const {
Evan Chengb371f452007-02-19 21:49:54 +0000775 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng6130f662008-03-05 00:59:57 +0000776 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000777 if (!MO.isReg() || !MO.isDef())
Evan Cheng6130f662008-03-05 00:59:57 +0000778 continue;
779 unsigned MOReg = MO.getReg();
780 if (MOReg == Reg ||
781 (TRI &&
782 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
783 TargetRegisterInfo::isPhysicalRegister(Reg) &&
784 TRI->isSubRegister(MOReg, Reg)))
785 if (!isDead || MO.isDead())
786 return i;
Evan Chengb371f452007-02-19 21:49:54 +0000787 }
Evan Cheng6130f662008-03-05 00:59:57 +0000788 return -1;
Evan Chengb371f452007-02-19 21:49:54 +0000789}
Evan Cheng19e3f312007-05-15 01:26:09 +0000790
Evan Chengf277ee42007-05-29 18:35:22 +0000791/// findFirstPredOperandIdx() - Find the index of the first operand in the
792/// operand list that is used to represent the predicate. It returns -1 if
793/// none is found.
794int MachineInstr::findFirstPredOperandIdx() const {
Chris Lattner749c6f62008-01-07 07:27:27 +0000795 const TargetInstrDesc &TID = getDesc();
796 if (TID.isPredicable()) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000797 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner749c6f62008-01-07 07:27:27 +0000798 if (TID.OpInfo[i].isPredicate())
Evan Chengf277ee42007-05-29 18:35:22 +0000799 return i;
Evan Cheng19e3f312007-05-15 01:26:09 +0000800 }
801
Evan Chengf277ee42007-05-29 18:35:22 +0000802 return -1;
Evan Cheng19e3f312007-05-15 01:26:09 +0000803}
Evan Chengb371f452007-02-19 21:49:54 +0000804
Bob Wilsond9df5012009-04-09 17:16:43 +0000805/// isRegTiedToUseOperand - Given the index of a register def operand,
806/// check if the register def is tied to a source operand, due to either
807/// two-address elimination or inline assembly constraints. Returns the
808/// first tied use operand index by reference is UseOpIdx is not null.
Jakob Stoklund Olesence9be2c2009-04-29 20:57:16 +0000809bool MachineInstr::
810isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const {
Chris Lattner518bb532010-02-09 19:54:29 +0000811 if (isInlineAsm()) {
Bob Wilsond9df5012009-04-09 17:16:43 +0000812 assert(DefOpIdx >= 2);
813 const MachineOperand &MO = getOperand(DefOpIdx);
Chris Lattnerc30aa7b2009-04-09 23:33:34 +0000814 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
Evan Chengfb112882009-03-23 08:01:15 +0000815 return false;
Evan Chengef5d0702009-06-24 02:05:51 +0000816 // Determine the actual operand index that corresponds to this index.
Evan Chengfb112882009-03-23 08:01:15 +0000817 unsigned DefNo = 0;
Evan Chengef5d0702009-06-24 02:05:51 +0000818 unsigned DefPart = 0;
Evan Chengfb112882009-03-23 08:01:15 +0000819 for (unsigned i = 1, e = getNumOperands(); i < e; ) {
820 const MachineOperand &FMO = getOperand(i);
Jakob Stoklund Olesen45d34fe2009-07-19 19:09:59 +0000821 // After the normal asm operands there may be additional imp-def regs.
822 if (!FMO.isImm())
823 return false;
Evan Chengfb112882009-03-23 08:01:15 +0000824 // Skip over this def.
Evan Chengef5d0702009-06-24 02:05:51 +0000825 unsigned NumOps = InlineAsm::getNumOperandRegisters(FMO.getImm());
826 unsigned PrevDef = i + 1;
827 i = PrevDef + NumOps;
828 if (i > DefOpIdx) {
829 DefPart = DefOpIdx - PrevDef;
Evan Chengfb112882009-03-23 08:01:15 +0000830 break;
Evan Chengef5d0702009-06-24 02:05:51 +0000831 }
Evan Chengfb112882009-03-23 08:01:15 +0000832 ++DefNo;
833 }
Evan Chengef5d0702009-06-24 02:05:51 +0000834 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Evan Chengfb112882009-03-23 08:01:15 +0000835 const MachineOperand &FMO = getOperand(i);
836 if (!FMO.isImm())
837 continue;
838 if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse())
839 continue;
840 unsigned Idx;
Evan Chengef5d0702009-06-24 02:05:51 +0000841 if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000842 Idx == DefNo) {
843 if (UseOpIdx)
Evan Chengef5d0702009-06-24 02:05:51 +0000844 *UseOpIdx = (unsigned)i + 1 + DefPart;
Evan Chengfb112882009-03-23 08:01:15 +0000845 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000846 }
Evan Chengfb112882009-03-23 08:01:15 +0000847 }
Evan Chengef5d0702009-06-24 02:05:51 +0000848 return false;
Evan Chengfb112882009-03-23 08:01:15 +0000849 }
850
Bob Wilsond9df5012009-04-09 17:16:43 +0000851 assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!");
Chris Lattner749c6f62008-01-07 07:27:27 +0000852 const TargetInstrDesc &TID = getDesc();
Evan Chengef0732d2008-07-10 07:35:43 +0000853 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
854 const MachineOperand &MO = getOperand(i);
Dan Gohman2ce7f202008-12-05 05:45:42 +0000855 if (MO.isReg() && MO.isUse() &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000856 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) {
857 if (UseOpIdx)
858 *UseOpIdx = (unsigned)i;
Evan Chengef0732d2008-07-10 07:35:43 +0000859 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000860 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000861 }
862 return false;
863}
864
Evan Chenga24752f2009-03-19 20:30:06 +0000865/// isRegTiedToDefOperand - Return true if the operand of the specified index
866/// is a register use and it is tied to an def operand. It also returns the def
867/// operand index by reference.
Jakob Stoklund Olesence9be2c2009-04-29 20:57:16 +0000868bool MachineInstr::
869isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const {
Chris Lattner518bb532010-02-09 19:54:29 +0000870 if (isInlineAsm()) {
Evan Chengfb112882009-03-23 08:01:15 +0000871 const MachineOperand &MO = getOperand(UseOpIdx);
Chris Lattner0c8382c2009-04-09 16:50:43 +0000872 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0)
Evan Chengfb112882009-03-23 08:01:15 +0000873 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000874
875 // Find the flag operand corresponding to UseOpIdx
876 unsigned FlagIdx, NumOps=0;
877 for (FlagIdx = 1; FlagIdx < UseOpIdx; FlagIdx += NumOps+1) {
878 const MachineOperand &UFMO = getOperand(FlagIdx);
Jakob Stoklund Olesen45d34fe2009-07-19 19:09:59 +0000879 // After the normal asm operands there may be additional imp-def regs.
880 if (!UFMO.isImm())
881 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000882 NumOps = InlineAsm::getNumOperandRegisters(UFMO.getImm());
883 assert(NumOps < getNumOperands() && "Invalid inline asm flag");
884 if (UseOpIdx < FlagIdx+NumOps+1)
885 break;
Evan Chengef5d0702009-06-24 02:05:51 +0000886 }
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000887 if (FlagIdx >= UseOpIdx)
Evan Chengef5d0702009-06-24 02:05:51 +0000888 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000889 const MachineOperand &UFMO = getOperand(FlagIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000890 unsigned DefNo;
891 if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) {
892 if (!DefOpIdx)
893 return true;
894
895 unsigned DefIdx = 1;
896 // Remember to adjust the index. First operand is asm string, then there
897 // is a flag for each.
898 while (DefNo) {
899 const MachineOperand &FMO = getOperand(DefIdx);
900 assert(FMO.isImm());
901 // Skip over this def.
902 DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
903 --DefNo;
904 }
Evan Chengef5d0702009-06-24 02:05:51 +0000905 *DefOpIdx = DefIdx + UseOpIdx - FlagIdx;
Evan Chengfb112882009-03-23 08:01:15 +0000906 return true;
907 }
908 return false;
909 }
910
Evan Chenga24752f2009-03-19 20:30:06 +0000911 const TargetInstrDesc &TID = getDesc();
912 if (UseOpIdx >= TID.getNumOperands())
913 return false;
914 const MachineOperand &MO = getOperand(UseOpIdx);
915 if (!MO.isReg() || !MO.isUse())
916 return false;
917 int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO);
918 if (DefIdx == -1)
919 return false;
920 if (DefOpIdx)
921 *DefOpIdx = (unsigned)DefIdx;
922 return true;
923}
924
Evan Cheng576d1232006-12-06 08:27:42 +0000925/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
926///
927void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
928 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
929 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000930 if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
Evan Cheng576d1232006-12-06 08:27:42 +0000931 continue;
932 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
933 MachineOperand &MOp = getOperand(j);
934 if (!MOp.isIdenticalTo(MO))
935 continue;
936 if (MO.isKill())
937 MOp.setIsKill();
938 else
939 MOp.setIsDead();
940 break;
941 }
942 }
943}
944
Evan Cheng19e3f312007-05-15 01:26:09 +0000945/// copyPredicates - Copies predicate operand(s) from MI.
946void MachineInstr::copyPredicates(const MachineInstr *MI) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000947 const TargetInstrDesc &TID = MI->getDesc();
Evan Chengb27087f2008-03-13 00:44:09 +0000948 if (!TID.isPredicable())
949 return;
950 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
951 if (TID.OpInfo[i].isPredicate()) {
952 // Predicated operands must be last operands.
953 addOperand(MI->getOperand(i));
Evan Cheng19e3f312007-05-15 01:26:09 +0000954 }
955 }
956}
957
Evan Cheng9f1c8312008-07-03 09:09:37 +0000958/// isSafeToMove - Return true if it is safe to move this instruction. If
959/// SawStore is set to true, it means that there is a store (or call) between
960/// the instruction's location and its intended destination.
Dan Gohmanb3b930a2008-11-18 19:04:29 +0000961bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
Evan Chengac1abde2010-03-02 19:03:01 +0000962 AliasAnalysis *AA,
963 bool &SawStore) const {
Evan Chengb27087f2008-03-13 00:44:09 +0000964 // Ignore stuff that we obviously can't move.
965 if (TID->mayStore() || TID->isCall()) {
966 SawStore = true;
967 return false;
968 }
Dan Gohman237dee12008-12-23 17:28:50 +0000969 if (TID->isTerminator() || TID->hasUnmodeledSideEffects())
Evan Chengb27087f2008-03-13 00:44:09 +0000970 return false;
971
972 // See if this instruction does a load. If so, we have to guarantee that the
973 // loaded value doesn't change between the load and the its intended
974 // destination. The check for isInvariantLoad gives the targe the chance to
975 // classify the load as always returning a constant, e.g. a constant pool
976 // load.
Dan Gohmana70dca12009-10-09 23:27:56 +0000977 if (TID->mayLoad() && !isInvariantLoad(AA))
Evan Chengb27087f2008-03-13 00:44:09 +0000978 // Otherwise, this is a real load. If there is a store between the load and
Evan Cheng7cc2c402009-07-28 21:49:18 +0000979 // end of block, or if the load is volatile, we can't move it.
Dan Gohmand790a5c2008-10-02 15:04:30 +0000980 return !SawStore && !hasVolatileMemoryRef();
Dan Gohman3e4fb702008-09-24 00:06:15 +0000981
Evan Chengb27087f2008-03-13 00:44:09 +0000982 return true;
983}
984
Evan Chengdf3b9932008-08-27 20:33:50 +0000985/// isSafeToReMat - Return true if it's safe to rematerialize the specified
986/// instruction which defined the specified register instead of copying it.
Dan Gohmanb3b930a2008-11-18 19:04:29 +0000987bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII,
Evan Chengac1abde2010-03-02 19:03:01 +0000988 AliasAnalysis *AA,
989 unsigned DstReg) const {
Evan Chengdf3b9932008-08-27 20:33:50 +0000990 bool SawStore = false;
Dan Gohmana70dca12009-10-09 23:27:56 +0000991 if (!TII->isTriviallyReMaterializable(this, AA) ||
Evan Chengac1abde2010-03-02 19:03:01 +0000992 !isSafeToMove(TII, AA, SawStore))
Evan Chengdf3b9932008-08-27 20:33:50 +0000993 return false;
994 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohmancbad42c2008-11-18 19:49:32 +0000995 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000996 if (!MO.isReg())
Evan Chengdf3b9932008-08-27 20:33:50 +0000997 continue;
998 // FIXME: For now, do not remat any instruction with register operands.
999 // Later on, we can loosen the restriction is the register operands have
1000 // not been modified between the def and use. Note, this is different from
Evan Cheng8763c1c2008-08-27 20:58:54 +00001001 // MachineSink because the code is no longer in two-address form (at least
Evan Chengdf3b9932008-08-27 20:33:50 +00001002 // partially).
1003 if (MO.isUse())
1004 return false;
1005 else if (!MO.isDead() && MO.getReg() != DstReg)
1006 return false;
1007 }
1008 return true;
1009}
1010
Dan Gohman3e4fb702008-09-24 00:06:15 +00001011/// hasVolatileMemoryRef - Return true if this instruction may have a
1012/// volatile memory reference, or if the information describing the
1013/// memory reference is not available. Return false if it is known to
1014/// have no volatile memory references.
1015bool MachineInstr::hasVolatileMemoryRef() const {
1016 // An instruction known never to access memory won't have a volatile access.
1017 if (!TID->mayStore() &&
1018 !TID->mayLoad() &&
1019 !TID->isCall() &&
1020 !TID->hasUnmodeledSideEffects())
1021 return false;
1022
1023 // Otherwise, if the instruction has no memory reference information,
1024 // conservatively assume it wasn't preserved.
1025 if (memoperands_empty())
1026 return true;
1027
1028 // Check the memory reference information for volatile references.
Dan Gohmanc76909a2009-09-25 20:36:54 +00001029 for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I)
1030 if ((*I)->isVolatile())
Dan Gohman3e4fb702008-09-24 00:06:15 +00001031 return true;
1032
1033 return false;
1034}
1035
Dan Gohmane33f44c2009-10-07 17:38:06 +00001036/// isInvariantLoad - Return true if this instruction is loading from a
1037/// location whose value is invariant across the function. For example,
Dan Gohmanf451cb82010-02-10 16:03:48 +00001038/// loading a value from the constant pool or from the argument area
Dan Gohmane33f44c2009-10-07 17:38:06 +00001039/// of a function if it does not change. This should only return true of
1040/// *all* loads the instruction does are invariant (if it does multiple loads).
1041bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const {
1042 // If the instruction doesn't load at all, it isn't an invariant load.
1043 if (!TID->mayLoad())
1044 return false;
1045
1046 // If the instruction has lost its memoperands, conservatively assume that
1047 // it may not be an invariant load.
1048 if (memoperands_empty())
1049 return false;
1050
1051 const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo();
1052
1053 for (mmo_iterator I = memoperands_begin(),
1054 E = memoperands_end(); I != E; ++I) {
1055 if ((*I)->isVolatile()) return false;
1056 if ((*I)->isStore()) return false;
1057
1058 if (const Value *V = (*I)->getValue()) {
1059 // A load from a constant PseudoSourceValue is invariant.
1060 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V))
1061 if (PSV->isConstant(MFI))
1062 continue;
1063 // If we have an AliasAnalysis, ask it whether the memory is constant.
1064 if (AA && AA->pointsToConstantMemory(V))
1065 continue;
1066 }
1067
1068 // Otherwise assume conservatively.
1069 return false;
1070 }
1071
1072 // Everything checks out.
1073 return true;
1074}
1075
Evan Cheng229694f2009-12-03 02:31:43 +00001076/// isConstantValuePHI - If the specified instruction is a PHI that always
1077/// merges together the same virtual register, return the register, otherwise
1078/// return 0.
1079unsigned MachineInstr::isConstantValuePHI() const {
Chris Lattner518bb532010-02-09 19:54:29 +00001080 if (!isPHI())
Evan Cheng229694f2009-12-03 02:31:43 +00001081 return 0;
Evan Chengd8f079c2009-12-07 23:10:34 +00001082 assert(getNumOperands() >= 3 &&
1083 "It's illegal to have a PHI without source operands");
Evan Cheng229694f2009-12-03 02:31:43 +00001084
1085 unsigned Reg = getOperand(1).getReg();
1086 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
1087 if (getOperand(i).getReg() != Reg)
1088 return 0;
1089 return Reg;
1090}
1091
Brian Gaeke21326fc2004-02-13 04:39:32 +00001092void MachineInstr::dump() const {
David Greene3b325332010-01-04 23:48:20 +00001093 dbgs() << " " << *this;
Mon P Wang5ca6bd12008-10-10 01:43:55 +00001094}
1095
1096void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const {
Dan Gohman80f6c582009-11-09 19:38:45 +00001097 // We can be a bit tidier if we know the TargetMachine and/or MachineFunction.
1098 const MachineFunction *MF = 0;
1099 if (const MachineBasicBlock *MBB = getParent()) {
1100 MF = MBB->getParent();
1101 if (!TM && MF)
1102 TM = &MF->getTarget();
1103 }
Dan Gohman0ba90f32009-10-31 20:19:03 +00001104
1105 // Print explicitly defined operands on the left of an assignment syntax.
Dan Gohman80f6c582009-11-09 19:38:45 +00001106 unsigned StartOp = 0, e = getNumOperands();
Dan Gohman0ba90f32009-10-31 20:19:03 +00001107 for (; StartOp < e && getOperand(StartOp).isReg() &&
1108 getOperand(StartOp).isDef() &&
1109 !getOperand(StartOp).isImplicit();
1110 ++StartOp) {
1111 if (StartOp != 0) OS << ", ";
1112 getOperand(StartOp).print(OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +00001113 }
Tanya Lattnerb1407622004-06-25 00:13:11 +00001114
Dan Gohman0ba90f32009-10-31 20:19:03 +00001115 if (StartOp != 0)
1116 OS << " = ";
1117
1118 // Print the opcode name.
Chris Lattner749c6f62008-01-07 07:27:27 +00001119 OS << getDesc().getName();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001120
Dan Gohman0ba90f32009-10-31 20:19:03 +00001121 // Print the rest of the operands.
Dan Gohman80f6c582009-11-09 19:38:45 +00001122 bool OmittedAnyCallClobbers = false;
1123 bool FirstOp = true;
Chris Lattner6a592272002-10-30 01:55:38 +00001124 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohman80f6c582009-11-09 19:38:45 +00001125 const MachineOperand &MO = getOperand(i);
1126
1127 // Omit call-clobbered registers which aren't used anywhere. This makes
1128 // call instructions much less noisy on targets where calls clobber lots
1129 // of registers. Don't rely on MO.isDead() because we may be called before
1130 // LiveVariables is run, or we may be looking at a non-allocatable reg.
1131 if (MF && getDesc().isCall() &&
1132 MO.isReg() && MO.isImplicit() && MO.isDef()) {
1133 unsigned Reg = MO.getReg();
1134 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) {
1135 const MachineRegisterInfo &MRI = MF->getRegInfo();
1136 if (MRI.use_empty(Reg) && !MRI.isLiveOut(Reg)) {
1137 bool HasAliasLive = false;
1138 for (const unsigned *Alias = TM->getRegisterInfo()->getAliasSet(Reg);
1139 unsigned AliasReg = *Alias; ++Alias)
1140 if (!MRI.use_empty(AliasReg) || MRI.isLiveOut(AliasReg)) {
1141 HasAliasLive = true;
1142 break;
1143 }
1144 if (!HasAliasLive) {
1145 OmittedAnyCallClobbers = true;
1146 continue;
1147 }
1148 }
1149 }
1150 }
1151
1152 if (FirstOp) FirstOp = false; else OS << ",";
Chris Lattner6a592272002-10-30 01:55:38 +00001153 OS << " ";
Jakob Stoklund Olesenb1bb4af2010-01-19 22:08:34 +00001154 if (i < getDesc().NumOperands) {
1155 const TargetOperandInfo &TOI = getDesc().OpInfo[i];
1156 if (TOI.isPredicate())
1157 OS << "pred:";
1158 if (TOI.isOptionalDef())
1159 OS << "opt:";
1160 }
Dan Gohman80f6c582009-11-09 19:38:45 +00001161 MO.print(OS, TM);
1162 }
1163
1164 // Briefly indicate whether any call clobbers were omitted.
1165 if (OmittedAnyCallClobbers) {
Bill Wendling164558e2009-12-25 13:45:50 +00001166 if (!FirstOp) OS << ",";
Dan Gohman80f6c582009-11-09 19:38:45 +00001167 OS << " ...";
Chris Lattner10491642002-10-30 00:48:05 +00001168 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001169
Dan Gohman0ba90f32009-10-31 20:19:03 +00001170 bool HaveSemi = false;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001171 if (!memoperands_empty()) {
Dan Gohman0ba90f32009-10-31 20:19:03 +00001172 if (!HaveSemi) OS << ";"; HaveSemi = true;
1173
1174 OS << " mem:";
Dan Gohmanc76909a2009-09-25 20:36:54 +00001175 for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
1176 i != e; ++i) {
1177 OS << **i;
Dan Gohmancd26ec52009-09-23 01:33:16 +00001178 if (next(i) != e)
1179 OS << " ";
Dan Gohman69de1932008-02-06 22:27:42 +00001180 }
1181 }
1182
Dan Gohman80f6c582009-11-09 19:38:45 +00001183 if (!debugLoc.isUnknown() && MF) {
Bill Wendlingad2cf9d2009-12-25 13:44:36 +00001184 if (!HaveSemi) OS << ";";
Dan Gohman0ba90f32009-10-31 20:19:03 +00001185
1186 // TODO: print InlinedAtLoc information
1187
Devang Patel6b61f582010-01-16 06:09:35 +00001188 DILocation DLT = MF->getDILocation(debugLoc);
1189 DIScope Scope = DLT.getScope();
Dan Gohman75ae5932009-11-23 21:29:08 +00001190 OS << " dbg:";
Dan Gohman4b808b02009-12-05 00:20:51 +00001191 // Omit the directory, since it's usually long and uninteresting.
Dan Gohman261a7d92009-12-01 00:45:56 +00001192 if (!Scope.isNull())
Dan Gohman4b808b02009-12-05 00:20:51 +00001193 OS << Scope.getFilename();
1194 else
1195 OS << "<unknown>";
Devang Patel6b61f582010-01-16 06:09:35 +00001196 OS << ':' << DLT.getLineNumber();
1197 if (DLT.getColumnNumber() != 0)
1198 OS << ':' << DLT.getColumnNumber();
Bill Wendlingb5ef2732009-02-19 21:44:55 +00001199 }
1200
Chris Lattner10491642002-10-30 00:48:05 +00001201 OS << "\n";
1202}
1203
Owen Andersonb487e722008-01-24 01:10:07 +00001204bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001205 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001206 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001207 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001208 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +00001209 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001210 SmallVector<unsigned,4> DeadOps;
Bill Wendling4a23d722008-03-03 22:14:33 +00001211 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1212 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenefb8e3e2009-08-04 20:09:25 +00001213 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001214 continue;
1215 unsigned Reg = MO.getReg();
1216 if (!Reg)
1217 continue;
Bill Wendling4a23d722008-03-03 22:14:33 +00001218
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001219 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001220 if (!Found) {
1221 if (MO.isKill())
1222 // The register is already marked kill.
1223 return true;
Jakob Stoklund Olesenece48182009-08-02 19:13:03 +00001224 if (isPhysReg && isRegTiedToDefOperand(i))
1225 // Two-address uses of physregs must not be marked kill.
1226 return true;
Dan Gohman3f629402008-09-03 15:56:16 +00001227 MO.setIsKill();
1228 Found = true;
1229 }
1230 } else if (hasAliases && MO.isKill() &&
1231 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001232 // A super-register kill already exists.
1233 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001234 return true;
1235 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001236 DeadOps.push_back(i);
Bill Wendling4a23d722008-03-03 22:14:33 +00001237 }
1238 }
1239
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001240 // Trim unneeded kill operands.
1241 while (!DeadOps.empty()) {
1242 unsigned OpIdx = DeadOps.back();
1243 if (getOperand(OpIdx).isImplicit())
1244 RemoveOperand(OpIdx);
1245 else
1246 getOperand(OpIdx).setIsKill(false);
1247 DeadOps.pop_back();
1248 }
1249
Bill Wendling4a23d722008-03-03 22:14:33 +00001250 // If not found, this means an alias of one of the operands is killed. Add a
Owen Andersonb487e722008-01-24 01:10:07 +00001251 // new implicit operand if required.
Dan Gohman3f629402008-09-03 15:56:16 +00001252 if (!Found && AddIfNotFound) {
Bill Wendling4a23d722008-03-03 22:14:33 +00001253 addOperand(MachineOperand::CreateReg(IncomingReg,
1254 false /*IsDef*/,
1255 true /*IsImp*/,
1256 true /*IsKill*/));
Owen Andersonb487e722008-01-24 01:10:07 +00001257 return true;
1258 }
Dan Gohman3f629402008-09-03 15:56:16 +00001259 return Found;
Owen Andersonb487e722008-01-24 01:10:07 +00001260}
1261
1262bool MachineInstr::addRegisterDead(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001263 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001264 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001265 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Evan Cheng01b2e232008-06-27 22:11:49 +00001266 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +00001267 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001268 SmallVector<unsigned,4> DeadOps;
Owen Andersonb487e722008-01-24 01:10:07 +00001269 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1270 MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001271 if (!MO.isReg() || !MO.isDef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001272 continue;
1273 unsigned Reg = MO.getReg();
Dan Gohman3f629402008-09-03 15:56:16 +00001274 if (!Reg)
1275 continue;
1276
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001277 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001278 if (!Found) {
1279 if (MO.isDead())
1280 // The register is already marked dead.
1281 return true;
1282 MO.setIsDead();
1283 Found = true;
1284 }
1285 } else if (hasAliases && MO.isDead() &&
1286 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001287 // There exists a super-register that's marked dead.
1288 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001289 return true;
Owen Anderson22ae9992008-08-14 18:34:18 +00001290 if (RegInfo->getSubRegisters(IncomingReg) &&
1291 RegInfo->getSuperRegisters(Reg) &&
1292 RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001293 DeadOps.push_back(i);
Owen Andersonb487e722008-01-24 01:10:07 +00001294 }
1295 }
1296
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001297 // Trim unneeded dead operands.
1298 while (!DeadOps.empty()) {
1299 unsigned OpIdx = DeadOps.back();
1300 if (getOperand(OpIdx).isImplicit())
1301 RemoveOperand(OpIdx);
1302 else
1303 getOperand(OpIdx).setIsDead(false);
1304 DeadOps.pop_back();
1305 }
1306
Dan Gohman3f629402008-09-03 15:56:16 +00001307 // If not found, this means an alias of one of the operands is dead. Add a
1308 // new implicit operand if required.
Chris Lattner31530612009-06-24 17:54:48 +00001309 if (Found || !AddIfNotFound)
1310 return Found;
1311
1312 addOperand(MachineOperand::CreateReg(IncomingReg,
1313 true /*IsDef*/,
1314 true /*IsImp*/,
1315 false /*IsKill*/,
1316 true /*IsDead*/));
1317 return true;
Owen Andersonb487e722008-01-24 01:10:07 +00001318}
Jakob Stoklund Olesen8efadf92010-01-06 00:29:28 +00001319
1320void MachineInstr::addRegisterDefined(unsigned IncomingReg,
1321 const TargetRegisterInfo *RegInfo) {
1322 MachineOperand *MO = findRegisterDefOperand(IncomingReg, false, RegInfo);
1323 if (!MO || MO->getSubReg())
1324 addOperand(MachineOperand::CreateReg(IncomingReg,
1325 true /*IsDef*/,
1326 true /*IsImp*/));
1327}