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:
@@ -171,10 +167,9 @@ class MIPrinter {
171
167
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,18 +783,22 @@ void MIPrinter::print(const MachineInstr &MI) {
814
783
815
784
SmallBitVector PrintedTypes (8 );
816
785
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),
825
798
/* PrintDef=*/ false );
826
799
}
827
800
828
- if (I )
801
+ if (NumDefs != 0 )
829
802
OS << " = " ;
830
803
if (MI.getFlag (MachineInstr::FrameSetup))
831
804
OS << " frame-setup " ;
@@ -869,74 +842,51 @@ void MIPrinter::print(const MachineInstr &MI) {
869
842
OS << " samesign " ;
870
843
871
844
OS << TII->getName (MI.getOpcode ());
872
- if (I < E)
873
- OS << ' ' ;
874
845
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
+ }
882
857
}
883
858
884
859
// Print any optional symbols attached to this instruction as-if they were
885
860
// operands.
886
861
if (MCSymbol *PreInstrSymbol = MI.getPreInstrSymbol ()) {
887
- if (NeedComma)
888
- OS << ' ,' ;
889
- OS << " pre-instr-symbol " ;
862
+ OS << LS << " pre-instr-symbol " ;
890
863
MachineOperand::printSymbol (OS, *PreInstrSymbol);
891
- NeedComma = true ;
892
864
}
893
865
if (MCSymbol *PostInstrSymbol = MI.getPostInstrSymbol ()) {
894
- if (NeedComma)
895
- OS << ' ,' ;
896
- OS << " post-instr-symbol " ;
866
+ OS << LS << " post-instr-symbol " ;
897
867
MachineOperand::printSymbol (OS, *PostInstrSymbol);
898
- NeedComma = true ;
899
868
}
900
869
if (MDNode *HeapAllocMarker = MI.getHeapAllocMarker ()) {
901
- if (NeedComma)
902
- OS << ' ,' ;
903
- OS << " heap-alloc-marker " ;
870
+ OS << LS << " heap-alloc-marker " ;
904
871
HeapAllocMarker->printAsOperand (OS, MST);
905
- NeedComma = true ;
906
872
}
907
873
if (MDNode *PCSections = MI.getPCSections ()) {
908
- if (NeedComma)
909
- OS << ' ,' ;
910
- OS << " pcsections " ;
874
+ OS << LS << " pcsections " ;
911
875
PCSections->printAsOperand (OS, MST);
912
- NeedComma = true ;
913
876
}
914
877
if (MDNode *MMRA = MI.getMMRAMetadata ()) {
915
- if (NeedComma)
916
- OS << ' ,' ;
917
- OS << " mmra " ;
878
+ OS << LS << " mmra " ;
918
879
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
880
}
881
+ if (uint32_t CFIType = MI.getCFIType ())
882
+ OS << LS << " cfi-type " << CFIType;
927
883
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;
934
886
935
887
if (PrintLocations) {
936
888
if (const DebugLoc &DL = MI.getDebugLoc ()) {
937
- if (NeedComma)
938
- OS << ' ,' ;
939
- OS << " debug-location " ;
889
+ OS << LS << " debug-location " ;
940
890
DL->printAsOperand (OS, MST);
941
891
}
942
892
}
@@ -945,12 +895,10 @@ void MIPrinter::print(const MachineInstr &MI) {
945
895
OS << " :: " ;
946
896
const LLVMContext &Context = MF->getFunction ().getContext ();
947
897
const MachineFrameInfo &MFI = MF->getFrameInfo ();
948
- bool NeedComma = false ;
898
+ LS. reset () ;
949
899
for (const auto *Op : MI.memoperands ()) {
950
- if (NeedComma)
951
- OS << " , " ;
900
+ OS << LS;
952
901
Op->print (OS, MST, SSNs, Context, &MFI, TII);
953
- NeedComma = true ;
954
902
}
955
903
}
956
904
}
0 commit comments