//===- SpillPlacement.cpp - Optimal Spill Code Placement ------------------===// // // 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 // //===----------------------------------------------------------------------===// // // This file implements the spill code placement analysis. // // Each edge bundle corresponds to a node in a Hopfield network. Constraints on // basic blocks are weighted by the block frequency and added to become the node // bias. // // Transparent basic blocks have the variable live through, but don't care if it // is spilled or in a register. These blocks become connections in the Hopfield // network, again weighted by block frequency. // // The Hopfield network minimizes (possibly locally) its energy function: // // E = -sum_n V_n * ( B_n + sum_{n, m linked by b} V_m * F_b ) // // The energy function represents the expected spill code execution frequency, // or the cost of spilling. This is a Lyapunov function which never increases // when a node is updated. It is guaranteed to converge to a local minimum. // //===----------------------------------------------------------------------===// #include "SpillPlacement.h" #include "llvm/ADT/BitVector.h" #include "llvm/CodeGen/EdgeBundles.h" #include "llvm/CodeGen/MachineBasicBlock.h" #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" #include "llvm/CodeGen/MachineFunction.h" #include "llvm/CodeGen/Passes.h" #include "llvm/InitializePasses.h" #include "llvm/Pass.h" #include <algorithm> #include <cassert> #include <cstdint> #include <utility> usingnamespacellvm; #define DEBUG_TYPE … char SpillPlacement::ID = …; char &llvm::SpillPlacementID = …; INITIALIZE_PASS_BEGIN(SpillPlacement, DEBUG_TYPE, "Spill Code Placement Analysis", true, true) INITIALIZE_PASS_DEPENDENCY(EdgeBundles) INITIALIZE_PASS_END(SpillPlacement, DEBUG_TYPE, "Spill Code Placement Analysis", true, true) void SpillPlacement::getAnalysisUsage(AnalysisUsage &AU) const { … } /// Node - Each edge bundle corresponds to a Hopfield node. /// /// The node contains precomputed frequency data that only depends on the CFG, /// but Bias and Links are computed each time placeSpills is called. /// /// The node Value is positive when the variable should be in a register. The /// value can change when linked nodes change, but convergence is very fast /// because all weights are positive. struct SpillPlacement::Node { … }; bool SpillPlacement::runOnMachineFunction(MachineFunction &mf) { … } void SpillPlacement::releaseMemory() { … } /// activate - mark node n as active if it wasn't already. void SpillPlacement::activate(unsigned n) { … } /// Set the threshold for a given entry frequency. /// /// Set the threshold relative to \c Entry. Since the threshold is used as a /// bound on the open interval (-Threshold;Threshold), 1 is the minimum /// threshold. void SpillPlacement::setThreshold(BlockFrequency Entry) { … } /// addConstraints - Compute node biases and weights from a set of constraints. /// Set a bit in NodeMask for each active node. void SpillPlacement::addConstraints(ArrayRef<BlockConstraint> LiveBlocks) { … } /// addPrefSpill - Same as addConstraints(PrefSpill) void SpillPlacement::addPrefSpill(ArrayRef<unsigned> Blocks, bool Strong) { … } void SpillPlacement::addLinks(ArrayRef<unsigned> Links) { … } bool SpillPlacement::scanActiveBundles() { … } bool SpillPlacement::update(unsigned n) { … } /// iterate - Repeatedly update the Hopfield nodes until stability or the /// maximum number of iterations is reached. void SpillPlacement::iterate() { … } void SpillPlacement::prepare(BitVector &RegBundles) { … } bool SpillPlacement::finish() { … } void SpillPlacement::BlockConstraint::print(raw_ostream &OS) const { … } void SpillPlacement::BlockConstraint::dump() const { … }