[llvm] [X86,SimplifyCFG] Use passthru to reduce select (PR #108754)

Phoebe Wang via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 15 06:09:38 PDT 2024


https://github.com/phoebewang created https://github.com/llvm/llvm-project/pull/108754

None

>From 0381a076d6a7cd7da83d98f8afb4f5ab957c97ae Mon Sep 17 00:00:00 2001
From: "Wang, Phoebe" <phoebe.wang at intel.com>
Date: Sun, 15 Sep 2024 21:04:07 +0800
Subject: [PATCH] [X86,SimplifyCFG] Use passthru to reduce select

---
 llvm/lib/Transforms/Utils/SimplifyCFG.cpp       | 17 ++++++++++++++---
 .../X86/hoist-loads-stores-with-cf.ll           | 10 ++++------
 2 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index f9db996cdc3583..5cebfbadf22069 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -3335,12 +3335,21 @@ bool SimplifyCFGOpt::speculativelyExecuteBB(BranchInst *BI,
     assert(!getLoadStoreType(I)->isVectorTy() && "not implemented");
     auto *Op0 = I->getOperand(0);
     Instruction *MaskedLoadStore = nullptr;
+    PHINode *PN = nullptr;
     if (auto *LI = dyn_cast<LoadInst>(I)) {
       // Handle Load.
       auto *Ty = I->getType();
-      MaskedLoadStore = Builder.CreateMaskedLoad(FixedVectorType::get(Ty, 1),
-                                                 Op0, LI->getAlign(), Mask);
-      I->replaceAllUsesWith(Builder.CreateBitCast(MaskedLoadStore, Ty));
+      Value *PassThru = nullptr;
+      if (I->hasOneUse())
+        if ((PN = dyn_cast<PHINode>(I->use_begin()->getUser())))
+          PassThru = Builder.CreateBitCast(PN->getIncomingValueForBlock(BB),
+                                           FixedVectorType::get(Ty, 1));
+      MaskedLoadStore = Builder.CreateMaskedLoad(
+          FixedVectorType::get(Ty, 1), Op0, LI->getAlign(), Mask, PassThru);
+      if (PN)
+        PN->replaceAllUsesWith(Builder.CreateBitCast(MaskedLoadStore, Ty));
+      else
+        I->replaceAllUsesWith(Builder.CreateBitCast(MaskedLoadStore, Ty));
     } else {
       // Handle Store.
       auto *StoredVal =
@@ -3365,6 +3374,8 @@ bool SimplifyCFGOpt::speculativelyExecuteBB(BranchInst *BI,
       return Node->getMetadataID() == Metadata::DIAssignIDKind;
     });
     MaskedLoadStore->copyMetadata(*I);
+    if (PN)
+      PN->eraseFromParent();
     I->eraseFromParent();
   }
 
diff --git a/llvm/test/Transforms/SimplifyCFG/X86/hoist-loads-stores-with-cf.ll b/llvm/test/Transforms/SimplifyCFG/X86/hoist-loads-stores-with-cf.ll
index 047ca717da8009..760334dc1d2815 100644
--- a/llvm/test/Transforms/SimplifyCFG/X86/hoist-loads-stores-with-cf.ll
+++ b/llvm/test/Transforms/SimplifyCFG/X86/hoist-loads-stores-with-cf.ll
@@ -72,10 +72,9 @@ define i32 @succ1to0_phi(ptr %p)  {
 ; CHECK-NEXT:    [[COND:%.*]] = icmp eq ptr [[P:%.*]], null
 ; CHECK-NEXT:    [[TMP0:%.*]] = xor i1 [[COND]], true
 ; CHECK-NEXT:    [[TMP1:%.*]] = bitcast i1 [[TMP0]] to <1 x i1>
-; CHECK-NEXT:    [[TMP2:%.*]] = call <1 x i32> @llvm.masked.load.v1i32.p0(ptr [[P]], i32 4, <1 x i1> [[TMP1]], <1 x i32> poison)
+; CHECK-NEXT:    [[TMP2:%.*]] = call <1 x i32> @llvm.masked.load.v1i32.p0(ptr [[P]], i32 4, <1 x i1> [[TMP1]], <1 x i32> zeroinitializer)
 ; CHECK-NEXT:    [[TMP3:%.*]] = bitcast <1 x i32> [[TMP2]] to i32
-; CHECK-NEXT:    [[SPEC_SELECT:%.*]] = select i1 [[COND]], i32 0, i32 [[TMP3]]
-; CHECK-NEXT:    ret i32 [[SPEC_SELECT]]
+; CHECK-NEXT:    ret i32 [[TMP3]]
 ;
 entry:
   %cond = icmp eq ptr %p, null
@@ -184,10 +183,9 @@ define i32 @load_from_gep(ptr %p)  {
 ; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds i8, ptr [[P]], i64 16
 ; CHECK-NEXT:    [[TMP0:%.*]] = xor i1 [[COND]], true
 ; CHECK-NEXT:    [[TMP1:%.*]] = bitcast i1 [[TMP0]] to <1 x i1>
-; CHECK-NEXT:    [[TMP2:%.*]] = call <1 x i32> @llvm.masked.load.v1i32.p0(ptr [[ARRAYIDX]], i32 4, <1 x i1> [[TMP1]], <1 x i32> poison)
+; CHECK-NEXT:    [[TMP2:%.*]] = call <1 x i32> @llvm.masked.load.v1i32.p0(ptr [[ARRAYIDX]], i32 4, <1 x i1> [[TMP1]], <1 x i32> zeroinitializer)
 ; CHECK-NEXT:    [[TMP3:%.*]] = bitcast <1 x i32> [[TMP2]] to i32
-; CHECK-NEXT:    [[SPEC_SELECT:%.*]] = select i1 [[COND]], i32 0, i32 [[TMP3]]
-; CHECK-NEXT:    ret i32 [[SPEC_SELECT]]
+; CHECK-NEXT:    ret i32 [[TMP3]]
 ;
 entry:
   %cond = icmp eq ptr %p, null



More information about the llvm-commits mailing list