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,20 @@ 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 {
52
54
53
55
uint64_t CurrentIdx = 0 ;
54
56
SmallVector<uint64_t , 4 > OnFailResumeAt;
57
+ NewMIVector OutMIs;
55
58
59
+ GISelChangeObserver *Observer = Builder.getObserver ();
56
60
// Bypass the flag check on the instruction, and only look at the MCInstrDesc.
57
61
bool NoFPException = !State.MIs [0 ]->getDesc ().mayRaiseFPException ();
58
62
@@ -71,14 +75,18 @@ bool GIMatchTableExecutor::executeMatchTable(
71
75
return RejectAndResume;
72
76
};
73
77
74
- auto propagateFlags = [=](NewMIVector &OutMIs ) {
78
+ const auto propagateFlags = [&]( ) {
75
79
for (auto MIB : OutMIs) {
76
80
// Set the NoFPExcept flag when no original matched instruction could
77
81
// raise an FP exception, but the new instruction potentially might.
78
82
uint16_t MIBFlags = Flags;
79
83
if (NoFPException && MIB->mayRaiseFPException ())
80
84
MIBFlags |= MachineInstr::NoFPExcept;
85
+ if (Observer)
86
+ Observer->changingInstr (*MIB);
81
87
MIB.setMIFlags (MIBFlags);
88
+ if (Observer)
89
+ Observer->changedInstr (*MIB);
82
90
}
83
91
84
92
return true ;
@@ -898,9 +906,13 @@ bool GIMatchTableExecutor::executeMatchTable(
898
906
if (NewInsnID >= OutMIs.size ())
899
907
OutMIs.resize (NewInsnID + 1 );
900
908
901
- OutMIs[NewInsnID] = MachineInstrBuilder (*State.MIs [OldInsnID]->getMF (),
902
- State.MIs [OldInsnID]);
909
+ MachineInstr *OldMI = State.MIs [OldInsnID];
910
+ if (Observer)
911
+ Observer->changingInstr (*OldMI);
912
+ OutMIs[NewInsnID] = MachineInstrBuilder (*OldMI->getMF (), OldMI);
903
913
OutMIs[NewInsnID]->setDesc (TII.get (NewOpcode));
914
+ if (Observer)
915
+ Observer->changedInstr (*OldMI);
904
916
DEBUG_WITH_TYPE (TgtExecutor::getName (),
905
917
dbgs () << CurrentIdx << " : GIR_MutateOpcode(OutMIs["
906
918
<< NewInsnID << " ], MIs[" << OldInsnID << " ], "
@@ -914,8 +926,7 @@ bool GIMatchTableExecutor::executeMatchTable(
914
926
if (NewInsnID >= OutMIs.size ())
915
927
OutMIs.resize (NewInsnID + 1 );
916
928
917
- OutMIs[NewInsnID] = BuildMI (*State.MIs [0 ]->getParent (), State.MIs [0 ],
918
- MIMetadata (*State.MIs [0 ]), TII.get (Opcode));
929
+ OutMIs[NewInsnID] = Builder.buildInstr (Opcode);
919
930
DEBUG_WITH_TYPE (TgtExecutor::getName (),
920
931
dbgs () << CurrentIdx << " : GIR_BuildMI(OutMIs["
921
932
<< NewInsnID << " ], " << Opcode << " )\n " );
@@ -1239,6 +1250,10 @@ bool GIMatchTableExecutor::executeMatchTable(
1239
1250
DEBUG_WITH_TYPE (TgtExecutor::getName (),
1240
1251
dbgs () << CurrentIdx << " : GIR_EraseFromParent(MIs["
1241
1252
<< InsnID << " ])\n " );
1253
+ // If we're erasing the insertion point, ensure we don't leave a dangling
1254
+ // pointer in the builder.
1255
+ if (Builder.getInsertPt () == MI)
1256
+ Builder.setInsertPt (*MI->getParent (), ++MI->getIterator ());
1242
1257
if (Observer)
1243
1258
Observer->erasingInstr (*MI);
1244
1259
MI->eraseFromParent ();
@@ -1309,11 +1324,7 @@ bool GIMatchTableExecutor::executeMatchTable(
1309
1324
case GIR_Done:
1310
1325
DEBUG_WITH_TYPE (TgtExecutor::getName (),
1311
1326
dbgs () << CurrentIdx << " : GIR_Done\n " );
1312
- if (Observer) {
1313
- for (MachineInstr *MI : OutMIs)
1314
- Observer->createdInstr (*MI);
1315
- }
1316
- propagateFlags (OutMIs);
1327
+ propagateFlags ();
1317
1328
return true ;
1318
1329
default :
1319
1330
llvm_unreachable (" Unexpected command" );
0 commit comments