[llvm] [VP] Refactoring some functions in ExpandVectorPredication.NFC (PR #115840)

via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 12 02:18:55 PST 2024


https://github.com/LiqinWeng updated https://github.com/llvm/llvm-project/pull/115840

>From 959d184d90345802b39858504cee90fcbe230444 Mon Sep 17 00:00:00 2001
From: LiqinWeng <liqin.weng at spacemit.com>
Date: Tue, 12 Nov 2024 18:09:56 +0800
Subject: [PATCH] [VP] Refactoring some functions in
 ExpandVectorPredication.NFC

---
 llvm/lib/CodeGen/ExpandVectorPredication.cpp | 116 +++++--------------
 1 file changed, 26 insertions(+), 90 deletions(-)

diff --git a/llvm/lib/CodeGen/ExpandVectorPredication.cpp b/llvm/lib/CodeGen/ExpandVectorPredication.cpp
index dd18b524e3f9c1..1e2679ad7c193d 100644
--- a/llvm/lib/CodeGen/ExpandVectorPredication.cpp
+++ b/llvm/lib/CodeGen/ExpandVectorPredication.cpp
@@ -176,8 +176,7 @@ struct CachingVPExpander {
                                            VPIntrinsic &PI);
 
   /// Lower this VP int call to a unpredicated int call.
-  Value *expandPredicationToIntCall(IRBuilder<> &Builder, VPIntrinsic &PI,
-                                    unsigned UnpredicatedIntrinsicID);
+  Value *expandPredicationToIntCall(IRBuilder<> &Builder, VPIntrinsic &PI);
 
   /// Lower this VP fp call to a unpredicated fp call.
   Value *expandPredicationToFPCall(IRBuilder<> &Builder, VPIntrinsic &PI,
@@ -288,33 +287,19 @@ CachingVPExpander::expandPredicationInBinaryOperator(IRBuilder<> &Builder,
   return NewBinOp;
 }
 
-Value *CachingVPExpander::expandPredicationToIntCall(
-    IRBuilder<> &Builder, VPIntrinsic &VPI, unsigned UnpredicatedIntrinsicID) {
-  switch (UnpredicatedIntrinsicID) {
-  case Intrinsic::abs:
-  case Intrinsic::smax:
-  case Intrinsic::smin:
-  case Intrinsic::umax:
-  case Intrinsic::umin: {
-    Value *Op0 = VPI.getOperand(0);
-    Value *Op1 = VPI.getOperand(1);
-    Value *NewOp = Builder.CreateIntrinsic(
-        UnpredicatedIntrinsicID, {VPI.getType()}, {Op0, Op1},
-        /*FMFSource=*/nullptr, VPI.getName());
-    replaceOperation(*NewOp, VPI);
-    return NewOp;
-  }
-  case Intrinsic::bswap:
-  case Intrinsic::bitreverse: {
-    Value *Op = VPI.getOperand(0);
-    Value *NewOp =
-        Builder.CreateIntrinsic(UnpredicatedIntrinsicID, {VPI.getType()}, {Op},
-                                /*FMFSource=*/nullptr, VPI.getName());
-    replaceOperation(*NewOp, VPI);
-    return NewOp;
-  }
+Value *CachingVPExpander::expandPredicationToIntCall(IRBuilder<> &Builder,
+                                                     VPIntrinsic &VPI) {
+  std::optional<unsigned> FID = VPI.getFunctionalIntrinsicID();
+  if (!FID)
+    return nullptr;
+  SmallVector<Value *, 2> Argument;
+  for (unsigned i = 0; i < VPI.getNumOperands() - 2; i++) {
+    Argument.push_back(VPI.getOperand(i));
   }
-  return nullptr;
+  Value *NewOp = Builder.CreateIntrinsic(FID.value(), {VPI.getType()}, Argument,
+                                         /*FMFSource=*/nullptr, VPI.getName());
+  replaceOperation(*NewOp, VPI);
+  return NewOp;
 }
 
 Value *CachingVPExpander::expandPredicationToFPCall(
@@ -324,20 +309,15 @@ Value *CachingVPExpander::expandPredicationToFPCall(
 
   switch (UnpredicatedIntrinsicID) {
   case Intrinsic::fabs:
-  case Intrinsic::sqrt: {
-    Value *Op0 = VPI.getOperand(0);
-    Value *NewOp =
-        Builder.CreateIntrinsic(UnpredicatedIntrinsicID, {VPI.getType()}, {Op0},
-                                /*FMFSource=*/nullptr, VPI.getName());
-    replaceOperation(*NewOp, VPI);
-    return NewOp;
-  }
+  case Intrinsic::sqrt:
   case Intrinsic::maxnum:
   case Intrinsic::minnum: {
-    Value *Op0 = VPI.getOperand(0);
-    Value *Op1 = VPI.getOperand(1);
+    SmallVector<Value *, 2> Argument;
+    for (unsigned i = 0; i < VPI.getNumOperands() - 2; i++) {
+      Argument.push_back(VPI.getOperand(i));
+    }
     Value *NewOp = Builder.CreateIntrinsic(
-        UnpredicatedIntrinsicID, {VPI.getType()}, {Op0, Op1},
+        UnpredicatedIntrinsicID, {VPI.getType()}, Argument,
         /*FMFSource=*/nullptr, VPI.getName());
     replaceOperation(*NewOp, VPI);
     return NewOp;
@@ -439,56 +419,13 @@ CachingVPExpander::expandPredicationInReduction(IRBuilder<> &Builder,
 
 Value *CachingVPExpander::expandPredicationToCastIntrinsic(IRBuilder<> &Builder,
                                                            VPIntrinsic &VPI) {
-  Value *CastOp = nullptr;
-  switch (VPI.getIntrinsicID()) {
-  default:
-    llvm_unreachable("Not a VP cast intrinsic");
-  case Intrinsic::vp_sext:
-    CastOp =
-        Builder.CreateSExt(VPI.getOperand(0), VPI.getType(), VPI.getName());
-    break;
-  case Intrinsic::vp_zext:
-    CastOp =
-        Builder.CreateZExt(VPI.getOperand(0), VPI.getType(), VPI.getName());
-    break;
-  case Intrinsic::vp_trunc:
-    CastOp =
-        Builder.CreateTrunc(VPI.getOperand(0), VPI.getType(), VPI.getName());
-    break;
-  case Intrinsic::vp_inttoptr:
-    CastOp =
-        Builder.CreateIntToPtr(VPI.getOperand(0), VPI.getType(), VPI.getName());
-    break;
-  case Intrinsic::vp_ptrtoint:
-    CastOp =
-        Builder.CreatePtrToInt(VPI.getOperand(0), VPI.getType(), VPI.getName());
-    break;
-  case Intrinsic::vp_fptosi:
-    CastOp =
-        Builder.CreateFPToSI(VPI.getOperand(0), VPI.getType(), VPI.getName());
-    break;
+  Intrinsic::ID VPID = VPI.getIntrinsicID();
+  unsigned CastOpcode = VPIntrinsic::getFunctionalOpcodeForVP(VPID).value();
+  assert(Instruction::isCast(CastOpcode));
+  Value *CastOp =
+      Builder.CreateCast(Instruction::CastOps(CastOpcode), VPI.getOperand(0),
+                         VPI.getType(), VPI.getName());
 
-  case Intrinsic::vp_fptoui:
-    CastOp =
-        Builder.CreateFPToUI(VPI.getOperand(0), VPI.getType(), VPI.getName());
-    break;
-  case Intrinsic::vp_sitofp:
-    CastOp =
-        Builder.CreateSIToFP(VPI.getOperand(0), VPI.getType(), VPI.getName());
-    break;
-  case Intrinsic::vp_uitofp:
-    CastOp =
-        Builder.CreateUIToFP(VPI.getOperand(0), VPI.getType(), VPI.getName());
-    break;
-  case Intrinsic::vp_fptrunc:
-    CastOp =
-        Builder.CreateFPTrunc(VPI.getOperand(0), VPI.getType(), VPI.getName());
-    break;
-  case Intrinsic::vp_fpext:
-    CastOp =
-        Builder.CreateFPExt(VPI.getOperand(0), VPI.getType(), VPI.getName());
-    break;
-  }
   replaceOperation(*CastOp, VPI);
   return CastOp;
 }
@@ -673,8 +610,7 @@ Value *CachingVPExpander::expandPredication(VPIntrinsic &VPI) {
   case Intrinsic::vp_umin:
   case Intrinsic::vp_bswap:
   case Intrinsic::vp_bitreverse:
-    return expandPredicationToIntCall(Builder, VPI,
-                                      VPI.getFunctionalIntrinsicID().value());
+    return expandPredicationToIntCall(Builder, VPI);
   case Intrinsic::vp_fabs:
   case Intrinsic::vp_sqrt:
   case Intrinsic::vp_maxnum:



More information about the llvm-commits mailing list