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

llvm/lib/Transforms/IPO/AttributorAttributes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,7 @@ bool AAPointerInfoFloating::collectConstantsForGEP(Attributor &A,
15571557
const OffsetInfo &PtrOI,
15581558
const GEPOperator *GEP) {
15591559
unsigned BitWidth = DL.getIndexTypeSizeInBits(GEP->getType());
1560-
MapVector<Value *, APInt> VariableOffsets;
1560+
SmallMapVector<Value *, APInt, 4> VariableOffsets;
15611561
APInt ConstantOffset(BitWidth, 0);
15621562

15631563
assert(!UsrOI.isUnknown() && !PtrOI.isUnknown() &&

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ struct Decomposition {
385385
struct OffsetResult {
386386
Value *BasePtr;
387387
APInt ConstantOffset;
388-
MapVector<Value *, APInt> VariableOffsets;
388+
SmallMapVector<Value *, APInt, 4> VariableOffsets;
389389
bool AllInbounds;
390390

391391
OffsetResult() : BasePtr(nullptr), ConstantOffset(0, uint64_t(0)) {}
@@ -410,7 +410,7 @@ static OffsetResult collectOffsets(GEPOperator &GEP, const DataLayout &DL) {
410410
// If we have a nested GEP, check if we can combine the constant offset of the
411411
// inner GEP with the outer GEP.
412412
if (auto *InnerGEP = dyn_cast<GetElementPtrInst>(Result.BasePtr)) {
413-
MapVector<Value *, APInt> VariableOffsets2;
413+
SmallMapVector<Value *, APInt, 4> VariableOffsets2;
414414
APInt ConstantOffset2(BitWidth, 0);
415415
bool CanCollectInner = InnerGEP->collectOffset(
416416
DL, BitWidth, VariableOffsets2, ConstantOffset2);

llvm/lib/Transforms/Scalar/GVN.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ GVNPass::Expression GVNPass::ValueTable::createGEPExpr(GetElementPtrInst *GEP) {
422422
Type *PtrTy = GEP->getType()->getScalarType();
423423
const DataLayout &DL = GEP->getDataLayout();
424424
unsigned BitWidth = DL.getIndexTypeSizeInBits(PtrTy);
425-
MapVector<Value *, APInt> VariableOffsets;
425+
SmallMapVector<Value *, APInt, 4> VariableOffsets;
426426
APInt ConstantOffset(BitWidth, 0);
427427
if (GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset)) {
428428
// Convert into offset representation, to recognize equivalent address

llvm/lib/Transforms/Scalar/JumpTableToSwitch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static std::optional<JumpTableTy> parseJumpTable(GetElementPtrInst *GEP,
5656
const DataLayout &DL = F.getDataLayout();
5757
const unsigned BitWidth =
5858
DL.getIndexSizeInBits(GEP->getPointerAddressSpace());
59-
MapVector<Value *, APInt> VariableOffsets;
59+
SmallMapVector<Value *, APInt, 4> VariableOffsets;
6060
APInt ConstantOffset(BitWidth, 0);
6161
if (!GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset))
6262
return std::nullopt;

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ,
925925
}
926926

927927
using PredBlockVector = SmallVector<BasicBlock *, 16>;
928-
using IncomingValueMap = DenseMap<BasicBlock *, Value *>;
928+
using IncomingValueMap = SmallDenseMap<BasicBlock *, Value *, 16>;
929929

930930
/// Determines the value to use as the phi node input for a block.
931931
///
@@ -2467,7 +2467,7 @@ Value *getSalvageOpsForGEP(GetElementPtrInst *GEP, const DataLayout &DL,
24672467
SmallVectorImpl<Value *> &AdditionalValues) {
24682468
unsigned BitWidth = DL.getIndexSizeInBits(GEP->getPointerAddressSpace());
24692469
// Rewrite a GEP into a DIExpression.
2470-
MapVector<Value *, APInt> VariableOffsets;
2470+
SmallMapVector<Value *, APInt, 4> VariableOffsets;
24712471
APInt ConstantOffset(BitWidth, 0);
24722472
if (!GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset))
24732473
return nullptr;

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5122,7 +5122,7 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
51225122
// Each 'key' in the map opens a new interval. The values
51235123
// of the map are the index of the 'last seen' usage of the
51245124
// instruction that is the key.
5125-
using IntervalMap = DenseMap<Instruction *, unsigned>;
5125+
using IntervalMap = SmallDenseMap<Instruction *, unsigned, 16>;
51265126

51275127
// Maps instruction to its index.
51285128
SmallVector<Instruction *, 64> IdxToInstr;
@@ -5165,7 +5165,7 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
51655165

51665166
// Saves the list of intervals that end with the index in 'key'.
51675167
using InstrList = SmallVector<Instruction *, 2>;
5168-
DenseMap<unsigned, InstrList> TransposeEnds;
5168+
SmallDenseMap<unsigned, InstrList, 16> TransposeEnds;
51695169

51705170
// Transpose the EndPoints to a list of values that end at each index.
51715171
for (auto &Interval : EndPoint)

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5470,7 +5470,7 @@ BoUpSLP::getReorderingData(const TreeEntry &TE, bool TopToBottom) {
54705470
}
54715471
return I1 < I2;
54725472
};
5473-
DenseMap<unsigned, unsigned> PhiToId;
5473+
SmallDenseMap<unsigned, unsigned, 16> PhiToId;
54745474
SmallVector<unsigned> Phis(TE.Scalars.size());
54755475
std::iota(Phis.begin(), Phis.end(), 0);
54765476
OrdersType ResOrder(TE.Scalars.size());
@@ -10319,7 +10319,7 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
1031910319
E->isAltShuffle() ? (unsigned)Instruction::ShuffleVector : E->getOpcode();
1032010320
if (E->CombinedOp != TreeEntry::NotCombinedOp)
1032110321
ShuffleOrOp = E->CombinedOp;
10322-
SetVector<Value *> UniqueValues(VL.begin(), VL.end());
10322+
SmallSetVector<Value *, 16> UniqueValues(VL.begin(), VL.end());
1032310323
const unsigned Sz = UniqueValues.size();
1032410324
SmallBitVector UsedScalars(Sz, false);
1032510325
for (unsigned I = 0; I < Sz; ++I) {
@@ -18013,7 +18013,7 @@ class HorizontalReduction {
1801318013
/// List of possibly reduced values.
1801418014
SmallVector<SmallVector<Value *>> ReducedVals;
1801518015
/// Maps reduced value to the corresponding reduction operation.
18016-
DenseMap<Value *, SmallVector<Instruction *>> ReducedValsToOps;
18016+
SmallDenseMap<Value *, SmallVector<Instruction *>, 16> ReducedValsToOps;
1801718017
WeakTrackingVH ReductionRoot;
1801818018
/// The type of reduction operation.
1801918019
RecurKind RdxKind;
@@ -18382,7 +18382,9 @@ class HorizontalReduction {
1838218382
// instruction op id and/or alternate op id, plus do extra analysis for
1838318383
// loads (grouping them by the distabce between pointers) and cmp
1838418384
// instructions (grouping them by the predicate).
18385-
MapVector<size_t, MapVector<size_t, MapVector<Value *, unsigned>>>
18385+
SmallMapVector<
18386+
size_t, SmallMapVector<size_t, SmallMapVector<Value *, unsigned, 2>, 2>,
18387+
8>
1838618388
PossibleReducedVals;
1838718389
initReductionOps(Root);
1838818390
DenseMap<Value *, SmallVector<LoadInst *>> LoadsMap;

0 commit comments

Comments
 (0)