[LV] Keep Primary Induction alive when folding tail by masking
authorAyal Zaks <ayal.zaks@intel.com>
Tue, 24 Nov 2020 10:15:18 +0000 (12:15 +0200)
committerAyal Zaks <ayal.zaks@intel.com>
Tue, 24 Nov 2020 13:12:54 +0000 (15:12 +0200)
Fix PR47390.

The primary induction should be considered alive when folding tail by masking,
because it will be used by said masking; even when it may otherwise appear
useless: feeding only its own 'bump', which is correctly considered dead, and
as the 'bump' of another induction variable, which may wrongfully want to
consider its bump = the primary induction, dead.

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

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
llvm/test/Transforms/LoopVectorize/dead_instructions.ll

index be5db9b4c5c41641cb2e308b0a0806d7dd98f811..3253678e6532eaf94265fc11635705dcc8a513b4 100644 (file)
@@ -7132,6 +7132,12 @@ void LoopVectorizationPlanner::collectTriviallyDeadInstructions(
   for (auto &Induction : Legal->getInductionVars()) {
     PHINode *Ind = Induction.first;
     auto *IndUpdate = cast<Instruction>(Ind->getIncomingValueForBlock(Latch));
+
+    // If the tail is to be folded by masking, the primary induction variable,
+    // if exists, isn't dead: it will be used for masking. Don't kill it.
+    if (CM.foldTailByMasking() && IndUpdate == Legal->getPrimaryInduction())
+      continue;
+
     if (llvm::all_of(IndUpdate->users(), [&](User *U) -> bool {
           return U == Ind || DeadInstructions.count(cast<Instruction>(U));
         }))
index fb929ee4ebda8555d99a96ef4cac8b2518b849bc..06215aac9b5ceb29d7631222ee737b606575510e 100644 (file)
@@ -40,3 +40,32 @@ for.end:
   %tmp3  = phi i64 [ %tmp2, %for.body ]
   ret i64 %tmp3
 }
+
+
+; CHECK-LABEL: @pr47390
+;
+; This test ensures that the primary induction is not considered dead when
+; acting as the 'add' of another induction, and otherwise feeding only its own
+; 'add' (recognized earlier as 'dead'), when the tail of the loop is folded by
+; masking. Such masking uses the primary induction.
+;
+; CHECK:     vector.body:
+;
+define void @pr47390(i32 *%a) {
+entry:
+  br label %loop
+
+exit:
+  ret void
+
+loop:
+  %primary = phi i32 [ 0, %entry ], [ %primary_add, %loop ]
+  %use_primary = phi i32 [ -1, %entry ], [ %primary, %loop ]
+  %secondary = phi i32 [ 1, %entry ], [ %secondary_add, %loop ]
+  %primary_add = add i32 %primary, 1
+  %secondary_add = add i32 %secondary, 1
+  %gep = getelementptr inbounds i32, i32* %a, i32 %secondary
+  %load = load i32, i32* %gep, align 8
+  %cmp = icmp eq i32 %secondary, 5
+  br i1 %cmp, label %exit, label %loop
+}