Skip to content

Commit 18d289c

Browse files
committed
[NFC][LLVM][MI] Refactor MI Printer code.
- 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. Also added `ListSeparator::reset()` to enable reuse of the same ListSeparator object for multiple lists.
1 parent ea443ee commit 18d289c

File tree

6 files changed

+94
-123
lines changed

6 files changed

+94
-123
lines changed

llvm/include/llvm/ADT/StringExtras.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,9 @@ class ListSeparator {
537537
}
538538
return Separator;
539539
}
540+
541+
// Enables the use of same ListSeparator object for multiple lists.
542+
void reset() { First = true; }
540543
};
541544

542545
/// A forward iterator over partitions of string over a separator.

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 ProbabilityRange>
67+
static void normalizeProbabilities(ProbabilityRange &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: 52 additions & 104 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:
@@ -171,10 +167,9 @@ class MIPrinter {
171167
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,18 +783,22 @@ void MIPrinter::print(const MachineInstr &MI) {
814783

815784
SmallBitVector PrintedTypes(8);
816785
bool ShouldPrintRegisterTies = MI.hasComplexRegisterTies();
817-
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),
786+
ListSeparator LS;
787+
unsigned NumDefs = MI.getNumExplicitDefs();
788+
789+
// For PATCHPOINT, the output register is optional. Also see
790+
// MachineVerifier::visitMachineOperand which does a similar fixup.
791+
if (NumDefs != 0 && MI.getOpcode() == TargetOpcode::PATCHPOINT)
792+
NumDefs = MI.getOperand(0).isReg() ? NumDefs : 0;
793+
794+
for (unsigned Idx : llvm::seq(NumDefs)) {
795+
OS << LS;
796+
print(MI, Idx, TRI, TII, ShouldPrintRegisterTies,
797+
MI.getTypeToPrint(Idx, PrintedTypes, MRI),
825798
/*PrintDef=*/false);
826799
}
827800

828-
if (I)
801+
if (NumDefs != 0)
829802
OS << " = ";
830803
if (MI.getFlag(MachineInstr::FrameSetup))
831804
OS << "frame-setup ";
@@ -869,74 +842,51 @@ void MIPrinter::print(const MachineInstr &MI) {
869842
OS << "samesign ";
870843

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

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;
846+
const unsigned NumOperands = MI.getNumOperands();
847+
const unsigned NumUses = NumOperands - NumDefs;
848+
LS.reset();
849+
850+
if (NumUses != 0) {
851+
OS << ' ';
852+
for (unsigned Idx : llvm::seq<unsigned>(NumDefs, NumOperands)) {
853+
OS << LS;
854+
print(MI, Idx, TRI, TII, ShouldPrintRegisterTies,
855+
MI.getTypeToPrint(Idx, PrintedTypes, MRI));
856+
}
882857
}
883858

884859
// Print any optional symbols attached to this instruction as-if they were
885860
// operands.
886861
if (MCSymbol *PreInstrSymbol = MI.getPreInstrSymbol()) {
887-
if (NeedComma)
888-
OS << ',';
889-
OS << " pre-instr-symbol ";
862+
OS << LS << " pre-instr-symbol ";
890863
MachineOperand::printSymbol(OS, *PreInstrSymbol);
891-
NeedComma = true;
892864
}
893865
if (MCSymbol *PostInstrSymbol = MI.getPostInstrSymbol()) {
894-
if (NeedComma)
895-
OS << ',';
896-
OS << " post-instr-symbol ";
866+
OS << LS << " post-instr-symbol ";
897867
MachineOperand::printSymbol(OS, *PostInstrSymbol);
898-
NeedComma = true;
899868
}
900869
if (MDNode *HeapAllocMarker = MI.getHeapAllocMarker()) {
901-
if (NeedComma)
902-
OS << ',';
903-
OS << " heap-alloc-marker ";
870+
OS << LS << " heap-alloc-marker ";
904871
HeapAllocMarker->printAsOperand(OS, MST);
905-
NeedComma = true;
906872
}
907873
if (MDNode *PCSections = MI.getPCSections()) {
908-
if (NeedComma)
909-
OS << ',';
910-
OS << " pcsections ";
874+
OS << LS << " pcsections ";
911875
PCSections->printAsOperand(OS, MST);
912-
NeedComma = true;
913876
}
914877
if (MDNode *MMRA = MI.getMMRAMetadata()) {
915-
if (NeedComma)
916-
OS << ',';
917-
OS << " mmra ";
878+
OS << LS << " mmra ";
918879
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;
926880
}
881+
if (uint32_t CFIType = MI.getCFIType())
882+
OS << LS << " cfi-type " << CFIType;
927883

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

935887
if (PrintLocations) {
936888
if (const DebugLoc &DL = MI.getDebugLoc()) {
937-
if (NeedComma)
938-
OS << ',';
939-
OS << " debug-location ";
889+
OS << LS << " debug-location ";
940890
DL->printAsOperand(OS, MST);
941891
}
942892
}
@@ -945,12 +895,10 @@ void MIPrinter::print(const MachineInstr &MI) {
945895
OS << " :: ";
946896
const LLVMContext &Context = MF->getFunction().getContext();
947897
const MachineFrameInfo &MFI = MF->getFrameInfo();
948-
bool NeedComma = false;
898+
LS.reset();
949899
for (const auto *Op : MI.memoperands()) {
950-
if (NeedComma)
951-
OS << ", ";
900+
OS << LS;
952901
Op->print(OS, MST, SSNs, Context, &MFI, TII);
953-
NeedComma = true;
954902
}
955903
}
956904
}

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)