blob: 8afa9ae3098e8ccae61dceb90b91b5e568f38fff [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"
Tim Northover6b3bd612016-07-29 20:32:59 +000029#include "llvm/IR/Intrinsics.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/Metadata.h"
32#include "llvm/IR/Module.h"
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +000033#include "llvm/IR/ModuleSlotTracker.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/Type.h"
35#include "llvm/IR/Value.h"
Evan Cheng6cc775f2011-06-28 19:10:37 +000036#include "llvm/MC/MCInstrDesc.h"
Chris Lattner6c604e32010-03-13 08:14:18 +000037#include "llvm/MC/MCSymbol.h"
Daniel Sanders1e97a0b2015-08-19 12:03:04 +000038#include "llvm/Support/CommandLine.h"
David Greene29388d62010-01-04 23:48:20 +000039#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000040#include "llvm/Support/ErrorHandling.h"
Dan Gohmanaedb4a62008-07-07 20:32:02 +000041#include "llvm/Support/MathExtras.h"
Chris Lattnera078d832008-08-24 20:37:32 +000042#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000043#include "llvm/Target/TargetInstrInfo.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000044#include "llvm/Target/TargetIntrinsicInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000045#include "llvm/Target/TargetMachine.h"
46#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000047#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner43df6c22004-02-23 18:38:20 +000048using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000049
Daniel Sanders1e97a0b2015-08-19 12:03:04 +000050static cl::opt<bool> PrintWholeRegMask(
51 "print-whole-regmask",
52 cl::desc("Print the full contents of regmask operands in IR dumps"),
53 cl::init(true), cl::Hidden);
54
Chris Lattner60055892007-12-30 21:56:09 +000055//===----------------------------------------------------------------------===//
56// MachineOperand Implementation
57//===----------------------------------------------------------------------===//
58
Chris Lattner961e7422008-01-01 01:12:31 +000059void MachineOperand::setReg(unsigned Reg) {
60 if (getReg() == Reg) return; // No change.
Jim Grosbachdee9e8a2011-08-24 16:44:17 +000061
Chris Lattner961e7422008-01-01 01:12:31 +000062 // Otherwise, we have to change the register. If this operand is embedded
63 // into a machine function, we need to update the old and new register's
64 // use/def lists.
65 if (MachineInstr *MI = getParent())
66 if (MachineBasicBlock *MBB = MI->getParent())
67 if (MachineFunction *MF = MBB->getParent()) {
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +000068 MachineRegisterInfo &MRI = MF->getRegInfo();
69 MRI.removeRegOperandFromUseList(this);
Jakob Stoklund Olesena4941692010-10-19 20:56:32 +000070 SmallContents.RegNo = Reg;
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +000071 MRI.addRegOperandToUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +000072 return;
73 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +000074
Chris Lattner961e7422008-01-01 01:12:31 +000075 // Otherwise, just change the register, no problem. :)
Jakob Stoklund Olesena4941692010-10-19 20:56:32 +000076 SmallContents.RegNo = Reg;
Chris Lattner961e7422008-01-01 01:12:31 +000077}
78
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +000079void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx,
80 const TargetRegisterInfo &TRI) {
81 assert(TargetRegisterInfo::isVirtualRegister(Reg));
82 if (SubIdx && getSubReg())
83 SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg());
84 setReg(Reg);
Jakob Stoklund Olesen7b0ac862010-06-01 22:39:25 +000085 if (SubIdx)
86 setSubReg(SubIdx);
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +000087}
88
89void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) {
90 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
91 if (getSubReg()) {
92 Reg = TRI.getSubReg(Reg, getSubReg());
Jakob Stoklund Olesen89bd2ae2011-05-08 19:21:08 +000093 // Note that getSubReg() may return 0 if the sub-register doesn't exist.
94 // That won't happen in legal code.
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +000095 setSubReg(0);
96 }
97 setReg(Reg);
98}
99
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000100/// Change a def to a use, or a use to a def.
101void MachineOperand::setIsDef(bool Val) {
102 assert(isReg() && "Wrong MachineOperand accessor");
103 assert((!Val || !isDebug()) && "Marking a debug operation as def");
104 if (IsDef == Val)
105 return;
106 // MRI may keep uses and defs in different list positions.
107 if (MachineInstr *MI = getParent())
108 if (MachineBasicBlock *MBB = MI->getParent())
109 if (MachineFunction *MF = MBB->getParent()) {
110 MachineRegisterInfo &MRI = MF->getRegInfo();
111 MRI.removeRegOperandFromUseList(this);
112 IsDef = Val;
113 MRI.addRegOperandToUseList(this);
114 return;
115 }
116 IsDef = Val;
117}
118
Matt Arsenault93ffe582014-09-28 19:24:59 +0000119// If this operand is currently a register operand, and if this is in a
120// function, deregister the operand from the register's use/def list.
121void MachineOperand::removeRegFromUses() {
122 if (!isReg() || !isOnRegUseList())
123 return;
124
125 if (MachineInstr *MI = getParent()) {
126 if (MachineBasicBlock *MBB = MI->getParent()) {
127 if (MachineFunction *MF = MBB->getParent())
128 MF->getRegInfo().removeRegOperandFromUseList(this);
129 }
130 }
131}
132
Chris Lattner961e7422008-01-01 01:12:31 +0000133/// ChangeToImmediate - Replace this operand with a new immediate operand of
134/// the specified value. If an operand is known to be an immediate already,
135/// the setImm method should be used.
136void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000137 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
Matt Arsenault93ffe582014-09-28 19:24:59 +0000138
139 removeRegFromUses();
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000140
Chris Lattner961e7422008-01-01 01:12:31 +0000141 OpKind = MO_Immediate;
142 Contents.ImmVal = ImmVal;
143}
144
Matt Arsenault93ffe582014-09-28 19:24:59 +0000145void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm) {
146 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
147
148 removeRegFromUses();
149
150 OpKind = MO_FPImmediate;
151 Contents.CFP = FPImm;
152}
153
Matt Arsenault633dba42015-05-06 17:05:54 +0000154void MachineOperand::ChangeToES(const char *SymName, unsigned char TargetFlags) {
155 assert((!isReg() || !isTied()) &&
156 "Cannot change a tied operand into an external symbol");
157
158 removeRegFromUses();
159
160 OpKind = MO_ExternalSymbol;
161 Contents.OffsetedInfo.Val.SymbolName = SymName;
162 setOffset(0); // Offset is always 0.
163 setTargetFlags(TargetFlags);
164}
165
166void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym) {
167 assert((!isReg() || !isTied()) &&
168 "Cannot change a tied operand into an MCSymbol");
169
170 removeRegFromUses();
171
172 OpKind = MO_MCSymbol;
173 Contents.Sym = Sym;
174}
175
Chris Lattner961e7422008-01-01 01:12:31 +0000176/// ChangeToRegister - Replace this operand with a new register operand of
177/// the specified value. If an operand is known to be an register already,
178/// the setReg method should be used.
179void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Dale Johannesend40d42c2010-02-10 00:41:49 +0000180 bool isKill, bool isDead, bool isUndef,
181 bool isDebug) {
Craig Topperc0196b12014-04-14 00:51:57 +0000182 MachineRegisterInfo *RegInfo = nullptr;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000183 if (MachineInstr *MI = getParent())
184 if (MachineBasicBlock *MBB = MI->getParent())
185 if (MachineFunction *MF = MBB->getParent())
186 RegInfo = &MF->getRegInfo();
187 // If this operand is already a register operand, remove it from the
Chris Lattner961e7422008-01-01 01:12:31 +0000188 // register's use/def lists.
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000189 bool WasReg = isReg();
190 if (RegInfo && WasReg)
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000191 RegInfo->removeRegOperandFromUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +0000192
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000193 // Change this to a register and set the reg#.
194 OpKind = MO_Register;
195 SmallContents.RegNo = Reg;
Jakob Stoklund Olesena1b246d2013-01-07 23:21:44 +0000196 SubReg_TargetFlags = 0;
Chris Lattner961e7422008-01-01 01:12:31 +0000197 IsDef = isDef;
198 IsImp = isImp;
199 IsKill = isKill;
200 IsDead = isDead;
Evan Cheng0dc101b2009-06-30 08:49:04 +0000201 IsUndef = isUndef;
Jakob Stoklund Olesenb0d91ab2011-12-07 00:22:07 +0000202 IsInternalRead = false;
Dale Johannesenc0d712d2008-09-14 01:44:36 +0000203 IsEarlyClobber = false;
Dale Johannesend40d42c2010-02-10 00:41:49 +0000204 IsDebug = isDebug;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000205 // Ensure isOnRegUseList() returns false.
Craig Topperc0196b12014-04-14 00:51:57 +0000206 Contents.Reg.Prev = nullptr;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000207 // Preserve the tie when the operand was already a register.
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000208 if (!WasReg)
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000209 TiedTo = 0;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000210
211 // If this operand is embedded in a function, add the operand to the
212 // register's use/def list.
213 if (RegInfo)
214 RegInfo->addRegOperandToUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +0000215}
216
Chris Lattner60055892007-12-30 21:56:09 +0000217/// isIdenticalTo - Return true if this operand is identical to the specified
Chandler Carruth264854f2012-07-05 11:06:22 +0000218/// operand. Note that this should stay in sync with the hash_value overload
219/// below.
Chris Lattner60055892007-12-30 21:56:09 +0000220bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
Chris Lattnerfd682802009-06-24 17:54:48 +0000221 if (getType() != Other.getType() ||
222 getTargetFlags() != Other.getTargetFlags())
223 return false;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000224
Chris Lattner60055892007-12-30 21:56:09 +0000225 switch (getType()) {
Chris Lattner60055892007-12-30 21:56:09 +0000226 case MachineOperand::MO_Register:
227 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
228 getSubReg() == Other.getSubReg();
229 case MachineOperand::MO_Immediate:
230 return getImm() == Other.getImm();
Cameron Zwarich7da0f9a2011-07-01 23:45:21 +0000231 case MachineOperand::MO_CImmediate:
232 return getCImm() == Other.getCImm();
Nate Begeman26b76b62008-02-14 07:39:30 +0000233 case MachineOperand::MO_FPImmediate:
234 return getFPImm() == Other.getFPImm();
Chris Lattner60055892007-12-30 21:56:09 +0000235 case MachineOperand::MO_MachineBasicBlock:
236 return getMBB() == Other.getMBB();
237 case MachineOperand::MO_FrameIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000238 return getIndex() == Other.getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000239 case MachineOperand::MO_ConstantPoolIndex:
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000240 case MachineOperand::MO_TargetIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000241 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattner60055892007-12-30 21:56:09 +0000242 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000243 return getIndex() == Other.getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000244 case MachineOperand::MO_GlobalAddress:
245 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
246 case MachineOperand::MO_ExternalSymbol:
247 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
248 getOffset() == Other.getOffset();
Dan Gohman6c938802009-10-30 01:27:03 +0000249 case MachineOperand::MO_BlockAddress:
Michael Liaoabb87d42012-09-12 21:43:09 +0000250 return getBlockAddress() == Other.getBlockAddress() &&
251 getOffset() == Other.getOffset();
Juergen Ributzkae8294752013-12-14 06:53:06 +0000252 case MachineOperand::MO_RegisterMask:
253 case MachineOperand::MO_RegisterLiveOut:
Jakob Stoklund Olesen374ed322012-01-16 19:22:00 +0000254 return getRegMask() == Other.getRegMask();
Chris Lattner6c604e32010-03-13 08:14:18 +0000255 case MachineOperand::MO_MCSymbol:
256 return getMCSymbol() == Other.getMCSymbol();
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000257 case MachineOperand::MO_CFIIndex:
258 return getCFIIndex() == Other.getCFIIndex();
Chris Lattnerf839ee02010-04-07 18:03:19 +0000259 case MachineOperand::MO_Metadata:
260 return getMetadata() == Other.getMetadata();
Tim Northover6b3bd612016-07-29 20:32:59 +0000261 case MachineOperand::MO_IntrinsicID:
262 return getIntrinsicID() == Other.getIntrinsicID();
Chris Lattner60055892007-12-30 21:56:09 +0000263 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000264 llvm_unreachable("Invalid machine operand type");
Chris Lattner60055892007-12-30 21:56:09 +0000265}
266
Chandler Carruth264854f2012-07-05 11:06:22 +0000267// Note: this must stay exactly in sync with isIdenticalTo above.
268hash_code llvm::hash_value(const MachineOperand &MO) {
269 switch (MO.getType()) {
270 case MachineOperand::MO_Register:
Jakob Stoklund Olesendba99d02012-08-28 18:05:48 +0000271 // Register operands don't have target flags.
272 return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef());
Chandler Carruth264854f2012-07-05 11:06:22 +0000273 case MachineOperand::MO_Immediate:
274 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm());
275 case MachineOperand::MO_CImmediate:
276 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm());
277 case MachineOperand::MO_FPImmediate:
278 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm());
279 case MachineOperand::MO_MachineBasicBlock:
280 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB());
281 case MachineOperand::MO_FrameIndex:
282 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
283 case MachineOperand::MO_ConstantPoolIndex:
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000284 case MachineOperand::MO_TargetIndex:
Chandler Carruth264854f2012-07-05 11:06:22 +0000285 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(),
286 MO.getOffset());
287 case MachineOperand::MO_JumpTableIndex:
288 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
289 case MachineOperand::MO_ExternalSymbol:
290 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(),
291 MO.getSymbolName());
292 case MachineOperand::MO_GlobalAddress:
293 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(),
294 MO.getOffset());
295 case MachineOperand::MO_BlockAddress:
296 return hash_combine(MO.getType(), MO.getTargetFlags(),
Michael Liaoabb87d42012-09-12 21:43:09 +0000297 MO.getBlockAddress(), MO.getOffset());
Chandler Carruth264854f2012-07-05 11:06:22 +0000298 case MachineOperand::MO_RegisterMask:
Juergen Ributzkae8294752013-12-14 06:53:06 +0000299 case MachineOperand::MO_RegisterLiveOut:
Chandler Carruth264854f2012-07-05 11:06:22 +0000300 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask());
301 case MachineOperand::MO_Metadata:
302 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata());
303 case MachineOperand::MO_MCSymbol:
304 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol());
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000305 case MachineOperand::MO_CFIIndex:
306 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex());
Tim Northover6b3bd612016-07-29 20:32:59 +0000307 case MachineOperand::MO_IntrinsicID:
308 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIntrinsicID());
Chandler Carruth264854f2012-07-05 11:06:22 +0000309 }
310 llvm_unreachable("Invalid machine operand type");
311}
312
Tim Northover6b3bd612016-07-29 20:32:59 +0000313void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI,
314 const TargetIntrinsicInfo *IntrinsicInfo) const {
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000315 ModuleSlotTracker DummyMST(nullptr);
Tim Northover6b3bd612016-07-29 20:32:59 +0000316 print(OS, DummyMST, TRI, IntrinsicInfo);
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000317}
318
319void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
Tim Northover6b3bd612016-07-29 20:32:59 +0000320 const TargetRegisterInfo *TRI,
321 const TargetIntrinsicInfo *IntrinsicInfo) const {
Chris Lattner60055892007-12-30 21:56:09 +0000322 switch (getType()) {
323 case MachineOperand::MO_Register:
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000324 OS << PrintReg(getReg(), TRI, getSubReg());
Dan Gohman0ab11442008-12-18 21:51:27 +0000325
Evan Cheng0dc101b2009-06-30 08:49:04 +0000326 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000327 isInternalRead() || isEarlyClobber() || isTied()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000328 OS << '<';
Chris Lattner60055892007-12-30 21:56:09 +0000329 bool NeedComma = false;
Evan Cheng70b1fa52009-10-14 23:37:31 +0000330 if (isDef()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000331 if (NeedComma) OS << ',';
Dale Johannesen1f3ab862008-09-12 17:49:03 +0000332 if (isEarlyClobber())
333 OS << "earlyclobber,";
Evan Cheng70b1fa52009-10-14 23:37:31 +0000334 if (isImplicit())
335 OS << "imp-";
Chris Lattner60055892007-12-30 21:56:09 +0000336 OS << "def";
337 NeedComma = true;
Jakob Stoklund Olesen7111a632012-04-20 21:45:33 +0000338 // <def,read-undef> only makes sense when getSubReg() is set.
339 // Don't clutter the output otherwise.
340 if (isUndef() && getSubReg())
341 OS << ",read-undef";
Evan Chengf781bd82009-10-21 07:56:02 +0000342 } else if (isImplicit()) {
Craig Topper9a9d58a2015-05-16 05:42:08 +0000343 OS << "imp-use";
344 NeedComma = true;
Evan Chengf781bd82009-10-21 07:56:02 +0000345 }
Evan Cheng70b1fa52009-10-14 23:37:31 +0000346
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000347 if (isKill()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000348 if (NeedComma) OS << ',';
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000349 OS << "kill";
350 NeedComma = true;
351 }
352 if (isDead()) {
353 if (NeedComma) OS << ',';
354 OS << "dead";
355 NeedComma = true;
356 }
357 if (isUndef() && isUse()) {
358 if (NeedComma) OS << ',';
359 OS << "undef";
360 NeedComma = true;
361 }
362 if (isInternalRead()) {
363 if (NeedComma) OS << ',';
364 OS << "internal";
365 NeedComma = true;
366 }
367 if (isTied()) {
368 if (NeedComma) OS << ',';
369 OS << "tied";
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000370 if (TiedTo != 15)
371 OS << unsigned(TiedTo - 1);
Chris Lattner60055892007-12-30 21:56:09 +0000372 }
Chris Lattnerfd682802009-06-24 17:54:48 +0000373 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000374 }
375 break;
376 case MachineOperand::MO_Immediate:
377 OS << getImm();
378 break;
Devang Patelf071d722011-06-24 20:46:11 +0000379 case MachineOperand::MO_CImmediate:
380 getCImm()->getValue().print(OS, false);
381 break;
Nate Begeman26b76b62008-02-14 07:39:30 +0000382 case MachineOperand::MO_FPImmediate:
Matt Arsenault59239732016-02-05 00:50:18 +0000383 if (getFPImm()->getType()->isFloatTy()) {
Nate Begeman26b76b62008-02-14 07:39:30 +0000384 OS << getFPImm()->getValueAPF().convertToFloat();
Matt Arsenault59239732016-02-05 00:50:18 +0000385 } else if (getFPImm()->getType()->isHalfTy()) {
386 APFloat APF = getFPImm()->getValueAPF();
387 bool Unused;
388 APF.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &Unused);
389 OS << "half " << APF.convertToFloat();
390 } else {
Nate Begeman26b76b62008-02-14 07:39:30 +0000391 OS << getFPImm()->getValueAPF().convertToDouble();
Matt Arsenault59239732016-02-05 00:50:18 +0000392 }
Nate Begeman26b76b62008-02-14 07:39:30 +0000393 break;
Chris Lattner60055892007-12-30 21:56:09 +0000394 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman34341e62009-10-31 20:19:03 +0000395 OS << "<BB#" << getMBB()->getNumber() << ">";
Chris Lattner60055892007-12-30 21:56:09 +0000396 break;
397 case MachineOperand::MO_FrameIndex:
Chris Lattnerfd682802009-06-24 17:54:48 +0000398 OS << "<fi#" << getIndex() << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000399 break;
400 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000401 OS << "<cp#" << getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000402 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000403 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000404 break;
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000405 case MachineOperand::MO_TargetIndex:
406 OS << "<ti#" << getIndex();
407 if (getOffset()) OS << "+" << getOffset();
408 OS << '>';
409 break;
Chris Lattner60055892007-12-30 21:56:09 +0000410 case MachineOperand::MO_JumpTableIndex:
Chris Lattnerfd682802009-06-24 17:54:48 +0000411 OS << "<jt#" << getIndex() << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000412 break;
413 case MachineOperand::MO_GlobalAddress:
Dan Gohman0080ee22009-11-06 18:03:10 +0000414 OS << "<ga:";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000415 getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Chris Lattner60055892007-12-30 21:56:09 +0000416 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000417 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000418 break;
419 case MachineOperand::MO_ExternalSymbol:
420 OS << "<es:" << getSymbolName();
421 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000422 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000423 break;
Dan Gohman6c938802009-10-30 01:27:03 +0000424 case MachineOperand::MO_BlockAddress:
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000425 OS << '<';
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000426 getBlockAddress()->printAsOperand(OS, /*PrintType=*/false, MST);
Michael Liaoabb87d42012-09-12 21:43:09 +0000427 if (getOffset()) OS << "+" << getOffset();
Dan Gohman6c938802009-10-30 01:27:03 +0000428 OS << '>';
429 break;
Daniel Sanders1e97a0b2015-08-19 12:03:04 +0000430 case MachineOperand::MO_RegisterMask: {
431 unsigned NumRegsInMask = 0;
432 unsigned NumRegsEmitted = 0;
433 OS << "<regmask";
434 for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {
435 unsigned MaskWord = i / 32;
436 unsigned MaskBit = i % 32;
437 if (getRegMask()[MaskWord] & (1 << MaskBit)) {
438 if (PrintWholeRegMask || NumRegsEmitted <= 10) {
439 OS << " " << PrintReg(i, TRI);
440 NumRegsEmitted++;
441 }
442 NumRegsInMask++;
443 }
444 }
445 if (NumRegsEmitted != NumRegsInMask)
446 OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more...";
447 OS << ">";
Jakob Stoklund Olesen374ed322012-01-16 19:22:00 +0000448 break;
Daniel Sanders1e97a0b2015-08-19 12:03:04 +0000449 }
Juergen Ributzkae8294752013-12-14 06:53:06 +0000450 case MachineOperand::MO_RegisterLiveOut:
451 OS << "<regliveout>";
452 break;
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000453 case MachineOperand::MO_Metadata:
454 OS << '<';
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000455 getMetadata()->printAsOperand(OS, MST);
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000456 OS << '>';
457 break;
Chris Lattner6c604e32010-03-13 08:14:18 +0000458 case MachineOperand::MO_MCSymbol:
459 OS << "<MCSym=" << *getMCSymbol() << '>';
460 break;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000461 case MachineOperand::MO_CFIIndex:
462 OS << "<call frame instruction>";
463 break;
Tim Northover6b3bd612016-07-29 20:32:59 +0000464 case MachineOperand::MO_IntrinsicID: {
465 Intrinsic::ID ID = getIntrinsicID();
466 if (ID < Intrinsic::num_intrinsics)
467 OS << "<intrinsic:@" << Intrinsic::getName(ID) << ')';
468 else if (IntrinsicInfo)
469 OS << "<intrinsic:@" << IntrinsicInfo->getName(ID) << ')';
470 else
471 OS << "<intrinsic:" << ID << '>';
472 break;
473 }
Chris Lattner60055892007-12-30 21:56:09 +0000474 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000475
Chris Lattnerfd682802009-06-24 17:54:48 +0000476 if (unsigned TF = getTargetFlags())
477 OS << "[TF=" << TF << ']';
Chris Lattner60055892007-12-30 21:56:09 +0000478}
479
480//===----------------------------------------------------------------------===//
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000481// MachineMemOperand Implementation
482//===----------------------------------------------------------------------===//
483
Chris Lattnerde93bb02010-09-21 05:39:30 +0000484/// getAddrSpace - Return the LLVM IR address space number that this pointer
485/// points into.
486unsigned MachinePointerInfo::getAddrSpace() const {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000487 if (V.isNull() || V.is<const PseudoSourceValue*>()) return 0;
488 return cast<PointerType>(V.get<const Value*>()->getType())->getAddressSpace();
Chris Lattnerde93bb02010-09-21 05:39:30 +0000489}
490
Chris Lattner82fd06d2010-09-21 06:22:23 +0000491/// getConstantPool - Return a MachinePointerInfo record that refers to the
492/// constant pool.
Alex Lorenze40c8a22015-08-11 23:09:45 +0000493MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) {
494 return MachinePointerInfo(MF.getPSVManager().getConstantPool());
Chris Lattner82fd06d2010-09-21 06:22:23 +0000495}
496
497/// getFixedStack - Return a MachinePointerInfo record that refers to the
498/// the specified FrameIndex.
Alex Lorenze40c8a22015-08-11 23:09:45 +0000499MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF,
500 int FI, int64_t Offset) {
501 return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset);
Chris Lattner82fd06d2010-09-21 06:22:23 +0000502}
503
Alex Lorenze40c8a22015-08-11 23:09:45 +0000504MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) {
505 return MachinePointerInfo(MF.getPSVManager().getJumpTable());
Chris Lattner50287ea2010-09-21 06:43:24 +0000506}
507
Alex Lorenze40c8a22015-08-11 23:09:45 +0000508MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) {
509 return MachinePointerInfo(MF.getPSVManager().getGOT());
Chris Lattner50287ea2010-09-21 06:43:24 +0000510}
Chris Lattnerde93bb02010-09-21 05:39:30 +0000511
Alex Lorenze40c8a22015-08-11 23:09:45 +0000512MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF,
513 int64_t Offset) {
514 return MachinePointerInfo(MF.getPSVManager().getStack(), Offset);
Chris Lattner886250c2010-09-21 18:51:21 +0000515}
516
Justin Lebara3b786a2016-07-14 17:07:44 +0000517MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000518 uint64_t s, unsigned int a,
Hal Finkelcc39b672014-07-24 12:16:19 +0000519 const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +0000520 const MDNode *Ranges)
Justin Lebara3b786a2016-07-14 17:07:44 +0000521 : PtrInfo(ptrinfo), Size(s), FlagVals(f), BaseAlignLog2(Log2_32(a) + 1),
522 AAInfo(AAInfo), Ranges(Ranges) {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000523 assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue*>() ||
524 isa<PointerType>(PtrInfo.V.get<const Value*>()->getType())) &&
Chris Lattner00ca0b82010-09-21 04:32:08 +0000525 "invalid pointer value");
Dan Gohmane7c82422009-09-21 19:47:04 +0000526 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
Dan Gohmanbf98f682008-07-16 15:56:42 +0000527 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000528}
529
Dan Gohman2da2bed2008-08-20 15:58:01 +0000530/// Profile - Gather unique data for the object.
531///
532void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
Chris Lattner187f6532010-09-21 04:23:39 +0000533 ID.AddInteger(getOffset());
Dan Gohman2da2bed2008-08-20 15:58:01 +0000534 ID.AddInteger(Size);
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000535 ID.AddPointer(getOpaqueValue());
Justin Lebara3b786a2016-07-14 17:07:44 +0000536 ID.AddInteger(getFlags());
537 ID.AddInteger(getBaseAlignment());
Dan Gohman2da2bed2008-08-20 15:58:01 +0000538}
539
Dan Gohman48b185d2009-09-25 20:36:54 +0000540void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
541 // The Value and Offset may differ due to CSE. But the flags and size
542 // should be the same.
543 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
544 assert(MMO->getSize() == getSize() && "Size mismatch!");
545
546 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
547 // Update the alignment value.
Justin Lebara3b786a2016-07-14 17:07:44 +0000548 BaseAlignLog2 = Log2_32(MMO->getBaseAlignment()) + 1;
Dan Gohman48b185d2009-09-25 20:36:54 +0000549 // Also update the base and offset, because the new alignment may
550 // not be applicable with the old ones.
Chris Lattner187f6532010-09-21 04:23:39 +0000551 PtrInfo = MMO->PtrInfo;
Dan Gohman48b185d2009-09-25 20:36:54 +0000552 }
553}
554
Dan Gohman5a6b11c2009-09-25 23:33:20 +0000555/// getAlignment - Return the minimum known alignment in bytes of the
556/// actual memory reference.
557uint64_t MachineMemOperand::getAlignment() const {
558 return MinAlign(getBaseAlignment(), getOffset());
559}
560
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000561void MachineMemOperand::print(raw_ostream &OS) const {
562 ModuleSlotTracker DummyMST(nullptr);
563 print(OS, DummyMST);
564}
565void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST) const {
566 assert((isLoad() || isStore()) &&
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000567 "SV has to be a load, store or both.");
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000568
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000569 if (isVolatile())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000570 OS << "Volatile ";
571
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000572 if (isLoad())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000573 OS << "LD";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000574 if (isStore())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000575 OS << "ST";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000576 OS << getSize();
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000577
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000578 // Print the address information.
579 OS << "[";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000580 if (const Value *V = getValue())
581 V->printAsOperand(OS, /*PrintType=*/false, MST);
582 else if (const PseudoSourceValue *PSV = getPseudoValue())
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000583 PSV->printCustom(OS);
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000584 else
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000585 OS << "<unknown>";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000586
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000587 unsigned AS = getAddrSpace();
Matt Arsenault68c38fd2013-12-14 00:24:02 +0000588 if (AS != 0)
589 OS << "(addrspace=" << AS << ')';
590
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000591 // If the alignment of the memory reference itself differs from the alignment
592 // of the base pointer, print the base alignment explicitly, next to the base
593 // pointer.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000594 if (getBaseAlignment() != getAlignment())
595 OS << "(align=" << getBaseAlignment() << ")";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000596
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000597 if (getOffset() != 0)
598 OS << "+" << getOffset();
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000599 OS << "]";
600
601 // Print the alignment of the reference.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000602 if (getBaseAlignment() != getAlignment() || getBaseAlignment() != getSize())
603 OS << "(align=" << getAlignment() << ")";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000604
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000605 // Print TBAA info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000606 if (const MDNode *TBAAInfo = getAAInfo().TBAA) {
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000607 OS << "(tbaa=";
608 if (TBAAInfo->getNumOperands() > 0)
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000609 TBAAInfo->getOperand(0)->printAsOperand(OS, MST);
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000610 else
611 OS << "<unknown>";
612 OS << ")";
613 }
614
Hal Finkel94146652014-07-24 14:25:39 +0000615 // Print AA scope info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000616 if (const MDNode *ScopeInfo = getAAInfo().Scope) {
Hal Finkel94146652014-07-24 14:25:39 +0000617 OS << "(alias.scope=";
618 if (ScopeInfo->getNumOperands() > 0)
619 for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000620 ScopeInfo->getOperand(i)->printAsOperand(OS, MST);
Hal Finkel94146652014-07-24 14:25:39 +0000621 if (i != ie-1)
622 OS << ",";
623 }
624 else
625 OS << "<unknown>";
626 OS << ")";
627 }
628
629 // Print AA noalias scope info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000630 if (const MDNode *NoAliasInfo = getAAInfo().NoAlias) {
Hal Finkel94146652014-07-24 14:25:39 +0000631 OS << "(noalias=";
632 if (NoAliasInfo->getNumOperands() > 0)
633 for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000634 NoAliasInfo->getOperand(i)->printAsOperand(OS, MST);
Hal Finkel94146652014-07-24 14:25:39 +0000635 if (i != ie-1)
636 OS << ",";
637 }
638 else
639 OS << "<unknown>";
640 OS << ")";
641 }
642
Bill Wendling9f638ab2011-04-29 23:45:22 +0000643 // Print nontemporal info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000644 if (isNonTemporal())
Bill Wendling9f638ab2011-04-29 23:45:22 +0000645 OS << "(nontemporal)";
646
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000647 if (isInvariant())
Matt Arsenault572c29a2015-06-26 19:00:11 +0000648 OS << "(invariant)";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000649}
650
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000651//===----------------------------------------------------------------------===//
Chris Lattner60055892007-12-30 21:56:09 +0000652// MachineInstr Implementation
653//===----------------------------------------------------------------------===//
654
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000655void MachineInstr::addImplicitDefUseOperands(MachineFunction &MF) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000656 if (MCID->ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000657 for (const MCPhysReg *ImpDefs = MCID->getImplicitDefs(); *ImpDefs;
658 ++ImpDefs)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000659 addOperand(MF, MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng6cc775f2011-06-28 19:10:37 +0000660 if (MCID->ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000661 for (const MCPhysReg *ImpUses = MCID->getImplicitUses(); *ImpUses;
662 ++ImpUses)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000663 addOperand(MF, MachineOperand::CreateReg(*ImpUses, false, true));
Evan Cheng77af6ac2006-11-13 23:34:06 +0000664}
665
Bob Wilson406f2702010-04-09 04:34:03 +0000666/// MachineInstr ctor - This constructor creates a MachineInstr and adds the
667/// implicit operands. It reserves space for the number of operands specified by
Evan Cheng6cc775f2011-06-28 19:10:37 +0000668/// the MCInstrDesc.
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000669MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid,
Benjamin Kramera9591b52015-02-07 12:28:15 +0000670 DebugLoc dl, bool NoImp)
671 : MCID(&tid), Parent(nullptr), Operands(nullptr), NumOperands(0), Flags(0),
672 AsmPrinterFlags(0), NumMemRefs(0), MemRefs(nullptr),
Quentin Colombet98551112016-02-11 18:22:37 +0000673 debugLoc(std::move(dl))
674#ifdef LLVM_BUILD_GLOBAL_ISEL
675 ,
Tim Northover98a56eb2016-07-22 22:13:36 +0000676 Tys(0)
Quentin Colombet98551112016-02-11 18:22:37 +0000677#endif
678{
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000679 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
680
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000681 // Reserve space for the expected number of operands.
682 if (unsigned NumOps = MCID->getNumOperands() +
683 MCID->getNumImplicitDefs() + MCID->getNumImplicitUses()) {
684 CapOperands = OperandCapacity::get(NumOps);
685 Operands = MF.allocateOperandArray(CapOperands);
686 }
687
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000688 if (!NoImp)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000689 addImplicitDefUseOperands(MF);
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000690}
691
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000692/// MachineInstr ctor - Copies MachineInstr arg exactly
693///
Evan Chenga7a20c42008-07-19 00:37:25 +0000694MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Quentin Colombet98551112016-02-11 18:22:37 +0000695 : MCID(&MI.getDesc()), Parent(nullptr), Operands(nullptr), NumOperands(0),
696 Flags(0), AsmPrinterFlags(0), NumMemRefs(MI.NumMemRefs),
697 MemRefs(MI.MemRefs), debugLoc(MI.getDebugLoc())
698#ifdef LLVM_BUILD_GLOBAL_ISEL
699 ,
Tim Northover98a56eb2016-07-22 22:13:36 +0000700 Tys(0)
Quentin Colombet98551112016-02-11 18:22:37 +0000701#endif
702{
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000703 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
704
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000705 CapOperands = OperandCapacity::get(MI.getNumOperands());
706 Operands = MF.allocateOperandArray(CapOperands);
Tanya Lattner9953d862004-05-23 20:58:02 +0000707
Jakob Stoklund Olesendc5285f2013-01-05 05:05:51 +0000708 // Copy operands.
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000709 for (const MachineOperand &MO : MI.operands())
710 addOperand(MF, MO);
Tanya Lattnerbcee21b2004-05-24 03:14:18 +0000711
Jakob Stoklund Olesena33f5042012-12-18 21:36:05 +0000712 // Copy all the sensible flags.
713 setFlags(MI.Flags);
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +0000714}
715
Chris Lattner961e7422008-01-01 01:12:31 +0000716/// getRegInfo - If this instruction is embedded into a MachineFunction,
717/// return the MachineRegisterInfo object for the current function, otherwise
718/// return null.
719MachineRegisterInfo *MachineInstr::getRegInfo() {
720 if (MachineBasicBlock *MBB = getParent())
Dan Gohmanf188fa42008-07-08 23:59:09 +0000721 return &MBB->getParent()->getRegInfo();
Craig Topperc0196b12014-04-14 00:51:57 +0000722 return nullptr;
Chris Lattner961e7422008-01-01 01:12:31 +0000723}
724
Quentin Colombet41bea872016-03-07 22:47:23 +0000725// Implement dummy setter and getter for type when
726// global-isel is not built.
727// The proper implementation is WIP and is tracked here:
728// PR26576.
729#ifndef LLVM_BUILD_GLOBAL_ISEL
Tim Northover98a56eb2016-07-22 22:13:36 +0000730unsigned MachineInstr::getNumTypes() const { return 0; }
Quentin Colombet41bea872016-03-07 22:47:23 +0000731
Tim Northover98a56eb2016-07-22 22:13:36 +0000732void MachineInstr::setType(LLT Ty, unsigned Idx) {}
733
734LLT MachineInstr::getType(unsigned Idx) const { return LLT{}; }
Quentin Colombet41bea872016-03-07 22:47:23 +0000735
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000736void MachineInstr::removeTypes() {}
737
Quentin Colombet41bea872016-03-07 22:47:23 +0000738#else
Tim Northover98a56eb2016-07-22 22:13:36 +0000739unsigned MachineInstr::getNumTypes() const { return Tys.size(); }
740
741void MachineInstr::setType(LLT Ty, unsigned Idx) {
Tim Northover62ae5682016-07-20 19:09:30 +0000742 assert((!Ty.isValid() || isPreISelGenericOpcode(getOpcode())) &&
Quentin Colombet41bea872016-03-07 22:47:23 +0000743 "Non generic instructions are not supposed to be typed");
Tim Northover98a56eb2016-07-22 22:13:36 +0000744 if (Tys.size() < Idx + 1)
745 Tys.resize(Idx+1);
746 Tys[Idx] = Ty;
Quentin Colombet41bea872016-03-07 22:47:23 +0000747}
748
Tim Northover98a56eb2016-07-22 22:13:36 +0000749LLT MachineInstr::getType(unsigned Idx) const { return Tys[Idx]; }
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000750
751void MachineInstr::removeTypes() {
752 Tys.clear();
753}
Quentin Colombet41bea872016-03-07 22:47:23 +0000754#endif // LLVM_BUILD_GLOBAL_ISEL
755
Chris Lattner961e7422008-01-01 01:12:31 +0000756/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
757/// this instruction from their respective use lists. This requires that the
758/// operands already be on their use lists.
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000759void MachineInstr::RemoveRegOperandsFromUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000760 for (MachineOperand &MO : operands())
761 if (MO.isReg())
762 MRI.removeRegOperandFromUseList(&MO);
Chris Lattner961e7422008-01-01 01:12:31 +0000763}
764
765/// AddRegOperandsToUseLists - Add all of the register operands in
766/// this instruction from their respective use lists. This requires that the
767/// operands not be on their use lists yet.
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000768void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000769 for (MachineOperand &MO : operands())
770 if (MO.isReg())
771 MRI.addRegOperandToUseList(&MO);
Chris Lattner961e7422008-01-01 01:12:31 +0000772}
773
Jakob Stoklund Olesen2455b5852012-12-20 22:54:05 +0000774void MachineInstr::addOperand(const MachineOperand &Op) {
775 MachineBasicBlock *MBB = getParent();
776 assert(MBB && "Use MachineInstrBuilder to add operands to dangling instrs");
777 MachineFunction *MF = MBB->getParent();
778 assert(MF && "Use MachineInstrBuilder to add operands to dangling instrs");
779 addOperand(*MF, Op);
780}
781
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000782/// Move NumOps MachineOperands from Src to Dst, with support for overlapping
783/// ranges. If MRI is non-null also update use-def chains.
784static void moveOperands(MachineOperand *Dst, MachineOperand *Src,
785 unsigned NumOps, MachineRegisterInfo *MRI) {
786 if (MRI)
787 return MRI->moveOperands(Dst, Src, NumOps);
788
JF Bastiena874d1a2016-03-26 18:20:02 +0000789 // MachineOperand is a trivially copyable type so we can just use memmove.
Benjamin Kramer5c0e64f2015-02-21 16:22:48 +0000790 std::memmove(Dst, Src, NumOps * sizeof(MachineOperand));
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000791}
792
Chris Lattner961e7422008-01-01 01:12:31 +0000793/// addOperand - Add the specified operand to the instruction. If it is an
794/// implicit operand, it is added to the end of the operand list. If it is
795/// an explicit operand it is added at the end of the explicit operand list
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000796/// (before the first implicit operand).
Jakob Stoklund Olesen2455b5852012-12-20 22:54:05 +0000797void MachineInstr::addOperand(MachineFunction &MF, const MachineOperand &Op) {
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000798 assert(MCID && "Cannot add operands before providing an instr descriptor");
Dan Gohman9356d8f2008-12-09 22:45:08 +0000799
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000800 // Check if we're adding one of our existing operands.
801 if (&Op >= Operands && &Op < Operands + NumOperands) {
802 // This is unusual: MI->addOperand(MI->getOperand(i)).
803 // If adding Op requires reallocating or moving existing operands around,
804 // the Op reference could go stale. Support it by copying Op.
805 MachineOperand CopyOp(Op);
806 return addOperand(MF, CopyOp);
807 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000808
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000809 // Find the insert location for the new operand. Implicit registers go at
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000810 // the end, everything else goes before the implicit regs.
811 //
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000812 // FIXME: Allow mixed explicit and implicit operands on inline asm.
813 // InstrEmitter::EmitSpecialNode() is marking inline asm clobbers as
814 // implicit-defs, but they must not be moved around. See the FIXME in
815 // InstrEmitter.cpp.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000816 unsigned OpNo = getNumOperands();
817 bool isImpReg = Op.isReg() && Op.isImplicit();
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000818 if (!isImpReg && !isInlineAsm()) {
819 while (OpNo && Operands[OpNo-1].isReg() && Operands[OpNo-1].isImplicit()) {
820 --OpNo;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000821 assert(!Operands[OpNo].isTied() && "Cannot move tied operands");
Chris Lattner961e7422008-01-01 01:12:31 +0000822 }
823 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000824
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000825#ifndef NDEBUG
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000826 bool isMetaDataOp = Op.getType() == MachineOperand::MO_Metadata;
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000827 // OpNo now points as the desired insertion point. Unless this is a variadic
828 // instruction, only implicit regs are allowed beyond MCID->getNumOperands().
Jakob Stoklund Olesenc300ef02012-07-04 23:53:23 +0000829 // RegMask operands go between the explicit and implicit operands.
830 assert((isImpReg || Op.isRegMask() || MCID->isVariadic() ||
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000831 OpNo < MCID->getNumOperands() || isMetaDataOp) &&
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000832 "Trying to add an operand to a machine instr that is already done!");
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000833#endif
Chris Lattner961e7422008-01-01 01:12:31 +0000834
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000835 MachineRegisterInfo *MRI = getRegInfo();
Chris Lattner961e7422008-01-01 01:12:31 +0000836
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000837 // Determine if the Operands array needs to be reallocated.
838 // Save the old capacity and operand array.
839 OperandCapacity OldCap = CapOperands;
840 MachineOperand *OldOperands = Operands;
841 if (!OldOperands || OldCap.getSize() == getNumOperands()) {
842 CapOperands = OldOperands ? OldCap.getNext() : OldCap.get(1);
843 Operands = MF.allocateOperandArray(CapOperands);
844 // Move the operands before the insertion point.
845 if (OpNo)
846 moveOperands(Operands, OldOperands, OpNo, MRI);
847 }
Chris Lattner961e7422008-01-01 01:12:31 +0000848
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000849 // Move the operands following the insertion point.
850 if (OpNo != NumOperands)
851 moveOperands(Operands + OpNo + 1, OldOperands + OpNo, NumOperands - OpNo,
852 MRI);
853 ++NumOperands;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000854
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000855 // Deallocate the old operand array.
856 if (OldOperands != Operands && OldOperands)
857 MF.deallocateOperandArray(OldCap, OldOperands);
858
859 // Copy Op into place. It still needs to be inserted into the MRI use lists.
860 MachineOperand *NewMO = new (Operands + OpNo) MachineOperand(Op);
861 NewMO->ParentMI = this;
862
863 // When adding a register operand, tell MRI about it.
864 if (NewMO->isReg()) {
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000865 // Ensure isOnRegUseList() returns false, regardless of Op's status.
Craig Topperc0196b12014-04-14 00:51:57 +0000866 NewMO->Contents.Reg.Prev = nullptr;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000867 // Ignore existing ties. This is not a property that can be copied.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000868 NewMO->TiedTo = 0;
869 // Add the new operand to MRI, but only for instructions in an MBB.
870 if (MRI)
871 MRI->addRegOperandToUseList(NewMO);
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000872 // The MCID operand information isn't accurate until we start adding
873 // explicit operands. The implicit operands are added first, then the
874 // explicits are inserted before them.
875 if (!isImpReg) {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000876 // Tie uses to defs as indicated in MCInstrDesc.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000877 if (NewMO->isUse()) {
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000878 int DefIdx = MCID->getOperandConstraint(OpNo, MCOI::TIED_TO);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +0000879 if (DefIdx != -1)
880 tieOperands(DefIdx, OpNo);
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000881 }
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000882 // If the register operand is flagged as early, mark the operand as such.
883 if (MCID->getOperandConstraint(OpNo, MCOI::EARLY_CLOBBER) != -1)
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000884 NewMO->setIsEarlyClobber(true);
Chris Lattner961e7422008-01-01 01:12:31 +0000885 }
Chris Lattner961e7422008-01-01 01:12:31 +0000886 }
887}
888
889/// RemoveOperand - Erase an operand from an instruction, leaving it with one
890/// fewer operand than it started with.
891///
892void MachineInstr::RemoveOperand(unsigned OpNo) {
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000893 assert(OpNo < getNumOperands() && "Invalid operand number");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000894 untieRegOperand(OpNo);
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000895
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000896#ifndef NDEBUG
897 // Moving tied operands would break the ties.
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000898 for (unsigned i = OpNo + 1, e = getNumOperands(); i != e; ++i)
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000899 if (Operands[i].isReg())
900 assert(!Operands[i].isTied() && "Cannot move tied operands");
901#endif
902
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000903 MachineRegisterInfo *MRI = getRegInfo();
904 if (MRI && Operands[OpNo].isReg())
905 MRI->removeRegOperandFromUseList(Operands + OpNo);
Chris Lattner961e7422008-01-01 01:12:31 +0000906
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000907 // Don't call the MachineOperand destructor. A lot of this code depends on
908 // MachineOperand having a trivial destructor anyway, and adding a call here
909 // wouldn't make it 'destructor-correct'.
910
911 if (unsigned N = NumOperands - 1 - OpNo)
912 moveOperands(Operands + OpNo, Operands + OpNo + 1, N, MRI);
913 --NumOperands;
Chris Lattner961e7422008-01-01 01:12:31 +0000914}
915
Dan Gohman48b185d2009-09-25 20:36:54 +0000916/// addMemOperand - Add a MachineMemOperand to the machine instruction.
917/// This function should be used only occasionally. The setMemRefs function
918/// is the primary method for setting up a MachineInstr's MemRefs list.
Dan Gohman3b460302008-07-07 23:14:23 +0000919void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohman48b185d2009-09-25 20:36:54 +0000920 MachineMemOperand *MO) {
921 mmo_iterator OldMemRefs = MemRefs;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000922 unsigned OldNumMemRefs = NumMemRefs;
Dan Gohman3b460302008-07-07 23:14:23 +0000923
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000924 unsigned NewNum = NumMemRefs + 1;
Dan Gohman48b185d2009-09-25 20:36:54 +0000925 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
Dan Gohman3b460302008-07-07 23:14:23 +0000926
Benjamin Kramerd03878b2012-03-16 16:39:27 +0000927 std::copy(OldMemRefs, OldMemRefs + OldNumMemRefs, NewMemRefs);
Dan Gohman48b185d2009-09-25 20:36:54 +0000928 NewMemRefs[NewNum - 1] = MO;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000929 setMemRefs(NewMemRefs, NewMemRefs + NewNum);
Dan Gohman48b185d2009-09-25 20:36:54 +0000930}
Chris Lattner961e7422008-01-01 01:12:31 +0000931
Philip Reames5eb90a72016-01-06 19:33:12 +0000932/// Check to see if the MMOs pointed to by the two MemRefs arrays are
Junmo Park820e3922016-02-26 02:07:36 +0000933/// identical.
Philip Reames5eb90a72016-01-06 19:33:12 +0000934static bool hasIdenticalMMOs(const MachineInstr &MI1, const MachineInstr &MI2) {
935 auto I1 = MI1.memoperands_begin(), E1 = MI1.memoperands_end();
936 auto I2 = MI2.memoperands_begin(), E2 = MI2.memoperands_end();
937 if ((E1 - I1) != (E2 - I2))
938 return false;
939 for (; I1 != E1; ++I1, ++I2) {
940 if (**I1 != **I2)
941 return false;
942 }
943 return true;
944}
945
Philip Reamesc86ed002016-01-06 04:39:03 +0000946std::pair<MachineInstr::mmo_iterator, unsigned>
947MachineInstr::mergeMemRefsWith(const MachineInstr& Other) {
Philip Reames5eb90a72016-01-06 19:33:12 +0000948
949 // If either of the incoming memrefs are empty, we must be conservative and
950 // treat this as if we've exhausted our space for memrefs and dropped them.
951 if (memoperands_empty() || Other.memoperands_empty())
952 return std::make_pair(nullptr, 0);
953
954 // If both instructions have identical memrefs, we don't need to merge them.
955 // Since many instructions have a single memref, and we tend to merge things
956 // like pairs of loads from the same location, this catches a large number of
957 // cases in practice.
958 if (hasIdenticalMMOs(*this, Other))
959 return std::make_pair(MemRefs, NumMemRefs);
Junmo Park820e3922016-02-26 02:07:36 +0000960
Philip Reamesc86ed002016-01-06 04:39:03 +0000961 // TODO: consider uniquing elements within the operand lists to reduce
962 // space usage and fall back to conservative information less often.
Philip Reames5eb90a72016-01-06 19:33:12 +0000963 size_t CombinedNumMemRefs = NumMemRefs + Other.NumMemRefs;
964
965 // If we don't have enough room to store this many memrefs, be conservative
966 // and drop them. Otherwise, we'd fail asserts when trying to add them to
967 // the new instruction.
968 if (CombinedNumMemRefs != uint8_t(CombinedNumMemRefs))
969 return std::make_pair(nullptr, 0);
Philip Reamesc86ed002016-01-06 04:39:03 +0000970
971 MachineFunction *MF = getParent()->getParent();
972 mmo_iterator MemBegin = MF->allocateMemRefsArray(CombinedNumMemRefs);
973 mmo_iterator MemEnd = std::copy(memoperands_begin(), memoperands_end(),
974 MemBegin);
975 MemEnd = std::copy(Other.memoperands_begin(), Other.memoperands_end(),
976 MemEnd);
Philip Reames2d2fc4a2016-01-06 05:53:09 +0000977 assert(MemEnd - MemBegin == (ptrdiff_t)CombinedNumMemRefs &&
978 "missing memrefs");
Junmo Park820e3922016-02-26 02:07:36 +0000979
Philip Reamesc86ed002016-01-06 04:39:03 +0000980 return std::make_pair(MemBegin, CombinedNumMemRefs);
981}
982
Benjamin Kramer97f889f2012-03-17 17:03:45 +0000983bool MachineInstr::hasPropertyInBundle(unsigned Mask, QueryType Type) const {
Jakob Stoklund Olesenf0615c72013-01-10 18:42:44 +0000984 assert(!isBundledWithPred() && "Must be called on bundle header");
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000985 for (MachineBasicBlock::const_instr_iterator MII = getIterator();; ++MII) {
Benjamin Kramer97f889f2012-03-17 17:03:45 +0000986 if (MII->getDesc().getFlags() & Mask) {
Evan Chengcdf89fd2011-12-08 19:23:10 +0000987 if (Type == AnyInBundle)
Evan Cheng7f8e5632011-12-07 07:15:52 +0000988 return true;
989 } else {
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +0000990 if (Type == AllInBundle && !MII->isBundle())
Evan Cheng7f8e5632011-12-07 07:15:52 +0000991 return false;
992 }
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +0000993 // This was the last instruction in the bundle.
994 if (!MII->isBundledWithSucc())
995 return Type == AllInBundle;
Evan Cheng2a81dd42011-12-06 22:12:01 +0000996 }
Evan Cheng2a81dd42011-12-06 22:12:01 +0000997}
998
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000999bool MachineInstr::isIdenticalTo(const MachineInstr &Other,
Evan Chenge9c46c22010-03-03 01:44:33 +00001000 MICheckType Check) const {
Evan Cheng0f260e12010-03-03 21:54:14 +00001001 // If opcodes or number of operands are not the same then the two
1002 // instructions are obviously not identical.
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001003 if (Other.getOpcode() != getOpcode() ||
1004 Other.getNumOperands() != getNumOperands())
Evan Cheng0f260e12010-03-03 21:54:14 +00001005 return false;
1006
Evan Cheng7fae11b2011-12-14 02:11:42 +00001007 if (isBundle()) {
1008 // Both instructions are bundles, compare MIs inside the bundle.
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001009 MachineBasicBlock::const_instr_iterator I1 = getIterator();
1010 MachineBasicBlock::const_instr_iterator E1 = getParent()->instr_end();
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001011 MachineBasicBlock::const_instr_iterator I2 = Other.getIterator();
1012 MachineBasicBlock::const_instr_iterator E2 = Other.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00001013 while (++I1 != E1 && I1->isInsideBundle()) {
1014 ++I2;
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001015 if (I2 == E2 || !I2->isInsideBundle() || !I1->isIdenticalTo(*I2, Check))
Evan Cheng7fae11b2011-12-14 02:11:42 +00001016 return false;
1017 }
1018 }
1019
Evan Cheng0f260e12010-03-03 21:54:14 +00001020 // Check operands to make sure they match.
1021 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1022 const MachineOperand &MO = getOperand(i);
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001023 const MachineOperand &OMO = Other.getOperand(i);
Evan Chengcfdf3392011-05-12 00:56:58 +00001024 if (!MO.isReg()) {
1025 if (!MO.isIdenticalTo(OMO))
1026 return false;
1027 continue;
1028 }
1029
Evan Cheng0f260e12010-03-03 21:54:14 +00001030 // Clients may or may not want to ignore defs when testing for equality.
1031 // For example, machine CSE pass only cares about finding common
1032 // subexpressions, so it's safe to ignore virtual register defs.
Evan Chengcfdf3392011-05-12 00:56:58 +00001033 if (MO.isDef()) {
Evan Cheng0f260e12010-03-03 21:54:14 +00001034 if (Check == IgnoreDefs)
1035 continue;
Evan Chengcfdf3392011-05-12 00:56:58 +00001036 else if (Check == IgnoreVRegDefs) {
1037 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) ||
1038 TargetRegisterInfo::isPhysicalRegister(OMO.getReg()))
1039 if (MO.getReg() != OMO.getReg())
1040 return false;
1041 } else {
1042 if (!MO.isIdenticalTo(OMO))
Evan Cheng0f260e12010-03-03 21:54:14 +00001043 return false;
Evan Chengcfdf3392011-05-12 00:56:58 +00001044 if (Check == CheckKillDead && MO.isDead() != OMO.isDead())
1045 return false;
1046 }
1047 } else {
1048 if (!MO.isIdenticalTo(OMO))
1049 return false;
1050 if (Check == CheckKillDead && MO.isKill() != OMO.isKill())
1051 return false;
1052 }
Evan Cheng0f260e12010-03-03 21:54:14 +00001053 }
Devang Patelbf8cc602011-07-07 17:45:33 +00001054 // If DebugLoc does not match then two dbg.values are not identical.
1055 if (isDebugValue())
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001056 if (getDebugLoc() && Other.getDebugLoc() &&
1057 getDebugLoc() != Other.getDebugLoc())
Devang Patelbf8cc602011-07-07 17:45:33 +00001058 return false;
Evan Cheng0f260e12010-03-03 21:54:14 +00001059 return true;
Evan Chenge9c46c22010-03-03 01:44:33 +00001060}
1061
Chris Lattnerbec79b42006-04-17 21:35:41 +00001062MachineInstr *MachineInstr::removeFromParent() {
1063 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001064 return getParent()->remove(this);
Chris Lattnerbec79b42006-04-17 21:35:41 +00001065}
1066
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001067MachineInstr *MachineInstr::removeFromBundle() {
1068 assert(getParent() && "Not embedded in a basic block!");
1069 return getParent()->remove_instr(this);
1070}
Chris Lattnerbec79b42006-04-17 21:35:41 +00001071
Dan Gohman3b460302008-07-07 23:14:23 +00001072void MachineInstr::eraseFromParent() {
1073 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001074 getParent()->erase(this);
Dan Gohman3b460302008-07-07 23:14:23 +00001075}
1076
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +00001077void MachineInstr::eraseFromParentAndMarkDBGValuesForRemoval() {
1078 assert(getParent() && "Not embedded in a basic block!");
1079 MachineBasicBlock *MBB = getParent();
1080 MachineFunction *MF = MBB->getParent();
1081 assert(MF && "Not embedded in a function!");
1082
1083 MachineInstr *MI = (MachineInstr *)this;
1084 MachineRegisterInfo &MRI = MF->getRegInfo();
1085
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001086 for (const MachineOperand &MO : MI->operands()) {
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +00001087 if (!MO.isReg() || !MO.isDef())
1088 continue;
1089 unsigned Reg = MO.getReg();
1090 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1091 continue;
1092 MRI.markUsesInDebugValueAsUndef(Reg);
1093 }
1094 MI->eraseFromParent();
1095}
1096
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001097void MachineInstr::eraseFromBundle() {
1098 assert(getParent() && "Not embedded in a basic block!");
1099 getParent()->erase_instr(this);
1100}
Dan Gohman3b460302008-07-07 23:14:23 +00001101
Evan Cheng4d728b02007-05-15 01:26:09 +00001102/// getNumExplicitOperands - Returns the number of non-implicit operands.
1103///
1104unsigned MachineInstr::getNumExplicitOperands() const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001105 unsigned NumOperands = MCID->getNumOperands();
1106 if (!MCID->isVariadic())
Evan Cheng4d728b02007-05-15 01:26:09 +00001107 return NumOperands;
1108
Dan Gohman37608532009-04-15 17:59:11 +00001109 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
1110 const MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001111 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng4d728b02007-05-15 01:26:09 +00001112 NumOperands++;
1113 }
1114 return NumOperands;
1115}
1116
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001117void MachineInstr::bundleWithPred() {
1118 assert(!isBundledWithPred() && "MI is already bundled with its predecessor");
1119 setFlag(BundledPred);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001120 MachineBasicBlock::instr_iterator Pred = getIterator();
1121 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001122 assert(!Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001123 Pred->setFlag(BundledSucc);
1124}
1125
1126void MachineInstr::bundleWithSucc() {
1127 assert(!isBundledWithSucc() && "MI is already bundled with its successor");
1128 setFlag(BundledSucc);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001129 MachineBasicBlock::instr_iterator Succ = getIterator();
1130 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001131 assert(!Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001132 Succ->setFlag(BundledPred);
1133}
1134
1135void MachineInstr::unbundleFromPred() {
1136 assert(isBundledWithPred() && "MI isn't bundled with its predecessor");
1137 clearFlag(BundledPred);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001138 MachineBasicBlock::instr_iterator Pred = getIterator();
1139 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001140 assert(Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001141 Pred->clearFlag(BundledSucc);
1142}
1143
1144void MachineInstr::unbundleFromSucc() {
1145 assert(isBundledWithSucc() && "MI isn't bundled with its successor");
1146 clearFlag(BundledSucc);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001147 MachineBasicBlock::instr_iterator Succ = getIterator();
1148 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001149 assert(Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001150 Succ->clearFlag(BundledPred);
1151}
1152
Evan Cheng6eb516d2011-01-07 23:50:32 +00001153bool MachineInstr::isStackAligningInlineAsm() const {
1154 if (isInlineAsm()) {
1155 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1156 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1157 return true;
1158 }
1159 return false;
1160}
Chris Lattner33f5af02006-10-20 22:39:59 +00001161
Chad Rosier994f4042012-09-05 21:00:58 +00001162InlineAsm::AsmDialect MachineInstr::getInlineAsmDialect() const {
1163 assert(isInlineAsm() && "getInlineAsmDialect() only works for inline asms!");
1164 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
Chad Rosiere53314f2012-09-05 22:40:13 +00001165 return InlineAsm::AsmDialect((ExtraInfo & InlineAsm::Extra_AsmDialect) != 0);
Chad Rosier994f4042012-09-05 21:00:58 +00001166}
1167
Jakob Stoklund Olesen1e737162011-10-12 23:37:33 +00001168int MachineInstr::findInlineAsmFlagIdx(unsigned OpIdx,
1169 unsigned *GroupNo) const {
1170 assert(isInlineAsm() && "Expected an inline asm instruction");
1171 assert(OpIdx < getNumOperands() && "OpIdx out of range");
1172
1173 // Ignore queries about the initial operands.
1174 if (OpIdx < InlineAsm::MIOp_FirstOperand)
1175 return -1;
1176
1177 unsigned Group = 0;
1178 unsigned NumOps;
1179 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1180 i += NumOps) {
1181 const MachineOperand &FlagMO = getOperand(i);
1182 // If we reach the implicit register operands, stop looking.
1183 if (!FlagMO.isImm())
1184 return -1;
1185 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1186 if (i + NumOps > OpIdx) {
1187 if (GroupNo)
1188 *GroupNo = Group;
1189 return i;
1190 }
1191 ++Group;
1192 }
1193 return -1;
1194}
1195
Reid Kleckner28865802016-04-14 18:29:59 +00001196const DILocalVariable *MachineInstr::getDebugVariable() const {
1197 assert(isDebugValue() && "not a DBG_VALUE");
1198 return cast<DILocalVariable>(getOperand(2).getMetadata());
1199}
1200
1201const DIExpression *MachineInstr::getDebugExpression() const {
1202 assert(isDebugValue() && "not a DBG_VALUE");
1203 return cast<DIExpression>(getOperand(3).getMetadata());
1204}
1205
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001206const TargetRegisterClass*
1207MachineInstr::getRegClassConstraint(unsigned OpIdx,
1208 const TargetInstrInfo *TII,
1209 const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001210 assert(getParent() && "Can't have an MBB reference here!");
1211 assert(getParent()->getParent() && "Can't have an MF reference here!");
1212 const MachineFunction &MF = *getParent()->getParent();
1213
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001214 // Most opcodes have fixed constraints in their MCInstrDesc.
1215 if (!isInlineAsm())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001216 return TII->getRegClass(getDesc(), OpIdx, TRI, MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001217
1218 if (!getOperand(OpIdx).isReg())
Craig Topperc0196b12014-04-14 00:51:57 +00001219 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001220
1221 // For tied uses on inline asm, get the constraint from the def.
1222 unsigned DefIdx;
1223 if (getOperand(OpIdx).isUse() && isRegTiedToDefOperand(OpIdx, &DefIdx))
1224 OpIdx = DefIdx;
1225
1226 // Inline asm stores register class constraints in the flag word.
1227 int FlagIdx = findInlineAsmFlagIdx(OpIdx);
1228 if (FlagIdx < 0)
Craig Topperc0196b12014-04-14 00:51:57 +00001229 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001230
1231 unsigned Flag = getOperand(FlagIdx).getImm();
1232 unsigned RCID;
Simon Dardisd32a2d32016-07-18 13:17:31 +00001233 if ((InlineAsm::getKind(Flag) == InlineAsm::Kind_RegUse ||
1234 InlineAsm::getKind(Flag) == InlineAsm::Kind_RegDef ||
1235 InlineAsm::getKind(Flag) == InlineAsm::Kind_RegDefEarlyClobber) &&
1236 InlineAsm::hasRegClassConstraint(Flag, RCID))
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001237 return TRI->getRegClass(RCID);
1238
1239 // Assume that all registers in a memory operand are pointers.
1240 if (InlineAsm::getKind(Flag) == InlineAsm::Kind_Mem)
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001241 return TRI->getPointerRegClass(MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001242
Craig Topperc0196b12014-04-14 00:51:57 +00001243 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001244}
1245
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001246const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVReg(
1247 unsigned Reg, const TargetRegisterClass *CurRC, const TargetInstrInfo *TII,
1248 const TargetRegisterInfo *TRI, bool ExploreBundle) const {
1249 // Check every operands inside the bundle if we have
1250 // been asked to.
1251 if (ExploreBundle)
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001252 for (ConstMIBundleOperands OpndIt(*this); OpndIt.isValid() && CurRC;
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001253 ++OpndIt)
1254 CurRC = OpndIt->getParent()->getRegClassConstraintEffectForVRegImpl(
1255 OpndIt.getOperandNo(), Reg, CurRC, TII, TRI);
1256 else
1257 // Otherwise, just check the current operands.
Matthias Braune41e1462015-05-29 02:56:46 +00001258 for (unsigned i = 0, e = NumOperands; i < e && CurRC; ++i)
1259 CurRC = getRegClassConstraintEffectForVRegImpl(i, Reg, CurRC, TII, TRI);
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001260 return CurRC;
1261}
1262
1263const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVRegImpl(
1264 unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC,
1265 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
1266 assert(CurRC && "Invalid initial register class");
1267 // Check if Reg is constrained by some of its use/def from MI.
1268 const MachineOperand &MO = getOperand(OpIdx);
1269 if (!MO.isReg() || MO.getReg() != Reg)
1270 return CurRC;
1271 // If yes, accumulate the constraints through the operand.
1272 return getRegClassConstraintEffect(OpIdx, CurRC, TII, TRI);
1273}
1274
1275const TargetRegisterClass *MachineInstr::getRegClassConstraintEffect(
1276 unsigned OpIdx, const TargetRegisterClass *CurRC,
1277 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
1278 const TargetRegisterClass *OpRC = getRegClassConstraint(OpIdx, TII, TRI);
1279 const MachineOperand &MO = getOperand(OpIdx);
1280 assert(MO.isReg() &&
1281 "Cannot get register constraints for non-register operand");
1282 assert(CurRC && "Invalid initial register class");
1283 if (unsigned SubIdx = MO.getSubReg()) {
1284 if (OpRC)
1285 CurRC = TRI->getMatchingSuperRegClass(CurRC, OpRC, SubIdx);
1286 else
1287 CurRC = TRI->getSubClassWithSubReg(CurRC, SubIdx);
1288 } else if (OpRC)
1289 CurRC = TRI->getCommonSubClass(CurRC, OpRC);
1290 return CurRC;
1291}
1292
Jakob Stoklund Olesen68d752b2013-01-09 18:28:16 +00001293/// Return the number of instructions inside the MI bundle, not counting the
1294/// header instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +00001295unsigned MachineInstr::getBundleSize() const {
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001296 MachineBasicBlock::const_instr_iterator I = getIterator();
Evan Cheng7fae11b2011-12-14 02:11:42 +00001297 unsigned Size = 0;
Richard Trieu7a083812016-02-18 22:09:30 +00001298 while (I->isBundledWithSucc()) {
1299 ++Size;
1300 ++I;
1301 }
Evan Cheng7fae11b2011-12-14 02:11:42 +00001302 return Size;
1303}
1304
Nicolai Haehnleb0c97482016-04-22 04:04:08 +00001305/// Returns true if the MachineInstr has an implicit-use operand of exactly
1306/// the given register (not considering sub/super-registers).
1307bool MachineInstr::hasRegisterImplicitUseOperand(unsigned Reg) const {
1308 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1309 const MachineOperand &MO = getOperand(i);
1310 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == Reg)
1311 return true;
1312 }
1313 return false;
1314}
1315
Evan Cheng910c8082007-04-26 19:00:32 +00001316/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbach9632c142009-09-17 17:57:26 +00001317/// the specific register or -1 if it is not found. It further tightens
Evan Cheng9965aeb2007-02-23 01:04:26 +00001318/// the search criteria to a use that kills the register if isKill is true.
Evan Cheng63254462008-03-05 00:59:57 +00001319int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
1320 const TargetRegisterInfo *TRI) const {
Evan Cheng75c21942006-12-06 08:27:42 +00001321 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng5983bdb2007-05-29 18:35:22 +00001322 const MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001323 if (!MO.isReg() || !MO.isUse())
Evan Cheng63254462008-03-05 00:59:57 +00001324 continue;
1325 unsigned MOReg = MO.getReg();
1326 if (!MOReg)
1327 continue;
1328 if (MOReg == Reg ||
1329 (TRI &&
1330 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
1331 TargetRegisterInfo::isPhysicalRegister(Reg) &&
1332 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng9965aeb2007-02-23 01:04:26 +00001333 if (!isKill || MO.isKill())
Evan Chengec3ac312007-03-26 22:37:45 +00001334 return i;
Evan Cheng75c21942006-12-06 08:27:42 +00001335 }
Evan Chengec3ac312007-03-26 22:37:45 +00001336 return -1;
Evan Cheng75c21942006-12-06 08:27:42 +00001337}
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001338
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001339/// readsWritesVirtualRegister - Return a pair of bools (reads, writes)
1340/// indicating if this instruction reads or writes Reg. This also considers
1341/// partial defines.
1342std::pair<bool,bool>
1343MachineInstr::readsWritesVirtualRegister(unsigned Reg,
1344 SmallVectorImpl<unsigned> *Ops) const {
1345 bool PartDef = false; // Partial redefine.
1346 bool FullDef = false; // Full define.
1347 bool Use = false;
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001348
1349 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1350 const MachineOperand &MO = getOperand(i);
1351 if (!MO.isReg() || MO.getReg() != Reg)
1352 continue;
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001353 if (Ops)
1354 Ops->push_back(i);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001355 if (MO.isUse())
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001356 Use |= !MO.isUndef();
Jakob Stoklund Olesen9eb77bf2011-08-19 00:30:17 +00001357 else if (MO.getSubReg() && !MO.isUndef())
1358 // A partial <def,undef> doesn't count as reading the register.
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001359 PartDef = true;
1360 else
1361 FullDef = true;
1362 }
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001363 // A partial redefine uses Reg unless there is also a full define.
1364 return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001365}
1366
Evan Cheng63254462008-03-05 00:59:57 +00001367/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman72a0bc12008-05-06 00:20:10 +00001368/// the specified register or -1 if it is not found. If isDead is true, defs
1369/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
1370/// also checks if there is a def of a super-register.
Evan Cheng38584512010-05-21 20:53:24 +00001371int
1372MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap,
1373 const TargetRegisterInfo *TRI) const {
1374 bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg);
Evan Chengf7ed82d2007-02-19 21:49:54 +00001375 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng63254462008-03-05 00:59:57 +00001376 const MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesene7d3f442012-02-14 23:49:37 +00001377 // Accept regmask operands when Overlap is set.
1378 // Ignore them when looking for a specific def operand (Overlap == false).
1379 if (isPhys && Overlap && MO.isRegMask() && MO.clobbersPhysReg(Reg))
1380 return i;
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001381 if (!MO.isReg() || !MO.isDef())
Evan Cheng63254462008-03-05 00:59:57 +00001382 continue;
1383 unsigned MOReg = MO.getReg();
Evan Cheng38584512010-05-21 20:53:24 +00001384 bool Found = (MOReg == Reg);
1385 if (!Found && TRI && isPhys &&
1386 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
1387 if (Overlap)
1388 Found = TRI->regsOverlap(MOReg, Reg);
1389 else
1390 Found = TRI->isSubRegister(MOReg, Reg);
1391 }
1392 if (Found && (!isDead || MO.isDead()))
1393 return i;
Evan Chengf7ed82d2007-02-19 21:49:54 +00001394 }
Evan Cheng63254462008-03-05 00:59:57 +00001395 return -1;
Evan Chengf7ed82d2007-02-19 21:49:54 +00001396}
Evan Cheng4d728b02007-05-15 01:26:09 +00001397
Evan Cheng5983bdb2007-05-29 18:35:22 +00001398/// findFirstPredOperandIdx() - Find the index of the first operand in the
1399/// operand list that is used to represent the predicate. It returns -1 if
1400/// none is found.
1401int MachineInstr::findFirstPredOperandIdx() const {
Jim Grosbached16ec42011-08-29 22:24:09 +00001402 // Don't call MCID.findFirstPredOperandIdx() because this variant
1403 // is sometimes called on an instruction that's not yet complete, and
1404 // so the number of operands is less than the MCID indicates. In
1405 // particular, the PTX target does this.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001406 const MCInstrDesc &MCID = getDesc();
1407 if (MCID.isPredicable()) {
Evan Cheng4d728b02007-05-15 01:26:09 +00001408 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Evan Cheng6cc775f2011-06-28 19:10:37 +00001409 if (MCID.OpInfo[i].isPredicate())
Evan Cheng5983bdb2007-05-29 18:35:22 +00001410 return i;
Evan Cheng4d728b02007-05-15 01:26:09 +00001411 }
1412
Evan Cheng5983bdb2007-05-29 18:35:22 +00001413 return -1;
Evan Cheng4d728b02007-05-15 01:26:09 +00001414}
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001415
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001416// MachineOperand::TiedTo is 4 bits wide.
1417const unsigned TiedMax = 15;
1418
1419/// tieOperands - Mark operands at DefIdx and UseIdx as tied to each other.
1420///
1421/// Use and def operands can be tied together, indicated by a non-zero TiedTo
1422/// field. TiedTo can have these values:
1423///
1424/// 0: Operand is not tied to anything.
1425/// 1 to TiedMax-1: Tied to getOperand(TiedTo-1).
1426/// TiedMax: Tied to an operand >= TiedMax-1.
1427///
1428/// The tied def must be one of the first TiedMax operands on a normal
1429/// instruction. INLINEASM instructions allow more tied defs.
1430///
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001431void MachineInstr::tieOperands(unsigned DefIdx, unsigned UseIdx) {
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001432 MachineOperand &DefMO = getOperand(DefIdx);
1433 MachineOperand &UseMO = getOperand(UseIdx);
1434 assert(DefMO.isDef() && "DefIdx must be a def operand");
1435 assert(UseMO.isUse() && "UseIdx must be a use operand");
1436 assert(!DefMO.isTied() && "Def is already tied to another use");
1437 assert(!UseMO.isTied() && "Use is already tied to another def");
1438
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001439 if (DefIdx < TiedMax)
1440 UseMO.TiedTo = DefIdx + 1;
1441 else {
1442 // Inline asm can use the group descriptors to find tied operands, but on
1443 // normal instruction, the tied def must be within the first TiedMax
1444 // operands.
1445 assert(isInlineAsm() && "DefIdx out of range");
1446 UseMO.TiedTo = TiedMax;
1447 }
1448
1449 // UseIdx can be out of range, we'll search for it in findTiedOperandIdx().
1450 DefMO.TiedTo = std::min(UseIdx + 1, TiedMax);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001451}
1452
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001453/// Given the index of a tied register operand, find the operand it is tied to.
1454/// Defs are tied to uses and vice versa. Returns the index of the tied operand
1455/// which must exist.
1456unsigned MachineInstr::findTiedOperandIdx(unsigned OpIdx) const {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001457 const MachineOperand &MO = getOperand(OpIdx);
1458 assert(MO.isTied() && "Operand isn't tied");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001459
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001460 // Normally TiedTo is in range.
1461 if (MO.TiedTo < TiedMax)
1462 return MO.TiedTo - 1;
1463
1464 // Uses on normal instructions can be out of range.
1465 if (!isInlineAsm()) {
1466 // Normal tied defs must be in the 0..TiedMax-1 range.
1467 if (MO.isUse())
1468 return TiedMax - 1;
1469 // MO is a def. Search for the tied use.
1470 for (unsigned i = TiedMax - 1, e = getNumOperands(); i != e; ++i) {
1471 const MachineOperand &UseMO = getOperand(i);
1472 if (UseMO.isReg() && UseMO.isUse() && UseMO.TiedTo == OpIdx + 1)
1473 return i;
1474 }
1475 llvm_unreachable("Can't find tied use");
1476 }
1477
1478 // Now deal with inline asm by parsing the operand group descriptor flags.
1479 // Find the beginning of each operand group.
1480 SmallVector<unsigned, 8> GroupIdx;
1481 unsigned OpIdxGroup = ~0u;
1482 unsigned NumOps;
1483 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1484 i += NumOps) {
1485 const MachineOperand &FlagMO = getOperand(i);
1486 assert(FlagMO.isImm() && "Invalid tied operand on inline asm");
1487 unsigned CurGroup = GroupIdx.size();
1488 GroupIdx.push_back(i);
1489 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1490 // OpIdx belongs to this operand group.
1491 if (OpIdx > i && OpIdx < i + NumOps)
1492 OpIdxGroup = CurGroup;
1493 unsigned TiedGroup;
1494 if (!InlineAsm::isUseOperandTiedToDef(FlagMO.getImm(), TiedGroup))
1495 continue;
1496 // Operands in this group are tied to operands in TiedGroup which must be
1497 // earlier. Find the number of operands between the two groups.
1498 unsigned Delta = i - GroupIdx[TiedGroup];
1499
1500 // OpIdx is a use tied to TiedGroup.
1501 if (OpIdxGroup == CurGroup)
1502 return OpIdx - Delta;
1503
1504 // OpIdx is a def tied to this use group.
1505 if (OpIdxGroup == TiedGroup)
1506 return OpIdx + Delta;
1507 }
1508 llvm_unreachable("Invalid tied operand on inline asm");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001509}
1510
Dan Gohmanc90f51c2010-05-13 20:34:42 +00001511/// clearKillInfo - Clears kill flags on all operands.
1512///
1513void MachineInstr::clearKillInfo() {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001514 for (MachineOperand &MO : operands()) {
Dan Gohmanc90f51c2010-05-13 20:34:42 +00001515 if (MO.isReg() && MO.isUse())
1516 MO.setIsKill(false);
1517 }
1518}
1519
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001520void MachineInstr::substituteRegister(unsigned FromReg,
1521 unsigned ToReg,
1522 unsigned SubIdx,
1523 const TargetRegisterInfo &RegInfo) {
1524 if (TargetRegisterInfo::isPhysicalRegister(ToReg)) {
1525 if (SubIdx)
1526 ToReg = RegInfo.getSubReg(ToReg, SubIdx);
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001527 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001528 if (!MO.isReg() || MO.getReg() != FromReg)
1529 continue;
1530 MO.substPhysReg(ToReg, RegInfo);
1531 }
1532 } else {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001533 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001534 if (!MO.isReg() || MO.getReg() != FromReg)
1535 continue;
1536 MO.substVirtReg(ToReg, SubIdx, RegInfo);
1537 }
1538 }
1539}
1540
Evan Cheng7d98a482008-07-03 09:09:37 +00001541/// isSafeToMove - Return true if it is safe to move this instruction. If
1542/// SawStore is set to true, it means that there is a store (or call) between
1543/// the instruction's location and its intended destination.
Matthias Braun07066cc2015-05-19 21:22:20 +00001544bool MachineInstr::isSafeToMove(AliasAnalysis *AA, bool &SawStore) const {
Evan Cheng399e1102008-03-13 00:44:09 +00001545 // Ignore stuff that we obviously can't move.
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001546 //
1547 // Treat volatile loads as stores. This is not strictly necessary for
Jakob Stoklund Olesend92e2bc2012-09-04 18:44:43 +00001548 // volatiles, but it is required for atomic loads. It is not allowed to move
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001549 // a load across an atomic load with Ordering > Monotonic.
1550 if (mayStore() || isCall() ||
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001551 (mayLoad() && hasOrderedMemoryRef())) {
Evan Cheng399e1102008-03-13 00:44:09 +00001552 SawStore = true;
1553 return false;
1554 }
Evan Cheng0638c202011-01-07 21:08:26 +00001555
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001556 if (isPosition() || isDebugValue() || isTerminator() ||
1557 hasUnmodeledSideEffects())
Evan Cheng399e1102008-03-13 00:44:09 +00001558 return false;
1559
1560 // See if this instruction does a load. If so, we have to guarantee that the
1561 // loaded value doesn't change between the load and the its intended
1562 // destination. The check for isInvariantLoad gives the targe the chance to
1563 // classify the load as always returning a constant, e.g. a constant pool
1564 // load.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001565 if (mayLoad() && !isInvariantLoad(AA))
Evan Cheng399e1102008-03-13 00:44:09 +00001566 // Otherwise, this is a real load. If there is a store between the load and
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001567 // end of block, we can't move it.
1568 return !SawStore;
Dan Gohman7c59ed62008-09-24 00:06:15 +00001569
Evan Cheng399e1102008-03-13 00:44:09 +00001570 return true;
1571}
1572
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001573/// hasOrderedMemoryRef - Return true if this instruction may have an ordered
1574/// or volatile memory reference, or if the information describing the memory
1575/// reference is not available. Return false if it is known to have no ordered
1576/// memory references.
1577bool MachineInstr::hasOrderedMemoryRef() const {
Dan Gohman7c59ed62008-09-24 00:06:15 +00001578 // An instruction known never to access memory won't have a volatile access.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001579 if (!mayStore() &&
1580 !mayLoad() &&
1581 !isCall() &&
Evan Cheng6eb516d2011-01-07 23:50:32 +00001582 !hasUnmodeledSideEffects())
Dan Gohman7c59ed62008-09-24 00:06:15 +00001583 return false;
1584
1585 // Otherwise, if the instruction has no memory reference information,
1586 // conservatively assume it wasn't preserved.
1587 if (memoperands_empty())
1588 return true;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001589
Justin Lebardede81e2016-07-13 22:35:19 +00001590 // Check if any of our memory operands are ordered.
1591 return any_of(memoperands(), [](const MachineMemOperand *MMO) {
1592 return !MMO->isUnordered();
1593 });
Dan Gohman7c59ed62008-09-24 00:06:15 +00001594}
1595
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001596/// isInvariantLoad - Return true if this instruction is loading from a
1597/// location whose value is invariant across the function. For example,
Dan Gohman4a618822010-02-10 16:03:48 +00001598/// loading a value from the constant pool or from the argument area
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001599/// of a function if it does not change. This should only return true of
1600/// *all* loads the instruction does are invariant (if it does multiple loads).
1601bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const {
1602 // If the instruction doesn't load at all, it isn't an invariant load.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001603 if (!mayLoad())
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001604 return false;
1605
1606 // If the instruction has lost its memoperands, conservatively assume that
1607 // it may not be an invariant load.
1608 if (memoperands_empty())
1609 return false;
1610
Matthias Braun941a7052016-07-28 18:40:00 +00001611 const MachineFrameInfo &MFI = getParent()->getParent()->getFrameInfo();
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001612
Justin Lebardede81e2016-07-13 22:35:19 +00001613 for (MachineMemOperand *MMO : memoperands()) {
1614 if (MMO->isVolatile()) return false;
1615 if (MMO->isStore()) return false;
1616 if (MMO->isInvariant()) continue;
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001617
1618 // A load from a constant PseudoSourceValue is invariant.
Justin Lebardede81e2016-07-13 22:35:19 +00001619 if (const PseudoSourceValue *PSV = MMO->getPseudoValue())
Matthias Braun941a7052016-07-28 18:40:00 +00001620 if (PSV->isConstant(&MFI))
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001621 continue;
1622
Justin Lebardede81e2016-07-13 22:35:19 +00001623 if (const Value *V = MMO->getValue()) {
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001624 // If we have an AliasAnalysis, ask it whether the memory is constant.
Chandler Carruthac80dc72015-06-17 07:18:54 +00001625 if (AA &&
1626 AA->pointsToConstantMemory(
Justin Lebardede81e2016-07-13 22:35:19 +00001627 MemoryLocation(V, MMO->getSize(), MMO->getAAInfo())))
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001628 continue;
1629 }
1630
1631 // Otherwise assume conservatively.
1632 return false;
1633 }
1634
1635 // Everything checks out.
1636 return true;
1637}
1638
Evan Cheng71453822009-12-03 02:31:43 +00001639/// isConstantValuePHI - If the specified instruction is a PHI that always
1640/// merges together the same virtual register, return the register, otherwise
1641/// return 0.
1642unsigned MachineInstr::isConstantValuePHI() const {
Chris Lattnerb06015a2010-02-09 19:54:29 +00001643 if (!isPHI())
Evan Cheng71453822009-12-03 02:31:43 +00001644 return 0;
Evan Cheng5c668a22009-12-07 23:10:34 +00001645 assert(getNumOperands() >= 3 &&
1646 "It's illegal to have a PHI without source operands");
Evan Cheng71453822009-12-03 02:31:43 +00001647
1648 unsigned Reg = getOperand(1).getReg();
1649 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
1650 if (getOperand(i).getReg() != Reg)
1651 return 0;
1652 return Reg;
1653}
1654
Evan Cheng6eb516d2011-01-07 23:50:32 +00001655bool MachineInstr::hasUnmodeledSideEffects() const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00001656 if (hasProperty(MCID::UnmodeledSideEffects))
Evan Cheng6eb516d2011-01-07 23:50:32 +00001657 return true;
1658 if (isInlineAsm()) {
1659 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1660 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1661 return true;
1662 }
1663
1664 return false;
1665}
1666
Michael Kupersteinbc7f99a2015-08-12 10:14:58 +00001667bool MachineInstr::isLoadFoldBarrier() const {
1668 return mayStore() || isCall() || hasUnmodeledSideEffects();
1669}
1670
Evan Chengb083c472010-04-08 20:02:37 +00001671/// allDefsAreDead - Return true if all the defs of this instruction are dead.
1672///
1673bool MachineInstr::allDefsAreDead() const {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001674 for (const MachineOperand &MO : operands()) {
Evan Chengb083c472010-04-08 20:02:37 +00001675 if (!MO.isReg() || MO.isUse())
1676 continue;
1677 if (!MO.isDead())
1678 return false;
1679 }
1680 return true;
1681}
1682
Evan Cheng21eedfb2010-10-22 21:49:09 +00001683/// copyImplicitOps - Copy implicit register operands from specified
1684/// instruction to this instruction.
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001685void MachineInstr::copyImplicitOps(MachineFunction &MF,
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001686 const MachineInstr &MI) {
1687 for (unsigned i = MI.getDesc().getNumOperands(), e = MI.getNumOperands();
Evan Cheng21eedfb2010-10-22 21:49:09 +00001688 i != e; ++i) {
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001689 const MachineOperand &MO = MI.getOperand(i);
Lang Hames7c8189c2014-03-17 01:22:54 +00001690 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001691 addOperand(MF, MO);
Evan Cheng21eedfb2010-10-22 21:49:09 +00001692 }
1693}
1694
Yaron Kereneb2a2542016-01-29 20:50:44 +00001695LLVM_DUMP_METHOD void MachineInstr::dump() const {
Manman Ren19f49ac2012-09-11 22:23:19 +00001696#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
David Greene29388d62010-01-04 23:48:20 +00001697 dbgs() << " " << *this;
Manman Ren742534c2012-09-06 19:06:06 +00001698#endif
Mon P Wangdfcc1ff2008-10-10 01:43:55 +00001699}
1700
Eric Christopher1cdefae2015-02-27 00:11:34 +00001701void MachineInstr::print(raw_ostream &OS, bool SkipOpers) const {
Duncan P. N. Exon Smithc0374522015-06-26 23:18:44 +00001702 const Module *M = nullptr;
1703 if (const MachineBasicBlock *MBB = getParent())
1704 if (const MachineFunction *MF = MBB->getParent())
1705 M = MF->getFunction()->getParent();
1706
1707 ModuleSlotTracker MST(M);
1708 print(OS, MST, SkipOpers);
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001709}
1710
1711void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
1712 bool SkipOpers) const {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001713 // We can be a bit tidier if we know the MachineFunction.
Craig Topperc0196b12014-04-14 00:51:57 +00001714 const MachineFunction *MF = nullptr;
Eric Christopher1cdefae2015-02-27 00:11:34 +00001715 const TargetRegisterInfo *TRI = nullptr;
Craig Topperc0196b12014-04-14 00:51:57 +00001716 const MachineRegisterInfo *MRI = nullptr;
Eric Christopher1cdefae2015-02-27 00:11:34 +00001717 const TargetInstrInfo *TII = nullptr;
Tim Northover6b3bd612016-07-29 20:32:59 +00001718 const TargetIntrinsicInfo *IntrinsicInfo = nullptr;
1719
Dan Gohman2745d192009-11-09 19:38:45 +00001720 if (const MachineBasicBlock *MBB = getParent()) {
1721 MF = MBB->getParent();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001722 if (MF) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001723 MRI = &MF->getRegInfo();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001724 TRI = MF->getSubtarget().getRegisterInfo();
1725 TII = MF->getSubtarget().getInstrInfo();
Tim Northover6b3bd612016-07-29 20:32:59 +00001726 IntrinsicInfo = MF->getTarget().getIntrinsicInfo();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001727 }
Dan Gohman2745d192009-11-09 19:38:45 +00001728 }
Dan Gohman34341e62009-10-31 20:19:03 +00001729
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001730 // Save a list of virtual registers.
1731 SmallVector<unsigned, 8> VirtRegs;
1732
Dan Gohman34341e62009-10-31 20:19:03 +00001733 // Print explicitly defined operands on the left of an assignment syntax.
Dan Gohman2745d192009-11-09 19:38:45 +00001734 unsigned StartOp = 0, e = getNumOperands();
Dan Gohman34341e62009-10-31 20:19:03 +00001735 for (; StartOp < e && getOperand(StartOp).isReg() &&
1736 getOperand(StartOp).isDef() &&
1737 !getOperand(StartOp).isImplicit();
1738 ++StartOp) {
1739 if (StartOp != 0) OS << ", ";
Tim Northover6b3bd612016-07-29 20:32:59 +00001740 getOperand(StartOp).print(OS, MST, TRI, IntrinsicInfo);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001741 unsigned Reg = getOperand(StartOp).getReg();
Quentin Colombet36ce1b02016-02-10 23:43:48 +00001742 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001743 VirtRegs.push_back(Reg);
Quentin Colombet36ce1b02016-02-10 23:43:48 +00001744 unsigned Size;
Quentin Colombet03c41962016-04-07 23:18:11 +00001745 if (MRI && (Size = MRI->getSize(Reg)))
Quentin Colombet36ce1b02016-02-10 23:43:48 +00001746 OS << '(' << Size << ')';
Quentin Colombet36ce1b02016-02-10 23:43:48 +00001747 }
Chris Lattnerac6e9742002-10-30 01:55:38 +00001748 }
Tanya Lattner23dbc812004-06-25 00:13:11 +00001749
Dan Gohman34341e62009-10-31 20:19:03 +00001750 if (StartOp != 0)
1751 OS << " = ";
1752
1753 // Print the opcode name.
Eric Christopher1cdefae2015-02-27 00:11:34 +00001754 if (TII)
1755 OS << TII->getName(getOpcode());
Benjamin Kramerbf152d52012-02-10 13:18:44 +00001756 else
1757 OS << "UNKNOWN";
Misha Brukman835702a2005-04-21 22:36:52 +00001758
Tim Northover98a56eb2016-07-22 22:13:36 +00001759 if (getNumTypes() > 0) {
1760 OS << " { ";
1761 for (unsigned i = 0; i < getNumTypes(); ++i) {
1762 getType(i).print(OS);
1763 if (i + 1 != getNumTypes())
1764 OS << ", ";
1765 }
1766 OS << " } ";
Quentin Colombet41bea872016-03-07 22:47:23 +00001767 }
Quentin Colombet98551112016-02-11 18:22:37 +00001768
Andrew Trickb36388a2013-01-25 07:45:25 +00001769 if (SkipOpers)
1770 return;
1771
Dan Gohman34341e62009-10-31 20:19:03 +00001772 // Print the rest of the operands.
Dan Gohman2745d192009-11-09 19:38:45 +00001773 bool OmittedAnyCallClobbers = false;
1774 bool FirstOp = true;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001775 unsigned AsmDescOp = ~0u;
1776 unsigned AsmOpCount = 0;
Evan Cheng6eb516d2011-01-07 23:50:32 +00001777
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +00001778 if (isInlineAsm() && e >= InlineAsm::MIOp_FirstOperand) {
Evan Cheng6eb516d2011-01-07 23:50:32 +00001779 // Print asm string.
1780 OS << " ";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001781 getOperand(InlineAsm::MIOp_AsmString).print(OS, MST, TRI);
Evan Cheng6eb516d2011-01-07 23:50:32 +00001782
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001783 // Print HasSideEffects, MayLoad, MayStore, IsAlignStack
Evan Cheng6eb516d2011-01-07 23:50:32 +00001784 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1785 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1786 OS << " [sideeffect]";
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001787 if (ExtraInfo & InlineAsm::Extra_MayLoad)
1788 OS << " [mayload]";
1789 if (ExtraInfo & InlineAsm::Extra_MayStore)
1790 OS << " [maystore]";
Wei Ding0526e7f2016-06-22 18:51:08 +00001791 if (ExtraInfo & InlineAsm::Extra_IsConvergent)
1792 OS << " [isconvergent]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00001793 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1794 OS << " [alignstack]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00001795 if (getInlineAsmDialect() == InlineAsm::AD_ATT)
Chad Rosier994f4042012-09-05 21:00:58 +00001796 OS << " [attdialect]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00001797 if (getInlineAsmDialect() == InlineAsm::AD_Intel)
Chad Rosier994f4042012-09-05 21:00:58 +00001798 OS << " [inteldialect]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00001799
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001800 StartOp = AsmDescOp = InlineAsm::MIOp_FirstOperand;
Evan Cheng6eb516d2011-01-07 23:50:32 +00001801 FirstOp = false;
1802 }
1803
Chris Lattnerac6e9742002-10-30 01:55:38 +00001804 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohman2745d192009-11-09 19:38:45 +00001805 const MachineOperand &MO = getOperand(i);
1806
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001807 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001808 VirtRegs.push_back(MO.getReg());
1809
Dan Gohman2745d192009-11-09 19:38:45 +00001810 // Omit call-clobbered registers which aren't used anywhere. This makes
1811 // call instructions much less noisy on targets where calls clobber lots
1812 // of registers. Don't rely on MO.isDead() because we may be called before
1813 // LiveVariables is run, or we may be looking at a non-allocatable reg.
Craig Toppercf0444b2014-11-17 05:50:14 +00001814 if (MRI && isCall() &&
Dan Gohman2745d192009-11-09 19:38:45 +00001815 MO.isReg() && MO.isImplicit() && MO.isDef()) {
1816 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001817 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Craig Toppercf0444b2014-11-17 05:50:14 +00001818 if (MRI->use_empty(Reg)) {
Dan Gohman2745d192009-11-09 19:38:45 +00001819 bool HasAliasLive = false;
Eric Christopher1cdefae2015-02-27 00:11:34 +00001820 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001821 unsigned AliasReg = *AI;
Craig Toppercf0444b2014-11-17 05:50:14 +00001822 if (!MRI->use_empty(AliasReg)) {
Dan Gohman2745d192009-11-09 19:38:45 +00001823 HasAliasLive = true;
1824 break;
1825 }
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001826 }
Dan Gohman2745d192009-11-09 19:38:45 +00001827 if (!HasAliasLive) {
1828 OmittedAnyCallClobbers = true;
1829 continue;
1830 }
1831 }
1832 }
1833 }
1834
1835 if (FirstOp) FirstOp = false; else OS << ",";
Chris Lattnerac6e9742002-10-30 01:55:38 +00001836 OS << " ";
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001837 if (i < getDesc().NumOperands) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001838 const MCOperandInfo &MCOI = getDesc().OpInfo[i];
1839 if (MCOI.isPredicate())
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001840 OS << "pred:";
Evan Cheng6cc775f2011-06-28 19:10:37 +00001841 if (MCOI.isOptionalDef())
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001842 OS << "opt:";
1843 }
Evan Chengd4d1a512010-04-28 20:03:13 +00001844 if (isDebugValue() && MO.isMetadata()) {
1845 // Pretty print DBG_VALUE instructions.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001846 auto *DIV = dyn_cast<DILocalVariable>(MO.getMetadata());
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +00001847 if (DIV && !DIV->getName().empty())
1848 OS << "!\"" << DIV->getName() << '\"';
Evan Chengd4d1a512010-04-28 20:03:13 +00001849 else
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001850 MO.print(OS, MST, TRI);
Eric Christopher1cdefae2015-02-27 00:11:34 +00001851 } else if (TRI && (isInsertSubreg() || isRegSequence()) && MO.isImm()) {
1852 OS << TRI->getSubRegIndexName(MO.getImm());
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001853 } else if (i == AsmDescOp && MO.isImm()) {
1854 // Pretty print the inline asm operand descriptor.
1855 OS << '$' << AsmOpCount++;
1856 unsigned Flag = MO.getImm();
1857 switch (InlineAsm::getKind(Flag)) {
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001858 case InlineAsm::Kind_RegUse: OS << ":[reguse"; break;
1859 case InlineAsm::Kind_RegDef: OS << ":[regdef"; break;
1860 case InlineAsm::Kind_RegDefEarlyClobber: OS << ":[regdef-ec"; break;
1861 case InlineAsm::Kind_Clobber: OS << ":[clobber"; break;
1862 case InlineAsm::Kind_Imm: OS << ":[imm"; break;
1863 case InlineAsm::Kind_Mem: OS << ":[mem"; break;
1864 default: OS << ":[??" << InlineAsm::getKind(Flag); break;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001865 }
1866
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001867 unsigned RCID = 0;
Simon Dardisd32a2d32016-07-18 13:17:31 +00001868 if (!InlineAsm::isImmKind(Flag) && !InlineAsm::isMemKind(Flag) &&
1869 InlineAsm::hasRegClassConstraint(Flag, RCID)) {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001870 if (TRI) {
1871 OS << ':' << TRI->getRegClassName(TRI->getRegClass(RCID));
Craig Toppercf0444b2014-11-17 05:50:14 +00001872 } else
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001873 OS << ":RC" << RCID;
Nick Lewycky84882252011-10-13 00:54:59 +00001874 }
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001875
Simon Dardisd32a2d32016-07-18 13:17:31 +00001876 if (InlineAsm::isMemKind(Flag)) {
1877 unsigned MCID = InlineAsm::getMemoryConstraintID(Flag);
1878 switch (MCID) {
1879 case InlineAsm::Constraint_es: OS << ":es"; break;
1880 case InlineAsm::Constraint_i: OS << ":i"; break;
1881 case InlineAsm::Constraint_m: OS << ":m"; break;
1882 case InlineAsm::Constraint_o: OS << ":o"; break;
1883 case InlineAsm::Constraint_v: OS << ":v"; break;
1884 case InlineAsm::Constraint_Q: OS << ":Q"; break;
1885 case InlineAsm::Constraint_R: OS << ":R"; break;
1886 case InlineAsm::Constraint_S: OS << ":S"; break;
1887 case InlineAsm::Constraint_T: OS << ":T"; break;
1888 case InlineAsm::Constraint_Um: OS << ":Um"; break;
1889 case InlineAsm::Constraint_Un: OS << ":Un"; break;
1890 case InlineAsm::Constraint_Uq: OS << ":Uq"; break;
1891 case InlineAsm::Constraint_Us: OS << ":Us"; break;
1892 case InlineAsm::Constraint_Ut: OS << ":Ut"; break;
1893 case InlineAsm::Constraint_Uv: OS << ":Uv"; break;
1894 case InlineAsm::Constraint_Uy: OS << ":Uy"; break;
1895 case InlineAsm::Constraint_X: OS << ":X"; break;
1896 case InlineAsm::Constraint_Z: OS << ":Z"; break;
1897 case InlineAsm::Constraint_ZC: OS << ":ZC"; break;
1898 case InlineAsm::Constraint_Zy: OS << ":Zy"; break;
1899 default: OS << ":?"; break;
1900 }
1901 }
1902
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001903 unsigned TiedTo = 0;
1904 if (InlineAsm::isUseOperandTiedToDef(Flag, TiedTo))
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001905 OS << " tiedto:$" << TiedTo;
1906
1907 OS << ']';
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001908
1909 // Compute the index of the next operand descriptor.
1910 AsmDescOp += 1 + InlineAsm::getNumOperandRegisters(Flag);
Evan Chengd4d1a512010-04-28 20:03:13 +00001911 } else
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001912 MO.print(OS, MST, TRI);
Dan Gohman2745d192009-11-09 19:38:45 +00001913 }
1914
1915 // Briefly indicate whether any call clobbers were omitted.
1916 if (OmittedAnyCallClobbers) {
Bill Wendlingec030f22009-12-25 13:45:50 +00001917 if (!FirstOp) OS << ",";
Dan Gohman2745d192009-11-09 19:38:45 +00001918 OS << " ...";
Chris Lattner214808f2002-10-30 00:48:05 +00001919 }
Misha Brukman835702a2005-04-21 22:36:52 +00001920
Dan Gohman34341e62009-10-31 20:19:03 +00001921 bool HaveSemi = false;
Michael Kuperstein098cd9f2015-09-16 11:18:25 +00001922 const unsigned PrintableFlags = FrameSetup | FrameDestroy;
Jakob Stoklund Olesen6922e9c2013-01-09 18:35:09 +00001923 if (Flags & PrintableFlags) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001924 if (!HaveSemi) {
1925 OS << ";";
1926 HaveSemi = true;
1927 }
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001928 OS << " flags: ";
1929
1930 if (Flags & FrameSetup)
1931 OS << "FrameSetup";
Michael Kuperstein098cd9f2015-09-16 11:18:25 +00001932
1933 if (Flags & FrameDestroy)
1934 OS << "FrameDestroy";
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001935 }
1936
Dan Gohman3b460302008-07-07 23:14:23 +00001937 if (!memoperands_empty()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001938 if (!HaveSemi) {
1939 OS << ";";
1940 HaveSemi = true;
1941 }
Dan Gohman34341e62009-10-31 20:19:03 +00001942
1943 OS << " mem:";
Dan Gohman48b185d2009-09-25 20:36:54 +00001944 for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
1945 i != e; ++i) {
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001946 (*i)->print(OS, MST);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001947 if (std::next(i) != e)
Dan Gohmanc0353bf2009-09-23 01:33:16 +00001948 OS << " ";
Dan Gohman2d489b52008-02-06 22:27:42 +00001949 }
1950 }
1951
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001952 // Print the regclass of any virtual registers encountered.
1953 if (MRI && !VirtRegs.empty()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001954 if (!HaveSemi) {
1955 OS << ";";
1956 HaveSemi = true;
1957 }
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001958 for (unsigned i = 0; i != VirtRegs.size(); ++i) {
Quentin Colombet03c41962016-04-07 23:18:11 +00001959 const RegClassOrRegBank &RC = MRI->getRegClassOrRegBank(VirtRegs[i]);
Quentin Colombete1494c32016-02-11 00:19:17 +00001960 if (!RC)
1961 continue;
Quentin Colombet03c41962016-04-07 23:18:11 +00001962 // Generic virtual registers do not have register classes.
1963 if (RC.is<const RegisterBank *>())
1964 OS << " " << RC.get<const RegisterBank *>()->getName();
1965 else
1966 OS << " "
1967 << TRI->getRegClassName(RC.get<const TargetRegisterClass *>());
1968 OS << ':' << PrintReg(VirtRegs[i]);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001969 for (unsigned j = i+1; j != VirtRegs.size();) {
Quentin Colombet03c41962016-04-07 23:18:11 +00001970 if (MRI->getRegClassOrRegBank(VirtRegs[j]) != RC) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001971 ++j;
1972 continue;
1973 }
1974 if (VirtRegs[i] != VirtRegs[j])
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +00001975 OS << "," << PrintReg(VirtRegs[j]);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001976 VirtRegs.erase(VirtRegs.begin()+j);
1977 }
1978 }
1979 }
1980
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001981 // Print debug location information.
Duncan P. N. Exon Smithc5bd3e02015-04-03 16:23:04 +00001982 if (isDebugValue() && getOperand(e - 2).isMetadata()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001983 if (!HaveSemi)
1984 OS << ";";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001985 auto *DV = cast<DILocalVariable>(getOperand(e - 2).getMetadata());
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +00001986 OS << " line no:" << DV->getLine();
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001987 if (auto *InlinedAt = debugLoc->getInlinedAt()) {
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00001988 DebugLoc InlinedAtDL(InlinedAt);
1989 if (InlinedAtDL && MF) {
Devang Pateld61b1d52011-08-04 20:44:26 +00001990 OS << " inlined @[ ";
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00001991 InlinedAtDL.print(OS);
Devang Pateld61b1d52011-08-04 20:44:26 +00001992 OS << " ]";
1993 }
1994 }
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001995 if (isIndirectDebugValue())
1996 OS << " indirect";
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00001997 } else if (debugLoc && MF) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001998 if (!HaveSemi)
1999 OS << ";";
Dan Gohman2e3f1872009-11-23 21:29:08 +00002000 OS << " dbg:";
Eric Christopherb9f00092015-02-26 23:32:17 +00002001 debugLoc.print(OS);
Bill Wendling1a0a3d02009-02-19 21:44:55 +00002002 }
2003
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00002004 OS << '\n';
Chris Lattner214808f2002-10-30 00:48:05 +00002005}
2006
Owen Anderson2a8a4852008-01-24 01:10:07 +00002007bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00002008 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00002009 bool AddIfNotFound) {
Evan Cheng6c177732008-04-16 09:41:59 +00002010 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00002011 bool hasAliases = isPhysReg &&
2012 MCRegAliasIterator(IncomingReg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00002013 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00002014 SmallVector<unsigned,4> DeadOps;
Bill Wendling7921ad02008-03-03 22:14:33 +00002015 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2016 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenf465f062009-08-04 20:09:25 +00002017 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng6c177732008-04-16 09:41:59 +00002018 continue;
Mandeep Singh Grange5a2f112016-05-10 17:57:27 +00002019
2020 // DEBUG_VALUE nodes do not contribute to code generation and should
2021 // always be ignored. Failure to do so may result in trying to modify
2022 // KILL flags on DEBUG_VALUE nodes.
2023 if (MO.isDebug())
2024 continue;
2025
Evan Cheng6c177732008-04-16 09:41:59 +00002026 unsigned Reg = MO.getReg();
2027 if (!Reg)
2028 continue;
Bill Wendling7921ad02008-03-03 22:14:33 +00002029
Evan Cheng6c177732008-04-16 09:41:59 +00002030 if (Reg == IncomingReg) {
Dan Gohmanc7367b42008-09-03 15:56:16 +00002031 if (!Found) {
2032 if (MO.isKill())
2033 // The register is already marked kill.
2034 return true;
Jakob Stoklund Olesenc59cd9b2009-08-02 19:13:03 +00002035 if (isPhysReg && isRegTiedToDefOperand(i))
2036 // Two-address uses of physregs must not be marked kill.
2037 return true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00002038 MO.setIsKill();
2039 Found = true;
2040 }
2041 } else if (hasAliases && MO.isKill() &&
2042 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00002043 // A super-register kill already exists.
2044 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohmanb2612922008-07-03 01:18:51 +00002045 return true;
2046 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng6c177732008-04-16 09:41:59 +00002047 DeadOps.push_back(i);
Bill Wendling7921ad02008-03-03 22:14:33 +00002048 }
2049 }
2050
Evan Cheng6c177732008-04-16 09:41:59 +00002051 // Trim unneeded kill operands.
2052 while (!DeadOps.empty()) {
2053 unsigned OpIdx = DeadOps.back();
2054 if (getOperand(OpIdx).isImplicit())
2055 RemoveOperand(OpIdx);
2056 else
2057 getOperand(OpIdx).setIsKill(false);
2058 DeadOps.pop_back();
2059 }
2060
Bill Wendling7921ad02008-03-03 22:14:33 +00002061 // If not found, this means an alias of one of the operands is killed. Add a
Owen Anderson2a8a4852008-01-24 01:10:07 +00002062 // new implicit operand if required.
Dan Gohmanc7367b42008-09-03 15:56:16 +00002063 if (!Found && AddIfNotFound) {
Bill Wendling7921ad02008-03-03 22:14:33 +00002064 addOperand(MachineOperand::CreateReg(IncomingReg,
2065 false /*IsDef*/,
2066 true /*IsImp*/,
2067 true /*IsKill*/));
Owen Anderson2a8a4852008-01-24 01:10:07 +00002068 return true;
2069 }
Dan Gohmanc7367b42008-09-03 15:56:16 +00002070 return Found;
Owen Anderson2a8a4852008-01-24 01:10:07 +00002071}
2072
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00002073void MachineInstr::clearRegisterKills(unsigned Reg,
2074 const TargetRegisterInfo *RegInfo) {
2075 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topperc0196b12014-04-14 00:51:57 +00002076 RegInfo = nullptr;
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002077 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00002078 if (!MO.isReg() || !MO.isUse() || !MO.isKill())
2079 continue;
2080 unsigned OpReg = MO.getReg();
Matthias Braunaca625a2016-02-24 19:21:48 +00002081 if ((RegInfo && RegInfo->regsOverlap(Reg, OpReg)) || Reg == OpReg)
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00002082 MO.setIsKill(false);
2083 }
2084}
2085
Matthias Braun1965bfa2013-10-10 21:28:38 +00002086bool MachineInstr::addRegisterDead(unsigned Reg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00002087 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00002088 bool AddIfNotFound) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00002089 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(Reg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00002090 bool hasAliases = isPhysReg &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00002091 MCRegAliasIterator(Reg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00002092 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00002093 SmallVector<unsigned,4> DeadOps;
Owen Anderson2a8a4852008-01-24 01:10:07 +00002094 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2095 MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00002096 if (!MO.isReg() || !MO.isDef())
Evan Cheng6c177732008-04-16 09:41:59 +00002097 continue;
Matthias Braun1965bfa2013-10-10 21:28:38 +00002098 unsigned MOReg = MO.getReg();
2099 if (!MOReg)
Dan Gohmanc7367b42008-09-03 15:56:16 +00002100 continue;
2101
Matthias Braun1965bfa2013-10-10 21:28:38 +00002102 if (MOReg == Reg) {
Jakob Stoklund Olesen76ad3de2011-04-05 16:53:50 +00002103 MO.setIsDead();
2104 Found = true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00002105 } else if (hasAliases && MO.isDead() &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00002106 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00002107 // There exists a super-register that's marked dead.
Matthias Braun1965bfa2013-10-10 21:28:38 +00002108 if (RegInfo->isSuperRegister(Reg, MOReg))
Dan Gohmanb2612922008-07-03 01:18:51 +00002109 return true;
Matthias Braun1965bfa2013-10-10 21:28:38 +00002110 if (RegInfo->isSubRegister(Reg, MOReg))
Evan Cheng6c177732008-04-16 09:41:59 +00002111 DeadOps.push_back(i);
Owen Anderson2a8a4852008-01-24 01:10:07 +00002112 }
2113 }
2114
Evan Cheng6c177732008-04-16 09:41:59 +00002115 // Trim unneeded dead operands.
2116 while (!DeadOps.empty()) {
2117 unsigned OpIdx = DeadOps.back();
2118 if (getOperand(OpIdx).isImplicit())
2119 RemoveOperand(OpIdx);
2120 else
2121 getOperand(OpIdx).setIsDead(false);
2122 DeadOps.pop_back();
2123 }
2124
Dan Gohmanc7367b42008-09-03 15:56:16 +00002125 // If not found, this means an alias of one of the operands is dead. Add a
2126 // new implicit operand if required.
Chris Lattnerfd682802009-06-24 17:54:48 +00002127 if (Found || !AddIfNotFound)
2128 return Found;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00002129
Matthias Braun1965bfa2013-10-10 21:28:38 +00002130 addOperand(MachineOperand::CreateReg(Reg,
Chris Lattnerfd682802009-06-24 17:54:48 +00002131 true /*IsDef*/,
2132 true /*IsImp*/,
2133 false /*IsKill*/,
2134 true /*IsDead*/));
2135 return true;
Owen Anderson2a8a4852008-01-24 01:10:07 +00002136}
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00002137
Matthias Braun26e7ea62015-02-04 19:35:16 +00002138void MachineInstr::clearRegisterDeads(unsigned Reg) {
2139 for (MachineOperand &MO : operands()) {
2140 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg)
2141 continue;
2142 MO.setIsDead(false);
2143 }
2144}
2145
Matthias Braun2c98d0f2015-11-11 00:41:58 +00002146void MachineInstr::setRegisterDefReadUndef(unsigned Reg, bool IsUndef) {
Matthias Braunc1988f32015-01-21 22:55:13 +00002147 for (MachineOperand &MO : operands()) {
2148 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg || MO.getSubReg() == 0)
2149 continue;
Matthias Braun2c98d0f2015-11-11 00:41:58 +00002150 MO.setIsUndef(IsUndef);
Matthias Braunc1988f32015-01-21 22:55:13 +00002151 }
2152}
2153
Matthias Braun1965bfa2013-10-10 21:28:38 +00002154void MachineInstr::addRegisterDefined(unsigned Reg,
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00002155 const TargetRegisterInfo *RegInfo) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00002156 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
2157 MachineOperand *MO = findRegisterDefOperand(Reg, false, RegInfo);
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00002158 if (MO)
2159 return;
2160 } else {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002161 for (const MachineOperand &MO : operands()) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00002162 if (MO.isReg() && MO.getReg() == Reg && MO.isDef() &&
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00002163 MO.getSubReg() == 0)
2164 return;
2165 }
2166 }
Matthias Braun1965bfa2013-10-10 21:28:38 +00002167 addOperand(MachineOperand::CreateReg(Reg,
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00002168 true /*IsDef*/,
2169 true /*IsImp*/));
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00002170}
Evan Cheng59d27fe2010-03-03 23:37:30 +00002171
Jakob Stoklund Olesen4290be42012-02-03 20:43:39 +00002172void MachineInstr::setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs,
Dan Gohman86936502010-06-18 23:28:01 +00002173 const TargetRegisterInfo &TRI) {
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00002174 bool HasRegMask = false;
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002175 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00002176 if (MO.isRegMask()) {
2177 HasRegMask = true;
2178 continue;
2179 }
Dan Gohman86936502010-06-18 23:28:01 +00002180 if (!MO.isReg() || !MO.isDef()) continue;
2181 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenf6507322012-02-03 20:43:35 +00002182 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Dan Gohman86936502010-06-18 23:28:01 +00002183 // If there are no uses, including partial uses, the def is dead.
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002184 if (std::none_of(UsedRegs.begin(), UsedRegs.end(),
2185 [&](unsigned Use) { return TRI.regsOverlap(Use, Reg); }))
2186 MO.setIsDead();
Dan Gohman86936502010-06-18 23:28:01 +00002187 }
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00002188
2189 // This is a call with a register mask operand.
2190 // Mask clobbers are always dead, so add defs for the non-dead defines.
2191 if (HasRegMask)
2192 for (ArrayRef<unsigned>::iterator I = UsedRegs.begin(), E = UsedRegs.end();
2193 I != E; ++I)
2194 addRegisterDefined(*I, &TRI);
Dan Gohman86936502010-06-18 23:28:01 +00002195}
2196
Evan Cheng59d27fe2010-03-03 23:37:30 +00002197unsigned
2198MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) {
Chandler Carruth962152c2012-03-07 09:39:46 +00002199 // Build up a buffer of hash code components.
Chandler Carruth962152c2012-03-07 09:39:46 +00002200 SmallVector<size_t, 8> HashComponents;
2201 HashComponents.reserve(MI->getNumOperands() + 1);
2202 HashComponents.push_back(MI->getOpcode());
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002203 for (const MachineOperand &MO : MI->operands()) {
Chandler Carruth264854f2012-07-05 11:06:22 +00002204 if (MO.isReg() && MO.isDef() &&
2205 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
2206 continue; // Skip virtual register defs.
2207
2208 HashComponents.push_back(hash_value(MO));
Evan Cheng59d27fe2010-03-03 23:37:30 +00002209 }
Chandler Carruth962152c2012-03-07 09:39:46 +00002210 return hash_combine_range(HashComponents.begin(), HashComponents.end());
Evan Cheng59d27fe2010-03-03 23:37:30 +00002211}
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002212
2213void MachineInstr::emitError(StringRef Msg) const {
2214 // Find the source location cookie.
2215 unsigned LocCookie = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00002216 const MDNode *LocMD = nullptr;
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002217 for (unsigned i = getNumOperands(); i != 0; --i) {
2218 if (getOperand(i-1).isMetadata() &&
2219 (LocMD = getOperand(i-1).getMetadata()) &&
2220 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002221 if (const ConstantInt *CI =
2222 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002223 LocCookie = CI->getZExtValue();
2224 break;
2225 }
2226 }
2227 }
2228
2229 if (const MachineBasicBlock *MBB = getParent())
2230 if (const MachineFunction *MF = MBB->getParent())
2231 return MF->getMMI().getModule()->getContext().emitError(LocCookie, Msg);
2232 report_fatal_error(Msg);
2233}
Reid Kleckner28865802016-04-14 18:29:59 +00002234
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002235MachineInstrBuilder llvm::BuildMI(MachineFunction &MF, const DebugLoc &DL,
Reid Kleckner28865802016-04-14 18:29:59 +00002236 const MCInstrDesc &MCID, bool IsIndirect,
2237 unsigned Reg, unsigned Offset,
2238 const MDNode *Variable, const MDNode *Expr) {
2239 assert(isa<DILocalVariable>(Variable) && "not a variable");
2240 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
2241 assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
2242 "Expected inlined-at fields to agree");
2243 if (IsIndirect)
2244 return BuildMI(MF, DL, MCID)
2245 .addReg(Reg, RegState::Debug)
2246 .addImm(Offset)
2247 .addMetadata(Variable)
2248 .addMetadata(Expr);
2249 else {
2250 assert(Offset == 0 && "A direct address cannot have an offset.");
2251 return BuildMI(MF, DL, MCID)
2252 .addReg(Reg, RegState::Debug)
2253 .addReg(0U, RegState::Debug)
2254 .addMetadata(Variable)
2255 .addMetadata(Expr);
2256 }
2257}
2258
2259MachineInstrBuilder llvm::BuildMI(MachineBasicBlock &BB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002260 MachineBasicBlock::iterator I,
2261 const DebugLoc &DL, const MCInstrDesc &MCID,
2262 bool IsIndirect, unsigned Reg,
2263 unsigned Offset, const MDNode *Variable,
2264 const MDNode *Expr) {
Reid Kleckner28865802016-04-14 18:29:59 +00002265 assert(isa<DILocalVariable>(Variable) && "not a variable");
2266 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
2267 MachineFunction &MF = *BB.getParent();
2268 MachineInstr *MI =
2269 BuildMI(MF, DL, MCID, IsIndirect, Reg, Offset, Variable, Expr);
2270 BB.insert(I, MI);
2271 return MachineInstrBuilder(MF, MI);
2272}