Skip to content

Commit 96f37ae

Browse files
authored
[NFC] Use initial-stack-allocations for more data structures (#110544)
This replaces some of the most frequent offenders of using a DenseMap that cause a malloc, where the typical element-count is small enough to fit in an initial stack allocation. Most of these are fairly obvious, one to highlight is the collectOffset method of GEP instructions: if there's a GEP, of course it's going to have at least one offset, but every time we've called collectOffset we end up calling malloc as well for the DenseMap in the MapVector.
1 parent 4980f21 commit 96f37ae

File tree

17 files changed

+29
-25
lines changed

17 files changed

+29
-25
lines changed

llvm/include/llvm/IR/Instructions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@ class GetElementPtrInst : public Instruction {
11171117
/// the base GEP pointer.
11181118
bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
11191119
bool collectOffset(const DataLayout &DL, unsigned BitWidth,
1120-
MapVector<Value *, APInt> &VariableOffsets,
1120+
SmallMapVector<Value *, APInt, 4> &VariableOffsets,
11211121
APInt &ConstantOffset) const;
11221122
// Methods for support type inquiry through isa, cast, and dyn_cast:
11231123
static bool classof(const Instruction *I) {

llvm/include/llvm/IR/Operator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ class GEPOperator
528528
/// Collect the offset of this GEP as a map of Values to their associated
529529
/// APInt multipliers, as well as a total Constant Offset.
530530
bool collectOffset(const DataLayout &DL, unsigned BitWidth,
531-
MapVector<Value *, APInt> &VariableOffsets,
531+
SmallMapVector<Value *, APInt, 4> &VariableOffsets,
532532
APInt &ConstantOffset) const;
533533
};
534534

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2831,7 +2831,8 @@ static void emitRangeList(
28312831

28322832
// Gather all the ranges that apply to the same section so they can share
28332833
// a base address entry.
2834-
MapVector<const MCSection *, std::vector<decltype(&*R.begin())>> SectionRanges;
2834+
SmallMapVector<const MCSection *, std::vector<decltype(&*R.begin())>, 16>
2835+
SectionRanges;
28352836

28362837
for (const auto &Range : R)
28372838
SectionRanges[&Range.Begin->getSection()].push_back(&Range);

llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ class VLocTracker {
10461046
/// transfer function for this block, as part of the dataflow analysis. The
10471047
/// movement of values between locations inside of a block is handled at a
10481048
/// much later stage, in the TransferTracker class.
1049-
MapVector<DebugVariableID, DbgValue> Vars;
1049+
SmallMapVector<DebugVariableID, DbgValue, 8> Vars;
10501050
SmallDenseMap<DebugVariableID, const DILocation *, 8> Scopes;
10511051
MachineBasicBlock *MBB = nullptr;
10521052
const OverlapMap &OverlappingFragments;
@@ -1128,7 +1128,7 @@ class InstrRefBasedLDV : public LDVImpl {
11281128

11291129
/// Live in/out structure for the variable values: a per-block map of
11301130
/// variables to their values.
1131-
using LiveIdxT = DenseMap<const MachineBasicBlock *, DbgValue *>;
1131+
using LiveIdxT = SmallDenseMap<const MachineBasicBlock *, DbgValue *, 16>;
11321132

11331133
using VarAndLoc = std::pair<DebugVariableID, DbgValue>;
11341134

llvm/lib/CodeGen/ScheduleDAGInstrs.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,8 @@ void ScheduleDAGInstrs::initSUnits() {
621621
}
622622
}
623623

624-
class ScheduleDAGInstrs::Value2SUsMap : public MapVector<ValueType, SUList> {
624+
class ScheduleDAGInstrs::Value2SUsMap
625+
: public SmallMapVector<ValueType, SUList, 4> {
625626
/// Current total number of SUs in map.
626627
unsigned NumNodes = 0;
627628

@@ -656,7 +657,7 @@ class ScheduleDAGInstrs::Value2SUsMap : public MapVector<ValueType, SUList> {
656657

657658
/// Clears map from all contents.
658659
void clear() {
659-
MapVector<ValueType, SUList>::clear();
660+
SmallMapVector<ValueType, SUList, 4>::clear();
660661
NumNodes = 0;
661662
}
662663

llvm/lib/CodeGen/SelectionDAG/ScheduleDAGRRList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class ScheduleDAGRRList : public ScheduleDAGSDNodes {
183183

184184
// Hack to keep track of the inverse of FindCallSeqStart without more crazy
185185
// DAG crawling.
186-
DenseMap<SUnit*, SUnit*> CallSeqEndForStart;
186+
SmallDenseMap<SUnit *, SUnit *, 16> CallSeqEndForStart;
187187

188188
public:
189189
ScheduleDAGRRList(MachineFunction &mf, bool needlatency,

llvm/lib/IR/Instructions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1584,7 +1584,7 @@ bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
15841584

15851585
bool GetElementPtrInst::collectOffset(
15861586
const DataLayout &DL, unsigned BitWidth,
1587-
MapVector<Value *, APInt> &VariableOffsets,
1587+
SmallMapVector<Value *, APInt, 4> &VariableOffsets,
15881588
APInt &ConstantOffset) const {
15891589
// Delegate to the generic GEPOperator implementation.
15901590
return cast<GEPOperator>(this)->collectOffset(DL, BitWidth, VariableOffsets,

llvm/lib/IR/Operator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ bool GEPOperator::accumulateConstantOffset(
201201

202202
bool GEPOperator::collectOffset(
203203
const DataLayout &DL, unsigned BitWidth,
204-
MapVector<Value *, APInt> &VariableOffsets,
204+
SmallMapVector<Value *, APInt, 4> &VariableOffsets,
205205
APInt &ConstantOffset) const {
206206
assert(BitWidth == DL.getIndexSizeInBits(getPointerAddressSpace()) &&
207207
"The offset bit width does not match DL specification.");

llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ static Value *GEPToVectorIndex(GetElementPtrInst *GEP, AllocaInst *Alloca,
402402
// TODO: Extracting a "multiple of X" from a GEP might be a useful generic
403403
// helper.
404404
unsigned BW = DL.getIndexTypeSizeInBits(GEP->getType());
405-
MapVector<Value *, APInt> VarOffsets;
405+
SmallMapVector<Value *, APInt, 4> VarOffsets;
406406
APInt ConstOffset(BW, 0);
407407
if (GEP->getPointerOperand()->stripPointerCasts() != Alloca ||
408408
!GEP->collectOffset(DL, BW, VarOffsets, ConstOffset))

llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ getStrideAndModOffsetOfGEP(Value *PtrOp, const DataLayout &DL) {
843843
// Return a minimum gep stride, greatest common divisor of consective gep
844844
// index scales(c.f. Bézout's identity).
845845
while (auto *GEP = dyn_cast<GEPOperator>(PtrOp)) {
846-
MapVector<Value *, APInt> VarOffsets;
846+
SmallMapVector<Value *, APInt, 4> VarOffsets;
847847
if (!GEP->collectOffset(DL, BW, VarOffsets, ModOffset))
848848
break;
849849

0 commit comments

Comments
 (0)