summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Simpson <mssimpso@codeaurora.org>2018-03-15 16:00:29 +0000
committerMatthew Simpson <mssimpso@codeaurora.org>2018-03-15 16:00:29 +0000
commitc1c4ad6e64fc0037f4f21952bc6d1a569bce2fa1 (patch)
tree4336aca3876235b0a7e20243a3ae58b4ea25bdbb
parentf3de222b0d3c169985ba9d61f479f3e8a3675100 (diff)
downloadbcm5719-llvm-c1c4ad6e64fc0037f4f21952bc6d1a569bce2fa1.tar.gz
bcm5719-llvm-c1c4ad6e64fc0037f4f21952bc6d1a569bce2fa1.zip
[ConstantFolding, InstSimplify] Handle more vector GEPs
This patch addresses some additional cases where the compiler crashes upon encountering vector GEPs. This should fix PR36116. Differential Revision: https://reviews.llvm.org/D44219 Reference: https://bugs.llvm.org/show_bug.cgi?id=36116 llvm-svn: 327638
-rw-r--r--llvm/lib/Analysis/InstructionSimplify.cpp4
-rw-r--r--llvm/lib/IR/ConstantFold.cpp25
-rw-r--r--llvm/test/Analysis/ConstantFolding/vectorgep-crash.ll26
-rw-r--r--llvm/test/Transforms/InstSimplify/vector_gep.ll18
4 files changed, 64 insertions, 9 deletions
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 5e43cb7cec1..67669c2eca1 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -3723,7 +3723,7 @@ static Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops,
if (Ops.size() == 2) {
// getelementptr P, 0 -> P.
- if (match(Ops[1], m_Zero()))
+ if (match(Ops[1], m_Zero()) && Ops[0]->getType() == GEPTy)
return Ops[0];
Type *Ty = SrcTy;
@@ -3732,7 +3732,7 @@ static Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops,
uint64_t C;
uint64_t TyAllocSize = Q.DL.getTypeAllocSize(Ty);
// getelementptr P, N -> P if P points to a type of zero size.
- if (TyAllocSize == 0)
+ if (TyAllocSize == 0 && Ops[0]->getType() == GEPTy)
return Ops[0];
// The following transforms are only safe if the ptrtoint cast
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index 92d35a27d69..7ad2d46bfb6 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -2027,8 +2027,16 @@ static bool isInBoundsIndices(ArrayRef<IndexTy> Idxs) {
// If the first index is one and all the rest are zero, it's in bounds,
// by the one-past-the-end rule.
- if (!cast<ConstantInt>(Idxs[0])->isOne())
- return false;
+ if (auto *CI = dyn_cast<ConstantInt>(Idxs[0])) {
+ if (!CI->isOne())
+ return false;
+ } else {
+ auto *CV = cast<ConstantDataVector>(Idxs[0]);
+ CI = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
+ if (!CI || !CI->isOne())
+ return false;
+ }
+
for (unsigned i = 1, e = Idxs.size(); i != e; ++i)
if (!cast<Constant>(Idxs[i])->isNullValue())
return false;
@@ -2058,15 +2066,18 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
ArrayRef<Value *> Idxs) {
if (Idxs.empty()) return C;
- if (isa<UndefValue>(C)) {
- Type *GEPTy = GetElementPtrInst::getGEPReturnType(
- C, makeArrayRef((Value * const *)Idxs.data(), Idxs.size()));
+ Type *GEPTy = GetElementPtrInst::getGEPReturnType(
+ C, makeArrayRef((Value *const *)Idxs.data(), Idxs.size()));
+
+ if (isa<UndefValue>(C))
return UndefValue::get(GEPTy);
- }
Constant *Idx0 = cast<Constant>(Idxs[0]);
if (Idxs.size() == 1 && (Idx0->isNullValue() || isa<UndefValue>(Idx0)))
- return C;
+ return GEPTy->isVectorTy() && !C->getType()->isVectorTy()
+ ? ConstantVector::getSplat(
+ cast<VectorType>(GEPTy)->getNumElements(), C)
+ : C;
if (C->isNullValue()) {
bool isNull = true;
diff --git a/llvm/test/Analysis/ConstantFolding/vectorgep-crash.ll b/llvm/test/Analysis/ConstantFolding/vectorgep-crash.ll
index e7a5117d6ed..b2faf89c2b7 100644
--- a/llvm/test/Analysis/ConstantFolding/vectorgep-crash.ll
+++ b/llvm/test/Analysis/ConstantFolding/vectorgep-crash.ll
@@ -38,3 +38,29 @@ vector.body:
%VectorGep = getelementptr [65 x %struct.A], [65 x %struct.A]* @G, <16 x i32> zeroinitializer, <16 x i64> <i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9, i64 10, i64 11, i64 12, i64 13, i64 14, i64 15, i64 16>, <16 x i32> zeroinitializer
ret <16 x i32*> %VectorGep
}
+
+@g = external global i8, align 1
+
+define <2 x i8*> @constant_zero_index() {
+; CHECK-LABEL: @constant_zero_index(
+; CHECK-NEXT: ret <2 x i8*> <i8* @g, i8* @g>
+;
+ %gep = getelementptr i8, i8* @g, <2 x i64> zeroinitializer
+ ret <2 x i8*> %gep
+}
+
+define <2 x i8*> @constant_undef_index() {
+; CHECK-LABEL: @constant_undef_index(
+; CHECK-NEXT: ret <2 x i8*> <i8* @g, i8* @g>
+;
+ %gep = getelementptr i8, i8* @g, <2 x i64> undef
+ ret <2 x i8*> %gep
+}
+
+define <2 x i8*> @constant_inbounds() {
+; CHECK-LABEL: @constant_inbounds(
+; CHECK-NEXT: ret <2 x i8*> getelementptr inbounds (i8, i8* @g, <2 x i64> <i64 1, i64 1>)
+;
+ %gep = getelementptr i8, i8* @g, <2 x i64> <i64 1, i64 1>
+ ret <2 x i8*> %gep
+}
diff --git a/llvm/test/Transforms/InstSimplify/vector_gep.ll b/llvm/test/Transforms/InstSimplify/vector_gep.ll
index 25f2255a2a7..42916c4c07a 100644
--- a/llvm/test/Transforms/InstSimplify/vector_gep.ll
+++ b/llvm/test/Transforms/InstSimplify/vector_gep.ll
@@ -86,3 +86,21 @@ define <4 x i8*> @laalaa() {
%patatino = getelementptr i8, <4 x i8 *> undef, i64 undef
ret <4 x i8*> %patatino
}
+
+define <2 x i8*> @zero_index(i8* %p) {
+; CHECK-LABEL: @zero_index(
+; CHECK-NEXT: %gep = getelementptr i8, i8* %p, <2 x i64> zeroinitializer
+; CHECK-NEXT: ret <2 x i8*> %gep
+;
+ %gep = getelementptr i8, i8* %p, <2 x i64> zeroinitializer
+ ret <2 x i8*> %gep
+}
+
+define <2 x {}*> @unsized({}* %p) {
+; CHECK-LABEL: @unsized(
+; CHECK-NEXT: %gep = getelementptr {}, {}* %p, <2 x i64> undef
+; CHECK-NEXT: ret <2 x {}*> %gep
+;
+ %gep = getelementptr {}, {}* %p, <2 x i64> undef
+ ret <2 x {}*> %gep
+}
OpenPOWER on IntegriCloud