[llvm] [SCEVExpander] Do not insert after callbr (PR #201322)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 3 04:26:57 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Le Gia Bao (L3GiaBao)

<details>
<summary>Changes</summary>

findInsertPointAfter handle invoke and otherwise use the instruction after def instruction as the insertion point.

This is not valid for callbr which is a terminator. Advancing the iterator moves the insertion point past the terminator, and the following code uses that invalid position, e.g when asm goto lowers to callbr in #<!-- -->200987

Use the insertion point before MustDominate for callbr.

Fixes #<!-- -->200987

---
Full diff: https://github.com/llvm/llvm-project/pull/201322.diff


1 Files Affected:

- (modified) llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp (+11-2) 


``````````diff
diff --git a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
index 39da38e491817..d21fb4339b664 100644
--- a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
+++ b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
@@ -147,9 +147,18 @@ Value *SCEVExpander::ReuseOrCreateCast(Value *V, Type *Ty,
 BasicBlock::iterator
 SCEVExpander::findInsertPointAfter(Instruction *I,
                                    Instruction *MustDominate) const {
-  BasicBlock::iterator IP = ++I->getIterator();
-  if (auto *II = dyn_cast<InvokeInst>(I))
+  BasicBlock::iterator IP;
+  if (auto *II = dyn_cast<InvokeInst>(I)) {
     IP = II->getNormalDest()->begin();
+  } else if (isa<CallBrInst>(I)) {
+    // callbr is a terminator, so there is no valid same-block point after it.
+    // Use the point we need the result to dominate.
+    assert(SE.DT.dominates(I, MustDominate) &&
+           "callbr result must dominate the insertion point");
+    IP = MustDominate->getIterator();
+  } else {
+    IP = ++I->getIterator();
+  }
 
   while (isa<PHINode>(IP))
     ++IP;

``````````

</details>


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


More information about the llvm-commits mailing list