xref: /llvm-project/llvm/lib/CodeGen/MachineOperand.cpp (revision a8a83d150f489733353b3bff0891248f59a0794c)
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 MachineInstr *MI = MO.getParent()) {
343     if (const MachineBasicBlock *MBB = MI->getParent()) {
344       if (const MachineFunction *MF = MBB->getParent()) {
345         TRI = MF->getSubtarget().getRegisterInfo();
346         IntrinsicInfo = MF->getTarget().getIntrinsicInfo();
347       }
348     }
349   }
350 }
351 
352 void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI,
353                            const TargetIntrinsicInfo *IntrinsicInfo) const {
354   tryToGetTargetInfo(*this, TRI, IntrinsicInfo);
355   ModuleSlotTracker DummyMST(nullptr);
356   print(OS, DummyMST, LLT{}, /*PrintDef=*/false,
357         /*ShouldPrintRegisterTies=*/true,
358         /*TiedOperandIdx=*/0, TRI, IntrinsicInfo);
359 }
360 
361 void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
362                            LLT TypeToPrint, bool PrintDef,
363                            bool ShouldPrintRegisterTies,
364                            unsigned TiedOperandIdx,
365                            const TargetRegisterInfo *TRI,
366                            const TargetIntrinsicInfo *IntrinsicInfo) const {
367   switch (getType()) {
368   case MachineOperand::MO_Register: {
369     unsigned Reg = getReg();
370     if (isImplicit())
371       OS << (isDef() ? "implicit-def " : "implicit ");
372     else if (PrintDef && isDef())
373       // Print the 'def' flag only when the operand is defined after '='.
374       OS << "def ";
375     if (isInternalRead())
376       OS << "internal ";
377     if (isDead())
378       OS << "dead ";
379     if (isKill())
380       OS << "killed ";
381     if (isUndef())
382       OS << "undef ";
383     if (isEarlyClobber())
384       OS << "early-clobber ";
385     if (isDebug())
386       OS << "debug-use ";
387     OS << printReg(Reg, TRI);
388     // Print the sub register.
389     if (unsigned SubReg = getSubReg()) {
390       if (TRI)
391         OS << '.' << TRI->getSubRegIndexName(SubReg);
392       else
393         OS << ".subreg" << SubReg;
394     }
395     // Print the register class / bank.
396     if (TargetRegisterInfo::isVirtualRegister(Reg)) {
397       if (const MachineInstr *MI = getParent()) {
398         if (const MachineBasicBlock *MBB = MI->getParent()) {
399           if (const MachineFunction *MF = MBB->getParent()) {
400             const MachineRegisterInfo &MRI = MF->getRegInfo();
401             if (!PrintDef || MRI.def_empty(Reg)) {
402               OS << ':';
403               OS << printRegClassOrBank(Reg, MRI, TRI);
404             }
405           }
406         }
407       }
408     }
409     // Print ties.
410     if (ShouldPrintRegisterTies && isTied() && !isDef())
411       OS << "(tied-def " << TiedOperandIdx << ")";
412     // Print types.
413     if (TypeToPrint.isValid())
414       OS << '(' << TypeToPrint << ')';
415     break;
416   }
417   case MachineOperand::MO_Immediate:
418     OS << getImm();
419     break;
420   case MachineOperand::MO_CImmediate:
421     getCImm()->getValue().print(OS, false);
422     break;
423   case MachineOperand::MO_FPImmediate:
424     if (getFPImm()->getType()->isFloatTy()) {
425       OS << getFPImm()->getValueAPF().convertToFloat();
426     } else if (getFPImm()->getType()->isHalfTy()) {
427       APFloat APF = getFPImm()->getValueAPF();
428       bool Unused;
429       APF.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &Unused);
430       OS << "half " << APF.convertToFloat();
431     } else if (getFPImm()->getType()->isFP128Ty()) {
432       APFloat APF = getFPImm()->getValueAPF();
433       SmallString<16> Str;
434       getFPImm()->getValueAPF().toString(Str);
435       OS << "quad " << Str;
436     } else if (getFPImm()->getType()->isX86_FP80Ty()) {
437       APFloat APF = getFPImm()->getValueAPF();
438       OS << "x86_fp80 0xK";
439       APInt API = APF.bitcastToAPInt();
440       OS << format_hex_no_prefix(API.getHiBits(16).getZExtValue(), 4,
441                                  /*Upper=*/true);
442       OS << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
443                                  /*Upper=*/true);
444     } else {
445       OS << getFPImm()->getValueAPF().convertToDouble();
446     }
447     break;
448   case MachineOperand::MO_MachineBasicBlock:
449     OS << printMBBReference(*getMBB());
450     break;
451   case MachineOperand::MO_FrameIndex:
452     OS << "<fi#" << getIndex() << '>';
453     break;
454   case MachineOperand::MO_ConstantPoolIndex:
455     OS << "<cp#" << getIndex();
456     if (getOffset())
457       OS << "+" << getOffset();
458     OS << '>';
459     break;
460   case MachineOperand::MO_TargetIndex:
461     OS << "<ti#" << getIndex();
462     if (getOffset())
463       OS << "+" << getOffset();
464     OS << '>';
465     break;
466   case MachineOperand::MO_JumpTableIndex:
467     OS << "<jt#" << getIndex() << '>';
468     break;
469   case MachineOperand::MO_GlobalAddress:
470     OS << "<ga:";
471     getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
472     if (getOffset())
473       OS << "+" << getOffset();
474     OS << '>';
475     break;
476   case MachineOperand::MO_ExternalSymbol:
477     OS << "<es:" << getSymbolName();
478     if (getOffset())
479       OS << "+" << getOffset();
480     OS << '>';
481     break;
482   case MachineOperand::MO_BlockAddress:
483     OS << '<';
484     getBlockAddress()->printAsOperand(OS, /*PrintType=*/false, MST);
485     if (getOffset())
486       OS << "+" << getOffset();
487     OS << '>';
488     break;
489   case MachineOperand::MO_RegisterMask: {
490     OS << "<regmask";
491     if (TRI) {
492       unsigned NumRegsInMask = 0;
493       unsigned NumRegsEmitted = 0;
494       for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {
495         unsigned MaskWord = i / 32;
496         unsigned MaskBit = i % 32;
497         if (getRegMask()[MaskWord] & (1 << MaskBit)) {
498           if (PrintRegMaskNumRegs < 0 ||
499               NumRegsEmitted <= static_cast<unsigned>(PrintRegMaskNumRegs)) {
500             OS << " " << printReg(i, TRI);
501             NumRegsEmitted++;
502           }
503           NumRegsInMask++;
504         }
505       }
506       if (NumRegsEmitted != NumRegsInMask)
507         OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more...";
508     } else {
509       OS << " ...";
510     }
511     OS << ">";
512     break;
513   }
514   case MachineOperand::MO_RegisterLiveOut:
515     OS << "<regliveout>";
516     break;
517   case MachineOperand::MO_Metadata:
518     OS << '<';
519     getMetadata()->printAsOperand(OS, MST);
520     OS << '>';
521     break;
522   case MachineOperand::MO_MCSymbol:
523     OS << "<MCSym=" << *getMCSymbol() << '>';
524     break;
525   case MachineOperand::MO_CFIIndex:
526     OS << "<call frame instruction>";
527     break;
528   case MachineOperand::MO_IntrinsicID: {
529     Intrinsic::ID ID = getIntrinsicID();
530     if (ID < Intrinsic::num_intrinsics)
531       OS << "<intrinsic:@" << Intrinsic::getName(ID, None) << '>';
532     else if (IntrinsicInfo)
533       OS << "<intrinsic:@" << IntrinsicInfo->getName(ID) << '>';
534     else
535       OS << "<intrinsic:" << ID << '>';
536     break;
537   }
538   case MachineOperand::MO_Predicate: {
539     auto Pred = static_cast<CmpInst::Predicate>(getPredicate());
540     OS << '<' << (CmpInst::isIntPredicate(Pred) ? "intpred" : "floatpred")
541        << CmpInst::getPredicateName(Pred) << '>';
542     break;
543   }
544   }
545   if (unsigned TF = getTargetFlags())
546     OS << "[TF=" << TF << ']';
547 }
548 
549 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
550 LLVM_DUMP_METHOD void MachineOperand::dump() const { dbgs() << *this << '\n'; }
551 #endif
552 
553 //===----------------------------------------------------------------------===//
554 // MachineMemOperand Implementation
555 //===----------------------------------------------------------------------===//
556 
557 /// getAddrSpace - Return the LLVM IR address space number that this pointer
558 /// points into.
559 unsigned MachinePointerInfo::getAddrSpace() const { return AddrSpace; }
560 
561 /// isDereferenceable - Return true if V is always dereferenceable for
562 /// Offset + Size byte.
563 bool MachinePointerInfo::isDereferenceable(unsigned Size, LLVMContext &C,
564                                            const DataLayout &DL) const {
565   if (!V.is<const Value *>())
566     return false;
567 
568   const Value *BasePtr = V.get<const Value *>();
569   if (BasePtr == nullptr)
570     return false;
571 
572   return isDereferenceableAndAlignedPointer(
573       BasePtr, 1, APInt(DL.getPointerSizeInBits(), Offset + Size), DL);
574 }
575 
576 /// getConstantPool - Return a MachinePointerInfo record that refers to the
577 /// constant pool.
578 MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) {
579   return MachinePointerInfo(MF.getPSVManager().getConstantPool());
580 }
581 
582 /// getFixedStack - Return a MachinePointerInfo record that refers to the
583 /// the specified FrameIndex.
584 MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF,
585                                                      int FI, int64_t Offset) {
586   return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset);
587 }
588 
589 MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) {
590   return MachinePointerInfo(MF.getPSVManager().getJumpTable());
591 }
592 
593 MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) {
594   return MachinePointerInfo(MF.getPSVManager().getGOT());
595 }
596 
597 MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF,
598                                                 int64_t Offset, uint8_t ID) {
599   return MachinePointerInfo(MF.getPSVManager().getStack(), Offset, ID);
600 }
601 
602 MachinePointerInfo MachinePointerInfo::getUnknownStack(MachineFunction &MF) {
603   return MachinePointerInfo(MF.getDataLayout().getAllocaAddrSpace());
604 }
605 
606 MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f,
607                                      uint64_t s, unsigned int a,
608                                      const AAMDNodes &AAInfo,
609                                      const MDNode *Ranges, SyncScope::ID SSID,
610                                      AtomicOrdering Ordering,
611                                      AtomicOrdering FailureOrdering)
612     : PtrInfo(ptrinfo), Size(s), FlagVals(f), BaseAlignLog2(Log2_32(a) + 1),
613       AAInfo(AAInfo), Ranges(Ranges) {
614   assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue *>() ||
615           isa<PointerType>(PtrInfo.V.get<const Value *>()->getType())) &&
616          "invalid pointer value");
617   assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
618   assert((isLoad() || isStore()) && "Not a load/store!");
619 
620   AtomicInfo.SSID = static_cast<unsigned>(SSID);
621   assert(getSyncScopeID() == SSID && "Value truncated");
622   AtomicInfo.Ordering = static_cast<unsigned>(Ordering);
623   assert(getOrdering() == Ordering && "Value truncated");
624   AtomicInfo.FailureOrdering = static_cast<unsigned>(FailureOrdering);
625   assert(getFailureOrdering() == FailureOrdering && "Value truncated");
626 }
627 
628 /// Profile - Gather unique data for the object.
629 ///
630 void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
631   ID.AddInteger(getOffset());
632   ID.AddInteger(Size);
633   ID.AddPointer(getOpaqueValue());
634   ID.AddInteger(getFlags());
635   ID.AddInteger(getBaseAlignment());
636 }
637 
638 void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
639   // The Value and Offset may differ due to CSE. But the flags and size
640   // should be the same.
641   assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
642   assert(MMO->getSize() == getSize() && "Size mismatch!");
643 
644   if (MMO->getBaseAlignment() >= getBaseAlignment()) {
645     // Update the alignment value.
646     BaseAlignLog2 = Log2_32(MMO->getBaseAlignment()) + 1;
647     // Also update the base and offset, because the new alignment may
648     // not be applicable with the old ones.
649     PtrInfo = MMO->PtrInfo;
650   }
651 }
652 
653 /// getAlignment - Return the minimum known alignment in bytes of the
654 /// actual memory reference.
655 uint64_t MachineMemOperand::getAlignment() const {
656   return MinAlign(getBaseAlignment(), getOffset());
657 }
658 
659 void MachineMemOperand::print(raw_ostream &OS) const {
660   ModuleSlotTracker DummyMST(nullptr);
661   print(OS, DummyMST);
662 }
663 void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST) const {
664   assert((isLoad() || isStore()) && "SV has to be a load, store or both.");
665 
666   if (isVolatile())
667     OS << "Volatile ";
668 
669   if (isLoad())
670     OS << "LD";
671   if (isStore())
672     OS << "ST";
673   OS << getSize();
674 
675   // Print the address information.
676   OS << "[";
677   if (const Value *V = getValue())
678     V->printAsOperand(OS, /*PrintType=*/false, MST);
679   else if (const PseudoSourceValue *PSV = getPseudoValue())
680     PSV->printCustom(OS);
681   else
682     OS << "<unknown>";
683 
684   unsigned AS = getAddrSpace();
685   if (AS != 0)
686     OS << "(addrspace=" << AS << ')';
687 
688   // If the alignment of the memory reference itself differs from the alignment
689   // of the base pointer, print the base alignment explicitly, next to the base
690   // pointer.
691   if (getBaseAlignment() != getAlignment())
692     OS << "(align=" << getBaseAlignment() << ")";
693 
694   if (getOffset() != 0)
695     OS << "+" << getOffset();
696   OS << "]";
697 
698   // Print the alignment of the reference.
699   if (getBaseAlignment() != getAlignment() || getBaseAlignment() != getSize())
700     OS << "(align=" << getAlignment() << ")";
701 
702   // Print TBAA info.
703   if (const MDNode *TBAAInfo = getAAInfo().TBAA) {
704     OS << "(tbaa=";
705     if (TBAAInfo->getNumOperands() > 0)
706       TBAAInfo->getOperand(0)->printAsOperand(OS, MST);
707     else
708       OS << "<unknown>";
709     OS << ")";
710   }
711 
712   // Print AA scope info.
713   if (const MDNode *ScopeInfo = getAAInfo().Scope) {
714     OS << "(alias.scope=";
715     if (ScopeInfo->getNumOperands() > 0)
716       for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) {
717         ScopeInfo->getOperand(i)->printAsOperand(OS, MST);
718         if (i != ie - 1)
719           OS << ",";
720       }
721     else
722       OS << "<unknown>";
723     OS << ")";
724   }
725 
726   // Print AA noalias scope info.
727   if (const MDNode *NoAliasInfo = getAAInfo().NoAlias) {
728     OS << "(noalias=";
729     if (NoAliasInfo->getNumOperands() > 0)
730       for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) {
731         NoAliasInfo->getOperand(i)->printAsOperand(OS, MST);
732         if (i != ie - 1)
733           OS << ",";
734       }
735     else
736       OS << "<unknown>";
737     OS << ")";
738   }
739 
740   if (const MDNode *Ranges = getRanges()) {
741     unsigned NumRanges = Ranges->getNumOperands();
742     if (NumRanges != 0) {
743       OS << "(ranges=";
744 
745       for (unsigned I = 0; I != NumRanges; ++I) {
746         Ranges->getOperand(I)->printAsOperand(OS, MST);
747         if (I != NumRanges - 1)
748           OS << ',';
749       }
750 
751       OS << ')';
752     }
753   }
754 
755   if (isNonTemporal())
756     OS << "(nontemporal)";
757   if (isDereferenceable())
758     OS << "(dereferenceable)";
759   if (isInvariant())
760     OS << "(invariant)";
761   if (getFlags() & MOTargetFlag1)
762     OS << "(flag1)";
763   if (getFlags() & MOTargetFlag2)
764     OS << "(flag2)";
765   if (getFlags() & MOTargetFlag3)
766     OS << "(flag3)";
767 }
768