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 15d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/CodeGen/Passes.h" 16dd479e9769eceee9fcc34e2173376024f3aa3c5fJakob Stoklund Olesen#include "AllocationOrder.h" 175907d863659eb972ebb2afe07bc863a4c616f0efJakob Stoklund Olesen#include "InterferenceCache.h" 18cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen#include "LiveDebugVariables.h" 19cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "RegAllocBase.h" 20b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen#include "SpillPlacement.h" 21d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "Spiller.h" 22d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen#include "SplitKit.h" 230db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen#include "llvm/ADT/Statistic.h" 24cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Analysis/AliasAnalysis.h" 25cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/CalcSpillWeights.h" 26b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen#include "llvm/CodeGen/EdgeBundles.h" 27cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/LiveIntervalAnalysis.h" 28789d5d85ba6e9259a8e0f0bcfbd06a59ad164512Pete Cooper#include "llvm/CodeGen/LiveRangeEdit.h" 291ead68d769f27f6d68d4aaeffe4199fa2cacbc95Jakob Stoklund Olesen#include "llvm/CodeGen/LiveRegMatrix.h" 30cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/LiveStackAnalysis.h" 314eed756153b84c211114a3e9186bf0cb55d4b394Benjamin Kramer#include "llvm/CodeGen/MachineBlockFrequencyInfo.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" 35cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/MachineRegisterInfo.h" 36cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/RegAllocRegistry.h" 3736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/CodeGen/RegisterClassInfo.h" 38d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/CodeGen/VirtRegMap.h" 39dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#include "llvm/IR/LLVMContext.h" 40d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/PassAnalysisSupport.h" 41dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#include "llvm/Support/BranchProbability.h" 4200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen#include "llvm/Support/CommandLine.h" 43cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Support/Debug.h" 44cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Support/ErrorHandling.h" 45533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen#include "llvm/Support/Timer.h" 46d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Support/raw_ostream.h" 47c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines#include "llvm/Target/TargetSubtargetInfo.h" 4898d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen#include <queue> 4998d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen 50cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenusing namespace llvm; 51cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 52dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#define DEBUG_TYPE "regalloc" 53dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 540db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund OlesenSTATISTIC(NumGlobalSplits, "Number of split global live ranges"); 550db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund OlesenSTATISTIC(NumLocalSplits, "Number of split local live ranges"); 560db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund OlesenSTATISTIC(NumEvicted, "Number of interferences evicted"); 570db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen 58708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund Olesenstatic cl::opt<SplitEditor::ComplementSpillMode> 59708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund OlesenSplitSpillMode("split-spill-mode", cl::Hidden, 60708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund Olesen cl::desc("Spill mode for splitting live ranges"), 61708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund Olesen cl::values(clEnumValN(SplitEditor::SM_Partition, "default", "Default"), 62708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund Olesen clEnumValN(SplitEditor::SM_Size, "size", "Optimize for size"), 63708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund Olesen clEnumValN(SplitEditor::SM_Speed, "speed", "Optimize for speed"), 64708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund Olesen clEnumValEnd), 65708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund Olesen cl::init(SplitEditor::SM_Partition)); 66708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund Olesen 6736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesstatic cl::opt<unsigned> 6836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinesLastChanceRecoloringMaxDepth("lcr-max-depth", cl::Hidden, 6936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines cl::desc("Last chance recoloring max depth"), 7036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines cl::init(5)); 7136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 7236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesstatic cl::opt<unsigned> LastChanceRecoloringMaxInterference( 7336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines "lcr-max-interf", cl::Hidden, 7436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines cl::desc("Last chance recoloring maximum number of considered" 7536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines " interference at a time"), 7636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines cl::init(8)); 7736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 78dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesstatic cl::opt<bool> 79dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen HinesExhaustiveSearch("exhaustive-register-search", cl::NotHidden, 80dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines cl::desc("Exhaustive Search for registers bypassing the depth " 81dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines "and interference cutoffs of last chance recoloring")); 82dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 83c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hinesstatic cl::opt<bool> EnableLocalReassignment( 84c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines "enable-local-reassign", cl::Hidden, 85c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines cl::desc("Local reassignment can yield better allocation decisions, but " 86c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines "may be compile time intensive"), 87c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines cl::init(false)); 88c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines 89cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarstatic cl::opt<bool> EnableDeferredSpilling( 90cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar "enable-deferred-spilling", cl::Hidden, 91cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar cl::desc("Instead of spilling a variable right away, defer the actual " 92cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar "code insertion to the end of the allocation. That way the " 93cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar "allocator might still find a suitable coloring for this " 94cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar "variable because of other evicted variables."), 95cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar cl::init(false)); 96cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 9736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// FIXME: Find a good default for this flag and remove the flag. 9836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesstatic cl::opt<unsigned> 9936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinesCSRFirstTimeCost("regalloc-csr-first-time-cost", 10036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines cl::desc("Cost for first time use of callee-saved register."), 10136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines cl::init(0), cl::Hidden); 10236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 103cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenstatic RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator", 104cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen createGreedyRegisterAllocator); 105cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 106cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesennamespace { 10792a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesenclass RAGreedy : public MachineFunctionPass, 10892a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen public RegAllocBase, 10992a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen private LiveRangeEdit::Delegate { 11036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Convenient shortcuts. 11136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines typedef std::priority_queue<std::pair<unsigned, unsigned> > PQueue; 11236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines typedef SmallPtrSet<LiveInterval *, 4> SmallLISet; 11336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines typedef SmallSet<unsigned, 16> SmallVirtRegSet; 11492a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen 115cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen // context 116cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen MachineFunction *MF; 117cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 11836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Shortcuts to some useful interface. 11936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const TargetInstrInfo *TII; 12036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const TargetRegisterInfo *TRI; 12136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines RegisterClassInfo RCI; 12236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 123cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen // analyses 124b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen SlotIndexes *Indexes; 1254eed756153b84c211114a3e9186bf0cb55d4b394Benjamin Kramer MachineBlockFrequencyInfo *MBFI; 126f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen MachineDominatorTree *DomTree; 127d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen MachineLoopInfo *Loops; 128b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen EdgeBundles *Bundles; 129b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen SpillPlacement *SpillPlacer; 130f42b66169d75301346e3685fd2b3e45e47806367Jakob Stoklund Olesen LiveDebugVariables *DebugVars; 131f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen 132cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen // state 13336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines std::unique_ptr<Spiller> SpillerInstance; 13436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines PQueue Queue; 1351a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen unsigned NextCascade; 13622a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen 13722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // Live ranges pass through a number of stages as we try to allocate them. 13822a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // Some of the stages may also create new live ranges: 13922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // 14022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // - Region splitting. 14122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // - Per-block splitting. 14222a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // - Local splitting. 14322a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // - Spilling. 14422a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // 14522a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // Ranges produced by one of the stages skip the previous stages when they are 14622a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // dequeued. This improves performance because we can skip interference checks 14722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // that are unlikely to give any results. It also guarantees that the live 14822a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // range splitting algorithm terminates, something that is otherwise hard to 14922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen // ensure. 15022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen enum LiveRangeStage { 151fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen /// Newly created live range that has never been queued. 152fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen RS_New, 153fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen 154fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen /// Only attempt assignment and eviction. Then requeue as RS_Split. 155fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen RS_Assign, 156fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen 157fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen /// Attempt live range splitting if assignment is impossible. 158fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen RS_Split, 159fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen 16049743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen /// Attempt more aggressive live range splitting that is guaranteed to make 16149743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen /// progress. This is used for split products that may not be making 16249743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen /// progress. 16349743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen RS_Split2, 16449743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen 165fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen /// Live range will be spilled. No more splitting will be attempted. 166fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen RS_Spill, 167fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen 168cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 169cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar /// Live range is in memory. Because of other evictions, it might get moved 170cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar /// in a register in the end. 171cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar RS_Memory, 172cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 173fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen /// There is nothing more we can do to this live range. Abort compilation 174fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen /// if it can't be assigned. 175fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen RS_Done 17622a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen }; 17722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen 178dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines // Enum CutOffStage to keep a track whether the register allocation failed 179dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines // because of the cutoffs encountered in last chance recoloring. 180dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines // Note: This is used as bitmask. New value should be next power of 2. 181dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines enum CutOffStage { 182dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines // No cutoffs encountered 183dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines CO_None = 0, 184dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 185dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines // lcr-max-depth cutoff encountered 186dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines CO_Depth = 1, 187dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 188dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines // lcr-max-interf cutoff encountered 189dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines CO_Interf = 2 190dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines }; 191dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 192dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines uint8_t CutOffInfo; 193dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 194ae43dac30037395cce2b54af0a02500985813183Eli Friedman#ifndef NDEBUG 195b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen static const char *const StageName[]; 196ae43dac30037395cce2b54af0a02500985813183Eli Friedman#endif 197b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen 1981a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen // RegInfo - Keep additional information about each live range. 1991a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen struct RegInfo { 2001a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen LiveRangeStage Stage; 2011a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen 2021a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen // Cascade - Eviction loop prevention. See canEvictInterference(). 2031a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen unsigned Cascade; 2041a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen 2051a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen RegInfo() : Stage(RS_New), Cascade(0) {} 2061a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen }; 2071a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen 2081a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen IndexedMap<RegInfo, VirtReg2IndexFunctor> ExtraRegInfo; 20922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen 21022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen LiveRangeStage getStage(const LiveInterval &VirtReg) const { 2111a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen return ExtraRegInfo[VirtReg.reg].Stage; 2121a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen } 2131a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen 2141a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen void setStage(const LiveInterval &VirtReg, LiveRangeStage Stage) { 2151a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen ExtraRegInfo.resize(MRI->getNumVirtRegs()); 2161a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen ExtraRegInfo[VirtReg.reg].Stage = Stage; 21722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen } 21822a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen 21922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen template<typename Iterator> 22022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen void setStage(Iterator Begin, Iterator End, LiveRangeStage NewStage) { 2211a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen ExtraRegInfo.resize(MRI->getNumVirtRegs()); 222f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen for (;Begin != End; ++Begin) { 2231feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey unsigned Reg = *Begin; 2241a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen if (ExtraRegInfo[Reg].Stage == RS_New) 2251a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen ExtraRegInfo[Reg].Stage = NewStage; 226f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen } 22722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen } 228cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 22951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen /// Cost of evicting interference. 23051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen struct EvictionCost { 23151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen unsigned BrokenHints; ///< Total number of broken hints. 23251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen float MaxWeight; ///< Maximum spill weight evicted. 23351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen 23436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines EvictionCost(): BrokenHints(0), MaxWeight(0) {} 23551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen 2366ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick bool isMax() const { return BrokenHints == ~0u; } 2376ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick 23836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void setMax() { BrokenHints = ~0u; } 23936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 24036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void setBrokenHints(unsigned NHints) { BrokenHints = NHints; } 24136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 24251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen bool operator<(const EvictionCost &O) const { 24336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return std::tie(BrokenHints, MaxWeight) < 24436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines std::tie(O.BrokenHints, O.MaxWeight); 24551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen } 24651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen }; 24751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen 248b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen // splitting state. 24936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines std::unique_ptr<SplitAnalysis> SA; 25036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines std::unique_ptr<SplitEditor> SE; 251b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 252eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen /// Cached per-block interference maps 253eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen InterferenceCache IntfCache; 254eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen 2557b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen /// All basic blocks where the current register has uses. 25696dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen SmallVector<SpillPlacement::BlockConstraint, 8> SplitConstraints; 257b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 25896dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen /// Global live range splitting candidate info. 25996dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen struct GlobalSplitCandidate { 26000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // Register intended for assignment, or 0. 26196dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen unsigned PhysReg; 26200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen 26300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // SplitKit interval index for this candidate. 26400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen unsigned IntvIdx; 26500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen 26600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // Interference for PhysReg. 267c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen InterferenceCache::Cursor Intf; 26800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen 26900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // Bundles where this candidate should be live. 27096dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen BitVector LiveBundles; 2715db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen SmallVector<unsigned, 8> ActiveBlocks; 2725db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen 273c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen void reset(InterferenceCache &Cache, unsigned Reg) { 2745db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen PhysReg = Reg; 27500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen IntvIdx = 0; 276c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen Intf.setPhysReg(Cache, Reg); 2775db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen LiveBundles.clear(); 2785db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen ActiveBlocks.clear(); 2795db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen } 28000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen 28100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // Set B[i] = C for every live bundle where B[i] was NoCand. 28200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen unsigned getBundles(SmallVectorImpl<unsigned> &B, unsigned C) { 28300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen unsigned Count = 0; 28400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen for (int i = LiveBundles.find_first(); i >= 0; 28500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen i = LiveBundles.find_next(i)) 28600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen if (B[i] == NoCand) { 28700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen B[i] = C; 28800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen Count++; 28900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen } 29000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen return Count; 29100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen } 29296dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen }; 29396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 29436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines /// Candidate info for each PhysReg in AllocationOrder. 29596dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen /// This vector never shrinks, but grows to the size of the largest register 29696dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen /// class. 29796dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen SmallVector<GlobalSplitCandidate, 32> GlobalCand; 29896dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 29936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines enum : unsigned { NoCand = ~0u }; 30000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen 30100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen /// Candidate map. Each edge bundle is assigned to a GlobalCand entry, or to 30200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen /// NoCand which indicates the stack interval. 30300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen SmallVector<unsigned, 32> BundleCand; 30400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen 305dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines /// Callee-save register cost, calculated once per machine function. 306dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines BlockFrequency CSRCost; 307dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 308c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines /// Run or not the local reassignment heuristic. This information is 309c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines /// obtained from the TargetSubtargetInfo. 310c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines bool EnableLocalReassign; 311c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines 312ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /// Set of broken hints that may be reconciled later because of eviction. 313ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines SmallSetVector<LiveInterval *, 8> SetOfBrokenHints; 314ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 315cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenpublic: 316cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen RAGreedy(); 317cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 318cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen /// Return the pass name. 31936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const char* getPassName() const override { 320533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen return "Greedy Register Allocator"; 321cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen } 322cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 323cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen /// RAGreedy analysis usage. 32436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void getAnalysisUsage(AnalysisUsage &AU) const override; 32536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void releaseMemory() override; 32636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Spiller &spiller() override { return *SpillerInstance; } 32736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void enqueue(LiveInterval *LI) override; 32836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines LiveInterval *dequeue() override; 32936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned selectOrSplit(LiveInterval&, SmallVectorImpl<unsigned>&) override; 330ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines void aboutToRemoveInterval(LiveInterval &) override; 331cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 332cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen /// Perform register allocation. 33336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines bool runOnMachineFunction(MachineFunction &mf) override; 334cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 335cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen static char ID; 336b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick 337b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trickprivate: 33836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned selectOrSplitImpl(LiveInterval &, SmallVectorImpl<unsigned> &, 33936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallVirtRegSet &, unsigned = 0); 34036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 34136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines bool LRE_CanEraseVirtReg(unsigned) override; 34236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void LRE_WillShrinkVirtReg(unsigned) override; 34336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void LRE_DidCloneVirtReg(unsigned, unsigned) override; 34436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void enqueue(PQueue &CurQueue, LiveInterval *LI); 34536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines LiveInterval *dequeue(PQueue &CurQueue); 34692a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen 34703ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen BlockFrequency calcSpillCost(); 34803ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen bool addSplitConstraints(InterferenceCache::Cursor, BlockFrequency&); 349f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen void addThroughConstraints(InterferenceCache::Cursor, ArrayRef<unsigned>); 350c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen void growRegion(GlobalSplitCandidate &Cand); 35103ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen BlockFrequency calcGlobalSplitCost(GlobalSplitCandidate&); 35287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen bool calcCompactRegion(GlobalSplitCandidate&); 35300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen void splitAroundRegion(LiveRangeEdit&, ArrayRef<unsigned>); 354034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen void calcGapWeights(unsigned, SmallVectorImpl<float>&); 3558adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick unsigned canReassign(LiveInterval &VirtReg, unsigned PhysReg); 35651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen bool shouldEvict(LiveInterval &A, bool, LiveInterval &B, bool); 35751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen bool canEvictInterference(LiveInterval&, unsigned, bool, EvictionCost&); 35851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen void evictInterference(LiveInterval&, unsigned, 3591feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey SmallVectorImpl<unsigned>&); 36036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines bool mayRecolorAllInterferences(unsigned PhysReg, LiveInterval &VirtReg, 36136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallLISet &RecoloringCandidates, 36236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const SmallVirtRegSet &FixedRegisters); 363b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen 3646bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen unsigned tryAssign(LiveInterval&, AllocationOrder&, 3651feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey SmallVectorImpl<unsigned>&); 36698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen unsigned tryEvict(LiveInterval&, AllocationOrder&, 3671feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey SmallVectorImpl<unsigned>&, unsigned = ~0u); 368b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen unsigned tryRegionSplit(LiveInterval&, AllocationOrder&, 3691feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey SmallVectorImpl<unsigned>&); 37036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines /// Calculate cost of region splitting. 37136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned calculateRegionSplitCost(LiveInterval &VirtReg, 37236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines AllocationOrder &Order, 37336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines BlockFrequency &BestCost, 37436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned &NumCands, bool IgnoreCSR); 37536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines /// Perform region splitting. 37636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned doRegionSplit(LiveInterval &VirtReg, unsigned BestCand, 37736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines bool HasCompact, 37836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallVectorImpl<unsigned> &NewVRegs); 37936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines /// Check other options before using a callee-saved register for the first 38036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines /// time. 38136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned tryAssignCSRFirstTime(LiveInterval &VirtReg, AllocationOrder &Order, 38236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned PhysReg, unsigned &CostPerUseLimit, 38336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallVectorImpl<unsigned> &NewVRegs); 384dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines void initializeCSRCost(); 385dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen unsigned tryBlockSplit(LiveInterval&, AllocationOrder&, 3861feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey SmallVectorImpl<unsigned>&); 387d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen unsigned tryInstructionSplit(LiveInterval&, AllocationOrder&, 3881feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey SmallVectorImpl<unsigned>&); 389034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen unsigned tryLocalSplit(LiveInterval&, AllocationOrder&, 3901feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey SmallVectorImpl<unsigned>&); 391b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen unsigned trySplit(LiveInterval&, AllocationOrder&, 3921feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey SmallVectorImpl<unsigned>&); 39336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned tryLastChanceRecoloring(LiveInterval &, AllocationOrder &, 39436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallVectorImpl<unsigned> &, 39536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallVirtRegSet &, unsigned); 39636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines bool tryRecoloringCandidates(PQueue &, SmallVectorImpl<unsigned> &, 39736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallVirtRegSet &, unsigned); 398ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines void tryHintRecoloring(LiveInterval &); 399ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines void tryHintsRecoloring(); 400ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 401ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /// Model the information carried by one end of a copy. 402ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines struct HintInfo { 403ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /// The frequency of the copy. 404ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines BlockFrequency Freq; 405ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /// The virtual register or physical register. 406ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines unsigned Reg; 407ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /// Its currently assigned register. 408ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /// In case of a physical register Reg == PhysReg. 409ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines unsigned PhysReg; 410ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines HintInfo(BlockFrequency Freq, unsigned Reg, unsigned PhysReg) 411ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines : Freq(Freq), Reg(Reg), PhysReg(PhysReg) {} 412ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines }; 413ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines typedef SmallVector<HintInfo, 4> HintsInfo; 414ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines BlockFrequency getBrokenHintFreq(const HintsInfo &, unsigned); 415ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines void collectHintInfo(unsigned, HintsInfo &); 416cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 417cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar bool isUnusedCalleeSavedReg(unsigned PhysReg) const; 418cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen}; 419cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} // end anonymous namespace 420cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 421cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenchar RAGreedy::ID = 0; 422cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 423b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen#ifndef NDEBUG 424b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesenconst char *const RAGreedy::StageName[] = { 425fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen "RS_New", 426fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen "RS_Assign", 427fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen "RS_Split", 42849743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen "RS_Split2", 429fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen "RS_Spill", 430cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar "RS_Memory", 431fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen "RS_Done" 432b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen}; 433b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen#endif 434b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen 435200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen// Hysteresis to use when comparing floats. 436200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen// This helps stabilize decisions based on float comparisons. 43736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesconst float Hysteresis = (2007 / 2048.0f); // 0.97998046875 438200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen 439200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen 440cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund OlesenFunctionPass* llvm::createGreedyRegisterAllocator() { 441cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen return new RAGreedy(); 442cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} 443cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 4441a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund OlesenRAGreedy::RAGreedy(): MachineFunctionPass(ID) { 445cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry()); 446b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); 447cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); 448cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); 4495b220213bfe9c37c2bb41a7ae0804e06a14f1007Rafael Espindola initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry()); 45042b7a71dc7381d1f38bf7b7201fc26dd80453364Andrew Trick initializeMachineSchedulerPass(*PassRegistry::getPassRegistry()); 451cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeLiveStacksPass(*PassRegistry::getPassRegistry()); 452cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry()); 453cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry()); 454cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen initializeVirtRegMapPass(*PassRegistry::getPassRegistry()); 455042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen initializeLiveRegMatrixPass(*PassRegistry::getPassRegistry()); 456b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen initializeEdgeBundlesPass(*PassRegistry::getPassRegistry()); 457b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen initializeSpillPlacementPass(*PassRegistry::getPassRegistry()); 458cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} 459cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 460cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenvoid RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const { 461cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.setPreservesCFG(); 4624eed756153b84c211114a3e9186bf0cb55d4b394Benjamin Kramer AU.addRequired<MachineBlockFrequencyInfo>(); 4634eed756153b84c211114a3e9186bf0cb55d4b394Benjamin Kramer AU.addPreserved<MachineBlockFrequencyInfo>(); 464cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar AU.addRequired<AAResultsWrapperPass>(); 465cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar AU.addPreserved<AAResultsWrapperPass>(); 466cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequired<LiveIntervals>(); 46705ec712e7f75635abbdd84dced69f4a45fe0f541Jakob Stoklund Olesen AU.addPreserved<LiveIntervals>(); 468b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen AU.addRequired<SlotIndexes>(); 469cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addPreserved<SlotIndexes>(); 470cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen AU.addRequired<LiveDebugVariables>(); 471cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen AU.addPreserved<LiveDebugVariables>(); 472cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequired<LiveStacks>(); 473cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addPreserved<LiveStacks>(); 474f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen AU.addRequired<MachineDominatorTree>(); 475f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen AU.addPreserved<MachineDominatorTree>(); 476cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequired<MachineLoopInfo>(); 477cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addPreserved<MachineLoopInfo>(); 478cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addRequired<VirtRegMap>(); 479cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen AU.addPreserved<VirtRegMap>(); 480042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen AU.addRequired<LiveRegMatrix>(); 481042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen AU.addPreserved<LiveRegMatrix>(); 482b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen AU.addRequired<EdgeBundles>(); 483b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen AU.addRequired<SpillPlacement>(); 484cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen MachineFunctionPass::getAnalysisUsage(AU); 485cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} 486cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 48792a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen 48892a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen//===----------------------------------------------------------------------===// 48992a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen// LiveRangeEdit delegate methods 49092a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen//===----------------------------------------------------------------------===// 49192a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen 4927792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesenbool RAGreedy::LRE_CanEraseVirtReg(unsigned VirtReg) { 493042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen if (VRM->hasPhys(VirtReg)) { 494ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines LiveInterval &LI = LIS->getInterval(VirtReg); 495ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines Matrix->unassign(LI); 496ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines aboutToRemoveInterval(LI); 4977792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen return true; 4987792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen } 4997792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen // Unassigned virtreg is probably in the priority queue. 5007792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen // RegAllocBase will erase it after dequeueing. 5017792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen return false; 5027792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen} 50392a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen 5041d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesenvoid RAGreedy::LRE_WillShrinkVirtReg(unsigned VirtReg) { 505042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen if (!VRM->hasPhys(VirtReg)) 5061d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen return; 5071d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen 5081d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen // Register is assigned, put it back on the queue for reassignment. 5091d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen LiveInterval &LI = LIS->getInterval(VirtReg); 510042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen Matrix->unassign(LI); 5111d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen enqueue(&LI); 5121d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen} 5131d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen 514f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesenvoid RAGreedy::LRE_DidCloneVirtReg(unsigned New, unsigned Old) { 5150d4fea786670473b53d285be378e619399e03488Jakob Stoklund Olesen // Cloning a register we haven't even heard about yet? Just ignore it. 5160d4fea786670473b53d285be378e619399e03488Jakob Stoklund Olesen if (!ExtraRegInfo.inBounds(Old)) 5170d4fea786670473b53d285be378e619399e03488Jakob Stoklund Olesen return; 5180d4fea786670473b53d285be378e619399e03488Jakob Stoklund Olesen 519f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen // LRE may clone a virtual register because dead code elimination causes it to 520165e231c4295deb5cabd124d08e231b551bcc0b2Jakob Stoklund Olesen // be split into connected components. The new components are much smaller 521165e231c4295deb5cabd124d08e231b551bcc0b2Jakob Stoklund Olesen // than the original, so they should get a new chance at being assigned. 522f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen // same stage as the parent. 523165e231c4295deb5cabd124d08e231b551bcc0b2Jakob Stoklund Olesen ExtraRegInfo[Old].Stage = RS_Assign; 5241a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen ExtraRegInfo.grow(New); 5251a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen ExtraRegInfo[New] = ExtraRegInfo[Old]; 526f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen} 527f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen 528cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenvoid RAGreedy::releaseMemory() { 52937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines SpillerInstance.reset(); 5301a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen ExtraRegInfo.clear(); 5315db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen GlobalCand.clear(); 532cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} 533cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 53436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesvoid RAGreedy::enqueue(LiveInterval *LI) { enqueue(Queue, LI); } 53536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 53636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesvoid RAGreedy::enqueue(PQueue &CurQueue, LiveInterval *LI) { 53798d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen // Prioritize live ranges by size, assigning larger ranges first. 53898d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen // The queue holds (size, reg) pairs. 539107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen const unsigned Size = LI->getSize(); 540107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen const unsigned Reg = LI->reg; 54198d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen assert(TargetRegisterInfo::isVirtualRegister(Reg) && 54298d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen "Can only enqueue virtual registers"); 543107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen unsigned Prio; 54490c1d7ddfc65654f7efe72d56cad65d1af9e6b2aJakob Stoklund Olesen 5451a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen ExtraRegInfo.grow(Reg); 5461a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen if (ExtraRegInfo[Reg].Stage == RS_New) 547fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen ExtraRegInfo[Reg].Stage = RS_Assign; 548f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen 549cc07e04262fe4bc35469fbadc53d2ec7bfd02fe2Jakob Stoklund Olesen if (ExtraRegInfo[Reg].Stage == RS_Split) { 550eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen // Unsplit ranges that couldn't be allocated immediately are deferred until 551a16a25ddeaf495b78b04e2a19feeac00d9824e63Jakob Stoklund Olesen // everything else has been allocated. 552a16a25ddeaf495b78b04e2a19feeac00d9824e63Jakob Stoklund Olesen Prio = Size; 553cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar } else if (ExtraRegInfo[Reg].Stage == RS_Memory) { 554cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // Memory operand should be considered last. 555cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // Change the priority such that Memory operand are assigned in 556cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // the reverse order that they came in. 557cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // TODO: Make this a member variable and probably do something about hints. 558cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar static unsigned MemOp = 0; 559cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar Prio = MemOp++; 560cc07e04262fe4bc35469fbadc53d2ec7bfd02fe2Jakob Stoklund Olesen } else { 56136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Giant live ranges fall back to the global assignment heuristic, which 56236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // prevents excessive spilling in pathological cases. 56336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines bool ReverseLocal = TRI->reverseLocalAssignment(); 5640c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar const TargetRegisterClass &RC = *MRI->getRegClass(Reg); 56537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines bool ForceGlobal = !ReverseLocal && 5660c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar (Size / SlotIndex::InstrDist) > (2 * RC.getNumRegs()); 56736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 56836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (ExtraRegInfo[Reg].Stage == RS_Assign && !ForceGlobal && !LI->empty() && 5696ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick LIS->intervalIsInOneMBB(*LI)) { 5706ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick // Allocate original local ranges in linear instruction order. Since they 5716ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick // are singly defined, this produces optimal coloring in the absence of 5726ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick // global interference and other constraints. 57336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (!ReverseLocal) 57436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Prio = LI->beginIndex().getInstrDistance(Indexes->getLastIndex()); 57536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines else { 57636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Allocating bottom up may allow many short LRGs to be assigned first 57736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // to one of the cheap registers. This could be much faster for very 57836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // large blocks on targets with many physical registers. 5790c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar Prio = Indexes->getZeroIndex().getInstrDistance(LI->endIndex()); 58036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 5810c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar Prio |= RC.AllocationPriority << 24; 5820c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar } else { 5836ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick // Allocate global and split ranges in long->short order. Long ranges that 5846ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick // don't fit should be spilled (or split) ASAP so they don't create 5856ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick // interference. Mark a bit to prioritize global above local ranges. 5866ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick Prio = (1u << 29) + Size; 5876ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick } 5886ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick // Mark a higher bit to prioritize global and local above RS_Split. 5896ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick Prio |= (1u << 31); 590d2a50734234a80893ad71da90d9f32032c47e000Jakob Stoklund Olesen 591eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen // Boost ranges that have a physical register hint. 592fc6374439edf2f74da4026f4cea8e341d092be5cJakob Stoklund Olesen if (VRM->hasKnownPreference(Reg)) 593eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen Prio |= (1u << 30); 594eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen } 595bef4c3e069a66c8b2d5871468cb57978f44ddc54Andrew Trick // The virtual register number is a tie breaker for same-sized ranges. 596bef4c3e069a66c8b2d5871468cb57978f44ddc54Andrew Trick // Give lower vreg numbers higher priority to assign them first. 59736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines CurQueue.push(std::make_pair(Prio, ~Reg)); 59890c1d7ddfc65654f7efe72d56cad65d1af9e6b2aJakob Stoklund Olesen} 59990c1d7ddfc65654f7efe72d56cad65d1af9e6b2aJakob Stoklund Olesen 60036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinesLiveInterval *RAGreedy::dequeue() { return dequeue(Queue); } 60136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 60236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinesLiveInterval *RAGreedy::dequeue(PQueue &CurQueue) { 60336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (CurQueue.empty()) 604dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines return nullptr; 60536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines LiveInterval *LI = &LIS->getInterval(~CurQueue.top().second); 60636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines CurQueue.pop(); 60798d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen return LI; 60898d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen} 609770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen 6106bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen 6116bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen//===----------------------------------------------------------------------===// 6126bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen// Direct Assignment 6136bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen//===----------------------------------------------------------------------===// 6146bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen 6156bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen/// tryAssign - Try to assign VirtReg to an available register. 6166bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesenunsigned RAGreedy::tryAssign(LiveInterval &VirtReg, 6176bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen AllocationOrder &Order, 6181feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey SmallVectorImpl<unsigned> &NewVRegs) { 6196bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen Order.rewind(); 6206bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen unsigned PhysReg; 621042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen while ((PhysReg = Order.next())) 622042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen if (!Matrix->checkInterference(VirtReg, PhysReg)) 6236bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen break; 624f7999fe1cb2c2bdb0a4080efabb4743719ce45caJakob Stoklund Olesen if (!PhysReg || Order.isHint()) 6256bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen return PhysReg; 6266bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen 62751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // PhysReg is available, but there may be a better choice. 62851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen 62951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // If we missed a simple hint, try to cheaply evict interference from the 63051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // preferred register. 63151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen if (unsigned Hint = MRI->getSimpleHint(VirtReg.reg)) 632042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen if (Order.isHint(Hint)) { 63351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen DEBUG(dbgs() << "missed hint " << PrintReg(Hint, TRI) << '\n'); 63436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines EvictionCost MaxCost; 63536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines MaxCost.setBrokenHints(1); 63651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen if (canEvictInterference(VirtReg, Hint, true, MaxCost)) { 63751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen evictInterference(VirtReg, Hint, NewVRegs); 63851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen return Hint; 63951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen } 64051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen } 64151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen 64251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // Try to evict interference from a cheaper alternative. 6436bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen unsigned Cost = TRI->getCostPerUse(PhysReg); 6446bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen 6456bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen // Most registers have 0 additional cost. 6466bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen if (!Cost) 6476bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen return PhysReg; 6486bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen 6496bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is available at cost " << Cost 6506bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen << '\n'); 6516bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen unsigned CheapReg = tryEvict(VirtReg, Order, NewVRegs, Cost); 6526bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen return CheapReg ? CheapReg : PhysReg; 6536bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen} 6546bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen 6556bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen 656770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen//===----------------------------------------------------------------------===// 65798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen// Interference eviction 65898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen//===----------------------------------------------------------------------===// 6592710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen 6608adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trickunsigned RAGreedy::canReassign(LiveInterval &VirtReg, unsigned PrevReg) { 661cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo, Matrix); 6628adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick unsigned PhysReg; 6638adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick while ((PhysReg = Order.next())) { 6648adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick if (PhysReg == PrevReg) 6658adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick continue; 6668adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick 6678adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick MCRegUnitIterator Units(PhysReg, TRI); 6688adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick for (; Units.isValid(); ++Units) { 6698adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick // Instantiate a "subquery", not to be confused with the Queries array. 6708adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick LiveIntervalUnion::Query subQ(&VirtReg, &Matrix->getLiveUnions()[*Units]); 6718adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick if (subQ.checkInterference()) 6728adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick break; 6738adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick } 6748adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick // If no units have interference, break out with the current PhysReg. 6758adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick if (!Units.isValid()) 6768adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick break; 6778adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick } 6788adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick if (PhysReg) 6798adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick DEBUG(dbgs() << "can reassign: " << VirtReg << " from " 6808adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick << PrintReg(PrevReg, TRI) << " to " << PrintReg(PhysReg, TRI) 6818adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick << '\n'); 6828adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick return PhysReg; 6838adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick} 6848adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick 68551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// shouldEvict - determine if A should evict the assigned live range B. The 68651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// eviction policy defined by this function together with the allocation order 68751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// defined by enqueue() decides which registers ultimately end up being split 68851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// and spilled. 689b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen/// 6901a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen/// Cascade numbers are used to prevent infinite loops if this function is a 6911a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen/// cyclic relation. 69251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// 69351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// @param A The live range to be assigned. 69451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// @param IsHint True when A is about to be assigned to its preferred 69551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// register. 69651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// @param B The live range to be evicted. 69751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// @param BreaksHint True when B is already assigned to its preferred register. 69851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesenbool RAGreedy::shouldEvict(LiveInterval &A, bool IsHint, 69951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen LiveInterval &B, bool BreaksHint) { 70049743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen bool CanSplit = getStage(B) < RS_Spill; 70151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen 70251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // Be fairly aggressive about following hints as long as the evictee can be 70351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // split. 70451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen if (CanSplit && IsHint && !BreaksHint) 70551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen return true; 70651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen 70736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (A.weight > B.weight) { 70836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines DEBUG(dbgs() << "should evict: " << B << " w= " << B.weight << '\n'); 70936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return true; 71036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 71136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return false; 712b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen} 713b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen 71451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// canEvictInterference - Return true if all interferences between VirtReg and 71536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// PhysReg can be evicted. 71651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// 71751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// @param VirtReg Live range that is about to be assigned. 71851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// @param PhysReg Desired register for assignment. 71967c8978617c3bce9d07210f93f6c64c715f77695Dmitri Gribenko/// @param IsHint True when PhysReg is VirtReg's preferred register. 72051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// @param MaxCost Only look for cheaper candidates and update with new cost 72151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// when returning true. 72251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// @returns True when interference can be evicted cheaper than MaxCost. 72398c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesenbool RAGreedy::canEvictInterference(LiveInterval &VirtReg, unsigned PhysReg, 72451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen bool IsHint, EvictionCost &MaxCost) { 725042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen // It is only possible to evict virtual register interference. 726042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen if (Matrix->checkInterference(VirtReg, PhysReg) > LiveRegMatrix::IK_VirtReg) 727042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen return false; 728042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen 7296ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick bool IsLocal = LIS->intervalIsInOneMBB(VirtReg); 7306ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick 7311a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen // Find VirtReg's cascade number. This will be unassigned if VirtReg was never 7321a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen // involved in an eviction before. If a cascade number was assigned, deny 7331a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen // evicting anything with the same or a newer cascade number. This prevents 7341a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen // infinite eviction loops. 7351a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen // 7361a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen // This works out so a register without a cascade number is allowed to evict 7371a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen // anything, and it can be evicted by anything. 7381a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen unsigned Cascade = ExtraRegInfo[VirtReg.reg].Cascade; 7391a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen if (!Cascade) 7401a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen Cascade = NextCascade; 7411a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen 74251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen EvictionCost Cost; 743042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { 744042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units); 7453f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen // If there is 10 or more interferences, chances are one is heavier. 74651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen if (Q.collectInterferingVRegs(10) >= 10) 74798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen return false; 74898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 7493f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen // Check if any interfering live range is heavier than MaxWeight. 7503f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen for (unsigned i = Q.interferingVRegs().size(); i; --i) { 7513f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen LiveInterval *Intf = Q.interferingVRegs()[i - 1]; 752042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen assert(TargetRegisterInfo::isVirtualRegister(Intf->reg) && 753042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen "Only expecting virtual register interference from query"); 75451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // Never evict spill products. They cannot split or spill. 755fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen if (getStage(*Intf) == RS_Done) 7561a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen return false; 75751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // Once a live range becomes small enough, it is urgent that we find a 75851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // register for it. This is indicated by an infinite spill weight. These 75951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // urgent live ranges get to evict almost anything. 7609cda1be0aa5e9e70ae493ef6944a8c202c1c70e6Jakob Stoklund Olesen // 7619cda1be0aa5e9e70ae493ef6944a8c202c1c70e6Jakob Stoklund Olesen // Also allow urgent evictions of unspillable ranges from a strictly 7629cda1be0aa5e9e70ae493ef6944a8c202c1c70e6Jakob Stoklund Olesen // larger allocation order. 7639cda1be0aa5e9e70ae493ef6944a8c202c1c70e6Jakob Stoklund Olesen bool Urgent = !VirtReg.isSpillable() && 7649cda1be0aa5e9e70ae493ef6944a8c202c1c70e6Jakob Stoklund Olesen (Intf->isSpillable() || 7659cda1be0aa5e9e70ae493ef6944a8c202c1c70e6Jakob Stoklund Olesen RegClassInfo.getNumAllocatableRegs(MRI->getRegClass(VirtReg.reg)) < 7669cda1be0aa5e9e70ae493ef6944a8c202c1c70e6Jakob Stoklund Olesen RegClassInfo.getNumAllocatableRegs(MRI->getRegClass(Intf->reg))); 76751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // Only evict older cascades or live ranges without a cascade. 76851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen unsigned IntfCascade = ExtraRegInfo[Intf->reg].Cascade; 76951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen if (Cascade <= IntfCascade) { 77051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen if (!Urgent) 77151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen return false; 77251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // We permit breaking cascades for urgent evictions. It should be the 77351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // last resort, though, so make it really expensive. 77451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen Cost.BrokenHints += 10; 77551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen } 77651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // Would this break a satisfied hint? 77751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen bool BreaksHint = VRM->hasPreferredPhys(Intf->reg); 77851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // Update eviction cost. 77951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen Cost.BrokenHints += BreaksHint; 78051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen Cost.MaxWeight = std::max(Cost.MaxWeight, Intf->weight); 78151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // Abort if this would be too expensive. 78251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen if (!(Cost < MaxCost)) 783b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen return false; 7846ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick if (Urgent) 7856ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick continue; 78636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Apply the eviction policy for non-urgent evictions. 78736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (!shouldEvict(VirtReg, IsHint, *Intf, BreaksHint)) 78836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return false; 7896ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick // If !MaxCost.isMax(), then we're just looking for a cheap register. 7906ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick // Evicting another local live range in this case could lead to suboptimal 7916ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick // coloring. 7928adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick if (!MaxCost.isMax() && IsLocal && LIS->intervalIsInOneMBB(*Intf) && 793c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines (!EnableLocalReassign || !canReassign(*Intf, PhysReg))) { 7946ea2b9608a38e9b53d208ff85051e8e3ed53192cAndrew Trick return false; 7958adae96fd905daa8835b6fde5e74e25d818c7471Andrew Trick } 7962710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen } 7972710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen } 79851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen MaxCost = Cost; 79998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen return true; 80098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen} 80198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 80251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// evictInterference - Evict any interferring registers that prevent VirtReg 80351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// from being assigned to Physreg. This assumes that canEvictInterference 80451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// returned true. 80551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesenvoid RAGreedy::evictInterference(LiveInterval &VirtReg, unsigned PhysReg, 8061feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey SmallVectorImpl<unsigned> &NewVRegs) { 80751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // Make sure that VirtReg has a cascade number, and assign that cascade 80851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // number to every evicted register. These live ranges than then only be 80951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // evicted by a newer cascade, preventing infinite loops. 81051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen unsigned Cascade = ExtraRegInfo[VirtReg.reg].Cascade; 81151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen if (!Cascade) 81251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen Cascade = ExtraRegInfo[VirtReg.reg].Cascade = NextCascade++; 81351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen 81451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen DEBUG(dbgs() << "evicting " << PrintReg(PhysReg, TRI) 81551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen << " interference: Cascade " << Cascade << '\n'); 816042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen 817042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen // Collect all interfering virtregs first. 818042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen SmallVector<LiveInterval*, 8> Intfs; 819042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { 820042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units); 82151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen assert(Q.seenAllInterferences() && "Didn't check all interfererences."); 822042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen ArrayRef<LiveInterval*> IVR = Q.interferingVRegs(); 823042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen Intfs.append(IVR.begin(), IVR.end()); 824042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen } 825042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen 826042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen // Evict them second. This will invalidate the queries. 827042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen for (unsigned i = 0, e = Intfs.size(); i != e; ++i) { 828042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen LiveInterval *Intf = Intfs[i]; 829042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen // The same VirtReg may be present in multiple RegUnits. Skip duplicates. 830042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen if (!VRM->hasPhys(Intf->reg)) 831042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen continue; 832042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen Matrix->unassign(*Intf); 833042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen assert((ExtraRegInfo[Intf->reg].Cascade < Cascade || 834042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen VirtReg.isSpillable() < Intf->isSpillable()) && 835042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen "Cannot decrease cascade number, illegal eviction"); 836042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen ExtraRegInfo[Intf->reg].Cascade = Cascade; 837042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen ++NumEvicted; 8381feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey NewVRegs.push_back(Intf->reg); 83951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen } 84051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen} 84151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen 842cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar/// Returns true if the given \p PhysReg is a callee saved register and has not 843cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar/// been used for allocation yet. 844cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarbool RAGreedy::isUnusedCalleeSavedReg(unsigned PhysReg) const { 845cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar unsigned CSR = RegClassInfo.getLastCalleeSavedAlias(PhysReg); 846cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (CSR == 0) 847cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar return false; 848cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 849cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar return !Matrix->isPhysRegUsed(PhysReg); 850cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar} 851cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 85298c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen/// tryEvict - Try to evict all interferences for a physreg. 85376395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen/// @param VirtReg Currently unassigned virtual register. 85476395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen/// @param Order Physregs to try. 85576395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen/// @return Physreg to assign VirtReg, or 0. 85698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesenunsigned RAGreedy::tryEvict(LiveInterval &VirtReg, 85798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen AllocationOrder &Order, 8581feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey SmallVectorImpl<unsigned> &NewVRegs, 8596bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen unsigned CostPerUseLimit) { 86098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen NamedRegionTimer T("Evict", TimerGroupName, TimePassesIsEnabled); 86198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 86251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // Keep track of the cheapest interference seen so far. 86336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines EvictionCost BestCost; 86436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines BestCost.setMax(); 86598c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen unsigned BestPhys = 0; 8666d6132986d2ef14bbf9d76f5acbf2a0bace32d69Jakob Stoklund Olesen unsigned OrderLimit = Order.getOrder().size(); 86798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 86851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // When we are just looking for a reduced cost per use, don't break any 86951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // hints, and only evict smaller spill weights. 87051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen if (CostPerUseLimit < ~0u) { 87151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen BestCost.BrokenHints = 0; 87251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen BestCost.MaxWeight = VirtReg.weight; 8736d6132986d2ef14bbf9d76f5acbf2a0bace32d69Jakob Stoklund Olesen 8746d6132986d2ef14bbf9d76f5acbf2a0bace32d69Jakob Stoklund Olesen // Check of any registers in RC are below CostPerUseLimit. 8756d6132986d2ef14bbf9d76f5acbf2a0bace32d69Jakob Stoklund Olesen const TargetRegisterClass *RC = MRI->getRegClass(VirtReg.reg); 8766d6132986d2ef14bbf9d76f5acbf2a0bace32d69Jakob Stoklund Olesen unsigned MinCost = RegClassInfo.getMinCost(RC); 8776d6132986d2ef14bbf9d76f5acbf2a0bace32d69Jakob Stoklund Olesen if (MinCost >= CostPerUseLimit) { 87837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines DEBUG(dbgs() << TRI->getRegClassName(RC) << " minimum cost = " << MinCost 8796d6132986d2ef14bbf9d76f5acbf2a0bace32d69Jakob Stoklund Olesen << ", no cheaper registers to be found.\n"); 8806d6132986d2ef14bbf9d76f5acbf2a0bace32d69Jakob Stoklund Olesen return 0; 8816d6132986d2ef14bbf9d76f5acbf2a0bace32d69Jakob Stoklund Olesen } 8826d6132986d2ef14bbf9d76f5acbf2a0bace32d69Jakob Stoklund Olesen 8836d6132986d2ef14bbf9d76f5acbf2a0bace32d69Jakob Stoklund Olesen // It is normal for register classes to have a long tail of registers with 8846d6132986d2ef14bbf9d76f5acbf2a0bace32d69Jakob Stoklund Olesen // the same cost. We don't need to look at them if they're too expensive. 8856d6132986d2ef14bbf9d76f5acbf2a0bace32d69Jakob Stoklund Olesen if (TRI->getCostPerUse(Order.getOrder().back()) >= CostPerUseLimit) { 8866d6132986d2ef14bbf9d76f5acbf2a0bace32d69Jakob Stoklund Olesen OrderLimit = RegClassInfo.getLastCostChange(RC); 8876d6132986d2ef14bbf9d76f5acbf2a0bace32d69Jakob Stoklund Olesen DEBUG(dbgs() << "Only trying the first " << OrderLimit << " regs.\n"); 8886d6132986d2ef14bbf9d76f5acbf2a0bace32d69Jakob Stoklund Olesen } 88951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen } 89051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen 89198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen Order.rewind(); 89236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines while (unsigned PhysReg = Order.next(OrderLimit)) { 8936bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen if (TRI->getCostPerUse(PhysReg) >= CostPerUseLimit) 8946bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen continue; 89551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // The first use of a callee-saved register in a function has cost 1. 89651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen // Don't start using a CSR when the CostPerUseLimit is low. 897cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (CostPerUseLimit == 1 && isUnusedCalleeSavedReg(PhysReg)) { 898cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " would clobber CSR " 899cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar << PrintReg(RegClassInfo.getLastCalleeSavedAlias(PhysReg), TRI) 900cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar << '\n'); 901cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar continue; 902cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar } 90351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen 90451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen if (!canEvictInterference(VirtReg, PhysReg, false, BestCost)) 90598c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen continue; 90698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 90798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen // Best so far. 90898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen BestPhys = PhysReg; 90951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen 91057f1e2cee06f9b57995727d786aeb1031c5376bdJakob Stoklund Olesen // Stop if the hint can be used. 911f7999fe1cb2c2bdb0a4080efabb4743719ce45caJakob Stoklund Olesen if (Order.isHint()) 91257f1e2cee06f9b57995727d786aeb1031c5376bdJakob Stoklund Olesen break; 9132710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen } 9142710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen 91598c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen if (!BestPhys) 91698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen return 0; 91798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen 91851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen evictInterference(VirtReg, BestPhys, NewVRegs); 91998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen return BestPhys; 920b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick} 921b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick 922770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen 923770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen//===----------------------------------------------------------------------===// 924b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen// Region Splitting 925b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen//===----------------------------------------------------------------------===// 926b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 9271b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen/// addSplitConstraints - Fill out the SplitConstraints vector based on the 9281b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen/// interference pattern in Physreg and its aliases. Add the constraints to 9291b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen/// SpillPlacement and return the static cost of this split in Cost, assuming 9301b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen/// that all preferences in SplitConstraints are met. 931f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen/// Return false if there are no bundles with positive bias. 932f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesenbool RAGreedy::addSplitConstraints(InterferenceCache::Cursor Intf, 93303ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen BlockFrequency &Cost) { 934db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); 935eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen 936b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen // Reset interference dependent info. 937db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen SplitConstraints.resize(UseBlocks.size()); 93803ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen BlockFrequency StaticCost = 0; 939db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen for (unsigned i = 0; i != UseBlocks.size(); ++i) { 940db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 94196dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen SpillPlacement::BlockConstraint &BC = SplitConstraints[i]; 94296dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 943f0ac26c51173a9a1d6e5b5794107dccc4c5b5792Jakob Stoklund Olesen BC.Number = BI.MBB->getNumber(); 944eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen Intf.moveToBlock(BC.Number); 945db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen BC.Entry = BI.LiveIn ? SpillPlacement::PrefReg : SpillPlacement::DontCare; 946db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen BC.Exit = BI.LiveOut ? SpillPlacement::PrefReg : SpillPlacement::DontCare; 947453f4f01302f00651aae2fc7658f6e23a2beadb0David Blaikie BC.ChangesValue = BI.FirstDef.isValid(); 948b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 949eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (!Intf.hasInterference()) 950eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen continue; 951eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen 95296dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen // Number of spill code instructions to insert. 95396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen unsigned Ins = 0; 95496dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 95596dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen // Interference for the live-in value. 956eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (BI.LiveIn) { 9576c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen if (Intf.first() <= Indexes->getMBBStartIdx(BC.Number)) 958db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen BC.Entry = SpillPlacement::MustSpill, ++Ins; 959fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen else if (Intf.first() < BI.FirstInstr) 96096dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen BC.Entry = SpillPlacement::PrefSpill, ++Ins; 961fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen else if (Intf.first() < BI.LastInstr) 96296dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen ++Ins; 963a50c539b7a9e74597da34bfaea5429a48481f18bJakob Stoklund Olesen } 964b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 96596dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen // Interference for the live-out value. 966eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen if (BI.LiveOut) { 967612f7807c581eafb7c8105e1a55c8d839033bfb3Jakob Stoklund Olesen if (Intf.last() >= SA->getLastSplitPoint(BC.Number)) 968db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen BC.Exit = SpillPlacement::MustSpill, ++Ins; 969fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen else if (Intf.last() > BI.LastInstr) 97096dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen BC.Exit = SpillPlacement::PrefSpill, ++Ins; 971fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen else if (Intf.last() > BI.FirstInstr) 97296dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen ++Ins; 973b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen } 974b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 97596dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen // Accumulate the total frequency of inserted spill code. 97603ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen while (Ins--) 97703ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen StaticCost += SpillPlacer->getBlockFrequency(BC.Number); 978b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen } 979f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen Cost = StaticCost; 980db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen 9811b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen // Add constraints for use-blocks. Note that these are the only constraints 9821b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen // that may add a positive bias, it is downhill from here. 9831b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen SpillPlacer->addConstraints(SplitConstraints); 984f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen return SpillPlacer->scanActiveBundles(); 985f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen} 986db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen 987db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen 988f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen/// addThroughConstraints - Add constraints and links to SpillPlacer from the 989f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen/// live-through blocks in Blocks. 990f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesenvoid RAGreedy::addThroughConstraints(InterferenceCache::Cursor Intf, 991f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen ArrayRef<unsigned> Blocks) { 9921b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen const unsigned GroupSize = 8; 9931b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen SpillPlacement::BlockConstraint BCS[GroupSize]; 994f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen unsigned TBS[GroupSize]; 995f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen unsigned B = 0, T = 0; 996db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen 997f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen for (unsigned i = 0; i != Blocks.size(); ++i) { 998f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen unsigned Number = Blocks[i]; 9991b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen Intf.moveToBlock(Number); 10001b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen 10017b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen if (!Intf.hasInterference()) { 1002f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen assert(T < GroupSize && "Array overflow"); 1003f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen TBS[T] = Number; 1004f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen if (++T == GroupSize) { 100539b5abf507b43da6b92f68b86406e0015ead18e9Frits van Bommel SpillPlacer->addLinks(makeArrayRef(TBS, T)); 1006f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen T = 0; 1007f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen } 10087b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen continue; 10091b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen } 10101b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen 1011f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen assert(B < GroupSize && "Array overflow"); 1012f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen BCS[B].Number = Number; 1013f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen 10147b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen // Interference for the live-in value. 10157b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen if (Intf.first() <= Indexes->getMBBStartIdx(Number)) 10167b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen BCS[B].Entry = SpillPlacement::MustSpill; 10177b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen else 10187b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen BCS[B].Entry = SpillPlacement::PrefSpill; 10197b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen 10207b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen // Interference for the live-out value. 10217b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen if (Intf.last() >= SA->getLastSplitPoint(Number)) 10227b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen BCS[B].Exit = SpillPlacement::MustSpill; 10237b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen else 10247b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen BCS[B].Exit = SpillPlacement::PrefSpill; 10257b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen 10261b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen if (++B == GroupSize) { 102737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines SpillPlacer->addConstraints(makeArrayRef(BCS, B)); 10281b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen B = 0; 10291b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen } 1030db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen } 1031db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen 103237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines SpillPlacer->addConstraints(makeArrayRef(BCS, B)); 103339b5abf507b43da6b92f68b86406e0015ead18e9Frits van Bommel SpillPlacer->addLinks(makeArrayRef(TBS, T)); 1034b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen} 1035b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 1036c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesenvoid RAGreedy::growRegion(GlobalSplitCandidate &Cand) { 10375db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen // Keep track of through blocks that have not been added to SpillPlacer. 10385db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen BitVector Todo = SA->getThroughBlocks(); 10395db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen SmallVectorImpl<unsigned> &ActiveBlocks = Cand.ActiveBlocks; 10405db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen unsigned AddedTo = 0; 1041f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen#ifndef NDEBUG 1042f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen unsigned Visited = 0; 1043f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen#endif 10445db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen 1045f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen for (;;) { 1046f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen ArrayRef<unsigned> NewBundles = SpillPlacer->getRecentPositive(); 1047f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen // Find new through blocks in the periphery of PrefRegBundles. 1048f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen for (int i = 0, e = NewBundles.size(); i != e; ++i) { 1049f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen unsigned Bundle = NewBundles[i]; 1050f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen // Look at all blocks connected to Bundle in the full graph. 1051f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen ArrayRef<unsigned> Blocks = Bundles->getBlocks(Bundle); 1052f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen for (ArrayRef<unsigned>::iterator I = Blocks.begin(), E = Blocks.end(); 1053f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen I != E; ++I) { 1054f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen unsigned Block = *I; 10555db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen if (!Todo.test(Block)) 1056f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen continue; 10575db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen Todo.reset(Block); 1058f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen // This is a new through block. Add it to SpillPlacer later. 10595db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen ActiveBlocks.push_back(Block); 1060f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen#ifndef NDEBUG 1061f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen ++Visited; 1062f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen#endif 1063f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen } 1064f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen } 1065f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen // Any new blocks to add? 1066549019792a8b14500cab093ac8f3c5f7331e86d7Jakob Stoklund Olesen if (ActiveBlocks.size() == AddedTo) 1067549019792a8b14500cab093ac8f3c5f7331e86d7Jakob Stoklund Olesen break; 1068b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen 1069b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen // Compute through constraints from the interference, or assume that all 1070b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen // through blocks prefer spilling when forming compact regions. 107137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines auto NewBlocks = makeArrayRef(ActiveBlocks).slice(AddedTo); 1072b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen if (Cand.PhysReg) 1073b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen addThroughConstraints(Cand.Intf, NewBlocks); 1074b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen else 1075b87f91b063a0ac853735f2af3bd94fb8551a11ffJakob Stoklund Olesen // Provide a strong negative bias on through blocks to prevent unwanted 1076b87f91b063a0ac853735f2af3bd94fb8551a11ffJakob Stoklund Olesen // liveness on loop backedges. 1077b87f91b063a0ac853735f2af3bd94fb8551a11ffJakob Stoklund Olesen SpillPlacer->addPrefSpill(NewBlocks, /* Strong= */ true); 1078549019792a8b14500cab093ac8f3c5f7331e86d7Jakob Stoklund Olesen AddedTo = ActiveBlocks.size(); 1079549019792a8b14500cab093ac8f3c5f7331e86d7Jakob Stoklund Olesen 1080f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen // Perhaps iterating can enable more bundles? 1081f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen SpillPlacer->iterate(); 1082f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen } 1083f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen DEBUG(dbgs() << ", v=" << Visited); 1084f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen} 108596dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 108687972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen/// calcCompactRegion - Compute the set of edge bundles that should be live 108787972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen/// when splitting the current live range into compact regions. Compact 108887972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen/// regions can be computed without looking at interference. They are the 108987972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen/// regions formed by removing all the live-through blocks from the live range. 109087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen/// 109187972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen/// Returns false if the current live range is already compact, or if the 109287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen/// compact regions would form single block regions anyway. 109387972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesenbool RAGreedy::calcCompactRegion(GlobalSplitCandidate &Cand) { 109487972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen // Without any through blocks, the live range is already compact. 109587972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen if (!SA->getNumThroughBlocks()) 109687972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen return false; 109787972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen 109887972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen // Compact regions don't correspond to any physreg. 109987972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen Cand.reset(IntfCache, 0); 110087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen 110187972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen DEBUG(dbgs() << "Compact region bundles"); 110287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen 110387972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen // Use the spill placer to determine the live bundles. GrowRegion pretends 110487972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen // that all the through blocks have interference when PhysReg is unset. 110587972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen SpillPlacer->prepare(Cand.LiveBundles); 110687972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen 110787972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen // The static split cost will be zero since Cand.Intf reports no interference. 110803ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen BlockFrequency Cost; 110987972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen if (!addSplitConstraints(Cand.Intf, Cost)) { 111087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen DEBUG(dbgs() << ", none.\n"); 111187972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen return false; 111287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen } 111387972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen 111487972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen growRegion(Cand); 111587972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen SpillPlacer->finish(); 111687972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen 111787972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen if (!Cand.LiveBundles.any()) { 111887972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen DEBUG(dbgs() << ", none.\n"); 111987972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen return false; 112087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen } 112187972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen 112287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen DEBUG({ 112387972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen for (int i = Cand.LiveBundles.find_first(); i>=0; 112487972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen i = Cand.LiveBundles.find_next(i)) 112587972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen dbgs() << " EB#" << i; 112687972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen dbgs() << ".\n"; 112787972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen }); 112887972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen return true; 112987972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen} 113087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen 1131200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen/// calcSpillCost - Compute how expensive it would be to split the live range in 1132200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen/// SA around all use blocks instead of forming bundle regions. 113303ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund OlesenBlockFrequency RAGreedy::calcSpillCost() { 113403ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen BlockFrequency Cost = 0; 1135200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); 1136200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen for (unsigned i = 0; i != UseBlocks.size(); ++i) { 1137200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 1138200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen unsigned Number = BI.MBB->getNumber(); 1139200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen // We normally only need one spill instruction - a load or a store. 1140200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen Cost += SpillPlacer->getBlockFrequency(Number); 1141200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen 1142200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen // Unless the value is redefined in the block. 11433f5beede1bb97ba4e06dc300e00b70e1013e7216Jakob Stoklund Olesen if (BI.LiveIn && BI.LiveOut && BI.FirstDef) 11443f5beede1bb97ba4e06dc300e00b70e1013e7216Jakob Stoklund Olesen Cost += SpillPlacer->getBlockFrequency(Number); 1145200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen } 1146200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen return Cost; 1147200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen} 1148200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen 1149b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen/// calcGlobalSplitCost - Return the global split cost of following the split 1150b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen/// pattern in LiveBundles. This cost should be added to the local cost of the 115196dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen/// interference pattern in SplitConstraints. 1152b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen/// 115303ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund OlesenBlockFrequency RAGreedy::calcGlobalSplitCost(GlobalSplitCandidate &Cand) { 115403ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen BlockFrequency GlobalCost = 0; 11555db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen const BitVector &LiveBundles = Cand.LiveBundles; 1156db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); 1157db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen for (unsigned i = 0; i != UseBlocks.size(); ++i) { 1158db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 115996dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen SpillPlacement::BlockConstraint &BC = SplitConstraints[i]; 1160874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen bool RegIn = LiveBundles[Bundles->getBundle(BC.Number, 0)]; 1161874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen bool RegOut = LiveBundles[Bundles->getBundle(BC.Number, 1)]; 1162874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen unsigned Ins = 0; 1163874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen 1164db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen if (BI.LiveIn) 1165db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen Ins += RegIn != (BC.Entry == SpillPlacement::PrefReg); 1166db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen if (BI.LiveOut) 1167db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen Ins += RegOut != (BC.Exit == SpillPlacement::PrefReg); 116803ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen while (Ins--) 116903ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen GlobalCost += SpillPlacer->getBlockFrequency(BC.Number); 1170b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen } 1171db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen 11725db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen for (unsigned i = 0, e = Cand.ActiveBlocks.size(); i != e; ++i) { 11735db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen unsigned Number = Cand.ActiveBlocks[i]; 1174db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen bool RegIn = LiveBundles[Bundles->getBundle(Number, 0)]; 1175db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen bool RegOut = LiveBundles[Bundles->getBundle(Number, 1)]; 11769a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen if (!RegIn && !RegOut) 11779a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen continue; 11789a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen if (RegIn && RegOut) { 11799a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen // We need double spill code if this block has interference. 1180c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen Cand.Intf.moveToBlock(Number); 118103ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen if (Cand.Intf.hasInterference()) { 118203ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen GlobalCost += SpillPlacer->getBlockFrequency(Number); 118303ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen GlobalCost += SpillPlacer->getBlockFrequency(Number); 118403ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen } 11859a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen continue; 11869a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen } 11879a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen // live-in / stack-out or stack-in live-out. 11889a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen GlobalCost += SpillPlacer->getBlockFrequency(Number); 1189db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen } 1190b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen return GlobalCost; 1191b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen} 1192b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 119300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// splitAroundRegion - Split the current live range around the regions 119400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// determined by BundleCand and GlobalCand. 1195ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// 119600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// Before calling this function, GlobalCand and BundleCand must be initialized 119700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// so each bundle is assigned to a valid candidate, or NoCand for the 119800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// stack-bound bundles. The shared SA/SE SplitAnalysis and SplitEditor 119900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// objects must be initialized for the current live range, and intervals 120000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// created for the used candidates. 1201ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// 120200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// @param LREdit The LiveRangeEdit object handling the current split. 120300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// @param UsedCands List of used GlobalCand entries. Every BundleCand value 120400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// must appear in this list. 120500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesenvoid RAGreedy::splitAroundRegion(LiveRangeEdit &LREdit, 120600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen ArrayRef<unsigned> UsedCands) { 120700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // These are the intervals created for new global ranges. We may create more 120800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // intervals for local ranges. 120900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen const unsigned NumGlobalIntvs = LREdit.size(); 121000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen DEBUG(dbgs() << "splitAroundRegion with " << NumGlobalIntvs << " globals.\n"); 121100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen assert(NumGlobalIntvs && "No global intervals configured"); 1212ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 12132d6d86be84ee355223ccd20b7f87a0c9971c50c9Jakob Stoklund Olesen // Isolate even single instructions when dealing with a proper sub-class. 121469145baf36219b07a49d8ce14b4a04870e72a123Jakob Stoklund Olesen // That guarantees register class inflation for the stack interval because it 12152d6d86be84ee355223ccd20b7f87a0c9971c50c9Jakob Stoklund Olesen // is all copies. 12162d6d86be84ee355223ccd20b7f87a0c9971c50c9Jakob Stoklund Olesen unsigned Reg = SA->getParent().reg; 12172d6d86be84ee355223ccd20b7f87a0c9971c50c9Jakob Stoklund Olesen bool SingleInstrs = RegClassInfo.isProperSubClass(MRI->getRegClass(Reg)); 12182d6d86be84ee355223ccd20b7f87a0c9971c50c9Jakob Stoklund Olesen 121987360f73ae205854f100ba5fb7eef7b90ac3bc27Jakob Stoklund Olesen // First handle all the blocks with uses. 1220db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); 1221db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen for (unsigned i = 0; i != UseBlocks.size(); ++i) { 1222db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 122300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen unsigned Number = BI.MBB->getNumber(); 122400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen unsigned IntvIn = 0, IntvOut = 0; 122500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen SlotIndex IntfIn, IntfOut; 122600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen if (BI.LiveIn) { 122700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen unsigned CandIn = BundleCand[Bundles->getBundle(Number, 0)]; 122800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen if (CandIn != NoCand) { 122900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen GlobalSplitCandidate &Cand = GlobalCand[CandIn]; 123000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen IntvIn = Cand.IntvIdx; 123100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen Cand.Intf.moveToBlock(Number); 123200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen IntfIn = Cand.Intf.first(); 123300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen } 123400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen } 123500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen if (BI.LiveOut) { 123600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen unsigned CandOut = BundleCand[Bundles->getBundle(Number, 1)]; 123700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen if (CandOut != NoCand) { 123800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen GlobalSplitCandidate &Cand = GlobalCand[CandOut]; 123900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen IntvOut = Cand.IntvIdx; 124000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen Cand.Intf.moveToBlock(Number); 124100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen IntfOut = Cand.Intf.last(); 124200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen } 124300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen } 1244ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 1245fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen // Create separate intervals for isolated blocks with multiple uses. 124600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen if (!IntvIn && !IntvOut) { 1247fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " isolated.\n"); 12482d6d86be84ee355223ccd20b7f87a0c9971c50c9Jakob Stoklund Olesen if (SA->shouldSplitSingleBlock(BI, SingleInstrs)) 124987360f73ae205854f100ba5fb7eef7b90ac3bc27Jakob Stoklund Olesen SE->splitSingleBlock(BI); 1250fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen continue; 1251fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen } 1252fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen 125300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen if (IntvIn && IntvOut) 125400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut); 125500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen else if (IntvIn) 125600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen SE->splitRegInBlock(BI, IntvIn, IntfIn); 1257b4ddedce599183362b0f0333922c2fe0e163a129Jakob Stoklund Olesen else 125800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen SE->splitRegOutBlock(BI, IntvOut, IntfOut); 1259ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen } 1260ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 126100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // Handle live-through blocks. The relevant live-through blocks are stored in 126200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // the ActiveBlocks list with each candidate. We need to filter out 126300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // duplicates. 126400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen BitVector Todo = SA->getThroughBlocks(); 126500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen for (unsigned c = 0; c != UsedCands.size(); ++c) { 126600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen ArrayRef<unsigned> Blocks = GlobalCand[UsedCands[c]].ActiveBlocks; 126700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen for (unsigned i = 0, e = Blocks.size(); i != e; ++i) { 126800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen unsigned Number = Blocks[i]; 126900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen if (!Todo.test(Number)) 127000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen continue; 127100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen Todo.reset(Number); 127200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen 127300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen unsigned IntvIn = 0, IntvOut = 0; 127400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen SlotIndex IntfIn, IntfOut; 127500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen 127600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen unsigned CandIn = BundleCand[Bundles->getBundle(Number, 0)]; 127700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen if (CandIn != NoCand) { 127800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen GlobalSplitCandidate &Cand = GlobalCand[CandIn]; 127900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen IntvIn = Cand.IntvIdx; 128000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen Cand.Intf.moveToBlock(Number); 128100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen IntfIn = Cand.Intf.first(); 128200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen } 128300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen 128400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen unsigned CandOut = BundleCand[Bundles->getBundle(Number, 1)]; 128500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen if (CandOut != NoCand) { 128600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen GlobalSplitCandidate &Cand = GlobalCand[CandOut]; 128700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen IntvOut = Cand.IntvIdx; 128800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen Cand.Intf.moveToBlock(Number); 128900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen IntfOut = Cand.Intf.last(); 129000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen } 129100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen if (!IntvIn && !IntvOut) 129200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen continue; 129300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut); 129400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen } 1295db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen } 1296db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen 12970db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen ++NumGlobalSplits; 1298ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 12995928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen SmallVector<unsigned, 8> IntvMap; 13005928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen SE->finish(&IntvMap); 13011feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey DebugVars->splitRegister(Reg, LREdit.regs(), *LIS); 1302f42b66169d75301346e3685fd2b3e45e47806367Jakob Stoklund Olesen 13031a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen ExtraRegInfo.resize(MRI->getNumVirtRegs()); 1304b2abfa0bf30edf292a27a06e091d03983e644c9bJakob Stoklund Olesen unsigned OrigBlocks = SA->getNumLiveBlocks(); 13055928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen 13065928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen // Sort out the new intervals created by splitting. We get four kinds: 13075928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen // - Remainder intervals should not be split again. 13085928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen // - Candidate intervals can be assigned to Cand.PhysReg. 13095928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen // - Block-local splits are candidates for local splitting. 13105928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen // - DCE leftovers should go back on the queue. 13115928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen for (unsigned i = 0, e = LREdit.size(); i != e; ++i) { 13121feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey LiveInterval &Reg = LIS->getInterval(LREdit.get(i)); 13135928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen 13145928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen // Ignore old intervals from DCE. 13151a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen if (getStage(Reg) != RS_New) 13165928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen continue; 13175928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen 13185928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen // Remainder interval. Don't try splitting again, spill if it doesn't 13195928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen // allocate. 13205928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen if (IntvMap[i] == 0) { 1321fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen setStage(Reg, RS_Spill); 13225928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen continue; 13235928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen } 13245928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen 132500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // Global intervals. Allow repeated splitting as long as the number of live 132600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // blocks is strictly decreasing. 132700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen if (IntvMap[i] < NumGlobalIntvs) { 13281a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen if (SA->countLiveBlocks(&Reg) >= OrigBlocks) { 13299f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen DEBUG(dbgs() << "Main interval covers the same " << OrigBlocks 13309f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen << " blocks as original.\n"); 13319f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen // Don't allow repeated splitting as a safe guard against looping. 133249743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen setStage(Reg, RS_Split2); 13339f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen } 13349f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen continue; 13359f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen } 13369f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen 13379f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen // Other intervals are treated as new. This includes local intervals created 13389f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen // for blocks with multiple uses, and anything created by DCE. 13395928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen } 13405928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen 1341eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen if (VerifyEnabled) 1342ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen MF->verify(this, "After splitting live range around region"); 1343ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen} 1344ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 1345b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesenunsigned RAGreedy::tryRegionSplit(LiveInterval &VirtReg, AllocationOrder &Order, 13461feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey SmallVectorImpl<unsigned> &NewVRegs) { 1347c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen unsigned NumCands = 0; 134803ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen BlockFrequency BestCost; 134900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen 135000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // Check if we can split this live range around a compact region. 1351a16a25ddeaf495b78b04e2a19feeac00d9824e63Jakob Stoklund Olesen bool HasCompact = calcCompactRegion(GlobalCand.front()); 135200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen if (HasCompact) { 135300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // Yes, keep GlobalCand[0] as the compact region candidate. 135400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen NumCands = 1; 135503ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen BestCost = BlockFrequency::getMaxFrequency(); 135600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen } else { 135700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // No benefit from the compact region, our fallback will be per-block 135800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // splitting. Make sure we find a solution that is cheaper than spilling. 135903ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen BestCost = calcSpillCost(); 136036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines DEBUG(dbgs() << "Cost of isolating all blocks = "; 136136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines MBFI->printBlockFreq(dbgs(), BestCost) << '\n'); 136200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen } 136396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 136436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned BestCand = 136536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines calculateRegionSplitCost(VirtReg, Order, BestCost, NumCands, 136636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines false/*IgnoreCSR*/); 136736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 136836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // No solutions found, fall back to single block splitting. 136936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (!HasCompact && BestCand == NoCand) 137036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return 0; 137136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 137236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return doRegionSplit(VirtReg, BestCand, HasCompact, NewVRegs); 137336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines} 137436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 137536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesunsigned RAGreedy::calculateRegionSplitCost(LiveInterval &VirtReg, 137636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines AllocationOrder &Order, 137736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines BlockFrequency &BestCost, 137836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned &NumCands, 137936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines bool IgnoreCSR) { 138036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned BestCand = NoCand; 1381b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen Order.rewind(); 1382c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen while (unsigned PhysReg = Order.next()) { 1383cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (IgnoreCSR && isUnusedCalleeSavedReg(PhysReg)) 1384cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar continue; 138536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 1386f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen // Discard bad candidates before we run out of interference cache cursors. 1387f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen // This will only affect register classes with a lot of registers (>32). 1388f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen if (NumCands == IntfCache.getMaxCursors()) { 1389f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen unsigned WorstCount = ~0u; 1390f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen unsigned Worst = 0; 1391f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen for (unsigned i = 0; i != NumCands; ++i) { 139200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen if (i == BestCand || !GlobalCand[i].PhysReg) 1393f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen continue; 1394f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen unsigned Count = GlobalCand[i].LiveBundles.count(); 1395f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen if (Count < WorstCount) 1396f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen Worst = i, WorstCount = Count; 1397f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen } 1398f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen --NumCands; 1399f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen GlobalCand[Worst] = GlobalCand[NumCands]; 14007bdf0060a00f04ad03d3c6f294d8db6f4951dbc2Jakob Stoklund Olesen if (BestCand == NumCands) 14017bdf0060a00f04ad03d3c6f294d8db6f4951dbc2Jakob Stoklund Olesen BestCand = Worst; 1402f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen } 1403f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen 1404c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen if (GlobalCand.size() <= NumCands) 1405c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen GlobalCand.resize(NumCands+1); 1406c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen GlobalSplitCandidate &Cand = GlobalCand[NumCands]; 1407c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen Cand.reset(IntfCache, PhysReg); 140896dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen 1409c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen SpillPlacer->prepare(Cand.LiveBundles); 141003ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen BlockFrequency Cost; 1411c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen if (!addSplitConstraints(Cand.Intf, Cost)) { 1412f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tno positive bundles\n"); 14131b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen continue; 14141b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen } 141536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tstatic = "; 141636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines MBFI->printBlockFreq(dbgs(), Cost)); 1417200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen if (Cost >= BestCost) { 1418200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen DEBUG({ 1419200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen if (BestCand == NoCand) 1420200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen dbgs() << " worse than no bundles\n"; 1421200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen else 1422200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen dbgs() << " worse than " 1423200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen << PrintReg(GlobalCand[BestCand].PhysReg, TRI) << '\n'; 1424200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen }); 1425b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen continue; 1426874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen } 1427c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen growRegion(Cand); 1428ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 14299efa2a263ea470caacef1c85f6ca45e32bf516d3Jakob Stoklund Olesen SpillPlacer->finish(); 14309efa2a263ea470caacef1c85f6ca45e32bf516d3Jakob Stoklund Olesen 1431ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen // No live bundles, defer to splitSingleBlocks(). 1432c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen if (!Cand.LiveBundles.any()) { 1433874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen DEBUG(dbgs() << " no bundles.\n"); 1434ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen continue; 1435874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen } 1436ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 1437c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen Cost += calcGlobalSplitCost(Cand); 1438874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen DEBUG({ 143936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines dbgs() << ", total = "; MBFI->printBlockFreq(dbgs(), Cost) 144036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines << " with bundles"; 1441c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen for (int i = Cand.LiveBundles.find_first(); i>=0; 1442c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen i = Cand.LiveBundles.find_next(i)) 1443874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen dbgs() << " EB#" << i; 1444874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen dbgs() << ".\n"; 1445874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen }); 1446200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen if (Cost < BestCost) { 1447c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen BestCand = NumCands; 144803ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen BestCost = Cost; 1449b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen } 1450c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen ++NumCands; 1451b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen } 145236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return BestCand; 145336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines} 1454ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 145536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesunsigned RAGreedy::doRegionSplit(LiveInterval &VirtReg, unsigned BestCand, 145636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines bool HasCompact, 145736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallVectorImpl<unsigned> &NewVRegs) { 145836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallVector<unsigned, 8> UsedCands; 145900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // Prepare split editor. 146020942dcd8634ad75091fe89669868cfebf74e869Jakob Stoklund Olesen LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this); 1461708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund Olesen SE->reset(LREdit, SplitSpillMode); 146200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen 146300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // Assign all edge bundles to the preferred candidate, or NoCand. 146400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen BundleCand.assign(Bundles->getNumBundles(), NoCand); 146500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen 146600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // Assign bundles for the best candidate region. 146700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen if (BestCand != NoCand) { 146800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen GlobalSplitCandidate &Cand = GlobalCand[BestCand]; 146900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen if (unsigned B = Cand.getBundles(BundleCand, BestCand)) { 147000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen UsedCands.push_back(BestCand); 147100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen Cand.IntvIdx = SE->openIntv(); 147200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen DEBUG(dbgs() << "Split for " << PrintReg(Cand.PhysReg, TRI) << " in " 147300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen << B << " bundles, intv " << Cand.IntvIdx << ".\n"); 147432668ea7a290ee1cb6bfe8cd677cdd4e5df05b4dChandler Carruth (void)B; 147500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen } 147600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen } 147700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen 147800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen // Assign bundles for the compact region. 147900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen if (HasCompact) { 148000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen GlobalSplitCandidate &Cand = GlobalCand.front(); 148100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen assert(!Cand.PhysReg && "Compact region has no physreg"); 148200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen if (unsigned B = Cand.getBundles(BundleCand, 0)) { 148300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen UsedCands.push_back(0); 148400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen Cand.IntvIdx = SE->openIntv(); 148500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen DEBUG(dbgs() << "Split for compact region in " << B << " bundles, intv " 148600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen << Cand.IntvIdx << ".\n"); 148732668ea7a290ee1cb6bfe8cd677cdd4e5df05b4dChandler Carruth (void)B; 148800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen } 148900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen } 149000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen 149100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen splitAroundRegion(LREdit, UsedCands); 1492b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen return 0; 1493b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen} 1494b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 1495ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 1496ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen//===----------------------------------------------------------------------===// 1497dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen// Per-Block Splitting 1498dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen//===----------------------------------------------------------------------===// 1499dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen 1500dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen/// tryBlockSplit - Split a global live range around every block with uses. This 1501dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen/// creates a lot of local live ranges, that will be split by tryLocalSplit if 1502dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen/// they don't allocate. 1503dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesenunsigned RAGreedy::tryBlockSplit(LiveInterval &VirtReg, AllocationOrder &Order, 15041feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey SmallVectorImpl<unsigned> &NewVRegs) { 1505dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen assert(&SA->getParent() == &VirtReg && "Live range wasn't analyzed"); 1506dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen unsigned Reg = VirtReg.reg; 1507dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen bool SingleInstrs = RegClassInfo.isProperSubClass(MRI->getRegClass(Reg)); 150820942dcd8634ad75091fe89669868cfebf74e869Jakob Stoklund Olesen LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this); 1509708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund Olesen SE->reset(LREdit, SplitSpillMode); 1510dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); 1511dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen for (unsigned i = 0; i != UseBlocks.size(); ++i) { 1512dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 1513dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen if (SA->shouldSplitSingleBlock(BI, SingleInstrs)) 1514dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen SE->splitSingleBlock(BI); 1515dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen } 1516dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen // No blocks were split. 1517dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen if (LREdit.empty()) 1518dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen return 0; 1519dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen 1520dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen // We did split for some blocks. 1521a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen SmallVector<unsigned, 8> IntvMap; 1522a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen SE->finish(&IntvMap); 15231f8804263ffc5e6843d81f5c7bd9c739aa90fde5Jakob Stoklund Olesen 15241f8804263ffc5e6843d81f5c7bd9c739aa90fde5Jakob Stoklund Olesen // Tell LiveDebugVariables about the new ranges. 15251feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey DebugVars->splitRegister(Reg, LREdit.regs(), *LIS); 15261f8804263ffc5e6843d81f5c7bd9c739aa90fde5Jakob Stoklund Olesen 1527a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen ExtraRegInfo.resize(MRI->getNumVirtRegs()); 1528a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen 1529a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen // Sort out the new intervals created by splitting. The remainder interval 1530a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen // goes straight to spilling, the new local ranges get to stay RS_New. 1531a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen for (unsigned i = 0, e = LREdit.size(); i != e; ++i) { 15321feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey LiveInterval &LI = LIS->getInterval(LREdit.get(i)); 1533a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen if (getStage(LI) == RS_New && IntvMap[i] == 0) 1534a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen setStage(LI, RS_Spill); 1535a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen } 1536a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen 1537dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen if (VerifyEnabled) 1538dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen MF->verify(this, "After splitting live range around basic blocks"); 1539dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen return 0; 1540dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen} 1541dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen 1542d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen 1543d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen//===----------------------------------------------------------------------===// 1544d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen// Per-Instruction Splitting 1545d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen//===----------------------------------------------------------------------===// 1546d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen 154736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// Get the number of allocatable registers that match the constraints of \p Reg 154836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// on \p MI and that are also in \p SuperRC. 154936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesstatic unsigned getNumAllocatableRegsForConstraints( 155036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const MachineInstr *MI, unsigned Reg, const TargetRegisterClass *SuperRC, 155136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const TargetInstrInfo *TII, const TargetRegisterInfo *TRI, 155236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const RegisterClassInfo &RCI) { 155336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines assert(SuperRC && "Invalid register class"); 155436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 155536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const TargetRegisterClass *ConstrainedRC = 155636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines MI->getRegClassConstraintEffectForVReg(Reg, SuperRC, TII, TRI, 155736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines /* ExploreBundle */ true); 155836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (!ConstrainedRC) 155936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return 0; 156036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return RCI.getNumAllocatableRegs(ConstrainedRC); 156136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines} 156236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 1563d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen/// tryInstructionSplit - Split a live range around individual instructions. 1564d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen/// This is normally not worthwhile since the spiller is doing essentially the 1565d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen/// same thing. However, when the live range is in a constrained register 1566d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen/// class, it may help to insert copies such that parts of the live range can 1567d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen/// be moved to a larger register class. 1568d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen/// 1569d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen/// This is similar to spilling to a larger register class. 1570d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesenunsigned 1571d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund OlesenRAGreedy::tryInstructionSplit(LiveInterval &VirtReg, AllocationOrder &Order, 15721feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey SmallVectorImpl<unsigned> &NewVRegs) { 157336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const TargetRegisterClass *CurRC = MRI->getRegClass(VirtReg.reg); 1574d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen // There is no point to this if there are no larger sub-classes. 157536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (!RegClassInfo.isProperSubClass(CurRC)) 1576d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen return 0; 1577d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen 1578d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen // Always enable split spill mode, since we're effectively spilling to a 1579d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen // register. 1580d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this); 1581d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen SE->reset(LREdit, SplitEditor::SM_Size); 1582d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen 1583d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen ArrayRef<SlotIndex> Uses = SA->getUseSlots(); 1584d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen if (Uses.size() <= 1) 1585d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen return 0; 1586d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen 1587d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen DEBUG(dbgs() << "Split around " << Uses.size() << " individual instrs.\n"); 1588d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen 15894c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar const TargetRegisterClass *SuperRC = 15904c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar TRI->getLargestLegalSuperClass(CurRC, *MF); 159136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned SuperRCNumAllocatableRegs = RCI.getNumAllocatableRegs(SuperRC); 159236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Split around every non-copy instruction if this split will relax 159336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // the constraints on the virtual register. 159436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Otherwise, splitting just inserts uncoalescable copies that do not help 159536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // the allocation. 1596d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen for (unsigned i = 0; i != Uses.size(); ++i) { 1597d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen if (const MachineInstr *MI = Indexes->getInstructionFromIndex(Uses[i])) 159836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (MI->isFullCopy() || 159936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SuperRCNumAllocatableRegs == 160036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines getNumAllocatableRegsForConstraints(MI, VirtReg.reg, SuperRC, TII, 160136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines TRI, RCI)) { 1602d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen DEBUG(dbgs() << " skip:\t" << Uses[i] << '\t' << *MI); 1603d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen continue; 1604d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen } 1605d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen SE->openIntv(); 1606d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen SlotIndex SegStart = SE->enterIntvBefore(Uses[i]); 1607d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen SlotIndex SegStop = SE->leaveIntvAfter(Uses[i]); 1608d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen SE->useIntv(SegStart, SegStop); 1609d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen } 1610d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen 1611d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen if (LREdit.empty()) { 1612d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen DEBUG(dbgs() << "All uses were copies.\n"); 1613d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen return 0; 1614d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen } 1615d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen 1616d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen SmallVector<unsigned, 8> IntvMap; 1617d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen SE->finish(&IntvMap); 16181feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey DebugVars->splitRegister(VirtReg.reg, LREdit.regs(), *LIS); 1619d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen ExtraRegInfo.resize(MRI->getNumVirtRegs()); 1620d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen 1621d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen // Assign all new registers to RS_Spill. This was the last chance. 1622d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen setStage(LREdit.begin(), LREdit.end(), RS_Spill); 1623d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen return 0; 1624d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen} 1625d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen 1626d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen 1627dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen//===----------------------------------------------------------------------===// 1628034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen// Local Splitting 1629034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen//===----------------------------------------------------------------------===// 1630034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1631034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1632034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// calcGapWeights - Compute the maximum spill weight that needs to be evicted 1633034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// in order to use PhysReg between two entries in SA->UseSlots. 1634034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// 1635034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// GapWeight[i] represents the gap between UseSlots[i] and UseSlots[i+1]. 1636034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// 1637034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesenvoid RAGreedy::calcGapWeights(unsigned PhysReg, 1638034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SmallVectorImpl<float> &GapWeight) { 1639db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen assert(SA->getUseBlocks().size() == 1 && "Not a local interval"); 1640db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front(); 1641b20b518f800293ea6b2bed04134c71293ac52403Jakob Stoklund Olesen ArrayRef<SlotIndex> Uses = SA->getUseSlots(); 1642034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const unsigned NumGaps = Uses.size()-1; 1643034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1644034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Start and end points for the interference check. 1645fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen SlotIndex StartIdx = 1646fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen BI.LiveIn ? BI.FirstInstr.getBaseIndex() : BI.FirstInstr; 1647fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen SlotIndex StopIdx = 1648fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen BI.LiveOut ? BI.LastInstr.getBoundaryIndex() : BI.LastInstr; 1649034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1650034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen GapWeight.assign(NumGaps, 0.0f); 1651034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1652034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Add interference from each overlapping register. 1653042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { 1654042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen if (!Matrix->query(const_cast<LiveInterval&>(SA->getParent()), *Units) 1655042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen .checkInterference()) 1656034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen continue; 1657034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1658fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen // We know that VirtReg is a continuous interval from FirstInstr to 1659fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen // LastInstr, so we don't need InterferenceQuery. 1660034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // 1661034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Interference that overlaps an instruction is counted in both gaps 1662034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // surrounding the instruction. The exception is interference before 1663034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // StartIdx and after StopIdx. 1664034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // 1665042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen LiveIntervalUnion::SegmentIter IntI = 1666042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen Matrix->getLiveUnions()[*Units] .find(StartIdx); 1667034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (unsigned Gap = 0; IntI.valid() && IntI.start() < StopIdx; ++IntI) { 1668034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Skip the gaps before IntI. 1669034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen while (Uses[Gap+1].getBoundaryIndex() < IntI.start()) 1670034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (++Gap == NumGaps) 1671034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen break; 1672034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Gap == NumGaps) 1673034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen break; 1674034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1675034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Update the gaps covered by IntI. 1676034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const float weight = IntI.value()->weight; 1677034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (; Gap != NumGaps; ++Gap) { 1678034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen GapWeight[Gap] = std::max(GapWeight[Gap], weight); 1679034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Uses[Gap+1].getBaseIndex() >= IntI.stop()) 1680034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen break; 1681034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1682034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Gap == NumGaps) 1683034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen break; 1684034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1685034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1686042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen 1687042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen // Add fixed interference. 1688042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { 16894f3b5e8c9232e43d1291aab8db5f5698d7ee0ea4Matthias Braun const LiveRange &LR = LIS->getRegUnit(*Units); 16904f3b5e8c9232e43d1291aab8db5f5698d7ee0ea4Matthias Braun LiveRange::const_iterator I = LR.find(StartIdx); 16914f3b5e8c9232e43d1291aab8db5f5698d7ee0ea4Matthias Braun LiveRange::const_iterator E = LR.end(); 1692042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen 1693042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen // Same loop as above. Mark any overlapped gaps as HUGE_VALF. 1694042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen for (unsigned Gap = 0; I != E && I->start < StopIdx; ++I) { 1695042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen while (Uses[Gap+1].getBoundaryIndex() < I->start) 1696042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen if (++Gap == NumGaps) 1697042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen break; 1698042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen if (Gap == NumGaps) 1699042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen break; 1700042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen 1701042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen for (; Gap != NumGaps; ++Gap) { 1702eb3602472026dc029beb45ccbe09bc84162ba949Aaron Ballman GapWeight[Gap] = llvm::huge_valf; 1703042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen if (Uses[Gap+1].getBaseIndex() >= I->end) 1704042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen break; 1705042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen } 1706042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen if (Gap == NumGaps) 1707042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen break; 1708042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen } 1709042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen } 1710034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen} 1711034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1712034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// tryLocalSplit - Try to split VirtReg into smaller intervals inside its only 1713034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// basic block. 1714034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// 1715034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesenunsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order, 17161feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey SmallVectorImpl<unsigned> &NewVRegs) { 1717db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen assert(SA->getUseBlocks().size() == 1 && "Not a local interval"); 1718db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front(); 1719034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1720034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Note that it is possible to have an interval that is live-in or live-out 1721034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // while only covering a single block - A phi-def can use undef values from 1722034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // predecessors, and the block could be a single-block loop. 1723034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // We don't bother doing anything clever about such a case, we simply assume 1724fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen // that the interval is continuous from FirstInstr to LastInstr. We should 1725fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen // make sure that we don't do anything illegal to such an interval, though. 1726034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1727b20b518f800293ea6b2bed04134c71293ac52403Jakob Stoklund Olesen ArrayRef<SlotIndex> Uses = SA->getUseSlots(); 1728034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Uses.size() <= 2) 1729034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen return 0; 1730034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const unsigned NumGaps = Uses.size()-1; 1731034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1732034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG({ 1733034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen dbgs() << "tryLocalSplit: "; 1734034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (unsigned i = 0, e = Uses.size(); i != e; ++i) 1735b20b518f800293ea6b2bed04134c71293ac52403Jakob Stoklund Olesen dbgs() << ' ' << Uses[i]; 1736034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen dbgs() << '\n'; 1737034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen }); 1738034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1739a6d513f47467b90344a74624d4fb77673942e3ceJakob Stoklund Olesen // If VirtReg is live across any register mask operands, compute a list of 1740a6d513f47467b90344a74624d4fb77673942e3ceJakob Stoklund Olesen // gaps with register masks. 1741a6d513f47467b90344a74624d4fb77673942e3ceJakob Stoklund Olesen SmallVector<unsigned, 8> RegMaskGaps; 1742042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen if (Matrix->checkRegMaskInterference(VirtReg)) { 1743a6d513f47467b90344a74624d4fb77673942e3ceJakob Stoklund Olesen // Get regmask slots for the whole block. 1744a6d513f47467b90344a74624d4fb77673942e3ceJakob Stoklund Olesen ArrayRef<SlotIndex> RMS = LIS->getRegMaskSlotsInBlock(BI.MBB->getNumber()); 1745cac5fa39bd861861018bb2c3f0b36cc71c2caa38Jakob Stoklund Olesen DEBUG(dbgs() << RMS.size() << " regmasks in block:"); 1746a6d513f47467b90344a74624d4fb77673942e3ceJakob Stoklund Olesen // Constrain to VirtReg's live range. 1747cac5fa39bd861861018bb2c3f0b36cc71c2caa38Jakob Stoklund Olesen unsigned ri = std::lower_bound(RMS.begin(), RMS.end(), 1748cac5fa39bd861861018bb2c3f0b36cc71c2caa38Jakob Stoklund Olesen Uses.front().getRegSlot()) - RMS.begin(); 1749a6d513f47467b90344a74624d4fb77673942e3ceJakob Stoklund Olesen unsigned re = RMS.size(); 1750a6d513f47467b90344a74624d4fb77673942e3ceJakob Stoklund Olesen for (unsigned i = 0; i != NumGaps && ri != re; ++i) { 1751cac5fa39bd861861018bb2c3f0b36cc71c2caa38Jakob Stoklund Olesen // Look for Uses[i] <= RMS <= Uses[i+1]. 1752cac5fa39bd861861018bb2c3f0b36cc71c2caa38Jakob Stoklund Olesen assert(!SlotIndex::isEarlierInstr(RMS[ri], Uses[i])); 1753cac5fa39bd861861018bb2c3f0b36cc71c2caa38Jakob Stoklund Olesen if (SlotIndex::isEarlierInstr(Uses[i+1], RMS[ri])) 1754a6d513f47467b90344a74624d4fb77673942e3ceJakob Stoklund Olesen continue; 1755cac5fa39bd861861018bb2c3f0b36cc71c2caa38Jakob Stoklund Olesen // Skip a regmask on the same instruction as the last use. It doesn't 1756cac5fa39bd861861018bb2c3f0b36cc71c2caa38Jakob Stoklund Olesen // overlap the live range. 1757cac5fa39bd861861018bb2c3f0b36cc71c2caa38Jakob Stoklund Olesen if (SlotIndex::isSameInstr(Uses[i+1], RMS[ri]) && i+1 == NumGaps) 1758cac5fa39bd861861018bb2c3f0b36cc71c2caa38Jakob Stoklund Olesen break; 1759cac5fa39bd861861018bb2c3f0b36cc71c2caa38Jakob Stoklund Olesen DEBUG(dbgs() << ' ' << RMS[ri] << ':' << Uses[i] << '-' << Uses[i+1]); 1760a6d513f47467b90344a74624d4fb77673942e3ceJakob Stoklund Olesen RegMaskGaps.push_back(i); 1761cac5fa39bd861861018bb2c3f0b36cc71c2caa38Jakob Stoklund Olesen // Advance ri to the next gap. A regmask on one of the uses counts in 1762cac5fa39bd861861018bb2c3f0b36cc71c2caa38Jakob Stoklund Olesen // both gaps. 1763cac5fa39bd861861018bb2c3f0b36cc71c2caa38Jakob Stoklund Olesen while (ri != re && SlotIndex::isEarlierInstr(RMS[ri], Uses[i+1])) 1764cac5fa39bd861861018bb2c3f0b36cc71c2caa38Jakob Stoklund Olesen ++ri; 1765a6d513f47467b90344a74624d4fb77673942e3ceJakob Stoklund Olesen } 1766cac5fa39bd861861018bb2c3f0b36cc71c2caa38Jakob Stoklund Olesen DEBUG(dbgs() << '\n'); 1767a6d513f47467b90344a74624d4fb77673942e3ceJakob Stoklund Olesen } 1768a6d513f47467b90344a74624d4fb77673942e3ceJakob Stoklund Olesen 1769b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // Since we allow local split results to be split again, there is a risk of 1770b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // creating infinite loops. It is tempting to require that the new live 1771b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // ranges have less instructions than the original. That would guarantee 1772b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // convergence, but it is too strict. A live range with 3 instructions can be 1773b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // split 2+3 (including the COPY), and we want to allow that. 1774b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // 1775b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // Instead we use these rules: 1776b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // 177749743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen // 1. Allow any split for ranges with getStage() < RS_Split2. (Except for the 1778b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // noop split, of course). 177949743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen // 2. Require progress be made for ranges with getStage() == RS_Split2. All 1780b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // the new ranges must have fewer instructions than before the split. 178149743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen // 3. New ranges with the same number of instructions are marked RS_Split2, 1782b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // smaller ranges are marked RS_New. 1783b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // 1784b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // These rules allow a 3 -> 2+3 split once, which we need. They also prevent 1785b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // excessive splitting and infinite loops. 1786b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // 178749743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen bool ProgressRequired = getStage(VirtReg) >= RS_Split2; 1788034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1789b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // Best split candidate. 1790034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen unsigned BestBefore = NumGaps; 1791034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen unsigned BestAfter = 0; 1792034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen float BestDiff = 0; 1793034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 179403ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen const float blockFreq = 179503ef600623704e6dbf6ef0322062395e7b70bcd2Jakob Stoklund Olesen SpillPlacer->getBlockFrequency(BI.MBB->getNumber()).getFrequency() * 179636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines (1.0f / MBFI->getEntryFreq()); 1797034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen SmallVector<float, 8> GapWeight; 1798034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1799034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen Order.rewind(); 1800034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen while (unsigned PhysReg = Order.next()) { 1801034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Keep track of the largest spill weight that would need to be evicted in 1802034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // order to make use of PhysReg between UseSlots[i] and UseSlots[i+1]. 1803034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen calcGapWeights(PhysReg, GapWeight); 1804034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1805a6d513f47467b90344a74624d4fb77673942e3ceJakob Stoklund Olesen // Remove any gaps with regmask clobbers. 1806042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen if (Matrix->checkRegMaskInterference(VirtReg, PhysReg)) 1807a6d513f47467b90344a74624d4fb77673942e3ceJakob Stoklund Olesen for (unsigned i = 0, e = RegMaskGaps.size(); i != e; ++i) 1808eb3602472026dc029beb45ccbe09bc84162ba949Aaron Ballman GapWeight[RegMaskGaps[i]] = llvm::huge_valf; 1809a6d513f47467b90344a74624d4fb77673942e3ceJakob Stoklund Olesen 1810034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Try to find the best sequence of gaps to close. 1811034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // The new spill weight must be larger than any gap interference. 1812034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1813034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // We will split before Uses[SplitBefore] and after Uses[SplitAfter]. 1814b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen unsigned SplitBefore = 0, SplitAfter = 1; 1815034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1816034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // MaxGap should always be max(GapWeight[SplitBefore..SplitAfter-1]). 1817034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // It is the spill weight that needs to be evicted. 1818034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen float MaxGap = GapWeight[0]; 1819034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1820034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (;;) { 1821034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Live before/after split? 1822034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const bool LiveBefore = SplitBefore != 0 || BI.LiveIn; 1823034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen const bool LiveAfter = SplitAfter != NumGaps || BI.LiveOut; 1824034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1825034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << PrintReg(PhysReg, TRI) << ' ' 1826034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen << Uses[SplitBefore] << '-' << Uses[SplitAfter] 1827034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen << " i=" << MaxGap); 1828034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1829034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Stop before the interval gets so big we wouldn't be making progress. 1830034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (!LiveBefore && !LiveAfter) { 1831034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << " all\n"); 1832034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen break; 1833034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1834034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Should the interval be extended or shrunk? 1835034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen bool Shrink = true; 1836034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1837b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // How many gaps would the new range have? 1838b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen unsigned NewGaps = LiveBefore + SplitAfter - SplitBefore + LiveAfter; 1839b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen 1840b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // Legally, without causing looping? 1841b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen bool Legal = !ProgressRequired || NewGaps < NumGaps; 1842b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen 1843eb3602472026dc029beb45ccbe09bc84162ba949Aaron Ballman if (Legal && MaxGap < llvm::huge_valf) { 1844b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // Estimate the new spill weight. Each instruction reads or writes the 1845b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // register. Conservatively assume there are no read-modify-write 1846b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // instructions. 1847034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // 1848b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // Try to guess the size of the new interval. 184937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines const float EstWeight = normalizeSpillWeight( 185037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines blockFreq * (NewGaps + 1), 185137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines Uses[SplitBefore].distance(Uses[SplitAfter]) + 185237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines (LiveBefore + LiveAfter) * SlotIndex::InstrDist, 185337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 1); 1854034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Would this split be possible to allocate? 1855034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Never allocate all gaps, we wouldn't be making progress. 185666446c803a11a26e4bb39b74091d146ac850ae4cJakob Stoklund Olesen DEBUG(dbgs() << " w=" << EstWeight); 185766446c803a11a26e4bb39b74091d146ac850ae4cJakob Stoklund Olesen if (EstWeight * Hysteresis >= MaxGap) { 1858034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen Shrink = false; 185966446c803a11a26e4bb39b74091d146ac850ae4cJakob Stoklund Olesen float Diff = EstWeight - MaxGap; 1860034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Diff > BestDiff) { 1861034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << " (best)"); 186266446c803a11a26e4bb39b74091d146ac850ae4cJakob Stoklund Olesen BestDiff = Hysteresis * Diff; 1863034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen BestBefore = SplitBefore; 1864034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen BestAfter = SplitAfter; 1865034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1866034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1867034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1868034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1869034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Try to shrink. 1870034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (Shrink) { 1871b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen if (++SplitBefore < SplitAfter) { 1872034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << " shrink\n"); 1873034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Recompute the max when necessary. 1874034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (GapWeight[SplitBefore - 1] >= MaxGap) { 1875034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen MaxGap = GapWeight[SplitBefore]; 1876034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen for (unsigned i = SplitBefore + 1; i != SplitAfter; ++i) 1877034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen MaxGap = std::max(MaxGap, GapWeight[i]); 1878034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1879034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen continue; 1880034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1881034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen MaxGap = 0; 1882034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1883034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1884034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Try to extend the interval. 1885034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (SplitAfter >= NumGaps) { 1886034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << " end\n"); 1887034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen break; 1888034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1889034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1890034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << " extend\n"); 1891b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen MaxGap = std::max(MaxGap, GapWeight[SplitAfter++]); 1892034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1893034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen } 1894034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1895034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Didn't find any candidates? 1896034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen if (BestBefore == NumGaps) 1897034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen return 0; 1898034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1899034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen DEBUG(dbgs() << "Best local split range: " << Uses[BestBefore] 1900034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen << '-' << Uses[BestAfter] << ", " << BestDiff 1901034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen << ", " << (BestAfter - BestBefore + 1) << " instrs\n"); 1902034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 190320942dcd8634ad75091fe89669868cfebf74e869Jakob Stoklund Olesen LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this); 1904bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->reset(LREdit); 1905bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen 1906bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->openIntv(); 1907bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SlotIndex SegStart = SE->enterIntvBefore(Uses[BestBefore]); 1908bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SlotIndex SegStop = SE->leaveIntvAfter(Uses[BestAfter]); 1909bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen SE->useIntv(SegStart, SegStop); 1910b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen SmallVector<unsigned, 8> IntvMap; 1911b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen SE->finish(&IntvMap); 19121feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey DebugVars->splitRegister(VirtReg.reg, LREdit.regs(), *LIS); 1913b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen 1914b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // If the new range has the same number of instructions as before, mark it as 191549743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen // RS_Split2 so the next split will be forced to make progress. Otherwise, 1916b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen // leave the new intervals as RS_New so they can compete. 1917b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen bool LiveBefore = BestBefore != 0 || BI.LiveIn; 1918b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen bool LiveAfter = BestAfter != NumGaps || BI.LiveOut; 1919b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen unsigned NewGaps = LiveBefore + BestAfter - BestBefore + LiveAfter; 1920b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen if (NewGaps >= NumGaps) { 1921b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen DEBUG(dbgs() << "Tagging non-progress ranges: "); 1922b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen assert(!ProgressRequired && "Didn't make progress when it was required."); 1923b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen for (unsigned i = 0, e = IntvMap.size(); i != e; ++i) 1924b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen if (IntvMap[i] == 1) { 19251feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey setStage(LIS->getInterval(LREdit.get(i)), RS_Split2); 19261feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey DEBUG(dbgs() << PrintReg(LREdit.get(i))); 1927b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen } 1928b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen DEBUG(dbgs() << '\n'); 1929b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen } 19300db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen ++NumLocalSplits; 1931034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1932034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen return 0; 1933034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen} 1934034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen 1935034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen//===----------------------------------------------------------------------===// 1936ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen// Live Range Splitting 1937ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen//===----------------------------------------------------------------------===// 1938ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 1939ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// trySplit - Try to split VirtReg or one of its interferences, making it 1940ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// assignable. 1941ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// @return Physreg when VirtReg may be assigned and/or new NewVRegs. 1942ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesenunsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order, 19431feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey SmallVectorImpl<unsigned>&NewVRegs) { 1944ccfa446450c9e3e0b3591343c4c5bea1e4cdc043Jakob Stoklund Olesen // Ranges must be Split2 or less. 1945ccfa446450c9e3e0b3591343c4c5bea1e4cdc043Jakob Stoklund Olesen if (getStage(VirtReg) >= RS_Spill) 1946ccfa446450c9e3e0b3591343c4c5bea1e4cdc043Jakob Stoklund Olesen return 0; 1947ccfa446450c9e3e0b3591343c4c5bea1e4cdc043Jakob Stoklund Olesen 1948034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen // Local intervals are handled separately. 1949a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen if (LIS->intervalIsInOneMBB(VirtReg)) { 1950a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen NamedRegionTimer T("Local Splitting", TimerGroupName, TimePassesIsEnabled); 195122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen SA->analyze(&VirtReg); 1952d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen unsigned PhysReg = tryLocalSplit(VirtReg, Order, NewVRegs); 1953d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen if (PhysReg || !NewVRegs.empty()) 1954d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen return PhysReg; 1955d74d2847573df690b6a91254688ef3fd974f83f7Jakob Stoklund Olesen return tryInstructionSplit(VirtReg, Order, NewVRegs); 1956a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen } 1957a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen 1958a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen NamedRegionTimer T("Global Splitting", TimerGroupName, TimePassesIsEnabled); 1959ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 196022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen SA->analyze(&VirtReg); 196122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen 19627d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen // FIXME: SplitAnalysis may repair broken live ranges coming from the 19637d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen // coalescer. That may cause the range to become allocatable which means that 19647d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen // tryRegionSplit won't be making progress. This check should be replaced with 19657d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen // an assertion when the coalescer is fixed. 19667d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen if (SA->didRepairRange()) { 19677d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen // VirtReg has changed, so all cached queries are invalid. 1968042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen Matrix->invalidateVirtRegs(); 19697d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs)) 19707d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen return PhysReg; 19717d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen } 19727d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen 197349743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen // First try to split around a region spanning multiple blocks. RS_Split2 197449743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen // ranges already made dubious progress with region splitting, so they go 197549743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen // straight to single block splitting. 197649743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen if (getStage(VirtReg) < RS_Split2) { 197749743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen unsigned PhysReg = tryRegionSplit(VirtReg, Order, NewVRegs); 197849743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen if (PhysReg || !NewVRegs.empty()) 197949743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen return PhysReg; 198049743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen } 1981ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 1982dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen // Then isolate blocks. 1983dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen return tryBlockSplit(VirtReg, Order, NewVRegs); 1984ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen} 1985ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 198636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines//===----------------------------------------------------------------------===// 198736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// Last Chance Recoloring 198836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines//===----------------------------------------------------------------------===// 198936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 199036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// mayRecolorAllInterferences - Check if the virtual registers that 199136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// interfere with \p VirtReg on \p PhysReg (or one of its aliases) may be 199236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// recolored to free \p PhysReg. 199336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// When true is returned, \p RecoloringCandidates has been augmented with all 199436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// the live intervals that need to be recolored in order to free \p PhysReg 199536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// for \p VirtReg. 199636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// \p FixedRegisters contains all the virtual registers that cannot be 199736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// recolored. 199836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesbool 199936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinesRAGreedy::mayRecolorAllInterferences(unsigned PhysReg, LiveInterval &VirtReg, 200036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallLISet &RecoloringCandidates, 200136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const SmallVirtRegSet &FixedRegisters) { 200236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const TargetRegisterClass *CurRC = MRI->getRegClass(VirtReg.reg); 200336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 200436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { 200536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units); 200636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // If there is LastChanceRecoloringMaxInterference or more interferences, 200736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // chances are one would not be recolorable. 200836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (Q.collectInterferingVRegs(LastChanceRecoloringMaxInterference) >= 2009dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines LastChanceRecoloringMaxInterference && !ExhaustiveSearch) { 201036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines DEBUG(dbgs() << "Early abort: too many interferences.\n"); 2011dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines CutOffInfo |= CO_Interf; 201236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return false; 201336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 201436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines for (unsigned i = Q.interferingVRegs().size(); i; --i) { 201536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines LiveInterval *Intf = Q.interferingVRegs()[i - 1]; 201636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // If Intf is done and sit on the same register class as VirtReg, 201736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // it would not be recolorable as it is in the same state as VirtReg. 201836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if ((getStage(*Intf) == RS_Done && 201936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines MRI->getRegClass(Intf->reg) == CurRC) || 202036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines FixedRegisters.count(Intf->reg)) { 202136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines DEBUG(dbgs() << "Early abort: the inteference is not recolorable.\n"); 202236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return false; 202336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 202436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines RecoloringCandidates.insert(Intf); 202536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 202636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 202736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return true; 202836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines} 202936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 203036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// tryLastChanceRecoloring - Try to assign a color to \p VirtReg by recoloring 203136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// its interferences. 203236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// Last chance recoloring chooses a color for \p VirtReg and recolors every 203336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// virtual register that was using it. The recoloring process may recursively 203436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// use the last chance recoloring. Therefore, when a virtual register has been 203536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// assigned a color by this mechanism, it is marked as Fixed, i.e., it cannot 203636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// be last-chance-recolored again during this recoloring "session". 203736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// E.g., 203836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// Let 203936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// vA can use {R1, R2 } 204036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// vB can use { R2, R3} 204136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// vC can use {R1 } 204236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// Where vA, vB, and vC cannot be split anymore (they are reloads for 204336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// instance) and they all interfere. 204436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// 204536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// vA is assigned R1 204636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// vB is assigned R2 204736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// vC tries to evict vA but vA is already done. 204836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// Regular register allocation fails. 204936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// 205036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// Last chance recoloring kicks in: 205136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// vC does as if vA was evicted => vC uses R1. 205236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// vC is marked as fixed. 205336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// vA needs to find a color. 205436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// None are available. 205536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// vA cannot evict vC: vC is a fixed virtual register now. 205636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// vA does as if vB was evicted => vA uses R2. 205736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// vB needs to find a color. 205836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// R3 is available. 205936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// Recoloring => vC = R1, vA = R2, vB = R3 206036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// 206136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// \p Order defines the preferred allocation order for \p VirtReg. 206236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// \p NewRegs will contain any new virtual register that have been created 206336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// (split, spill) during the process and that must be assigned. 206436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// \p FixedRegisters contains all the virtual registers that cannot be 206536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// recolored. 206636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// \p Depth gives the current depth of the last chance recoloring. 206736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// \return a physical register that can be used for VirtReg or ~0u if none 206836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// exists. 206936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesunsigned RAGreedy::tryLastChanceRecoloring(LiveInterval &VirtReg, 207036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines AllocationOrder &Order, 207136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallVectorImpl<unsigned> &NewVRegs, 207236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallVirtRegSet &FixedRegisters, 207336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned Depth) { 207436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines DEBUG(dbgs() << "Try last chance recoloring for " << VirtReg << '\n'); 207536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Ranges must be Done. 207636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines assert((getStage(VirtReg) >= RS_Done || !VirtReg.isSpillable()) && 207736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines "Last chance recoloring should really be last chance"); 207836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Set the max depth to LastChanceRecoloringMaxDepth. 207936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // We may want to reconsider that if we end up with a too large search space 208036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // for target with hundreds of registers. 208136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Indeed, in that case we may want to cut the search space earlier. 2082dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (Depth >= LastChanceRecoloringMaxDepth && !ExhaustiveSearch) { 208336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines DEBUG(dbgs() << "Abort because max depth has been reached.\n"); 2084dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines CutOffInfo |= CO_Depth; 208536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return ~0u; 208636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 208736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 208836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Set of Live intervals that will need to be recolored. 208936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallLISet RecoloringCandidates; 209036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Record the original mapping virtual register to physical register in case 209136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // the recoloring fails. 209236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines DenseMap<unsigned, unsigned> VirtRegToPhysReg; 209336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Mark VirtReg as fixed, i.e., it will not be recolored pass this point in 209436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // this recoloring "session". 209536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines FixedRegisters.insert(VirtReg.reg); 209636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 209736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Order.rewind(); 209836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines while (unsigned PhysReg = Order.next()) { 209936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines DEBUG(dbgs() << "Try to assign: " << VirtReg << " to " 210036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines << PrintReg(PhysReg, TRI) << '\n'); 210136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines RecoloringCandidates.clear(); 210236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines VirtRegToPhysReg.clear(); 210336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 210436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // It is only possible to recolor virtual register interference. 210536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (Matrix->checkInterference(VirtReg, PhysReg) > 210636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines LiveRegMatrix::IK_VirtReg) { 210736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines DEBUG(dbgs() << "Some inteferences are not with virtual registers.\n"); 210836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 210936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines continue; 211036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 211136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 211236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Early give up on this PhysReg if it is obvious we cannot recolor all 211336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // the interferences. 211436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (!mayRecolorAllInterferences(PhysReg, VirtReg, RecoloringCandidates, 211536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines FixedRegisters)) { 211636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines DEBUG(dbgs() << "Some inteferences cannot be recolored.\n"); 211736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines continue; 211836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 211936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 212036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // RecoloringCandidates contains all the virtual registers that interfer 212136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // with VirtReg on PhysReg (or one of its aliases). 212236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Enqueue them for recoloring and perform the actual recoloring. 212336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines PQueue RecoloringQueue; 212436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines for (SmallLISet::iterator It = RecoloringCandidates.begin(), 212536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines EndIt = RecoloringCandidates.end(); 212636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines It != EndIt; ++It) { 212736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned ItVirtReg = (*It)->reg; 212836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines enqueue(RecoloringQueue, *It); 212936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines assert(VRM->hasPhys(ItVirtReg) && 213036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines "Interferences are supposed to be with allocated vairables"); 213136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 213236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Record the current allocation. 213336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines VirtRegToPhysReg[ItVirtReg] = VRM->getPhys(ItVirtReg); 213436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // unset the related struct. 213536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Matrix->unassign(**It); 213636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 213736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 213836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Do as if VirtReg was assigned to PhysReg so that the underlying 213936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // recoloring has the right information about the interferes and 214036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // available colors. 214136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Matrix->assign(VirtReg, PhysReg); 214236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 214336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Save the current recoloring state. 214436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // If we cannot recolor all the interferences, we will have to start again 214536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // at this point for the next physical register. 214636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallVirtRegSet SaveFixedRegisters(FixedRegisters); 214736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (tryRecoloringCandidates(RecoloringQueue, NewVRegs, FixedRegisters, 214836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Depth)) { 214936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Do not mess up with the global assignment process. 215036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // I.e., VirtReg must be unassigned. 215136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Matrix->unassign(VirtReg); 215236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return PhysReg; 215336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 215436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 215536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines DEBUG(dbgs() << "Fail to assign: " << VirtReg << " to " 215636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines << PrintReg(PhysReg, TRI) << '\n'); 215736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 215836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // The recoloring attempt failed, undo the changes. 215936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines FixedRegisters = SaveFixedRegisters; 216036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Matrix->unassign(VirtReg); 216136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 216236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines for (SmallLISet::iterator It = RecoloringCandidates.begin(), 216336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines EndIt = RecoloringCandidates.end(); 216436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines It != EndIt; ++It) { 216536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned ItVirtReg = (*It)->reg; 216636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (VRM->hasPhys(ItVirtReg)) 216736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Matrix->unassign(**It); 2168cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar unsigned ItPhysReg = VirtRegToPhysReg[ItVirtReg]; 2169cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar Matrix->assign(**It, ItPhysReg); 217036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 217136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 217236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 217336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Last chance recoloring did not worked either, give up. 217436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return ~0u; 217536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines} 217636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 217736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// tryRecoloringCandidates - Try to assign a new color to every register 217836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// in \RecoloringQueue. 217936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// \p NewRegs will contain any new virtual register created during the 218036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// recoloring process. 218136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// \p FixedRegisters[in/out] contains all the registers that have been 218236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// recolored. 218336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// \return true if all virtual registers in RecoloringQueue were successfully 218436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// recolored, false otherwise. 218536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesbool RAGreedy::tryRecoloringCandidates(PQueue &RecoloringQueue, 218636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallVectorImpl<unsigned> &NewVRegs, 218736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallVirtRegSet &FixedRegisters, 218836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned Depth) { 218936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines while (!RecoloringQueue.empty()) { 219036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines LiveInterval *LI = dequeue(RecoloringQueue); 219136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines DEBUG(dbgs() << "Try to recolor: " << *LI << '\n'); 219236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned PhysReg; 219336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines PhysReg = selectOrSplitImpl(*LI, NewVRegs, FixedRegisters, Depth + 1); 219436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (PhysReg == ~0u || !PhysReg) 219536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return false; 219636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines DEBUG(dbgs() << "Recoloring of " << *LI 219736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines << " succeeded with: " << PrintReg(PhysReg, TRI) << '\n'); 219836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Matrix->assign(*LI, PhysReg); 219936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines FixedRegisters.insert(LI->reg); 220036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 220136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return true; 220236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines} 2203ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 2204b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen//===----------------------------------------------------------------------===// 2205770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen// Main Entry Point 2206770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen//===----------------------------------------------------------------------===// 2207770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen 2208770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesenunsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg, 22091feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey SmallVectorImpl<unsigned> &NewVRegs) { 2210dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines CutOffInfo = CO_None; 2211dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines LLVMContext &Ctx = MF->getFunction()->getContext(); 221236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallVirtRegSet FixedRegisters; 2213dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines unsigned Reg = selectOrSplitImpl(VirtReg, NewVRegs, FixedRegisters); 2214dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (Reg == ~0U && (CutOffInfo != CO_None)) { 2215dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines uint8_t CutOffEncountered = CutOffInfo & (CO_Depth | CO_Interf); 2216dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (CutOffEncountered == CO_Depth) 2217dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines Ctx.emitError("register allocation failed: maximum depth for recoloring " 2218dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines "reached. Use -fexhaustive-register-search to skip " 2219dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines "cutoffs"); 2220dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines else if (CutOffEncountered == CO_Interf) 2221dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines Ctx.emitError("register allocation failed: maximum interference for " 2222dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines "recoloring reached. Use -fexhaustive-register-search " 2223dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines "to skip cutoffs"); 2224dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines else if (CutOffEncountered == (CO_Depth | CO_Interf)) 2225dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines Ctx.emitError("register allocation failed: maximum interference and " 2226dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines "depth for recoloring reached. Use " 2227dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines "-fexhaustive-register-search to skip cutoffs"); 2228dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines } 2229dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines return Reg; 223036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines} 223136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 223236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// Using a CSR for the first time has a cost because it causes push|pop 223336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// to be added to prologue|epilogue. Splitting a cold section of the live 223436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// range can have lower cost than using the CSR for the first time; 223536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// Spilling a live range in the cold path can have lower cost than using 223636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// the CSR for the first time. Returns the physical register if we decide 223736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines/// to use the CSR; otherwise return 0. 223836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesunsigned RAGreedy::tryAssignCSRFirstTime(LiveInterval &VirtReg, 223936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines AllocationOrder &Order, 224036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned PhysReg, 224136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned &CostPerUseLimit, 224236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallVectorImpl<unsigned> &NewVRegs) { 224336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (getStage(VirtReg) == RS_Spill && VirtReg.isSpillable()) { 224436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // We choose spill over using the CSR for the first time if the spill cost 224536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // is lower than CSRCost. 224636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SA->analyze(&VirtReg); 224736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (calcSpillCost() >= CSRCost) 224836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return PhysReg; 224936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 225036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // We are going to spill, set CostPerUseLimit to 1 to make sure that 225136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // we will not use a callee-saved register in tryEvict. 225236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines CostPerUseLimit = 1; 225336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return 0; 225436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 225536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (getStage(VirtReg) < RS_Split) { 225636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // We choose pre-splitting over using the CSR for the first time if 225736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // the cost of splitting is lower than CSRCost. 225836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SA->analyze(&VirtReg); 225936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned NumCands = 0; 2260dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines BlockFrequency BestCost = CSRCost; // Don't modify CSRCost. 2261dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines unsigned BestCand = calculateRegionSplitCost(VirtReg, Order, BestCost, 2262dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines NumCands, true /*IgnoreCSR*/); 226336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (BestCand == NoCand) 226436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Use the CSR if we can't find a region split below CSRCost. 226536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return PhysReg; 226636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 226736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Perform the actual pre-splitting. 226836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines doRegionSplit(VirtReg, BestCand, false/*HasCompact*/, NewVRegs); 226936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return 0; 227036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 227136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return PhysReg; 227236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines} 227336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 2274ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesvoid RAGreedy::aboutToRemoveInterval(LiveInterval &LI) { 2275ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // Do not keep invalid information around. 2276ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines SetOfBrokenHints.remove(&LI); 2277ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines} 2278ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 2279dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesvoid RAGreedy::initializeCSRCost() { 2280dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines // We use the larger one out of the command-line option and the value report 2281dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines // by TRI. 2282dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines CSRCost = BlockFrequency( 2283dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines std::max((unsigned)CSRFirstTimeCost, TRI->getCSRFirstUseCost())); 2284dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (!CSRCost.getFrequency()) 2285dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines return; 2286dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 2287dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines // Raw cost is relative to Entry == 2^14; scale it appropriately. 2288dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines uint64_t ActualEntry = MBFI->getEntryFreq(); 2289dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (!ActualEntry) { 2290dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines CSRCost = 0; 2291dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines return; 2292dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines } 2293dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines uint64_t FixedEntry = 1 << 14; 2294dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (ActualEntry < FixedEntry) 2295dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines CSRCost *= BranchProbability(ActualEntry, FixedEntry); 2296dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines else if (ActualEntry <= UINT32_MAX) 2297dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines // Invert the fraction and divide. 2298dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines CSRCost /= BranchProbability(FixedEntry, ActualEntry); 2299dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines else 2300dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines // Can't use BranchProbability in general, since it takes 32-bit numbers. 2301dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines CSRCost = CSRCost.getFrequency() * (ActualEntry / FixedEntry); 2302dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines} 2303dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 2304ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// \brief Collect the hint info for \p Reg. 2305ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// The results are stored into \p Out. 2306ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// \p Out is not cleared before being populated. 2307ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesvoid RAGreedy::collectHintInfo(unsigned Reg, HintsInfo &Out) { 2308ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines for (const MachineInstr &Instr : MRI->reg_nodbg_instructions(Reg)) { 2309ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (!Instr.isFullCopy()) 2310ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines continue; 2311ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // Look for the other end of the copy. 2312ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines unsigned OtherReg = Instr.getOperand(0).getReg(); 2313ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (OtherReg == Reg) { 2314ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines OtherReg = Instr.getOperand(1).getReg(); 2315ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (OtherReg == Reg) 2316ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines continue; 2317ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 2318ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // Get the current assignment. 2319ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines unsigned OtherPhysReg = TargetRegisterInfo::isPhysicalRegister(OtherReg) 2320ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines ? OtherReg 2321ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines : VRM->getPhys(OtherReg); 2322ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // Push the collected information. 2323ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines Out.push_back(HintInfo(MBFI->getBlockFreq(Instr.getParent()), OtherReg, 2324ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines OtherPhysReg)); 2325ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 2326ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines} 2327ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 2328ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// \brief Using the given \p List, compute the cost of the broken hints if 2329ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// \p PhysReg was used. 2330ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// \return The cost of \p List for \p PhysReg. 2331ebe69fe11e48d322045d5949c83283927a0d790bStephen HinesBlockFrequency RAGreedy::getBrokenHintFreq(const HintsInfo &List, 2332ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines unsigned PhysReg) { 2333ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines BlockFrequency Cost = 0; 2334ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines for (const HintInfo &Info : List) { 2335ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (Info.PhysReg != PhysReg) 2336ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines Cost += Info.Freq; 2337ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 2338ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines return Cost; 2339ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines} 2340ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 2341ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// \brief Using the register assigned to \p VirtReg, try to recolor 2342ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// all the live ranges that are copy-related with \p VirtReg. 2343ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// The recoloring is then propagated to all the live-ranges that have 2344ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// been recolored and so on, until no more copies can be coalesced or 2345ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// it is not profitable. 2346ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// For a given live range, profitability is determined by the sum of the 2347ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// frequencies of the non-identity copies it would introduce with the old 2348ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// and new register. 2349ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesvoid RAGreedy::tryHintRecoloring(LiveInterval &VirtReg) { 2350ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // We have a broken hint, check if it is possible to fix it by 2351ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // reusing PhysReg for the copy-related live-ranges. Indeed, we evicted 2352ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // some register and PhysReg may be available for the other live-ranges. 2353ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines SmallSet<unsigned, 4> Visited; 2354ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines SmallVector<unsigned, 2> RecoloringCandidates; 2355ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines HintsInfo Info; 2356ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines unsigned Reg = VirtReg.reg; 2357ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines unsigned PhysReg = VRM->getPhys(Reg); 2358ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // Start the recoloring algorithm from the input live-interval, then 2359ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // it will propagate to the ones that are copy-related with it. 2360ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines Visited.insert(Reg); 2361ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines RecoloringCandidates.push_back(Reg); 2362ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 2363ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines DEBUG(dbgs() << "Trying to reconcile hints for: " << PrintReg(Reg, TRI) << '(' 2364ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines << PrintReg(PhysReg, TRI) << ")\n"); 2365ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 2366ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines do { 2367ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines Reg = RecoloringCandidates.pop_back_val(); 2368ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 2369ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // We cannot recolor physcal register. 2370ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (TargetRegisterInfo::isPhysicalRegister(Reg)) 2371ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines continue; 2372ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 2373ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines assert(VRM->hasPhys(Reg) && "We have unallocated variable!!"); 2374ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 2375ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // Get the live interval mapped with this virtual register to be able 2376ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // to check for the interference with the new color. 2377ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines LiveInterval &LI = LIS->getInterval(Reg); 2378ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines unsigned CurrPhys = VRM->getPhys(Reg); 2379ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // Check that the new color matches the register class constraints and 2380ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // that it is free for this live range. 2381ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (CurrPhys != PhysReg && (!MRI->getRegClass(Reg)->contains(PhysReg) || 2382ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines Matrix->checkInterference(LI, PhysReg))) 2383ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines continue; 2384ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 2385ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines DEBUG(dbgs() << PrintReg(Reg, TRI) << '(' << PrintReg(CurrPhys, TRI) 2386ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines << ") is recolorable.\n"); 2387ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 2388ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // Gather the hint info. 2389ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines Info.clear(); 2390ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines collectHintInfo(Reg, Info); 2391ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // Check if recoloring the live-range will increase the cost of the 2392ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // non-identity copies. 2393ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (CurrPhys != PhysReg) { 2394ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines DEBUG(dbgs() << "Checking profitability:\n"); 2395ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines BlockFrequency OldCopiesCost = getBrokenHintFreq(Info, CurrPhys); 2396ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines BlockFrequency NewCopiesCost = getBrokenHintFreq(Info, PhysReg); 2397ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines DEBUG(dbgs() << "Old Cost: " << OldCopiesCost.getFrequency() 2398ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines << "\nNew Cost: " << NewCopiesCost.getFrequency() << '\n'); 2399ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (OldCopiesCost < NewCopiesCost) { 2400ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines DEBUG(dbgs() << "=> Not profitable.\n"); 2401ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines continue; 2402ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 2403ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // At this point, the cost is either cheaper or equal. If it is 2404ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // equal, we consider this is profitable because it may expose 2405ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // more recoloring opportunities. 2406ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines DEBUG(dbgs() << "=> Profitable.\n"); 2407ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // Recolor the live-range. 2408ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines Matrix->unassign(LI); 2409ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines Matrix->assign(LI, PhysReg); 2410ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 2411ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // Push all copy-related live-ranges to keep reconciling the broken 2412ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // hints. 2413ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines for (const HintInfo &HI : Info) { 2414ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (Visited.insert(HI.Reg).second) 2415ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines RecoloringCandidates.push_back(HI.Reg); 2416ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 2417ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } while (!RecoloringCandidates.empty()); 2418ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines} 2419ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 2420ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// \brief Try to recolor broken hints. 2421ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// Broken hints may be repaired by recoloring when an evicted variable 2422ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// freed up a register for a larger live-range. 2423ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// Consider the following example: 2424ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// BB1: 2425ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// a = 2426ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// b = 2427ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// BB2: 2428ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// ... 2429ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// = b 2430ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// = a 2431ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// Let us assume b gets split: 2432ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// BB1: 2433ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// a = 2434ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// b = 2435ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// BB2: 2436ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// c = b 2437ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// ... 2438ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// d = c 2439ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// = d 2440ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// = a 2441ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// Because of how the allocation work, b, c, and d may be assigned different 2442ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// colors. Now, if a gets evicted later: 2443ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// BB1: 2444ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// a = 2445ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// st a, SpillSlot 2446ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// b = 2447ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// BB2: 2448ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// c = b 2449ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// ... 2450ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// d = c 2451ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// = d 2452ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// e = ld SpillSlot 2453ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// = e 2454ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// This is likely that we can assign the same register for b, c, and d, 2455ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// getting rid of 2 copies. 2456ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesvoid RAGreedy::tryHintsRecoloring() { 2457ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines for (LiveInterval *LI : SetOfBrokenHints) { 2458ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines assert(TargetRegisterInfo::isVirtualRegister(LI->reg) && 2459ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines "Recoloring is possible only for virtual registers"); 2460ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // Some dead defs may be around (e.g., because of debug uses). 2461ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // Ignore those. 2462ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (!VRM->hasPhys(LI->reg)) 2463ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines continue; 2464ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines tryHintRecoloring(*LI); 2465ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 2466ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines} 2467ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 246836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesunsigned RAGreedy::selectOrSplitImpl(LiveInterval &VirtReg, 246936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallVectorImpl<unsigned> &NewVRegs, 247036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines SmallVirtRegSet &FixedRegisters, 247136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned Depth) { 247236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned CostPerUseLimit = ~0u; 2473770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen // First try assigning a free register. 2474cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo, Matrix); 247536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs)) { 247636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // When NewVRegs is not empty, we may have made decisions such as evicting 247736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // a virtual register, go with the earlier decisions and use the physical 247836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // register. 2479cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (CSRCost.getFrequency() && isUnusedCalleeSavedReg(PhysReg) && 2480cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar NewVRegs.empty()) { 248136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned CSRReg = tryAssignCSRFirstTime(VirtReg, Order, PhysReg, 248236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines CostPerUseLimit, NewVRegs); 248336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (CSRReg || !NewVRegs.empty()) 248436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Return now if we decide to use a CSR or create new vregs due to 248536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // pre-splitting. 248636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return CSRReg; 248736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } else 248836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return PhysReg; 248936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines } 2490b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick 2491b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen LiveRangeStage Stage = getStage(VirtReg); 24921a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen DEBUG(dbgs() << StageName[Stage] 24931a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen << " Cascade " << ExtraRegInfo[VirtReg.reg].Cascade << '\n'); 2494b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen 249576395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen // Try to evict a less worthy live range, but only for ranges from the primary 2496fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen // queue. The RS_Split ranges already failed to do this, and they should not 249776395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen // get a second chance until they have been split. 2498fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen if (Stage != RS_Split) 2499ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (unsigned PhysReg = 2500ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines tryEvict(VirtReg, Order, NewVRegs, CostPerUseLimit)) { 2501ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines unsigned Hint = MRI->getSimpleHint(VirtReg.reg); 2502ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // If VirtReg has a hint and that hint is broken record this 2503ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // virtual register as a recoloring candidate for broken hint. 2504ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // Indeed, since we evicted a variable in its neighborhood it is 2505ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // likely we can at least partially recolor some of the 2506ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines // copy-related live-ranges. 2507ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (Hint && Hint != PhysReg) 2508ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines SetOfBrokenHints.insert(&VirtReg); 250976395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen return PhysReg; 2510ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 2511b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick 2512ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen assert(NewVRegs.empty() && "Cannot append to existing NewVRegs"); 2513ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen 2514107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen // The first time we see a live range, don't try to split or spill. 2515107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen // Wait until the second time, when all smaller ranges have been allocated. 2516107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen // This gives a better picture of the interference to split around. 2517fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen if (Stage < RS_Split) { 2518fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen setStage(VirtReg, RS_Split); 2519c1655e1a3c3a566b91b0513b56d61b58da1e36baJakob Stoklund Olesen DEBUG(dbgs() << "wait for second round\n"); 25201feb5854aeeda897e9318c8193d187673c8576b8Mark Lacey NewVRegs.push_back(VirtReg.reg); 2521107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen return 0; 2522107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen } 2523107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen 2524bf4e10f2f69db24c107cb61d6fe10ed5b2047374Jakob Stoklund Olesen // If we couldn't allocate a register from spilling, there is probably some 2525bf4e10f2f69db24c107cb61d6fe10ed5b2047374Jakob Stoklund Olesen // invalid inline assembly. The base class wil report it. 2526fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen if (Stage >= RS_Done || !VirtReg.isSpillable()) 252736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return tryLastChanceRecoloring(VirtReg, Order, NewVRegs, FixedRegisters, 252836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Depth); 252922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen 253046c83c80c5737743c955ff007fa6409804a7abf0Jakob Stoklund Olesen // Try splitting VirtReg or interferences. 2531ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen unsigned PhysReg = trySplit(VirtReg, Order, NewVRegs); 2532ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen if (PhysReg || !NewVRegs.empty()) 2533b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen return PhysReg; 2534b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen 2535770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen // Finally spill VirtReg itself. 2536cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (EnableDeferredSpilling && getStage(VirtReg) < RS_Memory) { 2537cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // TODO: This is experimental and in particular, we do not model 2538cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // the live range splitting done by spilling correctly. 2539cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // We would need a deep integration with the spiller to do the 2540cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // right thing here. Anyway, that is still good for early testing. 2541cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar setStage(VirtReg, RS_Memory); 2542cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar DEBUG(dbgs() << "Do as if this register is in memory\n"); 2543cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar NewVRegs.push_back(VirtReg.reg); 2544cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar } else { 2545cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled); 2546cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar LiveRangeEdit LRE(&VirtReg, NewVRegs, *MF, *LIS, VRM, this); 2547cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar spiller().spill(LRE); 2548cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar setStage(NewVRegs.begin(), NewVRegs.end(), RS_Done); 2549cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 2550cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (VerifyEnabled) 2551cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar MF->verify(this, "After spilling"); 2552cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar } 2553c46570dc05851395829bef904bb6ddb1260400d1Jakob Stoklund Olesen 2554cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen // The live virtual register requesting allocation was spilled, so tell 2555cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen // the caller not to allocate anything during this round. 2556cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen return 0; 2557cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} 2558cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 2559cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenbool RAGreedy::runOnMachineFunction(MachineFunction &mf) { 2560cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n" 2561986d76d7b3844b9a2f3d01a48975952749267a93David Blaikie << "********** Function: " << mf.getName() << '\n'); 2562cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen 2563cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen MF = &mf; 256437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines TRI = MF->getSubtarget().getRegisterInfo(); 256537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines TII = MF->getSubtarget().getInstrInfo(); 256636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines RCI.runOnMachineFunction(mf); 2567c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines 2568c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines EnableLocalReassign = EnableLocalReassignment || 256937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines MF->getSubtarget().enableRALocalReassignment( 257037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines MF->getTarget().getOptLevel()); 2571c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines 2572af24964251e27c2dd863239ba66ffd967b593be5Jakob Stoklund Olesen if (VerifyEnabled) 257389cab93fe999f6d81b4b99a71ac797b7ecfec277Jakob Stoklund Olesen MF->verify(this, "Before greedy register allocator"); 2574af24964251e27c2dd863239ba66ffd967b593be5Jakob Stoklund Olesen 2575d4348a2dc24c4fb012c1b9b20e71908f52049283Jakob Stoklund Olesen RegAllocBase::init(getAnalysis<VirtRegMap>(), 2576d4348a2dc24c4fb012c1b9b20e71908f52049283Jakob Stoklund Olesen getAnalysis<LiveIntervals>(), 2577d4348a2dc24c4fb012c1b9b20e71908f52049283Jakob Stoklund Olesen getAnalysis<LiveRegMatrix>()); 2578b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen Indexes = &getAnalysis<SlotIndexes>(); 25794eed756153b84c211114a3e9186bf0cb55d4b394Benjamin Kramer MBFI = &getAnalysis<MachineBlockFrequencyInfo>(); 2580f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen DomTree = &getAnalysis<MachineDominatorTree>(); 2581f6dff84d4e44d6c4a46c4f8a18e13c78f804547cJakob Stoklund Olesen SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM)); 2582d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen Loops = &getAnalysis<MachineLoopInfo>(); 2583b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen Bundles = &getAnalysis<EdgeBundles>(); 2584b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen SpillPlacer = &getAnalysis<SpillPlacement>(); 2585f42b66169d75301346e3685fd2b3e45e47806367Jakob Stoklund Olesen DebugVars = &getAnalysis<LiveDebugVariables>(); 2586b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen 2587dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines initializeCSRCost(); 2588dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 2589cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar calculateSpillWeightsAndHints(*LIS, mf, VRM, *Loops, *MBFI); 2590a77da0579bc141eba62760e21a216e5d3eafd792Arnaud A. de Grandmaison 25915dca61397878a7207986455b42fa2117fbf78000Andrew Trick DEBUG(LIS->dump()); 25925dca61397878a7207986455b42fa2117fbf78000Andrew Trick 25931b847deb26b52051de39f4cbecd224c9fbd0d1c2Jakob Stoklund Olesen SA.reset(new SplitAnalysis(*VRM, *LIS, *Loops)); 25944eed756153b84c211114a3e9186bf0cb55d4b394Benjamin Kramer SE.reset(new SplitEditor(*SA, *LIS, *VRM, *DomTree, *MBFI)); 25951a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen ExtraRegInfo.clear(); 25961a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen ExtraRegInfo.resize(MRI->getNumVirtRegs()); 25971a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen NextCascade = 1; 2598042888db2bb195c86bf34afbb6907d70855d2830Jakob Stoklund Olesen IntfCache.init(MF, Matrix->getLiveUnions(), Indexes, LIS, TRI); 259900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen GlobalCand.resize(32); // This will grow as needed. 2600ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines SetOfBrokenHints.clear(); 2601d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen 2602cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen allocatePhysRegs(); 2603ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines tryHintsRecoloring(); 2604cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen releaseMemory(); 2605cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen return true; 2606cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} 2607