Skip to content

Commit ac3fedf

Browse files
michaelmaitlandIcohedron
authored andcommitted
[ReachingDefAnalysis] Fix management of MBBFrameObjsReachingDefs (llvm#124943)
MBBFrameObjsReachingDefs was not being built correctly since we were not inserting into a reference of Frame2InstrIdx. If there was multiple stack slot defs in the same basic block, then the bug would occur. This PR fixes this problem while simplifying the insertion logic. Additionally, when lookup into MBBFrameObjsReachingDefs was occurring, there was a chance that there was no entry in the map, in the case that there was no reaching def. This was causing us to return a default value, which may or may not have been correct. This patch returns the correct value now.
1 parent 41c6931 commit ac3fedf

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

llvm/include/llvm/CodeGen/ReachingDefAnalysis.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,12 @@ class ReachingDefAnalysis : public MachineFunctionPass {
141141
DenseMap<MachineInstr *, int> InstIds;
142142

143143
MBBReachingDefsInfo MBBReachingDefs;
144+
145+
/// MBBFrameObjsReachingDefs[{i, j}] is a list of instruction indices
146+
/// (relative to begining of MBB i) that define frame index j in MBB i. This
147+
/// is used in answering reaching definition queries.
144148
using MBBFrameObjsReachingDefsInfo =
145-
DenseMap<unsigned, DenseMap<int, SmallVector<int>>>;
146-
// MBBFrameObjsReachingDefs[i][j] is a list of instruction indices (relative
147-
// to begining of MBB) that define frame index (j +
148-
// MF->getFrameInfo().getObjectIndexBegin()) in MBB i. This is used in
149-
// answering reaching definition queries.
149+
DenseMap<std::pair<unsigned, int>, SmallVector<int>>;
150150
MBBFrameObjsReachingDefsInfo MBBFrameObjsReachingDefs;
151151

152152
/// Default values are 'nothing happened a long time ago'.

llvm/lib/CodeGen/ReachingDefAnalysis.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,7 @@ void ReachingDefAnalysis::processDefs(MachineInstr *MI) {
147147
assert(FrameIndex >= 0 && "Can't handle negative frame indicies yet!");
148148
if (!isFIDef(*MI, FrameIndex, TII))
149149
continue;
150-
if (MBBFrameObjsReachingDefs.contains(MBBNumber)) {
151-
auto Frame2InstrIdx = MBBFrameObjsReachingDefs[MBBNumber];
152-
if (Frame2InstrIdx.count(FrameIndex - ObjectIndexBegin) > 0)
153-
Frame2InstrIdx[FrameIndex - ObjectIndexBegin].push_back(CurInstr);
154-
else
155-
Frame2InstrIdx[FrameIndex - ObjectIndexBegin] = {CurInstr};
156-
} else {
157-
MBBFrameObjsReachingDefs[MBBNumber] = {
158-
{FrameIndex - ObjectIndexBegin, {CurInstr}}};
159-
}
150+
MBBFrameObjsReachingDefs[{MBBNumber, FrameIndex}].push_back(CurInstr);
160151
}
161152
if (!isValidRegDef(MO))
162153
continue;
@@ -351,9 +342,13 @@ int ReachingDefAnalysis::getReachingDef(MachineInstr *MI, Register Reg) const {
351342
int LatestDef = ReachingDefDefaultVal;
352343

353344
if (Reg.isStack()) {
345+
// Check that there was a reaching def.
354346
int FrameIndex = Reg.stackSlotIndex();
355-
for (int Def : MBBFrameObjsReachingDefs.lookup(MBBNumber).lookup(
356-
FrameIndex - ObjectIndexBegin)) {
347+
auto Lookup = MBBFrameObjsReachingDefs.find({MBBNumber, FrameIndex});
348+
if (Lookup == MBBFrameObjsReachingDefs.end())
349+
return LatestDef;
350+
auto &Defs = Lookup->second;
351+
for (int Def : Defs) {
357352
if (Def >= InstId)
358353
break;
359354
DefRes = Def;

llvm/test/CodeGen/RISCV/rda-stack.mir

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,43 @@ body: |
149149
$x10 = LD %stack.0, 0 :: (load (s64))
150150
PseudoRET implicit $x10
151151
...
152+
---
153+
name: test4
154+
tracksRegLiveness: true
155+
fixedStack:
156+
- { id: 0, type: default, offset: 0, size: 4, alignment: 16,
157+
isImmutable: true, isAliased: false }
158+
stack:
159+
- { id: 0, name: '', type: default, offset: 0, size: 4, alignment: 4,
160+
stack-id: default, callee-saved-register: '', callee-saved-restored: true,
161+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
162+
- { id: 1, name: '', type: default, offset: 0, size: 4, alignment: 4,
163+
stack-id: default, callee-saved-register: '', callee-saved-restored: true,
164+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
165+
body: |
166+
; CHECK: RDA results for test4
167+
; CHECK-NEXT: $x10:{ }
168+
; CHECK-NEXT: %stack.0:{ }
169+
; CHECK-NEXT: 0: SD $x10, %stack.0, 0 :: (store (s64))
170+
; CHECK-EMPTY:
171+
; CHECK-NEXT: $x11:{ }
172+
; CHECK-NEXT: %stack.0:{ 0 }
173+
; CHECK-NEXT: 1: SD $x11, %stack.0, 0 :: (store (s64))
174+
; CHECK-EMPTY:
175+
; CHECK-NEXT: $x10:{ }
176+
; CHECK-NEXT: %stack.1:{ }
177+
; CHECK-NEXT: 2: SD $x10, %stack.1, 0 :: (store (s64))
178+
; CHECK-EMPTY:
179+
; CHECK-NEXT: $x11:{ }
180+
; CHECK-NEXT: %stack.1:{ 2 }
181+
; CHECK-NEXT: 3: SD $x11, %stack.1, 0 :: (store (s64))
182+
; CHECK-EMPTY:
183+
; CHECK-NEXT: 4: PseudoRET
184+
bb.0.entry:
185+
liveins: $x10, $x11
186+
SD $x10, %stack.0, 0 :: (store (s64))
187+
SD $x11, %stack.0, 0 :: (store (s64))
188+
SD $x10, %stack.1, 0 :: (store (s64))
189+
SD $x11, %stack.1, 0 :: (store (s64))
190+
PseudoRET
191+
...

0 commit comments

Comments
 (0)