Skip to content

Commit

Permalink
[SCCP] Infer nneg on existing zext (#72143)
Browse files Browse the repository at this point in the history
This patch infers `nneg` flags for existing zext instructions in SCCP.

Similar patch: #72052
  • Loading branch information
dtcxzyw committed Nov 14, 2023
1 parent f633f32 commit ed96430
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
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

0 comments on commit ed96430

Please sign in to comment.