[llvm] [CodeGen] Use SmallVector for FixedStackPSVs (PR #91760)

via llvm-commits llvm-commits at lists.llvm.org
Tue May 14 02:51:38 PDT 2024


https://github.com/aengelke updated https://github.com/llvm/llvm-project/pull/91760

>From fb5344cd02eac553b121594ab050a3fd75e84e00 Mon Sep 17 00:00:00 2001
From: Alexis Engelke <engelke at in.tum.de>
Date: Fri, 10 May 2024 14:45:06 +0200
Subject: [PATCH 1/2] [CodeGen] Use SmallVector for FixedStackPSVs

Frame indices are dense and consecutive, so use a vector instead of a
std::map. Due to possible negative frame indices, use zig-zag encoding.
IndexedMap was not usable, as it attempted to copy the null value, which
is not possible with a std::unique_ptr.

This is just a minor performance improvement, but a low-hanging fruit.
---
 llvm/include/llvm/CodeGen/PseudoSourceValueManager.h | 4 ++--
 llvm/lib/CodeGen/PseudoSourceValue.cpp               | 7 ++++++-
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/PseudoSourceValueManager.h b/llvm/include/llvm/CodeGen/PseudoSourceValueManager.h
index 4be6ae0b60cb8..8ea043bf0327d 100644
--- a/llvm/include/llvm/CodeGen/PseudoSourceValueManager.h
+++ b/llvm/include/llvm/CodeGen/PseudoSourceValueManager.h
@@ -13,10 +13,10 @@
 #ifndef LLVM_CODEGEN_PSEUDOSOURCEVALUEMANAGER_H
 #define LLVM_CODEGEN_PSEUDOSOURCEVALUEMANAGER_H
 
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/CodeGen/PseudoSourceValue.h"
 #include "llvm/IR/ValueMap.h"
-#include <map>
 
 namespace llvm {
 
@@ -27,7 +27,7 @@ class TargetMachine;
 class PseudoSourceValueManager {
   const TargetMachine &TM;
   const PseudoSourceValue StackPSV, GOTPSV, JumpTablePSV, ConstantPoolPSV;
-  std::map<int, std::unique_ptr<FixedStackPseudoSourceValue>> FSValues;
+  SmallVector<std::unique_ptr<FixedStackPseudoSourceValue>> FSValues;
   StringMap<std::unique_ptr<const ExternalSymbolPseudoSourceValue>>
       ExternalCallEntries;
   ValueMap<const GlobalValue *,
diff --git a/llvm/lib/CodeGen/PseudoSourceValue.cpp b/llvm/lib/CodeGen/PseudoSourceValue.cpp
index 0e1a2c921c5cb..f438b38dd481a 100644
--- a/llvm/lib/CodeGen/PseudoSourceValue.cpp
+++ b/llvm/lib/CodeGen/PseudoSourceValue.cpp
@@ -122,7 +122,12 @@ const PseudoSourceValue *PseudoSourceValueManager::getJumpTable() {
 
 const PseudoSourceValue *
 PseudoSourceValueManager::getFixedStack(int FI) {
-  std::unique_ptr<FixedStackPseudoSourceValue> &V = FSValues[FI];
+  // Frame index is often continuously positive, but can be negative. Use
+  // zig-zag encoding for dense index into FSValues vector.
+  unsigned Idx = FI >= 0 ? FI << 1 : (-FI - 1) << 1 | 1;
+  if (FSValues.size() <= Idx)
+    FSValues.resize(Idx + 1);
+  std::unique_ptr<FixedStackPseudoSourceValue> &V = FSValues[Idx];
   if (!V)
     V = std::make_unique<FixedStackPseudoSourceValue>(FI, TM);
   return V.get();

>From d42eab03cb555906ecd9cb934844e63bfec6a4fb Mon Sep 17 00:00:00 2001
From: Alexis Engelke <engelke at in.tum.de>
Date: Tue, 14 May 2024 11:42:34 +0200
Subject: [PATCH 2/2] [CodeGen] More efficient zig-zag encoding

---
 llvm/lib/CodeGen/PseudoSourceValue.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/PseudoSourceValue.cpp b/llvm/lib/CodeGen/PseudoSourceValue.cpp
index f438b38dd481a..242889d317849 100644
--- a/llvm/lib/CodeGen/PseudoSourceValue.cpp
+++ b/llvm/lib/CodeGen/PseudoSourceValue.cpp
@@ -124,7 +124,7 @@ const PseudoSourceValue *
 PseudoSourceValueManager::getFixedStack(int FI) {
   // Frame index is often continuously positive, but can be negative. Use
   // zig-zag encoding for dense index into FSValues vector.
-  unsigned Idx = FI >= 0 ? FI << 1 : (-FI - 1) << 1 | 1;
+  unsigned Idx = (2 * unsigned(FI)) ^ (FI >> (sizeof(FI) * 8 - 1));
   if (FSValues.size() <= Idx)
     FSValues.resize(Idx + 1);
   std::unique_ptr<FixedStackPseudoSourceValue> &V = FSValues[Idx];



More information about the llvm-commits mailing list