[ConstraintElimination] Use AddOverflow for offset summation.

Fixes an incorrect transformation due to values overflowing
https://alive2.llvm.org/ce/z/uizoea
This commit is contained in:
Florian Hahn 2022-03-25 18:08:24 +00:00
parent 8530259985
commit 8c3281db49
No known key found for this signature in database
GPG Key ID: CF59919C6547A668
2 changed files with 9 additions and 3 deletions

View File

@ -27,6 +27,7 @@
#include "llvm/Pass.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DebugCounter.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Transforms/Scalar.h"
#include <string>
@ -320,8 +321,13 @@ getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
for (const auto &KV : VariablesB)
R[GetOrAddIndex(KV.second)] -= KV.first;
R[0] = Offset1 + Offset2 +
(Pred == (IsSigned ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT) ? -1 : 0);
int64_t OffsetSum;
if (AddOverflow(Offset1, Offset2, OffsetSum))
return {};
if (Pred == (IsSigned ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT))
if (AddOverflow(OffsetSum, int64_t(-1), OffsetSum))
return {};
R[0] = OffsetSum;
Res.Preconditions = std::move(Preconditions);
return Res;
}

View File

@ -362,7 +362,7 @@ define i1 @wrapping_offset_sum(i64 %x) {
; CHECK-NEXT: call void @llvm.assume(i1 [[NON_ZERO]])
; CHECK-NEXT: [[ADD:%.*]] = sub nuw i64 [[X]], 9223372036854775802
; CHECK-NEXT: [[ULT:%.*]] = icmp ugt i64 200, [[ADD]]
; CHECK-NEXT: ret i1 false
; CHECK-NEXT: ret i1 [[ULT]]
;
%non.zero = icmp ugt i64 %x, 0
call void @llvm.assume(i1 %non.zero)