[llvm] Optimize circular buffers (PR #75722)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 14 16:30:37 PST 2024


https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/75722

>From 9b55522318670a579f09daa1200124ef9ce75374 Mon Sep 17 00:00:00 2001
From: Rose <83477269+AtariDreams at users.noreply.github.com>
Date: Sat, 16 Dec 2023 19:14:05 -0500
Subject: [PATCH] Optimize circular buffers

Optimized circular buffers where it can be proven that we can compare instead of doing the division.
---
 llvm/include/llvm/DebugInfo/PDB/Native/HashTable.h | 3 ++-
 llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp    | 2 +-
 llvm/lib/Transforms/Utils/LoopUnroll.cpp           | 4 +++-
 llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp    | 8 ++++----
 llvm/tools/llvm-profdata/llvm-profdata.cpp         | 3 ++-
 llvm/tools/llvm-xray/xray-color-helper.cpp         | 6 +++---
 6 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/llvm/include/llvm/DebugInfo/PDB/Native/HashTable.h b/llvm/include/llvm/DebugInfo/PDB/Native/HashTable.h
index eb03397ba69400..0aa0999d795859 100644
--- a/llvm/include/llvm/DebugInfo/PDB/Native/HashTable.h
+++ b/llvm/include/llvm/DebugInfo/PDB/Native/HashTable.h
@@ -234,7 +234,8 @@ class HashTable {
         if (!isDeleted(I))
           break;
       }
-      I = (I + 1) % capacity();
+      if (++I == capacity())
+        I = 0;
     } while (I != H);
 
     // The only way FirstUnused would not be set is if every single entry in the
diff --git a/llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp b/llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp
index 1437a2b5acbcf6..6f061a578bad12 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp
@@ -3380,7 +3380,7 @@ static bool tryBitfieldInsertOpFromOr(SDNode *N, const APInt &UsefulBits,
   // Several of these combinations may match, so check with BiggerPattern=false
   // first since that will produce better results by matching more instructions
   // and/or inserting fewer extra instructions.
-  for (int I = 0; I < 4; ++I) {
+  for (unsigned I = 0; I < 4; ++I) {
 
     SDValue Dst, Src;
     unsigned ImmR, ImmS;
diff --git a/llvm/lib/Transforms/Utils/LoopUnroll.cpp b/llvm/lib/Transforms/Utils/LoopUnroll.cpp
index ee6f7b35750af0..8f4ca3b465965b 100644
--- a/llvm/lib/Transforms/Utils/LoopUnroll.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUnroll.cpp
@@ -766,7 +766,9 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
     ExitInfo &Info = Pair.second;
     for (unsigned i = 0, e = Info.ExitingBlocks.size(); i != e; ++i) {
       // The branch destination.
-      unsigned j = (i + 1) % e;
+      unsigned j = i + 1;
+      if (j == e)
+        j = 0;
       bool IsLatch = Pair.first == LatchBlock;
       std::optional<bool> KnownWillExit = WillExit(Info, i, j, IsLatch);
       if (!KnownWillExit) {
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index ba804c10127e2c..6ca2eedcd4660f 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -5690,7 +5690,7 @@ void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth,
     SmallVector<SmallVector<std::pair<Value *, Value *>>> Candidates;
     auto *I1 = cast<Instruction>(VL.front());
     auto *I2 = cast<Instruction>(VL.back());
-    for (int Op = 0, E = S.MainOp->getNumOperands(); Op < E; ++Op)
+    for (unsigned Op = 0, E = S.MainOp->getNumOperands(); Op != E; ++Op)
       Candidates.emplace_back().emplace_back(I1->getOperand(Op),
                                              I2->getOperand(Op));
     if (static_cast<unsigned>(count_if(
@@ -5703,9 +5703,9 @@ void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth,
     if (IsCommutative) {
       // Check permuted operands.
       Candidates.clear();
-      for (int Op = 0, E = S.MainOp->getNumOperands(); Op < E; ++Op)
-        Candidates.emplace_back().emplace_back(I1->getOperand(Op),
-                                               I2->getOperand((Op + 1) % E));
+      for (unsigned Op = 0, E = S.MainOp->getNumOperands(); Op != E; ++Op)
+        Candidates.emplace_back().emplace_back(
+            I1->getOperand(Op), I2->getOperand(Op + 1 == E ? 0 : Op + 1));
       if (any_of(
               Candidates, [this](ArrayRef<std::pair<Value *, Value *>> Cand) {
                 return findBestRootPair(Cand, LookAheadHeuristics::ScoreSplat);
diff --git a/llvm/tools/llvm-profdata/llvm-profdata.cpp b/llvm/tools/llvm-profdata/llvm-profdata.cpp
index e6dc81ba1f5b02..5f4654760ba2f8 100644
--- a/llvm/tools/llvm-profdata/llvm-profdata.cpp
+++ b/llvm/tools/llvm-profdata/llvm-profdata.cpp
@@ -842,7 +842,8 @@ static void mergeInstrProfile(const WeightedFileVector &Inputs,
     for (const auto &Input : Inputs) {
       Pool.async(loadInput, Input, Remapper, Correlator.get(), ProfiledBinary,
                  Contexts[Ctx].get());
-      Ctx = (Ctx + 1) % NumThreads;
+      if (++Ctx >= NumThreads)
+        Ctx = 0;
     }
     Pool.wait();
 
diff --git a/llvm/tools/llvm-xray/xray-color-helper.cpp b/llvm/tools/llvm-xray/xray-color-helper.cpp
index 3dd5143ca8d0b9..ce6a93202a8e43 100644
--- a/llvm/tools/llvm-xray/xray-color-helper.cpp
+++ b/llvm/tools/llvm-xray/xray-color-helper.cpp
@@ -84,9 +84,9 @@ static std::tuple<double, double, double>
 convertToHSV(const std::tuple<uint8_t, uint8_t, uint8_t> &Color) {
   double Scaled[3] = {std::get<0>(Color) / 255.0, std::get<1>(Color) / 255.0,
                       std::get<2>(Color) / 255.0};
-  int Min = 0;
-  int Max = 0;
-  for (int i = 1; i < 3; ++i) {
+  unsigned Min = 0;
+  unsigned Max = 0;
+  for (unsigned i = 1; i < 3; ++i) {
     if (Scaled[i] < Scaled[Min])
       Min = i;
     if (Scaled[i] > Scaled[Max])



More information about the llvm-commits mailing list