RegAllocGreedy.cpp revision 6c8afd728eb02742ce320ecd39bcf3774d8423b7
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" 18f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen#include "LiveRangeEdit.h" 19cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "RegAllocBase.h" 20cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "Spiller.h" 21b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen#include "SpillPlacement.h" 22d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen#include "SplitKit.h" 23cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "VirtRegMap.h" 240db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen#include "llvm/ADT/Statistic.h" 25cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Analysis/AliasAnalysis.h" 26cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Function.h" 27cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/PassAnalysisSupport.h" 28cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/CalcSpillWeights.h" 29b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen#include "llvm/CodeGen/EdgeBundles.h" 30cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/LiveIntervalAnalysis.h" 31cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/LiveStackAnalysis.h" 32f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen#include "llvm/CodeGen/MachineDominators.h" 33cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/MachineFunctionPass.h" 34cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/MachineLoopInfo.h" 35d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen#include "llvm/CodeGen/MachineLoopRanges.h" 36cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/MachineRegisterInfo.h" 37cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/Passes.h" 38cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/RegAllocRegistry.h" 39cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/RegisterCoalescer.h" 40cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Target/TargetOptions.h" 41cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Support/Debug.h" 42cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Support/ErrorHandling.h" 43cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Support/raw_ostream.h" 44533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen#include "llvm/Support/Timer.h" 45cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 4698d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen#include <queue> 4798d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen 48cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenusing namespace llvm; 49cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 500db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund OlesenSTATISTIC(NumGlobalSplits, "Number of split global live ranges"); 510db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund OlesenSTATISTIC(NumLocalSplits, "Number of split local live ranges"); 520db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund OlesenSTATISTIC(NumEvicted, "Number of interferences evicted"); 530db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen 54cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenstatic RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator", 55cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen createGreedyRegisterAllocator); 56cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 57cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesennamespace { 5892a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesenclass RAGreedy : public MachineFunctionPass, 5992a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen public RegAllocBase, 6092a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen private LiveRangeEdit::Delegate { 6192a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen 62cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen // context 63cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen MachineFunction *MF; 64cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen BitVector ReservedRegs; 65cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 66cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen // analyses 67b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen SlotIndexes *Indexes; 68cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen LiveStacks *LS; 69f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen MachineDominatorTree *DomTree; 70d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen MachineLoopInfo *Loops; 71d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen MachineLoopRanges *LoopRanges; 72b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen EdgeBundles *Bundles; 73b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen SpillPlacement *SpillPlacer; 74f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen 75cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen // state 76cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen std::auto_ptr<Spiller> SpillerInstance; 7798d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen std::priority_queue<std::pair<unsigned, unsigned> > Queue; 7822a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen 7922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // Live ranges pass through a number of stages as we try to allocate them. 8022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // Some of the stages may also create new live ranges: 8122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // 8222a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // - Region splitting. 8322a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // - Per-block splitting. 8422a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // - Local splitting. 8522a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // - Spilling. 8622a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // 8722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // Ranges produced by one of the stages skip the previous stages when they are 8822a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // dequeued. This improves performance because we can skip interference checks 8922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // that are unlikely to give any results. It also guarantees that the live 9022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // range splitting algorithm terminates, something that is otherwise hard to 9122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // ensure. 9222a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen enum LiveRangeStage { 93f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen RS_New, ///< Never seen before. 94f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen RS_First, ///< First time in the queue. 9522a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen RS_Second, ///< Second time in the queue. 9622a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen RS_Region, ///< Produced by region splitting. 9722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen RS_Block, ///< Produced by per-block 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 125b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen /// All basic blocks where the current register is live. 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; 13296dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen }; 13396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 13496dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen /// Candidate info for for each PhysReg in AllocationOrder. 13596dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen /// This vector never shrinks, but grows to the size of the largest register 13696dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen /// class. 13796dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen SmallVector<GlobalSplitCandidate, 32> GlobalCand; 13896dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 139034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen /// For every instruction in SA->UseSlots, store the previous non-copy 140034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen /// instruction. 141034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SmallVector<SlotIndex, 8> PrevSlot; 142034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 143cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenpublic: 144cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen RAGreedy(); 145cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 146cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen /// Return the pass name. 147cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen virtual const char* getPassName() const { 148533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen return "Greedy Register Allocator"; 149cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen } 150cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 151cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen /// RAGreedy analysis usage. 152cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen virtual void getAnalysisUsage(AnalysisUsage &AU) const; 153cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen virtual void releaseMemory(); 154cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen virtual Spiller &spiller() { return *SpillerInstance; } 15598d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen virtual void enqueue(LiveInterval *LI); 15698d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen virtual LiveInterval *dequeue(); 157ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen virtual unsigned selectOrSplit(LiveInterval&, 158ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen SmallVectorImpl<LiveInterval*>&); 159cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 160cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen /// Perform register allocation. 161cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen virtual bool runOnMachineFunction(MachineFunction &mf); 162cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 163cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen static char ID; 164b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick 165b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trickprivate: 16692a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen void LRE_WillEraseInstruction(MachineInstr*); 1677792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen bool LRE_CanEraseVirtReg(unsigned); 1681d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen void LRE_WillShrinkVirtReg(unsigned); 169f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen void LRE_DidCloneVirtReg(unsigned, unsigned); 17092a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen 171eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen float calcSplitConstraints(unsigned); 172b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen float calcGlobalSplitCost(const BitVector&); 173ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen void splitAroundRegion(LiveInterval&, unsigned, const BitVector&, 174ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen SmallVectorImpl<LiveInterval*>&); 175034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen void calcGapWeights(unsigned, SmallVectorImpl<float>&); 176034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SlotIndex getPrevMappedIndex(const MachineInstr*); 177034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen void calcPrevSlots(); 178034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen unsigned nextSplitPoint(unsigned); 179d17924b1bd0329acb8be2d7dfc5fc4434c24b832Jakob Stoklund Olesen bool canEvictInterference(LiveInterval&, unsigned, float&); 180b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen 18198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen unsigned tryEvict(LiveInterval&, AllocationOrder&, 18298c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen SmallVectorImpl<LiveInterval*>&); 183b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen unsigned tryRegionSplit(LiveInterval&, AllocationOrder&, 184b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen SmallVectorImpl<LiveInterval*>&); 185034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen unsigned tryLocalSplit(LiveInterval&, AllocationOrder&, 186034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SmallVectorImpl<LiveInterval*>&); 187b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen unsigned trySplit(LiveInterval&, AllocationOrder&, 188b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen SmallVectorImpl<LiveInterval*>&); 189cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen}; 190cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} // end anonymous namespace 191cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 192cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenchar RAGreedy::ID = 0; 193cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 194cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund OlesenFunctionPass* llvm::createGreedyRegisterAllocator() { 195cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen return new RAGreedy(); 196cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} 197cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 198f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund OlesenRAGreedy::RAGreedy(): MachineFunctionPass(ID), LRStage(RS_New) { 199b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); 200cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); 201cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); 202cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry()); 203cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeRegisterCoalescerAnalysisGroup(*PassRegistry::getPassRegistry()); 204cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry()); 205cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeLiveStacksPass(*PassRegistry::getPassRegistry()); 206cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry()); 207cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry()); 208d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen initializeMachineLoopRangesPass(*PassRegistry::getPassRegistry()); 209cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeVirtRegMapPass(*PassRegistry::getPassRegistry()); 210b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen initializeEdgeBundlesPass(*PassRegistry::getPassRegistry()); 211b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen initializeSpillPlacementPass(*PassRegistry::getPassRegistry()); 212cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} 213cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 214cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenvoid RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const { 215cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.setPreservesCFG(); 216cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequired<AliasAnalysis>(); 217cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addPreserved<AliasAnalysis>(); 218cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequired<LiveIntervals>(); 219b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen AU.addRequired<SlotIndexes>(); 220cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addPreserved<SlotIndexes>(); 221cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen if (StrongPHIElim) 222cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequiredID(StrongPHIEliminationID); 223cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequiredTransitive<RegisterCoalescer>(); 224cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequired<CalculateSpillWeights>(); 225cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequired<LiveStacks>(); 226cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addPreserved<LiveStacks>(); 227f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen AU.addRequired<MachineDominatorTree>(); 228f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen AU.addPreserved<MachineDominatorTree>(); 229cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequired<MachineLoopInfo>(); 230cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addPreserved<MachineLoopInfo>(); 231d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen AU.addRequired<MachineLoopRanges>(); 232d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen AU.addPreserved<MachineLoopRanges>(); 233cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequired<VirtRegMap>(); 234cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addPreserved<VirtRegMap>(); 235b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen AU.addRequired<EdgeBundles>(); 236b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen AU.addRequired<SpillPlacement>(); 237cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen MachineFunctionPass::getAnalysisUsage(AU); 238cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} 239cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 24092a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen 24192a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen//===----------------------------------------------------------------------===// 24292a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen// LiveRangeEdit delegate methods 24392a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen//===----------------------------------------------------------------------===// 24492a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen 24592a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesenvoid RAGreedy::LRE_WillEraseInstruction(MachineInstr *MI) { 24692a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen // LRE itself will remove from SlotIndexes and parent basic block. 24792a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen VRM->RemoveMachineInstrFromMaps(MI); 24892a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen} 24992a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen 2507792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesenbool RAGreedy::LRE_CanEraseVirtReg(unsigned VirtReg) { 2517792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen if (unsigned PhysReg = VRM->getPhys(VirtReg)) { 2527792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen unassign(LIS->getInterval(VirtReg), PhysReg); 2537792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen return true; 2547792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen } 2557792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen // Unassigned virtreg is probably in the priority queue. 2567792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen // RegAllocBase will erase it after dequeueing. 2577792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen return false; 2587792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen} 25992a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen 2601d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesenvoid RAGreedy::LRE_WillShrinkVirtReg(unsigned VirtReg) { 2611d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen unsigned PhysReg = VRM->getPhys(VirtReg); 2621d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen if (!PhysReg) 2631d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen return; 2641d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen 2651d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen // Register is assigned, put it back on the queue for reassignment. 2661d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen LiveInterval &LI = LIS->getInterval(VirtReg); 2671d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen unassign(LI, PhysReg); 2681d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen enqueue(&LI); 2691d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen} 2701d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen 271f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesenvoid RAGreedy::LRE_DidCloneVirtReg(unsigned New, unsigned Old) { 272f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen // LRE may clone a virtual register because dead code elimination causes it to 273f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen // be split into connected components. Ensure that the new register gets the 274f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen // same stage as the parent. 275f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen LRStage.grow(New); 276f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen LRStage[New] = LRStage[Old]; 277f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen} 278f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen 279cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenvoid RAGreedy::releaseMemory() { 280cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen SpillerInstance.reset(0); 28122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen LRStage.clear(); 282cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen RegAllocBase::releaseMemory(); 283cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} 284cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 28598d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesenvoid RAGreedy::enqueue(LiveInterval *LI) { 28698d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen // Prioritize live ranges by size, assigning larger ranges first. 28798d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen // The queue holds (size, reg) pairs. 288107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen const unsigned Size = LI->getSize(); 289107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen const unsigned Reg = LI->reg; 29098d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen assert(TargetRegisterInfo::isVirtualRegister(Reg) && 29198d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen "Can only enqueue virtual registers"); 292107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen unsigned Prio; 29390c1d7ddfc65654f7efe72d56cad65d1af9e6b2aJakob Stoklund Olesen 29422a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen LRStage.grow(Reg); 295f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen if (LRStage[Reg] == RS_New) 296f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen LRStage[Reg] = RS_First; 297f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen 298eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen if (LRStage[Reg] == RS_Second) 299eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen // Unsplit ranges that couldn't be allocated immediately are deferred until 300eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen // everything else has been allocated. Long ranges are allocated last so 301eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen // they are split against realistic interference. 302eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen Prio = (1u << 31) - Size; 303eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen else { 304eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen // Everything else is allocated in long->short order. Long ranges that don't 305eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen // fit should be spilled ASAP so they don't create interference. 306107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen Prio = (1u << 31) + Size; 307d2a50734234a80893ad71da90d9f32032c47e000Jakob Stoklund Olesen 308eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen // Boost ranges that have a physical register hint. 309eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen if (TargetRegisterInfo::isPhysicalRegister(VRM->getRegAllocPref(Reg))) 310eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen Prio |= (1u << 30); 311eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen } 312107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen 313107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen Queue.push(std::make_pair(Prio, Reg)); 31490c1d7ddfc65654f7efe72d56cad65d1af9e6b2aJakob Stoklund Olesen} 31590c1d7ddfc65654f7efe72d56cad65d1af9e6b2aJakob Stoklund Olesen 31698d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund OlesenLiveInterval *RAGreedy::dequeue() { 31798d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen if (Queue.empty()) 31898d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen return 0; 31998d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen LiveInterval *LI = &LIS->getInterval(Queue.top().second); 32098d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen Queue.pop(); 32198d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen return LI; 32298d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen} 323770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen 324770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen//===----------------------------------------------------------------------===// 32598c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen// Interference eviction 32698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen//===----------------------------------------------------------------------===// 3272710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen 32898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen/// canEvict - Return true if all interferences between VirtReg and PhysReg can 32998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen/// be evicted. Set maxWeight to the maximal spill weight of an interference. 33098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesenbool RAGreedy::canEvictInterference(LiveInterval &VirtReg, unsigned PhysReg, 331d17924b1bd0329acb8be2d7dfc5fc4434c24b832Jakob Stoklund Olesen float &MaxWeight) { 33298c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen float Weight = 0; 33398c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) { 33498c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI); 33598c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen // If there is 10 or more interferences, chances are one is smaller. 33698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen if (Q.collectInterferingVRegs(10) >= 10) 33798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen return false; 33898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 339d17924b1bd0329acb8be2d7dfc5fc4434c24b832Jakob Stoklund Olesen // Check if any interfering live range is heavier than VirtReg. 34098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen for (unsigned i = 0, e = Q.interferingVRegs().size(); i != e; ++i) { 34198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen LiveInterval *Intf = Q.interferingVRegs()[i]; 34298c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen if (TargetRegisterInfo::isPhysicalRegister(Intf->reg)) 34398c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen return false; 344d17924b1bd0329acb8be2d7dfc5fc4434c24b832Jakob Stoklund Olesen if (Intf->weight >= VirtReg.weight) 34598c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen return false; 34698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen Weight = std::max(Weight, Intf->weight); 3472710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen } 3482710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen } 34998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen MaxWeight = Weight; 35098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen return true; 35198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen} 35298c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 35398c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen/// tryEvict - Try to evict all interferences for a physreg. 35498c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen/// @param VirtReg Currently unassigned virtual register. 35598c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen/// @param Order Physregs to try. 35698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen/// @return Physreg to assign VirtReg, or 0. 35798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesenunsigned RAGreedy::tryEvict(LiveInterval &VirtReg, 35898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen AllocationOrder &Order, 35998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen SmallVectorImpl<LiveInterval*> &NewVRegs){ 36098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen NamedRegionTimer T("Evict", TimerGroupName, TimePassesIsEnabled); 36198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 36298c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen // Keep track of the lightest single interference seen so far. 36398c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen float BestWeight = 0; 36498c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen unsigned BestPhys = 0; 36598c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 36698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen Order.rewind(); 36798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen while (unsigned PhysReg = Order.next()) { 36898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen float Weight = 0; 369d17924b1bd0329acb8be2d7dfc5fc4434c24b832Jakob Stoklund Olesen if (!canEvictInterference(VirtReg, PhysReg, Weight)) 37098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen continue; 37198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 37298c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen // This is an eviction candidate. 37398c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen DEBUG(dbgs() << "max " << PrintReg(PhysReg, TRI) << " interference = " 37498c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen << Weight << '\n'); 37598c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen if (BestPhys && Weight >= BestWeight) 37698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen continue; 37798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 37898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen // Best so far. 37998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen BestPhys = PhysReg; 38098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen BestWeight = Weight; 38157f1e2cee06f9b57995727d786aeb1031c5376bdJakob Stoklund Olesen // Stop if the hint can be used. 38257f1e2cee06f9b57995727d786aeb1031c5376bdJakob Stoklund Olesen if (Order.isHint(PhysReg)) 38357f1e2cee06f9b57995727d786aeb1031c5376bdJakob Stoklund Olesen break; 3842710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen } 3852710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen 38698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen if (!BestPhys) 38798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen return 0; 38898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 38998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen DEBUG(dbgs() << "evicting " << PrintReg(BestPhys, TRI) << " interference\n"); 39098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen for (const unsigned *AliasI = TRI->getOverlaps(BestPhys); *AliasI; ++AliasI) { 39198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI); 39298c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen assert(Q.seenAllInterferences() && "Didn't check all interfererences."); 39398c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen for (unsigned i = 0, e = Q.interferingVRegs().size(); i != e; ++i) { 39498c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen LiveInterval *Intf = Q.interferingVRegs()[i]; 39598c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen unassign(*Intf, VRM->getPhys(Intf->reg)); 39698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen ++NumEvicted; 39798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen NewVRegs.push_back(Intf); 39898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen } 39998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen } 40098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen return BestPhys; 401b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick} 402b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick 403770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen 404770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen//===----------------------------------------------------------------------===// 405b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen// Region Splitting 406b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen//===----------------------------------------------------------------------===// 407b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 40896dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen/// calcSplitConstraints - Fill out the SplitConstraints vector based on the 409eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen/// interference pattern in Physreg and its aliases. Return the static cost of 410eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen/// this split, assuming that all preferences in SplitConstraints are met. 411eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesenfloat RAGreedy::calcSplitConstraints(unsigned PhysReg) { 412eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen InterferenceCache::Cursor Intf(IntfCache, PhysReg); 413eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen 414b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen // Reset interference dependent info. 41596dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen SplitConstraints.resize(SA->LiveBlocks.size()); 41696dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen float StaticCost = 0; 417f0ac26c51173a9a1d6e5b5794107dccc4c5b5792Jakob Stoklund Olesen for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) { 418f0ac26c51173a9a1d6e5b5794107dccc4c5b5792Jakob Stoklund Olesen SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i]; 41996dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen SpillPlacement::BlockConstraint &BC = SplitConstraints[i]; 42096dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 421f0ac26c51173a9a1d6e5b5794107dccc4c5b5792Jakob Stoklund Olesen BC.Number = BI.MBB->getNumber(); 422eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen Intf.moveToBlock(BC.Number); 423b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen BC.Entry = (BI.Uses && BI.LiveIn) ? 424b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen SpillPlacement::PrefReg : SpillPlacement::DontCare; 425b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen BC.Exit = (BI.Uses && BI.LiveOut) ? 426b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen SpillPlacement::PrefReg : SpillPlacement::DontCare; 427b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 428eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (!Intf.hasInterference()) 429eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen continue; 430eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen 43196dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen // Number of spill code instructions to insert. 43296dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen unsigned Ins = 0; 43396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 43496dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen // Interference for the live-in value. 435eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (BI.LiveIn) { 4366c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen if (Intf.first() <= Indexes->getMBBStartIdx(BC.Number)) 43796dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen BC.Entry = SpillPlacement::MustSpill, Ins += BI.Uses; 43896dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen else if (!BI.Uses) 43996dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen BC.Entry = SpillPlacement::PrefSpill; 440eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen else if (Intf.first() < BI.FirstUse) 44196dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen BC.Entry = SpillPlacement::PrefSpill, ++Ins; 442eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen else if (Intf.first() < (BI.LiveThrough ? BI.LastUse : BI.Kill)) 44396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen ++Ins; 444a50c539b7a9e74597da34bfaea5429a48481f18bJakob Stoklund Olesen } 445b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 44696dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen // Interference for the live-out value. 447eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (BI.LiveOut) { 448eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (Intf.last() >= BI.LastSplitPoint) 44996dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen BC.Exit = SpillPlacement::MustSpill, Ins += BI.Uses; 45096dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen else if (!BI.Uses) 45196dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen BC.Exit = SpillPlacement::PrefSpill; 452eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen else if (Intf.last() > BI.LastUse) 45396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen BC.Exit = SpillPlacement::PrefSpill, ++Ins; 454eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen else if (Intf.last() > (BI.LiveThrough ? BI.FirstUse : BI.Def)) 45596dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen ++Ins; 456b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen } 457b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 45896dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen // Accumulate the total frequency of inserted spill code. 45996dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen if (Ins) 46096dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen StaticCost += Ins * SpillPlacer->getBlockFrequency(BC.Number); 461b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen } 46296dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen return StaticCost; 463b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen} 464b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 46596dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 466b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen/// calcGlobalSplitCost - Return the global split cost of following the split 467b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen/// pattern in LiveBundles. This cost should be added to the local cost of the 46896dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen/// interference pattern in SplitConstraints. 469b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen/// 470b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesenfloat RAGreedy::calcGlobalSplitCost(const BitVector &LiveBundles) { 471b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen float GlobalCost = 0; 472874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) { 473874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i]; 47496dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen SpillPlacement::BlockConstraint &BC = SplitConstraints[i]; 475874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen bool RegIn = LiveBundles[Bundles->getBundle(BC.Number, 0)]; 476874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen bool RegOut = LiveBundles[Bundles->getBundle(BC.Number, 1)]; 477874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen unsigned Ins = 0; 478874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen 479874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen if (!BI.Uses) 480874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen Ins += RegIn != RegOut; 481874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen else { 482874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen if (BI.LiveIn) 483874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen Ins += RegIn != (BC.Entry == SpillPlacement::PrefReg); 484874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen if (BI.LiveOut) 485874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen Ins += RegOut != (BC.Exit == SpillPlacement::PrefReg); 486874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen } 487874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen if (Ins) 488874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen GlobalCost += Ins * SpillPlacer->getBlockFrequency(BC.Number); 489b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen } 490b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen return GlobalCost; 491b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen} 492b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 493ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// splitAroundRegion - Split VirtReg around the region determined by 494ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// LiveBundles. Make an effort to avoid interference from PhysReg. 495ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// 496ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// The 'register' interval is going to contain as many uses as possible while 497ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// avoiding interference. The 'stack' interval is the complement constructed by 498ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// SplitEditor. It will contain the rest. 499ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// 500ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesenvoid RAGreedy::splitAroundRegion(LiveInterval &VirtReg, unsigned PhysReg, 501ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen const BitVector &LiveBundles, 502ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen SmallVectorImpl<LiveInterval*> &NewVRegs) { 503ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG({ 504ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen dbgs() << "Splitting around region for " << PrintReg(PhysReg, TRI) 505ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen << " with bundles"; 506ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen for (int i = LiveBundles.find_first(); i>=0; i = LiveBundles.find_next(i)) 507ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen dbgs() << " EB#" << i; 508ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen dbgs() << ".\n"; 509ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen }); 510ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 511eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen InterferenceCache::Cursor Intf(IntfCache, PhysReg); 51292a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen LiveRangeEdit LREdit(VirtReg, NewVRegs, this); 513bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->reset(LREdit); 514ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 515ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Create the main cross-block interval. 516bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->openIntv(); 517ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 518ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // First add all defs that are live out of a block. 519f0ac26c51173a9a1d6e5b5794107dccc4c5b5792Jakob Stoklund Olesen for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) { 520f0ac26c51173a9a1d6e5b5794107dccc4c5b5792Jakob Stoklund Olesen SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i]; 521ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen bool RegIn = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 0)]; 522ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen bool RegOut = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 1)]; 523ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 524ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Should the register be live out? 525ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (!BI.LiveOut || !RegOut) 526ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 527ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 5286c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SlotIndex Start, Stop; 5296c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen tie(Start, Stop) = Indexes->getMBBRange(BI.MBB); 530eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen Intf.moveToBlock(BI.MBB->getNumber()); 531ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " -> EB#" 5322dfbb3e9125aa0a66feab7a7638815b57da85968Jakob Stoklund Olesen << Bundles->getBundle(BI.MBB->getNumber(), 1) 5336c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen << " [" << Start << ';' << BI.LastSplitPoint << '-' 5346c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen << Stop << ") intf [" << Intf.first() << ';' << Intf.last() 535c46570dc05851395829bef904bb6ddb1260400d1Jakob Stoklund Olesen << ')'); 5362dfbb3e9125aa0a66feab7a7638815b57da85968Jakob Stoklund Olesen 5372dfbb3e9125aa0a66feab7a7638815b57da85968Jakob Stoklund Olesen // The interference interval should either be invalid or overlap MBB. 5386c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen assert((!Intf.hasInterference() || Intf.first() < Stop) 539eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen && "Bad interference"); 5406c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen assert((!Intf.hasInterference() || Intf.last() > Start) 54136d61863bc83bd2301e0224adc560098b35ec0dcJakob Stoklund Olesen && "Bad interference"); 542ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 543ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Check interference leaving the block. 544eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (!Intf.hasInterference()) { 545ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Block is interference-free. 546ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << ", no interference"); 547ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (!BI.Uses) { 548ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen assert(BI.LiveThrough && "No uses, but not live through block?"); 549ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Block is live-through without interference. 550ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << ", no uses" 551ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen << (RegIn ? ", live-through.\n" : ", stack in.\n")); 552ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (!RegIn) 553bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->enterIntvAtEnd(*BI.MBB); 554ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 555ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 556ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (!BI.LiveThrough) { 557ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << ", not live-through.\n"); 5586c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(SE->enterIntvBefore(BI.Def), Stop); 559ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 560ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 561ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (!RegIn) { 562ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Block is live-through, but entry bundle is on the stack. 563ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Reload just before the first use. 564ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << ", not live-in, enter before first use.\n"); 5656c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(SE->enterIntvBefore(BI.FirstUse), Stop); 566ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 567ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 568ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << ", live-through.\n"); 569ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 570ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 571ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 572ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Block has interference. 573eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen DEBUG(dbgs() << ", interference to " << Intf.last()); 574fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen 575eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (!BI.LiveThrough && Intf.last() <= BI.Def) { 576fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen // The interference doesn't reach the outgoing segment. 577fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen DEBUG(dbgs() << " doesn't affect def from " << BI.Def << '\n'); 5786c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(BI.Def, Stop); 579fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen continue; 580fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen } 581fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen 582fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen 583ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (!BI.Uses) { 584ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // No uses in block, avoid interference by reloading as late as possible. 585ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << ", no uses.\n"); 586bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SlotIndex SegStart = SE->enterIntvAtEnd(*BI.MBB); 587eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen assert(SegStart >= Intf.last() && "Couldn't avoid interference"); 588ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 589ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 590fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen 591eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (Intf.last().getBoundaryIndex() < BI.LastUse) { 592ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // There are interference-free uses at the end of the block. 593ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Find the first use that can get the live-out register. 594c0de99571297720a37ae405c77fb2ef4aaf00ccdJakob Stoklund Olesen SmallVectorImpl<SlotIndex>::const_iterator UI = 595fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen std::lower_bound(SA->UseSlots.begin(), SA->UseSlots.end(), 596eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen Intf.last().getBoundaryIndex()); 597c0de99571297720a37ae405c77fb2ef4aaf00ccdJakob Stoklund Olesen assert(UI != SA->UseSlots.end() && "Couldn't find last use"); 598c0de99571297720a37ae405c77fb2ef4aaf00ccdJakob Stoklund Olesen SlotIndex Use = *UI; 599c0de99571297720a37ae405c77fb2ef4aaf00ccdJakob Stoklund Olesen assert(Use <= BI.LastUse && "Couldn't find last use"); 6008a2bbdeee24b40da6187199658646d04329c139eJakob Stoklund Olesen // Only attempt a split befroe the last split point. 6018a2bbdeee24b40da6187199658646d04329c139eJakob Stoklund Olesen if (Use.getBaseIndex() <= BI.LastSplitPoint) { 6028a2bbdeee24b40da6187199658646d04329c139eJakob Stoklund Olesen DEBUG(dbgs() << ", free use at " << Use << ".\n"); 603bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SlotIndex SegStart = SE->enterIntvBefore(Use); 604eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen assert(SegStart >= Intf.last() && "Couldn't avoid interference"); 6058a2bbdeee24b40da6187199658646d04329c139eJakob Stoklund Olesen assert(SegStart < BI.LastSplitPoint && "Impossible split point"); 6066c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(SegStart, Stop); 6078a2bbdeee24b40da6187199658646d04329c139eJakob Stoklund Olesen continue; 6088a2bbdeee24b40da6187199658646d04329c139eJakob Stoklund Olesen } 609ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 610ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 611ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Interference is after the last use. 612ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << " after last use.\n"); 613bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SlotIndex SegStart = SE->enterIntvAtEnd(*BI.MBB); 614eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen assert(SegStart >= Intf.last() && "Couldn't avoid interference"); 615ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 616ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 617ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Now all defs leading to live bundles are handled, do everything else. 618f0ac26c51173a9a1d6e5b5794107dccc4c5b5792Jakob Stoklund Olesen for (unsigned i = 0, e = SA->LiveBlocks.size(); i != e; ++i) { 619f0ac26c51173a9a1d6e5b5794107dccc4c5b5792Jakob Stoklund Olesen SplitAnalysis::BlockInfo &BI = SA->LiveBlocks[i]; 620ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen bool RegIn = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 0)]; 621ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen bool RegOut = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 1)]; 622ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 623ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Is the register live-in? 624ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (!BI.LiveIn || !RegIn) 625ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 626ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 627ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // We have an incoming register. Check for interference. 6286c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SlotIndex Start, Stop; 6296c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen tie(Start, Stop) = Indexes->getMBBRange(BI.MBB); 630eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen Intf.moveToBlock(BI.MBB->getNumber()); 631ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << "EB#" << Bundles->getBundle(BI.MBB->getNumber(), 0) 6326c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen << " -> BB#" << BI.MBB->getNumber() << " [" << Start << ';' 6336c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen << BI.LastSplitPoint << '-' << Stop << ')'); 634ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 635ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Check interference entering the block. 636eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (!Intf.hasInterference()) { 637ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Block is interference-free. 638ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << ", no interference"); 639ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (!BI.Uses) { 640ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen assert(BI.LiveThrough && "No uses, but not live through block?"); 641ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Block is live-through without interference. 642ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (RegOut) { 643ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << ", no uses, live-through.\n"); 6446c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(Start, Stop); 645ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } else { 646ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << ", no uses, stack-out.\n"); 647bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->leaveIntvAtTop(*BI.MBB); 648ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 649ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 650ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 651ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (!BI.LiveThrough) { 652ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << ", killed in block.\n"); 6536c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(Start, SE->leaveIntvAfter(BI.Kill)); 654ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 655ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 656ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (!RegOut) { 657ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Block is live-through, but exit bundle is on the stack. 658ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Spill immediately after the last use. 6595c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen if (BI.LastUse < BI.LastSplitPoint) { 6605c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen DEBUG(dbgs() << ", uses, stack-out.\n"); 6616c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(Start, SE->leaveIntvAfter(BI.LastUse)); 6625c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen continue; 6635c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen } 6645c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen // The last use is after the last split point, it is probably an 6655c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen // indirect jump. 6665c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen DEBUG(dbgs() << ", uses at " << BI.LastUse << " after split point " 6675c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen << BI.LastSplitPoint << ", stack-out.\n"); 668bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SlotIndex SegEnd = SE->leaveIntvBefore(BI.LastSplitPoint); 6696c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(Start, SegEnd); 6705c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen // Run a double interval from the split to the last use. 6715c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen // This makes it possible to spill the complement without affecting the 6725c716bdccce2fa504e1aa0b67226165d181d2459Jakob Stoklund Olesen // indirect branch. 673bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->overlapIntv(SegEnd, BI.LastUse); 674ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 675ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 676ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Register is live-through. 677ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << ", uses, live-through.\n"); 6786c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(Start, Stop); 679ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 680ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 681ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 682ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Block has interference. 683eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen DEBUG(dbgs() << ", interference from " << Intf.first()); 684fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen 685eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (!BI.LiveThrough && Intf.first() >= BI.Kill) { 686fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen // The interference doesn't reach the outgoing segment. 687fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen DEBUG(dbgs() << " doesn't affect kill at " << BI.Kill << '\n'); 6886c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(Start, BI.Kill); 689fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen continue; 690fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen } 691fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen 692ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (!BI.Uses) { 693ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // No uses in block, avoid interference by spilling as soon as possible. 694ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << ", no uses.\n"); 695bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SlotIndex SegEnd = SE->leaveIntvAtTop(*BI.MBB); 696eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen assert(SegEnd <= Intf.first() && "Couldn't avoid interference"); 697ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 698ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 699eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (Intf.first().getBaseIndex() > BI.FirstUse) { 700ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // There are interference-free uses at the beginning of the block. 701ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Find the last use that can get the register. 702c0de99571297720a37ae405c77fb2ef4aaf00ccdJakob Stoklund Olesen SmallVectorImpl<SlotIndex>::const_iterator UI = 703fe3f99f95c7c9ceaac3ceebbea31e40cfbc157e3Jakob Stoklund Olesen std::lower_bound(SA->UseSlots.begin(), SA->UseSlots.end(), 704eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen Intf.first().getBaseIndex()); 705c0de99571297720a37ae405c77fb2ef4aaf00ccdJakob Stoklund Olesen assert(UI != SA->UseSlots.begin() && "Couldn't find first use"); 706c0de99571297720a37ae405c77fb2ef4aaf00ccdJakob Stoklund Olesen SlotIndex Use = (--UI)->getBoundaryIndex(); 707c0de99571297720a37ae405c77fb2ef4aaf00ccdJakob Stoklund Olesen DEBUG(dbgs() << ", free use at " << *UI << ".\n"); 708bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SlotIndex SegEnd = SE->leaveIntvAfter(Use); 709eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen assert(SegEnd <= Intf.first() && "Couldn't avoid interference"); 7106c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen SE->useIntv(Start, SegEnd); 711ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 712ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 713ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 714ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Interference is before the first use. 715ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen DEBUG(dbgs() << " before first use.\n"); 716bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SlotIndex SegEnd = SE->leaveIntvAtTop(*BI.MBB); 717eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen assert(SegEnd <= Intf.first() && "Couldn't avoid interference"); 718ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 719ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 720bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->closeIntv(); 721ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 722ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // FIXME: Should we be more aggressive about splitting the stack region into 723ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // per-block segments? The current approach allows the stack region to 724ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // separate into connected components. Some components may be allocatable. 725bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->finish(); 7260db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen ++NumGlobalSplits; 727ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 728eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen if (VerifyEnabled) 729ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen MF->verify(this, "After splitting live range around region"); 730ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen} 731ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 732b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesenunsigned RAGreedy::tryRegionSplit(LiveInterval &VirtReg, AllocationOrder &Order, 733b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen SmallVectorImpl<LiveInterval*> &NewVRegs) { 734b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen BitVector LiveBundles, BestBundles; 735b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen float BestCost = 0; 736b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen unsigned BestReg = 0; 73796dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 738b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen Order.rewind(); 73996dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen for (unsigned Cand = 0; unsigned PhysReg = Order.next(); ++Cand) { 74096dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen if (GlobalCand.size() <= Cand) 74196dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen GlobalCand.resize(Cand+1); 74296dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen GlobalCand[Cand].PhysReg = PhysReg; 74396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 744eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen float Cost = calcSplitConstraints(PhysReg); 745874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tstatic = " << Cost); 746874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen if (BestReg && Cost >= BestCost) { 747874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen DEBUG(dbgs() << " higher.\n"); 748b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen continue; 749874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen } 750ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 75196dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen SpillPlacer->placeSpills(SplitConstraints, LiveBundles); 752ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // No live bundles, defer to splitSingleBlocks(). 753874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen if (!LiveBundles.any()) { 754874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen DEBUG(dbgs() << " no bundles.\n"); 755ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 756874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen } 757ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 758ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen Cost += calcGlobalSplitCost(LiveBundles); 759874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen DEBUG({ 760874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen dbgs() << ", total = " << Cost << " with bundles"; 761874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen for (int i = LiveBundles.find_first(); i>=0; i = LiveBundles.find_next(i)) 762874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen dbgs() << " EB#" << i; 763874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen dbgs() << ".\n"; 764874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen }); 765b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen if (!BestReg || Cost < BestCost) { 766b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen BestReg = PhysReg; 767874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen BestCost = 0.98f * Cost; // Prevent rounding effects. 768b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen BestBundles.swap(LiveBundles); 769b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen } 770b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen } 771ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 772ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (!BestReg) 773ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen return 0; 774ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 775ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen splitAroundRegion(VirtReg, BestReg, BestBundles, NewVRegs); 77622a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen setStage(NewVRegs.begin(), NewVRegs.end(), RS_Region); 777b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen return 0; 778b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen} 779b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 780ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 781ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen//===----------------------------------------------------------------------===// 782034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen// Local Splitting 783034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen//===----------------------------------------------------------------------===// 784034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 785034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 786034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// calcGapWeights - Compute the maximum spill weight that needs to be evicted 787034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// in order to use PhysReg between two entries in SA->UseSlots. 788034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// 789034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// GapWeight[i] represents the gap between UseSlots[i] and UseSlots[i+1]. 790034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// 791034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesenvoid RAGreedy::calcGapWeights(unsigned PhysReg, 792034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SmallVectorImpl<float> &GapWeight) { 793034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen assert(SA->LiveBlocks.size() == 1 && "Not a local interval"); 794034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const SplitAnalysis::BlockInfo &BI = SA->LiveBlocks.front(); 795034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots; 796034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const unsigned NumGaps = Uses.size()-1; 797034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 798034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Start and end points for the interference check. 799034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SlotIndex StartIdx = BI.LiveIn ? BI.FirstUse.getBaseIndex() : BI.FirstUse; 800034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SlotIndex StopIdx = BI.LiveOut ? BI.LastUse.getBoundaryIndex() : BI.LastUse; 801034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 802034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen GapWeight.assign(NumGaps, 0.0f); 803034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 804034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Add interference from each overlapping register. 805034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) { 806034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (!query(const_cast<LiveInterval&>(SA->getParent()), *AI) 807034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen .checkInterference()) 808034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen continue; 809034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 810034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // We know that VirtReg is a continuous interval from FirstUse to LastUse, 811034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // so we don't need InterferenceQuery. 812034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // 813034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Interference that overlaps an instruction is counted in both gaps 814034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // surrounding the instruction. The exception is interference before 815034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // StartIdx and after StopIdx. 816034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // 817034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen LiveIntervalUnion::SegmentIter IntI = PhysReg2LiveUnion[*AI].find(StartIdx); 818034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (unsigned Gap = 0; IntI.valid() && IntI.start() < StopIdx; ++IntI) { 819034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Skip the gaps before IntI. 820034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen while (Uses[Gap+1].getBoundaryIndex() < IntI.start()) 821034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (++Gap == NumGaps) 822034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen break; 823034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Gap == NumGaps) 824034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen break; 825034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 826034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Update the gaps covered by IntI. 827034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const float weight = IntI.value()->weight; 828034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (; Gap != NumGaps; ++Gap) { 829034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen GapWeight[Gap] = std::max(GapWeight[Gap], weight); 830034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Uses[Gap+1].getBaseIndex() >= IntI.stop()) 831034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen break; 832034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 833034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Gap == NumGaps) 834034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen break; 835034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 836034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 837034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen} 838034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 839034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// getPrevMappedIndex - Return the slot index of the last non-copy instruction 840034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// before MI that has a slot index. If MI is the first mapped instruction in 841034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// its block, return the block start index instead. 842034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// 843034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund OlesenSlotIndex RAGreedy::getPrevMappedIndex(const MachineInstr *MI) { 844034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen assert(MI && "Missing MachineInstr"); 845034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const MachineBasicBlock *MBB = MI->getParent(); 846034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen MachineBasicBlock::const_iterator B = MBB->begin(), I = MI; 847034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen while (I != B) 848034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (!(--I)->isDebugValue() && !I->isCopy()) 849034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen return Indexes->getInstructionIndex(I); 850034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen return Indexes->getMBBStartIdx(MBB); 851034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen} 852034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 853034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// calcPrevSlots - Fill in the PrevSlot array with the index of the previous 854034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// real non-copy instruction for each instruction in SA->UseSlots. 855034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// 856034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesenvoid RAGreedy::calcPrevSlots() { 857034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots; 858034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen PrevSlot.clear(); 859034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen PrevSlot.reserve(Uses.size()); 860034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (unsigned i = 0, e = Uses.size(); i != e; ++i) { 861034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const MachineInstr *MI = Indexes->getInstructionFromIndex(Uses[i]); 862034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen PrevSlot.push_back(getPrevMappedIndex(MI).getDefIndex()); 863034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 864034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen} 865034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 866034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// nextSplitPoint - Find the next index into SA->UseSlots > i such that it may 867034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// be beneficial to split before UseSlots[i]. 868034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// 869034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// 0 is always a valid split point 870034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesenunsigned RAGreedy::nextSplitPoint(unsigned i) { 871034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots; 872034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const unsigned Size = Uses.size(); 873034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen assert(i != Size && "No split points after the end"); 874034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Allow split before i when Uses[i] is not adjacent to the previous use. 875034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen while (++i != Size && PrevSlot[i].getBaseIndex() <= Uses[i-1].getBaseIndex()) 876034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen ; 877034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen return i; 878034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen} 879034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 880034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// tryLocalSplit - Try to split VirtReg into smaller intervals inside its only 881034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// basic block. 882034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// 883034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesenunsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order, 884034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SmallVectorImpl<LiveInterval*> &NewVRegs) { 885034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen assert(SA->LiveBlocks.size() == 1 && "Not a local interval"); 886034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const SplitAnalysis::BlockInfo &BI = SA->LiveBlocks.front(); 887034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 888034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Note that it is possible to have an interval that is live-in or live-out 889034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // while only covering a single block - A phi-def can use undef values from 890034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // predecessors, and the block could be a single-block loop. 891034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // We don't bother doing anything clever about such a case, we simply assume 892034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // that the interval is continuous from FirstUse to LastUse. We should make 893034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // sure that we don't do anything illegal to such an interval, though. 894034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 895034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots; 896034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Uses.size() <= 2) 897034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen return 0; 898034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const unsigned NumGaps = Uses.size()-1; 899034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 900034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG({ 901034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen dbgs() << "tryLocalSplit: "; 902034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (unsigned i = 0, e = Uses.size(); i != e; ++i) 903034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen dbgs() << ' ' << SA->UseSlots[i]; 904034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen dbgs() << '\n'; 905034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen }); 906034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 907034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // For every use, find the previous mapped non-copy instruction. 908034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // We use this to detect valid split points, and to estimate new interval 909034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // sizes. 910034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen calcPrevSlots(); 911034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 912034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen unsigned BestBefore = NumGaps; 913034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen unsigned BestAfter = 0; 914034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen float BestDiff = 0; 915034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 91640a42a2ccaaa19a109667ed7abf224cc8733cd9cJakob Stoklund Olesen const float blockFreq = SpillPlacer->getBlockFrequency(BI.MBB->getNumber()); 917034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SmallVector<float, 8> GapWeight; 918034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 919034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen Order.rewind(); 920034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen while (unsigned PhysReg = Order.next()) { 921034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Keep track of the largest spill weight that would need to be evicted in 922034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // order to make use of PhysReg between UseSlots[i] and UseSlots[i+1]. 923034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen calcGapWeights(PhysReg, GapWeight); 924034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 925034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Try to find the best sequence of gaps to close. 926034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // The new spill weight must be larger than any gap interference. 927034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 928034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // We will split before Uses[SplitBefore] and after Uses[SplitAfter]. 929034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen unsigned SplitBefore = 0, SplitAfter = nextSplitPoint(1) - 1; 930034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 931034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // MaxGap should always be max(GapWeight[SplitBefore..SplitAfter-1]). 932034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // It is the spill weight that needs to be evicted. 933034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen float MaxGap = GapWeight[0]; 934034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (unsigned i = 1; i != SplitAfter; ++i) 935034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen MaxGap = std::max(MaxGap, GapWeight[i]); 936034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 937034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (;;) { 938034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Live before/after split? 939034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const bool LiveBefore = SplitBefore != 0 || BI.LiveIn; 940034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const bool LiveAfter = SplitAfter != NumGaps || BI.LiveOut; 941034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 942034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << PrintReg(PhysReg, TRI) << ' ' 943034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen << Uses[SplitBefore] << '-' << Uses[SplitAfter] 944034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen << " i=" << MaxGap); 945034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 946034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Stop before the interval gets so big we wouldn't be making progress. 947034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (!LiveBefore && !LiveAfter) { 948034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << " all\n"); 949034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen break; 950034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 951034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Should the interval be extended or shrunk? 952034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen bool Shrink = true; 953034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (MaxGap < HUGE_VALF) { 954034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Estimate the new spill weight. 955034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // 956034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Each instruction reads and writes the register, except the first 957034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // instr doesn't read when !FirstLive, and the last instr doesn't write 958034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // when !LastLive. 959034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // 960034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // We will be inserting copies before and after, so the total number of 961034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // reads and writes is 2 * EstUses. 962034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // 963034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const unsigned EstUses = 2*(SplitAfter - SplitBefore) + 964034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 2*(LiveBefore + LiveAfter); 965034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 966034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Try to guess the size of the new interval. This should be trivial, 967034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // but the slot index of an inserted copy can be a lot smaller than the 968034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // instruction it is inserted before if there are many dead indexes 969034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // between them. 970034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // 971034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // We measure the distance from the instruction before SplitBefore to 972034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // get a conservative estimate. 973034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // 974034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // The final distance can still be different if inserting copies 975034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // triggers a slot index renumbering. 976034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // 977034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const float EstWeight = normalizeSpillWeight(blockFreq * EstUses, 978034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen PrevSlot[SplitBefore].distance(Uses[SplitAfter])); 979034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Would this split be possible to allocate? 980034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Never allocate all gaps, we wouldn't be making progress. 981034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen float Diff = EstWeight - MaxGap; 982034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << " w=" << EstWeight << " d=" << Diff); 983034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Diff > 0) { 984034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen Shrink = false; 985034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Diff > BestDiff) { 986034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << " (best)"); 987034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen BestDiff = Diff; 988034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen BestBefore = SplitBefore; 989034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen BestAfter = SplitAfter; 990034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 991034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 992034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 993034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 994034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Try to shrink. 995034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Shrink) { 996034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SplitBefore = nextSplitPoint(SplitBefore); 997034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (SplitBefore < SplitAfter) { 998034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << " shrink\n"); 999034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Recompute the max when necessary. 1000034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (GapWeight[SplitBefore - 1] >= MaxGap) { 1001034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen MaxGap = GapWeight[SplitBefore]; 1002034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (unsigned i = SplitBefore + 1; i != SplitAfter; ++i) 1003034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen MaxGap = std::max(MaxGap, GapWeight[i]); 1004034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1005034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen continue; 1006034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1007034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen MaxGap = 0; 1008034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1009034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1010034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Try to extend the interval. 1011034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (SplitAfter >= NumGaps) { 1012034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << " end\n"); 1013034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen break; 1014034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1015034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1016034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << " extend\n"); 1017034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (unsigned e = nextSplitPoint(SplitAfter + 1) - 1; 1018034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SplitAfter != e; ++SplitAfter) 1019034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen MaxGap = std::max(MaxGap, GapWeight[SplitAfter]); 1020034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen continue; 1021034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1022034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1023034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1024034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Didn't find any candidates? 1025034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (BestBefore == NumGaps) 1026034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen return 0; 1027034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1028034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << "Best local split range: " << Uses[BestBefore] 1029034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen << '-' << Uses[BestAfter] << ", " << BestDiff 1030034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen << ", " << (BestAfter - BestBefore + 1) << " instrs\n"); 1031034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 103292a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen LiveRangeEdit LREdit(VirtReg, NewVRegs, this); 1033bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->reset(LREdit); 1034bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen 1035bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->openIntv(); 1036bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SlotIndex SegStart = SE->enterIntvBefore(Uses[BestBefore]); 1037bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SlotIndex SegStop = SE->leaveIntvAfter(Uses[BestAfter]); 1038bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->useIntv(SegStart, SegStop); 1039bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->closeIntv(); 1040bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->finish(); 104122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen setStage(NewVRegs.begin(), NewVRegs.end(), RS_Local); 10420db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen ++NumLocalSplits; 1043034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1044034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen return 0; 1045034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen} 1046034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1047034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen//===----------------------------------------------------------------------===// 1048ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen// Live Range Splitting 1049ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen//===----------------------------------------------------------------------===// 1050ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 1051ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// trySplit - Try to split VirtReg or one of its interferences, making it 1052ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// assignable. 1053ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// @return Physreg when VirtReg may be assigned and/or new NewVRegs. 1054ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesenunsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order, 1055ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen SmallVectorImpl<LiveInterval*>&NewVRegs) { 1056034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Local intervals are handled separately. 1057a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen if (LIS->intervalIsInOneMBB(VirtReg)) { 1058a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen NamedRegionTimer T("Local Splitting", TimerGroupName, TimePassesIsEnabled); 105922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen SA->analyze(&VirtReg); 1060034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen return tryLocalSplit(VirtReg, Order, NewVRegs); 1061a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen } 1062a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen 1063a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen NamedRegionTimer T("Global Splitting", TimerGroupName, TimePassesIsEnabled); 1064ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 106522a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // Don't iterate global splitting. 106622a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // Move straight to spilling if this range was produced by a global split. 106722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen LiveRangeStage Stage = getStage(VirtReg); 106822a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen if (Stage >= RS_Block) 106922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen return 0; 107022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen 107122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen SA->analyze(&VirtReg); 107222a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen 1073ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // First try to split around a region spanning multiple blocks. 107422a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen if (Stage < RS_Region) { 107522a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen unsigned PhysReg = tryRegionSplit(VirtReg, Order, NewVRegs); 107622a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen if (PhysReg || !NewVRegs.empty()) 107722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen return PhysReg; 107822a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen } 1079ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 1080ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Then isolate blocks with multiple uses. 108122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen if (Stage < RS_Block) { 108222a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen SplitAnalysis::BlockPtrSet Blocks; 108322a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen if (SA->getMultiUseBlocks(Blocks)) { 108492a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen LiveRangeEdit LREdit(VirtReg, NewVRegs, this); 1085bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->reset(LREdit); 1086bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->splitSingleBlocks(Blocks); 108722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen setStage(NewVRegs.begin(), NewVRegs.end(), RS_Block); 108822a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen if (VerifyEnabled) 108922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen MF->verify(this, "After splitting live range around basic blocks"); 109022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen } 1091ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 1092ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 1093ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // Don't assign any physregs. 1094ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen return 0; 1095ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen} 1096ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 1097ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 1098b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen//===----------------------------------------------------------------------===// 1099770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen// Main Entry Point 1100770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen//===----------------------------------------------------------------------===// 1101770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen 1102770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesenunsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg, 1103ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen SmallVectorImpl<LiveInterval*> &NewVRegs) { 1104770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen // First try assigning a free register. 1105dd479e9769eceee9fcc34e2173376024f3aa3c5fJakob Stoklund Olesen AllocationOrder Order(VirtReg.reg, *VRM, ReservedRegs); 1106dd479e9769eceee9fcc34e2173376024f3aa3c5fJakob Stoklund Olesen while (unsigned PhysReg = Order.next()) { 1107770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen if (!checkPhysRegInterference(VirtReg, PhysReg)) 1108cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen return PhysReg; 1109cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen } 1110b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick 111198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen if (unsigned PhysReg = tryEvict(VirtReg, Order, NewVRegs)) 111246c83c80c5737743c955ff007fa6409804a7abf0Jakob Stoklund Olesen return PhysReg; 1113b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick 1114ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen assert(NewVRegs.empty() && "Cannot append to existing NewVRegs"); 1115ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 1116107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen // The first time we see a live range, don't try to split or spill. 1117107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen // Wait until the second time, when all smaller ranges have been allocated. 1118107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen // This gives a better picture of the interference to split around. 1119eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen LiveRangeStage Stage = getStage(VirtReg); 1120f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen if (Stage == RS_First) { 1121eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen LRStage[VirtReg.reg] = RS_Second; 1122c1655e1a3c3a566b91b0513b56d61b58da1e36baJakob Stoklund Olesen DEBUG(dbgs() << "wait for second round\n"); 1123107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen NewVRegs.push_back(&VirtReg); 1124107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen return 0; 1125107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen } 1126107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen 112722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen assert(Stage < RS_Spill && "Cannot allocate after spilling"); 112822a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen 112946c83c80c5737743c955ff007fa6409804a7abf0Jakob Stoklund Olesen // Try splitting VirtReg or interferences. 1130ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen unsigned PhysReg = trySplit(VirtReg, Order, NewVRegs); 1131ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (PhysReg || !NewVRegs.empty()) 1132b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen return PhysReg; 1133b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen 1134770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen // Finally spill VirtReg itself. 1135770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled); 113647dbf6cef761c25cfeb0aa7d624a6f98288bb96aJakob Stoklund Olesen LiveRangeEdit LRE(VirtReg, NewVRegs, this); 113747dbf6cef761c25cfeb0aa7d624a6f98288bb96aJakob Stoklund Olesen spiller().spill(LRE); 11386094bd87d845afabba5b99ec4848fa6116bac682Jakob Stoklund Olesen setStage(NewVRegs.begin(), NewVRegs.end(), RS_Spill); 1139cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 1140c46570dc05851395829bef904bb6ddb1260400d1Jakob Stoklund Olesen if (VerifyEnabled) 1141c46570dc05851395829bef904bb6ddb1260400d1Jakob Stoklund Olesen MF->verify(this, "After spilling"); 1142c46570dc05851395829bef904bb6ddb1260400d1Jakob Stoklund Olesen 1143cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen // The live virtual register requesting allocation was spilled, so tell 1144cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen // the caller not to allocate anything during this round. 1145cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen return 0; 1146cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} 1147cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 1148cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenbool RAGreedy::runOnMachineFunction(MachineFunction &mf) { 1149cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n" 1150cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen << "********** Function: " 1151cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen << ((Value*)mf.getFunction())->getName() << '\n'); 1152cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 1153cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen MF = &mf; 1154af24964251e27c2dd863239ba66ffd967b593be5Jakob Stoklund Olesen if (VerifyEnabled) 115589cab93fe999f6d81b4b99a71ac797b7ecfec277Jakob Stoklund Olesen MF->verify(this, "Before greedy register allocator"); 1156af24964251e27c2dd863239ba66ffd967b593be5Jakob Stoklund Olesen 11574680dec5fb3a1b624f13ca9b2a555ca90a07973eJakob Stoklund Olesen RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>()); 1158b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen Indexes = &getAnalysis<SlotIndexes>(); 1159f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen DomTree = &getAnalysis<MachineDominatorTree>(); 1160cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen ReservedRegs = TRI->getReservedRegs(*MF); 1161f6dff84d4e44d6c4a46c4f8a18e13c78f804547cJakob Stoklund Olesen SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM)); 1162d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen Loops = &getAnalysis<MachineLoopInfo>(); 1163d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen LoopRanges = &getAnalysis<MachineLoopRanges>(); 1164b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen Bundles = &getAnalysis<EdgeBundles>(); 1165b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen SpillPlacer = &getAnalysis<SpillPlacement>(); 1166b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 11671b847deb26b52051de39f4cbecd224c9fbd0d1c2Jakob Stoklund Olesen SA.reset(new SplitAnalysis(*VRM, *LIS, *Loops)); 1168bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE.reset(new SplitEditor(*SA, *LIS, *VRM, *DomTree)); 116922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen LRStage.clear(); 117022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen LRStage.resize(MRI->getNumVirtRegs()); 1171eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen IntfCache.init(MF, &PhysReg2LiveUnion[0], Indexes, TRI); 1172d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen 1173cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen allocatePhysRegs(); 1174cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen addMBBLiveIns(MF); 11758a61da8a689ee95874c833af4c7aa965fab5c0a9Jakob Stoklund Olesen LIS->addKillFlags(); 1176cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 1177cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen // Run rewriter 1178533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen { 1179533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen NamedRegionTimer T("Rewriter", TimerGroupName, TimePassesIsEnabled); 1180ba05c01dabc40373760a20c874103fc58d4377f0Jakob Stoklund Olesen VRM->rewrite(Indexes); 1181533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen } 1182cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 1183cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen // The pass output is in VirtRegMap. Release all the transient data. 1184cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen releaseMemory(); 1185cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 1186cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen return true; 1187cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} 1188