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