From 386f3903910aa1897c424ced8e1af7993a6df5ce Mon Sep 17 00:00:00 2001 From: Carl Ritson Date: Sat, 21 Oct 2023 16:31:19 +0900 Subject: [MachineBasicBlock] Fix SlotIndexUpdater for insertion order (#69424) Follow up fix for #68786 to address that MachineFunction handleInsertion is actually called before a new instruction has been inserted into the block. Hence new instructions must be recorded and SlotIndex updates performed after the delegate call. --- llvm/lib/CodeGen/MachineBasicBlock.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'llvm/lib/CodeGen/MachineBasicBlock.cpp') diff --git a/llvm/lib/CodeGen/MachineBasicBlock.cpp b/llvm/lib/CodeGen/MachineBasicBlock.cpp index 14d9bb29..5f9e4a6 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 @@ public: 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); } }; -- cgit v1.1