xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/MachineOperand.cpp (revision e64bea71c21eb42e97aa615188ba91f6cce0d36d)
1 //===- lib/CodeGen/MachineOperand.cpp -------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file Methods common to all machine operands.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/CodeGen/MachineOperand.h"
14 #include "llvm/ADT/StableHashing.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/Analysis/Loads.h"
17 #include "llvm/CodeGen/MIRFormatter.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/CodeGen/MachineJumpTableInfo.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/PseudoSourceValueManager.h"
22 #include "llvm/CodeGen/TargetInstrInfo.h"
23 #include "llvm/CodeGen/TargetRegisterInfo.h"
24 #include "llvm/Config/llvm-config.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/IRPrintingPasses.h"
27 #include "llvm/IR/Instructions.h"
28 #include "llvm/IR/ModuleSlotTracker.h"
29 #include "llvm/MC/MCDwarf.h"
30 #include "llvm/Target/TargetMachine.h"
31 #include <optional>
32 
33 using namespace llvm;
34 
35 static cl::opt<int>
36     PrintRegMaskNumRegs("print-regmask-num-regs",
37                         cl::desc("Number of registers to limit to when "
38                                  "printing regmask operands in IR dumps. "
39                                  "unlimited = -1"),
40                         cl::init(32), cl::Hidden);
41 
getMFIfAvailable(const MachineOperand & MO)42 static const MachineFunction *getMFIfAvailable(const MachineOperand &MO) {
43   if (const MachineInstr *MI = MO.getParent())
44     if (const MachineBasicBlock *MBB = MI->getParent())
45       if (const MachineFunction *MF = MBB->getParent())
46         return MF;
47   return nullptr;
48 }
49 
getMFIfAvailable(MachineOperand & MO)50 static MachineFunction *getMFIfAvailable(MachineOperand &MO) {
51   return const_cast<MachineFunction *>(
52       getMFIfAvailable(const_cast<const MachineOperand &>(MO)));
53 }
54 
getOperandNo() const55 unsigned MachineOperand::getOperandNo() const {
56   assert(getParent() && "Operand does not belong to any instruction!");
57   return getParent()->getOperandNo(this);
58 }
59 
setReg(Register Reg)60 void MachineOperand::setReg(Register Reg) {
61   if (getReg() == Reg)
62     return; // No change.
63 
64   // Clear the IsRenamable bit to keep it conservatively correct.
65   IsRenamable = false;
66 
67   // Otherwise, we have to change the register.  If this operand is embedded
68   // into a machine function, we need to update the old and new register's
69   // use/def lists.
70   if (MachineFunction *MF = getMFIfAvailable(*this)) {
71     MachineRegisterInfo &MRI = MF->getRegInfo();
72     MRI.removeRegOperandFromUseList(this);
73     SmallContents.RegNo = Reg.id();
74     MRI.addRegOperandToUseList(this);
75     return;
76   }
77 
78   // Otherwise, just change the register, no problem.  :)
79   SmallContents.RegNo = Reg.id();
80 }
81 
substVirtReg(Register Reg,unsigned SubIdx,const TargetRegisterInfo & TRI)82 void MachineOperand::substVirtReg(Register Reg, unsigned SubIdx,
83                                   const TargetRegisterInfo &TRI) {
84   assert(Reg.isVirtual());
85   if (SubIdx && getSubReg())
86     SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg());
87   setReg(Reg);
88   if (SubIdx)
89     setSubReg(SubIdx);
90 }
91 
substPhysReg(MCRegister Reg,const TargetRegisterInfo & TRI)92 void MachineOperand::substPhysReg(MCRegister Reg, const TargetRegisterInfo &TRI) {
93   assert(Reg.isPhysical());
94   if (getSubReg()) {
95     Reg = TRI.getSubReg(Reg, getSubReg());
96     // Note that getSubReg() may return 0 if the sub-register doesn't exist.
97     // That won't happen in legal code.
98     setSubReg(0);
99     if (isDef())
100       setIsUndef(false);
101   }
102   setReg(Reg);
103 }
104 
105 /// Change a def to a use, or a use to a def.
setIsDef(bool Val)106 void MachineOperand::setIsDef(bool Val) {
107   assert(isReg() && "Wrong MachineOperand accessor");
108   assert((!Val || !isDebug()) && "Marking a debug operation as def");
109   if (IsDef == Val)
110     return;
111   assert(!IsDeadOrKill && "Changing def/use with dead/kill set not supported");
112   // MRI may keep uses and defs in different list positions.
113   if (MachineFunction *MF = getMFIfAvailable(*this)) {
114     MachineRegisterInfo &MRI = MF->getRegInfo();
115     MRI.removeRegOperandFromUseList(this);
116     IsDef = Val;
117     MRI.addRegOperandToUseList(this);
118     return;
119   }
120   IsDef = Val;
121 }
122 
isRenamable() const123 bool MachineOperand::isRenamable() const {
124   assert(isReg() && "Wrong MachineOperand accessor");
125   assert(getReg().isPhysical() &&
126          "isRenamable should only be checked on physical registers");
127   if (!IsRenamable)
128     return false;
129 
130   const MachineInstr *MI = getParent();
131   if (!MI)
132     return true;
133 
134   if (isDef())
135     return !MI->hasExtraDefRegAllocReq(MachineInstr::IgnoreBundle);
136 
137   assert(isUse() && "Reg is not def or use");
138   return !MI->hasExtraSrcRegAllocReq(MachineInstr::IgnoreBundle);
139 }
140 
setIsRenamable(bool Val)141 void MachineOperand::setIsRenamable(bool Val) {
142   assert(isReg() && "Wrong MachineOperand accessor");
143   assert(getReg().isPhysical() &&
144          "setIsRenamable should only be called on physical registers");
145   IsRenamable = Val;
146 }
147 
148 // If this operand is currently a register operand, and if this is in a
149 // function, deregister the operand from the register's use/def list.
removeRegFromUses()150 void MachineOperand::removeRegFromUses() {
151   if (!isReg() || !isOnRegUseList())
152     return;
153 
154   if (MachineFunction *MF = getMFIfAvailable(*this))
155     MF->getRegInfo().removeRegOperandFromUseList(this);
156 }
157 
158 /// ChangeToImmediate - Replace this operand with a new immediate operand of
159 /// the specified value.  If an operand is known to be an immediate already,
160 /// the setImm method should be used.
ChangeToImmediate(int64_t ImmVal,unsigned TargetFlags)161 void MachineOperand::ChangeToImmediate(int64_t ImmVal, unsigned TargetFlags) {
162   assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
163 
164   removeRegFromUses();
165 
166   OpKind = MO_Immediate;
167   Contents.ImmVal = ImmVal;
168   setTargetFlags(TargetFlags);
169 }
170 
ChangeToFPImmediate(const ConstantFP * FPImm,unsigned TargetFlags)171 void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm,
172                                          unsigned TargetFlags) {
173   assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
174 
175   removeRegFromUses();
176 
177   OpKind = MO_FPImmediate;
178   Contents.CFP = FPImm;
179   setTargetFlags(TargetFlags);
180 }
181 
ChangeToES(const char * SymName,unsigned TargetFlags)182 void MachineOperand::ChangeToES(const char *SymName,
183                                 unsigned TargetFlags) {
184   assert((!isReg() || !isTied()) &&
185          "Cannot change a tied operand into an external symbol");
186 
187   removeRegFromUses();
188 
189   OpKind = MO_ExternalSymbol;
190   Contents.OffsetedInfo.Val.SymbolName = SymName;
191   setOffset(0); // Offset is always 0.
192   setTargetFlags(TargetFlags);
193 }
194 
ChangeToGA(const GlobalValue * GV,int64_t Offset,unsigned TargetFlags)195 void MachineOperand::ChangeToGA(const GlobalValue *GV, int64_t Offset,
196                                 unsigned TargetFlags) {
197   assert((!isReg() || !isTied()) &&
198          "Cannot change a tied operand into a global address");
199 
200   removeRegFromUses();
201 
202   OpKind = MO_GlobalAddress;
203   Contents.OffsetedInfo.Val.GV = GV;
204   setOffset(Offset);
205   setTargetFlags(TargetFlags);
206 }
207 
ChangeToBA(const BlockAddress * BA,int64_t Offset,unsigned TargetFlags)208 void MachineOperand::ChangeToBA(const BlockAddress *BA, int64_t Offset,
209                                 unsigned TargetFlags) {
210   assert((!isReg() || !isTied()) &&
211          "Cannot change a tied operand into a block address");
212 
213   removeRegFromUses();
214 
215   OpKind = MO_BlockAddress;
216   Contents.OffsetedInfo.Val.BA = BA;
217   setOffset(Offset);
218   setTargetFlags(TargetFlags);
219 }
220 
ChangeToCPI(unsigned Idx,int Offset,unsigned TargetFlags)221 void MachineOperand::ChangeToCPI(unsigned Idx, int Offset,
222                                  unsigned TargetFlags) {
223   assert((!isReg() || !isTied()) &&
224          "Cannot change a tied operand into a constant pool index");
225 
226   removeRegFromUses();
227 
228   OpKind = MO_ConstantPoolIndex;
229   setIndex(Idx);
230   setOffset(Offset);
231   setTargetFlags(TargetFlags);
232 }
233 
ChangeToMCSymbol(MCSymbol * Sym,unsigned TargetFlags)234 void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym, unsigned TargetFlags) {
235   assert((!isReg() || !isTied()) &&
236          "Cannot change a tied operand into an MCSymbol");
237 
238   removeRegFromUses();
239 
240   OpKind = MO_MCSymbol;
241   Contents.Sym = Sym;
242   setTargetFlags(TargetFlags);
243 }
244 
ChangeToFrameIndex(int Idx,unsigned TargetFlags)245 void MachineOperand::ChangeToFrameIndex(int Idx, unsigned TargetFlags) {
246   assert((!isReg() || !isTied()) &&
247          "Cannot change a tied operand into a FrameIndex");
248 
249   removeRegFromUses();
250 
251   OpKind = MO_FrameIndex;
252   setIndex(Idx);
253   setTargetFlags(TargetFlags);
254 }
255 
ChangeToTargetIndex(unsigned Idx,int64_t Offset,unsigned TargetFlags)256 void MachineOperand::ChangeToTargetIndex(unsigned Idx, int64_t Offset,
257                                          unsigned TargetFlags) {
258   assert((!isReg() || !isTied()) &&
259          "Cannot change a tied operand into a FrameIndex");
260 
261   removeRegFromUses();
262 
263   OpKind = MO_TargetIndex;
264   setIndex(Idx);
265   setOffset(Offset);
266   setTargetFlags(TargetFlags);
267 }
268 
ChangeToDbgInstrRef(unsigned InstrIdx,unsigned OpIdx,unsigned TargetFlags)269 void MachineOperand::ChangeToDbgInstrRef(unsigned InstrIdx, unsigned OpIdx,
270                                          unsigned TargetFlags) {
271   assert((!isReg() || !isTied()) &&
272          "Cannot change a tied operand into a DbgInstrRef");
273 
274   removeRegFromUses();
275 
276   OpKind = MO_DbgInstrRef;
277   setInstrRefInstrIndex(InstrIdx);
278   setInstrRefOpIndex(OpIdx);
279   setTargetFlags(TargetFlags);
280 }
281 
282 /// ChangeToRegister - Replace this operand with a new register operand of
283 /// the specified value.  If an operand is known to be an register already,
284 /// the setReg method should be used.
ChangeToRegister(Register Reg,bool isDef,bool isImp,bool isKill,bool isDead,bool isUndef,bool isDebug)285 void MachineOperand::ChangeToRegister(Register Reg, bool isDef, bool isImp,
286                                       bool isKill, bool isDead, bool isUndef,
287                                       bool isDebug) {
288   MachineRegisterInfo *RegInfo = nullptr;
289   if (MachineFunction *MF = getMFIfAvailable(*this))
290     RegInfo = &MF->getRegInfo();
291   // If this operand is already a register operand, remove it from the
292   // register's use/def lists.
293   bool WasReg = isReg();
294   if (RegInfo && WasReg)
295     RegInfo->removeRegOperandFromUseList(this);
296 
297   // Ensure debug instructions set debug flag on register uses.
298   const MachineInstr *MI = getParent();
299   if (!isDef && MI && MI->isDebugInstr())
300     isDebug = true;
301 
302   // Change this to a register and set the reg#.
303   assert(!(isDead && !isDef) && "Dead flag on non-def");
304   assert(!(isKill && isDef) && "Kill flag on def");
305   OpKind = MO_Register;
306   SmallContents.RegNo = Reg.id();
307   SubReg_TargetFlags = 0;
308   IsDef = isDef;
309   IsImp = isImp;
310   IsDeadOrKill = isKill | isDead;
311   IsRenamable = false;
312   IsUndef = isUndef;
313   IsInternalRead = false;
314   IsEarlyClobber = false;
315   IsDebug = isDebug;
316   // Ensure isOnRegUseList() returns false.
317   Contents.Reg.Prev = nullptr;
318   // Preserve the tie when the operand was already a register.
319   if (!WasReg)
320     TiedTo = 0;
321 
322   // If this operand is embedded in a function, add the operand to the
323   // register's use/def list.
324   if (RegInfo)
325     RegInfo->addRegOperandToUseList(this);
326 }
327 
328 /// isIdenticalTo - Return true if this operand is identical to the specified
329 /// operand. Note that this should stay in sync with the hash_value overload
330 /// below.
isIdenticalTo(const MachineOperand & Other) const331 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
332   if (getType() != Other.getType() ||
333       getTargetFlags() != Other.getTargetFlags())
334     return false;
335 
336   switch (getType()) {
337   case MachineOperand::MO_Register:
338     return getReg() == Other.getReg() && isDef() == Other.isDef() &&
339            getSubReg() == Other.getSubReg();
340   case MachineOperand::MO_Immediate:
341     return getImm() == Other.getImm();
342   case MachineOperand::MO_CImmediate:
343     return getCImm() == Other.getCImm();
344   case MachineOperand::MO_FPImmediate:
345     return getFPImm() == Other.getFPImm();
346   case MachineOperand::MO_MachineBasicBlock:
347     return getMBB() == Other.getMBB();
348   case MachineOperand::MO_FrameIndex:
349     return getIndex() == Other.getIndex();
350   case MachineOperand::MO_ConstantPoolIndex:
351   case MachineOperand::MO_TargetIndex:
352     return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
353   case MachineOperand::MO_JumpTableIndex:
354     return getIndex() == Other.getIndex();
355   case MachineOperand::MO_GlobalAddress:
356     return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
357   case MachineOperand::MO_ExternalSymbol:
358     return strcmp(getSymbolName(), Other.getSymbolName()) == 0 &&
359            getOffset() == Other.getOffset();
360   case MachineOperand::MO_BlockAddress:
361     return getBlockAddress() == Other.getBlockAddress() &&
362            getOffset() == Other.getOffset();
363   case MachineOperand::MO_RegisterMask:
364   case MachineOperand::MO_RegisterLiveOut: {
365     // Shallow compare of the two RegMasks
366     const uint32_t *RegMask = getRegMask();
367     const uint32_t *OtherRegMask = Other.getRegMask();
368     if (RegMask == OtherRegMask)
369       return true;
370 
371     if (const MachineFunction *MF = getMFIfAvailable(*this)) {
372       const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
373       unsigned RegMaskSize = MachineOperand::getRegMaskSize(TRI->getNumRegs());
374       // Deep compare of the two RegMasks
375       return std::equal(RegMask, RegMask + RegMaskSize, OtherRegMask);
376     }
377     // We don't know the size of the RegMask, so we can't deep compare the two
378     // reg masks.
379     return false;
380   }
381   case MachineOperand::MO_MCSymbol:
382     return getMCSymbol() == Other.getMCSymbol();
383   case MachineOperand::MO_DbgInstrRef:
384     return getInstrRefInstrIndex() == Other.getInstrRefInstrIndex() &&
385            getInstrRefOpIndex() == Other.getInstrRefOpIndex();
386   case MachineOperand::MO_CFIIndex:
387     return getCFIIndex() == Other.getCFIIndex();
388   case MachineOperand::MO_Metadata:
389     return getMetadata() == Other.getMetadata();
390   case MachineOperand::MO_IntrinsicID:
391     return getIntrinsicID() == Other.getIntrinsicID();
392   case MachineOperand::MO_Predicate:
393     return getPredicate() == Other.getPredicate();
394   case MachineOperand::MO_ShuffleMask:
395     return getShuffleMask() == Other.getShuffleMask();
396   }
397   llvm_unreachable("Invalid machine operand type");
398 }
399 
400 // Note: this must stay exactly in sync with isIdenticalTo above.
hash_value(const MachineOperand & MO)401 hash_code llvm::hash_value(const MachineOperand &MO) {
402   switch (MO.getType()) {
403   case MachineOperand::MO_Register:
404     // Register operands don't have target flags.
405     return hash_combine(MO.getType(), MO.getReg().id(), MO.getSubReg(),
406                         MO.isDef());
407   case MachineOperand::MO_Immediate:
408     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm());
409   case MachineOperand::MO_CImmediate:
410     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm());
411   case MachineOperand::MO_FPImmediate:
412     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm());
413   case MachineOperand::MO_MachineBasicBlock:
414     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB());
415   case MachineOperand::MO_FrameIndex:
416     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
417   case MachineOperand::MO_ConstantPoolIndex:
418   case MachineOperand::MO_TargetIndex:
419     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(),
420                         MO.getOffset());
421   case MachineOperand::MO_JumpTableIndex:
422     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
423   case MachineOperand::MO_ExternalSymbol:
424     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(),
425                         StringRef(MO.getSymbolName()));
426   case MachineOperand::MO_GlobalAddress:
427     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(),
428                         MO.getOffset());
429   case MachineOperand::MO_BlockAddress:
430     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getBlockAddress(),
431                         MO.getOffset());
432   case MachineOperand::MO_RegisterMask:
433   case MachineOperand::MO_RegisterLiveOut: {
434     if (const MachineFunction *MF = getMFIfAvailable(MO)) {
435       const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
436       unsigned RegMaskSize = MachineOperand::getRegMaskSize(TRI->getNumRegs());
437       const uint32_t *RegMask = MO.getRegMask();
438       std::vector<stable_hash> RegMaskHashes(RegMask, RegMask + RegMaskSize);
439       return hash_combine(MO.getType(), MO.getTargetFlags(),
440                           stable_hash_combine(RegMaskHashes));
441     }
442 
443     assert(0 && "MachineOperand not associated with any MachineFunction");
444     return hash_combine(MO.getType(), MO.getTargetFlags());
445   }
446   case MachineOperand::MO_Metadata:
447     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata());
448   case MachineOperand::MO_MCSymbol:
449     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol());
450   case MachineOperand::MO_DbgInstrRef:
451     return hash_combine(MO.getType(), MO.getTargetFlags(),
452                         MO.getInstrRefInstrIndex(), MO.getInstrRefOpIndex());
453   case MachineOperand::MO_CFIIndex:
454     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex());
455   case MachineOperand::MO_IntrinsicID:
456     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIntrinsicID());
457   case MachineOperand::MO_Predicate:
458     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getPredicate());
459   case MachineOperand::MO_ShuffleMask:
460     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getShuffleMask());
461   }
462   llvm_unreachable("Invalid machine operand type");
463 }
464 
465 // Try to crawl up to the machine function and get TRI from it.
tryToGetTargetInfo(const MachineOperand & MO,const TargetRegisterInfo * & TRI)466 static void tryToGetTargetInfo(const MachineOperand &MO,
467                                const TargetRegisterInfo *&TRI) {
468   if (const MachineFunction *MF = getMFIfAvailable(MO)) {
469     TRI = MF->getSubtarget().getRegisterInfo();
470   }
471 }
472 
getTargetIndexName(const MachineFunction & MF,int Index)473 static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
474   const auto *TII = MF.getSubtarget().getInstrInfo();
475   assert(TII && "expected instruction info");
476   auto Indices = TII->getSerializableTargetIndices();
477   auto Found = find_if(Indices, [&](const std::pair<int, const char *> &I) {
478     return I.first == Index;
479   });
480   if (Found != Indices.end())
481     return Found->second;
482   return nullptr;
483 }
484 
getTargetIndexName() const485 const char *MachineOperand::getTargetIndexName() const {
486   const MachineFunction *MF = getMFIfAvailable(*this);
487   return MF ? ::getTargetIndexName(*MF, this->getIndex()) : nullptr;
488 }
489 
getTargetFlagName(const TargetInstrInfo * TII,unsigned TF)490 static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
491   auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
492   for (const auto &I : Flags) {
493     if (I.first == TF) {
494       return I.second;
495     }
496   }
497   return nullptr;
498 }
499 
printCFIRegister(unsigned DwarfReg,raw_ostream & OS,const TargetRegisterInfo * TRI)500 static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
501                              const TargetRegisterInfo *TRI) {
502   if (!TRI) {
503     OS << "%dwarfreg." << DwarfReg;
504     return;
505   }
506 
507   if (std::optional<MCRegister> Reg = TRI->getLLVMRegNum(DwarfReg, true))
508     OS << printReg(*Reg, TRI);
509   else
510     OS << "<badreg>";
511 }
512 
printIRBlockReference(raw_ostream & OS,const BasicBlock & BB,ModuleSlotTracker & MST)513 static void printIRBlockReference(raw_ostream &OS, const BasicBlock &BB,
514                                   ModuleSlotTracker &MST) {
515   OS << "%ir-block.";
516   if (BB.hasName()) {
517     printLLVMNameWithoutPrefix(OS, BB.getName());
518     return;
519   }
520   std::optional<int> Slot;
521   if (const Function *F = BB.getParent()) {
522     if (F == MST.getCurrentFunction()) {
523       Slot = MST.getLocalSlot(&BB);
524     } else if (const Module *M = F->getParent()) {
525       ModuleSlotTracker CustomMST(M, /*ShouldInitializeAllMetadata=*/false);
526       CustomMST.incorporateFunction(*F);
527       Slot = CustomMST.getLocalSlot(&BB);
528     }
529   }
530   if (Slot)
531     MachineOperand::printIRSlotNumber(OS, *Slot);
532   else
533     OS << "<unknown>";
534 }
535 
printSyncScope(raw_ostream & OS,const LLVMContext & Context,SyncScope::ID SSID,SmallVectorImpl<StringRef> & SSNs)536 static void printSyncScope(raw_ostream &OS, const LLVMContext &Context,
537                            SyncScope::ID SSID,
538                            SmallVectorImpl<StringRef> &SSNs) {
539   switch (SSID) {
540   case SyncScope::System:
541     break;
542   default:
543     if (SSNs.empty())
544       Context.getSyncScopeNames(SSNs);
545 
546     OS << "syncscope(\"";
547     printEscapedString(SSNs[SSID], OS);
548     OS << "\") ";
549     break;
550   }
551 }
552 
getTargetMMOFlagName(const TargetInstrInfo & TII,unsigned TMMOFlag)553 static const char *getTargetMMOFlagName(const TargetInstrInfo &TII,
554                                         unsigned TMMOFlag) {
555   auto Flags = TII.getSerializableMachineMemOperandTargetFlags();
556   for (const auto &I : Flags) {
557     if (I.first == TMMOFlag) {
558       return I.second;
559     }
560   }
561   return nullptr;
562 }
563 
printFrameIndex(raw_ostream & OS,int FrameIndex,bool IsFixed,const MachineFrameInfo * MFI)564 static void printFrameIndex(raw_ostream& OS, int FrameIndex, bool IsFixed,
565                             const MachineFrameInfo *MFI) {
566   StringRef Name;
567   if (MFI) {
568     IsFixed = MFI->isFixedObjectIndex(FrameIndex);
569     if (const AllocaInst *Alloca = MFI->getObjectAllocation(FrameIndex))
570       if (Alloca->hasName())
571         Name = Alloca->getName();
572     if (IsFixed)
573       FrameIndex -= MFI->getObjectIndexBegin();
574   }
575   MachineOperand::printStackObjectReference(OS, FrameIndex, IsFixed, Name);
576 }
577 
printSubRegIdx(raw_ostream & OS,uint64_t Index,const TargetRegisterInfo * TRI)578 void MachineOperand::printSubRegIdx(raw_ostream &OS, uint64_t Index,
579                                     const TargetRegisterInfo *TRI) {
580   OS << "%subreg.";
581   if (TRI && Index != 0 && Index < TRI->getNumSubRegIndices())
582     OS << TRI->getSubRegIndexName(Index);
583   else
584     OS << Index;
585 }
586 
printTargetFlags(raw_ostream & OS,const MachineOperand & Op)587 void MachineOperand::printTargetFlags(raw_ostream &OS,
588                                       const MachineOperand &Op) {
589   if (!Op.getTargetFlags())
590     return;
591   const MachineFunction *MF = getMFIfAvailable(Op);
592   if (!MF)
593     return;
594 
595   const auto *TII = MF->getSubtarget().getInstrInfo();
596   assert(TII && "expected instruction info");
597   auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
598   OS << "target-flags(";
599   const bool HasDirectFlags = Flags.first;
600   const bool HasBitmaskFlags = Flags.second;
601   if (!HasDirectFlags && !HasBitmaskFlags) {
602     OS << "<unknown>) ";
603     return;
604   }
605   if (HasDirectFlags) {
606     if (const auto *Name = getTargetFlagName(TII, Flags.first))
607       OS << Name;
608     else
609       OS << "<unknown target flag>";
610   }
611   if (!HasBitmaskFlags) {
612     OS << ") ";
613     return;
614   }
615   bool IsCommaNeeded = HasDirectFlags;
616   unsigned BitMask = Flags.second;
617   auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
618   for (const auto &Mask : BitMasks) {
619     // Check if the flag's bitmask has the bits of the current mask set.
620     if ((BitMask & Mask.first) == Mask.first) {
621       if (IsCommaNeeded)
622         OS << ", ";
623       IsCommaNeeded = true;
624       OS << Mask.second;
625       // Clear the bits which were serialized from the flag's bitmask.
626       BitMask &= ~(Mask.first);
627     }
628   }
629   if (BitMask) {
630     // When the resulting flag's bitmask isn't zero, we know that we didn't
631     // serialize all of the bit flags.
632     if (IsCommaNeeded)
633       OS << ", ";
634     OS << "<unknown bitmask target flag>";
635   }
636   OS << ") ";
637 }
638 
printSymbol(raw_ostream & OS,MCSymbol & Sym)639 void MachineOperand::printSymbol(raw_ostream &OS, MCSymbol &Sym) {
640   OS << "<mcsymbol " << Sym << ">";
641 }
642 
printStackObjectReference(raw_ostream & OS,unsigned FrameIndex,bool IsFixed,StringRef Name)643 void MachineOperand::printStackObjectReference(raw_ostream &OS,
644                                                unsigned FrameIndex,
645                                                bool IsFixed, StringRef Name) {
646   if (IsFixed) {
647     OS << "%fixed-stack." << FrameIndex;
648     return;
649   }
650 
651   OS << "%stack." << FrameIndex;
652   if (!Name.empty())
653     OS << '.' << Name;
654 }
655 
printOperandOffset(raw_ostream & OS,int64_t Offset)656 void MachineOperand::printOperandOffset(raw_ostream &OS, int64_t Offset) {
657   if (Offset == 0)
658     return;
659   if (Offset < 0) {
660     OS << " - " << -Offset;
661     return;
662   }
663   OS << " + " << Offset;
664 }
665 
printIRSlotNumber(raw_ostream & OS,int Slot)666 void MachineOperand::printIRSlotNumber(raw_ostream &OS, int Slot) {
667   if (Slot == -1)
668     OS << "<badref>";
669   else
670     OS << Slot;
671 }
672 
printCFI(raw_ostream & OS,const MCCFIInstruction & CFI,const TargetRegisterInfo * TRI)673 static void printCFI(raw_ostream &OS, const MCCFIInstruction &CFI,
674                      const TargetRegisterInfo *TRI) {
675   switch (CFI.getOperation()) {
676   case MCCFIInstruction::OpSameValue:
677     OS << "same_value ";
678     if (MCSymbol *Label = CFI.getLabel())
679       MachineOperand::printSymbol(OS, *Label);
680     printCFIRegister(CFI.getRegister(), OS, TRI);
681     break;
682   case MCCFIInstruction::OpRememberState:
683     OS << "remember_state ";
684     if (MCSymbol *Label = CFI.getLabel())
685       MachineOperand::printSymbol(OS, *Label);
686     break;
687   case MCCFIInstruction::OpRestoreState:
688     OS << "restore_state ";
689     if (MCSymbol *Label = CFI.getLabel())
690       MachineOperand::printSymbol(OS, *Label);
691     break;
692   case MCCFIInstruction::OpOffset:
693     OS << "offset ";
694     if (MCSymbol *Label = CFI.getLabel())
695       MachineOperand::printSymbol(OS, *Label);
696     printCFIRegister(CFI.getRegister(), OS, TRI);
697     OS << ", " << CFI.getOffset();
698     break;
699   case MCCFIInstruction::OpDefCfaRegister:
700     OS << "def_cfa_register ";
701     if (MCSymbol *Label = CFI.getLabel())
702       MachineOperand::printSymbol(OS, *Label);
703     printCFIRegister(CFI.getRegister(), OS, TRI);
704     break;
705   case MCCFIInstruction::OpDefCfaOffset:
706     OS << "def_cfa_offset ";
707     if (MCSymbol *Label = CFI.getLabel())
708       MachineOperand::printSymbol(OS, *Label);
709     OS << CFI.getOffset();
710     break;
711   case MCCFIInstruction::OpDefCfa:
712     OS << "def_cfa ";
713     if (MCSymbol *Label = CFI.getLabel())
714       MachineOperand::printSymbol(OS, *Label);
715     printCFIRegister(CFI.getRegister(), OS, TRI);
716     OS << ", " << CFI.getOffset();
717     break;
718   case MCCFIInstruction::OpLLVMDefAspaceCfa:
719     OS << "llvm_def_aspace_cfa ";
720     if (MCSymbol *Label = CFI.getLabel())
721       MachineOperand::printSymbol(OS, *Label);
722     printCFIRegister(CFI.getRegister(), OS, TRI);
723     OS << ", " << CFI.getOffset();
724     OS << ", " << CFI.getAddressSpace();
725     break;
726   case MCCFIInstruction::OpRelOffset:
727     OS << "rel_offset ";
728     if (MCSymbol *Label = CFI.getLabel())
729       MachineOperand::printSymbol(OS, *Label);
730     printCFIRegister(CFI.getRegister(), OS, TRI);
731     OS << ", " << CFI.getOffset();
732     break;
733   case MCCFIInstruction::OpAdjustCfaOffset:
734     OS << "adjust_cfa_offset ";
735     if (MCSymbol *Label = CFI.getLabel())
736       MachineOperand::printSymbol(OS, *Label);
737     OS << CFI.getOffset();
738     break;
739   case MCCFIInstruction::OpRestore:
740     OS << "restore ";
741     if (MCSymbol *Label = CFI.getLabel())
742       MachineOperand::printSymbol(OS, *Label);
743     printCFIRegister(CFI.getRegister(), OS, TRI);
744     break;
745   case MCCFIInstruction::OpEscape: {
746     OS << "escape ";
747     if (MCSymbol *Label = CFI.getLabel())
748       MachineOperand::printSymbol(OS, *Label);
749     if (!CFI.getValues().empty()) {
750       size_t e = CFI.getValues().size() - 1;
751       for (size_t i = 0; i < e; ++i)
752         OS << format("0x%02x", uint8_t(CFI.getValues()[i])) << ", ";
753       OS << format("0x%02x", uint8_t(CFI.getValues()[e]));
754     }
755     break;
756   }
757   case MCCFIInstruction::OpUndefined:
758     OS << "undefined ";
759     if (MCSymbol *Label = CFI.getLabel())
760       MachineOperand::printSymbol(OS, *Label);
761     printCFIRegister(CFI.getRegister(), OS, TRI);
762     break;
763   case MCCFIInstruction::OpRegister:
764     OS << "register ";
765     if (MCSymbol *Label = CFI.getLabel())
766       MachineOperand::printSymbol(OS, *Label);
767     printCFIRegister(CFI.getRegister(), OS, TRI);
768     OS << ", ";
769     printCFIRegister(CFI.getRegister2(), OS, TRI);
770     break;
771   case MCCFIInstruction::OpWindowSave:
772     OS << "window_save ";
773     if (MCSymbol *Label = CFI.getLabel())
774       MachineOperand::printSymbol(OS, *Label);
775     break;
776   case MCCFIInstruction::OpNegateRAState:
777     OS << "negate_ra_sign_state ";
778     if (MCSymbol *Label = CFI.getLabel())
779       MachineOperand::printSymbol(OS, *Label);
780     break;
781   case MCCFIInstruction::OpNegateRAStateWithPC:
782     OS << "negate_ra_sign_state_with_pc ";
783     if (MCSymbol *Label = CFI.getLabel())
784       MachineOperand::printSymbol(OS, *Label);
785     break;
786   default:
787     // TODO: Print the other CFI Operations.
788     OS << "<unserializable cfi directive>";
789     break;
790   }
791 }
792 
print(raw_ostream & OS,const TargetRegisterInfo * TRI) const793 void MachineOperand::print(raw_ostream &OS,
794                            const TargetRegisterInfo *TRI) const {
795   print(OS, LLT{}, TRI);
796 }
797 
print(raw_ostream & OS,LLT TypeToPrint,const TargetRegisterInfo * TRI) const798 void MachineOperand::print(raw_ostream &OS, LLT TypeToPrint,
799                            const TargetRegisterInfo *TRI) const {
800   tryToGetTargetInfo(*this, TRI);
801   ModuleSlotTracker DummyMST(nullptr);
802   print(OS, DummyMST, TypeToPrint, std::nullopt, /*PrintDef=*/false,
803         /*IsStandalone=*/true,
804         /*ShouldPrintRegisterTies=*/true,
805         /*TiedOperandIdx=*/0, TRI);
806 }
807 
print(raw_ostream & OS,ModuleSlotTracker & MST,LLT TypeToPrint,std::optional<unsigned> OpIdx,bool PrintDef,bool IsStandalone,bool ShouldPrintRegisterTies,unsigned TiedOperandIdx,const TargetRegisterInfo * TRI) const808 void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
809                            LLT TypeToPrint, std::optional<unsigned> OpIdx,
810                            bool PrintDef, bool IsStandalone,
811                            bool ShouldPrintRegisterTies,
812                            unsigned TiedOperandIdx,
813                            const TargetRegisterInfo *TRI) const {
814   printTargetFlags(OS, *this);
815   switch (getType()) {
816   case MachineOperand::MO_Register: {
817     Register Reg = getReg();
818     if (isImplicit())
819       OS << (isDef() ? "implicit-def " : "implicit ");
820     else if (PrintDef && isDef())
821       // Print the 'def' flag only when the operand is defined after '='.
822       OS << "def ";
823     if (isInternalRead())
824       OS << "internal ";
825     if (isDead())
826       OS << "dead ";
827     if (isKill())
828       OS << "killed ";
829     if (isUndef())
830       OS << "undef ";
831     if (isEarlyClobber())
832       OS << "early-clobber ";
833     if (getReg().isPhysical() && isRenamable())
834       OS << "renamable ";
835     // isDebug() is exactly true for register operands of a DBG_VALUE. So we
836     // simply infer it when parsing and do not need to print it.
837 
838     const MachineRegisterInfo *MRI = nullptr;
839     if (Reg.isVirtual()) {
840       if (const MachineFunction *MF = getMFIfAvailable(*this)) {
841         MRI = &MF->getRegInfo();
842       }
843     }
844 
845     OS << printReg(Reg, TRI, 0, MRI);
846     // Print the sub register.
847     if (unsigned SubReg = getSubReg()) {
848       if (TRI)
849         OS << '.' << TRI->getSubRegIndexName(SubReg);
850       else
851         OS << ".subreg" << SubReg;
852     }
853     // Print the register class / bank.
854     if (Reg.isVirtual()) {
855       if (const MachineFunction *MF = getMFIfAvailable(*this)) {
856         const MachineRegisterInfo &MRI = MF->getRegInfo();
857         if (IsStandalone || !PrintDef || MRI.def_empty(Reg)) {
858           OS << ':';
859           OS << printRegClassOrBank(Reg, MRI, TRI);
860         }
861       }
862     }
863     // Print ties.
864     if (ShouldPrintRegisterTies && isTied() && !isDef())
865       OS << "(tied-def " << TiedOperandIdx << ")";
866     // Print types.
867     if (TypeToPrint.isValid())
868       OS << '(' << TypeToPrint << ')';
869     break;
870   }
871   case MachineOperand::MO_Immediate: {
872     const MIRFormatter *Formatter = nullptr;
873     if (const MachineFunction *MF = getMFIfAvailable(*this)) {
874       const auto *TII = MF->getSubtarget().getInstrInfo();
875       assert(TII && "expected instruction info");
876       Formatter = TII->getMIRFormatter();
877     }
878     if (Formatter)
879       Formatter->printImm(OS, *getParent(), OpIdx, getImm());
880     else
881       OS << getImm();
882     break;
883   }
884   case MachineOperand::MO_CImmediate:
885     getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
886     break;
887   case MachineOperand::MO_FPImmediate:
888     getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
889     break;
890   case MachineOperand::MO_MachineBasicBlock:
891     OS << printMBBReference(*getMBB());
892     break;
893   case MachineOperand::MO_FrameIndex: {
894     int FrameIndex = getIndex();
895     bool IsFixed = false;
896     const MachineFrameInfo *MFI = nullptr;
897     if (const MachineFunction *MF = getMFIfAvailable(*this))
898       MFI = &MF->getFrameInfo();
899     printFrameIndex(OS, FrameIndex, IsFixed, MFI);
900     break;
901   }
902   case MachineOperand::MO_ConstantPoolIndex:
903     OS << "%const." << getIndex();
904     printOperandOffset(OS, getOffset());
905     break;
906   case MachineOperand::MO_TargetIndex: {
907     OS << "target-index(";
908     const char *Name = "<unknown>";
909     if (const MachineFunction *MF = getMFIfAvailable(*this))
910       if (const auto *TargetIndexName = ::getTargetIndexName(*MF, getIndex()))
911         Name = TargetIndexName;
912     OS << Name << ')';
913     printOperandOffset(OS, getOffset());
914     break;
915   }
916   case MachineOperand::MO_JumpTableIndex:
917     OS << printJumpTableEntryReference(getIndex());
918     break;
919   case MachineOperand::MO_GlobalAddress:
920     if (auto *GV = getGlobal())
921       GV->printAsOperand(OS, /*PrintType=*/false, MST);
922     else // Invalid, but may appear in debugging scenarios.
923       OS << "globaladdress(null)";
924 
925     printOperandOffset(OS, getOffset());
926     break;
927   case MachineOperand::MO_ExternalSymbol: {
928     StringRef Name = getSymbolName();
929     OS << '&';
930     if (Name.empty()) {
931       OS << "\"\"";
932     } else {
933       printLLVMNameWithoutPrefix(OS, Name);
934     }
935     printOperandOffset(OS, getOffset());
936     break;
937   }
938   case MachineOperand::MO_BlockAddress: {
939     OS << "blockaddress(";
940     getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
941                                                      MST);
942     OS << ", ";
943     printIRBlockReference(OS, *getBlockAddress()->getBasicBlock(), MST);
944     OS << ')';
945     MachineOperand::printOperandOffset(OS, getOffset());
946     break;
947   }
948   case MachineOperand::MO_RegisterMask: {
949     OS << "<regmask";
950     if (TRI) {
951       unsigned NumRegsInMask = 0;
952       unsigned NumRegsEmitted = 0;
953       for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {
954         unsigned MaskWord = i / 32;
955         unsigned MaskBit = i % 32;
956         if (getRegMask()[MaskWord] & (1 << MaskBit)) {
957           if (PrintRegMaskNumRegs < 0 ||
958               NumRegsEmitted <= static_cast<unsigned>(PrintRegMaskNumRegs)) {
959             OS << " " << printReg(i, TRI);
960             NumRegsEmitted++;
961           }
962           NumRegsInMask++;
963         }
964       }
965       if (NumRegsEmitted != NumRegsInMask)
966         OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more...";
967     } else {
968       OS << " ...";
969     }
970     OS << ">";
971     break;
972   }
973   case MachineOperand::MO_RegisterLiveOut: {
974     const uint32_t *RegMask = getRegLiveOut();
975     OS << "liveout(";
976     if (!TRI) {
977       OS << "<unknown>";
978     } else {
979       bool IsCommaNeeded = false;
980       for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
981         if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
982           if (IsCommaNeeded)
983             OS << ", ";
984           OS << printReg(Reg, TRI);
985           IsCommaNeeded = true;
986         }
987       }
988     }
989     OS << ")";
990     break;
991   }
992   case MachineOperand::MO_Metadata:
993     getMetadata()->printAsOperand(OS, MST);
994     break;
995   case MachineOperand::MO_MCSymbol:
996     printSymbol(OS, *getMCSymbol());
997     break;
998   case MachineOperand::MO_DbgInstrRef: {
999     OS << "dbg-instr-ref(" << getInstrRefInstrIndex() << ", "
1000        << getInstrRefOpIndex() << ')';
1001     break;
1002   }
1003   case MachineOperand::MO_CFIIndex: {
1004     if (const MachineFunction *MF = getMFIfAvailable(*this))
1005       printCFI(OS, MF->getFrameInstructions()[getCFIIndex()], TRI);
1006     else
1007       OS << "<cfi directive>";
1008     break;
1009   }
1010   case MachineOperand::MO_IntrinsicID: {
1011     Intrinsic::ID ID = getIntrinsicID();
1012     if (ID < Intrinsic::num_intrinsics)
1013       OS << "intrinsic(@" << Intrinsic::getBaseName(ID) << ')';
1014     else
1015       OS << "intrinsic(" << ID << ')';
1016     break;
1017   }
1018   case MachineOperand::MO_Predicate: {
1019     auto Pred = static_cast<CmpInst::Predicate>(getPredicate());
1020     OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
1021        << Pred << ')';
1022     break;
1023   }
1024   case MachineOperand::MO_ShuffleMask:
1025     OS << "shufflemask(";
1026     ArrayRef<int> Mask = getShuffleMask();
1027     StringRef Separator;
1028     for (int Elt : Mask) {
1029       if (Elt == -1)
1030         OS << Separator << "undef";
1031       else
1032         OS << Separator << Elt;
1033       Separator = ", ";
1034     }
1035 
1036     OS << ')';
1037     break;
1038   }
1039 }
1040 
1041 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const1042 LLVM_DUMP_METHOD void MachineOperand::dump() const { dbgs() << *this << '\n'; }
1043 #endif
1044 
1045 //===----------------------------------------------------------------------===//
1046 // MachineMemOperand Implementation
1047 //===----------------------------------------------------------------------===//
1048 
1049 /// getAddrSpace - Return the LLVM IR address space number that this pointer
1050 /// points into.
getAddrSpace() const1051 unsigned MachinePointerInfo::getAddrSpace() const { return AddrSpace; }
1052 
1053 /// isDereferenceable - Return true if V is always dereferenceable for
1054 /// Offset + Size byte.
isDereferenceable(unsigned Size,LLVMContext & C,const DataLayout & DL) const1055 bool MachinePointerInfo::isDereferenceable(unsigned Size, LLVMContext &C,
1056                                            const DataLayout &DL) const {
1057   if (!isa<const Value *>(V))
1058     return false;
1059 
1060   const Value *BasePtr = cast<const Value *>(V);
1061   if (BasePtr == nullptr)
1062     return false;
1063 
1064   return isDereferenceableAndAlignedPointer(
1065       BasePtr, Align(1), APInt(DL.getPointerSizeInBits(), Offset + Size), DL,
1066       dyn_cast<Instruction>(BasePtr));
1067 }
1068 
1069 /// getConstantPool - Return a MachinePointerInfo record that refers to the
1070 /// constant pool.
getConstantPool(MachineFunction & MF)1071 MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) {
1072   return MachinePointerInfo(MF.getPSVManager().getConstantPool());
1073 }
1074 
1075 /// getFixedStack - Return a MachinePointerInfo record that refers to the
1076 /// the specified FrameIndex.
getFixedStack(MachineFunction & MF,int FI,int64_t Offset)1077 MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF,
1078                                                      int FI, int64_t Offset) {
1079   return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset);
1080 }
1081 
getJumpTable(MachineFunction & MF)1082 MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) {
1083   return MachinePointerInfo(MF.getPSVManager().getJumpTable());
1084 }
1085 
getGOT(MachineFunction & MF)1086 MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) {
1087   return MachinePointerInfo(MF.getPSVManager().getGOT());
1088 }
1089 
getStack(MachineFunction & MF,int64_t Offset,uint8_t ID)1090 MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF,
1091                                                 int64_t Offset, uint8_t ID) {
1092   return MachinePointerInfo(MF.getPSVManager().getStack(), Offset, ID);
1093 }
1094 
getUnknownStack(MachineFunction & MF)1095 MachinePointerInfo MachinePointerInfo::getUnknownStack(MachineFunction &MF) {
1096   return MachinePointerInfo(MF.getDataLayout().getAllocaAddrSpace());
1097 }
1098 
MachineMemOperand(MachinePointerInfo ptrinfo,Flags f,LLT type,Align a,const AAMDNodes & AAInfo,const MDNode * Ranges,SyncScope::ID SSID,AtomicOrdering Ordering,AtomicOrdering FailureOrdering)1099 MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f,
1100                                      LLT type, Align a, const AAMDNodes &AAInfo,
1101                                      const MDNode *Ranges, SyncScope::ID SSID,
1102                                      AtomicOrdering Ordering,
1103                                      AtomicOrdering FailureOrdering)
1104     : PtrInfo(ptrinfo), MemoryType(type), FlagVals(f), BaseAlign(a),
1105       AAInfo(AAInfo), Ranges(Ranges) {
1106   assert((PtrInfo.V.isNull() || isa<const PseudoSourceValue *>(PtrInfo.V) ||
1107           isa<PointerType>(cast<const Value *>(PtrInfo.V)->getType())) &&
1108          "invalid pointer value");
1109   assert((isLoad() || isStore()) && "Not a load/store!");
1110 
1111   AtomicInfo.SSID = static_cast<unsigned>(SSID);
1112   assert(getSyncScopeID() == SSID && "Value truncated");
1113   AtomicInfo.Ordering = static_cast<unsigned>(Ordering);
1114   assert(getSuccessOrdering() == Ordering && "Value truncated");
1115   AtomicInfo.FailureOrdering = static_cast<unsigned>(FailureOrdering);
1116   assert(getFailureOrdering() == FailureOrdering && "Value truncated");
1117 }
1118 
MachineMemOperand(MachinePointerInfo ptrinfo,Flags F,LocationSize TS,Align BaseAlignment,const AAMDNodes & AAInfo,const MDNode * Ranges,SyncScope::ID SSID,AtomicOrdering Ordering,AtomicOrdering FailureOrdering)1119 MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags F,
1120                                      LocationSize TS, Align BaseAlignment,
1121                                      const AAMDNodes &AAInfo,
1122                                      const MDNode *Ranges, SyncScope::ID SSID,
1123                                      AtomicOrdering Ordering,
1124                                      AtomicOrdering FailureOrdering)
1125     : MachineMemOperand(
1126           ptrinfo, F,
1127           !TS.hasValue() ? LLT()
1128           : TS.isScalable()
1129               ? LLT::scalable_vector(1, 8 * TS.getValue().getKnownMinValue())
1130               : LLT::scalar(8 * TS.getValue().getKnownMinValue()),
1131           BaseAlignment, AAInfo, Ranges, SSID, Ordering, FailureOrdering) {}
1132 
refineAlignment(const MachineMemOperand * MMO)1133 void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
1134   // The Value and Offset may differ due to CSE. But the flags and size
1135   // should be the same.
1136   assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
1137   assert((!MMO->getSize().hasValue() || !getSize().hasValue() ||
1138           MMO->getSize() == getSize()) &&
1139          "Size mismatch!");
1140   if (MMO->getBaseAlign() >= getBaseAlign()) {
1141     // Update the alignment value.
1142     BaseAlign = MMO->getBaseAlign();
1143     // Also update the base and offset, because the new alignment may
1144     // not be applicable with the old ones.
1145     PtrInfo = MMO->PtrInfo;
1146   }
1147 }
1148 
1149 /// getAlign - Return the minimum known alignment in bytes of the
1150 /// actual memory reference.
getAlign() const1151 Align MachineMemOperand::getAlign() const {
1152   return commonAlignment(getBaseAlign(), getOffset());
1153 }
1154 
print(raw_ostream & OS,ModuleSlotTracker & MST,SmallVectorImpl<StringRef> & SSNs,const LLVMContext & Context,const MachineFrameInfo * MFI,const TargetInstrInfo * TII) const1155 void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
1156                               SmallVectorImpl<StringRef> &SSNs,
1157                               const LLVMContext &Context,
1158                               const MachineFrameInfo *MFI,
1159                               const TargetInstrInfo *TII) const {
1160   OS << '(';
1161   if (isVolatile())
1162     OS << "volatile ";
1163   if (isNonTemporal())
1164     OS << "non-temporal ";
1165   if (isDereferenceable())
1166     OS << "dereferenceable ";
1167   if (isInvariant())
1168     OS << "invariant ";
1169   if (TII) {
1170     if (getFlags() & MachineMemOperand::MOTargetFlag1)
1171       OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag1)
1172          << "\" ";
1173     if (getFlags() & MachineMemOperand::MOTargetFlag2)
1174       OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag2)
1175          << "\" ";
1176     if (getFlags() & MachineMemOperand::MOTargetFlag3)
1177       OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag3)
1178          << "\" ";
1179     if (getFlags() & MachineMemOperand::MOTargetFlag4)
1180       OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag4)
1181          << "\" ";
1182   } else {
1183     if (getFlags() & MachineMemOperand::MOTargetFlag1)
1184       OS << "\"MOTargetFlag1\" ";
1185     if (getFlags() & MachineMemOperand::MOTargetFlag2)
1186       OS << "\"MOTargetFlag2\" ";
1187     if (getFlags() & MachineMemOperand::MOTargetFlag3)
1188       OS << "\"MOTargetFlag3\" ";
1189     if (getFlags() & MachineMemOperand::MOTargetFlag4)
1190       OS << "\"MOTargetFlag4\" ";
1191   }
1192 
1193   assert((isLoad() || isStore()) &&
1194          "machine memory operand must be a load or store (or both)");
1195   if (isLoad())
1196     OS << "load ";
1197   if (isStore())
1198     OS << "store ";
1199 
1200   printSyncScope(OS, Context, getSyncScopeID(), SSNs);
1201 
1202   if (getSuccessOrdering() != AtomicOrdering::NotAtomic)
1203     OS << toIRString(getSuccessOrdering()) << ' ';
1204   if (getFailureOrdering() != AtomicOrdering::NotAtomic)
1205     OS << toIRString(getFailureOrdering()) << ' ';
1206 
1207   if (getMemoryType().isValid())
1208     OS << '(' << getMemoryType() << ')';
1209   else
1210     OS << "unknown-size";
1211 
1212   if (const Value *Val = getValue()) {
1213     OS << ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into ");
1214     MIRFormatter::printIRValue(OS, *Val, MST);
1215   } else if (const PseudoSourceValue *PVal = getPseudoValue()) {
1216     OS << ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into ");
1217     assert(PVal && "Expected a pseudo source value");
1218     switch (PVal->kind()) {
1219     case PseudoSourceValue::Stack:
1220       OS << "stack";
1221       break;
1222     case PseudoSourceValue::GOT:
1223       OS << "got";
1224       break;
1225     case PseudoSourceValue::JumpTable:
1226       OS << "jump-table";
1227       break;
1228     case PseudoSourceValue::ConstantPool:
1229       OS << "constant-pool";
1230       break;
1231     case PseudoSourceValue::FixedStack: {
1232       int FrameIndex = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex();
1233       bool IsFixed = true;
1234       printFrameIndex(OS, FrameIndex, IsFixed, MFI);
1235       break;
1236     }
1237     case PseudoSourceValue::GlobalValueCallEntry:
1238       OS << "call-entry ";
1239       cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
1240           OS, /*PrintType=*/false, MST);
1241       break;
1242     case PseudoSourceValue::ExternalSymbolCallEntry:
1243       OS << "call-entry &";
1244       printLLVMNameWithoutPrefix(
1245           OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
1246       break;
1247     default: {
1248       // FIXME: This is not necessarily the correct MIR serialization format for
1249       // a custom pseudo source value, but at least it allows
1250       // MIR printing to work on a target with custom pseudo source
1251       // values.
1252       OS << "custom \"";
1253       if (TII) {
1254         const MIRFormatter *Formatter = TII->getMIRFormatter();
1255         Formatter->printCustomPseudoSourceValue(OS, MST, *PVal);
1256       } else {
1257         PVal->printCustom(OS);
1258       }
1259       OS << '\"';
1260       break;
1261     }
1262     }
1263   } else if (getOpaqueValue() == nullptr && getOffset() != 0) {
1264     OS << ((isLoad() && isStore()) ? " on "
1265            : isLoad()              ? " from "
1266                                    : " into ")
1267        << "unknown-address";
1268   }
1269   MachineOperand::printOperandOffset(OS, getOffset());
1270   if (!getSize().hasValue() ||
1271       (!getSize().isZero() &&
1272        getAlign() != getSize().getValue().getKnownMinValue()))
1273     OS << ", align " << getAlign().value();
1274   if (getAlign() != getBaseAlign())
1275     OS << ", basealign " << getBaseAlign().value();
1276   auto AAInfo = getAAInfo();
1277   if (AAInfo.TBAA) {
1278     OS << ", !tbaa ";
1279     AAInfo.TBAA->printAsOperand(OS, MST);
1280   }
1281   if (AAInfo.Scope) {
1282     OS << ", !alias.scope ";
1283     AAInfo.Scope->printAsOperand(OS, MST);
1284   }
1285   if (AAInfo.NoAlias) {
1286     OS << ", !noalias ";
1287     AAInfo.NoAlias->printAsOperand(OS, MST);
1288   }
1289   if (getRanges()) {
1290     OS << ", !range ";
1291     getRanges()->printAsOperand(OS, MST);
1292   }
1293   // FIXME: Implement addrspace printing/parsing in MIR.
1294   // For now, print this even though parsing it is not available in MIR.
1295   if (unsigned AS = getAddrSpace())
1296     OS << ", addrspace " << AS;
1297 
1298   OS << ')';
1299 }
1300