[llvm] Add llvm.looptrap intrinsic. (PR #181299)

Florian Mayer via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 13 13:06:44 PST 2026


================
@@ -631,6 +632,36 @@ static bool expandCondLoop(Function &Intr) {
   return true;
 }
 
+static bool expandLoopTrap(Function &Intr) {
+  for (User *U : make_early_inc_range(Intr.users())) {
+    auto *Call = cast<CallInst>(U);
+    if (!Call->getParent()->isEntryBlock() &&
+        std::all_of(Call->getParent()->begin(), BasicBlock::iterator(Call),
+                    [](Instruction &I) { return !I.mayHaveSideEffects(); })) {
+      for (auto *BB : predecessors(Call->getParent())) {
+        auto *BI = dyn_cast<BranchInst>(BB->getTerminator());
+        if (!BI || BI->isUnconditional())
+          continue;
+        IRBuilder<> B(BI);
+        Value *Cond;
+        if (BI->getSuccessor(0) == Call->getParent()) {
----------------
fmayer wrote:

I would add some comment. Like:

```
// The looptrap is on the true branch. We insert the conditional cond loop
// before the branch, and force the branch to take the false branch (because
// we never reach the branch if condition is true.
```

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


More information about the llvm-commits mailing list