[SCEV] Do not plant SCEV checks unnecessarily
authorPaul Osmialowski <pawel.osmialowski@arm.com>
Tue, 25 Apr 2023 20:44:35 +0000 (21:44 +0100)
committerPaul Osmialowski <pawel.osmialowski@arm.com>
Tue, 25 Apr 2023 20:47:14 +0000 (21:47 +0100)
The vectorisation analysis collects strides for loop invariant
pointers, which is wrong because they are not strided. We don't
need to generate SCEV checks (which are costly performancewise)
for such pointers, we just need to do the appropriate aliasing
checks.

This patch fixes the problem by changing getStrideFromPointer()
to treat loop invariant pointers as having no stride.

Originally proposed by David Sherwood with further suggestions
from Florian Hahn.

Reviewed By: fhahn

Differential Revision: https://reviews.llvm.org/D146958

llvm/lib/Analysis/LoopAccessAnalysis.cpp
llvm/test/Transforms/LoopVectorize/vector-no-scevcheck.ll

index 623cd22..4420d72 100644 (file)
@@ -2642,6 +2642,11 @@ static Value *getStrideFromPointer(Value *Ptr, ScalarEvolution *SE, Loop *Lp) {
   if (!S)
     return nullptr;
 
+  // If the pointer is invariant then there is no stride and it makes no
+  // sense to add it here.
+  if (Lp != S->getLoop())
+    return nullptr;
+
   V = S->getStepRecurrence(*SE);
   if (!V)
     return nullptr;
index 3b37e15..60d7828 100644 (file)
@@ -5,8 +5,7 @@
 
 define void @foo(ptr %pout, ptr %pin, i64 %val0, i64 %val1, i64 %val2) {
 ; CHECK-LABEL: @foo(
-; FIXME: CHECK below needs to be changed to CHECK-NOT to confirm the change.
-; CHECK: vector.scevcheck
+; CHECK-NOT: vector.scevcheck
 ; CHECK: vector.body
 entry:
   %0 = getelementptr double, ptr %pin, i64 %val0
@@ -45,8 +44,7 @@ exit:                                             ; preds = %loop1.latch
 
 define void @bar(ptr %pout, ptr %pin, i64 %val0, i64 %val1, i64 %val2) {
 ; CHECK-LABEL: @bar(
-; FIXME: CHECK below needs to be changed to CHECK-NOT to confirm the change.
-; CHECK: vector.scevcheck
+; CHECK-NOT: vector.scevcheck
 ; CHECK: vector.body
 entry:
   %0 = getelementptr double, ptr %pin, i64 %val0