1818#include " llvm/ADT/SmallVector.h"
1919#include " llvm/CodeGen/GlobalISel/GIMatchTableExecutor.h"
2020#include " llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
21+ #include " llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
2122#include " llvm/CodeGen/GlobalISel/Utils.h"
2223#include " llvm/CodeGen/MachineInstrBuilder.h"
2324#include " llvm/CodeGen/MachineOperand.h"
@@ -42,17 +43,20 @@ namespace llvm {
4243template <class TgtExecutor , class PredicateBitset , class ComplexMatcherMemFn ,
4344 class CustomRendererFn >
4445bool GIMatchTableExecutor::executeMatchTable (
45- TgtExecutor &Exec, NewMIVector &OutMIs, MatcherState &State,
46+ TgtExecutor &Exec, MatcherState &State,
4647 const ExecInfoTy<PredicateBitset, ComplexMatcherMemFn, CustomRendererFn>
4748 &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 {
5254
5355 uint64_t CurrentIdx = 0 ;
5456 SmallVector<uint64_t , 4 > OnFailResumeAt;
57+ NewMIVector OutMIs;
5558
59+ GISelChangeObserver *Observer = Builder.getObserver ();
5660 // Bypass the flag check on the instruction, and only look at the MCInstrDesc.
5761 bool NoFPException = !State.MIs [0 ]->getDesc ().mayRaiseFPException ();
5862
@@ -71,14 +75,18 @@ bool GIMatchTableExecutor::executeMatchTable(
7175 return RejectAndResume;
7276 };
7377
74- auto propagateFlags = [=](NewMIVector &OutMIs ) {
78+ const auto propagateFlags = [&]( ) {
7579 for (auto MIB : OutMIs) {
7680 // Set the NoFPExcept flag when no original matched instruction could
7781 // raise an FP exception, but the new instruction potentially might.
7882 uint16_t MIBFlags = Flags;
7983 if (NoFPException && MIB->mayRaiseFPException ())
8084 MIBFlags |= MachineInstr::NoFPExcept;
85+ if (Observer)
86+ Observer->changingInstr (*MIB);
8187 MIB.setMIFlags (MIBFlags);
88+ if (Observer)
89+ Observer->changedInstr (*MIB);
8290 }
8391
8492 return true ;
@@ -898,9 +906,13 @@ bool GIMatchTableExecutor::executeMatchTable(
898906 if (NewInsnID >= OutMIs.size ())
899907 OutMIs.resize (NewInsnID + 1 );
900908
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);
903913 OutMIs[NewInsnID]->setDesc (TII.get (NewOpcode));
914+ if (Observer)
915+ Observer->changedInstr (*OldMI);
904916 DEBUG_WITH_TYPE (TgtExecutor::getName (),
905917 dbgs () << CurrentIdx << " : GIR_MutateOpcode(OutMIs["
906918 << NewInsnID << " ], MIs[" << OldInsnID << " ], "
@@ -914,8 +926,7 @@ bool GIMatchTableExecutor::executeMatchTable(
914926 if (NewInsnID >= OutMIs.size ())
915927 OutMIs.resize (NewInsnID + 1 );
916928
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);
919930 DEBUG_WITH_TYPE (TgtExecutor::getName (),
920931 dbgs () << CurrentIdx << " : GIR_BuildMI(OutMIs["
921932 << NewInsnID << " ], " << Opcode << " )\n " );
@@ -1239,6 +1250,10 @@ bool GIMatchTableExecutor::executeMatchTable(
12391250 DEBUG_WITH_TYPE (TgtExecutor::getName (),
12401251 dbgs () << CurrentIdx << " : GIR_EraseFromParent(MIs["
12411252 << 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 ());
12421257 if (Observer)
12431258 Observer->erasingInstr (*MI);
12441259 MI->eraseFromParent ();
@@ -1309,11 +1324,7 @@ bool GIMatchTableExecutor::executeMatchTable(
13091324 case GIR_Done:
13101325 DEBUG_WITH_TYPE (TgtExecutor::getName (),
13111326 dbgs () << CurrentIdx << " : GIR_Done\n " );
1312- if (Observer) {
1313- for (MachineInstr *MI : OutMIs)
1314- Observer->createdInstr (*MI);
1315- }
1316- propagateFlags (OutMIs);
1327+ propagateFlags ();
13171328 return true ;
13181329 default :
13191330 llvm_unreachable (" Unexpected command" );
0 commit comments