Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit b20d324

Browse files
committed
Merging r309330:
------------------------------------------------------------------------ r309330 | davide | 2017-07-27 15:20:44 -0700 (Thu, 27 Jul 2017) | 13 lines [ConstantFolder] Don't try to fold gep when the idx is a vector. The code in ConstantFoldGetElementPtr() assumes integers, and therefore it crashes trying to get the integer bidwith of a vector type (in this case <4 x i32>. I just changed the code to prevent the folding in case of vectors and I didn't bother to generalize as this doesn't seem to me something that really happens in practice, but I'm willing to change the patch if you think it's worth it. This is hard to trigger from -instsimplify or -instcombine only as the second instruction is dead, so the test uses loop-unroll. Differential Revision: https://reviews.llvm.org/D35956 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_50@309595 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 2f5b5ca commit b20d324

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

lib/IR/ConstantFold.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,15 +2097,19 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
20972097
// Subsequent evaluation would get confused and produce erroneous results.
20982098
//
20992099
// The following prohibits such a GEP from being formed by checking to see
2100-
// if the index is in-range with respect to an array or vector.
2100+
// if the index is in-range with respect to an array.
2101+
// TODO: This code may be extended to handle vectors as well.
21012102
bool PerformFold = false;
21022103
if (Idx0->isNullValue())
21032104
PerformFold = true;
21042105
else if (LastI.isSequential())
21052106
if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx0))
2106-
PerformFold =
2107-
!LastI.isBoundedSequential() ||
2108-
isIndexInRangeOfArrayType(LastI.getSequentialNumElements(), CI);
2107+
PerformFold = (!LastI.isBoundedSequential() ||
2108+
isIndexInRangeOfArrayType(
2109+
LastI.getSequentialNumElements(), CI)) &&
2110+
!CE->getOperand(CE->getNumOperands() - 1)
2111+
->getType()
2112+
->isVectorTy();
21092113

21102114
if (PerformFold) {
21112115
SmallVector<Value*, 16> NewIndices;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -loop-unroll -S %s | FileCheck %s
3+
4+
%struct.bar = type { i32 }
5+
6+
@global = external constant [78 x %struct.bar], align 4
7+
8+
define void @patatino(i32 %x) {
9+
; CHECK-LABEL: @patatino(
10+
; CHECK-NEXT: bb:
11+
; CHECK-NEXT: br i1 true, label [[BB1_PREHEADER:%.*]], label [[BB3:%.*]]
12+
; CHECK: bb1.preheader:
13+
; CHECK-NEXT: br label [[BB1:%.*]]
14+
; CHECK: bb1:
15+
; CHECK-NEXT: br label [[BB3]]
16+
; CHECK: bb3:
17+
; CHECK-NEXT: ret void
18+
;
19+
bb:
20+
br i1 true, label %bb1, label %bb3
21+
22+
bb1:
23+
%tmp = getelementptr inbounds [78 x %struct.bar], [78 x %struct.bar]* @global, i32 0, <4 x i32> undef
24+
%tmp2 = getelementptr inbounds %struct.bar, <4 x %struct.bar*> %tmp, i32 1
25+
br i1 true, label %bb3, label %bb1
26+
27+
bb3:
28+
ret void
29+
}

0 commit comments

Comments
 (0)