1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- RegAllocLinearScan.cpp - Linear Scan register allocator -----------===// 2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The LLVM Compiler Infrastructure 4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source 6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details. 7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===// 9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file implements a linear scan register allocator. 11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===// 13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define DEBUG_TYPE "regalloc" 1519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "LiveDebugVariables.h" 1619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "LiveRangeEdit.h" 17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "VirtRegMap.h" 18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "VirtRegRewriter.h" 1919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "RegisterClassInfo.h" 20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "Spiller.h" 2119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Analysis/AliasAnalysis.h" 22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Function.h" 23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/CalcSpillWeights.h" 24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/LiveIntervalAnalysis.h" 25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineFunctionPass.h" 26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineInstr.h" 27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineLoopInfo.h" 28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineRegisterInfo.h" 29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/Passes.h" 30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/RegAllocRegistry.h" 31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetRegisterInfo.h" 32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetMachine.h" 33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetOptions.h" 34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetInstrInfo.h" 35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/EquivalenceClasses.h" 36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/SmallSet.h" 37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/Statistic.h" 38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/STLExtras.h" 39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/Debug.h" 40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/ErrorHandling.h" 41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/raw_ostream.h" 42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <algorithm> 43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <queue> 44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <memory> 45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <cmath> 46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm; 48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumIters , "Number of iterations performed"); 50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumBacktracks, "Number of times we had to backtrack"); 51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumCoalesce, "Number of copies coalesced"); 52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumDowngrade, "Number of registers downgraded"); 53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic cl::opt<bool> 55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanNewHeuristic("new-spilling-heuristic", 56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman cl::desc("Use new spilling heuristic"), 57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman cl::init(false), cl::Hidden); 58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic cl::opt<bool> 60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanTrivCoalesceEnds("trivial-coalesce-ends", 61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman cl::desc("Attempt trivial coalescing of interval ends"), 62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman cl::init(false), cl::Hidden); 63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 6419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic cl::opt<bool> 6519bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAvoidWAWHazard("avoid-waw-hazard", 6619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman cl::desc("Avoid write-write hazards for some register classes"), 6719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman cl::init(false), cl::Hidden); 6819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic RegisterRegAlloc 70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanlinearscanRegAlloc("linearscan", "linear scan register allocator", 71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman createLinearScanRegisterAllocator); 72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace { 74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // When we allocate a register, add it to a fixed-size queue of 75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // registers to skip in subsequent allocations. This trades a small 76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // amount of register pressure and increased spills for flexibility in 77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // the post-pass scheduler. 78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // 79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Note that in a the number of registers used for reloading spills 80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // will be one greater than the value of this option. 81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // 82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // One big limitation of this is that it doesn't differentiate between 83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // different register classes. So on x86-64, if there is xmm register 84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // pressure, it can caused fewer GPRs to be held in the queue. 85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static cl::opt<unsigned> 86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NumRecentlyUsedRegs("linearscan-skip-count", 87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman cl::desc("Number of registers for linearscan to remember" 88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "to skip."), 89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman cl::init(0), 90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman cl::Hidden); 9119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman struct RALinScan : public MachineFunctionPass { 93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static char ID; 94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RALinScan() : MachineFunctionPass(ID) { 9519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry()); 9619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); 9719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry()); 9819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman initializeRegisterCoalescerPass( 9919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman *PassRegistry::getPassRegistry()); 10019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry()); 10119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman initializeLiveStacksPass(*PassRegistry::getPassRegistry()); 10219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry()); 10319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry()); 10419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman initializeVirtRegMapPass(*PassRegistry::getPassRegistry()); 10519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry()); 10619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Initialize the queue to record recently-used registers. 108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (NumRecentlyUsedRegs > 0) 109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RecentRegs.resize(NumRecentlyUsedRegs, 0); 110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RecentNext = RecentRegs.begin(); 11119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman avoidWAW_ = 0; 112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr; 115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef SmallVector<IntervalPtr, 32> IntervalPtrs; 116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman private: 117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// RelatedRegClasses - This structure is built the first time a function is 118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// compiled, and keeps track of which register classes have registers that 119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// belong to multiple classes or have aliases that are in other classes. 120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman EquivalenceClasses<const TargetRegisterClass*> RelatedRegClasses; 121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DenseMap<unsigned, const TargetRegisterClass*> OneClassForEachPhysReg; 122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // NextReloadMap - For each register in the map, it maps to the another 124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // register which is defined by a reload from the same stack slot and 125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // both reloads are in the same basic block. 126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DenseMap<unsigned, unsigned> NextReloadMap; 127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // DowngradedRegs - A set of registers which are being "downgraded", i.e. 129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // un-favored for allocation. 130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallSet<unsigned, 8> DowngradedRegs; 131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // DowngradeMap - A map from virtual registers to physical registers being 133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // downgraded for the virtual registers. 134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DenseMap<unsigned, unsigned> DowngradeMap; 135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineFunction* mf_; 137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineRegisterInfo* mri_; 138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetMachine* tm_; 139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterInfo* tri_; 140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetInstrInfo* tii_; 141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BitVector allocatableRegs_; 14219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman BitVector reservedRegs_; 143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveIntervals* li_; 144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineLoopInfo *loopInfo; 14519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman RegisterClassInfo RegClassInfo; 146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// handled_ - Intervals are added to the handled_ set in the order of their 148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// start value. This is uses for backtracking. 149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<LiveInterval*> handled_; 150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// fixed_ - Intervals that correspond to machine registers. 152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman IntervalPtrs fixed_; 154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// active_ - Intervals that are currently being processed, and which have a 156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// live range active for the current point. 157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman IntervalPtrs active_; 158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// inactive_ - Intervals that are currently being processed, but which have 160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// a hold at the current point. 161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman IntervalPtrs inactive_; 162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef std::priority_queue<LiveInterval*, 164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<LiveInterval*, 64>, 165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman greater_ptr<LiveInterval> > IntervalHeap; 166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman IntervalHeap unhandled_; 167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// regUse_ - Tracks register usage. 169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<unsigned, 32> regUse_; 170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<unsigned, 32> regUseBackUp_; 171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// vrm_ - Tracks register assignments. 173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VirtRegMap* vrm_; 174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::auto_ptr<VirtRegRewriter> rewriter_; 176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::auto_ptr<Spiller> spiller_; 178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // The queue of recently-used registers. 180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<unsigned, 4> RecentRegs; 181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<unsigned, 4>::iterator RecentNext; 182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 18319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Last write-after-write register written. 18419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned avoidWAW_; 18519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Record that we just picked this register. 187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void recordRecentlyUsed(unsigned reg) { 188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(reg != 0 && "Recently used register is NOREG!"); 189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!RecentRegs.empty()) { 190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman *RecentNext++ = reg; 191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (RecentNext == RecentRegs.end()) 192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RecentNext = RecentRegs.begin(); 193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman virtual const char* getPassName() const { 198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return "Linear Scan Register Allocator"; 199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman virtual void getAnalysisUsage(AnalysisUsage &AU) const { 202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AU.setPreservesCFG(); 20319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman AU.addRequired<AliasAnalysis>(); 20419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman AU.addPreserved<AliasAnalysis>(); 205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AU.addRequired<LiveIntervals>(); 206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AU.addPreserved<SlotIndexes>(); 207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (StrongPHIElim) 208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AU.addRequiredID(StrongPHIEliminationID); 209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Make sure PassManager knows which analyses to make available 210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // to coalescing and which analyses coalescing invalidates. 21119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman AU.addRequiredTransitiveID(RegisterCoalescerPassID); 212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AU.addRequired<CalculateSpillWeights>(); 21319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman AU.addRequiredID(LiveStacksID); 21419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman AU.addPreservedID(LiveStacksID); 215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AU.addRequired<MachineLoopInfo>(); 216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AU.addPreserved<MachineLoopInfo>(); 217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AU.addRequired<VirtRegMap>(); 218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AU.addPreserved<VirtRegMap>(); 21919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman AU.addRequired<LiveDebugVariables>(); 22019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman AU.addPreserved<LiveDebugVariables>(); 22119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman AU.addRequiredID(MachineDominatorsID); 222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman AU.addPreservedID(MachineDominatorsID); 223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineFunctionPass::getAnalysisUsage(AU); 224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// runOnMachineFunction - register allocate the whole function 227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool runOnMachineFunction(MachineFunction&); 228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Determine if we skip this register due to its being recently used. 230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isRecentlyUsed(unsigned reg) const { 23119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return reg == avoidWAW_ || 23219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman std::find(RecentRegs.begin(), RecentRegs.end(), reg) != RecentRegs.end(); 233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman private: 236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// linearScan - the linear scan algorithm 237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void linearScan(); 238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// initIntervalSets - initialize the interval sets. 240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void initIntervalSets(); 242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// processActiveIntervals - expire old intervals and move non-overlapping 244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// ones to the inactive list. 245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void processActiveIntervals(SlotIndex CurPoint); 246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// processInactiveIntervals - expire old intervals and move overlapping 248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// ones to the active list. 249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void processInactiveIntervals(SlotIndex CurPoint); 250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// hasNextReloadInterval - Return the next liveinterval that's being 252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// defined by a reload from the same SS as the specified one. 253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval *hasNextReloadInterval(LiveInterval *cur); 254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// DowngradeRegister - Downgrade a register for allocation. 256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void DowngradeRegister(LiveInterval *li, unsigned Reg); 257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// UpgradeRegister - Upgrade a register for allocation. 259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void UpgradeRegister(unsigned Reg); 260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// assignRegOrStackSlotAtInterval - assign a register if one 262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// is available, or spill. 263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void assignRegOrStackSlotAtInterval(LiveInterval* cur); 264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void updateSpillWeights(std::vector<float> &Weights, 266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned reg, float weight, 267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *RC); 268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// findIntervalsToSpill - Determine the intervals to spill for the 270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// specified interval. It's passed the physical registers whose spill 271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// weight is the lowest among all the registers whose live intervals 272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// conflict with the interval. 273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void findIntervalsToSpill(LiveInterval *cur, 274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<std::pair<unsigned,float> > &Candidates, 275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned NumCands, 276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<LiveInterval*, 8> &SpillIntervals); 277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// attemptTrivialCoalescing - If a simple interval is defined by a copy, 279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// try to allocate the definition to the same register as the source, 280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// if the register is not defined during the life time of the interval. 281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// This eliminates a copy, and is used to coalesce copies which were not 282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// coalesced away before allocation either due to dest and src being in 283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// different register classes or because the coalescer was overly 284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// conservative. 285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg); 286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// Register usage / availability tracking helpers. 289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void initRegUses() { 292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman regUse_.resize(tri_->getNumRegs(), 0); 293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman regUseBackUp_.resize(tri_->getNumRegs(), 0); 294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void finalizeRegUses() { 297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef NDEBUG 298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Verify all the registers are "freed". 299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool Error = false; 300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = tri_->getNumRegs(); i != e; ++i) { 301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (regUse_[i] != 0) { 302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman dbgs() << tri_->getName(i) << " is still in use!\n"; 303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Error = true; 304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Error) 307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman llvm_unreachable(0); 308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman regUse_.clear(); 310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman regUseBackUp_.clear(); 311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void addRegUse(unsigned physReg) { 314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(TargetRegisterInfo::isPhysicalRegister(physReg) && 315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "should be physical register!"); 316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++regUse_[physReg]; 317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (const unsigned* as = tri_->getAliasSet(physReg); *as; ++as) 318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++regUse_[*as]; 319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void delRegUse(unsigned physReg) { 322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(TargetRegisterInfo::isPhysicalRegister(physReg) && 323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "should be physical register!"); 324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(regUse_[physReg] != 0); 325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman --regUse_[physReg]; 326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (const unsigned* as = tri_->getAliasSet(physReg); *as; ++as) { 327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(regUse_[*as] != 0); 328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman --regUse_[*as]; 329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isRegAvail(unsigned physReg) const { 333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(TargetRegisterInfo::isPhysicalRegister(physReg) && 334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "should be physical register!"); 335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return regUse_[physReg] == 0; 336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void backUpRegUses() { 339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman regUseBackUp_ = regUse_; 340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void restoreRegUses() { 343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman regUse_ = regUseBackUp_; 344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// Register handling helpers. 348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// getFreePhysReg - return a free physical register for this virtual 351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// register interval if we have one, otherwise return 0. 352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned getFreePhysReg(LiveInterval* cur); 353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned getFreePhysReg(LiveInterval* cur, 354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *RC, 355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned MaxInactiveCount, 356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<unsigned, 256> &inactiveCounts, 357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool SkipDGRegs); 358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 35919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman /// getFirstNonReservedPhysReg - return the first non-reserved physical 36019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman /// register in the register class. 36119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned getFirstNonReservedPhysReg(const TargetRegisterClass *RC) { 36219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ArrayRef<unsigned> O = RegClassInfo.getOrder(RC); 36319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman assert(!O.empty() && "All registers reserved?!"); 36419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return O.front(); 36519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 36619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void ComputeRelatedRegClasses(); 368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman template <typename ItTy> 370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void printIntervals(const char* const str, ItTy i, ItTy e) const { 371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG({ 372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (str) 373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman dbgs() << str << " intervals:\n"; 374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (; i != e; ++i) { 37619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman dbgs() << '\t' << *i->first << " -> "; 377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned reg = i->first->reg; 379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TargetRegisterInfo::isVirtualRegister(reg)) 380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman reg = vrm_->getPhys(reg); 381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman dbgs() << tri_->getName(reg) << '\n'; 383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman }); 385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman }; 387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman char RALinScan::ID = 0; 388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 39019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanINITIALIZE_PASS_BEGIN(RALinScan, "linearscan-regalloc", 39119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman "Linear Scan Register Allocator", false, false) 39219bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanINITIALIZE_PASS_DEPENDENCY(LiveIntervals) 39319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanINITIALIZE_PASS_DEPENDENCY(StrongPHIElimination) 39419bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanINITIALIZE_PASS_DEPENDENCY(CalculateSpillWeights) 39519bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanINITIALIZE_PASS_DEPENDENCY(LiveStacks) 39619bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanINITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) 39719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanINITIALIZE_PASS_DEPENDENCY(VirtRegMap) 39819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanINITIALIZE_PASS_DEPENDENCY(RegisterCoalescer) 39919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanINITIALIZE_AG_DEPENDENCY(AliasAnalysis) 40019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanINITIALIZE_PASS_END(RALinScan, "linearscan-regalloc", 40119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman "Linear Scan Register Allocator", false, false) 402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid RALinScan::ComputeRelatedRegClasses() { 404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // First pass, add all reg classes to the union, and determine at least one 405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // reg class that each register is in. 406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool HasAliases = false; 407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (TargetRegisterInfo::regclass_iterator RCI = tri_->regclass_begin(), 408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman E = tri_->regclass_end(); RCI != E; ++RCI) { 409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RelatedRegClasses.insert(*RCI); 410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (TargetRegisterClass::iterator I = (*RCI)->begin(), E = (*RCI)->end(); 411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman I != E; ++I) { 412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman HasAliases = HasAliases || *tri_->getAliasSet(*I) != 0; 41319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *&PRC = OneClassForEachPhysReg[*I]; 415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (PRC) { 416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Already processed this register. Just make sure we know that 417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // multiple register classes share a register. 418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RelatedRegClasses.unionSets(PRC, *RCI); 419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else { 420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman PRC = *RCI; 421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 42419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Second pass, now that we know conservatively what register classes each reg 426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // belongs to, add info about aliases. We don't need to do this for targets 427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // without register aliases. 428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (HasAliases) 429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (DenseMap<unsigned, const TargetRegisterClass*>::iterator 430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman I = OneClassForEachPhysReg.begin(), E = OneClassForEachPhysReg.end(); 431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman I != E; ++I) 43219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman for (const unsigned *AS = tri_->getAliasSet(I->first); *AS; ++AS) { 43319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman const TargetRegisterClass *AliasClass = 43419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman OneClassForEachPhysReg.lookup(*AS); 43519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (AliasClass) 43619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman RelatedRegClasses.unionSets(I->second, AliasClass); 43719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// attemptTrivialCoalescing - If a simple interval is defined by a copy, try 441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// allocate the definition the same register as the source register if the 442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// register is not defined during live time of the interval. If the interval is 443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// killed by a copy, try to use the destination register. This eliminates a 444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// copy. This is used to coalesce copies which were not coalesced away before 445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// allocation either due to dest and src being in different register classes or 446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// because the coalescer was overly conservative. 447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned RALinScan::attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg) { 448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Preference = vrm_->getRegAllocPref(cur.reg); 449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if ((Preference && Preference == Reg) || !cur.containsOneValue()) 450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Reg; 451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We cannot handle complicated live ranges. Simple linear stuff only. 453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (cur.ranges.size() != 1) 454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Reg; 455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const LiveRange &range = cur.ranges.front(); 457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VNInfo *vni = range.valno; 45919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (vni->isUnused() || !vni->def.isValid()) 460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Reg; 461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned CandReg; 463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman { 464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *CopyMI; 46519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if ((CopyMI = li_->getInstructionFromIndex(vni->def)) && CopyMI->isCopy()) 466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Defined by a copy, try to extend SrcReg forward 467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CandReg = CopyMI->getOperand(1).getReg(); 468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman else if (TrivCoalesceEnds && 469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (CopyMI = li_->getInstructionFromIndex(range.end.getBaseIndex())) && 470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CopyMI->isCopy() && cur.reg == CopyMI->getOperand(1).getReg()) 471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Only used by a copy, try to extend DstReg backwards 472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CandReg = CopyMI->getOperand(0).getReg(); 473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman else 474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Reg; 47519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 47619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If the target of the copy is a sub-register then don't coalesce. 47719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if(CopyMI->getOperand(0).getSubReg()) 47819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return Reg; 479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TargetRegisterInfo::isVirtualRegister(CandReg)) { 482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!vrm_->isAssignedReg(CandReg)) 483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Reg; 484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CandReg = vrm_->getPhys(CandReg); 485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Reg == CandReg) 487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Reg; 488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *RC = mri_->getRegClass(cur.reg); 490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!RC->contains(CandReg)) 491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Reg; 492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (li_->conflictsWithPhysReg(cur, *vrm_, CandReg)) 494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Reg; 495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Try to coalesce. 497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "Coalescing: " << cur << " -> " << tri_->getName(CandReg) 498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman << '\n'); 499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman vrm_->clearVirt(cur.reg); 500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman vrm_->assignVirt2Phys(cur.reg, CandReg); 501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumCoalesce; 503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return CandReg; 504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool RALinScan::runOnMachineFunction(MachineFunction &fn) { 507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman mf_ = &fn; 508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman mri_ = &fn.getRegInfo(); 509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman tm_ = &fn.getTarget(); 510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman tri_ = tm_->getRegisterInfo(); 511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman tii_ = tm_->getInstrInfo(); 512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman allocatableRegs_ = tri_->getAllocatableSet(fn); 51319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman reservedRegs_ = tri_->getReservedRegs(fn); 514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman li_ = &getAnalysis<LiveIntervals>(); 515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman loopInfo = &getAnalysis<MachineLoopInfo>(); 51619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman RegClassInfo.runOnMachineFunction(fn); 517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We don't run the coalescer here because we have no reason to 519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // interact with it. If the coalescer requires interaction, it 520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // won't do anything. If it doesn't require interaction, we assume 521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // it was run as a separate pass. 522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this is the first function compiled, compute the related reg classes. 524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (RelatedRegClasses.empty()) 525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ComputeRelatedRegClasses(); 526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Also resize register usage trackers. 528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman initRegUses(); 529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman vrm_ = &getAnalysis<VirtRegMap>(); 531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!rewriter_.get()) rewriter_.reset(createVirtRegRewriter()); 53219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman spiller_.reset(createSpiller(*this, *mf_, *vrm_)); 53419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman initIntervalSets(); 536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman linearScan(); 538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Rewrite spill code and update the PhysRegsUsed set. 540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman rewriter_->runOnMachineFunction(*mf_, *vrm_, li_); 541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 54219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Write out new DBG_VALUE instructions. 54319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman getAnalysis<LiveDebugVariables>().emitDebugValues(vrm_); 54419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(unhandled_.empty() && "Unhandled live intervals remain!"); 546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman finalizeRegUses(); 548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman fixed_.clear(); 550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman active_.clear(); 551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman inactive_.clear(); 552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman handled_.clear(); 553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NextReloadMap.clear(); 554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DowngradedRegs.clear(); 555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DowngradeMap.clear(); 556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman spiller_.reset(0); 557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// initIntervalSets - initialize the interval sets. 562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// 563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid RALinScan::initIntervalSets() 564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman{ 565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(unhandled_.empty() && fixed_.empty() && 566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman active_.empty() && inactive_.empty() && 567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "interval sets should be empty on initialization"); 568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman handled_.reserve(li_->getNumIntervals()); 570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) { 572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TargetRegisterInfo::isPhysicalRegister(i->second->reg)) { 57319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (!i->second->empty() && allocatableRegs_.test(i->second->reg)) { 574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman mri_->setPhysRegUsed(i->second->reg); 575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman fixed_.push_back(std::make_pair(i->second, i->second->begin())); 576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else { 578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (i->second->empty()) { 579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assignRegOrStackSlotAtInterval(i->second); 580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman else 582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unhandled_.push(i->second); 583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid RALinScan::linearScan() { 588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // linear scan algorithm 589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG({ 590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman dbgs() << "********** LINEAR SCAN **********\n" 59119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman << "********** Function: " 592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman << mf_->getFunction()->getName() << '\n'; 593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman printIntervals("fixed", fixed_.begin(), fixed_.end()); 594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman }); 595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman while (!unhandled_.empty()) { 597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // pick the interval with the earliest start point 598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval* cur = unhandled_.top(); 599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unhandled_.pop(); 600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumIters; 601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "\n*** CURRENT ***: " << *cur << '\n'); 602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(!cur->empty() && "Empty interval in unhandled set."); 604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman processActiveIntervals(cur->beginIndex()); 606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman processInactiveIntervals(cur->beginIndex()); 607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(TargetRegisterInfo::isVirtualRegister(cur->reg) && 609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Can only allocate virtual registers!"); 610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Allocating a virtual register. try to find a free 612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // physical register or spill an interval (possibly this one) in order to 613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // assign it one. 614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assignRegOrStackSlotAtInterval(cur); 615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG({ 617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman printIntervals("active", active_.begin(), active_.end()); 618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman printIntervals("inactive", inactive_.begin(), inactive_.end()); 619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman }); 620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Expire any remaining active intervals 623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman while (!active_.empty()) { 624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman IntervalPtr &IP = active_.back(); 625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned reg = IP.first->reg; 626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "\tinterval " << *IP.first << " expired\n"); 627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(TargetRegisterInfo::isVirtualRegister(reg) && 628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Can only allocate virtual registers!"); 629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman reg = vrm_->getPhys(reg); 630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman delRegUse(reg); 631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman active_.pop_back(); 632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Expire any remaining inactive intervals 635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG({ 636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (IntervalPtrs::reverse_iterator 637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman i = inactive_.rbegin(); i != inactive_.rend(); ++i) 638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman dbgs() << "\tinterval " << *i->first << " expired\n"; 639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman }); 640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman inactive_.clear(); 641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Add live-ins to every BB except for entry. Also perform trivial coalescing. 643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineFunction::iterator EntryMBB = mf_->begin(); 644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<MachineBasicBlock*, 8> LiveInMBBs; 645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) { 646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval &cur = *i->second; 647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Reg = 0; 648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isPhys = TargetRegisterInfo::isPhysicalRegister(cur.reg); 649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (isPhys) 650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Reg = cur.reg; 651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman else if (vrm_->isAssignedReg(cur.reg)) 652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Reg = attemptTrivialCoalescing(cur, vrm_->getPhys(cur.reg)); 653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!Reg) 654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Ignore splited live intervals. 656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!isPhys && vrm_->getPreSplitReg(cur.reg)) 657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (LiveInterval::Ranges::const_iterator I = cur.begin(), E = cur.end(); 660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman I != E; ++I) { 661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const LiveRange &LR = *I; 662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (li_->findLiveInMBBs(LR.start, LR.end, LiveInMBBs)) { 663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = LiveInMBBs.size(); i != e; ++i) 664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (LiveInMBBs[i] != EntryMBB) { 665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(TargetRegisterInfo::isPhysicalRegister(Reg) && 666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Adding a virtual register to livein set?"); 667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInMBBs[i]->addLiveIn(Reg); 668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInMBBs.clear(); 670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << *vrm_); 675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Look for physical registers that end up not being allocated even though 677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // register allocator had to spill other registers in its register class. 678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!vrm_->FindUnusedRegisters(li_)) 679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return; 680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// processActiveIntervals - expire old intervals and move non-overlapping ones 683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// to the inactive list. 684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid RALinScan::processActiveIntervals(SlotIndex CurPoint) 685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman{ 686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "\tprocessing active intervals:\n"); 687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = active_.size(); i != e; ++i) { 689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval *Interval = active_[i].first; 690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval::iterator IntervalPos = active_[i].second; 691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned reg = Interval->reg; 692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman IntervalPos = Interval->advanceTo(IntervalPos, CurPoint); 694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (IntervalPos == Interval->end()) { // Remove expired intervals. 696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "\t\tinterval " << *Interval << " expired\n"); 697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(TargetRegisterInfo::isVirtualRegister(reg) && 698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Can only allocate virtual registers!"); 699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman reg = vrm_->getPhys(reg); 700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman delRegUse(reg); 701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Pop off the end of the list. 703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman active_[i] = active_.back(); 704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman active_.pop_back(); 705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman --i; --e; 706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else if (IntervalPos->start > CurPoint) { 708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Move inactive intervals to inactive list. 709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "\t\tinterval " << *Interval << " inactive\n"); 710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(TargetRegisterInfo::isVirtualRegister(reg) && 711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Can only allocate virtual registers!"); 712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman reg = vrm_->getPhys(reg); 713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman delRegUse(reg); 714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // add to inactive. 715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman inactive_.push_back(std::make_pair(Interval, IntervalPos)); 716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Pop off the end of the list. 718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman active_[i] = active_.back(); 719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman active_.pop_back(); 720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman --i; --e; 721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else { 722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Otherwise, just update the iterator position. 723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman active_[i].second = IntervalPos; 724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// processInactiveIntervals - expire old intervals and move overlapping 729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ones to the active list. 730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid RALinScan::processInactiveIntervals(SlotIndex CurPoint) 731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman{ 732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "\tprocessing inactive intervals:\n"); 733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = inactive_.size(); i != e; ++i) { 735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval *Interval = inactive_[i].first; 736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval::iterator IntervalPos = inactive_[i].second; 737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned reg = Interval->reg; 738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman IntervalPos = Interval->advanceTo(IntervalPos, CurPoint); 740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (IntervalPos == Interval->end()) { // remove expired intervals. 742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "\t\tinterval " << *Interval << " expired\n"); 743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Pop off the end of the list. 745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman inactive_[i] = inactive_.back(); 746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman inactive_.pop_back(); 747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman --i; --e; 748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else if (IntervalPos->start <= CurPoint) { 749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // move re-activated intervals in active list 750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "\t\tinterval " << *Interval << " active\n"); 751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(TargetRegisterInfo::isVirtualRegister(reg) && 752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Can only allocate virtual registers!"); 753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman reg = vrm_->getPhys(reg); 754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman addRegUse(reg); 755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // add to active 756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman active_.push_back(std::make_pair(Interval, IntervalPos)); 757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Pop off the end of the list. 759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman inactive_[i] = inactive_.back(); 760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman inactive_.pop_back(); 761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman --i; --e; 762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else { 763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Otherwise, just update the iterator position. 764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman inactive_[i].second = IntervalPos; 765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// updateSpillWeights - updates the spill weights of the specifed physical 770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// register and its weight. 771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid RALinScan::updateSpillWeights(std::vector<float> &Weights, 772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned reg, float weight, 773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *RC) { 774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallSet<unsigned, 4> Processed; 775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallSet<unsigned, 4> SuperAdded; 776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<unsigned, 4> Supers; 777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Weights[reg] += weight; 778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Processed.insert(reg); 779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (const unsigned* as = tri_->getAliasSet(reg); *as; ++as) { 780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Weights[*as] += weight; 781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Processed.insert(*as); 782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (tri_->isSubRegister(*as, reg) && 783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SuperAdded.insert(*as) && 784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RC->contains(*as)) { 785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Supers.push_back(*as); 786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the alias is a super-register, and the super-register is in the 790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // register class we are trying to allocate. Then add the weight to all 791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // sub-registers of the super-register even if they are not aliases. 792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // e.g. allocating for GR32, bh is not used, updating bl spill weight. 79319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // bl should get the same spill weight otherwise it will be chosen 794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // as a spill candidate since spilling bh doesn't make ebx available. 795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = Supers.size(); i != e; ++i) { 796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (const unsigned *sr = tri_->getSubRegisters(Supers[i]); *sr; ++sr) 797894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!Processed.count(*sr)) 798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Weights[*sr] += weight; 799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic 803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanRALinScan::IntervalPtrs::iterator 804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanFindIntervalInVector(RALinScan::IntervalPtrs &IP, LiveInterval *LI) { 805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (RALinScan::IntervalPtrs::iterator I = IP.begin(), E = IP.end(); 806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman I != E; ++I) 807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (I->first == LI) return I; 808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return IP.end(); 809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 81119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic void RevertVectorIteratorsTo(RALinScan::IntervalPtrs &V, 81219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman SlotIndex Point){ 813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = V.size(); i != e; ++i) { 814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RALinScan::IntervalPtr &IP = V[i]; 815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval::iterator I = std::upper_bound(IP.first->begin(), 816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman IP.second, Point); 817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (I != IP.first->begin()) --I; 818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman IP.second = I; 819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getConflictWeight - Return the number of conflicts between cur 823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// live interval and defs and uses of Reg weighted by loop depthes. 824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic 825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanfloat getConflictWeight(LiveInterval *cur, unsigned Reg, LiveIntervals *li_, 826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineRegisterInfo *mri_, 827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineLoopInfo *loopInfo) { 828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman float Conflicts = 0; 829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(Reg), 830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman E = mri_->reg_end(); I != E; ++I) { 831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *MI = &*I; 832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (cur->liveAt(li_->getInstructionIndex(MI))) { 833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned loopDepth = loopInfo->getLoopDepth(MI->getParent()); 834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Conflicts += std::pow(10.0f, (float)loopDepth); 835894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Conflicts; 838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 839894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// findIntervalsToSpill - Determine the intervals to spill for the 841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specified interval. It's passed the physical registers whose spill 842894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// weight is the lowest among all the registers whose live intervals 843894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// conflict with the interval. 844894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid RALinScan::findIntervalsToSpill(LiveInterval *cur, 845894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<std::pair<unsigned,float> > &Candidates, 846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned NumCands, 847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<LiveInterval*, 8> &SpillIntervals) { 848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We have figured out the *best* register to spill. But there are other 849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // registers that are pretty good as well (spill weight within 3%). Spill 850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // the one that has fewest defs and uses that conflict with cur. 851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman float Conflicts[3] = { 0.0f, 0.0f, 0.0f }; 852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<LiveInterval*, 8> SLIs[3]; 853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG({ 855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman dbgs() << "\tConsidering " << NumCands << " candidates: "; 856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0; i != NumCands; ++i) 857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman dbgs() << tri_->getName(Candidates[i].first) << " "; 858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman dbgs() << "\n"; 859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman }); 86019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Calculate the number of conflicts of each candidate. 862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) { 863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Reg = i->first->reg; 864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned PhysReg = vrm_->getPhys(Reg); 865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!cur->overlapsFrom(*i->first, i->second)) 866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned j = 0; j < NumCands; ++j) { 868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Candidate = Candidates[j].first; 869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (tri_->regsOverlap(PhysReg, Candidate)) { 870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (NumCands > 1) 871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Conflicts[j] += getConflictWeight(cur, Reg, li_, mri_, loopInfo); 872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SLIs[j].push_back(i->first); 873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){ 878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Reg = i->first->reg; 879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned PhysReg = vrm_->getPhys(Reg); 880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!cur->overlapsFrom(*i->first, i->second-1)) 881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned j = 0; j < NumCands; ++j) { 883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Candidate = Candidates[j].first; 884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (tri_->regsOverlap(PhysReg, Candidate)) { 885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (NumCands > 1) 886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Conflicts[j] += getConflictWeight(cur, Reg, li_, mri_, loopInfo); 887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SLIs[j].push_back(i->first); 888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 892894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Which is the best candidate? 893894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned BestCandidate = 0; 894894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman float MinConflicts = Conflicts[0]; 895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 1; i != NumCands; ++i) { 896894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Conflicts[i] < MinConflicts) { 897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman BestCandidate = i; 898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MinConflicts = Conflicts[i]; 899894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::copy(SLIs[BestCandidate].begin(), SLIs[BestCandidate].end(), 903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::back_inserter(SpillIntervals)); 904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace { 907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman struct WeightCompare { 908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman private: 909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const RALinScan &Allocator; 910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman WeightCompare(const RALinScan &Alloc) : Allocator(Alloc) {} 913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef std::pair<unsigned, float> RegWeightPair; 915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool operator()(const RegWeightPair &LHS, const RegWeightPair &RHS) const { 916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return LHS.second < RHS.second && !Allocator.isRecentlyUsed(LHS.first); 917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman }; 919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool weightsAreClose(float w1, float w2) { 922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!NewHeuristic) 923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return false; 924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman float diff = w1 - w2; 926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (diff <= 0.02f) // Within 0.02f 927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return true; 928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return (diff / w2) <= 0.05f; // Within 5%. 929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 931894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanLiveInterval *RALinScan::hasNextReloadInterval(LiveInterval *cur) { 932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DenseMap<unsigned, unsigned>::iterator I = NextReloadMap.find(cur->reg); 933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (I == NextReloadMap.end()) 934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return 0; 935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return &li_->getInterval(I->second); 936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid RALinScan::DowngradeRegister(LiveInterval *li, unsigned Reg) { 93919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman for (const unsigned *AS = tri_->getOverlaps(Reg); *AS; ++AS) { 94019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman bool isNew = DowngradedRegs.insert(*AS); 94119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman (void)isNew; // Silence compiler warning. 942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(isNew && "Multiple reloads holding the same register?"); 943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DowngradeMap.insert(std::make_pair(li->reg, *AS)); 944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumDowngrade; 946894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid RALinScan::UpgradeRegister(unsigned Reg) { 949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Reg) { 950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DowngradedRegs.erase(Reg); 951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (const unsigned *AS = tri_->getAliasSet(Reg); *AS; ++AS) 952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DowngradedRegs.erase(*AS); 953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 954894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 955894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 956894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace { 957894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman struct LISorter { 958894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool operator()(LiveInterval* A, LiveInterval* B) { 959894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return A->beginIndex() < B->beginIndex(); 960894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 961894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman }; 962894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 964894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// assignRegOrStackSlotAtInterval - assign a register if one is available, or 965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// spill. 966894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid RALinScan::assignRegOrStackSlotAtInterval(LiveInterval* cur) { 96719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman const TargetRegisterClass *RC = mri_->getRegClass(cur->reg); 96819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman DEBUG(dbgs() << "\tallocating current interval from " 96919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman << RC->getName() << ": "); 970894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 971894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This is an implicitly defined live interval, just assign any register. 972894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (cur->empty()) { 973894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned physReg = vrm_->getRegAllocPref(cur->reg); 974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!physReg) 97519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman physReg = getFirstNonReservedPhysReg(RC); 976894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << tri_->getName(physReg) << '\n'); 977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Note the register is not really in use. 978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman vrm_->assignVirt2Phys(cur->reg, physReg); 979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return; 980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman backUpRegUses(); 983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<std::pair<unsigned, float> > SpillWeightsToAdd; 985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SlotIndex StartPosition = cur->beginIndex(); 986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC); 987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If start of this live interval is defined by a move instruction and its 989894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // source is assigned a physical register that is compatible with the target 990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // register class, then we should try to assign it the same register. 991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This can happen when the move is from a larger register class to a smaller 992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // one, e.g. X86::mov32to32_. These move instructions are not coalescable. 993894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!vrm_->getRegAllocPref(cur->reg) && cur->hasAtLeastOneValue()) { 994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman VNInfo *vni = cur->begin()->valno; 99519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (!vni->isUnused() && vni->def.isValid()) { 996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def); 997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (CopyMI && CopyMI->isCopy()) { 998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned DstSubReg = CopyMI->getOperand(0).getSubReg(); 999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned SrcReg = CopyMI->getOperand(1).getReg(); 1000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned SrcSubReg = CopyMI->getOperand(1).getSubReg(); 1001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Reg = 0; 1002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (TargetRegisterInfo::isPhysicalRegister(SrcReg)) 1003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Reg = SrcReg; 1004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman else if (vrm_->isAssignedReg(SrcReg)) 1005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Reg = vrm_->getPhys(SrcReg); 1006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Reg) { 1007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SrcSubReg) 1008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Reg = tri_->getSubReg(Reg, SrcSubReg); 1009894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (DstSubReg) 1010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Reg = tri_->getMatchingSuperReg(Reg, DstSubReg, RC); 1011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Reg && allocatableRegs_[Reg] && RC->contains(Reg)) 1012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman mri_->setRegAllocationHint(cur->reg, 0, Reg); 1013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // For every interval in inactive we overlap with, mark the 1019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // register as not free and update spill weights. 1020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (IntervalPtrs::const_iterator i = inactive_.begin(), 1021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman e = inactive_.end(); i != e; ++i) { 1022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Reg = i->first->reg; 1023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(TargetRegisterInfo::isVirtualRegister(Reg) && 1024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Can only allocate virtual registers!"); 1025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *RegRC = mri_->getRegClass(Reg); 102619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If this is not in a related reg class to the register we're allocating, 1027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // don't check it. 1028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader && 1029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman cur->overlapsFrom(*i->first, i->second-1)) { 1030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Reg = vrm_->getPhys(Reg); 1031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman addRegUse(Reg); 1032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SpillWeightsToAdd.push_back(std::make_pair(Reg, i->first->weight)); 1033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 103519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Speculatively check to see if we can get a register right now. If not, 1037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // we know we won't be able to by adding more constraints. If so, we can 1038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // check to see if it is valid. Doing an exhaustive search of the fixed_ list 1039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // is very bad (it contains all callee clobbered registers for any functions 1040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // with a call), so we want to avoid doing that if possible. 1041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned physReg = getFreePhysReg(cur); 1042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned BestPhysReg = physReg; 1043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (physReg) { 1044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We got a register. However, if it's in the fixed_ list, we might 1045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // conflict with it. Check to see if we conflict with it or any of its 1046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // aliases. 1047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallSet<unsigned, 8> RegAliases; 1048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (const unsigned *AS = tri_->getAliasSet(physReg); *AS; ++AS) 1049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RegAliases.insert(*AS); 105019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool ConflictsWithFixed = false; 1052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = fixed_.size(); i != e; ++i) { 1053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman IntervalPtr &IP = fixed_[i]; 1054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (physReg == IP.first->reg || RegAliases.count(IP.first->reg)) { 1055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Okay, this reg is on the fixed list. Check to see if we actually 1056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // conflict. 1057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval *I = IP.first; 1058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (I->endIndex() > StartPosition) { 1059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition); 1060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman IP.second = II; 1061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (II != I->begin() && II->start > StartPosition) 1062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman --II; 1063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (cur->overlapsFrom(*I, II)) { 1064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ConflictsWithFixed = true; 1065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 1066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1068894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1069894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 107019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Okay, the register picked by our speculative getFreePhysReg call turned 1072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // out to be in use. Actually add all of the conflicting fixed registers to 1073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // regUse_ so we can do an accurate query. 1074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ConflictsWithFixed) { 1075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // For every interval in fixed we overlap with, mark the register as not 1076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // free and update spill weights. 1077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = fixed_.size(); i != e; ++i) { 1078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman IntervalPtr &IP = fixed_[i]; 1079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval *I = IP.first; 1080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg]; 108219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader && 1083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman I->endIndex() > StartPosition) { 1084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition); 1085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman IP.second = II; 1086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (II != I->begin() && II->start > StartPosition) 1087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman --II; 1088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (cur->overlapsFrom(*I, II)) { 1089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned reg = I->reg; 1090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman addRegUse(reg); 1091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight)); 1092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Using the newly updated regUse_ object, which includes conflicts in the 1097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // future, see if there are any registers available. 1098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman physReg = getFreePhysReg(cur); 1099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 110119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Restore the physical register tracker, removing information about the 1103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // future. 1104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman restoreRegUses(); 110519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If we find a free register, we are done: assign this virtual to 1107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // the free physical register and add this interval to the active 1108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // list. 1109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (physReg) { 1110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << tri_->getName(physReg) << '\n'); 111119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman assert(RC->contains(physReg) && "Invalid candidate"); 1112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman vrm_->assignVirt2Phys(cur->reg, physReg); 1113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman addRegUse(physReg); 1114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman active_.push_back(std::make_pair(cur, cur->begin())); 1115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman handled_.push_back(cur); 1116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 111719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Remember physReg for avoiding a write-after-write hazard in the next 111819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // instruction. 111919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (AvoidWAWHazard && 112019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman tri_->avoidWriteAfterWrite(mri_->getRegClass(cur->reg))) 112119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman avoidWAW_ = physReg; 112219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // "Upgrade" the physical register since it has been allocated. 1124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman UpgradeRegister(physReg); 1125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (LiveInterval *NextReloadLI = hasNextReloadInterval(cur)) { 1126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // "Downgrade" physReg to try to keep physReg from being allocated until 112719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // the next reload from the same SS is allocated. 1128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman mri_->setRegAllocationHint(NextReloadLI->reg, 0, physReg); 1129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DowngradeRegister(cur, physReg); 1130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return; 1132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "no free registers\n"); 1134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Compile the spill weights into an array that is better for scanning. 1136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<float> SpillWeights(tri_->getNumRegs(), 0.0f); 1137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (std::vector<std::pair<unsigned, float> >::iterator 1138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I) 1139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman updateSpillWeights(SpillWeights, I->first, I->second, RC); 114019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // for each interval in active, update spill weights. 1142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end(); 1143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman i != e; ++i) { 1144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned reg = i->first->reg; 1145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(TargetRegisterInfo::isVirtualRegister(reg) && 1146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Can only allocate virtual registers!"); 1147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman reg = vrm_->getPhys(reg); 1148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman updateSpillWeights(SpillWeights, reg, i->first->weight, RC); 1149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 115019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "\tassigning stack slot at interval "<< *cur << ":\n"); 1152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Find a register to spill. 1154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman float minWeight = HUGE_VALF; 1155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned minReg = 0; 1156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool Found = false; 1158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::vector<std::pair<unsigned,float> > RegsWeights; 115919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ArrayRef<unsigned> Order = RegClassInfo.getOrder(RC); 1160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!minReg || SpillWeights[minReg] == HUGE_VALF) 116119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman for (unsigned i = 0; i != Order.size(); ++i) { 116219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned reg = Order[i]; 1163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman float regWeight = SpillWeights[reg]; 116419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Skip recently allocated registers and reserved registers. 1165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (minWeight > regWeight && !isRecentlyUsed(reg)) 1166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Found = true; 1167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RegsWeights.push_back(std::make_pair(reg, regWeight)); 1168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 116919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If we didn't find a register that is spillable, try aliases? 1171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!Found) { 117219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman for (unsigned i = 0; i != Order.size(); ++i) { 117319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned reg = Order[i]; 1174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // No need to worry about if the alias register size < regsize of RC. 1175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We are going to spill all registers that alias it anyway. 1176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (const unsigned* as = tri_->getAliasSet(reg); *as; ++as) 1177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RegsWeights.push_back(std::make_pair(*as, SpillWeights[*as])); 1178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Sort all potential spill candidates by weight. 1182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::sort(RegsWeights.begin(), RegsWeights.end(), WeightCompare(*this)); 1183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman minReg = RegsWeights[0].first; 1184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman minWeight = RegsWeights[0].second; 1185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (minWeight == HUGE_VALF) { 1186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // All registers must have inf weight. Just grab one! 118719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman minReg = BestPhysReg ? BestPhysReg : getFirstNonReservedPhysReg(RC); 1188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (cur->weight == HUGE_VALF || 1189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman li_->getApproximateInstructionCount(*cur) == 0) { 1190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Spill a physical register around defs and uses. 1191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (li_->spillPhysRegAroundRegDefsUses(*cur, minReg, *vrm_)) { 1192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // spillPhysRegAroundRegDefsUses may have invalidated iterator stored 1193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // in fixed_. Reset them. 1194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = fixed_.size(); i != e; ++i) { 1195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman IntervalPtr &IP = fixed_[i]; 1196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval *I = IP.first; 1197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (I->reg == minReg || tri_->isSubRegister(minReg, I->reg)) 1198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman IP.second = I->advanceTo(I->begin(), StartPosition); 1199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DowngradedRegs.clear(); 1202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assignRegOrStackSlotAtInterval(cur); 1203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else { 1204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(false && "Ran out of registers during register allocation!"); 1205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman report_fatal_error("Ran out of registers during register allocation!"); 1206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return; 1208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Find up to 3 registers to consider as spill candidates. 1212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned LastCandidate = RegsWeights.size() >= 3 ? 3 : 1; 1213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman while (LastCandidate > 1) { 1214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (weightsAreClose(RegsWeights[LastCandidate-1].second, minWeight)) 1215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 1216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman --LastCandidate; 1217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG({ 1220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman dbgs() << "\t\tregister(s) with min weight(s): "; 1221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0; i != LastCandidate; ++i) 1223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman dbgs() << tri_->getName(RegsWeights[i].first) 1224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman << " (" << RegsWeights[i].second << ")\n"; 1225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman }); 1226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If the current has the minimum weight, we need to spill it and 1228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // add any added intervals back to unhandled, and restart 1229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // linearscan. 1230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (cur->weight != HUGE_VALF && cur->weight <= minWeight) { 1231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "\t\t\tspilling(c): " << *cur << '\n'); 123219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman SmallVector<LiveInterval*, 8> added; 123319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman LiveRangeEdit LRE(*cur, added); 123419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman spiller_->spill(LRE); 1235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::sort(added.begin(), added.end(), LISorter()); 1237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (added.empty()) 1238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return; // Early exit if all spills were folded. 1239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Merge added with unhandled. Note that we have already sorted 1241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // intervals returned by addIntervalsForSpills by their starting 1242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // point. 1243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This also update the NextReloadMap. That is, it adds mapping from a 1244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // register defined by a reload from SS to the next reload from SS in the 1245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // same basic block. 1246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock *LastReloadMBB = 0; 1247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval *LastReload = 0; 1248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int LastReloadSS = VirtRegMap::NO_STACK_SLOT; 1249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = added.size(); i != e; ++i) { 1250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval *ReloadLi = added[i]; 1251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ReloadLi->weight == HUGE_VALF && 1252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman li_->getApproximateInstructionCount(*ReloadLi) == 0) { 1253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SlotIndex ReloadIdx = ReloadLi->beginIndex(); 1254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock *ReloadMBB = li_->getMBBFromIndex(ReloadIdx); 1255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int ReloadSS = vrm_->getStackSlot(ReloadLi->reg); 1256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (LastReloadMBB == ReloadMBB && LastReloadSS == ReloadSS) { 1257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Last reload of same SS is in the same MBB. We want to try to 1258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // allocate both reloads the same register and make sure the reg 1259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // isn't clobbered in between if at all possible. 1260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(LastReload->beginIndex() < ReloadIdx); 1261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NextReloadMap.insert(std::make_pair(LastReload->reg, ReloadLi->reg)); 1262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LastReloadMBB = ReloadMBB; 1264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LastReload = ReloadLi; 1265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LastReloadSS = ReloadSS; 1266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unhandled_.push(ReloadLi); 1268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return; 1270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++NumBacktracks; 1273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Push the current interval back to unhandled since we are going 1275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // to re-run at least this iteration. Since we didn't modify it it 1276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // should go back right in the front of the list 1277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unhandled_.push(cur); 1278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(TargetRegisterInfo::isPhysicalRegister(minReg) && 1280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "did not choose a register to spill?"); 1281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We spill all intervals aliasing the register with 1283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // minimum weight, rollback to the interval with the earliest 1284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // start point and let the linear scan algorithm run again 1285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<LiveInterval*, 8> spillIs; 1286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Determine which intervals have to be spilled. 1288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman findIntervalsToSpill(cur, RegsWeights, LastCandidate, spillIs); 1289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Set of spilled vregs (used later to rollback properly) 1291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallSet<unsigned, 8> spilled; 1292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // The earliest start of a Spilled interval indicates up to where 1294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // in handled we need to roll back 129519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman assert(!spillIs.empty() && "No spill intervals?"); 1296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SlotIndex earliestStart = spillIs[0]->beginIndex(); 129719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Spill live intervals of virtual regs mapped to the physical register we 1299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // want to clear (and its aliases). We only spill those that overlap with the 1300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // current interval as the rest do not affect its allocation. we also keep 1301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // track of the earliest start of all spilled live intervals since this will 1302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // mark our rollback point. 130319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman SmallVector<LiveInterval*, 8> added; 1304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman while (!spillIs.empty()) { 1305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval *sli = spillIs.back(); 1306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman spillIs.pop_back(); 1307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "\t\t\tspilling(a): " << *sli << '\n'); 1308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (sli->beginIndex() < earliestStart) 1309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman earliestStart = sli->beginIndex(); 131019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman LiveRangeEdit LRE(*sli, added, 0, &spillIs); 131119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman spiller_->spill(LRE); 1312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman spilled.insert(sli->reg); 1313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 131519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Include any added intervals in earliestStart. 131619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman for (unsigned i = 0, e = added.size(); i != e; ++i) { 131719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman SlotIndex SI = added[i]->beginIndex(); 131819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (SI < earliestStart) 131919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman earliestStart = SI; 132019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 132119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "\t\trolling back to: " << earliestStart << '\n'); 1323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Scan handled in reverse order up to the earliest start of a 1325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // spilled live interval and undo each one, restoring the state of 1326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // unhandled. 1327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman while (!handled_.empty()) { 1328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval* i = handled_.back(); 1329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If this interval starts before t we are done. 1330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!i->empty() && i->beginIndex() < earliestStart) 1331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 1332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "\t\t\tundo changes for: " << *i << '\n'); 1333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman handled_.pop_back(); 1334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // When undoing a live interval allocation we must know if it is active or 1336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // inactive to properly update regUse_ and the VirtRegMap. 1337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman IntervalPtrs::iterator it; 1338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if ((it = FindIntervalInVector(active_, i)) != active_.end()) { 1339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman active_.erase(it); 1340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(!TargetRegisterInfo::isPhysicalRegister(i->reg)); 1341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!spilled.count(i->reg)) 1342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unhandled_.push(i); 1343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman delRegUse(vrm_->getPhys(i->reg)); 1344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman vrm_->clearVirt(i->reg); 1345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) { 1346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman inactive_.erase(it); 1347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(!TargetRegisterInfo::isPhysicalRegister(i->reg)); 1348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!spilled.count(i->reg)) 1349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unhandled_.push(i); 1350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman vrm_->clearVirt(i->reg); 1351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } else { 1352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(TargetRegisterInfo::isVirtualRegister(i->reg) && 1353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Can only allocate virtual registers!"); 1354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman vrm_->clearVirt(i->reg); 1355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unhandled_.push(i); 1356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DenseMap<unsigned, unsigned>::iterator ii = DowngradeMap.find(i->reg); 1359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ii == DowngradeMap.end()) 1360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // It interval has a preference, it must be defined by a copy. Clear the 1361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // preference now since the source interval allocation may have been 1362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // undone as well. 1363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman mri_->setRegAllocationHint(i->reg, 0, 0); 1364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman else { 1365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman UpgradeRegister(ii->second); 1366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Rewind the iterators in the active, inactive, and fixed lists back to the 1370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // point we reverted to. 1371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RevertVectorIteratorsTo(active_, earliestStart); 1372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RevertVectorIteratorsTo(inactive_, earliestStart); 1373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RevertVectorIteratorsTo(fixed_, earliestStart); 1374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Scan the rest and undo each interval that expired after t and 1376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // insert it in active (the next iteration of the algorithm will 1377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // put it in inactive if required) 1378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = handled_.size(); i != e; ++i) { 1379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval *HI = handled_[i]; 1380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!HI->expiredAt(earliestStart) && 1381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman HI->expiredAt(cur->beginIndex())) { 1382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "\t\t\tundo changes for: " << *HI << '\n'); 1383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman active_.push_back(std::make_pair(HI, HI->begin())); 1384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(!TargetRegisterInfo::isPhysicalRegister(HI->reg)); 1385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman addRegUse(vrm_->getPhys(HI->reg)); 1386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Merge added with unhandled. 1390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This also update the NextReloadMap. That is, it adds mapping from a 1391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // register defined by a reload from SS to the next reload from SS in the 1392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // same basic block. 1393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock *LastReloadMBB = 0; 1394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval *LastReload = 0; 1395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int LastReloadSS = VirtRegMap::NO_STACK_SLOT; 1396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::sort(added.begin(), added.end(), LISorter()); 1397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (unsigned i = 0, e = added.size(); i != e; ++i) { 1398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LiveInterval *ReloadLi = added[i]; 1399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (ReloadLi->weight == HUGE_VALF && 1400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman li_->getApproximateInstructionCount(*ReloadLi) == 0) { 1401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SlotIndex ReloadIdx = ReloadLi->beginIndex(); 1402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MachineBasicBlock *ReloadMBB = li_->getMBBFromIndex(ReloadIdx); 1403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int ReloadSS = vrm_->getStackSlot(ReloadLi->reg); 1404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (LastReloadMBB == ReloadMBB && LastReloadSS == ReloadSS) { 1405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Last reload of same SS is in the same MBB. We want to try to 1406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // allocate both reloads the same register and make sure the reg 1407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // isn't clobbered in between if at all possible. 1408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(LastReload->beginIndex() < ReloadIdx); 1409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman NextReloadMap.insert(std::make_pair(LastReload->reg, ReloadLi->reg)); 1410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LastReloadMBB = ReloadMBB; 1412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LastReload = ReloadLi; 1413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman LastReloadSS = ReloadSS; 1414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unhandled_.push(ReloadLi); 1416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned RALinScan::getFreePhysReg(LiveInterval* cur, 1420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *RC, 1421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned MaxInactiveCount, 1422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<unsigned, 256> &inactiveCounts, 1423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool SkipDGRegs) { 1424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned FreeReg = 0; 1425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned FreeRegInactiveCount = 0; 1426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::pair<unsigned, unsigned> Hint = mri_->getRegAllocationHint(cur->reg); 1428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Resolve second part of the hint (if possible) given the current allocation. 1429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned physReg = Hint.second; 143019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (TargetRegisterInfo::isVirtualRegister(physReg) && vrm_->hasPhys(physReg)) 1431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman physReg = vrm_->getPhys(physReg); 1432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 143319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ArrayRef<unsigned> Order; 143419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (Hint.first) 143519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Order = tri_->getRawAllocationOrder(RC, Hint.first, physReg, *mf_); 143619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman else 143719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman Order = RegClassInfo.getOrder(RC); 143819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 143919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman assert(!Order.empty() && "No allocatable register in this register class!"); 1440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Scan for the first available register. 144219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman for (unsigned i = 0; i != Order.size(); ++i) { 144319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned Reg = Order[i]; 1444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Ignore "downgraded" registers. 1445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SkipDGRegs && DowngradedRegs.count(Reg)) 1446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 144719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Skip reserved registers. 144819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (reservedRegs_.test(Reg)) 144919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman continue; 1450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Skip recently allocated registers. 145119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (isRegAvail(Reg) && (!SkipDGRegs || !isRecentlyUsed(Reg))) { 1452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman FreeReg = Reg; 1453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (FreeReg < inactiveCounts.size()) 1454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman FreeRegInactiveCount = inactiveCounts[FreeReg]; 1455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman else 1456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman FreeRegInactiveCount = 0; 1457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; 1458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If there are no free regs, or if this reg has the max inactive count, 1462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // return this register. 1463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) { 1464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Remember what register we picked so we can skip it next time. 1465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (FreeReg != 0) recordRecentlyUsed(FreeReg); 1466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return FreeReg; 1467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Continue scanning the registers, looking for the one with the highest 1470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // inactive count. Alkis found that this reduced register pressure very 1471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // slightly on X86 (in rev 1.94 of this file), though this should probably be 1472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // reevaluated now. 147319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman for (unsigned i = 0; i != Order.size(); ++i) { 147419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned Reg = Order[i]; 1475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Ignore "downgraded" registers. 1476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (SkipDGRegs && DowngradedRegs.count(Reg)) 1477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman continue; 147819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Skip reserved registers. 147919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (reservedRegs_.test(Reg)) 148019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman continue; 1481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (isRegAvail(Reg) && Reg < inactiveCounts.size() && 148219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman FreeRegInactiveCount < inactiveCounts[Reg] && 148319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman (!SkipDGRegs || !isRecentlyUsed(Reg))) { 1484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman FreeReg = Reg; 1485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman FreeRegInactiveCount = inactiveCounts[Reg]; 1486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (FreeRegInactiveCount == MaxInactiveCount) 1487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman break; // We found the one with the max inactive count. 1488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Remember what register we picked so we can skip it next time. 1492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman recordRecentlyUsed(FreeReg); 1493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return FreeReg; 1495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getFreePhysReg - return a free physical register for this virtual register 1498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// interval if we have one, otherwise return 0. 1499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned RALinScan::getFreePhysReg(LiveInterval *cur) { 1500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SmallVector<unsigned, 256> inactiveCounts; 1501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned MaxInactiveCount = 0; 150219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *RC = mri_->getRegClass(cur->reg); 1504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC); 150519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end(); 1507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman i != e; ++i) { 1508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned reg = i->first->reg; 1509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman assert(TargetRegisterInfo::isVirtualRegister(reg) && 1510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman "Can only allocate virtual registers!"); 1511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 151219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // If this is not in a related reg class to the register we're allocating, 1513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // don't check it. 1514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const TargetRegisterClass *RegRC = mri_->getRegClass(reg); 1515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader) { 1516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman reg = vrm_->getPhys(reg); 1517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (inactiveCounts.size() <= reg) 1518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman inactiveCounts.resize(reg+1); 1519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ++inactiveCounts[reg]; 1520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]); 1521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // If copy coalescer has assigned a "preferred" register, check if it's 1525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // available first. 1526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned Preference = vrm_->getRegAllocPref(cur->reg); 1527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (Preference) { 1528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DEBUG(dbgs() << "(preferred: " << tri_->getName(Preference) << ") "); 152919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (isRegAvail(Preference) && 1530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RC->contains(Preference)) 1531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return Preference; 1532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 153419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman unsigned FreeReg = getFreePhysReg(cur, RC, MaxInactiveCount, inactiveCounts, 153519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman true); 153619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (FreeReg) 153719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return FreeReg; 1538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return getFreePhysReg(cur, RC, MaxInactiveCount, inactiveCounts, false); 1539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanFunctionPass* llvm::createLinearScanRegisterAllocator() { 1542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return new RALinScan(); 1543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1544