[llvm] [MachineCSE] Walk the terminator the hoisted copy is placed in front of (PR #215158)

Matt Turner via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 2 05:27:56 PDT 2026


================
@@ -0,0 +1,62 @@
+# RUN: llc -mtriple=x86_64-unknown-linux-gnu -run-pass=machine-cse -verify-machineinstrs -o - %s | FileCheck %s
+
+# Partial redundancy elimination hoists a computation into the nearest common
+# dominator of the two blocks holding it, in front of that block's first
+# terminator.  A computation that reads a physical register may only be hoisted
+# if the register still holds the same value where the copy lands, and the walk
+# that checks that has to start at the terminator rather than after it: the copy
+# goes in front of the terminator, so whatever the terminator clobbers lies
+# between the copy and the original.
+#
+# EH_SjLj_Setup is such a terminator.  It carries a register mask preserving
+# nothing, which is what keeps a value from staying in a register across a
+# setjmp, and it names the block a returning longjmp resumes at, so its block
+# has a fallthrough successor as well.  The rip-relative lea below must not be
+# hoisted in front of it.
+
+--- |
----------------
mattst88 wrote:

AFAICT, the IR section is necessary. `ProcessBlockPRE` only reaches the hoist when both blocks holding the computation have an IR basic block:

```cpp
auto BB = MBB->getBasicBlock(), BB1 = MBB1->getBasicBlock();
if (BB != nullptr && BB1 != nullptr && (isPotentiallyReachable(BB1, BB) || ...
```

Without it, `getBasicBlock()` is null for every block and the pass bails before the hoist. I tried it: with the names dropped, reverting the fix still leaves the LEA unhoisted, so the test passes either way. I added a note saying why the IR section is here.

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


More information about the llvm-commits mailing list