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, MachineModuleInfo &MMI,
88 ModuleSlotTracker &MST,
89 const TargetRegisterInfo *TRI);
92 void initRegisterMaskIds(const MachineFunction &MF);
95 /// This class prints out the machine instructions using the MIR serialization
99 ModuleSlotTracker &MST;
100 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
101 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
104 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
105 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
106 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
107 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
108 StackObjectOperandMapping(StackObjectOperandMapping) {}
110 void print(const MachineBasicBlock &MBB);
112 void print(const MachineInstr &MI);
113 void printMBBReference(const MachineBasicBlock &MBB);
114 void printIRBlockReference(const BasicBlock &BB);
115 void printIRValueReference(const Value &V);
116 void printStackObjectReference(int FrameIndex);
117 void printOffset(int64_t Offset);
118 void printTargetFlags(const MachineOperand &Op);
119 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
121 void print(const MachineMemOperand &Op);
123 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
126 } // end namespace llvm
131 /// This struct serializes the LLVM IR module.
132 template <> struct BlockScalarTraits<Module> {
133 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
134 Mod.print(OS, nullptr);
136 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
137 llvm_unreachable("LLVM Module is supposed to be parsed separately");
142 } // end namespace yaml
143 } // end namespace llvm
145 static void printReg(unsigned Reg, raw_ostream &OS,
146 const TargetRegisterInfo *TRI) {
147 // TODO: Print Stack Slots.
150 else if (TargetRegisterInfo::isVirtualRegister(Reg))
151 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
152 else if (Reg < TRI->getNumRegs())
153 OS << '%' << StringRef(TRI->getName(Reg)).lower();
155 llvm_unreachable("Can't print this kind of register yet");
158 static void printReg(unsigned Reg, yaml::StringValue &Dest,
159 const TargetRegisterInfo *TRI) {
160 raw_string_ostream OS(Dest.Value);
161 printReg(Reg, OS, TRI);
164 void MIRPrinter::print(const MachineFunction &MF) {
165 initRegisterMaskIds(MF);
167 yaml::MachineFunction YamlMF;
168 YamlMF.Name = MF.getName();
169 YamlMF.Alignment = MF.getAlignment();
170 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
171 YamlMF.HasInlineAsm = MF.hasInlineAsm();
172 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
173 ModuleSlotTracker MST(MF.getFunction()->getParent());
174 MST.incorporateFunction(*MF.getFunction());
175 convert(MST, YamlMF.FrameInfo, *MF.getFrameInfo());
176 convertStackObjects(YamlMF, *MF.getFrameInfo(), MF.getMMI(), MST,
177 MF.getSubtarget().getRegisterInfo());
178 if (const auto *ConstantPool = MF.getConstantPool())
179 convert(YamlMF, *ConstantPool);
180 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
181 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
182 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
183 bool IsNewlineNeeded = false;
184 for (const auto &MBB : MF) {
187 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
189 IsNewlineNeeded = true;
192 yaml::Output Out(OS);
196 void MIRPrinter::convert(yaml::MachineFunction &MF,
197 const MachineRegisterInfo &RegInfo,
198 const TargetRegisterInfo *TRI) {
199 MF.IsSSA = RegInfo.isSSA();
200 MF.TracksRegLiveness = RegInfo.tracksLiveness();
201 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
203 // Print the virtual register definitions.
204 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
205 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
206 yaml::VirtualRegisterDefinition VReg;
209 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
210 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
212 printReg(PreferredReg, VReg.PreferredRegister, TRI);
213 MF.VirtualRegisters.push_back(VReg);
216 // Print the live ins.
217 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
218 yaml::MachineFunctionLiveIn LiveIn;
219 printReg(I->first, LiveIn.Register, TRI);
221 printReg(I->second, LiveIn.VirtualRegister, TRI);
222 MF.LiveIns.push_back(LiveIn);
224 // The used physical register mask is printed as an inverted callee saved
226 const BitVector &UsedPhysRegMask = RegInfo.getUsedPhysRegsMask();
227 if (UsedPhysRegMask.none())
229 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
230 for (unsigned I = 0, E = UsedPhysRegMask.size(); I != E; ++I) {
231 if (!UsedPhysRegMask[I]) {
232 yaml::FlowStringValue Reg;
233 printReg(I, Reg, TRI);
234 CalleeSavedRegisters.push_back(Reg);
237 MF.CalleeSavedRegisters = CalleeSavedRegisters;
240 void MIRPrinter::convert(ModuleSlotTracker &MST,
241 yaml::MachineFrameInfo &YamlMFI,
242 const MachineFrameInfo &MFI) {
243 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
244 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
245 YamlMFI.HasStackMap = MFI.hasStackMap();
246 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
247 YamlMFI.StackSize = MFI.getStackSize();
248 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
249 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
250 YamlMFI.AdjustsStack = MFI.adjustsStack();
251 YamlMFI.HasCalls = MFI.hasCalls();
252 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
253 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
254 YamlMFI.HasVAStart = MFI.hasVAStart();
255 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
256 if (MFI.getSavePoint()) {
257 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
258 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
259 .printMBBReference(*MFI.getSavePoint());
261 if (MFI.getRestorePoint()) {
262 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
263 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
264 .printMBBReference(*MFI.getRestorePoint());
268 void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
269 const MachineFrameInfo &MFI,
270 MachineModuleInfo &MMI,
271 ModuleSlotTracker &MST,
272 const TargetRegisterInfo *TRI) {
273 // Process fixed stack objects.
275 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
276 if (MFI.isDeadObjectIndex(I))
279 yaml::FixedMachineStackObject YamlObject;
281 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
282 ? yaml::FixedMachineStackObject::SpillSlot
283 : yaml::FixedMachineStackObject::DefaultType;
284 YamlObject.Offset = MFI.getObjectOffset(I);
285 YamlObject.Size = MFI.getObjectSize(I);
286 YamlObject.Alignment = MFI.getObjectAlignment(I);
287 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
288 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
289 MF.FixedStackObjects.push_back(YamlObject);
290 StackObjectOperandMapping.insert(
291 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
294 // Process ordinary stack objects.
296 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
297 if (MFI.isDeadObjectIndex(I))
300 yaml::MachineStackObject YamlObject;
302 if (const auto *Alloca = MFI.getObjectAllocation(I))
303 YamlObject.Name.Value =
304 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
305 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
306 ? yaml::MachineStackObject::SpillSlot
307 : MFI.isVariableSizedObjectIndex(I)
308 ? yaml::MachineStackObject::VariableSized
309 : yaml::MachineStackObject::DefaultType;
310 YamlObject.Offset = MFI.getObjectOffset(I);
311 YamlObject.Size = MFI.getObjectSize(I);
312 YamlObject.Alignment = MFI.getObjectAlignment(I);
314 MF.StackObjects.push_back(YamlObject);
315 StackObjectOperandMapping.insert(std::make_pair(
316 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
319 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
320 yaml::StringValue Reg;
321 printReg(CSInfo.getReg(), Reg, TRI);
322 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
323 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
324 "Invalid stack object index");
325 const FrameIndexOperand &StackObject = StackObjectInfo->second;
326 if (StackObject.IsFixed)
327 MF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
329 MF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
331 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
332 auto LocalObject = MFI.getLocalFrameObjectMap(I);
333 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
334 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
335 "Invalid stack object index");
336 const FrameIndexOperand &StackObject = StackObjectInfo->second;
337 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
338 MF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
341 // Print the stack object references in the frame information class after
342 // converting the stack objects.
343 if (MFI.hasStackProtectorIndex()) {
344 raw_string_ostream StrOS(MF.FrameInfo.StackProtector.Value);
345 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
346 .printStackObjectReference(MFI.getStackProtectorIndex());
349 // Print the debug variable information.
350 for (MachineModuleInfo::VariableDbgInfo &DebugVar :
351 MMI.getVariableDbgInfo()) {
352 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
353 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
354 "Invalid stack object index");
355 const FrameIndexOperand &StackObject = StackObjectInfo->second;
356 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
357 auto &Object = MF.StackObjects[StackObject.ID];
359 raw_string_ostream StrOS(Object.DebugVar.Value);
360 DebugVar.Var->printAsOperand(StrOS, MST);
363 raw_string_ostream StrOS(Object.DebugExpr.Value);
364 DebugVar.Expr->printAsOperand(StrOS, MST);
367 raw_string_ostream StrOS(Object.DebugLoc.Value);
368 DebugVar.Loc->printAsOperand(StrOS, MST);
373 void MIRPrinter::convert(yaml::MachineFunction &MF,
374 const MachineConstantPool &ConstantPool) {
376 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
377 // TODO: Serialize target specific constant pool entries.
378 if (Constant.isMachineConstantPoolEntry())
379 llvm_unreachable("Can't print target specific constant pool entries yet");
381 yaml::MachineConstantPoolValue YamlConstant;
383 raw_string_ostream StrOS(Str);
384 Constant.Val.ConstVal->printAsOperand(StrOS);
385 YamlConstant.ID = ID++;
386 YamlConstant.Value = StrOS.str();
387 YamlConstant.Alignment = Constant.getAlignment();
388 MF.Constants.push_back(YamlConstant);
392 void MIRPrinter::convert(ModuleSlotTracker &MST,
393 yaml::MachineJumpTable &YamlJTI,
394 const MachineJumpTableInfo &JTI) {
395 YamlJTI.Kind = JTI.getEntryKind();
397 for (const auto &Table : JTI.getJumpTables()) {
399 yaml::MachineJumpTable::Entry Entry;
401 for (const auto *MBB : Table.MBBs) {
402 raw_string_ostream StrOS(Str);
403 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
404 .printMBBReference(*MBB);
405 Entry.Blocks.push_back(StrOS.str());
408 YamlJTI.Entries.push_back(Entry);
412 void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
413 const auto *TRI = MF.getSubtarget().getRegisterInfo();
415 for (const uint32_t *Mask : TRI->getRegMasks())
416 RegisterMaskIds.insert(std::make_pair(Mask, I++));
419 void MIPrinter::print(const MachineBasicBlock &MBB) {
420 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
421 OS << "bb." << MBB.getNumber();
422 bool HasAttributes = false;
423 if (const auto *BB = MBB.getBasicBlock()) {
425 OS << "." << BB->getName();
427 HasAttributes = true;
429 int Slot = MST.getLocalSlot(BB);
431 OS << "<ir-block badref>";
433 OS << (Twine("%ir-block.") + Twine(Slot)).str();
436 if (MBB.hasAddressTaken()) {
437 OS << (HasAttributes ? ", " : " (");
438 OS << "address-taken";
439 HasAttributes = true;
441 if (MBB.isLandingPad()) {
442 OS << (HasAttributes ? ", " : " (");
444 HasAttributes = true;
446 if (MBB.getAlignment()) {
447 OS << (HasAttributes ? ", " : " (");
448 OS << "align " << MBB.getAlignment();
449 HasAttributes = true;
455 bool HasLineAttributes = false;
456 // Print the successors
457 if (!MBB.succ_empty()) {
458 OS.indent(2) << "successors: ";
459 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
460 if (I != MBB.succ_begin())
462 printMBBReference(**I);
463 if (MBB.hasSuccessorWeights())
464 OS << '(' << MBB.getSuccWeight(I) << ')';
467 HasLineAttributes = true;
470 // Print the live in registers.
471 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
472 assert(TRI && "Expected target register info");
473 if (!MBB.livein_empty()) {
474 OS.indent(2) << "liveins: ";
475 for (auto I = MBB.livein_begin(), E = MBB.livein_end(); I != E; ++I) {
476 if (I != MBB.livein_begin())
478 printReg(*I, OS, TRI);
481 HasLineAttributes = true;
484 if (HasLineAttributes)
486 bool IsInBundle = false;
487 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
488 const MachineInstr &MI = *I;
489 if (IsInBundle && !MI.isInsideBundle()) {
490 OS.indent(2) << "}\n";
493 OS.indent(IsInBundle ? 4 : 2);
495 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
502 OS.indent(2) << "}\n";
505 void MIPrinter::print(const MachineInstr &MI) {
506 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
507 const auto *TRI = SubTarget.getRegisterInfo();
508 assert(TRI && "Expected target register info");
509 const auto *TII = SubTarget.getInstrInfo();
510 assert(TII && "Expected target instruction info");
511 if (MI.isCFIInstruction())
512 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
514 unsigned I = 0, E = MI.getNumOperands();
515 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
516 !MI.getOperand(I).isImplicit();
520 print(MI.getOperand(I), TRI, /*IsDef=*/true);
525 if (MI.getFlag(MachineInstr::FrameSetup))
526 OS << "frame-setup ";
527 OS << TII->getName(MI.getOpcode());
531 bool NeedComma = false;
535 print(MI.getOperand(I), TRI);
539 if (MI.getDebugLoc()) {
542 OS << " debug-location ";
543 MI.getDebugLoc()->printAsOperand(OS, MST);
546 if (!MI.memoperands_empty()) {
548 bool NeedComma = false;
549 for (const auto *Op : MI.memoperands()) {
558 void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
559 OS << "%bb." << MBB.getNumber();
560 if (const auto *BB = MBB.getBasicBlock()) {
562 OS << '.' << BB->getName();
566 void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
569 printLLVMNameWithoutPrefix(OS, BB.getName());
572 const Function *F = BB.getParent();
574 if (F == MST.getCurrentFunction()) {
575 Slot = MST.getLocalSlot(&BB);
577 ModuleSlotTracker CustomMST(F->getParent(),
578 /*ShouldInitializeAllMetadata=*/false);
579 CustomMST.incorporateFunction(*F);
580 Slot = CustomMST.getLocalSlot(&BB);
588 void MIPrinter::printIRValueReference(const Value &V) {
591 printLLVMNameWithoutPrefix(OS, V.getName());
594 // TODO: Serialize the unnamed IR value references.
595 OS << "<unserializable ir value>";
598 void MIPrinter::printStackObjectReference(int FrameIndex) {
599 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
600 assert(ObjectInfo != StackObjectOperandMapping.end() &&
601 "Invalid frame index");
602 const FrameIndexOperand &Operand = ObjectInfo->second;
603 if (Operand.IsFixed) {
604 OS << "%fixed-stack." << Operand.ID;
607 OS << "%stack." << Operand.ID;
608 if (!Operand.Name.empty())
609 OS << '.' << Operand.Name;
612 void MIPrinter::printOffset(int64_t Offset) {
616 OS << " - " << -Offset;
619 OS << " + " << Offset;
622 static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
623 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
624 for (const auto &I : Flags) {
632 void MIPrinter::printTargetFlags(const MachineOperand &Op) {
633 if (!Op.getTargetFlags())
636 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
637 assert(TII && "expected instruction info");
638 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
639 OS << "target-flags(";
640 const bool HasDirectFlags = Flags.first;
641 const bool HasBitmaskFlags = Flags.second;
642 if (!HasDirectFlags && !HasBitmaskFlags) {
646 if (HasDirectFlags) {
647 if (const auto *Name = getTargetFlagName(TII, Flags.first))
650 OS << "<unknown target flag>";
652 if (!HasBitmaskFlags) {
656 bool IsCommaNeeded = HasDirectFlags;
657 unsigned BitMask = Flags.second;
658 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
659 for (const auto &Mask : BitMasks) {
660 // Check if the flag's bitmask has the bits of the current mask set.
661 if ((BitMask & Mask.first) == Mask.first) {
664 IsCommaNeeded = true;
666 // Clear the bits which were serialized from the flag's bitmask.
667 BitMask &= ~(Mask.first);
671 // When the resulting flag's bitmask isn't zero, we know that we didn't
672 // serialize all of the bit flags.
675 OS << "<unknown bitmask target flag>";
680 static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
681 const auto *TII = MF.getSubtarget().getInstrInfo();
682 assert(TII && "expected instruction info");
683 auto Indices = TII->getSerializableTargetIndices();
684 for (const auto &I : Indices) {
685 if (I.first == Index) {
692 void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
694 printTargetFlags(Op);
695 switch (Op.getType()) {
696 case MachineOperand::MO_Register:
697 // FIXME: Serialize the tied register.
699 OS << (Op.isDef() ? "implicit-def " : "implicit ");
700 else if (!IsDef && Op.isDef())
701 // Print the 'def' flag only when the operand is defined after '='.
703 if (Op.isInternalRead())
711 if (Op.isEarlyClobber())
712 OS << "early-clobber ";
715 printReg(Op.getReg(), OS, TRI);
716 // Print the sub register.
717 if (Op.getSubReg() != 0)
718 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
720 case MachineOperand::MO_Immediate:
723 case MachineOperand::MO_CImmediate:
724 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
726 case MachineOperand::MO_FPImmediate:
727 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
729 case MachineOperand::MO_MachineBasicBlock:
730 printMBBReference(*Op.getMBB());
732 case MachineOperand::MO_FrameIndex:
733 printStackObjectReference(Op.getIndex());
735 case MachineOperand::MO_ConstantPoolIndex:
736 OS << "%const." << Op.getIndex();
737 printOffset(Op.getOffset());
739 case MachineOperand::MO_TargetIndex: {
740 OS << "target-index(";
741 if (const auto *Name = getTargetIndexName(
742 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
747 printOffset(Op.getOffset());
750 case MachineOperand::MO_JumpTableIndex:
751 OS << "%jump-table." << Op.getIndex();
753 case MachineOperand::MO_ExternalSymbol:
755 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
756 printOffset(Op.getOffset());
758 case MachineOperand::MO_GlobalAddress:
759 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
760 printOffset(Op.getOffset());
762 case MachineOperand::MO_BlockAddress:
763 OS << "blockaddress(";
764 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
767 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
769 printOffset(Op.getOffset());
771 case MachineOperand::MO_RegisterMask: {
772 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
773 if (RegMaskInfo != RegisterMaskIds.end())
774 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
776 llvm_unreachable("Can't print this machine register mask yet.");
779 case MachineOperand::MO_RegisterLiveOut: {
780 const uint32_t *RegMask = Op.getRegLiveOut();
782 bool IsCommaNeeded = false;
783 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
784 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
787 printReg(Reg, OS, TRI);
788 IsCommaNeeded = true;
794 case MachineOperand::MO_Metadata:
795 Op.getMetadata()->printAsOperand(OS, MST);
797 case MachineOperand::MO_CFIIndex: {
798 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
799 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
803 // TODO: Print the other machine operands.
804 llvm_unreachable("Can't print this machine operand at the moment");
808 void MIPrinter::print(const MachineMemOperand &Op) {
810 // TODO: Print operand's target specific flags.
813 if (Op.isNonTemporal())
814 OS << "non-temporal ";
815 if (Op.isInvariant())
820 assert(Op.isStore() && "Non load machine operand must be a store");
823 OS << Op.getSize() << (Op.isLoad() ? " from " : " into ");
824 if (const Value *Val = Op.getValue()) {
825 printIRValueReference(*Val);
827 const PseudoSourceValue *PVal = Op.getPseudoValue();
828 assert(PVal && "Expected a pseudo source value");
829 switch (PVal->kind()) {
830 case PseudoSourceValue::Stack:
833 case PseudoSourceValue::GOT:
836 case PseudoSourceValue::JumpTable:
839 case PseudoSourceValue::ConstantPool:
840 OS << "constant-pool";
842 case PseudoSourceValue::FixedStack:
843 printStackObjectReference(
844 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
846 case PseudoSourceValue::GlobalValueCallEntry:
847 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
848 OS, /*PrintType=*/false, MST);
850 case PseudoSourceValue::ExternalSymbolCallEntry:
852 printLLVMNameWithoutPrefix(
853 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
857 printOffset(Op.getOffset());
858 if (Op.getBaseAlignment() != Op.getSize())
859 OS << ", align " << Op.getBaseAlignment();
860 auto AAInfo = Op.getAAInfo();
863 AAInfo.TBAA->printAsOperand(OS, MST);
866 OS << ", !alias.scope ";
867 AAInfo.Scope->printAsOperand(OS, MST);
869 if (AAInfo.NoAlias) {
871 AAInfo.NoAlias->printAsOperand(OS, MST);
873 if (Op.getRanges()) {
875 Op.getRanges()->printAsOperand(OS, MST);
880 static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
881 const TargetRegisterInfo *TRI) {
882 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
887 printReg(Reg, OS, TRI);
890 void MIPrinter::print(const MCCFIInstruction &CFI,
891 const TargetRegisterInfo *TRI) {
892 switch (CFI.getOperation()) {
893 case MCCFIInstruction::OpSameValue:
894 OS << ".cfi_same_value ";
897 printCFIRegister(CFI.getRegister(), OS, TRI);
899 case MCCFIInstruction::OpOffset:
900 OS << ".cfi_offset ";
903 printCFIRegister(CFI.getRegister(), OS, TRI);
904 OS << ", " << CFI.getOffset();
906 case MCCFIInstruction::OpDefCfaRegister:
907 OS << ".cfi_def_cfa_register ";
910 printCFIRegister(CFI.getRegister(), OS, TRI);
912 case MCCFIInstruction::OpDefCfaOffset:
913 OS << ".cfi_def_cfa_offset ";
916 OS << CFI.getOffset();
918 case MCCFIInstruction::OpDefCfa:
919 OS << ".cfi_def_cfa ";
922 printCFIRegister(CFI.getRegister(), OS, TRI);
923 OS << ", " << CFI.getOffset();
926 // TODO: Print the other CFI Operations.
927 OS << "<unserializable cfi operation>";
932 void llvm::printMIR(raw_ostream &OS, const Module &M) {
933 yaml::Output Out(OS);
934 Out << const_cast<Module &>(M);
937 void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
938 MIRPrinter Printer(OS);