1 //===- MIRPrinter.cpp - MIR serialization format printer ------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the class that prints out the LLVM IR and machine
10 // functions using the MIR serialization format.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/MIRPrinter.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/None.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallBitVector.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/CodeGen/GlobalISel/RegisterBank.h"
24 #include "llvm/CodeGen/MIRYamlMapping.h"
25 #include "llvm/CodeGen/MachineBasicBlock.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "llvm/CodeGen/MachineJumpTableInfo.h"
31 #include "llvm/CodeGen/MachineMemOperand.h"
32 #include "llvm/CodeGen/MachineOperand.h"
33 #include "llvm/CodeGen/MachineRegisterInfo.h"
34 #include "llvm/CodeGen/PseudoSourceValue.h"
35 #include "llvm/CodeGen/TargetInstrInfo.h"
36 #include "llvm/CodeGen/TargetRegisterInfo.h"
37 #include "llvm/CodeGen/TargetSubtargetInfo.h"
38 #include "llvm/IR/BasicBlock.h"
39 #include "llvm/IR/Constants.h"
40 #include "llvm/IR/DebugInfo.h"
41 #include "llvm/IR/DebugLoc.h"
42 #include "llvm/IR/Function.h"
43 #include "llvm/IR/GlobalValue.h"
44 #include "llvm/IR/IRPrintingPasses.h"
45 #include "llvm/IR/InstrTypes.h"
46 #include "llvm/IR/Instructions.h"
47 #include "llvm/IR/Intrinsics.h"
48 #include "llvm/IR/Module.h"
49 #include "llvm/IR/ModuleSlotTracker.h"
50 #include "llvm/IR/Value.h"
51 #include "llvm/MC/LaneBitmask.h"
52 #include "llvm/MC/MCContext.h"
53 #include "llvm/MC/MCDwarf.h"
54 #include "llvm/MC/MCSymbol.h"
55 #include "llvm/Support/AtomicOrdering.h"
56 #include "llvm/Support/BranchProbability.h"
57 #include "llvm/Support/Casting.h"
58 #include "llvm/Support/CommandLine.h"
59 #include "llvm/Support/ErrorHandling.h"
60 #include "llvm/Support/Format.h"
61 #include "llvm/Support/LowLevelTypeImpl.h"
62 #include "llvm/Support/YAMLTraits.h"
63 #include "llvm/Support/raw_ostream.h"
64 #include "llvm/Target/TargetIntrinsicInfo.h"
65 #include "llvm/Target/TargetMachine.h"
77 static cl::opt
<bool> SimplifyMIR(
78 "simplify-mir", cl::Hidden
,
79 cl::desc("Leave out unnecessary information when printing MIR"));
83 /// This structure describes how to print out stack object references.
84 struct FrameIndexOperand
{
89 FrameIndexOperand(StringRef Name
, unsigned ID
, bool IsFixed
)
90 : Name(Name
.str()), ID(ID
), IsFixed(IsFixed
) {}
92 /// Return an ordinary stack object reference.
93 static FrameIndexOperand
create(StringRef Name
, unsigned ID
) {
94 return FrameIndexOperand(Name
, ID
, /*IsFixed=*/false);
97 /// Return a fixed stack object reference.
98 static FrameIndexOperand
createFixed(unsigned ID
) {
99 return FrameIndexOperand("", ID
, /*IsFixed=*/true);
103 } // end anonymous namespace
107 /// This class prints out the machine functions using the MIR serialization
111 DenseMap
<const uint32_t *, unsigned> RegisterMaskIds
;
112 /// Maps from stack object indices to operand indices which will be used when
113 /// printing frame index machine operands.
114 DenseMap
<int, FrameIndexOperand
> StackObjectOperandMapping
;
117 MIRPrinter(raw_ostream
&OS
) : OS(OS
) {}
119 void print(const MachineFunction
&MF
);
121 void convert(yaml::MachineFunction
&MF
, const MachineRegisterInfo
&RegInfo
,
122 const TargetRegisterInfo
*TRI
);
123 void convert(ModuleSlotTracker
&MST
, yaml::MachineFrameInfo
&YamlMFI
,
124 const MachineFrameInfo
&MFI
);
125 void convert(yaml::MachineFunction
&MF
,
126 const MachineConstantPool
&ConstantPool
);
127 void convert(ModuleSlotTracker
&MST
, yaml::MachineJumpTable
&YamlJTI
,
128 const MachineJumpTableInfo
&JTI
);
129 void convertStackObjects(yaml::MachineFunction
&YMF
,
130 const MachineFunction
&MF
, ModuleSlotTracker
&MST
);
133 void initRegisterMaskIds(const MachineFunction
&MF
);
136 /// This class prints out the machine instructions using the MIR serialization
140 ModuleSlotTracker
&MST
;
141 const DenseMap
<const uint32_t *, unsigned> &RegisterMaskIds
;
142 const DenseMap
<int, FrameIndexOperand
> &StackObjectOperandMapping
;
143 /// Synchronization scope names registered with LLVMContext.
144 SmallVector
<StringRef
, 8> SSNs
;
146 bool canPredictBranchProbabilities(const MachineBasicBlock
&MBB
) const;
147 bool canPredictSuccessors(const MachineBasicBlock
&MBB
) const;
150 MIPrinter(raw_ostream
&OS
, ModuleSlotTracker
&MST
,
151 const DenseMap
<const uint32_t *, unsigned> &RegisterMaskIds
,
152 const DenseMap
<int, FrameIndexOperand
> &StackObjectOperandMapping
)
153 : OS(OS
), MST(MST
), RegisterMaskIds(RegisterMaskIds
),
154 StackObjectOperandMapping(StackObjectOperandMapping
) {}
156 void print(const MachineBasicBlock
&MBB
);
158 void print(const MachineInstr
&MI
);
159 void printStackObjectReference(int FrameIndex
);
160 void print(const MachineInstr
&MI
, unsigned OpIdx
,
161 const TargetRegisterInfo
*TRI
, bool ShouldPrintRegisterTies
,
162 LLT TypeToPrint
, bool PrintDef
= true);
165 } // end namespace llvm
170 /// This struct serializes the LLVM IR module.
171 template <> struct BlockScalarTraits
<Module
> {
172 static void output(const Module
&Mod
, void *Ctxt
, raw_ostream
&OS
) {
173 Mod
.print(OS
, nullptr);
176 static StringRef
input(StringRef Str
, void *Ctxt
, Module
&Mod
) {
177 llvm_unreachable("LLVM Module is supposed to be parsed separately");
182 } // end namespace yaml
183 } // end namespace llvm
185 static void printRegMIR(unsigned Reg
, yaml::StringValue
&Dest
,
186 const TargetRegisterInfo
*TRI
) {
187 raw_string_ostream
OS(Dest
.Value
);
188 OS
<< printReg(Reg
, TRI
);
191 void MIRPrinter::print(const MachineFunction
&MF
) {
192 initRegisterMaskIds(MF
);
194 yaml::MachineFunction YamlMF
;
195 YamlMF
.Name
= MF
.getName();
196 YamlMF
.Alignment
= MF
.getAlignment();
197 YamlMF
.ExposesReturnsTwice
= MF
.exposesReturnsTwice();
198 YamlMF
.HasWinCFI
= MF
.hasWinCFI();
200 YamlMF
.Legalized
= MF
.getProperties().hasProperty(
201 MachineFunctionProperties::Property::Legalized
);
202 YamlMF
.RegBankSelected
= MF
.getProperties().hasProperty(
203 MachineFunctionProperties::Property::RegBankSelected
);
204 YamlMF
.Selected
= MF
.getProperties().hasProperty(
205 MachineFunctionProperties::Property::Selected
);
206 YamlMF
.FailedISel
= MF
.getProperties().hasProperty(
207 MachineFunctionProperties::Property::FailedISel
);
209 convert(YamlMF
, MF
.getRegInfo(), MF
.getSubtarget().getRegisterInfo());
210 ModuleSlotTracker
MST(MF
.getFunction().getParent());
211 MST
.incorporateFunction(MF
.getFunction());
212 convert(MST
, YamlMF
.FrameInfo
, MF
.getFrameInfo());
213 convertStackObjects(YamlMF
, MF
, MST
);
214 if (const auto *ConstantPool
= MF
.getConstantPool())
215 convert(YamlMF
, *ConstantPool
);
216 if (const auto *JumpTableInfo
= MF
.getJumpTableInfo())
217 convert(MST
, YamlMF
.JumpTableInfo
, *JumpTableInfo
);
218 raw_string_ostream
StrOS(YamlMF
.Body
.Value
.Value
);
219 bool IsNewlineNeeded
= false;
220 for (const auto &MBB
: MF
) {
223 MIPrinter(StrOS
, MST
, RegisterMaskIds
, StackObjectOperandMapping
)
225 IsNewlineNeeded
= true;
228 yaml::Output
Out(OS
);
230 Out
.setWriteDefaultValues(true);
234 static void printCustomRegMask(const uint32_t *RegMask
, raw_ostream
&OS
,
235 const TargetRegisterInfo
*TRI
) {
236 assert(RegMask
&& "Can't print an empty register mask");
237 OS
<< StringRef("CustomRegMask(");
239 bool IsRegInRegMaskFound
= false;
240 for (int I
= 0, E
= TRI
->getNumRegs(); I
< E
; I
++) {
241 // Check whether the register is asserted in regmask.
242 if (RegMask
[I
/ 32] & (1u << (I
% 32))) {
243 if (IsRegInRegMaskFound
)
245 OS
<< printReg(I
, TRI
);
246 IsRegInRegMaskFound
= true;
253 static void printRegClassOrBank(unsigned Reg
, yaml::StringValue
&Dest
,
254 const MachineRegisterInfo
&RegInfo
,
255 const TargetRegisterInfo
*TRI
) {
256 raw_string_ostream
OS(Dest
.Value
);
257 OS
<< printRegClassOrBank(Reg
, RegInfo
, TRI
);
260 template <typename T
>
262 printStackObjectDbgInfo(const MachineFunction::VariableDbgInfo
&DebugVar
,
263 T
&Object
, ModuleSlotTracker
&MST
) {
264 std::array
<std::string
*, 3> Outputs
{{&Object
.DebugVar
.Value
,
265 &Object
.DebugExpr
.Value
,
266 &Object
.DebugLoc
.Value
}};
267 std::array
<const Metadata
*, 3> Metas
{{DebugVar
.Var
,
270 for (unsigned i
= 0; i
< 3; ++i
) {
271 raw_string_ostream
StrOS(*Outputs
[i
]);
272 Metas
[i
]->printAsOperand(StrOS
, MST
);
276 void MIRPrinter::convert(yaml::MachineFunction
&MF
,
277 const MachineRegisterInfo
&RegInfo
,
278 const TargetRegisterInfo
*TRI
) {
279 MF
.TracksRegLiveness
= RegInfo
.tracksLiveness();
281 // Print the virtual register definitions.
282 for (unsigned I
= 0, E
= RegInfo
.getNumVirtRegs(); I
< E
; ++I
) {
283 unsigned Reg
= TargetRegisterInfo::index2VirtReg(I
);
284 yaml::VirtualRegisterDefinition VReg
;
286 if (RegInfo
.getVRegName(Reg
) != "")
288 ::printRegClassOrBank(Reg
, VReg
.Class
, RegInfo
, TRI
);
289 unsigned PreferredReg
= RegInfo
.getSimpleHint(Reg
);
291 printRegMIR(PreferredReg
, VReg
.PreferredRegister
, TRI
);
292 MF
.VirtualRegisters
.push_back(VReg
);
295 // Print the live ins.
296 for (std::pair
<unsigned, unsigned> LI
: RegInfo
.liveins()) {
297 yaml::MachineFunctionLiveIn LiveIn
;
298 printRegMIR(LI
.first
, LiveIn
.Register
, TRI
);
300 printRegMIR(LI
.second
, LiveIn
.VirtualRegister
, TRI
);
301 MF
.LiveIns
.push_back(LiveIn
);
304 // Prints the callee saved registers.
305 if (RegInfo
.isUpdatedCSRsInitialized()) {
306 const MCPhysReg
*CalleeSavedRegs
= RegInfo
.getCalleeSavedRegs();
307 std::vector
<yaml::FlowStringValue
> CalleeSavedRegisters
;
308 for (const MCPhysReg
*I
= CalleeSavedRegs
; *I
; ++I
) {
309 yaml::FlowStringValue Reg
;
310 printRegMIR(*I
, Reg
, TRI
);
311 CalleeSavedRegisters
.push_back(Reg
);
313 MF
.CalleeSavedRegisters
= CalleeSavedRegisters
;
317 void MIRPrinter::convert(ModuleSlotTracker
&MST
,
318 yaml::MachineFrameInfo
&YamlMFI
,
319 const MachineFrameInfo
&MFI
) {
320 YamlMFI
.IsFrameAddressTaken
= MFI
.isFrameAddressTaken();
321 YamlMFI
.IsReturnAddressTaken
= MFI
.isReturnAddressTaken();
322 YamlMFI
.HasStackMap
= MFI
.hasStackMap();
323 YamlMFI
.HasPatchPoint
= MFI
.hasPatchPoint();
324 YamlMFI
.StackSize
= MFI
.getStackSize();
325 YamlMFI
.OffsetAdjustment
= MFI
.getOffsetAdjustment();
326 YamlMFI
.MaxAlignment
= MFI
.getMaxAlignment();
327 YamlMFI
.AdjustsStack
= MFI
.adjustsStack();
328 YamlMFI
.HasCalls
= MFI
.hasCalls();
329 YamlMFI
.MaxCallFrameSize
= MFI
.isMaxCallFrameSizeComputed()
330 ? MFI
.getMaxCallFrameSize() : ~0u;
331 YamlMFI
.CVBytesOfCalleeSavedRegisters
=
332 MFI
.getCVBytesOfCalleeSavedRegisters();
333 YamlMFI
.HasOpaqueSPAdjustment
= MFI
.hasOpaqueSPAdjustment();
334 YamlMFI
.HasVAStart
= MFI
.hasVAStart();
335 YamlMFI
.HasMustTailInVarArgFunc
= MFI
.hasMustTailInVarArgFunc();
336 YamlMFI
.LocalFrameSize
= MFI
.getLocalFrameSize();
337 if (MFI
.getSavePoint()) {
338 raw_string_ostream
StrOS(YamlMFI
.SavePoint
.Value
);
339 StrOS
<< printMBBReference(*MFI
.getSavePoint());
341 if (MFI
.getRestorePoint()) {
342 raw_string_ostream
StrOS(YamlMFI
.RestorePoint
.Value
);
343 StrOS
<< printMBBReference(*MFI
.getRestorePoint());
347 void MIRPrinter::convertStackObjects(yaml::MachineFunction
&YMF
,
348 const MachineFunction
&MF
,
349 ModuleSlotTracker
&MST
) {
350 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
351 const TargetRegisterInfo
*TRI
= MF
.getSubtarget().getRegisterInfo();
352 // Process fixed stack objects.
354 for (int I
= MFI
.getObjectIndexBegin(); I
< 0; ++I
) {
355 if (MFI
.isDeadObjectIndex(I
))
358 yaml::FixedMachineStackObject YamlObject
;
360 YamlObject
.Type
= MFI
.isSpillSlotObjectIndex(I
)
361 ? yaml::FixedMachineStackObject::SpillSlot
362 : yaml::FixedMachineStackObject::DefaultType
;
363 YamlObject
.Offset
= MFI
.getObjectOffset(I
);
364 YamlObject
.Size
= MFI
.getObjectSize(I
);
365 YamlObject
.Alignment
= MFI
.getObjectAlignment(I
);
366 YamlObject
.StackID
= MFI
.getStackID(I
);
367 YamlObject
.IsImmutable
= MFI
.isImmutableObjectIndex(I
);
368 YamlObject
.IsAliased
= MFI
.isAliasedObjectIndex(I
);
369 YMF
.FixedStackObjects
.push_back(YamlObject
);
370 StackObjectOperandMapping
.insert(
371 std::make_pair(I
, FrameIndexOperand::createFixed(ID
++)));
374 // Process ordinary stack objects.
376 for (int I
= 0, E
= MFI
.getObjectIndexEnd(); I
< E
; ++I
) {
377 if (MFI
.isDeadObjectIndex(I
))
380 yaml::MachineStackObject YamlObject
;
382 if (const auto *Alloca
= MFI
.getObjectAllocation(I
))
383 YamlObject
.Name
.Value
=
384 Alloca
->hasName() ? Alloca
->getName() : "<unnamed alloca>";
385 YamlObject
.Type
= MFI
.isSpillSlotObjectIndex(I
)
386 ? yaml::MachineStackObject::SpillSlot
387 : MFI
.isVariableSizedObjectIndex(I
)
388 ? yaml::MachineStackObject::VariableSized
389 : yaml::MachineStackObject::DefaultType
;
390 YamlObject
.Offset
= MFI
.getObjectOffset(I
);
391 YamlObject
.Size
= MFI
.getObjectSize(I
);
392 YamlObject
.Alignment
= MFI
.getObjectAlignment(I
);
393 YamlObject
.StackID
= MFI
.getStackID(I
);
395 YMF
.StackObjects
.push_back(YamlObject
);
396 StackObjectOperandMapping
.insert(std::make_pair(
397 I
, FrameIndexOperand::create(YamlObject
.Name
.Value
, ID
++)));
400 for (const auto &CSInfo
: MFI
.getCalleeSavedInfo()) {
401 yaml::StringValue Reg
;
402 printRegMIR(CSInfo
.getReg(), Reg
, TRI
);
403 if (!CSInfo
.isSpilledToReg()) {
404 auto StackObjectInfo
= StackObjectOperandMapping
.find(CSInfo
.getFrameIdx());
405 assert(StackObjectInfo
!= StackObjectOperandMapping
.end() &&
406 "Invalid stack object index");
407 const FrameIndexOperand
&StackObject
= StackObjectInfo
->second
;
408 if (StackObject
.IsFixed
) {
409 YMF
.FixedStackObjects
[StackObject
.ID
].CalleeSavedRegister
= Reg
;
410 YMF
.FixedStackObjects
[StackObject
.ID
].CalleeSavedRestored
=
413 YMF
.StackObjects
[StackObject
.ID
].CalleeSavedRegister
= Reg
;
414 YMF
.StackObjects
[StackObject
.ID
].CalleeSavedRestored
=
419 for (unsigned I
= 0, E
= MFI
.getLocalFrameObjectCount(); I
< E
; ++I
) {
420 auto LocalObject
= MFI
.getLocalFrameObjectMap(I
);
421 auto StackObjectInfo
= StackObjectOperandMapping
.find(LocalObject
.first
);
422 assert(StackObjectInfo
!= StackObjectOperandMapping
.end() &&
423 "Invalid stack object index");
424 const FrameIndexOperand
&StackObject
= StackObjectInfo
->second
;
425 assert(!StackObject
.IsFixed
&& "Expected a locally mapped stack object");
426 YMF
.StackObjects
[StackObject
.ID
].LocalOffset
= LocalObject
.second
;
429 // Print the stack object references in the frame information class after
430 // converting the stack objects.
431 if (MFI
.hasStackProtectorIndex()) {
432 raw_string_ostream
StrOS(YMF
.FrameInfo
.StackProtector
.Value
);
433 MIPrinter(StrOS
, MST
, RegisterMaskIds
, StackObjectOperandMapping
)
434 .printStackObjectReference(MFI
.getStackProtectorIndex());
437 // Print the debug variable information.
438 for (const MachineFunction::VariableDbgInfo
&DebugVar
:
439 MF
.getVariableDbgInfo()) {
440 auto StackObjectInfo
= StackObjectOperandMapping
.find(DebugVar
.Slot
);
441 assert(StackObjectInfo
!= StackObjectOperandMapping
.end() &&
442 "Invalid stack object index");
443 const FrameIndexOperand
&StackObject
= StackObjectInfo
->second
;
444 if (StackObject
.IsFixed
) {
445 auto &Object
= YMF
.FixedStackObjects
[StackObject
.ID
];
446 printStackObjectDbgInfo(DebugVar
, Object
, MST
);
448 auto &Object
= YMF
.StackObjects
[StackObject
.ID
];
449 printStackObjectDbgInfo(DebugVar
, Object
, MST
);
454 void MIRPrinter::convert(yaml::MachineFunction
&MF
,
455 const MachineConstantPool
&ConstantPool
) {
457 for (const MachineConstantPoolEntry
&Constant
: ConstantPool
.getConstants()) {
459 raw_string_ostream
StrOS(Str
);
460 if (Constant
.isMachineConstantPoolEntry()) {
461 Constant
.Val
.MachineCPVal
->print(StrOS
);
463 Constant
.Val
.ConstVal
->printAsOperand(StrOS
);
466 yaml::MachineConstantPoolValue YamlConstant
;
467 YamlConstant
.ID
= ID
++;
468 YamlConstant
.Value
= StrOS
.str();
469 YamlConstant
.Alignment
= Constant
.getAlignment();
470 YamlConstant
.IsTargetSpecific
= Constant
.isMachineConstantPoolEntry();
472 MF
.Constants
.push_back(YamlConstant
);
476 void MIRPrinter::convert(ModuleSlotTracker
&MST
,
477 yaml::MachineJumpTable
&YamlJTI
,
478 const MachineJumpTableInfo
&JTI
) {
479 YamlJTI
.Kind
= JTI
.getEntryKind();
481 for (const auto &Table
: JTI
.getJumpTables()) {
483 yaml::MachineJumpTable::Entry Entry
;
485 for (const auto *MBB
: Table
.MBBs
) {
486 raw_string_ostream
StrOS(Str
);
487 StrOS
<< printMBBReference(*MBB
);
488 Entry
.Blocks
.push_back(StrOS
.str());
491 YamlJTI
.Entries
.push_back(Entry
);
495 void MIRPrinter::initRegisterMaskIds(const MachineFunction
&MF
) {
496 const auto *TRI
= MF
.getSubtarget().getRegisterInfo();
498 for (const uint32_t *Mask
: TRI
->getRegMasks())
499 RegisterMaskIds
.insert(std::make_pair(Mask
, I
++));
502 void llvm::guessSuccessors(const MachineBasicBlock
&MBB
,
503 SmallVectorImpl
<MachineBasicBlock
*> &Result
,
504 bool &IsFallthrough
) {
505 SmallPtrSet
<MachineBasicBlock
*,8> Seen
;
507 for (const MachineInstr
&MI
: MBB
) {
510 for (const MachineOperand
&MO
: MI
.operands()) {
513 MachineBasicBlock
*Succ
= MO
.getMBB();
514 auto RP
= Seen
.insert(Succ
);
516 Result
.push_back(Succ
);
519 MachineBasicBlock::const_iterator I
= MBB
.getLastNonDebugInstr();
520 IsFallthrough
= I
== MBB
.end() || !I
->isBarrier();
524 MIPrinter::canPredictBranchProbabilities(const MachineBasicBlock
&MBB
) const {
525 if (MBB
.succ_size() <= 1)
527 if (!MBB
.hasSuccessorProbabilities())
530 SmallVector
<BranchProbability
,8> Normalized(MBB
.Probs
.begin(),
532 BranchProbability::normalizeProbabilities(Normalized
.begin(),
534 SmallVector
<BranchProbability
,8> Equal(Normalized
.size());
535 BranchProbability::normalizeProbabilities(Equal
.begin(), Equal
.end());
537 return std::equal(Normalized
.begin(), Normalized
.end(), Equal
.begin());
540 bool MIPrinter::canPredictSuccessors(const MachineBasicBlock
&MBB
) const {
541 SmallVector
<MachineBasicBlock
*,8> GuessedSuccs
;
542 bool GuessedFallthrough
;
543 guessSuccessors(MBB
, GuessedSuccs
, GuessedFallthrough
);
544 if (GuessedFallthrough
) {
545 const MachineFunction
&MF
= *MBB
.getParent();
546 MachineFunction::const_iterator NextI
= std::next(MBB
.getIterator());
547 if (NextI
!= MF
.end()) {
548 MachineBasicBlock
*Next
= const_cast<MachineBasicBlock
*>(&*NextI
);
549 if (!is_contained(GuessedSuccs
, Next
))
550 GuessedSuccs
.push_back(Next
);
553 if (GuessedSuccs
.size() != MBB
.succ_size())
555 return std::equal(MBB
.succ_begin(), MBB
.succ_end(), GuessedSuccs
.begin());
558 void MIPrinter::print(const MachineBasicBlock
&MBB
) {
559 assert(MBB
.getNumber() >= 0 && "Invalid MBB number");
560 OS
<< "bb." << MBB
.getNumber();
561 bool HasAttributes
= false;
562 if (const auto *BB
= MBB
.getBasicBlock()) {
564 OS
<< "." << BB
->getName();
566 HasAttributes
= true;
568 int Slot
= MST
.getLocalSlot(BB
);
570 OS
<< "<ir-block badref>";
572 OS
<< (Twine("%ir-block.") + Twine(Slot
)).str();
575 if (MBB
.hasAddressTaken()) {
576 OS
<< (HasAttributes
? ", " : " (");
577 OS
<< "address-taken";
578 HasAttributes
= true;
581 OS
<< (HasAttributes
? ", " : " (");
583 HasAttributes
= true;
585 if (MBB
.getAlignment()) {
586 OS
<< (HasAttributes
? ", " : " (");
587 OS
<< "align " << MBB
.getAlignment();
588 HasAttributes
= true;
594 bool HasLineAttributes
= false;
595 // Print the successors
596 bool canPredictProbs
= canPredictBranchProbabilities(MBB
);
597 // Even if the list of successors is empty, if we cannot guess it,
598 // we need to print it to tell the parser that the list is empty.
599 // This is needed, because MI model unreachable as empty blocks
600 // with an empty successor list. If the parser would see that
601 // without the successor list, it would guess the code would
603 if ((!MBB
.succ_empty() && !SimplifyMIR
) || !canPredictProbs
||
604 !canPredictSuccessors(MBB
)) {
605 OS
.indent(2) << "successors: ";
606 for (auto I
= MBB
.succ_begin(), E
= MBB
.succ_end(); I
!= E
; ++I
) {
607 if (I
!= MBB
.succ_begin())
609 OS
<< printMBBReference(**I
);
610 if (!SimplifyMIR
|| !canPredictProbs
)
612 << format("0x%08" PRIx32
, MBB
.getSuccProbability(I
).getNumerator())
616 HasLineAttributes
= true;
619 // Print the live in registers.
620 const MachineRegisterInfo
&MRI
= MBB
.getParent()->getRegInfo();
621 if (MRI
.tracksLiveness() && !MBB
.livein_empty()) {
622 const TargetRegisterInfo
&TRI
= *MRI
.getTargetRegisterInfo();
623 OS
.indent(2) << "liveins: ";
625 for (const auto &LI
: MBB
.liveins()) {
629 OS
<< printReg(LI
.PhysReg
, &TRI
);
630 if (!LI
.LaneMask
.all())
631 OS
<< ":0x" << PrintLaneMask(LI
.LaneMask
);
634 HasLineAttributes
= true;
637 if (HasLineAttributes
)
639 bool IsInBundle
= false;
640 for (auto I
= MBB
.instr_begin(), E
= MBB
.instr_end(); I
!= E
; ++I
) {
641 const MachineInstr
&MI
= *I
;
642 if (IsInBundle
&& !MI
.isInsideBundle()) {
643 OS
.indent(2) << "}\n";
646 OS
.indent(IsInBundle
? 4 : 2);
648 if (!IsInBundle
&& MI
.getFlag(MachineInstr::BundledSucc
)) {
655 OS
.indent(2) << "}\n";
658 void MIPrinter::print(const MachineInstr
&MI
) {
659 const auto *MF
= MI
.getMF();
660 const auto &MRI
= MF
->getRegInfo();
661 const auto &SubTarget
= MF
->getSubtarget();
662 const auto *TRI
= SubTarget
.getRegisterInfo();
663 assert(TRI
&& "Expected target register info");
664 const auto *TII
= SubTarget
.getInstrInfo();
665 assert(TII
&& "Expected target instruction info");
666 if (MI
.isCFIInstruction())
667 assert(MI
.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
669 SmallBitVector
PrintedTypes(8);
670 bool ShouldPrintRegisterTies
= MI
.hasComplexRegisterTies();
671 unsigned I
= 0, E
= MI
.getNumOperands();
672 for (; I
< E
&& MI
.getOperand(I
).isReg() && MI
.getOperand(I
).isDef() &&
673 !MI
.getOperand(I
).isImplicit();
677 print(MI
, I
, TRI
, ShouldPrintRegisterTies
,
678 MI
.getTypeToPrint(I
, PrintedTypes
, MRI
),
684 if (MI
.getFlag(MachineInstr::FrameSetup
))
685 OS
<< "frame-setup ";
686 if (MI
.getFlag(MachineInstr::FrameDestroy
))
687 OS
<< "frame-destroy ";
688 if (MI
.getFlag(MachineInstr::FmNoNans
))
690 if (MI
.getFlag(MachineInstr::FmNoInfs
))
692 if (MI
.getFlag(MachineInstr::FmNsz
))
694 if (MI
.getFlag(MachineInstr::FmArcp
))
696 if (MI
.getFlag(MachineInstr::FmContract
))
698 if (MI
.getFlag(MachineInstr::FmAfn
))
700 if (MI
.getFlag(MachineInstr::FmReassoc
))
702 if (MI
.getFlag(MachineInstr::NoUWrap
))
704 if (MI
.getFlag(MachineInstr::NoSWrap
))
706 if (MI
.getFlag(MachineInstr::IsExact
))
709 OS
<< TII
->getName(MI
.getOpcode());
713 bool NeedComma
= false;
717 print(MI
, I
, TRI
, ShouldPrintRegisterTies
,
718 MI
.getTypeToPrint(I
, PrintedTypes
, MRI
));
722 // Print any optional symbols attached to this instruction as-if they were
724 if (MCSymbol
*PreInstrSymbol
= MI
.getPreInstrSymbol()) {
727 OS
<< " pre-instr-symbol ";
728 MachineOperand::printSymbol(OS
, *PreInstrSymbol
);
731 if (MCSymbol
*PostInstrSymbol
= MI
.getPostInstrSymbol()) {
734 OS
<< " post-instr-symbol ";
735 MachineOperand::printSymbol(OS
, *PostInstrSymbol
);
739 if (const DebugLoc
&DL
= MI
.getDebugLoc()) {
742 OS
<< " debug-location ";
743 DL
->printAsOperand(OS
, MST
);
746 if (!MI
.memoperands_empty()) {
748 const LLVMContext
&Context
= MF
->getFunction().getContext();
749 const MachineFrameInfo
&MFI
= MF
->getFrameInfo();
750 bool NeedComma
= false;
751 for (const auto *Op
: MI
.memoperands()) {
754 Op
->print(OS
, MST
, SSNs
, Context
, &MFI
, TII
);
760 void MIPrinter::printStackObjectReference(int FrameIndex
) {
761 auto ObjectInfo
= StackObjectOperandMapping
.find(FrameIndex
);
762 assert(ObjectInfo
!= StackObjectOperandMapping
.end() &&
763 "Invalid frame index");
764 const FrameIndexOperand
&Operand
= ObjectInfo
->second
;
765 MachineOperand::printStackObjectReference(OS
, Operand
.ID
, Operand
.IsFixed
,
769 void MIPrinter::print(const MachineInstr
&MI
, unsigned OpIdx
,
770 const TargetRegisterInfo
*TRI
,
771 bool ShouldPrintRegisterTies
, LLT TypeToPrint
,
773 const MachineOperand
&Op
= MI
.getOperand(OpIdx
);
774 switch (Op
.getType()) {
775 case MachineOperand::MO_Immediate
:
776 if (MI
.isOperandSubregIdx(OpIdx
)) {
777 MachineOperand::printTargetFlags(OS
, Op
);
778 MachineOperand::printSubRegIdx(OS
, Op
.getImm(), TRI
);
782 case MachineOperand::MO_Register
:
783 case MachineOperand::MO_CImmediate
:
784 case MachineOperand::MO_FPImmediate
:
785 case MachineOperand::MO_MachineBasicBlock
:
786 case MachineOperand::MO_ConstantPoolIndex
:
787 case MachineOperand::MO_TargetIndex
:
788 case MachineOperand::MO_JumpTableIndex
:
789 case MachineOperand::MO_ExternalSymbol
:
790 case MachineOperand::MO_GlobalAddress
:
791 case MachineOperand::MO_RegisterLiveOut
:
792 case MachineOperand::MO_Metadata
:
793 case MachineOperand::MO_MCSymbol
:
794 case MachineOperand::MO_CFIIndex
:
795 case MachineOperand::MO_IntrinsicID
:
796 case MachineOperand::MO_Predicate
:
797 case MachineOperand::MO_BlockAddress
: {
798 unsigned TiedOperandIdx
= 0;
799 if (ShouldPrintRegisterTies
&& Op
.isReg() && Op
.isTied() && !Op
.isDef())
800 TiedOperandIdx
= Op
.getParent()->findTiedOperandIdx(OpIdx
);
801 const TargetIntrinsicInfo
*TII
= MI
.getMF()->getTarget().getIntrinsicInfo();
802 Op
.print(OS
, MST
, TypeToPrint
, PrintDef
, /*IsStandalone=*/false,
803 ShouldPrintRegisterTies
, TiedOperandIdx
, TRI
, TII
);
806 case MachineOperand::MO_FrameIndex
:
807 printStackObjectReference(Op
.getIndex());
809 case MachineOperand::MO_RegisterMask
: {
810 auto RegMaskInfo
= RegisterMaskIds
.find(Op
.getRegMask());
811 if (RegMaskInfo
!= RegisterMaskIds
.end())
812 OS
<< StringRef(TRI
->getRegMaskNames()[RegMaskInfo
->second
]).lower();
814 printCustomRegMask(Op
.getRegMask(), OS
, TRI
);
820 void llvm::printMIR(raw_ostream
&OS
, const Module
&M
) {
821 yaml::Output
Out(OS
);
822 Out
<< const_cast<Module
&>(M
);
825 void llvm::printMIR(raw_ostream
&OS
, const MachineFunction
&MF
) {
826 MIRPrinter
Printer(OS
);