Skip to content

Commit e744de2

Browse files
jurahulGeorgeARM
authored andcommitted
[NFC][LLVM][CodeGen] Refactor MIR Printer (llvm#137361)
- Move `MIPrinter` class to anonymous namespace, and remove it as a friend of `MachineBasicBlock`. - Move `canPredictBranchProbabilities` to `MachineBasicBlock` and change it to use the new `BranchProbability::normalizeProbabilities` function that accepts a range, and also to use `llvm::equal()` to check equality of the two vectors. - Use `ListSeparator` to print comma separate lists instead of manual code to do that.
1 parent d7a08f0 commit e744de2

File tree

5 files changed

+89
-127
lines changed

5 files changed

+89
-127
lines changed

llvm/include/llvm/CodeGen/MachineBasicBlock.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1263,14 +1263,16 @@ class MachineBasicBlock
12631263
/// MachineBranchProbabilityInfo class.
12641264
BranchProbability getSuccProbability(const_succ_iterator Succ) const;
12651265

1266+
// Helper function for MIRPrinter.
1267+
bool canPredictBranchProbabilities() const;
1268+
12661269
private:
12671270
/// Return probability iterator corresponding to the I successor iterator.
12681271
probability_iterator getProbabilityIterator(succ_iterator I);
12691272
const_probability_iterator
12701273
getProbabilityIterator(const_succ_iterator I) const;
12711274

12721275
friend class MachineBranchProbabilityInfo;
1273-
friend class MIPrinter;
12741276

12751277
// Methods used to maintain doubly linked list of blocks...
12761278
friend struct ilist_callback_traits<MachineBasicBlock>;

llvm/include/llvm/Support/BranchProbability.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef LLVM_SUPPORT_BRANCHPROBABILITY_H
1414
#define LLVM_SUPPORT_BRANCHPROBABILITY_H
1515

16+
#include "llvm/ADT/ADL.h"
1617
#include "llvm/Support/DataTypes.h"
1718
#include <algorithm>
1819
#include <cassert>
@@ -62,6 +63,11 @@ class BranchProbability {
6263
static void normalizeProbabilities(ProbabilityIter Begin,
6364
ProbabilityIter End);
6465

66+
template <class ProbabilityContainer>
67+
static void normalizeProbabilities(ProbabilityContainer &&R) {
68+
normalizeProbabilities(adl_begin(R), adl_end(R));
69+
}
70+
6571
uint32_t getNumerator() const { return N; }
6672
static uint32_t getDenominator() { return D; }
6773

llvm/lib/CodeGen/MIRPrinter.cpp

Lines changed: 50 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/ADT/SmallBitVector.h"
1818
#include "llvm/ADT/SmallPtrSet.h"
1919
#include "llvm/ADT/SmallVector.h"
20+
#include "llvm/ADT/StringExtras.h"
2021
#include "llvm/ADT/StringRef.h"
2122
#include "llvm/CodeGen/MIRYamlMapping.h"
2223
#include "llvm/CodeGen/MachineBasicBlock.h"
@@ -93,10 +94,6 @@ struct FrameIndexOperand {
9394
}
9495
};
9596

96-
} // end anonymous namespace
97-
98-
namespace llvm {
99-
10097
/// This class prints out the machine functions using the MIR serialization
10198
/// format.
10299
class MIRPrinter {
@@ -151,7 +148,6 @@ class MIPrinter {
151148
/// Synchronization scope names registered with LLVMContext.
152149
SmallVector<StringRef, 8> SSNs;
153150

154-
bool canPredictBranchProbabilities(const MachineBasicBlock &MBB) const;
155151
bool canPredictSuccessors(const MachineBasicBlock &MBB) const;
156152

157153
public:
@@ -167,14 +163,13 @@ class MIPrinter {
167163
void printStackObjectReference(int FrameIndex);
168164
void print(const MachineInstr &MI, unsigned OpIdx,
169165
const TargetRegisterInfo *TRI, const TargetInstrInfo *TII,
170-
bool ShouldPrintRegisterTies, LLT TypeToPrint,
171-
bool PrintDef = true);
166+
bool ShouldPrintRegisterTies, SmallBitVector &PrintedTypes,
167+
const MachineRegisterInfo &MRI, bool PrintDef = true);
172168
};
173169

174-
} // end namespace llvm
170+
} // end anonymous namespace
175171

