[llvm] [LICM] Allow hoisting of InsertElementInst's past non-hoistable InsertElementInsts (PR #200532)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 8 07:11:25 PDT 2026


================
@@ -1058,6 +1074,82 @@ bool llvm::hoistRegion(DomTreeNode *N, AAResults *AA, LoopInfo *LI,
   return Changed;
 }
 
+static InsertElementInst *canBypassInsert(InsertElementInst *CurrIns,
+                                          Loop *CurLoop,
+                                          SmallSet<uint64_t, 4> &SeenIndices) {
+  // Must have constant insertion lane
+  auto *InsertedIdxCI = dyn_cast<ConstantInt>(CurrIns->getOperand(2));
+  if (!InsertedIdxCI)
+    return nullptr;
+  auto *VecTy = cast<VectorType>(CurrIns->getType());
+
+  // Avoid hoisting past out of bounds inserts
+  if (InsertedIdxCI->isNegative() ||
+      InsertedIdxCI->getValue().uge(
+          VecTy->getElementCount().getKnownMinValue()))
+    return nullptr;
+
+  // Make sure not hoisting past insertions into the same lane
+  if (!SeenIndices.insert(InsertedIdxCI->getValue().getLimitedValue()).second)
+    return nullptr;
+
+  Value *InnerVal = CurrIns->getOperand(0);
+  // If the value we are inserting into is not invariant/poison, recurse
+  // if it is another insert
+  if (!CurLoop->isLoopInvariant(InnerVal) && !isa<PoisonValue>(InnerVal)) {
----------------
lukel97 wrote:

Do we need the `isa<PoisonValue>` check here? I think the `dyn_cast<InsertElementInst>` check means it can't be poison anyway. Might be better writing this as like


```suggestion
  if (auto *InnerIns = dyn_cast<InsertElementInst>(InnerVal)) {
    if (CurLoop->isLoopInvariant(InnerIns))
      return nullptr;
      ...
```

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


More information about the llvm-commits mailing list