1 //===-- SIFixSGPRCopies.cpp - Remove potential VGPR => SGPR copies --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 /// Copies from VGPR to SGPR registers are illegal and the register coalescer
12 /// will sometimes generate these illegal copies in situations like this:
13 ///
14 ///  Register Class <vsrc> is the union of <vgpr> and <sgpr>
15 ///
16 /// BB0:
17 ///   %vreg0 <sgpr> = SCALAR_INST
18 ///   %vreg1 <vsrc> = COPY %vreg0 <sgpr>
19 ///    ...
20 ///    BRANCH %cond BB1, BB2
21 ///  BB1:
22 ///    %vreg2 <vgpr> = VECTOR_INST
23 ///    %vreg3 <vsrc> = COPY %vreg2 <vgpr>
24 ///  BB2:
25 ///    %vreg4 <vsrc> = PHI %vreg1 <vsrc>, <BB#0>, %vreg3 <vrsc>, <BB#1>
26 ///    %vreg5 <vgpr> = VECTOR_INST %vreg4 <vsrc>
27 ///
28 ///
29 /// The coalescer will begin at BB0 and eliminate its copy, then the resulting
30 /// code will look like this:
31 ///
32 /// BB0:
33 ///   %vreg0 <sgpr> = SCALAR_INST
34 ///    ...
35 ///    BRANCH %cond BB1, BB2
36 /// BB1:
37 ///   %vreg2 <vgpr> = VECTOR_INST
38 ///   %vreg3 <vsrc> = COPY %vreg2 <vgpr>
39 /// BB2:
40 ///   %vreg4 <sgpr> = PHI %vreg0 <sgpr>, <BB#0>, %vreg3 <vsrc>, <BB#1>
41 ///   %vreg5 <vgpr> = VECTOR_INST %vreg4 <sgpr>
42 ///
43 /// Now that the result of the PHI instruction is an SGPR, the register
44 /// allocator is now forced to constrain the register class of %vreg3 to
45 /// <sgpr> so we end up with final code like this:
46 ///
47 /// BB0:
48 ///   %vreg0 <sgpr> = SCALAR_INST
49 ///    ...
50 ///    BRANCH %cond BB1, BB2
51 /// BB1:
52 ///   %vreg2 <vgpr> = VECTOR_INST
53 ///   %vreg3 <sgpr> = COPY %vreg2 <vgpr>
54 /// BB2:
55 ///   %vreg4 <sgpr> = PHI %vreg0 <sgpr>, <BB#0>, %vreg3 <sgpr>, <BB#1>
56 ///   %vreg5 <vgpr> = VECTOR_INST %vreg4 <sgpr>
57 ///
58 /// Now this code contains an illegal copy from a VGPR to an SGPR.
59 ///
60 /// In order to avoid this problem, this pass searches for PHI instructions
61 /// which define a <vsrc> register and constrains its definition class to
62 /// <vgpr> if the user of the PHI's definition register is a vector instruction.
63 /// If the PHI's definition class is constrained to <vgpr> then the coalescer
64 /// will be unable to perform the COPY removal from the above example  which
65 /// ultimately led to the creation of an illegal COPY.
66 //===----------------------------------------------------------------------===//
67 
68 #include "AMDGPU.h"
69 #include "AMDGPUSubtarget.h"
70 #include "SIInstrInfo.h"
71 #include "llvm/CodeGen/MachineFunctionPass.h"
72 #include "llvm/CodeGen/MachineInstrBuilder.h"
73 #include "llvm/CodeGen/MachineRegisterInfo.h"
74 #include "llvm/Support/Debug.h"
75 #include "llvm/Support/raw_ostream.h"
76 #include "llvm/Target/TargetMachine.h"
77 
78 using namespace llvm;
79 
80 #define DEBUG_TYPE "si-fix-sgpr-copies"
81 
82 namespace {
83 
84 class SIFixSGPRCopies : public MachineFunctionPass {
85 public:
86   static char ID;
87 
88   SIFixSGPRCopies() : MachineFunctionPass(ID) { }
89 
90   bool runOnMachineFunction(MachineFunction &MF) override;
91 
92   StringRef getPassName() const override { return "SI Fix SGPR copies"; }
93 
94   void getAnalysisUsage(AnalysisUsage &AU) const override {
95     AU.setPreservesCFG();
96     MachineFunctionPass::getAnalysisUsage(AU);
97   }
98 };
99 
100 } // End anonymous namespace
101 
102 INITIALIZE_PASS(SIFixSGPRCopies, DEBUG_TYPE,
103                 "SI Fix SGPR copies", false, false)
104 
105 char SIFixSGPRCopies::ID = 0;
106 
107 char &llvm::SIFixSGPRCopiesID = SIFixSGPRCopies::ID;
108 
109 FunctionPass *llvm::createSIFixSGPRCopiesPass() {
110   return new SIFixSGPRCopies();
111 }
112 
113 static bool hasVGPROperands(const MachineInstr &MI, const SIRegisterInfo *TRI) {
114   const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
115   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
116     if (!MI.getOperand(i).isReg() ||
117         !TargetRegisterInfo::isVirtualRegister(MI.getOperand(i).getReg()))
118       continue;
119 
120     if (TRI->hasVGPRs(MRI.getRegClass(MI.getOperand(i).getReg())))
121       return true;
122   }
123   return false;
124 }
125 
126 static std::pair<const TargetRegisterClass *, const TargetRegisterClass *>
127 getCopyRegClasses(const MachineInstr &Copy,
128                   const SIRegisterInfo &TRI,
129                   const MachineRegisterInfo &MRI) {
130   unsigned DstReg = Copy.getOperand(0).getReg();
131   unsigned SrcReg = Copy.getOperand(1).getReg();
132 
133   const TargetRegisterClass *SrcRC =
134     TargetRegisterInfo::isVirtualRegister(SrcReg) ?
135     MRI.getRegClass(SrcReg) :
136     TRI.getPhysRegClass(SrcReg);
137 
138   // We don't really care about the subregister here.
139   // SrcRC = TRI.getSubRegClass(SrcRC, Copy.getOperand(1).getSubReg());
140 
141   const TargetRegisterClass *DstRC =
142     TargetRegisterInfo::isVirtualRegister(DstReg) ?
143     MRI.getRegClass(DstReg) :
144     TRI.getPhysRegClass(DstReg);
145 
146   return std::make_pair(SrcRC, DstRC);
147 }
148 
149 static bool isVGPRToSGPRCopy(const TargetRegisterClass *SrcRC,
150                              const TargetRegisterClass *DstRC,
151                              const SIRegisterInfo &TRI) {
152   return TRI.isSGPRClass(DstRC) && TRI.hasVGPRs(SrcRC);
153 }
154 
155 static bool isSGPRToVGPRCopy(const TargetRegisterClass *SrcRC,
156                              const TargetRegisterClass *DstRC,
157                              const SIRegisterInfo &TRI) {
158   return TRI.isSGPRClass(SrcRC) && TRI.hasVGPRs(DstRC);
159 }
160 
161 // Distribute an SGPR->VGPR copy of a REG_SEQUENCE into a VGPR REG_SEQUENCE.
162 //
163 // SGPRx = ...
164 // SGPRy = REG_SEQUENCE SGPRx, sub0 ...
165 // VGPRz = COPY SGPRy
166 //
167 // ==>
168 //
169 // VGPRx = COPY SGPRx
170 // VGPRz = REG_SEQUENCE VGPRx, sub0
171 //
172 // This exposes immediate folding opportunities when materializing 64-bit
173 // immediates.
174 static bool foldVGPRCopyIntoRegSequence(MachineInstr &MI,
175                                         const SIRegisterInfo *TRI,
176                                         const SIInstrInfo *TII,
177                                         MachineRegisterInfo &MRI) {
178   assert(MI.isRegSequence());
179 
180   unsigned DstReg = MI.getOperand(0).getReg();
181   if (!TRI->isSGPRClass(MRI.getRegClass(DstReg)))
182     return false;
183 
184   if (!MRI.hasOneUse(DstReg))
185     return false;
186 
187   MachineInstr &CopyUse = *MRI.use_instr_begin(DstReg);
188   if (!CopyUse.isCopy())
189     return false;
190 
191   const TargetRegisterClass *SrcRC, *DstRC;
192   std::tie(SrcRC, DstRC) = getCopyRegClasses(CopyUse, *TRI, MRI);
193 
194   if (!isSGPRToVGPRCopy(SrcRC, DstRC, *TRI))
195     return false;
196 
197   // TODO: Could have multiple extracts?
198   unsigned SubReg = CopyUse.getOperand(1).getSubReg();
199   if (SubReg != AMDGPU::NoSubRegister)
200     return false;
201 
202   MRI.setRegClass(DstReg, DstRC);
203 
204   // SGPRx = ...
205   // SGPRy = REG_SEQUENCE SGPRx, sub0 ...
206   // VGPRz = COPY SGPRy
207 
208   // =>
209   // VGPRx = COPY SGPRx
210   // VGPRz = REG_SEQUENCE VGPRx, sub0
211 
212   MI.getOperand(0).setReg(CopyUse.getOperand(0).getReg());
213 
214   for (unsigned I = 1, N = MI.getNumOperands(); I != N; I += 2) {
215     unsigned SrcReg = MI.getOperand(I).getReg();
216     unsigned SrcSubReg = MI.getOperand(I).getSubReg();
217 
218     const TargetRegisterClass *SrcRC = MRI.getRegClass(SrcReg);
219     assert(TRI->isSGPRClass(SrcRC) &&
220            "Expected SGPR REG_SEQUENCE to only have SGPR inputs");
221 
222     SrcRC = TRI->getSubRegClass(SrcRC, SrcSubReg);
223     const TargetRegisterClass *NewSrcRC = TRI->getEquivalentVGPRClass(SrcRC);
224 
225     unsigned TmpReg = MRI.createVirtualRegister(NewSrcRC);
226 
227     BuildMI(*MI.getParent(), &MI, MI.getDebugLoc(), TII->get(AMDGPU::COPY), TmpReg)
228       .addOperand(MI.getOperand(I));
229 
230     MI.getOperand(I).setReg(TmpReg);
231   }
232 
233   CopyUse.eraseFromParent();
234   return true;
235 }
236 
237 static bool phiHasVGPROperands(const MachineInstr &PHI,
238                                const MachineRegisterInfo &MRI,
239                                const SIRegisterInfo *TRI,
240                                const SIInstrInfo *TII) {
241 
242   for (unsigned i = 1; i < PHI.getNumOperands(); i += 2) {
243     unsigned Reg = PHI.getOperand(i).getReg();
244     if (TRI->hasVGPRs(MRI.getRegClass(Reg)))
245       return true;
246   }
247   return false;
248 }
249 static bool phiHasBreakDef(const MachineInstr &PHI,
250                            const MachineRegisterInfo &MRI,
251                            SmallSet<unsigned, 8> &Visited) {
252 
253   for (unsigned i = 1; i < PHI.getNumOperands(); i += 2) {
254     unsigned Reg = PHI.getOperand(i).getReg();
255     if (Visited.count(Reg))
256       continue;
257 
258     Visited.insert(Reg);
259 
260     MachineInstr *DefInstr = MRI.getUniqueVRegDef(Reg);
261     assert(DefInstr);
262     switch (DefInstr->getOpcode()) {
263     default:
264       break;
265     case AMDGPU::SI_BREAK:
266     case AMDGPU::SI_IF_BREAK:
267     case AMDGPU::SI_ELSE_BREAK:
268       return true;
269     case AMDGPU::PHI:
270       if (phiHasBreakDef(*DefInstr, MRI, Visited))
271         return true;
272     }
273   }
274   return false;
275 }
276 
277 bool SIFixSGPRCopies::runOnMachineFunction(MachineFunction &MF) {
278   const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
279   MachineRegisterInfo &MRI = MF.getRegInfo();
280   const SIRegisterInfo *TRI = ST.getRegisterInfo();
281   const SIInstrInfo *TII = ST.getInstrInfo();
282 
283   SmallVector<MachineInstr *, 16> Worklist;
284 
285   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
286                                                   BI != BE; ++BI) {
287 
288     MachineBasicBlock &MBB = *BI;
289     for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
290          I != E; ++I) {
291       MachineInstr &MI = *I;
292 
293       switch (MI.getOpcode()) {
294       default:
295         continue;
296       case AMDGPU::COPY: {
297         // If the destination register is a physical register there isn't really
298         // much we can do to fix this.
299         if (!TargetRegisterInfo::isVirtualRegister(MI.getOperand(0).getReg()))
300           continue;
301 
302         const TargetRegisterClass *SrcRC, *DstRC;
303         std::tie(SrcRC, DstRC) = getCopyRegClasses(MI, *TRI, MRI);
304         if (isVGPRToSGPRCopy(SrcRC, DstRC, *TRI)) {
305           DEBUG(dbgs() << "Fixing VGPR -> SGPR copy: " << MI);
306           TII->moveToVALU(MI);
307         }
308 
309         break;
310       }
311       case AMDGPU::PHI: {
312         DEBUG(dbgs() << "Fixing PHI: " << MI);
313         unsigned Reg = MI.getOperand(0).getReg();
314         if (!TRI->isSGPRClass(MRI.getRegClass(Reg)))
315           break;
316 
317         // If a PHI node defines an SGPR and any of its operands are VGPRs,
318         // then we need to move it to the VALU.
319         //
320         // Also, if a PHI node defines an SGPR and has all SGPR operands
321         // we must move it to the VALU, because the SGPR operands will
322         // all end up being assigned the same register, which means
323         // there is a potential for a conflict if different threads take
324         // different control flow paths.
325         //
326         // For Example:
327         //
328         // sgpr0 = def;
329         // ...
330         // sgpr1 = def;
331         // ...
332         // sgpr2 = PHI sgpr0, sgpr1
333         // use sgpr2;
334         //
335         // Will Become:
336         //
337         // sgpr2 = def;
338         // ...
339         // sgpr2 = def;
340         // ...
341         // use sgpr2
342         //
343         // FIXME: This is OK if the branching decision is made based on an
344         // SGPR value.
345         bool SGPRBranch = false;
346 
347         // The one exception to this rule is when one of the operands
348         // is defined by a SI_BREAK, SI_IF_BREAK, or SI_ELSE_BREAK
349         // instruction.  In this case, there we know the program will
350         // never enter the second block (the loop) without entering
351         // the first block (where the condition is computed), so there
352         // is no chance for values to be over-written.
353 
354         SmallSet<unsigned, 8> Visited;
355         if (phiHasVGPROperands(MI, MRI, TRI, TII) ||
356             (!SGPRBranch && !phiHasBreakDef(MI, MRI, Visited))) {
357           TII->moveToVALU(MI);
358         }
359         break;
360       }
361       case AMDGPU::REG_SEQUENCE: {
362         if (TRI->hasVGPRs(TII->getOpRegClass(MI, 0)) ||
363             !hasVGPROperands(MI, TRI)) {
364           foldVGPRCopyIntoRegSequence(MI, TRI, TII, MRI);
365           continue;
366         }
367 
368         DEBUG(dbgs() << "Fixing REG_SEQUENCE: " << MI);
369 
370         TII->moveToVALU(MI);
371         break;
372       }
373       case AMDGPU::INSERT_SUBREG: {
374         const TargetRegisterClass *DstRC, *Src0RC, *Src1RC;
375         DstRC = MRI.getRegClass(MI.getOperand(0).getReg());
376         Src0RC = MRI.getRegClass(MI.getOperand(1).getReg());
377         Src1RC = MRI.getRegClass(MI.getOperand(2).getReg());
378         if (TRI->isSGPRClass(DstRC) &&
379             (TRI->hasVGPRs(Src0RC) || TRI->hasVGPRs(Src1RC))) {
380           DEBUG(dbgs() << " Fixing INSERT_SUBREG: " << MI);
381           TII->moveToVALU(MI);
382         }
383         break;
384       }
385       }
386     }
387   }
388 
389   return true;
390 }
391