Skip to content

Commit 1f66504

Browse files
committed
[LVI][CVP] LazyValueInfoImpl::solveBlockValueBinaryOp(): use no-wrap flags from add op
Summary: This was suggested in https://reviews.llvm.org/D69277#1717210 In this form (this is what was suggested, right?), the results aren't staggering (especially since given LVI cross-block focus) this does catch some things (as per test-suite), but not too much: | statistic | old | new | delta | % change | | correlated-value-propagation.NumAddNSW | 4981 | 4982 | 1 | 0.0201% | | correlated-value-propagation.NumAddNW | 12125 | 12126 | 1 | 0.0082% | | correlated-value-propagation.NumCmps | 1199 | 1202 | 3 | 0.2502% | | correlated-value-propagation.NumDeadCases | 112 | 111 | -1 | -0.8929% | | correlated-value-propagation.NumMulNSW | 275 | 278 | 3 | 1.0909% | | correlated-value-propagation.NumMulNUW | 1323 | 1326 | 3 | 0.2268% | | correlated-value-propagation.NumMulNW | 1598 | 1604 | 6 | 0.3755% | | correlated-value-propagation.NumNSW | 7158 | 7167 | 9 | 0.1257% | | correlated-value-propagation.NumNUW | 13304 | 13310 | 6 | 0.0451% | | correlated-value-propagation.NumNW | 20462 | 20477 | 15 | 0.0733% | | correlated-value-propagation.NumOverflows | 4 | 7 | 3 | 75.0000% | | correlated-value-propagation.NumPhis | 15366 | 15381 | 15 | 0.0976% | | correlated-value-propagation.NumSExt | 6273 | 6277 | 4 | 0.0638% | | correlated-value-propagation.NumShlNSW | 1172 | 1171 | -1 | -0.0853% | | correlated-value-propagation.NumShlNUW | 2793 | 2794 | 1 | 0.0358% | | correlated-value-propagation.NumSubNSW | 730 | 736 | 6 | 0.8219% | | correlated-value-propagation.NumSubNUW | 2044 | 2046 | 2 | 0.0978% | | correlated-value-propagation.NumSubNW | 2774 | 2782 | 8 | 0.2884% | | instcount.NumAddInst | 277586 | 277569 | -17 | -0.0061% | | instcount.NumAndInst | 66056 | 66054 | -2 | -0.0030% | | instcount.NumBrInst | 709147 | 709146 | -1 | -0.0001% | | instcount.NumCallInst | 528579 | 528576 | -3 | -0.0006% | | instcount.NumExtractValueInst | 18307 | 18301 | -6 | -0.0328% | | instcount.NumOrInst | 102660 | 102665 | 5 | 0.0049% | | instcount.NumPHIInst | 318008 | 318007 | -1 | -0.0003% | | instcount.NumSelectInst | 46373 | 46370 | -3 | -0.0065% | | instcount.NumSExtInst | 79496 | 79488 | -8 | -0.0101% | | instcount.NumShlInst | 40654 | 40657 | 3 | 0.0074% | | instcount.NumTruncInst | 62251 | 62249 | -2 | -0.0032% | | instcount.NumZExtInst | 68211 | 68221 | 10 | 0.0147% | | instcount.TotalBlocks | 843910 | 843909 | -1 | -0.0001% | | instcount.TotalInsts | 7387448 | 7387423 | -25 | -0.0003% | Reviewers: nikic, reames Reviewed By: nikic Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D69321
1 parent be86fdb commit 1f66504

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

