[llvm] r346870 - [VPlan, SLP] Use SmallPtrSet for Candidates.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 14 07:58:40 PST 2018


Author: fhahn
Date: Wed Nov 14 07:58:40 2018
New Revision: 346870

URL: http://llvm.org/viewvc/llvm-project?rev=346870&view=rev
Log:
[VPlan, SLP] Use SmallPtrSet for Candidates.

This slightly improves the candidate handling in getBest().

Modified:
    llvm/trunk/lib/Transforms/Vectorize/VPlan.h
    llvm/trunk/lib/Transforms/Vectorize/VPlanSLP.cpp

Modified: llvm/trunk/lib/Transforms/Vectorize/VPlan.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Vectorize/VPlan.h?rev=346870&r1=346869&r2=346870&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Vectorize/VPlan.h (original)
+++ llvm/trunk/lib/Transforms/Vectorize/VPlan.h Wed Nov 14 07:58:40 2018
@@ -1607,7 +1607,7 @@ private:
   /// candidates to choose from are values with an opcode matching \p Last's
   /// or loads consecutive to \p Last.
   std::pair<OpMode, VPValue *> getBest(OpMode Mode, VPValue *Last,
-                                       SmallVectorImpl<VPValue *> &Candidates,
+                                       SmallPtrSetImpl<VPValue *> &Candidates,
                                        VPInterleavedAccessInfo &IAI);
 
   /// Print bundle \p Values to dbgs().

Modified: llvm/trunk/lib/Transforms/Vectorize/VPlanSLP.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Vectorize/VPlanSLP.cpp?rev=346870&r1=346869&r2=346870&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Vectorize/VPlanSLP.cpp (original)
+++ llvm/trunk/lib/Transforms/Vectorize/VPlanSLP.cpp Wed Nov 14 07:58:40 2018
@@ -240,12 +240,13 @@ static unsigned getLAScore(VPValue *V1,
 
 std::pair<VPlanSlp::OpMode, VPValue *>
 VPlanSlp::getBest(OpMode Mode, VPValue *Last,
-                  SmallVectorImpl<VPValue *> &Candidates,
+                  SmallPtrSetImpl<VPValue *> &Candidates,
                   VPInterleavedAccessInfo &IAI) {
+  assert((Mode == OpMode::Load || Mode == OpMode::Opcode) &&
+         "Currently we only handle load and commutative opcodes");
   LLVM_DEBUG(dbgs() << "      getBest\n");
-  VPValue *Best = Candidates[0];
-  SmallVector<VPValue *, 4> BestCandidates;
 
+  SmallVector<VPValue *, 4> BestCandidates;
   LLVM_DEBUG(dbgs() << "        Candidates  for "
                     << *cast<VPInstruction>(Last)->getUnderlyingInstr() << " ");
   for (auto *Candidate : Candidates) {
@@ -265,34 +266,33 @@ VPlanSlp::getBest(OpMode Mode, VPValue *
   if (BestCandidates.size() == 1)
     return {Mode, BestCandidates[0]};
 
-  if (Mode == OpMode::Opcode) {
-    unsigned BestScore = 0;
-    for (unsigned Depth = 1; Depth < LookaheadMaxDepth; Depth++) {
-      unsigned PrevScore = ~0u;
-      bool AllSame = true;
-
-      // FIXME: Avoid visiting the same operands multiple times.
-      for (auto *Candidate : BestCandidates) {
-        unsigned Score = getLAScore(Last, Candidate, Depth, IAI);
-        if (PrevScore == ~0u)
-          PrevScore = Score;
-        if (PrevScore != Score)
-          AllSame = false;
+  VPValue *Best = nullptr;
+  unsigned BestScore = 0;
+  for (unsigned Depth = 1; Depth < LookaheadMaxDepth; Depth++) {
+    unsigned PrevScore = ~0u;
+    bool AllSame = true;
+
+    // FIXME: Avoid visiting the same operands multiple times.
+    for (auto *Candidate : BestCandidates) {
+      unsigned Score = getLAScore(Last, Candidate, Depth, IAI);
+      if (PrevScore == ~0u)
         PrevScore = Score;
-
-        if (Score > BestScore) {
-          BestScore = Score;
-          Best = Candidate;
-        }
+      if (PrevScore != Score)
+        AllSame = false;
+      PrevScore = Score;
+
+      if (Score > BestScore) {
+        BestScore = Score;
+        Best = Candidate;
       }
-      if (!AllSame)
-        break;
     }
+    if (!AllSame)
+      break;
   }
   LLVM_DEBUG(dbgs() << "Found best "
                     << *cast<VPInstruction>(Best)->getUnderlyingInstr()
                     << "\n");
-  std::remove(Candidates.begin(), Candidates.end(), Best);
+  Candidates.erase(Best);
 
   return {Mode, Best};
 }
@@ -316,14 +316,13 @@ SmallVector<VPlanSlp::MultiNodeOpTy, 4>
 
   for (unsigned Lane = 1, E = MultiNodeOps[0].second.size(); Lane < E; ++Lane) {
     LLVM_DEBUG(dbgs() << "  Finding best value for lane " << Lane << "\n");
-    SmallVector<VPValue *, 4> Candidates;
-    Candidates.reserve(MultiNodeOps.size());
+    SmallPtrSet<VPValue *, 4> Candidates;
     LLVM_DEBUG(dbgs() << "  Candidates  ");
     for (auto Ops : MultiNodeOps) {
       LLVM_DEBUG(
           dbgs() << *cast<VPInstruction>(Ops.second[Lane])->getUnderlyingInstr()
                  << " ");
-      Candidates.push_back(Ops.second[Lane]);
+      Candidates.insert(Ops.second[Lane]);
     }
     LLVM_DEBUG(dbgs() << "\n");
 




More information about the llvm-commits mailing list