[llvm] 30982a5 - [ConstraintElimination] Make decompose() infallible
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 14 01:42:12 PST 2022
Author: Nikita Popov
Date: 2022-11-14T10:42:04+01:00
New Revision: 30982a595da66c4224ca0c2601fbad16ce75a78a
URL: https://github.com/llvm/llvm-project/commit/30982a595da66c4224ca0c2601fbad16ce75a78a
DIFF: https://github.com/llvm/llvm-project/commit/30982a595da66c4224ca0c2601fbad16ce75a78a.diff
LOG: [ConstraintElimination] Make decompose() infallible
decompose() currently returns a mix of {} and 0 + 1*V on failure.
This changes it to always return the 0 + 1*V form, thus making
decompose() infallible.
This makes the code marginally more powerful, e.g. we now fold
sub_decomp_i80 by treating the constant as a symbolic value.
Differential Revision: https://reviews.llvm.org/D137847
Added:
Modified:
llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
llvm/test/Transforms/ConstraintElimination/large-constant-ints.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index bd214e004a022..940f149c7f0df 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -210,7 +210,7 @@ decomposeGEP(GetElementPtrInst &GEP,
// Do not reason about pointers where the index size is larger than 64 bits,
// as the coefficients used to encode constraints are 64 bit integers.
if (DL.getIndexTypeSizeInBits(GEP.getPointerOperand()->getType()) > 64)
- return {};
+ return {{0, nullptr}, {1, &GEP}};
if (!GEP.isInBounds())
return {{0, nullptr}, {1, &GEP}};
@@ -226,7 +226,7 @@ decomposeGEP(GetElementPtrInst &GEP,
auto GTI = gep_type_begin(GEP);
// Bail out for scalable vectors for now.
if (isa<ScalableVectorType>(GTI.getIndexedType()))
- return {};
+ return {{0, nullptr}, {1, &GEP}};
int64_t Scale = static_cast<int64_t>(
DL.getTypeAllocSize(GTI.getIndexedType()).getFixedSize());
@@ -253,7 +253,7 @@ decomposeGEP(GetElementPtrInst &GEP,
// Bail out for scalable vectors for now.
if (isa<ScalableVectorType>(GTI.getIndexedType()))
- return {};
+ return {{0, nullptr}, {1, &GEP}};
// Struct indices must be constants (and reference an existing field). Add
// them to the constant factor.
@@ -274,14 +274,11 @@ decomposeGEP(GetElementPtrInst &GEP,
unsigned Scale = DL.getTypeAllocSize(GTI.getIndexedType()).getFixedSize();
auto IdxResult = decompose(Index, Preconditions, IsSigned, DL);
- if (IdxResult.empty()) {
- Result.emplace_back(Scale, Index);
- } else {
- for (auto &KV : IdxResult)
- KV.Coefficient = multiplyWithOverflow(KV.Coefficient, Scale);
- Result[0].Coefficient += IdxResult[0].Coefficient;
- append_range(Result, ArrayRef<DecompEntry>(IdxResult).drop_front());
- }
+ for (auto &KV : IdxResult)
+ KV.Coefficient = multiplyWithOverflow(KV.Coefficient, Scale);
+ Result[0].Coefficient += IdxResult[0].Coefficient;
+ append_range(Result, ArrayRef<DecompEntry>(IdxResult).drop_front());
+
// If Op0 is signed non-negative, the GEP is increasing monotonically and
// can be de-composed.
if (!isKnownNonNegative(Index, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1))
@@ -304,8 +301,6 @@ decompose(Value *V, SmallVector<PreconditionTy, 4> &Preconditions,
bool IsSignedB) -> SmallVector<DecompEntry, 4> {
auto ResA = decompose(A, Preconditions, IsSigned, DL);
auto ResB = decompose(B, Preconditions, IsSignedB, DL);
- if (ResA.empty() || ResB.empty())
- return {};
ResA[0].Coefficient += ResB[0].Coefficient;
append_range(ResA, drop_begin(ResB));
return ResA;
@@ -327,7 +322,7 @@ decompose(Value *V, SmallVector<PreconditionTy, 4> &Preconditions,
if (auto *CI = dyn_cast<ConstantInt>(V)) {
if (CI->uge(MaxConstraintValue))
- return {};
+ return {{0, nullptr}, {1, V}};
return {{int64_t(CI->getZExtValue()), nullptr}};
}
@@ -434,10 +429,6 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
Preconditions, IsSigned, DL);
auto BDec = decompose(Op1->stripPointerCastsSameRepresentation(),
Preconditions, IsSigned, DL);
- // Skip if decomposing either of the values failed.
- if (ADec.empty() || BDec.empty())
- return {};
-
int64_t Offset1 = ADec[0].Coefficient;
int64_t Offset2 = BDec[0].Coefficient;
Offset1 *= -1;
diff --git a/llvm/test/Transforms/ConstraintElimination/large-constant-ints.ll b/llvm/test/Transforms/ConstraintElimination/large-constant-ints.ll
index ac4b32443962b..fa8a4a60eac14 100644
--- a/llvm/test/Transforms/ConstraintElimination/large-constant-ints.ll
+++ b/llvm/test/Transforms/ConstraintElimination/large-constant-ints.ll
@@ -105,7 +105,7 @@ define i1 @sub_decomp_i80(i80 %a) {
; CHECK: then:
; CHECK-NEXT: [[SUB_1:%.*]] = sub nuw i80 [[A]], 1973801615886922022913
; CHECK-NEXT: [[C_1:%.*]] = icmp ult i80 [[SUB_1]], 1346612317380797267967
-; CHECK-NEXT: ret i1 [[C_1]]
+; CHECK-NEXT: ret i1 true
; CHECK: else:
; CHECK-NEXT: ret i1 false
;
More information about the llvm-commits
mailing list