1 //===-- AArch64PBQPRegAlloc.cpp - AArch64 specific PBQP constraints -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // This file contains the AArch64 / Cortex-A57 specific register allocation
10 // constraints for use by the PBQP register allocator.
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 //===----------------------------------------------------------------------===//
18 #define DEBUG_TYPE "aarch64-pbqp"
21 #include "AArch64RegisterInfo.h"
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"
33 #define PBQP_BUILDER PBQPBuilderWithCoalescing
40 bool isFPReg(unsigned reg) {
41 return AArch64::FPR32RegClass.contains(reg) ||
42 AArch64::FPR64RegClass.contains(reg) ||
43 AArch64::FPR128RegClass.contains(reg);
47 bool isOdd(unsigned reg) {
50 llvm_unreachable("Register is not from the expected class !");
153 bool haveSameParity(unsigned reg1, unsigned reg2) {
154 assert(isFPReg(reg1) && "Expecting an FP register for reg1");
155 assert(isFPReg(reg2) && "Expecting an FP register for reg2");
157 return isOdd(reg1) == isOdd(reg2);
160 class A57PBQPBuilder : public PBQP_BUILDER {
162 A57PBQPBuilder() : PBQP_BUILDER(), TRI(nullptr), LIs(nullptr), Chains() {}
164 // Build a PBQP instance to represent the register allocation problem for
165 // the given MachineFunction.
166 std::unique_ptr<PBQPRAProblem>
167 build(MachineFunction *MF, const LiveIntervals *LI,
168 const MachineBlockFrequencyInfo *blockInfo,
169 const RegSet &VRegs) override;
172 const AArch64RegisterInfo *TRI;
173 const LiveIntervals *LIs;
174 SmallSetVector<unsigned, 32> Chains;
176 // Return true if reg is a physical register
177 bool isPhysicalReg(unsigned reg) const {
178 return TRI->isPhysicalRegister(reg);
181 // Add the accumulator chaining constraint, inside the chain, i.e. so that
182 // parity(Rd) == parity(Ra).
183 // \return true if a constraint was added
184 bool addIntraChainConstraint(PBQPRAProblem *p, unsigned Rd, unsigned Ra);
186 // Add constraints between existing chains
187 void addInterChainConstraint(PBQPRAProblem *p, unsigned Rd, unsigned Ra);
189 } // Anonymous namespace
191 bool A57PBQPBuilder::addIntraChainConstraint(PBQPRAProblem *p, unsigned Rd,
196 if (isPhysicalReg(Rd) || isPhysicalReg(Ra)) {
197 DEBUG(dbgs() << "Rd is a physical reg:" << isPhysicalReg(Rd) << '\n');
198 DEBUG(dbgs() << "Ra is a physical reg:" << isPhysicalReg(Ra) << '\n');
202 const PBQPRAProblem::AllowedSet *vRdAllowed = &p->getAllowedSet(Rd);
203 const PBQPRAProblem::AllowedSet *vRaAllowed = &p->getAllowedSet(Ra);
205 PBQPRAGraph &g = p->getGraph();
206 PBQPRAGraph::NodeId node1 = p->getNodeForVReg(Rd);
207 PBQPRAGraph::NodeId node2 = p->getNodeForVReg(Ra);
208 PBQPRAGraph::EdgeId edge = g.findEdge(node1, node2);
210 // The edge does not exist. Create one with the appropriate interference
212 if (edge == g.invalidEdgeId()) {
213 const LiveInterval &ld = LIs->getInterval(Rd);
214 const LiveInterval &la = LIs->getInterval(Ra);
215 bool livesOverlap = ld.overlaps(la);
217 PBQP::Matrix costs(vRdAllowed->size() + 1, vRaAllowed->size() + 1, 0);
218 for (unsigned i = 0, ie = vRdAllowed->size(); i != ie; ++i) {
219 unsigned pRd = (*vRdAllowed)[i];
220 for (unsigned j = 0, je = vRaAllowed->size(); j != je; ++j) {
221 unsigned pRa = (*vRaAllowed)[j];
222 if (livesOverlap && TRI->regsOverlap(pRd, pRa))
223 costs[i + 1][j + 1] = std::numeric_limits<PBQP::PBQPNum>::infinity();
225 costs[i + 1][j + 1] = haveSameParity(pRd, pRa) ? 0.0 : 1.0;
228 g.addEdge(node1, node2, std::move(costs));
232 if (g.getEdgeNode1Id(edge) == node2) {
233 std::swap(node1, node2);
234 std::swap(vRdAllowed, vRaAllowed);
237 // Enforce minCost(sameParity(RaClass)) > maxCost(otherParity(RdClass))
238 PBQP::Matrix costs(g.getEdgeCosts(edge));
239 for (unsigned i = 0, ie = vRdAllowed->size(); i != ie; ++i) {
240 unsigned pRd = (*vRdAllowed)[i];
242 // Get the maximum cost (excluding unallocatable reg) for same parity
244 PBQP::PBQPNum sameParityMax = std::numeric_limits<PBQP::PBQPNum>::min();
245 for (unsigned j = 0, je = vRaAllowed->size(); j != je; ++j) {
246 unsigned pRa = (*vRaAllowed)[j];
247 if (haveSameParity(pRd, pRa))
248 if (costs[i + 1][j + 1] !=
249 std::numeric_limits<PBQP::PBQPNum>::infinity() &&
250 costs[i + 1][j + 1] > sameParityMax)
251 sameParityMax = costs[i + 1][j + 1];
254 // Ensure all registers with a different parity have a higher cost
255 // than sameParityMax
256 for (unsigned j = 0, je = vRaAllowed->size(); j != je; ++j) {
257 unsigned pRa = (*vRaAllowed)[j];
258 if (!haveSameParity(pRd, pRa))
259 if (sameParityMax > costs[i + 1][j + 1])
260 costs[i + 1][j + 1] = sameParityMax + 1.0;
263 g.setEdgeCosts(edge, costs);
269 A57PBQPBuilder::addInterChainConstraint(PBQPRAProblem *p, unsigned Rd,
271 // Do some Chain management
272 if (Chains.count(Ra)) {
274 DEBUG(dbgs() << "Moving acc chain from " << PrintReg(Ra, TRI) << " to "
275 << PrintReg(Rd, TRI) << '\n';);
280 DEBUG(dbgs() << "Creating new acc chain for " << PrintReg(Rd, TRI)
285 const LiveInterval &ld = LIs->getInterval(Rd);
286 for (auto r : Chains) {
291 const LiveInterval &lr = LIs->getInterval(r);
292 if (ld.overlaps(lr)) {
293 const PBQPRAProblem::AllowedSet *vRdAllowed = &p->getAllowedSet(Rd);
294 const PBQPRAProblem::AllowedSet *vRrAllowed = &p->getAllowedSet(r);
296 PBQPRAGraph &g = p->getGraph();
297 PBQPRAGraph::NodeId node1 = p->getNodeForVReg(Rd);
298 PBQPRAGraph::NodeId node2 = p->getNodeForVReg(r);
299 PBQPRAGraph::EdgeId edge = g.findEdge(node1, node2);
300 assert(edge != g.invalidEdgeId() &&
301 "PBQP error ! The edge should exist !");
303 DEBUG(dbgs() << "Refining constraint !\n";);
305 if (g.getEdgeNode1Id(edge) == node2) {
306 std::swap(node1, node2);
307 std::swap(vRdAllowed, vRrAllowed);
310 // Enforce that cost is higher with all other Chains of the same parity
311 PBQP::Matrix costs(g.getEdgeCosts(edge));
312 for (unsigned i = 0, ie = vRdAllowed->size(); i != ie; ++i) {
313 unsigned pRd = (*vRdAllowed)[i];
315 // Get the maximum cost (excluding unallocatable reg) for all other
317 PBQP::PBQPNum sameParityMax = std::numeric_limits<PBQP::PBQPNum>::min();
318 for (unsigned j = 0, je = vRrAllowed->size(); j != je; ++j) {
319 unsigned pRa = (*vRrAllowed)[j];
320 if (!haveSameParity(pRd, pRa))
321 if (costs[i + 1][j + 1] !=
322 std::numeric_limits<PBQP::PBQPNum>::infinity() &&
323 costs[i + 1][j + 1] > sameParityMax)
324 sameParityMax = costs[i + 1][j + 1];
327 // Ensure all registers with same parity have a higher cost
328 // than sameParityMax
329 for (unsigned j = 0, je = vRrAllowed->size(); j != je; ++j) {
330 unsigned pRa = (*vRrAllowed)[j];
331 if (haveSameParity(pRd, pRa))
332 if (sameParityMax > costs[i + 1][j + 1])
333 costs[i + 1][j + 1] = sameParityMax + 1.0;
336 g.setEdgeCosts(edge, costs);
341 std::unique_ptr<PBQPRAProblem>
342 A57PBQPBuilder::build(MachineFunction *MF, const LiveIntervals *LI,
343 const MachineBlockFrequencyInfo *blockInfo,
344 const RegSet &VRegs) {
345 std::unique_ptr<PBQPRAProblem> p =
346 PBQP_BUILDER::build(MF, LI, blockInfo, VRegs);
348 TRI = static_cast<const AArch64RegisterInfo *>(
349 MF->getTarget().getSubtargetImpl()->getRegisterInfo());
354 for (MachineFunction::const_iterator mbbItr = MF->begin(), mbbEnd = MF->end();
355 mbbItr != mbbEnd; ++mbbItr) {
356 const MachineBasicBlock *MBB = &*mbbItr;
357 Chains.clear(); // FIXME: really needed ? Could not work at MF level ?
359 for (MachineBasicBlock::const_iterator miItr = MBB->begin(),
361 miItr != miEnd; ++miItr) {
362 const MachineInstr *MI = &*miItr;
363 switch (MI->getOpcode()) {
364 case AArch64::FMSUBSrrr:
365 case AArch64::FMADDSrrr:
366 case AArch64::FNMSUBSrrr:
367 case AArch64::FNMADDSrrr:
368 case AArch64::FMSUBDrrr:
369 case AArch64::FMADDDrrr:
370 case AArch64::FNMSUBDrrr:
371 case AArch64::FNMADDDrrr: {
372 unsigned Rd = MI->getOperand(0).getReg();
373 unsigned Ra = MI->getOperand(3).getReg();
375 if (addIntraChainConstraint(p.get(), Rd, Ra))
376 addInterChainConstraint(p.get(), Rd, Ra);
380 case AArch64::FMLAv2f32:
381 case AArch64::FMLSv2f32: {
382 unsigned Rd = MI->getOperand(0).getReg();
383 addInterChainConstraint(p.get(), Rd, Rd);
388 // Forget Chains which have been killed
389 for (auto r : Chains) {
390 SmallVector<unsigned, 8> toDel;
391 if (MI->killsRegister(r)) {
392 DEBUG(dbgs() << "Killing chain " << PrintReg(r, TRI) << " at ";
397 while (!toDel.empty()) {
398 Chains.remove(toDel.back());
409 // Factory function used by AArch64TargetMachine to add the pass to the
411 FunctionPass *llvm::createAArch64A57PBQPRegAlloc() {
412 std::unique_ptr<PBQP_BUILDER> builder = llvm::make_unique<A57PBQPBuilder>();
413 return createPBQPRegisterAllocator(std::move(builder), nullptr);