1*9880d681SAndroid Build Coastguard Worker //===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // Methods common to all machine instructions.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstr.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/FoldingSet.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Hashing.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AliasAnalysis.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineConstantPool.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBuilder.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineMemOperand.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineModuleInfo.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/PseudoSourceValue.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DebugInfo.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/InlineAsm.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LLVMContext.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Metadata.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/ModuleSlotTracker.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Type.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Value.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCInstrDesc.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCSymbol.h"
37*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
38*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
39*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
40*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/MathExtras.h"
41*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
42*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
43*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
44*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetRegisterInfo.h"
45*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
46*9880d681SAndroid Build Coastguard Worker using namespace llvm;
47*9880d681SAndroid Build Coastguard Worker
48*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> PrintWholeRegMask(
49*9880d681SAndroid Build Coastguard Worker "print-whole-regmask",
50*9880d681SAndroid Build Coastguard Worker cl::desc("Print the full contents of regmask operands in IR dumps"),
51*9880d681SAndroid Build Coastguard Worker cl::init(true), cl::Hidden);
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
54*9880d681SAndroid Build Coastguard Worker // MachineOperand Implementation
55*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
56*9880d681SAndroid Build Coastguard Worker
setReg(unsigned Reg)57*9880d681SAndroid Build Coastguard Worker void MachineOperand::setReg(unsigned Reg) {
58*9880d681SAndroid Build Coastguard Worker if (getReg() == Reg) return; // No change.
59*9880d681SAndroid Build Coastguard Worker
60*9880d681SAndroid Build Coastguard Worker // Otherwise, we have to change the register. If this operand is embedded
61*9880d681SAndroid Build Coastguard Worker // into a machine function, we need to update the old and new register's
62*9880d681SAndroid Build Coastguard Worker // use/def lists.
63*9880d681SAndroid Build Coastguard Worker if (MachineInstr *MI = getParent())
64*9880d681SAndroid Build Coastguard Worker if (MachineBasicBlock *MBB = MI->getParent())
65*9880d681SAndroid Build Coastguard Worker if (MachineFunction *MF = MBB->getParent()) {
66*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo &MRI = MF->getRegInfo();
67*9880d681SAndroid Build Coastguard Worker MRI.removeRegOperandFromUseList(this);
68*9880d681SAndroid Build Coastguard Worker SmallContents.RegNo = Reg;
69*9880d681SAndroid Build Coastguard Worker MRI.addRegOperandToUseList(this);
70*9880d681SAndroid Build Coastguard Worker return;
71*9880d681SAndroid Build Coastguard Worker }
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard Worker // Otherwise, just change the register, no problem. :)
74*9880d681SAndroid Build Coastguard Worker SmallContents.RegNo = Reg;
75*9880d681SAndroid Build Coastguard Worker }
76*9880d681SAndroid Build Coastguard Worker
substVirtReg(unsigned Reg,unsigned SubIdx,const TargetRegisterInfo & TRI)77*9880d681SAndroid Build Coastguard Worker void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx,
78*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo &TRI) {
79*9880d681SAndroid Build Coastguard Worker assert(TargetRegisterInfo::isVirtualRegister(Reg));
80*9880d681SAndroid Build Coastguard Worker if (SubIdx && getSubReg())
81*9880d681SAndroid Build Coastguard Worker SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg());
82*9880d681SAndroid Build Coastguard Worker setReg(Reg);
83*9880d681SAndroid Build Coastguard Worker if (SubIdx)
84*9880d681SAndroid Build Coastguard Worker setSubReg(SubIdx);
85*9880d681SAndroid Build Coastguard Worker }
86*9880d681SAndroid Build Coastguard Worker
substPhysReg(unsigned Reg,const TargetRegisterInfo & TRI)87*9880d681SAndroid Build Coastguard Worker void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) {
88*9880d681SAndroid Build Coastguard Worker assert(TargetRegisterInfo::isPhysicalRegister(Reg));
89*9880d681SAndroid Build Coastguard Worker if (getSubReg()) {
90*9880d681SAndroid Build Coastguard Worker Reg = TRI.getSubReg(Reg, getSubReg());
91*9880d681SAndroid Build Coastguard Worker // Note that getSubReg() may return 0 if the sub-register doesn't exist.
92*9880d681SAndroid Build Coastguard Worker // That won't happen in legal code.
93*9880d681SAndroid Build Coastguard Worker setSubReg(0);
94*9880d681SAndroid Build Coastguard Worker }
95*9880d681SAndroid Build Coastguard Worker setReg(Reg);
96*9880d681SAndroid Build Coastguard Worker }
97*9880d681SAndroid Build Coastguard Worker
98*9880d681SAndroid Build Coastguard Worker /// Change a def to a use, or a use to a def.
setIsDef(bool Val)99*9880d681SAndroid Build Coastguard Worker void MachineOperand::setIsDef(bool Val) {
100*9880d681SAndroid Build Coastguard Worker assert(isReg() && "Wrong MachineOperand accessor");
101*9880d681SAndroid Build Coastguard Worker assert((!Val || !isDebug()) && "Marking a debug operation as def");
102*9880d681SAndroid Build Coastguard Worker if (IsDef == Val)
103*9880d681SAndroid Build Coastguard Worker return;
104*9880d681SAndroid Build Coastguard Worker // MRI may keep uses and defs in different list positions.
105*9880d681SAndroid Build Coastguard Worker if (MachineInstr *MI = getParent())
106*9880d681SAndroid Build Coastguard Worker if (MachineBasicBlock *MBB = MI->getParent())
107*9880d681SAndroid Build Coastguard Worker if (MachineFunction *MF = MBB->getParent()) {
108*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo &MRI = MF->getRegInfo();
109*9880d681SAndroid Build Coastguard Worker MRI.removeRegOperandFromUseList(this);
110*9880d681SAndroid Build Coastguard Worker IsDef = Val;
111*9880d681SAndroid Build Coastguard Worker MRI.addRegOperandToUseList(this);
112*9880d681SAndroid Build Coastguard Worker return;
113*9880d681SAndroid Build Coastguard Worker }
114*9880d681SAndroid Build Coastguard Worker IsDef = Val;
115*9880d681SAndroid Build Coastguard Worker }
116*9880d681SAndroid Build Coastguard Worker
117*9880d681SAndroid Build Coastguard Worker // If this operand is currently a register operand, and if this is in a
118*9880d681SAndroid Build Coastguard Worker // function, deregister the operand from the register's use/def list.
removeRegFromUses()119*9880d681SAndroid Build Coastguard Worker void MachineOperand::removeRegFromUses() {
120*9880d681SAndroid Build Coastguard Worker if (!isReg() || !isOnRegUseList())
121*9880d681SAndroid Build Coastguard Worker return;
122*9880d681SAndroid Build Coastguard Worker
123*9880d681SAndroid Build Coastguard Worker if (MachineInstr *MI = getParent()) {
124*9880d681SAndroid Build Coastguard Worker if (MachineBasicBlock *MBB = MI->getParent()) {
125*9880d681SAndroid Build Coastguard Worker if (MachineFunction *MF = MBB->getParent())
126*9880d681SAndroid Build Coastguard Worker MF->getRegInfo().removeRegOperandFromUseList(this);
127*9880d681SAndroid Build Coastguard Worker }
128*9880d681SAndroid Build Coastguard Worker }
129*9880d681SAndroid Build Coastguard Worker }
130*9880d681SAndroid Build Coastguard Worker
131*9880d681SAndroid Build Coastguard Worker /// ChangeToImmediate - Replace this operand with a new immediate operand of
132*9880d681SAndroid Build Coastguard Worker /// the specified value. If an operand is known to be an immediate already,
133*9880d681SAndroid Build Coastguard Worker /// the setImm method should be used.
ChangeToImmediate(int64_t ImmVal)134*9880d681SAndroid Build Coastguard Worker void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
135*9880d681SAndroid Build Coastguard Worker assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
136*9880d681SAndroid Build Coastguard Worker
137*9880d681SAndroid Build Coastguard Worker removeRegFromUses();
138*9880d681SAndroid Build Coastguard Worker
139*9880d681SAndroid Build Coastguard Worker OpKind = MO_Immediate;
140*9880d681SAndroid Build Coastguard Worker Contents.ImmVal = ImmVal;
141*9880d681SAndroid Build Coastguard Worker }
142*9880d681SAndroid Build Coastguard Worker
ChangeToFPImmediate(const ConstantFP * FPImm)143*9880d681SAndroid Build Coastguard Worker void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm) {
144*9880d681SAndroid Build Coastguard Worker assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
145*9880d681SAndroid Build Coastguard Worker
146*9880d681SAndroid Build Coastguard Worker removeRegFromUses();
147*9880d681SAndroid Build Coastguard Worker
148*9880d681SAndroid Build Coastguard Worker OpKind = MO_FPImmediate;
149*9880d681SAndroid Build Coastguard Worker Contents.CFP = FPImm;
150*9880d681SAndroid Build Coastguard Worker }
151*9880d681SAndroid Build Coastguard Worker
ChangeToES(const char * SymName,unsigned char TargetFlags)152*9880d681SAndroid Build Coastguard Worker void MachineOperand::ChangeToES(const char *SymName, unsigned char TargetFlags) {
153*9880d681SAndroid Build Coastguard Worker assert((!isReg() || !isTied()) &&
154*9880d681SAndroid Build Coastguard Worker "Cannot change a tied operand into an external symbol");
155*9880d681SAndroid Build Coastguard Worker
156*9880d681SAndroid Build Coastguard Worker removeRegFromUses();
157*9880d681SAndroid Build Coastguard Worker
158*9880d681SAndroid Build Coastguard Worker OpKind = MO_ExternalSymbol;
159*9880d681SAndroid Build Coastguard Worker Contents.OffsetedInfo.Val.SymbolName = SymName;
160*9880d681SAndroid Build Coastguard Worker setOffset(0); // Offset is always 0.
161*9880d681SAndroid Build Coastguard Worker setTargetFlags(TargetFlags);
162*9880d681SAndroid Build Coastguard Worker }
163*9880d681SAndroid Build Coastguard Worker
ChangeToMCSymbol(MCSymbol * Sym)164*9880d681SAndroid Build Coastguard Worker void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym) {
165*9880d681SAndroid Build Coastguard Worker assert((!isReg() || !isTied()) &&
166*9880d681SAndroid Build Coastguard Worker "Cannot change a tied operand into an MCSymbol");
167*9880d681SAndroid Build Coastguard Worker
168*9880d681SAndroid Build Coastguard Worker removeRegFromUses();
169*9880d681SAndroid Build Coastguard Worker
170*9880d681SAndroid Build Coastguard Worker OpKind = MO_MCSymbol;
171*9880d681SAndroid Build Coastguard Worker Contents.Sym = Sym;
172*9880d681SAndroid Build Coastguard Worker }
173*9880d681SAndroid Build Coastguard Worker
174*9880d681SAndroid Build Coastguard Worker /// ChangeToRegister - Replace this operand with a new register operand of
175*9880d681SAndroid Build Coastguard Worker /// the specified value. If an operand is known to be an register already,
176*9880d681SAndroid Build Coastguard Worker /// the setReg method should be used.
ChangeToRegister(unsigned Reg,bool isDef,bool isImp,bool isKill,bool isDead,bool isUndef,bool isDebug)177*9880d681SAndroid Build Coastguard Worker void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
178*9880d681SAndroid Build Coastguard Worker bool isKill, bool isDead, bool isUndef,
179*9880d681SAndroid Build Coastguard Worker bool isDebug) {
180*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo *RegInfo = nullptr;
181*9880d681SAndroid Build Coastguard Worker if (MachineInstr *MI = getParent())
182*9880d681SAndroid Build Coastguard Worker if (MachineBasicBlock *MBB = MI->getParent())
183*9880d681SAndroid Build Coastguard Worker if (MachineFunction *MF = MBB->getParent())
184*9880d681SAndroid Build Coastguard Worker RegInfo = &MF->getRegInfo();
185*9880d681SAndroid Build Coastguard Worker // If this operand is already a register operand, remove it from the
186*9880d681SAndroid Build Coastguard Worker // register's use/def lists.
187*9880d681SAndroid Build Coastguard Worker bool WasReg = isReg();
188*9880d681SAndroid Build Coastguard Worker if (RegInfo && WasReg)
189*9880d681SAndroid Build Coastguard Worker RegInfo->removeRegOperandFromUseList(this);
190*9880d681SAndroid Build Coastguard Worker
191*9880d681SAndroid Build Coastguard Worker // Change this to a register and set the reg#.
192*9880d681SAndroid Build Coastguard Worker OpKind = MO_Register;
193*9880d681SAndroid Build Coastguard Worker SmallContents.RegNo = Reg;
194*9880d681SAndroid Build Coastguard Worker SubReg_TargetFlags = 0;
195*9880d681SAndroid Build Coastguard Worker IsDef = isDef;
196*9880d681SAndroid Build Coastguard Worker IsImp = isImp;
197*9880d681SAndroid Build Coastguard Worker IsKill = isKill;
198*9880d681SAndroid Build Coastguard Worker IsDead = isDead;
199*9880d681SAndroid Build Coastguard Worker IsUndef = isUndef;
200*9880d681SAndroid Build Coastguard Worker IsInternalRead = false;
201*9880d681SAndroid Build Coastguard Worker IsEarlyClobber = false;
202*9880d681SAndroid Build Coastguard Worker IsDebug = isDebug;
203*9880d681SAndroid Build Coastguard Worker // Ensure isOnRegUseList() returns false.
204*9880d681SAndroid Build Coastguard Worker Contents.Reg.Prev = nullptr;
205*9880d681SAndroid Build Coastguard Worker // Preserve the tie when the operand was already a register.
206*9880d681SAndroid Build Coastguard Worker if (!WasReg)
207*9880d681SAndroid Build Coastguard Worker TiedTo = 0;
208*9880d681SAndroid Build Coastguard Worker
209*9880d681SAndroid Build Coastguard Worker // If this operand is embedded in a function, add the operand to the
210*9880d681SAndroid Build Coastguard Worker // register's use/def list.
211*9880d681SAndroid Build Coastguard Worker if (RegInfo)
212*9880d681SAndroid Build Coastguard Worker RegInfo->addRegOperandToUseList(this);
213*9880d681SAndroid Build Coastguard Worker }
214*9880d681SAndroid Build Coastguard Worker
215*9880d681SAndroid Build Coastguard Worker /// isIdenticalTo - Return true if this operand is identical to the specified
216*9880d681SAndroid Build Coastguard Worker /// operand. Note that this should stay in sync with the hash_value overload
217*9880d681SAndroid Build Coastguard Worker /// below.
isIdenticalTo(const MachineOperand & Other) const218*9880d681SAndroid Build Coastguard Worker bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
219*9880d681SAndroid Build Coastguard Worker if (getType() != Other.getType() ||
220*9880d681SAndroid Build Coastguard Worker getTargetFlags() != Other.getTargetFlags())
221*9880d681SAndroid Build Coastguard Worker return false;
222*9880d681SAndroid Build Coastguard Worker
223*9880d681SAndroid Build Coastguard Worker switch (getType()) {
224*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_Register:
225*9880d681SAndroid Build Coastguard Worker return getReg() == Other.getReg() && isDef() == Other.isDef() &&
226*9880d681SAndroid Build Coastguard Worker getSubReg() == Other.getSubReg();
227*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_Immediate:
228*9880d681SAndroid Build Coastguard Worker return getImm() == Other.getImm();
229*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_CImmediate:
230*9880d681SAndroid Build Coastguard Worker return getCImm() == Other.getCImm();
231*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_FPImmediate:
232*9880d681SAndroid Build Coastguard Worker return getFPImm() == Other.getFPImm();
233*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_MachineBasicBlock:
234*9880d681SAndroid Build Coastguard Worker return getMBB() == Other.getMBB();
235*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_FrameIndex:
236*9880d681SAndroid Build Coastguard Worker return getIndex() == Other.getIndex();
237*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_ConstantPoolIndex:
238*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_TargetIndex:
239*9880d681SAndroid Build Coastguard Worker return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
240*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_JumpTableIndex:
241*9880d681SAndroid Build Coastguard Worker return getIndex() == Other.getIndex();
242*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_GlobalAddress:
243*9880d681SAndroid Build Coastguard Worker return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
244*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_ExternalSymbol:
245*9880d681SAndroid Build Coastguard Worker return !strcmp(getSymbolName(), Other.getSymbolName()) &&
246*9880d681SAndroid Build Coastguard Worker getOffset() == Other.getOffset();
247*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_BlockAddress:
248*9880d681SAndroid Build Coastguard Worker return getBlockAddress() == Other.getBlockAddress() &&
249*9880d681SAndroid Build Coastguard Worker getOffset() == Other.getOffset();
250*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_RegisterMask:
251*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_RegisterLiveOut:
252*9880d681SAndroid Build Coastguard Worker return getRegMask() == Other.getRegMask();
253*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_MCSymbol:
254*9880d681SAndroid Build Coastguard Worker return getMCSymbol() == Other.getMCSymbol();
255*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_CFIIndex:
256*9880d681SAndroid Build Coastguard Worker return getCFIIndex() == Other.getCFIIndex();
257*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_Metadata:
258*9880d681SAndroid Build Coastguard Worker return getMetadata() == Other.getMetadata();
259*9880d681SAndroid Build Coastguard Worker }
260*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Invalid machine operand type");
261*9880d681SAndroid Build Coastguard Worker }
262*9880d681SAndroid Build Coastguard Worker
263*9880d681SAndroid Build Coastguard Worker // Note: this must stay exactly in sync with isIdenticalTo above.
hash_value(const MachineOperand & MO)264*9880d681SAndroid Build Coastguard Worker hash_code llvm::hash_value(const MachineOperand &MO) {
265*9880d681SAndroid Build Coastguard Worker switch (MO.getType()) {
266*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_Register:
267*9880d681SAndroid Build Coastguard Worker // Register operands don't have target flags.
268*9880d681SAndroid Build Coastguard Worker return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef());
269*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_Immediate:
270*9880d681SAndroid Build Coastguard Worker return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm());
271*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_CImmediate:
272*9880d681SAndroid Build Coastguard Worker return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm());
273*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_FPImmediate:
274*9880d681SAndroid Build Coastguard Worker return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm());
275*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_MachineBasicBlock:
276*9880d681SAndroid Build Coastguard Worker return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB());
277*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_FrameIndex:
278*9880d681SAndroid Build Coastguard Worker return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
279*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_ConstantPoolIndex:
280*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_TargetIndex:
281*9880d681SAndroid Build Coastguard Worker return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(),
282*9880d681SAndroid Build Coastguard Worker MO.getOffset());
283*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_JumpTableIndex:
284*9880d681SAndroid Build Coastguard Worker return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
285*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_ExternalSymbol:
286*9880d681SAndroid Build Coastguard Worker return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(),
287*9880d681SAndroid Build Coastguard Worker MO.getSymbolName());
288*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_GlobalAddress:
289*9880d681SAndroid Build Coastguard Worker return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(),
290*9880d681SAndroid Build Coastguard Worker MO.getOffset());
291*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_BlockAddress:
292*9880d681SAndroid Build Coastguard Worker return hash_combine(MO.getType(), MO.getTargetFlags(),
293*9880d681SAndroid Build Coastguard Worker MO.getBlockAddress(), MO.getOffset());
294*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_RegisterMask:
295*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_RegisterLiveOut:
296*9880d681SAndroid Build Coastguard Worker return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask());
297*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_Metadata:
298*9880d681SAndroid Build Coastguard Worker return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata());
299*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_MCSymbol:
300*9880d681SAndroid Build Coastguard Worker return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol());
301*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_CFIIndex:
302*9880d681SAndroid Build Coastguard Worker return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex());
303*9880d681SAndroid Build Coastguard Worker }
304*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Invalid machine operand type");
305*9880d681SAndroid Build Coastguard Worker }
306*9880d681SAndroid Build Coastguard Worker
print(raw_ostream & OS,const TargetRegisterInfo * TRI) const307*9880d681SAndroid Build Coastguard Worker void MachineOperand::print(raw_ostream &OS,
308*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI) const {
309*9880d681SAndroid Build Coastguard Worker ModuleSlotTracker DummyMST(nullptr);
310*9880d681SAndroid Build Coastguard Worker print(OS, DummyMST, TRI);
311*9880d681SAndroid Build Coastguard Worker }
312*9880d681SAndroid Build Coastguard Worker
print(raw_ostream & OS,ModuleSlotTracker & MST,const TargetRegisterInfo * TRI) const313*9880d681SAndroid Build Coastguard Worker void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
314*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI) const {
315*9880d681SAndroid Build Coastguard Worker switch (getType()) {
316*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_Register:
317*9880d681SAndroid Build Coastguard Worker OS << PrintReg(getReg(), TRI, getSubReg());
318*9880d681SAndroid Build Coastguard Worker
319*9880d681SAndroid Build Coastguard Worker if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
320*9880d681SAndroid Build Coastguard Worker isInternalRead() || isEarlyClobber() || isTied()) {
321*9880d681SAndroid Build Coastguard Worker OS << '<';
322*9880d681SAndroid Build Coastguard Worker bool NeedComma = false;
323*9880d681SAndroid Build Coastguard Worker if (isDef()) {
324*9880d681SAndroid Build Coastguard Worker if (NeedComma) OS << ',';
325*9880d681SAndroid Build Coastguard Worker if (isEarlyClobber())
326*9880d681SAndroid Build Coastguard Worker OS << "earlyclobber,";
327*9880d681SAndroid Build Coastguard Worker if (isImplicit())
328*9880d681SAndroid Build Coastguard Worker OS << "imp-";
329*9880d681SAndroid Build Coastguard Worker OS << "def";
330*9880d681SAndroid Build Coastguard Worker NeedComma = true;
331*9880d681SAndroid Build Coastguard Worker // <def,read-undef> only makes sense when getSubReg() is set.
332*9880d681SAndroid Build Coastguard Worker // Don't clutter the output otherwise.
333*9880d681SAndroid Build Coastguard Worker if (isUndef() && getSubReg())
334*9880d681SAndroid Build Coastguard Worker OS << ",read-undef";
335*9880d681SAndroid Build Coastguard Worker } else if (isImplicit()) {
336*9880d681SAndroid Build Coastguard Worker OS << "imp-use";
337*9880d681SAndroid Build Coastguard Worker NeedComma = true;
338*9880d681SAndroid Build Coastguard Worker }
339*9880d681SAndroid Build Coastguard Worker
340*9880d681SAndroid Build Coastguard Worker if (isKill()) {
341*9880d681SAndroid Build Coastguard Worker if (NeedComma) OS << ',';
342*9880d681SAndroid Build Coastguard Worker OS << "kill";
343*9880d681SAndroid Build Coastguard Worker NeedComma = true;
344*9880d681SAndroid Build Coastguard Worker }
345*9880d681SAndroid Build Coastguard Worker if (isDead()) {
346*9880d681SAndroid Build Coastguard Worker if (NeedComma) OS << ',';
347*9880d681SAndroid Build Coastguard Worker OS << "dead";
348*9880d681SAndroid Build Coastguard Worker NeedComma = true;
349*9880d681SAndroid Build Coastguard Worker }
350*9880d681SAndroid Build Coastguard Worker if (isUndef() && isUse()) {
351*9880d681SAndroid Build Coastguard Worker if (NeedComma) OS << ',';
352*9880d681SAndroid Build Coastguard Worker OS << "undef";
353*9880d681SAndroid Build Coastguard Worker NeedComma = true;
354*9880d681SAndroid Build Coastguard Worker }
355*9880d681SAndroid Build Coastguard Worker if (isInternalRead()) {
356*9880d681SAndroid Build Coastguard Worker if (NeedComma) OS << ',';
357*9880d681SAndroid Build Coastguard Worker OS << "internal";
358*9880d681SAndroid Build Coastguard Worker NeedComma = true;
359*9880d681SAndroid Build Coastguard Worker }
360*9880d681SAndroid Build Coastguard Worker if (isTied()) {
361*9880d681SAndroid Build Coastguard Worker if (NeedComma) OS << ',';
362*9880d681SAndroid Build Coastguard Worker OS << "tied";
363*9880d681SAndroid Build Coastguard Worker if (TiedTo != 15)
364*9880d681SAndroid Build Coastguard Worker OS << unsigned(TiedTo - 1);
365*9880d681SAndroid Build Coastguard Worker }
366*9880d681SAndroid Build Coastguard Worker OS << '>';
367*9880d681SAndroid Build Coastguard Worker }
368*9880d681SAndroid Build Coastguard Worker break;
369*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_Immediate:
370*9880d681SAndroid Build Coastguard Worker OS << getImm();
371*9880d681SAndroid Build Coastguard Worker break;
372*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_CImmediate:
373*9880d681SAndroid Build Coastguard Worker getCImm()->getValue().print(OS, false);
374*9880d681SAndroid Build Coastguard Worker break;
375*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_FPImmediate:
376*9880d681SAndroid Build Coastguard Worker if (getFPImm()->getType()->isFloatTy()) {
377*9880d681SAndroid Build Coastguard Worker OS << getFPImm()->getValueAPF().convertToFloat();
378*9880d681SAndroid Build Coastguard Worker } else if (getFPImm()->getType()->isHalfTy()) {
379*9880d681SAndroid Build Coastguard Worker APFloat APF = getFPImm()->getValueAPF();
380*9880d681SAndroid Build Coastguard Worker bool Unused;
381*9880d681SAndroid Build Coastguard Worker APF.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &Unused);
382*9880d681SAndroid Build Coastguard Worker OS << "half " << APF.convertToFloat();
383*9880d681SAndroid Build Coastguard Worker } else {
384*9880d681SAndroid Build Coastguard Worker OS << getFPImm()->getValueAPF().convertToDouble();
385*9880d681SAndroid Build Coastguard Worker }
386*9880d681SAndroid Build Coastguard Worker break;
387*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_MachineBasicBlock:
388*9880d681SAndroid Build Coastguard Worker OS << "<BB#" << getMBB()->getNumber() << ">";
389*9880d681SAndroid Build Coastguard Worker break;
390*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_FrameIndex:
391*9880d681SAndroid Build Coastguard Worker OS << "<fi#" << getIndex() << '>';
392*9880d681SAndroid Build Coastguard Worker break;
393*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_ConstantPoolIndex:
394*9880d681SAndroid Build Coastguard Worker OS << "<cp#" << getIndex();
395*9880d681SAndroid Build Coastguard Worker if (getOffset()) OS << "+" << getOffset();
396*9880d681SAndroid Build Coastguard Worker OS << '>';
397*9880d681SAndroid Build Coastguard Worker break;
398*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_TargetIndex:
399*9880d681SAndroid Build Coastguard Worker OS << "<ti#" << getIndex();
400*9880d681SAndroid Build Coastguard Worker if (getOffset()) OS << "+" << getOffset();
401*9880d681SAndroid Build Coastguard Worker OS << '>';
402*9880d681SAndroid Build Coastguard Worker break;
403*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_JumpTableIndex:
404*9880d681SAndroid Build Coastguard Worker OS << "<jt#" << getIndex() << '>';
405*9880d681SAndroid Build Coastguard Worker break;
406*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_GlobalAddress:
407*9880d681SAndroid Build Coastguard Worker OS << "<ga:";
408*9880d681SAndroid Build Coastguard Worker getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
409*9880d681SAndroid Build Coastguard Worker if (getOffset()) OS << "+" << getOffset();
410*9880d681SAndroid Build Coastguard Worker OS << '>';
411*9880d681SAndroid Build Coastguard Worker break;
412*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_ExternalSymbol:
413*9880d681SAndroid Build Coastguard Worker OS << "<es:" << getSymbolName();
414*9880d681SAndroid Build Coastguard Worker if (getOffset()) OS << "+" << getOffset();
415*9880d681SAndroid Build Coastguard Worker OS << '>';
416*9880d681SAndroid Build Coastguard Worker break;
417*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_BlockAddress:
418*9880d681SAndroid Build Coastguard Worker OS << '<';
419*9880d681SAndroid Build Coastguard Worker getBlockAddress()->printAsOperand(OS, /*PrintType=*/false, MST);
420*9880d681SAndroid Build Coastguard Worker if (getOffset()) OS << "+" << getOffset();
421*9880d681SAndroid Build Coastguard Worker OS << '>';
422*9880d681SAndroid Build Coastguard Worker break;
423*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_RegisterMask: {
424*9880d681SAndroid Build Coastguard Worker unsigned NumRegsInMask = 0;
425*9880d681SAndroid Build Coastguard Worker unsigned NumRegsEmitted = 0;
426*9880d681SAndroid Build Coastguard Worker OS << "<regmask";
427*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {
428*9880d681SAndroid Build Coastguard Worker unsigned MaskWord = i / 32;
429*9880d681SAndroid Build Coastguard Worker unsigned MaskBit = i % 32;
430*9880d681SAndroid Build Coastguard Worker if (getRegMask()[MaskWord] & (1 << MaskBit)) {
431*9880d681SAndroid Build Coastguard Worker if (PrintWholeRegMask || NumRegsEmitted <= 10) {
432*9880d681SAndroid Build Coastguard Worker OS << " " << PrintReg(i, TRI);
433*9880d681SAndroid Build Coastguard Worker NumRegsEmitted++;
434*9880d681SAndroid Build Coastguard Worker }
435*9880d681SAndroid Build Coastguard Worker NumRegsInMask++;
436*9880d681SAndroid Build Coastguard Worker }
437*9880d681SAndroid Build Coastguard Worker }
438*9880d681SAndroid Build Coastguard Worker if (NumRegsEmitted != NumRegsInMask)
439*9880d681SAndroid Build Coastguard Worker OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more...";
440*9880d681SAndroid Build Coastguard Worker OS << ">";
441*9880d681SAndroid Build Coastguard Worker break;
442*9880d681SAndroid Build Coastguard Worker }
443*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_RegisterLiveOut:
444*9880d681SAndroid Build Coastguard Worker OS << "<regliveout>";
445*9880d681SAndroid Build Coastguard Worker break;
446*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_Metadata:
447*9880d681SAndroid Build Coastguard Worker OS << '<';
448*9880d681SAndroid Build Coastguard Worker getMetadata()->printAsOperand(OS, MST);
449*9880d681SAndroid Build Coastguard Worker OS << '>';
450*9880d681SAndroid Build Coastguard Worker break;
451*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_MCSymbol:
452*9880d681SAndroid Build Coastguard Worker OS << "<MCSym=" << *getMCSymbol() << '>';
453*9880d681SAndroid Build Coastguard Worker break;
454*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_CFIIndex:
455*9880d681SAndroid Build Coastguard Worker OS << "<call frame instruction>";
456*9880d681SAndroid Build Coastguard Worker break;
457*9880d681SAndroid Build Coastguard Worker }
458*9880d681SAndroid Build Coastguard Worker
459*9880d681SAndroid Build Coastguard Worker if (unsigned TF = getTargetFlags())
460*9880d681SAndroid Build Coastguard Worker OS << "[TF=" << TF << ']';
461*9880d681SAndroid Build Coastguard Worker }
462*9880d681SAndroid Build Coastguard Worker
463*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
464*9880d681SAndroid Build Coastguard Worker // MachineMemOperand Implementation
465*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
466*9880d681SAndroid Build Coastguard Worker
467*9880d681SAndroid Build Coastguard Worker /// getAddrSpace - Return the LLVM IR address space number that this pointer
468*9880d681SAndroid Build Coastguard Worker /// points into.
getAddrSpace() const469*9880d681SAndroid Build Coastguard Worker unsigned MachinePointerInfo::getAddrSpace() const {
470*9880d681SAndroid Build Coastguard Worker if (V.isNull() || V.is<const PseudoSourceValue*>()) return 0;
471*9880d681SAndroid Build Coastguard Worker return cast<PointerType>(V.get<const Value*>()->getType())->getAddressSpace();
472*9880d681SAndroid Build Coastguard Worker }
473*9880d681SAndroid Build Coastguard Worker
474*9880d681SAndroid Build Coastguard Worker /// getConstantPool - Return a MachinePointerInfo record that refers to the
475*9880d681SAndroid Build Coastguard Worker /// constant pool.
getConstantPool(MachineFunction & MF)476*9880d681SAndroid Build Coastguard Worker MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) {
477*9880d681SAndroid Build Coastguard Worker return MachinePointerInfo(MF.getPSVManager().getConstantPool());
478*9880d681SAndroid Build Coastguard Worker }
479*9880d681SAndroid Build Coastguard Worker
480*9880d681SAndroid Build Coastguard Worker /// getFixedStack - Return a MachinePointerInfo record that refers to the
481*9880d681SAndroid Build Coastguard Worker /// the specified FrameIndex.
getFixedStack(MachineFunction & MF,int FI,int64_t Offset)482*9880d681SAndroid Build Coastguard Worker MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF,
483*9880d681SAndroid Build Coastguard Worker int FI, int64_t Offset) {
484*9880d681SAndroid Build Coastguard Worker return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset);
485*9880d681SAndroid Build Coastguard Worker }
486*9880d681SAndroid Build Coastguard Worker
getJumpTable(MachineFunction & MF)487*9880d681SAndroid Build Coastguard Worker MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) {
488*9880d681SAndroid Build Coastguard Worker return MachinePointerInfo(MF.getPSVManager().getJumpTable());
489*9880d681SAndroid Build Coastguard Worker }
490*9880d681SAndroid Build Coastguard Worker
getGOT(MachineFunction & MF)491*9880d681SAndroid Build Coastguard Worker MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) {
492*9880d681SAndroid Build Coastguard Worker return MachinePointerInfo(MF.getPSVManager().getGOT());
493*9880d681SAndroid Build Coastguard Worker }
494*9880d681SAndroid Build Coastguard Worker
getStack(MachineFunction & MF,int64_t Offset)495*9880d681SAndroid Build Coastguard Worker MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF,
496*9880d681SAndroid Build Coastguard Worker int64_t Offset) {
497*9880d681SAndroid Build Coastguard Worker return MachinePointerInfo(MF.getPSVManager().getStack(), Offset);
498*9880d681SAndroid Build Coastguard Worker }
499*9880d681SAndroid Build Coastguard Worker
MachineMemOperand(MachinePointerInfo ptrinfo,Flags f,uint64_t s,unsigned int a,const AAMDNodes & AAInfo,const MDNode * Ranges)500*9880d681SAndroid Build Coastguard Worker MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f,
501*9880d681SAndroid Build Coastguard Worker uint64_t s, unsigned int a,
502*9880d681SAndroid Build Coastguard Worker const AAMDNodes &AAInfo,
503*9880d681SAndroid Build Coastguard Worker const MDNode *Ranges)
504*9880d681SAndroid Build Coastguard Worker : PtrInfo(ptrinfo), Size(s), FlagVals(f), BaseAlignLog2(Log2_32(a) + 1),
505*9880d681SAndroid Build Coastguard Worker AAInfo(AAInfo), Ranges(Ranges) {
506*9880d681SAndroid Build Coastguard Worker assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue*>() ||
507*9880d681SAndroid Build Coastguard Worker isa<PointerType>(PtrInfo.V.get<const Value*>()->getType())) &&
508*9880d681SAndroid Build Coastguard Worker "invalid pointer value");
509*9880d681SAndroid Build Coastguard Worker assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
510*9880d681SAndroid Build Coastguard Worker assert((isLoad() || isStore()) && "Not a load/store!");
511*9880d681SAndroid Build Coastguard Worker }
512*9880d681SAndroid Build Coastguard Worker
513*9880d681SAndroid Build Coastguard Worker /// Profile - Gather unique data for the object.
514*9880d681SAndroid Build Coastguard Worker ///
Profile(FoldingSetNodeID & ID) const515*9880d681SAndroid Build Coastguard Worker void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
516*9880d681SAndroid Build Coastguard Worker ID.AddInteger(getOffset());
517*9880d681SAndroid Build Coastguard Worker ID.AddInteger(Size);
518*9880d681SAndroid Build Coastguard Worker ID.AddPointer(getOpaqueValue());
519*9880d681SAndroid Build Coastguard Worker ID.AddInteger(getFlags());
520*9880d681SAndroid Build Coastguard Worker ID.AddInteger(getBaseAlignment());
521*9880d681SAndroid Build Coastguard Worker }
522*9880d681SAndroid Build Coastguard Worker
refineAlignment(const MachineMemOperand * MMO)523*9880d681SAndroid Build Coastguard Worker void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
524*9880d681SAndroid Build Coastguard Worker // The Value and Offset may differ due to CSE. But the flags and size
525*9880d681SAndroid Build Coastguard Worker // should be the same.
526*9880d681SAndroid Build Coastguard Worker assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
527*9880d681SAndroid Build Coastguard Worker assert(MMO->getSize() == getSize() && "Size mismatch!");
528*9880d681SAndroid Build Coastguard Worker
529*9880d681SAndroid Build Coastguard Worker if (MMO->getBaseAlignment() >= getBaseAlignment()) {
530*9880d681SAndroid Build Coastguard Worker // Update the alignment value.
531*9880d681SAndroid Build Coastguard Worker BaseAlignLog2 = Log2_32(MMO->getBaseAlignment()) + 1;
532*9880d681SAndroid Build Coastguard Worker // Also update the base and offset, because the new alignment may
533*9880d681SAndroid Build Coastguard Worker // not be applicable with the old ones.
534*9880d681SAndroid Build Coastguard Worker PtrInfo = MMO->PtrInfo;
535*9880d681SAndroid Build Coastguard Worker }
536*9880d681SAndroid Build Coastguard Worker }
537*9880d681SAndroid Build Coastguard Worker
538*9880d681SAndroid Build Coastguard Worker /// getAlignment - Return the minimum known alignment in bytes of the
539*9880d681SAndroid Build Coastguard Worker /// actual memory reference.
getAlignment() const540*9880d681SAndroid Build Coastguard Worker uint64_t MachineMemOperand::getAlignment() const {
541*9880d681SAndroid Build Coastguard Worker return MinAlign(getBaseAlignment(), getOffset());
542*9880d681SAndroid Build Coastguard Worker }
543*9880d681SAndroid Build Coastguard Worker
print(raw_ostream & OS) const544*9880d681SAndroid Build Coastguard Worker void MachineMemOperand::print(raw_ostream &OS) const {
545*9880d681SAndroid Build Coastguard Worker ModuleSlotTracker DummyMST(nullptr);
546*9880d681SAndroid Build Coastguard Worker print(OS, DummyMST);
547*9880d681SAndroid Build Coastguard Worker }
print(raw_ostream & OS,ModuleSlotTracker & MST) const548*9880d681SAndroid Build Coastguard Worker void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST) const {
549*9880d681SAndroid Build Coastguard Worker assert((isLoad() || isStore()) &&
550*9880d681SAndroid Build Coastguard Worker "SV has to be a load, store or both.");
551*9880d681SAndroid Build Coastguard Worker
552*9880d681SAndroid Build Coastguard Worker if (isVolatile())
553*9880d681SAndroid Build Coastguard Worker OS << "Volatile ";
554*9880d681SAndroid Build Coastguard Worker
555*9880d681SAndroid Build Coastguard Worker if (isLoad())
556*9880d681SAndroid Build Coastguard Worker OS << "LD";
557*9880d681SAndroid Build Coastguard Worker if (isStore())
558*9880d681SAndroid Build Coastguard Worker OS << "ST";
559*9880d681SAndroid Build Coastguard Worker OS << getSize();
560*9880d681SAndroid Build Coastguard Worker
561*9880d681SAndroid Build Coastguard Worker // Print the address information.
562*9880d681SAndroid Build Coastguard Worker OS << "[";
563*9880d681SAndroid Build Coastguard Worker if (const Value *V = getValue())
564*9880d681SAndroid Build Coastguard Worker V->printAsOperand(OS, /*PrintType=*/false, MST);
565*9880d681SAndroid Build Coastguard Worker else if (const PseudoSourceValue *PSV = getPseudoValue())
566*9880d681SAndroid Build Coastguard Worker PSV->printCustom(OS);
567*9880d681SAndroid Build Coastguard Worker else
568*9880d681SAndroid Build Coastguard Worker OS << "<unknown>";
569*9880d681SAndroid Build Coastguard Worker
570*9880d681SAndroid Build Coastguard Worker unsigned AS = getAddrSpace();
571*9880d681SAndroid Build Coastguard Worker if (AS != 0)
572*9880d681SAndroid Build Coastguard Worker OS << "(addrspace=" << AS << ')';
573*9880d681SAndroid Build Coastguard Worker
574*9880d681SAndroid Build Coastguard Worker // If the alignment of the memory reference itself differs from the alignment
575*9880d681SAndroid Build Coastguard Worker // of the base pointer, print the base alignment explicitly, next to the base
576*9880d681SAndroid Build Coastguard Worker // pointer.
577*9880d681SAndroid Build Coastguard Worker if (getBaseAlignment() != getAlignment())
578*9880d681SAndroid Build Coastguard Worker OS << "(align=" << getBaseAlignment() << ")";
579*9880d681SAndroid Build Coastguard Worker
580*9880d681SAndroid Build Coastguard Worker if (getOffset() != 0)
581*9880d681SAndroid Build Coastguard Worker OS << "+" << getOffset();
582*9880d681SAndroid Build Coastguard Worker OS << "]";
583*9880d681SAndroid Build Coastguard Worker
584*9880d681SAndroid Build Coastguard Worker // Print the alignment of the reference.
585*9880d681SAndroid Build Coastguard Worker if (getBaseAlignment() != getAlignment() || getBaseAlignment() != getSize())
586*9880d681SAndroid Build Coastguard Worker OS << "(align=" << getAlignment() << ")";
587*9880d681SAndroid Build Coastguard Worker
588*9880d681SAndroid Build Coastguard Worker // Print TBAA info.
589*9880d681SAndroid Build Coastguard Worker if (const MDNode *TBAAInfo = getAAInfo().TBAA) {
590*9880d681SAndroid Build Coastguard Worker OS << "(tbaa=";
591*9880d681SAndroid Build Coastguard Worker if (TBAAInfo->getNumOperands() > 0)
592*9880d681SAndroid Build Coastguard Worker TBAAInfo->getOperand(0)->printAsOperand(OS, MST);
593*9880d681SAndroid Build Coastguard Worker else
594*9880d681SAndroid Build Coastguard Worker OS << "<unknown>";
595*9880d681SAndroid Build Coastguard Worker OS << ")";
596*9880d681SAndroid Build Coastguard Worker }
597*9880d681SAndroid Build Coastguard Worker
598*9880d681SAndroid Build Coastguard Worker // Print AA scope info.
599*9880d681SAndroid Build Coastguard Worker if (const MDNode *ScopeInfo = getAAInfo().Scope) {
600*9880d681SAndroid Build Coastguard Worker OS << "(alias.scope=";
601*9880d681SAndroid Build Coastguard Worker if (ScopeInfo->getNumOperands() > 0)
602*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) {
603*9880d681SAndroid Build Coastguard Worker ScopeInfo->getOperand(i)->printAsOperand(OS, MST);
604*9880d681SAndroid Build Coastguard Worker if (i != ie-1)
605*9880d681SAndroid Build Coastguard Worker OS << ",";
606*9880d681SAndroid Build Coastguard Worker }
607*9880d681SAndroid Build Coastguard Worker else
608*9880d681SAndroid Build Coastguard Worker OS << "<unknown>";
609*9880d681SAndroid Build Coastguard Worker OS << ")";
610*9880d681SAndroid Build Coastguard Worker }
611*9880d681SAndroid Build Coastguard Worker
612*9880d681SAndroid Build Coastguard Worker // Print AA noalias scope info.
613*9880d681SAndroid Build Coastguard Worker if (const MDNode *NoAliasInfo = getAAInfo().NoAlias) {
614*9880d681SAndroid Build Coastguard Worker OS << "(noalias=";
615*9880d681SAndroid Build Coastguard Worker if (NoAliasInfo->getNumOperands() > 0)
616*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) {
617*9880d681SAndroid Build Coastguard Worker NoAliasInfo->getOperand(i)->printAsOperand(OS, MST);
618*9880d681SAndroid Build Coastguard Worker if (i != ie-1)
619*9880d681SAndroid Build Coastguard Worker OS << ",";
620*9880d681SAndroid Build Coastguard Worker }
621*9880d681SAndroid Build Coastguard Worker else
622*9880d681SAndroid Build Coastguard Worker OS << "<unknown>";
623*9880d681SAndroid Build Coastguard Worker OS << ")";
624*9880d681SAndroid Build Coastguard Worker }
625*9880d681SAndroid Build Coastguard Worker
626*9880d681SAndroid Build Coastguard Worker // Print nontemporal info.
627*9880d681SAndroid Build Coastguard Worker if (isNonTemporal())
628*9880d681SAndroid Build Coastguard Worker OS << "(nontemporal)";
629*9880d681SAndroid Build Coastguard Worker
630*9880d681SAndroid Build Coastguard Worker if (isInvariant())
631*9880d681SAndroid Build Coastguard Worker OS << "(invariant)";
632*9880d681SAndroid Build Coastguard Worker }
633*9880d681SAndroid Build Coastguard Worker
634*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
635*9880d681SAndroid Build Coastguard Worker // MachineInstr Implementation
636*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
637*9880d681SAndroid Build Coastguard Worker
addImplicitDefUseOperands(MachineFunction & MF)638*9880d681SAndroid Build Coastguard Worker void MachineInstr::addImplicitDefUseOperands(MachineFunction &MF) {
639*9880d681SAndroid Build Coastguard Worker if (MCID->ImplicitDefs)
640*9880d681SAndroid Build Coastguard Worker for (const MCPhysReg *ImpDefs = MCID->getImplicitDefs(); *ImpDefs;
641*9880d681SAndroid Build Coastguard Worker ++ImpDefs)
642*9880d681SAndroid Build Coastguard Worker addOperand(MF, MachineOperand::CreateReg(*ImpDefs, true, true));
643*9880d681SAndroid Build Coastguard Worker if (MCID->ImplicitUses)
644*9880d681SAndroid Build Coastguard Worker for (const MCPhysReg *ImpUses = MCID->getImplicitUses(); *ImpUses;
645*9880d681SAndroid Build Coastguard Worker ++ImpUses)
646*9880d681SAndroid Build Coastguard Worker addOperand(MF, MachineOperand::CreateReg(*ImpUses, false, true));
647*9880d681SAndroid Build Coastguard Worker }
648*9880d681SAndroid Build Coastguard Worker
649*9880d681SAndroid Build Coastguard Worker /// MachineInstr ctor - This constructor creates a MachineInstr and adds the
650*9880d681SAndroid Build Coastguard Worker /// implicit operands. It reserves space for the number of operands specified by
651*9880d681SAndroid Build Coastguard Worker /// the MCInstrDesc.
MachineInstr(MachineFunction & MF,const MCInstrDesc & tid,DebugLoc dl,bool NoImp)652*9880d681SAndroid Build Coastguard Worker MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid,
653*9880d681SAndroid Build Coastguard Worker DebugLoc dl, bool NoImp)
654*9880d681SAndroid Build Coastguard Worker : MCID(&tid), Parent(nullptr), Operands(nullptr), NumOperands(0), Flags(0),
655*9880d681SAndroid Build Coastguard Worker AsmPrinterFlags(0), NumMemRefs(0), MemRefs(nullptr),
656*9880d681SAndroid Build Coastguard Worker debugLoc(std::move(dl))
657*9880d681SAndroid Build Coastguard Worker #ifdef LLVM_BUILD_GLOBAL_ISEL
658*9880d681SAndroid Build Coastguard Worker ,
659*9880d681SAndroid Build Coastguard Worker Ty(nullptr)
660*9880d681SAndroid Build Coastguard Worker #endif
661*9880d681SAndroid Build Coastguard Worker {
662*9880d681SAndroid Build Coastguard Worker assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
663*9880d681SAndroid Build Coastguard Worker
664*9880d681SAndroid Build Coastguard Worker // Reserve space for the expected number of operands.
665*9880d681SAndroid Build Coastguard Worker if (unsigned NumOps = MCID->getNumOperands() +
666*9880d681SAndroid Build Coastguard Worker MCID->getNumImplicitDefs() + MCID->getNumImplicitUses()) {
667*9880d681SAndroid Build Coastguard Worker CapOperands = OperandCapacity::get(NumOps);
668*9880d681SAndroid Build Coastguard Worker Operands = MF.allocateOperandArray(CapOperands);
669*9880d681SAndroid Build Coastguard Worker }
670*9880d681SAndroid Build Coastguard Worker
671*9880d681SAndroid Build Coastguard Worker if (!NoImp)
672*9880d681SAndroid Build Coastguard Worker addImplicitDefUseOperands(MF);
673*9880d681SAndroid Build Coastguard Worker }
674*9880d681SAndroid Build Coastguard Worker
675*9880d681SAndroid Build Coastguard Worker /// MachineInstr ctor - Copies MachineInstr arg exactly
676*9880d681SAndroid Build Coastguard Worker ///
MachineInstr(MachineFunction & MF,const MachineInstr & MI)677*9880d681SAndroid Build Coastguard Worker MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
678*9880d681SAndroid Build Coastguard Worker : MCID(&MI.getDesc()), Parent(nullptr), Operands(nullptr), NumOperands(0),
679*9880d681SAndroid Build Coastguard Worker Flags(0), AsmPrinterFlags(0), NumMemRefs(MI.NumMemRefs),
680*9880d681SAndroid Build Coastguard Worker MemRefs(MI.MemRefs), debugLoc(MI.getDebugLoc())
681*9880d681SAndroid Build Coastguard Worker #ifdef LLVM_BUILD_GLOBAL_ISEL
682*9880d681SAndroid Build Coastguard Worker ,
683*9880d681SAndroid Build Coastguard Worker Ty(nullptr)
684*9880d681SAndroid Build Coastguard Worker #endif
685*9880d681SAndroid Build Coastguard Worker {
686*9880d681SAndroid Build Coastguard Worker assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
687*9880d681SAndroid Build Coastguard Worker
688*9880d681SAndroid Build Coastguard Worker CapOperands = OperandCapacity::get(MI.getNumOperands());
689*9880d681SAndroid Build Coastguard Worker Operands = MF.allocateOperandArray(CapOperands);
690*9880d681SAndroid Build Coastguard Worker
691*9880d681SAndroid Build Coastguard Worker // Copy operands.
692*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : MI.operands())
693*9880d681SAndroid Build Coastguard Worker addOperand(MF, MO);
694*9880d681SAndroid Build Coastguard Worker
695*9880d681SAndroid Build Coastguard Worker // Copy all the sensible flags.
696*9880d681SAndroid Build Coastguard Worker setFlags(MI.Flags);
697*9880d681SAndroid Build Coastguard Worker }
698*9880d681SAndroid Build Coastguard Worker
699*9880d681SAndroid Build Coastguard Worker /// getRegInfo - If this instruction is embedded into a MachineFunction,
700*9880d681SAndroid Build Coastguard Worker /// return the MachineRegisterInfo object for the current function, otherwise
701*9880d681SAndroid Build Coastguard Worker /// return null.
getRegInfo()702*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo *MachineInstr::getRegInfo() {
703*9880d681SAndroid Build Coastguard Worker if (MachineBasicBlock *MBB = getParent())
704*9880d681SAndroid Build Coastguard Worker return &MBB->getParent()->getRegInfo();
705*9880d681SAndroid Build Coastguard Worker return nullptr;
706*9880d681SAndroid Build Coastguard Worker }
707*9880d681SAndroid Build Coastguard Worker
708*9880d681SAndroid Build Coastguard Worker // Implement dummy setter and getter for type when
709*9880d681SAndroid Build Coastguard Worker // global-isel is not built.
710*9880d681SAndroid Build Coastguard Worker // The proper implementation is WIP and is tracked here:
711*9880d681SAndroid Build Coastguard Worker // PR26576.
712*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_BUILD_GLOBAL_ISEL
setType(Type * Ty)713*9880d681SAndroid Build Coastguard Worker void MachineInstr::setType(Type *Ty) {}
714*9880d681SAndroid Build Coastguard Worker
getType() const715*9880d681SAndroid Build Coastguard Worker Type *MachineInstr::getType() const { return nullptr; }
716*9880d681SAndroid Build Coastguard Worker
717*9880d681SAndroid Build Coastguard Worker #else
setType(Type * Ty)718*9880d681SAndroid Build Coastguard Worker void MachineInstr::setType(Type *Ty) {
719*9880d681SAndroid Build Coastguard Worker assert((!Ty || isPreISelGenericOpcode(getOpcode())) &&
720*9880d681SAndroid Build Coastguard Worker "Non generic instructions are not supposed to be typed");
721*9880d681SAndroid Build Coastguard Worker this->Ty = Ty;
722*9880d681SAndroid Build Coastguard Worker }
723*9880d681SAndroid Build Coastguard Worker
getType() const724*9880d681SAndroid Build Coastguard Worker Type *MachineInstr::getType() const { return Ty; }
725*9880d681SAndroid Build Coastguard Worker #endif // LLVM_BUILD_GLOBAL_ISEL
726*9880d681SAndroid Build Coastguard Worker
727*9880d681SAndroid Build Coastguard Worker /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
728*9880d681SAndroid Build Coastguard Worker /// this instruction from their respective use lists. This requires that the
729*9880d681SAndroid Build Coastguard Worker /// operands already be on their use lists.
RemoveRegOperandsFromUseLists(MachineRegisterInfo & MRI)730*9880d681SAndroid Build Coastguard Worker void MachineInstr::RemoveRegOperandsFromUseLists(MachineRegisterInfo &MRI) {
731*9880d681SAndroid Build Coastguard Worker for (MachineOperand &MO : operands())
732*9880d681SAndroid Build Coastguard Worker if (MO.isReg())
733*9880d681SAndroid Build Coastguard Worker MRI.removeRegOperandFromUseList(&MO);
734*9880d681SAndroid Build Coastguard Worker }
735*9880d681SAndroid Build Coastguard Worker
736*9880d681SAndroid Build Coastguard Worker /// AddRegOperandsToUseLists - Add all of the register operands in
737*9880d681SAndroid Build Coastguard Worker /// this instruction from their respective use lists. This requires that the
738*9880d681SAndroid Build Coastguard Worker /// operands not be on their use lists yet.
AddRegOperandsToUseLists(MachineRegisterInfo & MRI)739*9880d681SAndroid Build Coastguard Worker void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &MRI) {
740*9880d681SAndroid Build Coastguard Worker for (MachineOperand &MO : operands())
741*9880d681SAndroid Build Coastguard Worker if (MO.isReg())
742*9880d681SAndroid Build Coastguard Worker MRI.addRegOperandToUseList(&MO);
743*9880d681SAndroid Build Coastguard Worker }
744*9880d681SAndroid Build Coastguard Worker
addOperand(const MachineOperand & Op)745*9880d681SAndroid Build Coastguard Worker void MachineInstr::addOperand(const MachineOperand &Op) {
746*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = getParent();
747*9880d681SAndroid Build Coastguard Worker assert(MBB && "Use MachineInstrBuilder to add operands to dangling instrs");
748*9880d681SAndroid Build Coastguard Worker MachineFunction *MF = MBB->getParent();
749*9880d681SAndroid Build Coastguard Worker assert(MF && "Use MachineInstrBuilder to add operands to dangling instrs");
750*9880d681SAndroid Build Coastguard Worker addOperand(*MF, Op);
751*9880d681SAndroid Build Coastguard Worker }
752*9880d681SAndroid Build Coastguard Worker
753*9880d681SAndroid Build Coastguard Worker /// Move NumOps MachineOperands from Src to Dst, with support for overlapping
754*9880d681SAndroid Build Coastguard Worker /// ranges. If MRI is non-null also update use-def chains.
moveOperands(MachineOperand * Dst,MachineOperand * Src,unsigned NumOps,MachineRegisterInfo * MRI)755*9880d681SAndroid Build Coastguard Worker static void moveOperands(MachineOperand *Dst, MachineOperand *Src,
756*9880d681SAndroid Build Coastguard Worker unsigned NumOps, MachineRegisterInfo *MRI) {
757*9880d681SAndroid Build Coastguard Worker if (MRI)
758*9880d681SAndroid Build Coastguard Worker return MRI->moveOperands(Dst, Src, NumOps);
759*9880d681SAndroid Build Coastguard Worker
760*9880d681SAndroid Build Coastguard Worker // MachineOperand is a trivially copyable type so we can just use memmove.
761*9880d681SAndroid Build Coastguard Worker std::memmove(Dst, Src, NumOps * sizeof(MachineOperand));
762*9880d681SAndroid Build Coastguard Worker }
763*9880d681SAndroid Build Coastguard Worker
764*9880d681SAndroid Build Coastguard Worker /// addOperand - Add the specified operand to the instruction. If it is an
765*9880d681SAndroid Build Coastguard Worker /// implicit operand, it is added to the end of the operand list. If it is
766*9880d681SAndroid Build Coastguard Worker /// an explicit operand it is added at the end of the explicit operand list
767*9880d681SAndroid Build Coastguard Worker /// (before the first implicit operand).
addOperand(MachineFunction & MF,const MachineOperand & Op)768*9880d681SAndroid Build Coastguard Worker void MachineInstr::addOperand(MachineFunction &MF, const MachineOperand &Op) {
769*9880d681SAndroid Build Coastguard Worker assert(MCID && "Cannot add operands before providing an instr descriptor");
770*9880d681SAndroid Build Coastguard Worker
771*9880d681SAndroid Build Coastguard Worker // Check if we're adding one of our existing operands.
772*9880d681SAndroid Build Coastguard Worker if (&Op >= Operands && &Op < Operands + NumOperands) {
773*9880d681SAndroid Build Coastguard Worker // This is unusual: MI->addOperand(MI->getOperand(i)).
774*9880d681SAndroid Build Coastguard Worker // If adding Op requires reallocating or moving existing operands around,
775*9880d681SAndroid Build Coastguard Worker // the Op reference could go stale. Support it by copying Op.
776*9880d681SAndroid Build Coastguard Worker MachineOperand CopyOp(Op);
777*9880d681SAndroid Build Coastguard Worker return addOperand(MF, CopyOp);
778*9880d681SAndroid Build Coastguard Worker }
779*9880d681SAndroid Build Coastguard Worker
780*9880d681SAndroid Build Coastguard Worker // Find the insert location for the new operand. Implicit registers go at
781*9880d681SAndroid Build Coastguard Worker // the end, everything else goes before the implicit regs.
782*9880d681SAndroid Build Coastguard Worker //
783*9880d681SAndroid Build Coastguard Worker // FIXME: Allow mixed explicit and implicit operands on inline asm.
784*9880d681SAndroid Build Coastguard Worker // InstrEmitter::EmitSpecialNode() is marking inline asm clobbers as
785*9880d681SAndroid Build Coastguard Worker // implicit-defs, but they must not be moved around. See the FIXME in
786*9880d681SAndroid Build Coastguard Worker // InstrEmitter.cpp.
787*9880d681SAndroid Build Coastguard Worker unsigned OpNo = getNumOperands();
788*9880d681SAndroid Build Coastguard Worker bool isImpReg = Op.isReg() && Op.isImplicit();
789*9880d681SAndroid Build Coastguard Worker if (!isImpReg && !isInlineAsm()) {
790*9880d681SAndroid Build Coastguard Worker while (OpNo && Operands[OpNo-1].isReg() && Operands[OpNo-1].isImplicit()) {
791*9880d681SAndroid Build Coastguard Worker --OpNo;
792*9880d681SAndroid Build Coastguard Worker assert(!Operands[OpNo].isTied() && "Cannot move tied operands");
793*9880d681SAndroid Build Coastguard Worker }
794*9880d681SAndroid Build Coastguard Worker }
795*9880d681SAndroid Build Coastguard Worker
796*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
797*9880d681SAndroid Build Coastguard Worker bool isMetaDataOp = Op.getType() == MachineOperand::MO_Metadata;
798*9880d681SAndroid Build Coastguard Worker // OpNo now points as the desired insertion point. Unless this is a variadic
799*9880d681SAndroid Build Coastguard Worker // instruction, only implicit regs are allowed beyond MCID->getNumOperands().
800*9880d681SAndroid Build Coastguard Worker // RegMask operands go between the explicit and implicit operands.
801*9880d681SAndroid Build Coastguard Worker assert((isImpReg || Op.isRegMask() || MCID->isVariadic() ||
802*9880d681SAndroid Build Coastguard Worker OpNo < MCID->getNumOperands() || isMetaDataOp) &&
803*9880d681SAndroid Build Coastguard Worker "Trying to add an operand to a machine instr that is already done!");
804*9880d681SAndroid Build Coastguard Worker #endif
805*9880d681SAndroid Build Coastguard Worker
806*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo *MRI = getRegInfo();
807*9880d681SAndroid Build Coastguard Worker
808*9880d681SAndroid Build Coastguard Worker // Determine if the Operands array needs to be reallocated.
809*9880d681SAndroid Build Coastguard Worker // Save the old capacity and operand array.
810*9880d681SAndroid Build Coastguard Worker OperandCapacity OldCap = CapOperands;
811*9880d681SAndroid Build Coastguard Worker MachineOperand *OldOperands = Operands;
812*9880d681SAndroid Build Coastguard Worker if (!OldOperands || OldCap.getSize() == getNumOperands()) {
813*9880d681SAndroid Build Coastguard Worker CapOperands = OldOperands ? OldCap.getNext() : OldCap.get(1);
814*9880d681SAndroid Build Coastguard Worker Operands = MF.allocateOperandArray(CapOperands);
815*9880d681SAndroid Build Coastguard Worker // Move the operands before the insertion point.
816*9880d681SAndroid Build Coastguard Worker if (OpNo)
817*9880d681SAndroid Build Coastguard Worker moveOperands(Operands, OldOperands, OpNo, MRI);
818*9880d681SAndroid Build Coastguard Worker }
819*9880d681SAndroid Build Coastguard Worker
820*9880d681SAndroid Build Coastguard Worker // Move the operands following the insertion point.
821*9880d681SAndroid Build Coastguard Worker if (OpNo != NumOperands)
822*9880d681SAndroid Build Coastguard Worker moveOperands(Operands + OpNo + 1, OldOperands + OpNo, NumOperands - OpNo,
823*9880d681SAndroid Build Coastguard Worker MRI);
824*9880d681SAndroid Build Coastguard Worker ++NumOperands;
825*9880d681SAndroid Build Coastguard Worker
826*9880d681SAndroid Build Coastguard Worker // Deallocate the old operand array.
827*9880d681SAndroid Build Coastguard Worker if (OldOperands != Operands && OldOperands)
828*9880d681SAndroid Build Coastguard Worker MF.deallocateOperandArray(OldCap, OldOperands);
829*9880d681SAndroid Build Coastguard Worker
830*9880d681SAndroid Build Coastguard Worker // Copy Op into place. It still needs to be inserted into the MRI use lists.
831*9880d681SAndroid Build Coastguard Worker MachineOperand *NewMO = new (Operands + OpNo) MachineOperand(Op);
832*9880d681SAndroid Build Coastguard Worker NewMO->ParentMI = this;
833*9880d681SAndroid Build Coastguard Worker
834*9880d681SAndroid Build Coastguard Worker // When adding a register operand, tell MRI about it.
835*9880d681SAndroid Build Coastguard Worker if (NewMO->isReg()) {
836*9880d681SAndroid Build Coastguard Worker // Ensure isOnRegUseList() returns false, regardless of Op's status.
837*9880d681SAndroid Build Coastguard Worker NewMO->Contents.Reg.Prev = nullptr;
838*9880d681SAndroid Build Coastguard Worker // Ignore existing ties. This is not a property that can be copied.
839*9880d681SAndroid Build Coastguard Worker NewMO->TiedTo = 0;
840*9880d681SAndroid Build Coastguard Worker // Add the new operand to MRI, but only for instructions in an MBB.
841*9880d681SAndroid Build Coastguard Worker if (MRI)
842*9880d681SAndroid Build Coastguard Worker MRI->addRegOperandToUseList(NewMO);
843*9880d681SAndroid Build Coastguard Worker // The MCID operand information isn't accurate until we start adding
844*9880d681SAndroid Build Coastguard Worker // explicit operands. The implicit operands are added first, then the
845*9880d681SAndroid Build Coastguard Worker // explicits are inserted before them.
846*9880d681SAndroid Build Coastguard Worker if (!isImpReg) {
847*9880d681SAndroid Build Coastguard Worker // Tie uses to defs as indicated in MCInstrDesc.
848*9880d681SAndroid Build Coastguard Worker if (NewMO->isUse()) {
849*9880d681SAndroid Build Coastguard Worker int DefIdx = MCID->getOperandConstraint(OpNo, MCOI::TIED_TO);
850*9880d681SAndroid Build Coastguard Worker if (DefIdx != -1)
851*9880d681SAndroid Build Coastguard Worker tieOperands(DefIdx, OpNo);
852*9880d681SAndroid Build Coastguard Worker }
853*9880d681SAndroid Build Coastguard Worker // If the register operand is flagged as early, mark the operand as such.
854*9880d681SAndroid Build Coastguard Worker if (MCID->getOperandConstraint(OpNo, MCOI::EARLY_CLOBBER) != -1)
855*9880d681SAndroid Build Coastguard Worker NewMO->setIsEarlyClobber(true);
856*9880d681SAndroid Build Coastguard Worker }
857*9880d681SAndroid Build Coastguard Worker }
858*9880d681SAndroid Build Coastguard Worker }
859*9880d681SAndroid Build Coastguard Worker
860*9880d681SAndroid Build Coastguard Worker /// RemoveOperand - Erase an operand from an instruction, leaving it with one
861*9880d681SAndroid Build Coastguard Worker /// fewer operand than it started with.
862*9880d681SAndroid Build Coastguard Worker ///
RemoveOperand(unsigned OpNo)863*9880d681SAndroid Build Coastguard Worker void MachineInstr::RemoveOperand(unsigned OpNo) {
864*9880d681SAndroid Build Coastguard Worker assert(OpNo < getNumOperands() && "Invalid operand number");
865*9880d681SAndroid Build Coastguard Worker untieRegOperand(OpNo);
866*9880d681SAndroid Build Coastguard Worker
867*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
868*9880d681SAndroid Build Coastguard Worker // Moving tied operands would break the ties.
869*9880d681SAndroid Build Coastguard Worker for (unsigned i = OpNo + 1, e = getNumOperands(); i != e; ++i)
870*9880d681SAndroid Build Coastguard Worker if (Operands[i].isReg())
871*9880d681SAndroid Build Coastguard Worker assert(!Operands[i].isTied() && "Cannot move tied operands");
872*9880d681SAndroid Build Coastguard Worker #endif
873*9880d681SAndroid Build Coastguard Worker
874*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo *MRI = getRegInfo();
875*9880d681SAndroid Build Coastguard Worker if (MRI && Operands[OpNo].isReg())
876*9880d681SAndroid Build Coastguard Worker MRI->removeRegOperandFromUseList(Operands + OpNo);
877*9880d681SAndroid Build Coastguard Worker
878*9880d681SAndroid Build Coastguard Worker // Don't call the MachineOperand destructor. A lot of this code depends on
879*9880d681SAndroid Build Coastguard Worker // MachineOperand having a trivial destructor anyway, and adding a call here
880*9880d681SAndroid Build Coastguard Worker // wouldn't make it 'destructor-correct'.
881*9880d681SAndroid Build Coastguard Worker
882*9880d681SAndroid Build Coastguard Worker if (unsigned N = NumOperands - 1 - OpNo)
883*9880d681SAndroid Build Coastguard Worker moveOperands(Operands + OpNo, Operands + OpNo + 1, N, MRI);
884*9880d681SAndroid Build Coastguard Worker --NumOperands;
885*9880d681SAndroid Build Coastguard Worker }
886*9880d681SAndroid Build Coastguard Worker
887*9880d681SAndroid Build Coastguard Worker /// addMemOperand - Add a MachineMemOperand to the machine instruction.
888*9880d681SAndroid Build Coastguard Worker /// This function should be used only occasionally. The setMemRefs function
889*9880d681SAndroid Build Coastguard Worker /// is the primary method for setting up a MachineInstr's MemRefs list.
addMemOperand(MachineFunction & MF,MachineMemOperand * MO)890*9880d681SAndroid Build Coastguard Worker void MachineInstr::addMemOperand(MachineFunction &MF,
891*9880d681SAndroid Build Coastguard Worker MachineMemOperand *MO) {
892*9880d681SAndroid Build Coastguard Worker mmo_iterator OldMemRefs = MemRefs;
893*9880d681SAndroid Build Coastguard Worker unsigned OldNumMemRefs = NumMemRefs;
894*9880d681SAndroid Build Coastguard Worker
895*9880d681SAndroid Build Coastguard Worker unsigned NewNum = NumMemRefs + 1;
896*9880d681SAndroid Build Coastguard Worker mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
897*9880d681SAndroid Build Coastguard Worker
898*9880d681SAndroid Build Coastguard Worker std::copy(OldMemRefs, OldMemRefs + OldNumMemRefs, NewMemRefs);
899*9880d681SAndroid Build Coastguard Worker NewMemRefs[NewNum - 1] = MO;
900*9880d681SAndroid Build Coastguard Worker setMemRefs(NewMemRefs, NewMemRefs + NewNum);
901*9880d681SAndroid Build Coastguard Worker }
902*9880d681SAndroid Build Coastguard Worker
903*9880d681SAndroid Build Coastguard Worker /// Check to see if the MMOs pointed to by the two MemRefs arrays are
904*9880d681SAndroid Build Coastguard Worker /// identical.
hasIdenticalMMOs(const MachineInstr & MI1,const MachineInstr & MI2)905*9880d681SAndroid Build Coastguard Worker static bool hasIdenticalMMOs(const MachineInstr &MI1, const MachineInstr &MI2) {
906*9880d681SAndroid Build Coastguard Worker auto I1 = MI1.memoperands_begin(), E1 = MI1.memoperands_end();
907*9880d681SAndroid Build Coastguard Worker auto I2 = MI2.memoperands_begin(), E2 = MI2.memoperands_end();
908*9880d681SAndroid Build Coastguard Worker if ((E1 - I1) != (E2 - I2))
909*9880d681SAndroid Build Coastguard Worker return false;
910*9880d681SAndroid Build Coastguard Worker for (; I1 != E1; ++I1, ++I2) {
911*9880d681SAndroid Build Coastguard Worker if (**I1 != **I2)
912*9880d681SAndroid Build Coastguard Worker return false;
913*9880d681SAndroid Build Coastguard Worker }
914*9880d681SAndroid Build Coastguard Worker return true;
915*9880d681SAndroid Build Coastguard Worker }
916*9880d681SAndroid Build Coastguard Worker
917*9880d681SAndroid Build Coastguard Worker std::pair<MachineInstr::mmo_iterator, unsigned>
mergeMemRefsWith(const MachineInstr & Other)918*9880d681SAndroid Build Coastguard Worker MachineInstr::mergeMemRefsWith(const MachineInstr& Other) {
919*9880d681SAndroid Build Coastguard Worker
920*9880d681SAndroid Build Coastguard Worker // If either of the incoming memrefs are empty, we must be conservative and
921*9880d681SAndroid Build Coastguard Worker // treat this as if we've exhausted our space for memrefs and dropped them.
922*9880d681SAndroid Build Coastguard Worker if (memoperands_empty() || Other.memoperands_empty())
923*9880d681SAndroid Build Coastguard Worker return std::make_pair(nullptr, 0);
924*9880d681SAndroid Build Coastguard Worker
925*9880d681SAndroid Build Coastguard Worker // If both instructions have identical memrefs, we don't need to merge them.
926*9880d681SAndroid Build Coastguard Worker // Since many instructions have a single memref, and we tend to merge things
927*9880d681SAndroid Build Coastguard Worker // like pairs of loads from the same location, this catches a large number of
928*9880d681SAndroid Build Coastguard Worker // cases in practice.
929*9880d681SAndroid Build Coastguard Worker if (hasIdenticalMMOs(*this, Other))
930*9880d681SAndroid Build Coastguard Worker return std::make_pair(MemRefs, NumMemRefs);
931*9880d681SAndroid Build Coastguard Worker
932*9880d681SAndroid Build Coastguard Worker // TODO: consider uniquing elements within the operand lists to reduce
933*9880d681SAndroid Build Coastguard Worker // space usage and fall back to conservative information less often.
934*9880d681SAndroid Build Coastguard Worker size_t CombinedNumMemRefs = NumMemRefs + Other.NumMemRefs;
935*9880d681SAndroid Build Coastguard Worker
936*9880d681SAndroid Build Coastguard Worker // If we don't have enough room to store this many memrefs, be conservative
937*9880d681SAndroid Build Coastguard Worker // and drop them. Otherwise, we'd fail asserts when trying to add them to
938*9880d681SAndroid Build Coastguard Worker // the new instruction.
939*9880d681SAndroid Build Coastguard Worker if (CombinedNumMemRefs != uint8_t(CombinedNumMemRefs))
940*9880d681SAndroid Build Coastguard Worker return std::make_pair(nullptr, 0);
941*9880d681SAndroid Build Coastguard Worker
942*9880d681SAndroid Build Coastguard Worker MachineFunction *MF = getParent()->getParent();
943*9880d681SAndroid Build Coastguard Worker mmo_iterator MemBegin = MF->allocateMemRefsArray(CombinedNumMemRefs);
944*9880d681SAndroid Build Coastguard Worker mmo_iterator MemEnd = std::copy(memoperands_begin(), memoperands_end(),
945*9880d681SAndroid Build Coastguard Worker MemBegin);
946*9880d681SAndroid Build Coastguard Worker MemEnd = std::copy(Other.memoperands_begin(), Other.memoperands_end(),
947*9880d681SAndroid Build Coastguard Worker MemEnd);
948*9880d681SAndroid Build Coastguard Worker assert(MemEnd - MemBegin == (ptrdiff_t)CombinedNumMemRefs &&
949*9880d681SAndroid Build Coastguard Worker "missing memrefs");
950*9880d681SAndroid Build Coastguard Worker
951*9880d681SAndroid Build Coastguard Worker return std::make_pair(MemBegin, CombinedNumMemRefs);
952*9880d681SAndroid Build Coastguard Worker }
953*9880d681SAndroid Build Coastguard Worker
hasPropertyInBundle(unsigned Mask,QueryType Type) const954*9880d681SAndroid Build Coastguard Worker bool MachineInstr::hasPropertyInBundle(unsigned Mask, QueryType Type) const {
955*9880d681SAndroid Build Coastguard Worker assert(!isBundledWithPred() && "Must be called on bundle header");
956*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::const_instr_iterator MII = getIterator();; ++MII) {
957*9880d681SAndroid Build Coastguard Worker if (MII->getDesc().getFlags() & Mask) {
958*9880d681SAndroid Build Coastguard Worker if (Type == AnyInBundle)
959*9880d681SAndroid Build Coastguard Worker return true;
960*9880d681SAndroid Build Coastguard Worker } else {
961*9880d681SAndroid Build Coastguard Worker if (Type == AllInBundle && !MII->isBundle())
962*9880d681SAndroid Build Coastguard Worker return false;
963*9880d681SAndroid Build Coastguard Worker }
964*9880d681SAndroid Build Coastguard Worker // This was the last instruction in the bundle.
965*9880d681SAndroid Build Coastguard Worker if (!MII->isBundledWithSucc())
966*9880d681SAndroid Build Coastguard Worker return Type == AllInBundle;
967*9880d681SAndroid Build Coastguard Worker }
968*9880d681SAndroid Build Coastguard Worker }
969*9880d681SAndroid Build Coastguard Worker
isIdenticalTo(const MachineInstr & Other,MICheckType Check) const970*9880d681SAndroid Build Coastguard Worker bool MachineInstr::isIdenticalTo(const MachineInstr &Other,
971*9880d681SAndroid Build Coastguard Worker MICheckType Check) const {
972*9880d681SAndroid Build Coastguard Worker // If opcodes or number of operands are not the same then the two
973*9880d681SAndroid Build Coastguard Worker // instructions are obviously not identical.
974*9880d681SAndroid Build Coastguard Worker if (Other.getOpcode() != getOpcode() ||
975*9880d681SAndroid Build Coastguard Worker Other.getNumOperands() != getNumOperands())
976*9880d681SAndroid Build Coastguard Worker return false;
977*9880d681SAndroid Build Coastguard Worker
978*9880d681SAndroid Build Coastguard Worker if (isBundle()) {
979*9880d681SAndroid Build Coastguard Worker // Both instructions are bundles, compare MIs inside the bundle.
980*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::const_instr_iterator I1 = getIterator();
981*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::const_instr_iterator E1 = getParent()->instr_end();
982*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::const_instr_iterator I2 = Other.getIterator();
983*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::const_instr_iterator E2 = Other.getParent()->instr_end();
984*9880d681SAndroid Build Coastguard Worker while (++I1 != E1 && I1->isInsideBundle()) {
985*9880d681SAndroid Build Coastguard Worker ++I2;
986*9880d681SAndroid Build Coastguard Worker if (I2 == E2 || !I2->isInsideBundle() || !I1->isIdenticalTo(*I2, Check))
987*9880d681SAndroid Build Coastguard Worker return false;
988*9880d681SAndroid Build Coastguard Worker }
989*9880d681SAndroid Build Coastguard Worker }
990*9880d681SAndroid Build Coastguard Worker
991*9880d681SAndroid Build Coastguard Worker // Check operands to make sure they match.
992*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
993*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = getOperand(i);
994*9880d681SAndroid Build Coastguard Worker const MachineOperand &OMO = Other.getOperand(i);
995*9880d681SAndroid Build Coastguard Worker if (!MO.isReg()) {
996*9880d681SAndroid Build Coastguard Worker if (!MO.isIdenticalTo(OMO))
997*9880d681SAndroid Build Coastguard Worker return false;
998*9880d681SAndroid Build Coastguard Worker continue;
999*9880d681SAndroid Build Coastguard Worker }
1000*9880d681SAndroid Build Coastguard Worker
1001*9880d681SAndroid Build Coastguard Worker // Clients may or may not want to ignore defs when testing for equality.
1002*9880d681SAndroid Build Coastguard Worker // For example, machine CSE pass only cares about finding common
1003*9880d681SAndroid Build Coastguard Worker // subexpressions, so it's safe to ignore virtual register defs.
1004*9880d681SAndroid Build Coastguard Worker if (MO.isDef()) {
1005*9880d681SAndroid Build Coastguard Worker if (Check == IgnoreDefs)
1006*9880d681SAndroid Build Coastguard Worker continue;
1007*9880d681SAndroid Build Coastguard Worker else if (Check == IgnoreVRegDefs) {
1008*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) ||
1009*9880d681SAndroid Build Coastguard Worker TargetRegisterInfo::isPhysicalRegister(OMO.getReg()))
1010*9880d681SAndroid Build Coastguard Worker if (MO.getReg() != OMO.getReg())
1011*9880d681SAndroid Build Coastguard Worker return false;
1012*9880d681SAndroid Build Coastguard Worker } else {
1013*9880d681SAndroid Build Coastguard Worker if (!MO.isIdenticalTo(OMO))
1014*9880d681SAndroid Build Coastguard Worker return false;
1015*9880d681SAndroid Build Coastguard Worker if (Check == CheckKillDead && MO.isDead() != OMO.isDead())
1016*9880d681SAndroid Build Coastguard Worker return false;
1017*9880d681SAndroid Build Coastguard Worker }
1018*9880d681SAndroid Build Coastguard Worker } else {
1019*9880d681SAndroid Build Coastguard Worker if (!MO.isIdenticalTo(OMO))
1020*9880d681SAndroid Build Coastguard Worker return false;
1021*9880d681SAndroid Build Coastguard Worker if (Check == CheckKillDead && MO.isKill() != OMO.isKill())
1022*9880d681SAndroid Build Coastguard Worker return false;
1023*9880d681SAndroid Build Coastguard Worker }
1024*9880d681SAndroid Build Coastguard Worker }
1025*9880d681SAndroid Build Coastguard Worker // If DebugLoc does not match then two dbg.values are not identical.
1026*9880d681SAndroid Build Coastguard Worker if (isDebugValue())
1027*9880d681SAndroid Build Coastguard Worker if (getDebugLoc() && Other.getDebugLoc() &&
1028*9880d681SAndroid Build Coastguard Worker getDebugLoc() != Other.getDebugLoc())
1029*9880d681SAndroid Build Coastguard Worker return false;
1030*9880d681SAndroid Build Coastguard Worker return true;
1031*9880d681SAndroid Build Coastguard Worker }
1032*9880d681SAndroid Build Coastguard Worker
removeFromParent()1033*9880d681SAndroid Build Coastguard Worker MachineInstr *MachineInstr::removeFromParent() {
1034*9880d681SAndroid Build Coastguard Worker assert(getParent() && "Not embedded in a basic block!");
1035*9880d681SAndroid Build Coastguard Worker return getParent()->remove(this);
1036*9880d681SAndroid Build Coastguard Worker }
1037*9880d681SAndroid Build Coastguard Worker
removeFromBundle()1038*9880d681SAndroid Build Coastguard Worker MachineInstr *MachineInstr::removeFromBundle() {
1039*9880d681SAndroid Build Coastguard Worker assert(getParent() && "Not embedded in a basic block!");
1040*9880d681SAndroid Build Coastguard Worker return getParent()->remove_instr(this);
1041*9880d681SAndroid Build Coastguard Worker }
1042*9880d681SAndroid Build Coastguard Worker
eraseFromParent()1043*9880d681SAndroid Build Coastguard Worker void MachineInstr::eraseFromParent() {
1044*9880d681SAndroid Build Coastguard Worker assert(getParent() && "Not embedded in a basic block!");
1045*9880d681SAndroid Build Coastguard Worker getParent()->erase(this);
1046*9880d681SAndroid Build Coastguard Worker }
1047*9880d681SAndroid Build Coastguard Worker
eraseFromParentAndMarkDBGValuesForRemoval()1048*9880d681SAndroid Build Coastguard Worker void MachineInstr::eraseFromParentAndMarkDBGValuesForRemoval() {
1049*9880d681SAndroid Build Coastguard Worker assert(getParent() && "Not embedded in a basic block!");
1050*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = getParent();
1051*9880d681SAndroid Build Coastguard Worker MachineFunction *MF = MBB->getParent();
1052*9880d681SAndroid Build Coastguard Worker assert(MF && "Not embedded in a function!");
1053*9880d681SAndroid Build Coastguard Worker
1054*9880d681SAndroid Build Coastguard Worker MachineInstr *MI = (MachineInstr *)this;
1055*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo &MRI = MF->getRegInfo();
1056*9880d681SAndroid Build Coastguard Worker
1057*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : MI->operands()) {
1058*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isDef())
1059*9880d681SAndroid Build Coastguard Worker continue;
1060*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
1061*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isVirtualRegister(Reg))
1062*9880d681SAndroid Build Coastguard Worker continue;
1063*9880d681SAndroid Build Coastguard Worker MRI.markUsesInDebugValueAsUndef(Reg);
1064*9880d681SAndroid Build Coastguard Worker }
1065*9880d681SAndroid Build Coastguard Worker MI->eraseFromParent();
1066*9880d681SAndroid Build Coastguard Worker }
1067*9880d681SAndroid Build Coastguard Worker
eraseFromBundle()1068*9880d681SAndroid Build Coastguard Worker void MachineInstr::eraseFromBundle() {
1069*9880d681SAndroid Build Coastguard Worker assert(getParent() && "Not embedded in a basic block!");
1070*9880d681SAndroid Build Coastguard Worker getParent()->erase_instr(this);
1071*9880d681SAndroid Build Coastguard Worker }
1072*9880d681SAndroid Build Coastguard Worker
1073*9880d681SAndroid Build Coastguard Worker /// getNumExplicitOperands - Returns the number of non-implicit operands.
1074*9880d681SAndroid Build Coastguard Worker ///
getNumExplicitOperands() const1075*9880d681SAndroid Build Coastguard Worker unsigned MachineInstr::getNumExplicitOperands() const {
1076*9880d681SAndroid Build Coastguard Worker unsigned NumOperands = MCID->getNumOperands();
1077*9880d681SAndroid Build Coastguard Worker if (!MCID->isVariadic())
1078*9880d681SAndroid Build Coastguard Worker return NumOperands;
1079*9880d681SAndroid Build Coastguard Worker
1080*9880d681SAndroid Build Coastguard Worker for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
1081*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = getOperand(i);
1082*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isImplicit())
1083*9880d681SAndroid Build Coastguard Worker NumOperands++;
1084*9880d681SAndroid Build Coastguard Worker }
1085*9880d681SAndroid Build Coastguard Worker return NumOperands;
1086*9880d681SAndroid Build Coastguard Worker }
1087*9880d681SAndroid Build Coastguard Worker
bundleWithPred()1088*9880d681SAndroid Build Coastguard Worker void MachineInstr::bundleWithPred() {
1089*9880d681SAndroid Build Coastguard Worker assert(!isBundledWithPred() && "MI is already bundled with its predecessor");
1090*9880d681SAndroid Build Coastguard Worker setFlag(BundledPred);
1091*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::instr_iterator Pred = getIterator();
1092*9880d681SAndroid Build Coastguard Worker --Pred;
1093*9880d681SAndroid Build Coastguard Worker assert(!Pred->isBundledWithSucc() && "Inconsistent bundle flags");
1094*9880d681SAndroid Build Coastguard Worker Pred->setFlag(BundledSucc);
1095*9880d681SAndroid Build Coastguard Worker }
1096*9880d681SAndroid Build Coastguard Worker
bundleWithSucc()1097*9880d681SAndroid Build Coastguard Worker void MachineInstr::bundleWithSucc() {
1098*9880d681SAndroid Build Coastguard Worker assert(!isBundledWithSucc() && "MI is already bundled with its successor");
1099*9880d681SAndroid Build Coastguard Worker setFlag(BundledSucc);
1100*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::instr_iterator Succ = getIterator();
1101*9880d681SAndroid Build Coastguard Worker ++Succ;
1102*9880d681SAndroid Build Coastguard Worker assert(!Succ->isBundledWithPred() && "Inconsistent bundle flags");
1103*9880d681SAndroid Build Coastguard Worker Succ->setFlag(BundledPred);
1104*9880d681SAndroid Build Coastguard Worker }
1105*9880d681SAndroid Build Coastguard Worker
unbundleFromPred()1106*9880d681SAndroid Build Coastguard Worker void MachineInstr::unbundleFromPred() {
1107*9880d681SAndroid Build Coastguard Worker assert(isBundledWithPred() && "MI isn't bundled with its predecessor");
1108*9880d681SAndroid Build Coastguard Worker clearFlag(BundledPred);
1109*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::instr_iterator Pred = getIterator();
1110*9880d681SAndroid Build Coastguard Worker --Pred;
1111*9880d681SAndroid Build Coastguard Worker assert(Pred->isBundledWithSucc() && "Inconsistent bundle flags");
1112*9880d681SAndroid Build Coastguard Worker Pred->clearFlag(BundledSucc);
1113*9880d681SAndroid Build Coastguard Worker }
1114*9880d681SAndroid Build Coastguard Worker
unbundleFromSucc()1115*9880d681SAndroid Build Coastguard Worker void MachineInstr::unbundleFromSucc() {
1116*9880d681SAndroid Build Coastguard Worker assert(isBundledWithSucc() && "MI isn't bundled with its successor");
1117*9880d681SAndroid Build Coastguard Worker clearFlag(BundledSucc);
1118*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::instr_iterator Succ = getIterator();
1119*9880d681SAndroid Build Coastguard Worker ++Succ;
1120*9880d681SAndroid Build Coastguard Worker assert(Succ->isBundledWithPred() && "Inconsistent bundle flags");
1121*9880d681SAndroid Build Coastguard Worker Succ->clearFlag(BundledPred);
1122*9880d681SAndroid Build Coastguard Worker }
1123*9880d681SAndroid Build Coastguard Worker
isStackAligningInlineAsm() const1124*9880d681SAndroid Build Coastguard Worker bool MachineInstr::isStackAligningInlineAsm() const {
1125*9880d681SAndroid Build Coastguard Worker if (isInlineAsm()) {
1126*9880d681SAndroid Build Coastguard Worker unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1127*9880d681SAndroid Build Coastguard Worker if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1128*9880d681SAndroid Build Coastguard Worker return true;
1129*9880d681SAndroid Build Coastguard Worker }
1130*9880d681SAndroid Build Coastguard Worker return false;
1131*9880d681SAndroid Build Coastguard Worker }
1132*9880d681SAndroid Build Coastguard Worker
getInlineAsmDialect() const1133*9880d681SAndroid Build Coastguard Worker InlineAsm::AsmDialect MachineInstr::getInlineAsmDialect() const {
1134*9880d681SAndroid Build Coastguard Worker assert(isInlineAsm() && "getInlineAsmDialect() only works for inline asms!");
1135*9880d681SAndroid Build Coastguard Worker unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1136*9880d681SAndroid Build Coastguard Worker return InlineAsm::AsmDialect((ExtraInfo & InlineAsm::Extra_AsmDialect) != 0);
1137*9880d681SAndroid Build Coastguard Worker }
1138*9880d681SAndroid Build Coastguard Worker
findInlineAsmFlagIdx(unsigned OpIdx,unsigned * GroupNo) const1139*9880d681SAndroid Build Coastguard Worker int MachineInstr::findInlineAsmFlagIdx(unsigned OpIdx,
1140*9880d681SAndroid Build Coastguard Worker unsigned *GroupNo) const {
1141*9880d681SAndroid Build Coastguard Worker assert(isInlineAsm() && "Expected an inline asm instruction");
1142*9880d681SAndroid Build Coastguard Worker assert(OpIdx < getNumOperands() && "OpIdx out of range");
1143*9880d681SAndroid Build Coastguard Worker
1144*9880d681SAndroid Build Coastguard Worker // Ignore queries about the initial operands.
1145*9880d681SAndroid Build Coastguard Worker if (OpIdx < InlineAsm::MIOp_FirstOperand)
1146*9880d681SAndroid Build Coastguard Worker return -1;
1147*9880d681SAndroid Build Coastguard Worker
1148*9880d681SAndroid Build Coastguard Worker unsigned Group = 0;
1149*9880d681SAndroid Build Coastguard Worker unsigned NumOps;
1150*9880d681SAndroid Build Coastguard Worker for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1151*9880d681SAndroid Build Coastguard Worker i += NumOps) {
1152*9880d681SAndroid Build Coastguard Worker const MachineOperand &FlagMO = getOperand(i);
1153*9880d681SAndroid Build Coastguard Worker // If we reach the implicit register operands, stop looking.
1154*9880d681SAndroid Build Coastguard Worker if (!FlagMO.isImm())
1155*9880d681SAndroid Build Coastguard Worker return -1;
1156*9880d681SAndroid Build Coastguard Worker NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1157*9880d681SAndroid Build Coastguard Worker if (i + NumOps > OpIdx) {
1158*9880d681SAndroid Build Coastguard Worker if (GroupNo)
1159*9880d681SAndroid Build Coastguard Worker *GroupNo = Group;
1160*9880d681SAndroid Build Coastguard Worker return i;
1161*9880d681SAndroid Build Coastguard Worker }
1162*9880d681SAndroid Build Coastguard Worker ++Group;
1163*9880d681SAndroid Build Coastguard Worker }
1164*9880d681SAndroid Build Coastguard Worker return -1;
1165*9880d681SAndroid Build Coastguard Worker }
1166*9880d681SAndroid Build Coastguard Worker
getDebugVariable() const1167*9880d681SAndroid Build Coastguard Worker const DILocalVariable *MachineInstr::getDebugVariable() const {
1168*9880d681SAndroid Build Coastguard Worker assert(isDebugValue() && "not a DBG_VALUE");
1169*9880d681SAndroid Build Coastguard Worker return cast<DILocalVariable>(getOperand(2).getMetadata());
1170*9880d681SAndroid Build Coastguard Worker }
1171*9880d681SAndroid Build Coastguard Worker
getDebugExpression() const1172*9880d681SAndroid Build Coastguard Worker const DIExpression *MachineInstr::getDebugExpression() const {
1173*9880d681SAndroid Build Coastguard Worker assert(isDebugValue() && "not a DBG_VALUE");
1174*9880d681SAndroid Build Coastguard Worker return cast<DIExpression>(getOperand(3).getMetadata());
1175*9880d681SAndroid Build Coastguard Worker }
1176*9880d681SAndroid Build Coastguard Worker
1177*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass*
getRegClassConstraint(unsigned OpIdx,const TargetInstrInfo * TII,const TargetRegisterInfo * TRI) const1178*9880d681SAndroid Build Coastguard Worker MachineInstr::getRegClassConstraint(unsigned OpIdx,
1179*9880d681SAndroid Build Coastguard Worker const TargetInstrInfo *TII,
1180*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI) const {
1181*9880d681SAndroid Build Coastguard Worker assert(getParent() && "Can't have an MBB reference here!");
1182*9880d681SAndroid Build Coastguard Worker assert(getParent()->getParent() && "Can't have an MF reference here!");
1183*9880d681SAndroid Build Coastguard Worker const MachineFunction &MF = *getParent()->getParent();
1184*9880d681SAndroid Build Coastguard Worker
1185*9880d681SAndroid Build Coastguard Worker // Most opcodes have fixed constraints in their MCInstrDesc.
1186*9880d681SAndroid Build Coastguard Worker if (!isInlineAsm())
1187*9880d681SAndroid Build Coastguard Worker return TII->getRegClass(getDesc(), OpIdx, TRI, MF);
1188*9880d681SAndroid Build Coastguard Worker
1189*9880d681SAndroid Build Coastguard Worker if (!getOperand(OpIdx).isReg())
1190*9880d681SAndroid Build Coastguard Worker return nullptr;
1191*9880d681SAndroid Build Coastguard Worker
1192*9880d681SAndroid Build Coastguard Worker // For tied uses on inline asm, get the constraint from the def.
1193*9880d681SAndroid Build Coastguard Worker unsigned DefIdx;
1194*9880d681SAndroid Build Coastguard Worker if (getOperand(OpIdx).isUse() && isRegTiedToDefOperand(OpIdx, &DefIdx))
1195*9880d681SAndroid Build Coastguard Worker OpIdx = DefIdx;
1196*9880d681SAndroid Build Coastguard Worker
1197*9880d681SAndroid Build Coastguard Worker // Inline asm stores register class constraints in the flag word.
1198*9880d681SAndroid Build Coastguard Worker int FlagIdx = findInlineAsmFlagIdx(OpIdx);
1199*9880d681SAndroid Build Coastguard Worker if (FlagIdx < 0)
1200*9880d681SAndroid Build Coastguard Worker return nullptr;
1201*9880d681SAndroid Build Coastguard Worker
1202*9880d681SAndroid Build Coastguard Worker unsigned Flag = getOperand(FlagIdx).getImm();
1203*9880d681SAndroid Build Coastguard Worker unsigned RCID;
1204*9880d681SAndroid Build Coastguard Worker if (InlineAsm::hasRegClassConstraint(Flag, RCID))
1205*9880d681SAndroid Build Coastguard Worker return TRI->getRegClass(RCID);
1206*9880d681SAndroid Build Coastguard Worker
1207*9880d681SAndroid Build Coastguard Worker // Assume that all registers in a memory operand are pointers.
1208*9880d681SAndroid Build Coastguard Worker if (InlineAsm::getKind(Flag) == InlineAsm::Kind_Mem)
1209*9880d681SAndroid Build Coastguard Worker return TRI->getPointerRegClass(MF);
1210*9880d681SAndroid Build Coastguard Worker
1211*9880d681SAndroid Build Coastguard Worker return nullptr;
1212*9880d681SAndroid Build Coastguard Worker }
1213*9880d681SAndroid Build Coastguard Worker
getRegClassConstraintEffectForVReg(unsigned Reg,const TargetRegisterClass * CurRC,const TargetInstrInfo * TII,const TargetRegisterInfo * TRI,bool ExploreBundle) const1214*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVReg(
1215*9880d681SAndroid Build Coastguard Worker unsigned Reg, const TargetRegisterClass *CurRC, const TargetInstrInfo *TII,
1216*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI, bool ExploreBundle) const {
1217*9880d681SAndroid Build Coastguard Worker // Check every operands inside the bundle if we have
1218*9880d681SAndroid Build Coastguard Worker // been asked to.
1219*9880d681SAndroid Build Coastguard Worker if (ExploreBundle)
1220*9880d681SAndroid Build Coastguard Worker for (ConstMIBundleOperands OpndIt(*this); OpndIt.isValid() && CurRC;
1221*9880d681SAndroid Build Coastguard Worker ++OpndIt)
1222*9880d681SAndroid Build Coastguard Worker CurRC = OpndIt->getParent()->getRegClassConstraintEffectForVRegImpl(
1223*9880d681SAndroid Build Coastguard Worker OpndIt.getOperandNo(), Reg, CurRC, TII, TRI);
1224*9880d681SAndroid Build Coastguard Worker else
1225*9880d681SAndroid Build Coastguard Worker // Otherwise, just check the current operands.
1226*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = NumOperands; i < e && CurRC; ++i)
1227*9880d681SAndroid Build Coastguard Worker CurRC = getRegClassConstraintEffectForVRegImpl(i, Reg, CurRC, TII, TRI);
1228*9880d681SAndroid Build Coastguard Worker return CurRC;
1229*9880d681SAndroid Build Coastguard Worker }
1230*9880d681SAndroid Build Coastguard Worker
getRegClassConstraintEffectForVRegImpl(unsigned OpIdx,unsigned Reg,const TargetRegisterClass * CurRC,const TargetInstrInfo * TII,const TargetRegisterInfo * TRI) const1231*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVRegImpl(
1232*9880d681SAndroid Build Coastguard Worker unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC,
1233*9880d681SAndroid Build Coastguard Worker const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
1234*9880d681SAndroid Build Coastguard Worker assert(CurRC && "Invalid initial register class");
1235*9880d681SAndroid Build Coastguard Worker // Check if Reg is constrained by some of its use/def from MI.
1236*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = getOperand(OpIdx);
1237*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || MO.getReg() != Reg)
1238*9880d681SAndroid Build Coastguard Worker return CurRC;
1239*9880d681SAndroid Build Coastguard Worker // If yes, accumulate the constraints through the operand.
1240*9880d681SAndroid Build Coastguard Worker return getRegClassConstraintEffect(OpIdx, CurRC, TII, TRI);
1241*9880d681SAndroid Build Coastguard Worker }
1242*9880d681SAndroid Build Coastguard Worker
getRegClassConstraintEffect(unsigned OpIdx,const TargetRegisterClass * CurRC,const TargetInstrInfo * TII,const TargetRegisterInfo * TRI) const1243*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *MachineInstr::getRegClassConstraintEffect(
1244*9880d681SAndroid Build Coastguard Worker unsigned OpIdx, const TargetRegisterClass *CurRC,
1245*9880d681SAndroid Build Coastguard Worker const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
1246*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *OpRC = getRegClassConstraint(OpIdx, TII, TRI);
1247*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = getOperand(OpIdx);
1248*9880d681SAndroid Build Coastguard Worker assert(MO.isReg() &&
1249*9880d681SAndroid Build Coastguard Worker "Cannot get register constraints for non-register operand");
1250*9880d681SAndroid Build Coastguard Worker assert(CurRC && "Invalid initial register class");
1251*9880d681SAndroid Build Coastguard Worker if (unsigned SubIdx = MO.getSubReg()) {
1252*9880d681SAndroid Build Coastguard Worker if (OpRC)
1253*9880d681SAndroid Build Coastguard Worker CurRC = TRI->getMatchingSuperRegClass(CurRC, OpRC, SubIdx);
1254*9880d681SAndroid Build Coastguard Worker else
1255*9880d681SAndroid Build Coastguard Worker CurRC = TRI->getSubClassWithSubReg(CurRC, SubIdx);
1256*9880d681SAndroid Build Coastguard Worker } else if (OpRC)
1257*9880d681SAndroid Build Coastguard Worker CurRC = TRI->getCommonSubClass(CurRC, OpRC);
1258*9880d681SAndroid Build Coastguard Worker return CurRC;
1259*9880d681SAndroid Build Coastguard Worker }
1260*9880d681SAndroid Build Coastguard Worker
1261*9880d681SAndroid Build Coastguard Worker /// Return the number of instructions inside the MI bundle, not counting the
1262*9880d681SAndroid Build Coastguard Worker /// header instruction.
getBundleSize() const1263*9880d681SAndroid Build Coastguard Worker unsigned MachineInstr::getBundleSize() const {
1264*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::const_instr_iterator I = getIterator();
1265*9880d681SAndroid Build Coastguard Worker unsigned Size = 0;
1266*9880d681SAndroid Build Coastguard Worker while (I->isBundledWithSucc()) {
1267*9880d681SAndroid Build Coastguard Worker ++Size;
1268*9880d681SAndroid Build Coastguard Worker ++I;
1269*9880d681SAndroid Build Coastguard Worker }
1270*9880d681SAndroid Build Coastguard Worker return Size;
1271*9880d681SAndroid Build Coastguard Worker }
1272*9880d681SAndroid Build Coastguard Worker
1273*9880d681SAndroid Build Coastguard Worker /// Returns true if the MachineInstr has an implicit-use operand of exactly
1274*9880d681SAndroid Build Coastguard Worker /// the given register (not considering sub/super-registers).
hasRegisterImplicitUseOperand(unsigned Reg) const1275*9880d681SAndroid Build Coastguard Worker bool MachineInstr::hasRegisterImplicitUseOperand(unsigned Reg) const {
1276*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1277*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = getOperand(i);
1278*9880d681SAndroid Build Coastguard Worker if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == Reg)
1279*9880d681SAndroid Build Coastguard Worker return true;
1280*9880d681SAndroid Build Coastguard Worker }
1281*9880d681SAndroid Build Coastguard Worker return false;
1282*9880d681SAndroid Build Coastguard Worker }
1283*9880d681SAndroid Build Coastguard Worker
1284*9880d681SAndroid Build Coastguard Worker /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
1285*9880d681SAndroid Build Coastguard Worker /// the specific register or -1 if it is not found. It further tightens
1286*9880d681SAndroid Build Coastguard Worker /// the search criteria to a use that kills the register if isKill is true.
findRegisterUseOperandIdx(unsigned Reg,bool isKill,const TargetRegisterInfo * TRI) const1287*9880d681SAndroid Build Coastguard Worker int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
1288*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI) const {
1289*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1290*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = getOperand(i);
1291*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isUse())
1292*9880d681SAndroid Build Coastguard Worker continue;
1293*9880d681SAndroid Build Coastguard Worker unsigned MOReg = MO.getReg();
1294*9880d681SAndroid Build Coastguard Worker if (!MOReg)
1295*9880d681SAndroid Build Coastguard Worker continue;
1296*9880d681SAndroid Build Coastguard Worker if (MOReg == Reg ||
1297*9880d681SAndroid Build Coastguard Worker (TRI &&
1298*9880d681SAndroid Build Coastguard Worker TargetRegisterInfo::isPhysicalRegister(MOReg) &&
1299*9880d681SAndroid Build Coastguard Worker TargetRegisterInfo::isPhysicalRegister(Reg) &&
1300*9880d681SAndroid Build Coastguard Worker TRI->isSubRegister(MOReg, Reg)))
1301*9880d681SAndroid Build Coastguard Worker if (!isKill || MO.isKill())
1302*9880d681SAndroid Build Coastguard Worker return i;
1303*9880d681SAndroid Build Coastguard Worker }
1304*9880d681SAndroid Build Coastguard Worker return -1;
1305*9880d681SAndroid Build Coastguard Worker }
1306*9880d681SAndroid Build Coastguard Worker
1307*9880d681SAndroid Build Coastguard Worker /// readsWritesVirtualRegister - Return a pair of bools (reads, writes)
1308*9880d681SAndroid Build Coastguard Worker /// indicating if this instruction reads or writes Reg. This also considers
1309*9880d681SAndroid Build Coastguard Worker /// partial defines.
1310*9880d681SAndroid Build Coastguard Worker std::pair<bool,bool>
readsWritesVirtualRegister(unsigned Reg,SmallVectorImpl<unsigned> * Ops) const1311*9880d681SAndroid Build Coastguard Worker MachineInstr::readsWritesVirtualRegister(unsigned Reg,
1312*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<unsigned> *Ops) const {
1313*9880d681SAndroid Build Coastguard Worker bool PartDef = false; // Partial redefine.
1314*9880d681SAndroid Build Coastguard Worker bool FullDef = false; // Full define.
1315*9880d681SAndroid Build Coastguard Worker bool Use = false;
1316*9880d681SAndroid Build Coastguard Worker
1317*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1318*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = getOperand(i);
1319*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || MO.getReg() != Reg)
1320*9880d681SAndroid Build Coastguard Worker continue;
1321*9880d681SAndroid Build Coastguard Worker if (Ops)
1322*9880d681SAndroid Build Coastguard Worker Ops->push_back(i);
1323*9880d681SAndroid Build Coastguard Worker if (MO.isUse())
1324*9880d681SAndroid Build Coastguard Worker Use |= !MO.isUndef();
1325*9880d681SAndroid Build Coastguard Worker else if (MO.getSubReg() && !MO.isUndef())
1326*9880d681SAndroid Build Coastguard Worker // A partial <def,undef> doesn't count as reading the register.
1327*9880d681SAndroid Build Coastguard Worker PartDef = true;
1328*9880d681SAndroid Build Coastguard Worker else
1329*9880d681SAndroid Build Coastguard Worker FullDef = true;
1330*9880d681SAndroid Build Coastguard Worker }
1331*9880d681SAndroid Build Coastguard Worker // A partial redefine uses Reg unless there is also a full define.
1332*9880d681SAndroid Build Coastguard Worker return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef);
1333*9880d681SAndroid Build Coastguard Worker }
1334*9880d681SAndroid Build Coastguard Worker
1335*9880d681SAndroid Build Coastguard Worker /// findRegisterDefOperandIdx() - Returns the operand index that is a def of
1336*9880d681SAndroid Build Coastguard Worker /// the specified register or -1 if it is not found. If isDead is true, defs
1337*9880d681SAndroid Build Coastguard Worker /// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
1338*9880d681SAndroid Build Coastguard Worker /// also checks if there is a def of a super-register.
1339*9880d681SAndroid Build Coastguard Worker int
findRegisterDefOperandIdx(unsigned Reg,bool isDead,bool Overlap,const TargetRegisterInfo * TRI) const1340*9880d681SAndroid Build Coastguard Worker MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap,
1341*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI) const {
1342*9880d681SAndroid Build Coastguard Worker bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg);
1343*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1344*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = getOperand(i);
1345*9880d681SAndroid Build Coastguard Worker // Accept regmask operands when Overlap is set.
1346*9880d681SAndroid Build Coastguard Worker // Ignore them when looking for a specific def operand (Overlap == false).
1347*9880d681SAndroid Build Coastguard Worker if (isPhys && Overlap && MO.isRegMask() && MO.clobbersPhysReg(Reg))
1348*9880d681SAndroid Build Coastguard Worker return i;
1349*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isDef())
1350*9880d681SAndroid Build Coastguard Worker continue;
1351*9880d681SAndroid Build Coastguard Worker unsigned MOReg = MO.getReg();
1352*9880d681SAndroid Build Coastguard Worker bool Found = (MOReg == Reg);
1353*9880d681SAndroid Build Coastguard Worker if (!Found && TRI && isPhys &&
1354*9880d681SAndroid Build Coastguard Worker TargetRegisterInfo::isPhysicalRegister(MOReg)) {
1355*9880d681SAndroid Build Coastguard Worker if (Overlap)
1356*9880d681SAndroid Build Coastguard Worker Found = TRI->regsOverlap(MOReg, Reg);
1357*9880d681SAndroid Build Coastguard Worker else
1358*9880d681SAndroid Build Coastguard Worker Found = TRI->isSubRegister(MOReg, Reg);
1359*9880d681SAndroid Build Coastguard Worker }
1360*9880d681SAndroid Build Coastguard Worker if (Found && (!isDead || MO.isDead()))
1361*9880d681SAndroid Build Coastguard Worker return i;
1362*9880d681SAndroid Build Coastguard Worker }
1363*9880d681SAndroid Build Coastguard Worker return -1;
1364*9880d681SAndroid Build Coastguard Worker }
1365*9880d681SAndroid Build Coastguard Worker
1366*9880d681SAndroid Build Coastguard Worker /// findFirstPredOperandIdx() - Find the index of the first operand in the
1367*9880d681SAndroid Build Coastguard Worker /// operand list that is used to represent the predicate. It returns -1 if
1368*9880d681SAndroid Build Coastguard Worker /// none is found.
findFirstPredOperandIdx() const1369*9880d681SAndroid Build Coastguard Worker int MachineInstr::findFirstPredOperandIdx() const {
1370*9880d681SAndroid Build Coastguard Worker // Don't call MCID.findFirstPredOperandIdx() because this variant
1371*9880d681SAndroid Build Coastguard Worker // is sometimes called on an instruction that's not yet complete, and
1372*9880d681SAndroid Build Coastguard Worker // so the number of operands is less than the MCID indicates. In
1373*9880d681SAndroid Build Coastguard Worker // particular, the PTX target does this.
1374*9880d681SAndroid Build Coastguard Worker const MCInstrDesc &MCID = getDesc();
1375*9880d681SAndroid Build Coastguard Worker if (MCID.isPredicable()) {
1376*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1377*9880d681SAndroid Build Coastguard Worker if (MCID.OpInfo[i].isPredicate())
1378*9880d681SAndroid Build Coastguard Worker return i;
1379*9880d681SAndroid Build Coastguard Worker }
1380*9880d681SAndroid Build Coastguard Worker
1381*9880d681SAndroid Build Coastguard Worker return -1;
1382*9880d681SAndroid Build Coastguard Worker }
1383*9880d681SAndroid Build Coastguard Worker
1384*9880d681SAndroid Build Coastguard Worker // MachineOperand::TiedTo is 4 bits wide.
1385*9880d681SAndroid Build Coastguard Worker const unsigned TiedMax = 15;
1386*9880d681SAndroid Build Coastguard Worker
1387*9880d681SAndroid Build Coastguard Worker /// tieOperands - Mark operands at DefIdx and UseIdx as tied to each other.
1388*9880d681SAndroid Build Coastguard Worker ///
1389*9880d681SAndroid Build Coastguard Worker /// Use and def operands can be tied together, indicated by a non-zero TiedTo
1390*9880d681SAndroid Build Coastguard Worker /// field. TiedTo can have these values:
1391*9880d681SAndroid Build Coastguard Worker ///
1392*9880d681SAndroid Build Coastguard Worker /// 0: Operand is not tied to anything.
1393*9880d681SAndroid Build Coastguard Worker /// 1 to TiedMax-1: Tied to getOperand(TiedTo-1).
1394*9880d681SAndroid Build Coastguard Worker /// TiedMax: Tied to an operand >= TiedMax-1.
1395*9880d681SAndroid Build Coastguard Worker ///
1396*9880d681SAndroid Build Coastguard Worker /// The tied def must be one of the first TiedMax operands on a normal
1397*9880d681SAndroid Build Coastguard Worker /// instruction. INLINEASM instructions allow more tied defs.
1398*9880d681SAndroid Build Coastguard Worker ///
tieOperands(unsigned DefIdx,unsigned UseIdx)1399*9880d681SAndroid Build Coastguard Worker void MachineInstr::tieOperands(unsigned DefIdx, unsigned UseIdx) {
1400*9880d681SAndroid Build Coastguard Worker MachineOperand &DefMO = getOperand(DefIdx);
1401*9880d681SAndroid Build Coastguard Worker MachineOperand &UseMO = getOperand(UseIdx);
1402*9880d681SAndroid Build Coastguard Worker assert(DefMO.isDef() && "DefIdx must be a def operand");
1403*9880d681SAndroid Build Coastguard Worker assert(UseMO.isUse() && "UseIdx must be a use operand");
1404*9880d681SAndroid Build Coastguard Worker assert(!DefMO.isTied() && "Def is already tied to another use");
1405*9880d681SAndroid Build Coastguard Worker assert(!UseMO.isTied() && "Use is already tied to another def");
1406*9880d681SAndroid Build Coastguard Worker
1407*9880d681SAndroid Build Coastguard Worker if (DefIdx < TiedMax)
1408*9880d681SAndroid Build Coastguard Worker UseMO.TiedTo = DefIdx + 1;
1409*9880d681SAndroid Build Coastguard Worker else {
1410*9880d681SAndroid Build Coastguard Worker // Inline asm can use the group descriptors to find tied operands, but on
1411*9880d681SAndroid Build Coastguard Worker // normal instruction, the tied def must be within the first TiedMax
1412*9880d681SAndroid Build Coastguard Worker // operands.
1413*9880d681SAndroid Build Coastguard Worker assert(isInlineAsm() && "DefIdx out of range");
1414*9880d681SAndroid Build Coastguard Worker UseMO.TiedTo = TiedMax;
1415*9880d681SAndroid Build Coastguard Worker }
1416*9880d681SAndroid Build Coastguard Worker
1417*9880d681SAndroid Build Coastguard Worker // UseIdx can be out of range, we'll search for it in findTiedOperandIdx().
1418*9880d681SAndroid Build Coastguard Worker DefMO.TiedTo = std::min(UseIdx + 1, TiedMax);
1419*9880d681SAndroid Build Coastguard Worker }
1420*9880d681SAndroid Build Coastguard Worker
1421*9880d681SAndroid Build Coastguard Worker /// Given the index of a tied register operand, find the operand it is tied to.
1422*9880d681SAndroid Build Coastguard Worker /// Defs are tied to uses and vice versa. Returns the index of the tied operand
1423*9880d681SAndroid Build Coastguard Worker /// which must exist.
findTiedOperandIdx(unsigned OpIdx) const1424*9880d681SAndroid Build Coastguard Worker unsigned MachineInstr::findTiedOperandIdx(unsigned OpIdx) const {
1425*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = getOperand(OpIdx);
1426*9880d681SAndroid Build Coastguard Worker assert(MO.isTied() && "Operand isn't tied");
1427*9880d681SAndroid Build Coastguard Worker
1428*9880d681SAndroid Build Coastguard Worker // Normally TiedTo is in range.
1429*9880d681SAndroid Build Coastguard Worker if (MO.TiedTo < TiedMax)
1430*9880d681SAndroid Build Coastguard Worker return MO.TiedTo - 1;
1431*9880d681SAndroid Build Coastguard Worker
1432*9880d681SAndroid Build Coastguard Worker // Uses on normal instructions can be out of range.
1433*9880d681SAndroid Build Coastguard Worker if (!isInlineAsm()) {
1434*9880d681SAndroid Build Coastguard Worker // Normal tied defs must be in the 0..TiedMax-1 range.
1435*9880d681SAndroid Build Coastguard Worker if (MO.isUse())
1436*9880d681SAndroid Build Coastguard Worker return TiedMax - 1;
1437*9880d681SAndroid Build Coastguard Worker // MO is a def. Search for the tied use.
1438*9880d681SAndroid Build Coastguard Worker for (unsigned i = TiedMax - 1, e = getNumOperands(); i != e; ++i) {
1439*9880d681SAndroid Build Coastguard Worker const MachineOperand &UseMO = getOperand(i);
1440*9880d681SAndroid Build Coastguard Worker if (UseMO.isReg() && UseMO.isUse() && UseMO.TiedTo == OpIdx + 1)
1441*9880d681SAndroid Build Coastguard Worker return i;
1442*9880d681SAndroid Build Coastguard Worker }
1443*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Can't find tied use");
1444*9880d681SAndroid Build Coastguard Worker }
1445*9880d681SAndroid Build Coastguard Worker
1446*9880d681SAndroid Build Coastguard Worker // Now deal with inline asm by parsing the operand group descriptor flags.
1447*9880d681SAndroid Build Coastguard Worker // Find the beginning of each operand group.
1448*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 8> GroupIdx;
1449*9880d681SAndroid Build Coastguard Worker unsigned OpIdxGroup = ~0u;
1450*9880d681SAndroid Build Coastguard Worker unsigned NumOps;
1451*9880d681SAndroid Build Coastguard Worker for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1452*9880d681SAndroid Build Coastguard Worker i += NumOps) {
1453*9880d681SAndroid Build Coastguard Worker const MachineOperand &FlagMO = getOperand(i);
1454*9880d681SAndroid Build Coastguard Worker assert(FlagMO.isImm() && "Invalid tied operand on inline asm");
1455*9880d681SAndroid Build Coastguard Worker unsigned CurGroup = GroupIdx.size();
1456*9880d681SAndroid Build Coastguard Worker GroupIdx.push_back(i);
1457*9880d681SAndroid Build Coastguard Worker NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1458*9880d681SAndroid Build Coastguard Worker // OpIdx belongs to this operand group.
1459*9880d681SAndroid Build Coastguard Worker if (OpIdx > i && OpIdx < i + NumOps)
1460*9880d681SAndroid Build Coastguard Worker OpIdxGroup = CurGroup;
1461*9880d681SAndroid Build Coastguard Worker unsigned TiedGroup;
1462*9880d681SAndroid Build Coastguard Worker if (!InlineAsm::isUseOperandTiedToDef(FlagMO.getImm(), TiedGroup))
1463*9880d681SAndroid Build Coastguard Worker continue;
1464*9880d681SAndroid Build Coastguard Worker // Operands in this group are tied to operands in TiedGroup which must be
1465*9880d681SAndroid Build Coastguard Worker // earlier. Find the number of operands between the two groups.
1466*9880d681SAndroid Build Coastguard Worker unsigned Delta = i - GroupIdx[TiedGroup];
1467*9880d681SAndroid Build Coastguard Worker
1468*9880d681SAndroid Build Coastguard Worker // OpIdx is a use tied to TiedGroup.
1469*9880d681SAndroid Build Coastguard Worker if (OpIdxGroup == CurGroup)
1470*9880d681SAndroid Build Coastguard Worker return OpIdx - Delta;
1471*9880d681SAndroid Build Coastguard Worker
1472*9880d681SAndroid Build Coastguard Worker // OpIdx is a def tied to this use group.
1473*9880d681SAndroid Build Coastguard Worker if (OpIdxGroup == TiedGroup)
1474*9880d681SAndroid Build Coastguard Worker return OpIdx + Delta;
1475*9880d681SAndroid Build Coastguard Worker }
1476*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Invalid tied operand on inline asm");
1477*9880d681SAndroid Build Coastguard Worker }
1478*9880d681SAndroid Build Coastguard Worker
1479*9880d681SAndroid Build Coastguard Worker /// clearKillInfo - Clears kill flags on all operands.
1480*9880d681SAndroid Build Coastguard Worker ///
clearKillInfo()1481*9880d681SAndroid Build Coastguard Worker void MachineInstr::clearKillInfo() {
1482*9880d681SAndroid Build Coastguard Worker for (MachineOperand &MO : operands()) {
1483*9880d681SAndroid Build Coastguard Worker if (MO.isReg() && MO.isUse())
1484*9880d681SAndroid Build Coastguard Worker MO.setIsKill(false);
1485*9880d681SAndroid Build Coastguard Worker }
1486*9880d681SAndroid Build Coastguard Worker }
1487*9880d681SAndroid Build Coastguard Worker
substituteRegister(unsigned FromReg,unsigned ToReg,unsigned SubIdx,const TargetRegisterInfo & RegInfo)1488*9880d681SAndroid Build Coastguard Worker void MachineInstr::substituteRegister(unsigned FromReg,
1489*9880d681SAndroid Build Coastguard Worker unsigned ToReg,
1490*9880d681SAndroid Build Coastguard Worker unsigned SubIdx,
1491*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo &RegInfo) {
1492*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isPhysicalRegister(ToReg)) {
1493*9880d681SAndroid Build Coastguard Worker if (SubIdx)
1494*9880d681SAndroid Build Coastguard Worker ToReg = RegInfo.getSubReg(ToReg, SubIdx);
1495*9880d681SAndroid Build Coastguard Worker for (MachineOperand &MO : operands()) {
1496*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || MO.getReg() != FromReg)
1497*9880d681SAndroid Build Coastguard Worker continue;
1498*9880d681SAndroid Build Coastguard Worker MO.substPhysReg(ToReg, RegInfo);
1499*9880d681SAndroid Build Coastguard Worker }
1500*9880d681SAndroid Build Coastguard Worker } else {
1501*9880d681SAndroid Build Coastguard Worker for (MachineOperand &MO : operands()) {
1502*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || MO.getReg() != FromReg)
1503*9880d681SAndroid Build Coastguard Worker continue;
1504*9880d681SAndroid Build Coastguard Worker MO.substVirtReg(ToReg, SubIdx, RegInfo);
1505*9880d681SAndroid Build Coastguard Worker }
1506*9880d681SAndroid Build Coastguard Worker }
1507*9880d681SAndroid Build Coastguard Worker }
1508*9880d681SAndroid Build Coastguard Worker
1509*9880d681SAndroid Build Coastguard Worker /// isSafeToMove - Return true if it is safe to move this instruction. If
1510*9880d681SAndroid Build Coastguard Worker /// SawStore is set to true, it means that there is a store (or call) between
1511*9880d681SAndroid Build Coastguard Worker /// the instruction's location and its intended destination.
isSafeToMove(AliasAnalysis * AA,bool & SawStore) const1512*9880d681SAndroid Build Coastguard Worker bool MachineInstr::isSafeToMove(AliasAnalysis *AA, bool &SawStore) const {
1513*9880d681SAndroid Build Coastguard Worker // Ignore stuff that we obviously can't move.
1514*9880d681SAndroid Build Coastguard Worker //
1515*9880d681SAndroid Build Coastguard Worker // Treat volatile loads as stores. This is not strictly necessary for
1516*9880d681SAndroid Build Coastguard Worker // volatiles, but it is required for atomic loads. It is not allowed to move
1517*9880d681SAndroid Build Coastguard Worker // a load across an atomic load with Ordering > Monotonic.
1518*9880d681SAndroid Build Coastguard Worker if (mayStore() || isCall() ||
1519*9880d681SAndroid Build Coastguard Worker (mayLoad() && hasOrderedMemoryRef())) {
1520*9880d681SAndroid Build Coastguard Worker SawStore = true;
1521*9880d681SAndroid Build Coastguard Worker return false;
1522*9880d681SAndroid Build Coastguard Worker }
1523*9880d681SAndroid Build Coastguard Worker
1524*9880d681SAndroid Build Coastguard Worker if (isPosition() || isDebugValue() || isTerminator() ||
1525*9880d681SAndroid Build Coastguard Worker hasUnmodeledSideEffects())
1526*9880d681SAndroid Build Coastguard Worker return false;
1527*9880d681SAndroid Build Coastguard Worker
1528*9880d681SAndroid Build Coastguard Worker // See if this instruction does a load. If so, we have to guarantee that the
1529*9880d681SAndroid Build Coastguard Worker // loaded value doesn't change between the load and the its intended
1530*9880d681SAndroid Build Coastguard Worker // destination. The check for isInvariantLoad gives the targe the chance to
1531*9880d681SAndroid Build Coastguard Worker // classify the load as always returning a constant, e.g. a constant pool
1532*9880d681SAndroid Build Coastguard Worker // load.
1533*9880d681SAndroid Build Coastguard Worker if (mayLoad() && !isInvariantLoad(AA))
1534*9880d681SAndroid Build Coastguard Worker // Otherwise, this is a real load. If there is a store between the load and
1535*9880d681SAndroid Build Coastguard Worker // end of block, we can't move it.
1536*9880d681SAndroid Build Coastguard Worker return !SawStore;
1537*9880d681SAndroid Build Coastguard Worker
1538*9880d681SAndroid Build Coastguard Worker return true;
1539*9880d681SAndroid Build Coastguard Worker }
1540*9880d681SAndroid Build Coastguard Worker
1541*9880d681SAndroid Build Coastguard Worker /// hasOrderedMemoryRef - Return true if this instruction may have an ordered
1542*9880d681SAndroid Build Coastguard Worker /// or volatile memory reference, or if the information describing the memory
1543*9880d681SAndroid Build Coastguard Worker /// reference is not available. Return false if it is known to have no ordered
1544*9880d681SAndroid Build Coastguard Worker /// memory references.
hasOrderedMemoryRef() const1545*9880d681SAndroid Build Coastguard Worker bool MachineInstr::hasOrderedMemoryRef() const {
1546*9880d681SAndroid Build Coastguard Worker // An instruction known never to access memory won't have a volatile access.
1547*9880d681SAndroid Build Coastguard Worker if (!mayStore() &&
1548*9880d681SAndroid Build Coastguard Worker !mayLoad() &&
1549*9880d681SAndroid Build Coastguard Worker !isCall() &&
1550*9880d681SAndroid Build Coastguard Worker !hasUnmodeledSideEffects())
1551*9880d681SAndroid Build Coastguard Worker return false;
1552*9880d681SAndroid Build Coastguard Worker
1553*9880d681SAndroid Build Coastguard Worker // Otherwise, if the instruction has no memory reference information,
1554*9880d681SAndroid Build Coastguard Worker // conservatively assume it wasn't preserved.
1555*9880d681SAndroid Build Coastguard Worker if (memoperands_empty())
1556*9880d681SAndroid Build Coastguard Worker return true;
1557*9880d681SAndroid Build Coastguard Worker
1558*9880d681SAndroid Build Coastguard Worker // Check if any of our memory operands are ordered.
1559*9880d681SAndroid Build Coastguard Worker return any_of(memoperands(), [](const MachineMemOperand *MMO) {
1560*9880d681SAndroid Build Coastguard Worker return !MMO->isUnordered();
1561*9880d681SAndroid Build Coastguard Worker });
1562*9880d681SAndroid Build Coastguard Worker }
1563*9880d681SAndroid Build Coastguard Worker
1564*9880d681SAndroid Build Coastguard Worker /// isInvariantLoad - Return true if this instruction is loading from a
1565*9880d681SAndroid Build Coastguard Worker /// location whose value is invariant across the function. For example,
1566*9880d681SAndroid Build Coastguard Worker /// loading a value from the constant pool or from the argument area
1567*9880d681SAndroid Build Coastguard Worker /// of a function if it does not change. This should only return true of
1568*9880d681SAndroid Build Coastguard Worker /// *all* loads the instruction does are invariant (if it does multiple loads).
isInvariantLoad(AliasAnalysis * AA) const1569*9880d681SAndroid Build Coastguard Worker bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const {
1570*9880d681SAndroid Build Coastguard Worker // If the instruction doesn't load at all, it isn't an invariant load.
1571*9880d681SAndroid Build Coastguard Worker if (!mayLoad())
1572*9880d681SAndroid Build Coastguard Worker return false;
1573*9880d681SAndroid Build Coastguard Worker
1574*9880d681SAndroid Build Coastguard Worker // If the instruction has lost its memoperands, conservatively assume that
1575*9880d681SAndroid Build Coastguard Worker // it may not be an invariant load.
1576*9880d681SAndroid Build Coastguard Worker if (memoperands_empty())
1577*9880d681SAndroid Build Coastguard Worker return false;
1578*9880d681SAndroid Build Coastguard Worker
1579*9880d681SAndroid Build Coastguard Worker const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo();
1580*9880d681SAndroid Build Coastguard Worker
1581*9880d681SAndroid Build Coastguard Worker for (MachineMemOperand *MMO : memoperands()) {
1582*9880d681SAndroid Build Coastguard Worker if (MMO->isVolatile()) return false;
1583*9880d681SAndroid Build Coastguard Worker if (MMO->isStore()) return false;
1584*9880d681SAndroid Build Coastguard Worker if (MMO->isInvariant()) continue;
1585*9880d681SAndroid Build Coastguard Worker
1586*9880d681SAndroid Build Coastguard Worker // A load from a constant PseudoSourceValue is invariant.
1587*9880d681SAndroid Build Coastguard Worker if (const PseudoSourceValue *PSV = MMO->getPseudoValue())
1588*9880d681SAndroid Build Coastguard Worker if (PSV->isConstant(MFI))
1589*9880d681SAndroid Build Coastguard Worker continue;
1590*9880d681SAndroid Build Coastguard Worker
1591*9880d681SAndroid Build Coastguard Worker if (const Value *V = MMO->getValue()) {
1592*9880d681SAndroid Build Coastguard Worker // If we have an AliasAnalysis, ask it whether the memory is constant.
1593*9880d681SAndroid Build Coastguard Worker if (AA &&
1594*9880d681SAndroid Build Coastguard Worker AA->pointsToConstantMemory(
1595*9880d681SAndroid Build Coastguard Worker MemoryLocation(V, MMO->getSize(), MMO->getAAInfo())))
1596*9880d681SAndroid Build Coastguard Worker continue;
1597*9880d681SAndroid Build Coastguard Worker }
1598*9880d681SAndroid Build Coastguard Worker
1599*9880d681SAndroid Build Coastguard Worker // Otherwise assume conservatively.
1600*9880d681SAndroid Build Coastguard Worker return false;
1601*9880d681SAndroid Build Coastguard Worker }
1602*9880d681SAndroid Build Coastguard Worker
1603*9880d681SAndroid Build Coastguard Worker // Everything checks out.
1604*9880d681SAndroid Build Coastguard Worker return true;
1605*9880d681SAndroid Build Coastguard Worker }
1606*9880d681SAndroid Build Coastguard Worker
1607*9880d681SAndroid Build Coastguard Worker /// isConstantValuePHI - If the specified instruction is a PHI that always
1608*9880d681SAndroid Build Coastguard Worker /// merges together the same virtual register, return the register, otherwise
1609*9880d681SAndroid Build Coastguard Worker /// return 0.
isConstantValuePHI() const1610*9880d681SAndroid Build Coastguard Worker unsigned MachineInstr::isConstantValuePHI() const {
1611*9880d681SAndroid Build Coastguard Worker if (!isPHI())
1612*9880d681SAndroid Build Coastguard Worker return 0;
1613*9880d681SAndroid Build Coastguard Worker assert(getNumOperands() >= 3 &&
1614*9880d681SAndroid Build Coastguard Worker "It's illegal to have a PHI without source operands");
1615*9880d681SAndroid Build Coastguard Worker
1616*9880d681SAndroid Build Coastguard Worker unsigned Reg = getOperand(1).getReg();
1617*9880d681SAndroid Build Coastguard Worker for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
1618*9880d681SAndroid Build Coastguard Worker if (getOperand(i).getReg() != Reg)
1619*9880d681SAndroid Build Coastguard Worker return 0;
1620*9880d681SAndroid Build Coastguard Worker return Reg;
1621*9880d681SAndroid Build Coastguard Worker }
1622*9880d681SAndroid Build Coastguard Worker
hasUnmodeledSideEffects() const1623*9880d681SAndroid Build Coastguard Worker bool MachineInstr::hasUnmodeledSideEffects() const {
1624*9880d681SAndroid Build Coastguard Worker if (hasProperty(MCID::UnmodeledSideEffects))
1625*9880d681SAndroid Build Coastguard Worker return true;
1626*9880d681SAndroid Build Coastguard Worker if (isInlineAsm()) {
1627*9880d681SAndroid Build Coastguard Worker unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1628*9880d681SAndroid Build Coastguard Worker if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1629*9880d681SAndroid Build Coastguard Worker return true;
1630*9880d681SAndroid Build Coastguard Worker }
1631*9880d681SAndroid Build Coastguard Worker
1632*9880d681SAndroid Build Coastguard Worker return false;
1633*9880d681SAndroid Build Coastguard Worker }
1634*9880d681SAndroid Build Coastguard Worker
isLoadFoldBarrier() const1635*9880d681SAndroid Build Coastguard Worker bool MachineInstr::isLoadFoldBarrier() const {
1636*9880d681SAndroid Build Coastguard Worker return mayStore() || isCall() || hasUnmodeledSideEffects();
1637*9880d681SAndroid Build Coastguard Worker }
1638*9880d681SAndroid Build Coastguard Worker
1639*9880d681SAndroid Build Coastguard Worker /// allDefsAreDead - Return true if all the defs of this instruction are dead.
1640*9880d681SAndroid Build Coastguard Worker ///
allDefsAreDead() const1641*9880d681SAndroid Build Coastguard Worker bool MachineInstr::allDefsAreDead() const {
1642*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : operands()) {
1643*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || MO.isUse())
1644*9880d681SAndroid Build Coastguard Worker continue;
1645*9880d681SAndroid Build Coastguard Worker if (!MO.isDead())
1646*9880d681SAndroid Build Coastguard Worker return false;
1647*9880d681SAndroid Build Coastguard Worker }
1648*9880d681SAndroid Build Coastguard Worker return true;
1649*9880d681SAndroid Build Coastguard Worker }
1650*9880d681SAndroid Build Coastguard Worker
1651*9880d681SAndroid Build Coastguard Worker /// copyImplicitOps - Copy implicit register operands from specified
1652*9880d681SAndroid Build Coastguard Worker /// instruction to this instruction.
copyImplicitOps(MachineFunction & MF,const MachineInstr & MI)1653*9880d681SAndroid Build Coastguard Worker void MachineInstr::copyImplicitOps(MachineFunction &MF,
1654*9880d681SAndroid Build Coastguard Worker const MachineInstr &MI) {
1655*9880d681SAndroid Build Coastguard Worker for (unsigned i = MI.getDesc().getNumOperands(), e = MI.getNumOperands();
1656*9880d681SAndroid Build Coastguard Worker i != e; ++i) {
1657*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = MI.getOperand(i);
1658*9880d681SAndroid Build Coastguard Worker if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
1659*9880d681SAndroid Build Coastguard Worker addOperand(MF, MO);
1660*9880d681SAndroid Build Coastguard Worker }
1661*9880d681SAndroid Build Coastguard Worker }
1662*9880d681SAndroid Build Coastguard Worker
dump() const1663*9880d681SAndroid Build Coastguard Worker LLVM_DUMP_METHOD void MachineInstr::dump() const {
1664*9880d681SAndroid Build Coastguard Worker #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1665*9880d681SAndroid Build Coastguard Worker dbgs() << " " << *this;
1666*9880d681SAndroid Build Coastguard Worker #endif
1667*9880d681SAndroid Build Coastguard Worker }
1668*9880d681SAndroid Build Coastguard Worker
print(raw_ostream & OS,bool SkipOpers) const1669*9880d681SAndroid Build Coastguard Worker void MachineInstr::print(raw_ostream &OS, bool SkipOpers) const {
1670*9880d681SAndroid Build Coastguard Worker const Module *M = nullptr;
1671*9880d681SAndroid Build Coastguard Worker if (const MachineBasicBlock *MBB = getParent())
1672*9880d681SAndroid Build Coastguard Worker if (const MachineFunction *MF = MBB->getParent())
1673*9880d681SAndroid Build Coastguard Worker M = MF->getFunction()->getParent();
1674*9880d681SAndroid Build Coastguard Worker
1675*9880d681SAndroid Build Coastguard Worker ModuleSlotTracker MST(M);
1676*9880d681SAndroid Build Coastguard Worker print(OS, MST, SkipOpers);
1677*9880d681SAndroid Build Coastguard Worker }
1678*9880d681SAndroid Build Coastguard Worker
print(raw_ostream & OS,ModuleSlotTracker & MST,bool SkipOpers) const1679*9880d681SAndroid Build Coastguard Worker void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
1680*9880d681SAndroid Build Coastguard Worker bool SkipOpers) const {
1681*9880d681SAndroid Build Coastguard Worker // We can be a bit tidier if we know the MachineFunction.
1682*9880d681SAndroid Build Coastguard Worker const MachineFunction *MF = nullptr;
1683*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI = nullptr;
1684*9880d681SAndroid Build Coastguard Worker const MachineRegisterInfo *MRI = nullptr;
1685*9880d681SAndroid Build Coastguard Worker const TargetInstrInfo *TII = nullptr;
1686*9880d681SAndroid Build Coastguard Worker if (const MachineBasicBlock *MBB = getParent()) {
1687*9880d681SAndroid Build Coastguard Worker MF = MBB->getParent();
1688*9880d681SAndroid Build Coastguard Worker if (MF) {
1689*9880d681SAndroid Build Coastguard Worker MRI = &MF->getRegInfo();
1690*9880d681SAndroid Build Coastguard Worker TRI = MF->getSubtarget().getRegisterInfo();
1691*9880d681SAndroid Build Coastguard Worker TII = MF->getSubtarget().getInstrInfo();
1692*9880d681SAndroid Build Coastguard Worker }
1693*9880d681SAndroid Build Coastguard Worker }
1694*9880d681SAndroid Build Coastguard Worker
1695*9880d681SAndroid Build Coastguard Worker // Save a list of virtual registers.
1696*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 8> VirtRegs;
1697*9880d681SAndroid Build Coastguard Worker
1698*9880d681SAndroid Build Coastguard Worker // Print explicitly defined operands on the left of an assignment syntax.
1699*9880d681SAndroid Build Coastguard Worker unsigned StartOp = 0, e = getNumOperands();
1700*9880d681SAndroid Build Coastguard Worker for (; StartOp < e && getOperand(StartOp).isReg() &&
1701*9880d681SAndroid Build Coastguard Worker getOperand(StartOp).isDef() &&
1702*9880d681SAndroid Build Coastguard Worker !getOperand(StartOp).isImplicit();
1703*9880d681SAndroid Build Coastguard Worker ++StartOp) {
1704*9880d681SAndroid Build Coastguard Worker if (StartOp != 0) OS << ", ";
1705*9880d681SAndroid Build Coastguard Worker getOperand(StartOp).print(OS, MST, TRI);
1706*9880d681SAndroid Build Coastguard Worker unsigned Reg = getOperand(StartOp).getReg();
1707*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1708*9880d681SAndroid Build Coastguard Worker VirtRegs.push_back(Reg);
1709*9880d681SAndroid Build Coastguard Worker unsigned Size;
1710*9880d681SAndroid Build Coastguard Worker if (MRI && (Size = MRI->getSize(Reg)))
1711*9880d681SAndroid Build Coastguard Worker OS << '(' << Size << ')';
1712*9880d681SAndroid Build Coastguard Worker }
1713*9880d681SAndroid Build Coastguard Worker }
1714*9880d681SAndroid Build Coastguard Worker
1715*9880d681SAndroid Build Coastguard Worker if (StartOp != 0)
1716*9880d681SAndroid Build Coastguard Worker OS << " = ";
1717*9880d681SAndroid Build Coastguard Worker
1718*9880d681SAndroid Build Coastguard Worker // Print the opcode name.
1719*9880d681SAndroid Build Coastguard Worker if (TII)
1720*9880d681SAndroid Build Coastguard Worker OS << TII->getName(getOpcode());
1721*9880d681SAndroid Build Coastguard Worker else
1722*9880d681SAndroid Build Coastguard Worker OS << "UNKNOWN";
1723*9880d681SAndroid Build Coastguard Worker
1724*9880d681SAndroid Build Coastguard Worker if (getType()) {
1725*9880d681SAndroid Build Coastguard Worker OS << ' ';
1726*9880d681SAndroid Build Coastguard Worker getType()->print(OS, /*IsForDebug*/ false, /*NoDetails*/ true);
1727*9880d681SAndroid Build Coastguard Worker OS << ' ';
1728*9880d681SAndroid Build Coastguard Worker }
1729*9880d681SAndroid Build Coastguard Worker
1730*9880d681SAndroid Build Coastguard Worker if (SkipOpers)
1731*9880d681SAndroid Build Coastguard Worker return;
1732*9880d681SAndroid Build Coastguard Worker
1733*9880d681SAndroid Build Coastguard Worker // Print the rest of the operands.
1734*9880d681SAndroid Build Coastguard Worker bool OmittedAnyCallClobbers = false;
1735*9880d681SAndroid Build Coastguard Worker bool FirstOp = true;
1736*9880d681SAndroid Build Coastguard Worker unsigned AsmDescOp = ~0u;
1737*9880d681SAndroid Build Coastguard Worker unsigned AsmOpCount = 0;
1738*9880d681SAndroid Build Coastguard Worker
1739*9880d681SAndroid Build Coastguard Worker if (isInlineAsm() && e >= InlineAsm::MIOp_FirstOperand) {
1740*9880d681SAndroid Build Coastguard Worker // Print asm string.
1741*9880d681SAndroid Build Coastguard Worker OS << " ";
1742*9880d681SAndroid Build Coastguard Worker getOperand(InlineAsm::MIOp_AsmString).print(OS, MST, TRI);
1743*9880d681SAndroid Build Coastguard Worker
1744*9880d681SAndroid Build Coastguard Worker // Print HasSideEffects, MayLoad, MayStore, IsAlignStack
1745*9880d681SAndroid Build Coastguard Worker unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1746*9880d681SAndroid Build Coastguard Worker if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1747*9880d681SAndroid Build Coastguard Worker OS << " [sideeffect]";
1748*9880d681SAndroid Build Coastguard Worker if (ExtraInfo & InlineAsm::Extra_MayLoad)
1749*9880d681SAndroid Build Coastguard Worker OS << " [mayload]";
1750*9880d681SAndroid Build Coastguard Worker if (ExtraInfo & InlineAsm::Extra_MayStore)
1751*9880d681SAndroid Build Coastguard Worker OS << " [maystore]";
1752*9880d681SAndroid Build Coastguard Worker if (ExtraInfo & InlineAsm::Extra_IsConvergent)
1753*9880d681SAndroid Build Coastguard Worker OS << " [isconvergent]";
1754*9880d681SAndroid Build Coastguard Worker if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1755*9880d681SAndroid Build Coastguard Worker OS << " [alignstack]";
1756*9880d681SAndroid Build Coastguard Worker if (getInlineAsmDialect() == InlineAsm::AD_ATT)
1757*9880d681SAndroid Build Coastguard Worker OS << " [attdialect]";
1758*9880d681SAndroid Build Coastguard Worker if (getInlineAsmDialect() == InlineAsm::AD_Intel)
1759*9880d681SAndroid Build Coastguard Worker OS << " [inteldialect]";
1760*9880d681SAndroid Build Coastguard Worker
1761*9880d681SAndroid Build Coastguard Worker StartOp = AsmDescOp = InlineAsm::MIOp_FirstOperand;
1762*9880d681SAndroid Build Coastguard Worker FirstOp = false;
1763*9880d681SAndroid Build Coastguard Worker }
1764*9880d681SAndroid Build Coastguard Worker
1765*9880d681SAndroid Build Coastguard Worker for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
1766*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = getOperand(i);
1767*9880d681SAndroid Build Coastguard Worker
1768*9880d681SAndroid Build Coastguard Worker if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg()))
1769*9880d681SAndroid Build Coastguard Worker VirtRegs.push_back(MO.getReg());
1770*9880d681SAndroid Build Coastguard Worker
1771*9880d681SAndroid Build Coastguard Worker // Omit call-clobbered registers which aren't used anywhere. This makes
1772*9880d681SAndroid Build Coastguard Worker // call instructions much less noisy on targets where calls clobber lots
1773*9880d681SAndroid Build Coastguard Worker // of registers. Don't rely on MO.isDead() because we may be called before
1774*9880d681SAndroid Build Coastguard Worker // LiveVariables is run, or we may be looking at a non-allocatable reg.
1775*9880d681SAndroid Build Coastguard Worker if (MRI && isCall() &&
1776*9880d681SAndroid Build Coastguard Worker MO.isReg() && MO.isImplicit() && MO.isDef()) {
1777*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
1778*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1779*9880d681SAndroid Build Coastguard Worker if (MRI->use_empty(Reg)) {
1780*9880d681SAndroid Build Coastguard Worker bool HasAliasLive = false;
1781*9880d681SAndroid Build Coastguard Worker for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
1782*9880d681SAndroid Build Coastguard Worker unsigned AliasReg = *AI;
1783*9880d681SAndroid Build Coastguard Worker if (!MRI->use_empty(AliasReg)) {
1784*9880d681SAndroid Build Coastguard Worker HasAliasLive = true;
1785*9880d681SAndroid Build Coastguard Worker break;
1786*9880d681SAndroid Build Coastguard Worker }
1787*9880d681SAndroid Build Coastguard Worker }
1788*9880d681SAndroid Build Coastguard Worker if (!HasAliasLive) {
1789*9880d681SAndroid Build Coastguard Worker OmittedAnyCallClobbers = true;
1790*9880d681SAndroid Build Coastguard Worker continue;
1791*9880d681SAndroid Build Coastguard Worker }
1792*9880d681SAndroid Build Coastguard Worker }
1793*9880d681SAndroid Build Coastguard Worker }
1794*9880d681SAndroid Build Coastguard Worker }
1795*9880d681SAndroid Build Coastguard Worker
1796*9880d681SAndroid Build Coastguard Worker if (FirstOp) FirstOp = false; else OS << ",";
1797*9880d681SAndroid Build Coastguard Worker OS << " ";
1798*9880d681SAndroid Build Coastguard Worker if (i < getDesc().NumOperands) {
1799*9880d681SAndroid Build Coastguard Worker const MCOperandInfo &MCOI = getDesc().OpInfo[i];
1800*9880d681SAndroid Build Coastguard Worker if (MCOI.isPredicate())
1801*9880d681SAndroid Build Coastguard Worker OS << "pred:";
1802*9880d681SAndroid Build Coastguard Worker if (MCOI.isOptionalDef())
1803*9880d681SAndroid Build Coastguard Worker OS << "opt:";
1804*9880d681SAndroid Build Coastguard Worker }
1805*9880d681SAndroid Build Coastguard Worker if (isDebugValue() && MO.isMetadata()) {
1806*9880d681SAndroid Build Coastguard Worker // Pretty print DBG_VALUE instructions.
1807*9880d681SAndroid Build Coastguard Worker auto *DIV = dyn_cast<DILocalVariable>(MO.getMetadata());
1808*9880d681SAndroid Build Coastguard Worker if (DIV && !DIV->getName().empty())
1809*9880d681SAndroid Build Coastguard Worker OS << "!\"" << DIV->getName() << '\"';
1810*9880d681SAndroid Build Coastguard Worker else
1811*9880d681SAndroid Build Coastguard Worker MO.print(OS, MST, TRI);
1812*9880d681SAndroid Build Coastguard Worker } else if (TRI && (isInsertSubreg() || isRegSequence()) && MO.isImm()) {
1813*9880d681SAndroid Build Coastguard Worker OS << TRI->getSubRegIndexName(MO.getImm());
1814*9880d681SAndroid Build Coastguard Worker } else if (i == AsmDescOp && MO.isImm()) {
1815*9880d681SAndroid Build Coastguard Worker // Pretty print the inline asm operand descriptor.
1816*9880d681SAndroid Build Coastguard Worker OS << '$' << AsmOpCount++;
1817*9880d681SAndroid Build Coastguard Worker unsigned Flag = MO.getImm();
1818*9880d681SAndroid Build Coastguard Worker switch (InlineAsm::getKind(Flag)) {
1819*9880d681SAndroid Build Coastguard Worker case InlineAsm::Kind_RegUse: OS << ":[reguse"; break;
1820*9880d681SAndroid Build Coastguard Worker case InlineAsm::Kind_RegDef: OS << ":[regdef"; break;
1821*9880d681SAndroid Build Coastguard Worker case InlineAsm::Kind_RegDefEarlyClobber: OS << ":[regdef-ec"; break;
1822*9880d681SAndroid Build Coastguard Worker case InlineAsm::Kind_Clobber: OS << ":[clobber"; break;
1823*9880d681SAndroid Build Coastguard Worker case InlineAsm::Kind_Imm: OS << ":[imm"; break;
1824*9880d681SAndroid Build Coastguard Worker case InlineAsm::Kind_Mem: OS << ":[mem"; break;
1825*9880d681SAndroid Build Coastguard Worker default: OS << ":[??" << InlineAsm::getKind(Flag); break;
1826*9880d681SAndroid Build Coastguard Worker }
1827*9880d681SAndroid Build Coastguard Worker
1828*9880d681SAndroid Build Coastguard Worker unsigned RCID = 0;
1829*9880d681SAndroid Build Coastguard Worker if (InlineAsm::hasRegClassConstraint(Flag, RCID)) {
1830*9880d681SAndroid Build Coastguard Worker if (TRI) {
1831*9880d681SAndroid Build Coastguard Worker OS << ':' << TRI->getRegClassName(TRI->getRegClass(RCID));
1832*9880d681SAndroid Build Coastguard Worker } else
1833*9880d681SAndroid Build Coastguard Worker OS << ":RC" << RCID;
1834*9880d681SAndroid Build Coastguard Worker }
1835*9880d681SAndroid Build Coastguard Worker
1836*9880d681SAndroid Build Coastguard Worker unsigned TiedTo = 0;
1837*9880d681SAndroid Build Coastguard Worker if (InlineAsm::isUseOperandTiedToDef(Flag, TiedTo))
1838*9880d681SAndroid Build Coastguard Worker OS << " tiedto:$" << TiedTo;
1839*9880d681SAndroid Build Coastguard Worker
1840*9880d681SAndroid Build Coastguard Worker OS << ']';
1841*9880d681SAndroid Build Coastguard Worker
1842*9880d681SAndroid Build Coastguard Worker // Compute the index of the next operand descriptor.
1843*9880d681SAndroid Build Coastguard Worker AsmDescOp += 1 + InlineAsm::getNumOperandRegisters(Flag);
1844*9880d681SAndroid Build Coastguard Worker } else
1845*9880d681SAndroid Build Coastguard Worker MO.print(OS, MST, TRI);
1846*9880d681SAndroid Build Coastguard Worker }
1847*9880d681SAndroid Build Coastguard Worker
1848*9880d681SAndroid Build Coastguard Worker // Briefly indicate whether any call clobbers were omitted.
1849*9880d681SAndroid Build Coastguard Worker if (OmittedAnyCallClobbers) {
1850*9880d681SAndroid Build Coastguard Worker if (!FirstOp) OS << ",";
1851*9880d681SAndroid Build Coastguard Worker OS << " ...";
1852*9880d681SAndroid Build Coastguard Worker }
1853*9880d681SAndroid Build Coastguard Worker
1854*9880d681SAndroid Build Coastguard Worker bool HaveSemi = false;
1855*9880d681SAndroid Build Coastguard Worker const unsigned PrintableFlags = FrameSetup | FrameDestroy;
1856*9880d681SAndroid Build Coastguard Worker if (Flags & PrintableFlags) {
1857*9880d681SAndroid Build Coastguard Worker if (!HaveSemi) {
1858*9880d681SAndroid Build Coastguard Worker OS << ";";
1859*9880d681SAndroid Build Coastguard Worker HaveSemi = true;
1860*9880d681SAndroid Build Coastguard Worker }
1861*9880d681SAndroid Build Coastguard Worker OS << " flags: ";
1862*9880d681SAndroid Build Coastguard Worker
1863*9880d681SAndroid Build Coastguard Worker if (Flags & FrameSetup)
1864*9880d681SAndroid Build Coastguard Worker OS << "FrameSetup";
1865*9880d681SAndroid Build Coastguard Worker
1866*9880d681SAndroid Build Coastguard Worker if (Flags & FrameDestroy)
1867*9880d681SAndroid Build Coastguard Worker OS << "FrameDestroy";
1868*9880d681SAndroid Build Coastguard Worker }
1869*9880d681SAndroid Build Coastguard Worker
1870*9880d681SAndroid Build Coastguard Worker if (!memoperands_empty()) {
1871*9880d681SAndroid Build Coastguard Worker if (!HaveSemi) {
1872*9880d681SAndroid Build Coastguard Worker OS << ";";
1873*9880d681SAndroid Build Coastguard Worker HaveSemi = true;
1874*9880d681SAndroid Build Coastguard Worker }
1875*9880d681SAndroid Build Coastguard Worker
1876*9880d681SAndroid Build Coastguard Worker OS << " mem:";
1877*9880d681SAndroid Build Coastguard Worker for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
1878*9880d681SAndroid Build Coastguard Worker i != e; ++i) {
1879*9880d681SAndroid Build Coastguard Worker (*i)->print(OS, MST);
1880*9880d681SAndroid Build Coastguard Worker if (std::next(i) != e)
1881*9880d681SAndroid Build Coastguard Worker OS << " ";
1882*9880d681SAndroid Build Coastguard Worker }
1883*9880d681SAndroid Build Coastguard Worker }
1884*9880d681SAndroid Build Coastguard Worker
1885*9880d681SAndroid Build Coastguard Worker // Print the regclass of any virtual registers encountered.
1886*9880d681SAndroid Build Coastguard Worker if (MRI && !VirtRegs.empty()) {
1887*9880d681SAndroid Build Coastguard Worker if (!HaveSemi) {
1888*9880d681SAndroid Build Coastguard Worker OS << ";";
1889*9880d681SAndroid Build Coastguard Worker HaveSemi = true;
1890*9880d681SAndroid Build Coastguard Worker }
1891*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != VirtRegs.size(); ++i) {
1892*9880d681SAndroid Build Coastguard Worker const RegClassOrRegBank &RC = MRI->getRegClassOrRegBank(VirtRegs[i]);
1893*9880d681SAndroid Build Coastguard Worker if (!RC)
1894*9880d681SAndroid Build Coastguard Worker continue;
1895*9880d681SAndroid Build Coastguard Worker // Generic virtual registers do not have register classes.
1896*9880d681SAndroid Build Coastguard Worker if (RC.is<const RegisterBank *>())
1897*9880d681SAndroid Build Coastguard Worker OS << " " << RC.get<const RegisterBank *>()->getName();
1898*9880d681SAndroid Build Coastguard Worker else
1899*9880d681SAndroid Build Coastguard Worker OS << " "
1900*9880d681SAndroid Build Coastguard Worker << TRI->getRegClassName(RC.get<const TargetRegisterClass *>());
1901*9880d681SAndroid Build Coastguard Worker OS << ':' << PrintReg(VirtRegs[i]);
1902*9880d681SAndroid Build Coastguard Worker for (unsigned j = i+1; j != VirtRegs.size();) {
1903*9880d681SAndroid Build Coastguard Worker if (MRI->getRegClassOrRegBank(VirtRegs[j]) != RC) {
1904*9880d681SAndroid Build Coastguard Worker ++j;
1905*9880d681SAndroid Build Coastguard Worker continue;
1906*9880d681SAndroid Build Coastguard Worker }
1907*9880d681SAndroid Build Coastguard Worker if (VirtRegs[i] != VirtRegs[j])
1908*9880d681SAndroid Build Coastguard Worker OS << "," << PrintReg(VirtRegs[j]);
1909*9880d681SAndroid Build Coastguard Worker VirtRegs.erase(VirtRegs.begin()+j);
1910*9880d681SAndroid Build Coastguard Worker }
1911*9880d681SAndroid Build Coastguard Worker }
1912*9880d681SAndroid Build Coastguard Worker }
1913*9880d681SAndroid Build Coastguard Worker
1914*9880d681SAndroid Build Coastguard Worker // Print debug location information.
1915*9880d681SAndroid Build Coastguard Worker if (isDebugValue() && getOperand(e - 2).isMetadata()) {
1916*9880d681SAndroid Build Coastguard Worker if (!HaveSemi)
1917*9880d681SAndroid Build Coastguard Worker OS << ";";
1918*9880d681SAndroid Build Coastguard Worker auto *DV = cast<DILocalVariable>(getOperand(e - 2).getMetadata());
1919*9880d681SAndroid Build Coastguard Worker OS << " line no:" << DV->getLine();
1920*9880d681SAndroid Build Coastguard Worker if (auto *InlinedAt = debugLoc->getInlinedAt()) {
1921*9880d681SAndroid Build Coastguard Worker DebugLoc InlinedAtDL(InlinedAt);
1922*9880d681SAndroid Build Coastguard Worker if (InlinedAtDL && MF) {
1923*9880d681SAndroid Build Coastguard Worker OS << " inlined @[ ";
1924*9880d681SAndroid Build Coastguard Worker InlinedAtDL.print(OS);
1925*9880d681SAndroid Build Coastguard Worker OS << " ]";
1926*9880d681SAndroid Build Coastguard Worker }
1927*9880d681SAndroid Build Coastguard Worker }
1928*9880d681SAndroid Build Coastguard Worker if (isIndirectDebugValue())
1929*9880d681SAndroid Build Coastguard Worker OS << " indirect";
1930*9880d681SAndroid Build Coastguard Worker } else if (debugLoc && MF) {
1931*9880d681SAndroid Build Coastguard Worker if (!HaveSemi)
1932*9880d681SAndroid Build Coastguard Worker OS << ";";
1933*9880d681SAndroid Build Coastguard Worker OS << " dbg:";
1934*9880d681SAndroid Build Coastguard Worker debugLoc.print(OS);
1935*9880d681SAndroid Build Coastguard Worker }
1936*9880d681SAndroid Build Coastguard Worker
1937*9880d681SAndroid Build Coastguard Worker OS << '\n';
1938*9880d681SAndroid Build Coastguard Worker }
1939*9880d681SAndroid Build Coastguard Worker
addRegisterKilled(unsigned IncomingReg,const TargetRegisterInfo * RegInfo,bool AddIfNotFound)1940*9880d681SAndroid Build Coastguard Worker bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
1941*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *RegInfo,
1942*9880d681SAndroid Build Coastguard Worker bool AddIfNotFound) {
1943*9880d681SAndroid Build Coastguard Worker bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
1944*9880d681SAndroid Build Coastguard Worker bool hasAliases = isPhysReg &&
1945*9880d681SAndroid Build Coastguard Worker MCRegAliasIterator(IncomingReg, RegInfo, false).isValid();
1946*9880d681SAndroid Build Coastguard Worker bool Found = false;
1947*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned,4> DeadOps;
1948*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1949*9880d681SAndroid Build Coastguard Worker MachineOperand &MO = getOperand(i);
1950*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isUse() || MO.isUndef())
1951*9880d681SAndroid Build Coastguard Worker continue;
1952*9880d681SAndroid Build Coastguard Worker
1953*9880d681SAndroid Build Coastguard Worker // DEBUG_VALUE nodes do not contribute to code generation and should
1954*9880d681SAndroid Build Coastguard Worker // always be ignored. Failure to do so may result in trying to modify
1955*9880d681SAndroid Build Coastguard Worker // KILL flags on DEBUG_VALUE nodes.
1956*9880d681SAndroid Build Coastguard Worker if (MO.isDebug())
1957*9880d681SAndroid Build Coastguard Worker continue;
1958*9880d681SAndroid Build Coastguard Worker
1959*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
1960*9880d681SAndroid Build Coastguard Worker if (!Reg)
1961*9880d681SAndroid Build Coastguard Worker continue;
1962*9880d681SAndroid Build Coastguard Worker
1963*9880d681SAndroid Build Coastguard Worker if (Reg == IncomingReg) {
1964*9880d681SAndroid Build Coastguard Worker if (!Found) {
1965*9880d681SAndroid Build Coastguard Worker if (MO.isKill())
1966*9880d681SAndroid Build Coastguard Worker // The register is already marked kill.
1967*9880d681SAndroid Build Coastguard Worker return true;
1968*9880d681SAndroid Build Coastguard Worker if (isPhysReg && isRegTiedToDefOperand(i))
1969*9880d681SAndroid Build Coastguard Worker // Two-address uses of physregs must not be marked kill.
1970*9880d681SAndroid Build Coastguard Worker return true;
1971*9880d681SAndroid Build Coastguard Worker MO.setIsKill();
1972*9880d681SAndroid Build Coastguard Worker Found = true;
1973*9880d681SAndroid Build Coastguard Worker }
1974*9880d681SAndroid Build Coastguard Worker } else if (hasAliases && MO.isKill() &&
1975*9880d681SAndroid Build Coastguard Worker TargetRegisterInfo::isPhysicalRegister(Reg)) {
1976*9880d681SAndroid Build Coastguard Worker // A super-register kill already exists.
1977*9880d681SAndroid Build Coastguard Worker if (RegInfo->isSuperRegister(IncomingReg, Reg))
1978*9880d681SAndroid Build Coastguard Worker return true;
1979*9880d681SAndroid Build Coastguard Worker if (RegInfo->isSubRegister(IncomingReg, Reg))
1980*9880d681SAndroid Build Coastguard Worker DeadOps.push_back(i);
1981*9880d681SAndroid Build Coastguard Worker }
1982*9880d681SAndroid Build Coastguard Worker }
1983*9880d681SAndroid Build Coastguard Worker
1984*9880d681SAndroid Build Coastguard Worker // Trim unneeded kill operands.
1985*9880d681SAndroid Build Coastguard Worker while (!DeadOps.empty()) {
1986*9880d681SAndroid Build Coastguard Worker unsigned OpIdx = DeadOps.back();
1987*9880d681SAndroid Build Coastguard Worker if (getOperand(OpIdx).isImplicit())
1988*9880d681SAndroid Build Coastguard Worker RemoveOperand(OpIdx);
1989*9880d681SAndroid Build Coastguard Worker else
1990*9880d681SAndroid Build Coastguard Worker getOperand(OpIdx).setIsKill(false);
1991*9880d681SAndroid Build Coastguard Worker DeadOps.pop_back();
1992*9880d681SAndroid Build Coastguard Worker }
1993*9880d681SAndroid Build Coastguard Worker
1994*9880d681SAndroid Build Coastguard Worker // If not found, this means an alias of one of the operands is killed. Add a
1995*9880d681SAndroid Build Coastguard Worker // new implicit operand if required.
1996*9880d681SAndroid Build Coastguard Worker if (!Found && AddIfNotFound) {
1997*9880d681SAndroid Build Coastguard Worker addOperand(MachineOperand::CreateReg(IncomingReg,
1998*9880d681SAndroid Build Coastguard Worker false /*IsDef*/,
1999*9880d681SAndroid Build Coastguard Worker true /*IsImp*/,
2000*9880d681SAndroid Build Coastguard Worker true /*IsKill*/));
2001*9880d681SAndroid Build Coastguard Worker return true;
2002*9880d681SAndroid Build Coastguard Worker }
2003*9880d681SAndroid Build Coastguard Worker return Found;
2004*9880d681SAndroid Build Coastguard Worker }
2005*9880d681SAndroid Build Coastguard Worker
clearRegisterKills(unsigned Reg,const TargetRegisterInfo * RegInfo)2006*9880d681SAndroid Build Coastguard Worker void MachineInstr::clearRegisterKills(unsigned Reg,
2007*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *RegInfo) {
2008*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isPhysicalRegister(Reg))
2009*9880d681SAndroid Build Coastguard Worker RegInfo = nullptr;
2010*9880d681SAndroid Build Coastguard Worker for (MachineOperand &MO : operands()) {
2011*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isUse() || !MO.isKill())
2012*9880d681SAndroid Build Coastguard Worker continue;
2013*9880d681SAndroid Build Coastguard Worker unsigned OpReg = MO.getReg();
2014*9880d681SAndroid Build Coastguard Worker if ((RegInfo && RegInfo->regsOverlap(Reg, OpReg)) || Reg == OpReg)
2015*9880d681SAndroid Build Coastguard Worker MO.setIsKill(false);
2016*9880d681SAndroid Build Coastguard Worker }
2017*9880d681SAndroid Build Coastguard Worker }
2018*9880d681SAndroid Build Coastguard Worker
addRegisterDead(unsigned Reg,const TargetRegisterInfo * RegInfo,bool AddIfNotFound)2019*9880d681SAndroid Build Coastguard Worker bool MachineInstr::addRegisterDead(unsigned Reg,
2020*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *RegInfo,
2021*9880d681SAndroid Build Coastguard Worker bool AddIfNotFound) {
2022*9880d681SAndroid Build Coastguard Worker bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(Reg);
2023*9880d681SAndroid Build Coastguard Worker bool hasAliases = isPhysReg &&
2024*9880d681SAndroid Build Coastguard Worker MCRegAliasIterator(Reg, RegInfo, false).isValid();
2025*9880d681SAndroid Build Coastguard Worker bool Found = false;
2026*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned,4> DeadOps;
2027*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2028*9880d681SAndroid Build Coastguard Worker MachineOperand &MO = getOperand(i);
2029*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isDef())
2030*9880d681SAndroid Build Coastguard Worker continue;
2031*9880d681SAndroid Build Coastguard Worker unsigned MOReg = MO.getReg();
2032*9880d681SAndroid Build Coastguard Worker if (!MOReg)
2033*9880d681SAndroid Build Coastguard Worker continue;
2034*9880d681SAndroid Build Coastguard Worker
2035*9880d681SAndroid Build Coastguard Worker if (MOReg == Reg) {
2036*9880d681SAndroid Build Coastguard Worker MO.setIsDead();
2037*9880d681SAndroid Build Coastguard Worker Found = true;
2038*9880d681SAndroid Build Coastguard Worker } else if (hasAliases && MO.isDead() &&
2039*9880d681SAndroid Build Coastguard Worker TargetRegisterInfo::isPhysicalRegister(MOReg)) {
2040*9880d681SAndroid Build Coastguard Worker // There exists a super-register that's marked dead.
2041*9880d681SAndroid Build Coastguard Worker if (RegInfo->isSuperRegister(Reg, MOReg))
2042*9880d681SAndroid Build Coastguard Worker return true;
2043*9880d681SAndroid Build Coastguard Worker if (RegInfo->isSubRegister(Reg, MOReg))
2044*9880d681SAndroid Build Coastguard Worker DeadOps.push_back(i);
2045*9880d681SAndroid Build Coastguard Worker }
2046*9880d681SAndroid Build Coastguard Worker }
2047*9880d681SAndroid Build Coastguard Worker
2048*9880d681SAndroid Build Coastguard Worker // Trim unneeded dead operands.
2049*9880d681SAndroid Build Coastguard Worker while (!DeadOps.empty()) {
2050*9880d681SAndroid Build Coastguard Worker unsigned OpIdx = DeadOps.back();
2051*9880d681SAndroid Build Coastguard Worker if (getOperand(OpIdx).isImplicit())
2052*9880d681SAndroid Build Coastguard Worker RemoveOperand(OpIdx);
2053*9880d681SAndroid Build Coastguard Worker else
2054*9880d681SAndroid Build Coastguard Worker getOperand(OpIdx).setIsDead(false);
2055*9880d681SAndroid Build Coastguard Worker DeadOps.pop_back();
2056*9880d681SAndroid Build Coastguard Worker }
2057*9880d681SAndroid Build Coastguard Worker
2058*9880d681SAndroid Build Coastguard Worker // If not found, this means an alias of one of the operands is dead. Add a
2059*9880d681SAndroid Build Coastguard Worker // new implicit operand if required.
2060*9880d681SAndroid Build Coastguard Worker if (Found || !AddIfNotFound)
2061*9880d681SAndroid Build Coastguard Worker return Found;
2062*9880d681SAndroid Build Coastguard Worker
2063*9880d681SAndroid Build Coastguard Worker addOperand(MachineOperand::CreateReg(Reg,
2064*9880d681SAndroid Build Coastguard Worker true /*IsDef*/,
2065*9880d681SAndroid Build Coastguard Worker true /*IsImp*/,
2066*9880d681SAndroid Build Coastguard Worker false /*IsKill*/,
2067*9880d681SAndroid Build Coastguard Worker true /*IsDead*/));
2068*9880d681SAndroid Build Coastguard Worker return true;
2069*9880d681SAndroid Build Coastguard Worker }
2070*9880d681SAndroid Build Coastguard Worker
clearRegisterDeads(unsigned Reg)2071*9880d681SAndroid Build Coastguard Worker void MachineInstr::clearRegisterDeads(unsigned Reg) {
2072*9880d681SAndroid Build Coastguard Worker for (MachineOperand &MO : operands()) {
2073*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg)
2074*9880d681SAndroid Build Coastguard Worker continue;
2075*9880d681SAndroid Build Coastguard Worker MO.setIsDead(false);
2076*9880d681SAndroid Build Coastguard Worker }
2077*9880d681SAndroid Build Coastguard Worker }
2078*9880d681SAndroid Build Coastguard Worker
setRegisterDefReadUndef(unsigned Reg,bool IsUndef)2079*9880d681SAndroid Build Coastguard Worker void MachineInstr::setRegisterDefReadUndef(unsigned Reg, bool IsUndef) {
2080*9880d681SAndroid Build Coastguard Worker for (MachineOperand &MO : operands()) {
2081*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg || MO.getSubReg() == 0)
2082*9880d681SAndroid Build Coastguard Worker continue;
2083*9880d681SAndroid Build Coastguard Worker MO.setIsUndef(IsUndef);
2084*9880d681SAndroid Build Coastguard Worker }
2085*9880d681SAndroid Build Coastguard Worker }
2086*9880d681SAndroid Build Coastguard Worker
addRegisterDefined(unsigned Reg,const TargetRegisterInfo * RegInfo)2087*9880d681SAndroid Build Coastguard Worker void MachineInstr::addRegisterDefined(unsigned Reg,
2088*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *RegInfo) {
2089*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
2090*9880d681SAndroid Build Coastguard Worker MachineOperand *MO = findRegisterDefOperand(Reg, false, RegInfo);
2091*9880d681SAndroid Build Coastguard Worker if (MO)
2092*9880d681SAndroid Build Coastguard Worker return;
2093*9880d681SAndroid Build Coastguard Worker } else {
2094*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : operands()) {
2095*9880d681SAndroid Build Coastguard Worker if (MO.isReg() && MO.getReg() == Reg && MO.isDef() &&
2096*9880d681SAndroid Build Coastguard Worker MO.getSubReg() == 0)
2097*9880d681SAndroid Build Coastguard Worker return;
2098*9880d681SAndroid Build Coastguard Worker }
2099*9880d681SAndroid Build Coastguard Worker }
2100*9880d681SAndroid Build Coastguard Worker addOperand(MachineOperand::CreateReg(Reg,
2101*9880d681SAndroid Build Coastguard Worker true /*IsDef*/,
2102*9880d681SAndroid Build Coastguard Worker true /*IsImp*/));
2103*9880d681SAndroid Build Coastguard Worker }
2104*9880d681SAndroid Build Coastguard Worker
setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs,const TargetRegisterInfo & TRI)2105*9880d681SAndroid Build Coastguard Worker void MachineInstr::setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs,
2106*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo &TRI) {
2107*9880d681SAndroid Build Coastguard Worker bool HasRegMask = false;
2108*9880d681SAndroid Build Coastguard Worker for (MachineOperand &MO : operands()) {
2109*9880d681SAndroid Build Coastguard Worker if (MO.isRegMask()) {
2110*9880d681SAndroid Build Coastguard Worker HasRegMask = true;
2111*9880d681SAndroid Build Coastguard Worker continue;
2112*9880d681SAndroid Build Coastguard Worker }
2113*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isDef()) continue;
2114*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
2115*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
2116*9880d681SAndroid Build Coastguard Worker // If there are no uses, including partial uses, the def is dead.
2117*9880d681SAndroid Build Coastguard Worker if (std::none_of(UsedRegs.begin(), UsedRegs.end(),
2118*9880d681SAndroid Build Coastguard Worker [&](unsigned Use) { return TRI.regsOverlap(Use, Reg); }))
2119*9880d681SAndroid Build Coastguard Worker MO.setIsDead();
2120*9880d681SAndroid Build Coastguard Worker }
2121*9880d681SAndroid Build Coastguard Worker
2122*9880d681SAndroid Build Coastguard Worker // This is a call with a register mask operand.
2123*9880d681SAndroid Build Coastguard Worker // Mask clobbers are always dead, so add defs for the non-dead defines.
2124*9880d681SAndroid Build Coastguard Worker if (HasRegMask)
2125*9880d681SAndroid Build Coastguard Worker for (ArrayRef<unsigned>::iterator I = UsedRegs.begin(), E = UsedRegs.end();
2126*9880d681SAndroid Build Coastguard Worker I != E; ++I)
2127*9880d681SAndroid Build Coastguard Worker addRegisterDefined(*I, &TRI);
2128*9880d681SAndroid Build Coastguard Worker }
2129*9880d681SAndroid Build Coastguard Worker
2130*9880d681SAndroid Build Coastguard Worker unsigned
getHashValue(const MachineInstr * const & MI)2131*9880d681SAndroid Build Coastguard Worker MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) {
2132*9880d681SAndroid Build Coastguard Worker // Build up a buffer of hash code components.
2133*9880d681SAndroid Build Coastguard Worker SmallVector<size_t, 8> HashComponents;
2134*9880d681SAndroid Build Coastguard Worker HashComponents.reserve(MI->getNumOperands() + 1);
2135*9880d681SAndroid Build Coastguard Worker HashComponents.push_back(MI->getOpcode());
2136*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : MI->operands()) {
2137*9880d681SAndroid Build Coastguard Worker if (MO.isReg() && MO.isDef() &&
2138*9880d681SAndroid Build Coastguard Worker TargetRegisterInfo::isVirtualRegister(MO.getReg()))
2139*9880d681SAndroid Build Coastguard Worker continue; // Skip virtual register defs.
2140*9880d681SAndroid Build Coastguard Worker
2141*9880d681SAndroid Build Coastguard Worker HashComponents.push_back(hash_value(MO));
2142*9880d681SAndroid Build Coastguard Worker }
2143*9880d681SAndroid Build Coastguard Worker return hash_combine_range(HashComponents.begin(), HashComponents.end());
2144*9880d681SAndroid Build Coastguard Worker }
2145*9880d681SAndroid Build Coastguard Worker
emitError(StringRef Msg) const2146*9880d681SAndroid Build Coastguard Worker void MachineInstr::emitError(StringRef Msg) const {
2147*9880d681SAndroid Build Coastguard Worker // Find the source location cookie.
2148*9880d681SAndroid Build Coastguard Worker unsigned LocCookie = 0;
2149*9880d681SAndroid Build Coastguard Worker const MDNode *LocMD = nullptr;
2150*9880d681SAndroid Build Coastguard Worker for (unsigned i = getNumOperands(); i != 0; --i) {
2151*9880d681SAndroid Build Coastguard Worker if (getOperand(i-1).isMetadata() &&
2152*9880d681SAndroid Build Coastguard Worker (LocMD = getOperand(i-1).getMetadata()) &&
2153*9880d681SAndroid Build Coastguard Worker LocMD->getNumOperands() != 0) {
2154*9880d681SAndroid Build Coastguard Worker if (const ConstantInt *CI =
2155*9880d681SAndroid Build Coastguard Worker mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
2156*9880d681SAndroid Build Coastguard Worker LocCookie = CI->getZExtValue();
2157*9880d681SAndroid Build Coastguard Worker break;
2158*9880d681SAndroid Build Coastguard Worker }
2159*9880d681SAndroid Build Coastguard Worker }
2160*9880d681SAndroid Build Coastguard Worker }
2161*9880d681SAndroid Build Coastguard Worker
2162*9880d681SAndroid Build Coastguard Worker if (const MachineBasicBlock *MBB = getParent())
2163*9880d681SAndroid Build Coastguard Worker if (const MachineFunction *MF = MBB->getParent())
2164*9880d681SAndroid Build Coastguard Worker return MF->getMMI().getModule()->getContext().emitError(LocCookie, Msg);
2165*9880d681SAndroid Build Coastguard Worker report_fatal_error(Msg);
2166*9880d681SAndroid Build Coastguard Worker }
2167*9880d681SAndroid Build Coastguard Worker
BuildMI(MachineFunction & MF,const DebugLoc & DL,const MCInstrDesc & MCID,bool IsIndirect,unsigned Reg,unsigned Offset,const MDNode * Variable,const MDNode * Expr)2168*9880d681SAndroid Build Coastguard Worker MachineInstrBuilder llvm::BuildMI(MachineFunction &MF, const DebugLoc &DL,
2169*9880d681SAndroid Build Coastguard Worker const MCInstrDesc &MCID, bool IsIndirect,
2170*9880d681SAndroid Build Coastguard Worker unsigned Reg, unsigned Offset,
2171*9880d681SAndroid Build Coastguard Worker const MDNode *Variable, const MDNode *Expr) {
2172*9880d681SAndroid Build Coastguard Worker assert(isa<DILocalVariable>(Variable) && "not a variable");
2173*9880d681SAndroid Build Coastguard Worker assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
2174*9880d681SAndroid Build Coastguard Worker assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
2175*9880d681SAndroid Build Coastguard Worker "Expected inlined-at fields to agree");
2176*9880d681SAndroid Build Coastguard Worker if (IsIndirect)
2177*9880d681SAndroid Build Coastguard Worker return BuildMI(MF, DL, MCID)
2178*9880d681SAndroid Build Coastguard Worker .addReg(Reg, RegState::Debug)
2179*9880d681SAndroid Build Coastguard Worker .addImm(Offset)
2180*9880d681SAndroid Build Coastguard Worker .addMetadata(Variable)
2181*9880d681SAndroid Build Coastguard Worker .addMetadata(Expr);
2182*9880d681SAndroid Build Coastguard Worker else {
2183*9880d681SAndroid Build Coastguard Worker assert(Offset == 0 && "A direct address cannot have an offset.");
2184*9880d681SAndroid Build Coastguard Worker return BuildMI(MF, DL, MCID)
2185*9880d681SAndroid Build Coastguard Worker .addReg(Reg, RegState::Debug)
2186*9880d681SAndroid Build Coastguard Worker .addReg(0U, RegState::Debug)
2187*9880d681SAndroid Build Coastguard Worker .addMetadata(Variable)
2188*9880d681SAndroid Build Coastguard Worker .addMetadata(Expr);
2189*9880d681SAndroid Build Coastguard Worker }
2190*9880d681SAndroid Build Coastguard Worker }
2191*9880d681SAndroid Build Coastguard Worker
BuildMI(MachineBasicBlock & BB,MachineBasicBlock::iterator I,const DebugLoc & DL,const MCInstrDesc & MCID,bool IsIndirect,unsigned Reg,unsigned Offset,const MDNode * Variable,const MDNode * Expr)2192*9880d681SAndroid Build Coastguard Worker MachineInstrBuilder llvm::BuildMI(MachineBasicBlock &BB,
2193*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator I,
2194*9880d681SAndroid Build Coastguard Worker const DebugLoc &DL, const MCInstrDesc &MCID,
2195*9880d681SAndroid Build Coastguard Worker bool IsIndirect, unsigned Reg,
2196*9880d681SAndroid Build Coastguard Worker unsigned Offset, const MDNode *Variable,
2197*9880d681SAndroid Build Coastguard Worker const MDNode *Expr) {
2198*9880d681SAndroid Build Coastguard Worker assert(isa<DILocalVariable>(Variable) && "not a variable");
2199*9880d681SAndroid Build Coastguard Worker assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
2200*9880d681SAndroid Build Coastguard Worker MachineFunction &MF = *BB.getParent();
2201*9880d681SAndroid Build Coastguard Worker MachineInstr *MI =
2202*9880d681SAndroid Build Coastguard Worker BuildMI(MF, DL, MCID, IsIndirect, Reg, Offset, Variable, Expr);
2203*9880d681SAndroid Build Coastguard Worker BB.insert(I, MI);
2204*9880d681SAndroid Build Coastguard Worker return MachineInstrBuilder(MF, MI);
2205*9880d681SAndroid Build Coastguard Worker }
2206