Skip to content

Commit c7c5da6

Browse files
committed
Reland "[StackColoring] Remap PseudoSourceValue frame indices via MachineFunction::getPSVManager()""
Reland 7a8b0b1, with a fix that checks `!E.value().empty()` to avoid inserting a zero to SlotRemap. Debugged by rnk@ in https://bugs.chromium.org/p/chromium/issues/detail?id=1045650#c33 Reviewed By: rnk Differential Revision: https://reviews.llvm.org/D73510
1 parent 7c90666 commit c7c5da6

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-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: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,8 @@ 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(
964+
MFI->getObjectIndexEnd());
963965
for (MachineBasicBlock &BB : *MF)
964966
for (MachineInstr &I : BB) {
965967
// Skip lifetime markers. We'll remove them soon.
@@ -1025,13 +1027,14 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
10251027
SmallVector<MachineMemOperand *, 2> NewMMOs;
10261028
bool ReplaceMemOps = false;
10271029
for (MachineMemOperand *MMO : I.memoperands()) {
1030+
// Collect MachineMemOperands which reference
1031+
// FixedStackPseudoSourceValues with old frame indices.
10281032
if (const auto *FSV = dyn_cast_or_null<FixedStackPseudoSourceValue>(
10291033
MMO->getPseudoValue())) {
10301034
int FI = FSV->getFrameIndex();
10311035
auto To = SlotRemap.find(FI);
10321036
if (To != SlotRemap.end())
1033-
const_cast<FixedStackPseudoSourceValue *>(FSV)->setFrameIndex(
1034-
To->second);
1037+
SSRefs[FI].push_back(MMO);
10351038
}
10361039

10371040
// If this memory location can be a slot remapped here,
@@ -1071,6 +1074,15 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
10711074
I.setMemRefs(*MF, NewMMOs);
10721075
}
10731076

1077+
// Rewrite MachineMemOperands that reference old frame indices.
1078+
for (auto E : enumerate(SSRefs))
1079+
if (!E.value().empty()) {
1080+
const PseudoSourceValue *NewSV =
1081+
MF->getPSVManager().getFixedStack(SlotRemap.find(E.index())->second);
1082+
for (MachineMemOperand *Ref : E.value())
1083+
Ref->setValue(NewSV);
1084+
}
1085+
10741086
// Update the location of C++ catch objects for the MSVC personality routine.
10751087
if (WinEHFuncInfo *EHInfo = MF->getWinEHFuncInfo())
10761088
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)