29#define DEBUG_TYPE "wasm-reg-coloring"
38 return "WebAssembly Register Coloring";
56char WebAssemblyRegColoring::ID = 0;
58 "Minimize number of registers used",
false,
false)
61 return new WebAssemblyRegColoring();
88 auto CloseNewDVRange = [&DbgVRegToValues, &ToInsert](
SlotIndex Slot) {
89 for (
auto *
X : ToInsert) {
90 for (
const auto &
Op :
X->debug_operands()) {
91 if (
Op.isReg() &&
Op.getReg().isVirtual())
92 DbgVRegToValues[
Op.getReg()].push_back({Slot,
X});
102 for (
auto &
MBB : MF) {
105 for (
auto &
MI :
MBB) {
106 if (
MI.isDebugValue()) {
108 return MO.isReg() && MO.getReg().isVirtual();
111 }
else if (!
MI.isDebugOrPseudoInstr()) {
113 CloseNewDVRange(CurrentSlot);
122 for (
auto &Pair : DbgVRegToValues)
124 return DbgVRegToValues;
138 for (
size_t I = 0,
E = Assignments.size();
I <
E; ++
I) {
139 const auto &CoalescedIntervals = Assignments[
I];
140 if (CoalescedIntervals.empty())
148 auto RegMapIt = DbgVRegToValues.find(Reg);
149 if (RegMapIt == DbgVRegToValues.end())
152 bool LastUndefResult =
false;
153 for (
auto [Slot,
DbgValue] : RegMapIt->second) {
162 if (Slot == LastSlot) {
163 if (LastUndefResult) {
170 LastUndefResult =
false;
208 auto *SegmentIt = OtherLI->find(Slot);
209 if (SegmentIt != OtherLI->end() && SegmentIt->contains(Slot)) {
212 LastUndefResult =
true;
221bool WebAssemblyRegColoring::runOnMachineFunction(
MachineFunction &MF) {
223 dbgs() <<
"********** Register Coloring **********\n"
224 <<
"********** Function: " << MF.
getName() <<
'\n';
237 &getAnalysis<MachineBlockFrequencyInfo>();
244 unsigned NumVRegs =
MRI->getNumVirtRegs();
246 SortedIntervals.
reserve(NumVRegs);
252 for (
unsigned I = 0;
I < NumVRegs; ++
I) {
257 if (
MRI->use_empty(VReg))
273 if (
MRI->isLiveIn(
LHS->reg()) !=
MRI->isLiveIn(
RHS->reg()))
274 return MRI->isLiveIn(
LHS->reg());
276 return LHS->weight() >
RHS->weight();
278 return !
LHS->empty() &&
RHS->empty();
285 SortedIntervals.
size());
287 bool Changed =
false;
288 for (
size_t I = 0,
E = SortedIntervals.
size();
I <
E; ++
I) {
295 if (!
MRI->isLiveIn(Old))
296 for (
unsigned C : UsedColors.set_bits()) {
297 if (
MRI->getRegClass(SortedIntervals[
C]->reg()) != RC)
300 if (!OtherLI->empty() && OtherLI->overlaps(*LI))
309 Changed |= Old !=
New;
310 UsedColors.set(Color);
311 Assignments[Color].push_back(LI);
325 for (
size_t I = 0,
E = SortedIntervals.
size();
I <
E; ++
I) {
326 Register Old = SortedIntervals[
I]->reg();
329 MRI->replaceRegWith(Old, New);
unsigned const MachineRegisterInfo * MRI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file declares WebAssembly-specific per-machine-function information.
static DenseMap< Register, std::vector< std::pair< SlotIndex, MachineInstr * > > > buildVRegToDbgValueMap(MachineFunction &MF, const LiveIntervals *Liveness)
static float computeWeight(const MachineRegisterInfo *MRI, const MachineBlockFrequencyInfo *MBFI, unsigned VReg)
static void undefInvalidDbgValues(const LiveIntervals *Liveness, const ArrayRef< SmallVector< LiveInterval *, 4 > > &Assignments, DenseMap< Register, std::vector< std::pair< SlotIndex, MachineInstr * > > > &DbgVRegToValues)
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
Class recording the (high level) value of a variable.
Represent the analysis usage information of a pass.
AnalysisUsage & addPreservedID(const void *ID)
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
This class represents an Operation in the Expression.
Implements a dense probed hash-table based set.
FunctionPass class - This class is used to implement most global optimizations.
LiveInterval - This class represents the liveness of a register, or stack slot.
void setWeight(float Value)
SlotIndexes * getSlotIndexes() const
LiveInterval & getInterval(Register Reg)
static float getSpillWeight(bool isDef, bool isUse, const MachineBlockFrequencyInfo *MBFI, const MachineInstr &MI)
Calculate the spill weight to assign to a single instruction.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
bool exposesReturnsTwice() const
exposesReturnsTwice - Returns true if the function calls setjmp or any other similar functions with a...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
MachineOperand class - Representation of each machine instruction operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Wrapper class representing virtual and physical registers.
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
static unsigned virtReg2Index(Register Reg)
Convert a virtual register number to a 0-based index.
SlotIndex - An opaque wrapper around machine indexes.
SlotIndex getMBBEndIdx(unsigned Num) const
Returns the last index in the given basic block number.
SlotIndex getInstructionIndex(const MachineInstr &MI, bool IgnoreBundle=false) const
Returns the base index for the given instruction.
SlotIndex getMBBStartIdx(unsigned Num) const
Returns the first index in the given basic block number.
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
void setFrameBaseVreg(unsigned Reg)
bool isVRegStackified(unsigned VReg) const
unsigned getFrameBaseVreg() const
bool isFrameBaseVirtual() const
std::pair< iterator, bool > insert(const ValueT &V)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
char & MachineDominatorsID
MachineDominators - This pass is a machine dominators analysis pass.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
void sort(IteratorTy Start, IteratorTy End)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
FunctionPass * createWebAssemblyRegColoring()
This struct contains the mappings from the slot numbers to unnamed metadata nodes,...