Skip to content

Commit 7a8b0b1

Browse files
committed
[StackColoring] Remap PseudoSourceValue frame indices via MachineFunction::getPSVManager()
Reviewed By: dantrushin Differential Revision: https://reviews.llvm.org/D73063
1 parent 2e667d0 commit 7a8b0b1

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

llvm/include/llvm/CodeGen/PseudoSourceValue.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class PseudoSourceValue {
9393
/// A specialized PseudoSourceValue for holding FixedStack values, which must
9494
/// include a frame index.
9595
class FixedStackPseudoSourceValue : public PseudoSourceValue {
96-
int FI;
96+
const int FI;
9797

9898
public:
9999
explicit FixedStackPseudoSourceValue(int FI, const TargetInstrInfo &TII)
@@ -112,7 +112,6 @@ class FixedStackPseudoSourceValue : public PseudoSourceValue {
112112
void printCustom(raw_ostream &OS) const override;
113113

114114
int getFrameIndex() const { return FI; }
115-
void setFrameIndex(int FI) { this->FI = FI; }
116115
};
117116

118117
class CallEntryPseudoSourceValue : public PseudoSourceValue {

llvm/lib/CodeGen/StackColoring.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,7 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
960960
}
961961

962962
// Remap all instructions to the new stack slots.
963+
std::vector<std::vector<MachineMemOperand *>> SSRefs(MFI->getObjectIndexEnd());
963964
for (MachineBasicBlock &BB : *MF)
964965
for (MachineInstr &I : BB) {
965966
// Skip lifetime markers. We'll remove them soon.
@@ -1025,13 +1026,14 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
10251026
SmallVector<MachineMemOperand *, 2> NewMMOs;
10261027
bool ReplaceMemOps = false;
10271028
for (MachineMemOperand *MMO : I.memoperands()) {
1029+
// Collect MachineMemOperands which reference
1030+
// FixedStackPseudoSourceValues with old frame indices.
10281031
if (const auto *FSV = dyn_cast_or_null<FixedStackPseudoSourceValue>(
10291032
MMO->getPseudoValue())) {
10301033
int FI = FSV->getFrameIndex();
10311034
auto To = SlotRemap.find(FI);
10321035
if (To != SlotRemap.end())
1033-
const_cast<FixedStackPseudoSourceValue *>(FSV)->setFrameIndex(
1034-
To->second);
1036+
SSRefs[FI].push_back(MMO);
10351037
}
10361038

10371039
// If this memory location can be a slot remapped here,
@@ -1071,6 +1073,14 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
10711073
I.setMemRefs(*MF, NewMMOs);
10721074
}
10731075

1076+
// Rewrite MachineMemOperands that reference old frame indices.
1077+
for (auto E : enumerate(SSRefs)) {
1078+
const PseudoSourceValue *NewSV =
1079+
MF->getPSVManager().getFixedStack(SlotRemap[E.index()]);
1080+
for (MachineMemOperand *Ref : E.value())
1081+
Ref->setValue(NewSV);
1082+
}
1083+
10741084
// Update the location of C++ catch objects for the MSVC personality routine.
10751085
if (WinEHFuncInfo *EHInfo = MF->getWinEHFuncInfo())
10761086
for (WinEHTryBlockMapEntry &TBME : EHInfo->TryBlockMap)

llvm/test/CodeGen/PowerPC/stack-coloring-vararg.mir

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
# RUN: llc -o - %s -start-before=stack-coloring
1+
# RUN: llc -run-pass=stack-coloring %s -o - | FileCheck %s
2+
3+
## Test %stack.1 is merged into %stack.0 and there is no MemoryMemOperand
4+
## referencing %stack.1. This regression test is sensitive to the StackColoring
5+
## algorithm. Please adjust or delete this test if the merging strategy
6+
## changes.
7+
8+
# CHECK: {{^}}stack:
9+
# CHECK-NEXT: - { id: 0,
10+
# CHECK-NOT: - { id: 1,
11+
# CHECK: - { id: 2,
12+
# CHECK-NOT: %stack.1
13+
214
--- |
315
; ModuleID = '<stdin>'
416
source_filename = "<stdin>"

0 commit comments

Comments
 (0)