[PATCH] D75887: [SCEV] Add support for GEPs over scalable vectors.

Eli Friedman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 9 18:54:14 PDT 2020


efriedma created this revision.
efriedma added reviewers: sdesmalen, reames.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.

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. :)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75887

Files:
  llvm/lib/Analysis/ScalarEvolution.cpp
  llvm/lib/Analysis/ValueTracking.cpp
  llvm/test/Analysis/ScalarEvolution/scalable-vector.ll


Index: llvm/test/Analysis/ScalarEvolution/scalable-vector.ll
===================================================================
--- /dev/null
+++ 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
+}
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -1447,7 +1447,7 @@
           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,
Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -3503,7 +3503,7 @@
   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();
   for (const SCEV *IndexExpr : IndexExprs) {
     // Compute the (potentially symbolic) offset in bytes for this index.
     if (StructType *STy = dyn_cast<StructType>(CurTy)) {
@@ -3519,7 +3519,10 @@
       CurTy = STy->getTypeAtIndex(Index);
     } else {
       // Update CurTy to its element type.
-      CurTy = cast<SequentialType>(CurTy)->getElementType();
+      if (CurTy == GEP->getType())
+        CurTy = GEP->getSourceElementType();
+      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.
@@ -3723,6 +3726,14 @@
   // 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));
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75887.249262.patch
Type: text/x-patch
Size: 3231 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200310/46e52bc9/attachment.bin>


More information about the llvm-commits mailing list