Skip to content

Commit fb19fd0

Browse files
committed
Add new StackColoring algo
1 parent eef9b6e commit fb19fd0

File tree

4 files changed

+530
-125
lines changed

4 files changed

+530
-125
lines changed

llvm/include/llvm/CodeGen/MachineFrameInfo.h

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,18 @@ class MachineFrameInfo {
120120
///< triggered protection. 3rd closest to the protector.
121121
};
122122

123+
static constexpr int NoUnderlyingSlot = std::numeric_limits<int>::min();
124+
static constexpr int IsUnderlyingSlot = std::numeric_limits<int>::min() + 1;
125+
123126
private:
124127
// Represent a single object allocated on the stack.
125128
struct StackObject {
126129
// The offset of this object from the stack pointer on entry to
127130
// the function. This field has no meaning for a variable sized element.
128-
int64_t SPOffset;
131+
// After getting placed this is relative to SP
132+
// If UnderlyingSlot is not NoUnderlyingSlot, this is relative to the start
133+
// of the UnderlyingSlot
134+
int64_t Offset;
129135

130136
// The size of this object on the stack. 0 means a variable sized object,
131137
// ~0ULL means a dead object.
@@ -134,6 +140,10 @@ class MachineFrameInfo {
134140
// The required alignment of this stack slot.
135141
Align Alignment;
136142

143+
// If not NoUnderlyingSlot, it Indicate that this slot should be placed
144+
// at Offset, into the slot UnderlyingSlot
145+
int UnderlyingSlot = NoUnderlyingSlot;
146+
137147
// If true, the value of the stack object is set before
138148
// entering the function and is not modified inside the function. By
139149
// default, fixed objects are immutable unless marked otherwise.
@@ -183,10 +193,10 @@ class MachineFrameInfo {
183193

184194
uint8_t SSPLayout = SSPLK_None;
185195

186-
StackObject(uint64_t Size, Align Alignment, int64_t SPOffset,
196+
StackObject(uint64_t Size, Align Alignment, int64_t Offset,
187197
bool IsImmutable, bool IsSpillSlot, const AllocaInst *Alloca,
188198
bool IsAliased, uint8_t StackID = 0)
189-
: SPOffset(SPOffset), Size(Size), Alignment(Alignment),
199+
: Offset(Offset), Size(Size), Alignment(Alignment),
190200
isImmutable(IsImmutable), isSpillSlot(IsSpillSlot), StackID(StackID),
191201
Alloca(Alloca), isAliased(IsAliased) {}
192202
};
@@ -532,7 +542,7 @@ class MachineFrameInfo {
532542
"Invalid Object Idx!");
533543
assert(!isDeadObjectIndex(ObjectIdx) &&
534544
"Getting frame offset for a dead object?");
535-
return Objects[ObjectIdx+NumFixedObjects].SPOffset;
545+
return Objects[ObjectIdx + NumFixedObjects].Offset;
536546
}
537547

538548
bool isObjectZExt(int ObjectIdx) const {
@@ -561,12 +571,12 @@ class MachineFrameInfo {
561571

562572
/// Set the stack frame offset of the specified object. The
563573
/// offset is relative to the stack pointer on entry to the function.
564-
void setObjectOffset(int ObjectIdx, int64_t SPOffset) {
574+
void setObjectOffset(int ObjectIdx, int64_t Offset) {
565575
assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
566576
"Invalid Object Idx!");
567577
assert(!isDeadObjectIndex(ObjectIdx) &&
568578
"Setting frame offset for a dead object?");
569-
Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
579+
Objects[ObjectIdx + NumFixedObjects].Offset = Offset;
570580
}
571581

572582
SSPLayoutKind getObjectSSPLayout(int ObjectIdx) const {
@@ -762,6 +772,18 @@ class MachineFrameInfo {
762772
// If ID == 0, MaxAlignment will need to be updated separately.
763773
}
764774

775+
int getUnderlyingSlot(int ObjectIdx) {
776+
assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
777+
"Invalid Object Idx!");
778+
return Objects[ObjectIdx + NumFixedObjects].UnderlyingSlot;
779+
}
780+
781+
void setUnderlyingSlot(int ObjectIdx, int Underlying) {
782+
assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
783+
"Invalid Object Idx!");
784+
Objects[ObjectIdx + NumFixedObjects].UnderlyingSlot = Underlying;
785+
}
786+
765787
/// Returns true if the specified index corresponds to a dead object.
766788
bool isDeadObjectIndex(int ObjectIdx) const {
767789
assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&

llvm/lib/CodeGen/MachineFrameInfo.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ int MachineFrameInfo::CreateVariableSizedObject(Align Alignment,
8181
return (int)Objects.size()-NumFixedObjects-1;
8282
}
8383

84-
int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
84+
int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t Offset,
8585
bool IsImmutable, bool IsAliased) {
8686
assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
8787
// The alignment of the frame index can be determined from its offset from
@@ -91,23 +91,22 @@ int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
9191
// stack needs realignment, we can't assume that the stack will in fact be
9292
// aligned.
9393
Align Alignment =
94-
commonAlignment(ForcedRealign ? Align(1) : StackAlignment, SPOffset);
94+
commonAlignment(ForcedRealign ? Align(1) : StackAlignment, Offset);
9595
Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
9696
Objects.insert(Objects.begin(),
97-
StackObject(Size, Alignment, SPOffset, IsImmutable,
97+
StackObject(Size, Alignment, Offset, IsImmutable,
9898
/*IsSpillSlot=*/false, /*Alloca=*/nullptr,
9999
IsAliased));
100100
return -++NumFixedObjects;
101101
}
102102

103-
int MachineFrameInfo::CreateFixedSpillStackObject(uint64_t Size,
104-
int64_t SPOffset,
103+
int MachineFrameInfo::CreateFixedSpillStackObject(uint64_t Size, int64_t Offset,
105104
bool IsImmutable) {
106105
Align Alignment =
107-
commonAlignment(ForcedRealign ? Align(1) : StackAlignment, SPOffset);
106+
commonAlignment(ForcedRealign ? Align(1) : StackAlignment, Offset);
108107
Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
109108
Objects.insert(Objects.begin(),
110-
StackObject(Size, Alignment, SPOffset, IsImmutable,
109+
StackObject(Size, Alignment, Offset, IsImmutable,
111110
/*IsSpillSlot=*/true, /*Alloca=*/nullptr,
112111
/*IsAliased=*/false));
113112
return -++NumFixedObjects;
@@ -240,8 +239,13 @@ void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
240239

241240
if (i < NumFixedObjects)
242241
OS << ", fixed";
243-
if (i < NumFixedObjects || SO.SPOffset != -1) {
244-
int64_t Off = SO.SPOffset - ValOffset;
242+
if (SO.UnderlyingSlot == MachineFrameInfo::IsUnderlyingSlot)
243+
OS << ", underlying";
244+
if (SO.UnderlyingSlot > MachineFrameInfo::IsUnderlyingSlot) {
245+
OS << ", placed=" << "fi#" << (int)(SO.UnderlyingSlot - NumFixedObjects)
246+
<< "+" << SO.Offset;
247+
} else if (i < NumFixedObjects || SO.Offset != -1) {
248+
int64_t Off = SO.Offset - ValOffset;
245249
OS << ", at location [SP";
246250
if (Off > 0)
247251
OS << "+" << Off;

llvm/lib/CodeGen/PrologEpilogInserter.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,13 @@ void PEIImpl::spillCalleeSavedRegs(MachineFunction &MF) {
694694
}
695695
}
696696

697+
static inline void UpdateOffset(MachineFrameInfo &MFI, int FrameIdx,
698+
int64_t Offset) {
699+
LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset
700+
<< "]\n");
701+
MFI.setObjectOffset(FrameIdx, Offset); // Set the computed offset
702+
}
703+
697704
/// AdjustStackOffset - Helper function used to adjust the stack frame offset.
698705
static inline void AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
699706
bool StackGrowsDown, int64_t &Offset,
@@ -712,13 +719,9 @@ static inline void AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
712719
Offset = alignTo(Offset, Alignment);
713720

714721
if (StackGrowsDown) {
715-
LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset
716-
<< "]\n");
717-
MFI.setObjectOffset(FrameIdx, -Offset); // Set the computed offset
722+
UpdateOffset(MFI, FrameIdx, -Offset);
718723
} else {
719-
LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset
720-
<< "]\n");
721-
MFI.setObjectOffset(FrameIdx, Offset);
724+
UpdateOffset(MFI, FrameIdx, Offset);
722725
Offset += MFI.getObjectSize(FrameIdx);
723726
}
724727
}
@@ -1044,6 +1047,7 @@ void PEIImpl::calculateFrameObjectOffsets(MachineFunction &MF) {
10441047
}
10451048

10461049
SmallVector<int, 8> ObjectsToAllocate;
1050+
SmallVector<int, 8> UpdateOffsetAfterAllocate;
10471051

10481052
// Then prepare to assign frame offsets to stack objects that are not used to
10491053
// spill callee saved registers.
@@ -1064,6 +1068,11 @@ void PEIImpl::calculateFrameObjectOffsets(MachineFunction &MF) {
10641068
if (MFI.getStackID(i) != TargetStackID::Default)
10651069
continue;
10661070

1071+
if (MFI.getUnderlyingSlot(i) > MachineFrameInfo::IsUnderlyingSlot) {
1072+
UpdateOffsetAfterAllocate.push_back(i);
1073+
continue;
1074+
}
1075+
10671076
// Add the objects that we need to allocate to our working set.
10681077
ObjectsToAllocate.push_back(i);
10691078
}
@@ -1104,6 +1113,14 @@ void PEIImpl::calculateFrameObjectOffsets(MachineFunction &MF) {
11041113
AdjustStackOffset(MFI, SFI, StackGrowsDown, Offset, MaxAlign);
11051114
}
11061115

1116+
for (int FrameIdx : UpdateOffsetAfterAllocate) {
1117+
int UnderlyingSlot = MFI.getUnderlyingSlot(FrameIdx);
1118+
int64_t ObjOffset =
1119+
MFI.getObjectOffset(UnderlyingSlot) + MFI.getObjectOffset(FrameIdx);
1120+
UpdateOffset(MFI, FrameIdx, ObjOffset);
1121+
MFI.setUnderlyingSlot(FrameIdx, MachineFrameInfo::NoUnderlyingSlot);
1122+
}
1123+
11071124
if (!TFI.targetHandlesStackFrameRounding()) {
11081125
// If we have reserved argument space for call sites in the function
11091126
// immediately on entry to the current function, count it as part of the

0 commit comments

Comments
 (0)