diff options
Diffstat (limited to 'llvm/lib/CodeGen/InlineSpiller.cpp')
-rw-r--r-- | llvm/lib/CodeGen/InlineSpiller.cpp | 83 |
1 files changed, 45 insertions, 38 deletions
diff --git a/llvm/lib/CodeGen/InlineSpiller.cpp b/llvm/lib/CodeGen/InlineSpiller.cpp index eda4f74c787..fc36bd5999b 100644 --- a/llvm/lib/CodeGen/InlineSpiller.cpp +++ b/llvm/lib/CodeGen/InlineSpiller.cpp @@ -1,4 +1,4 @@ -//===-------- InlineSpiller.cpp - Insert spills and restores inline -------===// +//===- InlineSpiller.cpp - Insert spills and restores inline --------------===// // // The LLVM Compiler Infrastructure // @@ -12,31 +12,52 @@ // //===----------------------------------------------------------------------===// +#include "LiveRangeCalc.h" #include "Spiller.h" #include "SplitKit.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/MapVector.h" +#include "llvm/ADT/None.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SetVector.h" +#include "llvm/ADT/SmallPtrSet.h" +#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/Statistic.h" -#include "llvm/ADT/TinyPtrVector.h" #include "llvm/Analysis/AliasAnalysis.h" +#include "llvm/CodeGen/LiveInterval.h" #include "llvm/CodeGen/LiveIntervalAnalysis.h" #include "llvm/CodeGen/LiveRangeEdit.h" #include "llvm/CodeGen/LiveStackAnalysis.h" +#include "llvm/CodeGen/MachineBasicBlock.h" #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" -#include "llvm/CodeGen/MachineBranchProbabilityInfo.h" #include "llvm/CodeGen/MachineDominators.h" -#include "llvm/CodeGen/MachineFrameInfo.h" #include "llvm/CodeGen/MachineFunction.h" +#include "llvm/CodeGen/MachineFunctionPass.h" +#include "llvm/CodeGen/MachineInstr.h" #include "llvm/CodeGen/MachineInstrBuilder.h" #include "llvm/CodeGen/MachineInstrBundle.h" #include "llvm/CodeGen/MachineLoopInfo.h" +#include "llvm/CodeGen/MachineOperand.h" #include "llvm/CodeGen/MachineRegisterInfo.h" +#include "llvm/CodeGen/SlotIndexes.h" #include "llvm/CodeGen/VirtRegMap.h" -#include "llvm/IR/DebugInfo.h" +#include "llvm/Support/BlockFrequency.h" +#include "llvm/Support/BranchProbability.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Compiler.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Target/TargetInstrInfo.h" +#include "llvm/Target/TargetOpcodes.h" +#include "llvm/Target/TargetRegisterInfo.h" +#include "llvm/Target/TargetSubtargetInfo.h" +#include <cassert> +#include <iterator> +#include <tuple> +#include <utility> +#include <vector> using namespace llvm; @@ -56,6 +77,7 @@ static cl::opt<bool> DisableHoisting("disable-spill-hoist", cl::Hidden, cl::desc("Disable inline spill hoisting")); namespace { + class HoistSpillHelper : private LiveRangeEdit::Delegate { MachineFunction &MF; LiveIntervals &LIS; @@ -64,7 +86,6 @@ class HoistSpillHelper : private LiveRangeEdit::Delegate { MachineDominatorTree &MDT; MachineLoopInfo &Loops; VirtRegMap &VRM; - MachineFrameInfo &MFI; MachineRegisterInfo &MRI; const TargetInstrInfo &TII; const TargetRegisterInfo &TRI; @@ -74,11 +95,12 @@ class HoistSpillHelper : private LiveRangeEdit::Delegate { // Map from StackSlot to its original register. DenseMap<int, unsigned> StackSlotToReg; + // Map from pair of (StackSlot and Original VNI) to a set of spills which // have the same stackslot and have equal values defined by Original VNI. // These spills are mergeable and are hoist candiates. - typedef MapVector<std::pair<int, VNInfo *>, SmallPtrSet<MachineInstr *, 16>> - MergeableSpillsMap; + using MergeableSpillsMap = + MapVector<std::pair<int, VNInfo *>, SmallPtrSet<MachineInstr *, 16>>; MergeableSpillsMap MergeableSpills; /// This is the map from original register to a set containing all its @@ -114,8 +136,7 @@ public: AA(&pass.getAnalysis<AAResultsWrapperPass>().getAAResults()), MDT(pass.getAnalysis<MachineDominatorTree>()), Loops(pass.getAnalysis<MachineLoopInfo>()), VRM(vrm), - MFI(mf.getFrameInfo()), MRI(mf.getRegInfo()), - TII(*mf.getSubtarget().getInstrInfo()), + MRI(mf.getRegInfo()), TII(*mf.getSubtarget().getInstrInfo()), TRI(*mf.getSubtarget().getRegisterInfo()), MBFI(pass.getAnalysis<MachineBlockFrequencyInfo>()), IPA(LIS, mf.getNumBlockIDs()) {} @@ -135,7 +156,6 @@ class InlineSpiller : public Spiller { MachineDominatorTree &MDT; MachineLoopInfo &Loops; VirtRegMap &VRM; - MachineFrameInfo &MFI; MachineRegisterInfo &MRI; const TargetInstrInfo &TII; const TargetRegisterInfo &TRI; @@ -163,7 +183,7 @@ class InlineSpiller : public Spiller { // Object records spills information and does the hoisting. HoistSpillHelper HSpiller; - ~InlineSpiller() override {} + ~InlineSpiller() override = default; public: InlineSpiller(MachineFunctionPass &pass, MachineFunction &mf, VirtRegMap &vrm) @@ -172,8 +192,7 @@ public: AA(&pass.getAnalysis<AAResultsWrapperPass>().getAAResults()), MDT(pass.getAnalysis<MachineDominatorTree>()), Loops(pass.getAnalysis<MachineLoopInfo>()), VRM(vrm), - MFI(mf.getFrameInfo()), MRI(mf.getRegInfo()), - TII(*mf.getSubtarget().getInstrInfo()), + MRI(mf.getRegInfo()), TII(*mf.getSubtarget().getInstrInfo()), TRI(*mf.getSubtarget().getRegisterInfo()), MBFI(pass.getAnalysis<MachineBlockFrequencyInfo>()), HSpiller(pass, mf, vrm) {} @@ -196,7 +215,7 @@ private: void reMaterializeAll(); bool coalesceStackAccess(MachineInstr *MI, unsigned Reg); - bool foldMemoryOperand(ArrayRef<std::pair<MachineInstr*, unsigned> >, + bool foldMemoryOperand(ArrayRef<std::pair<MachineInstr *, unsigned>>, MachineInstr *LoadMI = nullptr); void insertReload(unsigned VReg, SlotIndex, MachineBasicBlock::iterator MI); void insertSpill(unsigned VReg, bool isKill, MachineBasicBlock::iterator MI); @@ -204,19 +223,17 @@ private: void spillAroundUses(unsigned Reg); void spillAll(); }; -} -namespace llvm { +} // end anonymous namespace -Spiller::~Spiller() { } -void Spiller::anchor() { } +Spiller::~Spiller() = default; -Spiller *createInlineSpiller(MachineFunctionPass &pass, - MachineFunction &mf, - VirtRegMap &vrm) { - return new InlineSpiller(pass, mf, vrm); -} +void Spiller::anchor() {} +Spiller *llvm::createInlineSpiller(MachineFunctionPass &pass, + MachineFunction &mf, + VirtRegMap &vrm) { + return new InlineSpiller(pass, mf, vrm); } //===----------------------------------------------------------------------===// @@ -457,7 +474,6 @@ void InlineSpiller::eliminateRedundantSpills(LiveInterval &SLI, VNInfo *VNI) { } while (!WorkList.empty()); } - //===----------------------------------------------------------------------===// // Rematerialization //===----------------------------------------------------------------------===// @@ -496,7 +512,6 @@ void InlineSpiller::markValueUsed(LiveInterval *LI, VNInfo *VNI) { /// reMaterializeFor - Attempt to rematerialize before MI instead of reloading. bool InlineSpiller::reMaterializeFor(LiveInterval &VirtReg, MachineInstr &MI) { - // Analyze instruction SmallVector<std::pair<MachineInstr *, unsigned>, 8> Ops; MIBundleOperands::VirtRegInfo RI = @@ -654,7 +669,6 @@ void InlineSpiller::reMaterializeAll() { DEBUG(dbgs() << RegsToSpill.size() << " registers to spill after remat.\n"); } - //===----------------------------------------------------------------------===// // Spilling //===----------------------------------------------------------------------===// @@ -731,7 +745,7 @@ static void dumpMachineInstrRangeWithSlotIndex(MachineBasicBlock::iterator B, /// @param LoadMI Load instruction to use instead of stack slot when non-null. /// @return True on success. bool InlineSpiller:: -foldMemoryOperand(ArrayRef<std::pair<MachineInstr*, unsigned> > Ops, +foldMemoryOperand(ArrayRef<std::pair<MachineInstr *, unsigned>> Ops, MachineInstr *LoadMI) { if (Ops.empty()) return false; @@ -1076,11 +1090,9 @@ void InlineSpiller::spill(LiveRangeEdit &edit) { } /// Optimizations after all the reg selections and spills are done. -/// void InlineSpiller::postOptimization() { HSpiller.hoistAllSpills(); } /// When a spill is inserted, add the spill to MergeableSpills map. -/// void HoistSpillHelper::addToMergeableSpills(MachineInstr &Spill, int StackSlot, unsigned Original) { StackSlotToReg[StackSlot] = Original; @@ -1092,7 +1104,6 @@ void HoistSpillHelper::addToMergeableSpills(MachineInstr &Spill, int StackSlot, /// When a spill is removed, remove the spill from MergeableSpills map. /// Return true if the spill is removed successfully. -/// bool HoistSpillHelper::rmFromMergeableSpills(MachineInstr &Spill, int StackSlot) { int Original = StackSlotToReg[StackSlot]; @@ -1106,7 +1117,6 @@ bool HoistSpillHelper::rmFromMergeableSpills(MachineInstr &Spill, /// Check BB to see if it is a possible target BB to place a hoisted spill, /// i.e., there should be a living sibling of OrigReg at the insert point. -/// bool HoistSpillHelper::isSpillCandBB(unsigned OrigReg, VNInfo &OrigVNI, MachineBasicBlock &BB, unsigned &LiveReg) { SlotIndex Idx; @@ -1133,7 +1143,6 @@ bool HoistSpillHelper::isSpillCandBB(unsigned OrigReg, VNInfo &OrigVNI, /// Remove redundant spills in the same BB. Save those redundant spills in /// SpillsToRm, and save the spill to keep and its BB in SpillBBToSpill map. -/// void HoistSpillHelper::rmRedundantSpills( SmallPtrSet<MachineInstr *, 16> &Spills, SmallVectorImpl<MachineInstr *> &SpillsToRm, @@ -1166,7 +1175,6 @@ void HoistSpillHelper::rmRedundantSpills( /// time. \p SpillBBToSpill will be populated as part of the process and /// maps a basic block to the first store occurring in the basic block. /// \post SpillsToRm.union(Spills\@post) == Spills\@pre -/// void HoistSpillHelper::getVisitOrders( MachineBasicBlock *Root, SmallPtrSet<MachineInstr *, 16> &Spills, SmallVectorImpl<MachineDomTreeNode *> &Orders, @@ -1254,7 +1262,6 @@ void HoistSpillHelper::getVisitOrders( /// Try to hoist spills according to BB hotness. The spills to removed will /// be saved in \p SpillsToRm. The spills to be inserted will be saved in /// \p SpillsToIns. -/// void HoistSpillHelper::runHoistSpills( unsigned OrigReg, VNInfo &OrigVNI, SmallPtrSet<MachineInstr *, 16> &Spills, SmallVectorImpl<MachineInstr *> &SpillsToRm, @@ -1280,9 +1287,10 @@ void HoistSpillHelper::runHoistSpills( // nodes set and the cost of all the spills inside those nodes. // The nodes set are the locations where spills are to be inserted // in the subtree of current node. - typedef std::pair<SmallPtrSet<MachineDomTreeNode *, 16>, BlockFrequency> - NodesCostPair; + using NodesCostPair = + std::pair<SmallPtrSet<MachineDomTreeNode *, 16>, BlockFrequency>; DenseMap<MachineDomTreeNode *, NodesCostPair> SpillsInSubTreeMap; + // Iterate Orders set in reverse order, which will be a bottom-up order // in the dominator tree. Once we visit a dom tree node, we know its // children have already been visited and the spill locations in the @@ -1391,7 +1399,6 @@ void HoistSpillHelper::runHoistSpills( /// bottom-up order, and for each node, we will decide whether to hoist spills /// inside its subtree to that node. In this way, we can get benefit locally /// even if hoisting all the equal spills to one cold place is impossible. -/// void HoistSpillHelper::hoistAllSpills() { SmallVector<unsigned, 4> NewVRegs; LiveRangeEdit Edit(nullptr, NewVRegs, MF, LIS, &VRM, this); |