1 //===-- llvm/CodeGen/AllocationOrder.cpp - Allocation Order ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements an allocation order for virtual registers.
10 //
11 // The preferred allocation order for a virtual register depends on allocation
12 // hints and target hooks. The AllocationOrder class encapsulates all of that.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "AllocationOrder.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
19 #include "llvm/CodeGen/RegisterClassInfo.h"
20 #include "llvm/CodeGen/VirtRegMap.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/raw_ostream.h"
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "regalloc"
28 
29 // Compare VirtRegMap::getRegAllocPref().
30 AllocationOrder AllocationOrder::create(unsigned VirtReg, const VirtRegMap &VRM,
31                                         const RegisterClassInfo &RegClassInfo,
32                                         const LiveRegMatrix *Matrix) {
33   const MachineFunction &MF = VRM.getMachineFunction();
34   const TargetRegisterInfo *TRI = &VRM.getTargetRegInfo();
35   auto Order = RegClassInfo.getOrder(MF.getRegInfo().getRegClass(VirtReg));
36   SmallVector<MCPhysReg, 16> Hints;
37   bool HardHints =
38       TRI->getRegAllocationHints(VirtReg, Order, Hints, MF, &VRM, Matrix);
39 
40   LLVM_DEBUG({
41     if (!Hints.empty()) {
42       dbgs() << "hints:";
43       for (unsigned I = 0, E = Hints.size(); I != E; ++I)
44         dbgs() << ' ' << printReg(Hints[I], TRI);
45       dbgs() << '\n';
46     }
47   });
48 #ifndef NDEBUG
49   for (unsigned I = 0, E = Hints.size(); I != E; ++I)
50     assert(is_contained(Order, Hints[I]) &&
51            "Target hint is outside allocation order.");
52 #endif
53   return AllocationOrder(std::move(Hints), Order, HardHints);
54 }
55