16
16
#include " llvm/CodeGen/GlobalISel/Legalizer.h"
17
17
#include " llvm/ADT/PostOrderIterator.h"
18
18
#include " llvm/ADT/SetVector.h"
19
+ #include " llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
19
20
#include " llvm/CodeGen/GlobalISel/GISelWorkList.h"
20
21
#include " llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h"
21
22
#include " llvm/CodeGen/GlobalISel/LegalizerHelper.h"
@@ -67,6 +68,42 @@ static bool isArtifact(const MachineInstr &MI) {
67
68
return true ;
68
69
}
69
70
}
71
+ using InstListTy = GISelWorkList<256 >;
72
+ using ArtifactListTy = GISelWorkList<128 >;
73
+
74
+ class LegalizerWorkListManager : public GISelChangeObserver {
75
+ InstListTy &InstList;
76
+ ArtifactListTy &ArtifactList;
77
+
78
+ public:
79
+ LegalizerWorkListManager (InstListTy &Insts, ArtifactListTy &Arts)
80
+ : InstList(Insts), ArtifactList(Arts) {}
81
+
82
+ void createdInstr (MachineInstr &MI) override {
83
+ // Only legalize pre-isel generic instructions.
84
+ // Legalization process could generate Target specific pseudo
85
+ // instructions with generic types. Don't record them
86
+ if (isPreISelGenericOpcode (MI.getOpcode ())) {
87
+ if (isArtifact (MI))
88
+ ArtifactList.insert (&MI);
89
+ else
90
+ InstList.insert (&MI);
91
+ }
92
+ LLVM_DEBUG (dbgs () << " .. .. New MI: " << MI;);
93
+ }
94
+
95
+ void erasedInstr (MachineInstr &MI) override {
96
+ InstList.remove (&MI);
97
+ ArtifactList.remove (&MI);
98
+ }
99
+
100
+ void changedInstr (MachineInstr &MI) override {
101
+ // When insts change, we want to revisit them to legalize them again.
102
+ // We'll consider them the same as created.
103
+ LLVM_DEBUG (dbgs () << " .. .. Changed MI: " << MI;);
104
+ createdInstr (MI);
105
+ }
106
+ };
70
107
71
108
bool Legalizer::runOnMachineFunction (MachineFunction &MF) {
72
109
// If the ISel pipeline failed, do not bother running that pass.
@@ -77,14 +114,13 @@ bool Legalizer::runOnMachineFunction(MachineFunction &MF) {
77
114
init (MF);
78
115
const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
79
116
MachineOptimizationRemarkEmitter MORE (MF, /* MBFI=*/ nullptr );
80
- LegalizerHelper Helper (MF);
81
117
82
118
const size_t NumBlocks = MF.size ();
83
119
MachineRegisterInfo &MRI = MF.getRegInfo ();
84
120
85
121
// Populate Insts
86
- GISelWorkList< 256 > InstList;
87
- GISelWorkList< 128 > ArtifactList;
122
+ InstListTy InstList;
123
+ ArtifactListTy ArtifactList;
88
124
ReversePostOrderTraversal<MachineFunction *> RPOT (&MF);
89
125
// Perform legalization bottom up so we can DCE as we legalize.
90
126
// Traverse BB in RPOT and within each basic block, add insts top down,
@@ -103,24 +139,12 @@ bool Legalizer::runOnMachineFunction(MachineFunction &MF) {
103
139
InstList.insert (&MI);
104
140
}
105
141
}
106
- Helper.MIRBuilder .recordInsertions ([&](MachineInstr *MI) {
107
- // Only legalize pre-isel generic instructions.
108
- // Legalization process could generate Target specific pseudo
109
- // instructions with generic types. Don't record them
110
- if (isPreISelGenericOpcode (MI->getOpcode ())) {
111
- if (isArtifact (*MI))
112
- ArtifactList.insert (MI);
113
- else
114
- InstList.insert (MI);
115
- }
116
- LLVM_DEBUG (dbgs () << " .. .. New MI: " << *MI;);
117
- });
142
+ LegalizerWorkListManager WorkListObserver (InstList, ArtifactList);
143
+ LegalizerHelper Helper (MF, WorkListObserver);
118
144
const LegalizerInfo &LInfo (Helper.getLegalizerInfo ());
119
145
LegalizationArtifactCombiner ArtCombiner (Helper.MIRBuilder , MF.getRegInfo (), LInfo);
120
- auto RemoveDeadInstFromLists = [&InstList,
121
- &ArtifactList](MachineInstr *DeadMI) {
122
- InstList.remove (DeadMI);
123
- ArtifactList.remove (DeadMI);
146
+ auto RemoveDeadInstFromLists = [&WorkListObserver](MachineInstr *DeadMI) {
147
+ WorkListObserver.erasedInstr (*DeadMI);
124
148
};
125
149
bool Changed = false ;
126
150
do {
@@ -138,7 +162,7 @@ bool Legalizer::runOnMachineFunction(MachineFunction &MF) {
138
162
// Error out if we couldn't legalize this instruction. We may want to
139
163
// fall back to DAG ISel instead in the future.
140
164
if (Res == LegalizerHelper::UnableToLegalize) {
141
- Helper.MIRBuilder .stopRecordingInsertions ();
165
+ Helper.MIRBuilder .stopObservingChanges ();
142
166
reportGISelFailure (MF, TPC, MORE, " gisel-legalize" ,
143
167
" unable to legalize instruction" , MI);
144
168
return false ;
0 commit comments