@@ -97,6 +97,45 @@ void GCNRegPressure::inc(unsigned Reg,
9797 Value[RegKind] += Sign;
9898}
9999
100+ struct RegExcess {
101+ unsigned SGPR = 0 ;
102+ unsigned VGPR = 0 ;
103+ unsigned ArchVGPR = 0 ;
104+ unsigned AGPR = 0 ;
105+
106+ bool anyExcess () const { return SGPR || VGPR || ArchVGPR || AGPR; }
107+
108+ RegExcess (const MachineFunction &MF, const GCNRegPressure &RP,
109+ unsigned MaxSGPRs, unsigned MaxVGPRs) {
110+ const GCNSubtarget &ST = MF.getSubtarget <GCNSubtarget>();
111+ SGPR = std::max (static_cast <int >(RP.getSGPRNum () - MaxSGPRs), 0 );
112+
113+ // The number of virtual VGPRs required to handle excess SGPR
114+ auto WaveSize = ST.getWavefrontSize ();
115+ unsigned VGPRForSGPRSpills = divideCeil (SGPR, WaveSize);
116+
117+ unsigned MaxArchVGPRs = ST.getAddressableNumArchVGPRs ();
118+
119+ // Unified excess pressure conditions, accounting for VGPRs used for SGPR
120+ // spills
121+ VGPR = std::max (static_cast <int >(RP.getVGPRNum (ST.hasGFX90AInsts ()) +
122+ VGPRForSGPRSpills - MaxVGPRs),
123+ 0 );
124+
125+ // Arch VGPR excess pressure conditions, accounting for VGPRs used for SGPR
126+ // spills
127+ ArchVGPR = std::max (static_cast <int >(RP.getVGPRNum (false ) +
128+ VGPRForSGPRSpills - MaxArchVGPRs),
129+ 0 );
130+
131+ // AGPR excess pressure conditions
132+ AGPR = std::max (static_cast <int >(ST.hasGFX90AInsts ()
133+ ? (RP.getAGPRNum () - MaxArchVGPRs)
134+ : (RP.getAGPRNum () - MaxVGPRs)),
135+ 0 );
136+ }
137+ };
138+
100139bool GCNRegPressure::less (const MachineFunction &MF, const GCNRegPressure &O,
101140 unsigned MaxOccupancy) const {
102141 const GCNSubtarget &ST = MF.getSubtarget <GCNSubtarget>();
@@ -125,61 +164,24 @@ bool GCNRegPressure::less(const MachineFunction &MF, const GCNRegPressure &O,
125164 unsigned MaxVGPRs = ST.getMaxNumVGPRs (MF);
126165 unsigned MaxSGPRs = ST.getMaxNumSGPRs (MF);
127166
128- // SGPR excess pressure conditions
129- unsigned ExcessSGPR = std::max (static_cast <int >(getSGPRNum () - MaxSGPRs), 0 );
130- unsigned OtherExcessSGPR =
131- std::max (static_cast <int >(O.getSGPRNum () - MaxSGPRs), 0 );
132-
133- auto WaveSize = ST.getWavefrontSize ();
134- // The number of virtual VGPRs required to handle excess SGPR
135- unsigned VGPRForSGPRSpills = (ExcessSGPR + (WaveSize - 1 )) / WaveSize;
136- unsigned OtherVGPRForSGPRSpills =
137- (OtherExcessSGPR + (WaveSize - 1 )) / WaveSize;
167+ RegExcess Excess (MF, *this , MaxSGPRs, MaxVGPRs);
168+ RegExcess OtherExcess (MF, O, MaxSGPRs, MaxVGPRs);
138169
139170 unsigned MaxArchVGPRs = ST.getAddressableNumArchVGPRs ();
140171
141- // Unified excess pressure conditions, accounting for VGPRs used for SGPR
142- // spills
143- unsigned ExcessVGPR =
144- std::max (static_cast <int >(getVGPRNum (ST.hasGFX90AInsts ()) +
145- VGPRForSGPRSpills - MaxVGPRs),
146- 0 );
147- unsigned OtherExcessVGPR =
148- std::max (static_cast <int >(O.getVGPRNum (ST.hasGFX90AInsts ()) +
149- OtherVGPRForSGPRSpills - MaxVGPRs),
150- 0 );
151- // Arch VGPR excess pressure conditions, accounting for VGPRs used for SGPR
152- // spills
153- unsigned ExcessArchVGPR = std::max (
154- static_cast <int >(getVGPRNum (false ) + VGPRForSGPRSpills - MaxArchVGPRs),
155- 0 );
156- unsigned OtherExcessArchVGPR =
157- std::max (static_cast <int >(O.getVGPRNum (false ) + OtherVGPRForSGPRSpills -
158- MaxArchVGPRs),
159- 0 );
160- // AGPR excess pressure conditions
161- unsigned ExcessAGPR = std::max (
162- static_cast <int >(ST.hasGFX90AInsts () ? (getAGPRNum () - MaxArchVGPRs)
163- : (getAGPRNum () - MaxVGPRs)),
164- 0 );
165- unsigned OtherExcessAGPR = std::max (
166- static_cast <int >(ST.hasGFX90AInsts () ? (O.getAGPRNum () - MaxArchVGPRs)
167- : (O.getAGPRNum () - MaxVGPRs)),
168- 0 );
169-
170- bool ExcessRP = ExcessSGPR || ExcessVGPR || ExcessArchVGPR || ExcessAGPR;
171- bool OtherExcessRP = OtherExcessSGPR || OtherExcessVGPR ||
172- OtherExcessArchVGPR || OtherExcessAGPR;
172+ bool ExcessRP = Excess.anyExcess ();
173+ bool OtherExcessRP = OtherExcess.anyExcess ();
173174
174175 // Give second precedence to the reduced number of spills to hold the register
175176 // pressure.
176177 if (ExcessRP || OtherExcessRP) {
177178 // The difference in excess VGPR pressure, after including VGPRs used for
178179 // SGPR spills
179- int VGPRDiff = ((OtherExcessVGPR + OtherExcessArchVGPR + OtherExcessAGPR) -
180- (ExcessVGPR + ExcessArchVGPR + ExcessAGPR));
180+ int VGPRDiff =
181+ ((OtherExcess.VGPR + OtherExcess.ArchVGPR + OtherExcess.AGPR ) -
182+ (Excess.VGPR + Excess.ArchVGPR + Excess.AGPR ));
181183
182- int SGPRDiff = OtherExcessSGPR - ExcessSGPR ;
184+ int SGPRDiff = OtherExcess. SGPR - Excess. SGPR ;
183185
184186 if (VGPRDiff != 0 )
185187 return VGPRDiff > 0 ;
0 commit comments