1 //===- MIRPrinter.cpp - MIR serialization format printer ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the class that prints out the LLVM IR and machine
10 // functions using the MIR serialization format.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/MIRPrinter.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallBitVector.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/CodeGen/MIRYamlMapping.h"
22 #include "llvm/CodeGen/MachineBasicBlock.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineInstr.h"
27 #include "llvm/CodeGen/MachineJumpTableInfo.h"
28 #include "llvm/CodeGen/MachineMemOperand.h"
29 #include "llvm/CodeGen/MachineModuleSlotTracker.h"
30 #include "llvm/CodeGen/MachineOperand.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/CodeGen/TargetFrameLowering.h"
33 #include "llvm/CodeGen/TargetInstrInfo.h"
34 #include "llvm/CodeGen/TargetRegisterInfo.h"
35 #include "llvm/CodeGen/TargetSubtargetInfo.h"
36 #include "llvm/IR/DebugInfoMetadata.h"
37 #include "llvm/IR/DebugLoc.h"
38 #include "llvm/IR/Function.h"
39 #include "llvm/IR/IRPrintingPasses.h"
40 #include "llvm/IR/Instructions.h"
41 #include "llvm/IR/Module.h"
42 #include "llvm/IR/ModuleSlotTracker.h"
43 #include "llvm/IR/Value.h"
44 #include "llvm/MC/LaneBitmask.h"
45 #include "llvm/Support/BranchProbability.h"
46 #include "llvm/Support/Casting.h"
47 #include "llvm/Support/CommandLine.h"
48 #include "llvm/Support/ErrorHandling.h"
49 #include "llvm/Support/Format.h"
50 #include "llvm/Support/LowLevelTypeImpl.h"
51 #include "llvm/Support/YAMLTraits.h"
52 #include "llvm/Support/raw_ostream.h"
53 #include "llvm/Target/TargetMachine.h"
54 #include <algorithm>
55 #include <cassert>
56 #include <cinttypes>
57 #include <cstdint>
58 #include <iterator>
59 #include <string>
60 #include <utility>
61 #include <vector>
62 
63 using namespace llvm;
64 
65 static cl::opt<bool> SimplifyMIR(
66     "simplify-mir", cl::Hidden,
67     cl::desc("Leave out unnecessary information when printing MIR"));
68 
69 static cl::opt<bool> PrintLocations("mir-debug-loc", cl::Hidden, cl::init(true),
70                                     cl::desc("Print MIR debug-locations"));
71 
72 namespace {
73 
74 /// This structure describes how to print out stack object references.
75 struct FrameIndexOperand {
76   std::string Name;
77   unsigned ID;
78   bool IsFixed;
79 
80   FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
81       : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
82 
83   /// Return an ordinary stack object reference.
84   static FrameIndexOperand create(StringRef Name, unsigned ID) {
85     return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
86   }
87 
88   /// Return a fixed stack object reference.
89   static FrameIndexOperand createFixed(unsigned ID) {
90     return FrameIndexOperand("", ID, /*IsFixed=*/true);
91   }
92 };
93 
94 } // end anonymous namespace
95 
96 namespace llvm {
97 
98 /// This class prints out the machine functions using the MIR serialization
99 /// format.
100 class MIRPrinter {
101   raw_ostream &OS;
102   DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
103   /// Maps from stack object indices to operand indices which will be used when
104   /// printing frame index machine operands.
105   DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
106 
107 public:
108   MIRPrinter(raw_ostream &OS) : OS(OS) {}
109 
110   void print(const MachineFunction &MF);
111 
112   void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
113                const TargetRegisterInfo *TRI);
114   void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
115                const MachineFrameInfo &MFI);
116   void convert(yaml::MachineFunction &MF,
117                const MachineConstantPool &ConstantPool);
118   void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
119                const MachineJumpTableInfo &JTI);
120   void convertStackObjects(yaml::MachineFunction &YMF,
121                            const MachineFunction &MF, ModuleSlotTracker &MST);
122   void convertCallSiteObjects(yaml::MachineFunction &YMF,
123                               const MachineFunction &MF,
124                               ModuleSlotTracker &MST);
125   void convertMachineMetadataNodes(yaml::MachineFunction &YMF,
126                                    const MachineFunction &MF,
127                                    MachineModuleSlotTracker &MST);
128 
129 private:
130   void initRegisterMaskIds(const MachineFunction &MF);
131 };
132 
133 /// This class prints out the machine instructions using the MIR serialization
134 /// format.
135 class MIPrinter {
136   raw_ostream &OS;
137   ModuleSlotTracker &MST;
138   const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
139   const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
140   /// Synchronization scope names registered with LLVMContext.
141   SmallVector<StringRef, 8> SSNs;
142 
143   bool canPredictBranchProbabilities(const MachineBasicBlock &MBB) const;
144   bool canPredictSuccessors(const MachineBasicBlock &MBB) const;
145 
146 public:
147   MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
148             const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
149             const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
150       : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
151         StackObjectOperandMapping(StackObjectOperandMapping) {}
152 
153   void print(const MachineBasicBlock &MBB);
154 
155   void print(const MachineInstr &MI);
156   void printStackObjectReference(int FrameIndex);
157   void print(const MachineInstr &MI, unsigned OpIdx,
158              const TargetRegisterInfo *TRI, const TargetInstrInfo *TII,
159              bool ShouldPrintRegisterTies, LLT TypeToPrint,
160              bool PrintDef = true);
161 };
162 
163 } // end namespace llvm
164 
165 namespace llvm {
166 namespace yaml {
167 
168 /// This struct serializes the LLVM IR module.
169 template <> struct BlockScalarTraits<Module> {
170   static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
171     Mod.print(OS, nullptr);
172   }
173 
174   static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
175     llvm_unreachable("LLVM Module is supposed to be parsed separately");
176     return "";
177   }
178 };
179 
180 } // end namespace yaml
181 } // end namespace llvm
182 
183 static void printRegMIR(unsigned Reg, yaml::StringValue &Dest,
184                         const TargetRegisterInfo *TRI) {
185   raw_string_ostream OS(Dest.Value);
186   OS << printReg(Reg, TRI);
187 }
188 
189 void MIRPrinter::print(const MachineFunction &MF) {
190   initRegisterMaskIds(MF);
191 
192   yaml::MachineFunction YamlMF;
193   YamlMF.Name = MF.getName();
194   YamlMF.Alignment = MF.getAlignment();
195   YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
196   YamlMF.HasWinCFI = MF.hasWinCFI();
197 
198   YamlMF.CallsEHReturn = MF.callsEHReturn();
199   YamlMF.CallsUnwindInit = MF.callsUnwindInit();
200   YamlMF.HasEHCatchret = MF.hasEHCatchret();
201   YamlMF.HasEHScopes = MF.hasEHScopes();
202   YamlMF.HasEHFunclets = MF.hasEHFunclets();
203 
204   YamlMF.Legalized = MF.getProperties().hasProperty(
205       MachineFunctionProperties::Property::Legalized);
206   YamlMF.RegBankSelected = MF.getProperties().hasProperty(
207       MachineFunctionProperties::Property::RegBankSelected);
208   YamlMF.Selected = MF.getProperties().hasProperty(
209       MachineFunctionProperties::Property::Selected);
210   YamlMF.FailedISel = MF.getProperties().hasProperty(
211       MachineFunctionProperties::Property::FailedISel);
212   YamlMF.FailsVerification = MF.getProperties().hasProperty(
213       MachineFunctionProperties::Property::FailsVerification);
214   YamlMF.TracksDebugUserValues = MF.getProperties().hasProperty(
215       MachineFunctionProperties::Property::TracksDebugUserValues);
216 
217   convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
218   MachineModuleSlotTracker MST(&MF);
219   MST.incorporateFunction(MF.getFunction());
220   convert(MST, YamlMF.FrameInfo, MF.getFrameInfo());
221   convertStackObjects(YamlMF, MF, MST);
222   convertCallSiteObjects(YamlMF, MF, MST);
223   for (const auto &Sub : MF.DebugValueSubstitutions) {
224     const auto &SubSrc = Sub.Src;
225     const auto &SubDest = Sub.Dest;
226     YamlMF.DebugValueSubstitutions.push_back({SubSrc.first, SubSrc.second,
227                                               SubDest.first,
228                                               SubDest.second,
229                                               Sub.Subreg});
230   }
231   if (const auto *ConstantPool = MF.getConstantPool())
232     convert(YamlMF, *ConstantPool);
233   if (const auto *JumpTableInfo = MF.getJumpTableInfo())
234     convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
235 
236   const TargetMachine &TM = MF.getTarget();
237   YamlMF.MachineFuncInfo =
238       std::unique_ptr<yaml::MachineFunctionInfo>(TM.convertFuncInfoToYAML(MF));
239 
240   raw_string_ostream StrOS(YamlMF.Body.Value.Value);
241   bool IsNewlineNeeded = false;
242   for (const auto &MBB : MF) {
243     if (IsNewlineNeeded)
244       StrOS << "\n";
245     MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
246         .print(MBB);
247     IsNewlineNeeded = true;
248   }
249   StrOS.flush();
250   // Convert machine metadata collected during the print of the machine
251   // function.
252   convertMachineMetadataNodes(YamlMF, MF, MST);
253 
254   yaml::Output Out(OS);
255   if (!SimplifyMIR)
256       Out.setWriteDefaultValues(true);
257   Out << YamlMF;
258 }
259 
260 static void printCustomRegMask(const uint32_t *RegMask, raw_ostream &OS,
261                                const TargetRegisterInfo *TRI) {
262   assert(RegMask && "Can't print an empty register mask");
263   OS << StringRef("CustomRegMask(");
264 
265   bool IsRegInRegMaskFound = false;
266   for (int I = 0, E = TRI->getNumRegs(); I < E; I++) {
267     // Check whether the register is asserted in regmask.
268     if (RegMask[I / 32] & (1u << (I % 32))) {
269       if (IsRegInRegMaskFound)
270         OS << ',';
271       OS << printReg(I, TRI);
272       IsRegInRegMaskFound = true;
273     }
274   }
275 
276   OS << ')';
277 }
278 
279 static void printRegClassOrBank(unsigned Reg, yaml::StringValue &Dest,
280                                 const MachineRegisterInfo &RegInfo,
281                                 const TargetRegisterInfo *TRI) {
282   raw_string_ostream OS(Dest.Value);
283   OS << printRegClassOrBank(Reg, RegInfo, TRI);
284 }
285 
286 template <typename T>
287 static void
288 printStackObjectDbgInfo(const MachineFunction::VariableDbgInfo &DebugVar,
289                         T &Object, ModuleSlotTracker &MST) {
290   std::array<std::string *, 3> Outputs{{&Object.DebugVar.Value,
291                                         &Object.DebugExpr.Value,
292                                         &Object.DebugLoc.Value}};
293   std::array<const Metadata *, 3> Metas{{DebugVar.Var,
294                                         DebugVar.Expr,
295                                         DebugVar.Loc}};
296   for (unsigned i = 0; i < 3; ++i) {
297     raw_string_ostream StrOS(*Outputs[i]);
298     Metas[i]->printAsOperand(StrOS, MST);
299   }
300 }
301 
302 void MIRPrinter::convert(yaml::MachineFunction &MF,
303                          const MachineRegisterInfo &RegInfo,
304                          const TargetRegisterInfo *TRI) {
305   MF.TracksRegLiveness = RegInfo.tracksLiveness();
306 
307   // Print the virtual register definitions.
308   for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
309     unsigned Reg = Register::index2VirtReg(I);
310     yaml::VirtualRegisterDefinition VReg;
311     VReg.ID = I;
312     if (RegInfo.getVRegName(Reg) != "")
313       continue;
314     ::printRegClassOrBank(Reg, VReg.Class, RegInfo, TRI);
315     unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
316     if (PreferredReg)
317       printRegMIR(PreferredReg, VReg.PreferredRegister, TRI);
318     MF.VirtualRegisters.push_back(VReg);
319   }
320 
321   // Print the live ins.
322   for (std::pair<unsigned, unsigned> LI : RegInfo.liveins()) {
323     yaml::MachineFunctionLiveIn LiveIn;
324     printRegMIR(LI.first, LiveIn.Register, TRI);
325     if (LI.second)
326       printRegMIR(LI.second, LiveIn.VirtualRegister, TRI);
327     MF.LiveIns.push_back(LiveIn);
328   }
329 
330   // Prints the callee saved registers.
331   if (RegInfo.isUpdatedCSRsInitialized()) {
332     const MCPhysReg *CalleeSavedRegs = RegInfo.getCalleeSavedRegs();
333     std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
334     for (const MCPhysReg *I = CalleeSavedRegs; *I; ++I) {
335       yaml::FlowStringValue Reg;
336       printRegMIR(*I, Reg, TRI);
337       CalleeSavedRegisters.push_back(Reg);
338     }
339     MF.CalleeSavedRegisters = CalleeSavedRegisters;
340   }
341 }
342 
343 void MIRPrinter::convert(ModuleSlotTracker &MST,
344                          yaml::MachineFrameInfo &YamlMFI,
345                          const MachineFrameInfo &MFI) {
346   YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
347   YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
348   YamlMFI.HasStackMap = MFI.hasStackMap();
349   YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
350   YamlMFI.StackSize = MFI.getStackSize();
351   YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
352   YamlMFI.MaxAlignment = MFI.getMaxAlign().value();
353   YamlMFI.AdjustsStack = MFI.adjustsStack();
354   YamlMFI.HasCalls = MFI.hasCalls();
355   YamlMFI.MaxCallFrameSize = MFI.isMaxCallFrameSizeComputed()
356     ? MFI.getMaxCallFrameSize() : ~0u;
357   YamlMFI.CVBytesOfCalleeSavedRegisters =
358       MFI.getCVBytesOfCalleeSavedRegisters();
359   YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
360   YamlMFI.HasVAStart = MFI.hasVAStart();
361   YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
362   YamlMFI.HasTailCall = MFI.hasTailCall();
363   YamlMFI.LocalFrameSize = MFI.getLocalFrameSize();
364   if (MFI.getSavePoint()) {
365     raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
366     StrOS << printMBBReference(*MFI.getSavePoint());
367   }
368   if (MFI.getRestorePoint()) {
369     raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
370     StrOS << printMBBReference(*MFI.getRestorePoint());
371   }
372 }
373 
374 void MIRPrinter::convertStackObjects(yaml::MachineFunction &YMF,
375                                      const MachineFunction &MF,
376                                      ModuleSlotTracker &MST) {
377   const MachineFrameInfo &MFI = MF.getFrameInfo();
378   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
379 
380   // Process fixed stack objects.
381   assert(YMF.FixedStackObjects.empty());
382   SmallVector<int, 32> FixedStackObjectsIdx;
383   const int BeginIdx = MFI.getObjectIndexBegin();
384   if (BeginIdx < 0)
385     FixedStackObjectsIdx.reserve(-BeginIdx);
386 
387   unsigned ID = 0;
388   for (int I = BeginIdx; I < 0; ++I, ++ID) {
389     FixedStackObjectsIdx.push_back(-1); // Fill index for possible dead.
390     if (MFI.isDeadObjectIndex(I))
391       continue;
392 
393     yaml::FixedMachineStackObject YamlObject;
394     YamlObject.ID = ID;
395     YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
396                           ? yaml::FixedMachineStackObject::SpillSlot
397                           : yaml::FixedMachineStackObject::DefaultType;
398     YamlObject.Offset = MFI.getObjectOffset(I);
399     YamlObject.Size = MFI.getObjectSize(I);
400     YamlObject.Alignment = MFI.getObjectAlign(I);
401     YamlObject.StackID = (TargetStackID::Value)MFI.getStackID(I);
402     YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
403     YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
404     // Save the ID' position in FixedStackObjects storage vector.
405     FixedStackObjectsIdx[ID] = YMF.FixedStackObjects.size();
406     YMF.FixedStackObjects.push_back(YamlObject);
407     StackObjectOperandMapping.insert(
408         std::make_pair(I, FrameIndexOperand::createFixed(ID)));
409   }
410 
411   // Process ordinary stack objects.
412   assert(YMF.StackObjects.empty());
413   SmallVector<unsigned, 32> StackObjectsIdx;
414   const int EndIdx = MFI.getObjectIndexEnd();
415   if (EndIdx > 0)
416     StackObjectsIdx.reserve(EndIdx);
417   ID = 0;
418   for (int I = 0; I < EndIdx; ++I, ++ID) {
419     StackObjectsIdx.push_back(-1); // Fill index for possible dead.
420     if (MFI.isDeadObjectIndex(I))
421       continue;
422 
423     yaml::MachineStackObject YamlObject;
424     YamlObject.ID = ID;
425     if (const auto *Alloca = MFI.getObjectAllocation(I))
426       YamlObject.Name.Value = std::string(
427           Alloca->hasName() ? Alloca->getName() : "");
428     YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
429                           ? yaml::MachineStackObject::SpillSlot
430                           : MFI.isVariableSizedObjectIndex(I)
431                                 ? yaml::MachineStackObject::VariableSized
432                                 : yaml::MachineStackObject::DefaultType;
433     YamlObject.Offset = MFI.getObjectOffset(I);
434     YamlObject.Size = MFI.getObjectSize(I);
435     YamlObject.Alignment = MFI.getObjectAlign(I);
436     YamlObject.StackID = (TargetStackID::Value)MFI.getStackID(I);
437 
438     // Save the ID' position in StackObjects storage vector.
439     StackObjectsIdx[ID] = YMF.StackObjects.size();
440     YMF.StackObjects.push_back(YamlObject);
441     StackObjectOperandMapping.insert(std::make_pair(
442         I, FrameIndexOperand::create(YamlObject.Name.Value, ID)));
443   }
444 
445   for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
446     const int FrameIdx = CSInfo.getFrameIdx();
447     if (!CSInfo.isSpilledToReg() && MFI.isDeadObjectIndex(FrameIdx))
448       continue;
449 
450     yaml::StringValue Reg;
451     printRegMIR(CSInfo.getReg(), Reg, TRI);
452     if (!CSInfo.isSpilledToReg()) {
453       assert(FrameIdx >= MFI.getObjectIndexBegin() &&
454              FrameIdx < MFI.getObjectIndexEnd() &&
455              "Invalid stack object index");
456       if (FrameIdx < 0) { // Negative index means fixed objects.
457         auto &Object =
458             YMF.FixedStackObjects
459                 [FixedStackObjectsIdx[FrameIdx + MFI.getNumFixedObjects()]];
460         Object.CalleeSavedRegister = Reg;
461         Object.CalleeSavedRestored = CSInfo.isRestored();
462       } else {
463         auto &Object = YMF.StackObjects[StackObjectsIdx[FrameIdx]];
464         Object.CalleeSavedRegister = Reg;
465         Object.CalleeSavedRestored = CSInfo.isRestored();
466       }
467     }
468   }
469   for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
470     auto LocalObject = MFI.getLocalFrameObjectMap(I);
471     assert(LocalObject.first >= 0 && "Expected a locally mapped stack object");
472     YMF.StackObjects[StackObjectsIdx[LocalObject.first]].LocalOffset =
473         LocalObject.second;
474   }
475 
476   // Print the stack object references in the frame information class after
477   // converting the stack objects.
478   if (MFI.hasStackProtectorIndex()) {
479     raw_string_ostream StrOS(YMF.FrameInfo.StackProtector.Value);
480     MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
481         .printStackObjectReference(MFI.getStackProtectorIndex());
482   }
483 
484   // Print the debug variable information.
485   for (const MachineFunction::VariableDbgInfo &DebugVar :
486        MF.getVariableDbgInfo()) {
487     assert(DebugVar.Slot >= MFI.getObjectIndexBegin() &&
488            DebugVar.Slot < MFI.getObjectIndexEnd() &&
489            "Invalid stack object index");
490     if (DebugVar.Slot < 0) { // Negative index means fixed objects.
491       auto &Object =
492           YMF.FixedStackObjects[FixedStackObjectsIdx[DebugVar.Slot +
493                                                      MFI.getNumFixedObjects()]];
494       printStackObjectDbgInfo(DebugVar, Object, MST);
495     } else {
496       auto &Object = YMF.StackObjects[StackObjectsIdx[DebugVar.Slot]];
497       printStackObjectDbgInfo(DebugVar, Object, MST);
498     }
499   }
500 }
501 
502 void MIRPrinter::convertCallSiteObjects(yaml::MachineFunction &YMF,
503                                         const MachineFunction &MF,
504                                         ModuleSlotTracker &MST) {
505   const auto *TRI = MF.getSubtarget().getRegisterInfo();
506   for (auto CSInfo : MF.getCallSitesInfo()) {
507     yaml::CallSiteInfo YmlCS;
508     yaml::CallSiteInfo::MachineInstrLoc CallLocation;
509 
510     // Prepare instruction position.
511     MachineBasicBlock::const_instr_iterator CallI = CSInfo.first->getIterator();
512     CallLocation.BlockNum = CallI->getParent()->getNumber();
513     // Get call instruction offset from the beginning of block.
514     CallLocation.Offset =
515         std::distance(CallI->getParent()->instr_begin(), CallI);
516     YmlCS.CallLocation = CallLocation;
517     // Construct call arguments and theirs forwarding register info.
518     for (auto ArgReg : CSInfo.second) {
519       yaml::CallSiteInfo::ArgRegPair YmlArgReg;
520       YmlArgReg.ArgNo = ArgReg.ArgNo;
521       printRegMIR(ArgReg.Reg, YmlArgReg.Reg, TRI);
522       YmlCS.ArgForwardingRegs.emplace_back(YmlArgReg);
523     }
524     YMF.CallSitesInfo.push_back(YmlCS);
525   }
526 
527   // Sort call info by position of call instructions.
528   llvm::sort(YMF.CallSitesInfo.begin(), YMF.CallSitesInfo.end(),
529              [](yaml::CallSiteInfo A, yaml::CallSiteInfo B) {
530                if (A.CallLocation.BlockNum == B.CallLocation.BlockNum)
531                  return A.CallLocation.Offset < B.CallLocation.Offset;
532                return A.CallLocation.BlockNum < B.CallLocation.BlockNum;
533              });
534 }
535 
536 void MIRPrinter::convertMachineMetadataNodes(yaml::MachineFunction &YMF,
537                                              const MachineFunction &MF,
538                                              MachineModuleSlotTracker &MST) {
539   MachineModuleSlotTracker::MachineMDNodeListType MDList;
540   MST.collectMachineMDNodes(MDList);
541   for (auto &MD : MDList) {
542     std::string NS;
543     raw_string_ostream StrOS(NS);
544     MD.second->print(StrOS, MST, MF.getFunction().getParent());
545     YMF.MachineMetadataNodes.push_back(StrOS.str());
546   }
547 }
548 
549 void MIRPrinter::convert(yaml::MachineFunction &MF,
550                          const MachineConstantPool &ConstantPool) {
551   unsigned ID = 0;
552   for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
553     std::string Str;
554     raw_string_ostream StrOS(Str);
555     if (Constant.isMachineConstantPoolEntry()) {
556       Constant.Val.MachineCPVal->print(StrOS);
557     } else {
558       Constant.Val.ConstVal->printAsOperand(StrOS);
559     }
560 
561     yaml::MachineConstantPoolValue YamlConstant;
562     YamlConstant.ID = ID++;
563     YamlConstant.Value = StrOS.str();
564     YamlConstant.Alignment = Constant.getAlign();
565     YamlConstant.IsTargetSpecific = Constant.isMachineConstantPoolEntry();
566 
567     MF.Constants.push_back(YamlConstant);
568   }
569 }
570 
571 void MIRPrinter::convert(ModuleSlotTracker &MST,
572                          yaml::MachineJumpTable &YamlJTI,
573                          const MachineJumpTableInfo &JTI) {
574   YamlJTI.Kind = JTI.getEntryKind();
575   unsigned ID = 0;
576   for (const auto &Table : JTI.getJumpTables()) {
577     std::string Str;
578     yaml::MachineJumpTable::Entry Entry;
579     Entry.ID = ID++;
580     for (const auto *MBB : Table.MBBs) {
581       raw_string_ostream StrOS(Str);
582       StrOS << printMBBReference(*MBB);
583       Entry.Blocks.push_back(StrOS.str());
584       Str.clear();
585     }
586     YamlJTI.Entries.push_back(Entry);
587   }
588 }
589 
590 void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
591   const auto *TRI = MF.getSubtarget().getRegisterInfo();
592   unsigned I = 0;
593   for (const uint32_t *Mask : TRI->getRegMasks())
594     RegisterMaskIds.insert(std::make_pair(Mask, I++));
595 }
596 
597 void llvm::guessSuccessors(const MachineBasicBlock &MBB,
598                            SmallVectorImpl<MachineBasicBlock*> &Result,
599                            bool &IsFallthrough) {
600   SmallPtrSet<MachineBasicBlock*,8> Seen;
601 
602   for (const MachineInstr &MI : MBB) {
603     if (MI.isPHI())
604       continue;
605     for (const MachineOperand &MO : MI.operands()) {
606       if (!MO.isMBB())
607         continue;
608       MachineBasicBlock *Succ = MO.getMBB();
609       auto RP = Seen.insert(Succ);
610       if (RP.second)
611         Result.push_back(Succ);
612     }
613   }
614   MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr();
615   IsFallthrough = I == MBB.end() || !I->isBarrier();
616 }
617 
618 bool
619 MIPrinter::canPredictBranchProbabilities(const MachineBasicBlock &MBB) const {
620   if (MBB.succ_size() <= 1)
621     return true;
622   if (!MBB.hasSuccessorProbabilities())
623     return true;
624 
625   SmallVector<BranchProbability,8> Normalized(MBB.Probs.begin(),
626                                               MBB.Probs.end());
627   BranchProbability::normalizeProbabilities(Normalized.begin(),
628                                             Normalized.end());
629   SmallVector<BranchProbability,8> Equal(Normalized.size());
630   BranchProbability::normalizeProbabilities(Equal.begin(), Equal.end());
631 
632   return std::equal(Normalized.begin(), Normalized.end(), Equal.begin());
633 }
634 
635 bool MIPrinter::canPredictSuccessors(const MachineBasicBlock &MBB) const {
636   SmallVector<MachineBasicBlock*,8> GuessedSuccs;
637   bool GuessedFallthrough;
638   guessSuccessors(MBB, GuessedSuccs, GuessedFallthrough);
639   if (GuessedFallthrough) {
640     const MachineFunction &MF = *MBB.getParent();
641     MachineFunction::const_iterator NextI = std::next(MBB.getIterator());
642     if (NextI != MF.end()) {
643       MachineBasicBlock *Next = const_cast<MachineBasicBlock*>(&*NextI);
644       if (!is_contained(GuessedSuccs, Next))
645         GuessedSuccs.push_back(Next);
646     }
647   }
648   if (GuessedSuccs.size() != MBB.succ_size())
649     return false;
650   return std::equal(MBB.succ_begin(), MBB.succ_end(), GuessedSuccs.begin());
651 }
652 
653 void MIPrinter::print(const MachineBasicBlock &MBB) {
654   assert(MBB.getNumber() >= 0 && "Invalid MBB number");
655   MBB.printName(OS,
656                 MachineBasicBlock::PrintNameIr |
657                     MachineBasicBlock::PrintNameAttributes,
658                 &MST);
659   OS << ":\n";
660 
661   bool HasLineAttributes = false;
662   // Print the successors
663   bool canPredictProbs = canPredictBranchProbabilities(MBB);
664   // Even if the list of successors is empty, if we cannot guess it,
665   // we need to print it to tell the parser that the list is empty.
666   // This is needed, because MI model unreachable as empty blocks
667   // with an empty successor list. If the parser would see that
668   // without the successor list, it would guess the code would
669   // fallthrough.
670   if ((!MBB.succ_empty() && !SimplifyMIR) || !canPredictProbs ||
671       !canPredictSuccessors(MBB)) {
672     OS.indent(2) << "successors: ";
673     for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
674       if (I != MBB.succ_begin())
675         OS << ", ";
676       OS << printMBBReference(**I);
677       if (!SimplifyMIR || !canPredictProbs)
678         OS << '('
679            << format("0x%08" PRIx32, MBB.getSuccProbability(I).getNumerator())
680            << ')';
681     }
682     OS << "\n";
683     HasLineAttributes = true;
684   }
685 
686   // Print the live in registers.
687   const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
688   if (MRI.tracksLiveness() && !MBB.livein_empty()) {
689     const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
690     OS.indent(2) << "liveins: ";
691     bool First = true;
692     for (const auto &LI : MBB.liveins()) {
693       if (!First)
694         OS << ", ";
695       First = false;
696       OS << printReg(LI.PhysReg, &TRI);
697       if (!LI.LaneMask.all())
698         OS << ":0x" << PrintLaneMask(LI.LaneMask);
699     }
700     OS << "\n";
701     HasLineAttributes = true;
702   }
703 
704   if (HasLineAttributes)
705     OS << "\n";
706   bool IsInBundle = false;
707   for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
708     const MachineInstr &MI = *I;
709     if (IsInBundle && !MI.isInsideBundle()) {
710       OS.indent(2) << "}\n";
711       IsInBundle = false;
712     }
713     OS.indent(IsInBundle ? 4 : 2);
714     print(MI);
715     if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
716       OS << " {";
717       IsInBundle = true;
718     }
719     OS << "\n";
720   }
721   if (IsInBundle)
722     OS.indent(2) << "}\n";
723 }
724 
725 void MIPrinter::print(const MachineInstr &MI) {
726   const auto *MF = MI.getMF();
727   const auto &MRI = MF->getRegInfo();
728   const auto &SubTarget = MF->getSubtarget();
729   const auto *TRI = SubTarget.getRegisterInfo();
730   assert(TRI && "Expected target register info");
731   const auto *TII = SubTarget.getInstrInfo();
732   assert(TII && "Expected target instruction info");
733   if (MI.isCFIInstruction())
734     assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
735 
736   SmallBitVector PrintedTypes(8);
737   bool ShouldPrintRegisterTies = MI.hasComplexRegisterTies();
738   unsigned I = 0, E = MI.getNumOperands();
739   for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
740          !MI.getOperand(I).isImplicit();
741        ++I) {
742     if (I)
743       OS << ", ";
744     print(MI, I, TRI, TII, ShouldPrintRegisterTies,
745           MI.getTypeToPrint(I, PrintedTypes, MRI),
746           /*PrintDef=*/false);
747   }
748 
749   if (I)
750     OS << " = ";
751   if (MI.getFlag(MachineInstr::FrameSetup))
752     OS << "frame-setup ";
753   if (MI.getFlag(MachineInstr::FrameDestroy))
754     OS << "frame-destroy ";
755   if (MI.getFlag(MachineInstr::FmNoNans))
756     OS << "nnan ";
757   if (MI.getFlag(MachineInstr::FmNoInfs))
758     OS << "ninf ";
759   if (MI.getFlag(MachineInstr::FmNsz))
760     OS << "nsz ";
761   if (MI.getFlag(MachineInstr::FmArcp))
762     OS << "arcp ";
763   if (MI.getFlag(MachineInstr::FmContract))
764     OS << "contract ";
765   if (MI.getFlag(MachineInstr::FmAfn))
766     OS << "afn ";
767   if (MI.getFlag(MachineInstr::FmReassoc))
768     OS << "reassoc ";
769   if (MI.getFlag(MachineInstr::NoUWrap))
770     OS << "nuw ";
771   if (MI.getFlag(MachineInstr::NoSWrap))
772     OS << "nsw ";
773   if (MI.getFlag(MachineInstr::IsExact))
774     OS << "exact ";
775   if (MI.getFlag(MachineInstr::NoFPExcept))
776     OS << "nofpexcept ";
777   if (MI.getFlag(MachineInstr::NoMerge))
778     OS << "nomerge ";
779 
780   OS << TII->getName(MI.getOpcode());
781   if (I < E)
782     OS << ' ';
783 
784   bool NeedComma = false;
785   for (; I < E; ++I) {
786     if (NeedComma)
787       OS << ", ";
788     print(MI, I, TRI, TII, ShouldPrintRegisterTies,
789           MI.getTypeToPrint(I, PrintedTypes, MRI));
790     NeedComma = true;
791   }
792 
793   // Print any optional symbols attached to this instruction as-if they were
794   // operands.
795   if (MCSymbol *PreInstrSymbol = MI.getPreInstrSymbol()) {
796     if (NeedComma)
797       OS << ',';
798     OS << " pre-instr-symbol ";
799     MachineOperand::printSymbol(OS, *PreInstrSymbol);
800     NeedComma = true;
801   }
802   if (MCSymbol *PostInstrSymbol = MI.getPostInstrSymbol()) {
803     if (NeedComma)
804       OS << ',';
805     OS << " post-instr-symbol ";
806     MachineOperand::printSymbol(OS, *PostInstrSymbol);
807     NeedComma = true;
808   }
809   if (MDNode *HeapAllocMarker = MI.getHeapAllocMarker()) {
810     if (NeedComma)
811       OS << ',';
812     OS << " heap-alloc-marker ";
813     HeapAllocMarker->printAsOperand(OS, MST);
814     NeedComma = true;
815   }
816 
817   if (auto Num = MI.peekDebugInstrNum()) {
818     if (NeedComma)
819       OS << ',';
820     OS << " debug-instr-number " << Num;
821     NeedComma = true;
822   }
823 
824   if (PrintLocations) {
825     if (const DebugLoc &DL = MI.getDebugLoc()) {
826       if (NeedComma)
827         OS << ',';
828       OS << " debug-location ";
829       DL->printAsOperand(OS, MST);
830     }
831   }
832 
833   if (!MI.memoperands_empty()) {
834     OS << " :: ";
835     const LLVMContext &Context = MF->getFunction().getContext();
836     const MachineFrameInfo &MFI = MF->getFrameInfo();
837     bool NeedComma = false;
838     for (const auto *Op : MI.memoperands()) {
839       if (NeedComma)
840         OS << ", ";
841       Op->print(OS, MST, SSNs, Context, &MFI, TII);
842       NeedComma = true;
843     }
844   }
845 }
846 
847 void MIPrinter::printStackObjectReference(int FrameIndex) {
848   auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
849   assert(ObjectInfo != StackObjectOperandMapping.end() &&
850          "Invalid frame index");
851   const FrameIndexOperand &Operand = ObjectInfo->second;
852   MachineOperand::printStackObjectReference(OS, Operand.ID, Operand.IsFixed,
853                                             Operand.Name);
854 }
855 
856 static std::string formatOperandComment(std::string Comment) {
857   if (Comment.empty())
858     return Comment;
859   return std::string(" /* " + Comment + " */");
860 }
861 
862 void MIPrinter::print(const MachineInstr &MI, unsigned OpIdx,
863                       const TargetRegisterInfo *TRI,
864                       const TargetInstrInfo *TII,
865                       bool ShouldPrintRegisterTies, LLT TypeToPrint,
866                       bool PrintDef) {
867   const MachineOperand &Op = MI.getOperand(OpIdx);
868   std::string MOComment = TII->createMIROperandComment(MI, Op, OpIdx, TRI);
869 
870   switch (Op.getType()) {
871   case MachineOperand::MO_Immediate:
872     if (MI.isOperandSubregIdx(OpIdx)) {
873       MachineOperand::printTargetFlags(OS, Op);
874       MachineOperand::printSubRegIdx(OS, Op.getImm(), TRI);
875       break;
876     }
877     LLVM_FALLTHROUGH;
878   case MachineOperand::MO_Register:
879   case MachineOperand::MO_CImmediate:
880   case MachineOperand::MO_FPImmediate:
881   case MachineOperand::MO_MachineBasicBlock:
882   case MachineOperand::MO_ConstantPoolIndex:
883   case MachineOperand::MO_TargetIndex:
884   case MachineOperand::MO_JumpTableIndex:
885   case MachineOperand::MO_ExternalSymbol:
886   case MachineOperand::MO_GlobalAddress:
887   case MachineOperand::MO_RegisterLiveOut:
888   case MachineOperand::MO_Metadata:
889   case MachineOperand::MO_MCSymbol:
890   case MachineOperand::MO_CFIIndex:
891   case MachineOperand::MO_IntrinsicID:
892   case MachineOperand::MO_Predicate:
893   case MachineOperand::MO_BlockAddress:
894   case MachineOperand::MO_ShuffleMask: {
895     unsigned TiedOperandIdx = 0;
896     if (ShouldPrintRegisterTies && Op.isReg() && Op.isTied() && !Op.isDef())
897       TiedOperandIdx = Op.getParent()->findTiedOperandIdx(OpIdx);
898     const TargetIntrinsicInfo *TII = MI.getMF()->getTarget().getIntrinsicInfo();
899     Op.print(OS, MST, TypeToPrint, OpIdx, PrintDef, /*IsStandalone=*/false,
900              ShouldPrintRegisterTies, TiedOperandIdx, TRI, TII);
901       OS << formatOperandComment(MOComment);
902     break;
903   }
904   case MachineOperand::MO_FrameIndex:
905     printStackObjectReference(Op.getIndex());
906     break;
907   case MachineOperand::MO_RegisterMask: {
908     auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
909     if (RegMaskInfo != RegisterMaskIds.end())
910       OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
911     else
912       printCustomRegMask(Op.getRegMask(), OS, TRI);
913     break;
914   }
915   }
916 }
917 
918 void MIRFormatter::printIRValue(raw_ostream &OS, const Value &V,
919                                 ModuleSlotTracker &MST) {
920   if (isa<GlobalValue>(V)) {
921     V.printAsOperand(OS, /*PrintType=*/false, MST);
922     return;
923   }
924   if (isa<Constant>(V)) {
925     // Machine memory operands can load/store to/from constant value pointers.
926     OS << '`';
927     V.printAsOperand(OS, /*PrintType=*/true, MST);
928     OS << '`';
929     return;
930   }
931   OS << "%ir.";
932   if (V.hasName()) {
933     printLLVMNameWithoutPrefix(OS, V.getName());
934     return;
935   }
936   int Slot = MST.getCurrentFunction() ? MST.getLocalSlot(&V) : -1;
937   MachineOperand::printIRSlotNumber(OS, Slot);
938 }
939 
940 void llvm::printMIR(raw_ostream &OS, const Module &M) {
941   yaml::Output Out(OS);
942   Out << const_cast<Module &>(M);
943 }
944 
945 void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
946   MIRPrinter Printer(OS);
947   Printer.print(MF);
948 }
949