[llvm] [LoopVectorize] Widen selected-base loads with masked loads (PR #213227)

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 3 07:56:35 PDT 2026


================
@@ -57,6 +57,93 @@ static std::optional<int64_t> getConstantStride(VPValue *Addr, Type *AccessTy,
   return getStrideFromAddRec(AddRec, L, AccessTy, /*Ptr=*/nullptr, PSE);
 }
 
+namespace {
+
+struct SelectedBaseLoadPattern {
+  VPInstruction *Addr;
+  SmallVector<VPValue *> GEPOperands;
+  VPValue *Cond;
+  VPValue *TrueBase;
+  VPValue *FalseBase;
+};
+
+/// Match a load through a GEP whose base pointer is selected per iteration
+/// between two loop-invariant candidates:
+///
+///   load (gep (select Cond, TrueBase, FalseBase), Indices...)
+///
+/// Legality, stride and profitability are checked separately by the caller.
+static std::optional<SelectedBaseLoadPattern>
+matchSelectedBaseLoad(VPInstruction *Load) {
+  if (Load->getOpcode() != Instruction::Load)
+    return std::nullopt;
+
+  // exclude volatile load, atomic load, other uneditable memory operations
+  auto *LI = dyn_cast_or_null<LoadInst>(Load->getUnderlyingInstr());
+  if (!LI || !LI->isSimple())
+    return std::nullopt;
+
+  VPValue *Addr = Load->getOperand(0);
+  auto *AddrI = dyn_cast<VPInstruction>(Addr);
+  if (!AddrI)
+    return std::nullopt;
+
+  Type *SourceElementTy = nullptr;
+  ArrayRef<VPValue *> GEPOperands;
+  // pattern with 3 params : cond + trueBase + falseBase
+  if (!match(Addr, m_GetElementPtr(SourceElementTy, GEPOperands)) ||
+      GEPOperands.size() < 2)
+    return std::nullopt;
+
+  VPValue *Cond = nullptr;
+  VPValue *TrueBase = nullptr;
+  VPValue *FalseBase = nullptr;
+  if (!match(GEPOperands.front(), m_Select(m_VPValue(Cond), m_VPValue(TrueBase),
+                                           m_VPValue(FalseBase))))
+    return std::nullopt;
+
+  if (!Cond->getScalarType()->isIntegerTy(1) || TrueBase == FalseBase)
+    return std::nullopt;
+
+  return SelectedBaseLoadPattern{AddrI, SmallVector<VPValue *>(GEPOperands),
+                                 Cond, TrueBase, FalseBase};
+}
+
+static InstructionCost getBlendedLoadCost(const TargetTransformInfo &TTI,
----------------
david-arm wrote:

Similar to the gather cost model below, it would be better to reuse existing cost model code where possible.

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


More information about the llvm-commits mailing list