[llvm] [VPlan] Tweak getUnderlyingInstr to improve code (NFC) (PR #188939)

via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 27 02:50:10 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-vectorizers

Author: Ramkumar Ramachandra (artagnon)

<details>
<summary>Changes</summary>

Move the helper to VPValue, and replace asserting-cast with cast_or_null.

---
Full diff: https://github.com/llvm/llvm-project/pull/188939.diff


4 Files Affected:

- (modified) llvm/lib/Transforms/Vectorize/LoopVectorize.cpp (+5-4) 
- (modified) llvm/lib/Transforms/Vectorize/VPlan.h (-8) 
- (modified) llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp (+5-7) 
- (modified) llvm/lib/Transforms/Vectorize/VPlanValue.h (+5) 


``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 7c401becc8634..82e23f4bec26a 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -7125,7 +7125,7 @@ static bool planContainsAdditionalSimplifications(VPlan &Plan,
   // First collect all instructions for the recipes in Plan.
   auto GetInstructionForCost = [](const VPRecipeBase *R) -> Instruction * {
     if (auto *S = dyn_cast<VPSingleDefRecipe>(R))
-      return dyn_cast_or_null<Instruction>(S->getUnderlyingValue());
+      return S->getUnderlyingInstr();
     if (auto *WidenMem = dyn_cast<VPWidenMemoryRecipe>(R))
       return &WidenMem->getIngredient();
     return nullptr;
@@ -8283,13 +8283,14 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(
       if (isa<VPWidenCanonicalIVRecipe, VPBlendRecipe, VPReductionRecipe>(&R))
         continue;
       auto *VPI = cast<VPInstruction>(&R);
-      if (!VPI->getUnderlyingValue())
-        continue;
 
       // TODO: Gradually replace uses of underlying instruction by analyses on
       // VPlan. Migrate code relying on the underlying instruction from VPlan0
       // to construct recipes below to not use the underlying instruction.
-      Instruction *Instr = cast<Instruction>(VPI->getUnderlyingValue());
+      Instruction *Instr = VPI->getUnderlyingInstr();
+      if (!Instr)
+        continue;
+
       Builder.setInsertPoint(VPI);
 
       // The stores with invariant address inside the loop will be deleted, and
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index 9ed9d07151d7f..896375f29fa98 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -671,14 +671,6 @@ class VPSingleDefRecipe : public VPRecipeBase, public VPRecipeValue {
 
   VPSingleDefRecipe *clone() override = 0;
 
-  /// Returns the underlying instruction.
-  Instruction *getUnderlyingInstr() {
-    return cast<Instruction>(getUnderlyingValue());
-  }
-  const Instruction *getUnderlyingInstr() const {
-    return cast<Instruction>(getUnderlyingValue());
-  }
-
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
   /// Print this VPSingleDefRecipe to dbgs() (for debugging).
   LLVM_ABI_FOR_TEST LLVM_DUMP_METHOD void dump() const;
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 7e8496a568643..7bfffdc02b245 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -63,11 +63,10 @@ bool VPlanTransforms::tryToConvertVPInstructionsToVPRecipes(
          make_early_inc_range(make_range(VPBB->begin(), EndIter))) {
 
       VPValue *VPV = Ingredient.getVPSingleValue();
-      if (!VPV->getUnderlyingValue())
+      Instruction *Inst = VPV->getUnderlyingInstr();
+      if (!Inst)
         continue;
 
-      Instruction *Inst = cast<Instruction>(VPV->getUnderlyingValue());
-
       VPRecipeBase *NewRecipe = nullptr;
       if (auto *PhiR = dyn_cast<VPPhi>(&Ingredient)) {
         auto *Phi = cast<PHINode>(PhiR->getUnderlyingValue());
@@ -2755,7 +2754,7 @@ void VPlanTransforms::truncateToMinimalBitwidths(
         continue;
 
       VPValue *ResultVPV = R.getVPSingleValue();
-      auto *UI = cast_or_null<Instruction>(ResultVPV->getUnderlyingValue());
+      auto *UI = ResultVPV->getUnderlyingInstr();
       unsigned NewResSizeInBits = MinBWs.lookup(UI);
       if (!NewResSizeInBits)
         continue;
@@ -3588,9 +3587,8 @@ void VPlanTransforms::dropPoisonGeneratingRecipes(
         } else
           RecWithFlags->dropPoisonGeneratingFlags();
       } else {
-        Instruction *Instr = dyn_cast_or_null<Instruction>(
-            CurRec->getVPSingleValue()->getUnderlyingValue());
-        (void)Instr;
+        [[maybe_unused]] Instruction *Instr =
+            CurRec->getVPSingleValue()->getUnderlyingInstr();
         assert((!Instr || !Instr->hasPoisonGeneratingFlags()) &&
                "found instruction with poison generating flags not covered by "
                "VPRecipeWithIRFlags");
diff --git a/llvm/lib/Transforms/Vectorize/VPlanValue.h b/llvm/lib/Transforms/Vectorize/VPlanValue.h
index d4adae4622860..6b6bf50d582de 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanValue.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanValue.h
@@ -69,6 +69,11 @@ class LLVM_ABI_FOR_TEST VPValue {
   /// Return the underlying Value attached to this VPValue.
   Value *getUnderlyingValue() const { return UnderlyingVal; }
 
+  /// Return the underlying Instruction attached to this VPValue.
+  Instruction *getUnderlyingInstr() const {
+    return cast_or_null<Instruction>(UnderlyingVal);
+  }
+
   /// Return the underlying IR value for a VPIRValue.
   Value *getLiveInIRValue() const;
 

``````````

</details>


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


More information about the llvm-commits mailing list