//===-- WebAssemblyRegColoring.cpp - Register coloring --------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// /// /// \file /// This file implements a virtual register coloring pass. /// /// WebAssembly doesn't have a fixed number of registers, but it is still /// desirable to minimize the total number of registers used in each function. /// /// This code is modeled after lib/CodeGen/StackSlotColoring.cpp. /// //===----------------------------------------------------------------------===// #include "WebAssembly.h" #include "WebAssemblyMachineFunctionInfo.h" #include "llvm/CodeGen/LiveIntervals.h" #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" #include "llvm/CodeGen/MachineRegisterInfo.h" #include "llvm/CodeGen/Passes.h" #include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" usingnamespacellvm; #define DEBUG_TYPE … namespace { class WebAssemblyRegColoring final : public MachineFunctionPass { … }; } // end anonymous namespace char WebAssemblyRegColoring::ID = …; INITIALIZE_PASS(…) FunctionPass *llvm::createWebAssemblyRegColoring() { … } // Compute the total spill weight for VReg. static float computeWeight(const MachineRegisterInfo *MRI, const MachineBlockFrequencyInfo *MBFI, unsigned VReg) { … } // Create a map of "Register -> vector of <SlotIndex, DBG_VALUE>". // The SlotIndex is the slot index of the next non-debug instruction or the end // of a BB, because DBG_VALUE's don't have slot index themselves. // Adapted from RegisterCoalescer::buildVRegToDbgValueMap. static DenseMap<Register, std::vector<std::pair<SlotIndex, MachineInstr *>>> buildVRegToDbgValueMap(MachineFunction &MF, const LiveIntervals *Liveness) { … } // After register coalescing, some DBG_VALUEs will be invalid. Set them undef. // This function has to run before the actual coalescing, i.e., the register // changes. static void undefInvalidDbgValues( const LiveIntervals *Liveness, ArrayRef<SmallVector<LiveInterval *, 4>> Assignments, DenseMap<Register, std::vector<std::pair<SlotIndex, MachineInstr *>>> &DbgVRegToValues) { … } bool WebAssemblyRegColoring::runOnMachineFunction(MachineFunction &MF) { … }