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 (
const auto &CoalescedIntervals : Assignments) {
139 if (CoalescedIntervals.empty())
147 auto RegMapIt = DbgVRegToValues.find(Reg);
148 if (RegMapIt == DbgVRegToValues.end())
151 bool LastUndefResult =
false;
152 for (
auto [Slot,
DbgValue] : RegMapIt->second) {
161 if (Slot == LastSlot) {
162 if (LastUndefResult) {
169 LastUndefResult =
false;
207 auto *SegmentIt = OtherLI->find(Slot);
208 if (SegmentIt != OtherLI->end() && SegmentIt->contains(Slot)) {
211 LastUndefResult =
true;
220bool WebAssemblyRegColoring::runOnMachineFunction(
MachineFunction &MF) {
222 dbgs() <<
"********** Register Coloring **********\n"
223 <<
"********** Function: " << MF.
getName() <<
'\n';
234 LiveIntervals *Liveness = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
236 &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
243 unsigned NumVRegs =
MRI->getNumVirtRegs();
245 SortedIntervals.
reserve(NumVRegs);
251 for (
unsigned I = 0;
I < NumVRegs; ++
I) {
256 if (
MRI->use_empty(VReg))
272 if (
MRI->isLiveIn(
LHS->reg()) !=
MRI->isLiveIn(
RHS->reg()))
273 return MRI->isLiveIn(
LHS->reg());
275 return LHS->weight() >
RHS->weight();
277 return !
LHS->empty() &&
RHS->empty();
284 SortedIntervals.
size());
286 bool Changed =
false;
287 for (
size_t I = 0, E = SortedIntervals.
size();
I < E; ++
I) {
294 if (!
MRI->isLiveIn(Old))
295 for (
unsigned C : UsedColors.set_bits()) {
296 if (
MRI->getRegClass(SortedIntervals[
C]->reg()) != RC)
299 if (!OtherLI->empty() && OtherLI->overlaps(*LI))
308 Changed |= Old !=
New;
309 UsedColors.set(Color);
310 Assignments[Color].push_back(LI);
324 for (
size_t I = 0, E = SortedIntervals.
size();
I < E; ++
I) {
325 Register Old = SortedIntervals[
I]->reg();
328 MRI->replaceRegWith(Old, New);
unsigned const MachineRegisterInfo * MRI
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 void undefInvalidDbgValues(const LiveIntervals *Liveness, ArrayRef< SmallVector< LiveInterval *, 4 > > Assignments, DenseMap< Register, std::vector< std::pair< SlotIndex, MachineInstr * > > > &DbgVRegToValues)
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)
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
static float getSpillWeight(bool isDef, bool isUse, const MachineBlockFrequencyInfo *MBFI, const MachineInstr &MI, ProfileSummaryInfo *PSI=nullptr)
Calculate the spill weight to assign to a single instruction.
LiveInterval & getInterval(Register Reg)
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)
@ C
The default llvm calling convention, compatible with C.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
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,...