18
18
#include " llvm/ADT/SmallVector.h"
19
19
#include " llvm/CodeGen/GlobalISel/GIMatchTableExecutor.h"
20
20
#include " llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
21
+ #include " llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
21
22
#include " llvm/CodeGen/GlobalISel/Utils.h"
22
23
#include " llvm/CodeGen/MachineInstrBuilder.h"
23
24
#include " llvm/CodeGen/MachineOperand.h"
@@ -42,17 +43,33 @@ namespace llvm {
42
43
template <class TgtExecutor , class PredicateBitset , class ComplexMatcherMemFn ,
43
44
class CustomRendererFn >
44
45
bool GIMatchTableExecutor::executeMatchTable (
45
- TgtExecutor &Exec, NewMIVector &OutMIs, MatcherState &State,
46
+ TgtExecutor &Exec, MatcherState &State,
46
47
const ExecInfoTy<PredicateBitset, ComplexMatcherMemFn, CustomRendererFn>
47
48
&ExecInfo,
48
- const int64_t *MatchTable, const TargetInstrInfo &TII,
49
- MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
50
- const RegisterBankInfo &RBI, const PredicateBitset &AvailableFeatures,
51
- CodeGenCoverage *CoverageInfo, GISelChangeObserver *Observer) const {
49
+ MachineIRBuilder &Builder, const int64_t *MatchTable,
50
+ const TargetInstrInfo &TII, MachineRegisterInfo &MRI,
51
+ const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI,
52
+ const PredicateBitset &AvailableFeatures,
53
+ CodeGenCoverage *CoverageInfo) const {
54
+
55
+ // Setup observer
56
+ GIMatchTableObserver MTObserver;
57
+ GISelObserverWrapper Observer (&MTObserver);
58
+ if (auto *CurObs = Builder.getObserver ())
59
+ Observer.addObserver (CurObs);
60
+
61
+ // TODO: Set MF delegate?
62
+
63
+ // Setup builder.
64
+ auto RestoreOldObserver = Builder.setTemporaryChangeObserver (Observer);
52
65
53
66
uint64_t CurrentIdx = 0 ;
54
67
SmallVector<uint64_t , 4 > OnFailResumeAt;
55
68
69
+ // We also record MachineInstrs manually in this vector so opcodes can address
70
+ // them.
71
+ SmallVector<MachineInstrBuilder, 4 > OutMIs;
72
+
56
73
// Bypass the flag check on the instruction, and only look at the MCInstrDesc.
57
74
bool NoFPException = !State.MIs [0 ]->getDesc ().mayRaiseFPException ();
58
75
@@ -71,14 +88,16 @@ bool GIMatchTableExecutor::executeMatchTable(
71
88
return RejectAndResume;
72
89
};
73
90
74
- auto propagateFlags = [=](NewMIVector &OutMIs ) {
75
- for (auto MIB : OutMIs ) {
91
+ auto propagateFlags = [&]( ) {
92
+ for (auto *MI : MTObserver. CreatedInsts ) {
76
93
// Set the NoFPExcept flag when no original matched instruction could
77
94
// raise an FP exception, but the new instruction potentially might.
78
95
uint16_t MIBFlags = Flags;
79
- if (NoFPException && MIB ->mayRaiseFPException ())
96
+ if (NoFPException && MI ->mayRaiseFPException ())
80
97
MIBFlags |= MachineInstr::NoFPExcept;
81
- MIB.setMIFlags (MIBFlags);
98
+ Observer.changingInstr (*MI);
99
+ MI->setFlags (MIBFlags);
100
+ Observer.changedInstr (*MI);
82
101
}
83
102
84
103
return true ;
@@ -901,6 +920,7 @@ bool GIMatchTableExecutor::executeMatchTable(
901
920
OutMIs[NewInsnID] = MachineInstrBuilder (*State.MIs [OldInsnID]->getMF (),
902
921
State.MIs [OldInsnID]);
903
922
OutMIs[NewInsnID]->setDesc (TII.get (NewOpcode));
923
+ MTObserver.CreatedInsts .insert (OutMIs[NewInsnID]);
904
924
DEBUG_WITH_TYPE (TgtExecutor::getName (),
905
925
dbgs () << CurrentIdx << " : GIR_MutateOpcode(OutMIs["
906
926
<< NewInsnID << " ], MIs[" << OldInsnID << " ], "
@@ -914,8 +934,7 @@ bool GIMatchTableExecutor::executeMatchTable(
914
934
if (NewInsnID >= OutMIs.size ())
915
935
OutMIs.resize (NewInsnID + 1 );
916
936
917
- OutMIs[NewInsnID] = BuildMI (*State.MIs [0 ]->getParent (), State.MIs [0 ],
918
- MIMetadata (*State.MIs [0 ]), TII.get (Opcode));
937
+ OutMIs[NewInsnID] = Builder.buildInstr (Opcode);
919
938
DEBUG_WITH_TYPE (TgtExecutor::getName (),
920
939
dbgs () << CurrentIdx << " : GIR_BuildMI(OutMIs["
921
940
<< NewInsnID << " ], " << Opcode << " )\n " );
@@ -1239,8 +1258,11 @@ bool GIMatchTableExecutor::executeMatchTable(
1239
1258
DEBUG_WITH_TYPE (TgtExecutor::getName (),
1240
1259
dbgs () << CurrentIdx << " : GIR_EraseFromParent(MIs["
1241
1260
<< InsnID << " ])\n " );
1242
- if (Observer)
1243
- Observer->erasingInstr (*MI);
1261
+ // If we're erasing the insertion point, ensure we don't leave a dangling
1262
+ // pointer in the builder.
1263
+ if (Builder.getInsertPt () == MI)
1264
+ Builder.setInsertPt (*MI->getParent (), ++MI->getIterator ());
1265
+ Observer.erasingInstr (*MI);
1244
1266
MI->eraseFromParent ();
1245
1267
break ;
1246
1268
}
@@ -1269,11 +1291,9 @@ bool GIMatchTableExecutor::executeMatchTable(
1269
1291
1270
1292
Register Old = State.MIs [OldInsnID]->getOperand (OldOpIdx).getReg ();
1271
1293
Register New = State.MIs [NewInsnID]->getOperand (NewOpIdx).getReg ();
1272
- if (Observer)
1273
- Observer->changingAllUsesOfReg (MRI, Old);
1294
+ Observer.changingAllUsesOfReg (MRI, Old);
1274
1295
MRI.replaceRegWith (Old, New);
1275
- if (Observer)
1276
- Observer->finishedChangingAllUsesOfReg ();
1296
+ Observer.finishedChangingAllUsesOfReg ();
1277
1297
break ;
1278
1298
}
1279
1299
case GIR_ReplaceRegWithTempReg: {
@@ -1288,11 +1308,9 @@ bool GIMatchTableExecutor::executeMatchTable(
1288
1308
1289
1309
Register Old = State.MIs [OldInsnID]->getOperand (OldOpIdx).getReg ();
1290
1310
Register New = State.TempRegisters [TempRegID];
1291
- if (Observer)
1292
- Observer->changingAllUsesOfReg (MRI, Old);
1311
+ Observer.changingAllUsesOfReg (MRI, Old);
1293
1312
MRI.replaceRegWith (Old, New);
1294
- if (Observer)
1295
- Observer->finishedChangingAllUsesOfReg ();
1313
+ Observer.finishedChangingAllUsesOfReg ();
1296
1314
break ;
1297
1315
}
1298
1316
case GIR_Coverage: {
@@ -1309,11 +1327,7 @@ bool GIMatchTableExecutor::executeMatchTable(
1309
1327
case GIR_Done:
1310
1328
DEBUG_WITH_TYPE (TgtExecutor::getName (),
1311
1329
dbgs () << CurrentIdx << " : GIR_Done\n " );
1312
- if (Observer) {
1313
- for (MachineInstr *MI : OutMIs)
1314
- Observer->createdInstr (*MI);
1315
- }
1316
- propagateFlags (OutMIs);
1330
+ propagateFlags ();
1317
1331
return true ;
1318
1332
default :
1319
1333
llvm_unreachable (" Unexpected command" );
0 commit comments