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