diff --git a/llvm/lib/CodeGen/MachineBasicBlock.cpp b/llvm/lib/CodeGen/MachineBasicBlock.cpp index 14d9bb292ddf2..5f9e4a66c0d22 100644 --- a/llvm/lib/CodeGen/MachineBasicBlock.cpp +++ b/llvm/lib/CodeGen/MachineBasicBlock.cpp @@ -1101,6 +1101,7 @@ class SlotIndexUpdateDelegate : public MachineFunction::Delegate { private: MachineFunction &MF; SlotIndexes *Indexes; + SmallSetVector Insertions; public: SlotIndexUpdateDelegate(MachineFunction &MF, SlotIndexes *Indexes) @@ -1108,15 +1109,20 @@ class SlotIndexUpdateDelegate : public MachineFunction::Delegate { MF.setDelegate(this); } - ~SlotIndexUpdateDelegate() { MF.resetDelegate(this); } + ~SlotIndexUpdateDelegate() { + MF.resetDelegate(this); + for (auto MI : Insertions) + Indexes->insertMachineInstrInMaps(*MI); + } void MF_HandleInsertion(MachineInstr &MI) override { + // This is called before MI is inserted into block so defer index update. if (Indexes) - Indexes->insertMachineInstrInMaps(MI); + Insertions.insert(&MI); } void MF_HandleRemoval(MachineInstr &MI) override { - if (Indexes) + if (Indexes && !Insertions.remove(&MI)) Indexes->removeMachineInstrFromMaps(MI); } };