33#define DEBUG_TYPE "wasm-reg-coloring"
42 return "WebAssembly Register Coloring";
58char WebAssemblyRegColoringLegacy::ID = 0;
60 "Minimize number of registers used",
false,
false)
63 return new WebAssemblyRegColoringLegacy();
90 auto CloseNewDVRange = [&DbgVRegToValues, &ToInsert](
SlotIndex Slot) {
91 for (
auto *
X : ToInsert) {
92 for (
const auto &
Op :
X->debug_operands()) {
93 if (
Op.isReg() &&
Op.getReg().isVirtual())
94 DbgVRegToValues[
Op.getReg()].push_back({Slot,
X});
104 for (
auto &
MBB : MF) {
107 for (
auto &
MI :
MBB) {
108 if (
MI.isDebugValue()) {
110 return MO.isReg() && MO.getReg().isVirtual();
113 }
else if (!
MI.isDebugOrPseudoInstr()) {
115 CloseNewDVRange(CurrentSlot);
124 for (
auto &Pair : DbgVRegToValues)
126 return DbgVRegToValues;
140 for (
const auto &CoalescedIntervals : Assignments) {
141 if (CoalescedIntervals.empty())
149 auto RegMapIt = DbgVRegToValues.find(
Reg);
150 if (RegMapIt == DbgVRegToValues.end())
153 bool LastUndefResult =
false;
154 for (
auto [Slot,
DbgValue] : RegMapIt->second) {
163 if (Slot == LastSlot) {
164 if (LastUndefResult) {
171 LastUndefResult =
false;
209 auto *SegmentIt = OtherLI->find(Slot);
210 if (SegmentIt != OtherLI->end() && SegmentIt->contains(Slot)) {
213 LastUndefResult =
true;
225 dbgs() <<
"********** Register Coloring **********\n"
226 <<
"********** Function: " << MF.
getName() <<
'\n';
236 unsigned NumVRegs = MRI->getNumVirtRegs();
238 SortedIntervals.
reserve(NumVRegs);
244 for (
unsigned I = 0;
I < NumVRegs; ++
I) {
249 if (MRI->use_empty(VReg))
265 if (MRI->isLiveIn(
LHS->reg()) != MRI->isLiveIn(
RHS->reg()))
266 return MRI->isLiveIn(
LHS->reg());
267 if (
LHS->weight() !=
RHS->weight())
268 return LHS->weight() >
RHS->weight();
269 if (
LHS->empty() ||
RHS->empty())
270 return !
LHS->empty() &&
RHS->empty();
277 SortedIntervals.
size());
280 for (
size_t I = 0,
E = SortedIntervals.
size();
I <
E; ++
I) {
287 if (!MRI->isLiveIn(Old))
288 for (
unsigned C : UsedColors.
set_bits()) {
289 if (MRI->getRegClass(SortedIntervals[
C]->reg()) != RC)
292 if (!OtherLI->empty() && OtherLI->overlaps(*LI))
299 Register New = SortedIntervals[Color]->reg();
302 UsedColors.
set(Color);
317 for (
size_t I = 0,
E = SortedIntervals.
size();
I <
E; ++
I) {
318 Register Old = SortedIntervals[
I]->reg();
321 MRI->replaceRegWith(Old, New);
326bool WebAssemblyRegColoringLegacy::runOnMachineFunction(
MachineFunction &MF) {
334 LiveIntervals *Liveness = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
335 const MachineBlockFrequencyInfo *MBFI =
336 &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This file declares WebAssembly-specific per-machine-function information.
static bool regColoring(MachineFunction &MF, LiveIntervals *Liveness, const MachineBlockFrequencyInfo *MBFI)
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.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
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.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represent a constant reference to an array (0 or more elements consecutively in memory),...
BitVector & set()
Set all bits in the bitvector.
iterator_range< const_set_bits_iterator > set_bits() const
Represents analyses that only rely on functions' control flow.
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.
LLVM_ABI void dump() const
void setWeight(float Value)
SlotIndexes * getSlotIndexes() const
static LLVM_ABI 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.
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,...
iterator_range< reg_nodbg_iterator > reg_nodbg_operands(Register Reg) const
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Wrapper class representing virtual and physical registers.
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
SlotIndex - An opaque wrapper around machine indexes.
SlotIndex getMBBEndIdx(unsigned Num) const
Returns the index past the last valid index in the given basic block.
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.
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)
unsigned getFrameBaseVreg() const
bool isVRegStackified(Register VReg) const
bool isFrameBaseVirtual() const
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
std::pair< iterator, bool > insert(const ValueT &V)
Pass manager infrastructure for declaring and invalidating analyses.
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.
LLVM_ABI char & MachineDominatorsID
MachineDominators - This pass is a machine dominators analysis pass.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
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)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
FunctionPass * createWebAssemblyRegColoringLegacyPass()
DWARFExpression::Operation Op
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.
MCRegisterClass TargetRegisterClass
This struct contains the mappings from the slot numbers to unnamed metadata nodes,...