[llvm] [Analysis] Teach isDereferenceableAndAlignedInLoop about SCEV predicates (PR #106562)

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 6 08:00:31 PDT 2024


================
@@ -1331,11 +1331,17 @@ bool LoopVectorizationLegality::canVectorizeWithIfConvert() {
     // we restrict this to loads; stores are more complicated due to
     // concurrency restrictions.
     ScalarEvolution &SE = *PSE.getSE();
+    SmallVector<const SCEVPredicate *, 4> Predicates;
     for (Instruction &I : *BB) {
       LoadInst *LI = dyn_cast<LoadInst>(&I);
+      // Pass the Predicates pointer to isDereferenceableAndAlignedInLoop so
+      // that it will consider loops that need guarding by SCEV checks. The
+      // vectoriser will generate these checks if we decide to vectorise.
       if (LI && !LI->getType()->isVectorTy() && !mustSuppressSpeculation(*LI) &&
-          isDereferenceableAndAlignedInLoop(LI, TheLoop, SE, *DT, AC))
+          isDereferenceableAndAlignedInLoop(LI, TheLoop, SE, *DT, AC,
+                                            &Predicates))
         SafePointers.insert(LI->getPointerOperand());
+      Predicates.clear();
----------------
david-arm wrote:

I realise it looks a bit confusing, but when we call `isDereferenceableAndAlignedInLoop` all that we really care about is whether or not we can dereference these loads unconditionally if we assume the predicates are set up correctly. In fact, if we actually decide to vectorise the loop we will collect *all* the predicates required for all blocks in the loop. This happens for free because at some point we call `PSE.getBackedgeTakenCount()` and the PredicatedScalarEvolution class will keep track of all the predicates, which will get code-generated later. I added the `Predicates` argument to `isDereferenceableAndAlignedInLoop` in case someone actually wants to inspect each predicate, but in this particular case it's safe to drop them.

https://github.com/llvm/llvm-project/pull/106562


More information about the llvm-commits mailing list