Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CVP] Improve the value solving of select at use #76700

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 26 additions & 25 deletions llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,31 @@ STATISTIC(NumUDivURemsNarrowedExpanded,
"Number of bound udiv's/urem's expanded");
STATISTIC(NumZExt, "Number of non-negative deductions");

static Constant *getConstantAt(Value *V, Instruction *At, LazyValueInfo *LVI) {
if (Constant *C = LVI->getConstant(V, At))
return C;

// TODO: The following really should be sunk inside LVI's core algorithm, or
// at least the outer shims around such.
auto *C = dyn_cast<CmpInst>(V);
if (!C)
return nullptr;

Value *Op0 = C->getOperand(0);
Constant *Op1 = dyn_cast<Constant>(C->getOperand(1));
if (!Op1)
return nullptr;

LazyValueInfo::Tristate Result = LVI->getPredicateAt(
C->getPredicate(), Op0, Op1, At, /*UseBlockValue=*/false);
if (Result == LazyValueInfo::Unknown)
return nullptr;

return (Result == LazyValueInfo::True)
? ConstantInt::getTrue(C->getContext())
: ConstantInt::getFalse(C->getContext());
}

static bool processSelect(SelectInst *S, LazyValueInfo *LVI) {
if (S->getType()->isVectorTy() || isa<Constant>(S->getCondition()))
return false;
Expand All @@ -106,7 +131,7 @@ static bool processSelect(SelectInst *S, LazyValueInfo *LVI) {
C = LVI->getConstantOnEdge(S->getCondition(), PN->getIncomingBlock(U),
I->getParent(), I);
else
C = LVI->getConstant(S->getCondition(), I);
C = getConstantAt(S->getCondition(), I, LVI);

auto *CI = dyn_cast_or_null<ConstantInt>(C);
if (!CI)
Expand Down Expand Up @@ -1109,30 +1134,6 @@ static bool processAnd(BinaryOperator *BinOp, LazyValueInfo *LVI) {
return true;
}


static Constant *getConstantAt(Value *V, Instruction *At, LazyValueInfo *LVI) {
if (Constant *C = LVI->getConstant(V, At))
return C;

// TODO: The following really should be sunk inside LVI's core algorithm, or
// at least the outer shims around such.
auto *C = dyn_cast<CmpInst>(V);
if (!C) return nullptr;

Value *Op0 = C->getOperand(0);
Constant *Op1 = dyn_cast<Constant>(C->getOperand(1));
if (!Op1) return nullptr;

LazyValueInfo::Tristate Result = LVI->getPredicateAt(
C->getPredicate(), Op0, Op1, At, /*UseBlockValue=*/false);
if (Result == LazyValueInfo::Unknown)
return nullptr;

return (Result == LazyValueInfo::True) ?
ConstantInt::getTrue(C->getContext()) :
ConstantInt::getFalse(C->getContext());
}

static bool runImpl(Function &F, LazyValueInfo *LVI, DominatorTree *DT,
const SimplifyQuery &SQ) {
bool FnChanged = false;
Expand Down
23 changes: 23 additions & 0 deletions llvm/test/Transforms/CorrelatedValuePropagation/select.ll
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,27 @@ define i64 @select_cond_may_undef(i32 %a) {
ret i64 %max
}

define i32 @test_solve_select_at_use(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: define i32 @test_solve_select_at_use
; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]], i32 [[C:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[A]], 0
; CHECK-NEXT: [[COND:%.*]] = icmp sgt i32 [[A]], -1
; CHECK-NEXT: br i1 [[COND]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: ret i32 [[C]]
; CHECK: if.else:
; CHECK-NEXT: ret i32 [[B]]
;
entry:
%cmp = icmp slt i32 %a, 0
%retval = select i1 %cmp, i32 %b, i32 %c
%cond = icmp sgt i32 %a, -1
br i1 %cond, label %if.then, label %if.else
if.then:
ret i32 %retval
if.else:
ret i32 %retval
}

!0 = !{}