Skip to content

Commit

Permalink
[SCEV] Add support for GEPs over scalable vectors.
Browse files Browse the repository at this point in the history
Because we have to use a ConstantExpr at some point, the canonical form
isn't set in stone, but this seems reasonable.

The pretty sizeof(<vscale x 4 x i32>) dumping is a relic of ancient
LLVM; I didn't have to touch that code. :)

Differential Revision: https://reviews.llvm.org/D75887
  • Loading branch information
efriedma-quic committed Mar 13, 2020
1 parent ad7b930 commit 65fc706
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
22 changes: 18 additions & 4 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Expand Up @@ -3532,9 +3532,8 @@ ScalarEvolution::getGEPExpr(GEPOperator *GEP,
: SCEV::FlagAnyWrap;

const SCEV *TotalOffset = getZero(IntIdxTy);
// The array size is unimportant. The first thing we do on CurTy is getting
// its element type.
Type *CurTy = ArrayType::get(GEP->getSourceElementType(), 0);
Type *CurTy = GEP->getType();
bool FirstIter = true;
for (const SCEV *IndexExpr : IndexExprs) {
// Compute the (potentially symbolic) offset in bytes for this index.
if (StructType *STy = dyn_cast<StructType>(CurTy)) {
Expand All @@ -3550,7 +3549,14 @@ ScalarEvolution::getGEPExpr(GEPOperator *GEP,
CurTy = STy->getTypeAtIndex(Index);
} else {
// Update CurTy to its element type.
CurTy = cast<SequentialType>(CurTy)->getElementType();
if (FirstIter) {
assert(isa<PointerType>(CurTy) &&
"The first index of a GEP indexes a pointer");
CurTy = GEP->getSourceElementType();
FirstIter = false;
} else {
CurTy = cast<SequentialType>(CurTy)->getElementType();
}
// For an array, add the element offset, explicitly scaled.
const SCEV *ElementSize = getSizeOfExpr(IntIdxTy, CurTy);
// Getelementptr indices are signed.
Expand Down Expand Up @@ -3754,6 +3760,14 @@ const SCEV *ScalarEvolution::getSizeOfExpr(Type *IntTy, Type *AllocTy) {
// We can bypass creating a target-independent
// constant expression and then folding it back into a ConstantInt.
// This is just a compile-time optimization.
if (auto *VecTy = dyn_cast<VectorType>(AllocTy)) {
if (VecTy->isScalable()) {
Constant *NullPtr = Constant::getNullValue(AllocTy->getPointerTo());
Constant *One = ConstantInt::get(IntTy, 1);
Constant *GEP = ConstantExpr::getGetElementPtr(AllocTy, NullPtr, One);
return getSCEV(ConstantExpr::getPtrToInt(GEP, IntTy));
}
}
return getConstant(IntTy, getDataLayout().getTypeAllocSize(AllocTy));
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/ValueTracking.cpp
Expand Up @@ -1447,7 +1447,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
break;
}
unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits();
uint64_t TypeSize = Q.DL.getTypeAllocSize(IndexedTy);
uint64_t TypeSize = Q.DL.getTypeAllocSize(IndexedTy).getKnownMinSize();
LocalKnown.Zero = LocalKnown.One = APInt(GEPOpiBits, 0);
computeKnownBits(Index, LocalKnown, Depth + 1, Q);
TrailZ = std::min(TrailZ,
Expand Down
11 changes: 11 additions & 0 deletions llvm/test/Analysis/ScalarEvolution/scalable-vector.ll
@@ -0,0 +1,11 @@
; RUN: opt -scalar-evolution -analyze < %s | FileCheck %s

; CHECK: %1 = getelementptr <vscale x 4 x i32>, <vscale x 4 x i32>* null, i32 3
; CHECK: --> (3 * sizeof(<vscale x 4 x i32>)) U: [0,-15) S: [-9223372036854775808,9223372036854775793)
; CHECK: %2 = getelementptr <vscale x 1 x i64>, <vscale x 1 x i64>* %p, i32 1
; CHECK: --> (sizeof(<vscale x 1 x i64>) + %p) U: full-set S: full-set
define void @a(<vscale x 1 x i64> *%p) {
getelementptr <vscale x 4 x i32>, <vscale x 4 x i32> *null, i32 3
getelementptr <vscale x 1 x i64>, <vscale x 1 x i64> *%p, i32 1
ret void
}

0 comments on commit 65fc706

Please sign in to comment.