1 //===- MIRPrinter.cpp - MIR serialization format printer ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the class that prints out the LLVM IR and machine
11 // functions using the MIR serialization format.
13 //===----------------------------------------------------------------------===//
15 #include "MIRPrinter.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/CodeGen/MachineConstantPool.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineMemOperand.h"
21 #include "llvm/CodeGen/MachineModuleInfo.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/MIRYamlMapping.h"
24 #include "llvm/IR/BasicBlock.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/Instructions.h"
27 #include "llvm/IR/IRPrintingPasses.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/ModuleSlotTracker.h"
30 #include "llvm/Support/MemoryBuffer.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "llvm/Support/YAMLTraits.h"
33 #include "llvm/Target/TargetInstrInfo.h"
34 #include "llvm/Target/TargetSubtargetInfo.h"
40 /// This structure describes how to print out stack object references.
41 struct FrameIndexOperand {
46 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
47 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
49 /// Return an ordinary stack object reference.
50 static FrameIndexOperand create(StringRef Name, unsigned ID) {
51 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
54 /// Return a fixed stack object reference.
55 static FrameIndexOperand createFixed(unsigned ID) {
56 return FrameIndexOperand("", ID, /*IsFixed=*/true);
60 } // end anonymous namespace
64 /// This class prints out the machine functions using the MIR serialization
68 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
69 /// Maps from stack object indices to operand indices which will be used when
70 /// printing frame index machine operands.
71 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
74 MIRPrinter(raw_ostream &OS) : OS(OS) {}
76 void print(const MachineFunction &MF);
78 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
79 const TargetRegisterInfo *TRI);
80 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
81 const MachineFrameInfo &MFI);
82 void convert(yaml::MachineFunction &MF,
83 const MachineConstantPool &ConstantPool);
84 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
85 const MachineJumpTableInfo &JTI);
86 void convertStackObjects(yaml::MachineFunction &MF,
87 const MachineFrameInfo &MFI,
88 const TargetRegisterInfo *TRI);
91 void initRegisterMaskIds(const MachineFunction &MF);
94 /// This class prints out the machine instructions using the MIR serialization
98 ModuleSlotTracker &MST;
99 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
100 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
103 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
104 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
105 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
106 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
107 StackObjectOperandMapping(StackObjectOperandMapping) {}
109 void print(const MachineBasicBlock &MBB);
111 void print(const MachineInstr &MI);
112 void printMBBReference(const MachineBasicBlock &MBB);
113 void printIRBlockReference(const BasicBlock &BB);
114 void printIRValueReference(const Value &V);
115 void printStackObjectReference(int FrameIndex);
116 void printOffset(int64_t Offset);
117 void printTargetFlags(const MachineOperand &Op);
118 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
119 void print(const MachineMemOperand &Op);
121 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
124 } // end namespace llvm
129 /// This struct serializes the LLVM IR module.
130 template <> struct BlockScalarTraits<Module> {
131 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
132 Mod.print(OS, nullptr);
134 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
135 llvm_unreachable("LLVM Module is supposed to be parsed separately");
140 } // end namespace yaml
141 } // end namespace llvm
143 static void printReg(unsigned Reg, raw_ostream &OS,
144 const TargetRegisterInfo *TRI) {
145 // TODO: Print Stack Slots.
148 else if (TargetRegisterInfo::isVirtualRegister(Reg))
149 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
150 else if (Reg < TRI->getNumRegs())
151 OS << '%' << StringRef(TRI->getName(Reg)).lower();
153 llvm_unreachable("Can't print this kind of register yet");
156 static void printReg(unsigned Reg, yaml::StringValue &Dest,
157 const TargetRegisterInfo *TRI) {
158 raw_string_ostream OS(Dest.Value);
159 printReg(Reg, OS, TRI);
162 void MIRPrinter::print(const MachineFunction &MF) {
163 initRegisterMaskIds(MF);
165 yaml::MachineFunction YamlMF;
166 YamlMF.Name = MF.getName();
167 YamlMF.Alignment = MF.getAlignment();
168 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
169 YamlMF.HasInlineAsm = MF.hasInlineAsm();
170 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
171 ModuleSlotTracker MST(MF.getFunction()->getParent());
172 MST.incorporateFunction(*MF.getFunction());
173 convert(MST, YamlMF.FrameInfo, *MF.getFrameInfo());
174 convertStackObjects(YamlMF, *MF.getFrameInfo(),
175 MF.getSubtarget().getRegisterInfo());
176 if (const auto *ConstantPool = MF.getConstantPool())
177 convert(YamlMF, *ConstantPool);
178 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
179 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
180 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
181 bool IsNewlineNeeded = false;
182 for (const auto &MBB : MF) {
185 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
187 IsNewlineNeeded = true;
190 yaml::Output Out(OS);
194 void MIRPrinter::convert(yaml::MachineFunction &MF,
195 const MachineRegisterInfo &RegInfo,
196 const TargetRegisterInfo *TRI) {
197 MF.IsSSA = RegInfo.isSSA();
198 MF.TracksRegLiveness = RegInfo.tracksLiveness();
199 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
201 // Print the virtual register definitions.
202 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
203 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
204 yaml::VirtualRegisterDefinition VReg;
207 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
208 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
210 printReg(PreferredReg, VReg.PreferredRegister, TRI);
211 MF.VirtualRegisters.push_back(VReg);
214 // Print the live ins.
215 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
216 yaml::MachineFunctionLiveIn LiveIn;
217 printReg(I->first, LiveIn.Register, TRI);
219 printReg(I->second, LiveIn.VirtualRegister, TRI);
220 MF.LiveIns.push_back(LiveIn);
222 // The used physical register mask is printed as an inverted callee saved
224 const BitVector &UsedPhysRegMask = RegInfo.getUsedPhysRegsMask();
225 if (UsedPhysRegMask.none())
227 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
228 for (unsigned I = 0, E = UsedPhysRegMask.size(); I != E; ++I) {
229 if (!UsedPhysRegMask[I]) {
230 yaml::FlowStringValue Reg;
231 printReg(I, Reg, TRI);
232 CalleeSavedRegisters.push_back(Reg);
235 MF.CalleeSavedRegisters = CalleeSavedRegisters;
238 void MIRPrinter::convert(ModuleSlotTracker &MST,
239 yaml::MachineFrameInfo &YamlMFI,
240 const MachineFrameInfo &MFI) {
241 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
242 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
243 YamlMFI.HasStackMap = MFI.hasStackMap();
244 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
245 YamlMFI.StackSize = MFI.getStackSize();
246 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
247 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
248 YamlMFI.AdjustsStack = MFI.adjustsStack();
249 YamlMFI.HasCalls = MFI.hasCalls();
250 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
251 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
252 YamlMFI.HasVAStart = MFI.hasVAStart();
253 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
254 if (MFI.getSavePoint()) {
255 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
256 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
257 .printMBBReference(*MFI.getSavePoint());
259 if (MFI.getRestorePoint()) {
260 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
261 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
262 .printMBBReference(*MFI.getRestorePoint());
266 void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
267 const MachineFrameInfo &MFI,
268 const TargetRegisterInfo *TRI) {
269 // Process fixed stack objects.
271 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
272 if (MFI.isDeadObjectIndex(I))
275 yaml::FixedMachineStackObject YamlObject;
277 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
278 ? yaml::FixedMachineStackObject::SpillSlot
279 : yaml::FixedMachineStackObject::DefaultType;
280 YamlObject.Offset = MFI.getObjectOffset(I);
281 YamlObject.Size = MFI.getObjectSize(I);
282 YamlObject.Alignment = MFI.getObjectAlignment(I);
283 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
284 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
285 MF.FixedStackObjects.push_back(YamlObject);
286 StackObjectOperandMapping.insert(
287 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
290 // Process ordinary stack objects.
292 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
293 if (MFI.isDeadObjectIndex(I))
296 yaml::MachineStackObject YamlObject;
298 if (const auto *Alloca = MFI.getObjectAllocation(I))
299 YamlObject.Name.Value =
300 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
301 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
302 ? yaml::MachineStackObject::SpillSlot
303 : MFI.isVariableSizedObjectIndex(I)
304 ? yaml::MachineStackObject::VariableSized
305 : yaml::MachineStackObject::DefaultType;
306 YamlObject.Offset = MFI.getObjectOffset(I);
307 YamlObject.Size = MFI.getObjectSize(I);
308 YamlObject.Alignment = MFI.getObjectAlignment(I);
310 MF.StackObjects.push_back(YamlObject);
311 StackObjectOperandMapping.insert(std::make_pair(
312 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
315 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
316 yaml::StringValue Reg;
317 printReg(CSInfo.getReg(), Reg, TRI);
318 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
319 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
320 "Invalid stack object index");
321 const FrameIndexOperand &StackObject = StackObjectInfo->second;
322 if (StackObject.IsFixed)
323 MF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
325 MF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
327 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
328 auto LocalObject = MFI.getLocalFrameObjectMap(I);
329 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
330 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
331 "Invalid stack object index");
332 const FrameIndexOperand &StackObject = StackObjectInfo->second;
333 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
334 MF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
338 void MIRPrinter::convert(yaml::MachineFunction &MF,
339 const MachineConstantPool &ConstantPool) {
341 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
342 // TODO: Serialize target specific constant pool entries.
343 if (Constant.isMachineConstantPoolEntry())
344 llvm_unreachable("Can't print target specific constant pool entries yet");
346 yaml::MachineConstantPoolValue YamlConstant;
348 raw_string_ostream StrOS(Str);
349 Constant.Val.ConstVal->printAsOperand(StrOS);
350 YamlConstant.ID = ID++;
351 YamlConstant.Value = StrOS.str();
352 YamlConstant.Alignment = Constant.getAlignment();
353 MF.Constants.push_back(YamlConstant);
357 void MIRPrinter::convert(ModuleSlotTracker &MST,
358 yaml::MachineJumpTable &YamlJTI,
359 const MachineJumpTableInfo &JTI) {
360 YamlJTI.Kind = JTI.getEntryKind();
362 for (const auto &Table : JTI.getJumpTables()) {
364 yaml::MachineJumpTable::Entry Entry;
366 for (const auto *MBB : Table.MBBs) {
367 raw_string_ostream StrOS(Str);
368 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
369 .printMBBReference(*MBB);
370 Entry.Blocks.push_back(StrOS.str());
373 YamlJTI.Entries.push_back(Entry);
377 void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
378 const auto *TRI = MF.getSubtarget().getRegisterInfo();
380 for (const uint32_t *Mask : TRI->getRegMasks())
381 RegisterMaskIds.insert(std::make_pair(Mask, I++));
384 void MIPrinter::print(const MachineBasicBlock &MBB) {
385 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
386 OS << "bb." << MBB.getNumber();
387 bool HasAttributes = false;
388 if (const auto *BB = MBB.getBasicBlock()) {
390 OS << "." << BB->getName();
392 HasAttributes = true;
394 int Slot = MST.getLocalSlot(BB);
396 OS << "<ir-block badref>";
398 OS << (Twine("%ir-block.") + Twine(Slot)).str();
401 if (MBB.hasAddressTaken()) {
402 OS << (HasAttributes ? ", " : " (");
403 OS << "address-taken";
404 HasAttributes = true;
406 if (MBB.isLandingPad()) {
407 OS << (HasAttributes ? ", " : " (");
409 HasAttributes = true;
411 if (MBB.getAlignment()) {
412 OS << (HasAttributes ? ", " : " (");
413 OS << "align " << MBB.getAlignment();
414 HasAttributes = true;
420 bool HasLineAttributes = false;
421 // Print the successors
422 if (!MBB.succ_empty()) {
423 OS.indent(2) << "successors: ";
424 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
425 if (I != MBB.succ_begin())
427 printMBBReference(**I);
428 if (MBB.hasSuccessorWeights())
429 OS << '(' << MBB.getSuccWeight(I) << ')';
432 HasLineAttributes = true;
435 // Print the live in registers.
436 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
437 assert(TRI && "Expected target register info");
438 if (!MBB.livein_empty()) {
439 OS.indent(2) << "liveins: ";
440 for (auto I = MBB.livein_begin(), E = MBB.livein_end(); I != E; ++I) {
441 if (I != MBB.livein_begin())
443 printReg(*I, OS, TRI);
446 HasLineAttributes = true;
449 if (HasLineAttributes)
451 bool IsInBundle = false;
452 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
453 const MachineInstr &MI = *I;
454 if (IsInBundle && !MI.isInsideBundle()) {
455 OS.indent(2) << "}\n";
458 OS.indent(IsInBundle ? 4 : 2);
460 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
467 OS.indent(2) << "}\n";
470 void MIPrinter::print(const MachineInstr &MI) {
471 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
472 const auto *TRI = SubTarget.getRegisterInfo();
473 assert(TRI && "Expected target register info");
474 const auto *TII = SubTarget.getInstrInfo();
475 assert(TII && "Expected target instruction info");
476 if (MI.isCFIInstruction())
477 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
479 unsigned I = 0, E = MI.getNumOperands();
480 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
481 !MI.getOperand(I).isImplicit();
485 print(MI.getOperand(I), TRI);
490 if (MI.getFlag(MachineInstr::FrameSetup))
491 OS << "frame-setup ";
492 OS << TII->getName(MI.getOpcode());
496 bool NeedComma = false;
500 print(MI.getOperand(I), TRI);
504 if (MI.getDebugLoc()) {
507 OS << " debug-location ";
508 MI.getDebugLoc()->printAsOperand(OS, MST);
511 if (!MI.memoperands_empty()) {
513 bool NeedComma = false;
514 for (const auto *Op : MI.memoperands()) {
523 void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
524 OS << "%bb." << MBB.getNumber();
525 if (const auto *BB = MBB.getBasicBlock()) {
527 OS << '.' << BB->getName();
531 void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
534 printLLVMNameWithoutPrefix(OS, BB.getName());
537 const Function *F = BB.getParent();
539 if (F == MST.getCurrentFunction()) {
540 Slot = MST.getLocalSlot(&BB);
542 ModuleSlotTracker CustomMST(F->getParent(),
543 /*ShouldInitializeAllMetadata=*/false);
544 CustomMST.incorporateFunction(*F);
545 Slot = CustomMST.getLocalSlot(&BB);
553 void MIPrinter::printIRValueReference(const Value &V) {
556 printLLVMNameWithoutPrefix(OS, V.getName());
559 // TODO: Serialize the unnamed IR value references.
560 OS << "<unserializable ir value>";
563 void MIPrinter::printStackObjectReference(int FrameIndex) {
564 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
565 assert(ObjectInfo != StackObjectOperandMapping.end() &&
566 "Invalid frame index");
567 const FrameIndexOperand &Operand = ObjectInfo->second;
568 if (Operand.IsFixed) {
569 OS << "%fixed-stack." << Operand.ID;
572 OS << "%stack." << Operand.ID;
573 if (!Operand.Name.empty())
574 OS << '.' << Operand.Name;
577 void MIPrinter::printOffset(int64_t Offset) {
581 OS << " - " << -Offset;
584 OS << " + " << Offset;
587 static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
588 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
589 for (const auto &I : Flags) {
597 void MIPrinter::printTargetFlags(const MachineOperand &Op) {
598 if (!Op.getTargetFlags())
601 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
602 assert(TII && "expected instruction info");
603 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
604 OS << "target-flags(";
605 if (const auto *Name = getTargetFlagName(TII, Flags.first))
608 OS << "<unknown target flag>";
609 // TODO: Print the target's bit flags.
613 static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
614 const auto *TII = MF.getSubtarget().getInstrInfo();
615 assert(TII && "expected instruction info");
616 auto Indices = TII->getSerializableTargetIndices();
617 for (const auto &I : Indices) {
618 if (I.first == Index) {
625 void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
626 printTargetFlags(Op);
627 switch (Op.getType()) {
628 case MachineOperand::MO_Register:
629 // FIXME: Serialize the tied register.
631 OS << (Op.isDef() ? "implicit-def " : "implicit ");
632 if (Op.isInternalRead())
640 if (Op.isEarlyClobber())
641 OS << "early-clobber ";
644 printReg(Op.getReg(), OS, TRI);
645 // Print the sub register.
646 if (Op.getSubReg() != 0)
647 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
649 case MachineOperand::MO_Immediate:
652 case MachineOperand::MO_CImmediate:
653 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
655 case MachineOperand::MO_FPImmediate:
656 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
658 case MachineOperand::MO_MachineBasicBlock:
659 printMBBReference(*Op.getMBB());
661 case MachineOperand::MO_FrameIndex:
662 printStackObjectReference(Op.getIndex());
664 case MachineOperand::MO_ConstantPoolIndex:
665 OS << "%const." << Op.getIndex();
666 printOffset(Op.getOffset());
668 case MachineOperand::MO_TargetIndex: {
669 OS << "target-index(";
670 if (const auto *Name = getTargetIndexName(
671 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
676 printOffset(Op.getOffset());
679 case MachineOperand::MO_JumpTableIndex:
680 OS << "%jump-table." << Op.getIndex();
682 case MachineOperand::MO_ExternalSymbol:
684 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
685 printOffset(Op.getOffset());
687 case MachineOperand::MO_GlobalAddress:
688 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
689 printOffset(Op.getOffset());
691 case MachineOperand::MO_BlockAddress:
692 OS << "blockaddress(";
693 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
696 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
698 printOffset(Op.getOffset());
700 case MachineOperand::MO_RegisterMask: {
701 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
702 if (RegMaskInfo != RegisterMaskIds.end())
703 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
705 llvm_unreachable("Can't print this machine register mask yet.");
708 case MachineOperand::MO_RegisterLiveOut: {
709 const uint32_t *RegMask = Op.getRegLiveOut();
711 bool IsCommaNeeded = false;
712 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
713 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
716 printReg(Reg, OS, TRI);
717 IsCommaNeeded = true;
723 case MachineOperand::MO_Metadata:
724 Op.getMetadata()->printAsOperand(OS, MST);
726 case MachineOperand::MO_CFIIndex: {
727 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
728 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
732 // TODO: Print the other machine operands.
733 llvm_unreachable("Can't print this machine operand at the moment");
737 void MIPrinter::print(const MachineMemOperand &Op) {
739 // TODO: Print operand's target specific flags.
742 if (Op.isNonTemporal())
743 OS << "non-temporal ";
744 if (Op.isInvariant())
749 assert(Op.isStore() && "Non load machine operand must be a store");
752 OS << Op.getSize() << (Op.isLoad() ? " from " : " into ");
753 if (const Value *Val = Op.getValue()) {
754 printIRValueReference(*Val);
756 const PseudoSourceValue *PVal = Op.getPseudoValue();
757 assert(PVal && "Expected a pseudo source value");
758 switch (PVal->kind()) {
759 case PseudoSourceValue::Stack:
762 case PseudoSourceValue::GOT:
765 case PseudoSourceValue::JumpTable:
768 case PseudoSourceValue::ConstantPool:
769 OS << "constant-pool";
771 case PseudoSourceValue::FixedStack:
772 printStackObjectReference(
773 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
775 case PseudoSourceValue::GlobalValueCallEntry:
776 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
777 OS, /*PrintType=*/false, MST);
779 case PseudoSourceValue::ExternalSymbolCallEntry:
781 printLLVMNameWithoutPrefix(
782 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
786 printOffset(Op.getOffset());
787 if (Op.getBaseAlignment() != Op.getSize())
788 OS << ", align " << Op.getBaseAlignment();
789 auto AAInfo = Op.getAAInfo();
792 AAInfo.TBAA->printAsOperand(OS, MST);
795 OS << ", !alias.scope ";
796 AAInfo.Scope->printAsOperand(OS, MST);
798 if (AAInfo.NoAlias) {
800 AAInfo.NoAlias->printAsOperand(OS, MST);
802 if (Op.getRanges()) {
804 Op.getRanges()->printAsOperand(OS, MST);
809 static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
810 const TargetRegisterInfo *TRI) {
811 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
816 printReg(Reg, OS, TRI);
819 void MIPrinter::print(const MCCFIInstruction &CFI,
820 const TargetRegisterInfo *TRI) {
821 switch (CFI.getOperation()) {
822 case MCCFIInstruction::OpSameValue:
823 OS << ".cfi_same_value ";
826 printCFIRegister(CFI.getRegister(), OS, TRI);
828 case MCCFIInstruction::OpOffset:
829 OS << ".cfi_offset ";
832 printCFIRegister(CFI.getRegister(), OS, TRI);
833 OS << ", " << CFI.getOffset();
835 case MCCFIInstruction::OpDefCfaRegister:
836 OS << ".cfi_def_cfa_register ";
839 printCFIRegister(CFI.getRegister(), OS, TRI);
841 case MCCFIInstruction::OpDefCfaOffset:
842 OS << ".cfi_def_cfa_offset ";
845 OS << CFI.getOffset();
847 case MCCFIInstruction::OpDefCfa:
848 OS << ".cfi_def_cfa ";
851 printCFIRegister(CFI.getRegister(), OS, TRI);
852 OS << ", " << CFI.getOffset();
855 // TODO: Print the other CFI Operations.
856 OS << "<unserializable cfi operation>";
861 void llvm::printMIR(raw_ostream &OS, const Module &M) {
862 yaml::Output Out(OS);
863 Out << const_cast<Module &>(M);
866 void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
867 MIRPrinter Printer(OS);