aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/IR/ConstantFold.cpp
diff options
context:
space:
mode:
authorNikita Popov <npopov@redhat.com>2023-02-01 14:34:38 +0100
committerNikita Popov <npopov@redhat.com>2023-02-01 15:14:11 +0100
commit78f88082de3627ea04501c83a08f52cf1e60b4f7 (patch)
treea45a995048543c7473631f512ad0fb74caba424b /llvm/lib/IR/ConstantFold.cpp
parent9c4591d7f3acaa00318900bdba4b4ba26c99c666 (diff)
downloadllvm-78f88082de3627ea04501c83a08f52cf1e60b4f7.zip
llvm-78f88082de3627ea04501c83a08f52cf1e60b4f7.tar.gz
llvm-78f88082de3627ea04501c83a08f52cf1e60b4f7.tar.bz2
[ConstantFold] Fix incorrect inbounds inference for [0 x T] GEPs
Previously all indices into [0 x T] arrays were considered in range, which resulted in us incorrectly inferring inbounds for all GEPs of that form. We should not consider them in range here, and instead bail out of the rewriting logic (which would divide by zero). Do continue to consider 0 always in range, to avoid changing behavior for zero-index GEPs.
Diffstat (limited to 'llvm/lib/IR/ConstantFold.cpp')
-rw-r--r--llvm/lib/IR/ConstantFold.cpp12
1 files changed, 9 insertions, 3 deletions
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index 5fa56d3..18f481a 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -1951,7 +1951,7 @@ static bool isIndexInRangeOfArrayType(uint64_t NumElements,
// A negative index or an index past the end of our sequential type is
// considered out-of-range.
int64_t IndexVal = CI->getSExtValue();
- if (IndexVal < 0 || (NumElements > 0 && (uint64_t)IndexVal >= NumElements))
+ if (IndexVal < 0 || (IndexVal != 0 && (uint64_t)IndexVal >= NumElements))
return false;
// Otherwise, it is in-range.
@@ -2202,11 +2202,17 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
Unknown = true;
continue;
}
+
+ // Determine the number of elements in our sequential type.
+ uint64_t NumElements = STy->getArrayNumElements();
+ if (!NumElements) {
+ Unknown = true;
+ continue;
+ }
+
// It's out of range, but we can factor it into the prior
// dimension.
NewIdxs.resize(Idxs.size());
- // Determine the number of elements in our sequential type.
- uint64_t NumElements = STy->getArrayNumElements();
// Expand the current index or the previous index to a vector from a scalar
// if necessary.