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/STLExtras.h"
17 #include "llvm/ADT/SmallBitVector.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/CodeGen/LowLevelType.h"
22 #include "llvm/CodeGen/MIRYamlMapping.h"
23 #include "llvm/CodeGen/MachineBasicBlock.h"
24 #include "llvm/CodeGen/MachineConstantPool.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineInstr.h"
28 #include "llvm/CodeGen/MachineJumpTableInfo.h"
29 #include "llvm/CodeGen/MachineMemOperand.h"
30 #include "llvm/CodeGen/MachineModuleSlotTracker.h"
31 #include "llvm/CodeGen/MachineOperand.h"
32 #include "llvm/CodeGen/MachineRegisterInfo.h"
33 #include "llvm/CodeGen/TargetFrameLowering.h"
34 #include "llvm/CodeGen/TargetInstrInfo.h"
35 #include "llvm/CodeGen/TargetRegisterInfo.h"
36 #include "llvm/CodeGen/TargetSubtargetInfo.h"
37 #include "llvm/IR/DebugInfoMetadata.h"
38 #include "llvm/IR/DebugLoc.h"
39 #include "llvm/IR/Function.h"
40 #include "llvm/IR/IRPrintingPasses.h"
41 #include "llvm/IR/Instructions.h"
42 #include "llvm/IR/Module.h"
43 #include "llvm/IR/ModuleSlotTracker.h"
44 #include "llvm/IR/Value.h"
45 #include "llvm/MC/LaneBitmask.h"
46 #include "llvm/Support/BranchProbability.h"
47 #include "llvm/Support/Casting.h"
48 #include "llvm/Support/CommandLine.h"
49 #include "llvm/Support/ErrorHandling.h"
50 #include "llvm/Support/Format.h"
51 #include "llvm/Support/YAMLTraits.h"
52 #include "llvm/Support/raw_ostream.h"
53 #include "llvm/Target/TargetMachine.h"
65 static cl::opt
<bool> SimplifyMIR(
66 "simplify-mir", cl::Hidden
,
67 cl::desc("Leave out unnecessary information when printing MIR"));
69 static cl::opt
<bool> PrintLocations("mir-debug-loc", cl::Hidden
, cl::init(true),
70 cl::desc("Print MIR debug-locations"));
74 /// This structure describes how to print out stack object references.
75 struct FrameIndexOperand
{
80 FrameIndexOperand(StringRef Name
, unsigned ID
, bool IsFixed
)
81 : Name(Name
.str()), ID(ID
), IsFixed(IsFixed
) {}
83 /// Return an ordinary stack object reference.
84 static FrameIndexOperand
create(StringRef Name
, unsigned ID
) {
85 return FrameIndexOperand(Name
, ID
, /*IsFixed=*/false);
88 /// Return a fixed stack object reference.
89 static FrameIndexOperand
createFixed(unsigned ID
) {
90 return FrameIndexOperand("", ID
, /*IsFixed=*/true);
94 } // end anonymous namespace
98 /// This class prints out the machine functions using the MIR serialization
102 DenseMap
<const uint32_t *, unsigned> RegisterMaskIds
;
103 /// Maps from stack object indices to operand indices which will be used when
104 /// printing frame index machine operands.
105 DenseMap
<int, FrameIndexOperand
> StackObjectOperandMapping
;
108 MIRPrinter(raw_ostream
&OS
) : OS(OS
) {}
110 void print(const MachineFunction
&MF
);
112 void convert(yaml::MachineFunction
&MF
, const MachineRegisterInfo
&RegInfo
,
113 const TargetRegisterInfo
*TRI
);
114 void convert(ModuleSlotTracker
&MST
, yaml::MachineFrameInfo
&YamlMFI
,
115 const MachineFrameInfo
&MFI
);
116 void convert(yaml::MachineFunction
&MF
,
117 const MachineConstantPool
&ConstantPool
);
118 void convert(ModuleSlotTracker
&MST
, yaml::MachineJumpTable
&YamlJTI
,
119 const MachineJumpTableInfo
&JTI
);
120 void convertStackObjects(yaml::MachineFunction
&YMF
,
121 const MachineFunction
&MF
, ModuleSlotTracker
&MST
);
122 void convertEntryValueObjects(yaml::MachineFunction
&YMF
,
123 const MachineFunction
&MF
,
124 ModuleSlotTracker
&MST
);
125 void convertCallSiteObjects(yaml::MachineFunction
&YMF
,
126 const MachineFunction
&MF
,
127 ModuleSlotTracker
&MST
);
128 void convertMachineMetadataNodes(yaml::MachineFunction
&YMF
,
129 const MachineFunction
&MF
,
130 MachineModuleSlotTracker
&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
, const TargetInstrInfo
*TII
,
162 bool ShouldPrintRegisterTies
, LLT TypeToPrint
,
163 bool PrintDef
= true);
166 } // end namespace llvm
171 /// This struct serializes the LLVM IR module.
172 template <> struct BlockScalarTraits
<Module
> {
173 static void output(const Module
&Mod
, void *Ctxt
, raw_ostream
&OS
) {
174 Mod
.print(OS
, nullptr);
177 static StringRef
input(StringRef Str
, void *Ctxt
, Module
&Mod
) {
178 llvm_unreachable("LLVM Module is supposed to be parsed separately");
183 } // end namespace yaml
184 } // end namespace llvm
186 static void printRegMIR(unsigned Reg
, yaml::StringValue
&Dest
,
187 const TargetRegisterInfo
*TRI
) {
188 raw_string_ostream
OS(Dest
.Value
);
189 OS
<< printReg(Reg
, TRI
);
192 void MIRPrinter::print(const MachineFunction
&MF
) {
193 initRegisterMaskIds(MF
);
195 yaml::MachineFunction YamlMF
;
196 YamlMF
.Name
= MF
.getName();
197 YamlMF
.Alignment
= MF
.getAlignment();
198 YamlMF
.ExposesReturnsTwice
= MF
.exposesReturnsTwice();
199 YamlMF
.HasWinCFI
= MF
.hasWinCFI();
201 YamlMF
.CallsEHReturn
= MF
.callsEHReturn();
202 YamlMF
.CallsUnwindInit
= MF
.callsUnwindInit();
203 YamlMF
.HasEHCatchret
= MF
.hasEHCatchret();
204 YamlMF
.HasEHScopes
= MF
.hasEHScopes();
205 YamlMF
.HasEHFunclets
= MF
.hasEHFunclets();
206 YamlMF
.IsOutlined
= MF
.isOutlined();
207 YamlMF
.UseDebugInstrRef
= MF
.useDebugInstrRef();
209 YamlMF
.Legalized
= MF
.getProperties().hasProperty(
210 MachineFunctionProperties::Property::Legalized
);
211 YamlMF
.RegBankSelected
= MF
.getProperties().hasProperty(
212 MachineFunctionProperties::Property::RegBankSelected
);
213 YamlMF
.Selected
= MF
.getProperties().hasProperty(
214 MachineFunctionProperties::Property::Selected
);
215 YamlMF
.FailedISel
= MF
.getProperties().hasProperty(
216 MachineFunctionProperties::Property::FailedISel
);
217 YamlMF
.FailsVerification
= MF
.getProperties().hasProperty(
218 MachineFunctionProperties::Property::FailsVerification
);
219 YamlMF
.TracksDebugUserValues
= MF
.getProperties().hasProperty(
220 MachineFunctionProperties::Property::TracksDebugUserValues
);
222 convert(YamlMF
, MF
.getRegInfo(), MF
.getSubtarget().getRegisterInfo());
223 MachineModuleSlotTracker
MST(&MF
);
224 MST
.incorporateFunction(MF
.getFunction());
225 convert(MST
, YamlMF
.FrameInfo
, MF
.getFrameInfo());
226 convertStackObjects(YamlMF
, MF
, MST
);
227 convertEntryValueObjects(YamlMF
, MF
, MST
);
228 convertCallSiteObjects(YamlMF
, MF
, MST
);
229 for (const auto &Sub
: MF
.DebugValueSubstitutions
) {
230 const auto &SubSrc
= Sub
.Src
;
231 const auto &SubDest
= Sub
.Dest
;
232 YamlMF
.DebugValueSubstitutions
.push_back({SubSrc
.first
, SubSrc
.second
,
237 if (const auto *ConstantPool
= MF
.getConstantPool())
238 convert(YamlMF
, *ConstantPool
);
239 if (const auto *JumpTableInfo
= MF
.getJumpTableInfo())
240 convert(MST
, YamlMF
.JumpTableInfo
, *JumpTableInfo
);
242 const TargetMachine
&TM
= MF
.getTarget();
243 YamlMF
.MachineFuncInfo
=
244 std::unique_ptr
<yaml::MachineFunctionInfo
>(TM
.convertFuncInfoToYAML(MF
));
246 raw_string_ostream
StrOS(YamlMF
.Body
.Value
.Value
);
247 bool IsNewlineNeeded
= false;
248 for (const auto &MBB
: MF
) {
251 MIPrinter(StrOS
, MST
, RegisterMaskIds
, StackObjectOperandMapping
)
253 IsNewlineNeeded
= true;
256 // Convert machine metadata collected during the print of the machine
258 convertMachineMetadataNodes(YamlMF
, MF
, MST
);
260 yaml::Output
Out(OS
);
262 Out
.setWriteDefaultValues(true);
266 static void printCustomRegMask(const uint32_t *RegMask
, raw_ostream
&OS
,
267 const TargetRegisterInfo
*TRI
) {
268 assert(RegMask
&& "Can't print an empty register mask");
269 OS
<< StringRef("CustomRegMask(");
271 bool IsRegInRegMaskFound
= false;
272 for (int I
= 0, E
= TRI
->getNumRegs(); I
< E
; I
++) {
273 // Check whether the register is asserted in regmask.
274 if (RegMask
[I
/ 32] & (1u << (I
% 32))) {
275 if (IsRegInRegMaskFound
)
277 OS
<< printReg(I
, TRI
);
278 IsRegInRegMaskFound
= true;
285 static void printRegClassOrBank(unsigned Reg
, yaml::StringValue
&Dest
,
286 const MachineRegisterInfo
&RegInfo
,
287 const TargetRegisterInfo
*TRI
) {
288 raw_string_ostream
OS(Dest
.Value
);
289 OS
<< printRegClassOrBank(Reg
, RegInfo
, TRI
);
292 template <typename T
>
294 printStackObjectDbgInfo(const MachineFunction::VariableDbgInfo
&DebugVar
,
295 T
&Object
, ModuleSlotTracker
&MST
) {
296 std::array
<std::string
*, 3> Outputs
{{&Object
.DebugVar
.Value
,
297 &Object
.DebugExpr
.Value
,
298 &Object
.DebugLoc
.Value
}};
299 std::array
<const Metadata
*, 3> Metas
{{DebugVar
.Var
,
302 for (unsigned i
= 0; i
< 3; ++i
) {
303 raw_string_ostream
StrOS(*Outputs
[i
]);
304 Metas
[i
]->printAsOperand(StrOS
, MST
);
308 void MIRPrinter::convert(yaml::MachineFunction
&MF
,
309 const MachineRegisterInfo
&RegInfo
,
310 const TargetRegisterInfo
*TRI
) {
311 MF
.TracksRegLiveness
= RegInfo
.tracksLiveness();
313 // Print the virtual register definitions.
314 for (unsigned I
= 0, E
= RegInfo
.getNumVirtRegs(); I
< E
; ++I
) {
315 Register Reg
= Register::index2VirtReg(I
);
316 yaml::VirtualRegisterDefinition VReg
;
318 if (RegInfo
.getVRegName(Reg
) != "")
320 ::printRegClassOrBank(Reg
, VReg
.Class
, RegInfo
, TRI
);
321 Register PreferredReg
= RegInfo
.getSimpleHint(Reg
);
323 printRegMIR(PreferredReg
, VReg
.PreferredRegister
, TRI
);
324 MF
.VirtualRegisters
.push_back(VReg
);
327 // Print the live ins.
328 for (std::pair
<unsigned, unsigned> LI
: RegInfo
.liveins()) {
329 yaml::MachineFunctionLiveIn LiveIn
;
330 printRegMIR(LI
.first
, LiveIn
.Register
, TRI
);
332 printRegMIR(LI
.second
, LiveIn
.VirtualRegister
, TRI
);
333 MF
.LiveIns
.push_back(LiveIn
);
336 // Prints the callee saved registers.
337 if (RegInfo
.isUpdatedCSRsInitialized()) {
338 const MCPhysReg
*CalleeSavedRegs
= RegInfo
.getCalleeSavedRegs();
339 std::vector
<yaml::FlowStringValue
> CalleeSavedRegisters
;
340 for (const MCPhysReg
*I
= CalleeSavedRegs
; *I
; ++I
) {
341 yaml::FlowStringValue Reg
;
342 printRegMIR(*I
, Reg
, TRI
);
343 CalleeSavedRegisters
.push_back(Reg
);
345 MF
.CalleeSavedRegisters
= CalleeSavedRegisters
;
349 void MIRPrinter::convert(ModuleSlotTracker
&MST
,
350 yaml::MachineFrameInfo
&YamlMFI
,
351 const MachineFrameInfo
&MFI
) {
352 YamlMFI
.IsFrameAddressTaken
= MFI
.isFrameAddressTaken();
353 YamlMFI
.IsReturnAddressTaken
= MFI
.isReturnAddressTaken();
354 YamlMFI
.HasStackMap
= MFI
.hasStackMap();
355 YamlMFI
.HasPatchPoint
= MFI
.hasPatchPoint();
356 YamlMFI
.StackSize
= MFI
.getStackSize();
357 YamlMFI
.OffsetAdjustment
= MFI
.getOffsetAdjustment();
358 YamlMFI
.MaxAlignment
= MFI
.getMaxAlign().value();
359 YamlMFI
.AdjustsStack
= MFI
.adjustsStack();
360 YamlMFI
.HasCalls
= MFI
.hasCalls();
361 YamlMFI
.MaxCallFrameSize
= MFI
.isMaxCallFrameSizeComputed()
362 ? MFI
.getMaxCallFrameSize() : ~0u;
363 YamlMFI
.CVBytesOfCalleeSavedRegisters
=
364 MFI
.getCVBytesOfCalleeSavedRegisters();
365 YamlMFI
.HasOpaqueSPAdjustment
= MFI
.hasOpaqueSPAdjustment();
366 YamlMFI
.HasVAStart
= MFI
.hasVAStart();
367 YamlMFI
.HasMustTailInVarArgFunc
= MFI
.hasMustTailInVarArgFunc();
368 YamlMFI
.HasTailCall
= MFI
.hasTailCall();
369 YamlMFI
.LocalFrameSize
= MFI
.getLocalFrameSize();
370 if (MFI
.getSavePoint()) {
371 raw_string_ostream
StrOS(YamlMFI
.SavePoint
.Value
);
372 StrOS
<< printMBBReference(*MFI
.getSavePoint());
374 if (MFI
.getRestorePoint()) {
375 raw_string_ostream
StrOS(YamlMFI
.RestorePoint
.Value
);
376 StrOS
<< printMBBReference(*MFI
.getRestorePoint());
380 void MIRPrinter::convertEntryValueObjects(yaml::MachineFunction
&YMF
,
381 const MachineFunction
&MF
,
382 ModuleSlotTracker
&MST
) {
383 const TargetRegisterInfo
*TRI
= MF
.getSubtarget().getRegisterInfo();
384 for (const MachineFunction::VariableDbgInfo
&DebugVar
:
385 MF
.getEntryValueVariableDbgInfo()) {
386 yaml::EntryValueObject
&Obj
= YMF
.EntryValueObjects
.emplace_back();
387 printStackObjectDbgInfo(DebugVar
, Obj
, MST
);
388 MCRegister EntryValReg
= DebugVar
.getEntryValueRegister();
389 printRegMIR(EntryValReg
, Obj
.EntryValueRegister
, TRI
);
393 void MIRPrinter::convertStackObjects(yaml::MachineFunction
&YMF
,
394 const MachineFunction
&MF
,
395 ModuleSlotTracker
&MST
) {
396 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
397 const TargetRegisterInfo
*TRI
= MF
.getSubtarget().getRegisterInfo();
399 // Process fixed stack objects.
400 assert(YMF
.FixedStackObjects
.empty());
401 SmallVector
<int, 32> FixedStackObjectsIdx
;
402 const int BeginIdx
= MFI
.getObjectIndexBegin();
404 FixedStackObjectsIdx
.reserve(-BeginIdx
);
407 for (int I
= BeginIdx
; I
< 0; ++I
, ++ID
) {
408 FixedStackObjectsIdx
.push_back(-1); // Fill index for possible dead.
409 if (MFI
.isDeadObjectIndex(I
))
412 yaml::FixedMachineStackObject YamlObject
;
414 YamlObject
.Type
= MFI
.isSpillSlotObjectIndex(I
)
415 ? yaml::FixedMachineStackObject::SpillSlot
416 : yaml::FixedMachineStackObject::DefaultType
;
417 YamlObject
.Offset
= MFI
.getObjectOffset(I
);
418 YamlObject
.Size
= MFI
.getObjectSize(I
);
419 YamlObject
.Alignment
= MFI
.getObjectAlign(I
);
420 YamlObject
.StackID
= (TargetStackID::Value
)MFI
.getStackID(I
);
421 YamlObject
.IsImmutable
= MFI
.isImmutableObjectIndex(I
);
422 YamlObject
.IsAliased
= MFI
.isAliasedObjectIndex(I
);
423 // Save the ID' position in FixedStackObjects storage vector.
424 FixedStackObjectsIdx
[ID
] = YMF
.FixedStackObjects
.size();
425 YMF
.FixedStackObjects
.push_back(YamlObject
);
426 StackObjectOperandMapping
.insert(
427 std::make_pair(I
, FrameIndexOperand::createFixed(ID
)));
430 // Process ordinary stack objects.
431 assert(YMF
.StackObjects
.empty());
432 SmallVector
<unsigned, 32> StackObjectsIdx
;
433 const int EndIdx
= MFI
.getObjectIndexEnd();
435 StackObjectsIdx
.reserve(EndIdx
);
437 for (int I
= 0; I
< EndIdx
; ++I
, ++ID
) {
438 StackObjectsIdx
.push_back(-1); // Fill index for possible dead.
439 if (MFI
.isDeadObjectIndex(I
))
442 yaml::MachineStackObject YamlObject
;
444 if (const auto *Alloca
= MFI
.getObjectAllocation(I
))
445 YamlObject
.Name
.Value
= std::string(
446 Alloca
->hasName() ? Alloca
->getName() : "");
447 YamlObject
.Type
= MFI
.isSpillSlotObjectIndex(I
)
448 ? yaml::MachineStackObject::SpillSlot
449 : MFI
.isVariableSizedObjectIndex(I
)
450 ? yaml::MachineStackObject::VariableSized
451 : yaml::MachineStackObject::DefaultType
;
452 YamlObject
.Offset
= MFI
.getObjectOffset(I
);
453 YamlObject
.Size
= MFI
.getObjectSize(I
);
454 YamlObject
.Alignment
= MFI
.getObjectAlign(I
);
455 YamlObject
.StackID
= (TargetStackID::Value
)MFI
.getStackID(I
);
457 // Save the ID' position in StackObjects storage vector.
458 StackObjectsIdx
[ID
] = YMF
.StackObjects
.size();
459 YMF
.StackObjects
.push_back(YamlObject
);
460 StackObjectOperandMapping
.insert(std::make_pair(
461 I
, FrameIndexOperand::create(YamlObject
.Name
.Value
, ID
)));
464 for (const auto &CSInfo
: MFI
.getCalleeSavedInfo()) {
465 const int FrameIdx
= CSInfo
.getFrameIdx();
466 if (!CSInfo
.isSpilledToReg() && MFI
.isDeadObjectIndex(FrameIdx
))
469 yaml::StringValue Reg
;
470 printRegMIR(CSInfo
.getReg(), Reg
, TRI
);
471 if (!CSInfo
.isSpilledToReg()) {
472 assert(FrameIdx
>= MFI
.getObjectIndexBegin() &&
473 FrameIdx
< MFI
.getObjectIndexEnd() &&
474 "Invalid stack object index");
475 if (FrameIdx
< 0) { // Negative index means fixed objects.
477 YMF
.FixedStackObjects
478 [FixedStackObjectsIdx
[FrameIdx
+ MFI
.getNumFixedObjects()]];
479 Object
.CalleeSavedRegister
= Reg
;
480 Object
.CalleeSavedRestored
= CSInfo
.isRestored();
482 auto &Object
= YMF
.StackObjects
[StackObjectsIdx
[FrameIdx
]];
483 Object
.CalleeSavedRegister
= Reg
;
484 Object
.CalleeSavedRestored
= CSInfo
.isRestored();
488 for (unsigned I
= 0, E
= MFI
.getLocalFrameObjectCount(); I
< E
; ++I
) {
489 auto LocalObject
= MFI
.getLocalFrameObjectMap(I
);
490 assert(LocalObject
.first
>= 0 && "Expected a locally mapped stack object");
491 YMF
.StackObjects
[StackObjectsIdx
[LocalObject
.first
]].LocalOffset
=
495 // Print the stack object references in the frame information class after
496 // converting the stack objects.
497 if (MFI
.hasStackProtectorIndex()) {
498 raw_string_ostream
StrOS(YMF
.FrameInfo
.StackProtector
.Value
);
499 MIPrinter(StrOS
, MST
, RegisterMaskIds
, StackObjectOperandMapping
)
500 .printStackObjectReference(MFI
.getStackProtectorIndex());
503 if (MFI
.hasFunctionContextIndex()) {
504 raw_string_ostream
StrOS(YMF
.FrameInfo
.FunctionContext
.Value
);
505 MIPrinter(StrOS
, MST
, RegisterMaskIds
, StackObjectOperandMapping
)
506 .printStackObjectReference(MFI
.getFunctionContextIndex());
509 // Print the debug variable information.
510 for (const MachineFunction::VariableDbgInfo
&DebugVar
:
511 MF
.getInStackSlotVariableDbgInfo()) {
512 int Idx
= DebugVar
.getStackSlot();
513 assert(Idx
>= MFI
.getObjectIndexBegin() && Idx
< MFI
.getObjectIndexEnd() &&
514 "Invalid stack object index");
515 if (Idx
< 0) { // Negative index means fixed objects.
517 YMF
.FixedStackObjects
[FixedStackObjectsIdx
[Idx
+
518 MFI
.getNumFixedObjects()]];
519 printStackObjectDbgInfo(DebugVar
, Object
, MST
);
521 auto &Object
= YMF
.StackObjects
[StackObjectsIdx
[Idx
]];
522 printStackObjectDbgInfo(DebugVar
, Object
, MST
);
527 void MIRPrinter::convertCallSiteObjects(yaml::MachineFunction
&YMF
,
528 const MachineFunction
&MF
,
529 ModuleSlotTracker
&MST
) {
530 const auto *TRI
= MF
.getSubtarget().getRegisterInfo();
531 for (auto CSInfo
: MF
.getCallSitesInfo()) {
532 yaml::CallSiteInfo YmlCS
;
533 yaml::CallSiteInfo::MachineInstrLoc CallLocation
;
535 // Prepare instruction position.
536 MachineBasicBlock::const_instr_iterator CallI
= CSInfo
.first
->getIterator();
537 CallLocation
.BlockNum
= CallI
->getParent()->getNumber();
538 // Get call instruction offset from the beginning of block.
539 CallLocation
.Offset
=
540 std::distance(CallI
->getParent()->instr_begin(), CallI
);
541 YmlCS
.CallLocation
= CallLocation
;
542 // Construct call arguments and theirs forwarding register info.
543 for (auto ArgReg
: CSInfo
.second
) {
544 yaml::CallSiteInfo::ArgRegPair YmlArgReg
;
545 YmlArgReg
.ArgNo
= ArgReg
.ArgNo
;
546 printRegMIR(ArgReg
.Reg
, YmlArgReg
.Reg
, TRI
);
547 YmlCS
.ArgForwardingRegs
.emplace_back(YmlArgReg
);
549 YMF
.CallSitesInfo
.push_back(YmlCS
);
552 // Sort call info by position of call instructions.
553 llvm::sort(YMF
.CallSitesInfo
.begin(), YMF
.CallSitesInfo
.end(),
554 [](yaml::CallSiteInfo A
, yaml::CallSiteInfo B
) {
555 if (A
.CallLocation
.BlockNum
== B
.CallLocation
.BlockNum
)
556 return A
.CallLocation
.Offset
< B
.CallLocation
.Offset
;
557 return A
.CallLocation
.BlockNum
< B
.CallLocation
.BlockNum
;
561 void MIRPrinter::convertMachineMetadataNodes(yaml::MachineFunction
&YMF
,
562 const MachineFunction
&MF
,
563 MachineModuleSlotTracker
&MST
) {
564 MachineModuleSlotTracker::MachineMDNodeListType MDList
;
565 MST
.collectMachineMDNodes(MDList
);
566 for (auto &MD
: MDList
) {
568 raw_string_ostream
StrOS(NS
);
569 MD
.second
->print(StrOS
, MST
, MF
.getFunction().getParent());
570 YMF
.MachineMetadataNodes
.push_back(StrOS
.str());
574 void MIRPrinter::convert(yaml::MachineFunction
&MF
,
575 const MachineConstantPool
&ConstantPool
) {
577 for (const MachineConstantPoolEntry
&Constant
: ConstantPool
.getConstants()) {
579 raw_string_ostream
StrOS(Str
);
580 if (Constant
.isMachineConstantPoolEntry()) {
581 Constant
.Val
.MachineCPVal
->print(StrOS
);
583 Constant
.Val
.ConstVal
->printAsOperand(StrOS
);
586 yaml::MachineConstantPoolValue YamlConstant
;
587 YamlConstant
.ID
= ID
++;
588 YamlConstant
.Value
= StrOS
.str();
589 YamlConstant
.Alignment
= Constant
.getAlign();
590 YamlConstant
.IsTargetSpecific
= Constant
.isMachineConstantPoolEntry();
592 MF
.Constants
.push_back(YamlConstant
);
596 void MIRPrinter::convert(ModuleSlotTracker
&MST
,
597 yaml::MachineJumpTable
&YamlJTI
,
598 const MachineJumpTableInfo
&JTI
) {
599 YamlJTI
.Kind
= JTI
.getEntryKind();
601 for (const auto &Table
: JTI
.getJumpTables()) {
603 yaml::MachineJumpTable::Entry Entry
;
605 for (const auto *MBB
: Table
.MBBs
) {
606 raw_string_ostream
StrOS(Str
);
607 StrOS
<< printMBBReference(*MBB
);
608 Entry
.Blocks
.push_back(StrOS
.str());
611 YamlJTI
.Entries
.push_back(Entry
);
615 void MIRPrinter::initRegisterMaskIds(const MachineFunction
&MF
) {
616 const auto *TRI
= MF
.getSubtarget().getRegisterInfo();
618 for (const uint32_t *Mask
: TRI
->getRegMasks())
619 RegisterMaskIds
.insert(std::make_pair(Mask
, I
++));
622 void llvm::guessSuccessors(const MachineBasicBlock
&MBB
,
623 SmallVectorImpl
<MachineBasicBlock
*> &Result
,
624 bool &IsFallthrough
) {
625 SmallPtrSet
<MachineBasicBlock
*,8> Seen
;
627 for (const MachineInstr
&MI
: MBB
) {
630 for (const MachineOperand
&MO
: MI
.operands()) {
633 MachineBasicBlock
*Succ
= MO
.getMBB();
634 auto RP
= Seen
.insert(Succ
);
636 Result
.push_back(Succ
);
639 MachineBasicBlock::const_iterator I
= MBB
.getLastNonDebugInstr();
640 IsFallthrough
= I
== MBB
.end() || !I
->isBarrier();
644 MIPrinter::canPredictBranchProbabilities(const MachineBasicBlock
&MBB
) const {
645 if (MBB
.succ_size() <= 1)
647 if (!MBB
.hasSuccessorProbabilities())
650 SmallVector
<BranchProbability
,8> Normalized(MBB
.Probs
.begin(),
652 BranchProbability::normalizeProbabilities(Normalized
.begin(),
654 SmallVector
<BranchProbability
,8> Equal(Normalized
.size());
655 BranchProbability::normalizeProbabilities(Equal
.begin(), Equal
.end());
657 return std::equal(Normalized
.begin(), Normalized
.end(), Equal
.begin());
660 bool MIPrinter::canPredictSuccessors(const MachineBasicBlock
&MBB
) const {
661 SmallVector
<MachineBasicBlock
*,8> GuessedSuccs
;
662 bool GuessedFallthrough
;
663 guessSuccessors(MBB
, GuessedSuccs
, GuessedFallthrough
);
664 if (GuessedFallthrough
) {
665 const MachineFunction
&MF
= *MBB
.getParent();
666 MachineFunction::const_iterator NextI
= std::next(MBB
.getIterator());
667 if (NextI
!= MF
.end()) {
668 MachineBasicBlock
*Next
= const_cast<MachineBasicBlock
*>(&*NextI
);
669 if (!is_contained(GuessedSuccs
, Next
))
670 GuessedSuccs
.push_back(Next
);
673 if (GuessedSuccs
.size() != MBB
.succ_size())
675 return std::equal(MBB
.succ_begin(), MBB
.succ_end(), GuessedSuccs
.begin());
678 void MIPrinter::print(const MachineBasicBlock
&MBB
) {
679 assert(MBB
.getNumber() >= 0 && "Invalid MBB number");
681 MachineBasicBlock::PrintNameIr
|
682 MachineBasicBlock::PrintNameAttributes
,
686 bool HasLineAttributes
= false;
687 // Print the successors
688 bool canPredictProbs
= canPredictBranchProbabilities(MBB
);
689 // Even if the list of successors is empty, if we cannot guess it,
690 // we need to print it to tell the parser that the list is empty.
691 // This is needed, because MI model unreachable as empty blocks
692 // with an empty successor list. If the parser would see that
693 // without the successor list, it would guess the code would
695 if ((!MBB
.succ_empty() && !SimplifyMIR
) || !canPredictProbs
||
696 !canPredictSuccessors(MBB
)) {
697 OS
.indent(2) << "successors: ";
698 for (auto I
= MBB
.succ_begin(), E
= MBB
.succ_end(); I
!= E
; ++I
) {
699 if (I
!= MBB
.succ_begin())
701 OS
<< printMBBReference(**I
);
702 if (!SimplifyMIR
|| !canPredictProbs
)
704 << format("0x%08" PRIx32
, MBB
.getSuccProbability(I
).getNumerator())
708 HasLineAttributes
= true;
711 // Print the live in registers.
712 const MachineRegisterInfo
&MRI
= MBB
.getParent()->getRegInfo();
713 if (!MBB
.livein_empty()) {
714 const TargetRegisterInfo
&TRI
= *MRI
.getTargetRegisterInfo();
715 OS
.indent(2) << "liveins: ";
717 for (const auto &LI
: MBB
.liveins_dbg()) {
721 OS
<< printReg(LI
.PhysReg
, &TRI
);
722 if (!LI
.LaneMask
.all())
723 OS
<< ":0x" << PrintLaneMask(LI
.LaneMask
);
726 HasLineAttributes
= true;
729 if (HasLineAttributes
)
731 bool IsInBundle
= false;
732 for (auto I
= MBB
.instr_begin(), E
= MBB
.instr_end(); I
!= E
; ++I
) {
733 const MachineInstr
&MI
= *I
;
734 if (IsInBundle
&& !MI
.isInsideBundle()) {
735 OS
.indent(2) << "}\n";
738 OS
.indent(IsInBundle
? 4 : 2);
740 if (!IsInBundle
&& MI
.getFlag(MachineInstr::BundledSucc
)) {
747 OS
.indent(2) << "}\n";
750 void MIPrinter::print(const MachineInstr
&MI
) {
751 const auto *MF
= MI
.getMF();
752 const auto &MRI
= MF
->getRegInfo();
753 const auto &SubTarget
= MF
->getSubtarget();
754 const auto *TRI
= SubTarget
.getRegisterInfo();
755 assert(TRI
&& "Expected target register info");
756 const auto *TII
= SubTarget
.getInstrInfo();
757 assert(TII
&& "Expected target instruction info");
758 if (MI
.isCFIInstruction())
759 assert(MI
.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
761 SmallBitVector
PrintedTypes(8);
762 bool ShouldPrintRegisterTies
= MI
.hasComplexRegisterTies();
763 unsigned I
= 0, E
= MI
.getNumOperands();
764 for (; I
< E
&& MI
.getOperand(I
).isReg() && MI
.getOperand(I
).isDef() &&
765 !MI
.getOperand(I
).isImplicit();
769 print(MI
, I
, TRI
, TII
, ShouldPrintRegisterTies
,
770 MI
.getTypeToPrint(I
, PrintedTypes
, MRI
),
776 if (MI
.getFlag(MachineInstr::FrameSetup
))
777 OS
<< "frame-setup ";
778 if (MI
.getFlag(MachineInstr::FrameDestroy
))
779 OS
<< "frame-destroy ";
780 if (MI
.getFlag(MachineInstr::FmNoNans
))
782 if (MI
.getFlag(MachineInstr::FmNoInfs
))
784 if (MI
.getFlag(MachineInstr::FmNsz
))
786 if (MI
.getFlag(MachineInstr::FmArcp
))
788 if (MI
.getFlag(MachineInstr::FmContract
))
790 if (MI
.getFlag(MachineInstr::FmAfn
))
792 if (MI
.getFlag(MachineInstr::FmReassoc
))
794 if (MI
.getFlag(MachineInstr::NoUWrap
))
796 if (MI
.getFlag(MachineInstr::NoSWrap
))
798 if (MI
.getFlag(MachineInstr::IsExact
))
800 if (MI
.getFlag(MachineInstr::NoFPExcept
))
802 if (MI
.getFlag(MachineInstr::NoMerge
))
804 if (MI
.getFlag(MachineInstr::Unpredictable
))
805 OS
<< "unpredictable ";
806 if (MI
.getFlag(MachineInstr::NoConvergent
))
807 OS
<< "noconvergent ";
809 OS
<< TII
->getName(MI
.getOpcode());
813 bool NeedComma
= false;
817 print(MI
, I
, TRI
, TII
, ShouldPrintRegisterTies
,
818 MI
.getTypeToPrint(I
, PrintedTypes
, MRI
));
822 // Print any optional symbols attached to this instruction as-if they were
824 if (MCSymbol
*PreInstrSymbol
= MI
.getPreInstrSymbol()) {
827 OS
<< " pre-instr-symbol ";
828 MachineOperand::printSymbol(OS
, *PreInstrSymbol
);
831 if (MCSymbol
*PostInstrSymbol
= MI
.getPostInstrSymbol()) {
834 OS
<< " post-instr-symbol ";
835 MachineOperand::printSymbol(OS
, *PostInstrSymbol
);
838 if (MDNode
*HeapAllocMarker
= MI
.getHeapAllocMarker()) {
841 OS
<< " heap-alloc-marker ";
842 HeapAllocMarker
->printAsOperand(OS
, MST
);
845 if (MDNode
*PCSections
= MI
.getPCSections()) {
848 OS
<< " pcsections ";
849 PCSections
->printAsOperand(OS
, MST
);
852 if (uint32_t CFIType
= MI
.getCFIType()) {
855 OS
<< " cfi-type " << CFIType
;
859 if (auto Num
= MI
.peekDebugInstrNum()) {
862 OS
<< " debug-instr-number " << Num
;
866 if (PrintLocations
) {
867 if (const DebugLoc
&DL
= MI
.getDebugLoc()) {
870 OS
<< " debug-location ";
871 DL
->printAsOperand(OS
, MST
);
875 if (!MI
.memoperands_empty()) {
877 const LLVMContext
&Context
= MF
->getFunction().getContext();
878 const MachineFrameInfo
&MFI
= MF
->getFrameInfo();
879 bool NeedComma
= false;
880 for (const auto *Op
: MI
.memoperands()) {
883 Op
->print(OS
, MST
, SSNs
, Context
, &MFI
, TII
);
889 void MIPrinter::printStackObjectReference(int FrameIndex
) {
890 auto ObjectInfo
= StackObjectOperandMapping
.find(FrameIndex
);
891 assert(ObjectInfo
!= StackObjectOperandMapping
.end() &&
892 "Invalid frame index");
893 const FrameIndexOperand
&Operand
= ObjectInfo
->second
;
894 MachineOperand::printStackObjectReference(OS
, Operand
.ID
, Operand
.IsFixed
,
898 static std::string
formatOperandComment(std::string Comment
) {
901 return std::string(" /* " + Comment
+ " */");
904 void MIPrinter::print(const MachineInstr
&MI
, unsigned OpIdx
,
905 const TargetRegisterInfo
*TRI
,
906 const TargetInstrInfo
*TII
,
907 bool ShouldPrintRegisterTies
, LLT TypeToPrint
,
909 const MachineOperand
&Op
= MI
.getOperand(OpIdx
);
910 std::string MOComment
= TII
->createMIROperandComment(MI
, Op
, OpIdx
, TRI
);
912 switch (Op
.getType()) {
913 case MachineOperand::MO_Immediate
:
914 if (MI
.isOperandSubregIdx(OpIdx
)) {
915 MachineOperand::printTargetFlags(OS
, Op
);
916 MachineOperand::printSubRegIdx(OS
, Op
.getImm(), TRI
);
920 case MachineOperand::MO_Register
:
921 case MachineOperand::MO_CImmediate
:
922 case MachineOperand::MO_FPImmediate
:
923 case MachineOperand::MO_MachineBasicBlock
:
924 case MachineOperand::MO_ConstantPoolIndex
:
925 case MachineOperand::MO_TargetIndex
:
926 case MachineOperand::MO_JumpTableIndex
:
927 case MachineOperand::MO_ExternalSymbol
:
928 case MachineOperand::MO_GlobalAddress
:
929 case MachineOperand::MO_RegisterLiveOut
:
930 case MachineOperand::MO_Metadata
:
931 case MachineOperand::MO_MCSymbol
:
932 case MachineOperand::MO_CFIIndex
:
933 case MachineOperand::MO_IntrinsicID
:
934 case MachineOperand::MO_Predicate
:
935 case MachineOperand::MO_BlockAddress
:
936 case MachineOperand::MO_DbgInstrRef
:
937 case MachineOperand::MO_ShuffleMask
: {
938 unsigned TiedOperandIdx
= 0;
939 if (ShouldPrintRegisterTies
&& Op
.isReg() && Op
.isTied() && !Op
.isDef())
940 TiedOperandIdx
= Op
.getParent()->findTiedOperandIdx(OpIdx
);
941 const TargetIntrinsicInfo
*TII
= MI
.getMF()->getTarget().getIntrinsicInfo();
942 Op
.print(OS
, MST
, TypeToPrint
, OpIdx
, PrintDef
, /*IsStandalone=*/false,
943 ShouldPrintRegisterTies
, TiedOperandIdx
, TRI
, TII
);
944 OS
<< formatOperandComment(MOComment
);
947 case MachineOperand::MO_FrameIndex
:
948 printStackObjectReference(Op
.getIndex());
950 case MachineOperand::MO_RegisterMask
: {
951 auto RegMaskInfo
= RegisterMaskIds
.find(Op
.getRegMask());
952 if (RegMaskInfo
!= RegisterMaskIds
.end())
953 OS
<< StringRef(TRI
->getRegMaskNames()[RegMaskInfo
->second
]).lower();
955 printCustomRegMask(Op
.getRegMask(), OS
, TRI
);
961 void MIRFormatter::printIRValue(raw_ostream
&OS
, const Value
&V
,
962 ModuleSlotTracker
&MST
) {
963 if (isa
<GlobalValue
>(V
)) {
964 V
.printAsOperand(OS
, /*PrintType=*/false, MST
);
967 if (isa
<Constant
>(V
)) {
968 // Machine memory operands can load/store to/from constant value pointers.
970 V
.printAsOperand(OS
, /*PrintType=*/true, MST
);
976 printLLVMNameWithoutPrefix(OS
, V
.getName());
979 int Slot
= MST
.getCurrentFunction() ? MST
.getLocalSlot(&V
) : -1;
980 MachineOperand::printIRSlotNumber(OS
, Slot
);
983 void llvm::printMIR(raw_ostream
&OS
, const Module
&M
) {
984 yaml::Output
Out(OS
);
985 Out
<< const_cast<Module
&>(M
);
988 void llvm::printMIR(raw_ostream
&OS
, const MachineFunction
&MF
) {
989 MIRPrinter
Printer(OS
);