Skip to content

Commit 6ae4fcf

Browse files
[SCCP] Extend visitBinaryOperator to overflowing binary ops
Leverage more refined ranges results when handling overflowing binary operators.
1 parent 589c7ab commit 6ae4fcf

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

llvm/lib/Transforms/Utils/SCCPSolver.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,13 @@ void SCCPInstVisitor::visitBinaryOperator(Instruction &I) {
14861486
// Try to simplify to a constant range.
14871487
ConstantRange A = getConstantRange(V1State, I.getType());
14881488
ConstantRange B = getConstantRange(V2State, I.getType());
1489-
ConstantRange R = A.binaryOp(cast<BinaryOperator>(&I)->getOpcode(), B);
1489+
1490+
auto *BO = cast<BinaryOperator>(&I);
1491+
ConstantRange R = ConstantRange::getEmpty(I.getType()->getScalarSizeInBits());
1492+
if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(BO))
1493+
R = A.overflowingBinaryOp(BO->getOpcode(), B, OBO->getNoWrapKind());
1494+
else
1495+
R = A.binaryOp(BO->getOpcode(), B);
14901496
mergeInValue(&I, ValueLatticeElement::getRange(R));
14911497

14921498
// TODO: Currently we do not exploit special values that produce something

llvm/test/Transforms/SCCP/add-nuw-nsw-flags.ll

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,32 @@ then:
240240
else:
241241
ret i16 0
242242
}
243+
244+
define i1 @test_add_nuw_sub(i32 %a) {
245+
; CHECK-LABEL: @test_add_nuw_sub(
246+
; CHECK-NEXT: entry:
247+
; CHECK-NEXT: [[ADD:%.*]] = add nuw i32 [[A:%.*]], 10000
248+
; CHECK-NEXT: [[SUB:%.*]] = add i32 [[ADD]], -5000
249+
; CHECK-NEXT: ret i1 false
250+
;
251+
entry:
252+
%add = add nuw i32 %a, 10000
253+
%sub = add i32 %add, -5000
254+
%cond = icmp ult i32 %sub, 5000
255+
ret i1 %cond
256+
}
257+
258+
define i1 @test_add_nsw_sub(i32 %a) {
259+
; CHECK-LABEL: @test_add_nsw_sub(
260+
; CHECK-NEXT: entry:
261+
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[A:%.*]], 10000
262+
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[ADD]], -5000
263+
; CHECK-NEXT: [[COND:%.*]] = icmp ult i32 [[SUB]], 5000
264+
; CHECK-NEXT: ret i1 [[COND]]
265+
;
266+
entry:
267+
%add = add nsw i32 %a, 10000
268+
%sub = add i32 %add, -5000
269+
%cond = icmp ult i32 %sub, 5000
270+
ret i1 %cond
271+
}

0 commit comments

Comments
 (0)