Skip to content

Commit cfd9119

Browse files
authored
[X86] Skip unused VRegs traverse (#78229)
Almost all loops with getNumVirtRegs skip unused registers by means of reg_nodbg_empty or empty live interval. Except for these two cases that are revealed by GlobalISel since it can skip RegClass assignment for unused registers. Closes #64452, closes #71926
1 parent 074630e commit cfd9119

File tree

4 files changed

+118
-1
lines changed

4 files changed

+118
-1
lines changed

llvm/lib/Target/X86/X86DomainReassignment.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,10 @@ bool X86DomainReassignment::runOnMachineFunction(MachineFunction &MF) {
754754
for (unsigned Idx = 0; Idx < MRI->getNumVirtRegs(); ++Idx) {
755755
Register Reg = Register::index2VirtReg(Idx);
756756

757+
// Skip unused VRegs.
758+
if (MRI->reg_nodbg_empty(Reg))
759+
continue;
760+
757761
// GPR only current source domain supported.
758762
if (!isGPR(MRI->getRegClass(Reg)))
759763
continue;

llvm/lib/Target/X86/X86FastPreTileConfig.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,8 @@ bool X86FastPreTileConfig::runOnMachineFunction(MachineFunction &MFunc) {
667667
bool HasVirtTileReg = false;
668668
for (unsigned I = 0, E = NumVirtRegs; I != E; ++I) {
669669
Register VirtReg = Register::index2VirtReg(I);
670-
if (MRI->getRegClass(VirtReg)->getID() == X86::TILERegClassID) {
670+
if (!MRI->reg_nodbg_empty(VirtReg) &&
671+
MRI->getRegClass(VirtReg)->getID() == X86::TILERegClassID) {
671672
HasVirtTileReg = true;
672673
break;
673674
}

llvm/test/CodeGen/X86/AMX/amx-fastpreconfig.mir

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,48 @@ body: |
5959
RET 0, killed $eax
6060
6161
...
62+
# GlobalIsel doesn't use all virtual registers and there may be virtual
63+
# registers without a class.
64+
# Note that %3 doesn't have a class: gpr instead of gr64.
65+
---
66+
name: test_unused
67+
legalized: true
68+
regBankSelected: true
69+
selected: true
70+
failedISel: false
71+
tracksRegLiveness: true
72+
registers:
73+
- { id: 0, class: gr64, preferred-register: '' }
74+
- { id: 1, class: gr64_with_sub_8bit, preferred-register: '' }
75+
- { id: 2, class: gr64, preferred-register: '' }
76+
- { id: 3, class: gpr, preferred-register: '' }
77+
- { id: 4, class: gr64, preferred-register: '' }
78+
- { id: 5, class: gr8, preferred-register: '' }
79+
liveins:
80+
- { reg: '$rdi', virtual-reg: '' }
81+
- { reg: '$rsi', virtual-reg: '' }
82+
body: |
83+
bb.1.entry:
84+
liveins: $rdi, $rsi
85+
86+
; CHECK-LABEL: name: test_unused
87+
; CHECK: liveins: $rdi, $rsi
88+
; CHECK-NEXT: {{ $}}
89+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gr64 = COPY $rdi
90+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:gr64_with_sub_8bit = COPY $rsi
91+
; CHECK-NEXT: [[COPY2:%[0-9]+]]:gr8 = COPY [[COPY1]].sub_8bit
92+
; CHECK-NEXT: $cl = COPY [[COPY2]]
93+
; CHECK-NEXT: [[SHR64rCL:%[0-9]+]]:gr64 = SHR64rCL [[COPY]], implicit-def $eflags, implicit $cl
94+
; CHECK-NEXT: [[ADD64ri32_:%[0-9]+]]:gr64 = ADD64ri32 [[SHR64rCL]], 123456789, implicit-def $eflags
95+
; CHECK-NEXT: $rax = COPY [[ADD64ri32_]]
96+
; CHECK-NEXT: RET 0, implicit $rax
97+
%0:gr64 = COPY $rdi
98+
%1:gr64_with_sub_8bit = COPY $rsi
99+
%5:gr8 = COPY %1.sub_8bit
100+
$cl = COPY %5
101+
%2:gr64 = SHR64rCL %0, implicit-def $eflags, implicit $cl
102+
%4:gr64 = ADD64ri32 %2, 123456789, implicit-def $eflags
103+
$rax = COPY %4
104+
RET 0, implicit $rax
105+
106+
...

llvm/test/CodeGen/X86/domain-reassignment.mir

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
define void @test_64bitext() #0 {
4747
ret void
4848
}
49+
; Note that this function need to be compiled with -global-isel
50+
; to obtain testable MIR
51+
define void @test_unused(i64 %0) #0 {
52+
%unused = lshr i64 %0, 7
53+
ret void
54+
}
4955
...
5056
---
5157
name: test_fcmp_storefloat
@@ -860,3 +866,64 @@ body: |
860866
RET 0
861867
862868
...
869+
---
870+
name: test_unused
871+
alignment: 16
872+
exposesReturnsTwice: false
873+
legalized: true
874+
regBankSelected: true
875+
selected: true
876+
failedISel: false
877+
tracksRegLiveness: true
878+
hasWinCFI: false
879+
callsEHReturn: false
880+
callsUnwindInit: false
881+
hasEHCatchret: false
882+
hasEHScopes: false
883+
hasEHFunclets: false
884+
isOutlined: false
885+
debugInstrRef: false
886+
failsVerification: false
887+
tracksDebugUserValues: false
888+
registers:
889+
# Note that this test is supposed to have registers without classes
890+
- { id: 0, class: _, preferred-register: '' }
891+
- { id: 1, class: _, preferred-register: '' }
892+
- { id: 2, class: _, preferred-register: '' }
893+
liveins:
894+
- { reg: '$rdi', virtual-reg: '' }
895+
frameInfo:
896+
isFrameAddressTaken: false
897+
isReturnAddressTaken: false
898+
hasStackMap: false
899+
hasPatchPoint: false
900+
stackSize: 0
901+
offsetAdjustment: 0
902+
maxAlignment: 1
903+
adjustsStack: false
904+
hasCalls: false
905+
stackProtector: ''
906+
functionContext: ''
907+
maxCallFrameSize: 4294967295
908+
cvBytesOfCalleeSavedRegisters: 0
909+
hasOpaqueSPAdjustment: false
910+
hasVAStart: false
911+
hasMustTailInVarArgFunc: false
912+
hasTailCall: false
913+
localFrameSize: 0
914+
savePoint: ''
915+
restorePoint: ''
916+
fixedStack: []
917+
stack: []
918+
entry_values: []
919+
callSites: []
920+
debugValueSubstitutions: []
921+
constants: []
922+
machineFunctionInfo: {}
923+
body: |
924+
bb.1 (%ir-block.1):
925+
liveins: $rdi
926+
927+
RET 0
928+
929+
...

0 commit comments

Comments
 (0)