blob: 6b46cb356c2560c02476da1abe9ad17e1eafb3f7 [file] [log] [blame]
Chris Lattner0cb9dd72008-01-01 20:36:19 +00001//===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaekee8f7c2f2004-02-13 04:39:32 +00009//
10// Methods common to all machine instructions.
11//
Chris Lattner959a5fb2002-08-09 20:08:06 +000012//===----------------------------------------------------------------------===//
Vikram S. Adveab9e5572001-07-21 12:41:50 +000013
Chris Lattner23fcc082001-09-07 17:18:30 +000014#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/FoldingSet.h"
16#include "llvm/ADT/Hashing.h"
17#include "llvm/Analysis/AliasAnalysis.h"
Evan Chenge9c46c22010-03-03 01:44:33 +000018#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner63f41ab2004-02-19 16:17:08 +000019#include "llvm/CodeGen/MachineFunction.h"
Reid Kleckner28865802016-04-14 18:29:59 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman48b185d2009-09-25 20:36:54 +000021#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +000022#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner961e7422008-01-01 01:12:31 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman2d489b52008-02-06 22:27:42 +000024#include "llvm/CodeGen/PseudoSourceValue.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Constants.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000026#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Function.h"
28#include "llvm/IR/InlineAsm.h"
29#include "llvm/IR/LLVMContext.h"
30#include "llvm/IR/Metadata.h"
31#include "llvm/IR/Module.h"
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +000032#include "llvm/IR/ModuleSlotTracker.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/Type.h"
34#include "llvm/IR/Value.h"
Evan Cheng6cc775f2011-06-28 19:10:37 +000035#include "llvm/MC/MCInstrDesc.h"
Chris Lattner6c604e32010-03-13 08:14:18 +000036#include "llvm/MC/MCSymbol.h"
Daniel Sanders1e97a0b2015-08-19 12:03:04 +000037#include "llvm/Support/CommandLine.h"
David Greene29388d62010-01-04 23:48:20 +000038#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000039#include "llvm/Support/ErrorHandling.h"
Dan Gohmanaedb4a62008-07-07 20:32:02 +000040#include "llvm/Support/MathExtras.h"
Chris Lattnera078d832008-08-24 20:37:32 +000041#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000042#include "llvm/Target/TargetInstrInfo.h"
43#include "llvm/Target/TargetMachine.h"
44#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000045#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner43df6c22004-02-23 18:38:20 +000046using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000047
Daniel Sanders1e97a0b2015-08-19 12:03:04 +000048static cl::opt<bool> PrintWholeRegMask(
49 "print-whole-regmask",
50 cl::desc("Print the full contents of regmask operands in IR dumps"),
51 cl::init(true), cl::Hidden);
52
Chris Lattner60055892007-12-30 21:56:09 +000053//===----------------------------------------------------------------------===//
54// MachineOperand Implementation
55//===----------------------------------------------------------------------===//
56
Chris Lattner961e7422008-01-01 01:12:31 +000057void MachineOperand::setReg(unsigned Reg) {
58 if (getReg() == Reg) return; // No change.
Jim Grosbachdee9e8a2011-08-24 16:44:17 +000059
Chris Lattner961e7422008-01-01 01:12:31 +000060 // Otherwise, we have to change the register. If this operand is embedded
61 // into a machine function, we need to update the old and new register's
62 // use/def lists.
63 if (MachineInstr *MI = getParent())
64 if (MachineBasicBlock *MBB = MI->getParent())
65 if (MachineFunction *MF = MBB->getParent()) {
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +000066 MachineRegisterInfo &MRI = MF->getRegInfo();
67 MRI.removeRegOperandFromUseList(this);
Jakob Stoklund Olesena4941692010-10-19 20:56:32 +000068 SmallContents.RegNo = Reg;
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +000069 MRI.addRegOperandToUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +000070 return;
71 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +000072
Chris Lattner961e7422008-01-01 01:12:31 +000073 // Otherwise, just change the register, no problem. :)
Jakob Stoklund Olesena4941692010-10-19 20:56:32 +000074 SmallContents.RegNo = Reg;
Chris Lattner961e7422008-01-01 01:12:31 +000075}
76
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +000077void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx,
78 const TargetRegisterInfo &TRI) {
79 assert(TargetRegisterInfo::isVirtualRegister(Reg));
80 if (SubIdx && getSubReg())
81 SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg());
82 setReg(Reg);
Jakob Stoklund Olesen7b0ac862010-06-01 22:39:25 +000083 if (SubIdx)
84 setSubReg(SubIdx);
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +000085}
86
87void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) {
88 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
89 if (getSubReg()) {
90 Reg = TRI.getSubReg(Reg, getSubReg());
Jakob Stoklund Olesen89bd2ae2011-05-08 19:21:08 +000091 // Note that getSubReg() may return 0 if the sub-register doesn't exist.
92 // That won't happen in legal code.
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +000093 setSubReg(0);
94 }
95 setReg(Reg);
96}
97
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +000098/// Change a def to a use, or a use to a def.
99void MachineOperand::setIsDef(bool Val) {
100 assert(isReg() && "Wrong MachineOperand accessor");
101 assert((!Val || !isDebug()) && "Marking a debug operation as def");
102 if (IsDef == Val)
103 return;
104 // MRI may keep uses and defs in different list positions.
105 if (MachineInstr *MI = getParent())
106 if (MachineBasicBlock *MBB = MI->getParent())
107 if (MachineFunction *MF = MBB->getParent()) {
108 MachineRegisterInfo &MRI = MF->getRegInfo();
109 MRI.removeRegOperandFromUseList(this);
110 IsDef = Val;
111 MRI.addRegOperandToUseList(this);
112 return;
113 }
114 IsDef = Val;
115}
116
Matt Arsenault93ffe582014-09-28 19:24:59 +0000117// If this operand is currently a register operand, and if this is in a
118// function, deregister the operand from the register's use/def list.
119void MachineOperand::removeRegFromUses() {
120 if (!isReg() || !isOnRegUseList())
121 return;
122
123 if (MachineInstr *MI = getParent()) {
124 if (MachineBasicBlock *MBB = MI->getParent()) {
125 if (MachineFunction *MF = MBB->getParent())
126 MF->getRegInfo().removeRegOperandFromUseList(this);
127 }
128 }
129}
130
Chris Lattner961e7422008-01-01 01:12:31 +0000131/// ChangeToImmediate - Replace this operand with a new immediate operand of
132/// the specified value. If an operand is known to be an immediate already,
133/// the setImm method should be used.
134void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000135 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
Matt Arsenault93ffe582014-09-28 19:24:59 +0000136
137 removeRegFromUses();
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000138
Chris Lattner961e7422008-01-01 01:12:31 +0000139 OpKind = MO_Immediate;
140 Contents.ImmVal = ImmVal;
141}
142
Matt Arsenault93ffe582014-09-28 19:24:59 +0000143void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm) {
144 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
145
146 removeRegFromUses();
147
148 OpKind = MO_FPImmediate;
149 Contents.CFP = FPImm;
150}
151
Matt Arsenault633dba42015-05-06 17:05:54 +0000152void MachineOperand::ChangeToES(const char *SymName, unsigned char TargetFlags) {
153 assert((!isReg() || !isTied()) &&
154 "Cannot change a tied operand into an external symbol");
155
156 removeRegFromUses();
157
158 OpKind = MO_ExternalSymbol;
159 Contents.OffsetedInfo.Val.SymbolName = SymName;
160 setOffset(0); // Offset is always 0.
161 setTargetFlags(TargetFlags);
162}
163
164void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym) {
165 assert((!isReg() || !isTied()) &&
166 "Cannot change a tied operand into an MCSymbol");
167
168 removeRegFromUses();
169
170 OpKind = MO_MCSymbol;
171 Contents.Sym = Sym;
172}
173
Chris Lattner961e7422008-01-01 01:12:31 +0000174/// ChangeToRegister - Replace this operand with a new register operand of
175/// the specified value. If an operand is known to be an register already,
176/// the setReg method should be used.
177void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Dale Johannesend40d42c2010-02-10 00:41:49 +0000178 bool isKill, bool isDead, bool isUndef,
179 bool isDebug) {
Craig Topperc0196b12014-04-14 00:51:57 +0000180 MachineRegisterInfo *RegInfo = nullptr;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000181 if (MachineInstr *MI = getParent())
182 if (MachineBasicBlock *MBB = MI->getParent())
183 if (MachineFunction *MF = MBB->getParent())
184 RegInfo = &MF->getRegInfo();
185 // If this operand is already a register operand, remove it from the
Chris Lattner961e7422008-01-01 01:12:31 +0000186 // register's use/def lists.
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000187 bool WasReg = isReg();
188 if (RegInfo && WasReg)
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000189 RegInfo->removeRegOperandFromUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +0000190
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000191 // Change this to a register and set the reg#.
192 OpKind = MO_Register;
193 SmallContents.RegNo = Reg;
Jakob Stoklund Olesena1b246d2013-01-07 23:21:44 +0000194 SubReg_TargetFlags = 0;
Chris Lattner961e7422008-01-01 01:12:31 +0000195 IsDef = isDef;
196 IsImp = isImp;
197 IsKill = isKill;
198 IsDead = isDead;
Evan Cheng0dc101b2009-06-30 08:49:04 +0000199 IsUndef = isUndef;
Jakob Stoklund Olesenb0d91ab2011-12-07 00:22:07 +0000200 IsInternalRead = false;
Dale Johannesenc0d712d2008-09-14 01:44:36 +0000201 IsEarlyClobber = false;
Dale Johannesend40d42c2010-02-10 00:41:49 +0000202 IsDebug = isDebug;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000203 // Ensure isOnRegUseList() returns false.
Craig Topperc0196b12014-04-14 00:51:57 +0000204 Contents.Reg.Prev = nullptr;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000205 // Preserve the tie when the operand was already a register.
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000206 if (!WasReg)
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000207 TiedTo = 0;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000208
209 // If this operand is embedded in a function, add the operand to the
210 // register's use/def list.
211 if (RegInfo)
212 RegInfo->addRegOperandToUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +0000213}
214
Chris Lattner60055892007-12-30 21:56:09 +0000215/// isIdenticalTo - Return true if this operand is identical to the specified
Chandler Carruth264854f2012-07-05 11:06:22 +0000216/// operand. Note that this should stay in sync with the hash_value overload
217/// below.
Chris Lattner60055892007-12-30 21:56:09 +0000218bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
Chris Lattnerfd682802009-06-24 17:54:48 +0000219 if (getType() != Other.getType() ||
220 getTargetFlags() != Other.getTargetFlags())
221 return false;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000222
Chris Lattner60055892007-12-30 21:56:09 +0000223 switch (getType()) {
Chris Lattner60055892007-12-30 21:56:09 +0000224 case MachineOperand::MO_Register:
225 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
226 getSubReg() == Other.getSubReg();
227 case MachineOperand::MO_Immediate:
228 return getImm() == Other.getImm();
Cameron Zwarich7da0f9a2011-07-01 23:45:21 +0000229 case MachineOperand::MO_CImmediate:
230 return getCImm() == Other.getCImm();
Nate Begeman26b76b62008-02-14 07:39:30 +0000231 case MachineOperand::MO_FPImmediate:
232 return getFPImm() == Other.getFPImm();
Chris Lattner60055892007-12-30 21:56:09 +0000233 case MachineOperand::MO_MachineBasicBlock:
234 return getMBB() == Other.getMBB();
235 case MachineOperand::MO_FrameIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000236 return getIndex() == Other.getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000237 case MachineOperand::MO_ConstantPoolIndex:
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000238 case MachineOperand::MO_TargetIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000239 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattner60055892007-12-30 21:56:09 +0000240 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000241 return getIndex() == Other.getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000242 case MachineOperand::MO_GlobalAddress:
243 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
244 case MachineOperand::MO_ExternalSymbol:
245 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
246 getOffset() == Other.getOffset();
Dan Gohman6c938802009-10-30 01:27:03 +0000247 case MachineOperand::MO_BlockAddress:
Michael Liaoabb87d42012-09-12 21:43:09 +0000248 return getBlockAddress() == Other.getBlockAddress() &&
249 getOffset() == Other.getOffset();
Juergen Ributzkae8294752013-12-14 06:53:06 +0000250 case MachineOperand::MO_RegisterMask:
251 case MachineOperand::MO_RegisterLiveOut:
Jakob Stoklund Olesen374ed322012-01-16 19:22:00 +0000252 return getRegMask() == Other.getRegMask();
Chris Lattner6c604e32010-03-13 08:14:18 +0000253 case MachineOperand::MO_MCSymbol:
254 return getMCSymbol() == Other.getMCSymbol();
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000255 case MachineOperand::MO_CFIIndex:
256 return getCFIIndex() == Other.getCFIIndex();
Chris Lattnerf839ee02010-04-07 18:03:19 +0000257 case MachineOperand::MO_Metadata:
258 return getMetadata() == Other.getMetadata();
Chris Lattner60055892007-12-30 21:56:09 +0000259 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000260 llvm_unreachable("Invalid machine operand type");
Chris Lattner60055892007-12-30 21:56:09 +0000261}
262
Chandler Carruth264854f2012-07-05 11:06:22 +0000263// Note: this must stay exactly in sync with isIdenticalTo above.
264hash_code llvm::hash_value(const MachineOperand &MO) {
265 switch (MO.getType()) {
266 case MachineOperand::MO_Register:
Jakob Stoklund Olesendba99d02012-08-28 18:05:48 +0000267 // Register operands don't have target flags.
268 return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef());
Chandler Carruth264854f2012-07-05 11:06:22 +0000269 case MachineOperand::MO_Immediate:
270 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm());
271 case MachineOperand::MO_CImmediate:
272 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm());
273 case MachineOperand::MO_FPImmediate:
274 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm());
275 case MachineOperand::MO_MachineBasicBlock:
276 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB());
277 case MachineOperand::MO_FrameIndex:
278 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
279 case MachineOperand::MO_ConstantPoolIndex:
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000280 case MachineOperand::MO_TargetIndex:
Chandler Carruth264854f2012-07-05 11:06:22 +0000281 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(),
282 MO.getOffset());
283 case MachineOperand::MO_JumpTableIndex:
284 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
285 case MachineOperand::MO_ExternalSymbol:
286 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(),
287 MO.getSymbolName());
288 case MachineOperand::MO_GlobalAddress:
289 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(),
290 MO.getOffset());
291 case MachineOperand::MO_BlockAddress:
292 return hash_combine(MO.getType(), MO.getTargetFlags(),
Michael Liaoabb87d42012-09-12 21:43:09 +0000293 MO.getBlockAddress(), MO.getOffset());
Chandler Carruth264854f2012-07-05 11:06:22 +0000294 case MachineOperand::MO_RegisterMask:
Juergen Ributzkae8294752013-12-14 06:53:06 +0000295 case MachineOperand::MO_RegisterLiveOut:
Chandler Carruth264854f2012-07-05 11:06:22 +0000296 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask());
297 case MachineOperand::MO_Metadata:
298 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata());
299 case MachineOperand::MO_MCSymbol:
300 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol());
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000301 case MachineOperand::MO_CFIIndex:
302 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex());
Chandler Carruth264854f2012-07-05 11:06:22 +0000303 }
304 llvm_unreachable("Invalid machine operand type");
305}
306
Eric Christopher1cdefae2015-02-27 00:11:34 +0000307void MachineOperand::print(raw_ostream &OS,
308 const TargetRegisterInfo *TRI) const {
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000309 ModuleSlotTracker DummyMST(nullptr);
310 print(OS, DummyMST, TRI);
311}
312
313void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
314 const TargetRegisterInfo *TRI) const {
Chris Lattner60055892007-12-30 21:56:09 +0000315 switch (getType()) {
316 case MachineOperand::MO_Register:
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000317 OS << PrintReg(getReg(), TRI, getSubReg());
Dan Gohman0ab11442008-12-18 21:51:27 +0000318
Evan Cheng0dc101b2009-06-30 08:49:04 +0000319 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000320 isInternalRead() || isEarlyClobber() || isTied()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000321 OS << '<';
Chris Lattner60055892007-12-30 21:56:09 +0000322 bool NeedComma = false;
Evan Cheng70b1fa52009-10-14 23:37:31 +0000323 if (isDef()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000324 if (NeedComma) OS << ',';
Dale Johannesen1f3ab862008-09-12 17:49:03 +0000325 if (isEarlyClobber())
326 OS << "earlyclobber,";
Evan Cheng70b1fa52009-10-14 23:37:31 +0000327 if (isImplicit())
328 OS << "imp-";
Chris Lattner60055892007-12-30 21:56:09 +0000329 OS << "def";
330 NeedComma = true;
Jakob Stoklund Olesen7111a632012-04-20 21:45:33 +0000331 // <def,read-undef> only makes sense when getSubReg() is set.
332 // Don't clutter the output otherwise.
333 if (isUndef() && getSubReg())
334 OS << ",read-undef";
Evan Chengf781bd82009-10-21 07:56:02 +0000335 } else if (isImplicit()) {
Craig Topper9a9d58a2015-05-16 05:42:08 +0000336 OS << "imp-use";
337 NeedComma = true;
Evan Chengf781bd82009-10-21 07:56:02 +0000338 }
Evan Cheng70b1fa52009-10-14 23:37:31 +0000339
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000340 if (isKill()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000341 if (NeedComma) OS << ',';
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000342 OS << "kill";
343 NeedComma = true;
344 }
345 if (isDead()) {
346 if (NeedComma) OS << ',';
347 OS << "dead";
348 NeedComma = true;
349 }
350 if (isUndef() && isUse()) {
351 if (NeedComma) OS << ',';
352 OS << "undef";
353 NeedComma = true;
354 }
355 if (isInternalRead()) {
356 if (NeedComma) OS << ',';
357 OS << "internal";
358 NeedComma = true;
359 }
360 if (isTied()) {
361 if (NeedComma) OS << ',';
362 OS << "tied";
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000363 if (TiedTo != 15)
364 OS << unsigned(TiedTo - 1);
Chris Lattner60055892007-12-30 21:56:09 +0000365 }
Chris Lattnerfd682802009-06-24 17:54:48 +0000366 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000367 }
368 break;
369 case MachineOperand::MO_Immediate:
370 OS << getImm();
371 break;
Devang Patelf071d722011-06-24 20:46:11 +0000372 case MachineOperand::MO_CImmediate:
373 getCImm()->getValue().print(OS, false);
374 break;
Nate Begeman26b76b62008-02-14 07:39:30 +0000375 case MachineOperand::MO_FPImmediate:
Matt Arsenault59239732016-02-05 00:50:18 +0000376 if (getFPImm()->getType()->isFloatTy()) {
Nate Begeman26b76b62008-02-14 07:39:30 +0000377 OS << getFPImm()->getValueAPF().convertToFloat();
Matt Arsenault59239732016-02-05 00:50:18 +0000378 } else if (getFPImm()->getType()->isHalfTy()) {
379 APFloat APF = getFPImm()->getValueAPF();
380 bool Unused;
381 APF.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &Unused);
382 OS << "half " << APF.convertToFloat();
383 } else {
Nate Begeman26b76b62008-02-14 07:39:30 +0000384 OS << getFPImm()->getValueAPF().convertToDouble();
Matt Arsenault59239732016-02-05 00:50:18 +0000385 }
Nate Begeman26b76b62008-02-14 07:39:30 +0000386 break;
Chris Lattner60055892007-12-30 21:56:09 +0000387 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman34341e62009-10-31 20:19:03 +0000388 OS << "<BB#" << getMBB()->getNumber() << ">";
Chris Lattner60055892007-12-30 21:56:09 +0000389 break;
390 case MachineOperand::MO_FrameIndex:
Chris Lattnerfd682802009-06-24 17:54:48 +0000391 OS << "<fi#" << getIndex() << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000392 break;
393 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000394 OS << "<cp#" << getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000395 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000396 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000397 break;
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000398 case MachineOperand::MO_TargetIndex:
399 OS << "<ti#" << getIndex();
400 if (getOffset()) OS << "+" << getOffset();
401 OS << '>';
402 break;
Chris Lattner60055892007-12-30 21:56:09 +0000403 case MachineOperand::MO_JumpTableIndex:
Chris Lattnerfd682802009-06-24 17:54:48 +0000404 OS << "<jt#" << getIndex() << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000405 break;
406 case MachineOperand::MO_GlobalAddress:
Dan Gohman0080ee22009-11-06 18:03:10 +0000407 OS << "<ga:";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000408 getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Chris Lattner60055892007-12-30 21:56:09 +0000409 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000410 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000411 break;
412 case MachineOperand::MO_ExternalSymbol:
413 OS << "<es:" << getSymbolName();
414 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000415 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000416 break;
Dan Gohman6c938802009-10-30 01:27:03 +0000417 case MachineOperand::MO_BlockAddress:
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000418 OS << '<';
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000419 getBlockAddress()->printAsOperand(OS, /*PrintType=*/false, MST);
Michael Liaoabb87d42012-09-12 21:43:09 +0000420 if (getOffset()) OS << "+" << getOffset();
Dan Gohman6c938802009-10-30 01:27:03 +0000421 OS << '>';
422 break;
Daniel Sanders1e97a0b2015-08-19 12:03:04 +0000423 case MachineOperand::MO_RegisterMask: {
424 unsigned NumRegsInMask = 0;
425 unsigned NumRegsEmitted = 0;
426 OS << "<regmask";
427 for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {
428 unsigned MaskWord = i / 32;
429 unsigned MaskBit = i % 32;
430 if (getRegMask()[MaskWord] & (1 << MaskBit)) {
431 if (PrintWholeRegMask || NumRegsEmitted <= 10) {
432 OS << " " << PrintReg(i, TRI);
433 NumRegsEmitted++;
434 }
435 NumRegsInMask++;
436 }
437 }
438 if (NumRegsEmitted != NumRegsInMask)
439 OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more...";
440 OS << ">";
Jakob Stoklund Olesen374ed322012-01-16 19:22:00 +0000441 break;
Daniel Sanders1e97a0b2015-08-19 12:03:04 +0000442 }
Juergen Ributzkae8294752013-12-14 06:53:06 +0000443 case MachineOperand::MO_RegisterLiveOut:
444 OS << "<regliveout>";
445 break;
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000446 case MachineOperand::MO_Metadata:
447 OS << '<';
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000448 getMetadata()->printAsOperand(OS, MST);
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000449 OS << '>';
450 break;
Chris Lattner6c604e32010-03-13 08:14:18 +0000451 case MachineOperand::MO_MCSymbol:
452 OS << "<MCSym=" << *getMCSymbol() << '>';
453 break;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000454 case MachineOperand::MO_CFIIndex:
455 OS << "<call frame instruction>";
456 break;
Chris Lattner60055892007-12-30 21:56:09 +0000457 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000458
Chris Lattnerfd682802009-06-24 17:54:48 +0000459 if (unsigned TF = getTargetFlags())
460 OS << "[TF=" << TF << ']';
Chris Lattner60055892007-12-30 21:56:09 +0000461}
462
463//===----------------------------------------------------------------------===//
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000464// MachineMemOperand Implementation
465//===----------------------------------------------------------------------===//
466
Chris Lattnerde93bb02010-09-21 05:39:30 +0000467/// getAddrSpace - Return the LLVM IR address space number that this pointer
468/// points into.
469unsigned MachinePointerInfo::getAddrSpace() const {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000470 if (V.isNull() || V.is<const PseudoSourceValue*>()) return 0;
471 return cast<PointerType>(V.get<const Value*>()->getType())->getAddressSpace();
Chris Lattnerde93bb02010-09-21 05:39:30 +0000472}
473
Chris Lattner82fd06d2010-09-21 06:22:23 +0000474/// getConstantPool - Return a MachinePointerInfo record that refers to the
475/// constant pool.
Alex Lorenze40c8a22015-08-11 23:09:45 +0000476MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) {
477 return MachinePointerInfo(MF.getPSVManager().getConstantPool());
Chris Lattner82fd06d2010-09-21 06:22:23 +0000478}
479
480/// getFixedStack - Return a MachinePointerInfo record that refers to the
481/// the specified FrameIndex.
Alex Lorenze40c8a22015-08-11 23:09:45 +0000482MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF,
483 int FI, int64_t Offset) {
484 return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset);
Chris Lattner82fd06d2010-09-21 06:22:23 +0000485}
486
Alex Lorenze40c8a22015-08-11 23:09:45 +0000487MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) {
488 return MachinePointerInfo(MF.getPSVManager().getJumpTable());
Chris Lattner50287ea2010-09-21 06:43:24 +0000489}
490
Alex Lorenze40c8a22015-08-11 23:09:45 +0000491MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) {
492 return MachinePointerInfo(MF.getPSVManager().getGOT());
Chris Lattner50287ea2010-09-21 06:43:24 +0000493}
Chris Lattnerde93bb02010-09-21 05:39:30 +0000494
Alex Lorenze40c8a22015-08-11 23:09:45 +0000495MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF,
496 int64_t Offset) {
497 return MachinePointerInfo(MF.getPSVManager().getStack(), Offset);
Chris Lattner886250c2010-09-21 18:51:21 +0000498}
499
Chris Lattner00ca0b82010-09-21 04:32:08 +0000500MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, unsigned f,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000501 uint64_t s, unsigned int a,
Hal Finkelcc39b672014-07-24 12:16:19 +0000502 const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +0000503 const MDNode *Ranges)
Chris Lattner00ca0b82010-09-21 04:32:08 +0000504 : PtrInfo(ptrinfo), Size(s),
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000505 Flags((f & ((1 << MOMaxBits) - 1)) | ((Log2_32(a) + 1) << MOMaxBits)),
Hal Finkelcc39b672014-07-24 12:16:19 +0000506 AAInfo(AAInfo), Ranges(Ranges) {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000507 assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue*>() ||
508 isa<PointerType>(PtrInfo.V.get<const Value*>()->getType())) &&
Chris Lattner00ca0b82010-09-21 04:32:08 +0000509 "invalid pointer value");
Dan Gohmane7c82422009-09-21 19:47:04 +0000510 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
Dan Gohmanbf98f682008-07-16 15:56:42 +0000511 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000512}
513
Dan Gohman2da2bed2008-08-20 15:58:01 +0000514/// Profile - Gather unique data for the object.
515///
516void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
Chris Lattner187f6532010-09-21 04:23:39 +0000517 ID.AddInteger(getOffset());
Dan Gohman2da2bed2008-08-20 15:58:01 +0000518 ID.AddInteger(Size);
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000519 ID.AddPointer(getOpaqueValue());
Dan Gohman2da2bed2008-08-20 15:58:01 +0000520 ID.AddInteger(Flags);
521}
522
Dan Gohman48b185d2009-09-25 20:36:54 +0000523void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
524 // The Value and Offset may differ due to CSE. But the flags and size
525 // should be the same.
526 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
527 assert(MMO->getSize() == getSize() && "Size mismatch!");
528
529 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
530 // Update the alignment value.
David Greene3a0412f2010-02-15 16:48:31 +0000531 Flags = (Flags & ((1 << MOMaxBits) - 1)) |
532 ((Log2_32(MMO->getBaseAlignment()) + 1) << MOMaxBits);
Dan Gohman48b185d2009-09-25 20:36:54 +0000533 // Also update the base and offset, because the new alignment may
534 // not be applicable with the old ones.
Chris Lattner187f6532010-09-21 04:23:39 +0000535 PtrInfo = MMO->PtrInfo;
Dan Gohman48b185d2009-09-25 20:36:54 +0000536 }
537}
538
Dan Gohman5a6b11c2009-09-25 23:33:20 +0000539/// getAlignment - Return the minimum known alignment in bytes of the
540/// actual memory reference.
541uint64_t MachineMemOperand::getAlignment() const {
542 return MinAlign(getBaseAlignment(), getOffset());
543}
544
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000545void MachineMemOperand::print(raw_ostream &OS) const {
546 ModuleSlotTracker DummyMST(nullptr);
547 print(OS, DummyMST);
548}
549void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST) const {
550 assert((isLoad() || isStore()) &&
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000551 "SV has to be a load, store or both.");
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000552
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000553 if (isVolatile())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000554 OS << "Volatile ";
555
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000556 if (isLoad())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000557 OS << "LD";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000558 if (isStore())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000559 OS << "ST";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000560 OS << getSize();
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000561
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000562 // Print the address information.
563 OS << "[";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000564 if (const Value *V = getValue())
565 V->printAsOperand(OS, /*PrintType=*/false, MST);
566 else if (const PseudoSourceValue *PSV = getPseudoValue())
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000567 PSV->printCustom(OS);
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000568 else
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000569 OS << "<unknown>";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000570
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000571 unsigned AS = getAddrSpace();
Matt Arsenault68c38fd2013-12-14 00:24:02 +0000572 if (AS != 0)
573 OS << "(addrspace=" << AS << ')';
574
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000575 // If the alignment of the memory reference itself differs from the alignment
576 // of the base pointer, print the base alignment explicitly, next to the base
577 // pointer.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000578 if (getBaseAlignment() != getAlignment())
579 OS << "(align=" << getBaseAlignment() << ")";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000580
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000581 if (getOffset() != 0)
582 OS << "+" << getOffset();
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000583 OS << "]";
584
585 // Print the alignment of the reference.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000586 if (getBaseAlignment() != getAlignment() || getBaseAlignment() != getSize())
587 OS << "(align=" << getAlignment() << ")";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000588
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000589 // Print TBAA info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000590 if (const MDNode *TBAAInfo = getAAInfo().TBAA) {
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000591 OS << "(tbaa=";
592 if (TBAAInfo->getNumOperands() > 0)
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000593 TBAAInfo->getOperand(0)->printAsOperand(OS, MST);
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000594 else
595 OS << "<unknown>";
596 OS << ")";
597 }
598
Hal Finkel94146652014-07-24 14:25:39 +0000599 // Print AA scope info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000600 if (const MDNode *ScopeInfo = getAAInfo().Scope) {
Hal Finkel94146652014-07-24 14:25:39 +0000601 OS << "(alias.scope=";
602 if (ScopeInfo->getNumOperands() > 0)
603 for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000604 ScopeInfo->getOperand(i)->printAsOperand(OS, MST);
Hal Finkel94146652014-07-24 14:25:39 +0000605 if (i != ie-1)
606 OS << ",";
607 }
608 else
609 OS << "<unknown>";
610 OS << ")";
611 }
612
613 // Print AA noalias scope info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000614 if (const MDNode *NoAliasInfo = getAAInfo().NoAlias) {
Hal Finkel94146652014-07-24 14:25:39 +0000615 OS << "(noalias=";
616 if (NoAliasInfo->getNumOperands() > 0)
617 for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000618 NoAliasInfo->getOperand(i)->printAsOperand(OS, MST);
Hal Finkel94146652014-07-24 14:25:39 +0000619 if (i != ie-1)
620 OS << ",";
621 }
622 else
623 OS << "<unknown>";
624 OS << ")";
625 }
626
Bill Wendling9f638ab2011-04-29 23:45:22 +0000627 // Print nontemporal info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000628 if (isNonTemporal())
Bill Wendling9f638ab2011-04-29 23:45:22 +0000629 OS << "(nontemporal)";
630
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000631 if (isInvariant())
Matt Arsenault572c29a2015-06-26 19:00:11 +0000632 OS << "(invariant)";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000633}
634
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000635//===----------------------------------------------------------------------===//
Chris Lattner60055892007-12-30 21:56:09 +0000636// MachineInstr Implementation
637//===----------------------------------------------------------------------===//
638
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000639void MachineInstr::addImplicitDefUseOperands(MachineFunction &MF) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000640 if (MCID->ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000641 for (const MCPhysReg *ImpDefs = MCID->getImplicitDefs(); *ImpDefs;
642 ++ImpDefs)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000643 addOperand(MF, MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng6cc775f2011-06-28 19:10:37 +0000644 if (MCID->ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000645 for (const MCPhysReg *ImpUses = MCID->getImplicitUses(); *ImpUses;
646 ++ImpUses)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000647 addOperand(MF, MachineOperand::CreateReg(*ImpUses, false, true));
Evan Cheng77af6ac2006-11-13 23:34:06 +0000648}
649
Bob Wilson406f2702010-04-09 04:34:03 +0000650/// MachineInstr ctor - This constructor creates a MachineInstr and adds the
651/// implicit operands. It reserves space for the number of operands specified by
Evan Cheng6cc775f2011-06-28 19:10:37 +0000652/// the MCInstrDesc.
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000653MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid,
Benjamin Kramera9591b52015-02-07 12:28:15 +0000654 DebugLoc dl, bool NoImp)
655 : MCID(&tid), Parent(nullptr), Operands(nullptr), NumOperands(0), Flags(0),
656 AsmPrinterFlags(0), NumMemRefs(0), MemRefs(nullptr),
Quentin Colombet98551112016-02-11 18:22:37 +0000657 debugLoc(std::move(dl))
658#ifdef LLVM_BUILD_GLOBAL_ISEL
659 ,
660 Ty(nullptr)
661#endif
662{
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000663 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
664
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000665 // Reserve space for the expected number of operands.
666 if (unsigned NumOps = MCID->getNumOperands() +
667 MCID->getNumImplicitDefs() + MCID->getNumImplicitUses()) {
668 CapOperands = OperandCapacity::get(NumOps);
669 Operands = MF.allocateOperandArray(CapOperands);
670 }
671
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000672 if (!NoImp)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000673 addImplicitDefUseOperands(MF);
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000674}
675
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000676/// MachineInstr ctor - Copies MachineInstr arg exactly
677///
Evan Chenga7a20c42008-07-19 00:37:25 +0000678MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Quentin Colombet98551112016-02-11 18:22:37 +0000679 : MCID(&MI.getDesc()), Parent(nullptr), Operands(nullptr), NumOperands(0),
680 Flags(0), AsmPrinterFlags(0), NumMemRefs(MI.NumMemRefs),
681 MemRefs(MI.MemRefs), debugLoc(MI.getDebugLoc())
682#ifdef LLVM_BUILD_GLOBAL_ISEL
683 ,
684 Ty(nullptr)
685#endif
686{
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000687 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
688
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000689 CapOperands = OperandCapacity::get(MI.getNumOperands());
690 Operands = MF.allocateOperandArray(CapOperands);
Tanya Lattner9953d862004-05-23 20:58:02 +0000691
Jakob Stoklund Olesendc5285f2013-01-05 05:05:51 +0000692 // Copy operands.
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000693 for (const MachineOperand &MO : MI.operands())
694 addOperand(MF, MO);
Tanya Lattnerbcee21b2004-05-24 03:14:18 +0000695
Jakob Stoklund Olesena33f5042012-12-18 21:36:05 +0000696 // Copy all the sensible flags.
697 setFlags(MI.Flags);
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +0000698}
699
Chris Lattner961e7422008-01-01 01:12:31 +0000700/// getRegInfo - If this instruction is embedded into a MachineFunction,
701/// return the MachineRegisterInfo object for the current function, otherwise
702/// return null.
703MachineRegisterInfo *MachineInstr::getRegInfo() {
704 if (MachineBasicBlock *MBB = getParent())
Dan Gohmanf188fa42008-07-08 23:59:09 +0000705 return &MBB->getParent()->getRegInfo();
Craig Topperc0196b12014-04-14 00:51:57 +0000706 return nullptr;
Chris Lattner961e7422008-01-01 01:12:31 +0000707}
708
Quentin Colombet41bea872016-03-07 22:47:23 +0000709// Implement dummy setter and getter for type when
710// global-isel is not built.
711// The proper implementation is WIP and is tracked here:
712// PR26576.
713#ifndef LLVM_BUILD_GLOBAL_ISEL
714void MachineInstr::setType(Type *Ty) {}
715
716Type *MachineInstr::getType() const { return nullptr; }
717
718#else
719void MachineInstr::setType(Type *Ty) {
720 assert((!Ty || isPreISelGenericOpcode(getOpcode())) &&
721 "Non generic instructions are not supposed to be typed");
722 this->Ty = Ty;
723}
724
725Type *MachineInstr::getType() const { return Ty; }
726#endif // LLVM_BUILD_GLOBAL_ISEL
727
Chris Lattner961e7422008-01-01 01:12:31 +0000728/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
729/// this instruction from their respective use lists. This requires that the
730/// operands already be on their use lists.
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000731void MachineInstr::RemoveRegOperandsFromUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000732 for (MachineOperand &MO : operands())
733 if (MO.isReg())
734 MRI.removeRegOperandFromUseList(&MO);
Chris Lattner961e7422008-01-01 01:12:31 +0000735}
736
737/// AddRegOperandsToUseLists - Add all of the register operands in
738/// this instruction from their respective use lists. This requires that the
739/// operands not be on their use lists yet.
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000740void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000741 for (MachineOperand &MO : operands())
742 if (MO.isReg())
743 MRI.addRegOperandToUseList(&MO);
Chris Lattner961e7422008-01-01 01:12:31 +0000744}
745
Jakob Stoklund Olesen2455b5852012-12-20 22:54:05 +0000746void MachineInstr::addOperand(const MachineOperand &Op) {
747 MachineBasicBlock *MBB = getParent();
748 assert(MBB && "Use MachineInstrBuilder to add operands to dangling instrs");
749 MachineFunction *MF = MBB->getParent();
750 assert(MF && "Use MachineInstrBuilder to add operands to dangling instrs");
751 addOperand(*MF, Op);
752}
753
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000754/// Move NumOps MachineOperands from Src to Dst, with support for overlapping
755/// ranges. If MRI is non-null also update use-def chains.
756static void moveOperands(MachineOperand *Dst, MachineOperand *Src,
757 unsigned NumOps, MachineRegisterInfo *MRI) {
758 if (MRI)
759 return MRI->moveOperands(Dst, Src, NumOps);
760
JF Bastiena874d1a2016-03-26 18:20:02 +0000761 // MachineOperand is a trivially copyable type so we can just use memmove.
Benjamin Kramer5c0e64f2015-02-21 16:22:48 +0000762 std::memmove(Dst, Src, NumOps * sizeof(MachineOperand));
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000763}
764
Chris Lattner961e7422008-01-01 01:12:31 +0000765/// addOperand - Add the specified operand to the instruction. If it is an
766/// implicit operand, it is added to the end of the operand list. If it is
767/// an explicit operand it is added at the end of the explicit operand list
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000768/// (before the first implicit operand).
Jakob Stoklund Olesen2455b5852012-12-20 22:54:05 +0000769void MachineInstr::addOperand(MachineFunction &MF, const MachineOperand &Op) {
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000770 assert(MCID && "Cannot add operands before providing an instr descriptor");
Dan Gohman9356d8f2008-12-09 22:45:08 +0000771
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000772 // Check if we're adding one of our existing operands.
773 if (&Op >= Operands && &Op < Operands + NumOperands) {
774 // This is unusual: MI->addOperand(MI->getOperand(i)).
775 // If adding Op requires reallocating or moving existing operands around,
776 // the Op reference could go stale. Support it by copying Op.
777 MachineOperand CopyOp(Op);
778 return addOperand(MF, CopyOp);
779 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000780
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000781 // Find the insert location for the new operand. Implicit registers go at
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000782 // the end, everything else goes before the implicit regs.
783 //
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000784 // FIXME: Allow mixed explicit and implicit operands on inline asm.
785 // InstrEmitter::EmitSpecialNode() is marking inline asm clobbers as
786 // implicit-defs, but they must not be moved around. See the FIXME in
787 // InstrEmitter.cpp.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000788 unsigned OpNo = getNumOperands();
789 bool isImpReg = Op.isReg() && Op.isImplicit();
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000790 if (!isImpReg && !isInlineAsm()) {
791 while (OpNo && Operands[OpNo-1].isReg() && Operands[OpNo-1].isImplicit()) {
792 --OpNo;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000793 assert(!Operands[OpNo].isTied() && "Cannot move tied operands");
Chris Lattner961e7422008-01-01 01:12:31 +0000794 }
795 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000796
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000797#ifndef NDEBUG
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000798 bool isMetaDataOp = Op.getType() == MachineOperand::MO_Metadata;
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000799 // OpNo now points as the desired insertion point. Unless this is a variadic
800 // instruction, only implicit regs are allowed beyond MCID->getNumOperands().
Jakob Stoklund Olesenc300ef02012-07-04 23:53:23 +0000801 // RegMask operands go between the explicit and implicit operands.
802 assert((isImpReg || Op.isRegMask() || MCID->isVariadic() ||
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000803 OpNo < MCID->getNumOperands() || isMetaDataOp) &&
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000804 "Trying to add an operand to a machine instr that is already done!");
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000805#endif
Chris Lattner961e7422008-01-01 01:12:31 +0000806
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000807 MachineRegisterInfo *MRI = getRegInfo();
Chris Lattner961e7422008-01-01 01:12:31 +0000808
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000809 // Determine if the Operands array needs to be reallocated.
810 // Save the old capacity and operand array.
811 OperandCapacity OldCap = CapOperands;
812 MachineOperand *OldOperands = Operands;
813 if (!OldOperands || OldCap.getSize() == getNumOperands()) {
814 CapOperands = OldOperands ? OldCap.getNext() : OldCap.get(1);
815 Operands = MF.allocateOperandArray(CapOperands);
816 // Move the operands before the insertion point.
817 if (OpNo)
818 moveOperands(Operands, OldOperands, OpNo, MRI);
819 }
Chris Lattner961e7422008-01-01 01:12:31 +0000820
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000821 // Move the operands following the insertion point.
822 if (OpNo != NumOperands)
823 moveOperands(Operands + OpNo + 1, OldOperands + OpNo, NumOperands - OpNo,
824 MRI);
825 ++NumOperands;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000826
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000827 // Deallocate the old operand array.
828 if (OldOperands != Operands && OldOperands)
829 MF.deallocateOperandArray(OldCap, OldOperands);
830
831 // Copy Op into place. It still needs to be inserted into the MRI use lists.
832 MachineOperand *NewMO = new (Operands + OpNo) MachineOperand(Op);
833 NewMO->ParentMI = this;
834
835 // When adding a register operand, tell MRI about it.
836 if (NewMO->isReg()) {
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000837 // Ensure isOnRegUseList() returns false, regardless of Op's status.
Craig Topperc0196b12014-04-14 00:51:57 +0000838 NewMO->Contents.Reg.Prev = nullptr;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000839 // Ignore existing ties. This is not a property that can be copied.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000840 NewMO->TiedTo = 0;
841 // Add the new operand to MRI, but only for instructions in an MBB.
842 if (MRI)
843 MRI->addRegOperandToUseList(NewMO);
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000844 // The MCID operand information isn't accurate until we start adding
845 // explicit operands. The implicit operands are added first, then the
846 // explicits are inserted before them.
847 if (!isImpReg) {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000848 // Tie uses to defs as indicated in MCInstrDesc.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000849 if (NewMO->isUse()) {
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000850 int DefIdx = MCID->getOperandConstraint(OpNo, MCOI::TIED_TO);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +0000851 if (DefIdx != -1)
852 tieOperands(DefIdx, OpNo);
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000853 }
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000854 // If the register operand is flagged as early, mark the operand as such.
855 if (MCID->getOperandConstraint(OpNo, MCOI::EARLY_CLOBBER) != -1)
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000856 NewMO->setIsEarlyClobber(true);
Chris Lattner961e7422008-01-01 01:12:31 +0000857 }
Chris Lattner961e7422008-01-01 01:12:31 +0000858 }
859}
860
861/// RemoveOperand - Erase an operand from an instruction, leaving it with one
862/// fewer operand than it started with.
863///
864void MachineInstr::RemoveOperand(unsigned OpNo) {
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000865 assert(OpNo < getNumOperands() && "Invalid operand number");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000866 untieRegOperand(OpNo);
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000867
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000868#ifndef NDEBUG
869 // Moving tied operands would break the ties.
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000870 for (unsigned i = OpNo + 1, e = getNumOperands(); i != e; ++i)
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000871 if (Operands[i].isReg())
872 assert(!Operands[i].isTied() && "Cannot move tied operands");
873#endif
874
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000875 MachineRegisterInfo *MRI = getRegInfo();
876 if (MRI && Operands[OpNo].isReg())
877 MRI->removeRegOperandFromUseList(Operands + OpNo);
Chris Lattner961e7422008-01-01 01:12:31 +0000878
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000879 // Don't call the MachineOperand destructor. A lot of this code depends on
880 // MachineOperand having a trivial destructor anyway, and adding a call here
881 // wouldn't make it 'destructor-correct'.
882
883 if (unsigned N = NumOperands - 1 - OpNo)
884 moveOperands(Operands + OpNo, Operands + OpNo + 1, N, MRI);
885 --NumOperands;
Chris Lattner961e7422008-01-01 01:12:31 +0000886}
887
Dan Gohman48b185d2009-09-25 20:36:54 +0000888/// addMemOperand - Add a MachineMemOperand to the machine instruction.
889/// This function should be used only occasionally. The setMemRefs function
890/// is the primary method for setting up a MachineInstr's MemRefs list.
Dan Gohman3b460302008-07-07 23:14:23 +0000891void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohman48b185d2009-09-25 20:36:54 +0000892 MachineMemOperand *MO) {
893 mmo_iterator OldMemRefs = MemRefs;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000894 unsigned OldNumMemRefs = NumMemRefs;
Dan Gohman3b460302008-07-07 23:14:23 +0000895
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000896 unsigned NewNum = NumMemRefs + 1;
Dan Gohman48b185d2009-09-25 20:36:54 +0000897 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
Dan Gohman3b460302008-07-07 23:14:23 +0000898
Benjamin Kramerd03878b2012-03-16 16:39:27 +0000899 std::copy(OldMemRefs, OldMemRefs + OldNumMemRefs, NewMemRefs);
Dan Gohman48b185d2009-09-25 20:36:54 +0000900 NewMemRefs[NewNum - 1] = MO;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000901 setMemRefs(NewMemRefs, NewMemRefs + NewNum);
Dan Gohman48b185d2009-09-25 20:36:54 +0000902}
Chris Lattner961e7422008-01-01 01:12:31 +0000903
Philip Reames5eb90a72016-01-06 19:33:12 +0000904/// Check to see if the MMOs pointed to by the two MemRefs arrays are
Junmo Park820e3922016-02-26 02:07:36 +0000905/// identical.
Philip Reames5eb90a72016-01-06 19:33:12 +0000906static bool hasIdenticalMMOs(const MachineInstr &MI1, const MachineInstr &MI2) {
907 auto I1 = MI1.memoperands_begin(), E1 = MI1.memoperands_end();
908 auto I2 = MI2.memoperands_begin(), E2 = MI2.memoperands_end();
909 if ((E1 - I1) != (E2 - I2))
910 return false;
911 for (; I1 != E1; ++I1, ++I2) {
912 if (**I1 != **I2)
913 return false;
914 }
915 return true;
916}
917
Philip Reamesc86ed002016-01-06 04:39:03 +0000918std::pair<MachineInstr::mmo_iterator, unsigned>
919MachineInstr::mergeMemRefsWith(const MachineInstr& Other) {
Philip Reames5eb90a72016-01-06 19:33:12 +0000920
921 // If either of the incoming memrefs are empty, we must be conservative and
922 // treat this as if we've exhausted our space for memrefs and dropped them.
923 if (memoperands_empty() || Other.memoperands_empty())
924 return std::make_pair(nullptr, 0);
925
926 // If both instructions have identical memrefs, we don't need to merge them.
927 // Since many instructions have a single memref, and we tend to merge things
928 // like pairs of loads from the same location, this catches a large number of
929 // cases in practice.
930 if (hasIdenticalMMOs(*this, Other))
931 return std::make_pair(MemRefs, NumMemRefs);
Junmo Park820e3922016-02-26 02:07:36 +0000932
Philip Reamesc86ed002016-01-06 04:39:03 +0000933 // TODO: consider uniquing elements within the operand lists to reduce
934 // space usage and fall back to conservative information less often.
Philip Reames5eb90a72016-01-06 19:33:12 +0000935 size_t CombinedNumMemRefs = NumMemRefs + Other.NumMemRefs;
936
937 // If we don't have enough room to store this many memrefs, be conservative
938 // and drop them. Otherwise, we'd fail asserts when trying to add them to
939 // the new instruction.
940 if (CombinedNumMemRefs != uint8_t(CombinedNumMemRefs))
941 return std::make_pair(nullptr, 0);
Philip Reamesc86ed002016-01-06 04:39:03 +0000942
943 MachineFunction *MF = getParent()->getParent();
944 mmo_iterator MemBegin = MF->allocateMemRefsArray(CombinedNumMemRefs);
945 mmo_iterator MemEnd = std::copy(memoperands_begin(), memoperands_end(),
946 MemBegin);
947 MemEnd = std::copy(Other.memoperands_begin(), Other.memoperands_end(),
948 MemEnd);
Philip Reames2d2fc4a2016-01-06 05:53:09 +0000949 assert(MemEnd - MemBegin == (ptrdiff_t)CombinedNumMemRefs &&
950 "missing memrefs");
Junmo Park820e3922016-02-26 02:07:36 +0000951
Philip Reamesc86ed002016-01-06 04:39:03 +0000952 return std::make_pair(MemBegin, CombinedNumMemRefs);
953}
954
Benjamin Kramer97f889f2012-03-17 17:03:45 +0000955bool MachineInstr::hasPropertyInBundle(unsigned Mask, QueryType Type) const {
Jakob Stoklund Olesenf0615c72013-01-10 18:42:44 +0000956 assert(!isBundledWithPred() && "Must be called on bundle header");
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000957 for (MachineBasicBlock::const_instr_iterator MII = getIterator();; ++MII) {
Benjamin Kramer97f889f2012-03-17 17:03:45 +0000958 if (MII->getDesc().getFlags() & Mask) {
Evan Chengcdf89fd2011-12-08 19:23:10 +0000959 if (Type == AnyInBundle)
Evan Cheng7f8e5632011-12-07 07:15:52 +0000960 return true;
961 } else {
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +0000962 if (Type == AllInBundle && !MII->isBundle())
Evan Cheng7f8e5632011-12-07 07:15:52 +0000963 return false;
964 }
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +0000965 // This was the last instruction in the bundle.
966 if (!MII->isBundledWithSucc())
967 return Type == AllInBundle;
Evan Cheng2a81dd42011-12-06 22:12:01 +0000968 }
Evan Cheng2a81dd42011-12-06 22:12:01 +0000969}
970
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000971bool MachineInstr::isIdenticalTo(const MachineInstr &Other,
Evan Chenge9c46c22010-03-03 01:44:33 +0000972 MICheckType Check) const {
Evan Cheng0f260e12010-03-03 21:54:14 +0000973 // If opcodes or number of operands are not the same then the two
974 // instructions are obviously not identical.
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000975 if (Other.getOpcode() != getOpcode() ||
976 Other.getNumOperands() != getNumOperands())
Evan Cheng0f260e12010-03-03 21:54:14 +0000977 return false;
978
Evan Cheng7fae11b2011-12-14 02:11:42 +0000979 if (isBundle()) {
980 // Both instructions are bundles, compare MIs inside the bundle.
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000981 MachineBasicBlock::const_instr_iterator I1 = getIterator();
982 MachineBasicBlock::const_instr_iterator E1 = getParent()->instr_end();
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000983 MachineBasicBlock::const_instr_iterator I2 = Other.getIterator();
984 MachineBasicBlock::const_instr_iterator E2 = Other.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000985 while (++I1 != E1 && I1->isInsideBundle()) {
986 ++I2;
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000987 if (I2 == E2 || !I2->isInsideBundle() || !I1->isIdenticalTo(*I2, Check))
Evan Cheng7fae11b2011-12-14 02:11:42 +0000988 return false;
989 }
990 }
991
Evan Cheng0f260e12010-03-03 21:54:14 +0000992 // Check operands to make sure they match.
993 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
994 const MachineOperand &MO = getOperand(i);
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000995 const MachineOperand &OMO = Other.getOperand(i);
Evan Chengcfdf3392011-05-12 00:56:58 +0000996 if (!MO.isReg()) {
997 if (!MO.isIdenticalTo(OMO))
998 return false;
999 continue;
1000 }
1001
Evan Cheng0f260e12010-03-03 21:54:14 +00001002 // Clients may or may not want to ignore defs when testing for equality.
1003 // For example, machine CSE pass only cares about finding common
1004 // subexpressions, so it's safe to ignore virtual register defs.
Evan Chengcfdf3392011-05-12 00:56:58 +00001005 if (MO.isDef()) {
Evan Cheng0f260e12010-03-03 21:54:14 +00001006 if (Check == IgnoreDefs)
1007 continue;
Evan Chengcfdf3392011-05-12 00:56:58 +00001008 else if (Check == IgnoreVRegDefs) {
1009 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) ||
1010 TargetRegisterInfo::isPhysicalRegister(OMO.getReg()))
1011 if (MO.getReg() != OMO.getReg())
1012 return false;
1013 } else {
1014 if (!MO.isIdenticalTo(OMO))
Evan Cheng0f260e12010-03-03 21:54:14 +00001015 return false;
Evan Chengcfdf3392011-05-12 00:56:58 +00001016 if (Check == CheckKillDead && MO.isDead() != OMO.isDead())
1017 return false;
1018 }
1019 } else {
1020 if (!MO.isIdenticalTo(OMO))
1021 return false;
1022 if (Check == CheckKillDead && MO.isKill() != OMO.isKill())
1023 return false;
1024 }
Evan Cheng0f260e12010-03-03 21:54:14 +00001025 }
Devang Patelbf8cc602011-07-07 17:45:33 +00001026 // If DebugLoc does not match then two dbg.values are not identical.
1027 if (isDebugValue())
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001028 if (getDebugLoc() && Other.getDebugLoc() &&
1029 getDebugLoc() != Other.getDebugLoc())
Devang Patelbf8cc602011-07-07 17:45:33 +00001030 return false;
Evan Cheng0f260e12010-03-03 21:54:14 +00001031 return true;
Evan Chenge9c46c22010-03-03 01:44:33 +00001032}
1033
Chris Lattnerbec79b42006-04-17 21:35:41 +00001034MachineInstr *MachineInstr::removeFromParent() {
1035 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001036 return getParent()->remove(this);
Chris Lattnerbec79b42006-04-17 21:35:41 +00001037}
1038
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001039MachineInstr *MachineInstr::removeFromBundle() {
1040 assert(getParent() && "Not embedded in a basic block!");
1041 return getParent()->remove_instr(this);
1042}
Chris Lattnerbec79b42006-04-17 21:35:41 +00001043
Dan Gohman3b460302008-07-07 23:14:23 +00001044void MachineInstr::eraseFromParent() {
1045 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001046 getParent()->erase(this);
Dan Gohman3b460302008-07-07 23:14:23 +00001047}
1048
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +00001049void MachineInstr::eraseFromParentAndMarkDBGValuesForRemoval() {
1050 assert(getParent() && "Not embedded in a basic block!");
1051 MachineBasicBlock *MBB = getParent();
1052 MachineFunction *MF = MBB->getParent();
1053 assert(MF && "Not embedded in a function!");
1054
1055 MachineInstr *MI = (MachineInstr *)this;
1056 MachineRegisterInfo &MRI = MF->getRegInfo();
1057
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001058 for (const MachineOperand &MO : MI->operands()) {
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +00001059 if (!MO.isReg() || !MO.isDef())
1060 continue;
1061 unsigned Reg = MO.getReg();
1062 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1063 continue;
1064 MRI.markUsesInDebugValueAsUndef(Reg);
1065 }
1066 MI->eraseFromParent();
1067}
1068
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001069void MachineInstr::eraseFromBundle() {
1070 assert(getParent() && "Not embedded in a basic block!");
1071 getParent()->erase_instr(this);
1072}
Dan Gohman3b460302008-07-07 23:14:23 +00001073
Evan Cheng4d728b02007-05-15 01:26:09 +00001074/// getNumExplicitOperands - Returns the number of non-implicit operands.
1075///
1076unsigned MachineInstr::getNumExplicitOperands() const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001077 unsigned NumOperands = MCID->getNumOperands();
1078 if (!MCID->isVariadic())
Evan Cheng4d728b02007-05-15 01:26:09 +00001079 return NumOperands;
1080
Dan Gohman37608532009-04-15 17:59:11 +00001081 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
1082 const MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001083 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng4d728b02007-05-15 01:26:09 +00001084 NumOperands++;
1085 }
1086 return NumOperands;
1087}
1088
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001089void MachineInstr::bundleWithPred() {
1090 assert(!isBundledWithPred() && "MI is already bundled with its predecessor");
1091 setFlag(BundledPred);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001092 MachineBasicBlock::instr_iterator Pred = getIterator();
1093 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001094 assert(!Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001095 Pred->setFlag(BundledSucc);
1096}
1097
1098void MachineInstr::bundleWithSucc() {
1099 assert(!isBundledWithSucc() && "MI is already bundled with its successor");
1100 setFlag(BundledSucc);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001101 MachineBasicBlock::instr_iterator Succ = getIterator();
1102 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001103 assert(!Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001104 Succ->setFlag(BundledPred);
1105}
1106
1107void MachineInstr::unbundleFromPred() {
1108 assert(isBundledWithPred() && "MI isn't bundled with its predecessor");
1109 clearFlag(BundledPred);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001110 MachineBasicBlock::instr_iterator Pred = getIterator();
1111 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001112 assert(Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001113 Pred->clearFlag(BundledSucc);
1114}
1115
1116void MachineInstr::unbundleFromSucc() {
1117 assert(isBundledWithSucc() && "MI isn't bundled with its successor");
1118 clearFlag(BundledSucc);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001119 MachineBasicBlock::instr_iterator Succ = getIterator();
1120 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001121 assert(Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001122 Succ->clearFlag(BundledPred);
1123}
1124
Evan Cheng6eb516d2011-01-07 23:50:32 +00001125bool MachineInstr::isStackAligningInlineAsm() const {
1126 if (isInlineAsm()) {
1127 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1128 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1129 return true;
1130 }
1131 return false;
1132}
Chris Lattner33f5af02006-10-20 22:39:59 +00001133
Chad Rosier994f4042012-09-05 21:00:58 +00001134InlineAsm::AsmDialect MachineInstr::getInlineAsmDialect() const {
1135 assert(isInlineAsm() && "getInlineAsmDialect() only works for inline asms!");
1136 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
Chad Rosiere53314f2012-09-05 22:40:13 +00001137 return InlineAsm::AsmDialect((ExtraInfo & InlineAsm::Extra_AsmDialect) != 0);
Chad Rosier994f4042012-09-05 21:00:58 +00001138}
1139
Jakob Stoklund Olesen1e737162011-10-12 23:37:33 +00001140int MachineInstr::findInlineAsmFlagIdx(unsigned OpIdx,
1141 unsigned *GroupNo) const {
1142 assert(isInlineAsm() && "Expected an inline asm instruction");
1143 assert(OpIdx < getNumOperands() && "OpIdx out of range");
1144
1145 // Ignore queries about the initial operands.
1146 if (OpIdx < InlineAsm::MIOp_FirstOperand)
1147 return -1;
1148
1149 unsigned Group = 0;
1150 unsigned NumOps;
1151 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1152 i += NumOps) {
1153 const MachineOperand &FlagMO = getOperand(i);
1154 // If we reach the implicit register operands, stop looking.
1155 if (!FlagMO.isImm())
1156 return -1;
1157 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1158 if (i + NumOps > OpIdx) {
1159 if (GroupNo)
1160 *GroupNo = Group;
1161 return i;
1162 }
1163 ++Group;
1164 }
1165 return -1;
1166}
1167
Reid Kleckner28865802016-04-14 18:29:59 +00001168const DILocalVariable *MachineInstr::getDebugVariable() const {
1169 assert(isDebugValue() && "not a DBG_VALUE");
1170 return cast<DILocalVariable>(getOperand(2).getMetadata());
1171}
1172
1173const DIExpression *MachineInstr::getDebugExpression() const {
1174 assert(isDebugValue() && "not a DBG_VALUE");
1175 return cast<DIExpression>(getOperand(3).getMetadata());
1176}
1177
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001178const TargetRegisterClass*
1179MachineInstr::getRegClassConstraint(unsigned OpIdx,
1180 const TargetInstrInfo *TII,
1181 const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001182 assert(getParent() && "Can't have an MBB reference here!");
1183 assert(getParent()->getParent() && "Can't have an MF reference here!");
1184 const MachineFunction &MF = *getParent()->getParent();
1185
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001186 // Most opcodes have fixed constraints in their MCInstrDesc.
1187 if (!isInlineAsm())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001188 return TII->getRegClass(getDesc(), OpIdx, TRI, MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001189
1190 if (!getOperand(OpIdx).isReg())
Craig Topperc0196b12014-04-14 00:51:57 +00001191 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001192
1193 // For tied uses on inline asm, get the constraint from the def.
1194 unsigned DefIdx;
1195 if (getOperand(OpIdx).isUse() && isRegTiedToDefOperand(OpIdx, &DefIdx))
1196 OpIdx = DefIdx;
1197
1198 // Inline asm stores register class constraints in the flag word.
1199 int FlagIdx = findInlineAsmFlagIdx(OpIdx);
1200 if (FlagIdx < 0)
Craig Topperc0196b12014-04-14 00:51:57 +00001201 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001202
1203 unsigned Flag = getOperand(FlagIdx).getImm();
1204 unsigned RCID;
1205 if (InlineAsm::hasRegClassConstraint(Flag, RCID))
1206 return TRI->getRegClass(RCID);
1207
1208 // Assume that all registers in a memory operand are pointers.
1209 if (InlineAsm::getKind(Flag) == InlineAsm::Kind_Mem)
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001210 return TRI->getPointerRegClass(MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001211
Craig Topperc0196b12014-04-14 00:51:57 +00001212 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001213}
1214
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001215const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVReg(
1216 unsigned Reg, const TargetRegisterClass *CurRC, const TargetInstrInfo *TII,
1217 const TargetRegisterInfo *TRI, bool ExploreBundle) const {
1218 // Check every operands inside the bundle if we have
1219 // been asked to.
1220 if (ExploreBundle)
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001221 for (ConstMIBundleOperands OpndIt(*this); OpndIt.isValid() && CurRC;
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001222 ++OpndIt)
1223 CurRC = OpndIt->getParent()->getRegClassConstraintEffectForVRegImpl(
1224 OpndIt.getOperandNo(), Reg, CurRC, TII, TRI);
1225 else
1226 // Otherwise, just check the current operands.
Matthias Braune41e1462015-05-29 02:56:46 +00001227 for (unsigned i = 0, e = NumOperands; i < e && CurRC; ++i)
1228 CurRC = getRegClassConstraintEffectForVRegImpl(i, Reg, CurRC, TII, TRI);
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001229 return CurRC;
1230}
1231
1232const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVRegImpl(
1233 unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC,
1234 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
1235 assert(CurRC && "Invalid initial register class");
1236 // Check if Reg is constrained by some of its use/def from MI.
1237 const MachineOperand &MO = getOperand(OpIdx);
1238 if (!MO.isReg() || MO.getReg() != Reg)
1239 return CurRC;
1240 // If yes, accumulate the constraints through the operand.
1241 return getRegClassConstraintEffect(OpIdx, CurRC, TII, TRI);
1242}
1243
1244const TargetRegisterClass *MachineInstr::getRegClassConstraintEffect(
1245 unsigned OpIdx, const TargetRegisterClass *CurRC,
1246 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
1247 const TargetRegisterClass *OpRC = getRegClassConstraint(OpIdx, TII, TRI);
1248 const MachineOperand &MO = getOperand(OpIdx);
1249 assert(MO.isReg() &&
1250 "Cannot get register constraints for non-register operand");
1251 assert(CurRC && "Invalid initial register class");
1252 if (unsigned SubIdx = MO.getSubReg()) {
1253 if (OpRC)
1254 CurRC = TRI->getMatchingSuperRegClass(CurRC, OpRC, SubIdx);
1255 else
1256 CurRC = TRI->getSubClassWithSubReg(CurRC, SubIdx);
1257 } else if (OpRC)
1258 CurRC = TRI->getCommonSubClass(CurRC, OpRC);
1259 return CurRC;
1260}
1261
Jakob Stoklund Olesen68d752b2013-01-09 18:28:16 +00001262/// Return the number of instructions inside the MI bundle, not counting the
1263/// header instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +00001264unsigned MachineInstr::getBundleSize() const {
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001265 MachineBasicBlock::const_instr_iterator I = getIterator();
Evan Cheng7fae11b2011-12-14 02:11:42 +00001266 unsigned Size = 0;
Richard Trieu7a083812016-02-18 22:09:30 +00001267 while (I->isBundledWithSucc()) {
1268 ++Size;
1269 ++I;
1270 }
Evan Cheng7fae11b2011-12-14 02:11:42 +00001271 return Size;
1272}
1273
Nicolai Haehnleb0c97482016-04-22 04:04:08 +00001274/// Returns true if the MachineInstr has an implicit-use operand of exactly
1275/// the given register (not considering sub/super-registers).
1276bool MachineInstr::hasRegisterImplicitUseOperand(unsigned Reg) const {
1277 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1278 const MachineOperand &MO = getOperand(i);
1279 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == Reg)
1280 return true;
1281 }
1282 return false;
1283}
1284
Evan Cheng910c8082007-04-26 19:00:32 +00001285/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbach9632c142009-09-17 17:57:26 +00001286/// the specific register or -1 if it is not found. It further tightens
Evan Cheng9965aeb2007-02-23 01:04:26 +00001287/// the search criteria to a use that kills the register if isKill is true.
Evan Cheng63254462008-03-05 00:59:57 +00001288int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
1289 const TargetRegisterInfo *TRI) const {
Evan Cheng75c21942006-12-06 08:27:42 +00001290 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng5983bdb2007-05-29 18:35:22 +00001291 const MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001292 if (!MO.isReg() || !MO.isUse())
Evan Cheng63254462008-03-05 00:59:57 +00001293 continue;
1294 unsigned MOReg = MO.getReg();
1295 if (!MOReg)
1296 continue;
1297 if (MOReg == Reg ||
1298 (TRI &&
1299 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
1300 TargetRegisterInfo::isPhysicalRegister(Reg) &&
1301 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng9965aeb2007-02-23 01:04:26 +00001302 if (!isKill || MO.isKill())
Evan Chengec3ac312007-03-26 22:37:45 +00001303 return i;
Evan Cheng75c21942006-12-06 08:27:42 +00001304 }
Evan Chengec3ac312007-03-26 22:37:45 +00001305 return -1;
Evan Cheng75c21942006-12-06 08:27:42 +00001306}
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001307
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001308/// readsWritesVirtualRegister - Return a pair of bools (reads, writes)
1309/// indicating if this instruction reads or writes Reg. This also considers
1310/// partial defines.
1311std::pair<bool,bool>
1312MachineInstr::readsWritesVirtualRegister(unsigned Reg,
1313 SmallVectorImpl<unsigned> *Ops) const {
1314 bool PartDef = false; // Partial redefine.
1315 bool FullDef = false; // Full define.
1316 bool Use = false;
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001317
1318 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1319 const MachineOperand &MO = getOperand(i);
1320 if (!MO.isReg() || MO.getReg() != Reg)
1321 continue;
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001322 if (Ops)
1323 Ops->push_back(i);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001324 if (MO.isUse())
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001325 Use |= !MO.isUndef();
Jakob Stoklund Olesen9eb77bf2011-08-19 00:30:17 +00001326 else if (MO.getSubReg() && !MO.isUndef())
1327 // A partial <def,undef> doesn't count as reading the register.
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001328 PartDef = true;
1329 else
1330 FullDef = true;
1331 }
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001332 // A partial redefine uses Reg unless there is also a full define.
1333 return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001334}
1335
Evan Cheng63254462008-03-05 00:59:57 +00001336/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman72a0bc12008-05-06 00:20:10 +00001337/// the specified register or -1 if it is not found. If isDead is true, defs
1338/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
1339/// also checks if there is a def of a super-register.
Evan Cheng38584512010-05-21 20:53:24 +00001340int
1341MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap,
1342 const TargetRegisterInfo *TRI) const {
1343 bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg);
Evan Chengf7ed82d2007-02-19 21:49:54 +00001344 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng63254462008-03-05 00:59:57 +00001345 const MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesene7d3f442012-02-14 23:49:37 +00001346 // Accept regmask operands when Overlap is set.
1347 // Ignore them when looking for a specific def operand (Overlap == false).
1348 if (isPhys && Overlap && MO.isRegMask() && MO.clobbersPhysReg(Reg))
1349 return i;
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001350 if (!MO.isReg() || !MO.isDef())
Evan Cheng63254462008-03-05 00:59:57 +00001351 continue;
1352 unsigned MOReg = MO.getReg();
Evan Cheng38584512010-05-21 20:53:24 +00001353 bool Found = (MOReg == Reg);
1354 if (!Found && TRI && isPhys &&
1355 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
1356 if (Overlap)
1357 Found = TRI->regsOverlap(MOReg, Reg);
1358 else
1359 Found = TRI->isSubRegister(MOReg, Reg);
1360 }
1361 if (Found && (!isDead || MO.isDead()))
1362 return i;
Evan Chengf7ed82d2007-02-19 21:49:54 +00001363 }
Evan Cheng63254462008-03-05 00:59:57 +00001364 return -1;
Evan Chengf7ed82d2007-02-19 21:49:54 +00001365}
Evan Cheng4d728b02007-05-15 01:26:09 +00001366
Evan Cheng5983bdb2007-05-29 18:35:22 +00001367/// findFirstPredOperandIdx() - Find the index of the first operand in the
1368/// operand list that is used to represent the predicate. It returns -1 if
1369/// none is found.
1370int MachineInstr::findFirstPredOperandIdx() const {
Jim Grosbached16ec42011-08-29 22:24:09 +00001371 // Don't call MCID.findFirstPredOperandIdx() because this variant
1372 // is sometimes called on an instruction that's not yet complete, and
1373 // so the number of operands is less than the MCID indicates. In
1374 // particular, the PTX target does this.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001375 const MCInstrDesc &MCID = getDesc();
1376 if (MCID.isPredicable()) {
Evan Cheng4d728b02007-05-15 01:26:09 +00001377 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Evan Cheng6cc775f2011-06-28 19:10:37 +00001378 if (MCID.OpInfo[i].isPredicate())
Evan Cheng5983bdb2007-05-29 18:35:22 +00001379 return i;
Evan Cheng4d728b02007-05-15 01:26:09 +00001380 }
1381
Evan Cheng5983bdb2007-05-29 18:35:22 +00001382 return -1;
Evan Cheng4d728b02007-05-15 01:26:09 +00001383}
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001384
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001385// MachineOperand::TiedTo is 4 bits wide.
1386const unsigned TiedMax = 15;
1387
1388/// tieOperands - Mark operands at DefIdx and UseIdx as tied to each other.
1389///
1390/// Use and def operands can be tied together, indicated by a non-zero TiedTo
1391/// field. TiedTo can have these values:
1392///
1393/// 0: Operand is not tied to anything.
1394/// 1 to TiedMax-1: Tied to getOperand(TiedTo-1).
1395/// TiedMax: Tied to an operand >= TiedMax-1.
1396///
1397/// The tied def must be one of the first TiedMax operands on a normal
1398/// instruction. INLINEASM instructions allow more tied defs.
1399///
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001400void MachineInstr::tieOperands(unsigned DefIdx, unsigned UseIdx) {
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001401 MachineOperand &DefMO = getOperand(DefIdx);
1402 MachineOperand &UseMO = getOperand(UseIdx);
1403 assert(DefMO.isDef() && "DefIdx must be a def operand");
1404 assert(UseMO.isUse() && "UseIdx must be a use operand");
1405 assert(!DefMO.isTied() && "Def is already tied to another use");
1406 assert(!UseMO.isTied() && "Use is already tied to another def");
1407
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001408 if (DefIdx < TiedMax)
1409 UseMO.TiedTo = DefIdx + 1;
1410 else {
1411 // Inline asm can use the group descriptors to find tied operands, but on
1412 // normal instruction, the tied def must be within the first TiedMax
1413 // operands.
1414 assert(isInlineAsm() && "DefIdx out of range");
1415 UseMO.TiedTo = TiedMax;
1416 }
1417
1418 // UseIdx can be out of range, we'll search for it in findTiedOperandIdx().
1419 DefMO.TiedTo = std::min(UseIdx + 1, TiedMax);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001420}
1421
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001422/// Given the index of a tied register operand, find the operand it is tied to.
1423/// Defs are tied to uses and vice versa. Returns the index of the tied operand
1424/// which must exist.
1425unsigned MachineInstr::findTiedOperandIdx(unsigned OpIdx) const {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001426 const MachineOperand &MO = getOperand(OpIdx);
1427 assert(MO.isTied() && "Operand isn't tied");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001428
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001429 // Normally TiedTo is in range.
1430 if (MO.TiedTo < TiedMax)
1431 return MO.TiedTo - 1;
1432
1433 // Uses on normal instructions can be out of range.
1434 if (!isInlineAsm()) {
1435 // Normal tied defs must be in the 0..TiedMax-1 range.
1436 if (MO.isUse())
1437 return TiedMax - 1;
1438 // MO is a def. Search for the tied use.
1439 for (unsigned i = TiedMax - 1, e = getNumOperands(); i != e; ++i) {
1440 const MachineOperand &UseMO = getOperand(i);
1441 if (UseMO.isReg() && UseMO.isUse() && UseMO.TiedTo == OpIdx + 1)
1442 return i;
1443 }
1444 llvm_unreachable("Can't find tied use");
1445 }
1446
1447 // Now deal with inline asm by parsing the operand group descriptor flags.
1448 // Find the beginning of each operand group.
1449 SmallVector<unsigned, 8> GroupIdx;
1450 unsigned OpIdxGroup = ~0u;
1451 unsigned NumOps;
1452 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1453 i += NumOps) {
1454 const MachineOperand &FlagMO = getOperand(i);
1455 assert(FlagMO.isImm() && "Invalid tied operand on inline asm");
1456 unsigned CurGroup = GroupIdx.size();
1457 GroupIdx.push_back(i);
1458 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1459 // OpIdx belongs to this operand group.
1460 if (OpIdx > i && OpIdx < i + NumOps)
1461 OpIdxGroup = CurGroup;
1462 unsigned TiedGroup;
1463 if (!InlineAsm::isUseOperandTiedToDef(FlagMO.getImm(), TiedGroup))
1464 continue;
1465 // Operands in this group are tied to operands in TiedGroup which must be
1466 // earlier. Find the number of operands between the two groups.
1467 unsigned Delta = i - GroupIdx[TiedGroup];
1468
1469 // OpIdx is a use tied to TiedGroup.
1470 if (OpIdxGroup == CurGroup)
1471 return OpIdx - Delta;
1472
1473 // OpIdx is a def tied to this use group.
1474 if (OpIdxGroup == TiedGroup)
1475 return OpIdx + Delta;
1476 }
1477 llvm_unreachable("Invalid tied operand on inline asm");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001478}
1479
Dan Gohmanc90f51c2010-05-13 20:34:42 +00001480/// clearKillInfo - Clears kill flags on all operands.
1481///
1482void MachineInstr::clearKillInfo() {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001483 for (MachineOperand &MO : operands()) {
Dan Gohmanc90f51c2010-05-13 20:34:42 +00001484 if (MO.isReg() && MO.isUse())
1485 MO.setIsKill(false);
1486 }
1487}
1488
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001489void MachineInstr::substituteRegister(unsigned FromReg,
1490 unsigned ToReg,
1491 unsigned SubIdx,
1492 const TargetRegisterInfo &RegInfo) {
1493 if (TargetRegisterInfo::isPhysicalRegister(ToReg)) {
1494 if (SubIdx)
1495 ToReg = RegInfo.getSubReg(ToReg, SubIdx);
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001496 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001497 if (!MO.isReg() || MO.getReg() != FromReg)
1498 continue;
1499 MO.substPhysReg(ToReg, RegInfo);
1500 }
1501 } else {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001502 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001503 if (!MO.isReg() || MO.getReg() != FromReg)
1504 continue;
1505 MO.substVirtReg(ToReg, SubIdx, RegInfo);
1506 }
1507 }
1508}
1509
Evan Cheng7d98a482008-07-03 09:09:37 +00001510/// isSafeToMove - Return true if it is safe to move this instruction. If
1511/// SawStore is set to true, it means that there is a store (or call) between
1512/// the instruction's location and its intended destination.
Matthias Braun07066cc2015-05-19 21:22:20 +00001513bool MachineInstr::isSafeToMove(AliasAnalysis *AA, bool &SawStore) const {
Evan Cheng399e1102008-03-13 00:44:09 +00001514 // Ignore stuff that we obviously can't move.
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001515 //
1516 // Treat volatile loads as stores. This is not strictly necessary for
Jakob Stoklund Olesend92e2bc2012-09-04 18:44:43 +00001517 // volatiles, but it is required for atomic loads. It is not allowed to move
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001518 // a load across an atomic load with Ordering > Monotonic.
1519 if (mayStore() || isCall() ||
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001520 (mayLoad() && hasOrderedMemoryRef())) {
Evan Cheng399e1102008-03-13 00:44:09 +00001521 SawStore = true;
1522 return false;
1523 }
Evan Cheng0638c202011-01-07 21:08:26 +00001524
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001525 if (isPosition() || isDebugValue() || isTerminator() ||
1526 hasUnmodeledSideEffects())
Evan Cheng399e1102008-03-13 00:44:09 +00001527 return false;
1528
1529 // See if this instruction does a load. If so, we have to guarantee that the
1530 // loaded value doesn't change between the load and the its intended
1531 // destination. The check for isInvariantLoad gives the targe the chance to
1532 // classify the load as always returning a constant, e.g. a constant pool
1533 // load.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001534 if (mayLoad() && !isInvariantLoad(AA))
Evan Cheng399e1102008-03-13 00:44:09 +00001535 // Otherwise, this is a real load. If there is a store between the load and
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001536 // end of block, we can't move it.
1537 return !SawStore;
Dan Gohman7c59ed62008-09-24 00:06:15 +00001538
Evan Cheng399e1102008-03-13 00:44:09 +00001539 return true;
1540}
1541
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001542/// hasOrderedMemoryRef - Return true if this instruction may have an ordered
1543/// or volatile memory reference, or if the information describing the memory
1544/// reference is not available. Return false if it is known to have no ordered
1545/// memory references.
1546bool MachineInstr::hasOrderedMemoryRef() const {
Dan Gohman7c59ed62008-09-24 00:06:15 +00001547 // An instruction known never to access memory won't have a volatile access.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001548 if (!mayStore() &&
1549 !mayLoad() &&
1550 !isCall() &&
Evan Cheng6eb516d2011-01-07 23:50:32 +00001551 !hasUnmodeledSideEffects())
Dan Gohman7c59ed62008-09-24 00:06:15 +00001552 return false;
1553
1554 // Otherwise, if the instruction has no memory reference information,
1555 // conservatively assume it wasn't preserved.
1556 if (memoperands_empty())
1557 return true;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001558
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001559 // Check the memory reference information for ordered references.
Dan Gohman48b185d2009-09-25 20:36:54 +00001560 for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I)
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001561 if (!(*I)->isUnordered())
Dan Gohman7c59ed62008-09-24 00:06:15 +00001562 return true;
1563
1564 return false;
1565}
1566
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001567/// isInvariantLoad - Return true if this instruction is loading from a
1568/// location whose value is invariant across the function. For example,
Dan Gohman4a618822010-02-10 16:03:48 +00001569/// loading a value from the constant pool or from the argument area
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001570/// of a function if it does not change. This should only return true of
1571/// *all* loads the instruction does are invariant (if it does multiple loads).
1572bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const {
1573 // If the instruction doesn't load at all, it isn't an invariant load.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001574 if (!mayLoad())
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001575 return false;
1576
1577 // If the instruction has lost its memoperands, conservatively assume that
1578 // it may not be an invariant load.
1579 if (memoperands_empty())
1580 return false;
1581
1582 const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo();
1583
1584 for (mmo_iterator I = memoperands_begin(),
1585 E = memoperands_end(); I != E; ++I) {
1586 if ((*I)->isVolatile()) return false;
1587 if ((*I)->isStore()) return false;
Pete Cooper82cd9e82011-11-08 18:42:53 +00001588 if ((*I)->isInvariant()) return true;
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001589
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001590
1591 // A load from a constant PseudoSourceValue is invariant.
1592 if (const PseudoSourceValue *PSV = (*I)->getPseudoValue())
1593 if (PSV->isConstant(MFI))
1594 continue;
1595
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001596 if (const Value *V = (*I)->getValue()) {
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001597 // If we have an AliasAnalysis, ask it whether the memory is constant.
Chandler Carruthac80dc72015-06-17 07:18:54 +00001598 if (AA &&
1599 AA->pointsToConstantMemory(
1600 MemoryLocation(V, (*I)->getSize(), (*I)->getAAInfo())))
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001601 continue;
1602 }
1603
1604 // Otherwise assume conservatively.
1605 return false;
1606 }
1607
1608 // Everything checks out.
1609 return true;
1610}
1611
Evan Cheng71453822009-12-03 02:31:43 +00001612/// isConstantValuePHI - If the specified instruction is a PHI that always
1613/// merges together the same virtual register, return the register, otherwise
1614/// return 0.
1615unsigned MachineInstr::isConstantValuePHI() const {
Chris Lattnerb06015a2010-02-09 19:54:29 +00001616 if (!isPHI())
Evan Cheng71453822009-12-03 02:31:43 +00001617 return 0;
Evan Cheng5c668a22009-12-07 23:10:34 +00001618 assert(getNumOperands() >= 3 &&
1619 "It's illegal to have a PHI without source operands");
Evan Cheng71453822009-12-03 02:31:43 +00001620
1621 unsigned Reg = getOperand(1).getReg();
1622 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
1623 if (getOperand(i).getReg() != Reg)
1624 return 0;
1625 return Reg;
1626}
1627
Evan Cheng6eb516d2011-01-07 23:50:32 +00001628bool MachineInstr::hasUnmodeledSideEffects() const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00001629 if (hasProperty(MCID::UnmodeledSideEffects))
Evan Cheng6eb516d2011-01-07 23:50:32 +00001630 return true;
1631 if (isInlineAsm()) {
1632 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1633 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1634 return true;
1635 }
1636
1637 return false;
1638}
1639
Michael Kupersteinbc7f99a2015-08-12 10:14:58 +00001640bool MachineInstr::isLoadFoldBarrier() const {
1641 return mayStore() || isCall() || hasUnmodeledSideEffects();
1642}
1643
Evan Chengb083c472010-04-08 20:02:37 +00001644/// allDefsAreDead - Return true if all the defs of this instruction are dead.
1645///
1646bool MachineInstr::allDefsAreDead() const {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001647 for (const MachineOperand &MO : operands()) {
Evan Chengb083c472010-04-08 20:02:37 +00001648 if (!MO.isReg() || MO.isUse())
1649 continue;
1650 if (!MO.isDead())
1651 return false;
1652 }
1653 return true;
1654}
1655
Evan Cheng21eedfb2010-10-22 21:49:09 +00001656/// copyImplicitOps - Copy implicit register operands from specified
1657/// instruction to this instruction.
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001658void MachineInstr::copyImplicitOps(MachineFunction &MF,
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001659 const MachineInstr &MI) {
1660 for (unsigned i = MI.getDesc().getNumOperands(), e = MI.getNumOperands();
Evan Cheng21eedfb2010-10-22 21:49:09 +00001661 i != e; ++i) {
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001662 const MachineOperand &MO = MI.getOperand(i);
Lang Hames7c8189c2014-03-17 01:22:54 +00001663 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001664 addOperand(MF, MO);
Evan Cheng21eedfb2010-10-22 21:49:09 +00001665 }
1666}
1667
Yaron Kereneb2a2542016-01-29 20:50:44 +00001668LLVM_DUMP_METHOD void MachineInstr::dump() const {
Manman Ren19f49ac2012-09-11 22:23:19 +00001669#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
David Greene29388d62010-01-04 23:48:20 +00001670 dbgs() << " " << *this;
Manman Ren742534c2012-09-06 19:06:06 +00001671#endif
Mon P Wangdfcc1ff2008-10-10 01:43:55 +00001672}
1673
Eric Christopher1cdefae2015-02-27 00:11:34 +00001674void MachineInstr::print(raw_ostream &OS, bool SkipOpers) const {
Duncan P. N. Exon Smithc0374522015-06-26 23:18:44 +00001675 const Module *M = nullptr;
1676 if (const MachineBasicBlock *MBB = getParent())
1677 if (const MachineFunction *MF = MBB->getParent())
1678 M = MF->getFunction()->getParent();
1679
1680 ModuleSlotTracker MST(M);
1681 print(OS, MST, SkipOpers);
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001682}
1683
1684void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
1685 bool SkipOpers) const {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001686 // We can be a bit tidier if we know the MachineFunction.
Craig Topperc0196b12014-04-14 00:51:57 +00001687 const MachineFunction *MF = nullptr;
Eric Christopher1cdefae2015-02-27 00:11:34 +00001688 const TargetRegisterInfo *TRI = nullptr;
Craig Topperc0196b12014-04-14 00:51:57 +00001689 const MachineRegisterInfo *MRI = nullptr;
Eric Christopher1cdefae2015-02-27 00:11:34 +00001690 const TargetInstrInfo *TII = nullptr;
Dan Gohman2745d192009-11-09 19:38:45 +00001691 if (const MachineBasicBlock *MBB = getParent()) {
1692 MF = MBB->getParent();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001693 if (MF) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001694 MRI = &MF->getRegInfo();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001695 TRI = MF->getSubtarget().getRegisterInfo();
1696 TII = MF->getSubtarget().getInstrInfo();
1697 }
Dan Gohman2745d192009-11-09 19:38:45 +00001698 }
Dan Gohman34341e62009-10-31 20:19:03 +00001699
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001700 // Save a list of virtual registers.
1701 SmallVector<unsigned, 8> VirtRegs;
1702
Dan Gohman34341e62009-10-31 20:19:03 +00001703 // Print explicitly defined operands on the left of an assignment syntax.
Dan Gohman2745d192009-11-09 19:38:45 +00001704 unsigned StartOp = 0, e = getNumOperands();
Dan Gohman34341e62009-10-31 20:19:03 +00001705 for (; StartOp < e && getOperand(StartOp).isReg() &&
1706 getOperand(StartOp).isDef() &&
1707 !getOperand(StartOp).isImplicit();
1708 ++StartOp) {
1709 if (StartOp != 0) OS << ", ";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001710 getOperand(StartOp).print(OS, MST, TRI);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001711 unsigned Reg = getOperand(StartOp).getReg();
Quentin Colombet36ce1b02016-02-10 23:43:48 +00001712 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001713 VirtRegs.push_back(Reg);
Quentin Colombet36ce1b02016-02-10 23:43:48 +00001714 unsigned Size;
Quentin Colombet03c41962016-04-07 23:18:11 +00001715 if (MRI && (Size = MRI->getSize(Reg)))
Quentin Colombet36ce1b02016-02-10 23:43:48 +00001716 OS << '(' << Size << ')';
Quentin Colombet36ce1b02016-02-10 23:43:48 +00001717 }
Chris Lattnerac6e9742002-10-30 01:55:38 +00001718 }
Tanya Lattner23dbc812004-06-25 00:13:11 +00001719
Dan Gohman34341e62009-10-31 20:19:03 +00001720 if (StartOp != 0)
1721 OS << " = ";
1722
1723 // Print the opcode name.
Eric Christopher1cdefae2015-02-27 00:11:34 +00001724 if (TII)
1725 OS << TII->getName(getOpcode());
Benjamin Kramerbf152d52012-02-10 13:18:44 +00001726 else
1727 OS << "UNKNOWN";
Misha Brukman835702a2005-04-21 22:36:52 +00001728
Quentin Colombet41bea872016-03-07 22:47:23 +00001729 if (getType()) {
1730 OS << ' ';
1731 getType()->print(OS, /*IsForDebug*/ false, /*NoDetails*/ true);
1732 OS << ' ';
1733 }
Quentin Colombet98551112016-02-11 18:22:37 +00001734
Andrew Trickb36388a2013-01-25 07:45:25 +00001735 if (SkipOpers)
1736 return;
1737
Dan Gohman34341e62009-10-31 20:19:03 +00001738 // Print the rest of the operands.
Dan Gohman2745d192009-11-09 19:38:45 +00001739 bool OmittedAnyCallClobbers = false;
1740 bool FirstOp = true;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001741 unsigned AsmDescOp = ~0u;
1742 unsigned AsmOpCount = 0;
Evan Cheng6eb516d2011-01-07 23:50:32 +00001743
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +00001744 if (isInlineAsm() && e >= InlineAsm::MIOp_FirstOperand) {
Evan Cheng6eb516d2011-01-07 23:50:32 +00001745 // Print asm string.
1746 OS << " ";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001747 getOperand(InlineAsm::MIOp_AsmString).print(OS, MST, TRI);
Evan Cheng6eb516d2011-01-07 23:50:32 +00001748
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001749 // Print HasSideEffects, MayLoad, MayStore, IsAlignStack
Evan Cheng6eb516d2011-01-07 23:50:32 +00001750 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1751 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1752 OS << " [sideeffect]";
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001753 if (ExtraInfo & InlineAsm::Extra_MayLoad)
1754 OS << " [mayload]";
1755 if (ExtraInfo & InlineAsm::Extra_MayStore)
1756 OS << " [maystore]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00001757 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1758 OS << " [alignstack]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00001759 if (getInlineAsmDialect() == InlineAsm::AD_ATT)
Chad Rosier994f4042012-09-05 21:00:58 +00001760 OS << " [attdialect]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00001761 if (getInlineAsmDialect() == InlineAsm::AD_Intel)
Chad Rosier994f4042012-09-05 21:00:58 +00001762 OS << " [inteldialect]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00001763
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001764 StartOp = AsmDescOp = InlineAsm::MIOp_FirstOperand;
Evan Cheng6eb516d2011-01-07 23:50:32 +00001765 FirstOp = false;
1766 }
1767
Chris Lattnerac6e9742002-10-30 01:55:38 +00001768 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohman2745d192009-11-09 19:38:45 +00001769 const MachineOperand &MO = getOperand(i);
1770
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001771 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001772 VirtRegs.push_back(MO.getReg());
1773
Dan Gohman2745d192009-11-09 19:38:45 +00001774 // Omit call-clobbered registers which aren't used anywhere. This makes
1775 // call instructions much less noisy on targets where calls clobber lots
1776 // of registers. Don't rely on MO.isDead() because we may be called before
1777 // LiveVariables is run, or we may be looking at a non-allocatable reg.
Craig Toppercf0444b2014-11-17 05:50:14 +00001778 if (MRI && isCall() &&
Dan Gohman2745d192009-11-09 19:38:45 +00001779 MO.isReg() && MO.isImplicit() && MO.isDef()) {
1780 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001781 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Craig Toppercf0444b2014-11-17 05:50:14 +00001782 if (MRI->use_empty(Reg)) {
Dan Gohman2745d192009-11-09 19:38:45 +00001783 bool HasAliasLive = false;
Eric Christopher1cdefae2015-02-27 00:11:34 +00001784 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001785 unsigned AliasReg = *AI;
Craig Toppercf0444b2014-11-17 05:50:14 +00001786 if (!MRI->use_empty(AliasReg)) {
Dan Gohman2745d192009-11-09 19:38:45 +00001787 HasAliasLive = true;
1788 break;
1789 }
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001790 }
Dan Gohman2745d192009-11-09 19:38:45 +00001791 if (!HasAliasLive) {
1792 OmittedAnyCallClobbers = true;
1793 continue;
1794 }
1795 }
1796 }
1797 }
1798
1799 if (FirstOp) FirstOp = false; else OS << ",";
Chris Lattnerac6e9742002-10-30 01:55:38 +00001800 OS << " ";
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001801 if (i < getDesc().NumOperands) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001802 const MCOperandInfo &MCOI = getDesc().OpInfo[i];
1803 if (MCOI.isPredicate())
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001804 OS << "pred:";
Evan Cheng6cc775f2011-06-28 19:10:37 +00001805 if (MCOI.isOptionalDef())
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001806 OS << "opt:";
1807 }
Evan Chengd4d1a512010-04-28 20:03:13 +00001808 if (isDebugValue() && MO.isMetadata()) {
1809 // Pretty print DBG_VALUE instructions.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001810 auto *DIV = dyn_cast<DILocalVariable>(MO.getMetadata());
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +00001811 if (DIV && !DIV->getName().empty())
1812 OS << "!\"" << DIV->getName() << '\"';
Evan Chengd4d1a512010-04-28 20:03:13 +00001813 else
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001814 MO.print(OS, MST, TRI);
Eric Christopher1cdefae2015-02-27 00:11:34 +00001815 } else if (TRI && (isInsertSubreg() || isRegSequence()) && MO.isImm()) {
1816 OS << TRI->getSubRegIndexName(MO.getImm());
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001817 } else if (i == AsmDescOp && MO.isImm()) {
1818 // Pretty print the inline asm operand descriptor.
1819 OS << '$' << AsmOpCount++;
1820 unsigned Flag = MO.getImm();
1821 switch (InlineAsm::getKind(Flag)) {
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001822 case InlineAsm::Kind_RegUse: OS << ":[reguse"; break;
1823 case InlineAsm::Kind_RegDef: OS << ":[regdef"; break;
1824 case InlineAsm::Kind_RegDefEarlyClobber: OS << ":[regdef-ec"; break;
1825 case InlineAsm::Kind_Clobber: OS << ":[clobber"; break;
1826 case InlineAsm::Kind_Imm: OS << ":[imm"; break;
1827 case InlineAsm::Kind_Mem: OS << ":[mem"; break;
1828 default: OS << ":[??" << InlineAsm::getKind(Flag); break;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001829 }
1830
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001831 unsigned RCID = 0;
Nick Lewycky84882252011-10-13 00:54:59 +00001832 if (InlineAsm::hasRegClassConstraint(Flag, RCID)) {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001833 if (TRI) {
1834 OS << ':' << TRI->getRegClassName(TRI->getRegClass(RCID));
Craig Toppercf0444b2014-11-17 05:50:14 +00001835 } else
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001836 OS << ":RC" << RCID;
Nick Lewycky84882252011-10-13 00:54:59 +00001837 }
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001838
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001839 unsigned TiedTo = 0;
1840 if (InlineAsm::isUseOperandTiedToDef(Flag, TiedTo))
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001841 OS << " tiedto:$" << TiedTo;
1842
1843 OS << ']';
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001844
1845 // Compute the index of the next operand descriptor.
1846 AsmDescOp += 1 + InlineAsm::getNumOperandRegisters(Flag);
Evan Chengd4d1a512010-04-28 20:03:13 +00001847 } else
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001848 MO.print(OS, MST, TRI);
Dan Gohman2745d192009-11-09 19:38:45 +00001849 }
1850
1851 // Briefly indicate whether any call clobbers were omitted.
1852 if (OmittedAnyCallClobbers) {
Bill Wendlingec030f22009-12-25 13:45:50 +00001853 if (!FirstOp) OS << ",";
Dan Gohman2745d192009-11-09 19:38:45 +00001854 OS << " ...";
Chris Lattner214808f2002-10-30 00:48:05 +00001855 }
Misha Brukman835702a2005-04-21 22:36:52 +00001856
Dan Gohman34341e62009-10-31 20:19:03 +00001857 bool HaveSemi = false;
Michael Kuperstein098cd9f2015-09-16 11:18:25 +00001858 const unsigned PrintableFlags = FrameSetup | FrameDestroy;
Jakob Stoklund Olesen6922e9c2013-01-09 18:35:09 +00001859 if (Flags & PrintableFlags) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001860 if (!HaveSemi) {
1861 OS << ";";
1862 HaveSemi = true;
1863 }
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001864 OS << " flags: ";
1865
1866 if (Flags & FrameSetup)
1867 OS << "FrameSetup";
Michael Kuperstein098cd9f2015-09-16 11:18:25 +00001868
1869 if (Flags & FrameDestroy)
1870 OS << "FrameDestroy";
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001871 }
1872
Dan Gohman3b460302008-07-07 23:14:23 +00001873 if (!memoperands_empty()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001874 if (!HaveSemi) {
1875 OS << ";";
1876 HaveSemi = true;
1877 }
Dan Gohman34341e62009-10-31 20:19:03 +00001878
1879 OS << " mem:";
Dan Gohman48b185d2009-09-25 20:36:54 +00001880 for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
1881 i != e; ++i) {
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001882 (*i)->print(OS, MST);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001883 if (std::next(i) != e)
Dan Gohmanc0353bf2009-09-23 01:33:16 +00001884 OS << " ";
Dan Gohman2d489b52008-02-06 22:27:42 +00001885 }
1886 }
1887
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001888 // Print the regclass of any virtual registers encountered.
1889 if (MRI && !VirtRegs.empty()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001890 if (!HaveSemi) {
1891 OS << ";";
1892 HaveSemi = true;
1893 }
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001894 for (unsigned i = 0; i != VirtRegs.size(); ++i) {
Quentin Colombet03c41962016-04-07 23:18:11 +00001895 const RegClassOrRegBank &RC = MRI->getRegClassOrRegBank(VirtRegs[i]);
Quentin Colombete1494c32016-02-11 00:19:17 +00001896 if (!RC)
1897 continue;
Quentin Colombet03c41962016-04-07 23:18:11 +00001898 // Generic virtual registers do not have register classes.
1899 if (RC.is<const RegisterBank *>())
1900 OS << " " << RC.get<const RegisterBank *>()->getName();
1901 else
1902 OS << " "
1903 << TRI->getRegClassName(RC.get<const TargetRegisterClass *>());
1904 OS << ':' << PrintReg(VirtRegs[i]);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001905 for (unsigned j = i+1; j != VirtRegs.size();) {
Quentin Colombet03c41962016-04-07 23:18:11 +00001906 if (MRI->getRegClassOrRegBank(VirtRegs[j]) != RC) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001907 ++j;
1908 continue;
1909 }
1910 if (VirtRegs[i] != VirtRegs[j])
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +00001911 OS << "," << PrintReg(VirtRegs[j]);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001912 VirtRegs.erase(VirtRegs.begin()+j);
1913 }
1914 }
1915 }
1916
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001917 // Print debug location information.
Duncan P. N. Exon Smithc5bd3e02015-04-03 16:23:04 +00001918 if (isDebugValue() && getOperand(e - 2).isMetadata()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001919 if (!HaveSemi)
1920 OS << ";";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001921 auto *DV = cast<DILocalVariable>(getOperand(e - 2).getMetadata());
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +00001922 OS << " line no:" << DV->getLine();
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001923 if (auto *InlinedAt = debugLoc->getInlinedAt()) {
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00001924 DebugLoc InlinedAtDL(InlinedAt);
1925 if (InlinedAtDL && MF) {
Devang Pateld61b1d52011-08-04 20:44:26 +00001926 OS << " inlined @[ ";
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00001927 InlinedAtDL.print(OS);
Devang Pateld61b1d52011-08-04 20:44:26 +00001928 OS << " ]";
1929 }
1930 }
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001931 if (isIndirectDebugValue())
1932 OS << " indirect";
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00001933 } else if (debugLoc && MF) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001934 if (!HaveSemi)
1935 OS << ";";
Dan Gohman2e3f1872009-11-23 21:29:08 +00001936 OS << " dbg:";
Eric Christopherb9f00092015-02-26 23:32:17 +00001937 debugLoc.print(OS);
Bill Wendling1a0a3d02009-02-19 21:44:55 +00001938 }
1939
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001940 OS << '\n';
Chris Lattner214808f2002-10-30 00:48:05 +00001941}
1942
Owen Anderson2a8a4852008-01-24 01:10:07 +00001943bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001944 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00001945 bool AddIfNotFound) {
Evan Cheng6c177732008-04-16 09:41:59 +00001946 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001947 bool hasAliases = isPhysReg &&
1948 MCRegAliasIterator(IncomingReg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00001949 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00001950 SmallVector<unsigned,4> DeadOps;
Bill Wendling7921ad02008-03-03 22:14:33 +00001951 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1952 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenf465f062009-08-04 20:09:25 +00001953 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng6c177732008-04-16 09:41:59 +00001954 continue;
1955 unsigned Reg = MO.getReg();
1956 if (!Reg)
1957 continue;
Bill Wendling7921ad02008-03-03 22:14:33 +00001958
Evan Cheng6c177732008-04-16 09:41:59 +00001959 if (Reg == IncomingReg) {
Dan Gohmanc7367b42008-09-03 15:56:16 +00001960 if (!Found) {
1961 if (MO.isKill())
1962 // The register is already marked kill.
1963 return true;
Jakob Stoklund Olesenc59cd9b2009-08-02 19:13:03 +00001964 if (isPhysReg && isRegTiedToDefOperand(i))
1965 // Two-address uses of physregs must not be marked kill.
1966 return true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00001967 MO.setIsKill();
1968 Found = true;
1969 }
1970 } else if (hasAliases && MO.isKill() &&
1971 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00001972 // A super-register kill already exists.
1973 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohmanb2612922008-07-03 01:18:51 +00001974 return true;
1975 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng6c177732008-04-16 09:41:59 +00001976 DeadOps.push_back(i);
Bill Wendling7921ad02008-03-03 22:14:33 +00001977 }
1978 }
1979
Evan Cheng6c177732008-04-16 09:41:59 +00001980 // Trim unneeded kill operands.
1981 while (!DeadOps.empty()) {
1982 unsigned OpIdx = DeadOps.back();
1983 if (getOperand(OpIdx).isImplicit())
1984 RemoveOperand(OpIdx);
1985 else
1986 getOperand(OpIdx).setIsKill(false);
1987 DeadOps.pop_back();
1988 }
1989
Bill Wendling7921ad02008-03-03 22:14:33 +00001990 // If not found, this means an alias of one of the operands is killed. Add a
Owen Anderson2a8a4852008-01-24 01:10:07 +00001991 // new implicit operand if required.
Dan Gohmanc7367b42008-09-03 15:56:16 +00001992 if (!Found && AddIfNotFound) {
Bill Wendling7921ad02008-03-03 22:14:33 +00001993 addOperand(MachineOperand::CreateReg(IncomingReg,
1994 false /*IsDef*/,
1995 true /*IsImp*/,
1996 true /*IsKill*/));
Owen Anderson2a8a4852008-01-24 01:10:07 +00001997 return true;
1998 }
Dan Gohmanc7367b42008-09-03 15:56:16 +00001999 return Found;
Owen Anderson2a8a4852008-01-24 01:10:07 +00002000}
2001
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00002002void MachineInstr::clearRegisterKills(unsigned Reg,
2003 const TargetRegisterInfo *RegInfo) {
2004 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topperc0196b12014-04-14 00:51:57 +00002005 RegInfo = nullptr;
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002006 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00002007 if (!MO.isReg() || !MO.isUse() || !MO.isKill())
2008 continue;
2009 unsigned OpReg = MO.getReg();
Matthias Braunaca625a2016-02-24 19:21:48 +00002010 if ((RegInfo && RegInfo->regsOverlap(Reg, OpReg)) || Reg == OpReg)
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00002011 MO.setIsKill(false);
2012 }
2013}
2014
Matthias Braun1965bfa2013-10-10 21:28:38 +00002015bool MachineInstr::addRegisterDead(unsigned Reg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00002016 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00002017 bool AddIfNotFound) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00002018 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(Reg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00002019 bool hasAliases = isPhysReg &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00002020 MCRegAliasIterator(Reg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00002021 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00002022 SmallVector<unsigned,4> DeadOps;
Owen Anderson2a8a4852008-01-24 01:10:07 +00002023 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2024 MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00002025 if (!MO.isReg() || !MO.isDef())
Evan Cheng6c177732008-04-16 09:41:59 +00002026 continue;
Matthias Braun1965bfa2013-10-10 21:28:38 +00002027 unsigned MOReg = MO.getReg();
2028 if (!MOReg)
Dan Gohmanc7367b42008-09-03 15:56:16 +00002029 continue;
2030
Matthias Braun1965bfa2013-10-10 21:28:38 +00002031 if (MOReg == Reg) {
Jakob Stoklund Olesen76ad3de2011-04-05 16:53:50 +00002032 MO.setIsDead();
2033 Found = true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00002034 } else if (hasAliases && MO.isDead() &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00002035 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00002036 // There exists a super-register that's marked dead.
Matthias Braun1965bfa2013-10-10 21:28:38 +00002037 if (RegInfo->isSuperRegister(Reg, MOReg))
Dan Gohmanb2612922008-07-03 01:18:51 +00002038 return true;
Matthias Braun1965bfa2013-10-10 21:28:38 +00002039 if (RegInfo->isSubRegister(Reg, MOReg))
Evan Cheng6c177732008-04-16 09:41:59 +00002040 DeadOps.push_back(i);
Owen Anderson2a8a4852008-01-24 01:10:07 +00002041 }
2042 }
2043
Evan Cheng6c177732008-04-16 09:41:59 +00002044 // Trim unneeded dead operands.
2045 while (!DeadOps.empty()) {
2046 unsigned OpIdx = DeadOps.back();
2047 if (getOperand(OpIdx).isImplicit())
2048 RemoveOperand(OpIdx);
2049 else
2050 getOperand(OpIdx).setIsDead(false);
2051 DeadOps.pop_back();
2052 }
2053
Dan Gohmanc7367b42008-09-03 15:56:16 +00002054 // If not found, this means an alias of one of the operands is dead. Add a
2055 // new implicit operand if required.
Chris Lattnerfd682802009-06-24 17:54:48 +00002056 if (Found || !AddIfNotFound)
2057 return Found;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00002058
Matthias Braun1965bfa2013-10-10 21:28:38 +00002059 addOperand(MachineOperand::CreateReg(Reg,
Chris Lattnerfd682802009-06-24 17:54:48 +00002060 true /*IsDef*/,
2061 true /*IsImp*/,
2062 false /*IsKill*/,
2063 true /*IsDead*/));
2064 return true;
Owen Anderson2a8a4852008-01-24 01:10:07 +00002065}
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00002066
Matthias Braun26e7ea62015-02-04 19:35:16 +00002067void MachineInstr::clearRegisterDeads(unsigned Reg) {
2068 for (MachineOperand &MO : operands()) {
2069 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg)
2070 continue;
2071 MO.setIsDead(false);
2072 }
2073}
2074
Matthias Braun2c98d0f2015-11-11 00:41:58 +00002075void MachineInstr::setRegisterDefReadUndef(unsigned Reg, bool IsUndef) {
Matthias Braunc1988f32015-01-21 22:55:13 +00002076 for (MachineOperand &MO : operands()) {
2077 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg || MO.getSubReg() == 0)
2078 continue;
Matthias Braun2c98d0f2015-11-11 00:41:58 +00002079 MO.setIsUndef(IsUndef);
Matthias Braunc1988f32015-01-21 22:55:13 +00002080 }
2081}
2082
Matthias Braun1965bfa2013-10-10 21:28:38 +00002083void MachineInstr::addRegisterDefined(unsigned Reg,
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00002084 const TargetRegisterInfo *RegInfo) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00002085 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
2086 MachineOperand *MO = findRegisterDefOperand(Reg, false, RegInfo);
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00002087 if (MO)
2088 return;
2089 } else {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002090 for (const MachineOperand &MO : operands()) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00002091 if (MO.isReg() && MO.getReg() == Reg && MO.isDef() &&
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00002092 MO.getSubReg() == 0)
2093 return;
2094 }
2095 }
Matthias Braun1965bfa2013-10-10 21:28:38 +00002096 addOperand(MachineOperand::CreateReg(Reg,
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00002097 true /*IsDef*/,
2098 true /*IsImp*/));
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00002099}
Evan Cheng59d27fe2010-03-03 23:37:30 +00002100
Jakob Stoklund Olesen4290be42012-02-03 20:43:39 +00002101void MachineInstr::setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs,
Dan Gohman86936502010-06-18 23:28:01 +00002102 const TargetRegisterInfo &TRI) {
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00002103 bool HasRegMask = false;
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002104 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00002105 if (MO.isRegMask()) {
2106 HasRegMask = true;
2107 continue;
2108 }
Dan Gohman86936502010-06-18 23:28:01 +00002109 if (!MO.isReg() || !MO.isDef()) continue;
2110 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenf6507322012-02-03 20:43:35 +00002111 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Dan Gohman86936502010-06-18 23:28:01 +00002112 // If there are no uses, including partial uses, the def is dead.
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002113 if (std::none_of(UsedRegs.begin(), UsedRegs.end(),
2114 [&](unsigned Use) { return TRI.regsOverlap(Use, Reg); }))
2115 MO.setIsDead();
Dan Gohman86936502010-06-18 23:28:01 +00002116 }
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00002117
2118 // This is a call with a register mask operand.
2119 // Mask clobbers are always dead, so add defs for the non-dead defines.
2120 if (HasRegMask)
2121 for (ArrayRef<unsigned>::iterator I = UsedRegs.begin(), E = UsedRegs.end();
2122 I != E; ++I)
2123 addRegisterDefined(*I, &TRI);
Dan Gohman86936502010-06-18 23:28:01 +00002124}
2125
Evan Cheng59d27fe2010-03-03 23:37:30 +00002126unsigned
2127MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) {
Chandler Carruth962152c2012-03-07 09:39:46 +00002128 // Build up a buffer of hash code components.
Chandler Carruth962152c2012-03-07 09:39:46 +00002129 SmallVector<size_t, 8> HashComponents;
2130 HashComponents.reserve(MI->getNumOperands() + 1);
2131 HashComponents.push_back(MI->getOpcode());
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002132 for (const MachineOperand &MO : MI->operands()) {
Chandler Carruth264854f2012-07-05 11:06:22 +00002133 if (MO.isReg() && MO.isDef() &&
2134 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
2135 continue; // Skip virtual register defs.
2136
2137 HashComponents.push_back(hash_value(MO));
Evan Cheng59d27fe2010-03-03 23:37:30 +00002138 }
Chandler Carruth962152c2012-03-07 09:39:46 +00002139 return hash_combine_range(HashComponents.begin(), HashComponents.end());
Evan Cheng59d27fe2010-03-03 23:37:30 +00002140}
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002141
2142void MachineInstr::emitError(StringRef Msg) const {
2143 // Find the source location cookie.
2144 unsigned LocCookie = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00002145 const MDNode *LocMD = nullptr;
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002146 for (unsigned i = getNumOperands(); i != 0; --i) {
2147 if (getOperand(i-1).isMetadata() &&
2148 (LocMD = getOperand(i-1).getMetadata()) &&
2149 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002150 if (const ConstantInt *CI =
2151 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002152 LocCookie = CI->getZExtValue();
2153 break;
2154 }
2155 }
2156 }
2157
2158 if (const MachineBasicBlock *MBB = getParent())
2159 if (const MachineFunction *MF = MBB->getParent())
2160 return MF->getMMI().getModule()->getContext().emitError(LocCookie, Msg);
2161 report_fatal_error(Msg);
2162}
Reid Kleckner28865802016-04-14 18:29:59 +00002163
2164MachineInstrBuilder llvm::BuildMI(MachineFunction &MF, DebugLoc DL,
2165 const MCInstrDesc &MCID, bool IsIndirect,
2166 unsigned Reg, unsigned Offset,
2167 const MDNode *Variable, const MDNode *Expr) {
2168 assert(isa<DILocalVariable>(Variable) && "not a variable");
2169 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
2170 assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
2171 "Expected inlined-at fields to agree");
2172 if (IsIndirect)
2173 return BuildMI(MF, DL, MCID)
2174 .addReg(Reg, RegState::Debug)
2175 .addImm(Offset)
2176 .addMetadata(Variable)
2177 .addMetadata(Expr);
2178 else {
2179 assert(Offset == 0 && "A direct address cannot have an offset.");
2180 return BuildMI(MF, DL, MCID)
2181 .addReg(Reg, RegState::Debug)
2182 .addReg(0U, RegState::Debug)
2183 .addMetadata(Variable)
2184 .addMetadata(Expr);
2185 }
2186}
2187
2188MachineInstrBuilder llvm::BuildMI(MachineBasicBlock &BB,
2189 MachineBasicBlock::iterator I, DebugLoc DL,
2190 const MCInstrDesc &MCID, bool IsIndirect,
2191 unsigned Reg, unsigned Offset,
2192 const MDNode *Variable, const MDNode *Expr) {
2193 assert(isa<DILocalVariable>(Variable) && "not a variable");
2194 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
2195 MachineFunction &MF = *BB.getParent();
2196 MachineInstr *MI =
2197 BuildMI(MF, DL, MCID, IsIndirect, Reg, Offset, Variable, Expr);
2198 BB.insert(I, MI);
2199 return MachineInstrBuilder(MF, MI);
2200}