Skip to content

Commit

Permalink
[CVP] Make select fold use-site specific (PR63756)
Browse files Browse the repository at this point in the history
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 #63756.
  • Loading branch information
nikic committed Jul 10, 2023
1 parent fa7915b commit 103f1ac
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
32 changes: 21 additions & 11 deletions llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 4 additions & 7 deletions llvm/test/Transforms/CorrelatedValuePropagation/select.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down Expand Up @@ -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:%.*]] ]
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 103f1ac

Please sign in to comment.