Skip to content

[SCCP] Infer nneg on existing zext #72143

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

Merged
merged 1 commit into from
Nov 14, 2023
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
45 changes: 26 additions & 19 deletions llvm/lib/Transforms/Utils/SCCPSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ bool SCCPSolver::tryToReplaceWithConstant(Value *V) {
static bool refineInstruction(SCCPSolver &Solver,
const SmallPtrSetImpl<Value *> &InsertedValues,
Instruction &Inst) {
if (!isa<OverflowingBinaryOperator>(Inst))
return false;

bool Changed = false;
auto GetRange = [&Solver, &InsertedValues](Value *Op) {
if (auto *Const = dyn_cast<ConstantInt>(Op))
return ConstantRange(Const->getValue());
Expand All @@ -120,23 +118,32 @@ static bool refineInstruction(SCCPSolver &Solver,
return getConstantRange(Solver.getLatticeValueFor(Op), Op->getType(),
/*UndefAllowed=*/false);
};
auto RangeA = GetRange(Inst.getOperand(0));
auto RangeB = GetRange(Inst.getOperand(1));
bool Changed = false;
if (!Inst.hasNoUnsignedWrap()) {
auto NUWRange = ConstantRange::makeGuaranteedNoWrapRegion(
Instruction::BinaryOps(Inst.getOpcode()), RangeB,
OverflowingBinaryOperator::NoUnsignedWrap);
if (NUWRange.contains(RangeA)) {
Inst.setHasNoUnsignedWrap();
Changed = true;

if (isa<OverflowingBinaryOperator>(Inst)) {
auto RangeA = GetRange(Inst.getOperand(0));
auto RangeB = GetRange(Inst.getOperand(1));
if (!Inst.hasNoUnsignedWrap()) {
auto NUWRange = ConstantRange::makeGuaranteedNoWrapRegion(
Instruction::BinaryOps(Inst.getOpcode()), RangeB,
OverflowingBinaryOperator::NoUnsignedWrap);
if (NUWRange.contains(RangeA)) {
Inst.setHasNoUnsignedWrap();
Changed = true;
}
}
}
if (!Inst.hasNoSignedWrap()) {
auto NSWRange = ConstantRange::makeGuaranteedNoWrapRegion(
Instruction::BinaryOps(Inst.getOpcode()), RangeB, OverflowingBinaryOperator::NoSignedWrap);
if (NSWRange.contains(RangeA)) {
Inst.setHasNoSignedWrap();
if (!Inst.hasNoSignedWrap()) {
auto NSWRange = ConstantRange::makeGuaranteedNoWrapRegion(
Instruction::BinaryOps(Inst.getOpcode()), RangeB,
OverflowingBinaryOperator::NoSignedWrap);
if (NSWRange.contains(RangeA)) {
Inst.setHasNoSignedWrap();
Changed = true;
}
}
} else if (isa<ZExtInst>(Inst) && !Inst.hasNonNeg()) {
auto Range = GetRange(Inst.getOperand(0));
if (Range.isAllNonNegative()) {
Inst.setNonNeg();
Changed = true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/SCCP/ip-ranges-casts.ll
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ define i1 @caller1() {
; x = [100, 301)
define internal i1 @f.zext(i32 %x, i32 %y) {
; CHECK-LABEL: @f.zext(
; CHECK-NEXT: [[T_1:%.*]] = zext i32 [[X:%.*]] to i64
; CHECK-NEXT: [[T_1:%.*]] = zext nneg i32 [[X:%.*]] to i64
; CHECK-NEXT: [[C_2:%.*]] = icmp sgt i64 [[T_1]], 299
; CHECK-NEXT: [[C_4:%.*]] = icmp slt i64 [[T_1]], 101
; CHECK-NEXT: [[RES_1:%.*]] = add nuw nsw i1 false, [[C_2]]
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/SCCP/ipsccp-range-crashes.ll
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ define i64 @crash_ctpop(i1 %cond, ptr %addr) {
; CHECK: if.else:
; CHECK-NEXT: [[LV:%.*]] = load i32, ptr [[ADDR:%.*]], align 8
; CHECK-NEXT: [[ANDV:%.*]] = and i32 [[LV]], 16777215
; CHECK-NEXT: [[TRUNCV:%.*]] = zext i32 [[ANDV]] to i64
; CHECK-NEXT: [[TRUNCV:%.*]] = zext nneg i32 [[ANDV]] to i64
; CHECK-NEXT: [[RES:%.*]] = tail call i64 @llvm.ctpop.i64(i64 [[TRUNCV]])
; CHECK-NEXT: ret i64 [[RES]]
;
Expand Down