[llvm] 103f1ac - [CVP] Make select fold use-site specific (PR63756)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 10 08:17:30 PDT 2023


Author: Nikita Popov
Date: 2023-07-10T17:17:09+02:00
New Revision: 103f1ac406d835a3c758a82f4d83072dff9bd473

URL: https://github.com/llvm/llvm-project/commit/103f1ac406d835a3c758a82f4d83072dff9bd473
DIFF: https://github.com/llvm/llvm-project/commit/103f1ac406d835a3c758a82f4d83072dff9bd473.diff

LOG: [CVP] Make select fold use-site specific (PR63756)

We may be able to replace the select with one of its select arms
at some but not all uses.

For uses in phi nodes this was already handled in the getValueOnEdge()
fold (which we can't drop entirely because it also handles an additional
case).

Fixes https://github.com/llvm/llvm-project/issues/63756.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
    llvm/test/Transforms/CorrelatedValuePropagation/select.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
index f788637ff901dc..48b27a1ea0a298 100644
--- a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -95,22 +95,32 @@ STATISTIC(NumUDivURemsNarrowedExpanded,
           "Number of bound udiv's/urem's expanded");
 
 static bool processSelect(SelectInst *S, LazyValueInfo *LVI) {
-  if (S->getType()->isVectorTy()) return false;
-  if (isa<Constant>(S->getCondition())) return false;
+  if (S->getType()->isVectorTy() || isa<Constant>(S->getCondition()))
+    return false;
 
-  Constant *C = LVI->getConstant(S->getCondition(), S);
-  if (!C) return false;
+  bool Changed = false;
+  for (Use &U : make_early_inc_range(S->uses())) {
+    auto *I = cast<Instruction>(U.getUser());
+    Constant *C;
+    if (auto *PN = dyn_cast<PHINode>(I))
+      C = LVI->getConstantOnEdge(S->getCondition(), PN->getIncomingBlock(U),
+                                 I->getParent(), I);
+    else
+      C = LVI->getConstant(S->getCondition(), I);
 
-  ConstantInt *CI = dyn_cast<ConstantInt>(C);
-  if (!CI) return false;
+    auto *CI = dyn_cast_or_null<ConstantInt>(C);
+    if (!CI)
+      continue;
 
-  Value *ReplaceWith = CI->isOne() ? S->getTrueValue() : S->getFalseValue();
-  S->replaceAllUsesWith(ReplaceWith);
-  S->eraseFromParent();
+    U.set(CI->isOne() ? S->getTrueValue() : S->getFalseValue());
+    Changed = true;
+    ++NumSelects;
+  }
 
-  ++NumSelects;
+  if (Changed && S->use_empty())
+    S->eraseFromParent();
 
-  return true;
+  return Changed;
 }
 
 /// Try to simplify a phi with constant incoming values that match the edge

diff  --git a/llvm/test/Transforms/CorrelatedValuePropagation/select.ll b/llvm/test/Transforms/CorrelatedValuePropagation/select.ll
index 85d3a022f19ced..28a8516a910280 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/select.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/select.ll
@@ -7,7 +7,6 @@ define i8 @simple_phi(i1 %c, i8 %a, i8 %b) {
 ; CHECK-LABEL: define i8 @simple_phi
 ; CHECK-SAME: (i1 [[C:%.*]], i8 [[A:%.*]], i8 [[B:%.*]]) {
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[C]], i8 [[A]], i8 [[B]]
 ; CHECK-NEXT:    br i1 [[C]], label [[THEN:%.*]], label [[ELSE:%.*]]
 ; CHECK:       then:
 ; CHECK-NEXT:    ret i8 [[A]]
@@ -36,7 +35,6 @@ define i8 @phi_other_edge(i1 %c, i8 %a, i8 %b, i32 %sw) {
 ; CHECK-NEXT:    i32 1, label [[ELSE:%.*]]
 ; CHECK-NEXT:    ]
 ; CHECK:       test:
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[C]], i8 [[A]], i8 [[B]]
 ; CHECK-NEXT:    br i1 [[C]], label [[THEN]], label [[ELSE]]
 ; CHECK:       then:
 ; CHECK-NEXT:    [[PHI1:%.*]] = phi i8 [ [[A]], [[TEST]] ], [ 1, [[ENTRY:%.*]] ]
@@ -96,12 +94,11 @@ define i8 @simple_non_phi(i1 %c, i8 %a, i8 %b) {
 ; CHECK-LABEL: define i8 @simple_non_phi
 ; CHECK-SAME: (i1 [[C:%.*]], i8 [[A:%.*]], i8 [[B:%.*]]) {
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[C]], i8 [[A]], i8 [[B]]
 ; CHECK-NEXT:    br i1 [[C]], label [[THEN:%.*]], label [[ELSE:%.*]]
 ; CHECK:       then:
-; CHECK-NEXT:    ret i8 [[S]]
+; CHECK-NEXT:    ret i8 [[A]]
 ; CHECK:       else:
-; CHECK-NEXT:    ret i8 [[S]]
+; CHECK-NEXT:    ret i8 [[B]]
 ;
 entry:
   %s = select i1 %c, i8 %a, i8 %b
@@ -123,10 +120,10 @@ define void @simple_multiple_uses(i1 %c, i8 %a, i8 %b) {
 ; CHECK-NEXT:    call void @use(i8 [[S]], i8 [[S]])
 ; CHECK-NEXT:    br i1 [[C]], label [[THEN:%.*]], label [[ELSE:%.*]]
 ; CHECK:       then:
-; CHECK-NEXT:    call void @use(i8 [[S]], i8 [[S]])
+; CHECK-NEXT:    call void @use(i8 [[A]], i8 [[A]])
 ; CHECK-NEXT:    ret void
 ; CHECK:       else:
-; CHECK-NEXT:    call void @use(i8 [[S]], i8 [[S]])
+; CHECK-NEXT:    call void @use(i8 [[B]], i8 [[B]])
 ; CHECK-NEXT:    ret void
 ;
 entry:


        


More information about the llvm-commits mailing list