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