176-
namespace llvm {
177-
namespace yaml {
172+
namespace llvm::yaml {
178173

179174
/// This struct serializes the LLVM IR module.
180175
template <> struct BlockScalarTraits<Module> {
@@ -188,8 +183,7 @@ template <> struct BlockScalarTraits<Module> {
188183
}
189184
};
190185

191-
} // end namespace yaml
192-
} // end namespace llvm
186+
} // end namespace llvm::yaml
193187

194188
static void printRegMIR(Register Reg, yaml::StringValue &Dest,
195189
const TargetRegisterInfo *TRI) {
@@ -327,9 +321,8 @@ static void printRegFlags(Register Reg,
327321
const MachineFunction &MF,
328322
const TargetRegisterInfo *TRI) {
329323
auto FlagValues = TRI->getVRegFlagsOfReg(Reg, MF);
330-
for (auto &Flag : FlagValues) {
324+
for (auto &Flag : FlagValues)
331325
RegisterFlags.push_back(yaml::FlowStringValue(Flag.str()));
332-
}
333326
}
334327

335328
void MIRPrinter::convert(yaml::MachineFunction &YamlMF,
@@ -618,9 +611,8 @@ void MIRPrinter::convertCalledGlobals(yaml::MachineFunction &YMF,
618611
// Sort by position of call instructions.
619612
llvm::sort(YMF.CalledGlobals.begin(), YMF.CalledGlobals.end(),
620613
[](yaml::CalledGlobal A, yaml::CalledGlobal B) {
621-
if (A.CallSite.BlockNum == B.CallSite.BlockNum)
622-
return A.CallSite.Offset < B.CallSite.Offset;
623-
return A.CallSite.BlockNum < B.CallSite.BlockNum;
614+
return std::tie(A.CallSite.BlockNum, A.CallSite.Offset) <
615+
std::tie(B.CallSite.BlockNum, B.CallSite.Offset);
624616
});
625617
}
626618

@@ -630,11 +622,10 @@ void MIRPrinter::convert(yaml::MachineFunction &MF,
630622
for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
631623
std::string Str;
632624
raw_string_ostream StrOS(Str);
633-
if (Constant.isMachineConstantPoolEntry()) {
625+
if (Constant.isMachineConstantPoolEntry())
634626
Constant.Val.MachineCPVal->print(StrOS);
635-
} else {
627+
else
636628
Constant.Val.ConstVal->printAsOperand(StrOS);
637-
}
638629

639630
yaml::MachineConstantPoolValue YamlConstant;
640631
YamlConstant.ID = ID++;
@@ -693,23 +684,6 @@ void llvm::guessSuccessors(const MachineBasicBlock &MBB,
693684
IsFallthrough = I == MBB.end() || !I->isBarrier();
694685
}
695686

696-
bool
697-
MIPrinter::canPredictBranchProbabilities(const MachineBasicBlock &MBB) const {
698-
if (MBB.succ_size() <= 1)
699-
return true;
700-
if (!MBB.hasSuccessorProbabilities())
701-
return true;
702-
703-
SmallVector<BranchProbability,8> Normalized(MBB.Probs.begin(),
704-
MBB.Probs.end());
705-
BranchProbability::normalizeProbabilities(Normalized.begin(),
706-
Normalized.end());
707-
SmallVector<BranchProbability,8> Equal(Normalized.size());
708-
BranchProbability::normalizeProbabilities(Equal.begin(), Equal.end());
709-
710-
return std::equal(Normalized.begin(), Normalized.end(), Equal.begin());
711-
}
712-
713687
bool MIPrinter::canPredictSuccessors(const MachineBasicBlock &MBB) const {
714688
SmallVector<MachineBasicBlock*,8> GuessedSuccs;
715689
bool GuessedFallthrough;
@@ -738,7 +712,7 @@ void MIPrinter::print(const MachineBasicBlock &MBB) {
738712

739713
bool HasLineAttributes = false;
740714
// Print the successors
741-
bool canPredictProbs = canPredictBranchProbabilities(MBB);
715+
bool canPredictProbs = MBB.canPredictBranchProbabilities();
742716
// Even if the list of successors is empty, if we cannot guess it,
743717
// we need to print it to tell the parser that the list is empty.
744718
// This is needed, because MI model unreachable as empty blocks
@@ -750,14 +724,12 @@ void MIPrinter::print(const MachineBasicBlock &MBB) {
750724
OS.indent(2) << "successors:";
751725
if (!MBB.succ_empty())
752726
OS << " ";
727+
ListSeparator LS;
753728
for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
754-
if (I != MBB.succ_begin())
755-
OS << ", ";
756-
OS << printMBBReference(**I);
729+
OS << LS << printMBBReference(**I);
757730
if (!SimplifyMIR || !canPredictProbs)
758-
OS << '('
759-
<< format("0x%08" PRIx32, MBB.getSuccProbability(I).getNumerator())
760-
<< ')';
731+
OS << format("(0x%08" PRIx32 ")",
732+
MBB.getSuccProbability(I).getNumerator());
761733
}
762734
OS << "\n";
763735
HasLineAttributes = true;
@@ -768,12 +740,9 @@ void MIPrinter::print(const MachineBasicBlock &MBB) {
768740
if (!MBB.livein_empty()) {
769741
const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
770742
OS.indent(2) << "liveins: ";
771-
bool First = true;
743+
ListSeparator LS;
772744
for (const auto &LI : MBB.liveins_dbg()) {
773-
if (!First)
774-
OS << ", ";
775-
First = false;
776-
OS << printReg(LI.PhysReg, &TRI);
745+
OS << LS << printReg(LI.PhysReg, &TRI);
777746
if (!LI.LaneMask.all())
778747
OS << ":0x" << PrintLaneMask(LI.LaneMask);
779748
}
@@ -814,14 +783,14 @@ void MIPrinter::print(const MachineInstr &MI) {
814783

815784
SmallBitVector PrintedTypes(8);
816785
bool ShouldPrintRegisterTies = MI.hasComplexRegisterTies();
786+
ListSeparator LS;
817787
unsigned I = 0, E = MI.getNumOperands();
818-
for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
819-
!MI.getOperand(I).isImplicit();
820-
++I) {
821-
if (I)
822-
OS << ", ";
823-
print(MI, I, TRI, TII, ShouldPrintRegisterTies,
824-
MI.getTypeToPrint(I, PrintedTypes, MRI),
788+
for (; I < E; ++I) {
789+
const MachineOperand MO = MI.getOperand(I);
790+
if (!MO.isReg() || !MO.isDef() || MO.isImplicit())
791+
break;
792+
OS << LS;
793+
print(MI, I, TRI, TII, ShouldPrintRegisterTies, PrintedTypes, MRI,
825794
/*PrintDef=*/false);
826795
}
827796

@@ -869,74 +838,48 @@ void MIPrinter::print(const MachineInstr &MI) {
869838
OS << "samesign ";
870839

871840
OS << TII->getName(MI.getOpcode());
872-
if (I < E)
873-
OS << ' ';
874841

875-
bool NeedComma = false;
876-
for (; I < E; ++I) {
877-
if (NeedComma)
878-
OS << ", ";
879-
print(MI, I, TRI, TII, ShouldPrintRegisterTies,
880-
MI.getTypeToPrint(I, PrintedTypes, MRI));
881-
NeedComma = true;
842+
LS = ListSeparator();
843+
844+
if (I < E) {
845+
OS << ' ';
846+
for (; I < E; ++I) {
847+
OS << LS;
848+
print(MI, I, TRI, TII, ShouldPrintRegisterTies, PrintedTypes, MRI);
849+
}
882850
}
883851

884852
// Print any optional symbols attached to this instruction as-if they were
885853
// operands.
886854
if (MCSymbol *PreInstrSymbol = MI.getPreInstrSymbol()) {
887-
if (NeedComma)
888-
OS << ',';
889-
OS << " pre-instr-symbol ";
855+
OS << LS << " pre-instr-symbol ";
890856
MachineOperand::printSymbol(OS, *PreInstrSymbol);
891-
NeedComma = true;
892857
}
893858
if (MCSymbol *PostInstrSymbol = MI.getPostInstrSymbol()) {
894-
if (NeedComma)
895-
OS << ',';
896-
OS << " post-instr-symbol ";
859+
OS << LS << " post-instr-symbol ";
897860
MachineOperand::printSymbol(OS, *PostInstrSymbol);
898-
NeedComma = true;
899861
}
900862
if (MDNode *HeapAllocMarker = MI.getHeapAllocMarker()) {
901-
if (NeedComma)
902-
OS << ',';
903-
OS << " heap-alloc-marker ";
863+
OS << LS << " heap-alloc-marker ";
904864
HeapAllocMarker->printAsOperand(OS, MST);
905-
NeedComma = true;
906865
}
907866
if (MDNode *PCSections = MI.getPCSections()) {
908-
if (NeedComma)
909-
OS << ',';
910-
OS << " pcsections ";
867+
OS << LS << " pcsections ";
911868
PCSections->printAsOperand(OS, MST);
912-
NeedComma = true;
913869
}
914870
if (MDNode *MMRA = MI.getMMRAMetadata()) {
915-
if (NeedComma)
916-
OS << ',';
917-
OS << " mmra ";
871+
OS << LS << " mmra ";
918872
MMRA->printAsOperand(OS, MST);
919-
NeedComma = true;
920-
}
921-
if (uint32_t CFIType = MI.getCFIType()) {
922-
if (NeedComma)
923-
OS << ',';
924-
OS << " cfi-type " << CFIType;
925-
NeedComma = true;
926873
}
874+
if (uint32_t CFIType = MI.getCFIType())
875+
OS << LS << " cfi-type " << CFIType;
927876

928-
if (auto Num = MI.peekDebugInstrNum()) {
929-
if (NeedComma)
930-
OS << ',';
931-
OS << " debug-instr-number " << Num;
932-
NeedComma = true;
933-
}
877+
if (auto Num = MI.peekDebugInstrNum())
878+
OS << LS << " debug-instr-number " << Num;
934879

935880
if (PrintLocations) {
936881
if (const DebugLoc &DL = MI.getDebugLoc()) {
937-
if (NeedComma)
938-
OS << ',';
939-
OS << " debug-location ";
882+
OS << LS << " debug-location ";
940883
DL->printAsOperand(OS, MST);
941884
}
942885
}
@@ -945,12 +888,10 @@ void MIPrinter::print(const MachineInstr &MI) {
945888
OS << " :: ";
946889
const LLVMContext &Context = MF->getFunction().getContext();
947890
const MachineFrameInfo &MFI = MF->getFrameInfo();
948-
bool NeedComma = false;
891+
LS = ListSeparator();
949892
for (const auto *Op : MI.memoperands()) {
950-
if (NeedComma)
951-
OS << ", ";
893+
OS << LS;
952894
Op->print(OS, MST, SSNs, Context, &MFI, TII);
953-
NeedComma = true;
954895
}
955896
}
956897
}
@@ -971,10 +912,11 @@ static std::string formatOperandComment(std::string Comment) {
971912
}
972913

973914
void MIPrinter::print(const MachineInstr &MI, unsigned OpIdx,
974-
const TargetRegisterInfo *TRI,
975-
const TargetInstrInfo *TII,
976-
bool ShouldPrintRegisterTies, LLT TypeToPrint,
977-
bool PrintDef) {
915+
const TargetRegisterInfo *TRI, const TargetInstrInfo *TII,
916+
bool ShouldPrintRegisterTies,
917+
SmallBitVector &PrintedTypes,
918+
const MachineRegisterInfo &MRI, bool PrintDef) {
919+
LLT TypeToPrint = MI.getTypeToPrint(OpIdx, PrintedTypes, MRI);
978920
const MachineOperand &Op = MI.getOperand(OpIdx);
979921
std::string MOComment = TII->createMIROperandComment(MI, Op, OpIdx, TRI);
980922

llvm/lib/CodeGen/MIRPrintingPass.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ char MIRPrintingPass::ID = 0;
8181
char &llvm::MIRPrintingPassID = MIRPrintingPass::ID;
8282
INITIALIZE_PASS(MIRPrintingPass, "mir-printer", "MIR Printer", false, false)
8383

84-
namespace llvm {
85-
86-
MachineFunctionPass *createPrintMIRPass(raw_ostream &OS) {
84+
MachineFunctionPass *llvm::createPrintMIRPass(raw_ostream &OS) {
8785
return new MIRPrintingPass(OS);
8886
}
89-
90-
} // end namespace llvm

0 commit comments

Comments
 (0)