llvm/include/llvm/IR/ConstantRange.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,14 @@ class LLVM_NODISCARD ConstantRange {
326326
ConstantRange binaryOp(Instruction::BinaryOps BinOp,
327327
const ConstantRange &Other) const;
328328

329+
/// Return a new range representing the possible values resulting
330+
/// from an application of the specified overflowing binary operator to a
331+
/// left hand side of this range and a right hand side of \p Other given
332+
/// the provided knowledge about lack of wrapping \p NoWrapKind.
333+
ConstantRange overflowingBinaryOp(Instruction::BinaryOps BinOp,
334+
const ConstantRange &Other,
335+
unsigned NoWrapKind) const;
336+
329337
/// Return a new range representing the possible values resulting
330338
/// from an addition of a value in this range and a value in \p Other.
331339
ConstantRange add(const ConstantRange &Other) const;

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,8 +1090,22 @@ bool LazyValueInfoImpl::solveBlockValueBinaryOp(ValueLatticeElement &BBLV,
10901090
return true;
10911091
}
10921092

1093-
return solveBlockValueBinaryOpImpl(BBLV, BO, BB,
1094-
[BO](const ConstantRange &CR1, const ConstantRange &CR2) {
1093+
if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(BO)) {
1094+
unsigned NoWrapKind = 0;
1095+
if (OBO->hasNoUnsignedWrap())
1096+
NoWrapKind |= OverflowingBinaryOperator::NoUnsignedWrap;
1097+
if (OBO->hasNoSignedWrap())
1098+
NoWrapKind |= OverflowingBinaryOperator::NoSignedWrap;
1099+
1100+
return solveBlockValueBinaryOpImpl(
1101+
BBLV, BO, BB,
1102+
[BO, NoWrapKind](const ConstantRange &CR1, const ConstantRange &CR2) {
1103+
return CR1.overflowingBinaryOp(BO->getOpcode(), CR2, NoWrapKind);
1104+
});
1105+
}
1106+
1107+
return solveBlockValueBinaryOpImpl(
1108+
BBLV, BO, BB, [BO](const ConstantRange &CR1, const ConstantRange &CR2) {
10951109
return CR1.binaryOp(BO->getOpcode(), CR2);
10961110
});
10971111
}

llvm/lib/IR/ConstantRange.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,21 @@ ConstantRange ConstantRange::binaryOp(Instruction::BinaryOps BinOp,
816816
}
817817
}
818818

819+
ConstantRange ConstantRange::overflowingBinaryOp(Instruction::BinaryOps BinOp,
820+
const ConstantRange &Other,
821+
unsigned NoWrapKind) const {
822+
assert(Instruction::isBinaryOp(BinOp) && "Binary operators only!");
823+
824+
switch (BinOp) {
825+
case Instruction::Add:
826+
return addWithNoWrap(Other, NoWrapKind);
827+
default:
828+
// Don't know about this Overflowing Binary Operation.
829+
// Conservatively fallback to plain binop handling.
830+
return binaryOp(BinOp, Other);
831+
}
832+
}
833+
819834
ConstantRange
820835
ConstantRange::add(const ConstantRange &Other) const {
821836
if (isEmptySet() || Other.isEmptySet())

llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,9 @@ define i1 @test8(i32 %a, i32 %b) {
289289
; CHECK-NEXT: [[ADD:%.*]] = add nuw nsw i32 [[A]], [[B]]
290290
; CHECK-NEXT: br label [[CONT:%.*]]
291291
; CHECK: cont:
292-
; CHECK-NEXT: [[RES:%.*]] = icmp sge i32 [[ADD]], 0
293292
; CHECK-NEXT: br label [[EXIT]]
294293
; CHECK: exit:
295-
; CHECK-NEXT: [[IV:%.*]] = phi i1 [ true, [[BEGIN:%.*]] ], [ [[RES]], [[CONT]] ]
296-
; CHECK-NEXT: ret i1 [[IV]]
294+
; CHECK-NEXT: ret i1 true
297295
;
298296
begin:
299297
%cmp0 = icmp sge i32 %a, 0
@@ -355,11 +353,9 @@ define i1 @test11(i32 %a, i32 %b) {
355353
; CHECK-NEXT: [[ADD:%.*]] = add nuw i32 [[A]], [[B:%.*]]
356354
; CHECK-NEXT: br label [[CONT:%.*]]
357355
; CHECK: cont:
358-
; CHECK-NEXT: [[RES:%.*]] = icmp uge i32 [[ADD]], -256
359356
; CHECK-NEXT: br label [[EXIT]]
360357
; CHECK: exit:
361-
; CHECK-NEXT: [[IV:%.*]] = phi i1 [ true, [[BEGIN:%.*]] ], [ [[RES]], [[CONT]] ]
362-
; CHECK-NEXT: ret i1 [[IV]]
358+
; CHECK-NEXT: ret i1 true
363359
;
364360
begin:
365361
%cmp = icmp uge i32 %a, 4294967040

0 commit comments

Comments
 (0)