blob: 7039aecc2198595d6693be84c3ad58e0a8432c70 [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);
Krzysztof Parzyszek673b3472016-08-22 14:50:12 +000096 if (isDef())
97 setIsUndef(false);
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +000098 }
99 setReg(Reg);
100}
101
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000102/// Change a def to a use, or a use to a def.
103void MachineOperand::setIsDef(bool Val) {
104 assert(isReg() && "Wrong MachineOperand accessor");
105 assert((!Val || !isDebug()) && "Marking a debug operation as def");
106 if (IsDef == Val)
107 return;
108 // MRI may keep uses and defs in different list positions.
109 if (MachineInstr *MI = getParent())
110 if (MachineBasicBlock *MBB = MI->getParent())
111 if (MachineFunction *MF = MBB->getParent()) {
112 MachineRegisterInfo &MRI = MF->getRegInfo();
113 MRI.removeRegOperandFromUseList(this);
114 IsDef = Val;
115 MRI.addRegOperandToUseList(this);
116 return;
117 }
118 IsDef = Val;
119}
120
Matt Arsenault93ffe582014-09-28 19:24:59 +0000121// If this operand is currently a register operand, and if this is in a
122// function, deregister the operand from the register's use/def list.
123void MachineOperand::removeRegFromUses() {
124 if (!isReg() || !isOnRegUseList())
125 return;
126
127 if (MachineInstr *MI = getParent()) {
128 if (MachineBasicBlock *MBB = MI->getParent()) {
129 if (MachineFunction *MF = MBB->getParent())
130 MF->getRegInfo().removeRegOperandFromUseList(this);
131 }
132 }
133}
134
Chris Lattner961e7422008-01-01 01:12:31 +0000135/// ChangeToImmediate - Replace this operand with a new immediate operand of
136/// the specified value. If an operand is known to be an immediate already,
137/// the setImm method should be used.
138void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000139 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
Matt Arsenault93ffe582014-09-28 19:24:59 +0000140
141 removeRegFromUses();
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000142
Chris Lattner961e7422008-01-01 01:12:31 +0000143 OpKind = MO_Immediate;
144 Contents.ImmVal = ImmVal;
145}
146
Matt Arsenault93ffe582014-09-28 19:24:59 +0000147void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm) {
148 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
149
150 removeRegFromUses();
151
152 OpKind = MO_FPImmediate;
153 Contents.CFP = FPImm;
154}
155
Matt Arsenault633dba42015-05-06 17:05:54 +0000156void MachineOperand::ChangeToES(const char *SymName, unsigned char TargetFlags) {
157 assert((!isReg() || !isTied()) &&
158 "Cannot change a tied operand into an external symbol");
159
160 removeRegFromUses();
161
162 OpKind = MO_ExternalSymbol;
163 Contents.OffsetedInfo.Val.SymbolName = SymName;
164 setOffset(0); // Offset is always 0.
165 setTargetFlags(TargetFlags);
166}
167
168void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym) {
169 assert((!isReg() || !isTied()) &&
170 "Cannot change a tied operand into an MCSymbol");
171
172 removeRegFromUses();
173
174 OpKind = MO_MCSymbol;
175 Contents.Sym = Sym;
176}
177
Matt Arsenault25dba302016-09-13 19:03:12 +0000178void MachineOperand::ChangeToFrameIndex(int Idx) {
179 assert((!isReg() || !isTied()) &&
180 "Cannot change a tied operand into a FrameIndex");
181
182 removeRegFromUses();
183
184 OpKind = MO_FrameIndex;
185 setIndex(Idx);
186}
187
Chris Lattner961e7422008-01-01 01:12:31 +0000188/// ChangeToRegister - Replace this operand with a new register operand of
189/// the specified value. If an operand is known to be an register already,
190/// the setReg method should be used.
191void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Dale Johannesend40d42c2010-02-10 00:41:49 +0000192 bool isKill, bool isDead, bool isUndef,
193 bool isDebug) {
Craig Topperc0196b12014-04-14 00:51:57 +0000194 MachineRegisterInfo *RegInfo = nullptr;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000195 if (MachineInstr *MI = getParent())
196 if (MachineBasicBlock *MBB = MI->getParent())
197 if (MachineFunction *MF = MBB->getParent())
198 RegInfo = &MF->getRegInfo();
199 // If this operand is already a register operand, remove it from the
Chris Lattner961e7422008-01-01 01:12:31 +0000200 // register's use/def lists.
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000201 bool WasReg = isReg();
202 if (RegInfo && WasReg)
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000203 RegInfo->removeRegOperandFromUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +0000204
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000205 // Change this to a register and set the reg#.
206 OpKind = MO_Register;
207 SmallContents.RegNo = Reg;
Jakob Stoklund Olesena1b246d2013-01-07 23:21:44 +0000208 SubReg_TargetFlags = 0;
Chris Lattner961e7422008-01-01 01:12:31 +0000209 IsDef = isDef;
210 IsImp = isImp;
211 IsKill = isKill;
212 IsDead = isDead;
Evan Cheng0dc101b2009-06-30 08:49:04 +0000213 IsUndef = isUndef;
Jakob Stoklund Olesenb0d91ab2011-12-07 00:22:07 +0000214 IsInternalRead = false;
Dale Johannesenc0d712d2008-09-14 01:44:36 +0000215 IsEarlyClobber = false;
Dale Johannesend40d42c2010-02-10 00:41:49 +0000216 IsDebug = isDebug;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000217 // Ensure isOnRegUseList() returns false.
Craig Topperc0196b12014-04-14 00:51:57 +0000218 Contents.Reg.Prev = nullptr;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000219 // Preserve the tie when the operand was already a register.
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000220 if (!WasReg)
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000221 TiedTo = 0;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000222
223 // If this operand is embedded in a function, add the operand to the
224 // register's use/def list.
225 if (RegInfo)
226 RegInfo->addRegOperandToUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +0000227}
228
Chris Lattner60055892007-12-30 21:56:09 +0000229/// isIdenticalTo - Return true if this operand is identical to the specified
Chandler Carruth264854f2012-07-05 11:06:22 +0000230/// operand. Note that this should stay in sync with the hash_value overload
231/// below.
Chris Lattner60055892007-12-30 21:56:09 +0000232bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
Chris Lattnerfd682802009-06-24 17:54:48 +0000233 if (getType() != Other.getType() ||
234 getTargetFlags() != Other.getTargetFlags())
235 return false;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000236
Chris Lattner60055892007-12-30 21:56:09 +0000237 switch (getType()) {
Chris Lattner60055892007-12-30 21:56:09 +0000238 case MachineOperand::MO_Register:
239 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
240 getSubReg() == Other.getSubReg();
241 case MachineOperand::MO_Immediate:
242 return getImm() == Other.getImm();
Cameron Zwarich7da0f9a2011-07-01 23:45:21 +0000243 case MachineOperand::MO_CImmediate:
244 return getCImm() == Other.getCImm();
Nate Begeman26b76b62008-02-14 07:39:30 +0000245 case MachineOperand::MO_FPImmediate:
246 return getFPImm() == Other.getFPImm();
Chris Lattner60055892007-12-30 21:56:09 +0000247 case MachineOperand::MO_MachineBasicBlock:
248 return getMBB() == Other.getMBB();
249 case MachineOperand::MO_FrameIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000250 return getIndex() == Other.getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000251 case MachineOperand::MO_ConstantPoolIndex:
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000252 case MachineOperand::MO_TargetIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000253 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattner60055892007-12-30 21:56:09 +0000254 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000255 return getIndex() == Other.getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000256 case MachineOperand::MO_GlobalAddress:
257 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
258 case MachineOperand::MO_ExternalSymbol:
259 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
260 getOffset() == Other.getOffset();
Dan Gohman6c938802009-10-30 01:27:03 +0000261 case MachineOperand::MO_BlockAddress:
Michael Liaoabb87d42012-09-12 21:43:09 +0000262 return getBlockAddress() == Other.getBlockAddress() &&
263 getOffset() == Other.getOffset();
Juergen Ributzkae8294752013-12-14 06:53:06 +0000264 case MachineOperand::MO_RegisterMask:
Oren Ben Simhonfe34c5e2017-03-14 09:09:26 +0000265 case MachineOperand::MO_RegisterLiveOut: {
266 // Shallow compare of the two RegMasks
267 const uint32_t *RegMask = getRegMask();
268 const uint32_t *OtherRegMask = Other.getRegMask();
269 if (RegMask == OtherRegMask)
270 return true;
271
272 // Calculate the size of the RegMask
273 const MachineFunction *MF = getParent()->getParent()->getParent();
274 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
275 unsigned RegMaskSize = (TRI->getNumRegs() + 31) / 32;
276
277 // Deep compare of the two RegMasks
278 return std::equal(RegMask, RegMask + RegMaskSize, OtherRegMask);
279 }
Chris Lattner6c604e32010-03-13 08:14:18 +0000280 case MachineOperand::MO_MCSymbol:
281 return getMCSymbol() == Other.getMCSymbol();
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000282 case MachineOperand::MO_CFIIndex:
283 return getCFIIndex() == Other.getCFIIndex();
Chris Lattnerf839ee02010-04-07 18:03:19 +0000284 case MachineOperand::MO_Metadata:
285 return getMetadata() == Other.getMetadata();
Tim Northover6b3bd612016-07-29 20:32:59 +0000286 case MachineOperand::MO_IntrinsicID:
287 return getIntrinsicID() == Other.getIntrinsicID();
Tim Northoverde3aea0412016-08-17 20:25:25 +0000288 case MachineOperand::MO_Predicate:
289 return getPredicate() == Other.getPredicate();
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000290 case MachineOperand::MO_Placeholder:
291 return true;
Chris Lattner60055892007-12-30 21:56:09 +0000292 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000293 llvm_unreachable("Invalid machine operand type");
Chris Lattner60055892007-12-30 21:56:09 +0000294}
295
Chandler Carruth264854f2012-07-05 11:06:22 +0000296// Note: this must stay exactly in sync with isIdenticalTo above.
297hash_code llvm::hash_value(const MachineOperand &MO) {
298 switch (MO.getType()) {
299 case MachineOperand::MO_Register:
Jakob Stoklund Olesendba99d02012-08-28 18:05:48 +0000300 // Register operands don't have target flags.
301 return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef());
Chandler Carruth264854f2012-07-05 11:06:22 +0000302 case MachineOperand::MO_Immediate:
303 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm());
304 case MachineOperand::MO_CImmediate:
305 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm());
306 case MachineOperand::MO_FPImmediate:
307 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm());
308 case MachineOperand::MO_MachineBasicBlock:
309 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB());
310 case MachineOperand::MO_FrameIndex:
311 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
312 case MachineOperand::MO_ConstantPoolIndex:
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000313 case MachineOperand::MO_TargetIndex:
Chandler Carruth264854f2012-07-05 11:06:22 +0000314 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(),
315 MO.getOffset());
316 case MachineOperand::MO_JumpTableIndex:
317 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
318 case MachineOperand::MO_ExternalSymbol:
319 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(),
320 MO.getSymbolName());
321 case MachineOperand::MO_GlobalAddress:
322 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(),
323 MO.getOffset());
324 case MachineOperand::MO_BlockAddress:
325 return hash_combine(MO.getType(), MO.getTargetFlags(),
Michael Liaoabb87d42012-09-12 21:43:09 +0000326 MO.getBlockAddress(), MO.getOffset());
Chandler Carruth264854f2012-07-05 11:06:22 +0000327 case MachineOperand::MO_RegisterMask:
Juergen Ributzkae8294752013-12-14 06:53:06 +0000328 case MachineOperand::MO_RegisterLiveOut:
Chandler Carruth264854f2012-07-05 11:06:22 +0000329 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask());
330 case MachineOperand::MO_Metadata:
331 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata());
332 case MachineOperand::MO_MCSymbol:
333 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol());
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000334 case MachineOperand::MO_CFIIndex:
335 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex());
Tim Northover6b3bd612016-07-29 20:32:59 +0000336 case MachineOperand::MO_IntrinsicID:
337 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIntrinsicID());
Tim Northoverde3aea0412016-08-17 20:25:25 +0000338 case MachineOperand::MO_Predicate:
339 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getPredicate());
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000340 case MachineOperand::MO_Placeholder:
341 return hash_combine();
Chandler Carruth264854f2012-07-05 11:06:22 +0000342 }
343 llvm_unreachable("Invalid machine operand type");
344}
345
Tim Northover6b3bd612016-07-29 20:32:59 +0000346void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI,
347 const TargetIntrinsicInfo *IntrinsicInfo) const {
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000348 ModuleSlotTracker DummyMST(nullptr);
Tim Northover6b3bd612016-07-29 20:32:59 +0000349 print(OS, DummyMST, TRI, IntrinsicInfo);
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000350}
351
352void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
Tim Northover6b3bd612016-07-29 20:32:59 +0000353 const TargetRegisterInfo *TRI,
354 const TargetIntrinsicInfo *IntrinsicInfo) const {
Chris Lattner60055892007-12-30 21:56:09 +0000355 switch (getType()) {
356 case MachineOperand::MO_Register:
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000357 OS << PrintReg(getReg(), TRI, getSubReg());
Dan Gohman0ab11442008-12-18 21:51:27 +0000358
Evan Cheng0dc101b2009-06-30 08:49:04 +0000359 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000360 isInternalRead() || isEarlyClobber() || isTied()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000361 OS << '<';
Chris Lattner60055892007-12-30 21:56:09 +0000362 bool NeedComma = false;
Evan Cheng70b1fa52009-10-14 23:37:31 +0000363 if (isDef()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000364 if (NeedComma) OS << ',';
Dale Johannesen1f3ab862008-09-12 17:49:03 +0000365 if (isEarlyClobber())
366 OS << "earlyclobber,";
Evan Cheng70b1fa52009-10-14 23:37:31 +0000367 if (isImplicit())
368 OS << "imp-";
Chris Lattner60055892007-12-30 21:56:09 +0000369 OS << "def";
370 NeedComma = true;
Jakob Stoklund Olesen7111a632012-04-20 21:45:33 +0000371 // <def,read-undef> only makes sense when getSubReg() is set.
372 // Don't clutter the output otherwise.
373 if (isUndef() && getSubReg())
374 OS << ",read-undef";
Evan Chengf781bd82009-10-21 07:56:02 +0000375 } else if (isImplicit()) {
Craig Topper9a9d58a2015-05-16 05:42:08 +0000376 OS << "imp-use";
377 NeedComma = true;
Evan Chengf781bd82009-10-21 07:56:02 +0000378 }
Evan Cheng70b1fa52009-10-14 23:37:31 +0000379
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000380 if (isKill()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000381 if (NeedComma) OS << ',';
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000382 OS << "kill";
383 NeedComma = true;
384 }
385 if (isDead()) {
386 if (NeedComma) OS << ',';
387 OS << "dead";
388 NeedComma = true;
389 }
390 if (isUndef() && isUse()) {
391 if (NeedComma) OS << ',';
392 OS << "undef";
393 NeedComma = true;
394 }
395 if (isInternalRead()) {
396 if (NeedComma) OS << ',';
397 OS << "internal";
398 NeedComma = true;
399 }
400 if (isTied()) {
401 if (NeedComma) OS << ',';
402 OS << "tied";
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000403 if (TiedTo != 15)
404 OS << unsigned(TiedTo - 1);
Chris Lattner60055892007-12-30 21:56:09 +0000405 }
Chris Lattnerfd682802009-06-24 17:54:48 +0000406 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000407 }
408 break;
409 case MachineOperand::MO_Immediate:
410 OS << getImm();
411 break;
Devang Patelf071d722011-06-24 20:46:11 +0000412 case MachineOperand::MO_CImmediate:
413 getCImm()->getValue().print(OS, false);
414 break;
Nate Begeman26b76b62008-02-14 07:39:30 +0000415 case MachineOperand::MO_FPImmediate:
Matt Arsenault59239732016-02-05 00:50:18 +0000416 if (getFPImm()->getType()->isFloatTy()) {
Nate Begeman26b76b62008-02-14 07:39:30 +0000417 OS << getFPImm()->getValueAPF().convertToFloat();
Matt Arsenault59239732016-02-05 00:50:18 +0000418 } else if (getFPImm()->getType()->isHalfTy()) {
419 APFloat APF = getFPImm()->getValueAPF();
420 bool Unused;
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000421 APF.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &Unused);
Matt Arsenault59239732016-02-05 00:50:18 +0000422 OS << "half " << APF.convertToFloat();
423 } else {
Nate Begeman26b76b62008-02-14 07:39:30 +0000424 OS << getFPImm()->getValueAPF().convertToDouble();
Matt Arsenault59239732016-02-05 00:50:18 +0000425 }
Nate Begeman26b76b62008-02-14 07:39:30 +0000426 break;
Chris Lattner60055892007-12-30 21:56:09 +0000427 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman34341e62009-10-31 20:19:03 +0000428 OS << "<BB#" << getMBB()->getNumber() << ">";
Chris Lattner60055892007-12-30 21:56:09 +0000429 break;
430 case MachineOperand::MO_FrameIndex:
Chris Lattnerfd682802009-06-24 17:54:48 +0000431 OS << "<fi#" << getIndex() << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000432 break;
433 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000434 OS << "<cp#" << getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000435 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000436 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000437 break;
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000438 case MachineOperand::MO_TargetIndex:
439 OS << "<ti#" << getIndex();
440 if (getOffset()) OS << "+" << getOffset();
441 OS << '>';
442 break;
Chris Lattner60055892007-12-30 21:56:09 +0000443 case MachineOperand::MO_JumpTableIndex:
Chris Lattnerfd682802009-06-24 17:54:48 +0000444 OS << "<jt#" << getIndex() << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000445 break;
446 case MachineOperand::MO_GlobalAddress:
Dan Gohman0080ee22009-11-06 18:03:10 +0000447 OS << "<ga:";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000448 getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Chris Lattner60055892007-12-30 21:56:09 +0000449 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000450 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000451 break;
452 case MachineOperand::MO_ExternalSymbol:
453 OS << "<es:" << getSymbolName();
454 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000455 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000456 break;
Dan Gohman6c938802009-10-30 01:27:03 +0000457 case MachineOperand::MO_BlockAddress:
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000458 OS << '<';
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000459 getBlockAddress()->printAsOperand(OS, /*PrintType=*/false, MST);
Michael Liaoabb87d42012-09-12 21:43:09 +0000460 if (getOffset()) OS << "+" << getOffset();
Dan Gohman6c938802009-10-30 01:27:03 +0000461 OS << '>';
462 break;
Daniel Sanders1e97a0b2015-08-19 12:03:04 +0000463 case MachineOperand::MO_RegisterMask: {
464 unsigned NumRegsInMask = 0;
465 unsigned NumRegsEmitted = 0;
466 OS << "<regmask";
467 for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {
468 unsigned MaskWord = i / 32;
469 unsigned MaskBit = i % 32;
470 if (getRegMask()[MaskWord] & (1 << MaskBit)) {
471 if (PrintWholeRegMask || NumRegsEmitted <= 10) {
472 OS << " " << PrintReg(i, TRI);
473 NumRegsEmitted++;
474 }
475 NumRegsInMask++;
476 }
477 }
478 if (NumRegsEmitted != NumRegsInMask)
479 OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more...";
480 OS << ">";
Jakob Stoklund Olesen374ed322012-01-16 19:22:00 +0000481 break;
Daniel Sanders1e97a0b2015-08-19 12:03:04 +0000482 }
Juergen Ributzkae8294752013-12-14 06:53:06 +0000483 case MachineOperand::MO_RegisterLiveOut:
484 OS << "<regliveout>";
485 break;
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000486 case MachineOperand::MO_Metadata:
487 OS << '<';
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000488 getMetadata()->printAsOperand(OS, MST);
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000489 OS << '>';
490 break;
Chris Lattner6c604e32010-03-13 08:14:18 +0000491 case MachineOperand::MO_MCSymbol:
492 OS << "<MCSym=" << *getMCSymbol() << '>';
493 break;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000494 case MachineOperand::MO_CFIIndex:
495 OS << "<call frame instruction>";
496 break;
Tim Northover6b3bd612016-07-29 20:32:59 +0000497 case MachineOperand::MO_IntrinsicID: {
498 Intrinsic::ID ID = getIntrinsicID();
499 if (ID < Intrinsic::num_intrinsics)
Ahmed Bougacha925961b2016-09-12 16:21:49 +0000500 OS << "<intrinsic:@" << Intrinsic::getName(ID, None) << '>';
Tim Northover6b3bd612016-07-29 20:32:59 +0000501 else if (IntrinsicInfo)
Ahmed Bougacha925961b2016-09-12 16:21:49 +0000502 OS << "<intrinsic:@" << IntrinsicInfo->getName(ID) << '>';
Tim Northover6b3bd612016-07-29 20:32:59 +0000503 else
504 OS << "<intrinsic:" << ID << '>';
505 break;
506 }
Tim Northoverde3aea0412016-08-17 20:25:25 +0000507 case MachineOperand::MO_Predicate: {
508 auto Pred = static_cast<CmpInst::Predicate>(getPredicate());
509 OS << '<' << (CmpInst::isIntPredicate(Pred) ? "intpred" : "floatpred")
510 << CmpInst::getPredicateName(Pred) << '>';
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000511 break;
Chris Lattner60055892007-12-30 21:56:09 +0000512 }
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000513 case MachineOperand::MO_Placeholder:
514 OS << "<placeholder>";
515 break;
Tim Northoverde3aea0412016-08-17 20:25:25 +0000516 }
Chris Lattnerfd682802009-06-24 17:54:48 +0000517 if (unsigned TF = getTargetFlags())
518 OS << "[TF=" << TF << ']';
Chris Lattner60055892007-12-30 21:56:09 +0000519}
520
Matthias Braun637488d2016-11-18 02:40:40 +0000521#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
522LLVM_DUMP_METHOD void MachineOperand::dump() const {
523 dbgs() << *this << '\n';
524}
525#endif
526
Chris Lattner60055892007-12-30 21:56:09 +0000527//===----------------------------------------------------------------------===//
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000528// MachineMemOperand Implementation
529//===----------------------------------------------------------------------===//
530
Chris Lattnerde93bb02010-09-21 05:39:30 +0000531/// getAddrSpace - Return the LLVM IR address space number that this pointer
532/// points into.
533unsigned MachinePointerInfo::getAddrSpace() const {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000534 if (V.isNull() || V.is<const PseudoSourceValue*>()) return 0;
535 return cast<PointerType>(V.get<const Value*>()->getType())->getAddressSpace();
Chris Lattnerde93bb02010-09-21 05:39:30 +0000536}
537
Chris Lattner82fd06d2010-09-21 06:22:23 +0000538/// getConstantPool - Return a MachinePointerInfo record that refers to the
539/// constant pool.
Alex Lorenze40c8a22015-08-11 23:09:45 +0000540MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) {
541 return MachinePointerInfo(MF.getPSVManager().getConstantPool());
Chris Lattner82fd06d2010-09-21 06:22:23 +0000542}
543
544/// getFixedStack - Return a MachinePointerInfo record that refers to the
545/// the specified FrameIndex.
Alex Lorenze40c8a22015-08-11 23:09:45 +0000546MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF,
547 int FI, int64_t Offset) {
548 return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset);
Chris Lattner82fd06d2010-09-21 06:22:23 +0000549}
550
Alex Lorenze40c8a22015-08-11 23:09:45 +0000551MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) {
552 return MachinePointerInfo(MF.getPSVManager().getJumpTable());
Chris Lattner50287ea2010-09-21 06:43:24 +0000553}
554
Alex Lorenze40c8a22015-08-11 23:09:45 +0000555MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) {
556 return MachinePointerInfo(MF.getPSVManager().getGOT());
Chris Lattner50287ea2010-09-21 06:43:24 +0000557}
Chris Lattnerde93bb02010-09-21 05:39:30 +0000558
Alex Lorenze40c8a22015-08-11 23:09:45 +0000559MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF,
560 int64_t Offset) {
561 return MachinePointerInfo(MF.getPSVManager().getStack(), Offset);
Chris Lattner886250c2010-09-21 18:51:21 +0000562}
563
Justin Lebara3b786a2016-07-14 17:07:44 +0000564MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000565 uint64_t s, unsigned int a,
Hal Finkelcc39b672014-07-24 12:16:19 +0000566 const AAMDNodes &AAInfo,
Konstantin Zhuravlyov8ea02462016-10-15 22:01:18 +0000567 const MDNode *Ranges,
568 SynchronizationScope SynchScope,
569 AtomicOrdering Ordering,
570 AtomicOrdering FailureOrdering)
Justin Lebara3b786a2016-07-14 17:07:44 +0000571 : PtrInfo(ptrinfo), Size(s), FlagVals(f), BaseAlignLog2(Log2_32(a) + 1),
572 AAInfo(AAInfo), Ranges(Ranges) {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000573 assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue*>() ||
574 isa<PointerType>(PtrInfo.V.get<const Value*>()->getType())) &&
Chris Lattner00ca0b82010-09-21 04:32:08 +0000575 "invalid pointer value");
Dan Gohmane7c82422009-09-21 19:47:04 +0000576 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
Dan Gohmanbf98f682008-07-16 15:56:42 +0000577 assert((isLoad() || isStore()) && "Not a load/store!");
Konstantin Zhuravlyov8ea02462016-10-15 22:01:18 +0000578
579 AtomicInfo.SynchScope = static_cast<unsigned>(SynchScope);
580 assert(getSynchScope() == SynchScope && "Value truncated");
581 AtomicInfo.Ordering = static_cast<unsigned>(Ordering);
582 assert(getOrdering() == Ordering && "Value truncated");
583 AtomicInfo.FailureOrdering = static_cast<unsigned>(FailureOrdering);
584 assert(getFailureOrdering() == FailureOrdering && "Value truncated");
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000585}
586
Dan Gohman2da2bed2008-08-20 15:58:01 +0000587/// Profile - Gather unique data for the object.
588///
589void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
Chris Lattner187f6532010-09-21 04:23:39 +0000590 ID.AddInteger(getOffset());
Dan Gohman2da2bed2008-08-20 15:58:01 +0000591 ID.AddInteger(Size);
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000592 ID.AddPointer(getOpaqueValue());
Justin Lebara3b786a2016-07-14 17:07:44 +0000593 ID.AddInteger(getFlags());
594 ID.AddInteger(getBaseAlignment());
Dan Gohman2da2bed2008-08-20 15:58:01 +0000595}
596
Dan Gohman48b185d2009-09-25 20:36:54 +0000597void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
598 // The Value and Offset may differ due to CSE. But the flags and size
599 // should be the same.
600 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
601 assert(MMO->getSize() == getSize() && "Size mismatch!");
602
603 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
604 // Update the alignment value.
Justin Lebara3b786a2016-07-14 17:07:44 +0000605 BaseAlignLog2 = Log2_32(MMO->getBaseAlignment()) + 1;
Dan Gohman48b185d2009-09-25 20:36:54 +0000606 // Also update the base and offset, because the new alignment may
607 // not be applicable with the old ones.
Chris Lattner187f6532010-09-21 04:23:39 +0000608 PtrInfo = MMO->PtrInfo;
Dan Gohman48b185d2009-09-25 20:36:54 +0000609 }
610}
611
Dan Gohman5a6b11c2009-09-25 23:33:20 +0000612/// getAlignment - Return the minimum known alignment in bytes of the
613/// actual memory reference.
614uint64_t MachineMemOperand::getAlignment() const {
615 return MinAlign(getBaseAlignment(), getOffset());
616}
617
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000618void MachineMemOperand::print(raw_ostream &OS) const {
619 ModuleSlotTracker DummyMST(nullptr);
620 print(OS, DummyMST);
621}
622void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST) const {
623 assert((isLoad() || isStore()) &&
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000624 "SV has to be a load, store or both.");
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000625
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000626 if (isVolatile())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000627 OS << "Volatile ";
628
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000629 if (isLoad())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000630 OS << "LD";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000631 if (isStore())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000632 OS << "ST";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000633 OS << getSize();
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000634
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000635 // Print the address information.
636 OS << "[";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000637 if (const Value *V = getValue())
638 V->printAsOperand(OS, /*PrintType=*/false, MST);
639 else if (const PseudoSourceValue *PSV = getPseudoValue())
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000640 PSV->printCustom(OS);
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000641 else
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000642 OS << "<unknown>";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000643
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000644 unsigned AS = getAddrSpace();
Matt Arsenault68c38fd2013-12-14 00:24:02 +0000645 if (AS != 0)
646 OS << "(addrspace=" << AS << ')';
647
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000648 // If the alignment of the memory reference itself differs from the alignment
649 // of the base pointer, print the base alignment explicitly, next to the base
650 // pointer.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000651 if (getBaseAlignment() != getAlignment())
652 OS << "(align=" << getBaseAlignment() << ")";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000653
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000654 if (getOffset() != 0)
655 OS << "+" << getOffset();
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000656 OS << "]";
657
658 // Print the alignment of the reference.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000659 if (getBaseAlignment() != getAlignment() || getBaseAlignment() != getSize())
660 OS << "(align=" << getAlignment() << ")";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000661
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000662 // Print TBAA info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000663 if (const MDNode *TBAAInfo = getAAInfo().TBAA) {
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000664 OS << "(tbaa=";
665 if (TBAAInfo->getNumOperands() > 0)
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000666 TBAAInfo->getOperand(0)->printAsOperand(OS, MST);
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000667 else
668 OS << "<unknown>";
669 OS << ")";
670 }
671
Hal Finkel94146652014-07-24 14:25:39 +0000672 // Print AA scope info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000673 if (const MDNode *ScopeInfo = getAAInfo().Scope) {
Hal Finkel94146652014-07-24 14:25:39 +0000674 OS << "(alias.scope=";
675 if (ScopeInfo->getNumOperands() > 0)
676 for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000677 ScopeInfo->getOperand(i)->printAsOperand(OS, MST);
Hal Finkel94146652014-07-24 14:25:39 +0000678 if (i != ie-1)
679 OS << ",";
680 }
681 else
682 OS << "<unknown>";
683 OS << ")";
684 }
685
686 // Print AA noalias scope info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000687 if (const MDNode *NoAliasInfo = getAAInfo().NoAlias) {
Hal Finkel94146652014-07-24 14:25:39 +0000688 OS << "(noalias=";
689 if (NoAliasInfo->getNumOperands() > 0)
690 for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000691 NoAliasInfo->getOperand(i)->printAsOperand(OS, MST);
Hal Finkel94146652014-07-24 14:25:39 +0000692 if (i != ie-1)
693 OS << ",";
694 }
695 else
696 OS << "<unknown>";
697 OS << ")";
698 }
699
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000700 if (isNonTemporal())
Bill Wendling9f638ab2011-04-29 23:45:22 +0000701 OS << "(nontemporal)";
Justin Lebaradbf09e2016-09-11 01:38:58 +0000702 if (isDereferenceable())
703 OS << "(dereferenceable)";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000704 if (isInvariant())
Matt Arsenault572c29a2015-06-26 19:00:11 +0000705 OS << "(invariant)";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000706}
707
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000708//===----------------------------------------------------------------------===//
Chris Lattner60055892007-12-30 21:56:09 +0000709// MachineInstr Implementation
710//===----------------------------------------------------------------------===//
711
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000712void MachineInstr::addImplicitDefUseOperands(MachineFunction &MF) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000713 if (MCID->ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000714 for (const MCPhysReg *ImpDefs = MCID->getImplicitDefs(); *ImpDefs;
715 ++ImpDefs)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000716 addOperand(MF, MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng6cc775f2011-06-28 19:10:37 +0000717 if (MCID->ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000718 for (const MCPhysReg *ImpUses = MCID->getImplicitUses(); *ImpUses;
719 ++ImpUses)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000720 addOperand(MF, MachineOperand::CreateReg(*ImpUses, false, true));
Evan Cheng77af6ac2006-11-13 23:34:06 +0000721}
722
Bob Wilson406f2702010-04-09 04:34:03 +0000723/// MachineInstr ctor - This constructor creates a MachineInstr and adds the
724/// implicit operands. It reserves space for the number of operands specified by
Evan Cheng6cc775f2011-06-28 19:10:37 +0000725/// the MCInstrDesc.
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000726MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid,
Benjamin Kramera9591b52015-02-07 12:28:15 +0000727 DebugLoc dl, bool NoImp)
728 : MCID(&tid), Parent(nullptr), Operands(nullptr), NumOperands(0), Flags(0),
729 AsmPrinterFlags(0), NumMemRefs(0), MemRefs(nullptr),
Tim Northover0f140c72016-09-09 11:46:34 +0000730 debugLoc(std::move(dl)) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000731 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
732
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000733 // Reserve space for the expected number of operands.
734 if (unsigned NumOps = MCID->getNumOperands() +
735 MCID->getNumImplicitDefs() + MCID->getNumImplicitUses()) {
736 CapOperands = OperandCapacity::get(NumOps);
737 Operands = MF.allocateOperandArray(CapOperands);
738 }
739
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000740 if (!NoImp)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000741 addImplicitDefUseOperands(MF);
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000742}
743
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000744/// MachineInstr ctor - Copies MachineInstr arg exactly
745///
Evan Chenga7a20c42008-07-19 00:37:25 +0000746MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Quentin Colombet98551112016-02-11 18:22:37 +0000747 : MCID(&MI.getDesc()), Parent(nullptr), Operands(nullptr), NumOperands(0),
748 Flags(0), AsmPrinterFlags(0), NumMemRefs(MI.NumMemRefs),
Tim Northover0f140c72016-09-09 11:46:34 +0000749 MemRefs(MI.MemRefs), debugLoc(MI.getDebugLoc()) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000750 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
751
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000752 CapOperands = OperandCapacity::get(MI.getNumOperands());
753 Operands = MF.allocateOperandArray(CapOperands);
Tanya Lattner9953d862004-05-23 20:58:02 +0000754
Jakob Stoklund Olesendc5285f2013-01-05 05:05:51 +0000755 // Copy operands.
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000756 for (const MachineOperand &MO : MI.operands())
757 addOperand(MF, MO);
Tanya Lattnerbcee21b2004-05-24 03:14:18 +0000758
Jakob Stoklund Olesena33f5042012-12-18 21:36:05 +0000759 // Copy all the sensible flags.
760 setFlags(MI.Flags);
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +0000761}
762
Chris Lattner961e7422008-01-01 01:12:31 +0000763/// getRegInfo - If this instruction is embedded into a MachineFunction,
764/// return the MachineRegisterInfo object for the current function, otherwise
765/// return null.
766MachineRegisterInfo *MachineInstr::getRegInfo() {
767 if (MachineBasicBlock *MBB = getParent())
Dan Gohmanf188fa42008-07-08 23:59:09 +0000768 return &MBB->getParent()->getRegInfo();
Craig Topperc0196b12014-04-14 00:51:57 +0000769 return nullptr;
Chris Lattner961e7422008-01-01 01:12:31 +0000770}
771
772/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
773/// this instruction from their respective use lists. This requires that the
774/// operands already be on their use lists.
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000775void MachineInstr::RemoveRegOperandsFromUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000776 for (MachineOperand &MO : operands())
777 if (MO.isReg())
778 MRI.removeRegOperandFromUseList(&MO);
Chris Lattner961e7422008-01-01 01:12:31 +0000779}
780
781/// AddRegOperandsToUseLists - Add all of the register operands in
782/// this instruction from their respective use lists. This requires that the
783/// operands not be on their use lists yet.
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000784void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000785 for (MachineOperand &MO : operands())
786 if (MO.isReg())
787 MRI.addRegOperandToUseList(&MO);
Chris Lattner961e7422008-01-01 01:12:31 +0000788}
789
Jakob Stoklund Olesen2455b5852012-12-20 22:54:05 +0000790void MachineInstr::addOperand(const MachineOperand &Op) {
791 MachineBasicBlock *MBB = getParent();
792 assert(MBB && "Use MachineInstrBuilder to add operands to dangling instrs");
793 MachineFunction *MF = MBB->getParent();
794 assert(MF && "Use MachineInstrBuilder to add operands to dangling instrs");
795 addOperand(*MF, Op);
796}
797
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000798/// Move NumOps MachineOperands from Src to Dst, with support for overlapping
799/// ranges. If MRI is non-null also update use-def chains.
800static void moveOperands(MachineOperand *Dst, MachineOperand *Src,
801 unsigned NumOps, MachineRegisterInfo *MRI) {
802 if (MRI)
803 return MRI->moveOperands(Dst, Src, NumOps);
804
JF Bastiena874d1a2016-03-26 18:20:02 +0000805 // MachineOperand is a trivially copyable type so we can just use memmove.
Benjamin Kramer5c0e64f2015-02-21 16:22:48 +0000806 std::memmove(Dst, Src, NumOps * sizeof(MachineOperand));
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000807}
808
Chris Lattner961e7422008-01-01 01:12:31 +0000809/// addOperand - Add the specified operand to the instruction. If it is an
810/// implicit operand, it is added to the end of the operand list. If it is
811/// an explicit operand it is added at the end of the explicit operand list
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000812/// (before the first implicit operand).
Jakob Stoklund Olesen2455b5852012-12-20 22:54:05 +0000813void MachineInstr::addOperand(MachineFunction &MF, const MachineOperand &Op) {
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000814 assert(MCID && "Cannot add operands before providing an instr descriptor");
Dan Gohman9356d8f2008-12-09 22:45:08 +0000815
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000816 // Check if we're adding one of our existing operands.
817 if (&Op >= Operands && &Op < Operands + NumOperands) {
818 // This is unusual: MI->addOperand(MI->getOperand(i)).
819 // If adding Op requires reallocating or moving existing operands around,
820 // the Op reference could go stale. Support it by copying Op.
821 MachineOperand CopyOp(Op);
822 return addOperand(MF, CopyOp);
823 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000824
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000825 // Find the insert location for the new operand. Implicit registers go at
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000826 // the end, everything else goes before the implicit regs.
827 //
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000828 // FIXME: Allow mixed explicit and implicit operands on inline asm.
829 // InstrEmitter::EmitSpecialNode() is marking inline asm clobbers as
830 // implicit-defs, but they must not be moved around. See the FIXME in
831 // InstrEmitter.cpp.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000832 unsigned OpNo = getNumOperands();
833 bool isImpReg = Op.isReg() && Op.isImplicit();
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000834 if (!isImpReg && !isInlineAsm()) {
835 while (OpNo && Operands[OpNo-1].isReg() && Operands[OpNo-1].isImplicit()) {
836 --OpNo;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000837 assert(!Operands[OpNo].isTied() && "Cannot move tied operands");
Chris Lattner961e7422008-01-01 01:12:31 +0000838 }
839 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000840
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000841#ifndef NDEBUG
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000842 bool isMetaDataOp = Op.getType() == MachineOperand::MO_Metadata;
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000843 // OpNo now points as the desired insertion point. Unless this is a variadic
844 // instruction, only implicit regs are allowed beyond MCID->getNumOperands().
Jakob Stoklund Olesenc300ef02012-07-04 23:53:23 +0000845 // RegMask operands go between the explicit and implicit operands.
846 assert((isImpReg || Op.isRegMask() || MCID->isVariadic() ||
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000847 OpNo < MCID->getNumOperands() || isMetaDataOp) &&
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000848 "Trying to add an operand to a machine instr that is already done!");
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000849#endif
Chris Lattner961e7422008-01-01 01:12:31 +0000850
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000851 MachineRegisterInfo *MRI = getRegInfo();
Chris Lattner961e7422008-01-01 01:12:31 +0000852
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000853 // Determine if the Operands array needs to be reallocated.
854 // Save the old capacity and operand array.
855 OperandCapacity OldCap = CapOperands;
856 MachineOperand *OldOperands = Operands;
857 if (!OldOperands || OldCap.getSize() == getNumOperands()) {
858 CapOperands = OldOperands ? OldCap.getNext() : OldCap.get(1);
859 Operands = MF.allocateOperandArray(CapOperands);
860 // Move the operands before the insertion point.
861 if (OpNo)
862 moveOperands(Operands, OldOperands, OpNo, MRI);
863 }
Chris Lattner961e7422008-01-01 01:12:31 +0000864
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000865 // Move the operands following the insertion point.
866 if (OpNo != NumOperands)
867 moveOperands(Operands + OpNo + 1, OldOperands + OpNo, NumOperands - OpNo,
868 MRI);
869 ++NumOperands;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000870
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000871 // Deallocate the old operand array.
872 if (OldOperands != Operands && OldOperands)
873 MF.deallocateOperandArray(OldCap, OldOperands);
874
875 // Copy Op into place. It still needs to be inserted into the MRI use lists.
876 MachineOperand *NewMO = new (Operands + OpNo) MachineOperand(Op);
877 NewMO->ParentMI = this;
878
879 // When adding a register operand, tell MRI about it.
880 if (NewMO->isReg()) {
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000881 // Ensure isOnRegUseList() returns false, regardless of Op's status.
Craig Topperc0196b12014-04-14 00:51:57 +0000882 NewMO->Contents.Reg.Prev = nullptr;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000883 // Ignore existing ties. This is not a property that can be copied.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000884 NewMO->TiedTo = 0;
885 // Add the new operand to MRI, but only for instructions in an MBB.
886 if (MRI)
887 MRI->addRegOperandToUseList(NewMO);
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000888 // The MCID operand information isn't accurate until we start adding
889 // explicit operands. The implicit operands are added first, then the
890 // explicits are inserted before them.
891 if (!isImpReg) {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000892 // Tie uses to defs as indicated in MCInstrDesc.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000893 if (NewMO->isUse()) {
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000894 int DefIdx = MCID->getOperandConstraint(OpNo, MCOI::TIED_TO);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +0000895 if (DefIdx != -1)
896 tieOperands(DefIdx, OpNo);
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000897 }
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000898 // If the register operand is flagged as early, mark the operand as such.
899 if (MCID->getOperandConstraint(OpNo, MCOI::EARLY_CLOBBER) != -1)
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000900 NewMO->setIsEarlyClobber(true);
Chris Lattner961e7422008-01-01 01:12:31 +0000901 }
Chris Lattner961e7422008-01-01 01:12:31 +0000902 }
903}
904
905/// RemoveOperand - Erase an operand from an instruction, leaving it with one
906/// fewer operand than it started with.
907///
908void MachineInstr::RemoveOperand(unsigned OpNo) {
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000909 assert(OpNo < getNumOperands() && "Invalid operand number");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000910 untieRegOperand(OpNo);
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000911
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000912#ifndef NDEBUG
913 // Moving tied operands would break the ties.
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000914 for (unsigned i = OpNo + 1, e = getNumOperands(); i != e; ++i)
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000915 if (Operands[i].isReg())
916 assert(!Operands[i].isTied() && "Cannot move tied operands");
917#endif
918
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000919 MachineRegisterInfo *MRI = getRegInfo();
920 if (MRI && Operands[OpNo].isReg())
921 MRI->removeRegOperandFromUseList(Operands + OpNo);
Chris Lattner961e7422008-01-01 01:12:31 +0000922
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000923 // Don't call the MachineOperand destructor. A lot of this code depends on
924 // MachineOperand having a trivial destructor anyway, and adding a call here
925 // wouldn't make it 'destructor-correct'.
926
927 if (unsigned N = NumOperands - 1 - OpNo)
928 moveOperands(Operands + OpNo, Operands + OpNo + 1, N, MRI);
929 --NumOperands;
Chris Lattner961e7422008-01-01 01:12:31 +0000930}
931
Dan Gohman48b185d2009-09-25 20:36:54 +0000932/// addMemOperand - Add a MachineMemOperand to the machine instruction.
933/// This function should be used only occasionally. The setMemRefs function
934/// is the primary method for setting up a MachineInstr's MemRefs list.
Dan Gohman3b460302008-07-07 23:14:23 +0000935void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohman48b185d2009-09-25 20:36:54 +0000936 MachineMemOperand *MO) {
937 mmo_iterator OldMemRefs = MemRefs;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000938 unsigned OldNumMemRefs = NumMemRefs;
Dan Gohman3b460302008-07-07 23:14:23 +0000939
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000940 unsigned NewNum = NumMemRefs + 1;
Dan Gohman48b185d2009-09-25 20:36:54 +0000941 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
Dan Gohman3b460302008-07-07 23:14:23 +0000942
Benjamin Kramerd03878b2012-03-16 16:39:27 +0000943 std::copy(OldMemRefs, OldMemRefs + OldNumMemRefs, NewMemRefs);
Dan Gohman48b185d2009-09-25 20:36:54 +0000944 NewMemRefs[NewNum - 1] = MO;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000945 setMemRefs(NewMemRefs, NewMemRefs + NewNum);
Dan Gohman48b185d2009-09-25 20:36:54 +0000946}
Chris Lattner961e7422008-01-01 01:12:31 +0000947
Philip Reames5eb90a72016-01-06 19:33:12 +0000948/// Check to see if the MMOs pointed to by the two MemRefs arrays are
Junmo Park820e3922016-02-26 02:07:36 +0000949/// identical.
Philip Reames5eb90a72016-01-06 19:33:12 +0000950static bool hasIdenticalMMOs(const MachineInstr &MI1, const MachineInstr &MI2) {
951 auto I1 = MI1.memoperands_begin(), E1 = MI1.memoperands_end();
952 auto I2 = MI2.memoperands_begin(), E2 = MI2.memoperands_end();
953 if ((E1 - I1) != (E2 - I2))
954 return false;
955 for (; I1 != E1; ++I1, ++I2) {
956 if (**I1 != **I2)
957 return false;
958 }
959 return true;
960}
961
Philip Reamesc86ed002016-01-06 04:39:03 +0000962std::pair<MachineInstr::mmo_iterator, unsigned>
963MachineInstr::mergeMemRefsWith(const MachineInstr& Other) {
Philip Reames5eb90a72016-01-06 19:33:12 +0000964
965 // If either of the incoming memrefs are empty, we must be conservative and
966 // treat this as if we've exhausted our space for memrefs and dropped them.
967 if (memoperands_empty() || Other.memoperands_empty())
968 return std::make_pair(nullptr, 0);
969
970 // If both instructions have identical memrefs, we don't need to merge them.
971 // Since many instructions have a single memref, and we tend to merge things
972 // like pairs of loads from the same location, this catches a large number of
973 // cases in practice.
974 if (hasIdenticalMMOs(*this, Other))
975 return std::make_pair(MemRefs, NumMemRefs);
Junmo Park820e3922016-02-26 02:07:36 +0000976
Philip Reamesc86ed002016-01-06 04:39:03 +0000977 // TODO: consider uniquing elements within the operand lists to reduce
978 // space usage and fall back to conservative information less often.
Philip Reames5eb90a72016-01-06 19:33:12 +0000979 size_t CombinedNumMemRefs = NumMemRefs + Other.NumMemRefs;
980
981 // If we don't have enough room to store this many memrefs, be conservative
982 // and drop them. Otherwise, we'd fail asserts when trying to add them to
983 // the new instruction.
984 if (CombinedNumMemRefs != uint8_t(CombinedNumMemRefs))
985 return std::make_pair(nullptr, 0);
Philip Reamesc86ed002016-01-06 04:39:03 +0000986
987 MachineFunction *MF = getParent()->getParent();
988 mmo_iterator MemBegin = MF->allocateMemRefsArray(CombinedNumMemRefs);
989 mmo_iterator MemEnd = std::copy(memoperands_begin(), memoperands_end(),
990 MemBegin);
991 MemEnd = std::copy(Other.memoperands_begin(), Other.memoperands_end(),
992 MemEnd);
Philip Reames2d2fc4a2016-01-06 05:53:09 +0000993 assert(MemEnd - MemBegin == (ptrdiff_t)CombinedNumMemRefs &&
994 "missing memrefs");
Junmo Park820e3922016-02-26 02:07:36 +0000995
Philip Reamesc86ed002016-01-06 04:39:03 +0000996 return std::make_pair(MemBegin, CombinedNumMemRefs);
997}
998
Benjamin Kramer97f889f2012-03-17 17:03:45 +0000999bool MachineInstr::hasPropertyInBundle(unsigned Mask, QueryType Type) const {
Jakob Stoklund Olesenf0615c72013-01-10 18:42:44 +00001000 assert(!isBundledWithPred() && "Must be called on bundle header");
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001001 for (MachineBasicBlock::const_instr_iterator MII = getIterator();; ++MII) {
Benjamin Kramer97f889f2012-03-17 17:03:45 +00001002 if (MII->getDesc().getFlags() & Mask) {
Evan Chengcdf89fd2011-12-08 19:23:10 +00001003 if (Type == AnyInBundle)
Evan Cheng7f8e5632011-12-07 07:15:52 +00001004 return true;
1005 } else {
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +00001006 if (Type == AllInBundle && !MII->isBundle())
Evan Cheng7f8e5632011-12-07 07:15:52 +00001007 return false;
1008 }
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +00001009 // This was the last instruction in the bundle.
1010 if (!MII->isBundledWithSucc())
1011 return Type == AllInBundle;
Evan Cheng2a81dd42011-12-06 22:12:01 +00001012 }
Evan Cheng2a81dd42011-12-06 22:12:01 +00001013}
1014
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001015bool MachineInstr::isIdenticalTo(const MachineInstr &Other,
Evan Chenge9c46c22010-03-03 01:44:33 +00001016 MICheckType Check) const {
Evan Cheng0f260e12010-03-03 21:54:14 +00001017 // If opcodes or number of operands are not the same then the two
1018 // instructions are obviously not identical.
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001019 if (Other.getOpcode() != getOpcode() ||
1020 Other.getNumOperands() != getNumOperands())
Evan Cheng0f260e12010-03-03 21:54:14 +00001021 return false;
1022
Evan Cheng7fae11b2011-12-14 02:11:42 +00001023 if (isBundle()) {
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +00001024 // We have passed the test above that both instructions have the same
1025 // opcode, so we know that both instructions are bundles here. Let's compare
1026 // MIs inside the bundle.
1027 assert(Other.isBundle() && "Expected that both instructions are bundles.");
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001028 MachineBasicBlock::const_instr_iterator I1 = getIterator();
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001029 MachineBasicBlock::const_instr_iterator I2 = Other.getIterator();
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +00001030 // Loop until we analysed the last intruction inside at least one of the
1031 // bundles.
1032 while (I1->isBundledWithSucc() && I2->isBundledWithSucc()) {
1033 ++I1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00001034 ++I2;
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +00001035 if (!I1->isIdenticalTo(*I2, Check))
Evan Cheng7fae11b2011-12-14 02:11:42 +00001036 return false;
1037 }
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +00001038 // If we've reached the end of just one of the two bundles, but not both,
1039 // the instructions are not identical.
1040 if (I1->isBundledWithSucc() || I2->isBundledWithSucc())
1041 return false;
Evan Cheng7fae11b2011-12-14 02:11:42 +00001042 }
1043
Evan Cheng0f260e12010-03-03 21:54:14 +00001044 // Check operands to make sure they match.
1045 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1046 const MachineOperand &MO = getOperand(i);
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001047 const MachineOperand &OMO = Other.getOperand(i);
Evan Chengcfdf3392011-05-12 00:56:58 +00001048 if (!MO.isReg()) {
1049 if (!MO.isIdenticalTo(OMO))
1050 return false;
1051 continue;
1052 }
1053
Evan Cheng0f260e12010-03-03 21:54:14 +00001054 // Clients may or may not want to ignore defs when testing for equality.
1055 // For example, machine CSE pass only cares about finding common
1056 // subexpressions, so it's safe to ignore virtual register defs.
Evan Chengcfdf3392011-05-12 00:56:58 +00001057 if (MO.isDef()) {
Evan Cheng0f260e12010-03-03 21:54:14 +00001058 if (Check == IgnoreDefs)
1059 continue;
Evan Chengcfdf3392011-05-12 00:56:58 +00001060 else if (Check == IgnoreVRegDefs) {
1061 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) ||
1062 TargetRegisterInfo::isPhysicalRegister(OMO.getReg()))
1063 if (MO.getReg() != OMO.getReg())
1064 return false;
1065 } else {
1066 if (!MO.isIdenticalTo(OMO))
Evan Cheng0f260e12010-03-03 21:54:14 +00001067 return false;
Evan Chengcfdf3392011-05-12 00:56:58 +00001068 if (Check == CheckKillDead && MO.isDead() != OMO.isDead())
1069 return false;
1070 }
1071 } else {
1072 if (!MO.isIdenticalTo(OMO))
1073 return false;
1074 if (Check == CheckKillDead && MO.isKill() != OMO.isKill())
1075 return false;
1076 }
Evan Cheng0f260e12010-03-03 21:54:14 +00001077 }
Devang Patelbf8cc602011-07-07 17:45:33 +00001078 // If DebugLoc does not match then two dbg.values are not identical.
1079 if (isDebugValue())
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001080 if (getDebugLoc() && Other.getDebugLoc() &&
1081 getDebugLoc() != Other.getDebugLoc())
Devang Patelbf8cc602011-07-07 17:45:33 +00001082 return false;
Evan Cheng0f260e12010-03-03 21:54:14 +00001083 return true;
Evan Chenge9c46c22010-03-03 01:44:33 +00001084}
1085
Chris Lattnerbec79b42006-04-17 21:35:41 +00001086MachineInstr *MachineInstr::removeFromParent() {
1087 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001088 return getParent()->remove(this);
Chris Lattnerbec79b42006-04-17 21:35:41 +00001089}
1090
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001091MachineInstr *MachineInstr::removeFromBundle() {
1092 assert(getParent() && "Not embedded in a basic block!");
1093 return getParent()->remove_instr(this);
1094}
Chris Lattnerbec79b42006-04-17 21:35:41 +00001095
Dan Gohman3b460302008-07-07 23:14:23 +00001096void MachineInstr::eraseFromParent() {
1097 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001098 getParent()->erase(this);
Dan Gohman3b460302008-07-07 23:14:23 +00001099}
1100
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +00001101void MachineInstr::eraseFromParentAndMarkDBGValuesForRemoval() {
1102 assert(getParent() && "Not embedded in a basic block!");
1103 MachineBasicBlock *MBB = getParent();
1104 MachineFunction *MF = MBB->getParent();
1105 assert(MF && "Not embedded in a function!");
1106
1107 MachineInstr *MI = (MachineInstr *)this;
1108 MachineRegisterInfo &MRI = MF->getRegInfo();
1109
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001110 for (const MachineOperand &MO : MI->operands()) {
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +00001111 if (!MO.isReg() || !MO.isDef())
1112 continue;
1113 unsigned Reg = MO.getReg();
1114 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1115 continue;
1116 MRI.markUsesInDebugValueAsUndef(Reg);
1117 }
1118 MI->eraseFromParent();
1119}
1120
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001121void MachineInstr::eraseFromBundle() {
1122 assert(getParent() && "Not embedded in a basic block!");
1123 getParent()->erase_instr(this);
1124}
Dan Gohman3b460302008-07-07 23:14:23 +00001125
Evan Cheng4d728b02007-05-15 01:26:09 +00001126/// getNumExplicitOperands - Returns the number of non-implicit operands.
1127///
1128unsigned MachineInstr::getNumExplicitOperands() const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001129 unsigned NumOperands = MCID->getNumOperands();
1130 if (!MCID->isVariadic())
Evan Cheng4d728b02007-05-15 01:26:09 +00001131 return NumOperands;
1132
Dan Gohman37608532009-04-15 17:59:11 +00001133 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
1134 const MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001135 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng4d728b02007-05-15 01:26:09 +00001136 NumOperands++;
1137 }
1138 return NumOperands;
1139}
1140
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001141void MachineInstr::bundleWithPred() {
1142 assert(!isBundledWithPred() && "MI is already bundled with its predecessor");
1143 setFlag(BundledPred);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001144 MachineBasicBlock::instr_iterator Pred = getIterator();
1145 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001146 assert(!Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001147 Pred->setFlag(BundledSucc);
1148}
1149
1150void MachineInstr::bundleWithSucc() {
1151 assert(!isBundledWithSucc() && "MI is already bundled with its successor");
1152 setFlag(BundledSucc);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001153 MachineBasicBlock::instr_iterator Succ = getIterator();
1154 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001155 assert(!Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001156 Succ->setFlag(BundledPred);
1157}
1158
1159void MachineInstr::unbundleFromPred() {
1160 assert(isBundledWithPred() && "MI isn't bundled with its predecessor");
1161 clearFlag(BundledPred);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001162 MachineBasicBlock::instr_iterator Pred = getIterator();
1163 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001164 assert(Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001165 Pred->clearFlag(BundledSucc);
1166}
1167
1168void MachineInstr::unbundleFromSucc() {
1169 assert(isBundledWithSucc() && "MI isn't bundled with its successor");
1170 clearFlag(BundledSucc);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001171 MachineBasicBlock::instr_iterator Succ = getIterator();
1172 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001173 assert(Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001174 Succ->clearFlag(BundledPred);
1175}
1176
Evan Cheng6eb516d2011-01-07 23:50:32 +00001177bool MachineInstr::isStackAligningInlineAsm() const {
1178 if (isInlineAsm()) {
1179 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1180 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1181 return true;
1182 }
1183 return false;
1184}
Chris Lattner33f5af02006-10-20 22:39:59 +00001185
Chad Rosier994f4042012-09-05 21:00:58 +00001186InlineAsm::AsmDialect MachineInstr::getInlineAsmDialect() const {
1187 assert(isInlineAsm() && "getInlineAsmDialect() only works for inline asms!");
1188 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
Chad Rosiere53314f2012-09-05 22:40:13 +00001189 return InlineAsm::AsmDialect((ExtraInfo & InlineAsm::Extra_AsmDialect) != 0);
Chad Rosier994f4042012-09-05 21:00:58 +00001190}
1191
Jakob Stoklund Olesen1e737162011-10-12 23:37:33 +00001192int MachineInstr::findInlineAsmFlagIdx(unsigned OpIdx,
1193 unsigned *GroupNo) const {
1194 assert(isInlineAsm() && "Expected an inline asm instruction");
1195 assert(OpIdx < getNumOperands() && "OpIdx out of range");
1196
1197 // Ignore queries about the initial operands.
1198 if (OpIdx < InlineAsm::MIOp_FirstOperand)
1199 return -1;
1200
1201 unsigned Group = 0;
1202 unsigned NumOps;
1203 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1204 i += NumOps) {
1205 const MachineOperand &FlagMO = getOperand(i);
1206 // If we reach the implicit register operands, stop looking.
1207 if (!FlagMO.isImm())
1208 return -1;
1209 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1210 if (i + NumOps > OpIdx) {
1211 if (GroupNo)
1212 *GroupNo = Group;
1213 return i;
1214 }
1215 ++Group;
1216 }
1217 return -1;
1218}
1219
Reid Kleckner28865802016-04-14 18:29:59 +00001220const DILocalVariable *MachineInstr::getDebugVariable() const {
1221 assert(isDebugValue() && "not a DBG_VALUE");
1222 return cast<DILocalVariable>(getOperand(2).getMetadata());
1223}
1224
1225const DIExpression *MachineInstr::getDebugExpression() const {
1226 assert(isDebugValue() && "not a DBG_VALUE");
1227 return cast<DIExpression>(getOperand(3).getMetadata());
1228}
1229
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001230const TargetRegisterClass*
1231MachineInstr::getRegClassConstraint(unsigned OpIdx,
1232 const TargetInstrInfo *TII,
1233 const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001234 assert(getParent() && "Can't have an MBB reference here!");
1235 assert(getParent()->getParent() && "Can't have an MF reference here!");
1236 const MachineFunction &MF = *getParent()->getParent();
1237
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001238 // Most opcodes have fixed constraints in their MCInstrDesc.
1239 if (!isInlineAsm())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001240 return TII->getRegClass(getDesc(), OpIdx, TRI, MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001241
1242 if (!getOperand(OpIdx).isReg())
Craig Topperc0196b12014-04-14 00:51:57 +00001243 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001244
1245 // For tied uses on inline asm, get the constraint from the def.
1246 unsigned DefIdx;
1247 if (getOperand(OpIdx).isUse() && isRegTiedToDefOperand(OpIdx, &DefIdx))
1248 OpIdx = DefIdx;
1249
1250 // Inline asm stores register class constraints in the flag word.
1251 int FlagIdx = findInlineAsmFlagIdx(OpIdx);
1252 if (FlagIdx < 0)
Craig Topperc0196b12014-04-14 00:51:57 +00001253 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001254
1255 unsigned Flag = getOperand(FlagIdx).getImm();
1256 unsigned RCID;
Simon Dardisd32a2d32016-07-18 13:17:31 +00001257 if ((InlineAsm::getKind(Flag) == InlineAsm::Kind_RegUse ||
1258 InlineAsm::getKind(Flag) == InlineAsm::Kind_RegDef ||
1259 InlineAsm::getKind(Flag) == InlineAsm::Kind_RegDefEarlyClobber) &&
1260 InlineAsm::hasRegClassConstraint(Flag, RCID))
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001261 return TRI->getRegClass(RCID);
1262
1263 // Assume that all registers in a memory operand are pointers.
1264 if (InlineAsm::getKind(Flag) == InlineAsm::Kind_Mem)
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001265 return TRI->getPointerRegClass(MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001266
Craig Topperc0196b12014-04-14 00:51:57 +00001267 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001268}
1269
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001270const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVReg(
1271 unsigned Reg, const TargetRegisterClass *CurRC, const TargetInstrInfo *TII,
1272 const TargetRegisterInfo *TRI, bool ExploreBundle) const {
1273 // Check every operands inside the bundle if we have
1274 // been asked to.
1275 if (ExploreBundle)
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001276 for (ConstMIBundleOperands OpndIt(*this); OpndIt.isValid() && CurRC;
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001277 ++OpndIt)
1278 CurRC = OpndIt->getParent()->getRegClassConstraintEffectForVRegImpl(
1279 OpndIt.getOperandNo(), Reg, CurRC, TII, TRI);
1280 else
1281 // Otherwise, just check the current operands.
Matthias Braune41e1462015-05-29 02:56:46 +00001282 for (unsigned i = 0, e = NumOperands; i < e && CurRC; ++i)
1283 CurRC = getRegClassConstraintEffectForVRegImpl(i, Reg, CurRC, TII, TRI);
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001284 return CurRC;
1285}
1286
1287const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVRegImpl(
1288 unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC,
1289 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
1290 assert(CurRC && "Invalid initial register class");
1291 // Check if Reg is constrained by some of its use/def from MI.
1292 const MachineOperand &MO = getOperand(OpIdx);
1293 if (!MO.isReg() || MO.getReg() != Reg)
1294 return CurRC;
1295 // If yes, accumulate the constraints through the operand.
1296 return getRegClassConstraintEffect(OpIdx, CurRC, TII, TRI);
1297}
1298
1299const TargetRegisterClass *MachineInstr::getRegClassConstraintEffect(
1300 unsigned OpIdx, const TargetRegisterClass *CurRC,
1301 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
1302 const TargetRegisterClass *OpRC = getRegClassConstraint(OpIdx, TII, TRI);
1303 const MachineOperand &MO = getOperand(OpIdx);
1304 assert(MO.isReg() &&
1305 "Cannot get register constraints for non-register operand");
1306 assert(CurRC && "Invalid initial register class");
1307 if (unsigned SubIdx = MO.getSubReg()) {
1308 if (OpRC)
1309 CurRC = TRI->getMatchingSuperRegClass(CurRC, OpRC, SubIdx);
1310 else
1311 CurRC = TRI->getSubClassWithSubReg(CurRC, SubIdx);
1312 } else if (OpRC)
1313 CurRC = TRI->getCommonSubClass(CurRC, OpRC);
1314 return CurRC;
1315}
1316
Jakob Stoklund Olesen68d752b2013-01-09 18:28:16 +00001317/// Return the number of instructions inside the MI bundle, not counting the
1318/// header instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +00001319unsigned MachineInstr::getBundleSize() const {
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001320 MachineBasicBlock::const_instr_iterator I = getIterator();
Evan Cheng7fae11b2011-12-14 02:11:42 +00001321 unsigned Size = 0;
Richard Trieu7a083812016-02-18 22:09:30 +00001322 while (I->isBundledWithSucc()) {
1323 ++Size;
1324 ++I;
1325 }
Evan Cheng7fae11b2011-12-14 02:11:42 +00001326 return Size;
1327}
1328
Nicolai Haehnleb0c97482016-04-22 04:04:08 +00001329/// Returns true if the MachineInstr has an implicit-use operand of exactly
1330/// the given register (not considering sub/super-registers).
1331bool MachineInstr::hasRegisterImplicitUseOperand(unsigned Reg) const {
1332 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1333 const MachineOperand &MO = getOperand(i);
1334 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == Reg)
1335 return true;
1336 }
1337 return false;
1338}
1339
Evan Cheng910c8082007-04-26 19:00:32 +00001340/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbach9632c142009-09-17 17:57:26 +00001341/// the specific register or -1 if it is not found. It further tightens
Evan Cheng9965aeb2007-02-23 01:04:26 +00001342/// the search criteria to a use that kills the register if isKill is true.
Fraser Cormack48d9fdc2016-10-11 09:09:21 +00001343int MachineInstr::findRegisterUseOperandIdx(
1344 unsigned Reg, bool isKill, const TargetRegisterInfo *TRI) const {
Evan Cheng75c21942006-12-06 08:27:42 +00001345 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng5983bdb2007-05-29 18:35:22 +00001346 const MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001347 if (!MO.isReg() || !MO.isUse())
Evan Cheng63254462008-03-05 00:59:57 +00001348 continue;
1349 unsigned MOReg = MO.getReg();
1350 if (!MOReg)
1351 continue;
Fraser Cormack48d9fdc2016-10-11 09:09:21 +00001352 if (MOReg == Reg || (TRI && TargetRegisterInfo::isPhysicalRegister(MOReg) &&
1353 TargetRegisterInfo::isPhysicalRegister(Reg) &&
1354 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng9965aeb2007-02-23 01:04:26 +00001355 if (!isKill || MO.isKill())
Evan Chengec3ac312007-03-26 22:37:45 +00001356 return i;
Evan Cheng75c21942006-12-06 08:27:42 +00001357 }
Evan Chengec3ac312007-03-26 22:37:45 +00001358 return -1;
Evan Cheng75c21942006-12-06 08:27:42 +00001359}
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001360
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001361/// readsWritesVirtualRegister - Return a pair of bools (reads, writes)
1362/// indicating if this instruction reads or writes Reg. This also considers
1363/// partial defines.
1364std::pair<bool,bool>
1365MachineInstr::readsWritesVirtualRegister(unsigned Reg,
1366 SmallVectorImpl<unsigned> *Ops) const {
1367 bool PartDef = false; // Partial redefine.
1368 bool FullDef = false; // Full define.
1369 bool Use = false;
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001370
1371 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1372 const MachineOperand &MO = getOperand(i);
1373 if (!MO.isReg() || MO.getReg() != Reg)
1374 continue;
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001375 if (Ops)
1376 Ops->push_back(i);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001377 if (MO.isUse())
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001378 Use |= !MO.isUndef();
Jakob Stoklund Olesen9eb77bf2011-08-19 00:30:17 +00001379 else if (MO.getSubReg() && !MO.isUndef())
1380 // A partial <def,undef> doesn't count as reading the register.
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001381 PartDef = true;
1382 else
1383 FullDef = true;
1384 }
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001385 // A partial redefine uses Reg unless there is also a full define.
1386 return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001387}
1388
Evan Cheng63254462008-03-05 00:59:57 +00001389/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman72a0bc12008-05-06 00:20:10 +00001390/// the specified register or -1 if it is not found. If isDead is true, defs
1391/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
1392/// also checks if there is a def of a super-register.
Evan Cheng38584512010-05-21 20:53:24 +00001393int
1394MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap,
1395 const TargetRegisterInfo *TRI) const {
1396 bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg);
Evan Chengf7ed82d2007-02-19 21:49:54 +00001397 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng63254462008-03-05 00:59:57 +00001398 const MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesene7d3f442012-02-14 23:49:37 +00001399 // Accept regmask operands when Overlap is set.
1400 // Ignore them when looking for a specific def operand (Overlap == false).
1401 if (isPhys && Overlap && MO.isRegMask() && MO.clobbersPhysReg(Reg))
1402 return i;
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001403 if (!MO.isReg() || !MO.isDef())
Evan Cheng63254462008-03-05 00:59:57 +00001404 continue;
1405 unsigned MOReg = MO.getReg();
Evan Cheng38584512010-05-21 20:53:24 +00001406 bool Found = (MOReg == Reg);
1407 if (!Found && TRI && isPhys &&
1408 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
1409 if (Overlap)
1410 Found = TRI->regsOverlap(MOReg, Reg);
1411 else
1412 Found = TRI->isSubRegister(MOReg, Reg);
1413 }
1414 if (Found && (!isDead || MO.isDead()))
1415 return i;
Evan Chengf7ed82d2007-02-19 21:49:54 +00001416 }
Evan Cheng63254462008-03-05 00:59:57 +00001417 return -1;
Evan Chengf7ed82d2007-02-19 21:49:54 +00001418}
Evan Cheng4d728b02007-05-15 01:26:09 +00001419
Evan Cheng5983bdb2007-05-29 18:35:22 +00001420/// findFirstPredOperandIdx() - Find the index of the first operand in the
1421/// operand list that is used to represent the predicate. It returns -1 if
1422/// none is found.
1423int MachineInstr::findFirstPredOperandIdx() const {
Jim Grosbached16ec42011-08-29 22:24:09 +00001424 // Don't call MCID.findFirstPredOperandIdx() because this variant
1425 // is sometimes called on an instruction that's not yet complete, and
1426 // so the number of operands is less than the MCID indicates. In
1427 // particular, the PTX target does this.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001428 const MCInstrDesc &MCID = getDesc();
1429 if (MCID.isPredicable()) {
Evan Cheng4d728b02007-05-15 01:26:09 +00001430 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Evan Cheng6cc775f2011-06-28 19:10:37 +00001431 if (MCID.OpInfo[i].isPredicate())
Evan Cheng5983bdb2007-05-29 18:35:22 +00001432 return i;
Evan Cheng4d728b02007-05-15 01:26:09 +00001433 }
1434
Evan Cheng5983bdb2007-05-29 18:35:22 +00001435 return -1;
Evan Cheng4d728b02007-05-15 01:26:09 +00001436}
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001437
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001438// MachineOperand::TiedTo is 4 bits wide.
1439const unsigned TiedMax = 15;
1440
1441/// tieOperands - Mark operands at DefIdx and UseIdx as tied to each other.
1442///
1443/// Use and def operands can be tied together, indicated by a non-zero TiedTo
1444/// field. TiedTo can have these values:
1445///
1446/// 0: Operand is not tied to anything.
1447/// 1 to TiedMax-1: Tied to getOperand(TiedTo-1).
1448/// TiedMax: Tied to an operand >= TiedMax-1.
1449///
1450/// The tied def must be one of the first TiedMax operands on a normal
1451/// instruction. INLINEASM instructions allow more tied defs.
1452///
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001453void MachineInstr::tieOperands(unsigned DefIdx, unsigned UseIdx) {
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001454 MachineOperand &DefMO = getOperand(DefIdx);
1455 MachineOperand &UseMO = getOperand(UseIdx);
1456 assert(DefMO.isDef() && "DefIdx must be a def operand");
1457 assert(UseMO.isUse() && "UseIdx must be a use operand");
1458 assert(!DefMO.isTied() && "Def is already tied to another use");
1459 assert(!UseMO.isTied() && "Use is already tied to another def");
1460
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001461 if (DefIdx < TiedMax)
1462 UseMO.TiedTo = DefIdx + 1;
1463 else {
1464 // Inline asm can use the group descriptors to find tied operands, but on
1465 // normal instruction, the tied def must be within the first TiedMax
1466 // operands.
1467 assert(isInlineAsm() && "DefIdx out of range");
1468 UseMO.TiedTo = TiedMax;
1469 }
1470
1471 // UseIdx can be out of range, we'll search for it in findTiedOperandIdx().
1472 DefMO.TiedTo = std::min(UseIdx + 1, TiedMax);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001473}
1474
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001475/// Given the index of a tied register operand, find the operand it is tied to.
1476/// Defs are tied to uses and vice versa. Returns the index of the tied operand
1477/// which must exist.
1478unsigned MachineInstr::findTiedOperandIdx(unsigned OpIdx) const {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001479 const MachineOperand &MO = getOperand(OpIdx);
1480 assert(MO.isTied() && "Operand isn't tied");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001481
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001482 // Normally TiedTo is in range.
1483 if (MO.TiedTo < TiedMax)
1484 return MO.TiedTo - 1;
1485
1486 // Uses on normal instructions can be out of range.
1487 if (!isInlineAsm()) {
1488 // Normal tied defs must be in the 0..TiedMax-1 range.
1489 if (MO.isUse())
1490 return TiedMax - 1;
1491 // MO is a def. Search for the tied use.
1492 for (unsigned i = TiedMax - 1, e = getNumOperands(); i != e; ++i) {
1493 const MachineOperand &UseMO = getOperand(i);
1494 if (UseMO.isReg() && UseMO.isUse() && UseMO.TiedTo == OpIdx + 1)
1495 return i;
1496 }
1497 llvm_unreachable("Can't find tied use");
1498 }
1499
1500 // Now deal with inline asm by parsing the operand group descriptor flags.
1501 // Find the beginning of each operand group.
1502 SmallVector<unsigned, 8> GroupIdx;
1503 unsigned OpIdxGroup = ~0u;
1504 unsigned NumOps;
1505 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1506 i += NumOps) {
1507 const MachineOperand &FlagMO = getOperand(i);
1508 assert(FlagMO.isImm() && "Invalid tied operand on inline asm");
1509 unsigned CurGroup = GroupIdx.size();
1510 GroupIdx.push_back(i);
1511 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1512 // OpIdx belongs to this operand group.
1513 if (OpIdx > i && OpIdx < i + NumOps)
1514 OpIdxGroup = CurGroup;
1515 unsigned TiedGroup;
1516 if (!InlineAsm::isUseOperandTiedToDef(FlagMO.getImm(), TiedGroup))
1517 continue;
1518 // Operands in this group are tied to operands in TiedGroup which must be
1519 // earlier. Find the number of operands between the two groups.
1520 unsigned Delta = i - GroupIdx[TiedGroup];
1521
1522 // OpIdx is a use tied to TiedGroup.
1523 if (OpIdxGroup == CurGroup)
1524 return OpIdx - Delta;
1525
1526 // OpIdx is a def tied to this use group.
1527 if (OpIdxGroup == TiedGroup)
1528 return OpIdx + Delta;
1529 }
1530 llvm_unreachable("Invalid tied operand on inline asm");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001531}
1532
Dan Gohmanc90f51c2010-05-13 20:34:42 +00001533/// clearKillInfo - Clears kill flags on all operands.
1534///
1535void MachineInstr::clearKillInfo() {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001536 for (MachineOperand &MO : operands()) {
Dan Gohmanc90f51c2010-05-13 20:34:42 +00001537 if (MO.isReg() && MO.isUse())
1538 MO.setIsKill(false);
1539 }
1540}
1541
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001542void MachineInstr::substituteRegister(unsigned FromReg,
1543 unsigned ToReg,
1544 unsigned SubIdx,
1545 const TargetRegisterInfo &RegInfo) {
1546 if (TargetRegisterInfo::isPhysicalRegister(ToReg)) {
1547 if (SubIdx)
1548 ToReg = RegInfo.getSubReg(ToReg, SubIdx);
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001549 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001550 if (!MO.isReg() || MO.getReg() != FromReg)
1551 continue;
1552 MO.substPhysReg(ToReg, RegInfo);
1553 }
1554 } else {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001555 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001556 if (!MO.isReg() || MO.getReg() != FromReg)
1557 continue;
1558 MO.substVirtReg(ToReg, SubIdx, RegInfo);
1559 }
1560 }
1561}
1562
Evan Cheng7d98a482008-07-03 09:09:37 +00001563/// isSafeToMove - Return true if it is safe to move this instruction. If
1564/// SawStore is set to true, it means that there is a store (or call) between
1565/// the instruction's location and its intended destination.
Matthias Braun07066cc2015-05-19 21:22:20 +00001566bool MachineInstr::isSafeToMove(AliasAnalysis *AA, bool &SawStore) const {
Evan Cheng399e1102008-03-13 00:44:09 +00001567 // Ignore stuff that we obviously can't move.
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001568 //
1569 // Treat volatile loads as stores. This is not strictly necessary for
Jakob Stoklund Olesend92e2bc2012-09-04 18:44:43 +00001570 // volatiles, but it is required for atomic loads. It is not allowed to move
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001571 // a load across an atomic load with Ordering > Monotonic.
1572 if (mayStore() || isCall() ||
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001573 (mayLoad() && hasOrderedMemoryRef())) {
Evan Cheng399e1102008-03-13 00:44:09 +00001574 SawStore = true;
1575 return false;
1576 }
Evan Cheng0638c202011-01-07 21:08:26 +00001577
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001578 if (isPosition() || isDebugValue() || isTerminator() ||
1579 hasUnmodeledSideEffects())
Evan Cheng399e1102008-03-13 00:44:09 +00001580 return false;
1581
1582 // See if this instruction does a load. If so, we have to guarantee that the
1583 // loaded value doesn't change between the load and the its intended
1584 // destination. The check for isInvariantLoad gives the targe the chance to
1585 // classify the load as always returning a constant, e.g. a constant pool
1586 // load.
Justin Lebard98cf002016-09-10 01:03:20 +00001587 if (mayLoad() && !isDereferenceableInvariantLoad(AA))
Evan Cheng399e1102008-03-13 00:44:09 +00001588 // Otherwise, this is a real load. If there is a store between the load and
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001589 // end of block, we can't move it.
1590 return !SawStore;
Dan Gohman7c59ed62008-09-24 00:06:15 +00001591
Evan Cheng399e1102008-03-13 00:44:09 +00001592 return true;
1593}
1594
Eli Friedman93f47e52017-03-09 23:33:36 +00001595bool MachineInstr::mayAlias(AliasAnalysis *AA, MachineInstr &Other,
1596 bool UseTBAA) {
1597 const MachineFunction *MF = getParent()->getParent();
1598 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1599
1600 // If neither instruction stores to memory, they can't alias in any
1601 // meaningful way, even if they read from the same address.
1602 if (!mayStore() && !Other.mayStore())
1603 return false;
1604
1605 // Let the target decide if memory accesses cannot possibly overlap.
1606 if (TII->areMemAccessesTriviallyDisjoint(*this, Other, AA))
1607 return false;
1608
1609 if (!AA)
1610 return true;
1611
1612 // FIXME: Need to handle multiple memory operands to support all targets.
1613 if (!hasOneMemOperand() || !Other.hasOneMemOperand())
1614 return true;
1615
1616 MachineMemOperand *MMOa = *memoperands_begin();
1617 MachineMemOperand *MMOb = *Other.memoperands_begin();
1618
1619 if (!MMOa->getValue() || !MMOb->getValue())
1620 return true;
1621
1622 // The following interface to AA is fashioned after DAGCombiner::isAlias
1623 // and operates with MachineMemOperand offset with some important
1624 // assumptions:
1625 // - LLVM fundamentally assumes flat address spaces.
1626 // - MachineOperand offset can *only* result from legalization and
1627 // cannot affect queries other than the trivial case of overlap
1628 // checking.
1629 // - These offsets never wrap and never step outside
1630 // of allocated objects.
1631 // - There should never be any negative offsets here.
1632 //
1633 // FIXME: Modify API to hide this math from "user"
1634 // FIXME: Even before we go to AA we can reason locally about some
1635 // memory objects. It can save compile time, and possibly catch some
1636 // corner cases not currently covered.
1637
1638 assert ((MMOa->getOffset() >= 0) && "Negative MachineMemOperand offset");
1639 assert ((MMOb->getOffset() >= 0) && "Negative MachineMemOperand offset");
1640
1641 int64_t MinOffset = std::min(MMOa->getOffset(), MMOb->getOffset());
1642 int64_t Overlapa = MMOa->getSize() + MMOa->getOffset() - MinOffset;
1643 int64_t Overlapb = MMOb->getSize() + MMOb->getOffset() - MinOffset;
1644
1645 AliasResult AAResult =
1646 AA->alias(MemoryLocation(MMOa->getValue(), Overlapa,
1647 UseTBAA ? MMOa->getAAInfo() : AAMDNodes()),
1648 MemoryLocation(MMOb->getValue(), Overlapb,
1649 UseTBAA ? MMOb->getAAInfo() : AAMDNodes()));
1650
1651 return (AAResult != NoAlias);
1652}
1653
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001654/// hasOrderedMemoryRef - Return true if this instruction may have an ordered
1655/// or volatile memory reference, or if the information describing the memory
1656/// reference is not available. Return false if it is known to have no ordered
1657/// memory references.
1658bool MachineInstr::hasOrderedMemoryRef() const {
Dan Gohman7c59ed62008-09-24 00:06:15 +00001659 // An instruction known never to access memory won't have a volatile access.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001660 if (!mayStore() &&
1661 !mayLoad() &&
1662 !isCall() &&
Evan Cheng6eb516d2011-01-07 23:50:32 +00001663 !hasUnmodeledSideEffects())
Dan Gohman7c59ed62008-09-24 00:06:15 +00001664 return false;
1665
1666 // Otherwise, if the instruction has no memory reference information,
1667 // conservatively assume it wasn't preserved.
1668 if (memoperands_empty())
1669 return true;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001670
Justin Lebardede81e2016-07-13 22:35:19 +00001671 // Check if any of our memory operands are ordered.
1672 return any_of(memoperands(), [](const MachineMemOperand *MMO) {
1673 return !MMO->isUnordered();
1674 });
Dan Gohman7c59ed62008-09-24 00:06:15 +00001675}
1676
Justin Lebard98cf002016-09-10 01:03:20 +00001677/// isDereferenceableInvariantLoad - Return true if this instruction will never
1678/// trap and is loading from a location whose value is invariant across a run of
1679/// this function.
1680bool MachineInstr::isDereferenceableInvariantLoad(AliasAnalysis *AA) const {
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001681 // If the instruction doesn't load at all, it isn't an invariant load.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001682 if (!mayLoad())
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001683 return false;
1684
1685 // If the instruction has lost its memoperands, conservatively assume that
1686 // it may not be an invariant load.
1687 if (memoperands_empty())
1688 return false;
1689
Matthias Braun941a7052016-07-28 18:40:00 +00001690 const MachineFrameInfo &MFI = getParent()->getParent()->getFrameInfo();
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001691
Justin Lebardede81e2016-07-13 22:35:19 +00001692 for (MachineMemOperand *MMO : memoperands()) {
1693 if (MMO->isVolatile()) return false;
1694 if (MMO->isStore()) return false;
Justin Lebaradbf09e2016-09-11 01:38:58 +00001695 if (MMO->isInvariant() && MMO->isDereferenceable())
1696 continue;
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001697
1698 // A load from a constant PseudoSourceValue is invariant.
Justin Lebardede81e2016-07-13 22:35:19 +00001699 if (const PseudoSourceValue *PSV = MMO->getPseudoValue())
Matthias Braun941a7052016-07-28 18:40:00 +00001700 if (PSV->isConstant(&MFI))
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001701 continue;
1702
Justin Lebardede81e2016-07-13 22:35:19 +00001703 if (const Value *V = MMO->getValue()) {
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001704 // If we have an AliasAnalysis, ask it whether the memory is constant.
Chandler Carruthac80dc72015-06-17 07:18:54 +00001705 if (AA &&
1706 AA->pointsToConstantMemory(
Justin Lebardede81e2016-07-13 22:35:19 +00001707 MemoryLocation(V, MMO->getSize(), MMO->getAAInfo())))
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001708 continue;
1709 }
1710
1711 // Otherwise assume conservatively.
1712 return false;
1713 }
1714
1715 // Everything checks out.
1716 return true;
1717}
1718
Evan Cheng71453822009-12-03 02:31:43 +00001719/// isConstantValuePHI - If the specified instruction is a PHI that always
1720/// merges together the same virtual register, return the register, otherwise
1721/// return 0.
1722unsigned MachineInstr::isConstantValuePHI() const {
Chris Lattnerb06015a2010-02-09 19:54:29 +00001723 if (!isPHI())
Evan Cheng71453822009-12-03 02:31:43 +00001724 return 0;
Evan Cheng5c668a22009-12-07 23:10:34 +00001725 assert(getNumOperands() >= 3 &&
1726 "It's illegal to have a PHI without source operands");
Evan Cheng71453822009-12-03 02:31:43 +00001727
1728 unsigned Reg = getOperand(1).getReg();
1729 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
1730 if (getOperand(i).getReg() != Reg)
1731 return 0;
1732 return Reg;
1733}
1734
Evan Cheng6eb516d2011-01-07 23:50:32 +00001735bool MachineInstr::hasUnmodeledSideEffects() const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00001736 if (hasProperty(MCID::UnmodeledSideEffects))
Evan Cheng6eb516d2011-01-07 23:50:32 +00001737 return true;
1738 if (isInlineAsm()) {
1739 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1740 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1741 return true;
1742 }
1743
1744 return false;
1745}
1746
Michael Kupersteinbc7f99a2015-08-12 10:14:58 +00001747bool MachineInstr::isLoadFoldBarrier() const {
1748 return mayStore() || isCall() || hasUnmodeledSideEffects();
1749}
1750
Evan Chengb083c472010-04-08 20:02:37 +00001751/// allDefsAreDead - Return true if all the defs of this instruction are dead.
1752///
1753bool MachineInstr::allDefsAreDead() const {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001754 for (const MachineOperand &MO : operands()) {
Evan Chengb083c472010-04-08 20:02:37 +00001755 if (!MO.isReg() || MO.isUse())
1756 continue;
1757 if (!MO.isDead())
1758 return false;
1759 }
1760 return true;
1761}
1762
Evan Cheng21eedfb2010-10-22 21:49:09 +00001763/// copyImplicitOps - Copy implicit register operands from specified
1764/// instruction to this instruction.
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001765void MachineInstr::copyImplicitOps(MachineFunction &MF,
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001766 const MachineInstr &MI) {
1767 for (unsigned i = MI.getDesc().getNumOperands(), e = MI.getNumOperands();
Evan Cheng21eedfb2010-10-22 21:49:09 +00001768 i != e; ++i) {
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001769 const MachineOperand &MO = MI.getOperand(i);
Lang Hames7c8189c2014-03-17 01:22:54 +00001770 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001771 addOperand(MF, MO);
Evan Cheng21eedfb2010-10-22 21:49:09 +00001772 }
1773}
1774
Manman Ren19f49ac2012-09-11 22:23:19 +00001775#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Brauna4976c62017-01-29 18:20:42 +00001776LLVM_DUMP_METHOD void MachineInstr::dump() const {
Sebastian Pop77794842016-12-21 01:41:12 +00001777 dbgs() << " ";
Matthias Brauna4976c62017-01-29 18:20:42 +00001778 print(dbgs());
Mon P Wangdfcc1ff2008-10-10 01:43:55 +00001779}
Matthias Braun8c209aa2017-01-28 02:02:38 +00001780#endif
Mon P Wangdfcc1ff2008-10-10 01:43:55 +00001781
Ahmed Bougacha43192242017-02-23 19:17:31 +00001782void MachineInstr::print(raw_ostream &OS, bool SkipOpers, bool SkipDebugLoc,
Sebastian Pop77794842016-12-21 01:41:12 +00001783 const TargetInstrInfo *TII) const {
Duncan P. N. Exon Smithc0374522015-06-26 23:18:44 +00001784 const Module *M = nullptr;
1785 if (const MachineBasicBlock *MBB = getParent())
1786 if (const MachineFunction *MF = MBB->getParent())
1787 M = MF->getFunction()->getParent();
1788
1789 ModuleSlotTracker MST(M);
Ahmed Bougacha43192242017-02-23 19:17:31 +00001790 print(OS, MST, SkipOpers, SkipDebugLoc, TII);
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001791}
1792
1793void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
Ahmed Bougacha43192242017-02-23 19:17:31 +00001794 bool SkipOpers, bool SkipDebugLoc,
1795 const TargetInstrInfo *TII) const {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001796 // We can be a bit tidier if we know the MachineFunction.
Craig Topperc0196b12014-04-14 00:51:57 +00001797 const MachineFunction *MF = nullptr;
Eric Christopher1cdefae2015-02-27 00:11:34 +00001798 const TargetRegisterInfo *TRI = nullptr;
Craig Topperc0196b12014-04-14 00:51:57 +00001799 const MachineRegisterInfo *MRI = nullptr;
Tim Northover6b3bd612016-07-29 20:32:59 +00001800 const TargetIntrinsicInfo *IntrinsicInfo = nullptr;
1801
Dan Gohman2745d192009-11-09 19:38:45 +00001802 if (const MachineBasicBlock *MBB = getParent()) {
1803 MF = MBB->getParent();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001804 if (MF) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001805 MRI = &MF->getRegInfo();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001806 TRI = MF->getSubtarget().getRegisterInfo();
Sebastian Pop77794842016-12-21 01:41:12 +00001807 if (!TII)
1808 TII = MF->getSubtarget().getInstrInfo();
Tim Northover6b3bd612016-07-29 20:32:59 +00001809 IntrinsicInfo = MF->getTarget().getIntrinsicInfo();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001810 }
Dan Gohman2745d192009-11-09 19:38:45 +00001811 }
Dan Gohman34341e62009-10-31 20:19:03 +00001812
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001813 // Save a list of virtual registers.
1814 SmallVector<unsigned, 8> VirtRegs;
1815
Dan Gohman34341e62009-10-31 20:19:03 +00001816 // Print explicitly defined operands on the left of an assignment syntax.
Dan Gohman2745d192009-11-09 19:38:45 +00001817 unsigned StartOp = 0, e = getNumOperands();
Dan Gohman34341e62009-10-31 20:19:03 +00001818 for (; StartOp < e && getOperand(StartOp).isReg() &&
1819 getOperand(StartOp).isDef() &&
1820 !getOperand(StartOp).isImplicit();
1821 ++StartOp) {
1822 if (StartOp != 0) OS << ", ";
Tim Northover6b3bd612016-07-29 20:32:59 +00001823 getOperand(StartOp).print(OS, MST, TRI, IntrinsicInfo);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001824 unsigned Reg = getOperand(StartOp).getReg();
Quentin Colombet36ce1b02016-02-10 23:43:48 +00001825 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001826 VirtRegs.push_back(Reg);
Tim Northover0f140c72016-09-09 11:46:34 +00001827 LLT Ty = MRI ? MRI->getType(Reg) : LLT{};
1828 if (Ty.isValid())
1829 OS << '(' << Ty << ')';
Quentin Colombet36ce1b02016-02-10 23:43:48 +00001830 }
Chris Lattnerac6e9742002-10-30 01:55:38 +00001831 }
Tanya Lattner23dbc812004-06-25 00:13:11 +00001832
Dan Gohman34341e62009-10-31 20:19:03 +00001833 if (StartOp != 0)
1834 OS << " = ";
1835
1836 // Print the opcode name.
Eric Christopher1cdefae2015-02-27 00:11:34 +00001837 if (TII)
1838 OS << TII->getName(getOpcode());
Benjamin Kramerbf152d52012-02-10 13:18:44 +00001839 else
1840 OS << "UNKNOWN";
Misha Brukman835702a2005-04-21 22:36:52 +00001841
Andrew Trickb36388a2013-01-25 07:45:25 +00001842 if (SkipOpers)
1843 return;
1844
Dan Gohman34341e62009-10-31 20:19:03 +00001845 // Print the rest of the operands.
Dan Gohman2745d192009-11-09 19:38:45 +00001846 bool OmittedAnyCallClobbers = false;
1847 bool FirstOp = true;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001848 unsigned AsmDescOp = ~0u;
1849 unsigned AsmOpCount = 0;
Evan Cheng6eb516d2011-01-07 23:50:32 +00001850
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +00001851 if (isInlineAsm() && e >= InlineAsm::MIOp_FirstOperand) {
Evan Cheng6eb516d2011-01-07 23:50:32 +00001852 // Print asm string.
1853 OS << " ";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001854 getOperand(InlineAsm::MIOp_AsmString).print(OS, MST, TRI);
Evan Cheng6eb516d2011-01-07 23:50:32 +00001855
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001856 // Print HasSideEffects, MayLoad, MayStore, IsAlignStack
Evan Cheng6eb516d2011-01-07 23:50:32 +00001857 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1858 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1859 OS << " [sideeffect]";
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001860 if (ExtraInfo & InlineAsm::Extra_MayLoad)
1861 OS << " [mayload]";
1862 if (ExtraInfo & InlineAsm::Extra_MayStore)
1863 OS << " [maystore]";
Wei Ding0526e7f2016-06-22 18:51:08 +00001864 if (ExtraInfo & InlineAsm::Extra_IsConvergent)
1865 OS << " [isconvergent]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00001866 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1867 OS << " [alignstack]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00001868 if (getInlineAsmDialect() == InlineAsm::AD_ATT)
Chad Rosier994f4042012-09-05 21:00:58 +00001869 OS << " [attdialect]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00001870 if (getInlineAsmDialect() == InlineAsm::AD_Intel)
Chad Rosier994f4042012-09-05 21:00:58 +00001871 OS << " [inteldialect]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00001872
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001873 StartOp = AsmDescOp = InlineAsm::MIOp_FirstOperand;
Evan Cheng6eb516d2011-01-07 23:50:32 +00001874 FirstOp = false;
1875 }
1876
Chris Lattnerac6e9742002-10-30 01:55:38 +00001877 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohman2745d192009-11-09 19:38:45 +00001878 const MachineOperand &MO = getOperand(i);
1879
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001880 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001881 VirtRegs.push_back(MO.getReg());
1882
Dan Gohman2745d192009-11-09 19:38:45 +00001883 // Omit call-clobbered registers which aren't used anywhere. This makes
1884 // call instructions much less noisy on targets where calls clobber lots
1885 // of registers. Don't rely on MO.isDead() because we may be called before
1886 // LiveVariables is run, or we may be looking at a non-allocatable reg.
Craig Toppercf0444b2014-11-17 05:50:14 +00001887 if (MRI && isCall() &&
Dan Gohman2745d192009-11-09 19:38:45 +00001888 MO.isReg() && MO.isImplicit() && MO.isDef()) {
1889 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001890 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Craig Toppercf0444b2014-11-17 05:50:14 +00001891 if (MRI->use_empty(Reg)) {
Dan Gohman2745d192009-11-09 19:38:45 +00001892 bool HasAliasLive = false;
Eric Christopher1cdefae2015-02-27 00:11:34 +00001893 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001894 unsigned AliasReg = *AI;
Craig Toppercf0444b2014-11-17 05:50:14 +00001895 if (!MRI->use_empty(AliasReg)) {
Dan Gohman2745d192009-11-09 19:38:45 +00001896 HasAliasLive = true;
1897 break;
1898 }
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001899 }
Dan Gohman2745d192009-11-09 19:38:45 +00001900 if (!HasAliasLive) {
1901 OmittedAnyCallClobbers = true;
1902 continue;
1903 }
1904 }
1905 }
1906 }
1907
1908 if (FirstOp) FirstOp = false; else OS << ",";
Chris Lattnerac6e9742002-10-30 01:55:38 +00001909 OS << " ";
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001910 if (i < getDesc().NumOperands) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001911 const MCOperandInfo &MCOI = getDesc().OpInfo[i];
1912 if (MCOI.isPredicate())
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001913 OS << "pred:";
Evan Cheng6cc775f2011-06-28 19:10:37 +00001914 if (MCOI.isOptionalDef())
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001915 OS << "opt:";
1916 }
Evan Chengd4d1a512010-04-28 20:03:13 +00001917 if (isDebugValue() && MO.isMetadata()) {
1918 // Pretty print DBG_VALUE instructions.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001919 auto *DIV = dyn_cast<DILocalVariable>(MO.getMetadata());
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +00001920 if (DIV && !DIV->getName().empty())
1921 OS << "!\"" << DIV->getName() << '\"';
Evan Chengd4d1a512010-04-28 20:03:13 +00001922 else
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001923 MO.print(OS, MST, TRI);
Matthias Brauna3743082017-01-09 21:38:10 +00001924 } else if (TRI && (isInsertSubreg() || isRegSequence() ||
1925 (isSubregToReg() && i == 3)) && MO.isImm()) {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001926 OS << TRI->getSubRegIndexName(MO.getImm());
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001927 } else if (i == AsmDescOp && MO.isImm()) {
1928 // Pretty print the inline asm operand descriptor.
1929 OS << '$' << AsmOpCount++;
1930 unsigned Flag = MO.getImm();
1931 switch (InlineAsm::getKind(Flag)) {
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001932 case InlineAsm::Kind_RegUse: OS << ":[reguse"; break;
1933 case InlineAsm::Kind_RegDef: OS << ":[regdef"; break;
1934 case InlineAsm::Kind_RegDefEarlyClobber: OS << ":[regdef-ec"; break;
1935 case InlineAsm::Kind_Clobber: OS << ":[clobber"; break;
1936 case InlineAsm::Kind_Imm: OS << ":[imm"; break;
1937 case InlineAsm::Kind_Mem: OS << ":[mem"; break;
1938 default: OS << ":[??" << InlineAsm::getKind(Flag); break;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001939 }
1940
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001941 unsigned RCID = 0;
Simon Dardisd32a2d32016-07-18 13:17:31 +00001942 if (!InlineAsm::isImmKind(Flag) && !InlineAsm::isMemKind(Flag) &&
1943 InlineAsm::hasRegClassConstraint(Flag, RCID)) {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001944 if (TRI) {
1945 OS << ':' << TRI->getRegClassName(TRI->getRegClass(RCID));
Craig Toppercf0444b2014-11-17 05:50:14 +00001946 } else
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001947 OS << ":RC" << RCID;
Nick Lewycky84882252011-10-13 00:54:59 +00001948 }
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001949
Simon Dardisd32a2d32016-07-18 13:17:31 +00001950 if (InlineAsm::isMemKind(Flag)) {
1951 unsigned MCID = InlineAsm::getMemoryConstraintID(Flag);
1952 switch (MCID) {
1953 case InlineAsm::Constraint_es: OS << ":es"; break;
1954 case InlineAsm::Constraint_i: OS << ":i"; break;
1955 case InlineAsm::Constraint_m: OS << ":m"; break;
1956 case InlineAsm::Constraint_o: OS << ":o"; break;
1957 case InlineAsm::Constraint_v: OS << ":v"; break;
1958 case InlineAsm::Constraint_Q: OS << ":Q"; break;
1959 case InlineAsm::Constraint_R: OS << ":R"; break;
1960 case InlineAsm::Constraint_S: OS << ":S"; break;
1961 case InlineAsm::Constraint_T: OS << ":T"; break;
1962 case InlineAsm::Constraint_Um: OS << ":Um"; break;
1963 case InlineAsm::Constraint_Un: OS << ":Un"; break;
1964 case InlineAsm::Constraint_Uq: OS << ":Uq"; break;
1965 case InlineAsm::Constraint_Us: OS << ":Us"; break;
1966 case InlineAsm::Constraint_Ut: OS << ":Ut"; break;
1967 case InlineAsm::Constraint_Uv: OS << ":Uv"; break;
1968 case InlineAsm::Constraint_Uy: OS << ":Uy"; break;
1969 case InlineAsm::Constraint_X: OS << ":X"; break;
1970 case InlineAsm::Constraint_Z: OS << ":Z"; break;
1971 case InlineAsm::Constraint_ZC: OS << ":ZC"; break;
1972 case InlineAsm::Constraint_Zy: OS << ":Zy"; break;
1973 default: OS << ":?"; break;
1974 }
1975 }
1976
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001977 unsigned TiedTo = 0;
1978 if (InlineAsm::isUseOperandTiedToDef(Flag, TiedTo))
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001979 OS << " tiedto:$" << TiedTo;
1980
1981 OS << ']';
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001982
1983 // Compute the index of the next operand descriptor.
1984 AsmDescOp += 1 + InlineAsm::getNumOperandRegisters(Flag);
Evan Chengd4d1a512010-04-28 20:03:13 +00001985 } else
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001986 MO.print(OS, MST, TRI);
Dan Gohman2745d192009-11-09 19:38:45 +00001987 }
1988
1989 // Briefly indicate whether any call clobbers were omitted.
1990 if (OmittedAnyCallClobbers) {
Bill Wendlingec030f22009-12-25 13:45:50 +00001991 if (!FirstOp) OS << ",";
Dan Gohman2745d192009-11-09 19:38:45 +00001992 OS << " ...";
Chris Lattner214808f2002-10-30 00:48:05 +00001993 }
Misha Brukman835702a2005-04-21 22:36:52 +00001994
Dan Gohman34341e62009-10-31 20:19:03 +00001995 bool HaveSemi = false;
Michael Kuperstein098cd9f2015-09-16 11:18:25 +00001996 const unsigned PrintableFlags = FrameSetup | FrameDestroy;
Jakob Stoklund Olesen6922e9c2013-01-09 18:35:09 +00001997 if (Flags & PrintableFlags) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001998 if (!HaveSemi) {
1999 OS << ";";
2000 HaveSemi = true;
2001 }
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00002002 OS << " flags: ";
2003
2004 if (Flags & FrameSetup)
2005 OS << "FrameSetup";
Michael Kuperstein098cd9f2015-09-16 11:18:25 +00002006
2007 if (Flags & FrameDestroy)
2008 OS << "FrameDestroy";
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00002009 }
2010
Dan Gohman3b460302008-07-07 23:14:23 +00002011 if (!memoperands_empty()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00002012 if (!HaveSemi) {
2013 OS << ";";
2014 HaveSemi = true;
2015 }
Dan Gohman34341e62009-10-31 20:19:03 +00002016
2017 OS << " mem:";
Dan Gohman48b185d2009-09-25 20:36:54 +00002018 for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
2019 i != e; ++i) {
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00002020 (*i)->print(OS, MST);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002021 if (std::next(i) != e)
Dan Gohmanc0353bf2009-09-23 01:33:16 +00002022 OS << " ";
Dan Gohman2d489b52008-02-06 22:27:42 +00002023 }
2024 }
2025
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00002026 // Print the regclass of any virtual registers encountered.
2027 if (MRI && !VirtRegs.empty()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00002028 if (!HaveSemi) {
2029 OS << ";";
2030 HaveSemi = true;
2031 }
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00002032 for (unsigned i = 0; i != VirtRegs.size(); ++i) {
Quentin Colombet03c41962016-04-07 23:18:11 +00002033 const RegClassOrRegBank &RC = MRI->getRegClassOrRegBank(VirtRegs[i]);
Quentin Colombete1494c32016-02-11 00:19:17 +00002034 if (!RC)
2035 continue;
Quentin Colombet03c41962016-04-07 23:18:11 +00002036 // Generic virtual registers do not have register classes.
2037 if (RC.is<const RegisterBank *>())
2038 OS << " " << RC.get<const RegisterBank *>()->getName();
2039 else
2040 OS << " "
2041 << TRI->getRegClassName(RC.get<const TargetRegisterClass *>());
2042 OS << ':' << PrintReg(VirtRegs[i]);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00002043 for (unsigned j = i+1; j != VirtRegs.size();) {
Quentin Colombet03c41962016-04-07 23:18:11 +00002044 if (MRI->getRegClassOrRegBank(VirtRegs[j]) != RC) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00002045 ++j;
2046 continue;
2047 }
2048 if (VirtRegs[i] != VirtRegs[j])
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +00002049 OS << "," << PrintReg(VirtRegs[j]);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00002050 VirtRegs.erase(VirtRegs.begin()+j);
2051 }
2052 }
2053 }
2054
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00002055 // Print debug location information.
Duncan P. N. Exon Smithc5bd3e02015-04-03 16:23:04 +00002056 if (isDebugValue() && getOperand(e - 2).isMetadata()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00002057 if (!HaveSemi)
2058 OS << ";";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002059 auto *DV = cast<DILocalVariable>(getOperand(e - 2).getMetadata());
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +00002060 OS << " line no:" << DV->getLine();
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00002061 if (auto *InlinedAt = debugLoc->getInlinedAt()) {
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00002062 DebugLoc InlinedAtDL(InlinedAt);
2063 if (InlinedAtDL && MF) {
Devang Pateld61b1d52011-08-04 20:44:26 +00002064 OS << " inlined @[ ";
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002065 InlinedAtDL.print(OS);
Devang Pateld61b1d52011-08-04 20:44:26 +00002066 OS << " ]";
2067 }
2068 }
Adrian Prantl87b7eb92014-10-01 18:55:02 +00002069 if (isIndirectDebugValue())
2070 OS << " indirect";
Ahmed Bougacha97119d42017-02-23 21:05:29 +00002071 } else if (SkipDebugLoc) {
2072 return;
2073 } else if (debugLoc && MF) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00002074 if (!HaveSemi)
2075 OS << ";";
Dan Gohman2e3f1872009-11-23 21:29:08 +00002076 OS << " dbg:";
Eric Christopherb9f00092015-02-26 23:32:17 +00002077 debugLoc.print(OS);
Bill Wendling1a0a3d02009-02-19 21:44:55 +00002078 }
2079
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00002080 OS << '\n';
Chris Lattner214808f2002-10-30 00:48:05 +00002081}
2082
Owen Anderson2a8a4852008-01-24 01:10:07 +00002083bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00002084 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00002085 bool AddIfNotFound) {
Evan Cheng6c177732008-04-16 09:41:59 +00002086 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00002087 bool hasAliases = isPhysReg &&
2088 MCRegAliasIterator(IncomingReg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00002089 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00002090 SmallVector<unsigned,4> DeadOps;
Bill Wendling7921ad02008-03-03 22:14:33 +00002091 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2092 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenf465f062009-08-04 20:09:25 +00002093 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng6c177732008-04-16 09:41:59 +00002094 continue;
Mandeep Singh Grange5a2f112016-05-10 17:57:27 +00002095
2096 // DEBUG_VALUE nodes do not contribute to code generation and should
2097 // always be ignored. Failure to do so may result in trying to modify
2098 // KILL flags on DEBUG_VALUE nodes.
2099 if (MO.isDebug())
2100 continue;
2101
Evan Cheng6c177732008-04-16 09:41:59 +00002102 unsigned Reg = MO.getReg();
2103 if (!Reg)
2104 continue;
Bill Wendling7921ad02008-03-03 22:14:33 +00002105
Evan Cheng6c177732008-04-16 09:41:59 +00002106 if (Reg == IncomingReg) {
Dan Gohmanc7367b42008-09-03 15:56:16 +00002107 if (!Found) {
2108 if (MO.isKill())
2109 // The register is already marked kill.
2110 return true;
Jakob Stoklund Olesenc59cd9b2009-08-02 19:13:03 +00002111 if (isPhysReg && isRegTiedToDefOperand(i))
2112 // Two-address uses of physregs must not be marked kill.
2113 return true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00002114 MO.setIsKill();
2115 Found = true;
2116 }
2117 } else if (hasAliases && MO.isKill() &&
2118 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00002119 // A super-register kill already exists.
2120 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohmanb2612922008-07-03 01:18:51 +00002121 return true;
2122 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng6c177732008-04-16 09:41:59 +00002123 DeadOps.push_back(i);
Bill Wendling7921ad02008-03-03 22:14:33 +00002124 }
2125 }
2126
Evan Cheng6c177732008-04-16 09:41:59 +00002127 // Trim unneeded kill operands.
2128 while (!DeadOps.empty()) {
2129 unsigned OpIdx = DeadOps.back();
2130 if (getOperand(OpIdx).isImplicit())
2131 RemoveOperand(OpIdx);
2132 else
2133 getOperand(OpIdx).setIsKill(false);
2134 DeadOps.pop_back();
2135 }
2136
Bill Wendling7921ad02008-03-03 22:14:33 +00002137 // If not found, this means an alias of one of the operands is killed. Add a
Owen Anderson2a8a4852008-01-24 01:10:07 +00002138 // new implicit operand if required.
Dan Gohmanc7367b42008-09-03 15:56:16 +00002139 if (!Found && AddIfNotFound) {
Bill Wendling7921ad02008-03-03 22:14:33 +00002140 addOperand(MachineOperand::CreateReg(IncomingReg,
2141 false /*IsDef*/,
2142 true /*IsImp*/,
2143 true /*IsKill*/));
Owen Anderson2a8a4852008-01-24 01:10:07 +00002144 return true;
2145 }
Dan Gohmanc7367b42008-09-03 15:56:16 +00002146 return Found;
Owen Anderson2a8a4852008-01-24 01:10:07 +00002147}
2148
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00002149void MachineInstr::clearRegisterKills(unsigned Reg,
2150 const TargetRegisterInfo *RegInfo) {
2151 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topperc0196b12014-04-14 00:51:57 +00002152 RegInfo = nullptr;
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002153 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00002154 if (!MO.isReg() || !MO.isUse() || !MO.isKill())
2155 continue;
2156 unsigned OpReg = MO.getReg();
Matthias Braunaca625a2016-02-24 19:21:48 +00002157 if ((RegInfo && RegInfo->regsOverlap(Reg, OpReg)) || Reg == OpReg)
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00002158 MO.setIsKill(false);
2159 }
2160}
2161
Matthias Braun1965bfa2013-10-10 21:28:38 +00002162bool MachineInstr::addRegisterDead(unsigned Reg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00002163 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00002164 bool AddIfNotFound) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00002165 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(Reg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00002166 bool hasAliases = isPhysReg &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00002167 MCRegAliasIterator(Reg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00002168 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00002169 SmallVector<unsigned,4> DeadOps;
Owen Anderson2a8a4852008-01-24 01:10:07 +00002170 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2171 MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00002172 if (!MO.isReg() || !MO.isDef())
Evan Cheng6c177732008-04-16 09:41:59 +00002173 continue;
Matthias Braun1965bfa2013-10-10 21:28:38 +00002174 unsigned MOReg = MO.getReg();
2175 if (!MOReg)
Dan Gohmanc7367b42008-09-03 15:56:16 +00002176 continue;
2177
Matthias Braun1965bfa2013-10-10 21:28:38 +00002178 if (MOReg == Reg) {
Jakob Stoklund Olesen76ad3de2011-04-05 16:53:50 +00002179 MO.setIsDead();
2180 Found = true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00002181 } else if (hasAliases && MO.isDead() &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00002182 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00002183 // There exists a super-register that's marked dead.
Matthias Braun1965bfa2013-10-10 21:28:38 +00002184 if (RegInfo->isSuperRegister(Reg, MOReg))
Dan Gohmanb2612922008-07-03 01:18:51 +00002185 return true;
Matthias Braun1965bfa2013-10-10 21:28:38 +00002186 if (RegInfo->isSubRegister(Reg, MOReg))
Evan Cheng6c177732008-04-16 09:41:59 +00002187 DeadOps.push_back(i);
Owen Anderson2a8a4852008-01-24 01:10:07 +00002188 }
2189 }
2190
Evan Cheng6c177732008-04-16 09:41:59 +00002191 // Trim unneeded dead operands.
2192 while (!DeadOps.empty()) {
2193 unsigned OpIdx = DeadOps.back();
2194 if (getOperand(OpIdx).isImplicit())
2195 RemoveOperand(OpIdx);
2196 else
2197 getOperand(OpIdx).setIsDead(false);
2198 DeadOps.pop_back();
2199 }
2200
Dan Gohmanc7367b42008-09-03 15:56:16 +00002201 // If not found, this means an alias of one of the operands is dead. Add a
2202 // new implicit operand if required.
Chris Lattnerfd682802009-06-24 17:54:48 +00002203 if (Found || !AddIfNotFound)
2204 return Found;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00002205
Matthias Braun1965bfa2013-10-10 21:28:38 +00002206 addOperand(MachineOperand::CreateReg(Reg,
Chris Lattnerfd682802009-06-24 17:54:48 +00002207 true /*IsDef*/,
2208 true /*IsImp*/,
2209 false /*IsKill*/,
2210 true /*IsDead*/));
2211 return true;
Owen Anderson2a8a4852008-01-24 01:10:07 +00002212}
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00002213
Matthias Braun26e7ea62015-02-04 19:35:16 +00002214void MachineInstr::clearRegisterDeads(unsigned Reg) {
2215 for (MachineOperand &MO : operands()) {
2216 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg)
2217 continue;
2218 MO.setIsDead(false);
2219 }
2220}
2221
Matthias Braun2c98d0f2015-11-11 00:41:58 +00002222void MachineInstr::setRegisterDefReadUndef(unsigned Reg, bool IsUndef) {
Matthias Braunc1988f32015-01-21 22:55:13 +00002223 for (MachineOperand &MO : operands()) {
2224 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg || MO.getSubReg() == 0)
2225 continue;
Matthias Braun2c98d0f2015-11-11 00:41:58 +00002226 MO.setIsUndef(IsUndef);
Matthias Braunc1988f32015-01-21 22:55:13 +00002227 }
2228}
2229
Matthias Braun1965bfa2013-10-10 21:28:38 +00002230void MachineInstr::addRegisterDefined(unsigned Reg,
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00002231 const TargetRegisterInfo *RegInfo) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00002232 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
2233 MachineOperand *MO = findRegisterDefOperand(Reg, false, RegInfo);
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00002234 if (MO)
2235 return;
2236 } else {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002237 for (const MachineOperand &MO : operands()) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00002238 if (MO.isReg() && MO.getReg() == Reg && MO.isDef() &&
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00002239 MO.getSubReg() == 0)
2240 return;
2241 }
2242 }
Matthias Braun1965bfa2013-10-10 21:28:38 +00002243 addOperand(MachineOperand::CreateReg(Reg,
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00002244 true /*IsDef*/,
2245 true /*IsImp*/));
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00002246}
Evan Cheng59d27fe2010-03-03 23:37:30 +00002247
Jakob Stoklund Olesen4290be42012-02-03 20:43:39 +00002248void MachineInstr::setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs,
Dan Gohman86936502010-06-18 23:28:01 +00002249 const TargetRegisterInfo &TRI) {
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00002250 bool HasRegMask = false;
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002251 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00002252 if (MO.isRegMask()) {
2253 HasRegMask = true;
2254 continue;
2255 }
Dan Gohman86936502010-06-18 23:28:01 +00002256 if (!MO.isReg() || !MO.isDef()) continue;
2257 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenf6507322012-02-03 20:43:35 +00002258 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Dan Gohman86936502010-06-18 23:28:01 +00002259 // If there are no uses, including partial uses, the def is dead.
David Majnemer0a16c222016-08-11 21:15:00 +00002260 if (none_of(UsedRegs,
2261 [&](unsigned Use) { return TRI.regsOverlap(Use, Reg); }))
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002262 MO.setIsDead();
Dan Gohman86936502010-06-18 23:28:01 +00002263 }
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00002264
2265 // This is a call with a register mask operand.
2266 // Mask clobbers are always dead, so add defs for the non-dead defines.
2267 if (HasRegMask)
2268 for (ArrayRef<unsigned>::iterator I = UsedRegs.begin(), E = UsedRegs.end();
2269 I != E; ++I)
2270 addRegisterDefined(*I, &TRI);
Dan Gohman86936502010-06-18 23:28:01 +00002271}
2272
Evan Cheng59d27fe2010-03-03 23:37:30 +00002273unsigned
2274MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) {
Chandler Carruth962152c2012-03-07 09:39:46 +00002275 // Build up a buffer of hash code components.
Chandler Carruth962152c2012-03-07 09:39:46 +00002276 SmallVector<size_t, 8> HashComponents;
2277 HashComponents.reserve(MI->getNumOperands() + 1);
2278 HashComponents.push_back(MI->getOpcode());
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002279 for (const MachineOperand &MO : MI->operands()) {
Chandler Carruth264854f2012-07-05 11:06:22 +00002280 if (MO.isReg() && MO.isDef() &&
2281 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
2282 continue; // Skip virtual register defs.
2283
2284 HashComponents.push_back(hash_value(MO));
Evan Cheng59d27fe2010-03-03 23:37:30 +00002285 }
Chandler Carruth962152c2012-03-07 09:39:46 +00002286 return hash_combine_range(HashComponents.begin(), HashComponents.end());
Evan Cheng59d27fe2010-03-03 23:37:30 +00002287}
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002288
2289void MachineInstr::emitError(StringRef Msg) const {
2290 // Find the source location cookie.
2291 unsigned LocCookie = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00002292 const MDNode *LocMD = nullptr;
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002293 for (unsigned i = getNumOperands(); i != 0; --i) {
2294 if (getOperand(i-1).isMetadata() &&
2295 (LocMD = getOperand(i-1).getMetadata()) &&
2296 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002297 if (const ConstantInt *CI =
2298 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002299 LocCookie = CI->getZExtValue();
2300 break;
2301 }
2302 }
2303 }
2304
2305 if (const MachineBasicBlock *MBB = getParent())
2306 if (const MachineFunction *MF = MBB->getParent())
2307 return MF->getMMI().getModule()->getContext().emitError(LocCookie, Msg);
2308 report_fatal_error(Msg);
2309}
Reid Kleckner28865802016-04-14 18:29:59 +00002310
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002311MachineInstrBuilder llvm::BuildMI(MachineFunction &MF, const DebugLoc &DL,
Reid Kleckner28865802016-04-14 18:29:59 +00002312 const MCInstrDesc &MCID, bool IsIndirect,
2313 unsigned Reg, unsigned Offset,
2314 const MDNode *Variable, const MDNode *Expr) {
2315 assert(isa<DILocalVariable>(Variable) && "not a variable");
2316 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
2317 assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
2318 "Expected inlined-at fields to agree");
2319 if (IsIndirect)
2320 return BuildMI(MF, DL, MCID)
2321 .addReg(Reg, RegState::Debug)
2322 .addImm(Offset)
2323 .addMetadata(Variable)
2324 .addMetadata(Expr);
2325 else {
2326 assert(Offset == 0 && "A direct address cannot have an offset.");
2327 return BuildMI(MF, DL, MCID)
2328 .addReg(Reg, RegState::Debug)
2329 .addReg(0U, RegState::Debug)
2330 .addMetadata(Variable)
2331 .addMetadata(Expr);
2332 }
2333}
2334
2335MachineInstrBuilder llvm::BuildMI(MachineBasicBlock &BB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002336 MachineBasicBlock::iterator I,
2337 const DebugLoc &DL, const MCInstrDesc &MCID,
2338 bool IsIndirect, unsigned Reg,
2339 unsigned Offset, const MDNode *Variable,
2340 const MDNode *Expr) {
Reid Kleckner28865802016-04-14 18:29:59 +00002341 assert(isa<DILocalVariable>(Variable) && "not a variable");
2342 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
2343 MachineFunction &MF = *BB.getParent();
2344 MachineInstr *MI =
2345 BuildMI(MF, DL, MCID, IsIndirect, Reg, Offset, Variable, Expr);
2346 BB.insert(I, MI);
2347 return MachineInstrBuilder(MF, MI);
2348}