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