Skip to content

Commit e6d3a42

Browse files
authored
[CodeGen] Use SmallVector for FixedStackPSVs (#91760)
Frame indices are dense and consecutive, so use a vector instead of a std::map. Due to possibly 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.
1 parent cbd72cb commit e6d3a42

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

llvm/include/llvm/CodeGen/PseudoSourceValueManager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
#ifndef LLVM_CODEGEN_PSEUDOSOURCEVALUEMANAGER_H
1414
#define LLVM_CODEGEN_PSEUDOSOURCEVALUEMANAGER_H
1515

16+
#include "llvm/ADT/SmallVector.h"
1617
#include "llvm/ADT/StringMap.h"
1718
#include "llvm/CodeGen/PseudoSourceValue.h"
1819
#include "llvm/IR/ValueMap.h"
19-
#include <map>
2020

2121
namespace llvm {
2222

@@ -27,7 +27,7 @@ class TargetMachine;
2727
class PseudoSourceValueManager {
2828
const TargetMachine &TM;
2929
const PseudoSourceValue StackPSV, GOTPSV, JumpTablePSV, ConstantPoolPSV;
30-
std::map<int, std::unique_ptr<FixedStackPseudoSourceValue>> FSValues;
30+
SmallVector<std::unique_ptr<FixedStackPseudoSourceValue>> FSValues;
3131
StringMap<std::unique_ptr<const ExternalSymbolPseudoSourceValue>>
3232
ExternalCallEntries;
3333
ValueMap<const GlobalValue *,

llvm/lib/CodeGen/PseudoSourceValue.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,12 @@ const PseudoSourceValue *PseudoSourceValueManager::getJumpTable() {
122122

123123
const PseudoSourceValue *
124124
PseudoSourceValueManager::getFixedStack(int FI) {
125-
std::unique_ptr<FixedStackPseudoSourceValue> &V = FSValues[FI];
125+
// Frame index is often continuously positive, but can be negative. Use
126+
// zig-zag encoding for dense index into FSValues vector.
127+
unsigned Idx = (2 * unsigned(FI)) ^ (FI >> (sizeof(FI) * 8 - 1));
128+
if (FSValues.size() <= Idx)
129+
FSValues.resize(Idx + 1);
130+
std::unique_ptr<FixedStackPseudoSourceValue> &V = FSValues[Idx];
126131
if (!V)
127132
V = std::make_unique<FixedStackPseudoSourceValue>(FI, TM);
128133
return V.get();

0 commit comments

Comments
 (0)