17
17
#include " llvm/ADT/SmallBitVector.h"
18
18
#include " llvm/ADT/SmallPtrSet.h"
19
19
#include " llvm/ADT/SmallVector.h"
20
+ #include " llvm/ADT/StringExtras.h"
20
21
#include " llvm/ADT/StringRef.h"
21
22
#include " llvm/CodeGen/MIRYamlMapping.h"
22
23
#include " llvm/CodeGen/MachineBasicBlock.h"
@@ -93,10 +94,6 @@ struct FrameIndexOperand {
93
94
}
94
95
};
95
96
96
- } // end anonymous namespace
97
-
98
- namespace llvm {
99
-
100
97
// / This class prints out the machine functions using the MIR serialization
101
98
// / format.
102
99
class MIRPrinter {
@@ -151,7 +148,6 @@ class MIPrinter {
151
148
// / Synchronization scope names registered with LLVMContext.
152
149
SmallVector<StringRef, 8 > SSNs;
153
150
154
- bool canPredictBranchProbabilities (const MachineBasicBlock &MBB) const ;
155
151
bool canPredictSuccessors (const MachineBasicBlock &MBB) const ;
156
152
157
153
public:
@@ -167,14 +163,13 @@ class MIPrinter {
167
163
void printStackObjectReference (int FrameIndex);
168
164
void print (const MachineInstr &MI, unsigned OpIdx,
169
165
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 );
172
168
};
173
169
174
- } // end namespace llvm
170
+ } // end anonymous namespace
175
171
176
- namespace llvm {
177
- namespace yaml {
172
+ namespace llvm ::yaml {
178
173
179
174
// / This struct serializes the LLVM IR module.
180
175
template <> struct BlockScalarTraits <Module> {
@@ -188,8 +183,7 @@ template <> struct BlockScalarTraits<Module> {
188
183
}
189
184
};
190
185
191
- } // end namespace yaml
192
- } // end namespace llvm
186
+ } // end namespace llvm::yaml
193
187
194
188
static void printRegMIR (Register Reg, yaml::StringValue &Dest,
195
189
const TargetRegisterInfo *TRI) {
@@ -327,9 +321,8 @@ static void printRegFlags(Register Reg,
327
321
const MachineFunction &MF,
328
322
const TargetRegisterInfo *TRI) {
329
323
auto FlagValues = TRI->getVRegFlagsOfReg (Reg, MF);
330
- for (auto &Flag : FlagValues) {
324
+ for (auto &Flag : FlagValues)
331
325
RegisterFlags.push_back (yaml::FlowStringValue (Flag.str ()));
332
- }
333
326
}
334
327
335
328
void MIRPrinter::convert (yaml::MachineFunction &YamlMF,
@@ -618,9 +611,8 @@ void MIRPrinter::convertCalledGlobals(yaml::MachineFunction &YMF,
618
611
// Sort by position of call instructions.
619
612
llvm::sort (YMF.CalledGlobals .begin (), YMF.CalledGlobals .end (),
620
613
[](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 );
624
616
});
625
617
}
626
618
@@ -630,11 +622,10 @@ void MIRPrinter::convert(yaml::MachineFunction &MF,
630
622
for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants ()) {
631
623
std::string Str;
632
624
raw_string_ostream StrOS (Str);
633
- if (Constant.isMachineConstantPoolEntry ()) {
625
+ if (Constant.isMachineConstantPoolEntry ())
634
626
Constant.Val .MachineCPVal ->print (StrOS);
635
- } else {
627
+ else
636
628
Constant.Val .ConstVal ->printAsOperand (StrOS);
637
- }
638
629
639
630
yaml::MachineConstantPoolValue YamlConstant;
640
631
YamlConstant.ID = ID++;
@@ -693,23 +684,6 @@ void llvm::guessSuccessors(const MachineBasicBlock &MBB,
693
684
IsFallthrough = I == MBB.end () || !I->isBarrier ();
694
685
}
695
686
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
-
713
687
bool MIPrinter::canPredictSuccessors (const MachineBasicBlock &MBB) const {
714
688
SmallVector<MachineBasicBlock*,8 > GuessedSuccs;
715
689
bool GuessedFallthrough;
@@ -738,7 +712,7 @@ void MIPrinter::print(const MachineBasicBlock &MBB) {
738
712
739
713
bool HasLineAttributes = false ;
740
714
// Print the successors
741
- bool canPredictProbs = canPredictBranchProbabilities (MBB );
715
+ bool canPredictProbs = MBB. canPredictBranchProbabilities ();
742
716
// Even if the list of successors is empty, if we cannot guess it,
743
717
// we need to print it to tell the parser that the list is empty.
744
718
// This is needed, because MI model unreachable as empty blocks
@@ -750,14 +724,12 @@ void MIPrinter::print(const MachineBasicBlock &MBB) {
750
724
OS.indent (2 ) << " successors:" ;
751
725
if (!MBB.succ_empty ())
752
726
OS << " " ;
727
+ ListSeparator LS;
753
728
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);
757
730
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 ());
761
733
}
762
734
OS << " \n " ;
763
735
HasLineAttributes = true ;
@@ -768,12 +740,9 @@ void MIPrinter::print(const MachineBasicBlock &MBB) {
768
740
if (!MBB.livein_empty ()) {
769
741
const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo ();
770
742
OS.indent (2 ) << " liveins: " ;
771
- bool First = true ;
743
+ ListSeparator LS ;
772
744
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);
777
746
if (!LI.LaneMask .all ())
778
747
OS << " :0x" << PrintLaneMask (LI.LaneMask );
779
748
}
@@ -814,14 +783,14 @@ void MIPrinter::print(const MachineInstr &MI) {
814
783
815
784
SmallBitVector PrintedTypes (8 );
816
785
bool ShouldPrintRegisterTies = MI.hasComplexRegisterTies ();
786
+ ListSeparator LS;
817
787
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,
825
794
/* PrintDef=*/ false );
826
795
}
827
796
@@ -869,74 +838,48 @@ void MIPrinter::print(const MachineInstr &MI) {
869
838
OS << " samesign " ;
870
839
871
840
OS << TII->getName (MI.getOpcode ());
872
- if (I < E)
873
- OS << ' ' ;
874
841
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
+ }
882
850
}
883
851
884
852
// Print any optional symbols attached to this instruction as-if they were
885
853
// operands.
886
854
if (MCSymbol *PreInstrSymbol = MI.getPreInstrSymbol ()) {
887
- if (NeedComma)
888
- OS << ' ,' ;
889
- OS << " pre-instr-symbol " ;
855
+ OS << LS << " pre-instr-symbol " ;
890
856
MachineOperand::printSymbol (OS, *PreInstrSymbol);
891
- NeedComma = true ;
892
857
}
893
858
if (MCSymbol *PostInstrSymbol = MI.getPostInstrSymbol ()) {
894
- if (NeedComma)
895
- OS << ' ,' ;
896
- OS << " post-instr-symbol " ;
859
+ OS << LS << " post-instr-symbol " ;
897
860
MachineOperand::printSymbol (OS, *PostInstrSymbol);
898
- NeedComma = true ;
899
861
}
900
862
if (MDNode *HeapAllocMarker = MI.getHeapAllocMarker ()) {
901
- if (NeedComma)
902
- OS << ' ,' ;
903
- OS << " heap-alloc-marker " ;
863
+ OS << LS << " heap-alloc-marker " ;
904
864
HeapAllocMarker->printAsOperand (OS, MST);
905
- NeedComma = true ;
906
865
}
907
866
if (MDNode *PCSections = MI.getPCSections ()) {
908
- if (NeedComma)
909
- OS << ' ,' ;
910
- OS << " pcsections " ;
867
+ OS << LS << " pcsections " ;
911
868
PCSections->printAsOperand (OS, MST);
912
- NeedComma = true ;
913
869
}
914
870
if (MDNode *MMRA = MI.getMMRAMetadata ()) {
915
- if (NeedComma)
916
- OS << ' ,' ;
917
- OS << " mmra " ;
871
+ OS << LS << " mmra " ;
918
872
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 ;
926
873
}
874
+ if (uint32_t CFIType = MI.getCFIType ())
875
+ OS << LS << " cfi-type " << CFIType;
927
876
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;
934
879
935
880
if (PrintLocations) {
936
881
if (const DebugLoc &DL = MI.getDebugLoc ()) {
937
- if (NeedComma)
938
- OS << ' ,' ;
939
- OS << " debug-location " ;
882
+ OS << LS << " debug-location " ;
940
883
DL->printAsOperand (OS, MST);
941
884
}
942
885
}
@@ -945,12 +888,10 @@ void MIPrinter::print(const MachineInstr &MI) {
945
888
OS << " :: " ;
946
889
const LLVMContext &Context = MF->getFunction ().getContext ();
947
890
const MachineFrameInfo &MFI = MF->getFrameInfo ();
948
- bool NeedComma = false ;
891
+ LS = ListSeparator () ;
949
892
for (const auto *Op : MI.memoperands ()) {
950
- if (NeedComma)
951
- OS << " , " ;
893
+ OS << LS;
952
894
Op->print (OS, MST, SSNs, Context, &MFI, TII);
953
- NeedComma = true ;
954
895
}
955
896
}
956
897
}
@@ -971,10 +912,11 @@ static std::string formatOperandComment(std::string Comment) {
971
912
}
972
913
973
914
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);
978
920
const MachineOperand &Op = MI.getOperand (OpIdx);
979
921
std::string MOComment = TII->createMIROperandComment (MI, Op, OpIdx, TRI);
980
922
0 commit comments