Skip to content

Commit 0a8b5a6

Browse files
committed
Use a SmallVector of fake uses instead of a SmallDenseMap
1 parent fd2c94e commit 0a8b5a6

File tree

2 files changed

+38
-93
lines changed

2 files changed

+38
-93
lines changed

llvm/lib/CodeGen/RemoveLoadsIntoFakeUses.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ bool RemoveLoadsIntoFakeUses::runOnMachineFunction(MachineFunction &MF) {
8686
const TargetInstrInfo *TII = ST.getInstrInfo();
8787
const TargetRegisterInfo *TRI = ST.getRegisterInfo();
8888

89-
SmallDenseMap<MCRegUnit, SmallVector<MachineInstr *>> RegFakeUses;
89+
SmallVector<MachineInstr *> RegFakeUses;
9090
LivePhysRegs.init(*TRI);
9191
SmallVector<MachineInstr *, 16> Statepoints;
9292
for (MachineBasicBlock *MBB : post_order(&MF)) {
@@ -101,8 +101,7 @@ bool RemoveLoadsIntoFakeUses::runOnMachineFunction(MachineFunction &MF) {
101101
// Track the Fake Uses that use these register units so that we can
102102
// delete them if we delete the corresponding load.
103103
if (FakeUseOp.isReg())
104-
for (MCRegUnit Unit : TRI->regunits(FakeUseOp.getReg()))
105-
RegFakeUses[Unit].push_back(&MI);
104+
RegFakeUses.push_back(&MI);
106105
// Do not record FAKE_USE uses in LivePhysRegs so that we can recognize
107106
// otherwise-unused loads.
108107
continue;
@@ -123,12 +122,12 @@ bool RemoveLoadsIntoFakeUses::runOnMachineFunction(MachineFunction &MF) {
123122
// choose to ignore it so that this pass has no side effects unrelated
124123
// to fake uses.
125124
SmallDenseSet<MachineInstr *> FakeUsesToDelete;
126-
for (MCRegUnit Unit : TRI->regunits(Reg)) {
127-
if (!RegFakeUses.contains(Unit))
128-
continue;
129-
for (MachineInstr *FakeUse : RegFakeUses[Unit])
125+
SmallVector<MachineInstr *> RemainingFakeUses;
126+
for (MachineInstr *&FakeUse : reverse(RegFakeUses)) {
127+
if (TRI->regsOverlap(Reg, FakeUse->getOperand(0).getReg())) {
130128
FakeUsesToDelete.insert(FakeUse);
131-
RegFakeUses.erase(Unit);
129+
RegFakeUses.erase(&FakeUse);
130+
}
132131
}
133132
if (!FakeUsesToDelete.empty()) {
134133
LLVM_DEBUG(dbgs() << "RemoveLoadsIntoFakeUses: DELETING: " << MI);
@@ -153,14 +152,15 @@ bool RemoveLoadsIntoFakeUses::runOnMachineFunction(MachineFunction &MF) {
153152
// that register.
154153
if (!RegFakeUses.empty()) {
155154
for (const MachineOperand &MO : MI.operands()) {
156-
if (MO.isReg() && MO.isDef()) {
157-
Register Reg = MO.getReg();
158-
// We clear RegFakeUses for this register and all subregisters,
159-
// because any such FAKE_USE encountered prior applies only to this
160-
// instruction.
161-
for (MCRegUnit Unit : TRI->regunits(Reg))
162-
RegFakeUses.erase(Unit);
163-
}
155+
if (!MO.isReg())
156+
continue;
157+
Register Reg = MO.getReg();
158+
// We clear RegFakeUses for this register and all subregisters,
159+
// because any such FAKE_USE encountered prior is no longer relevant
160+
// for later encountered loads.
161+
for (MachineInstr *&FakeUse : reverse(RegFakeUses))
162+
if (!TRI->regsOverlap(Reg, FakeUse->getOperand(0).getReg()))
163+
RegFakeUses.erase(&FakeUse);
164164
}
165165
}
166166
LivePhysRegs.stepBackward(MI);

llvm/test/CodeGen/X86/fake-use-remove-loads.mir

Lines changed: 22 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,17 @@
1313

1414
## Also verify that the store to the stack slot still exists.
1515

16-
# CHECK-LABEL: bb.5:
17-
# CHECK: MOV64mi32 $rbp, 1, $noreg, -48, $noreg, 0 :: (store (s64) into %stack.0)
18-
# CHECK-LABEL: bb.6:
16+
# CHECK-LABEL: bb.0:
17+
# CHECK: MOV64mr $rbp, 1, $noreg, -48, $noreg, killed renamable $rax :: (store (s64) into %stack.0)
1918

19+
## Finally, verify that when the register has a use between the restore and the
20+
## FAKE_USE, we do not delete the load or fake use.
21+
22+
# CHECK: renamable $r11 = MOV64rm $rbp, 1, $noreg, -40, $noreg :: (load (s64) from %stack.1)
23+
# CHECK: renamable $r12d = XOR32rr $r12d, $r11d, implicit-def dead $eflags
24+
# CHECK: FAKE_USE killed renamable $r11d
2025

21-
--- |
22-
define void @_ZN1g1jEv(ptr %this, i1 %cmp36, ptr %ref.tmp5) {
23-
entry:
24-
ret void
25-
}
2626

27-
...
2827
---
2928
name: _ZN1g1jEv
3029
alignment: 16
@@ -40,90 +39,36 @@ liveins:
4039
frameInfo:
4140
isCalleeSavedInfoValid: true
4241
stack:
43-
- { id: 0, name: '', type: spill-slot, offset: -64, size: 8, alignment: 8,
42+
- { id: 0, name: '', type: spill-slot, offset: -8, size: 8, alignment: 8,
43+
stack-id: default, callee-saved-register: '', callee-saved-restored: true,
44+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
45+
- { id: 1, name: '', type: spill-slot, offset: -16, size: 8, alignment: 8,
4446
stack-id: default, callee-saved-register: '', callee-saved-restored: true,
4547
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
4648
body: |
4749
bb.0:
48-
successors: %bb.2(0x80000000)
49-
liveins: $esi, $rdi, $rdx, $r15, $r14, $r13, $r12, $rbx
50-
51-
frame-setup PUSH64r killed $rbp, implicit-def $rsp, implicit $rsp
52-
frame-setup CFI_INSTRUCTION def_cfa_offset 16
53-
frame-setup CFI_INSTRUCTION offset $rbp, -16
54-
$rbp = frame-setup MOV64rr $rsp
55-
frame-setup CFI_INSTRUCTION def_cfa_register $rbp
56-
frame-setup PUSH64r killed $r15, implicit-def $rsp, implicit $rsp
57-
frame-setup PUSH64r killed $r14, implicit-def $rsp, implicit $rsp
58-
frame-setup PUSH64r killed $r13, implicit-def $rsp, implicit $rsp
59-
frame-setup PUSH64r killed $r12, implicit-def $rsp, implicit $rsp
60-
frame-setup PUSH64r killed $rbx, implicit-def $rsp, implicit $rsp
61-
frame-setup PUSH64r undef $rax, implicit-def $rsp, implicit $rsp
62-
CFI_INSTRUCTION offset $rbx, -56
63-
CFI_INSTRUCTION offset $r12, -48
64-
CFI_INSTRUCTION offset $r13, -40
65-
CFI_INSTRUCTION offset $r14, -32
66-
CFI_INSTRUCTION offset $r15, -24
50+
liveins: $esi, $rdi, $rdx, $r15, $r14, $r13, $r12, $r11, $rbx
51+
6752
$rbx = MOV64rr $rdx
6853
$r14d = MOV32rr $esi
6954
$r15 = MOV64rr $rdi
7055
renamable $r12d = XOR32rr undef $r12d, undef $r12d, implicit-def dead $eflags, implicit-def $r12
7156
renamable $eax = XOR32rr undef $eax, undef $eax, implicit-def dead $eflags, implicit-def $rax
72-
JMP_1 %bb.2
73-
74-
bb.1:
75-
successors: %bb.2(0x783e0f0f), %bb.6(0x07c1f0f1)
76-
liveins: $rbx, $r12, $r15, $r13d, $r14d
77-
78-
renamable $rax = MOV64rm $rbp, 1, $noreg, -48, $noreg :: (load (s64) from %stack.0)
79-
FAKE_USE renamable $eax, implicit killed $rax
80-
renamable $eax = MOV32ri 1, implicit-def $rax
81-
TEST32rr killed renamable $r13d, renamable $r13d, implicit-def $eflags
82-
JCC_1 %bb.6, 9, implicit $eflags
83-
84-
bb.2:
85-
successors: %bb.3(0x80000000)
86-
liveins: $rax, $rbx, $r12, $r15, $r14d
87-
8857
MOV64mr $rbp, 1, $noreg, -48, $noreg, killed renamable $rax :: (store (s64) into %stack.0)
58+
MOV64mr $rbp, 1, $noreg, -40, $noreg, killed renamable $r11 :: (store (s64) into %stack.1)
8959
renamable $eax = XOR32rr undef $eax, undef $eax, implicit-def dead $eflags
90-
91-
bb.3:
92-
successors: %bb.4(0x04000000), %bb.3(0x7c000000)
93-
liveins: $eax, $rbx, $r12, $r15, $r14d
94-
9560
$r13d = MOV32rr killed $eax
9661
$rdi = MOV64rr $r15
9762
CALL64r renamable $r12, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, implicit-def $rsp, implicit-def $ssp
98-
dead renamable $eax = MOV32rm renamable $rbx, 1, $noreg, 0, $noreg :: (volatile load (s32) from %ir.ref.tmp5)
63+
dead renamable $eax = MOV32rm renamable $rbx, 1, $noreg, 0, $noreg
9964
renamable $eax = MOV32ri 1
10065
TEST8ri renamable $r14b, 1, implicit-def $eflags
101-
JCC_1 %bb.3, 4, implicit $eflags
102-
103-
bb.4:
104-
successors: %bb.5(0x40000000), %bb.1(0x40000000)
105-
liveins: $eflags, $rbx, $r12, $r15, $r13d, $r14d
106-
107-
JCC_1 %bb.1, 4, implicit $eflags
108-
109-
bb.5:
110-
successors: %bb.1(0x80000000)
111-
liveins: $rbx, $r12, $r15, $r13d, $r14d
112-
113-
renamable $eax = XOR32rr undef $eax, undef $eax, implicit-def dead $eflags, implicit-def $rax
114-
MOV64mi32 $rbp, 1, $noreg, -48, $noreg, 0 :: (store (s64) into %stack.0)
115-
CALL64r killed renamable $rax, csr_64, implicit $rsp, implicit $ssp, implicit-def $rsp, implicit-def $ssp, implicit-def dead $eax
116-
JMP_1 %bb.1
117-
118-
bb.6:
119-
$rsp = frame-destroy ADD64ri32 $rsp, 8, implicit-def dead $eflags
120-
$rbx = frame-destroy POP64r implicit-def $rsp, implicit $rsp
121-
$r12 = frame-destroy POP64r implicit-def $rsp, implicit $rsp
122-
$r13 = frame-destroy POP64r implicit-def $rsp, implicit $rsp
123-
$r14 = frame-destroy POP64r implicit-def $rsp, implicit $rsp
124-
$r15 = frame-destroy POP64r implicit-def $rsp, implicit $rsp
125-
$rbp = frame-destroy POP64r implicit-def $rsp, implicit $rsp
126-
frame-destroy CFI_INSTRUCTION def_cfa $rsp, 8
66+
renamable $rax = MOV64rm $rbp, 1, $noreg, -48, $noreg :: (load (s64) from %stack.0)
67+
FAKE_USE renamable $eax, implicit killed $rax
68+
renamable $r11 = MOV64rm $rbp, 1, $noreg, -40, $noreg :: (load (s64) from %stack.1)
69+
renamable $r12d = XOR32rr $r12d, $r11d, implicit-def dead $eflags
70+
FAKE_USE killed renamable $r11d
71+
TEST32rr killed renamable $r13d, renamable $r13d, implicit-def $eflags
12772
RET64
12873
12974
...

0 commit comments

Comments
 (0)