33#define DEBUG_TYPE "wasm-reg-coloring"
42 return "WebAssembly Register Coloring";
56char WebAssemblyRegColoringLegacy::ID = 0;
58 "Minimize number of registers used",
false,
false)
61 return new WebAssemblyRegColoringLegacy();
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;
223 dbgs() <<
"********** Register Coloring **********\n"
224 <<
"********** Function: " << MF.
getName() <<
'\n';
234 unsigned NumVRegs = MRI->getNumVirtRegs();
236 SortedIntervals.
reserve(NumVRegs);
242 for (
unsigned I = 0;
I < NumVRegs; ++
I) {
247 if (MRI->use_empty(VReg))
263 if (MRI->isLiveIn(
LHS->reg()) != MRI->isLiveIn(
RHS->reg()))
264 return MRI->isLiveIn(
LHS->reg());
265 if (
LHS->weight() !=
RHS->weight())
266 return LHS->weight() >
RHS->weight();
267 if (
LHS->empty() ||
RHS->empty())
268 return !
LHS->empty() &&
RHS->empty();
275 SortedIntervals.
size());
278 for (
size_t I = 0,
E = SortedIntervals.
size();
I <
E; ++
I) {
285 if (!MRI->isLiveIn(Old))
286 for (
unsigned C : UsedColors.
set_bits()) {
287 if (MRI->getRegClass(SortedIntervals[
C]->reg()) != RC)
290 if (!OtherLI->empty() && OtherLI->overlaps(*LI))
297 Register New = SortedIntervals[Color]->reg();
300 UsedColors.
set(Color);
315 for (
size_t I = 0,
E = SortedIntervals.
size();
I <
E; ++
I) {
316 Register Old = SortedIntervals[
I]->reg();
319 MRI->replaceRegWith(Old, New);
324bool WebAssemblyRegColoringLegacy::runOnMachineFunction(
MachineFunction &MF) {
332 LiveIntervals *Liveness = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
333 const MachineBlockFrequencyInfo *MBFI =
334 &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
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 & addRequired()
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.
This is an optimization pass for GlobalISel generic memory operations.
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,...