xref: /llvm-project/llvm/lib/CodeGen/MachineOperand.cpp (revision b3a0d513749623b8ee51183c127be74350052426)
1 //===- lib/CodeGen/MachineOperand.cpp -------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file Methods common to all machine operands.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/MachineOperand.h"
15 #include "llvm/Analysis/Loads.h"
16 #include "llvm/CodeGen/MIRPrinter.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/CodeGen/TargetInstrInfo.h"
19 #include "llvm/CodeGen/TargetRegisterInfo.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/ModuleSlotTracker.h"
22 #include "llvm/Target/TargetIntrinsicInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 
25 using namespace llvm;
26 
27 static cl::opt<int>
28     PrintRegMaskNumRegs("print-regmask-num-regs",
29                         cl::desc("Number of registers to limit to when "
30                                  "printing regmask operands in IR dumps. "
31                                  "unlimited = -1"),
32                         cl::init(32), cl::Hidden);
33 
34 static const MachineFunction *getMFIfAvailable(const MachineOperand &MO) {
35   if (const MachineInstr *MI = MO.getParent())
36     if (const MachineBasicBlock *MBB = MI->getParent())
37       if (const MachineFunction *MF = MBB->getParent())
38         return MF;
39   return nullptr;
40 }
41 static MachineFunction *getMFIfAvailable(MachineOperand &MO) {
42   return const_cast<MachineFunction *>(
43       getMFIfAvailable(const_cast<const MachineOperand &>(MO)));
44 }
45 
46 void MachineOperand::setReg(unsigned Reg) {
47   if (getReg() == Reg)
48     return; // No change.
49 
50   // Otherwise, we have to change the register.  If this operand is embedded
51   // into a machine function, we need to update the old and new register's
52   // use/def lists.
53   if (MachineFunction *MF = getMFIfAvailable(*this)) {
54     MachineRegisterInfo &MRI = MF->getRegInfo();
55     MRI.removeRegOperandFromUseList(this);
56     SmallContents.RegNo = Reg;
57     MRI.addRegOperandToUseList(this);
58     return;
59   }
60 
61   // Otherwise, just change the register, no problem.  :)
62   SmallContents.RegNo = Reg;
63 }
64 
65 void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx,
66                                   const TargetRegisterInfo &TRI) {
67   assert(TargetRegisterInfo::isVirtualRegister(Reg));
68   if (SubIdx && getSubReg())
69     SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg());
70   setReg(Reg);
71   if (SubIdx)
72     setSubReg(SubIdx);
73 }
74 
75 void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) {
76   assert(TargetRegisterInfo::isPhysicalRegister(Reg));
77   if (getSubReg()) {
78     Reg = TRI.getSubReg(Reg, getSubReg());
79     // Note that getSubReg() may return 0 if the sub-register doesn't exist.
80     // That won't happen in legal code.
81     setSubReg(0);
82     if (isDef())
83       setIsUndef(false);
84   }
85   setReg(Reg);
86 }
87 
88 /// Change a def to a use, or a use to a def.
89 void MachineOperand::setIsDef(bool Val) {
90   assert(isReg() && "Wrong MachineOperand accessor");
91   assert((!Val || !isDebug()) && "Marking a debug operation as def");
92   if (IsDef == Val)
93     return;
94   assert(!IsDeadOrKill && "Changing def/use with dead/kill set not supported");
95   // MRI may keep uses and defs in different list positions.
96   if (MachineFunction *MF = getMFIfAvailable(*this)) {
97     MachineRegisterInfo &MRI = MF->getRegInfo();
98     MRI.removeRegOperandFromUseList(this);
99     IsDef = Val;
100     MRI.addRegOperandToUseList(this);
101     return;
102   }
103   IsDef = Val;
104 }
105 
106 bool MachineOperand::isRenamable() const {
107   assert(isReg() && "Wrong MachineOperand accessor");
108   assert(TargetRegisterInfo::isPhysicalRegister(getReg()) &&
109          "isRenamable should only be checked on physical registers");
110   return IsRenamable;
111 }
112 
113 void MachineOperand::setIsRenamable(bool Val) {
114   assert(isReg() && "Wrong MachineOperand accessor");
115   assert(TargetRegisterInfo::isPhysicalRegister(getReg()) &&
116          "setIsRenamable should only be called on physical registers");
117   if (const MachineInstr *MI = getParent())
118     if ((isDef() && MI->hasExtraDefRegAllocReq()) ||
119         (isUse() && MI->hasExtraSrcRegAllocReq()))
120       assert(!Val && "isRenamable should be false for "
121                      "hasExtraDefRegAllocReq/hasExtraSrcRegAllocReq opcodes");
122   IsRenamable = Val;
123 }
124 
125 void MachineOperand::setIsRenamableIfNoExtraRegAllocReq() {
126   if (const MachineInstr *MI = getParent())
127     if ((isDef() && MI->hasExtraDefRegAllocReq()) ||
128         (isUse() && MI->hasExtraSrcRegAllocReq()))
129       return;
130 
131   setIsRenamable(true);
132 }
133 
134 // If this operand is currently a register operand, and if this is in a
135 // function, deregister the operand from the register's use/def list.
136 void MachineOperand::removeRegFromUses() {
137   if (!isReg() || !isOnRegUseList())
138     return;
139 
140   if (MachineFunction *MF = getMFIfAvailable(*this))
141     MF->getRegInfo().removeRegOperandFromUseList(this);
142 }
143 
144 /// ChangeToImmediate - Replace this operand with a new immediate operand of
145 /// the specified value.  If an operand is known to be an immediate already,
146 /// the setImm method should be used.
147 void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
148   assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
149 
150   removeRegFromUses();
151 
152   OpKind = MO_Immediate;
153   Contents.ImmVal = ImmVal;
154 }
155 
156 void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm) {
157   assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
158 
159   removeRegFromUses();
160 
161   OpKind = MO_FPImmediate;
162   Contents.CFP = FPImm;
163 }
164 
165 void MachineOperand::ChangeToES(const char *SymName,
166                                 unsigned char TargetFlags) {
167   assert((!isReg() || !isTied()) &&
168          "Cannot change a tied operand into an external symbol");
169 
170   removeRegFromUses();
171 
172   OpKind = MO_ExternalSymbol;
173   Contents.OffsetedInfo.Val.SymbolName = SymName;
174   setOffset(0); // Offset is always 0.
175   setTargetFlags(TargetFlags);
176 }
177 
178 void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym) {
179   assert((!isReg() || !isTied()) &&
180          "Cannot change a tied operand into an MCSymbol");
181 
182   removeRegFromUses();
183 
184   OpKind = MO_MCSymbol;
185   Contents.Sym = Sym;
186 }
187 
188 void MachineOperand::ChangeToFrameIndex(int Idx) {
189   assert((!isReg() || !isTied()) &&
190          "Cannot change a tied operand into a FrameIndex");
191 
192   removeRegFromUses();
193 
194   OpKind = MO_FrameIndex;
195   setIndex(Idx);
196 }
197 
198 void MachineOperand::ChangeToTargetIndex(unsigned Idx, int64_t Offset,
199                                          unsigned char TargetFlags) {
200   assert((!isReg() || !isTied()) &&
201          "Cannot change a tied operand into a FrameIndex");
202 
203   removeRegFromUses();
204 
205   OpKind = MO_TargetIndex;
206   setIndex(Idx);
207   setOffset(Offset);
208   setTargetFlags(TargetFlags);
209 }
210 
211 /// ChangeToRegister - Replace this operand with a new register operand of
212 /// the specified value.  If an operand is known to be an register already,
213 /// the setReg method should be used.
214 void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
215                                       bool isKill, bool isDead, bool isUndef,
216                                       bool isDebug) {
217   MachineRegisterInfo *RegInfo = nullptr;
218   if (MachineFunction *MF = getMFIfAvailable(*this))
219     RegInfo = &MF->getRegInfo();
220   // If this operand is already a register operand, remove it from the
221   // register's use/def lists.
222   bool WasReg = isReg();
223   if (RegInfo && WasReg)
224     RegInfo->removeRegOperandFromUseList(this);
225 
226   // Change this to a register and set the reg#.
227   assert(!(isDead && !isDef) && "Dead flag on non-def");
228   assert(!(isKill && isDef) && "Kill flag on def");
229   OpKind = MO_Register;
230   SmallContents.RegNo = Reg;
231   SubReg_TargetFlags = 0;
232   IsDef = isDef;
233   IsImp = isImp;
234   IsDeadOrKill = isKill | isDead;
235   IsRenamable = false;
236   IsUndef = isUndef;
237   IsInternalRead = false;
238   IsEarlyClobber = false;
239   IsDebug = isDebug;
240   // Ensure isOnRegUseList() returns false.
241   Contents.Reg.Prev = nullptr;
242   // Preserve the tie when the operand was already a register.
243   if (!WasReg)
244     TiedTo = 0;
245 
246   // If this operand is embedded in a function, add the operand to the
247   // register's use/def list.
248   if (RegInfo)
249     RegInfo->addRegOperandToUseList(this);
250 }
251 
252 /// isIdenticalTo - Return true if this operand is identical to the specified
253 /// operand. Note that this should stay in sync with the hash_value overload
254 /// below.
255 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
256   if (getType() != Other.getType() ||
257       getTargetFlags() != Other.getTargetFlags())
258     return false;
259 
260   switch (getType()) {
261   case MachineOperand::MO_Register:
262     return getReg() == Other.getReg() && isDef() == Other.isDef() &&
263            getSubReg() == Other.getSubReg();
264   case MachineOperand::MO_Immediate:
265     return getImm() == Other.getImm();
266   case MachineOperand::MO_CImmediate:
267     return getCImm() == Other.getCImm();
268   case MachineOperand::MO_FPImmediate:
269     return getFPImm() == Other.getFPImm();
270   case MachineOperand::MO_MachineBasicBlock:
271     return getMBB() == Other.getMBB();
272   case MachineOperand::MO_FrameIndex:
273     return getIndex() == Other.getIndex();
274   case MachineOperand::MO_ConstantPoolIndex:
275   case MachineOperand::MO_TargetIndex:
276     return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
277   case MachineOperand::MO_JumpTableIndex:
278     return getIndex() == Other.getIndex();
279   case MachineOperand::MO_GlobalAddress:
280     return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
281   case MachineOperand::MO_ExternalSymbol:
282     return strcmp(getSymbolName(), Other.getSymbolName()) == 0 &&
283            getOffset() == Other.getOffset();
284   case MachineOperand::MO_BlockAddress:
285     return getBlockAddress() == Other.getBlockAddress() &&
286            getOffset() == Other.getOffset();
287   case MachineOperand::MO_RegisterMask:
288   case MachineOperand::MO_RegisterLiveOut: {
289     // Shallow compare of the two RegMasks
290     const uint32_t *RegMask = getRegMask();
291     const uint32_t *OtherRegMask = Other.getRegMask();
292     if (RegMask == OtherRegMask)
293       return true;
294 
295     if (const MachineFunction *MF = getMFIfAvailable(*this)) {
296       // Calculate the size of the RegMask
297       const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
298       unsigned RegMaskSize = (TRI->getNumRegs() + 31) / 32;
299 
300       // Deep compare of the two RegMasks
301       return std::equal(RegMask, RegMask + RegMaskSize, OtherRegMask);
302     }
303     // We don't know the size of the RegMask, so we can't deep compare the two
304     // reg masks.
305     return false;
306   }
307   case MachineOperand::MO_MCSymbol:
308     return getMCSymbol() == Other.getMCSymbol();
309   case MachineOperand::MO_CFIIndex:
310     return getCFIIndex() == Other.getCFIIndex();
311   case MachineOperand::MO_Metadata:
312     return getMetadata() == Other.getMetadata();
313   case MachineOperand::MO_IntrinsicID:
314     return getIntrinsicID() == Other.getIntrinsicID();
315   case MachineOperand::MO_Predicate:
316     return getPredicate() == Other.getPredicate();
317   }
318   llvm_unreachable("Invalid machine operand type");
319 }
320 
321 // Note: this must stay exactly in sync with isIdenticalTo above.
322 hash_code llvm::hash_value(const MachineOperand &MO) {
323   switch (MO.getType()) {
324   case MachineOperand::MO_Register:
325     // Register operands don't have target flags.
326     return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef());
327   case MachineOperand::MO_Immediate:
328     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm());
329   case MachineOperand::MO_CImmediate:
330     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm());
331   case MachineOperand::MO_FPImmediate:
332     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm());
333   case MachineOperand::MO_MachineBasicBlock:
334     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB());
335   case MachineOperand::MO_FrameIndex:
336     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
337   case MachineOperand::MO_ConstantPoolIndex:
338   case MachineOperand::MO_TargetIndex:
339     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(),
340                         MO.getOffset());
341   case MachineOperand::MO_JumpTableIndex:
342     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
343   case MachineOperand::MO_ExternalSymbol:
344     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(),
345                         MO.getSymbolName());
346   case MachineOperand::MO_GlobalAddress:
347     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(),
348                         MO.getOffset());
349   case MachineOperand::MO_BlockAddress:
350     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getBlockAddress(),
351                         MO.getOffset());
352   case MachineOperand::MO_RegisterMask:
353   case MachineOperand::MO_RegisterLiveOut:
354     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask());
355   case MachineOperand::MO_Metadata:
356     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata());
357   case MachineOperand::MO_MCSymbol:
358     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol());
359   case MachineOperand::MO_CFIIndex:
360     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex());
361   case MachineOperand::MO_IntrinsicID:
362     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIntrinsicID());
363   case MachineOperand::MO_Predicate:
364     return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getPredicate());
365   }
366   llvm_unreachable("Invalid machine operand type");
367 }
368 
369 // Try to crawl up to the machine function and get TRI and IntrinsicInfo from
370 // it.
371 static void tryToGetTargetInfo(const MachineOperand &MO,
372                                const TargetRegisterInfo *&TRI,
373                                const TargetIntrinsicInfo *&IntrinsicInfo) {
374   if (const MachineFunction *MF = getMFIfAvailable(MO)) {
375     TRI = MF->getSubtarget().getRegisterInfo();
376     IntrinsicInfo = MF->getTarget().getIntrinsicInfo();
377   }
378 }
379 
380 static void printOffset(raw_ostream &OS, int64_t Offset) {
381   if (Offset == 0)
382     return;
383   if (Offset < 0) {
384     OS << " - " << -Offset;
385     return;
386   }
387   OS << " + " << Offset;
388 }
389 
390 static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
391   const auto *TII = MF.getSubtarget().getInstrInfo();
392   assert(TII && "expected instruction info");
393   auto Indices = TII->getSerializableTargetIndices();
394   auto Found = find_if(Indices, [&](const std::pair<int, const char *> &I) {
395     return I.first == Index;
396   });
397   if (Found != Indices.end())
398     return Found->second;
399   return nullptr;
400 }
401 
402 void MachineOperand::printSubregIdx(raw_ostream &OS, uint64_t Index,
403                                     const TargetRegisterInfo *TRI) {
404   OS << "%subreg.";
405   if (TRI)
406     OS << TRI->getSubRegIndexName(Index);
407   else
408     OS << Index;
409 }
410 
411 void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI,
412                            const TargetIntrinsicInfo *IntrinsicInfo) const {
413   tryToGetTargetInfo(*this, TRI, IntrinsicInfo);
414   ModuleSlotTracker DummyMST(nullptr);
415   print(OS, DummyMST, LLT{}, /*PrintDef=*/false,
416         /*ShouldPrintRegisterTies=*/true,
417         /*TiedOperandIdx=*/0, TRI, IntrinsicInfo);
418 }
419 
420 void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
421                            LLT TypeToPrint, bool PrintDef,
422                            bool ShouldPrintRegisterTies,
423                            unsigned TiedOperandIdx,
424                            const TargetRegisterInfo *TRI,
425                            const TargetIntrinsicInfo *IntrinsicInfo) const {
426   switch (getType()) {
427   case MachineOperand::MO_Register: {
428     unsigned Reg = getReg();
429     if (isImplicit())
430       OS << (isDef() ? "implicit-def " : "implicit ");
431     else if (PrintDef && isDef())
432       // Print the 'def' flag only when the operand is defined after '='.
433       OS << "def ";
434     if (isInternalRead())
435       OS << "internal ";
436     if (isDead())
437       OS << "dead ";
438     if (isKill())
439       OS << "killed ";
440     if (isUndef())
441       OS << "undef ";
442     if (isEarlyClobber())
443       OS << "early-clobber ";
444     if (isDebug())
445       OS << "debug-use ";
446     if (TargetRegisterInfo::isPhysicalRegister(getReg()) && isRenamable())
447       OS << "renamable ";
448     OS << printReg(Reg, TRI);
449     // Print the sub register.
450     if (unsigned SubReg = getSubReg()) {
451       if (TRI)
452         OS << '.' << TRI->getSubRegIndexName(SubReg);
453       else
454         OS << ".subreg" << SubReg;
455     }
456     // Print the register class / bank.
457     if (TargetRegisterInfo::isVirtualRegister(Reg)) {
458       if (const MachineFunction *MF = getMFIfAvailable(*this)) {
459         const MachineRegisterInfo &MRI = MF->getRegInfo();
460         if (!PrintDef || MRI.def_empty(Reg)) {
461           OS << ':';
462           OS << printRegClassOrBank(Reg, MRI, TRI);
463         }
464       }
465     }
466     // Print ties.
467     if (ShouldPrintRegisterTies && isTied() && !isDef())
468       OS << "(tied-def " << TiedOperandIdx << ")";
469     // Print types.
470     if (TypeToPrint.isValid())
471       OS << '(' << TypeToPrint << ')';
472     break;
473   }
474   case MachineOperand::MO_Immediate:
475     OS << getImm();
476     break;
477   case MachineOperand::MO_CImmediate:
478     getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
479     break;
480   case MachineOperand::MO_FPImmediate:
481     if (getFPImm()->getType()->isFloatTy()) {
482       OS << getFPImm()->getValueAPF().convertToFloat();
483     } else if (getFPImm()->getType()->isHalfTy()) {
484       APFloat APF = getFPImm()->getValueAPF();
485       bool Unused;
486       APF.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &Unused);
487       OS << "half " << APF.convertToFloat();
488     } else if (getFPImm()->getType()->isFP128Ty()) {
489       APFloat APF = getFPImm()->getValueAPF();
490       SmallString<16> Str;
491       getFPImm()->getValueAPF().toString(Str);
492       OS << "quad " << Str;
493     } else if (getFPImm()->getType()->isX86_FP80Ty()) {
494       APFloat APF = getFPImm()->getValueAPF();
495       OS << "x86_fp80 0xK";
496       APInt API = APF.bitcastToAPInt();
497       OS << format_hex_no_prefix(API.getHiBits(16).getZExtValue(), 4,
498                                  /*Upper=*/true);
499       OS << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
500                                  /*Upper=*/true);
501     } else {
502       OS << getFPImm()->getValueAPF().convertToDouble();
503     }
504     break;
505   case MachineOperand::MO_MachineBasicBlock:
506     OS << printMBBReference(*getMBB());
507     break;
508   case MachineOperand::MO_FrameIndex:
509     OS << "<fi#" << getIndex() << '>';
510     break;
511   case MachineOperand::MO_ConstantPoolIndex:
512     OS << "%const." << getIndex();
513     printOffset(OS, getOffset());
514     break;
515   case MachineOperand::MO_TargetIndex: {
516     OS << "target-index(";
517     const char *Name = "<unknown>";
518     if (const MachineFunction *MF = getMFIfAvailable(*this))
519       if (const auto *TargetIndexName = getTargetIndexName(*MF, getIndex()))
520         Name = TargetIndexName;
521     OS << Name << ')';
522     printOffset(OS, getOffset());
523     break;
524   }
525   case MachineOperand::MO_JumpTableIndex:
526     OS << "<jt#" << getIndex() << '>';
527     break;
528   case MachineOperand::MO_GlobalAddress:
529     OS << "<ga:";
530     getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
531     if (getOffset())
532       OS << "+" << getOffset();
533     OS << '>';
534     break;
535   case MachineOperand::MO_ExternalSymbol:
536     OS << "<es:" << getSymbolName();
537     if (getOffset())
538       OS << "+" << getOffset();
539     OS << '>';
540     break;
541   case MachineOperand::MO_BlockAddress:
542     OS << '<';
543     getBlockAddress()->printAsOperand(OS, /*PrintType=*/false, MST);
544     if (getOffset())
545       OS << "+" << getOffset();
546     OS << '>';
547     break;
548   case MachineOperand::MO_RegisterMask: {
549     OS << "<regmask";
550     if (TRI) {
551       unsigned NumRegsInMask = 0;
552       unsigned NumRegsEmitted = 0;
553       for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {
554         unsigned MaskWord = i / 32;
555         unsigned MaskBit = i % 32;
556         if (getRegMask()[MaskWord] & (1 << MaskBit)) {
557           if (PrintRegMaskNumRegs < 0 ||
558               NumRegsEmitted <= static_cast<unsigned>(PrintRegMaskNumRegs)) {
559             OS << " " << printReg(i, TRI);
560             NumRegsEmitted++;
561           }
562           NumRegsInMask++;
563         }
564       }
565       if (NumRegsEmitted != NumRegsInMask)
566         OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more...";
567     } else {
568       OS << " ...";
569     }
570     OS << ">";
571     break;
572   }
573   case MachineOperand::MO_RegisterLiveOut:
574     OS << "<regliveout>";
575     break;
576   case MachineOperand::MO_Metadata:
577     OS << '<';
578     getMetadata()->printAsOperand(OS, MST);
579     OS << '>';
580     break;
581   case MachineOperand::MO_MCSymbol:
582     OS << "<MCSym=" << *getMCSymbol() << '>';
583     break;
584   case MachineOperand::MO_CFIIndex:
585     OS << "<call frame instruction>";
586     break;
587   case MachineOperand::MO_IntrinsicID: {
588     Intrinsic::ID ID = getIntrinsicID();
589     if (ID < Intrinsic::num_intrinsics)
590       OS << "<intrinsic:@" << Intrinsic::getName(ID, None) << '>';
591     else if (IntrinsicInfo)
592       OS << "<intrinsic:@" << IntrinsicInfo->getName(ID) << '>';
593     else
594       OS << "<intrinsic:" << ID << '>';
595     break;
596   }
597   case MachineOperand::MO_Predicate: {
598     auto Pred = static_cast<CmpInst::Predicate>(getPredicate());
599     OS << '<' << (CmpInst::isIntPredicate(Pred) ? "intpred" : "floatpred")
600        << CmpInst::getPredicateName(Pred) << '>';
601     break;
602   }
603   }
604   if (unsigned TF = getTargetFlags())
605     OS << "[TF=" << TF << ']';
606 }
607 
608 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
609 LLVM_DUMP_METHOD void MachineOperand::dump() const { dbgs() << *this << '\n'; }
610 #endif
611 
612 //===----------------------------------------------------------------------===//
613 // MachineMemOperand Implementation
614 //===----------------------------------------------------------------------===//
615 
616 /// getAddrSpace - Return the LLVM IR address space number that this pointer
617 /// points into.
618 unsigned MachinePointerInfo::getAddrSpace() const { return AddrSpace; }
619 
620 /// isDereferenceable - Return true if V is always dereferenceable for
621 /// Offset + Size byte.
622 bool MachinePointerInfo::isDereferenceable(unsigned Size, LLVMContext &C,
623                                            const DataLayout &DL) const {
624   if (!V.is<const Value *>())
625     return false;
626 
627   const Value *BasePtr = V.get<const Value *>();
628   if (BasePtr == nullptr)
629     return false;
630 
631   return isDereferenceableAndAlignedPointer(
632       BasePtr, 1, APInt(DL.getPointerSizeInBits(), Offset + Size), DL);
633 }
634 
635 /// getConstantPool - Return a MachinePointerInfo record that refers to the
636 /// constant pool.
637 MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) {
638   return MachinePointerInfo(MF.getPSVManager().getConstantPool());
639 }
640 
641 /// getFixedStack - Return a MachinePointerInfo record that refers to the
642 /// the specified FrameIndex.
643 MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF,
644                                                      int FI, int64_t Offset) {
645   return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset);
646 }
647 
648 MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) {
649   return MachinePointerInfo(MF.getPSVManager().getJumpTable());
650 }
651 
652 MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) {
653   return MachinePointerInfo(MF.getPSVManager().getGOT());
654 }
655 
656 MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF,
657                                                 int64_t Offset, uint8_t ID) {
658   return MachinePointerInfo(MF.getPSVManager().getStack(), Offset, ID);
659 }
660 
661 MachinePointerInfo MachinePointerInfo::getUnknownStack(MachineFunction &MF) {
662   return MachinePointerInfo(MF.getDataLayout().getAllocaAddrSpace());
663 }
664 
665 MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f,
666                                      uint64_t s, unsigned int a,
667                                      const AAMDNodes &AAInfo,
668                                      const MDNode *Ranges, SyncScope::ID SSID,
669                                      AtomicOrdering Ordering,
670                                      AtomicOrdering FailureOrdering)
671     : PtrInfo(ptrinfo), Size(s), FlagVals(f), BaseAlignLog2(Log2_32(a) + 1),
672       AAInfo(AAInfo), Ranges(Ranges) {
673   assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue *>() ||
674           isa<PointerType>(PtrInfo.V.get<const Value *>()->getType())) &&
675          "invalid pointer value");
676   assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
677   assert((isLoad() || isStore()) && "Not a load/store!");
678 
679   AtomicInfo.SSID = static_cast<unsigned>(SSID);
680   assert(getSyncScopeID() == SSID && "Value truncated");
681   AtomicInfo.Ordering = static_cast<unsigned>(Ordering);
682   assert(getOrdering() == Ordering && "Value truncated");
683   AtomicInfo.FailureOrdering = static_cast<unsigned>(FailureOrdering);
684   assert(getFailureOrdering() == FailureOrdering && "Value truncated");
685 }
686 
687 /// Profile - Gather unique data for the object.
688 ///
689 void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
690   ID.AddInteger(getOffset());
691   ID.AddInteger(Size);
692   ID.AddPointer(getOpaqueValue());
693   ID.AddInteger(getFlags());
694   ID.AddInteger(getBaseAlignment());
695 }
696 
697 void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
698   // The Value and Offset may differ due to CSE. But the flags and size
699   // should be the same.
700   assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
701   assert(MMO->getSize() == getSize() && "Size mismatch!");
702 
703   if (MMO->getBaseAlignment() >= getBaseAlignment()) {
704     // Update the alignment value.
705     BaseAlignLog2 = Log2_32(MMO->getBaseAlignment()) + 1;
706     // Also update the base and offset, because the new alignment may
707     // not be applicable with the old ones.
708     PtrInfo = MMO->PtrInfo;
709   }
710 }
711 
712 /// getAlignment - Return the minimum known alignment in bytes of the
713 /// actual memory reference.
714 uint64_t MachineMemOperand::getAlignment() const {
715   return MinAlign(getBaseAlignment(), getOffset());
716 }
717 
718 void MachineMemOperand::print(raw_ostream &OS) const {
719   ModuleSlotTracker DummyMST(nullptr);
720   print(OS, DummyMST);
721 }
722 void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST) const {
723   assert((isLoad() || isStore()) && "SV has to be a load, store or both.");
724 
725   if (isVolatile())
726     OS << "Volatile ";
727 
728   if (isLoad())
729     OS << "LD";
730   if (isStore())
731     OS << "ST";
732   OS << getSize();
733 
734   // Print the address information.
735   OS << "[";
736   if (const Value *V = getValue())
737     V->printAsOperand(OS, /*PrintType=*/false, MST);
738   else if (const PseudoSourceValue *PSV = getPseudoValue())
739     PSV->printCustom(OS);
740   else
741     OS << "<unknown>";
742 
743   unsigned AS = getAddrSpace();
744   if (AS != 0)
745     OS << "(addrspace=" << AS << ')';
746 
747   // If the alignment of the memory reference itself differs from the alignment
748   // of the base pointer, print the base alignment explicitly, next to the base
749   // pointer.
750   if (getBaseAlignment() != getAlignment())
751     OS << "(align=" << getBaseAlignment() << ")";
752 
753   if (getOffset() != 0)
754     OS << "+" << getOffset();
755   OS << "]";
756 
757   // Print the alignment of the reference.
758   if (getBaseAlignment() != getAlignment() || getBaseAlignment() != getSize())
759     OS << "(align=" << getAlignment() << ")";
760 
761   // Print TBAA info.
762   if (const MDNode *TBAAInfo = getAAInfo().TBAA) {
763     OS << "(tbaa=";
764     if (TBAAInfo->getNumOperands() > 0)
765       TBAAInfo->getOperand(0)->printAsOperand(OS, MST);
766     else
767       OS << "<unknown>";
768     OS << ")";
769   }
770 
771   // Print AA scope info.
772   if (const MDNode *ScopeInfo = getAAInfo().Scope) {
773     OS << "(alias.scope=";
774     if (ScopeInfo->getNumOperands() > 0)
775       for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) {
776         ScopeInfo->getOperand(i)->printAsOperand(OS, MST);
777         if (i != ie - 1)
778           OS << ",";
779       }
780     else
781       OS << "<unknown>";
782     OS << ")";
783   }
784 
785   // Print AA noalias scope info.
786   if (const MDNode *NoAliasInfo = getAAInfo().NoAlias) {
787     OS << "(noalias=";
788     if (NoAliasInfo->getNumOperands() > 0)
789       for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) {
790         NoAliasInfo->getOperand(i)->printAsOperand(OS, MST);
791         if (i != ie - 1)
792           OS << ",";
793       }
794     else
795       OS << "<unknown>";
796     OS << ")";
797   }
798 
799   if (const MDNode *Ranges = getRanges()) {
800     unsigned NumRanges = Ranges->getNumOperands();
801     if (NumRanges != 0) {
802       OS << "(ranges=";
803 
804       for (unsigned I = 0; I != NumRanges; ++I) {
805         Ranges->getOperand(I)->printAsOperand(OS, MST);
806         if (I != NumRanges - 1)
807           OS << ',';
808       }
809 
810       OS << ')';
811     }
812   }
813 
814   if (isNonTemporal())
815     OS << "(nontemporal)";
816   if (isDereferenceable())
817     OS << "(dereferenceable)";
818   if (isInvariant())
819     OS << "(invariant)";
820   if (getFlags() & MOTargetFlag1)
821     OS << "(flag1)";
822   if (getFlags() & MOTargetFlag2)
823     OS << "(flag2)";
824   if (getFlags() & MOTargetFlag3)
825     OS << "(flag3)";
826 }
827