[llvm] [PeepholeOptimizer] Fix use-after-free of recycled MachineInstr in LocalMIs (PR #201285)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 5 09:15:28 PDT 2026


================
@@ -1761,9 +1761,11 @@ bool PeepholeOptimizer::run(MachineFunction &MF) {
     // "given a pointer to an MI in the current BB, is it located before or
     // after the current instruction".
     // To perform this, the following set keeps track of the MIs already seen
-    // during the scan, if a MI is not in the set, it is assumed to be located
-    // after. Newly created MIs have to be inserted in the set as well.
+    // during the scan; if a MI is not in the set, it is assumed to be located
+    // after.
     SmallPtrSet<MachineInstr *, 16> LocalMIs;
+    SaveAndRestore<SmallPtrSetImpl<MachineInstr *> *> RestoreCurLocalMIs(
----------------
lijinpei-amd wrote:

Yes. Alternatively, I can do the followings without including `SaveAndRestore.h`
```
SmallPtrSet<MachineInstr *, 16> LocalMIs;
CurLocalMIs = &LocalMIs;
for (......) {
  LocalMIs.clear();
}
CurLocalMIs = nullptr;
```
Say the word if the alternative way is more preferable.

https://github.com/llvm/llvm-project/pull/201285


More information about the llvm-commits mailing list