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] Simplify minmax at use #125341

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
37 changes: 36 additions & 1 deletion llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,42 @@ static bool processMinMaxIntrinsic(MinMaxIntrinsic *MM, LazyValueInfo *LVI) {
return true;
}

return false;
bool Changed = false;
for (Use &U : make_early_inc_range(MM->uses())) {
ConstantRange LHS_CR =
ConstantRange::getEmpty(MM->getType()->getScalarSizeInBits());
ConstantRange RHS_CR = LHS_CR;
auto *CxtI = cast<Instruction>(U.getUser());
if (auto *PN = dyn_cast<PHINode>(CxtI)) {
BasicBlock *FromBB = PN->getIncomingBlock(U);
LHS_CR = LVI->getConstantRangeOnEdge(MM->getOperand(0), FromBB,
CxtI->getParent(), CxtI);
RHS_CR = LVI->getConstantRangeOnEdge(MM->getOperand(1), FromBB,
CxtI->getParent(), CxtI);
} else {
LHS_CR = LVI->getConstantRange(MM->getOperand(0), CxtI,
/*UndefAllowed=*/false);
RHS_CR = LVI->getConstantRange(MM->getOperand(1), CxtI,
/*UndefAllowed=*/false);
}
if (LHS_CR.icmp(Pred, RHS_CR)) {
Changed = true;
++NumMinMax;
U.set(MM->getLHS());
continue;
}
if (RHS_CR.icmp(Pred, LHS_CR)) {
Changed = true;
++NumMinMax;
U.set(MM->getRHS());
continue;
}
}

if (MM->use_empty())
MM->eraseFromParent();

return Changed;
}

// Rewrite this with.overflow intrinsic as non-overflowing.
Expand Down
24 changes: 24 additions & 0 deletions llvm/test/Transforms/CorrelatedValuePropagation/min-max.ll
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,27 @@ define i8 @test_umax_nneg(i8 %a, i8 %b) {
%ret = call i8 @llvm.umax.i8(i8 %nneg_a, i8 %nneg_b)
ret i8 %ret
}

define i64 @test_at_use2(i32 %x) {
; CHECK-LABEL: @test_at_use2(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[COND:%.*]] = icmp slt i32 [[X:%.*]], 0
; CHECK-NEXT: br i1 [[COND]], label [[IF_END:%.*]], label [[IF_THEN:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[EXT:%.*]] = zext nneg i32 [[X]] to i64
; CHECK-NEXT: ret i64 [[EXT]]
; CHECK: if.end:
; CHECK-NEXT: ret i64 0
;
entry:
%cond = icmp slt i32 %x, 0
%smax = call i32 @llvm.smax.i32(i32 %x, i32 -1)
br i1 %cond, label %if.end, label %if.then

if.then:
%ext = zext nneg i32 %smax to i64
ret i64 %ext

if.end:
ret i64 0
}