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

Ryan Buchner via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 8 08:49:58 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)) {
----------------
bababuck wrote:

Good catch, I don't think the poison element is needed. Not sure if we can reorder the two checks because we would miss cases like:
```
  %1 = insertelement <4 x i32> poison, i32 %x, i32 1
loop:
  %2 = insertelement <4 x i32> poison, i32 %y, i32 2
  %3 = insertelement <4 x i32> poison, i32 %invariant, i32 3
```

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


More information about the llvm-commits mailing list