blob: ce0c5e6a87c3cd2ebe1e5b830cd4794ce862d67b [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 Lattner72aaa3c2010-03-13 08:14:18 +000018#include "llvm/Metadata.h"
Chris Lattner5e9cd432009-12-28 08:30:43 +000019#include "llvm/Type.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000020#include "llvm/Value.h"
Dan Gohmancd26ec52009-09-23 01:33:16 +000021#include "llvm/Assembly/Writer.h"
Evan Cheng506049f2010-03-03 01:44:33 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner8517e1f2004-02-19 16:17:08 +000023#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000024#include "llvm/CodeGen/MachineMemOperand.h"
Chris Lattner62ed6b92008-01-01 01:12:31 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000026#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner72aaa3c2010-03-13 08:14:18 +000027#include "llvm/MC/MCSymbol.h"
Chris Lattner10491642002-10-30 00:48:05 +000028#include "llvm/Target/TargetMachine.h"
Evan Chengbb81d972008-01-31 09:59:15 +000029#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerf14cf852008-01-07 07:42:25 +000030#include "llvm/Target/TargetInstrDesc.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000031#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmane33f44c2009-10-07 17:38:06 +000032#include "llvm/Analysis/AliasAnalysis.h"
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +000033#include "llvm/Analysis/DebugInfo.h"
David Greene3b325332010-01-04 23:48:20 +000034#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000036#include "llvm/Support/LeakDetector.h"
Dan Gohmance42e402008-07-07 20:32:02 +000037#include "llvm/Support/MathExtras.h"
Chris Lattneredfb72c2008-08-24 20:37:32 +000038#include "llvm/Support/raw_ostream.h"
Dan Gohmanb8d2f552008-08-20 15:58:01 +000039#include "llvm/ADT/FoldingSet.h"
Chris Lattner0742b592004-02-23 18:38:20 +000040using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000041
Chris Lattnerf7382302007-12-30 21:56:09 +000042//===----------------------------------------------------------------------===//
43// MachineOperand Implementation
44//===----------------------------------------------------------------------===//
45
Chris Lattner62ed6b92008-01-01 01:12:31 +000046/// AddRegOperandToRegInfo - Add this register operand to the specified
47/// MachineRegisterInfo. If it is null, then the next/prev fields should be
48/// explicitly nulled out.
49void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) {
Dan Gohmand735b802008-10-03 15:45:36 +000050 assert(isReg() && "Can only add reg operand to use lists");
Chris Lattner62ed6b92008-01-01 01:12:31 +000051
52 // If the reginfo pointer is null, just explicitly null out or next/prev
53 // pointers, to ensure they are not garbage.
54 if (RegInfo == 0) {
55 Contents.Reg.Prev = 0;
56 Contents.Reg.Next = 0;
57 return;
58 }
59
60 // Otherwise, add this operand to the head of the registers use/def list.
Chris Lattner80fe5312008-01-01 21:08:22 +000061 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
Chris Lattner62ed6b92008-01-01 01:12:31 +000062
Chris Lattner80fe5312008-01-01 21:08:22 +000063 // For SSA values, we prefer to keep the definition at the start of the list.
64 // we do this by skipping over the definition if it is at the head of the
65 // list.
66 if (*Head && (*Head)->isDef())
67 Head = &(*Head)->Contents.Reg.Next;
68
69 Contents.Reg.Next = *Head;
Chris Lattner62ed6b92008-01-01 01:12:31 +000070 if (Contents.Reg.Next) {
71 assert(getReg() == Contents.Reg.Next->getReg() &&
72 "Different regs on the same list!");
73 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
74 }
75
Chris Lattner80fe5312008-01-01 21:08:22 +000076 Contents.Reg.Prev = Head;
77 *Head = this;
Chris Lattner62ed6b92008-01-01 01:12:31 +000078}
79
Dan Gohman3bc1a372009-04-15 01:17:37 +000080/// RemoveRegOperandFromRegInfo - Remove this register operand from the
81/// MachineRegisterInfo it is linked with.
82void MachineOperand::RemoveRegOperandFromRegInfo() {
83 assert(isOnRegUseList() && "Reg operand is not on a use list");
84 // Unlink this from the doubly linked list of operands.
85 MachineOperand *NextOp = Contents.Reg.Next;
86 *Contents.Reg.Prev = NextOp;
87 if (NextOp) {
88 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
89 NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
90 }
91 Contents.Reg.Prev = 0;
92 Contents.Reg.Next = 0;
93}
94
Chris Lattner62ed6b92008-01-01 01:12:31 +000095void MachineOperand::setReg(unsigned Reg) {
96 if (getReg() == Reg) return; // No change.
97
98 // Otherwise, we have to change the register. If this operand is embedded
99 // into a machine function, we need to update the old and new register's
100 // use/def lists.
101 if (MachineInstr *MI = getParent())
102 if (MachineBasicBlock *MBB = MI->getParent())
103 if (MachineFunction *MF = MBB->getParent()) {
104 RemoveRegOperandFromRegInfo();
105 Contents.Reg.RegNo = Reg;
106 AddRegOperandToRegInfo(&MF->getRegInfo());
107 return;
108 }
109
110 // Otherwise, just change the register, no problem. :)
111 Contents.Reg.RegNo = Reg;
112}
113
Jakob Stoklund Olesen2da53372010-05-28 18:18:53 +0000114void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx,
115 const TargetRegisterInfo &TRI) {
116 assert(TargetRegisterInfo::isVirtualRegister(Reg));
117 if (SubIdx && getSubReg())
118 SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg());
119 setReg(Reg);
120 setSubReg(SubIdx);
121}
122
123void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) {
124 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
125 if (getSubReg()) {
126 Reg = TRI.getSubReg(Reg, getSubReg());
127 assert(Reg && "Invalid SubReg for physical register");
128 setSubReg(0);
129 }
130 setReg(Reg);
131}
132
Chris Lattner62ed6b92008-01-01 01:12:31 +0000133/// ChangeToImmediate - Replace this operand with a new immediate operand of
134/// the specified value. If an operand is known to be an immediate already,
135/// the setImm method should be used.
136void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
137 // If this operand is currently a register operand, and if this is in a
138 // function, deregister the operand from the register's use/def list.
Dan Gohmand735b802008-10-03 15:45:36 +0000139 if (isReg() && getParent() && getParent()->getParent() &&
Chris Lattner62ed6b92008-01-01 01:12:31 +0000140 getParent()->getParent()->getParent())
141 RemoveRegOperandFromRegInfo();
142
143 OpKind = MO_Immediate;
144 Contents.ImmVal = ImmVal;
145}
146
147/// ChangeToRegister - Replace this operand with a new register operand of
148/// the specified value. If an operand is known to be an register already,
149/// the setReg method should be used.
150void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Dale Johannesen9653f9e2010-02-10 00:41:49 +0000151 bool isKill, bool isDead, bool isUndef,
152 bool isDebug) {
Chris Lattner62ed6b92008-01-01 01:12:31 +0000153 // If this operand is already a register operand, use setReg to update the
154 // register's use/def lists.
Dan Gohmand735b802008-10-03 15:45:36 +0000155 if (isReg()) {
Dale Johannesene0091802008-09-14 01:44:36 +0000156 assert(!isEarlyClobber());
Chris Lattner62ed6b92008-01-01 01:12:31 +0000157 setReg(Reg);
158 } else {
159 // Otherwise, change this to a register and set the reg#.
160 OpKind = MO_Register;
161 Contents.Reg.RegNo = Reg;
162
163 // If this operand is embedded in a function, add the operand to the
164 // register's use/def list.
165 if (MachineInstr *MI = getParent())
166 if (MachineBasicBlock *MBB = MI->getParent())
167 if (MachineFunction *MF = MBB->getParent())
168 AddRegOperandToRegInfo(&MF->getRegInfo());
169 }
170
171 IsDef = isDef;
172 IsImp = isImp;
173 IsKill = isKill;
174 IsDead = isDead;
Evan Cheng4784f1f2009-06-30 08:49:04 +0000175 IsUndef = isUndef;
Dale Johannesene0091802008-09-14 01:44:36 +0000176 IsEarlyClobber = false;
Dale Johannesen9653f9e2010-02-10 00:41:49 +0000177 IsDebug = isDebug;
Chris Lattner62ed6b92008-01-01 01:12:31 +0000178 SubReg = 0;
179}
180
Chris Lattnerf7382302007-12-30 21:56:09 +0000181/// isIdenticalTo - Return true if this operand is identical to the specified
182/// operand.
183bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
Chris Lattner31530612009-06-24 17:54:48 +0000184 if (getType() != Other.getType() ||
185 getTargetFlags() != Other.getTargetFlags())
186 return false;
Chris Lattnerf7382302007-12-30 21:56:09 +0000187
188 switch (getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000189 default: llvm_unreachable("Unrecognized operand type");
Chris Lattnerf7382302007-12-30 21:56:09 +0000190 case MachineOperand::MO_Register:
191 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
192 getSubReg() == Other.getSubReg();
193 case MachineOperand::MO_Immediate:
194 return getImm() == Other.getImm();
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000195 case MachineOperand::MO_FPImmediate:
196 return getFPImm() == Other.getFPImm();
Chris Lattnerf7382302007-12-30 21:56:09 +0000197 case MachineOperand::MO_MachineBasicBlock:
198 return getMBB() == Other.getMBB();
199 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000200 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000201 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000202 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattnerf7382302007-12-30 21:56:09 +0000203 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000204 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000205 case MachineOperand::MO_GlobalAddress:
206 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
207 case MachineOperand::MO_ExternalSymbol:
208 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
209 getOffset() == Other.getOffset();
Dan Gohman8c2b5252009-10-30 01:27:03 +0000210 case MachineOperand::MO_BlockAddress:
211 return getBlockAddress() == Other.getBlockAddress();
Chris Lattner72aaa3c2010-03-13 08:14:18 +0000212 case MachineOperand::MO_MCSymbol:
213 return getMCSymbol() == Other.getMCSymbol();
Chris Lattner24ad3ed2010-04-07 18:03:19 +0000214 case MachineOperand::MO_Metadata:
215 return getMetadata() == Other.getMetadata();
Chris Lattnerf7382302007-12-30 21:56:09 +0000216 }
217}
218
219/// print - Print the specified machine operand.
220///
Mon P Wang5ca6bd12008-10-10 01:43:55 +0000221void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
Dan Gohman80f6c582009-11-09 19:38:45 +0000222 // If the instruction is embedded into a basic block, we can find the
223 // target info for the instruction.
224 if (!TM)
225 if (const MachineInstr *MI = getParent())
226 if (const MachineBasicBlock *MBB = MI->getParent())
227 if (const MachineFunction *MF = MBB->getParent())
228 TM = &MF->getTarget();
229
Chris Lattnerf7382302007-12-30 21:56:09 +0000230 switch (getType()) {
231 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000232 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000233 OS << "%reg" << getReg();
234 } else {
Chris Lattnerf7382302007-12-30 21:56:09 +0000235 if (TM)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000236 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
Chris Lattnerf7382302007-12-30 21:56:09 +0000237 else
Dan Gohman0ba90f32009-10-31 20:19:03 +0000238 OS << "%physreg" << getReg();
Chris Lattnerf7382302007-12-30 21:56:09 +0000239 }
Dan Gohman2ccc8392008-12-18 21:51:27 +0000240
Jakob Stoklund Olesen1fc8e752010-05-25 19:49:38 +0000241 if (getSubReg() != 0) {
242 if (TM)
243 OS << ':' << TM->getRegisterInfo()->getSubRegIndexName(getSubReg());
244 else
245 OS << ':' << getSubReg();
246 }
Dan Gohman2ccc8392008-12-18 21:51:27 +0000247
Evan Cheng4784f1f2009-06-30 08:49:04 +0000248 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
249 isEarlyClobber()) {
Chris Lattner31530612009-06-24 17:54:48 +0000250 OS << '<';
Chris Lattnerf7382302007-12-30 21:56:09 +0000251 bool NeedComma = false;
Evan Cheng07897072009-10-14 23:37:31 +0000252 if (isDef()) {
Chris Lattner31530612009-06-24 17:54:48 +0000253 if (NeedComma) OS << ',';
Dale Johannesen913d3df2008-09-12 17:49:03 +0000254 if (isEarlyClobber())
255 OS << "earlyclobber,";
Evan Cheng07897072009-10-14 23:37:31 +0000256 if (isImplicit())
257 OS << "imp-";
Chris Lattnerf7382302007-12-30 21:56:09 +0000258 OS << "def";
259 NeedComma = true;
Evan Cheng5affca02009-10-21 07:56:02 +0000260 } else if (isImplicit()) {
Evan Cheng07897072009-10-14 23:37:31 +0000261 OS << "imp-use";
Evan Cheng5affca02009-10-21 07:56:02 +0000262 NeedComma = true;
263 }
Evan Cheng07897072009-10-14 23:37:31 +0000264
Evan Cheng4784f1f2009-06-30 08:49:04 +0000265 if (isKill() || isDead() || isUndef()) {
Chris Lattner31530612009-06-24 17:54:48 +0000266 if (NeedComma) OS << ',';
Bill Wendling181eb732008-02-24 00:56:13 +0000267 if (isKill()) OS << "kill";
268 if (isDead()) OS << "dead";
Evan Cheng4784f1f2009-06-30 08:49:04 +0000269 if (isUndef()) {
270 if (isKill() || isDead())
271 OS << ',';
272 OS << "undef";
273 }
Chris Lattnerf7382302007-12-30 21:56:09 +0000274 }
Chris Lattner31530612009-06-24 17:54:48 +0000275 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000276 }
277 break;
278 case MachineOperand::MO_Immediate:
279 OS << getImm();
280 break;
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000281 case MachineOperand::MO_FPImmediate:
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000282 if (getFPImm()->getType()->isFloatTy())
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000283 OS << getFPImm()->getValueAPF().convertToFloat();
Chris Lattner31530612009-06-24 17:54:48 +0000284 else
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000285 OS << getFPImm()->getValueAPF().convertToDouble();
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000286 break;
Chris Lattnerf7382302007-12-30 21:56:09 +0000287 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman0ba90f32009-10-31 20:19:03 +0000288 OS << "<BB#" << getMBB()->getNumber() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000289 break;
290 case MachineOperand::MO_FrameIndex:
Chris Lattner31530612009-06-24 17:54:48 +0000291 OS << "<fi#" << getIndex() << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000292 break;
293 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000294 OS << "<cp#" << getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000295 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000296 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000297 break;
298 case MachineOperand::MO_JumpTableIndex:
Chris Lattner31530612009-06-24 17:54:48 +0000299 OS << "<jt#" << getIndex() << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000300 break;
301 case MachineOperand::MO_GlobalAddress:
Dan Gohman8d4e3b52009-11-06 18:03:10 +0000302 OS << "<ga:";
303 WriteAsOperand(OS, getGlobal(), /*PrintType=*/false);
Chris Lattnerf7382302007-12-30 21:56:09 +0000304 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000305 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000306 break;
307 case MachineOperand::MO_ExternalSymbol:
308 OS << "<es:" << getSymbolName();
309 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000310 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000311 break;
Dan Gohman8c2b5252009-10-30 01:27:03 +0000312 case MachineOperand::MO_BlockAddress:
Dale Johannesen5f72a5e2010-01-13 00:00:24 +0000313 OS << '<';
Dan Gohman0ba90f32009-10-31 20:19:03 +0000314 WriteAsOperand(OS, getBlockAddress(), /*PrintType=*/false);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000315 OS << '>';
316 break;
Dale Johannesen5f72a5e2010-01-13 00:00:24 +0000317 case MachineOperand::MO_Metadata:
318 OS << '<';
319 WriteAsOperand(OS, getMetadata(), /*PrintType=*/false);
320 OS << '>';
321 break;
Chris Lattner72aaa3c2010-03-13 08:14:18 +0000322 case MachineOperand::MO_MCSymbol:
323 OS << "<MCSym=" << *getMCSymbol() << '>';
324 break;
Chris Lattnerf7382302007-12-30 21:56:09 +0000325 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000326 llvm_unreachable("Unrecognized operand type");
Chris Lattnerf7382302007-12-30 21:56:09 +0000327 }
Chris Lattner31530612009-06-24 17:54:48 +0000328
329 if (unsigned TF = getTargetFlags())
330 OS << "[TF=" << TF << ']';
Chris Lattnerf7382302007-12-30 21:56:09 +0000331}
332
333//===----------------------------------------------------------------------===//
Dan Gohmance42e402008-07-07 20:32:02 +0000334// MachineMemOperand Implementation
335//===----------------------------------------------------------------------===//
336
337MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f,
338 int64_t o, uint64_t s, unsigned int a)
339 : Offset(o), Size(s), V(v),
David Greeneba2b2972010-02-15 16:48:31 +0000340 Flags((f & ((1 << MOMaxBits) - 1)) | ((Log2_32(a) + 1) << MOMaxBits)) {
Dan Gohman28f02fd2009-09-21 19:47:04 +0000341 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
Dan Gohmanc5e1f982008-07-16 15:56:42 +0000342 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmance42e402008-07-07 20:32:02 +0000343}
344
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000345/// Profile - Gather unique data for the object.
346///
347void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
348 ID.AddInteger(Offset);
349 ID.AddInteger(Size);
350 ID.AddPointer(V);
351 ID.AddInteger(Flags);
352}
353
Dan Gohmanc76909a2009-09-25 20:36:54 +0000354void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
355 // The Value and Offset may differ due to CSE. But the flags and size
356 // should be the same.
357 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
358 assert(MMO->getSize() == getSize() && "Size mismatch!");
359
360 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
361 // Update the alignment value.
David Greeneba2b2972010-02-15 16:48:31 +0000362 Flags = (Flags & ((1 << MOMaxBits) - 1)) |
363 ((Log2_32(MMO->getBaseAlignment()) + 1) << MOMaxBits);
Dan Gohmanc76909a2009-09-25 20:36:54 +0000364 // Also update the base and offset, because the new alignment may
365 // not be applicable with the old ones.
366 V = MMO->getValue();
367 Offset = MMO->getOffset();
368 }
369}
370
Dan Gohman4b2ebc12009-09-25 23:33:20 +0000371/// getAlignment - Return the minimum known alignment in bytes of the
372/// actual memory reference.
373uint64_t MachineMemOperand::getAlignment() const {
374 return MinAlign(getBaseAlignment(), getOffset());
375}
376
Dan Gohmanc76909a2009-09-25 20:36:54 +0000377raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) {
378 assert((MMO.isLoad() || MMO.isStore()) &&
Dan Gohmancd26ec52009-09-23 01:33:16 +0000379 "SV has to be a load, store or both.");
380
Dan Gohmanc76909a2009-09-25 20:36:54 +0000381 if (MMO.isVolatile())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000382 OS << "Volatile ";
383
Dan Gohmanc76909a2009-09-25 20:36:54 +0000384 if (MMO.isLoad())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000385 OS << "LD";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000386 if (MMO.isStore())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000387 OS << "ST";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000388 OS << MMO.getSize();
Dan Gohmancd26ec52009-09-23 01:33:16 +0000389
390 // Print the address information.
391 OS << "[";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000392 if (!MMO.getValue())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000393 OS << "<unknown>";
394 else
Dan Gohmanc76909a2009-09-25 20:36:54 +0000395 WriteAsOperand(OS, MMO.getValue(), /*PrintType=*/false);
Dan Gohmancd26ec52009-09-23 01:33:16 +0000396
397 // If the alignment of the memory reference itself differs from the alignment
398 // of the base pointer, print the base alignment explicitly, next to the base
399 // pointer.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000400 if (MMO.getBaseAlignment() != MMO.getAlignment())
401 OS << "(align=" << MMO.getBaseAlignment() << ")";
Dan Gohmancd26ec52009-09-23 01:33:16 +0000402
Dan Gohmanc76909a2009-09-25 20:36:54 +0000403 if (MMO.getOffset() != 0)
404 OS << "+" << MMO.getOffset();
Dan Gohmancd26ec52009-09-23 01:33:16 +0000405 OS << "]";
406
407 // Print the alignment of the reference.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000408 if (MMO.getBaseAlignment() != MMO.getAlignment() ||
409 MMO.getBaseAlignment() != MMO.getSize())
410 OS << "(align=" << MMO.getAlignment() << ")";
Dan Gohmancd26ec52009-09-23 01:33:16 +0000411
412 return OS;
413}
414
Dan Gohmance42e402008-07-07 20:32:02 +0000415//===----------------------------------------------------------------------===//
Chris Lattnerf7382302007-12-30 21:56:09 +0000416// MachineInstr Implementation
417//===----------------------------------------------------------------------===//
418
Evan Chengc0f64ff2006-11-27 23:37:22 +0000419/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
Evan Cheng67f660c2006-11-30 07:08:44 +0000420/// TID NULL and no operands.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000421MachineInstr::MachineInstr()
Dan Gohman834651c2009-11-16 22:49:38 +0000422 : TID(0), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000423 Parent(0) {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000424 // Make sure that we get added to a machine basicblock
425 LeakDetector::addGarbageObject(this);
Chris Lattner72791222002-10-28 20:59:49 +0000426}
427
Evan Cheng67f660c2006-11-30 07:08:44 +0000428void MachineInstr::addImplicitDefUseOperands() {
429 if (TID->ImplicitDefs)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000430 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner8019f412007-12-30 00:41:17 +0000431 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng67f660c2006-11-30 07:08:44 +0000432 if (TID->ImplicitUses)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000433 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner8019f412007-12-30 00:41:17 +0000434 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Evan Chengd7de4962006-11-13 23:34:06 +0000435}
436
Bob Wilson0855cad2010-04-09 04:34:03 +0000437/// MachineInstr ctor - This constructor creates a MachineInstr and adds the
438/// implicit operands. It reserves space for the number of operands specified by
439/// the TargetInstrDesc.
Chris Lattner749c6f62008-01-07 07:27:27 +0000440MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
Dan Gohman834651c2009-11-16 22:49:38 +0000441 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0),
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000442 MemRefs(0), MemRefsEnd(0), Parent(0) {
Bob Wilson1793ab92010-04-09 04:46:43 +0000443 if (!NoImp)
444 NumImplicitOps = TID->getNumImplicitDefs() + TID->getNumImplicitUses();
Chris Lattner349c4952008-01-07 03:13:06 +0000445 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Chengfa945722007-10-13 02:23:01 +0000446 if (!NoImp)
447 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000448 // Make sure that we get added to a machine basicblock
449 LeakDetector::addGarbageObject(this);
Evan Chengd7de4962006-11-13 23:34:06 +0000450}
451
Dale Johannesen06efc022009-01-27 23:20:29 +0000452/// MachineInstr ctor - As above, but with a DebugLoc.
453MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl,
454 bool NoImp)
Dan Gohman834651c2009-11-16 22:49:38 +0000455 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000456 Parent(0), debugLoc(dl) {
Bob Wilson1793ab92010-04-09 04:46:43 +0000457 if (!NoImp)
458 NumImplicitOps = TID->getNumImplicitDefs() + TID->getNumImplicitUses();
Dale Johannesen06efc022009-01-27 23:20:29 +0000459 Operands.reserve(NumImplicitOps + TID->getNumOperands());
460 if (!NoImp)
461 addImplicitDefUseOperands();
462 // Make sure that we get added to a machine basicblock
463 LeakDetector::addGarbageObject(this);
464}
465
466/// MachineInstr ctor - Work exactly the same as the ctor two above, except
467/// that the MachineInstr is created and added to the end of the specified
468/// basic block.
Dale Johannesen06efc022009-01-27 23:20:29 +0000469MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid)
Dan Gohman834651c2009-11-16 22:49:38 +0000470 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0),
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000471 MemRefs(0), MemRefsEnd(0), Parent(0) {
Dale Johannesen06efc022009-01-27 23:20:29 +0000472 assert(MBB && "Cannot use inserting ctor with null basic block!");
Bob Wilson1793ab92010-04-09 04:46:43 +0000473 NumImplicitOps = TID->getNumImplicitDefs() + TID->getNumImplicitUses();
Dale Johannesen06efc022009-01-27 23:20:29 +0000474 Operands.reserve(NumImplicitOps + TID->getNumOperands());
475 addImplicitDefUseOperands();
476 // Make sure that we get added to a machine basicblock
477 LeakDetector::addGarbageObject(this);
478 MBB->push_back(this); // Add instruction to end of basic block!
479}
480
481/// MachineInstr ctor - As above, but with a DebugLoc.
482///
483MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl,
Chris Lattner749c6f62008-01-07 07:27:27 +0000484 const TargetInstrDesc &tid)
Dan Gohman834651c2009-11-16 22:49:38 +0000485 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000486 Parent(0), debugLoc(dl) {
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000487 assert(MBB && "Cannot use inserting ctor with null basic block!");
Bob Wilson1793ab92010-04-09 04:46:43 +0000488 NumImplicitOps = TID->getNumImplicitDefs() + TID->getNumImplicitUses();
Chris Lattner349c4952008-01-07 03:13:06 +0000489 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Cheng67f660c2006-11-30 07:08:44 +0000490 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000491 // Make sure that we get added to a machine basicblock
492 LeakDetector::addGarbageObject(this);
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000493 MBB->push_back(this); // Add instruction to end of basic block!
494}
495
Misha Brukmance22e762004-07-09 14:45:17 +0000496/// MachineInstr ctor - Copies MachineInstr arg exactly
497///
Evan Cheng1ed99222008-07-19 00:37:25 +0000498MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Dan Gohman834651c2009-11-16 22:49:38 +0000499 : TID(&MI.getDesc()), NumImplicitOps(0), AsmPrinterFlags(0),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000500 MemRefs(MI.MemRefs), MemRefsEnd(MI.MemRefsEnd),
501 Parent(0), debugLoc(MI.getDebugLoc()) {
Chris Lattner943b5e12006-05-04 19:14:44 +0000502 Operands.reserve(MI.getNumOperands());
Tanya Lattnerb5159ed2004-05-23 20:58:02 +0000503
Misha Brukmance22e762004-07-09 14:45:17 +0000504 // Add operands
Evan Cheng1ed99222008-07-19 00:37:25 +0000505 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
506 addOperand(MI.getOperand(i));
507 NumImplicitOps = MI.NumImplicitOps;
Tanya Lattner0c63e032004-05-24 03:14:18 +0000508
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000509 // Set parent to null.
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000510 Parent = 0;
Dan Gohman6116a732008-07-21 18:47:29 +0000511
512 LeakDetector::addGarbageObject(this);
Tanya Lattner466b5342004-05-23 19:35:12 +0000513}
514
Misha Brukmance22e762004-07-09 14:45:17 +0000515MachineInstr::~MachineInstr() {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000516 LeakDetector::removeGarbageObject(this);
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000517#ifndef NDEBUG
Chris Lattner62ed6b92008-01-01 01:12:31 +0000518 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000519 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Dan Gohmand735b802008-10-03 15:45:36 +0000520 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
Chris Lattner62ed6b92008-01-01 01:12:31 +0000521 "Reg operand def/use list corrupted");
522 }
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000523#endif
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000524}
525
Chris Lattner62ed6b92008-01-01 01:12:31 +0000526/// getRegInfo - If this instruction is embedded into a MachineFunction,
527/// return the MachineRegisterInfo object for the current function, otherwise
528/// return null.
529MachineRegisterInfo *MachineInstr::getRegInfo() {
530 if (MachineBasicBlock *MBB = getParent())
Dan Gohman4e526b92008-07-08 23:59:09 +0000531 return &MBB->getParent()->getRegInfo();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000532 return 0;
533}
534
535/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
536/// this instruction from their respective use lists. This requires that the
537/// operands already be on their use lists.
538void MachineInstr::RemoveRegOperandsFromUseLists() {
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].RemoveRegOperandFromRegInfo();
542 }
543}
544
545/// AddRegOperandsToUseLists - Add all of the register operands in
546/// this instruction from their respective use lists. This requires that the
547/// operands not be on their use lists yet.
548void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
549 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000550 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000551 Operands[i].AddRegOperandToRegInfo(&RegInfo);
552 }
553}
554
555
556/// addOperand - Add the specified operand to the instruction. If it is an
557/// implicit operand, it is added to the end of the operand list. If it is
558/// an explicit operand it is added at the end of the explicit operand list
559/// (before the first implicit operand).
560void MachineInstr::addOperand(const MachineOperand &Op) {
Dan Gohmand735b802008-10-03 15:45:36 +0000561 bool isImpReg = Op.isReg() && Op.isImplicit();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000562 assert((isImpReg || !OperandsComplete()) &&
563 "Trying to add an operand to a machine instr that is already done!");
564
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000565 MachineRegisterInfo *RegInfo = getRegInfo();
566
Chris Lattner62ed6b92008-01-01 01:12:31 +0000567 // If we are adding the operand to the end of the list, our job is simpler.
568 // This is true most of the time, so this is a reasonable optimization.
569 if (isImpReg || NumImplicitOps == 0) {
570 // We can only do this optimization if we know that the operand list won't
571 // reallocate.
572 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
573 Operands.push_back(Op);
574
575 // Set the parent of the operand.
576 Operands.back().ParentMI = this;
577
578 // If the operand is a register, update the operand's use list.
Jim Grosbach06801722009-12-16 19:43:02 +0000579 if (Op.isReg()) {
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000580 Operands.back().AddRegOperandToRegInfo(RegInfo);
Jim Grosbach06801722009-12-16 19:43:02 +0000581 // If the register operand is flagged as early, mark the operand as such
582 unsigned OpNo = Operands.size() - 1;
583 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
584 Operands[OpNo].setIsEarlyClobber(true);
585 }
Chris Lattner62ed6b92008-01-01 01:12:31 +0000586 return;
587 }
588 }
589
590 // Otherwise, we have to insert a real operand before any implicit ones.
591 unsigned OpNo = Operands.size()-NumImplicitOps;
592
Chris Lattner62ed6b92008-01-01 01:12:31 +0000593 // If this instruction isn't embedded into a function, then we don't need to
594 // update any operand lists.
595 if (RegInfo == 0) {
596 // Simple insertion, no reginfo update needed for other register operands.
597 Operands.insert(Operands.begin()+OpNo, Op);
598 Operands[OpNo].ParentMI = this;
599
600 // Do explicitly set the reginfo for this operand though, to ensure the
601 // next/prev fields are properly nulled out.
Jim Grosbach06801722009-12-16 19:43:02 +0000602 if (Operands[OpNo].isReg()) {
Chris Lattner62ed6b92008-01-01 01:12:31 +0000603 Operands[OpNo].AddRegOperandToRegInfo(0);
Jim Grosbach06801722009-12-16 19:43:02 +0000604 // If the register operand is flagged as early, mark the operand as such
605 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
606 Operands[OpNo].setIsEarlyClobber(true);
607 }
Chris Lattner62ed6b92008-01-01 01:12:31 +0000608
609 } else if (Operands.size()+1 <= Operands.capacity()) {
610 // Otherwise, we have to remove register operands from their register use
611 // list, add the operand, then add the register operands back to their use
612 // list. This also must handle the case when the operand list reallocates
613 // to somewhere else.
614
615 // If insertion of this operand won't cause reallocation of the operand
616 // list, just remove the implicit operands, add the operand, then re-add all
617 // the rest of the operands.
618 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000619 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000620 Operands[i].RemoveRegOperandFromRegInfo();
621 }
622
623 // Add the operand. If it is a register, add it to the reg list.
624 Operands.insert(Operands.begin()+OpNo, Op);
625 Operands[OpNo].ParentMI = this;
626
Jim Grosbach06801722009-12-16 19:43:02 +0000627 if (Operands[OpNo].isReg()) {
Chris Lattner62ed6b92008-01-01 01:12:31 +0000628 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
Jim Grosbach06801722009-12-16 19:43:02 +0000629 // If the register operand is flagged as early, mark the operand as such
630 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
631 Operands[OpNo].setIsEarlyClobber(true);
632 }
Chris Lattner62ed6b92008-01-01 01:12:31 +0000633
634 // Re-add all the implicit ops.
635 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000636 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000637 Operands[i].AddRegOperandToRegInfo(RegInfo);
638 }
639 } else {
640 // Otherwise, we will be reallocating the operand list. Remove all reg
641 // operands from their list, then readd them after the operand list is
642 // reallocated.
643 RemoveRegOperandsFromUseLists();
644
645 Operands.insert(Operands.begin()+OpNo, Op);
646 Operands[OpNo].ParentMI = this;
647
648 // Re-add all the operands.
649 AddRegOperandsToUseLists(*RegInfo);
Jim Grosbach06801722009-12-16 19:43:02 +0000650
651 // If the register operand is flagged as early, mark the operand as such
652 if (Operands[OpNo].isReg()
653 && TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
654 Operands[OpNo].setIsEarlyClobber(true);
Chris Lattner62ed6b92008-01-01 01:12:31 +0000655 }
656}
657
658/// RemoveOperand - Erase an operand from an instruction, leaving it with one
659/// fewer operand than it started with.
660///
661void MachineInstr::RemoveOperand(unsigned OpNo) {
662 assert(OpNo < Operands.size() && "Invalid operand number");
663
664 // Special case removing the last one.
665 if (OpNo == Operands.size()-1) {
666 // If needed, remove from the reg def/use list.
Dan Gohmand735b802008-10-03 15:45:36 +0000667 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000668 Operands.back().RemoveRegOperandFromRegInfo();
669
670 Operands.pop_back();
671 return;
672 }
673
674 // Otherwise, we are removing an interior operand. If we have reginfo to
675 // update, remove all operands that will be shifted down from their reg lists,
676 // move everything down, then re-add them.
677 MachineRegisterInfo *RegInfo = getRegInfo();
678 if (RegInfo) {
679 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000680 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000681 Operands[i].RemoveRegOperandFromRegInfo();
682 }
683 }
684
685 Operands.erase(Operands.begin()+OpNo);
686
687 if (RegInfo) {
688 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000689 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000690 Operands[i].AddRegOperandToRegInfo(RegInfo);
691 }
692 }
693}
694
Dan Gohmanc76909a2009-09-25 20:36:54 +0000695/// addMemOperand - Add a MachineMemOperand to the machine instruction.
696/// This function should be used only occasionally. The setMemRefs function
697/// is the primary method for setting up a MachineInstr's MemRefs list.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000698void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohmanc76909a2009-09-25 20:36:54 +0000699 MachineMemOperand *MO) {
700 mmo_iterator OldMemRefs = MemRefs;
701 mmo_iterator OldMemRefsEnd = MemRefsEnd;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000702
Dan Gohmanc76909a2009-09-25 20:36:54 +0000703 size_t NewNum = (MemRefsEnd - MemRefs) + 1;
704 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
705 mmo_iterator NewMemRefsEnd = NewMemRefs + NewNum;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000706
Dan Gohmanc76909a2009-09-25 20:36:54 +0000707 std::copy(OldMemRefs, OldMemRefsEnd, NewMemRefs);
708 NewMemRefs[NewNum - 1] = MO;
709
710 MemRefs = NewMemRefs;
711 MemRefsEnd = NewMemRefsEnd;
712}
Chris Lattner62ed6b92008-01-01 01:12:31 +0000713
Evan Cheng506049f2010-03-03 01:44:33 +0000714bool MachineInstr::isIdenticalTo(const MachineInstr *Other,
715 MICheckType Check) const {
Evan Cheng34cdf6e2010-03-03 21:54:14 +0000716 // If opcodes or number of operands are not the same then the two
717 // instructions are obviously not identical.
718 if (Other->getOpcode() != getOpcode() ||
719 Other->getNumOperands() != getNumOperands())
720 return false;
721
722 // Check operands to make sure they match.
723 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
724 const MachineOperand &MO = getOperand(i);
725 const MachineOperand &OMO = Other->getOperand(i);
726 // Clients may or may not want to ignore defs when testing for equality.
727 // For example, machine CSE pass only cares about finding common
728 // subexpressions, so it's safe to ignore virtual register defs.
729 if (Check != CheckDefs && MO.isReg() && MO.isDef()) {
730 if (Check == IgnoreDefs)
731 continue;
732 // Check == IgnoreVRegDefs
733 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) ||
734 TargetRegisterInfo::isPhysicalRegister(OMO.getReg()))
735 if (MO.getReg() != OMO.getReg())
736 return false;
737 } else if (!MO.isIdenticalTo(OMO))
Evan Cheng506049f2010-03-03 01:44:33 +0000738 return false;
Evan Cheng34cdf6e2010-03-03 21:54:14 +0000739 }
740 return true;
Evan Cheng506049f2010-03-03 01:44:33 +0000741}
742
Chris Lattner48d7c062006-04-17 21:35:41 +0000743/// removeFromParent - This method unlinks 'this' from the containing basic
744/// block, and returns it, but does not delete it.
745MachineInstr *MachineInstr::removeFromParent() {
746 assert(getParent() && "Not embedded in a basic block!");
747 getParent()->remove(this);
748 return this;
749}
750
751
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000752/// eraseFromParent - This method unlinks 'this' from the containing basic
753/// block, and deletes it.
754void MachineInstr::eraseFromParent() {
755 assert(getParent() && "Not embedded in a basic block!");
756 getParent()->erase(this);
757}
758
759
Brian Gaeke21326fc2004-02-13 04:39:32 +0000760/// OperandComplete - Return true if it's illegal to add a new operand
761///
Chris Lattner2a90ba62004-02-12 16:09:53 +0000762bool MachineInstr::OperandsComplete() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000763 unsigned short NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000764 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000765 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000766 return false;
767}
768
Evan Cheng19e3f312007-05-15 01:26:09 +0000769/// getNumExplicitOperands - Returns the number of non-implicit operands.
770///
771unsigned MachineInstr::getNumExplicitOperands() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000772 unsigned NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000773 if (!TID->isVariadic())
Evan Cheng19e3f312007-05-15 01:26:09 +0000774 return NumOperands;
775
Dan Gohman9407cd42009-04-15 17:59:11 +0000776 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
777 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000778 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng19e3f312007-05-15 01:26:09 +0000779 NumOperands++;
780 }
781 return NumOperands;
782}
783
Chris Lattner8ace2cd2006-10-20 22:39:59 +0000784
Evan Chengfaa51072007-04-26 19:00:32 +0000785/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbachf9ca50e2009-09-17 17:57:26 +0000786/// the specific register or -1 if it is not found. It further tightens
Evan Cheng76d7e762007-02-23 01:04:26 +0000787/// the search criteria to a use that kills the register if isKill is true.
Evan Cheng6130f662008-03-05 00:59:57 +0000788int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
789 const TargetRegisterInfo *TRI) const {
Evan Cheng576d1232006-12-06 08:27:42 +0000790 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengf277ee42007-05-29 18:35:22 +0000791 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000792 if (!MO.isReg() || !MO.isUse())
Evan Cheng6130f662008-03-05 00:59:57 +0000793 continue;
794 unsigned MOReg = MO.getReg();
795 if (!MOReg)
796 continue;
797 if (MOReg == Reg ||
798 (TRI &&
799 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
800 TargetRegisterInfo::isPhysicalRegister(Reg) &&
801 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng76d7e762007-02-23 01:04:26 +0000802 if (!isKill || MO.isKill())
Evan Cheng32eb1f12007-03-26 22:37:45 +0000803 return i;
Evan Cheng576d1232006-12-06 08:27:42 +0000804 }
Evan Cheng32eb1f12007-03-26 22:37:45 +0000805 return -1;
Evan Cheng576d1232006-12-06 08:27:42 +0000806}
Jakob Stoklund Olesen7ebc4d62010-05-19 20:36:22 +0000807
Jakob Stoklund Olesen18b2c9d2010-05-21 20:02:01 +0000808/// readsWritesVirtualRegister - Return a pair of bools (reads, writes)
809/// indicating if this instruction reads or writes Reg. This also considers
810/// partial defines.
811std::pair<bool,bool>
812MachineInstr::readsWritesVirtualRegister(unsigned Reg,
813 SmallVectorImpl<unsigned> *Ops) const {
814 bool PartDef = false; // Partial redefine.
815 bool FullDef = false; // Full define.
816 bool Use = false;
Jakob Stoklund Olesen7ebc4d62010-05-19 20:36:22 +0000817
818 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
819 const MachineOperand &MO = getOperand(i);
820 if (!MO.isReg() || MO.getReg() != Reg)
821 continue;
Jakob Stoklund Olesen18b2c9d2010-05-21 20:02:01 +0000822 if (Ops)
823 Ops->push_back(i);
Jakob Stoklund Olesen7ebc4d62010-05-19 20:36:22 +0000824 if (MO.isUse())
Jakob Stoklund Olesen18b2c9d2010-05-21 20:02:01 +0000825 Use |= !MO.isUndef();
826 else if (MO.getSubReg())
Jakob Stoklund Olesen7ebc4d62010-05-19 20:36:22 +0000827 PartDef = true;
828 else
829 FullDef = true;
830 }
Jakob Stoklund Olesen18b2c9d2010-05-21 20:02:01 +0000831 // A partial redefine uses Reg unless there is also a full define.
832 return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef);
Jakob Stoklund Olesen7ebc4d62010-05-19 20:36:22 +0000833}
834
Evan Cheng6130f662008-03-05 00:59:57 +0000835/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman703bfe62008-05-06 00:20:10 +0000836/// the specified register or -1 if it is not found. If isDead is true, defs
837/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
838/// also checks if there is a def of a super-register.
Evan Cheng1015ba72010-05-21 20:53:24 +0000839int
840MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap,
841 const TargetRegisterInfo *TRI) const {
842 bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg);
Evan Chengb371f452007-02-19 21:49:54 +0000843 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng6130f662008-03-05 00:59:57 +0000844 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000845 if (!MO.isReg() || !MO.isDef())
Evan Cheng6130f662008-03-05 00:59:57 +0000846 continue;
847 unsigned MOReg = MO.getReg();
Evan Cheng1015ba72010-05-21 20:53:24 +0000848 bool Found = (MOReg == Reg);
849 if (!Found && TRI && isPhys &&
850 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
851 if (Overlap)
852 Found = TRI->regsOverlap(MOReg, Reg);
853 else
854 Found = TRI->isSubRegister(MOReg, Reg);
855 }
856 if (Found && (!isDead || MO.isDead()))
857 return i;
Evan Chengb371f452007-02-19 21:49:54 +0000858 }
Evan Cheng6130f662008-03-05 00:59:57 +0000859 return -1;
Evan Chengb371f452007-02-19 21:49:54 +0000860}
Evan Cheng19e3f312007-05-15 01:26:09 +0000861
Evan Chengf277ee42007-05-29 18:35:22 +0000862/// findFirstPredOperandIdx() - Find the index of the first operand in the
863/// operand list that is used to represent the predicate. It returns -1 if
864/// none is found.
865int MachineInstr::findFirstPredOperandIdx() const {
Chris Lattner749c6f62008-01-07 07:27:27 +0000866 const TargetInstrDesc &TID = getDesc();
867 if (TID.isPredicable()) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000868 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner749c6f62008-01-07 07:27:27 +0000869 if (TID.OpInfo[i].isPredicate())
Evan Chengf277ee42007-05-29 18:35:22 +0000870 return i;
Evan Cheng19e3f312007-05-15 01:26:09 +0000871 }
872
Evan Chengf277ee42007-05-29 18:35:22 +0000873 return -1;
Evan Cheng19e3f312007-05-15 01:26:09 +0000874}
Evan Chengb371f452007-02-19 21:49:54 +0000875
Bob Wilsond9df5012009-04-09 17:16:43 +0000876/// isRegTiedToUseOperand - Given the index of a register def operand,
877/// check if the register def is tied to a source operand, due to either
878/// two-address elimination or inline assembly constraints. Returns the
879/// first tied use operand index by reference is UseOpIdx is not null.
Jakob Stoklund Olesence9be2c2009-04-29 20:57:16 +0000880bool MachineInstr::
881isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const {
Chris Lattner518bb532010-02-09 19:54:29 +0000882 if (isInlineAsm()) {
Bob Wilsond9df5012009-04-09 17:16:43 +0000883 assert(DefOpIdx >= 2);
884 const MachineOperand &MO = getOperand(DefOpIdx);
Chris Lattnerc30aa7b2009-04-09 23:33:34 +0000885 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
Evan Chengfb112882009-03-23 08:01:15 +0000886 return false;
Evan Chengef5d0702009-06-24 02:05:51 +0000887 // Determine the actual operand index that corresponds to this index.
Evan Chengfb112882009-03-23 08:01:15 +0000888 unsigned DefNo = 0;
Evan Chengef5d0702009-06-24 02:05:51 +0000889 unsigned DefPart = 0;
Evan Chengfb112882009-03-23 08:01:15 +0000890 for (unsigned i = 1, e = getNumOperands(); i < e; ) {
891 const MachineOperand &FMO = getOperand(i);
Jakob Stoklund Olesen45d34fe2009-07-19 19:09:59 +0000892 // After the normal asm operands there may be additional imp-def regs.
893 if (!FMO.isImm())
894 return false;
Evan Chengfb112882009-03-23 08:01:15 +0000895 // Skip over this def.
Evan Chengef5d0702009-06-24 02:05:51 +0000896 unsigned NumOps = InlineAsm::getNumOperandRegisters(FMO.getImm());
897 unsigned PrevDef = i + 1;
898 i = PrevDef + NumOps;
899 if (i > DefOpIdx) {
900 DefPart = DefOpIdx - PrevDef;
Evan Chengfb112882009-03-23 08:01:15 +0000901 break;
Evan Chengef5d0702009-06-24 02:05:51 +0000902 }
Evan Chengfb112882009-03-23 08:01:15 +0000903 ++DefNo;
904 }
Evan Chengef5d0702009-06-24 02:05:51 +0000905 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Evan Chengfb112882009-03-23 08:01:15 +0000906 const MachineOperand &FMO = getOperand(i);
907 if (!FMO.isImm())
908 continue;
909 if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse())
910 continue;
911 unsigned Idx;
Evan Chengef5d0702009-06-24 02:05:51 +0000912 if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000913 Idx == DefNo) {
914 if (UseOpIdx)
Evan Chengef5d0702009-06-24 02:05:51 +0000915 *UseOpIdx = (unsigned)i + 1 + DefPart;
Evan Chengfb112882009-03-23 08:01:15 +0000916 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000917 }
Evan Chengfb112882009-03-23 08:01:15 +0000918 }
Evan Chengef5d0702009-06-24 02:05:51 +0000919 return false;
Evan Chengfb112882009-03-23 08:01:15 +0000920 }
921
Bob Wilsond9df5012009-04-09 17:16:43 +0000922 assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!");
Chris Lattner749c6f62008-01-07 07:27:27 +0000923 const TargetInstrDesc &TID = getDesc();
Evan Chengef0732d2008-07-10 07:35:43 +0000924 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
925 const MachineOperand &MO = getOperand(i);
Dan Gohman2ce7f202008-12-05 05:45:42 +0000926 if (MO.isReg() && MO.isUse() &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000927 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) {
928 if (UseOpIdx)
929 *UseOpIdx = (unsigned)i;
Evan Chengef0732d2008-07-10 07:35:43 +0000930 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000931 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000932 }
933 return false;
934}
935
Evan Chenga24752f2009-03-19 20:30:06 +0000936/// isRegTiedToDefOperand - Return true if the operand of the specified index
937/// is a register use and it is tied to an def operand. It also returns the def
938/// operand index by reference.
Jakob Stoklund Olesence9be2c2009-04-29 20:57:16 +0000939bool MachineInstr::
940isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const {
Chris Lattner518bb532010-02-09 19:54:29 +0000941 if (isInlineAsm()) {
Evan Chengfb112882009-03-23 08:01:15 +0000942 const MachineOperand &MO = getOperand(UseOpIdx);
Chris Lattner0c8382c2009-04-09 16:50:43 +0000943 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0)
Evan Chengfb112882009-03-23 08:01:15 +0000944 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000945
946 // Find the flag operand corresponding to UseOpIdx
947 unsigned FlagIdx, NumOps=0;
948 for (FlagIdx = 1; FlagIdx < UseOpIdx; FlagIdx += NumOps+1) {
949 const MachineOperand &UFMO = getOperand(FlagIdx);
Jakob Stoklund Olesen45d34fe2009-07-19 19:09:59 +0000950 // After the normal asm operands there may be additional imp-def regs.
951 if (!UFMO.isImm())
952 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000953 NumOps = InlineAsm::getNumOperandRegisters(UFMO.getImm());
954 assert(NumOps < getNumOperands() && "Invalid inline asm flag");
955 if (UseOpIdx < FlagIdx+NumOps+1)
956 break;
Evan Chengef5d0702009-06-24 02:05:51 +0000957 }
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000958 if (FlagIdx >= UseOpIdx)
Evan Chengef5d0702009-06-24 02:05:51 +0000959 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000960 const MachineOperand &UFMO = getOperand(FlagIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000961 unsigned DefNo;
962 if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) {
963 if (!DefOpIdx)
964 return true;
965
966 unsigned DefIdx = 1;
967 // Remember to adjust the index. First operand is asm string, then there
968 // is a flag for each.
969 while (DefNo) {
970 const MachineOperand &FMO = getOperand(DefIdx);
971 assert(FMO.isImm());
972 // Skip over this def.
973 DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
974 --DefNo;
975 }
Evan Chengef5d0702009-06-24 02:05:51 +0000976 *DefOpIdx = DefIdx + UseOpIdx - FlagIdx;
Evan Chengfb112882009-03-23 08:01:15 +0000977 return true;
978 }
979 return false;
980 }
981
Evan Chenga24752f2009-03-19 20:30:06 +0000982 const TargetInstrDesc &TID = getDesc();
983 if (UseOpIdx >= TID.getNumOperands())
984 return false;
985 const MachineOperand &MO = getOperand(UseOpIdx);
986 if (!MO.isReg() || !MO.isUse())
987 return false;
988 int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO);
989 if (DefIdx == -1)
990 return false;
991 if (DefOpIdx)
992 *DefOpIdx = (unsigned)DefIdx;
993 return true;
994}
995
Dan Gohmane6cd7572010-05-13 20:34:42 +0000996/// clearKillInfo - Clears kill flags on all operands.
997///
998void MachineInstr::clearKillInfo() {
999 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1000 MachineOperand &MO = getOperand(i);
1001 if (MO.isReg() && MO.isUse())
1002 MO.setIsKill(false);
1003 }
1004}
1005
Evan Cheng576d1232006-12-06 08:27:42 +00001006/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
1007///
1008void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
1009 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1010 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001011 if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
Evan Cheng576d1232006-12-06 08:27:42 +00001012 continue;
1013 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
1014 MachineOperand &MOp = getOperand(j);
1015 if (!MOp.isIdenticalTo(MO))
1016 continue;
1017 if (MO.isKill())
1018 MOp.setIsKill();
1019 else
1020 MOp.setIsDead();
1021 break;
1022 }
1023 }
1024}
1025
Evan Cheng19e3f312007-05-15 01:26:09 +00001026/// copyPredicates - Copies predicate operand(s) from MI.
1027void MachineInstr::copyPredicates(const MachineInstr *MI) {
Chris Lattner749c6f62008-01-07 07:27:27 +00001028 const TargetInstrDesc &TID = MI->getDesc();
Evan Chengb27087f2008-03-13 00:44:09 +00001029 if (!TID.isPredicable())
1030 return;
1031 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1032 if (TID.OpInfo[i].isPredicate()) {
1033 // Predicated operands must be last operands.
1034 addOperand(MI->getOperand(i));
Evan Cheng19e3f312007-05-15 01:26:09 +00001035 }
1036 }
1037}
1038
Evan Cheng9f1c8312008-07-03 09:09:37 +00001039/// isSafeToMove - Return true if it is safe to move this instruction. If
1040/// SawStore is set to true, it means that there is a store (or call) between
1041/// the instruction's location and its intended destination.
Dan Gohmanb3b930a2008-11-18 19:04:29 +00001042bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
Evan Chengac1abde2010-03-02 19:03:01 +00001043 AliasAnalysis *AA,
1044 bool &SawStore) const {
Evan Chengb27087f2008-03-13 00:44:09 +00001045 // Ignore stuff that we obviously can't move.
1046 if (TID->mayStore() || TID->isCall()) {
1047 SawStore = true;
1048 return false;
1049 }
Dan Gohman237dee12008-12-23 17:28:50 +00001050 if (TID->isTerminator() || TID->hasUnmodeledSideEffects())
Evan Chengb27087f2008-03-13 00:44:09 +00001051 return false;
1052
1053 // See if this instruction does a load. If so, we have to guarantee that the
1054 // loaded value doesn't change between the load and the its intended
1055 // destination. The check for isInvariantLoad gives the targe the chance to
1056 // classify the load as always returning a constant, e.g. a constant pool
1057 // load.
Dan Gohmana70dca12009-10-09 23:27:56 +00001058 if (TID->mayLoad() && !isInvariantLoad(AA))
Evan Chengb27087f2008-03-13 00:44:09 +00001059 // Otherwise, this is a real load. If there is a store between the load and
Evan Cheng7cc2c402009-07-28 21:49:18 +00001060 // end of block, or if the load is volatile, we can't move it.
Dan Gohmand790a5c2008-10-02 15:04:30 +00001061 return !SawStore && !hasVolatileMemoryRef();
Dan Gohman3e4fb702008-09-24 00:06:15 +00001062
Evan Chengb27087f2008-03-13 00:44:09 +00001063 return true;
1064}
1065
Evan Chengdf3b9932008-08-27 20:33:50 +00001066/// isSafeToReMat - Return true if it's safe to rematerialize the specified
1067/// instruction which defined the specified register instead of copying it.
Dan Gohmanb3b930a2008-11-18 19:04:29 +00001068bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII,
Evan Chengac1abde2010-03-02 19:03:01 +00001069 AliasAnalysis *AA,
1070 unsigned DstReg) const {
Evan Chengdf3b9932008-08-27 20:33:50 +00001071 bool SawStore = false;
Dan Gohmana70dca12009-10-09 23:27:56 +00001072 if (!TII->isTriviallyReMaterializable(this, AA) ||
Evan Chengac1abde2010-03-02 19:03:01 +00001073 !isSafeToMove(TII, AA, SawStore))
Evan Chengdf3b9932008-08-27 20:33:50 +00001074 return false;
1075 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohmancbad42c2008-11-18 19:49:32 +00001076 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001077 if (!MO.isReg())
Evan Chengdf3b9932008-08-27 20:33:50 +00001078 continue;
1079 // FIXME: For now, do not remat any instruction with register operands.
1080 // Later on, we can loosen the restriction is the register operands have
1081 // not been modified between the def and use. Note, this is different from
Evan Cheng8763c1c2008-08-27 20:58:54 +00001082 // MachineSink because the code is no longer in two-address form (at least
Evan Chengdf3b9932008-08-27 20:33:50 +00001083 // partially).
1084 if (MO.isUse())
1085 return false;
1086 else if (!MO.isDead() && MO.getReg() != DstReg)
1087 return false;
1088 }
1089 return true;
1090}
1091
Dan Gohman3e4fb702008-09-24 00:06:15 +00001092/// hasVolatileMemoryRef - Return true if this instruction may have a
1093/// volatile memory reference, or if the information describing the
1094/// memory reference is not available. Return false if it is known to
1095/// have no volatile memory references.
1096bool MachineInstr::hasVolatileMemoryRef() const {
1097 // An instruction known never to access memory won't have a volatile access.
1098 if (!TID->mayStore() &&
1099 !TID->mayLoad() &&
1100 !TID->isCall() &&
1101 !TID->hasUnmodeledSideEffects())
1102 return false;
1103
1104 // Otherwise, if the instruction has no memory reference information,
1105 // conservatively assume it wasn't preserved.
1106 if (memoperands_empty())
1107 return true;
1108
1109 // Check the memory reference information for volatile references.
Dan Gohmanc76909a2009-09-25 20:36:54 +00001110 for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I)
1111 if ((*I)->isVolatile())
Dan Gohman3e4fb702008-09-24 00:06:15 +00001112 return true;
1113
1114 return false;
1115}
1116
Dan Gohmane33f44c2009-10-07 17:38:06 +00001117/// isInvariantLoad - Return true if this instruction is loading from a
1118/// location whose value is invariant across the function. For example,
Dan Gohmanf451cb82010-02-10 16:03:48 +00001119/// loading a value from the constant pool or from the argument area
Dan Gohmane33f44c2009-10-07 17:38:06 +00001120/// of a function if it does not change. This should only return true of
1121/// *all* loads the instruction does are invariant (if it does multiple loads).
1122bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const {
1123 // If the instruction doesn't load at all, it isn't an invariant load.
1124 if (!TID->mayLoad())
1125 return false;
1126
1127 // If the instruction has lost its memoperands, conservatively assume that
1128 // it may not be an invariant load.
1129 if (memoperands_empty())
1130 return false;
1131
1132 const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo();
1133
1134 for (mmo_iterator I = memoperands_begin(),
1135 E = memoperands_end(); I != E; ++I) {
1136 if ((*I)->isVolatile()) return false;
1137 if ((*I)->isStore()) return false;
1138
1139 if (const Value *V = (*I)->getValue()) {
1140 // A load from a constant PseudoSourceValue is invariant.
1141 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V))
1142 if (PSV->isConstant(MFI))
1143 continue;
1144 // If we have an AliasAnalysis, ask it whether the memory is constant.
1145 if (AA && AA->pointsToConstantMemory(V))
1146 continue;
1147 }
1148
1149 // Otherwise assume conservatively.
1150 return false;
1151 }
1152
1153 // Everything checks out.
1154 return true;
1155}
1156
Evan Cheng229694f2009-12-03 02:31:43 +00001157/// isConstantValuePHI - If the specified instruction is a PHI that always
1158/// merges together the same virtual register, return the register, otherwise
1159/// return 0.
1160unsigned MachineInstr::isConstantValuePHI() const {
Chris Lattner518bb532010-02-09 19:54:29 +00001161 if (!isPHI())
Evan Cheng229694f2009-12-03 02:31:43 +00001162 return 0;
Evan Chengd8f079c2009-12-07 23:10:34 +00001163 assert(getNumOperands() >= 3 &&
1164 "It's illegal to have a PHI without source operands");
Evan Cheng229694f2009-12-03 02:31:43 +00001165
1166 unsigned Reg = getOperand(1).getReg();
1167 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
1168 if (getOperand(i).getReg() != Reg)
1169 return 0;
1170 return Reg;
1171}
1172
Evan Chenga57fabe2010-04-08 20:02:37 +00001173/// allDefsAreDead - Return true if all the defs of this instruction are dead.
1174///
1175bool MachineInstr::allDefsAreDead() const {
1176 for (unsigned i = 0, e = getNumOperands(); i < e; ++i) {
1177 const MachineOperand &MO = getOperand(i);
1178 if (!MO.isReg() || MO.isUse())
1179 continue;
1180 if (!MO.isDead())
1181 return false;
1182 }
1183 return true;
1184}
1185
Brian Gaeke21326fc2004-02-13 04:39:32 +00001186void MachineInstr::dump() const {
David Greene3b325332010-01-04 23:48:20 +00001187 dbgs() << " " << *this;
Mon P Wang5ca6bd12008-10-10 01:43:55 +00001188}
1189
1190void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const {
Dan Gohman80f6c582009-11-09 19:38:45 +00001191 // We can be a bit tidier if we know the TargetMachine and/or MachineFunction.
1192 const MachineFunction *MF = 0;
1193 if (const MachineBasicBlock *MBB = getParent()) {
1194 MF = MBB->getParent();
1195 if (!TM && MF)
1196 TM = &MF->getTarget();
1197 }
Dan Gohman0ba90f32009-10-31 20:19:03 +00001198
1199 // Print explicitly defined operands on the left of an assignment syntax.
Dan Gohman80f6c582009-11-09 19:38:45 +00001200 unsigned StartOp = 0, e = getNumOperands();
Dan Gohman0ba90f32009-10-31 20:19:03 +00001201 for (; StartOp < e && getOperand(StartOp).isReg() &&
1202 getOperand(StartOp).isDef() &&
1203 !getOperand(StartOp).isImplicit();
1204 ++StartOp) {
1205 if (StartOp != 0) OS << ", ";
1206 getOperand(StartOp).print(OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +00001207 }
Tanya Lattnerb1407622004-06-25 00:13:11 +00001208
Dan Gohman0ba90f32009-10-31 20:19:03 +00001209 if (StartOp != 0)
1210 OS << " = ";
1211
1212 // Print the opcode name.
Chris Lattner749c6f62008-01-07 07:27:27 +00001213 OS << getDesc().getName();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001214
Dan Gohman0ba90f32009-10-31 20:19:03 +00001215 // Print the rest of the operands.
Dan Gohman80f6c582009-11-09 19:38:45 +00001216 bool OmittedAnyCallClobbers = false;
1217 bool FirstOp = true;
Chris Lattner6a592272002-10-30 01:55:38 +00001218 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohman80f6c582009-11-09 19:38:45 +00001219 const MachineOperand &MO = getOperand(i);
1220
1221 // Omit call-clobbered registers which aren't used anywhere. This makes
1222 // call instructions much less noisy on targets where calls clobber lots
1223 // of registers. Don't rely on MO.isDead() because we may be called before
1224 // LiveVariables is run, or we may be looking at a non-allocatable reg.
1225 if (MF && getDesc().isCall() &&
1226 MO.isReg() && MO.isImplicit() && MO.isDef()) {
1227 unsigned Reg = MO.getReg();
1228 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) {
1229 const MachineRegisterInfo &MRI = MF->getRegInfo();
1230 if (MRI.use_empty(Reg) && !MRI.isLiveOut(Reg)) {
1231 bool HasAliasLive = false;
1232 for (const unsigned *Alias = TM->getRegisterInfo()->getAliasSet(Reg);
1233 unsigned AliasReg = *Alias; ++Alias)
1234 if (!MRI.use_empty(AliasReg) || MRI.isLiveOut(AliasReg)) {
1235 HasAliasLive = true;
1236 break;
1237 }
1238 if (!HasAliasLive) {
1239 OmittedAnyCallClobbers = true;
1240 continue;
1241 }
1242 }
1243 }
1244 }
1245
1246 if (FirstOp) FirstOp = false; else OS << ",";
Chris Lattner6a592272002-10-30 01:55:38 +00001247 OS << " ";
Jakob Stoklund Olesenb1bb4af2010-01-19 22:08:34 +00001248 if (i < getDesc().NumOperands) {
1249 const TargetOperandInfo &TOI = getDesc().OpInfo[i];
1250 if (TOI.isPredicate())
1251 OS << "pred:";
1252 if (TOI.isOptionalDef())
1253 OS << "opt:";
1254 }
Evan Cheng59b36552010-04-28 20:03:13 +00001255 if (isDebugValue() && MO.isMetadata()) {
1256 // Pretty print DBG_VALUE instructions.
1257 const MDNode *MD = MO.getMetadata();
1258 if (const MDString *MDS = dyn_cast<MDString>(MD->getOperand(2)))
1259 OS << "!\"" << MDS->getString() << '\"';
1260 else
1261 MO.print(OS, TM);
1262 } else
1263 MO.print(OS, TM);
Dan Gohman80f6c582009-11-09 19:38:45 +00001264 }
1265
1266 // Briefly indicate whether any call clobbers were omitted.
1267 if (OmittedAnyCallClobbers) {
Bill Wendling164558e2009-12-25 13:45:50 +00001268 if (!FirstOp) OS << ",";
Dan Gohman80f6c582009-11-09 19:38:45 +00001269 OS << " ...";
Chris Lattner10491642002-10-30 00:48:05 +00001270 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001271
Dan Gohman0ba90f32009-10-31 20:19:03 +00001272 bool HaveSemi = false;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001273 if (!memoperands_empty()) {
Dan Gohman0ba90f32009-10-31 20:19:03 +00001274 if (!HaveSemi) OS << ";"; HaveSemi = true;
1275
1276 OS << " mem:";
Dan Gohmanc76909a2009-09-25 20:36:54 +00001277 for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
1278 i != e; ++i) {
1279 OS << **i;
Dan Gohmancd26ec52009-09-23 01:33:16 +00001280 if (next(i) != e)
1281 OS << " ";
Dan Gohman69de1932008-02-06 22:27:42 +00001282 }
1283 }
1284
Dan Gohman80f6c582009-11-09 19:38:45 +00001285 if (!debugLoc.isUnknown() && MF) {
Bill Wendlingad2cf9d2009-12-25 13:44:36 +00001286 if (!HaveSemi) OS << ";";
Dan Gohman0ba90f32009-10-31 20:19:03 +00001287
1288 // TODO: print InlinedAtLoc information
1289
Chris Lattnerde4845c2010-04-02 19:42:39 +00001290 DIScope Scope(debugLoc.getScope(MF->getFunction()->getContext()));
Dan Gohman75ae5932009-11-23 21:29:08 +00001291 OS << " dbg:";
Dan Gohman4b808b02009-12-05 00:20:51 +00001292 // Omit the directory, since it's usually long and uninteresting.
Devang Patel3c91b052010-03-08 20:52:55 +00001293 if (Scope.Verify())
Dan Gohman4b808b02009-12-05 00:20:51 +00001294 OS << Scope.getFilename();
1295 else
1296 OS << "<unknown>";
Chris Lattnerde4845c2010-04-02 19:42:39 +00001297 OS << ':' << debugLoc.getLine();
1298 if (debugLoc.getCol() != 0)
1299 OS << ':' << debugLoc.getCol();
Bill Wendlingb5ef2732009-02-19 21:44:55 +00001300 }
1301
Chris Lattner10491642002-10-30 00:48:05 +00001302 OS << "\n";
1303}
1304
Owen Andersonb487e722008-01-24 01:10:07 +00001305bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001306 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001307 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001308 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001309 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +00001310 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001311 SmallVector<unsigned,4> DeadOps;
Bill Wendling4a23d722008-03-03 22:14:33 +00001312 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1313 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenefb8e3e2009-08-04 20:09:25 +00001314 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001315 continue;
1316 unsigned Reg = MO.getReg();
1317 if (!Reg)
1318 continue;
Bill Wendling4a23d722008-03-03 22:14:33 +00001319
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001320 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001321 if (!Found) {
1322 if (MO.isKill())
1323 // The register is already marked kill.
1324 return true;
Jakob Stoklund Olesenece48182009-08-02 19:13:03 +00001325 if (isPhysReg && isRegTiedToDefOperand(i))
1326 // Two-address uses of physregs must not be marked kill.
1327 return true;
Dan Gohman3f629402008-09-03 15:56:16 +00001328 MO.setIsKill();
1329 Found = true;
1330 }
1331 } else if (hasAliases && MO.isKill() &&
1332 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001333 // A super-register kill already exists.
1334 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001335 return true;
1336 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001337 DeadOps.push_back(i);
Bill Wendling4a23d722008-03-03 22:14:33 +00001338 }
1339 }
1340
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001341 // Trim unneeded kill operands.
1342 while (!DeadOps.empty()) {
1343 unsigned OpIdx = DeadOps.back();
1344 if (getOperand(OpIdx).isImplicit())
1345 RemoveOperand(OpIdx);
1346 else
1347 getOperand(OpIdx).setIsKill(false);
1348 DeadOps.pop_back();
1349 }
1350
Bill Wendling4a23d722008-03-03 22:14:33 +00001351 // If not found, this means an alias of one of the operands is killed. Add a
Owen Andersonb487e722008-01-24 01:10:07 +00001352 // new implicit operand if required.
Dan Gohman3f629402008-09-03 15:56:16 +00001353 if (!Found && AddIfNotFound) {
Bill Wendling4a23d722008-03-03 22:14:33 +00001354 addOperand(MachineOperand::CreateReg(IncomingReg,
1355 false /*IsDef*/,
1356 true /*IsImp*/,
1357 true /*IsKill*/));
Owen Andersonb487e722008-01-24 01:10:07 +00001358 return true;
1359 }
Dan Gohman3f629402008-09-03 15:56:16 +00001360 return Found;
Owen Andersonb487e722008-01-24 01:10:07 +00001361}
1362
1363bool MachineInstr::addRegisterDead(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001364 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001365 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001366 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Evan Cheng01b2e232008-06-27 22:11:49 +00001367 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +00001368 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001369 SmallVector<unsigned,4> DeadOps;
Owen Andersonb487e722008-01-24 01:10:07 +00001370 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1371 MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001372 if (!MO.isReg() || !MO.isDef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001373 continue;
1374 unsigned Reg = MO.getReg();
Dan Gohman3f629402008-09-03 15:56:16 +00001375 if (!Reg)
1376 continue;
1377
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001378 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001379 if (!Found) {
1380 if (MO.isDead())
1381 // The register is already marked dead.
1382 return true;
1383 MO.setIsDead();
1384 Found = true;
1385 }
1386 } else if (hasAliases && MO.isDead() &&
1387 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001388 // There exists a super-register that's marked dead.
1389 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001390 return true;
Owen Anderson22ae9992008-08-14 18:34:18 +00001391 if (RegInfo->getSubRegisters(IncomingReg) &&
1392 RegInfo->getSuperRegisters(Reg) &&
1393 RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001394 DeadOps.push_back(i);
Owen Andersonb487e722008-01-24 01:10:07 +00001395 }
1396 }
1397
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001398 // Trim unneeded dead operands.
1399 while (!DeadOps.empty()) {
1400 unsigned OpIdx = DeadOps.back();
1401 if (getOperand(OpIdx).isImplicit())
1402 RemoveOperand(OpIdx);
1403 else
1404 getOperand(OpIdx).setIsDead(false);
1405 DeadOps.pop_back();
1406 }
1407
Dan Gohman3f629402008-09-03 15:56:16 +00001408 // If not found, this means an alias of one of the operands is dead. Add a
1409 // new implicit operand if required.
Chris Lattner31530612009-06-24 17:54:48 +00001410 if (Found || !AddIfNotFound)
1411 return Found;
1412
1413 addOperand(MachineOperand::CreateReg(IncomingReg,
1414 true /*IsDef*/,
1415 true /*IsImp*/,
1416 false /*IsKill*/,
1417 true /*IsDead*/));
1418 return true;
Owen Andersonb487e722008-01-24 01:10:07 +00001419}
Jakob Stoklund Olesen8efadf92010-01-06 00:29:28 +00001420
1421void MachineInstr::addRegisterDefined(unsigned IncomingReg,
1422 const TargetRegisterInfo *RegInfo) {
Jakob Stoklund Olesen63e6a482010-05-21 16:32:16 +00001423 if (TargetRegisterInfo::isPhysicalRegister(IncomingReg)) {
1424 MachineOperand *MO = findRegisterDefOperand(IncomingReg, false, RegInfo);
1425 if (MO)
1426 return;
1427 } else {
1428 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1429 const MachineOperand &MO = getOperand(i);
1430 if (MO.isReg() && MO.getReg() == IncomingReg && MO.isDef() &&
1431 MO.getSubReg() == 0)
1432 return;
1433 }
1434 }
1435 addOperand(MachineOperand::CreateReg(IncomingReg,
1436 true /*IsDef*/,
1437 true /*IsImp*/));
Jakob Stoklund Olesen8efadf92010-01-06 00:29:28 +00001438}
Evan Cheng67eaa082010-03-03 23:37:30 +00001439
1440unsigned
1441MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) {
1442 unsigned Hash = MI->getOpcode() * 37;
1443 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1444 const MachineOperand &MO = MI->getOperand(i);
1445 uint64_t Key = (uint64_t)MO.getType() << 32;
1446 switch (MO.getType()) {
Chris Lattner72aaa3c2010-03-13 08:14:18 +00001447 default: break;
1448 case MachineOperand::MO_Register:
1449 if (MO.isDef() && MO.getReg() &&
1450 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
1451 continue; // Skip virtual register defs.
1452 Key |= MO.getReg();
1453 break;
1454 case MachineOperand::MO_Immediate:
1455 Key |= MO.getImm();
1456 break;
1457 case MachineOperand::MO_FrameIndex:
1458 case MachineOperand::MO_ConstantPoolIndex:
1459 case MachineOperand::MO_JumpTableIndex:
1460 Key |= MO.getIndex();
1461 break;
1462 case MachineOperand::MO_MachineBasicBlock:
1463 Key |= DenseMapInfo<void*>::getHashValue(MO.getMBB());
1464 break;
1465 case MachineOperand::MO_GlobalAddress:
1466 Key |= DenseMapInfo<void*>::getHashValue(MO.getGlobal());
1467 break;
1468 case MachineOperand::MO_BlockAddress:
1469 Key |= DenseMapInfo<void*>::getHashValue(MO.getBlockAddress());
1470 break;
1471 case MachineOperand::MO_MCSymbol:
1472 Key |= DenseMapInfo<void*>::getHashValue(MO.getMCSymbol());
1473 break;
Evan Cheng67eaa082010-03-03 23:37:30 +00001474 }
1475 Key += ~(Key << 32);
1476 Key ^= (Key >> 22);
1477 Key += ~(Key << 13);
1478 Key ^= (Key >> 8);
1479 Key += (Key << 3);
1480 Key ^= (Key >> 15);
1481 Key += ~(Key << 27);
1482 Key ^= (Key >> 31);
1483 Hash = (unsigned)Key + Hash * 37;
1484 }
1485 return Hash;
1486}