18f0fd8f6SDimitry Andric //===- MIRPrinter.cpp - MIR serialization format printer ------------------===//
28f0fd8f6SDimitry Andric //
38f0fd8f6SDimitry Andric // The LLVM Compiler Infrastructure
48f0fd8f6SDimitry Andric //
58f0fd8f6SDimitry Andric // This file is distributed under the University of Illinois Open Source
68f0fd8f6SDimitry Andric // License. See LICENSE.TXT for details.
78f0fd8f6SDimitry Andric //
88f0fd8f6SDimitry Andric //===----------------------------------------------------------------------===//
98f0fd8f6SDimitry Andric //
108f0fd8f6SDimitry Andric // This file implements the class that prints out the LLVM IR and machine
118f0fd8f6SDimitry Andric // functions using the MIR serialization format.
128f0fd8f6SDimitry Andric //
138f0fd8f6SDimitry Andric //===----------------------------------------------------------------------===//
148f0fd8f6SDimitry Andric
152cab237bSDimitry Andric #include "llvm/CodeGen/MIRPrinter.h"
16db17bf38SDimitry Andric #include "llvm/ADT/DenseMap.h"
17db17bf38SDimitry Andric #include "llvm/ADT/None.h"
182cab237bSDimitry Andric #include "llvm/ADT/STLExtras.h"
19d88c1a5aSDimitry Andric #include "llvm/ADT/SmallBitVector.h"
20db17bf38SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
21db17bf38SDimitry Andric #include "llvm/ADT/SmallVector.h"
22db17bf38SDimitry Andric #include "llvm/ADT/StringRef.h"
23db17bf38SDimitry Andric #include "llvm/ADT/Twine.h"
243ca95b02SDimitry Andric #include "llvm/CodeGen/GlobalISel/RegisterBank.h"
252cab237bSDimitry Andric #include "llvm/CodeGen/MIRYamlMapping.h"
26db17bf38SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
277d523365SDimitry Andric #include "llvm/CodeGen/MachineConstantPool.h"
28875ed548SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
293ca95b02SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
30db17bf38SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
31db17bf38SDimitry Andric #include "llvm/CodeGen/MachineJumpTableInfo.h"
327d523365SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h"
33db17bf38SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
343dac3a9bSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
35db17bf38SDimitry Andric #include "llvm/CodeGen/PseudoSourceValue.h"
362cab237bSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
372cab237bSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
382cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
398f0fd8f6SDimitry Andric #include "llvm/IR/BasicBlock.h"
407d523365SDimitry Andric #include "llvm/IR/Constants.h"
413ca95b02SDimitry Andric #include "llvm/IR/DebugInfo.h"
42db17bf38SDimitry Andric #include "llvm/IR/DebugLoc.h"
43db17bf38SDimitry Andric #include "llvm/IR/Function.h"
44db17bf38SDimitry Andric #include "llvm/IR/GlobalValue.h"
452cab237bSDimitry Andric #include "llvm/IR/IRPrintingPasses.h"
46db17bf38SDimitry Andric #include "llvm/IR/InstrTypes.h"
473ca95b02SDimitry Andric #include "llvm/IR/Instructions.h"
48d88c1a5aSDimitry Andric #include "llvm/IR/Intrinsics.h"
498f0fd8f6SDimitry Andric #include "llvm/IR/Module.h"
50875ed548SDimitry Andric #include "llvm/IR/ModuleSlotTracker.h"
51db17bf38SDimitry Andric #include "llvm/IR/Value.h"
52db17bf38SDimitry Andric #include "llvm/MC/LaneBitmask.h"
53*b5893f02SDimitry Andric #include "llvm/MC/MCContext.h"
54db17bf38SDimitry Andric #include "llvm/MC/MCDwarf.h"
557d523365SDimitry Andric #include "llvm/MC/MCSymbol.h"
56db17bf38SDimitry Andric #include "llvm/Support/AtomicOrdering.h"
57db17bf38SDimitry Andric #include "llvm/Support/BranchProbability.h"
58db17bf38SDimitry Andric #include "llvm/Support/Casting.h"
59db17bf38SDimitry Andric #include "llvm/Support/CommandLine.h"
60db17bf38SDimitry Andric #include "llvm/Support/ErrorHandling.h"
61d88c1a5aSDimitry Andric #include "llvm/Support/Format.h"
62db17bf38SDimitry Andric #include "llvm/Support/LowLevelTypeImpl.h"
63db17bf38SDimitry Andric #include "llvm/Support/YAMLTraits.h"
642cab237bSDimitry Andric #include "llvm/Support/raw_ostream.h"
65d88c1a5aSDimitry Andric #include "llvm/Target/TargetIntrinsicInfo.h"
66db17bf38SDimitry Andric #include "llvm/Target/TargetMachine.h"
67db17bf38SDimitry Andric #include <algorithm>
68db17bf38SDimitry Andric #include <cassert>
69db17bf38SDimitry Andric #include <cinttypes>
70db17bf38SDimitry Andric #include <cstdint>
71db17bf38SDimitry Andric #include <iterator>
72db17bf38SDimitry Andric #include <string>
73db17bf38SDimitry Andric #include <utility>
74db17bf38SDimitry Andric #include <vector>
758f0fd8f6SDimitry Andric
768f0fd8f6SDimitry Andric using namespace llvm;
778f0fd8f6SDimitry Andric
782cab237bSDimitry Andric static cl::opt<bool> SimplifyMIR(
792cab237bSDimitry Andric "simplify-mir", cl::Hidden,
800f5676f4SDimitry Andric cl::desc("Leave out unnecessary information when printing MIR"));
810f5676f4SDimitry Andric
828f0fd8f6SDimitry Andric namespace {
838f0fd8f6SDimitry Andric
847d523365SDimitry Andric /// This structure describes how to print out stack object references.
857d523365SDimitry Andric struct FrameIndexOperand {
867d523365SDimitry Andric std::string Name;
877d523365SDimitry Andric unsigned ID;
887d523365SDimitry Andric bool IsFixed;
897d523365SDimitry Andric
FrameIndexOperand__anona85e4b250111::FrameIndexOperand907d523365SDimitry Andric FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
917d523365SDimitry Andric : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
927d523365SDimitry Andric
937d523365SDimitry Andric /// Return an ordinary stack object reference.
create__anona85e4b250111::FrameIndexOperand947d523365SDimitry Andric static FrameIndexOperand create(StringRef Name, unsigned ID) {
957d523365SDimitry Andric return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
967d523365SDimitry Andric }
977d523365SDimitry Andric
987d523365SDimitry Andric /// Return a fixed stack object reference.
createFixed__anona85e4b250111::FrameIndexOperand997d523365SDimitry Andric static FrameIndexOperand createFixed(unsigned ID) {
1007d523365SDimitry Andric return FrameIndexOperand("", ID, /*IsFixed=*/true);
1017d523365SDimitry Andric }
1027d523365SDimitry Andric };
1037d523365SDimitry Andric
1047d523365SDimitry Andric } // end anonymous namespace
1057d523365SDimitry Andric
1067d523365SDimitry Andric namespace llvm {
1077d523365SDimitry Andric
1088f0fd8f6SDimitry Andric /// This class prints out the machine functions using the MIR serialization
1098f0fd8f6SDimitry Andric /// format.
1108f0fd8f6SDimitry Andric class MIRPrinter {
1118f0fd8f6SDimitry Andric raw_ostream &OS;
1123dac3a9bSDimitry Andric DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
1137d523365SDimitry Andric /// Maps from stack object indices to operand indices which will be used when
1147d523365SDimitry Andric /// printing frame index machine operands.
1157d523365SDimitry Andric DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
1168f0fd8f6SDimitry Andric
1178f0fd8f6SDimitry Andric public:
MIRPrinter(raw_ostream & OS)1188f0fd8f6SDimitry Andric MIRPrinter(raw_ostream &OS) : OS(OS) {}
1198f0fd8f6SDimitry Andric
1208f0fd8f6SDimitry Andric void print(const MachineFunction &MF);
1218f0fd8f6SDimitry Andric
122875ed548SDimitry Andric void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
123875ed548SDimitry Andric const TargetRegisterInfo *TRI);
1247d523365SDimitry Andric void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
125875ed548SDimitry Andric const MachineFrameInfo &MFI);
1267d523365SDimitry Andric void convert(yaml::MachineFunction &MF,
1277d523365SDimitry Andric const MachineConstantPool &ConstantPool);
1287d523365SDimitry Andric void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
1297d523365SDimitry Andric const MachineJumpTableInfo &JTI);
130d88c1a5aSDimitry Andric void convertStackObjects(yaml::MachineFunction &YMF,
131d88c1a5aSDimitry Andric const MachineFunction &MF, ModuleSlotTracker &MST);
1323dac3a9bSDimitry Andric
1333dac3a9bSDimitry Andric private:
1343dac3a9bSDimitry Andric void initRegisterMaskIds(const MachineFunction &MF);
1353dac3a9bSDimitry Andric };
1363dac3a9bSDimitry Andric
1373dac3a9bSDimitry Andric /// This class prints out the machine instructions using the MIR serialization
1383dac3a9bSDimitry Andric /// format.
1393dac3a9bSDimitry Andric class MIPrinter {
1403dac3a9bSDimitry Andric raw_ostream &OS;
141875ed548SDimitry Andric ModuleSlotTracker &MST;
1423dac3a9bSDimitry Andric const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
1437d523365SDimitry Andric const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
144c4394386SDimitry Andric /// Synchronization scope names registered with LLVMContext.
145c4394386SDimitry Andric SmallVector<StringRef, 8> SSNs;
1463dac3a9bSDimitry Andric
1470f5676f4SDimitry Andric bool canPredictBranchProbabilities(const MachineBasicBlock &MBB) const;
1480f5676f4SDimitry Andric bool canPredictSuccessors(const MachineBasicBlock &MBB) const;
1490f5676f4SDimitry Andric
1503dac3a9bSDimitry Andric public:
MIPrinter(raw_ostream & OS,ModuleSlotTracker & MST,const DenseMap<const uint32_t *,unsigned> & RegisterMaskIds,const DenseMap<int,FrameIndexOperand> & StackObjectOperandMapping)151875ed548SDimitry Andric MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
1527d523365SDimitry Andric const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
1537d523365SDimitry Andric const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
1547d523365SDimitry Andric : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
1557d523365SDimitry Andric StackObjectOperandMapping(StackObjectOperandMapping) {}
1567d523365SDimitry Andric
1577d523365SDimitry Andric void print(const MachineBasicBlock &MBB);
1583dac3a9bSDimitry Andric
1593dac3a9bSDimitry Andric void print(const MachineInstr &MI);
1607d523365SDimitry Andric void printStackObjectReference(int FrameIndex);
1612cab237bSDimitry Andric void print(const MachineInstr &MI, unsigned OpIdx,
1622cab237bSDimitry Andric const TargetRegisterInfo *TRI, bool ShouldPrintRegisterTies,
1632cab237bSDimitry Andric LLT TypeToPrint, bool PrintDef = true);
1648f0fd8f6SDimitry Andric };
1658f0fd8f6SDimitry Andric
1667d523365SDimitry Andric } // end namespace llvm
1678f0fd8f6SDimitry Andric
1688f0fd8f6SDimitry Andric namespace llvm {
1698f0fd8f6SDimitry Andric namespace yaml {
1708f0fd8f6SDimitry Andric
1718f0fd8f6SDimitry Andric /// This struct serializes the LLVM IR module.
1728f0fd8f6SDimitry Andric template <> struct BlockScalarTraits<Module> {
outputllvm::yaml::BlockScalarTraits1738f0fd8f6SDimitry Andric static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
1748f0fd8f6SDimitry Andric Mod.print(OS, nullptr);
1758f0fd8f6SDimitry Andric }
176db17bf38SDimitry Andric
inputllvm::yaml::BlockScalarTraits1778f0fd8f6SDimitry Andric static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
1788f0fd8f6SDimitry Andric llvm_unreachable("LLVM Module is supposed to be parsed separately");
1798f0fd8f6SDimitry Andric return "";
1808f0fd8f6SDimitry Andric }
1818f0fd8f6SDimitry Andric };
1828f0fd8f6SDimitry Andric
1838f0fd8f6SDimitry Andric } // end namespace yaml
1848f0fd8f6SDimitry Andric } // end namespace llvm
1858f0fd8f6SDimitry Andric
printRegMIR(unsigned Reg,yaml::StringValue & Dest,const TargetRegisterInfo * TRI)1862cab237bSDimitry Andric static void printRegMIR(unsigned Reg, yaml::StringValue &Dest,
1877d523365SDimitry Andric const TargetRegisterInfo *TRI) {
1887d523365SDimitry Andric raw_string_ostream OS(Dest.Value);
1892cab237bSDimitry Andric OS << printReg(Reg, TRI);
1907d523365SDimitry Andric }
1917d523365SDimitry Andric
print(const MachineFunction & MF)1928f0fd8f6SDimitry Andric void MIRPrinter::print(const MachineFunction &MF) {
1933dac3a9bSDimitry Andric initRegisterMaskIds(MF);
1943dac3a9bSDimitry Andric
1958f0fd8f6SDimitry Andric yaml::MachineFunction YamlMF;
1968f0fd8f6SDimitry Andric YamlMF.Name = MF.getName();
1978f0fd8f6SDimitry Andric YamlMF.Alignment = MF.getAlignment();
1988f0fd8f6SDimitry Andric YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
199*b5893f02SDimitry Andric YamlMF.HasWinCFI = MF.hasWinCFI();
200d88c1a5aSDimitry Andric
201d88c1a5aSDimitry Andric YamlMF.Legalized = MF.getProperties().hasProperty(
202d88c1a5aSDimitry Andric MachineFunctionProperties::Property::Legalized);
203d88c1a5aSDimitry Andric YamlMF.RegBankSelected = MF.getProperties().hasProperty(
204d88c1a5aSDimitry Andric MachineFunctionProperties::Property::RegBankSelected);
205d88c1a5aSDimitry Andric YamlMF.Selected = MF.getProperties().hasProperty(
206d88c1a5aSDimitry Andric MachineFunctionProperties::Property::Selected);
2074ba319b5SDimitry Andric YamlMF.FailedISel = MF.getProperties().hasProperty(
2084ba319b5SDimitry Andric MachineFunctionProperties::Property::FailedISel);
2093ca95b02SDimitry Andric
210875ed548SDimitry Andric convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
2112cab237bSDimitry Andric ModuleSlotTracker MST(MF.getFunction().getParent());
2122cab237bSDimitry Andric MST.incorporateFunction(MF.getFunction());
213d88c1a5aSDimitry Andric convert(MST, YamlMF.FrameInfo, MF.getFrameInfo());
214d88c1a5aSDimitry Andric convertStackObjects(YamlMF, MF, MST);
2157d523365SDimitry Andric if (const auto *ConstantPool = MF.getConstantPool())
2167d523365SDimitry Andric convert(YamlMF, *ConstantPool);
2177d523365SDimitry Andric if (const auto *JumpTableInfo = MF.getJumpTableInfo())
2187d523365SDimitry Andric convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
2197d523365SDimitry Andric raw_string_ostream StrOS(YamlMF.Body.Value.Value);
2207d523365SDimitry Andric bool IsNewlineNeeded = false;
2218f0fd8f6SDimitry Andric for (const auto &MBB : MF) {
2227d523365SDimitry Andric if (IsNewlineNeeded)
2237d523365SDimitry Andric StrOS << "\n";
2247d523365SDimitry Andric MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
2257d523365SDimitry Andric .print(MBB);
2267d523365SDimitry Andric IsNewlineNeeded = true;
2278f0fd8f6SDimitry Andric }
2287d523365SDimitry Andric StrOS.flush();
2298f0fd8f6SDimitry Andric yaml::Output Out(OS);
230db17bf38SDimitry Andric if (!SimplifyMIR)
231db17bf38SDimitry Andric Out.setWriteDefaultValues(true);
2328f0fd8f6SDimitry Andric Out << YamlMF;
2338f0fd8f6SDimitry Andric }
2348f0fd8f6SDimitry Andric
printCustomRegMask(const uint32_t * RegMask,raw_ostream & OS,const TargetRegisterInfo * TRI)2357a7e6055SDimitry Andric static void printCustomRegMask(const uint32_t *RegMask, raw_ostream &OS,
2367a7e6055SDimitry Andric const TargetRegisterInfo *TRI) {
2377a7e6055SDimitry Andric assert(RegMask && "Can't print an empty register mask");
2387a7e6055SDimitry Andric OS << StringRef("CustomRegMask(");
2397a7e6055SDimitry Andric
2407a7e6055SDimitry Andric bool IsRegInRegMaskFound = false;
2417a7e6055SDimitry Andric for (int I = 0, E = TRI->getNumRegs(); I < E; I++) {
2427a7e6055SDimitry Andric // Check whether the register is asserted in regmask.
2437a7e6055SDimitry Andric if (RegMask[I / 32] & (1u << (I % 32))) {
2447a7e6055SDimitry Andric if (IsRegInRegMaskFound)
2457a7e6055SDimitry Andric OS << ',';
2462cab237bSDimitry Andric OS << printReg(I, TRI);
2477a7e6055SDimitry Andric IsRegInRegMaskFound = true;
2487a7e6055SDimitry Andric }
2497a7e6055SDimitry Andric }
2507a7e6055SDimitry Andric
2517a7e6055SDimitry Andric OS << ')';
2527a7e6055SDimitry Andric }
2537a7e6055SDimitry Andric
printRegClassOrBank(unsigned Reg,yaml::StringValue & Dest,const MachineRegisterInfo & RegInfo,const TargetRegisterInfo * TRI)2542cab237bSDimitry Andric static void printRegClassOrBank(unsigned Reg, yaml::StringValue &Dest,
2552cab237bSDimitry Andric const MachineRegisterInfo &RegInfo,
2562cab237bSDimitry Andric const TargetRegisterInfo *TRI) {
2572cab237bSDimitry Andric raw_string_ostream OS(Dest.Value);
2582cab237bSDimitry Andric OS << printRegClassOrBank(Reg, RegInfo, TRI);
2592cab237bSDimitry Andric }
2602cab237bSDimitry Andric
2614ba319b5SDimitry Andric template <typename T>
2624ba319b5SDimitry Andric static void
printStackObjectDbgInfo(const MachineFunction::VariableDbgInfo & DebugVar,T & Object,ModuleSlotTracker & MST)2634ba319b5SDimitry Andric printStackObjectDbgInfo(const MachineFunction::VariableDbgInfo &DebugVar,
2644ba319b5SDimitry Andric T &Object, ModuleSlotTracker &MST) {
2654ba319b5SDimitry Andric std::array<std::string *, 3> Outputs{{&Object.DebugVar.Value,
2664ba319b5SDimitry Andric &Object.DebugExpr.Value,
2674ba319b5SDimitry Andric &Object.DebugLoc.Value}};
2684ba319b5SDimitry Andric std::array<const Metadata *, 3> Metas{{DebugVar.Var,
2694ba319b5SDimitry Andric DebugVar.Expr,
2704ba319b5SDimitry Andric DebugVar.Loc}};
2714ba319b5SDimitry Andric for (unsigned i = 0; i < 3; ++i) {
2724ba319b5SDimitry Andric raw_string_ostream StrOS(*Outputs[i]);
2734ba319b5SDimitry Andric Metas[i]->printAsOperand(StrOS, MST);
2744ba319b5SDimitry Andric }
2754ba319b5SDimitry Andric }
2762cab237bSDimitry Andric
convert(yaml::MachineFunction & MF,const MachineRegisterInfo & RegInfo,const TargetRegisterInfo * TRI)2773dac3a9bSDimitry Andric void MIRPrinter::convert(yaml::MachineFunction &MF,
278875ed548SDimitry Andric const MachineRegisterInfo &RegInfo,
279875ed548SDimitry Andric const TargetRegisterInfo *TRI) {
2803dac3a9bSDimitry Andric MF.TracksRegLiveness = RegInfo.tracksLiveness();
281875ed548SDimitry Andric
282875ed548SDimitry Andric // Print the virtual register definitions.
283875ed548SDimitry Andric for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
284875ed548SDimitry Andric unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
285875ed548SDimitry Andric yaml::VirtualRegisterDefinition VReg;
286875ed548SDimitry Andric VReg.ID = I;
2874ba319b5SDimitry Andric if (RegInfo.getVRegName(Reg) != "")
2884ba319b5SDimitry Andric continue;
2892cab237bSDimitry Andric ::printRegClassOrBank(Reg, VReg.Class, RegInfo, TRI);
2907d523365SDimitry Andric unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
2917d523365SDimitry Andric if (PreferredReg)
2922cab237bSDimitry Andric printRegMIR(PreferredReg, VReg.PreferredRegister, TRI);
293875ed548SDimitry Andric MF.VirtualRegisters.push_back(VReg);
294875ed548SDimitry Andric }
2957d523365SDimitry Andric
2967d523365SDimitry Andric // Print the live ins.
2972cab237bSDimitry Andric for (std::pair<unsigned, unsigned> LI : RegInfo.liveins()) {
2987d523365SDimitry Andric yaml::MachineFunctionLiveIn LiveIn;
2992cab237bSDimitry Andric printRegMIR(LI.first, LiveIn.Register, TRI);
3002cab237bSDimitry Andric if (LI.second)
3012cab237bSDimitry Andric printRegMIR(LI.second, LiveIn.VirtualRegister, TRI);
3027d523365SDimitry Andric MF.LiveIns.push_back(LiveIn);
3037d523365SDimitry Andric }
3047a7e6055SDimitry Andric
3057a7e6055SDimitry Andric // Prints the callee saved registers.
3067a7e6055SDimitry Andric if (RegInfo.isUpdatedCSRsInitialized()) {
3077a7e6055SDimitry Andric const MCPhysReg *CalleeSavedRegs = RegInfo.getCalleeSavedRegs();
3087d523365SDimitry Andric std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
3097a7e6055SDimitry Andric for (const MCPhysReg *I = CalleeSavedRegs; *I; ++I) {
3107d523365SDimitry Andric yaml::FlowStringValue Reg;
3112cab237bSDimitry Andric printRegMIR(*I, Reg, TRI);
3127d523365SDimitry Andric CalleeSavedRegisters.push_back(Reg);
3137d523365SDimitry Andric }
3147d523365SDimitry Andric MF.CalleeSavedRegisters = CalleeSavedRegisters;
3153dac3a9bSDimitry Andric }
3167a7e6055SDimitry Andric }
3173dac3a9bSDimitry Andric
convert(ModuleSlotTracker & MST,yaml::MachineFrameInfo & YamlMFI,const MachineFrameInfo & MFI)3187d523365SDimitry Andric void MIRPrinter::convert(ModuleSlotTracker &MST,
3197d523365SDimitry Andric yaml::MachineFrameInfo &YamlMFI,
320875ed548SDimitry Andric const MachineFrameInfo &MFI) {
321875ed548SDimitry Andric YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
322875ed548SDimitry Andric YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
323875ed548SDimitry Andric YamlMFI.HasStackMap = MFI.hasStackMap();
324875ed548SDimitry Andric YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
325875ed548SDimitry Andric YamlMFI.StackSize = MFI.getStackSize();
326875ed548SDimitry Andric YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
327875ed548SDimitry Andric YamlMFI.MaxAlignment = MFI.getMaxAlignment();
328875ed548SDimitry Andric YamlMFI.AdjustsStack = MFI.adjustsStack();
329875ed548SDimitry Andric YamlMFI.HasCalls = MFI.hasCalls();
330f37b6182SDimitry Andric YamlMFI.MaxCallFrameSize = MFI.isMaxCallFrameSizeComputed()
331f37b6182SDimitry Andric ? MFI.getMaxCallFrameSize() : ~0u;
332*b5893f02SDimitry Andric YamlMFI.CVBytesOfCalleeSavedRegisters =
333*b5893f02SDimitry Andric MFI.getCVBytesOfCalleeSavedRegisters();
334875ed548SDimitry Andric YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
335875ed548SDimitry Andric YamlMFI.HasVAStart = MFI.hasVAStart();
336875ed548SDimitry Andric YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
3374ba319b5SDimitry Andric YamlMFI.LocalFrameSize = MFI.getLocalFrameSize();
3387d523365SDimitry Andric if (MFI.getSavePoint()) {
3397d523365SDimitry Andric raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
3402cab237bSDimitry Andric StrOS << printMBBReference(*MFI.getSavePoint());
3417d523365SDimitry Andric }
3427d523365SDimitry Andric if (MFI.getRestorePoint()) {
3437d523365SDimitry Andric raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
3442cab237bSDimitry Andric StrOS << printMBBReference(*MFI.getRestorePoint());
3457d523365SDimitry Andric }
346875ed548SDimitry Andric }
347875ed548SDimitry Andric
convertStackObjects(yaml::MachineFunction & YMF,const MachineFunction & MF,ModuleSlotTracker & MST)348d88c1a5aSDimitry Andric void MIRPrinter::convertStackObjects(yaml::MachineFunction &YMF,
349d88c1a5aSDimitry Andric const MachineFunction &MF,
350d88c1a5aSDimitry Andric ModuleSlotTracker &MST) {
351d88c1a5aSDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo();
352d88c1a5aSDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
353875ed548SDimitry Andric // Process fixed stack objects.
354875ed548SDimitry Andric unsigned ID = 0;
355875ed548SDimitry Andric for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
356875ed548SDimitry Andric if (MFI.isDeadObjectIndex(I))
357875ed548SDimitry Andric continue;
358875ed548SDimitry Andric
359875ed548SDimitry Andric yaml::FixedMachineStackObject YamlObject;
3607d523365SDimitry Andric YamlObject.ID = ID;
361875ed548SDimitry Andric YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
362875ed548SDimitry Andric ? yaml::FixedMachineStackObject::SpillSlot
363875ed548SDimitry Andric : yaml::FixedMachineStackObject::DefaultType;
364875ed548SDimitry Andric YamlObject.Offset = MFI.getObjectOffset(I);
365875ed548SDimitry Andric YamlObject.Size = MFI.getObjectSize(I);
366875ed548SDimitry Andric YamlObject.Alignment = MFI.getObjectAlignment(I);
3672cab237bSDimitry Andric YamlObject.StackID = MFI.getStackID(I);
368875ed548SDimitry Andric YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
369875ed548SDimitry Andric YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
370d88c1a5aSDimitry Andric YMF.FixedStackObjects.push_back(YamlObject);
3717d523365SDimitry Andric StackObjectOperandMapping.insert(
3727d523365SDimitry Andric std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
373875ed548SDimitry Andric }
374875ed548SDimitry Andric
375875ed548SDimitry Andric // Process ordinary stack objects.
376875ed548SDimitry Andric ID = 0;
377875ed548SDimitry Andric for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
378875ed548SDimitry Andric if (MFI.isDeadObjectIndex(I))
379875ed548SDimitry Andric continue;
380875ed548SDimitry Andric
381875ed548SDimitry Andric yaml::MachineStackObject YamlObject;
3827d523365SDimitry Andric YamlObject.ID = ID;
3837d523365SDimitry Andric if (const auto *Alloca = MFI.getObjectAllocation(I))
3847d523365SDimitry Andric YamlObject.Name.Value =
3857d523365SDimitry Andric Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
386875ed548SDimitry Andric YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
387875ed548SDimitry Andric ? yaml::MachineStackObject::SpillSlot
388875ed548SDimitry Andric : MFI.isVariableSizedObjectIndex(I)
389875ed548SDimitry Andric ? yaml::MachineStackObject::VariableSized
390875ed548SDimitry Andric : yaml::MachineStackObject::DefaultType;
391875ed548SDimitry Andric YamlObject.Offset = MFI.getObjectOffset(I);
392875ed548SDimitry Andric YamlObject.Size = MFI.getObjectSize(I);
393875ed548SDimitry Andric YamlObject.Alignment = MFI.getObjectAlignment(I);
3942cab237bSDimitry Andric YamlObject.StackID = MFI.getStackID(I);
395875ed548SDimitry Andric
396d88c1a5aSDimitry Andric YMF.StackObjects.push_back(YamlObject);
3977d523365SDimitry Andric StackObjectOperandMapping.insert(std::make_pair(
3987d523365SDimitry Andric I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
3997d523365SDimitry Andric }
4007d523365SDimitry Andric
4017d523365SDimitry Andric for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
4027d523365SDimitry Andric yaml::StringValue Reg;
4032cab237bSDimitry Andric printRegMIR(CSInfo.getReg(), Reg, TRI);
404*b5893f02SDimitry Andric if (!CSInfo.isSpilledToReg()) {
4057d523365SDimitry Andric auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
4067d523365SDimitry Andric assert(StackObjectInfo != StackObjectOperandMapping.end() &&
4077d523365SDimitry Andric "Invalid stack object index");
4087d523365SDimitry Andric const FrameIndexOperand &StackObject = StackObjectInfo->second;
4092cab237bSDimitry Andric if (StackObject.IsFixed) {
410d88c1a5aSDimitry Andric YMF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
4112cab237bSDimitry Andric YMF.FixedStackObjects[StackObject.ID].CalleeSavedRestored =
4122cab237bSDimitry Andric CSInfo.isRestored();
4132cab237bSDimitry Andric } else {
414d88c1a5aSDimitry Andric YMF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
4152cab237bSDimitry Andric YMF.StackObjects[StackObject.ID].CalleeSavedRestored =
4162cab237bSDimitry Andric CSInfo.isRestored();
4172cab237bSDimitry Andric }
4187d523365SDimitry Andric }
419*b5893f02SDimitry Andric }
4207d523365SDimitry Andric for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
4217d523365SDimitry Andric auto LocalObject = MFI.getLocalFrameObjectMap(I);
4227d523365SDimitry Andric auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
4237d523365SDimitry Andric assert(StackObjectInfo != StackObjectOperandMapping.end() &&
4247d523365SDimitry Andric "Invalid stack object index");
4257d523365SDimitry Andric const FrameIndexOperand &StackObject = StackObjectInfo->second;
4267d523365SDimitry Andric assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
427d88c1a5aSDimitry Andric YMF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
4287d523365SDimitry Andric }
4297d523365SDimitry Andric
4307d523365SDimitry Andric // Print the stack object references in the frame information class after
4317d523365SDimitry Andric // converting the stack objects.
4327d523365SDimitry Andric if (MFI.hasStackProtectorIndex()) {
433d88c1a5aSDimitry Andric raw_string_ostream StrOS(YMF.FrameInfo.StackProtector.Value);
4347d523365SDimitry Andric MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
4357d523365SDimitry Andric .printStackObjectReference(MFI.getStackProtectorIndex());
4367d523365SDimitry Andric }
4377d523365SDimitry Andric
4387d523365SDimitry Andric // Print the debug variable information.
439d88c1a5aSDimitry Andric for (const MachineFunction::VariableDbgInfo &DebugVar :
440d88c1a5aSDimitry Andric MF.getVariableDbgInfo()) {
4417d523365SDimitry Andric auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
4427d523365SDimitry Andric assert(StackObjectInfo != StackObjectOperandMapping.end() &&
4437d523365SDimitry Andric "Invalid stack object index");
4447d523365SDimitry Andric const FrameIndexOperand &StackObject = StackObjectInfo->second;
4454ba319b5SDimitry Andric if (StackObject.IsFixed) {
4464ba319b5SDimitry Andric auto &Object = YMF.FixedStackObjects[StackObject.ID];
4474ba319b5SDimitry Andric printStackObjectDbgInfo(DebugVar, Object, MST);
4484ba319b5SDimitry Andric } else {
449d88c1a5aSDimitry Andric auto &Object = YMF.StackObjects[StackObject.ID];
4504ba319b5SDimitry Andric printStackObjectDbgInfo(DebugVar, Object, MST);
4517d523365SDimitry Andric }
4527d523365SDimitry Andric }
4537d523365SDimitry Andric }
4547d523365SDimitry Andric
convert(yaml::MachineFunction & MF,const MachineConstantPool & ConstantPool)4557d523365SDimitry Andric void MIRPrinter::convert(yaml::MachineFunction &MF,
4567d523365SDimitry Andric const MachineConstantPool &ConstantPool) {
4577d523365SDimitry Andric unsigned ID = 0;
4587d523365SDimitry Andric for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
4597d523365SDimitry Andric std::string Str;
4607d523365SDimitry Andric raw_string_ostream StrOS(Str);
4612cab237bSDimitry Andric if (Constant.isMachineConstantPoolEntry()) {
4622cab237bSDimitry Andric Constant.Val.MachineCPVal->print(StrOS);
4632cab237bSDimitry Andric } else {
4647d523365SDimitry Andric Constant.Val.ConstVal->printAsOperand(StrOS);
4652cab237bSDimitry Andric }
4662cab237bSDimitry Andric
4672cab237bSDimitry Andric yaml::MachineConstantPoolValue YamlConstant;
4687d523365SDimitry Andric YamlConstant.ID = ID++;
4697d523365SDimitry Andric YamlConstant.Value = StrOS.str();
4707d523365SDimitry Andric YamlConstant.Alignment = Constant.getAlignment();
4712cab237bSDimitry Andric YamlConstant.IsTargetSpecific = Constant.isMachineConstantPoolEntry();
4722cab237bSDimitry Andric
4737d523365SDimitry Andric MF.Constants.push_back(YamlConstant);
474875ed548SDimitry Andric }
475875ed548SDimitry Andric }
476875ed548SDimitry Andric
convert(ModuleSlotTracker & MST,yaml::MachineJumpTable & YamlJTI,const MachineJumpTableInfo & JTI)477875ed548SDimitry Andric void MIRPrinter::convert(ModuleSlotTracker &MST,
4787d523365SDimitry Andric yaml::MachineJumpTable &YamlJTI,
4797d523365SDimitry Andric const MachineJumpTableInfo &JTI) {
4807d523365SDimitry Andric YamlJTI.Kind = JTI.getEntryKind();
4817d523365SDimitry Andric unsigned ID = 0;
4827d523365SDimitry Andric for (const auto &Table : JTI.getJumpTables()) {
4833dac3a9bSDimitry Andric std::string Str;
4847d523365SDimitry Andric yaml::MachineJumpTable::Entry Entry;
4857d523365SDimitry Andric Entry.ID = ID++;
4867d523365SDimitry Andric for (const auto *MBB : Table.MBBs) {
4873dac3a9bSDimitry Andric raw_string_ostream StrOS(Str);
4882cab237bSDimitry Andric StrOS << printMBBReference(*MBB);
4897d523365SDimitry Andric Entry.Blocks.push_back(StrOS.str());
4903dac3a9bSDimitry Andric Str.clear();
4913dac3a9bSDimitry Andric }
4927d523365SDimitry Andric YamlJTI.Entries.push_back(Entry);
4937d523365SDimitry Andric }
4943dac3a9bSDimitry Andric }
4953dac3a9bSDimitry Andric
initRegisterMaskIds(const MachineFunction & MF)4963dac3a9bSDimitry Andric void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
4973dac3a9bSDimitry Andric const auto *TRI = MF.getSubtarget().getRegisterInfo();
4983dac3a9bSDimitry Andric unsigned I = 0;
4993dac3a9bSDimitry Andric for (const uint32_t *Mask : TRI->getRegMasks())
5003dac3a9bSDimitry Andric RegisterMaskIds.insert(std::make_pair(Mask, I++));
5013dac3a9bSDimitry Andric }
5023dac3a9bSDimitry Andric
guessSuccessors(const MachineBasicBlock & MBB,SmallVectorImpl<MachineBasicBlock * > & Result,bool & IsFallthrough)5030f5676f4SDimitry Andric void llvm::guessSuccessors(const MachineBasicBlock &MBB,
5040f5676f4SDimitry Andric SmallVectorImpl<MachineBasicBlock*> &Result,
5050f5676f4SDimitry Andric bool &IsFallthrough) {
5060f5676f4SDimitry Andric SmallPtrSet<MachineBasicBlock*,8> Seen;
5070f5676f4SDimitry Andric
5080f5676f4SDimitry Andric for (const MachineInstr &MI : MBB) {
5090f5676f4SDimitry Andric if (MI.isPHI())
5100f5676f4SDimitry Andric continue;
5110f5676f4SDimitry Andric for (const MachineOperand &MO : MI.operands()) {
5120f5676f4SDimitry Andric if (!MO.isMBB())
5130f5676f4SDimitry Andric continue;
5140f5676f4SDimitry Andric MachineBasicBlock *Succ = MO.getMBB();
5150f5676f4SDimitry Andric auto RP = Seen.insert(Succ);
5160f5676f4SDimitry Andric if (RP.second)
5170f5676f4SDimitry Andric Result.push_back(Succ);
5180f5676f4SDimitry Andric }
5190f5676f4SDimitry Andric }
5200f5676f4SDimitry Andric MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr();
5210f5676f4SDimitry Andric IsFallthrough = I == MBB.end() || !I->isBarrier();
5220f5676f4SDimitry Andric }
5230f5676f4SDimitry Andric
5240f5676f4SDimitry Andric bool
canPredictBranchProbabilities(const MachineBasicBlock & MBB) const5250f5676f4SDimitry Andric MIPrinter::canPredictBranchProbabilities(const MachineBasicBlock &MBB) const {
5260f5676f4SDimitry Andric if (MBB.succ_size() <= 1)
5270f5676f4SDimitry Andric return true;
5280f5676f4SDimitry Andric if (!MBB.hasSuccessorProbabilities())
5290f5676f4SDimitry Andric return true;
5300f5676f4SDimitry Andric
5310f5676f4SDimitry Andric SmallVector<BranchProbability,8> Normalized(MBB.Probs.begin(),
5320f5676f4SDimitry Andric MBB.Probs.end());
5330f5676f4SDimitry Andric BranchProbability::normalizeProbabilities(Normalized.begin(),
5340f5676f4SDimitry Andric Normalized.end());
5350f5676f4SDimitry Andric SmallVector<BranchProbability,8> Equal(Normalized.size());
5360f5676f4SDimitry Andric BranchProbability::normalizeProbabilities(Equal.begin(), Equal.end());
5370f5676f4SDimitry Andric
5380f5676f4SDimitry Andric return std::equal(Normalized.begin(), Normalized.end(), Equal.begin());
5390f5676f4SDimitry Andric }
5400f5676f4SDimitry Andric
canPredictSuccessors(const MachineBasicBlock & MBB) const5410f5676f4SDimitry Andric bool MIPrinter::canPredictSuccessors(const MachineBasicBlock &MBB) const {
5420f5676f4SDimitry Andric SmallVector<MachineBasicBlock*,8> GuessedSuccs;
5430f5676f4SDimitry Andric bool GuessedFallthrough;
5440f5676f4SDimitry Andric guessSuccessors(MBB, GuessedSuccs, GuessedFallthrough);
5450f5676f4SDimitry Andric if (GuessedFallthrough) {
5460f5676f4SDimitry Andric const MachineFunction &MF = *MBB.getParent();
5470f5676f4SDimitry Andric MachineFunction::const_iterator NextI = std::next(MBB.getIterator());
5480f5676f4SDimitry Andric if (NextI != MF.end()) {
5490f5676f4SDimitry Andric MachineBasicBlock *Next = const_cast<MachineBasicBlock*>(&*NextI);
5500f5676f4SDimitry Andric if (!is_contained(GuessedSuccs, Next))
5510f5676f4SDimitry Andric GuessedSuccs.push_back(Next);
5520f5676f4SDimitry Andric }
5530f5676f4SDimitry Andric }
5540f5676f4SDimitry Andric if (GuessedSuccs.size() != MBB.succ_size())
5550f5676f4SDimitry Andric return false;
5560f5676f4SDimitry Andric return std::equal(MBB.succ_begin(), MBB.succ_end(), GuessedSuccs.begin());
5570f5676f4SDimitry Andric }
5580f5676f4SDimitry Andric
print(const MachineBasicBlock & MBB)5597d523365SDimitry Andric void MIPrinter::print(const MachineBasicBlock &MBB) {
5607d523365SDimitry Andric assert(MBB.getNumber() >= 0 && "Invalid MBB number");
5617d523365SDimitry Andric OS << "bb." << MBB.getNumber();
5627d523365SDimitry Andric bool HasAttributes = false;
5637d523365SDimitry Andric if (const auto *BB = MBB.getBasicBlock()) {
5647d523365SDimitry Andric if (BB->hasName()) {
5657d523365SDimitry Andric OS << "." << BB->getName();
5667d523365SDimitry Andric } else {
5677d523365SDimitry Andric HasAttributes = true;
5687d523365SDimitry Andric OS << " (";
5697d523365SDimitry Andric int Slot = MST.getLocalSlot(BB);
5707d523365SDimitry Andric if (Slot == -1)
5717d523365SDimitry Andric OS << "<ir-block badref>";
5727d523365SDimitry Andric else
5737d523365SDimitry Andric OS << (Twine("%ir-block.") + Twine(Slot)).str();
5747d523365SDimitry Andric }
5757d523365SDimitry Andric }
5767d523365SDimitry Andric if (MBB.hasAddressTaken()) {
5777d523365SDimitry Andric OS << (HasAttributes ? ", " : " (");
5787d523365SDimitry Andric OS << "address-taken";
5797d523365SDimitry Andric HasAttributes = true;
5807d523365SDimitry Andric }
5817d523365SDimitry Andric if (MBB.isEHPad()) {
5827d523365SDimitry Andric OS << (HasAttributes ? ", " : " (");
5837d523365SDimitry Andric OS << "landing-pad";
5847d523365SDimitry Andric HasAttributes = true;
5857d523365SDimitry Andric }
5867d523365SDimitry Andric if (MBB.getAlignment()) {
5877d523365SDimitry Andric OS << (HasAttributes ? ", " : " (");
5887d523365SDimitry Andric OS << "align " << MBB.getAlignment();
5897d523365SDimitry Andric HasAttributes = true;
5907d523365SDimitry Andric }
5917d523365SDimitry Andric if (HasAttributes)
5927d523365SDimitry Andric OS << ")";
5937d523365SDimitry Andric OS << ":\n";
5947d523365SDimitry Andric
5957d523365SDimitry Andric bool HasLineAttributes = false;
5967d523365SDimitry Andric // Print the successors
5970f5676f4SDimitry Andric bool canPredictProbs = canPredictBranchProbabilities(MBB);
5982cab237bSDimitry Andric // Even if the list of successors is empty, if we cannot guess it,
5992cab237bSDimitry Andric // we need to print it to tell the parser that the list is empty.
6002cab237bSDimitry Andric // This is needed, because MI model unreachable as empty blocks
6012cab237bSDimitry Andric // with an empty successor list. If the parser would see that
6022cab237bSDimitry Andric // without the successor list, it would guess the code would
6032cab237bSDimitry Andric // fallthrough.
6042cab237bSDimitry Andric if ((!MBB.succ_empty() && !SimplifyMIR) || !canPredictProbs ||
6052cab237bSDimitry Andric !canPredictSuccessors(MBB)) {
6067d523365SDimitry Andric OS.indent(2) << "successors: ";
6077d523365SDimitry Andric for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
6087d523365SDimitry Andric if (I != MBB.succ_begin())
6097d523365SDimitry Andric OS << ", ";
6102cab237bSDimitry Andric OS << printMBBReference(**I);
6110f5676f4SDimitry Andric if (!SimplifyMIR || !canPredictProbs)
612d88c1a5aSDimitry Andric OS << '('
613d88c1a5aSDimitry Andric << format("0x%08" PRIx32, MBB.getSuccProbability(I).getNumerator())
614d88c1a5aSDimitry Andric << ')';
6157d523365SDimitry Andric }
6167d523365SDimitry Andric OS << "\n";
6177d523365SDimitry Andric HasLineAttributes = true;
6187d523365SDimitry Andric }
6197d523365SDimitry Andric
6207d523365SDimitry Andric // Print the live in registers.
62195ec533aSDimitry Andric const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
62295ec533aSDimitry Andric if (MRI.tracksLiveness() && !MBB.livein_empty()) {
62395ec533aSDimitry Andric const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
6247d523365SDimitry Andric OS.indent(2) << "liveins: ";
6257d523365SDimitry Andric bool First = true;
6267d523365SDimitry Andric for (const auto &LI : MBB.liveins()) {
6277d523365SDimitry Andric if (!First)
6287d523365SDimitry Andric OS << ", ";
6297d523365SDimitry Andric First = false;
6302cab237bSDimitry Andric OS << printReg(LI.PhysReg, &TRI);
631d88c1a5aSDimitry Andric if (!LI.LaneMask.all())
632d88c1a5aSDimitry Andric OS << ":0x" << PrintLaneMask(LI.LaneMask);
6337d523365SDimitry Andric }
6347d523365SDimitry Andric OS << "\n";
6357d523365SDimitry Andric HasLineAttributes = true;
6367d523365SDimitry Andric }
6377d523365SDimitry Andric
6387d523365SDimitry Andric if (HasLineAttributes)
6397d523365SDimitry Andric OS << "\n";
6407d523365SDimitry Andric bool IsInBundle = false;
6417d523365SDimitry Andric for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
6427d523365SDimitry Andric const MachineInstr &MI = *I;
6437d523365SDimitry Andric if (IsInBundle && !MI.isInsideBundle()) {
6447d523365SDimitry Andric OS.indent(2) << "}\n";
6457d523365SDimitry Andric IsInBundle = false;
6467d523365SDimitry Andric }
6477d523365SDimitry Andric OS.indent(IsInBundle ? 4 : 2);
6487d523365SDimitry Andric print(MI);
6497d523365SDimitry Andric if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
6507d523365SDimitry Andric OS << " {";
6517d523365SDimitry Andric IsInBundle = true;
6527d523365SDimitry Andric }
6537d523365SDimitry Andric OS << "\n";
6547d523365SDimitry Andric }
6557d523365SDimitry Andric if (IsInBundle)
6567d523365SDimitry Andric OS.indent(2) << "}\n";
6577d523365SDimitry Andric }
6587d523365SDimitry Andric
print(const MachineInstr & MI)6593dac3a9bSDimitry Andric void MIPrinter::print(const MachineInstr &MI) {
6602cab237bSDimitry Andric const auto *MF = MI.getMF();
6613ca95b02SDimitry Andric const auto &MRI = MF->getRegInfo();
6623ca95b02SDimitry Andric const auto &SubTarget = MF->getSubtarget();
6633dac3a9bSDimitry Andric const auto *TRI = SubTarget.getRegisterInfo();
6643dac3a9bSDimitry Andric assert(TRI && "Expected target register info");
6653dac3a9bSDimitry Andric const auto *TII = SubTarget.getInstrInfo();
6663dac3a9bSDimitry Andric assert(TII && "Expected target instruction info");
6677d523365SDimitry Andric if (MI.isCFIInstruction())
6687d523365SDimitry Andric assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
6693dac3a9bSDimitry Andric
670d88c1a5aSDimitry Andric SmallBitVector PrintedTypes(8);
6712cab237bSDimitry Andric bool ShouldPrintRegisterTies = MI.hasComplexRegisterTies();
6723dac3a9bSDimitry Andric unsigned I = 0, E = MI.getNumOperands();
6733dac3a9bSDimitry Andric for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
6743dac3a9bSDimitry Andric !MI.getOperand(I).isImplicit();
6753dac3a9bSDimitry Andric ++I) {
6763dac3a9bSDimitry Andric if (I)
6773dac3a9bSDimitry Andric OS << ", ";
6782cab237bSDimitry Andric print(MI, I, TRI, ShouldPrintRegisterTies,
6792cab237bSDimitry Andric MI.getTypeToPrint(I, PrintedTypes, MRI),
6802cab237bSDimitry Andric /*PrintDef=*/false);
6813dac3a9bSDimitry Andric }
6823dac3a9bSDimitry Andric
6833dac3a9bSDimitry Andric if (I)
6843dac3a9bSDimitry Andric OS << " = ";
6857d523365SDimitry Andric if (MI.getFlag(MachineInstr::FrameSetup))
6867d523365SDimitry Andric OS << "frame-setup ";
6874ba319b5SDimitry Andric if (MI.getFlag(MachineInstr::FrameDestroy))
6884ba319b5SDimitry Andric OS << "frame-destroy ";
6894ba319b5SDimitry Andric if (MI.getFlag(MachineInstr::FmNoNans))
6904ba319b5SDimitry Andric OS << "nnan ";
6914ba319b5SDimitry Andric if (MI.getFlag(MachineInstr::FmNoInfs))
6924ba319b5SDimitry Andric OS << "ninf ";
6934ba319b5SDimitry Andric if (MI.getFlag(MachineInstr::FmNsz))
6944ba319b5SDimitry Andric OS << "nsz ";
6954ba319b5SDimitry Andric if (MI.getFlag(MachineInstr::FmArcp))
6964ba319b5SDimitry Andric OS << "arcp ";
6974ba319b5SDimitry Andric if (MI.getFlag(MachineInstr::FmContract))
6984ba319b5SDimitry Andric OS << "contract ";
6994ba319b5SDimitry Andric if (MI.getFlag(MachineInstr::FmAfn))
7004ba319b5SDimitry Andric OS << "afn ";
7014ba319b5SDimitry Andric if (MI.getFlag(MachineInstr::FmReassoc))
7024ba319b5SDimitry Andric OS << "reassoc ";
703*b5893f02SDimitry Andric if (MI.getFlag(MachineInstr::NoUWrap))
704*b5893f02SDimitry Andric OS << "nuw ";
705*b5893f02SDimitry Andric if (MI.getFlag(MachineInstr::NoSWrap))
706*b5893f02SDimitry Andric OS << "nsw ";
707*b5893f02SDimitry Andric if (MI.getFlag(MachineInstr::IsExact))
708*b5893f02SDimitry Andric OS << "exact ";
7094ba319b5SDimitry Andric
7103dac3a9bSDimitry Andric OS << TII->getName(MI.getOpcode());
7113dac3a9bSDimitry Andric if (I < E)
7123dac3a9bSDimitry Andric OS << ' ';
7133dac3a9bSDimitry Andric
7143dac3a9bSDimitry Andric bool NeedComma = false;
7153dac3a9bSDimitry Andric for (; I < E; ++I) {
7163dac3a9bSDimitry Andric if (NeedComma)
7173dac3a9bSDimitry Andric OS << ", ";
7182cab237bSDimitry Andric print(MI, I, TRI, ShouldPrintRegisterTies,
7192cab237bSDimitry Andric MI.getTypeToPrint(I, PrintedTypes, MRI));
7203dac3a9bSDimitry Andric NeedComma = true;
7213dac3a9bSDimitry Andric }
7227d523365SDimitry Andric
723*b5893f02SDimitry Andric // Print any optional symbols attached to this instruction as-if they were
724*b5893f02SDimitry Andric // operands.
725*b5893f02SDimitry Andric if (MCSymbol *PreInstrSymbol = MI.getPreInstrSymbol()) {
726*b5893f02SDimitry Andric if (NeedComma)
727*b5893f02SDimitry Andric OS << ',';
728*b5893f02SDimitry Andric OS << " pre-instr-symbol ";
729*b5893f02SDimitry Andric MachineOperand::printSymbol(OS, *PreInstrSymbol);
730*b5893f02SDimitry Andric NeedComma = true;
731*b5893f02SDimitry Andric }
732*b5893f02SDimitry Andric if (MCSymbol *PostInstrSymbol = MI.getPostInstrSymbol()) {
733*b5893f02SDimitry Andric if (NeedComma)
734*b5893f02SDimitry Andric OS << ',';
735*b5893f02SDimitry Andric OS << " post-instr-symbol ";
736*b5893f02SDimitry Andric MachineOperand::printSymbol(OS, *PostInstrSymbol);
737*b5893f02SDimitry Andric NeedComma = true;
738*b5893f02SDimitry Andric }
739*b5893f02SDimitry Andric
7404ba319b5SDimitry Andric if (const DebugLoc &DL = MI.getDebugLoc()) {
7417d523365SDimitry Andric if (NeedComma)
7427d523365SDimitry Andric OS << ',';
7437d523365SDimitry Andric OS << " debug-location ";
7444ba319b5SDimitry Andric DL->printAsOperand(OS, MST);
7457d523365SDimitry Andric }
7467d523365SDimitry Andric
7477d523365SDimitry Andric if (!MI.memoperands_empty()) {
7487d523365SDimitry Andric OS << " :: ";
7492cab237bSDimitry Andric const LLVMContext &Context = MF->getFunction().getContext();
7504ba319b5SDimitry Andric const MachineFrameInfo &MFI = MF->getFrameInfo();
7517d523365SDimitry Andric bool NeedComma = false;
7527d523365SDimitry Andric for (const auto *Op : MI.memoperands()) {
7537d523365SDimitry Andric if (NeedComma)
7547d523365SDimitry Andric OS << ", ";
7554ba319b5SDimitry Andric Op->print(OS, MST, SSNs, Context, &MFI, TII);
7567d523365SDimitry Andric NeedComma = true;
7577d523365SDimitry Andric }
7587d523365SDimitry Andric }
7593dac3a9bSDimitry Andric }
7603dac3a9bSDimitry Andric
printStackObjectReference(int FrameIndex)7617d523365SDimitry Andric void MIPrinter::printStackObjectReference(int FrameIndex) {
7627d523365SDimitry Andric auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
7637d523365SDimitry Andric assert(ObjectInfo != StackObjectOperandMapping.end() &&
7647d523365SDimitry Andric "Invalid frame index");
7657d523365SDimitry Andric const FrameIndexOperand &Operand = ObjectInfo->second;
7662cab237bSDimitry Andric MachineOperand::printStackObjectReference(OS, Operand.ID, Operand.IsFixed,
7672cab237bSDimitry Andric Operand.Name);
7687d523365SDimitry Andric }
7697d523365SDimitry Andric
print(const MachineInstr & MI,unsigned OpIdx,const TargetRegisterInfo * TRI,bool ShouldPrintRegisterTies,LLT TypeToPrint,bool PrintDef)7702cab237bSDimitry Andric void MIPrinter::print(const MachineInstr &MI, unsigned OpIdx,
7712cab237bSDimitry Andric const TargetRegisterInfo *TRI,
7722cab237bSDimitry Andric bool ShouldPrintRegisterTies, LLT TypeToPrint,
7732cab237bSDimitry Andric bool PrintDef) {
7742cab237bSDimitry Andric const MachineOperand &Op = MI.getOperand(OpIdx);
7753dac3a9bSDimitry Andric switch (Op.getType()) {
7763dac3a9bSDimitry Andric case MachineOperand::MO_Immediate:
7772cab237bSDimitry Andric if (MI.isOperandSubregIdx(OpIdx)) {
7782cab237bSDimitry Andric MachineOperand::printTargetFlags(OS, Op);
7794ba319b5SDimitry Andric MachineOperand::printSubRegIdx(OS, Op.getImm(), TRI);
7803dac3a9bSDimitry Andric break;
7812cab237bSDimitry Andric }
7822cab237bSDimitry Andric LLVM_FALLTHROUGH;
7832cab237bSDimitry Andric case MachineOperand::MO_Register:
7847d523365SDimitry Andric case MachineOperand::MO_CImmediate:
785da09e106SDimitry Andric case MachineOperand::MO_FPImmediate:
7862cab237bSDimitry Andric case MachineOperand::MO_MachineBasicBlock:
7872cab237bSDimitry Andric case MachineOperand::MO_ConstantPoolIndex:
7882cab237bSDimitry Andric case MachineOperand::MO_TargetIndex:
7892cab237bSDimitry Andric case MachineOperand::MO_JumpTableIndex:
7902cab237bSDimitry Andric case MachineOperand::MO_ExternalSymbol:
7912cab237bSDimitry Andric case MachineOperand::MO_GlobalAddress:
7922cab237bSDimitry Andric case MachineOperand::MO_RegisterLiveOut:
7932cab237bSDimitry Andric case MachineOperand::MO_Metadata:
794da09e106SDimitry Andric case MachineOperand::MO_MCSymbol:
795da09e106SDimitry Andric case MachineOperand::MO_CFIIndex:
796da09e106SDimitry Andric case MachineOperand::MO_IntrinsicID:
797da09e106SDimitry Andric case MachineOperand::MO_Predicate:
798da09e106SDimitry Andric case MachineOperand::MO_BlockAddress: {
7992cab237bSDimitry Andric unsigned TiedOperandIdx = 0;
8002cab237bSDimitry Andric if (ShouldPrintRegisterTies && Op.isReg() && Op.isTied() && !Op.isDef())
8012cab237bSDimitry Andric TiedOperandIdx = Op.getParent()->findTiedOperandIdx(OpIdx);
8022cab237bSDimitry Andric const TargetIntrinsicInfo *TII = MI.getMF()->getTarget().getIntrinsicInfo();
8034ba319b5SDimitry Andric Op.print(OS, MST, TypeToPrint, PrintDef, /*IsStandalone=*/false,
8044ba319b5SDimitry Andric ShouldPrintRegisterTies, TiedOperandIdx, TRI, TII);
8057d523365SDimitry Andric break;
8062cab237bSDimitry Andric }
8077d523365SDimitry Andric case MachineOperand::MO_FrameIndex:
8087d523365SDimitry Andric printStackObjectReference(Op.getIndex());
8097d523365SDimitry Andric break;
8103dac3a9bSDimitry Andric case MachineOperand::MO_RegisterMask: {
8113dac3a9bSDimitry Andric auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
8123dac3a9bSDimitry Andric if (RegMaskInfo != RegisterMaskIds.end())
8133dac3a9bSDimitry Andric OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
8143dac3a9bSDimitry Andric else
8157a7e6055SDimitry Andric printCustomRegMask(Op.getRegMask(), OS, TRI);
8163dac3a9bSDimitry Andric break;
8173dac3a9bSDimitry Andric }
8187d523365SDimitry Andric }
8197d523365SDimitry Andric }
8207d523365SDimitry Andric
printMIR(raw_ostream & OS,const Module & M)8218f0fd8f6SDimitry Andric void llvm::printMIR(raw_ostream &OS, const Module &M) {
8228f0fd8f6SDimitry Andric yaml::Output Out(OS);
8238f0fd8f6SDimitry Andric Out << const_cast<Module &>(M);
8248f0fd8f6SDimitry Andric }
8258f0fd8f6SDimitry Andric
printMIR(raw_ostream & OS,const MachineFunction & MF)8268f0fd8f6SDimitry Andric void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
8278f0fd8f6SDimitry Andric MIRPrinter Printer(OS);
8288f0fd8f6SDimitry Andric Printer.print(MF);
8298f0fd8f6SDimitry Andric }
830