MIRPrinter.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. //===- MIRPrinter.cpp - MIR serialization format printer ------------------===//
  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. // This file implements the class that prints out the LLVM IR and machine
  11. // functions using the MIR serialization format.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "MIRPrinter.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/CodeGen/MachineFunction.h"
  17. #include "llvm/CodeGen/MachineFrameInfo.h"
  18. #include "llvm/CodeGen/MachineRegisterInfo.h"
  19. #include "llvm/CodeGen/MIRYamlMapping.h"
  20. #include "llvm/IR/BasicBlock.h"
  21. #include "llvm/IR/Module.h"
  22. #include "llvm/IR/ModuleSlotTracker.h"
  23. #include "llvm/Support/MemoryBuffer.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include "llvm/Support/YAMLTraits.h"
  26. #include "llvm/Target/TargetInstrInfo.h"
  27. #include "llvm/Target/TargetSubtargetInfo.h"
  28. using namespace llvm;
  29. namespace {
  30. /// This class prints out the machine functions using the MIR serialization
  31. /// format.
  32. class MIRPrinter {
  33. raw_ostream &OS;
  34. DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
  35. public:
  36. MIRPrinter(raw_ostream &OS) : OS(OS) {}
  37. void print(const MachineFunction &MF);
  38. void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
  39. const TargetRegisterInfo *TRI);
  40. void convert(yaml::MachineFrameInfo &YamlMFI, const MachineFrameInfo &MFI);
  41. void convert(ModuleSlotTracker &MST, yaml::MachineBasicBlock &YamlMBB,
  42. const MachineBasicBlock &MBB);
  43. void convertStackObjects(yaml::MachineFunction &MF,
  44. const MachineFrameInfo &MFI);
  45. private:
  46. void initRegisterMaskIds(const MachineFunction &MF);
  47. };
  48. /// This class prints out the machine instructions using the MIR serialization
  49. /// format.
  50. class MIPrinter {
  51. raw_ostream &OS;
  52. ModuleSlotTracker &MST;
  53. const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
  54. public:
  55. MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
  56. const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds)
  57. : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds) {}
  58. void print(const MachineInstr &MI);
  59. void printMBBReference(const MachineBasicBlock &MBB);
  60. void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
  61. };
  62. } // end anonymous namespace
  63. namespace llvm {
  64. namespace yaml {
  65. /// This struct serializes the LLVM IR module.
  66. template <> struct BlockScalarTraits<Module> {
  67. static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
  68. Mod.print(OS, nullptr);
  69. }
  70. static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
  71. llvm_unreachable("LLVM Module is supposed to be parsed separately");
  72. return "";
  73. }
  74. };
  75. } // end namespace yaml
  76. } // end namespace llvm
  77. static void printReg(unsigned Reg, raw_ostream &OS,
  78. const TargetRegisterInfo *TRI) {
  79. // TODO: Print Stack Slots.
  80. if (!Reg)
  81. OS << '_';
  82. else if (TargetRegisterInfo::isVirtualRegister(Reg))
  83. OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
  84. else if (Reg < TRI->getNumRegs())
  85. OS << '%' << StringRef(TRI->getName(Reg)).lower();
  86. else
  87. llvm_unreachable("Can't print this kind of register yet");
  88. }
  89. void MIRPrinter::print(const MachineFunction &MF) {
  90. initRegisterMaskIds(MF);
  91. yaml::MachineFunction YamlMF;
  92. YamlMF.Name = MF.getName();
  93. YamlMF.Alignment = MF.getAlignment();
  94. YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
  95. YamlMF.HasInlineAsm = MF.hasInlineAsm();
  96. convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
  97. convert(YamlMF.FrameInfo, *MF.getFrameInfo());
  98. convertStackObjects(YamlMF, *MF.getFrameInfo());
  99. int I = 0;
  100. ModuleSlotTracker MST(MF.getFunction()->getParent());
  101. for (const auto &MBB : MF) {
  102. // TODO: Allow printing of non sequentially numbered MBBs.
  103. // This is currently needed as the basic block references get their index
  104. // from MBB.getNumber(), thus it should be sequential so that the parser can
  105. // map back to the correct MBBs when parsing the output.
  106. assert(MBB.getNumber() == I++ &&
  107. "Can't print MBBs that aren't sequentially numbered");
  108. (void)I;
  109. yaml::MachineBasicBlock YamlMBB;
  110. convert(MST, YamlMBB, MBB);
  111. YamlMF.BasicBlocks.push_back(YamlMBB);
  112. }
  113. yaml::Output Out(OS);
  114. Out << YamlMF;
  115. }
  116. void MIRPrinter::convert(yaml::MachineFunction &MF,
  117. const MachineRegisterInfo &RegInfo,
  118. const TargetRegisterInfo *TRI) {
  119. MF.IsSSA = RegInfo.isSSA();
  120. MF.TracksRegLiveness = RegInfo.tracksLiveness();
  121. MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
  122. // Print the virtual register definitions.
  123. for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
  124. unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
  125. yaml::VirtualRegisterDefinition VReg;
  126. VReg.ID = I;
  127. VReg.Class =
  128. StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
  129. MF.VirtualRegisters.push_back(VReg);
  130. }
  131. }
  132. void MIRPrinter::convert(yaml::MachineFrameInfo &YamlMFI,
  133. const MachineFrameInfo &MFI) {
  134. YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
  135. YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
  136. YamlMFI.HasStackMap = MFI.hasStackMap();
  137. YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
  138. YamlMFI.StackSize = MFI.getStackSize();
  139. YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
  140. YamlMFI.MaxAlignment = MFI.getMaxAlignment();
  141. YamlMFI.AdjustsStack = MFI.adjustsStack();
  142. YamlMFI.HasCalls = MFI.hasCalls();
  143. YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
  144. YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
  145. YamlMFI.HasVAStart = MFI.hasVAStart();
  146. YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
  147. }
  148. void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
  149. const MachineFrameInfo &MFI) {
  150. // Process fixed stack objects.
  151. unsigned ID = 0;
  152. for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
  153. if (MFI.isDeadObjectIndex(I))
  154. continue;
  155. yaml::FixedMachineStackObject YamlObject;
  156. YamlObject.ID = ID++;
  157. YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
  158. ? yaml::FixedMachineStackObject::SpillSlot
  159. : yaml::FixedMachineStackObject::DefaultType;
  160. YamlObject.Offset = MFI.getObjectOffset(I);
  161. YamlObject.Size = MFI.getObjectSize(I);
  162. YamlObject.Alignment = MFI.getObjectAlignment(I);
  163. YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
  164. YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
  165. MF.FixedStackObjects.push_back(YamlObject);
  166. // TODO: Store the mapping between fixed object IDs and object indices to
  167. // print the fixed stack object references correctly.
  168. }
  169. // Process ordinary stack objects.
  170. ID = 0;
  171. for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
  172. if (MFI.isDeadObjectIndex(I))
  173. continue;
  174. yaml::MachineStackObject YamlObject;
  175. YamlObject.ID = ID++;
  176. YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
  177. ? yaml::MachineStackObject::SpillSlot
  178. : MFI.isVariableSizedObjectIndex(I)
  179. ? yaml::MachineStackObject::VariableSized
  180. : yaml::MachineStackObject::DefaultType;
  181. YamlObject.Offset = MFI.getObjectOffset(I);
  182. YamlObject.Size = MFI.getObjectSize(I);
  183. YamlObject.Alignment = MFI.getObjectAlignment(I);
  184. MF.StackObjects.push_back(YamlObject);
  185. // TODO: Store the mapping between object IDs and object indices to print
  186. // the stack object references correctly.
  187. }
  188. }
  189. void MIRPrinter::convert(ModuleSlotTracker &MST,
  190. yaml::MachineBasicBlock &YamlMBB,
  191. const MachineBasicBlock &MBB) {
  192. assert(MBB.getNumber() >= 0 && "Invalid MBB number");
  193. YamlMBB.ID = (unsigned)MBB.getNumber();
  194. // TODO: Serialize unnamed BB references.
  195. if (const auto *BB = MBB.getBasicBlock())
  196. YamlMBB.Name.Value = BB->hasName() ? BB->getName() : "<unnamed bb>";
  197. else
  198. YamlMBB.Name.Value = "";
  199. YamlMBB.Alignment = MBB.getAlignment();
  200. YamlMBB.AddressTaken = MBB.hasAddressTaken();
  201. YamlMBB.IsLandingPad = MBB.isLandingPad();
  202. for (const auto *SuccMBB : MBB.successors()) {
  203. std::string Str;
  204. raw_string_ostream StrOS(Str);
  205. MIPrinter(StrOS, MST, RegisterMaskIds).printMBBReference(*SuccMBB);
  206. YamlMBB.Successors.push_back(StrOS.str());
  207. }
  208. // Print the live in registers.
  209. const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
  210. assert(TRI && "Expected target register info");
  211. for (auto I = MBB.livein_begin(), E = MBB.livein_end(); I != E; ++I) {
  212. std::string Str;
  213. raw_string_ostream StrOS(Str);
  214. printReg(*I, StrOS, TRI);
  215. YamlMBB.LiveIns.push_back(StrOS.str());
  216. }
  217. // Print the machine instructions.
  218. YamlMBB.Instructions.reserve(MBB.size());
  219. std::string Str;
  220. for (const auto &MI : MBB) {
  221. raw_string_ostream StrOS(Str);
  222. MIPrinter(StrOS, MST, RegisterMaskIds).print(MI);
  223. YamlMBB.Instructions.push_back(StrOS.str());
  224. Str.clear();
  225. }
  226. }
  227. void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
  228. const auto *TRI = MF.getSubtarget().getRegisterInfo();
  229. unsigned I = 0;
  230. for (const uint32_t *Mask : TRI->getRegMasks())
  231. RegisterMaskIds.insert(std::make_pair(Mask, I++));
  232. }
  233. void MIPrinter::print(const MachineInstr &MI) {
  234. const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
  235. const auto *TRI = SubTarget.getRegisterInfo();
  236. assert(TRI && "Expected target register info");
  237. const auto *TII = SubTarget.getInstrInfo();
  238. assert(TII && "Expected target instruction info");
  239. unsigned I = 0, E = MI.getNumOperands();
  240. for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
  241. !MI.getOperand(I).isImplicit();
  242. ++I) {
  243. if (I)
  244. OS << ", ";
  245. print(MI.getOperand(I), TRI);
  246. }
  247. if (I)
  248. OS << " = ";
  249. OS << TII->getName(MI.getOpcode());
  250. // TODO: Print the instruction flags, machine mem operands.
  251. if (I < E)
  252. OS << ' ';
  253. bool NeedComma = false;
  254. for (; I < E; ++I) {
  255. if (NeedComma)
  256. OS << ", ";
  257. print(MI.getOperand(I), TRI);
  258. NeedComma = true;
  259. }
  260. }
  261. void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
  262. OS << "%bb." << MBB.getNumber();
  263. if (const auto *BB = MBB.getBasicBlock()) {
  264. if (BB->hasName())
  265. OS << '.' << BB->getName();
  266. }
  267. }
  268. void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
  269. switch (Op.getType()) {
  270. case MachineOperand::MO_Register:
  271. // TODO: Print the other register flags.
  272. if (Op.isImplicit())
  273. OS << (Op.isDef() ? "implicit-def " : "implicit ");
  274. if (Op.isDead())
  275. OS << "dead ";
  276. if (Op.isKill())
  277. OS << "killed ";
  278. if (Op.isUndef())
  279. OS << "undef ";
  280. printReg(Op.getReg(), OS, TRI);
  281. // Print the sub register.
  282. if (Op.getSubReg() != 0)
  283. OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
  284. break;
  285. case MachineOperand::MO_Immediate:
  286. OS << Op.getImm();
  287. break;
  288. case MachineOperand::MO_MachineBasicBlock:
  289. printMBBReference(*Op.getMBB());
  290. break;
  291. case MachineOperand::MO_GlobalAddress:
  292. Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
  293. // TODO: Print offset and target flags.
  294. break;
  295. case MachineOperand::MO_RegisterMask: {
  296. auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
  297. if (RegMaskInfo != RegisterMaskIds.end())
  298. OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
  299. else
  300. llvm_unreachable("Can't print this machine register mask yet.");
  301. break;
  302. }
  303. default:
  304. // TODO: Print the other machine operands.
  305. llvm_unreachable("Can't print this machine operand at the moment");
  306. }
  307. }
  308. void llvm::printMIR(raw_ostream &OS, const Module &M) {
  309. yaml::Output Out(OS);
  310. Out << const_cast<Module &>(M);
  311. }
  312. void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
  313. MIRPrinter Printer(OS);
  314. Printer.print(MF);
  315. }