[llvm] [LoopFusion] Fix sink instructions (PR #147501)

Michael Kruse via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 21 04:05:46 PDT 2025


================
@@ -1176,6 +1176,27 @@ struct LoopFuser {
     return true;
   }
 
+  // This function fixes sunk PHI nodes after fusion.
+  void fixPHINodes(SmallVector<Instruction *, 4> &SafeToSink,
+                   const FusionCandidate &FC0,
+                   const FusionCandidate &FC1) const {
+    // Iterate over SafeToSink instructions and update PHI nodes
+    // to take values from the latch block of FC0 if they are taking
+    // from the latch block of FC1.
+    for (Instruction *Inst : SafeToSink) {
+      // Continue if the instruction is not a PHI node.
+      if (!isa<PHINode>(Inst))
+        continue;
+      PHINode *Phi = dyn_cast<PHINode>(Inst);
----------------
Meinersbur wrote:

```suggestion
      PHINode *Phi = dyn_cast<PHINode>(Inst);
      if (!Phi)
        continue;
```
You don't need to `dyn_cast` if you already checked it's the right type. `cast` is sufficient. It's also an idiom to check the result of `dyn_cast` for nullptr.

The comment isn't helpful
<img src="https://github.com/user-attachments/assets/c9d469ba-5842-44b7-9eee-860d4a321b90" />[^1]

[^1]: I think this is my new favorite meme

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


More information about the llvm-commits mailing list