87e3502c9aa38566c2cbb03222cbd14a69fe73a4
[oota-llvm.git] / lib / Target / AArch64 / AArch64PBQPRegAlloc.cpp
1 //===-- AArch64PBQPRegAlloc.cpp - AArch64 specific PBQP constraints -------===//
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 // This file contains the AArch64 / Cortex-A57 specific register allocation
10 // constraints for use by the PBQP register allocator.
11 //
12 // It is essentially a transcription of what is contained in
13 // AArch64A57FPLoadBalancing, which tries to use a balanced
14 // mix of odd and even D-registers when performing a critical sequence of
15 // independent, non-quadword FP/ASIMD floating-point multiply-accumulates.
16 //===----------------------------------------------------------------------===//
17
18 #define DEBUG_TYPE "aarch64-pbqp"
19
20 #include "AArch64.h"
21 #include "AArch64RegisterInfo.h"
22
23 #include "llvm/ADT/SetVector.h"
24 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
25 #include "llvm/CodeGen/MachineBasicBlock.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/RegAllocPBQP.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/raw_ostream.h"
32
33 #define PBQP_BUILDER PBQPBuilderWithCoalescing
34 //#define PBQP_BUILDER PBQPBuilder
35
36 using namespace llvm;
37
38 namespace {
39
40 #ifndef NDEBUG
41 bool isFPReg(unsigned reg) {
42   return AArch64::FPR32RegClass.contains(reg) ||
43          AArch64::FPR64RegClass.contains(reg) ||
44          AArch64::FPR128RegClass.contains(reg);
45 };
46 #endif
47
48 bool isOdd(unsigned reg) {
49   switch (reg) {
50   default:
51     llvm_unreachable("Register is not from the expected class !");
52   case AArch64::S1:
53   case AArch64::S3:
54   case AArch64::S5:
55   case AArch64::S7:
56   case AArch64::S9:
57   case AArch64::S11:
58   case AArch64::S13:
59   case AArch64::S15:
60   case AArch64::S17:
61   case AArch64::S19:
62   case AArch64::S21:
63   case AArch64::S23:
64   case AArch64::S25:
65   case AArch64::S27:
66   case AArch64::S29:
67   case AArch64::S31:
68   case AArch64::D1:
69   case AArch64::D3:
70   case AArch64::D5:
71   case AArch64::D7:
72   case AArch64::D9:
73   case AArch64::D11:
74   case AArch64::D13:
75   case AArch64::D15:
76   case AArch64::D17:
77   case AArch64::D19:
78   case AArch64::D21:
79   case AArch64::D23:
80   case AArch64::D25:
81   case AArch64::D27:
82   case AArch64::D29:
83   case AArch64::D31:
84   case AArch64::Q1:
85   case AArch64::Q3:
86   case AArch64::Q5:
87   case AArch64::Q7:
88   case AArch64::Q9:
89   case AArch64::Q11:
90   case AArch64::Q13:
91   case AArch64::Q15:
92   case AArch64::Q17:
93   case AArch64::Q19:
94   case AArch64::Q21:
95   case AArch64::Q23:
96   case AArch64::Q25:
97   case AArch64::Q27:
98   case AArch64::Q29:
99   case AArch64::Q31:
100     return true;
101   case AArch64::S0:
102   case AArch64::S2:
103   case AArch64::S4:
104   case AArch64::S6:
105   case AArch64::S8:
106   case AArch64::S10:
107   case AArch64::S12:
108   case AArch64::S14:
109   case AArch64::S16:
110   case AArch64::S18:
111   case AArch64::S20:
112   case AArch64::S22:
113   case AArch64::S24:
114   case AArch64::S26:
115   case AArch64::S28:
116   case AArch64::S30:
117   case AArch64::D0:
118   case AArch64::D2:
119   case AArch64::D4:
120   case AArch64::D6:
121   case AArch64::D8:
122   case AArch64::D10:
123   case AArch64::D12:
124   case AArch64::D14:
125   case AArch64::D16:
126   case AArch64::D18:
127   case AArch64::D20:
128   case AArch64::D22:
129   case AArch64::D24:
130   case AArch64::D26:
131   case AArch64::D28:
132   case AArch64::D30:
133   case AArch64::Q0:
134   case AArch64::Q2:
135   case AArch64::Q4:
136   case AArch64::Q6:
137   case AArch64::Q8:
138   case AArch64::Q10:
139   case AArch64::Q12:
140   case AArch64::Q14:
141   case AArch64::Q16:
142   case AArch64::Q18:
143   case AArch64::Q20:
144   case AArch64::Q22:
145   case AArch64::Q24:
146   case AArch64::Q26:
147   case AArch64::Q28:
148   case AArch64::Q30:
149     return false;
150
151   }
152 }
153
154 bool haveSameParity(unsigned reg1, unsigned reg2) {
155   assert(isFPReg(reg1) && "Expecting an FP register for reg1");
156   assert(isFPReg(reg2) && "Expecting an FP register for reg2");
157
158   return isOdd(reg1) == isOdd(reg2);
159 }
160
161 class A57PBQPBuilder : public PBQP_BUILDER {
162 public:
163   A57PBQPBuilder() : PBQP_BUILDER(), TRI(nullptr), LIs(nullptr), Chains() {}
164
165   // Build a PBQP instance to represent the register allocation problem for
166   // the given MachineFunction.
167   std::unique_ptr<PBQPRAProblem>
168   build(MachineFunction *MF, const LiveIntervals *LI,
169         const MachineBlockFrequencyInfo *blockInfo,
170         const RegSet &VRegs) override;
171
172 private:
173   const AArch64RegisterInfo *TRI;
174   const LiveIntervals *LIs;
175   SmallSetVector<unsigned, 32> Chains;
176
177   // Return true if reg is a physical register
178   bool isPhysicalReg(unsigned reg) const {
179     return TRI->isPhysicalRegister(reg);
180   }
181
182   // Add the accumulator chaining constraint, inside the chain, i.e. so that
183   // parity(Rd) == parity(Ra).
184   // \return true if a constraint was added
185   bool addIntraChainConstraint(PBQPRAProblem *p, unsigned Rd, unsigned Ra);
186
187   // Add constraints between existing chains
188   void addInterChainConstraint(PBQPRAProblem *p, unsigned Rd, unsigned Ra);
189 };
190 } // Anonymous namespace
191
192 bool A57PBQPBuilder::addIntraChainConstraint(PBQPRAProblem *p, unsigned Rd,
193                                              unsigned Ra) {
194   if (Rd == Ra)
195     return false;
196
197   if (isPhysicalReg(Rd) || isPhysicalReg(Ra)) {
198     dbgs() << "Rd is a physical reg:" << isPhysicalReg(Rd) << '\n';
199     dbgs() << "Ra is a physical reg:" << isPhysicalReg(Ra) << '\n';
200     return false;
201   }
202
203   const PBQPRAProblem::AllowedSet *vRdAllowed = &p->getAllowedSet(Rd);
204   const PBQPRAProblem::AllowedSet *vRaAllowed = &p->getAllowedSet(Ra);
205
206   PBQPRAGraph &g = p->getGraph();
207   PBQPRAGraph::NodeId node1 = p->getNodeForVReg(Rd);
208   PBQPRAGraph::NodeId node2 = p->getNodeForVReg(Ra);
209   PBQPRAGraph::EdgeId edge = g.findEdge(node1, node2);
210
211   // The edge does not exist. Create one with the appropriate interference
212   // costs.
213   if (edge == g.invalidEdgeId()) {
214     const LiveInterval &ld = LIs->getInterval(Rd);
215     const LiveInterval &la = LIs->getInterval(Ra);
216     bool livesOverlap = ld.overlaps(la);
217
218     PBQP::Matrix costs(vRdAllowed->size() + 1, vRaAllowed->size() + 1, 0);
219     for (unsigned i = 0; i != vRdAllowed->size(); ++i) {
220       unsigned pRd = (*vRdAllowed)[i];
221       for (unsigned j = 0; j != vRaAllowed->size(); ++j) {
222         unsigned pRa = (*vRaAllowed)[j];
223         if (livesOverlap && TRI->regsOverlap(pRd, pRa))
224           costs[i + 1][j + 1] = std::numeric_limits<PBQP::PBQPNum>::infinity();
225         else
226           costs[i + 1][j + 1] = haveSameParity(pRd, pRa) ? 0.0 : 1.0;
227       }
228     }
229     g.addEdge(node1, node2, std::move(costs));
230     return true;
231   }
232
233   if (g.getEdgeNode1Id(edge) == node2) {
234     std::swap(node1, node2);
235     std::swap(vRdAllowed, vRaAllowed);
236   }
237
238   // Enforce minCost(sameParity(RaClass)) > maxCost(otherParity(RdClass))
239   PBQP::Matrix costs(g.getEdgeCosts(edge));
240   for (unsigned i = 0; i != vRdAllowed->size(); ++i) {
241     unsigned pRd = (*vRdAllowed)[i];
242
243     // Get the maximum cost (excluding unallocatable reg) for same parity
244     // registers
245     PBQP::PBQPNum sameParityMax = std::numeric_limits<PBQP::PBQPNum>::min();
246     for (unsigned j = 0; j != vRaAllowed->size(); ++j) {
247       unsigned pRa = (*vRaAllowed)[j];
248       if (haveSameParity(pRd, pRa))
249         if (costs[i + 1][j + 1] !=
250                 std::numeric_limits<PBQP::PBQPNum>::infinity() &&
251             costs[i + 1][j + 1] > sameParityMax)
252           sameParityMax = costs[i + 1][j + 1];
253     }
254
255     // Ensure all registers with a different parity have a higher cost
256     // than sameParityMax
257     for (unsigned j = 0; j != vRaAllowed->size(); ++j) {
258       unsigned pRa = (*vRaAllowed)[j];
259       if (!haveSameParity(pRd, pRa))
260         if (sameParityMax > costs[i + 1][j + 1])
261           costs[i + 1][j + 1] = sameParityMax + 1.0;
262     }
263   }
264   g.setEdgeCosts(edge, costs);
265
266   return true;
267 }
268
269 void
270 A57PBQPBuilder::addInterChainConstraint(PBQPRAProblem *p, unsigned Rd,
271                                         unsigned Ra) {
272   // Do some Chain management
273   if (Chains.count(Ra)) {
274     if (Rd != Ra) {
275       DEBUG(dbgs() << "Moving acc chain from " << PrintReg(Ra, TRI) << " to "
276                    << PrintReg(Rd, TRI) << '\n';);
277       Chains.remove(Ra);
278       Chains.insert(Rd);
279     }
280   } else {
281     DEBUG(dbgs() << "Creating new acc chain for " << PrintReg(Rd, TRI)
282                  << '\n';);
283     Chains.insert(Rd);
284   }
285
286   const LiveInterval &ld = LIs->getInterval(Rd);
287   for (auto r : Chains) {
288     // Skip self
289     if (r == Rd)
290       continue;
291
292     const LiveInterval &lr = LIs->getInterval(r);
293     if (ld.overlaps(lr)) {
294       const PBQPRAProblem::AllowedSet *vRdAllowed = &p->getAllowedSet(Rd);
295       const PBQPRAProblem::AllowedSet *vRrAllowed = &p->getAllowedSet(r);
296
297       PBQPRAGraph &g = p->getGraph();
298       PBQPRAGraph::NodeId node1 = p->getNodeForVReg(Rd);
299       PBQPRAGraph::NodeId node2 = p->getNodeForVReg(r);
300       PBQPRAGraph::EdgeId edge = g.findEdge(node1, node2);
301       assert(edge != g.invalidEdgeId() &&
302              "PBQP error ! The edge should exist !");
303
304       DEBUG(dbgs() << "Refining constraint !\n";);
305
306       if (g.getEdgeNode1Id(edge) == node2) {
307         std::swap(node1, node2);
308         std::swap(vRdAllowed, vRrAllowed);
309       }
310
311       // Enforce that cost is higher with all other Chains of the same parity
312       PBQP::Matrix costs(g.getEdgeCosts(edge));
313       for (unsigned i = 0; i != vRdAllowed->size(); ++i) {
314         unsigned pRd = (*vRdAllowed)[i];
315
316         // Get the maximum cost (excluding unallocatable reg) for all other
317         // parity registers
318         PBQP::PBQPNum sameParityMax = std::numeric_limits<PBQP::PBQPNum>::min();
319         for (unsigned j = 0; j != vRrAllowed->size(); ++j) {
320           unsigned pRa = (*vRrAllowed)[j];
321           if (!haveSameParity(pRd, pRa))
322             if (costs[i + 1][j + 1] !=
323                     std::numeric_limits<PBQP::PBQPNum>::infinity() &&
324                 costs[i + 1][j + 1] > sameParityMax)
325               sameParityMax = costs[i + 1][j + 1];
326         }
327
328         // Ensure all registers with same parity have a higher cost
329         // than sameParityMax
330         for (unsigned j = 0; j != vRrAllowed->size(); ++j) {
331           unsigned pRa = (*vRrAllowed)[j];
332           if (haveSameParity(pRd, pRa))
333             if (sameParityMax > costs[i + 1][j + 1])
334               costs[i + 1][j + 1] = sameParityMax + 1.0;
335         }
336       }
337       g.setEdgeCosts(edge, costs);
338     }
339   }
340 }
341
342 std::unique_ptr<PBQPRAProblem>
343 A57PBQPBuilder::build(MachineFunction *MF, const LiveIntervals *LI,
344                       const MachineBlockFrequencyInfo *blockInfo,
345                       const RegSet &VRegs) {
346   std::unique_ptr<PBQPRAProblem> p =
347       PBQP_BUILDER::build(MF, LI, blockInfo, VRegs);
348
349   TRI = static_cast<const AArch64RegisterInfo *>(
350       MF->getTarget().getSubtargetImpl()->getRegisterInfo());
351   LIs = LI;
352
353   DEBUG(MF->dump(););
354
355   for (MachineFunction::const_iterator mbbItr = MF->begin(), mbbEnd = MF->end();
356        mbbItr != mbbEnd; ++mbbItr) {
357     const MachineBasicBlock *MBB = &*mbbItr;
358     Chains.clear(); // FIXME: really needed ? Could not work at MF level ?
359
360     for (MachineBasicBlock::const_iterator miItr = MBB->begin(),
361                                            miEnd = MBB->end();
362          miItr != miEnd; ++miItr) {
363       const MachineInstr *MI = &*miItr;
364       switch (MI->getOpcode()) {
365       case AArch64::FMSUBSrrr:
366       case AArch64::FMADDSrrr:
367       case AArch64::FNMSUBSrrr:
368       case AArch64::FNMADDSrrr:
369       case AArch64::FMSUBDrrr:
370       case AArch64::FMADDDrrr:
371       case AArch64::FNMSUBDrrr:
372       case AArch64::FNMADDDrrr: {
373         unsigned Rd = MI->getOperand(0).getReg();
374         unsigned Ra = MI->getOperand(3).getReg();
375
376         if (addIntraChainConstraint(p.get(), Rd, Ra))
377           addInterChainConstraint(p.get(), Rd, Ra);
378         break;
379       }
380
381       case AArch64::FMLAv2f32:
382       case AArch64::FMLSv2f32: {
383         unsigned Rd = MI->getOperand(0).getReg();
384         addInterChainConstraint(p.get(), Rd, Rd);
385         break;
386       }
387
388       default:
389         // Forget Chains which have been killed
390         for (auto r : Chains) {
391           SmallVector<unsigned, 8> toDel;
392           if (MI->killsRegister(r)) {
393             DEBUG(dbgs() << "Killing chain " << PrintReg(r, TRI) << " at ";
394                   MI->print(dbgs()););
395             toDel.push_back(r);
396           }
397
398           while (!toDel.empty()) {
399             Chains.remove(toDel.back());
400             toDel.pop_back();
401           }
402         }
403       }
404     }
405   }
406
407   return p;
408 }
409
410 // Factory function used by AArch64TargetMachine to add the pass to the
411 // passmanager.
412 FunctionPass *llvm::createAArch64A57PBQPRegAlloc() {
413   std::unique_ptr<PBQP_BUILDER> builder = llvm::make_unique<A57PBQPBuilder>();
414   return createPBQPRegisterAllocator(std::move(builder), nullptr);
415 }