blob: e54cd5cf9492c73dc7e188c231dd17dfb475df56 [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
114/// ChangeToImmediate - Replace this operand with a new immediate operand of
115/// the specified value. If an operand is known to be an immediate already,
116/// the setImm method should be used.
117void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
118 // If this operand is currently a register operand, and if this is in a
119 // function, deregister the operand from the register's use/def list.
Dan Gohmand735b802008-10-03 15:45:36 +0000120 if (isReg() && getParent() && getParent()->getParent() &&
Chris Lattner62ed6b92008-01-01 01:12:31 +0000121 getParent()->getParent()->getParent())
122 RemoveRegOperandFromRegInfo();
123
124 OpKind = MO_Immediate;
125 Contents.ImmVal = ImmVal;
126}
127
128/// ChangeToRegister - Replace this operand with a new register operand of
129/// the specified value. If an operand is known to be an register already,
130/// the setReg method should be used.
131void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Dale Johannesen9653f9e2010-02-10 00:41:49 +0000132 bool isKill, bool isDead, bool isUndef,
133 bool isDebug) {
Chris Lattner62ed6b92008-01-01 01:12:31 +0000134 // If this operand is already a register operand, use setReg to update the
135 // register's use/def lists.
Dan Gohmand735b802008-10-03 15:45:36 +0000136 if (isReg()) {
Dale Johannesene0091802008-09-14 01:44:36 +0000137 assert(!isEarlyClobber());
Chris Lattner62ed6b92008-01-01 01:12:31 +0000138 setReg(Reg);
139 } else {
140 // Otherwise, change this to a register and set the reg#.
141 OpKind = MO_Register;
142 Contents.Reg.RegNo = Reg;
143
144 // If this operand is embedded in a function, add the operand to the
145 // register's use/def list.
146 if (MachineInstr *MI = getParent())
147 if (MachineBasicBlock *MBB = MI->getParent())
148 if (MachineFunction *MF = MBB->getParent())
149 AddRegOperandToRegInfo(&MF->getRegInfo());
150 }
151
152 IsDef = isDef;
153 IsImp = isImp;
154 IsKill = isKill;
155 IsDead = isDead;
Evan Cheng4784f1f2009-06-30 08:49:04 +0000156 IsUndef = isUndef;
Dale Johannesene0091802008-09-14 01:44:36 +0000157 IsEarlyClobber = false;
Dale Johannesen9653f9e2010-02-10 00:41:49 +0000158 IsDebug = isDebug;
Chris Lattner62ed6b92008-01-01 01:12:31 +0000159 SubReg = 0;
160}
161
Chris Lattnerf7382302007-12-30 21:56:09 +0000162/// isIdenticalTo - Return true if this operand is identical to the specified
163/// operand.
164bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
Chris Lattner31530612009-06-24 17:54:48 +0000165 if (getType() != Other.getType() ||
166 getTargetFlags() != Other.getTargetFlags())
167 return false;
Chris Lattnerf7382302007-12-30 21:56:09 +0000168
169 switch (getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000170 default: llvm_unreachable("Unrecognized operand type");
Chris Lattnerf7382302007-12-30 21:56:09 +0000171 case MachineOperand::MO_Register:
172 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
173 getSubReg() == Other.getSubReg();
174 case MachineOperand::MO_Immediate:
175 return getImm() == Other.getImm();
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000176 case MachineOperand::MO_FPImmediate:
177 return getFPImm() == Other.getFPImm();
Chris Lattnerf7382302007-12-30 21:56:09 +0000178 case MachineOperand::MO_MachineBasicBlock:
179 return getMBB() == Other.getMBB();
180 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000181 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000182 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000183 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattnerf7382302007-12-30 21:56:09 +0000184 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000185 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000186 case MachineOperand::MO_GlobalAddress:
187 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
188 case MachineOperand::MO_ExternalSymbol:
189 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
190 getOffset() == Other.getOffset();
Dan Gohman8c2b5252009-10-30 01:27:03 +0000191 case MachineOperand::MO_BlockAddress:
192 return getBlockAddress() == Other.getBlockAddress();
Chris Lattner72aaa3c2010-03-13 08:14:18 +0000193 case MachineOperand::MO_MCSymbol:
194 return getMCSymbol() == Other.getMCSymbol();
Chris Lattner24ad3ed2010-04-07 18:03:19 +0000195 case MachineOperand::MO_Metadata:
196 return getMetadata() == Other.getMetadata();
Chris Lattnerf7382302007-12-30 21:56:09 +0000197 }
198}
199
200/// print - Print the specified machine operand.
201///
Mon P Wang5ca6bd12008-10-10 01:43:55 +0000202void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
Dan Gohman80f6c582009-11-09 19:38:45 +0000203 // If the instruction is embedded into a basic block, we can find the
204 // target info for the instruction.
205 if (!TM)
206 if (const MachineInstr *MI = getParent())
207 if (const MachineBasicBlock *MBB = MI->getParent())
208 if (const MachineFunction *MF = MBB->getParent())
209 TM = &MF->getTarget();
210
Chris Lattnerf7382302007-12-30 21:56:09 +0000211 switch (getType()) {
212 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000213 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000214 OS << "%reg" << getReg();
215 } else {
Chris Lattnerf7382302007-12-30 21:56:09 +0000216 if (TM)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000217 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
Chris Lattnerf7382302007-12-30 21:56:09 +0000218 else
Dan Gohman0ba90f32009-10-31 20:19:03 +0000219 OS << "%physreg" << getReg();
Chris Lattnerf7382302007-12-30 21:56:09 +0000220 }
Dan Gohman2ccc8392008-12-18 21:51:27 +0000221
Jakob Stoklund Olesen1fc8e752010-05-25 19:49:38 +0000222 if (getSubReg() != 0) {
223 if (TM)
224 OS << ':' << TM->getRegisterInfo()->getSubRegIndexName(getSubReg());
225 else
226 OS << ':' << getSubReg();
227 }
Dan Gohman2ccc8392008-12-18 21:51:27 +0000228
Evan Cheng4784f1f2009-06-30 08:49:04 +0000229 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
230 isEarlyClobber()) {
Chris Lattner31530612009-06-24 17:54:48 +0000231 OS << '<';
Chris Lattnerf7382302007-12-30 21:56:09 +0000232 bool NeedComma = false;
Evan Cheng07897072009-10-14 23:37:31 +0000233 if (isDef()) {
Chris Lattner31530612009-06-24 17:54:48 +0000234 if (NeedComma) OS << ',';
Dale Johannesen913d3df2008-09-12 17:49:03 +0000235 if (isEarlyClobber())
236 OS << "earlyclobber,";
Evan Cheng07897072009-10-14 23:37:31 +0000237 if (isImplicit())
238 OS << "imp-";
Chris Lattnerf7382302007-12-30 21:56:09 +0000239 OS << "def";
240 NeedComma = true;
Evan Cheng5affca02009-10-21 07:56:02 +0000241 } else if (isImplicit()) {
Evan Cheng07897072009-10-14 23:37:31 +0000242 OS << "imp-use";
Evan Cheng5affca02009-10-21 07:56:02 +0000243 NeedComma = true;
244 }
Evan Cheng07897072009-10-14 23:37:31 +0000245
Evan Cheng4784f1f2009-06-30 08:49:04 +0000246 if (isKill() || isDead() || isUndef()) {
Chris Lattner31530612009-06-24 17:54:48 +0000247 if (NeedComma) OS << ',';
Bill Wendling181eb732008-02-24 00:56:13 +0000248 if (isKill()) OS << "kill";
249 if (isDead()) OS << "dead";
Evan Cheng4784f1f2009-06-30 08:49:04 +0000250 if (isUndef()) {
251 if (isKill() || isDead())
252 OS << ',';
253 OS << "undef";
254 }
Chris Lattnerf7382302007-12-30 21:56:09 +0000255 }
Chris Lattner31530612009-06-24 17:54:48 +0000256 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000257 }
258 break;
259 case MachineOperand::MO_Immediate:
260 OS << getImm();
261 break;
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000262 case MachineOperand::MO_FPImmediate:
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000263 if (getFPImm()->getType()->isFloatTy())
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000264 OS << getFPImm()->getValueAPF().convertToFloat();
Chris Lattner31530612009-06-24 17:54:48 +0000265 else
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000266 OS << getFPImm()->getValueAPF().convertToDouble();
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000267 break;
Chris Lattnerf7382302007-12-30 21:56:09 +0000268 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman0ba90f32009-10-31 20:19:03 +0000269 OS << "<BB#" << getMBB()->getNumber() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000270 break;
271 case MachineOperand::MO_FrameIndex:
Chris Lattner31530612009-06-24 17:54:48 +0000272 OS << "<fi#" << getIndex() << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000273 break;
274 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000275 OS << "<cp#" << getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000276 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000277 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000278 break;
279 case MachineOperand::MO_JumpTableIndex:
Chris Lattner31530612009-06-24 17:54:48 +0000280 OS << "<jt#" << getIndex() << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000281 break;
282 case MachineOperand::MO_GlobalAddress:
Dan Gohman8d4e3b52009-11-06 18:03:10 +0000283 OS << "<ga:";
284 WriteAsOperand(OS, getGlobal(), /*PrintType=*/false);
Chris Lattnerf7382302007-12-30 21:56:09 +0000285 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000286 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000287 break;
288 case MachineOperand::MO_ExternalSymbol:
289 OS << "<es:" << getSymbolName();
290 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000291 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000292 break;
Dan Gohman8c2b5252009-10-30 01:27:03 +0000293 case MachineOperand::MO_BlockAddress:
Dale Johannesen5f72a5e2010-01-13 00:00:24 +0000294 OS << '<';
Dan Gohman0ba90f32009-10-31 20:19:03 +0000295 WriteAsOperand(OS, getBlockAddress(), /*PrintType=*/false);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000296 OS << '>';
297 break;
Dale Johannesen5f72a5e2010-01-13 00:00:24 +0000298 case MachineOperand::MO_Metadata:
299 OS << '<';
300 WriteAsOperand(OS, getMetadata(), /*PrintType=*/false);
301 OS << '>';
302 break;
Chris Lattner72aaa3c2010-03-13 08:14:18 +0000303 case MachineOperand::MO_MCSymbol:
304 OS << "<MCSym=" << *getMCSymbol() << '>';
305 break;
Chris Lattnerf7382302007-12-30 21:56:09 +0000306 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000307 llvm_unreachable("Unrecognized operand type");
Chris Lattnerf7382302007-12-30 21:56:09 +0000308 }
Chris Lattner31530612009-06-24 17:54:48 +0000309
310 if (unsigned TF = getTargetFlags())
311 OS << "[TF=" << TF << ']';
Chris Lattnerf7382302007-12-30 21:56:09 +0000312}
313
314//===----------------------------------------------------------------------===//
Dan Gohmance42e402008-07-07 20:32:02 +0000315// MachineMemOperand Implementation
316//===----------------------------------------------------------------------===//
317
318MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f,
319 int64_t o, uint64_t s, unsigned int a)
320 : Offset(o), Size(s), V(v),
David Greeneba2b2972010-02-15 16:48:31 +0000321 Flags((f & ((1 << MOMaxBits) - 1)) | ((Log2_32(a) + 1) << MOMaxBits)) {
Dan Gohman28f02fd2009-09-21 19:47:04 +0000322 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
Dan Gohmanc5e1f982008-07-16 15:56:42 +0000323 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmance42e402008-07-07 20:32:02 +0000324}
325
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000326/// Profile - Gather unique data for the object.
327///
328void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
329 ID.AddInteger(Offset);
330 ID.AddInteger(Size);
331 ID.AddPointer(V);
332 ID.AddInteger(Flags);
333}
334
Dan Gohmanc76909a2009-09-25 20:36:54 +0000335void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
336 // The Value and Offset may differ due to CSE. But the flags and size
337 // should be the same.
338 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
339 assert(MMO->getSize() == getSize() && "Size mismatch!");
340
341 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
342 // Update the alignment value.
David Greeneba2b2972010-02-15 16:48:31 +0000343 Flags = (Flags & ((1 << MOMaxBits) - 1)) |
344 ((Log2_32(MMO->getBaseAlignment()) + 1) << MOMaxBits);
Dan Gohmanc76909a2009-09-25 20:36:54 +0000345 // Also update the base and offset, because the new alignment may
346 // not be applicable with the old ones.
347 V = MMO->getValue();
348 Offset = MMO->getOffset();
349 }
350}
351
Dan Gohman4b2ebc12009-09-25 23:33:20 +0000352/// getAlignment - Return the minimum known alignment in bytes of the
353/// actual memory reference.
354uint64_t MachineMemOperand::getAlignment() const {
355 return MinAlign(getBaseAlignment(), getOffset());
356}
357
Dan Gohmanc76909a2009-09-25 20:36:54 +0000358raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) {
359 assert((MMO.isLoad() || MMO.isStore()) &&
Dan Gohmancd26ec52009-09-23 01:33:16 +0000360 "SV has to be a load, store or both.");
361
Dan Gohmanc76909a2009-09-25 20:36:54 +0000362 if (MMO.isVolatile())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000363 OS << "Volatile ";
364
Dan Gohmanc76909a2009-09-25 20:36:54 +0000365 if (MMO.isLoad())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000366 OS << "LD";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000367 if (MMO.isStore())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000368 OS << "ST";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000369 OS << MMO.getSize();
Dan Gohmancd26ec52009-09-23 01:33:16 +0000370
371 // Print the address information.
372 OS << "[";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000373 if (!MMO.getValue())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000374 OS << "<unknown>";
375 else
Dan Gohmanc76909a2009-09-25 20:36:54 +0000376 WriteAsOperand(OS, MMO.getValue(), /*PrintType=*/false);
Dan Gohmancd26ec52009-09-23 01:33:16 +0000377
378 // If the alignment of the memory reference itself differs from the alignment
379 // of the base pointer, print the base alignment explicitly, next to the base
380 // pointer.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000381 if (MMO.getBaseAlignment() != MMO.getAlignment())
382 OS << "(align=" << MMO.getBaseAlignment() << ")";
Dan Gohmancd26ec52009-09-23 01:33:16 +0000383
Dan Gohmanc76909a2009-09-25 20:36:54 +0000384 if (MMO.getOffset() != 0)
385 OS << "+" << MMO.getOffset();
Dan Gohmancd26ec52009-09-23 01:33:16 +0000386 OS << "]";
387
388 // Print the alignment of the reference.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000389 if (MMO.getBaseAlignment() != MMO.getAlignment() ||
390 MMO.getBaseAlignment() != MMO.getSize())
391 OS << "(align=" << MMO.getAlignment() << ")";
Dan Gohmancd26ec52009-09-23 01:33:16 +0000392
393 return OS;
394}
395
Dan Gohmance42e402008-07-07 20:32:02 +0000396//===----------------------------------------------------------------------===//
Chris Lattnerf7382302007-12-30 21:56:09 +0000397// MachineInstr Implementation
398//===----------------------------------------------------------------------===//
399
Evan Chengc0f64ff2006-11-27 23:37:22 +0000400/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
Evan Cheng67f660c2006-11-30 07:08:44 +0000401/// TID NULL and no operands.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000402MachineInstr::MachineInstr()
Dan Gohman834651c2009-11-16 22:49:38 +0000403 : TID(0), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000404 Parent(0) {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000405 // Make sure that we get added to a machine basicblock
406 LeakDetector::addGarbageObject(this);
Chris Lattner72791222002-10-28 20:59:49 +0000407}
408
Evan Cheng67f660c2006-11-30 07:08:44 +0000409void MachineInstr::addImplicitDefUseOperands() {
410 if (TID->ImplicitDefs)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000411 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner8019f412007-12-30 00:41:17 +0000412 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng67f660c2006-11-30 07:08:44 +0000413 if (TID->ImplicitUses)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000414 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner8019f412007-12-30 00:41:17 +0000415 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Evan Chengd7de4962006-11-13 23:34:06 +0000416}
417
Bob Wilson0855cad2010-04-09 04:34:03 +0000418/// MachineInstr ctor - This constructor creates a MachineInstr and adds the
419/// implicit operands. It reserves space for the number of operands specified by
420/// the TargetInstrDesc.
Chris Lattner749c6f62008-01-07 07:27:27 +0000421MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
Dan Gohman834651c2009-11-16 22:49:38 +0000422 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0),
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000423 MemRefs(0), MemRefsEnd(0), Parent(0) {
Bob Wilson1793ab92010-04-09 04:46:43 +0000424 if (!NoImp)
425 NumImplicitOps = TID->getNumImplicitDefs() + TID->getNumImplicitUses();
Chris Lattner349c4952008-01-07 03:13:06 +0000426 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Chengfa945722007-10-13 02:23:01 +0000427 if (!NoImp)
428 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000429 // Make sure that we get added to a machine basicblock
430 LeakDetector::addGarbageObject(this);
Evan Chengd7de4962006-11-13 23:34:06 +0000431}
432
Dale Johannesen06efc022009-01-27 23:20:29 +0000433/// MachineInstr ctor - As above, but with a DebugLoc.
434MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl,
435 bool NoImp)
Dan Gohman834651c2009-11-16 22:49:38 +0000436 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000437 Parent(0), debugLoc(dl) {
Bob Wilson1793ab92010-04-09 04:46:43 +0000438 if (!NoImp)
439 NumImplicitOps = TID->getNumImplicitDefs() + TID->getNumImplicitUses();
Dale Johannesen06efc022009-01-27 23:20:29 +0000440 Operands.reserve(NumImplicitOps + TID->getNumOperands());
441 if (!NoImp)
442 addImplicitDefUseOperands();
443 // Make sure that we get added to a machine basicblock
444 LeakDetector::addGarbageObject(this);
445}
446
447/// MachineInstr ctor - Work exactly the same as the ctor two above, except
448/// that the MachineInstr is created and added to the end of the specified
449/// basic block.
Dale Johannesen06efc022009-01-27 23:20:29 +0000450MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid)
Dan Gohman834651c2009-11-16 22:49:38 +0000451 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0),
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000452 MemRefs(0), MemRefsEnd(0), Parent(0) {
Dale Johannesen06efc022009-01-27 23:20:29 +0000453 assert(MBB && "Cannot use inserting ctor with null basic block!");
Bob Wilson1793ab92010-04-09 04:46:43 +0000454 NumImplicitOps = TID->getNumImplicitDefs() + TID->getNumImplicitUses();
Dale Johannesen06efc022009-01-27 23:20:29 +0000455 Operands.reserve(NumImplicitOps + TID->getNumOperands());
456 addImplicitDefUseOperands();
457 // Make sure that we get added to a machine basicblock
458 LeakDetector::addGarbageObject(this);
459 MBB->push_back(this); // Add instruction to end of basic block!
460}
461
462/// MachineInstr ctor - As above, but with a DebugLoc.
463///
464MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl,
Chris Lattner749c6f62008-01-07 07:27:27 +0000465 const TargetInstrDesc &tid)
Dan Gohman834651c2009-11-16 22:49:38 +0000466 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000467 Parent(0), debugLoc(dl) {
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000468 assert(MBB && "Cannot use inserting ctor with null basic block!");
Bob Wilson1793ab92010-04-09 04:46:43 +0000469 NumImplicitOps = TID->getNumImplicitDefs() + TID->getNumImplicitUses();
Chris Lattner349c4952008-01-07 03:13:06 +0000470 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Cheng67f660c2006-11-30 07:08:44 +0000471 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000472 // Make sure that we get added to a machine basicblock
473 LeakDetector::addGarbageObject(this);
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000474 MBB->push_back(this); // Add instruction to end of basic block!
475}
476
Misha Brukmance22e762004-07-09 14:45:17 +0000477/// MachineInstr ctor - Copies MachineInstr arg exactly
478///
Evan Cheng1ed99222008-07-19 00:37:25 +0000479MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Dan Gohman834651c2009-11-16 22:49:38 +0000480 : TID(&MI.getDesc()), NumImplicitOps(0), AsmPrinterFlags(0),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000481 MemRefs(MI.MemRefs), MemRefsEnd(MI.MemRefsEnd),
482 Parent(0), debugLoc(MI.getDebugLoc()) {
Chris Lattner943b5e12006-05-04 19:14:44 +0000483 Operands.reserve(MI.getNumOperands());
Tanya Lattnerb5159ed2004-05-23 20:58:02 +0000484
Misha Brukmance22e762004-07-09 14:45:17 +0000485 // Add operands
Evan Cheng1ed99222008-07-19 00:37:25 +0000486 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
487 addOperand(MI.getOperand(i));
488 NumImplicitOps = MI.NumImplicitOps;
Tanya Lattner0c63e032004-05-24 03:14:18 +0000489
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000490 // Set parent to null.
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000491 Parent = 0;
Dan Gohman6116a732008-07-21 18:47:29 +0000492
493 LeakDetector::addGarbageObject(this);
Tanya Lattner466b5342004-05-23 19:35:12 +0000494}
495
Misha Brukmance22e762004-07-09 14:45:17 +0000496MachineInstr::~MachineInstr() {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000497 LeakDetector::removeGarbageObject(this);
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000498#ifndef NDEBUG
Chris Lattner62ed6b92008-01-01 01:12:31 +0000499 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000500 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Dan Gohmand735b802008-10-03 15:45:36 +0000501 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
Chris Lattner62ed6b92008-01-01 01:12:31 +0000502 "Reg operand def/use list corrupted");
503 }
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000504#endif
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000505}
506
Chris Lattner62ed6b92008-01-01 01:12:31 +0000507/// getRegInfo - If this instruction is embedded into a MachineFunction,
508/// return the MachineRegisterInfo object for the current function, otherwise
509/// return null.
510MachineRegisterInfo *MachineInstr::getRegInfo() {
511 if (MachineBasicBlock *MBB = getParent())
Dan Gohman4e526b92008-07-08 23:59:09 +0000512 return &MBB->getParent()->getRegInfo();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000513 return 0;
514}
515
516/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
517/// this instruction from their respective use lists. This requires that the
518/// operands already be on their use lists.
519void MachineInstr::RemoveRegOperandsFromUseLists() {
520 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000521 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000522 Operands[i].RemoveRegOperandFromRegInfo();
523 }
524}
525
526/// AddRegOperandsToUseLists - Add all of the register operands in
527/// this instruction from their respective use lists. This requires that the
528/// operands not be on their use lists yet.
529void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
530 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000531 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000532 Operands[i].AddRegOperandToRegInfo(&RegInfo);
533 }
534}
535
536
537/// addOperand - Add the specified operand to the instruction. If it is an
538/// implicit operand, it is added to the end of the operand list. If it is
539/// an explicit operand it is added at the end of the explicit operand list
540/// (before the first implicit operand).
541void MachineInstr::addOperand(const MachineOperand &Op) {
Dan Gohmand735b802008-10-03 15:45:36 +0000542 bool isImpReg = Op.isReg() && Op.isImplicit();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000543 assert((isImpReg || !OperandsComplete()) &&
544 "Trying to add an operand to a machine instr that is already done!");
545
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000546 MachineRegisterInfo *RegInfo = getRegInfo();
547
Chris Lattner62ed6b92008-01-01 01:12:31 +0000548 // If we are adding the operand to the end of the list, our job is simpler.
549 // This is true most of the time, so this is a reasonable optimization.
550 if (isImpReg || NumImplicitOps == 0) {
551 // We can only do this optimization if we know that the operand list won't
552 // reallocate.
553 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
554 Operands.push_back(Op);
555
556 // Set the parent of the operand.
557 Operands.back().ParentMI = this;
558
559 // If the operand is a register, update the operand's use list.
Jim Grosbach06801722009-12-16 19:43:02 +0000560 if (Op.isReg()) {
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000561 Operands.back().AddRegOperandToRegInfo(RegInfo);
Jim Grosbach06801722009-12-16 19:43:02 +0000562 // If the register operand is flagged as early, mark the operand as such
563 unsigned OpNo = Operands.size() - 1;
564 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
565 Operands[OpNo].setIsEarlyClobber(true);
566 }
Chris Lattner62ed6b92008-01-01 01:12:31 +0000567 return;
568 }
569 }
570
571 // Otherwise, we have to insert a real operand before any implicit ones.
572 unsigned OpNo = Operands.size()-NumImplicitOps;
573
Chris Lattner62ed6b92008-01-01 01:12:31 +0000574 // If this instruction isn't embedded into a function, then we don't need to
575 // update any operand lists.
576 if (RegInfo == 0) {
577 // Simple insertion, no reginfo update needed for other register operands.
578 Operands.insert(Operands.begin()+OpNo, Op);
579 Operands[OpNo].ParentMI = this;
580
581 // Do explicitly set the reginfo for this operand though, to ensure the
582 // next/prev fields are properly nulled out.
Jim Grosbach06801722009-12-16 19:43:02 +0000583 if (Operands[OpNo].isReg()) {
Chris Lattner62ed6b92008-01-01 01:12:31 +0000584 Operands[OpNo].AddRegOperandToRegInfo(0);
Jim Grosbach06801722009-12-16 19:43:02 +0000585 // If the register operand is flagged as early, mark the operand as such
586 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
587 Operands[OpNo].setIsEarlyClobber(true);
588 }
Chris Lattner62ed6b92008-01-01 01:12:31 +0000589
590 } else if (Operands.size()+1 <= Operands.capacity()) {
591 // Otherwise, we have to remove register operands from their register use
592 // list, add the operand, then add the register operands back to their use
593 // list. This also must handle the case when the operand list reallocates
594 // to somewhere else.
595
596 // If insertion of this operand won't cause reallocation of the operand
597 // list, just remove the implicit operands, add the operand, then re-add all
598 // the rest of the operands.
599 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000600 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000601 Operands[i].RemoveRegOperandFromRegInfo();
602 }
603
604 // Add the operand. If it is a register, add it to the reg list.
605 Operands.insert(Operands.begin()+OpNo, Op);
606 Operands[OpNo].ParentMI = this;
607
Jim Grosbach06801722009-12-16 19:43:02 +0000608 if (Operands[OpNo].isReg()) {
Chris Lattner62ed6b92008-01-01 01:12:31 +0000609 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
Jim Grosbach06801722009-12-16 19:43:02 +0000610 // If the register operand is flagged as early, mark the operand as such
611 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
612 Operands[OpNo].setIsEarlyClobber(true);
613 }
Chris Lattner62ed6b92008-01-01 01:12:31 +0000614
615 // Re-add all the implicit ops.
616 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000617 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000618 Operands[i].AddRegOperandToRegInfo(RegInfo);
619 }
620 } else {
621 // Otherwise, we will be reallocating the operand list. Remove all reg
622 // operands from their list, then readd them after the operand list is
623 // reallocated.
624 RemoveRegOperandsFromUseLists();
625
626 Operands.insert(Operands.begin()+OpNo, Op);
627 Operands[OpNo].ParentMI = this;
628
629 // Re-add all the operands.
630 AddRegOperandsToUseLists(*RegInfo);
Jim Grosbach06801722009-12-16 19:43:02 +0000631
632 // If the register operand is flagged as early, mark the operand as such
633 if (Operands[OpNo].isReg()
634 && TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
635 Operands[OpNo].setIsEarlyClobber(true);
Chris Lattner62ed6b92008-01-01 01:12:31 +0000636 }
637}
638
639/// RemoveOperand - Erase an operand from an instruction, leaving it with one
640/// fewer operand than it started with.
641///
642void MachineInstr::RemoveOperand(unsigned OpNo) {
643 assert(OpNo < Operands.size() && "Invalid operand number");
644
645 // Special case removing the last one.
646 if (OpNo == Operands.size()-1) {
647 // If needed, remove from the reg def/use list.
Dan Gohmand735b802008-10-03 15:45:36 +0000648 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000649 Operands.back().RemoveRegOperandFromRegInfo();
650
651 Operands.pop_back();
652 return;
653 }
654
655 // Otherwise, we are removing an interior operand. If we have reginfo to
656 // update, remove all operands that will be shifted down from their reg lists,
657 // move everything down, then re-add them.
658 MachineRegisterInfo *RegInfo = getRegInfo();
659 if (RegInfo) {
660 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000661 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000662 Operands[i].RemoveRegOperandFromRegInfo();
663 }
664 }
665
666 Operands.erase(Operands.begin()+OpNo);
667
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].AddRegOperandToRegInfo(RegInfo);
672 }
673 }
674}
675
Dan Gohmanc76909a2009-09-25 20:36:54 +0000676/// addMemOperand - Add a MachineMemOperand to the machine instruction.
677/// This function should be used only occasionally. The setMemRefs function
678/// is the primary method for setting up a MachineInstr's MemRefs list.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000679void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohmanc76909a2009-09-25 20:36:54 +0000680 MachineMemOperand *MO) {
681 mmo_iterator OldMemRefs = MemRefs;
682 mmo_iterator OldMemRefsEnd = MemRefsEnd;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000683
Dan Gohmanc76909a2009-09-25 20:36:54 +0000684 size_t NewNum = (MemRefsEnd - MemRefs) + 1;
685 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
686 mmo_iterator NewMemRefsEnd = NewMemRefs + NewNum;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000687
Dan Gohmanc76909a2009-09-25 20:36:54 +0000688 std::copy(OldMemRefs, OldMemRefsEnd, NewMemRefs);
689 NewMemRefs[NewNum - 1] = MO;
690
691 MemRefs = NewMemRefs;
692 MemRefsEnd = NewMemRefsEnd;
693}
Chris Lattner62ed6b92008-01-01 01:12:31 +0000694
Evan Cheng506049f2010-03-03 01:44:33 +0000695bool MachineInstr::isIdenticalTo(const MachineInstr *Other,
696 MICheckType Check) const {
Evan Cheng34cdf6e2010-03-03 21:54:14 +0000697 // If opcodes or number of operands are not the same then the two
698 // instructions are obviously not identical.
699 if (Other->getOpcode() != getOpcode() ||
700 Other->getNumOperands() != getNumOperands())
701 return false;
702
703 // Check operands to make sure they match.
704 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
705 const MachineOperand &MO = getOperand(i);
706 const MachineOperand &OMO = Other->getOperand(i);
707 // Clients may or may not want to ignore defs when testing for equality.
708 // For example, machine CSE pass only cares about finding common
709 // subexpressions, so it's safe to ignore virtual register defs.
710 if (Check != CheckDefs && MO.isReg() && MO.isDef()) {
711 if (Check == IgnoreDefs)
712 continue;
713 // Check == IgnoreVRegDefs
714 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) ||
715 TargetRegisterInfo::isPhysicalRegister(OMO.getReg()))
716 if (MO.getReg() != OMO.getReg())
717 return false;
718 } else if (!MO.isIdenticalTo(OMO))
Evan Cheng506049f2010-03-03 01:44:33 +0000719 return false;
Evan Cheng34cdf6e2010-03-03 21:54:14 +0000720 }
721 return true;
Evan Cheng506049f2010-03-03 01:44:33 +0000722}
723
Chris Lattner48d7c062006-04-17 21:35:41 +0000724/// removeFromParent - This method unlinks 'this' from the containing basic
725/// block, and returns it, but does not delete it.
726MachineInstr *MachineInstr::removeFromParent() {
727 assert(getParent() && "Not embedded in a basic block!");
728 getParent()->remove(this);
729 return this;
730}
731
732
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000733/// eraseFromParent - This method unlinks 'this' from the containing basic
734/// block, and deletes it.
735void MachineInstr::eraseFromParent() {
736 assert(getParent() && "Not embedded in a basic block!");
737 getParent()->erase(this);
738}
739
740
Brian Gaeke21326fc2004-02-13 04:39:32 +0000741/// OperandComplete - Return true if it's illegal to add a new operand
742///
Chris Lattner2a90ba62004-02-12 16:09:53 +0000743bool MachineInstr::OperandsComplete() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000744 unsigned short NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000745 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000746 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000747 return false;
748}
749
Evan Cheng19e3f312007-05-15 01:26:09 +0000750/// getNumExplicitOperands - Returns the number of non-implicit operands.
751///
752unsigned MachineInstr::getNumExplicitOperands() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000753 unsigned NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000754 if (!TID->isVariadic())
Evan Cheng19e3f312007-05-15 01:26:09 +0000755 return NumOperands;
756
Dan Gohman9407cd42009-04-15 17:59:11 +0000757 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
758 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000759 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng19e3f312007-05-15 01:26:09 +0000760 NumOperands++;
761 }
762 return NumOperands;
763}
764
Chris Lattner8ace2cd2006-10-20 22:39:59 +0000765
Evan Chengfaa51072007-04-26 19:00:32 +0000766/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbachf9ca50e2009-09-17 17:57:26 +0000767/// the specific register or -1 if it is not found. It further tightens
Evan Cheng76d7e762007-02-23 01:04:26 +0000768/// the search criteria to a use that kills the register if isKill is true.
Evan Cheng6130f662008-03-05 00:59:57 +0000769int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
770 const TargetRegisterInfo *TRI) const {
Evan Cheng576d1232006-12-06 08:27:42 +0000771 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengf277ee42007-05-29 18:35:22 +0000772 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000773 if (!MO.isReg() || !MO.isUse())
Evan Cheng6130f662008-03-05 00:59:57 +0000774 continue;
775 unsigned MOReg = MO.getReg();
776 if (!MOReg)
777 continue;
778 if (MOReg == Reg ||
779 (TRI &&
780 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
781 TargetRegisterInfo::isPhysicalRegister(Reg) &&
782 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng76d7e762007-02-23 01:04:26 +0000783 if (!isKill || MO.isKill())
Evan Cheng32eb1f12007-03-26 22:37:45 +0000784 return i;
Evan Cheng576d1232006-12-06 08:27:42 +0000785 }
Evan Cheng32eb1f12007-03-26 22:37:45 +0000786 return -1;
Evan Cheng576d1232006-12-06 08:27:42 +0000787}
Jakob Stoklund Olesen7ebc4d62010-05-19 20:36:22 +0000788
Jakob Stoklund Olesen18b2c9d2010-05-21 20:02:01 +0000789/// readsWritesVirtualRegister - Return a pair of bools (reads, writes)
790/// indicating if this instruction reads or writes Reg. This also considers
791/// partial defines.
792std::pair<bool,bool>
793MachineInstr::readsWritesVirtualRegister(unsigned Reg,
794 SmallVectorImpl<unsigned> *Ops) const {
795 bool PartDef = false; // Partial redefine.
796 bool FullDef = false; // Full define.
797 bool Use = false;
Jakob Stoklund Olesen7ebc4d62010-05-19 20:36:22 +0000798
799 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
800 const MachineOperand &MO = getOperand(i);
801 if (!MO.isReg() || MO.getReg() != Reg)
802 continue;
Jakob Stoklund Olesen18b2c9d2010-05-21 20:02:01 +0000803 if (Ops)
804 Ops->push_back(i);
Jakob Stoklund Olesen7ebc4d62010-05-19 20:36:22 +0000805 if (MO.isUse())
Jakob Stoklund Olesen18b2c9d2010-05-21 20:02:01 +0000806 Use |= !MO.isUndef();
807 else if (MO.getSubReg())
Jakob Stoklund Olesen7ebc4d62010-05-19 20:36:22 +0000808 PartDef = true;
809 else
810 FullDef = true;
811 }
Jakob Stoklund Olesen18b2c9d2010-05-21 20:02:01 +0000812 // A partial redefine uses Reg unless there is also a full define.
813 return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef);
Jakob Stoklund Olesen7ebc4d62010-05-19 20:36:22 +0000814}
815
Evan Cheng6130f662008-03-05 00:59:57 +0000816/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman703bfe62008-05-06 00:20:10 +0000817/// the specified register or -1 if it is not found. If isDead is true, defs
818/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
819/// also checks if there is a def of a super-register.
Evan Cheng1015ba72010-05-21 20:53:24 +0000820int
821MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap,
822 const TargetRegisterInfo *TRI) const {
823 bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg);
Evan Chengb371f452007-02-19 21:49:54 +0000824 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng6130f662008-03-05 00:59:57 +0000825 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000826 if (!MO.isReg() || !MO.isDef())
Evan Cheng6130f662008-03-05 00:59:57 +0000827 continue;
828 unsigned MOReg = MO.getReg();
Evan Cheng1015ba72010-05-21 20:53:24 +0000829 bool Found = (MOReg == Reg);
830 if (!Found && TRI && isPhys &&
831 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
832 if (Overlap)
833 Found = TRI->regsOverlap(MOReg, Reg);
834 else
835 Found = TRI->isSubRegister(MOReg, Reg);
836 }
837 if (Found && (!isDead || MO.isDead()))
838 return i;
Evan Chengb371f452007-02-19 21:49:54 +0000839 }
Evan Cheng6130f662008-03-05 00:59:57 +0000840 return -1;
Evan Chengb371f452007-02-19 21:49:54 +0000841}
Evan Cheng19e3f312007-05-15 01:26:09 +0000842
Evan Chengf277ee42007-05-29 18:35:22 +0000843/// findFirstPredOperandIdx() - Find the index of the first operand in the
844/// operand list that is used to represent the predicate. It returns -1 if
845/// none is found.
846int MachineInstr::findFirstPredOperandIdx() const {
Chris Lattner749c6f62008-01-07 07:27:27 +0000847 const TargetInstrDesc &TID = getDesc();
848 if (TID.isPredicable()) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000849 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner749c6f62008-01-07 07:27:27 +0000850 if (TID.OpInfo[i].isPredicate())
Evan Chengf277ee42007-05-29 18:35:22 +0000851 return i;
Evan Cheng19e3f312007-05-15 01:26:09 +0000852 }
853
Evan Chengf277ee42007-05-29 18:35:22 +0000854 return -1;
Evan Cheng19e3f312007-05-15 01:26:09 +0000855}
Evan Chengb371f452007-02-19 21:49:54 +0000856
Bob Wilsond9df5012009-04-09 17:16:43 +0000857/// isRegTiedToUseOperand - Given the index of a register def operand,
858/// check if the register def is tied to a source operand, due to either
859/// two-address elimination or inline assembly constraints. Returns the
860/// first tied use operand index by reference is UseOpIdx is not null.
Jakob Stoklund Olesence9be2c2009-04-29 20:57:16 +0000861bool MachineInstr::
862isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const {
Chris Lattner518bb532010-02-09 19:54:29 +0000863 if (isInlineAsm()) {
Bob Wilsond9df5012009-04-09 17:16:43 +0000864 assert(DefOpIdx >= 2);
865 const MachineOperand &MO = getOperand(DefOpIdx);
Chris Lattnerc30aa7b2009-04-09 23:33:34 +0000866 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
Evan Chengfb112882009-03-23 08:01:15 +0000867 return false;
Evan Chengef5d0702009-06-24 02:05:51 +0000868 // Determine the actual operand index that corresponds to this index.
Evan Chengfb112882009-03-23 08:01:15 +0000869 unsigned DefNo = 0;
Evan Chengef5d0702009-06-24 02:05:51 +0000870 unsigned DefPart = 0;
Evan Chengfb112882009-03-23 08:01:15 +0000871 for (unsigned i = 1, e = getNumOperands(); i < e; ) {
872 const MachineOperand &FMO = getOperand(i);
Jakob Stoklund Olesen45d34fe2009-07-19 19:09:59 +0000873 // After the normal asm operands there may be additional imp-def regs.
874 if (!FMO.isImm())
875 return false;
Evan Chengfb112882009-03-23 08:01:15 +0000876 // Skip over this def.
Evan Chengef5d0702009-06-24 02:05:51 +0000877 unsigned NumOps = InlineAsm::getNumOperandRegisters(FMO.getImm());
878 unsigned PrevDef = i + 1;
879 i = PrevDef + NumOps;
880 if (i > DefOpIdx) {
881 DefPart = DefOpIdx - PrevDef;
Evan Chengfb112882009-03-23 08:01:15 +0000882 break;
Evan Chengef5d0702009-06-24 02:05:51 +0000883 }
Evan Chengfb112882009-03-23 08:01:15 +0000884 ++DefNo;
885 }
Evan Chengef5d0702009-06-24 02:05:51 +0000886 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Evan Chengfb112882009-03-23 08:01:15 +0000887 const MachineOperand &FMO = getOperand(i);
888 if (!FMO.isImm())
889 continue;
890 if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse())
891 continue;
892 unsigned Idx;
Evan Chengef5d0702009-06-24 02:05:51 +0000893 if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000894 Idx == DefNo) {
895 if (UseOpIdx)
Evan Chengef5d0702009-06-24 02:05:51 +0000896 *UseOpIdx = (unsigned)i + 1 + DefPart;
Evan Chengfb112882009-03-23 08:01:15 +0000897 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000898 }
Evan Chengfb112882009-03-23 08:01:15 +0000899 }
Evan Chengef5d0702009-06-24 02:05:51 +0000900 return false;
Evan Chengfb112882009-03-23 08:01:15 +0000901 }
902
Bob Wilsond9df5012009-04-09 17:16:43 +0000903 assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!");
Chris Lattner749c6f62008-01-07 07:27:27 +0000904 const TargetInstrDesc &TID = getDesc();
Evan Chengef0732d2008-07-10 07:35:43 +0000905 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
906 const MachineOperand &MO = getOperand(i);
Dan Gohman2ce7f202008-12-05 05:45:42 +0000907 if (MO.isReg() && MO.isUse() &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000908 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) {
909 if (UseOpIdx)
910 *UseOpIdx = (unsigned)i;
Evan Chengef0732d2008-07-10 07:35:43 +0000911 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000912 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000913 }
914 return false;
915}
916
Evan Chenga24752f2009-03-19 20:30:06 +0000917/// isRegTiedToDefOperand - Return true if the operand of the specified index
918/// is a register use and it is tied to an def operand. It also returns the def
919/// operand index by reference.
Jakob Stoklund Olesence9be2c2009-04-29 20:57:16 +0000920bool MachineInstr::
921isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const {
Chris Lattner518bb532010-02-09 19:54:29 +0000922 if (isInlineAsm()) {
Evan Chengfb112882009-03-23 08:01:15 +0000923 const MachineOperand &MO = getOperand(UseOpIdx);
Chris Lattner0c8382c2009-04-09 16:50:43 +0000924 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0)
Evan Chengfb112882009-03-23 08:01:15 +0000925 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000926
927 // Find the flag operand corresponding to UseOpIdx
928 unsigned FlagIdx, NumOps=0;
929 for (FlagIdx = 1; FlagIdx < UseOpIdx; FlagIdx += NumOps+1) {
930 const MachineOperand &UFMO = getOperand(FlagIdx);
Jakob Stoklund Olesen45d34fe2009-07-19 19:09:59 +0000931 // After the normal asm operands there may be additional imp-def regs.
932 if (!UFMO.isImm())
933 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000934 NumOps = InlineAsm::getNumOperandRegisters(UFMO.getImm());
935 assert(NumOps < getNumOperands() && "Invalid inline asm flag");
936 if (UseOpIdx < FlagIdx+NumOps+1)
937 break;
Evan Chengef5d0702009-06-24 02:05:51 +0000938 }
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000939 if (FlagIdx >= UseOpIdx)
Evan Chengef5d0702009-06-24 02:05:51 +0000940 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000941 const MachineOperand &UFMO = getOperand(FlagIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000942 unsigned DefNo;
943 if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) {
944 if (!DefOpIdx)
945 return true;
946
947 unsigned DefIdx = 1;
948 // Remember to adjust the index. First operand is asm string, then there
949 // is a flag for each.
950 while (DefNo) {
951 const MachineOperand &FMO = getOperand(DefIdx);
952 assert(FMO.isImm());
953 // Skip over this def.
954 DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
955 --DefNo;
956 }
Evan Chengef5d0702009-06-24 02:05:51 +0000957 *DefOpIdx = DefIdx + UseOpIdx - FlagIdx;
Evan Chengfb112882009-03-23 08:01:15 +0000958 return true;
959 }
960 return false;
961 }
962
Evan Chenga24752f2009-03-19 20:30:06 +0000963 const TargetInstrDesc &TID = getDesc();
964 if (UseOpIdx >= TID.getNumOperands())
965 return false;
966 const MachineOperand &MO = getOperand(UseOpIdx);
967 if (!MO.isReg() || !MO.isUse())
968 return false;
969 int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO);
970 if (DefIdx == -1)
971 return false;
972 if (DefOpIdx)
973 *DefOpIdx = (unsigned)DefIdx;
974 return true;
975}
976
Dan Gohmane6cd7572010-05-13 20:34:42 +0000977/// clearKillInfo - Clears kill flags on all operands.
978///
979void MachineInstr::clearKillInfo() {
980 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
981 MachineOperand &MO = getOperand(i);
982 if (MO.isReg() && MO.isUse())
983 MO.setIsKill(false);
984 }
985}
986
Evan Cheng576d1232006-12-06 08:27:42 +0000987/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
988///
989void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
990 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
991 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000992 if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
Evan Cheng576d1232006-12-06 08:27:42 +0000993 continue;
994 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
995 MachineOperand &MOp = getOperand(j);
996 if (!MOp.isIdenticalTo(MO))
997 continue;
998 if (MO.isKill())
999 MOp.setIsKill();
1000 else
1001 MOp.setIsDead();
1002 break;
1003 }
1004 }
1005}
1006
Evan Cheng19e3f312007-05-15 01:26:09 +00001007/// copyPredicates - Copies predicate operand(s) from MI.
1008void MachineInstr::copyPredicates(const MachineInstr *MI) {
Chris Lattner749c6f62008-01-07 07:27:27 +00001009 const TargetInstrDesc &TID = MI->getDesc();
Evan Chengb27087f2008-03-13 00:44:09 +00001010 if (!TID.isPredicable())
1011 return;
1012 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1013 if (TID.OpInfo[i].isPredicate()) {
1014 // Predicated operands must be last operands.
1015 addOperand(MI->getOperand(i));
Evan Cheng19e3f312007-05-15 01:26:09 +00001016 }
1017 }
1018}
1019
Evan Cheng9f1c8312008-07-03 09:09:37 +00001020/// isSafeToMove - Return true if it is safe to move this instruction. If
1021/// SawStore is set to true, it means that there is a store (or call) between
1022/// the instruction's location and its intended destination.
Dan Gohmanb3b930a2008-11-18 19:04:29 +00001023bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
Evan Chengac1abde2010-03-02 19:03:01 +00001024 AliasAnalysis *AA,
1025 bool &SawStore) const {
Evan Chengb27087f2008-03-13 00:44:09 +00001026 // Ignore stuff that we obviously can't move.
1027 if (TID->mayStore() || TID->isCall()) {
1028 SawStore = true;
1029 return false;
1030 }
Dan Gohman237dee12008-12-23 17:28:50 +00001031 if (TID->isTerminator() || TID->hasUnmodeledSideEffects())
Evan Chengb27087f2008-03-13 00:44:09 +00001032 return false;
1033
1034 // See if this instruction does a load. If so, we have to guarantee that the
1035 // loaded value doesn't change between the load and the its intended
1036 // destination. The check for isInvariantLoad gives the targe the chance to
1037 // classify the load as always returning a constant, e.g. a constant pool
1038 // load.
Dan Gohmana70dca12009-10-09 23:27:56 +00001039 if (TID->mayLoad() && !isInvariantLoad(AA))
Evan Chengb27087f2008-03-13 00:44:09 +00001040 // Otherwise, this is a real load. If there is a store between the load and
Evan Cheng7cc2c402009-07-28 21:49:18 +00001041 // end of block, or if the load is volatile, we can't move it.
Dan Gohmand790a5c2008-10-02 15:04:30 +00001042 return !SawStore && !hasVolatileMemoryRef();
Dan Gohman3e4fb702008-09-24 00:06:15 +00001043
Evan Chengb27087f2008-03-13 00:44:09 +00001044 return true;
1045}
1046
Evan Chengdf3b9932008-08-27 20:33:50 +00001047/// isSafeToReMat - Return true if it's safe to rematerialize the specified
1048/// instruction which defined the specified register instead of copying it.
Dan Gohmanb3b930a2008-11-18 19:04:29 +00001049bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII,
Evan Chengac1abde2010-03-02 19:03:01 +00001050 AliasAnalysis *AA,
1051 unsigned DstReg) const {
Evan Chengdf3b9932008-08-27 20:33:50 +00001052 bool SawStore = false;
Dan Gohmana70dca12009-10-09 23:27:56 +00001053 if (!TII->isTriviallyReMaterializable(this, AA) ||
Evan Chengac1abde2010-03-02 19:03:01 +00001054 !isSafeToMove(TII, AA, SawStore))
Evan Chengdf3b9932008-08-27 20:33:50 +00001055 return false;
1056 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohmancbad42c2008-11-18 19:49:32 +00001057 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001058 if (!MO.isReg())
Evan Chengdf3b9932008-08-27 20:33:50 +00001059 continue;
1060 // FIXME: For now, do not remat any instruction with register operands.
1061 // Later on, we can loosen the restriction is the register operands have
1062 // not been modified between the def and use. Note, this is different from
Evan Cheng8763c1c2008-08-27 20:58:54 +00001063 // MachineSink because the code is no longer in two-address form (at least
Evan Chengdf3b9932008-08-27 20:33:50 +00001064 // partially).
1065 if (MO.isUse())
1066 return false;
1067 else if (!MO.isDead() && MO.getReg() != DstReg)
1068 return false;
1069 }
1070 return true;
1071}
1072
Dan Gohman3e4fb702008-09-24 00:06:15 +00001073/// hasVolatileMemoryRef - Return true if this instruction may have a
1074/// volatile memory reference, or if the information describing the
1075/// memory reference is not available. Return false if it is known to
1076/// have no volatile memory references.
1077bool MachineInstr::hasVolatileMemoryRef() const {
1078 // An instruction known never to access memory won't have a volatile access.
1079 if (!TID->mayStore() &&
1080 !TID->mayLoad() &&
1081 !TID->isCall() &&
1082 !TID->hasUnmodeledSideEffects())
1083 return false;
1084
1085 // Otherwise, if the instruction has no memory reference information,
1086 // conservatively assume it wasn't preserved.
1087 if (memoperands_empty())
1088 return true;
1089
1090 // Check the memory reference information for volatile references.
Dan Gohmanc76909a2009-09-25 20:36:54 +00001091 for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I)
1092 if ((*I)->isVolatile())
Dan Gohman3e4fb702008-09-24 00:06:15 +00001093 return true;
1094
1095 return false;
1096}
1097
Dan Gohmane33f44c2009-10-07 17:38:06 +00001098/// isInvariantLoad - Return true if this instruction is loading from a
1099/// location whose value is invariant across the function. For example,
Dan Gohmanf451cb82010-02-10 16:03:48 +00001100/// loading a value from the constant pool or from the argument area
Dan Gohmane33f44c2009-10-07 17:38:06 +00001101/// of a function if it does not change. This should only return true of
1102/// *all* loads the instruction does are invariant (if it does multiple loads).
1103bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const {
1104 // If the instruction doesn't load at all, it isn't an invariant load.
1105 if (!TID->mayLoad())
1106 return false;
1107
1108 // If the instruction has lost its memoperands, conservatively assume that
1109 // it may not be an invariant load.
1110 if (memoperands_empty())
1111 return false;
1112
1113 const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo();
1114
1115 for (mmo_iterator I = memoperands_begin(),
1116 E = memoperands_end(); I != E; ++I) {
1117 if ((*I)->isVolatile()) return false;
1118 if ((*I)->isStore()) return false;
1119
1120 if (const Value *V = (*I)->getValue()) {
1121 // A load from a constant PseudoSourceValue is invariant.
1122 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V))
1123 if (PSV->isConstant(MFI))
1124 continue;
1125 // If we have an AliasAnalysis, ask it whether the memory is constant.
1126 if (AA && AA->pointsToConstantMemory(V))
1127 continue;
1128 }
1129
1130 // Otherwise assume conservatively.
1131 return false;
1132 }
1133
1134 // Everything checks out.
1135 return true;
1136}
1137
Evan Cheng229694f2009-12-03 02:31:43 +00001138/// isConstantValuePHI - If the specified instruction is a PHI that always
1139/// merges together the same virtual register, return the register, otherwise
1140/// return 0.
1141unsigned MachineInstr::isConstantValuePHI() const {
Chris Lattner518bb532010-02-09 19:54:29 +00001142 if (!isPHI())
Evan Cheng229694f2009-12-03 02:31:43 +00001143 return 0;
Evan Chengd8f079c2009-12-07 23:10:34 +00001144 assert(getNumOperands() >= 3 &&
1145 "It's illegal to have a PHI without source operands");
Evan Cheng229694f2009-12-03 02:31:43 +00001146
1147 unsigned Reg = getOperand(1).getReg();
1148 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
1149 if (getOperand(i).getReg() != Reg)
1150 return 0;
1151 return Reg;
1152}
1153
Evan Chenga57fabe2010-04-08 20:02:37 +00001154/// allDefsAreDead - Return true if all the defs of this instruction are dead.
1155///
1156bool MachineInstr::allDefsAreDead() const {
1157 for (unsigned i = 0, e = getNumOperands(); i < e; ++i) {
1158 const MachineOperand &MO = getOperand(i);
1159 if (!MO.isReg() || MO.isUse())
1160 continue;
1161 if (!MO.isDead())
1162 return false;
1163 }
1164 return true;
1165}
1166
Brian Gaeke21326fc2004-02-13 04:39:32 +00001167void MachineInstr::dump() const {
David Greene3b325332010-01-04 23:48:20 +00001168 dbgs() << " " << *this;
Mon P Wang5ca6bd12008-10-10 01:43:55 +00001169}
1170
1171void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const {
Dan Gohman80f6c582009-11-09 19:38:45 +00001172 // We can be a bit tidier if we know the TargetMachine and/or MachineFunction.
1173 const MachineFunction *MF = 0;
1174 if (const MachineBasicBlock *MBB = getParent()) {
1175 MF = MBB->getParent();
1176 if (!TM && MF)
1177 TM = &MF->getTarget();
1178 }
Dan Gohman0ba90f32009-10-31 20:19:03 +00001179
1180 // Print explicitly defined operands on the left of an assignment syntax.
Dan Gohman80f6c582009-11-09 19:38:45 +00001181 unsigned StartOp = 0, e = getNumOperands();
Dan Gohman0ba90f32009-10-31 20:19:03 +00001182 for (; StartOp < e && getOperand(StartOp).isReg() &&
1183 getOperand(StartOp).isDef() &&
1184 !getOperand(StartOp).isImplicit();
1185 ++StartOp) {
1186 if (StartOp != 0) OS << ", ";
1187 getOperand(StartOp).print(OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +00001188 }
Tanya Lattnerb1407622004-06-25 00:13:11 +00001189
Dan Gohman0ba90f32009-10-31 20:19:03 +00001190 if (StartOp != 0)
1191 OS << " = ";
1192
1193 // Print the opcode name.
Chris Lattner749c6f62008-01-07 07:27:27 +00001194 OS << getDesc().getName();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001195
Dan Gohman0ba90f32009-10-31 20:19:03 +00001196 // Print the rest of the operands.
Dan Gohman80f6c582009-11-09 19:38:45 +00001197 bool OmittedAnyCallClobbers = false;
1198 bool FirstOp = true;
Chris Lattner6a592272002-10-30 01:55:38 +00001199 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohman80f6c582009-11-09 19:38:45 +00001200 const MachineOperand &MO = getOperand(i);
1201
1202 // Omit call-clobbered registers which aren't used anywhere. This makes
1203 // call instructions much less noisy on targets where calls clobber lots
1204 // of registers. Don't rely on MO.isDead() because we may be called before
1205 // LiveVariables is run, or we may be looking at a non-allocatable reg.
1206 if (MF && getDesc().isCall() &&
1207 MO.isReg() && MO.isImplicit() && MO.isDef()) {
1208 unsigned Reg = MO.getReg();
1209 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) {
1210 const MachineRegisterInfo &MRI = MF->getRegInfo();
1211 if (MRI.use_empty(Reg) && !MRI.isLiveOut(Reg)) {
1212 bool HasAliasLive = false;
1213 for (const unsigned *Alias = TM->getRegisterInfo()->getAliasSet(Reg);
1214 unsigned AliasReg = *Alias; ++Alias)
1215 if (!MRI.use_empty(AliasReg) || MRI.isLiveOut(AliasReg)) {
1216 HasAliasLive = true;
1217 break;
1218 }
1219 if (!HasAliasLive) {
1220 OmittedAnyCallClobbers = true;
1221 continue;
1222 }
1223 }
1224 }
1225 }
1226
1227 if (FirstOp) FirstOp = false; else OS << ",";
Chris Lattner6a592272002-10-30 01:55:38 +00001228 OS << " ";
Jakob Stoklund Olesenb1bb4af2010-01-19 22:08:34 +00001229 if (i < getDesc().NumOperands) {
1230 const TargetOperandInfo &TOI = getDesc().OpInfo[i];
1231 if (TOI.isPredicate())
1232 OS << "pred:";
1233 if (TOI.isOptionalDef())
1234 OS << "opt:";
1235 }
Evan Cheng59b36552010-04-28 20:03:13 +00001236 if (isDebugValue() && MO.isMetadata()) {
1237 // Pretty print DBG_VALUE instructions.
1238 const MDNode *MD = MO.getMetadata();
1239 if (const MDString *MDS = dyn_cast<MDString>(MD->getOperand(2)))
1240 OS << "!\"" << MDS->getString() << '\"';
1241 else
1242 MO.print(OS, TM);
1243 } else
1244 MO.print(OS, TM);
Dan Gohman80f6c582009-11-09 19:38:45 +00001245 }
1246
1247 // Briefly indicate whether any call clobbers were omitted.
1248 if (OmittedAnyCallClobbers) {
Bill Wendling164558e2009-12-25 13:45:50 +00001249 if (!FirstOp) OS << ",";
Dan Gohman80f6c582009-11-09 19:38:45 +00001250 OS << " ...";
Chris Lattner10491642002-10-30 00:48:05 +00001251 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001252
Dan Gohman0ba90f32009-10-31 20:19:03 +00001253 bool HaveSemi = false;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001254 if (!memoperands_empty()) {
Dan Gohman0ba90f32009-10-31 20:19:03 +00001255 if (!HaveSemi) OS << ";"; HaveSemi = true;
1256
1257 OS << " mem:";
Dan Gohmanc76909a2009-09-25 20:36:54 +00001258 for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
1259 i != e; ++i) {
1260 OS << **i;
Dan Gohmancd26ec52009-09-23 01:33:16 +00001261 if (next(i) != e)
1262 OS << " ";
Dan Gohman69de1932008-02-06 22:27:42 +00001263 }
1264 }
1265
Dan Gohman80f6c582009-11-09 19:38:45 +00001266 if (!debugLoc.isUnknown() && MF) {
Bill Wendlingad2cf9d2009-12-25 13:44:36 +00001267 if (!HaveSemi) OS << ";";
Dan Gohman0ba90f32009-10-31 20:19:03 +00001268
1269 // TODO: print InlinedAtLoc information
1270
Chris Lattnerde4845c2010-04-02 19:42:39 +00001271 DIScope Scope(debugLoc.getScope(MF->getFunction()->getContext()));
Dan Gohman75ae5932009-11-23 21:29:08 +00001272 OS << " dbg:";
Dan Gohman4b808b02009-12-05 00:20:51 +00001273 // Omit the directory, since it's usually long and uninteresting.
Devang Patel3c91b052010-03-08 20:52:55 +00001274 if (Scope.Verify())
Dan Gohman4b808b02009-12-05 00:20:51 +00001275 OS << Scope.getFilename();
1276 else
1277 OS << "<unknown>";
Chris Lattnerde4845c2010-04-02 19:42:39 +00001278 OS << ':' << debugLoc.getLine();
1279 if (debugLoc.getCol() != 0)
1280 OS << ':' << debugLoc.getCol();
Bill Wendlingb5ef2732009-02-19 21:44:55 +00001281 }
1282
Chris Lattner10491642002-10-30 00:48:05 +00001283 OS << "\n";
1284}
1285
Owen Andersonb487e722008-01-24 01:10:07 +00001286bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001287 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001288 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001289 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001290 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +00001291 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001292 SmallVector<unsigned,4> DeadOps;
Bill Wendling4a23d722008-03-03 22:14:33 +00001293 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1294 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenefb8e3e2009-08-04 20:09:25 +00001295 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001296 continue;
1297 unsigned Reg = MO.getReg();
1298 if (!Reg)
1299 continue;
Bill Wendling4a23d722008-03-03 22:14:33 +00001300
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001301 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001302 if (!Found) {
1303 if (MO.isKill())
1304 // The register is already marked kill.
1305 return true;
Jakob Stoklund Olesenece48182009-08-02 19:13:03 +00001306 if (isPhysReg && isRegTiedToDefOperand(i))
1307 // Two-address uses of physregs must not be marked kill.
1308 return true;
Dan Gohman3f629402008-09-03 15:56:16 +00001309 MO.setIsKill();
1310 Found = true;
1311 }
1312 } else if (hasAliases && MO.isKill() &&
1313 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001314 // A super-register kill already exists.
1315 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001316 return true;
1317 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001318 DeadOps.push_back(i);
Bill Wendling4a23d722008-03-03 22:14:33 +00001319 }
1320 }
1321
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001322 // Trim unneeded kill operands.
1323 while (!DeadOps.empty()) {
1324 unsigned OpIdx = DeadOps.back();
1325 if (getOperand(OpIdx).isImplicit())
1326 RemoveOperand(OpIdx);
1327 else
1328 getOperand(OpIdx).setIsKill(false);
1329 DeadOps.pop_back();
1330 }
1331
Bill Wendling4a23d722008-03-03 22:14:33 +00001332 // If not found, this means an alias of one of the operands is killed. Add a
Owen Andersonb487e722008-01-24 01:10:07 +00001333 // new implicit operand if required.
Dan Gohman3f629402008-09-03 15:56:16 +00001334 if (!Found && AddIfNotFound) {
Bill Wendling4a23d722008-03-03 22:14:33 +00001335 addOperand(MachineOperand::CreateReg(IncomingReg,
1336 false /*IsDef*/,
1337 true /*IsImp*/,
1338 true /*IsKill*/));
Owen Andersonb487e722008-01-24 01:10:07 +00001339 return true;
1340 }
Dan Gohman3f629402008-09-03 15:56:16 +00001341 return Found;
Owen Andersonb487e722008-01-24 01:10:07 +00001342}
1343
1344bool MachineInstr::addRegisterDead(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001345 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001346 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001347 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Evan Cheng01b2e232008-06-27 22:11:49 +00001348 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +00001349 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001350 SmallVector<unsigned,4> DeadOps;
Owen Andersonb487e722008-01-24 01:10:07 +00001351 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1352 MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001353 if (!MO.isReg() || !MO.isDef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001354 continue;
1355 unsigned Reg = MO.getReg();
Dan Gohman3f629402008-09-03 15:56:16 +00001356 if (!Reg)
1357 continue;
1358
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001359 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001360 if (!Found) {
1361 if (MO.isDead())
1362 // The register is already marked dead.
1363 return true;
1364 MO.setIsDead();
1365 Found = true;
1366 }
1367 } else if (hasAliases && MO.isDead() &&
1368 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001369 // There exists a super-register that's marked dead.
1370 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001371 return true;
Owen Anderson22ae9992008-08-14 18:34:18 +00001372 if (RegInfo->getSubRegisters(IncomingReg) &&
1373 RegInfo->getSuperRegisters(Reg) &&
1374 RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001375 DeadOps.push_back(i);
Owen Andersonb487e722008-01-24 01:10:07 +00001376 }
1377 }
1378
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001379 // Trim unneeded dead operands.
1380 while (!DeadOps.empty()) {
1381 unsigned OpIdx = DeadOps.back();
1382 if (getOperand(OpIdx).isImplicit())
1383 RemoveOperand(OpIdx);
1384 else
1385 getOperand(OpIdx).setIsDead(false);
1386 DeadOps.pop_back();
1387 }
1388
Dan Gohman3f629402008-09-03 15:56:16 +00001389 // If not found, this means an alias of one of the operands is dead. Add a
1390 // new implicit operand if required.
Chris Lattner31530612009-06-24 17:54:48 +00001391 if (Found || !AddIfNotFound)
1392 return Found;
1393
1394 addOperand(MachineOperand::CreateReg(IncomingReg,
1395 true /*IsDef*/,
1396 true /*IsImp*/,
1397 false /*IsKill*/,
1398 true /*IsDead*/));
1399 return true;
Owen Andersonb487e722008-01-24 01:10:07 +00001400}
Jakob Stoklund Olesen8efadf92010-01-06 00:29:28 +00001401
1402void MachineInstr::addRegisterDefined(unsigned IncomingReg,
1403 const TargetRegisterInfo *RegInfo) {
Jakob Stoklund Olesen63e6a482010-05-21 16:32:16 +00001404 if (TargetRegisterInfo::isPhysicalRegister(IncomingReg)) {
1405 MachineOperand *MO = findRegisterDefOperand(IncomingReg, false, RegInfo);
1406 if (MO)
1407 return;
1408 } else {
1409 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1410 const MachineOperand &MO = getOperand(i);
1411 if (MO.isReg() && MO.getReg() == IncomingReg && MO.isDef() &&
1412 MO.getSubReg() == 0)
1413 return;
1414 }
1415 }
1416 addOperand(MachineOperand::CreateReg(IncomingReg,
1417 true /*IsDef*/,
1418 true /*IsImp*/));
Jakob Stoklund Olesen8efadf92010-01-06 00:29:28 +00001419}
Evan Cheng67eaa082010-03-03 23:37:30 +00001420
1421unsigned
1422MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) {
1423 unsigned Hash = MI->getOpcode() * 37;
1424 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1425 const MachineOperand &MO = MI->getOperand(i);
1426 uint64_t Key = (uint64_t)MO.getType() << 32;
1427 switch (MO.getType()) {
Chris Lattner72aaa3c2010-03-13 08:14:18 +00001428 default: break;
1429 case MachineOperand::MO_Register:
1430 if (MO.isDef() && MO.getReg() &&
1431 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
1432 continue; // Skip virtual register defs.
1433 Key |= MO.getReg();
1434 break;
1435 case MachineOperand::MO_Immediate:
1436 Key |= MO.getImm();
1437 break;
1438 case MachineOperand::MO_FrameIndex:
1439 case MachineOperand::MO_ConstantPoolIndex:
1440 case MachineOperand::MO_JumpTableIndex:
1441 Key |= MO.getIndex();
1442 break;
1443 case MachineOperand::MO_MachineBasicBlock:
1444 Key |= DenseMapInfo<void*>::getHashValue(MO.getMBB());
1445 break;
1446 case MachineOperand::MO_GlobalAddress:
1447 Key |= DenseMapInfo<void*>::getHashValue(MO.getGlobal());
1448 break;
1449 case MachineOperand::MO_BlockAddress:
1450 Key |= DenseMapInfo<void*>::getHashValue(MO.getBlockAddress());
1451 break;
1452 case MachineOperand::MO_MCSymbol:
1453 Key |= DenseMapInfo<void*>::getHashValue(MO.getMCSymbol());
1454 break;
Evan Cheng67eaa082010-03-03 23:37:30 +00001455 }
1456 Key += ~(Key << 32);
1457 Key ^= (Key >> 22);
1458 Key += ~(Key << 13);
1459 Key ^= (Key >> 8);
1460 Key += (Key << 3);
1461 Key ^= (Key >> 15);
1462 Key += ~(Key << 27);
1463 Key ^= (Key >> 31);
1464 Hash = (unsigned)Key + Hash * 37;
1465 }
1466 return Hash;
1467}