LLVM 24.0.0git
AllocationOrder.cpp
Go to the documentation of this file.
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, anti-hints, and target hooks. The AllocationOrder class encapsulates
13// all of that.
14//
15//===----------------------------------------------------------------------===//
16
17#include "AllocationOrder.h"
18#include "llvm/ADT/BitVector.h"
23#include "llvm/Support/Debug.h"
25
26using namespace llvm;
27
28#define DEBUG_TYPE "regalloc"
29
30static void getBitVecRegAntiHints(Register VReg, BitVector &AntiHintedRegUnits,
31 const VirtRegMap &VRM,
32 const MachineRegisterInfo &MRI,
33 const TargetRegisterInfo &TRI) {
34 assert(VReg.isVirtual() && "Anti-hints are only for virtual registers");
35 for (Register AntiHintVReg : MRI.getRegAllocationAntiHints(VReg)) {
36 // Check if the anti-hinted register has been allocated.
37 if (!VRM.hasPhys(AntiHintVReg))
38 continue;
39 // Delay until first allocated anti-hinted register so unused cases keep
40 // default empty BitVector.
41 if (AntiHintedRegUnits.empty())
42 AntiHintedRegUnits.resize(TRI.getNumRegUnits());
43 for (MCRegUnit Unit : TRI.regunits(VRM.getPhys(AntiHintVReg)))
44 AntiHintedRegUnits.set(static_cast<unsigned>(Unit));
45 }
46}
47
48// Compare VirtRegMap::getRegAllocPref().
50 const RegisterClassInfo &RegClassInfo,
51 const LiveRegMatrix *Matrix) {
52 const MachineFunction &MF = VRM.getMachineFunction();
54 const MachineRegisterInfo &MRI = MF.getRegInfo();
55 auto Order = RegClassInfo.getOrder(MF.getRegInfo().getRegClass(VirtReg));
56
57 // HintsAndCustomOrder holds Hints first followed by the custom order if the
58 // anti-hints reorders it.
59 SmallVector<MCPhysReg, 16> HintsAndCustomOrder;
60
61 // Get Hints.
62 bool HardHints = TRI->getRegAllocationHints(
63 VirtReg, Order, HintsAndCustomOrder, MF, &VRM, Matrix);
64 const int NumHints = static_cast<int>(HintsAndCustomOrder.size());
65
66 // HintsAndCustomOrder only holds Hints (custom order is not added yet).
68 if (NumHints) {
69 dbgs() << "hints:";
70 for (MCPhysReg Hint : HintsAndCustomOrder)
71 dbgs() << ' ' << printReg(Hint, TRI);
72 dbgs() << '\n';
73 }
74 });
75
76 // Get anti-hints.
77 BitVector AntiHintedRegUnits;
78 getBitVecRegAntiHints(VirtReg, AntiHintedRegUnits, VRM, MRI, *TRI);
79
81 if (AntiHintedRegUnits.any()) {
82 dbgs() << "anti-hints:";
83 for (Register AntiHintVReg : MRI.getRegAllocationAntiHints(VirtReg)) {
84 if (!VRM.hasPhys(AntiHintVReg))
85 continue;
86 dbgs() << ' ' << printReg(VRM.getPhys(AntiHintVReg), TRI);
87 }
88 dbgs() << '\n';
89 }
90 });
91
92 if (AntiHintedRegUnits.any())
93 TRI->applyRegAllocationAntiHints(VirtReg, Order, HintsAndCustomOrder,
94 NumHints, AntiHintedRegUnits, MF, Matrix,
95 &RegClassInfo);
96
97 // Create allocation order object.
98 AllocationOrder AO(std::move(HintsAndCustomOrder), NumHints, Order,
99 HardHints);
100
101 assert(all_of(AO.hints(),
102 [&](MCPhysReg Hint) { return is_contained(AO.Order, Hint); }) &&
103 "Target hint is outside allocation order.");
104 return AO;
105}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static void getBitVecRegAntiHints(Register VReg, BitVector &AntiHintedRegUnits, const VirtRegMap &VRM, const MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI)
This file implements the BitVector class.
Live Register Matrix
Register const TargetRegisterInfo * TRI
#define LLVM_DEBUG(...)
Definition Debug.h:119
AllocationOrder(SmallVector< MCPhysReg, 16 > &&HintsAndCustomOrder, int NumHints, ArrayRef< MCPhysReg > Order, bool HardHints)
Create an AllocationOrder from HintsAndCustomOrder that contains NumHints Hints optionally followed b...
static AllocationOrder create(Register VirtReg, const VirtRegMap &VRM, const RegisterClassInfo &RegClassInfo, const LiveRegMatrix *Matrix)
Create a new AllocationOrder for VirtReg.
void resize(unsigned N, bool t=false)
Grow or shrink the bitvector.
Definition BitVector.h:355
BitVector & set()
Set all bits in the bitvector.
Definition BitVector.h:366
bool empty() const
Returns whether there are no bits in this bitvector.
Definition BitVector.h:175
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
ArrayRef< Register > getRegAllocationAntiHints(Register VReg) const
Return the vector of anti-hints for VReg.
ArrayRef< MCPhysReg > getOrder(const TargetRegisterClass *RC) const
getOrder - Returns the preferred allocation order for RC.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
MachineFunction & getMachineFunction() const
Definition VirtRegMap.h:75
MCRegister getPhys(Register virtReg) const
returns the physical register mapped to the specified virtual register
Definition VirtRegMap.h:91
bool hasPhys(Register virtReg) const
returns true if the specified virtual register is mapped to a physical register
Definition VirtRegMap.h:87
const TargetRegisterInfo & getTargetRegInfo() const
Definition VirtRegMap.h:81
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1755
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.