[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 1 02:36:19 PDT 2026


================
@@ -1058,6 +1074,87 @@ bool llvm::hoistRegion(DomTreeNode *N, AAResults *AA, LoopInfo *LI,
   return Changed;
 }
 
+static bool hoistInsertPastInsert(
+    InsertElementInst *Ins, Loop *CurLoop, AAResults *AA, DominatorTree *DT,
+    const TargetLibraryInfo *TLI, BasicBlock *Preheader, BasicBlock *HoistDest,
+    ICFLoopSafetyInfo *SafetyInfo, MemorySSAUpdater &MSSAU, AssumptionCache *AC,
+    ScalarEvolution *SE, SinkAndHoistLICMFlags &Flags,
+    OptimizationRemarkEmitter *ORE,
+    SmallVectorImpl<Instruction *> &HoistedInstructions,
+    bool AllowSpeculation) {
+  SmallSet<uint64_t, 4> SeenIndexes;
+  InsertElementInst *Inner;
+  auto CanBypass = [&](auto &CanBypass, InsertElementInst *CurrIns,
+                       bool IsHoistedInstruction) -> bool {
+    // Instruction being hoisted past must only have one use
+    if (!IsHoistedInstruction && !CurrIns->hasOneUse())
+      return false;
+
+    // Must have constant insertion lane
+    auto *InsertedIdxCI = dyn_cast<ConstantInt>(CurrIns->getOperand(2));
+    if (!InsertedIdxCI)
+      return false;
+    auto *VecTy = cast<VectorType>(CurrIns->getType());
+
+    // Avoid hoisting past out of bounds inserts
+    if (InsertedIdxCI->isNegative() ||
+        InsertedIdxCI->getValue().uge(
+            VecTy->getElementCount().getKnownMinValue()))
+      return false;
----------------
lukel97 wrote:

An out of bounds insert is poison, so do we need this check? Can we just treat it as if it was in bounds?

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


More information about the llvm-commits mailing list