RegAllocGreedy.cpp revision 9f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5
1cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//===-- RegAllocGreedy.cpp - greedy register allocator --------------------===// 2cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen// 3cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen// The LLVM Compiler Infrastructure 4cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen// 5cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen// This file is distributed under the University of Illinois Open Source 6cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen// License. See LICENSE.TXT for details. 7cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen// 8cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//===----------------------------------------------------------------------===// 9cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen// 10cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen// This file defines the RAGreedy function pass for register allocation in 11cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen// optimized builds. 12cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen// 13cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//===----------------------------------------------------------------------===// 14cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 15cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#define DEBUG_TYPE "regalloc" 16dd479e9769eceee9fcc34e2173376024f3aa3c5fJakob Stoklund Olesen#include "AllocationOrder.h" 175907d863659eb972ebb2afe07bc863a4c616f0efJakob Stoklund Olesen#include "InterferenceCache.h" 18cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen#include "LiveDebugVariables.h" 19f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen#include "LiveRangeEdit.h" 20cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "RegAllocBase.h" 21cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "Spiller.h" 22b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen#include "SpillPlacement.h" 23d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen#include "SplitKit.h" 24cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "VirtRegMap.h" 250db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen#include "llvm/ADT/Statistic.h" 26cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Analysis/AliasAnalysis.h" 27cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Function.h" 28cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/PassAnalysisSupport.h" 29cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/CalcSpillWeights.h" 30b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen#include "llvm/CodeGen/EdgeBundles.h" 31cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/LiveIntervalAnalysis.h" 32cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/LiveStackAnalysis.h" 33f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen#include "llvm/CodeGen/MachineDominators.h" 34cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/MachineFunctionPass.h" 35cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/MachineLoopInfo.h" 36d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen#include "llvm/CodeGen/MachineLoopRanges.h" 37cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/MachineRegisterInfo.h" 38cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/Passes.h" 39cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/RegAllocRegistry.h" 40cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/RegisterCoalescer.h" 41cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Target/TargetOptions.h" 42cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Support/Debug.h" 43cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Support/ErrorHandling.h" 44cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Support/raw_ostream.h" 45533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen#include "llvm/Support/Timer.h" 46cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 4798d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen#include <queue> 4898d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen 49cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenusing namespace llvm; 50cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 510db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund OlesenSTATISTIC(NumGlobalSplits, "Number of split global live ranges"); 520db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund OlesenSTATISTIC(NumLocalSplits, "Number of split local live ranges"); 530db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund OlesenSTATISTIC(NumEvicted, "Number of interferences evicted"); 540db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen 55cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenstatic RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator", 56cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen createGreedyRegisterAllocator); 57cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 58cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesennamespace { 5992a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesenclass RAGreedy : public MachineFunctionPass, 6092a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen public RegAllocBase, 6192a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen private LiveRangeEdit::Delegate { 6292a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen 63cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen // context 64cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen MachineFunction *MF; 65cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen BitVector ReservedRegs; 66cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 67cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen // analyses 68b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen SlotIndexes *Indexes; 69cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen LiveStacks *LS; 70f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen MachineDominatorTree *DomTree; 71d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen MachineLoopInfo *Loops; 72d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen MachineLoopRanges *LoopRanges; 73b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen EdgeBundles *Bundles; 74b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen SpillPlacement *SpillPlacer; 75f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen 76cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen // state 77cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen std::auto_ptr<Spiller> SpillerInstance; 7898d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen std::priority_queue<std::pair<unsigned, unsigned> > Queue; 7922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen 8022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // Live ranges pass through a number of stages as we try to allocate them. 8122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // Some of the stages may also create new live ranges: 8222a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // 8322a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // - Region splitting. 8422a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // - Per-block splitting. 8522a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // - Local splitting. 8622a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // - Spilling. 8722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // 8822a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // Ranges produced by one of the stages skip the previous stages when they are 8922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // dequeued. This improves performance because we can skip interference checks 9022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // that are unlikely to give any results. It also guarantees that the live 9122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // range splitting algorithm terminates, something that is otherwise hard to 9222a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // ensure. 9322a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen enum LiveRangeStage { 94f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen RS_New, ///< Never seen before. 95f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen RS_First, ///< First time in the queue. 9622a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen RS_Second, ///< Second time in the queue. 97fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen RS_Global, ///< Produced by global splitting. 9822a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen RS_Local, ///< Produced by local splitting. 9922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen RS_Spill ///< Produced by spilling. 10022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen }; 10122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen 10222a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen IndexedMap<unsigned char, VirtReg2IndexFunctor> LRStage; 10322a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen 10422a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen LiveRangeStage getStage(const LiveInterval &VirtReg) const { 10522a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen return LiveRangeStage(LRStage[VirtReg.reg]); 10622a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen } 10722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen 10822a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen template<typename Iterator> 10922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen void setStage(Iterator Begin, Iterator End, LiveRangeStage NewStage) { 11022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen LRStage.resize(MRI->getNumVirtRegs()); 111f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen for (;Begin != End; ++Begin) { 112f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen unsigned Reg = (*Begin)->reg; 113f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen if (LRStage[Reg] == RS_New) 114f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen LRStage[Reg] = NewStage; 115f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen } 11622a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen } 117cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 118b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen // splitting state. 11922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen std::auto_ptr<SplitAnalysis> SA; 120bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen std::auto_ptr<SplitEditor> SE; 121b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 122eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen /// Cached per-block interference maps 123eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen InterferenceCache IntfCache; 124eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen 1257b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen /// All basic blocks where the current register has uses. 12696dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen SmallVector<SpillPlacement::BlockConstraint, 8> SplitConstraints; 127b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 12896dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen /// Global live range splitting candidate info. 12996dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen struct GlobalSplitCandidate { 13096dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen unsigned PhysReg; 13196dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen BitVector LiveBundles; 1325db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen SmallVector<unsigned, 8> ActiveBlocks; 1335db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen 1345db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen void reset(unsigned Reg) { 1355db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen PhysReg = Reg; 1365db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen LiveBundles.clear(); 1375db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen ActiveBlocks.clear(); 1385db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen } 13996dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen }; 14096dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 14196dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen /// Candidate info for for each PhysReg in AllocationOrder. 14296dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen /// This vector never shrinks, but grows to the size of the largest register 14396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen /// class. 14496dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen SmallVector<GlobalSplitCandidate, 32> GlobalCand; 14596dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 146034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen /// For every instruction in SA->UseSlots, store the previous non-copy 147034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen /// instruction. 148034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SmallVector<SlotIndex, 8> PrevSlot; 149034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 150cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenpublic: 151cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen RAGreedy(); 152cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 153cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen /// Return the pass name. 154cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen virtual const char* getPassName() const { 155533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen return "Greedy Register Allocator"; 156cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen } 157cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 158cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen /// RAGreedy analysis usage. 159cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen virtual void getAnalysisUsage(AnalysisUsage &AU) const; 160cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen virtual void releaseMemory(); 161cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen virtual Spiller &spiller() { return *SpillerInstance; } 16298d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen virtual void enqueue(LiveInterval *LI); 16398d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen virtual LiveInterval *dequeue(); 164ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen virtual unsigned selectOrSplit(LiveInterval&, 165ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen SmallVectorImpl<LiveInterval*>&); 166cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 167cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen /// Perform register allocation. 168cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen virtual bool runOnMachineFunction(MachineFunction &mf); 169cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 170cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen static char ID; 171b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick 172b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trickprivate: 17392a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen void LRE_WillEraseInstruction(MachineInstr*); 1747792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen bool LRE_CanEraseVirtReg(unsigned); 1751d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen void LRE_WillShrinkVirtReg(unsigned); 176f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen void LRE_DidCloneVirtReg(unsigned, unsigned); 17792a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen 178200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen float calcSpillCost(); 179f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen bool addSplitConstraints(InterferenceCache::Cursor, float&); 180f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen void addThroughConstraints(InterferenceCache::Cursor, ArrayRef<unsigned>); 1815db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen void growRegion(GlobalSplitCandidate &Cand, InterferenceCache::Cursor); 1825db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen float calcGlobalSplitCost(GlobalSplitCandidate&, InterferenceCache::Cursor); 1835db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen void splitAroundRegion(LiveInterval&, GlobalSplitCandidate&, 184ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen SmallVectorImpl<LiveInterval*>&); 185034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen void calcGapWeights(unsigned, SmallVectorImpl<float>&); 186034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SlotIndex getPrevMappedIndex(const MachineInstr*); 187034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen void calcPrevSlots(); 188034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen unsigned nextSplitPoint(unsigned); 189d17924b1bd0329acb8be2d7dfc5fc4434c24b832Jakob Stoklund Olesen bool canEvictInterference(LiveInterval&, unsigned, float&); 190b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen 1916bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen unsigned tryAssign(LiveInterval&, AllocationOrder&, 1926bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen SmallVectorImpl<LiveInterval*>&); 19398c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen unsigned tryEvict(LiveInterval&, AllocationOrder&, 1946bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen SmallVectorImpl<LiveInterval*>&, unsigned = ~0u); 195b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen unsigned tryRegionSplit(LiveInterval&, AllocationOrder&, 196b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen SmallVectorImpl<LiveInterval*>&); 197034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen unsigned tryLocalSplit(LiveInterval&, AllocationOrder&, 198034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SmallVectorImpl<LiveInterval*>&); 199b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen unsigned trySplit(LiveInterval&, AllocationOrder&, 200b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen SmallVectorImpl<LiveInterval*>&); 201cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen}; 202cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} // end anonymous namespace 203cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 204cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenchar RAGreedy::ID = 0; 205cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 206200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen// Hysteresis to use when comparing floats. 207200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen// This helps stabilize decisions based on float comparisons. 208200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesenconst float Hysteresis = 0.98f; 209200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen 210200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen 211cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund OlesenFunctionPass* llvm::createGreedyRegisterAllocator() { 212cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen return new RAGreedy(); 213cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} 214cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 215f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund OlesenRAGreedy::RAGreedy(): MachineFunctionPass(ID), LRStage(RS_New) { 216cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry()); 217b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); 218cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); 219cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); 220cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry()); 221cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeRegisterCoalescerAnalysisGroup(*PassRegistry::getPassRegistry()); 222cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry()); 223cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeLiveStacksPass(*PassRegistry::getPassRegistry()); 224cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry()); 225cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry()); 226d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen initializeMachineLoopRangesPass(*PassRegistry::getPassRegistry()); 227cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeVirtRegMapPass(*PassRegistry::getPassRegistry()); 228b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen initializeEdgeBundlesPass(*PassRegistry::getPassRegistry()); 229b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen initializeSpillPlacementPass(*PassRegistry::getPassRegistry()); 230cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} 231cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 232cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenvoid RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const { 233cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.setPreservesCFG(); 234cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequired<AliasAnalysis>(); 235cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addPreserved<AliasAnalysis>(); 236cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequired<LiveIntervals>(); 237b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen AU.addRequired<SlotIndexes>(); 238cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addPreserved<SlotIndexes>(); 239cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen AU.addRequired<LiveDebugVariables>(); 240cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen AU.addPreserved<LiveDebugVariables>(); 241cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen if (StrongPHIElim) 242cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequiredID(StrongPHIEliminationID); 243cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequiredTransitive<RegisterCoalescer>(); 244cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequired<CalculateSpillWeights>(); 245cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequired<LiveStacks>(); 246cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addPreserved<LiveStacks>(); 247f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen AU.addRequired<MachineDominatorTree>(); 248f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen AU.addPreserved<MachineDominatorTree>(); 249cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequired<MachineLoopInfo>(); 250cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addPreserved<MachineLoopInfo>(); 251d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen AU.addRequired<MachineLoopRanges>(); 252d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen AU.addPreserved<MachineLoopRanges>(); 253cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequired<VirtRegMap>(); 254cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addPreserved<VirtRegMap>(); 255b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen AU.addRequired<EdgeBundles>(); 256b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen AU.addRequired<SpillPlacement>(); 257cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen MachineFunctionPass::getAnalysisUsage(AU); 258cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} 259cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 26092a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen 26192a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen//===----------------------------------------------------------------------===// 26292a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen// LiveRangeEdit delegate methods 26392a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen//===----------------------------------------------------------------------===// 26492a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen 26592a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesenvoid RAGreedy::LRE_WillEraseInstruction(MachineInstr *MI) { 26692a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen // LRE itself will remove from SlotIndexes and parent basic block. 26792a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen VRM->RemoveMachineInstrFromMaps(MI); 26892a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen} 26992a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen 2707792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesenbool RAGreedy::LRE_CanEraseVirtReg(unsigned VirtReg) { 2717792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen if (unsigned PhysReg = VRM->getPhys(VirtReg)) { 2727792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen unassign(LIS->getInterval(VirtReg), PhysReg); 2737792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen return true; 2747792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen } 2757792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen // Unassigned virtreg is probably in the priority queue. 2767792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen // RegAllocBase will erase it after dequeueing. 2777792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen return false; 2787792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen} 27992a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen 2801d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesenvoid RAGreedy::LRE_WillShrinkVirtReg(unsigned VirtReg) { 2811d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen unsigned PhysReg = VRM->getPhys(VirtReg); 2821d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen if (!PhysReg) 2831d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen return; 2841d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen 2851d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen // Register is assigned, put it back on the queue for reassignment. 2861d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen LiveInterval &LI = LIS->getInterval(VirtReg); 2871d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen unassign(LI, PhysReg); 2881d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen enqueue(&LI); 2891d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen} 2901d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen 291f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesenvoid RAGreedy::LRE_DidCloneVirtReg(unsigned New, unsigned Old) { 292f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen // LRE may clone a virtual register because dead code elimination causes it to 293f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen // be split into connected components. Ensure that the new register gets the 294f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen // same stage as the parent. 295f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen LRStage.grow(New); 296f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen LRStage[New] = LRStage[Old]; 297f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen} 298f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen 299cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenvoid RAGreedy::releaseMemory() { 300cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen SpillerInstance.reset(0); 30122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen LRStage.clear(); 3025db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen GlobalCand.clear(); 303cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen RegAllocBase::releaseMemory(); 304cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} 305cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 30698d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesenvoid RAGreedy::enqueue(LiveInterval *LI) { 30798d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen // Prioritize live ranges by size, assigning larger ranges first. 30898d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen // The queue holds (size, reg) pairs. 309107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen const unsigned Size = LI->getSize(); 310107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen const unsigned Reg = LI->reg; 31198d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen assert(TargetRegisterInfo::isVirtualRegister(Reg) && 31298d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen "Can only enqueue virtual registers"); 313107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen unsigned Prio; 31490c1d7ddfc65654f7efe72d56cad65d1af9e6b2aJakob Stoklund Olesen 31522a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen LRStage.grow(Reg); 316f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen if (LRStage[Reg] == RS_New) 317f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen LRStage[Reg] = RS_First; 318f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen 319eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen if (LRStage[Reg] == RS_Second) 320eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen // Unsplit ranges that couldn't be allocated immediately are deferred until 321eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen // everything else has been allocated. Long ranges are allocated last so 322eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen // they are split against realistic interference. 323eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen Prio = (1u << 31) - Size; 324eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen else { 325eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen // Everything else is allocated in long->short order. Long ranges that don't 326eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen // fit should be spilled ASAP so they don't create interference. 327107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen Prio = (1u << 31) + Size; 328d2a50734234a80893ad71da90d9f32032c47e000Jakob Stoklund Olesen 329eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen // Boost ranges that have a physical register hint. 330eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen if (TargetRegisterInfo::isPhysicalRegister(VRM->getRegAllocPref(Reg))) 331eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen Prio |= (1u << 30); 332eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen } 333107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen 334107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen Queue.push(std::make_pair(Prio, Reg)); 33590c1d7ddfc65654f7efe72d56cad65d1af9e6b2aJakob Stoklund Olesen} 33690c1d7ddfc65654f7efe72d56cad65d1af9e6b2aJakob Stoklund Olesen 33798d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund OlesenLiveInterval *RAGreedy::dequeue() { 33898d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen if (Queue.empty()) 33998d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen return 0; 34098d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen LiveInterval *LI = &LIS->getInterval(Queue.top().second); 34198d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen Queue.pop(); 34298d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen return LI; 34398d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen} 344770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen 3456bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen 3466bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen//===----------------------------------------------------------------------===// 3476bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen// Direct Assignment 3486bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen//===----------------------------------------------------------------------===// 3496bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen 3506bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen/// tryAssign - Try to assign VirtReg to an available register. 3516bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesenunsigned RAGreedy::tryAssign(LiveInterval &VirtReg, 3526bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen AllocationOrder &Order, 3536bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen SmallVectorImpl<LiveInterval*> &NewVRegs) { 3546bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen Order.rewind(); 3556bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen unsigned PhysReg; 3566bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen while ((PhysReg = Order.next())) 3576bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen if (!checkPhysRegInterference(VirtReg, PhysReg)) 3586bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen break; 3596bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen if (!PhysReg || Order.isHint(PhysReg)) 3606bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen return PhysReg; 3616bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen 3626bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen // PhysReg is available. Try to evict interference from a cheaper alternative. 3636bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen unsigned Cost = TRI->getCostPerUse(PhysReg); 3646bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen 3656bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen // Most registers have 0 additional cost. 3666bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen if (!Cost) 3676bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen return PhysReg; 3686bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen 3696bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is available at cost " << Cost 3706bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen << '\n'); 3716bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen unsigned CheapReg = tryEvict(VirtReg, Order, NewVRegs, Cost); 3726bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen return CheapReg ? CheapReg : PhysReg; 3736bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen} 3746bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen 3756bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen 376770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen//===----------------------------------------------------------------------===// 37798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen// Interference eviction 37898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen//===----------------------------------------------------------------------===// 3792710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen 38098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen/// canEvict - Return true if all interferences between VirtReg and PhysReg can 3813f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen/// be evicted. 3823f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen/// Return false if any interference is heavier than MaxWeight. 3833f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen/// On return, set MaxWeight to the maximal spill weight of an interference. 38498c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesenbool RAGreedy::canEvictInterference(LiveInterval &VirtReg, unsigned PhysReg, 385d17924b1bd0329acb8be2d7dfc5fc4434c24b832Jakob Stoklund Olesen float &MaxWeight) { 38698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen float Weight = 0; 38798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) { 38898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI); 3893f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen // If there is 10 or more interferences, chances are one is heavier. 3903f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen if (Q.collectInterferingVRegs(10, MaxWeight) >= 10) 39198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen return false; 39298c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 3933f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen // Check if any interfering live range is heavier than MaxWeight. 3943f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen for (unsigned i = Q.interferingVRegs().size(); i; --i) { 3953f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen LiveInterval *Intf = Q.interferingVRegs()[i - 1]; 39698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen if (TargetRegisterInfo::isPhysicalRegister(Intf->reg)) 39798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen return false; 3983f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen if (Intf->weight >= MaxWeight) 39998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen return false; 40098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen Weight = std::max(Weight, Intf->weight); 4012710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen } 4022710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen } 40398c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen MaxWeight = Weight; 40498c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen return true; 40598c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen} 40698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 40798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen/// tryEvict - Try to evict all interferences for a physreg. 40898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen/// @param VirtReg Currently unassigned virtual register. 40998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen/// @param Order Physregs to try. 41098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen/// @return Physreg to assign VirtReg, or 0. 41198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesenunsigned RAGreedy::tryEvict(LiveInterval &VirtReg, 41298c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen AllocationOrder &Order, 4136bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen SmallVectorImpl<LiveInterval*> &NewVRegs, 4146bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen unsigned CostPerUseLimit) { 41598c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen NamedRegionTimer T("Evict", TimerGroupName, TimePassesIsEnabled); 41698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 41798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen // Keep track of the lightest single interference seen so far. 4183f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen float BestWeight = VirtReg.weight; 41998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen unsigned BestPhys = 0; 42098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 42198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen Order.rewind(); 42298c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen while (unsigned PhysReg = Order.next()) { 4236bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen if (TRI->getCostPerUse(PhysReg) >= CostPerUseLimit) 4246bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen continue; 4256bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen // The first use of a register in a function has cost 1. 4266bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen if (CostPerUseLimit == 1 && !MRI->isPhysRegUsed(PhysReg)) 4276bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen continue; 4286bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen 4293f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen float Weight = BestWeight; 430d17924b1bd0329acb8be2d7dfc5fc4434c24b832Jakob Stoklund Olesen if (!canEvictInterference(VirtReg, PhysReg, Weight)) 43198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen continue; 43298c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 43398c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen // This is an eviction candidate. 4343f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " interference = " 43598c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen << Weight << '\n'); 43698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen if (BestPhys && Weight >= BestWeight) 43798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen continue; 43898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 43998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen // Best so far. 44098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen BestPhys = PhysReg; 44198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen BestWeight = Weight; 44257f1e2cee06f9b57995727d786aeb1031c5376bdJakob Stoklund Olesen // Stop if the hint can be used. 44357f1e2cee06f9b57995727d786aeb1031c5376bdJakob Stoklund Olesen if (Order.isHint(PhysReg)) 44457f1e2cee06f9b57995727d786aeb1031c5376bdJakob Stoklund Olesen break; 4452710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen } 4462710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen 44798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen if (!BestPhys) 44898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen return 0; 44998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 45098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen DEBUG(dbgs() << "evicting " << PrintReg(BestPhys, TRI) << " interference\n"); 45198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen for (const unsigned *AliasI = TRI->getOverlaps(BestPhys); *AliasI; ++AliasI) { 45298c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI); 45398c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen assert(Q.seenAllInterferences() && "Didn't check all interfererences."); 45498c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen for (unsigned i = 0, e = Q.interferingVRegs().size(); i != e; ++i) { 45598c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen LiveInterval *Intf = Q.interferingVRegs()[i]; 45698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen unassign(*Intf, VRM->getPhys(Intf->reg)); 45798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen ++NumEvicted; 45898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen NewVRegs.push_back(Intf); 45998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen } 46098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen } 46198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen return BestPhys; 462b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick} 463b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick 464770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen 465770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen//===----------------------------------------------------------------------===// 466b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen// Region Splitting 467b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen//===----------------------------------------------------------------------===// 468b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 4691b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen/// addSplitConstraints - Fill out the SplitConstraints vector based on the 4701b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen/// interference pattern in Physreg and its aliases. Add the constraints to 4711b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen/// SpillPlacement and return the static cost of this split in Cost, assuming 4721b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen/// that all preferences in SplitConstraints are met. 473f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen/// Return false if there are no bundles with positive bias. 474f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesenbool RAGreedy::addSplitConstraints(InterferenceCache::Cursor Intf, 475f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen float &Cost) { 476db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); 477eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen 478b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen // Reset interference dependent info. 479db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen SplitConstraints.resize(UseBlocks.size()); 48096dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen float StaticCost = 0; 481db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen for (unsigned i = 0; i != UseBlocks.size(); ++i) { 482db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 48396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen SpillPlacement::BlockConstraint &BC = SplitConstraints[i]; 48496dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 485f0ac26c51173a9a1d6e5b5794107dccc4c5b5792Jakob Stoklund Olesen BC.Number = BI.MBB->getNumber(); 486eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen Intf.moveToBlock(BC.Number); 487db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen BC.Entry = BI.LiveIn ? SpillPlacement::PrefReg : SpillPlacement::DontCare; 488db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen BC.Exit = BI.LiveOut ? SpillPlacement::PrefReg : SpillPlacement::DontCare; 489b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 490eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (!Intf.hasInterference()) 491eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen continue; 492eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen 49396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen // Number of spill code instructions to insert. 49496dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen unsigned Ins = 0; 49596dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 49696dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen // Interference for the live-in value. 497eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (BI.LiveIn) { 4986c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen if (Intf.first() <= Indexes->getMBBStartIdx(BC.Number)) 499db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen BC.Entry = SpillPlacement::MustSpill, ++Ins; 500eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen else if (Intf.first() < BI.FirstUse) 50196dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen BC.Entry = SpillPlacement::PrefSpill, ++Ins; 502eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen else if (Intf.first() < (BI.LiveThrough ? BI.LastUse : BI.Kill)) 50396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen ++Ins; 504a50c539b7a9e74597da34bfaea5429a48481f18bJakob Stoklund Olesen } 505b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 50696dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen // Interference for the live-out value. 507eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (BI.LiveOut) { 508612f7807c581eafb7c8105e1a55c8d839033bfb3Jakob Stoklund Olesen if (Intf.last() >= SA->getLastSplitPoint(BC.Number)) 509db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen BC.Exit = SpillPlacement::MustSpill, ++Ins; 510eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen else if (Intf.last() > BI.LastUse) 51196dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen BC.Exit = SpillPlacement::PrefSpill, ++Ins; 512eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen else if (Intf.last() > (BI.LiveThrough ? BI.FirstUse : BI.Def)) 51396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen ++Ins; 514b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen } 515b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 51696dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen // Accumulate the total frequency of inserted spill code. 51796dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen if (Ins) 51896dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen StaticCost += Ins * SpillPlacer->getBlockFrequency(BC.Number); 519b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen } 520f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen Cost = StaticCost; 521db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen 5221b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen // Add constraints for use-blocks. Note that these are the only constraints 5231b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen // that may add a positive bias, it is downhill from here. 5241b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen SpillPlacer->addConstraints(SplitConstraints); 525f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen return SpillPlacer->scanActiveBundles(); 526f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen} 527db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen 528db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen 529f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen/// addThroughConstraints - Add constraints and links to SpillPlacer from the 530f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen/// live-through blocks in Blocks. 531f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesenvoid RAGreedy::addThroughConstraints(InterferenceCache::Cursor Intf, 532f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen ArrayRef<unsigned> Blocks) { 5331b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen const unsigned GroupSize = 8; 5341b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen SpillPlacement::BlockConstraint BCS[GroupSize]; 535f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen unsigned TBS[GroupSize]; 536f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen unsigned B = 0, T = 0; 537db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen 538f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen for (unsigned i = 0; i != Blocks.size(); ++i) { 539f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen unsigned Number = Blocks[i]; 5401b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen Intf.moveToBlock(Number); 5411b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen 5427b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen if (!Intf.hasInterference()) { 543f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen assert(T < GroupSize && "Array overflow"); 544f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen TBS[T] = Number; 545f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen if (++T == GroupSize) { 546f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen SpillPlacer->addLinks(ArrayRef<unsigned>(TBS, T)); 547f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen T = 0; 548f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen } 5497b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen continue; 5501b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen } 5511b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen 552f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen assert(B < GroupSize && "Array overflow"); 553f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen BCS[B].Number = Number; 554f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen 5557b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen // Interference for the live-in value. 5567b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen if (Intf.first() <= Indexes->getMBBStartIdx(Number)) 5577b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen BCS[B].Entry = SpillPlacement::MustSpill; 5587b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen else 5597b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen BCS[B].Entry = SpillPlacement::PrefSpill; 5607b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen 5617b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen // Interference for the live-out value. 5627b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen if (Intf.last() >= SA->getLastSplitPoint(Number)) 5637b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen BCS[B].Exit = SpillPlacement::MustSpill; 5647b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen else 5657b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen BCS[B].Exit = SpillPlacement::PrefSpill; 5667b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen 5671b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen if (++B == GroupSize) { 5681b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen ArrayRef<SpillPlacement::BlockConstraint> Array(BCS, B); 5691b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen SpillPlacer->addConstraints(Array); 5701b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen B = 0; 5711b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen } 572db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen } 573db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen 5741b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen ArrayRef<SpillPlacement::BlockConstraint> Array(BCS, B); 5751b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen SpillPlacer->addConstraints(Array); 576f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen SpillPlacer->addLinks(ArrayRef<unsigned>(TBS, T)); 577b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen} 578b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 5795db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesenvoid RAGreedy::growRegion(GlobalSplitCandidate &Cand, 5805db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen InterferenceCache::Cursor Intf) { 5815db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen // Keep track of through blocks that have not been added to SpillPlacer. 5825db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen BitVector Todo = SA->getThroughBlocks(); 5835db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen SmallVectorImpl<unsigned> &ActiveBlocks = Cand.ActiveBlocks; 5845db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen unsigned AddedTo = 0; 585f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen#ifndef NDEBUG 586f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen unsigned Visited = 0; 587f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen#endif 5885db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen 589f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen for (;;) { 590f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen ArrayRef<unsigned> NewBundles = SpillPlacer->getRecentPositive(); 591f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen if (NewBundles.empty()) 592f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen break; 593f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen // Find new through blocks in the periphery of PrefRegBundles. 594f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen for (int i = 0, e = NewBundles.size(); i != e; ++i) { 595f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen unsigned Bundle = NewBundles[i]; 596f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen // Look at all blocks connected to Bundle in the full graph. 597f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen ArrayRef<unsigned> Blocks = Bundles->getBlocks(Bundle); 598f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen for (ArrayRef<unsigned>::iterator I = Blocks.begin(), E = Blocks.end(); 599f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen I != E; ++I) { 600f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen unsigned Block = *I; 6015db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen if (!Todo.test(Block)) 602f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen continue; 6035db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen Todo.reset(Block); 604f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen // This is a new through block. Add it to SpillPlacer later. 6055db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen ActiveBlocks.push_back(Block); 606f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen#ifndef NDEBUG 607f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen ++Visited; 608f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen#endif 609f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen } 610f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen } 611f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen // Any new blocks to add? 6125db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen if (ActiveBlocks.size() > AddedTo) { 6135db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen ArrayRef<unsigned> Add(&ActiveBlocks[AddedTo], 6145db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen ActiveBlocks.size() - AddedTo); 6155db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen addThroughConstraints(Intf, Add); 6165db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen AddedTo = ActiveBlocks.size(); 617f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen } 618f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen // Perhaps iterating can enable more bundles? 619f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen SpillPlacer->iterate(); 620f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen } 621f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen DEBUG(dbgs() << ", v=" << Visited); 622f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen} 62396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 624200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen/// calcSpillCost - Compute how expensive it would be to split the live range in 625200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen/// SA around all use blocks instead of forming bundle regions. 626200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesenfloat RAGreedy::calcSpillCost() { 627200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen float Cost = 0; 628200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen const LiveInterval &LI = SA->getParent(); 629200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); 630200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen for (unsigned i = 0; i != UseBlocks.size(); ++i) { 631200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 632200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen unsigned Number = BI.MBB->getNumber(); 633200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen // We normally only need one spill instruction - a load or a store. 634200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen Cost += SpillPlacer->getBlockFrequency(Number); 635200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen 636200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen // Unless the value is redefined in the block. 637200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen if (BI.LiveIn && BI.LiveOut) { 638200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen SlotIndex Start, Stop; 639200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen tie(Start, Stop) = Indexes->getMBBRange(Number); 640200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen LiveInterval::const_iterator I = LI.find(Start); 641200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen assert(I != LI.end() && "Expected live-in value"); 642200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen // Is there a different live-out value? If so, we need an extra spill 643200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen // instruction. 644200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen if (I->end < Stop) 645200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen Cost += SpillPlacer->getBlockFrequency(Number); 646200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen } 647200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen } 648200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen return Cost; 649200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen} 650200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen 651b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen/// calcGlobalSplitCost - Return the global split cost of following the split 652b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen/// pattern in LiveBundles. This cost should be added to the local cost of the 65396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen/// interference pattern in SplitConstraints. 654b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen/// 6555db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesenfloat RAGreedy::calcGlobalSplitCost(GlobalSplitCandidate &Cand, 6565db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen InterferenceCache::Cursor Intf) { 657b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen float GlobalCost = 0; 6585db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen const BitVector &LiveBundles = Cand.LiveBundles; 659db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); 660db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen for (unsigned i = 0; i != UseBlocks.size(); ++i) { 661db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 66296dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen SpillPlacement::BlockConstraint &BC = SplitConstraints[i]; 663874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen bool RegIn = LiveBundles[Bundles->getBundle(BC.Number, 0)]; 664874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen bool RegOut = LiveBundles[Bundles->getBundle(BC.Number, 1)]; 665874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen unsigned Ins = 0; 666874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen 667db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen if (BI.LiveIn) 668db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen Ins += RegIn != (BC.Entry == SpillPlacement::PrefReg); 669db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen if (BI.LiveOut) 670db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen Ins += RegOut != (BC.Exit == SpillPlacement::PrefReg); 671874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen if (Ins) 672874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen GlobalCost += Ins * SpillPlacer->getBlockFrequency(BC.Number); 673b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen } 674db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen 6755db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen for (unsigned i = 0, e = Cand.ActiveBlocks.size(); i != e; ++i) { 6765db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen unsigned Number = Cand.ActiveBlocks[i]; 677db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen bool RegIn = LiveBundles[Bundles->getBundle(Number, 0)]; 678db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen bool RegOut = LiveBundles[Bundles->getBundle(Number, 1)]; 6799a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen if (!RegIn && !RegOut) 6809a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen continue; 6819a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen if (RegIn && RegOut) { 6829a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen // We need double spill code if this block has interference. 6839a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen Intf.moveToBlock(Number); 6849a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen if (Intf.hasInterference()) 6859a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen GlobalCost += 2*SpillPlacer->getBlockFrequency(Number); 6869a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen continue; 6879a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen } 6889a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen // live-in / stack-out or stack-in live-out. 6899a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen GlobalCost += SpillPlacer->getBlockFrequency(Number); 690db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen } 691b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen return GlobalCost; 692b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen} 693b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 694ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// splitAroundRegion - Split VirtReg around the region determined by 695ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// LiveBundles. Make an effort to avoid interference from PhysReg. 696ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// 697ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// The 'register' interval is going to contain as many uses as possible while 698ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// avoiding interference. The 'stack' interval is the complement constructed by 699ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// SplitEditor. It will contain the rest. 700ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// 7015db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesenvoid RAGreedy::splitAroundRegion(LiveInterval &VirtReg, 7025db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen GlobalSplitCandidate &Cand, 703ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen SmallVectorImpl<LiveInterval*> &NewVRegs) { 7045db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen const BitVector &LiveBundles = Cand.LiveBundles; 7055db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen 706ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG({ 7075db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen dbgs() << "Splitting around region for " << PrintReg(Cand.PhysReg, TRI) 708ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen << " with bundles"; 709ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen for (int i = LiveBundles.find_first(); i>=0; i = LiveBundles.find_next(i)) 710ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen dbgs() << " EB#" << i; 711ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen dbgs() << ".\n"; 712ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen }); 713ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 7145db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen InterferenceCache::Cursor Intf(IntfCache, Cand.PhysReg); 71592a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen LiveRangeEdit LREdit(VirtReg, NewVRegs, this); 716bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->reset(LREdit); 717ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 718ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Create the main cross-block interval. 719fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen const unsigned MainIntv = SE->openIntv(); 720ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 721ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // First add all defs that are live out of a block. 722db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); 723db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen for (unsigned i = 0; i != UseBlocks.size(); ++i) { 724db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 725ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen bool RegIn = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 0)]; 726ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen bool RegOut = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 1)]; 727ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 728fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen // Create separate intervals for isolated blocks with multiple uses. 729fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen if (!RegIn && !RegOut && BI.FirstUse != BI.LastUse) { 730fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " isolated.\n"); 731fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen SE->splitSingleBlock(BI); 732fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen SE->selectIntv(MainIntv); 733fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen continue; 734fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen } 735fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen 736ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Should the register be live out? 737ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (!BI.LiveOut || !RegOut) 738ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 739ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 7406c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SlotIndex Start, Stop; 7416c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen tie(Start, Stop) = Indexes->getMBBRange(BI.MBB); 742eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen Intf.moveToBlock(BI.MBB->getNumber()); 743ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " -> EB#" 7442dfbb3e9125aa0a66feab7a7638815b57da85968Jakob Stoklund Olesen << Bundles->getBundle(BI.MBB->getNumber(), 1) 745612f7807c581eafb7c8105e1a55c8d839033bfb3Jakob Stoklund Olesen << " [" << Start << ';' 746612f7807c581eafb7c8105e1a55c8d839033bfb3Jakob Stoklund Olesen << SA->getLastSplitPoint(BI.MBB->getNumber()) << '-' << Stop 747612f7807c581eafb7c8105e1a55c8d839033bfb3Jakob Stoklund Olesen << ") intf [" << Intf.first() << ';' << Intf.last() << ')'); 7482dfbb3e9125aa0a66feab7a7638815b57da85968Jakob Stoklund Olesen 7492dfbb3e9125aa0a66feab7a7638815b57da85968Jakob Stoklund Olesen // The interference interval should either be invalid or overlap MBB. 7506c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen assert((!Intf.hasInterference() || Intf.first() < Stop) 751eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen && "Bad interference"); 7526c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen assert((!Intf.hasInterference() || Intf.last() > Start) 75336d61863bc83bd2301e0224adc560098b35ec0dcJakob Stoklund Olesen && "Bad interference"); 754ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 755ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Check interference leaving the block. 756eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (!Intf.hasInterference()) { 757ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Block is interference-free. 758ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << ", no interference"); 759ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (!BI.LiveThrough) { 760ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << ", not live-through.\n"); 7616c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(SE->enterIntvBefore(BI.Def), Stop); 762ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 763ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 764ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (!RegIn) { 765ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Block is live-through, but entry bundle is on the stack. 766ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Reload just before the first use. 767ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << ", not live-in, enter before first use.\n"); 7686c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(SE->enterIntvBefore(BI.FirstUse), Stop); 769ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 770ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 771ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << ", live-through.\n"); 772ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 773ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 774ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 775ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Block has interference. 776eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen DEBUG(dbgs() << ", interference to " << Intf.last()); 777fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen 778eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (!BI.LiveThrough && Intf.last() <= BI.Def) { 779fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen // The interference doesn't reach the outgoing segment. 780fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen DEBUG(dbgs() << " doesn't affect def from " << BI.Def << '\n'); 7816c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(BI.Def, Stop); 782fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen continue; 783fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen } 784fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen 785612f7807c581eafb7c8105e1a55c8d839033bfb3Jakob Stoklund Olesen SlotIndex LastSplitPoint = SA->getLastSplitPoint(BI.MBB->getNumber()); 786eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (Intf.last().getBoundaryIndex() < BI.LastUse) { 787ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // There are interference-free uses at the end of the block. 788ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Find the first use that can get the live-out register. 789c0de99571297720a37ae405c77fb2ef4aaf00ccdJakob Stoklund Olesen SmallVectorImpl<SlotIndex>::const_iterator UI = 790fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen std::lower_bound(SA->UseSlots.begin(), SA->UseSlots.end(), 791eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen Intf.last().getBoundaryIndex()); 792c0de99571297720a37ae405c77fb2ef4aaf00ccdJakob Stoklund Olesen assert(UI != SA->UseSlots.end() && "Couldn't find last use"); 793c0de99571297720a37ae405c77fb2ef4aaf00ccdJakob Stoklund Olesen SlotIndex Use = *UI; 794c0de99571297720a37ae405c77fb2ef4aaf00ccdJakob Stoklund Olesen assert(Use <= BI.LastUse && "Couldn't find last use"); 7958a2bbdeee24b40da6187199658646d04329c139eJakob Stoklund Olesen // Only attempt a split befroe the last split point. 796612f7807c581eafb7c8105e1a55c8d839033bfb3Jakob Stoklund Olesen if (Use.getBaseIndex() <= LastSplitPoint) { 7978a2bbdeee24b40da6187199658646d04329c139eJakob Stoklund Olesen DEBUG(dbgs() << ", free use at " << Use << ".\n"); 798bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SlotIndex SegStart = SE->enterIntvBefore(Use); 799eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen assert(SegStart >= Intf.last() && "Couldn't avoid interference"); 800612f7807c581eafb7c8105e1a55c8d839033bfb3Jakob Stoklund Olesen assert(SegStart < LastSplitPoint && "Impossible split point"); 8016c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(SegStart, Stop); 8028a2bbdeee24b40da6187199658646d04329c139eJakob Stoklund Olesen continue; 8038a2bbdeee24b40da6187199658646d04329c139eJakob Stoklund Olesen } 804ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 805ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 806ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Interference is after the last use. 807ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << " after last use.\n"); 808bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SlotIndex SegStart = SE->enterIntvAtEnd(*BI.MBB); 809eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen assert(SegStart >= Intf.last() && "Couldn't avoid interference"); 810ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 811ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 812ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Now all defs leading to live bundles are handled, do everything else. 813db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen for (unsigned i = 0; i != UseBlocks.size(); ++i) { 814db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 815ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen bool RegIn = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 0)]; 816ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen bool RegOut = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 1)]; 817ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 818ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Is the register live-in? 819ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (!BI.LiveIn || !RegIn) 820ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 821ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 822ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // We have an incoming register. Check for interference. 8236c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SlotIndex Start, Stop; 8246c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen tie(Start, Stop) = Indexes->getMBBRange(BI.MBB); 825eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen Intf.moveToBlock(BI.MBB->getNumber()); 826ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << "EB#" << Bundles->getBundle(BI.MBB->getNumber(), 0) 8276c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen << " -> BB#" << BI.MBB->getNumber() << " [" << Start << ';' 828612f7807c581eafb7c8105e1a55c8d839033bfb3Jakob Stoklund Olesen << SA->getLastSplitPoint(BI.MBB->getNumber()) << '-' << Stop 829612f7807c581eafb7c8105e1a55c8d839033bfb3Jakob Stoklund Olesen << ')'); 830ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 831ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Check interference entering the block. 832eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (!Intf.hasInterference()) { 833ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Block is interference-free. 834ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << ", no interference"); 835ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (!BI.LiveThrough) { 836ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << ", killed in block.\n"); 8376c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(Start, SE->leaveIntvAfter(BI.Kill)); 838ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 839ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 840ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (!RegOut) { 841612f7807c581eafb7c8105e1a55c8d839033bfb3Jakob Stoklund Olesen SlotIndex LastSplitPoint = SA->getLastSplitPoint(BI.MBB->getNumber()); 842ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Block is live-through, but exit bundle is on the stack. 843ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Spill immediately after the last use. 844612f7807c581eafb7c8105e1a55c8d839033bfb3Jakob Stoklund Olesen if (BI.LastUse < LastSplitPoint) { 8455c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen DEBUG(dbgs() << ", uses, stack-out.\n"); 8466c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(Start, SE->leaveIntvAfter(BI.LastUse)); 8475c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen continue; 8485c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen } 8495c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen // The last use is after the last split point, it is probably an 8505c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen // indirect jump. 8515c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen DEBUG(dbgs() << ", uses at " << BI.LastUse << " after split point " 852612f7807c581eafb7c8105e1a55c8d839033bfb3Jakob Stoklund Olesen << LastSplitPoint << ", stack-out.\n"); 853612f7807c581eafb7c8105e1a55c8d839033bfb3Jakob Stoklund Olesen SlotIndex SegEnd = SE->leaveIntvBefore(LastSplitPoint); 8546c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(Start, SegEnd); 8555c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen // Run a double interval from the split to the last use. 8565c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen // This makes it possible to spill the complement without affecting the 8575c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen // indirect branch. 858bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->overlapIntv(SegEnd, BI.LastUse); 859ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 860ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 861ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Register is live-through. 862ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << ", uses, live-through.\n"); 8636c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(Start, Stop); 864ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 865ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 866ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 867ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Block has interference. 868eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen DEBUG(dbgs() << ", interference from " << Intf.first()); 869fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen 870eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (!BI.LiveThrough && Intf.first() >= BI.Kill) { 871fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen // The interference doesn't reach the outgoing segment. 872fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen DEBUG(dbgs() << " doesn't affect kill at " << BI.Kill << '\n'); 8736c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(Start, BI.Kill); 874fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen continue; 875fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen } 876fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen 877eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (Intf.first().getBaseIndex() > BI.FirstUse) { 878ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // There are interference-free uses at the beginning of the block. 879ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Find the last use that can get the register. 880c0de99571297720a37ae405c77fb2ef4aaf00ccdJakob Stoklund Olesen SmallVectorImpl<SlotIndex>::const_iterator UI = 881fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen std::lower_bound(SA->UseSlots.begin(), SA->UseSlots.end(), 882eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen Intf.first().getBaseIndex()); 883c0de99571297720a37ae405c77fb2ef4aaf00ccdJakob Stoklund Olesen assert(UI != SA->UseSlots.begin() && "Couldn't find first use"); 884c0de99571297720a37ae405c77fb2ef4aaf00ccdJakob Stoklund Olesen SlotIndex Use = (--UI)->getBoundaryIndex(); 885c0de99571297720a37ae405c77fb2ef4aaf00ccdJakob Stoklund Olesen DEBUG(dbgs() << ", free use at " << *UI << ".\n"); 886bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SlotIndex SegEnd = SE->leaveIntvAfter(Use); 887eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen assert(SegEnd <= Intf.first() && "Couldn't avoid interference"); 8886c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(Start, SegEnd); 889ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 890ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 891ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 892ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Interference is before the first use. 893ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << " before first use.\n"); 894bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SlotIndex SegEnd = SE->leaveIntvAtTop(*BI.MBB); 895eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen assert(SegEnd <= Intf.first() && "Couldn't avoid interference"); 896ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 897ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 898db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen // Handle live-through blocks. 8995db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen for (unsigned i = 0, e = Cand.ActiveBlocks.size(); i != e; ++i) { 9005db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen unsigned Number = Cand.ActiveBlocks[i]; 901db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen bool RegIn = LiveBundles[Bundles->getBundle(Number, 0)]; 902db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen bool RegOut = LiveBundles[Bundles->getBundle(Number, 1)]; 903db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen DEBUG(dbgs() << "Live through BB#" << Number << '\n'); 904db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen if (RegIn && RegOut) { 905db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen Intf.moveToBlock(Number); 906db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen if (!Intf.hasInterference()) { 907db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen SE->useIntv(Indexes->getMBBStartIdx(Number), 908db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen Indexes->getMBBEndIdx(Number)); 909db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen continue; 910db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen } 911db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen } 912db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen MachineBasicBlock *MBB = MF->getBlockNumbered(Number); 913db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen if (RegIn) 914db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen SE->leaveIntvAtTop(*MBB); 915db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen if (RegOut) 916db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen SE->enterIntvAtEnd(*MBB); 917db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen } 918db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen 9190db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen ++NumGlobalSplits; 920ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 9215928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen SmallVector<unsigned, 8> IntvMap; 9225928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen SE->finish(&IntvMap); 9235928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen LRStage.resize(MRI->getNumVirtRegs()); 9249f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen unsigned OrigBlocks = SA->getNumThroughBlocks() + SA->getUseBlocks().size(); 9255928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen 9265928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen // Sort out the new intervals created by splitting. We get four kinds: 9275928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen // - Remainder intervals should not be split again. 9285928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen // - Candidate intervals can be assigned to Cand.PhysReg. 9295928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen // - Block-local splits are candidates for local splitting. 9305928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen // - DCE leftovers should go back on the queue. 9315928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen for (unsigned i = 0, e = LREdit.size(); i != e; ++i) { 9325928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen unsigned Reg = LREdit.get(i)->reg; 9335928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen 9345928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen // Ignore old intervals from DCE. 9355928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen if (LRStage[Reg] != RS_New) 9365928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen continue; 9375928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen 9385928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen // Remainder interval. Don't try splitting again, spill if it doesn't 9395928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen // allocate. 9405928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen if (IntvMap[i] == 0) { 9415928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen LRStage[Reg] = RS_Global; 9425928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen continue; 9435928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen } 9445928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen 9459f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen // Main interval. Allow repeated splitting as long as the number of live 9469f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen // blocks is strictly decreasing. 9479f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen if (IntvMap[i] == MainIntv) { 9489f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen if (SA->countLiveBlocks(LREdit.get(i)) >= OrigBlocks) { 9499f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen DEBUG(dbgs() << "Main interval covers the same " << OrigBlocks 9509f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen << " blocks as original.\n"); 9519f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen // Don't allow repeated splitting as a safe guard against looping. 9529f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen LRStage[Reg] = RS_Global; 9539f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen } 9549f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen continue; 9559f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen } 9569f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen 9579f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen // Other intervals are treated as new. This includes local intervals created 9589f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen // for blocks with multiple uses, and anything created by DCE. 9595928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen } 9605928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen 961eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen if (VerifyEnabled) 962ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen MF->verify(this, "After splitting live range around region"); 963ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen} 964ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 965b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesenunsigned RAGreedy::tryRegionSplit(LiveInterval &VirtReg, AllocationOrder &Order, 966b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen SmallVectorImpl<LiveInterval*> &NewVRegs) { 967200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen float BestCost = Hysteresis * calcSpillCost(); 968200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen DEBUG(dbgs() << "Cost of isolating all blocks = " << BestCost << '\n'); 9695db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen const unsigned NoCand = ~0u; 9705db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen unsigned BestCand = NoCand; 97196dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 972b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen Order.rewind(); 97396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen for (unsigned Cand = 0; unsigned PhysReg = Order.next(); ++Cand) { 97496dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen if (GlobalCand.size() <= Cand) 97596dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen GlobalCand.resize(Cand+1); 9765db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen GlobalCand[Cand].reset(PhysReg); 97796dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 9785db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen SpillPlacer->prepare(GlobalCand[Cand].LiveBundles); 9791b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen float Cost; 980f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen InterferenceCache::Cursor Intf(IntfCache, PhysReg); 981f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen if (!addSplitConstraints(Intf, Cost)) { 982f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tno positive bundles\n"); 9831b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen continue; 9841b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen } 985f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tstatic = " << Cost); 986200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen if (Cost >= BestCost) { 987200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen DEBUG({ 988200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen if (BestCand == NoCand) 989200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen dbgs() << " worse than no bundles\n"; 990200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen else 991200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen dbgs() << " worse than " 992200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen << PrintReg(GlobalCand[BestCand].PhysReg, TRI) << '\n'; 993200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen }); 994b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen continue; 995874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen } 9965db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen growRegion(GlobalCand[Cand], Intf); 997ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 9989efa2a263ea470caacef1c85f6ca45e32bf516d3Jakob Stoklund Olesen SpillPlacer->finish(); 9999efa2a263ea470caacef1c85f6ca45e32bf516d3Jakob Stoklund Olesen 1000ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // No live bundles, defer to splitSingleBlocks(). 10015db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen if (!GlobalCand[Cand].LiveBundles.any()) { 1002874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen DEBUG(dbgs() << " no bundles.\n"); 1003ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 1004874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen } 1005ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 10065db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen Cost += calcGlobalSplitCost(GlobalCand[Cand], Intf); 1007874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen DEBUG({ 1008874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen dbgs() << ", total = " << Cost << " with bundles"; 10095db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen for (int i = GlobalCand[Cand].LiveBundles.find_first(); i>=0; 10105db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen i = GlobalCand[Cand].LiveBundles.find_next(i)) 1011874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen dbgs() << " EB#" << i; 1012874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen dbgs() << ".\n"; 1013874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen }); 1014200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen if (Cost < BestCost) { 10155db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen BestCand = Cand; 1016200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen BestCost = Hysteresis * Cost; // Prevent rounding effects. 1017b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen } 1018b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen } 1019ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 10205db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen if (BestCand == NoCand) 1021ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen return 0; 1022ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 10235db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen splitAroundRegion(VirtReg, GlobalCand[BestCand], NewVRegs); 1024b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen return 0; 1025b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen} 1026b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 1027ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 1028ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen//===----------------------------------------------------------------------===// 1029034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen// Local Splitting 1030034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen//===----------------------------------------------------------------------===// 1031034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1032034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1033034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// calcGapWeights - Compute the maximum spill weight that needs to be evicted 1034034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// in order to use PhysReg between two entries in SA->UseSlots. 1035034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// 1036034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// GapWeight[i] represents the gap between UseSlots[i] and UseSlots[i+1]. 1037034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// 1038034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesenvoid RAGreedy::calcGapWeights(unsigned PhysReg, 1039034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SmallVectorImpl<float> &GapWeight) { 1040db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen assert(SA->getUseBlocks().size() == 1 && "Not a local interval"); 1041db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front(); 1042034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots; 1043034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const unsigned NumGaps = Uses.size()-1; 1044034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1045034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Start and end points for the interference check. 1046034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SlotIndex StartIdx = BI.LiveIn ? BI.FirstUse.getBaseIndex() : BI.FirstUse; 1047034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SlotIndex StopIdx = BI.LiveOut ? BI.LastUse.getBoundaryIndex() : BI.LastUse; 1048034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1049034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen GapWeight.assign(NumGaps, 0.0f); 1050034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1051034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Add interference from each overlapping register. 1052034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) { 1053034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (!query(const_cast<LiveInterval&>(SA->getParent()), *AI) 1054034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen .checkInterference()) 1055034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen continue; 1056034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1057034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // We know that VirtReg is a continuous interval from FirstUse to LastUse, 1058034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // so we don't need InterferenceQuery. 1059034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // 1060034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Interference that overlaps an instruction is counted in both gaps 1061034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // surrounding the instruction. The exception is interference before 1062034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // StartIdx and after StopIdx. 1063034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // 1064034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen LiveIntervalUnion::SegmentIter IntI = PhysReg2LiveUnion[*AI].find(StartIdx); 1065034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (unsigned Gap = 0; IntI.valid() && IntI.start() < StopIdx; ++IntI) { 1066034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Skip the gaps before IntI. 1067034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen while (Uses[Gap+1].getBoundaryIndex() < IntI.start()) 1068034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (++Gap == NumGaps) 1069034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen break; 1070034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Gap == NumGaps) 1071034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen break; 1072034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1073034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Update the gaps covered by IntI. 1074034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const float weight = IntI.value()->weight; 1075034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (; Gap != NumGaps; ++Gap) { 1076034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen GapWeight[Gap] = std::max(GapWeight[Gap], weight); 1077034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Uses[Gap+1].getBaseIndex() >= IntI.stop()) 1078034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen break; 1079034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1080034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Gap == NumGaps) 1081034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen break; 1082034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1083034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1084034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen} 1085034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1086034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// getPrevMappedIndex - Return the slot index of the last non-copy instruction 1087034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// before MI that has a slot index. If MI is the first mapped instruction in 1088034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// its block, return the block start index instead. 1089034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// 1090034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund OlesenSlotIndex RAGreedy::getPrevMappedIndex(const MachineInstr *MI) { 1091034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen assert(MI && "Missing MachineInstr"); 1092034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const MachineBasicBlock *MBB = MI->getParent(); 1093034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen MachineBasicBlock::const_iterator B = MBB->begin(), I = MI; 1094034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen while (I != B) 1095034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (!(--I)->isDebugValue() && !I->isCopy()) 1096034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen return Indexes->getInstructionIndex(I); 1097034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen return Indexes->getMBBStartIdx(MBB); 1098034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen} 1099034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1100034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// calcPrevSlots - Fill in the PrevSlot array with the index of the previous 1101034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// real non-copy instruction for each instruction in SA->UseSlots. 1102034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// 1103034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesenvoid RAGreedy::calcPrevSlots() { 1104034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots; 1105034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen PrevSlot.clear(); 1106034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen PrevSlot.reserve(Uses.size()); 1107034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (unsigned i = 0, e = Uses.size(); i != e; ++i) { 1108034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const MachineInstr *MI = Indexes->getInstructionFromIndex(Uses[i]); 1109034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen PrevSlot.push_back(getPrevMappedIndex(MI).getDefIndex()); 1110034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1111034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen} 1112034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1113034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// nextSplitPoint - Find the next index into SA->UseSlots > i such that it may 1114034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// be beneficial to split before UseSlots[i]. 1115034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// 1116034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// 0 is always a valid split point 1117034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesenunsigned RAGreedy::nextSplitPoint(unsigned i) { 1118034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots; 1119034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const unsigned Size = Uses.size(); 1120034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen assert(i != Size && "No split points after the end"); 1121034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Allow split before i when Uses[i] is not adjacent to the previous use. 1122034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen while (++i != Size && PrevSlot[i].getBaseIndex() <= Uses[i-1].getBaseIndex()) 1123034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen ; 1124034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen return i; 1125034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen} 1126034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1127034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// tryLocalSplit - Try to split VirtReg into smaller intervals inside its only 1128034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// basic block. 1129034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// 1130034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesenunsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order, 1131034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SmallVectorImpl<LiveInterval*> &NewVRegs) { 1132db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen assert(SA->getUseBlocks().size() == 1 && "Not a local interval"); 1133db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front(); 1134034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1135034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Note that it is possible to have an interval that is live-in or live-out 1136034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // while only covering a single block - A phi-def can use undef values from 1137034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // predecessors, and the block could be a single-block loop. 1138034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // We don't bother doing anything clever about such a case, we simply assume 1139034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // that the interval is continuous from FirstUse to LastUse. We should make 1140034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // sure that we don't do anything illegal to such an interval, though. 1141034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1142034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots; 1143034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Uses.size() <= 2) 1144034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen return 0; 1145034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const unsigned NumGaps = Uses.size()-1; 1146034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1147034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG({ 1148034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen dbgs() << "tryLocalSplit: "; 1149034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (unsigned i = 0, e = Uses.size(); i != e; ++i) 1150034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen dbgs() << ' ' << SA->UseSlots[i]; 1151034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen dbgs() << '\n'; 1152034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen }); 1153034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1154034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // For every use, find the previous mapped non-copy instruction. 1155034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // We use this to detect valid split points, and to estimate new interval 1156034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // sizes. 1157034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen calcPrevSlots(); 1158034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1159034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen unsigned BestBefore = NumGaps; 1160034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen unsigned BestAfter = 0; 1161034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen float BestDiff = 0; 1162034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 116340a42a2ccaaa19a109667ed7abf224cc8733cd9cJakob Stoklund Olesen const float blockFreq = SpillPlacer->getBlockFrequency(BI.MBB->getNumber()); 1164034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SmallVector<float, 8> GapWeight; 1165034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1166034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen Order.rewind(); 1167034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen while (unsigned PhysReg = Order.next()) { 1168034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Keep track of the largest spill weight that would need to be evicted in 1169034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // order to make use of PhysReg between UseSlots[i] and UseSlots[i+1]. 1170034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen calcGapWeights(PhysReg, GapWeight); 1171034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1172034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Try to find the best sequence of gaps to close. 1173034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // The new spill weight must be larger than any gap interference. 1174034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1175034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // We will split before Uses[SplitBefore] and after Uses[SplitAfter]. 1176034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen unsigned SplitBefore = 0, SplitAfter = nextSplitPoint(1) - 1; 1177034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1178034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // MaxGap should always be max(GapWeight[SplitBefore..SplitAfter-1]). 1179034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // It is the spill weight that needs to be evicted. 1180034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen float MaxGap = GapWeight[0]; 1181034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (unsigned i = 1; i != SplitAfter; ++i) 1182034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen MaxGap = std::max(MaxGap, GapWeight[i]); 1183034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1184034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (;;) { 1185034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Live before/after split? 1186034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const bool LiveBefore = SplitBefore != 0 || BI.LiveIn; 1187034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const bool LiveAfter = SplitAfter != NumGaps || BI.LiveOut; 1188034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1189034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << PrintReg(PhysReg, TRI) << ' ' 1190034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen << Uses[SplitBefore] << '-' << Uses[SplitAfter] 1191034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen << " i=" << MaxGap); 1192034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1193034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Stop before the interval gets so big we wouldn't be making progress. 1194034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (!LiveBefore && !LiveAfter) { 1195034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << " all\n"); 1196034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen break; 1197034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1198034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Should the interval be extended or shrunk? 1199034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen bool Shrink = true; 1200034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (MaxGap < HUGE_VALF) { 1201034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Estimate the new spill weight. 1202034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // 1203034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Each instruction reads and writes the register, except the first 1204034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // instr doesn't read when !FirstLive, and the last instr doesn't write 1205034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // when !LastLive. 1206034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // 1207034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // We will be inserting copies before and after, so the total number of 1208034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // reads and writes is 2 * EstUses. 1209034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // 1210034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const unsigned EstUses = 2*(SplitAfter - SplitBefore) + 1211034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 2*(LiveBefore + LiveAfter); 1212034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1213034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Try to guess the size of the new interval. This should be trivial, 1214034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // but the slot index of an inserted copy can be a lot smaller than the 1215034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // instruction it is inserted before if there are many dead indexes 1216034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // between them. 1217034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // 1218034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // We measure the distance from the instruction before SplitBefore to 1219034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // get a conservative estimate. 1220034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // 1221034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // The final distance can still be different if inserting copies 1222034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // triggers a slot index renumbering. 1223034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // 1224034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const float EstWeight = normalizeSpillWeight(blockFreq * EstUses, 1225034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen PrevSlot[SplitBefore].distance(Uses[SplitAfter])); 1226034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Would this split be possible to allocate? 1227034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Never allocate all gaps, we wouldn't be making progress. 1228034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen float Diff = EstWeight - MaxGap; 1229034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << " w=" << EstWeight << " d=" << Diff); 1230034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Diff > 0) { 1231034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen Shrink = false; 1232034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Diff > BestDiff) { 1233034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << " (best)"); 1234034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen BestDiff = Diff; 1235034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen BestBefore = SplitBefore; 1236034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen BestAfter = SplitAfter; 1237034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1238034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1239034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1240034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1241034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Try to shrink. 1242034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Shrink) { 1243034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SplitBefore = nextSplitPoint(SplitBefore); 1244034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (SplitBefore < SplitAfter) { 1245034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << " shrink\n"); 1246034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Recompute the max when necessary. 1247034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (GapWeight[SplitBefore - 1] >= MaxGap) { 1248034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen MaxGap = GapWeight[SplitBefore]; 1249034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (unsigned i = SplitBefore + 1; i != SplitAfter; ++i) 1250034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen MaxGap = std::max(MaxGap, GapWeight[i]); 1251034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1252034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen continue; 1253034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1254034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen MaxGap = 0; 1255034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1256034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1257034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Try to extend the interval. 1258034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (SplitAfter >= NumGaps) { 1259034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << " end\n"); 1260034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen break; 1261034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1262034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1263034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << " extend\n"); 1264034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (unsigned e = nextSplitPoint(SplitAfter + 1) - 1; 1265034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SplitAfter != e; ++SplitAfter) 1266034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen MaxGap = std::max(MaxGap, GapWeight[SplitAfter]); 1267034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen continue; 1268034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1269034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1270034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1271034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Didn't find any candidates? 1272034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (BestBefore == NumGaps) 1273034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen return 0; 1274034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1275034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << "Best local split range: " << Uses[BestBefore] 1276034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen << '-' << Uses[BestAfter] << ", " << BestDiff 1277034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen << ", " << (BestAfter - BestBefore + 1) << " instrs\n"); 1278034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 127992a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen LiveRangeEdit LREdit(VirtReg, NewVRegs, this); 1280bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->reset(LREdit); 1281bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen 1282bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->openIntv(); 1283bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SlotIndex SegStart = SE->enterIntvBefore(Uses[BestBefore]); 1284bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SlotIndex SegStop = SE->leaveIntvAfter(Uses[BestAfter]); 1285bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->useIntv(SegStart, SegStop); 1286bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->finish(); 128722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen setStage(NewVRegs.begin(), NewVRegs.end(), RS_Local); 12880db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen ++NumLocalSplits; 1289034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1290034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen return 0; 1291034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen} 1292034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1293034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen//===----------------------------------------------------------------------===// 1294ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen// Live Range Splitting 1295ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen//===----------------------------------------------------------------------===// 1296ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 1297ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// trySplit - Try to split VirtReg or one of its interferences, making it 1298ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// assignable. 1299ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// @return Physreg when VirtReg may be assigned and/or new NewVRegs. 1300ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesenunsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order, 1301ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen SmallVectorImpl<LiveInterval*>&NewVRegs) { 1302034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Local intervals are handled separately. 1303a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen if (LIS->intervalIsInOneMBB(VirtReg)) { 1304a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen NamedRegionTimer T("Local Splitting", TimerGroupName, TimePassesIsEnabled); 130522a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen SA->analyze(&VirtReg); 1306034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen return tryLocalSplit(VirtReg, Order, NewVRegs); 1307a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen } 1308a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen 1309a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen NamedRegionTimer T("Global Splitting", TimerGroupName, TimePassesIsEnabled); 1310ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 131122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // Don't iterate global splitting. 131222a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // Move straight to spilling if this range was produced by a global split. 1313fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen if (getStage(VirtReg) >= RS_Global) 131422a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen return 0; 131522a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen 131622a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen SA->analyze(&VirtReg); 131722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen 1318ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // First try to split around a region spanning multiple blocks. 1319fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen unsigned PhysReg = tryRegionSplit(VirtReg, Order, NewVRegs); 1320fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen if (PhysReg || !NewVRegs.empty()) 1321fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen return PhysReg; 1322ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 1323ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Then isolate blocks with multiple uses. 1324fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen SplitAnalysis::BlockPtrSet Blocks; 1325fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen if (SA->getMultiUseBlocks(Blocks)) { 1326fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen LiveRangeEdit LREdit(VirtReg, NewVRegs, this); 1327fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen SE->reset(LREdit); 1328fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen SE->splitSingleBlocks(Blocks); 1329fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen setStage(NewVRegs.begin(), NewVRegs.end(), RS_Global); 1330fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen if (VerifyEnabled) 1331fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen MF->verify(this, "After splitting live range around basic blocks"); 1332ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 1333ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 1334ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Don't assign any physregs. 1335ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen return 0; 1336ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen} 1337ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 1338ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 1339b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen//===----------------------------------------------------------------------===// 1340770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen// Main Entry Point 1341770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen//===----------------------------------------------------------------------===// 1342770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen 1343770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesenunsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg, 1344ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen SmallVectorImpl<LiveInterval*> &NewVRegs) { 1345770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen // First try assigning a free register. 1346dd479e9769eceee9fcc34e2173376024f3aa3c5fJakob Stoklund Olesen AllocationOrder Order(VirtReg.reg, *VRM, ReservedRegs); 13476bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs)) 13486bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen return PhysReg; 1349b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick 135098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen if (unsigned PhysReg = tryEvict(VirtReg, Order, NewVRegs)) 135146c83c80c5737743c955ff007fa6409804a7abf0Jakob Stoklund Olesen return PhysReg; 1352b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick 1353ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen assert(NewVRegs.empty() && "Cannot append to existing NewVRegs"); 1354ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 1355107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen // The first time we see a live range, don't try to split or spill. 1356107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen // Wait until the second time, when all smaller ranges have been allocated. 1357107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen // This gives a better picture of the interference to split around. 1358eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen LiveRangeStage Stage = getStage(VirtReg); 1359f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen if (Stage == RS_First) { 1360eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen LRStage[VirtReg.reg] = RS_Second; 1361c1655e1a3c3a566b91b0513b56d61b58da1e36baJakob Stoklund Olesen DEBUG(dbgs() << "wait for second round\n"); 1362107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen NewVRegs.push_back(&VirtReg); 1363107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen return 0; 1364107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen } 1365107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen 136622a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen assert(Stage < RS_Spill && "Cannot allocate after spilling"); 136722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen 136846c83c80c5737743c955ff007fa6409804a7abf0Jakob Stoklund Olesen // Try splitting VirtReg or interferences. 1369ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen unsigned PhysReg = trySplit(VirtReg, Order, NewVRegs); 1370ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (PhysReg || !NewVRegs.empty()) 1371b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen return PhysReg; 1372b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen 1373770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen // Finally spill VirtReg itself. 1374770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled); 137547dbf6cef761c25cfeb0aa7d624a6f98288bb96aJakob Stoklund Olesen LiveRangeEdit LRE(VirtReg, NewVRegs, this); 137647dbf6cef761c25cfeb0aa7d624a6f98288bb96aJakob Stoklund Olesen spiller().spill(LRE); 13776094bd87d845afabba5b99ec4848fa6116bac682Jakob Stoklund Olesen setStage(NewVRegs.begin(), NewVRegs.end(), RS_Spill); 1378cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 1379c46570dc05851395829bef904bb6ddb1260400d1Jakob Stoklund Olesen if (VerifyEnabled) 1380c46570dc05851395829bef904bb6ddb1260400d1Jakob Stoklund Olesen MF->verify(this, "After spilling"); 1381c46570dc05851395829bef904bb6ddb1260400d1Jakob Stoklund Olesen 1382cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen // The live virtual register requesting allocation was spilled, so tell 1383cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen // the caller not to allocate anything during this round. 1384cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen return 0; 1385cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} 1386cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 1387cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenbool RAGreedy::runOnMachineFunction(MachineFunction &mf) { 1388cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n" 1389cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen << "********** Function: " 1390cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen << ((Value*)mf.getFunction())->getName() << '\n'); 1391cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 1392cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen MF = &mf; 1393af24964251e27c2dd863239ba66ffd967b593be5Jakob Stoklund Olesen if (VerifyEnabled) 139489cab93fe999f6d81b4b99a71ac797b7ecfec277Jakob Stoklund Olesen MF->verify(this, "Before greedy register allocator"); 1395af24964251e27c2dd863239ba66ffd967b593be5Jakob Stoklund Olesen 13964680dec5fb3a1b624f13ca9b2a555ca90a07973eJakob Stoklund Olesen RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>()); 1397b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen Indexes = &getAnalysis<SlotIndexes>(); 1398f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen DomTree = &getAnalysis<MachineDominatorTree>(); 1399cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen ReservedRegs = TRI->getReservedRegs(*MF); 1400f6dff84d4e44d6c4a46c4f8a18e13c78f804547cJakob Stoklund Olesen SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM)); 1401d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen Loops = &getAnalysis<MachineLoopInfo>(); 1402d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen LoopRanges = &getAnalysis<MachineLoopRanges>(); 1403b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen Bundles = &getAnalysis<EdgeBundles>(); 1404b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen SpillPlacer = &getAnalysis<SpillPlacement>(); 1405b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 14061b847deb26b52051de39f4cbecd224c9fbd0d1c2Jakob Stoklund Olesen SA.reset(new SplitAnalysis(*VRM, *LIS, *Loops)); 1407bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE.reset(new SplitEditor(*SA, *LIS, *VRM, *DomTree)); 140822a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen LRStage.clear(); 140922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen LRStage.resize(MRI->getNumVirtRegs()); 1410eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen IntfCache.init(MF, &PhysReg2LiveUnion[0], Indexes, TRI); 1411d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen 1412cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen allocatePhysRegs(); 1413cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen addMBBLiveIns(MF); 14148a61da8a689ee95874c833af4c7aa965fab5c0a9Jakob Stoklund Olesen LIS->addKillFlags(); 1415cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 1416cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen // Run rewriter 1417533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen { 1418533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen NamedRegionTimer T("Rewriter", TimerGroupName, TimePassesIsEnabled); 1419ba05c01dabc40373760a20c874103fc58d4377f0Jakob Stoklund Olesen VRM->rewrite(Indexes); 1420533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen } 1421cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 1422cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen // Write out new DBG_VALUE instructions. 1423cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen getAnalysis<LiveDebugVariables>().emitDebugValues(VRM); 1424cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen 1425cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen // The pass output is in VirtRegMap. Release all the transient data. 1426cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen releaseMemory(); 1427cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 1428cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen return true; 1429cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} 1430