RegAllocGreedy.cpp revision 32668ea7a290ee1cb6bfe8cd677cdd4e5df05b4d
1cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//===-- RegAllocGreedy.cpp - greedy register allocator --------------------===//
2cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//
3cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//                     The LLVM Compiler Infrastructure
4cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//
5cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen// This file is distributed under the University of Illinois Open Source
6cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen// License. See LICENSE.TXT for details.
7cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//
8cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//===----------------------------------------------------------------------===//
9cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//
10cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen// This file defines the RAGreedy function pass for register allocation in
11cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen// optimized builds.
12cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//
13cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//===----------------------------------------------------------------------===//
14cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
15cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#define DEBUG_TYPE "regalloc"
16dd479e9769eceee9fcc34e2173376024f3aa3c5fJakob Stoklund Olesen#include "AllocationOrder.h"
175907d863659eb972ebb2afe07bc863a4c616f0efJakob Stoklund Olesen#include "InterferenceCache.h"
18cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen#include "LiveDebugVariables.h"
19f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen#include "LiveRangeEdit.h"
20cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "RegAllocBase.h"
21cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "Spiller.h"
22b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen#include "SpillPlacement.h"
23d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen#include "SplitKit.h"
24cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "VirtRegMap.h"
25fdf16ca44f130afe80c57481d0c08130aa08cc09Rafael Espindola#include "RegisterCoalescer.h"
260db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen#include "llvm/ADT/Statistic.h"
27cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Analysis/AliasAnalysis.h"
28cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Function.h"
29cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/PassAnalysisSupport.h"
30cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/CalcSpillWeights.h"
31b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen#include "llvm/CodeGen/EdgeBundles.h"
32cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/LiveIntervalAnalysis.h"
33cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/LiveStackAnalysis.h"
34f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen#include "llvm/CodeGen/MachineDominators.h"
35cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/MachineFunctionPass.h"
36cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/MachineLoopInfo.h"
37cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/MachineRegisterInfo.h"
38cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/Passes.h"
39cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/CodeGen/RegAllocRegistry.h"
40cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Target/TargetOptions.h"
4100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen#include "llvm/Support/CommandLine.h"
42cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Support/Debug.h"
43cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Support/ErrorHandling.h"
44cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Support/raw_ostream.h"
45533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen#include "llvm/Support/Timer.h"
46cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
4798d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen#include <queue>
4898d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen
49cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenusing namespace llvm;
50cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
510db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund OlesenSTATISTIC(NumGlobalSplits, "Number of split global live ranges");
520db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund OlesenSTATISTIC(NumLocalSplits,  "Number of split local live ranges");
530db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund OlesenSTATISTIC(NumEvicted,      "Number of interferences evicted");
540db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen
5521384c4ea8e1a8097a1feef1813c1414af9dae2aJakob Stoklund Olesencl::opt<bool> CompactRegions("compact-regions");
5600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
57cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenstatic RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator",
58cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen                                       createGreedyRegisterAllocator);
59cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
60cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesennamespace {
6192a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesenclass RAGreedy : public MachineFunctionPass,
6292a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen                 public RegAllocBase,
6392a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen                 private LiveRangeEdit::Delegate {
6492a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen
65cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  // context
66cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  MachineFunction *MF;
67cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
68cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  // analyses
69b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  SlotIndexes *Indexes;
70cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  LiveStacks *LS;
71f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen  MachineDominatorTree *DomTree;
72d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen  MachineLoopInfo *Loops;
73b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  EdgeBundles *Bundles;
74b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  SpillPlacement *SpillPlacer;
75f42b66169d75301346e3685fd2b3e45e47806367Jakob Stoklund Olesen  LiveDebugVariables *DebugVars;
76f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen
77cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  // state
78cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  std::auto_ptr<Spiller> SpillerInstance;
7998d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen  std::priority_queue<std::pair<unsigned, unsigned> > Queue;
801a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  unsigned NextCascade;
8122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen
8222a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  // Live ranges pass through a number of stages as we try to allocate them.
8322a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  // Some of the stages may also create new live ranges:
8422a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  //
8522a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  // - Region splitting.
8622a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  // - Per-block splitting.
8722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  // - Local splitting.
8822a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  // - Spilling.
8922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  //
9022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  // Ranges produced by one of the stages skip the previous stages when they are
9122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  // dequeued. This improves performance because we can skip interference checks
9222a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  // that are unlikely to give any results. It also guarantees that the live
9322a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  // range splitting algorithm terminates, something that is otherwise hard to
9422a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  // ensure.
9522a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  enum LiveRangeStage {
96fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    /// Newly created live range that has never been queued.
97fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    RS_New,
98fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen
99fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    /// Only attempt assignment and eviction. Then requeue as RS_Split.
100fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    RS_Assign,
101fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen
102fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    /// Attempt live range splitting if assignment is impossible.
103fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    RS_Split,
104fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen
10549743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen    /// Attempt more aggressive live range splitting that is guaranteed to make
10649743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen    /// progress.  This is used for split products that may not be making
10749743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen    /// progress.
10849743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen    RS_Split2,
10949743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen
110fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    /// Live range will be spilled.  No more splitting will be attempted.
111fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    RS_Spill,
112fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen
113fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    /// There is nothing more we can do to this live range.  Abort compilation
114fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    /// if it can't be assigned.
115fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    RS_Done
11622a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  };
11722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen
118b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen  static const char *const StageName[];
119b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen
1201a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  // RegInfo - Keep additional information about each live range.
1211a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  struct RegInfo {
1221a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    LiveRangeStage Stage;
1231a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen
1241a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    // Cascade - Eviction loop prevention. See canEvictInterference().
1251a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    unsigned Cascade;
1261a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen
1271a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    RegInfo() : Stage(RS_New), Cascade(0) {}
1281a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  };
1291a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen
1301a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  IndexedMap<RegInfo, VirtReg2IndexFunctor> ExtraRegInfo;
13122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen
13222a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  LiveRangeStage getStage(const LiveInterval &VirtReg) const {
1331a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    return ExtraRegInfo[VirtReg.reg].Stage;
1341a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  }
1351a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen
1361a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  void setStage(const LiveInterval &VirtReg, LiveRangeStage Stage) {
1371a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    ExtraRegInfo.resize(MRI->getNumVirtRegs());
1381a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    ExtraRegInfo[VirtReg.reg].Stage = Stage;
13922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  }
14022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen
14122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  template<typename Iterator>
14222a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  void setStage(Iterator Begin, Iterator End, LiveRangeStage NewStage) {
1431a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    ExtraRegInfo.resize(MRI->getNumVirtRegs());
144f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen    for (;Begin != End; ++Begin) {
145f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen      unsigned Reg = (*Begin)->reg;
1461a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen      if (ExtraRegInfo[Reg].Stage == RS_New)
1471a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen        ExtraRegInfo[Reg].Stage = NewStage;
148f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen    }
14922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  }
150cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
15151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  /// Cost of evicting interference.
15251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  struct EvictionCost {
15351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    unsigned BrokenHints; ///< Total number of broken hints.
15451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    float MaxWeight;      ///< Maximum spill weight evicted.
15551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
15651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    EvictionCost(unsigned B = 0) : BrokenHints(B), MaxWeight(0) {}
15751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
15851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    bool operator<(const EvictionCost &O) const {
15951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      if (BrokenHints != O.BrokenHints)
16051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        return BrokenHints < O.BrokenHints;
16151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      return MaxWeight < O.MaxWeight;
16251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    }
16351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  };
16451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
165b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  // splitting state.
16622a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  std::auto_ptr<SplitAnalysis> SA;
167bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen  std::auto_ptr<SplitEditor> SE;
168b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
169eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen  /// Cached per-block interference maps
170eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen  InterferenceCache IntfCache;
171eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen
1727b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen  /// All basic blocks where the current register has uses.
17396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  SmallVector<SpillPlacement::BlockConstraint, 8> SplitConstraints;
174b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
17596dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  /// Global live range splitting candidate info.
17696dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  struct GlobalSplitCandidate {
17700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    // Register intended for assignment, or 0.
17896dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen    unsigned PhysReg;
17900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
18000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    // SplitKit interval index for this candidate.
18100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    unsigned IntvIdx;
18200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
18300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    // Interference for PhysReg.
184c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    InterferenceCache::Cursor Intf;
18500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
18600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    // Bundles where this candidate should be live.
18796dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen    BitVector LiveBundles;
1885db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen    SmallVector<unsigned, 8> ActiveBlocks;
1895db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen
190c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    void reset(InterferenceCache &Cache, unsigned Reg) {
1915db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen      PhysReg = Reg;
19200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      IntvIdx = 0;
193c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen      Intf.setPhysReg(Cache, Reg);
1945db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen      LiveBundles.clear();
1955db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen      ActiveBlocks.clear();
1965db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen    }
19700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
19800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    // Set B[i] = C for every live bundle where B[i] was NoCand.
19900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    unsigned getBundles(SmallVectorImpl<unsigned> &B, unsigned C) {
20000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      unsigned Count = 0;
20100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      for (int i = LiveBundles.find_first(); i >= 0;
20200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen           i = LiveBundles.find_next(i))
20300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        if (B[i] == NoCand) {
20400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          B[i] = C;
20500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          Count++;
20600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        }
20700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      return Count;
20800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    }
20996dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  };
21096dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen
21196dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  /// Candidate info for for each PhysReg in AllocationOrder.
21296dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  /// This vector never shrinks, but grows to the size of the largest register
21396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  /// class.
21496dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  SmallVector<GlobalSplitCandidate, 32> GlobalCand;
21596dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen
21600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  enum { NoCand = ~0u };
21700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
21800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  /// Candidate map. Each edge bundle is assigned to a GlobalCand entry, or to
21900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  /// NoCand which indicates the stack interval.
22000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  SmallVector<unsigned, 32> BundleCand;
22100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
222cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenpublic:
223cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  RAGreedy();
224cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
225cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  /// Return the pass name.
226cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  virtual const char* getPassName() const {
227533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen    return "Greedy Register Allocator";
228cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  }
229cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
230cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  /// RAGreedy analysis usage.
231cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
232cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  virtual void releaseMemory();
233cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  virtual Spiller &spiller() { return *SpillerInstance; }
23498d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen  virtual void enqueue(LiveInterval *LI);
23598d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen  virtual LiveInterval *dequeue();
236ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  virtual unsigned selectOrSplit(LiveInterval&,
237ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen                                 SmallVectorImpl<LiveInterval*>&);
238cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
239cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  /// Perform register allocation.
240cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  virtual bool runOnMachineFunction(MachineFunction &mf);
241cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
242cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  static char ID;
243b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick
244b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trickprivate:
24592a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen  void LRE_WillEraseInstruction(MachineInstr*);
2467792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen  bool LRE_CanEraseVirtReg(unsigned);
2471d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen  void LRE_WillShrinkVirtReg(unsigned);
248f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen  void LRE_DidCloneVirtReg(unsigned, unsigned);
24992a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen
250200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen  float calcSpillCost();
251f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  bool addSplitConstraints(InterferenceCache::Cursor, float&);
252f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  void addThroughConstraints(InterferenceCache::Cursor, ArrayRef<unsigned>);
253c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen  void growRegion(GlobalSplitCandidate &Cand);
254c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen  float calcGlobalSplitCost(GlobalSplitCandidate&);
25587972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  bool calcCompactRegion(GlobalSplitCandidate&);
25600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  void splitAroundRegion(LiveRangeEdit&, ArrayRef<unsigned>);
257034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  void calcGapWeights(unsigned, SmallVectorImpl<float>&);
25851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  bool shouldEvict(LiveInterval &A, bool, LiveInterval &B, bool);
25951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  bool canEvictInterference(LiveInterval&, unsigned, bool, EvictionCost&);
26051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  void evictInterference(LiveInterval&, unsigned,
26151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen                         SmallVectorImpl<LiveInterval*>&);
262b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen
2636bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  unsigned tryAssign(LiveInterval&, AllocationOrder&,
2646bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen                     SmallVectorImpl<LiveInterval*>&);
26598c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen  unsigned tryEvict(LiveInterval&, AllocationOrder&,
2666bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen                    SmallVectorImpl<LiveInterval*>&, unsigned = ~0u);
267b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  unsigned tryRegionSplit(LiveInterval&, AllocationOrder&,
268b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen                          SmallVectorImpl<LiveInterval*>&);
269034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  unsigned tryLocalSplit(LiveInterval&, AllocationOrder&,
270034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    SmallVectorImpl<LiveInterval*>&);
271b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen  unsigned trySplit(LiveInterval&, AllocationOrder&,
272b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen                    SmallVectorImpl<LiveInterval*>&);
273cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen};
274cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} // end anonymous namespace
275cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
276cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenchar RAGreedy::ID = 0;
277cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
278b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen#ifndef NDEBUG
279b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesenconst char *const RAGreedy::StageName[] = {
280fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    "RS_New",
281fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    "RS_Assign",
282fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    "RS_Split",
28349743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen    "RS_Split2",
284fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    "RS_Spill",
285fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    "RS_Done"
286b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen};
287b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen#endif
288b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen
289200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen// Hysteresis to use when comparing floats.
290200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen// This helps stabilize decisions based on float comparisons.
291200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesenconst float Hysteresis = 0.98f;
292200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen
293200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen
294cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund OlesenFunctionPass* llvm::createGreedyRegisterAllocator() {
295cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  return new RAGreedy();
296cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen}
297cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
2981a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund OlesenRAGreedy::RAGreedy(): MachineFunctionPass(ID) {
299cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen  initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
300b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
301cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
302cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
303cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry());
3045b220213bfe9c37c2bb41a7ae0804e06a14f1007Rafael Espindola  initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry());
305cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
306cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  initializeLiveStacksPass(*PassRegistry::getPassRegistry());
307cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
308cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
309cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
310b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  initializeEdgeBundlesPass(*PassRegistry::getPassRegistry());
311b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  initializeSpillPlacementPass(*PassRegistry::getPassRegistry());
312cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen}
313cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
314cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenvoid RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
315cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  AU.setPreservesCFG();
316cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  AU.addRequired<AliasAnalysis>();
317cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  AU.addPreserved<AliasAnalysis>();
318cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  AU.addRequired<LiveIntervals>();
319b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  AU.addRequired<SlotIndexes>();
320cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  AU.addPreserved<SlotIndexes>();
321cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen  AU.addRequired<LiveDebugVariables>();
322cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen  AU.addPreserved<LiveDebugVariables>();
323cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  if (StrongPHIElim)
324cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen    AU.addRequiredID(StrongPHIEliminationID);
325cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  AU.addRequiredTransitive<RegisterCoalescer>();
326cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  AU.addRequired<CalculateSpillWeights>();
327cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  AU.addRequired<LiveStacks>();
328cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  AU.addPreserved<LiveStacks>();
329f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen  AU.addRequired<MachineDominatorTree>();
330f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen  AU.addPreserved<MachineDominatorTree>();
331cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  AU.addRequired<MachineLoopInfo>();
332cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  AU.addPreserved<MachineLoopInfo>();
333cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  AU.addRequired<VirtRegMap>();
334cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  AU.addPreserved<VirtRegMap>();
335b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  AU.addRequired<EdgeBundles>();
336b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  AU.addRequired<SpillPlacement>();
337cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  MachineFunctionPass::getAnalysisUsage(AU);
338cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen}
339cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
34092a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen
34192a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen//===----------------------------------------------------------------------===//
34292a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen//                     LiveRangeEdit delegate methods
34392a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen//===----------------------------------------------------------------------===//
34492a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen
34592a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesenvoid RAGreedy::LRE_WillEraseInstruction(MachineInstr *MI) {
34692a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen  // LRE itself will remove from SlotIndexes and parent basic block.
34792a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen  VRM->RemoveMachineInstrFromMaps(MI);
34892a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen}
34992a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen
3507792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesenbool RAGreedy::LRE_CanEraseVirtReg(unsigned VirtReg) {
3517792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen  if (unsigned PhysReg = VRM->getPhys(VirtReg)) {
3527792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen    unassign(LIS->getInterval(VirtReg), PhysReg);
3537792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen    return true;
3547792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen  }
3557792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen  // Unassigned virtreg is probably in the priority queue.
3567792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen  // RegAllocBase will erase it after dequeueing.
3577792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen  return false;
3587792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen}
35992a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen
3601d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesenvoid RAGreedy::LRE_WillShrinkVirtReg(unsigned VirtReg) {
3611d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen  unsigned PhysReg = VRM->getPhys(VirtReg);
3621d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen  if (!PhysReg)
3631d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen    return;
3641d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen
3651d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen  // Register is assigned, put it back on the queue for reassignment.
3661d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen  LiveInterval &LI = LIS->getInterval(VirtReg);
3671d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen  unassign(LI, PhysReg);
3681d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen  enqueue(&LI);
3691d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen}
3701d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen
371f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesenvoid RAGreedy::LRE_DidCloneVirtReg(unsigned New, unsigned Old) {
372f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen  // LRE may clone a virtual register because dead code elimination causes it to
373165e231c4295deb5cabd124d08e231b551bcc0b2Jakob Stoklund Olesen  // be split into connected components. The new components are much smaller
374165e231c4295deb5cabd124d08e231b551bcc0b2Jakob Stoklund Olesen  // than the original, so they should get a new chance at being assigned.
375f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen  // same stage as the parent.
376165e231c4295deb5cabd124d08e231b551bcc0b2Jakob Stoklund Olesen  ExtraRegInfo[Old].Stage = RS_Assign;
3771a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  ExtraRegInfo.grow(New);
3781a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  ExtraRegInfo[New] = ExtraRegInfo[Old];
379f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen}
380f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen
381cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenvoid RAGreedy::releaseMemory() {
382cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  SpillerInstance.reset(0);
3831a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  ExtraRegInfo.clear();
3845db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen  GlobalCand.clear();
385cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  RegAllocBase::releaseMemory();
386cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen}
387cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
38898d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesenvoid RAGreedy::enqueue(LiveInterval *LI) {
38998d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen  // Prioritize live ranges by size, assigning larger ranges first.
39098d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen  // The queue holds (size, reg) pairs.
391107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen  const unsigned Size = LI->getSize();
392107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen  const unsigned Reg = LI->reg;
39398d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen  assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
39498d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen         "Can only enqueue virtual registers");
395107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen  unsigned Prio;
39690c1d7ddfc65654f7efe72d56cad65d1af9e6b2aJakob Stoklund Olesen
3971a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  ExtraRegInfo.grow(Reg);
3981a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  if (ExtraRegInfo[Reg].Stage == RS_New)
399fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    ExtraRegInfo[Reg].Stage = RS_Assign;
400f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen
401cc07e04262fe4bc35469fbadc53d2ec7bfd02fe2Jakob Stoklund Olesen  if (ExtraRegInfo[Reg].Stage == RS_Split) {
402eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen    // Unsplit ranges that couldn't be allocated immediately are deferred until
403eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen    // everything else has been allocated. Long ranges are allocated last so
404eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen    // they are split against realistic interference.
405cc07e04262fe4bc35469fbadc53d2ec7bfd02fe2Jakob Stoklund Olesen    if (CompactRegions)
406cc07e04262fe4bc35469fbadc53d2ec7bfd02fe2Jakob Stoklund Olesen      Prio = Size;
407cc07e04262fe4bc35469fbadc53d2ec7bfd02fe2Jakob Stoklund Olesen    else
408cc07e04262fe4bc35469fbadc53d2ec7bfd02fe2Jakob Stoklund Olesen      Prio = (1u << 31) - Size;
409cc07e04262fe4bc35469fbadc53d2ec7bfd02fe2Jakob Stoklund Olesen  } else {
410eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen    // Everything else is allocated in long->short order. Long ranges that don't
411eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen    // fit should be spilled ASAP so they don't create interference.
412107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen    Prio = (1u << 31) + Size;
413d2a50734234a80893ad71da90d9f32032c47e000Jakob Stoklund Olesen
414eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen    // Boost ranges that have a physical register hint.
415eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen    if (TargetRegisterInfo::isPhysicalRegister(VRM->getRegAllocPref(Reg)))
416eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen      Prio |= (1u << 30);
417eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen  }
418107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen
419107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen  Queue.push(std::make_pair(Prio, Reg));
42090c1d7ddfc65654f7efe72d56cad65d1af9e6b2aJakob Stoklund Olesen}
42190c1d7ddfc65654f7efe72d56cad65d1af9e6b2aJakob Stoklund Olesen
42298d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund OlesenLiveInterval *RAGreedy::dequeue() {
42398d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen  if (Queue.empty())
42498d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen    return 0;
42598d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen  LiveInterval *LI = &LIS->getInterval(Queue.top().second);
42698d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen  Queue.pop();
42798d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen  return LI;
42898d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen}
429770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen
4306bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen
4316bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen//===----------------------------------------------------------------------===//
4326bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen//                            Direct Assignment
4336bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen//===----------------------------------------------------------------------===//
4346bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen
4356bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen/// tryAssign - Try to assign VirtReg to an available register.
4366bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesenunsigned RAGreedy::tryAssign(LiveInterval &VirtReg,
4376bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen                             AllocationOrder &Order,
4386bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen                             SmallVectorImpl<LiveInterval*> &NewVRegs) {
4396bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  Order.rewind();
4406bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  unsigned PhysReg;
4416bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  while ((PhysReg = Order.next()))
4426bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen    if (!checkPhysRegInterference(VirtReg, PhysReg))
4436bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen      break;
4446bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  if (!PhysReg || Order.isHint(PhysReg))
4456bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen    return PhysReg;
4466bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen
44751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // PhysReg is available, but there may be a better choice.
44851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
44951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // If we missed a simple hint, try to cheaply evict interference from the
45051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // preferred register.
45151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  if (unsigned Hint = MRI->getSimpleHint(VirtReg.reg))
45251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    if (Order.isHint(Hint)) {
45351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      DEBUG(dbgs() << "missed hint " << PrintReg(Hint, TRI) << '\n');
45451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      EvictionCost MaxCost(1);
45551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      if (canEvictInterference(VirtReg, Hint, true, MaxCost)) {
45651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        evictInterference(VirtReg, Hint, NewVRegs);
45751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        return Hint;
45851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      }
45951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    }
46051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
46151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // Try to evict interference from a cheaper alternative.
4626bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  unsigned Cost = TRI->getCostPerUse(PhysReg);
4636bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen
4646bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  // Most registers have 0 additional cost.
4656bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  if (!Cost)
4666bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen    return PhysReg;
4676bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen
4686bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is available at cost " << Cost
4696bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen               << '\n');
4706bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  unsigned CheapReg = tryEvict(VirtReg, Order, NewVRegs, Cost);
4716bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  return CheapReg ? CheapReg : PhysReg;
4726bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen}
4736bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen
4746bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen
475770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen//===----------------------------------------------------------------------===//
47698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen//                         Interference eviction
47798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen//===----------------------------------------------------------------------===//
4782710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen
47951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// shouldEvict - determine if A should evict the assigned live range B. The
48051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// eviction policy defined by this function together with the allocation order
48151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// defined by enqueue() decides which registers ultimately end up being split
48251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// and spilled.
483b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen///
4841a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen/// Cascade numbers are used to prevent infinite loops if this function is a
4851a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen/// cyclic relation.
48651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen///
48751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// @param A          The live range to be assigned.
48851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// @param IsHint     True when A is about to be assigned to its preferred
48951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen///                   register.
49051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// @param B          The live range to be evicted.
49151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// @param BreaksHint True when B is already assigned to its preferred register.
49251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesenbool RAGreedy::shouldEvict(LiveInterval &A, bool IsHint,
49351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen                           LiveInterval &B, bool BreaksHint) {
49449743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  bool CanSplit = getStage(B) < RS_Spill;
49551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
49651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // Be fairly aggressive about following hints as long as the evictee can be
49751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // split.
49851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  if (CanSplit && IsHint && !BreaksHint)
49951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    return true;
50051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
5011a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  return A.weight > B.weight;
502b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen}
503b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen
50451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// canEvictInterference - Return true if all interferences between VirtReg and
50551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// PhysReg can be evicted.  When OnlyCheap is set, don't do anything
50651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen///
50751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// @param VirtReg Live range that is about to be assigned.
50851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// @param PhysReg Desired register for assignment.
50951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// @prarm IsHint  True when PhysReg is VirtReg's preferred register.
51051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// @param MaxCost Only look for cheaper candidates and update with new cost
51151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen///                when returning true.
51251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// @returns True when interference can be evicted cheaper than MaxCost.
51398c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesenbool RAGreedy::canEvictInterference(LiveInterval &VirtReg, unsigned PhysReg,
51451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen                                    bool IsHint, EvictionCost &MaxCost) {
5151a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  // Find VirtReg's cascade number. This will be unassigned if VirtReg was never
5161a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  // involved in an eviction before. If a cascade number was assigned, deny
5171a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  // evicting anything with the same or a newer cascade number. This prevents
5181a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  // infinite eviction loops.
5191a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  //
5201a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  // This works out so a register without a cascade number is allowed to evict
5211a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  // anything, and it can be evicted by anything.
5221a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  unsigned Cascade = ExtraRegInfo[VirtReg.reg].Cascade;
5231a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  if (!Cascade)
5241a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    Cascade = NextCascade;
5251a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen
52651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  EvictionCost Cost;
52798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen  for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) {
52898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen    LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI);
5293f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen    // If there is 10 or more interferences, chances are one is heavier.
53051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    if (Q.collectInterferingVRegs(10) >= 10)
53198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen      return false;
53298c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen
5333f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen    // Check if any interfering live range is heavier than MaxWeight.
5343f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen    for (unsigned i = Q.interferingVRegs().size(); i; --i) {
5353f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen      LiveInterval *Intf = Q.interferingVRegs()[i - 1];
53698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen      if (TargetRegisterInfo::isPhysicalRegister(Intf->reg))
53798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen        return false;
53851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      // Never evict spill products. They cannot split or spill.
539fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen      if (getStage(*Intf) == RS_Done)
5401a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen        return false;
54151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      // Once a live range becomes small enough, it is urgent that we find a
54251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      // register for it. This is indicated by an infinite spill weight. These
54351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      // urgent live ranges get to evict almost anything.
54451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      bool Urgent = !VirtReg.isSpillable() && Intf->isSpillable();
54551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      // Only evict older cascades or live ranges without a cascade.
54651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      unsigned IntfCascade = ExtraRegInfo[Intf->reg].Cascade;
54751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      if (Cascade <= IntfCascade) {
54851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        if (!Urgent)
54951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen          return false;
55051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        // We permit breaking cascades for urgent evictions. It should be the
55151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        // last resort, though, so make it really expensive.
55251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        Cost.BrokenHints += 10;
55351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      }
55451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      // Would this break a satisfied hint?
55551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      bool BreaksHint = VRM->hasPreferredPhys(Intf->reg);
55651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      // Update eviction cost.
55751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      Cost.BrokenHints += BreaksHint;
55851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      Cost.MaxWeight = std::max(Cost.MaxWeight, Intf->weight);
55951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      // Abort if this would be too expensive.
56051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      if (!(Cost < MaxCost))
561b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen        return false;
56251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      // Finally, apply the eviction policy for non-urgent evictions.
56351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      if (!Urgent && !shouldEvict(VirtReg, IsHint, *Intf, BreaksHint))
56476395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen        return false;
5652710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen    }
5662710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen  }
56751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  MaxCost = Cost;
56898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen  return true;
56998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen}
57098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen
57151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// evictInterference - Evict any interferring registers that prevent VirtReg
57251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// from being assigned to Physreg. This assumes that canEvictInterference
57351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen/// returned true.
57451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesenvoid RAGreedy::evictInterference(LiveInterval &VirtReg, unsigned PhysReg,
57551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen                                 SmallVectorImpl<LiveInterval*> &NewVRegs) {
57651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // Make sure that VirtReg has a cascade number, and assign that cascade
57751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // number to every evicted register. These live ranges than then only be
57851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // evicted by a newer cascade, preventing infinite loops.
57951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  unsigned Cascade = ExtraRegInfo[VirtReg.reg].Cascade;
58051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  if (!Cascade)
58151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    Cascade = ExtraRegInfo[VirtReg.reg].Cascade = NextCascade++;
58251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
58351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  DEBUG(dbgs() << "evicting " << PrintReg(PhysReg, TRI)
58451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen               << " interference: Cascade " << Cascade << '\n');
58551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) {
58651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI);
58751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    assert(Q.seenAllInterferences() && "Didn't check all interfererences.");
58851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    for (unsigned i = 0, e = Q.interferingVRegs().size(); i != e; ++i) {
58951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      LiveInterval *Intf = Q.interferingVRegs()[i];
59051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      unassign(*Intf, VRM->getPhys(Intf->reg));
59151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      assert((ExtraRegInfo[Intf->reg].Cascade < Cascade ||
59251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen              VirtReg.isSpillable() < Intf->isSpillable()) &&
59351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen             "Cannot decrease cascade number, illegal eviction");
59451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      ExtraRegInfo[Intf->reg].Cascade = Cascade;
59551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      ++NumEvicted;
59651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      NewVRegs.push_back(Intf);
59751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    }
59851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  }
59951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen}
60051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
60198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen/// tryEvict - Try to evict all interferences for a physreg.
60276395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen/// @param  VirtReg Currently unassigned virtual register.
60376395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen/// @param  Order   Physregs to try.
60476395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen/// @return         Physreg to assign VirtReg, or 0.
60598c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesenunsigned RAGreedy::tryEvict(LiveInterval &VirtReg,
60698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen                            AllocationOrder &Order,
6076bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen                            SmallVectorImpl<LiveInterval*> &NewVRegs,
6086bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen                            unsigned CostPerUseLimit) {
60998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen  NamedRegionTimer T("Evict", TimerGroupName, TimePassesIsEnabled);
61098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen
61151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // Keep track of the cheapest interference seen so far.
61251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  EvictionCost BestCost(~0u);
61398c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen  unsigned BestPhys = 0;
61498c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen
61551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // When we are just looking for a reduced cost per use, don't break any
61651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // hints, and only evict smaller spill weights.
61751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  if (CostPerUseLimit < ~0u) {
61851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    BestCost.BrokenHints = 0;
61951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    BestCost.MaxWeight = VirtReg.weight;
62051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  }
62151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
62298c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen  Order.rewind();
62398c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen  while (unsigned PhysReg = Order.next()) {
6246bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen    if (TRI->getCostPerUse(PhysReg) >= CostPerUseLimit)
6256bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen      continue;
62651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    // The first use of a callee-saved register in a function has cost 1.
62751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    // Don't start using a CSR when the CostPerUseLimit is low.
62851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    if (CostPerUseLimit == 1)
62951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen     if (unsigned CSR = RegClassInfo.getLastCalleeSavedAlias(PhysReg))
63051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen       if (!MRI->isPhysRegUsed(CSR)) {
63151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen         DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " would clobber CSR "
63251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen                      << PrintReg(CSR, TRI) << '\n');
63351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen         continue;
63451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen       }
63551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
63651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    if (!canEvictInterference(VirtReg, PhysReg, false, BestCost))
63798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen      continue;
63898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen
63998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen    // Best so far.
64098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen    BestPhys = PhysReg;
64151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
64257f1e2cee06f9b57995727d786aeb1031c5376bdJakob Stoklund Olesen    // Stop if the hint can be used.
64357f1e2cee06f9b57995727d786aeb1031c5376bdJakob Stoklund Olesen    if (Order.isHint(PhysReg))
64457f1e2cee06f9b57995727d786aeb1031c5376bdJakob Stoklund Olesen      break;
6452710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen  }
6462710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen
64798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen  if (!BestPhys)
64898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen    return 0;
64998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen
65051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  evictInterference(VirtReg, BestPhys, NewVRegs);
65198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen  return BestPhys;
652b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick}
653b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick
654770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen
655770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen//===----------------------------------------------------------------------===//
656b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen//                              Region Splitting
657b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen//===----------------------------------------------------------------------===//
658b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
6591b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen/// addSplitConstraints - Fill out the SplitConstraints vector based on the
6601b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen/// interference pattern in Physreg and its aliases. Add the constraints to
6611b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen/// SpillPlacement and return the static cost of this split in Cost, assuming
6621b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen/// that all preferences in SplitConstraints are met.
663f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen/// Return false if there are no bundles with positive bias.
664f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesenbool RAGreedy::addSplitConstraints(InterferenceCache::Cursor Intf,
665f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen                                   float &Cost) {
666db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
667eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen
668b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  // Reset interference dependent info.
669db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  SplitConstraints.resize(UseBlocks.size());
67096dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  float StaticCost = 0;
671db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  for (unsigned i = 0; i != UseBlocks.size(); ++i) {
672db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
67396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen    SpillPlacement::BlockConstraint &BC = SplitConstraints[i];
67496dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen
675f0ac26c51173a9a1d6e5b5794107dccc4c5b5792Jakob Stoklund Olesen    BC.Number = BI.MBB->getNumber();
676eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen    Intf.moveToBlock(BC.Number);
677db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    BC.Entry = BI.LiveIn ? SpillPlacement::PrefReg : SpillPlacement::DontCare;
678db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    BC.Exit = BI.LiveOut ? SpillPlacement::PrefReg : SpillPlacement::DontCare;
6795ebca793db6262386d7464d03cdaefeb5b640ad3Jakob Stoklund Olesen    BC.ChangesValue = BI.FirstDef;
680b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
681eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen    if (!Intf.hasInterference())
682eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen      continue;
683eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen
68496dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen    // Number of spill code instructions to insert.
68596dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen    unsigned Ins = 0;
68696dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen
68796dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen    // Interference for the live-in value.
688eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen    if (BI.LiveIn) {
6896c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen      if (Intf.first() <= Indexes->getMBBStartIdx(BC.Number))
690db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen        BC.Entry = SpillPlacement::MustSpill, ++Ins;
691fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen      else if (Intf.first() < BI.FirstInstr)
69296dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen        BC.Entry = SpillPlacement::PrefSpill, ++Ins;
693fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen      else if (Intf.first() < BI.LastInstr)
69496dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen        ++Ins;
695a50c539b7a9e74597da34bfaea5429a48481f18bJakob Stoklund Olesen    }
696b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
69796dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen    // Interference for the live-out value.
698eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen    if (BI.LiveOut) {
699612f7807c581eafb7c8105e1a55c8d839033bfb3Jakob Stoklund Olesen      if (Intf.last() >= SA->getLastSplitPoint(BC.Number))
700db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen        BC.Exit = SpillPlacement::MustSpill, ++Ins;
701fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen      else if (Intf.last() > BI.LastInstr)
70296dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen        BC.Exit = SpillPlacement::PrefSpill, ++Ins;
703fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen      else if (Intf.last() > BI.FirstInstr)
70496dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen        ++Ins;
705b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen    }
706b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
70796dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen    // Accumulate the total frequency of inserted spill code.
70896dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen    if (Ins)
70996dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen      StaticCost += Ins * SpillPlacer->getBlockFrequency(BC.Number);
710b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  }
711f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  Cost = StaticCost;
712db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen
7131b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen  // Add constraints for use-blocks. Note that these are the only constraints
7141b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen  // that may add a positive bias, it is downhill from here.
7151b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen  SpillPlacer->addConstraints(SplitConstraints);
716f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  return SpillPlacer->scanActiveBundles();
717f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen}
718db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen
719db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen
720f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen/// addThroughConstraints - Add constraints and links to SpillPlacer from the
721f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen/// live-through blocks in Blocks.
722f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesenvoid RAGreedy::addThroughConstraints(InterferenceCache::Cursor Intf,
723f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen                                     ArrayRef<unsigned> Blocks) {
7241b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen  const unsigned GroupSize = 8;
7251b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen  SpillPlacement::BlockConstraint BCS[GroupSize];
726f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  unsigned TBS[GroupSize];
727f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  unsigned B = 0, T = 0;
728db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen
729f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  for (unsigned i = 0; i != Blocks.size(); ++i) {
730f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    unsigned Number = Blocks[i];
7311b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen    Intf.moveToBlock(Number);
7321b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen
7337b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen    if (!Intf.hasInterference()) {
734f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen      assert(T < GroupSize && "Array overflow");
735f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen      TBS[T] = Number;
736f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen      if (++T == GroupSize) {
73739b5abf507b43da6b92f68b86406e0015ead18e9Frits van Bommel        SpillPlacer->addLinks(makeArrayRef(TBS, T));
738f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen        T = 0;
739f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen      }
7407b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen      continue;
7411b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen    }
7421b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen
743f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    assert(B < GroupSize && "Array overflow");
744f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    BCS[B].Number = Number;
745f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen
7467b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen    // Interference for the live-in value.
7477b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen    if (Intf.first() <= Indexes->getMBBStartIdx(Number))
7487b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen      BCS[B].Entry = SpillPlacement::MustSpill;
7497b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen    else
7507b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen      BCS[B].Entry = SpillPlacement::PrefSpill;
7517b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen
7527b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen    // Interference for the live-out value.
7537b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen    if (Intf.last() >= SA->getLastSplitPoint(Number))
7547b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen      BCS[B].Exit = SpillPlacement::MustSpill;
7557b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen    else
7567b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen      BCS[B].Exit = SpillPlacement::PrefSpill;
7577b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen
7581b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen    if (++B == GroupSize) {
7591b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen      ArrayRef<SpillPlacement::BlockConstraint> Array(BCS, B);
7601b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen      SpillPlacer->addConstraints(Array);
7611b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen      B = 0;
7621b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen    }
763db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  }
764db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen
7651b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen  ArrayRef<SpillPlacement::BlockConstraint> Array(BCS, B);
7661b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen  SpillPlacer->addConstraints(Array);
76739b5abf507b43da6b92f68b86406e0015ead18e9Frits van Bommel  SpillPlacer->addLinks(makeArrayRef(TBS, T));
768b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen}
769b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
770c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesenvoid RAGreedy::growRegion(GlobalSplitCandidate &Cand) {
7715db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen  // Keep track of through blocks that have not been added to SpillPlacer.
7725db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen  BitVector Todo = SA->getThroughBlocks();
7735db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen  SmallVectorImpl<unsigned> &ActiveBlocks = Cand.ActiveBlocks;
7745db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen  unsigned AddedTo = 0;
775f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen#ifndef NDEBUG
776f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  unsigned Visited = 0;
777f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen#endif
7785db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen
779f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  for (;;) {
780f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    ArrayRef<unsigned> NewBundles = SpillPlacer->getRecentPositive();
781f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    // Find new through blocks in the periphery of PrefRegBundles.
782f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    for (int i = 0, e = NewBundles.size(); i != e; ++i) {
783f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen      unsigned Bundle = NewBundles[i];
784f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen      // Look at all blocks connected to Bundle in the full graph.
785f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen      ArrayRef<unsigned> Blocks = Bundles->getBlocks(Bundle);
786f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen      for (ArrayRef<unsigned>::iterator I = Blocks.begin(), E = Blocks.end();
787f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen           I != E; ++I) {
788f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen        unsigned Block = *I;
7895db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen        if (!Todo.test(Block))
790f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen          continue;
7915db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen        Todo.reset(Block);
792f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen        // This is a new through block. Add it to SpillPlacer later.
7935db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen        ActiveBlocks.push_back(Block);
794f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen#ifndef NDEBUG
795f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen        ++Visited;
796f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen#endif
797f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen      }
798f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    }
799f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    // Any new blocks to add?
800549019792a8b14500cab093ac8f3c5f7331e86d7Jakob Stoklund Olesen    if (ActiveBlocks.size() == AddedTo)
801549019792a8b14500cab093ac8f3c5f7331e86d7Jakob Stoklund Olesen      break;
802b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen
803b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen    // Compute through constraints from the interference, or assume that all
804b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen    // through blocks prefer spilling when forming compact regions.
805b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen    ArrayRef<unsigned> NewBlocks = makeArrayRef(ActiveBlocks).slice(AddedTo);
806b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen    if (Cand.PhysReg)
807b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen      addThroughConstraints(Cand.Intf, NewBlocks);
808b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen    else
809b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen      SpillPlacer->addPrefSpill(NewBlocks);
810549019792a8b14500cab093ac8f3c5f7331e86d7Jakob Stoklund Olesen    AddedTo = ActiveBlocks.size();
811549019792a8b14500cab093ac8f3c5f7331e86d7Jakob Stoklund Olesen
812f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    // Perhaps iterating can enable more bundles?
813f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    SpillPlacer->iterate();
814f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  }
815f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  DEBUG(dbgs() << ", v=" << Visited);
816f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen}
81796dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen
81887972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen/// calcCompactRegion - Compute the set of edge bundles that should be live
81987972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen/// when splitting the current live range into compact regions.  Compact
82087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen/// regions can be computed without looking at interference.  They are the
82187972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen/// regions formed by removing all the live-through blocks from the live range.
82287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen///
82387972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen/// Returns false if the current live range is already compact, or if the
82487972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen/// compact regions would form single block regions anyway.
82587972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesenbool RAGreedy::calcCompactRegion(GlobalSplitCandidate &Cand) {
82687972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  // Without any through blocks, the live range is already compact.
82787972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  if (!SA->getNumThroughBlocks())
82887972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    return false;
82987972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen
83087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  // Compact regions don't correspond to any physreg.
83187972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  Cand.reset(IntfCache, 0);
83287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen
83387972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  DEBUG(dbgs() << "Compact region bundles");
83487972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen
83587972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  // Use the spill placer to determine the live bundles. GrowRegion pretends
83687972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  // that all the through blocks have interference when PhysReg is unset.
83787972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  SpillPlacer->prepare(Cand.LiveBundles);
83887972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen
83987972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  // The static split cost will be zero since Cand.Intf reports no interference.
84087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  float Cost;
84187972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  if (!addSplitConstraints(Cand.Intf, Cost)) {
84287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    DEBUG(dbgs() << ", none.\n");
84387972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    return false;
84487972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  }
84587972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen
84687972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  growRegion(Cand);
84787972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  SpillPlacer->finish();
84887972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen
84987972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  if (!Cand.LiveBundles.any()) {
85087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    DEBUG(dbgs() << ", none.\n");
85187972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    return false;
85287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  }
85387972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen
85487972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  DEBUG({
85587972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    for (int i = Cand.LiveBundles.find_first(); i>=0;
85687972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen         i = Cand.LiveBundles.find_next(i))
85787972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    dbgs() << " EB#" << i;
85887972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    dbgs() << ".\n";
85987972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  });
86087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  return true;
86187972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen}
86287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen
863200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen/// calcSpillCost - Compute how expensive it would be to split the live range in
864200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen/// SA around all use blocks instead of forming bundle regions.
865200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesenfloat RAGreedy::calcSpillCost() {
866200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen  float Cost = 0;
867200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen  ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
868200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen  for (unsigned i = 0; i != UseBlocks.size(); ++i) {
869200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
870200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    unsigned Number = BI.MBB->getNumber();
871200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    // We normally only need one spill instruction - a load or a store.
872200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    Cost += SpillPlacer->getBlockFrequency(Number);
873200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen
874200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    // Unless the value is redefined in the block.
8753f5beede1bb97ba4e06dc300e00b70e1013e7216Jakob Stoklund Olesen    if (BI.LiveIn && BI.LiveOut && BI.FirstDef)
8763f5beede1bb97ba4e06dc300e00b70e1013e7216Jakob Stoklund Olesen      Cost += SpillPlacer->getBlockFrequency(Number);
877200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen  }
878200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen  return Cost;
879200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen}
880200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen
881b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen/// calcGlobalSplitCost - Return the global split cost of following the split
882b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen/// pattern in LiveBundles. This cost should be added to the local cost of the
88396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen/// interference pattern in SplitConstraints.
884b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen///
885c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesenfloat RAGreedy::calcGlobalSplitCost(GlobalSplitCandidate &Cand) {
886b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  float GlobalCost = 0;
8875db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen  const BitVector &LiveBundles = Cand.LiveBundles;
888db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
889db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  for (unsigned i = 0; i != UseBlocks.size(); ++i) {
890db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
89196dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen    SpillPlacement::BlockConstraint &BC = SplitConstraints[i];
892874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen    bool RegIn  = LiveBundles[Bundles->getBundle(BC.Number, 0)];
893874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen    bool RegOut = LiveBundles[Bundles->getBundle(BC.Number, 1)];
894874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen    unsigned Ins = 0;
895874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen
896db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    if (BI.LiveIn)
897db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen      Ins += RegIn != (BC.Entry == SpillPlacement::PrefReg);
898db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    if (BI.LiveOut)
899db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen      Ins += RegOut != (BC.Exit == SpillPlacement::PrefReg);
900874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen    if (Ins)
901874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen      GlobalCost += Ins * SpillPlacer->getBlockFrequency(BC.Number);
902b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  }
903db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen
9045db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen  for (unsigned i = 0, e = Cand.ActiveBlocks.size(); i != e; ++i) {
9055db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen    unsigned Number = Cand.ActiveBlocks[i];
906db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    bool RegIn  = LiveBundles[Bundles->getBundle(Number, 0)];
907db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    bool RegOut = LiveBundles[Bundles->getBundle(Number, 1)];
9089a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen    if (!RegIn && !RegOut)
9099a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen      continue;
9109a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen    if (RegIn && RegOut) {
9119a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen      // We need double spill code if this block has interference.
912c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen      Cand.Intf.moveToBlock(Number);
913c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen      if (Cand.Intf.hasInterference())
9149a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen        GlobalCost += 2*SpillPlacer->getBlockFrequency(Number);
9159a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen      continue;
9169a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen    }
9179a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen    // live-in / stack-out or stack-in live-out.
9189a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen    GlobalCost += SpillPlacer->getBlockFrequency(Number);
919db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  }
920b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  return GlobalCost;
921b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen}
922b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
92300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// splitAroundRegion - Split the current live range around the regions
92400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// determined by BundleCand and GlobalCand.
925ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen///
92600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// Before calling this function, GlobalCand and BundleCand must be initialized
92700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// so each bundle is assigned to a valid candidate, or NoCand for the
92800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// stack-bound bundles.  The shared SA/SE SplitAnalysis and SplitEditor
92900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// objects must be initialized for the current live range, and intervals
93000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// created for the used candidates.
931ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen///
93200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// @param LREdit    The LiveRangeEdit object handling the current split.
93300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// @param UsedCands List of used GlobalCand entries. Every BundleCand value
93400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen///                  must appear in this list.
93500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesenvoid RAGreedy::splitAroundRegion(LiveRangeEdit &LREdit,
93600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen                                 ArrayRef<unsigned> UsedCands) {
93700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // These are the intervals created for new global ranges. We may create more
93800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // intervals for local ranges.
93900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  const unsigned NumGlobalIntvs = LREdit.size();
94000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  DEBUG(dbgs() << "splitAroundRegion with " << NumGlobalIntvs << " globals.\n");
94100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  assert(NumGlobalIntvs && "No global intervals configured");
942ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
94387360f73ae205854f100ba5fb7eef7b90ac3bc27Jakob Stoklund Olesen  // First handle all the blocks with uses.
944db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
945db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  for (unsigned i = 0; i != UseBlocks.size(); ++i) {
946db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
94700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    unsigned Number = BI.MBB->getNumber();
94800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    unsigned IntvIn = 0, IntvOut = 0;
94900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    SlotIndex IntfIn, IntfOut;
95000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (BI.LiveIn) {
95100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      unsigned CandIn = BundleCand[Bundles->getBundle(Number, 0)];
95200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      if (CandIn != NoCand) {
95300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        GlobalSplitCandidate &Cand = GlobalCand[CandIn];
95400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        IntvIn = Cand.IntvIdx;
95500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        Cand.Intf.moveToBlock(Number);
95600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        IntfIn = Cand.Intf.first();
95700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      }
95800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    }
95900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (BI.LiveOut) {
96000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      unsigned CandOut = BundleCand[Bundles->getBundle(Number, 1)];
96100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      if (CandOut != NoCand) {
96200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        GlobalSplitCandidate &Cand = GlobalCand[CandOut];
96300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        IntvOut = Cand.IntvIdx;
96400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        Cand.Intf.moveToBlock(Number);
96500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        IntfOut = Cand.Intf.last();
96600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      }
96700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    }
968ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
969fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen    // Create separate intervals for isolated blocks with multiple uses.
97000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (!IntvIn && !IntvOut) {
971fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen      DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " isolated.\n");
97200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      if (!BI.isOneInstr())
97387360f73ae205854f100ba5fb7eef7b90ac3bc27Jakob Stoklund Olesen        SE->splitSingleBlock(BI);
974fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen      continue;
975fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen    }
976fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen
97700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (IntvIn && IntvOut)
97800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut);
97900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    else if (IntvIn)
98000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      SE->splitRegInBlock(BI, IntvIn, IntfIn);
981b4ddedce599183362b0f0333922c2fe0e163a129Jakob Stoklund Olesen    else
98200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      SE->splitRegOutBlock(BI, IntvOut, IntfOut);
983ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  }
984ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
98500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // Handle live-through blocks. The relevant live-through blocks are stored in
98600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // the ActiveBlocks list with each candidate. We need to filter out
98700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // duplicates.
98800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  BitVector Todo = SA->getThroughBlocks();
98900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  for (unsigned c = 0; c != UsedCands.size(); ++c) {
99000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    ArrayRef<unsigned> Blocks = GlobalCand[UsedCands[c]].ActiveBlocks;
99100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    for (unsigned i = 0, e = Blocks.size(); i != e; ++i) {
99200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      unsigned Number = Blocks[i];
99300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      if (!Todo.test(Number))
99400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        continue;
99500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      Todo.reset(Number);
99600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
99700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      unsigned IntvIn = 0, IntvOut = 0;
99800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      SlotIndex IntfIn, IntfOut;
99900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
100000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      unsigned CandIn = BundleCand[Bundles->getBundle(Number, 0)];
100100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      if (CandIn != NoCand) {
100200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        GlobalSplitCandidate &Cand = GlobalCand[CandIn];
100300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        IntvIn = Cand.IntvIdx;
100400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        Cand.Intf.moveToBlock(Number);
100500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        IntfIn = Cand.Intf.first();
100600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      }
100700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
100800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      unsigned CandOut = BundleCand[Bundles->getBundle(Number, 1)];
100900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      if (CandOut != NoCand) {
101000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        GlobalSplitCandidate &Cand = GlobalCand[CandOut];
101100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        IntvOut = Cand.IntvIdx;
101200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        Cand.Intf.moveToBlock(Number);
101300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        IntfOut = Cand.Intf.last();
101400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      }
101500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      if (!IntvIn && !IntvOut)
101600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        continue;
101700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut);
101800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    }
1019db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  }
1020db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen
10210db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen  ++NumGlobalSplits;
1022ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
10235928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  SmallVector<unsigned, 8> IntvMap;
10245928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  SE->finish(&IntvMap);
102500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  DebugVars->splitRegister(SA->getParent().reg, LREdit.regs());
1026f42b66169d75301346e3685fd2b3e45e47806367Jakob Stoklund Olesen
10271a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  ExtraRegInfo.resize(MRI->getNumVirtRegs());
1028b2abfa0bf30edf292a27a06e091d03983e644c9bJakob Stoklund Olesen  unsigned OrigBlocks = SA->getNumLiveBlocks();
10295928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen
10305928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  // Sort out the new intervals created by splitting. We get four kinds:
10315928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  // - Remainder intervals should not be split again.
10325928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  // - Candidate intervals can be assigned to Cand.PhysReg.
10335928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  // - Block-local splits are candidates for local splitting.
10345928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  // - DCE leftovers should go back on the queue.
10355928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  for (unsigned i = 0, e = LREdit.size(); i != e; ++i) {
10361a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    LiveInterval &Reg = *LREdit.get(i);
10375928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen
10385928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen    // Ignore old intervals from DCE.
10391a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    if (getStage(Reg) != RS_New)
10405928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen      continue;
10415928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen
10425928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen    // Remainder interval. Don't try splitting again, spill if it doesn't
10435928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen    // allocate.
10445928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen    if (IntvMap[i] == 0) {
1045fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen      setStage(Reg, RS_Spill);
10465928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen      continue;
10475928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen    }
10485928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen
104900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    // Global intervals. Allow repeated splitting as long as the number of live
105000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    // blocks is strictly decreasing.
105100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (IntvMap[i] < NumGlobalIntvs) {
10521a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen      if (SA->countLiveBlocks(&Reg) >= OrigBlocks) {
10539f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen        DEBUG(dbgs() << "Main interval covers the same " << OrigBlocks
10549f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen                     << " blocks as original.\n");
10559f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen        // Don't allow repeated splitting as a safe guard against looping.
105649743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen        setStage(Reg, RS_Split2);
10579f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen      }
10589f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen      continue;
10599f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen    }
10609f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen
10619f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen    // Other intervals are treated as new. This includes local intervals created
10629f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen    // for blocks with multiple uses, and anything created by DCE.
10635928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  }
10645928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen
1065eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen  if (VerifyEnabled)
1066ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen    MF->verify(this, "After splitting live range around region");
1067ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen}
1068ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1069b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesenunsigned RAGreedy::tryRegionSplit(LiveInterval &VirtReg, AllocationOrder &Order,
1070b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen                                  SmallVectorImpl<LiveInterval*> &NewVRegs) {
1071c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen  unsigned NumCands = 0;
107200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  unsigned BestCand = NoCand;
107300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  float BestCost;
107400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  SmallVector<unsigned, 8> UsedCands;
107500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
107600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // Check if we can split this live range around a compact region.
107700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  bool HasCompact = CompactRegions && calcCompactRegion(GlobalCand.front());
107800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  if (HasCompact) {
107900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    // Yes, keep GlobalCand[0] as the compact region candidate.
108000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    NumCands = 1;
108100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    BestCost = HUGE_VALF;
108200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  } else {
108300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    // No benefit from the compact region, our fallback will be per-block
108400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    // splitting. Make sure we find a solution that is cheaper than spilling.
108500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    BestCost = Hysteresis * calcSpillCost();
108600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    DEBUG(dbgs() << "Cost of isolating all blocks = " << BestCost << '\n');
108700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  }
108896dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen
1089b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  Order.rewind();
1090c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen  while (unsigned PhysReg = Order.next()) {
1091f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen    // Discard bad candidates before we run out of interference cache cursors.
1092f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen    // This will only affect register classes with a lot of registers (>32).
1093f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen    if (NumCands == IntfCache.getMaxCursors()) {
1094f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen      unsigned WorstCount = ~0u;
1095f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen      unsigned Worst = 0;
1096f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen      for (unsigned i = 0; i != NumCands; ++i) {
109700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        if (i == BestCand || !GlobalCand[i].PhysReg)
1098f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen          continue;
1099f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen        unsigned Count = GlobalCand[i].LiveBundles.count();
1100f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen        if (Count < WorstCount)
1101f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen          Worst = i, WorstCount = Count;
1102f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen      }
1103f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen      --NumCands;
1104f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen      GlobalCand[Worst] = GlobalCand[NumCands];
1105f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen    }
1106f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen
1107c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    if (GlobalCand.size() <= NumCands)
1108c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen      GlobalCand.resize(NumCands+1);
1109c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    GlobalSplitCandidate &Cand = GlobalCand[NumCands];
1110c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    Cand.reset(IntfCache, PhysReg);
111196dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen
1112c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    SpillPlacer->prepare(Cand.LiveBundles);
11131b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen    float Cost;
1114c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    if (!addSplitConstraints(Cand.Intf, Cost)) {
1115f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen      DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tno positive bundles\n");
11161b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen      continue;
11171b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen    }
1118f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tstatic = " << Cost);
1119200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    if (Cost >= BestCost) {
1120200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen      DEBUG({
1121200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen        if (BestCand == NoCand)
1122200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen          dbgs() << " worse than no bundles\n";
1123200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen        else
1124200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen          dbgs() << " worse than "
1125200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen                 << PrintReg(GlobalCand[BestCand].PhysReg, TRI) << '\n';
1126200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen      });
1127b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen      continue;
1128874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen    }
1129c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    growRegion(Cand);
1130ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
11319efa2a263ea470caacef1c85f6ca45e32bf516d3Jakob Stoklund Olesen    SpillPlacer->finish();
11329efa2a263ea470caacef1c85f6ca45e32bf516d3Jakob Stoklund Olesen
1133ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen    // No live bundles, defer to splitSingleBlocks().
1134c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    if (!Cand.LiveBundles.any()) {
1135874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen      DEBUG(dbgs() << " no bundles.\n");
1136ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen      continue;
1137874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen    }
1138ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1139c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    Cost += calcGlobalSplitCost(Cand);
1140874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen    DEBUG({
1141874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen      dbgs() << ", total = " << Cost << " with bundles";
1142c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen      for (int i = Cand.LiveBundles.find_first(); i>=0;
1143c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen           i = Cand.LiveBundles.find_next(i))
1144874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen        dbgs() << " EB#" << i;
1145874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen      dbgs() << ".\n";
1146874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen    });
1147200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    if (Cost < BestCost) {
1148c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen      BestCand = NumCands;
1149200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen      BestCost = Hysteresis * Cost; // Prevent rounding effects.
1150b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen    }
1151c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    ++NumCands;
1152b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  }
1153ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
115400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // No solutions found, fall back to single block splitting.
115500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  if (!HasCompact && BestCand == NoCand)
1156ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen    return 0;
1157ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
115800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // Prepare split editor.
115900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  LiveRangeEdit LREdit(VirtReg, NewVRegs, this);
116000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  SE->reset(LREdit);
116100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
116200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // Assign all edge bundles to the preferred candidate, or NoCand.
116300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  BundleCand.assign(Bundles->getNumBundles(), NoCand);
116400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
116500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // Assign bundles for the best candidate region.
116600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  if (BestCand != NoCand) {
116700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    GlobalSplitCandidate &Cand = GlobalCand[BestCand];
116800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (unsigned B = Cand.getBundles(BundleCand, BestCand)) {
116900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      UsedCands.push_back(BestCand);
117000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      Cand.IntvIdx = SE->openIntv();
117100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      DEBUG(dbgs() << "Split for " << PrintReg(Cand.PhysReg, TRI) << " in "
117200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen                   << B << " bundles, intv " << Cand.IntvIdx << ".\n");
117332668ea7a290ee1cb6bfe8cd677cdd4e5df05b4dChandler Carruth      (void)B;
117400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    }
117500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  }
117600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
117700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // Assign bundles for the compact region.
117800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  if (HasCompact) {
117900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    GlobalSplitCandidate &Cand = GlobalCand.front();
118000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    assert(!Cand.PhysReg && "Compact region has no physreg");
118100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (unsigned B = Cand.getBundles(BundleCand, 0)) {
118200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      UsedCands.push_back(0);
118300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      Cand.IntvIdx = SE->openIntv();
118400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      DEBUG(dbgs() << "Split for compact region in " << B << " bundles, intv "
118500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen                   << Cand.IntvIdx << ".\n");
118632668ea7a290ee1cb6bfe8cd677cdd4e5df05b4dChandler Carruth      (void)B;
118700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    }
118800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  }
118900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
119000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  splitAroundRegion(LREdit, UsedCands);
1191b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  return 0;
1192b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen}
1193b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
1194ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1195ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen//===----------------------------------------------------------------------===//
1196034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen//                             Local Splitting
1197034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen//===----------------------------------------------------------------------===//
1198034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1199034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1200034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// calcGapWeights - Compute the maximum spill weight that needs to be evicted
1201034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// in order to use PhysReg between two entries in SA->UseSlots.
1202034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen///
1203034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// GapWeight[i] represents the gap between UseSlots[i] and UseSlots[i+1].
1204034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen///
1205034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesenvoid RAGreedy::calcGapWeights(unsigned PhysReg,
1206034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen                              SmallVectorImpl<float> &GapWeight) {
1207db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  assert(SA->getUseBlocks().size() == 1 && "Not a local interval");
1208db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front();
1209034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
1210034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  const unsigned NumGaps = Uses.size()-1;
1211034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1212034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // Start and end points for the interference check.
1213fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen  SlotIndex StartIdx =
1214fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen    BI.LiveIn ? BI.FirstInstr.getBaseIndex() : BI.FirstInstr;
1215fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen  SlotIndex StopIdx =
1216fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen    BI.LiveOut ? BI.LastInstr.getBoundaryIndex() : BI.LastInstr;
1217034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1218034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  GapWeight.assign(NumGaps, 0.0f);
1219034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1220034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // Add interference from each overlapping register.
1221034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
1222034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    if (!query(const_cast<LiveInterval&>(SA->getParent()), *AI)
1223034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen           .checkInterference())
1224034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      continue;
1225034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1226fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen    // We know that VirtReg is a continuous interval from FirstInstr to
1227fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen    // LastInstr, so we don't need InterferenceQuery.
1228034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    //
1229034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // Interference that overlaps an instruction is counted in both gaps
1230034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // surrounding the instruction. The exception is interference before
1231034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // StartIdx and after StopIdx.
1232034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    //
1233034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    LiveIntervalUnion::SegmentIter IntI = PhysReg2LiveUnion[*AI].find(StartIdx);
1234034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    for (unsigned Gap = 0; IntI.valid() && IntI.start() < StopIdx; ++IntI) {
1235034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Skip the gaps before IntI.
1236034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      while (Uses[Gap+1].getBoundaryIndex() < IntI.start())
1237034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        if (++Gap == NumGaps)
1238034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          break;
1239034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      if (Gap == NumGaps)
1240034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        break;
1241034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1242034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Update the gaps covered by IntI.
1243034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      const float weight = IntI.value()->weight;
1244034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      for (; Gap != NumGaps; ++Gap) {
1245034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        GapWeight[Gap] = std::max(GapWeight[Gap], weight);
1246034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        if (Uses[Gap+1].getBaseIndex() >= IntI.stop())
1247034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          break;
1248034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      }
1249034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      if (Gap == NumGaps)
1250034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        break;
1251034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    }
1252034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  }
1253034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen}
1254034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1255034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// tryLocalSplit - Try to split VirtReg into smaller intervals inside its only
1256034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// basic block.
1257034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen///
1258034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesenunsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order,
1259034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen                                 SmallVectorImpl<LiveInterval*> &NewVRegs) {
1260db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  assert(SA->getUseBlocks().size() == 1 && "Not a local interval");
1261db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front();
1262034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1263034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // Note that it is possible to have an interval that is live-in or live-out
1264034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // while only covering a single block - A phi-def can use undef values from
1265034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // predecessors, and the block could be a single-block loop.
1266034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // We don't bother doing anything clever about such a case, we simply assume
1267fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen  // that the interval is continuous from FirstInstr to LastInstr. We should
1268fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen  // make sure that we don't do anything illegal to such an interval, though.
1269034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1270034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
1271034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  if (Uses.size() <= 2)
1272034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    return 0;
1273034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  const unsigned NumGaps = Uses.size()-1;
1274034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1275034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  DEBUG({
1276034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    dbgs() << "tryLocalSplit: ";
1277034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    for (unsigned i = 0, e = Uses.size(); i != e; ++i)
1278034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      dbgs() << ' ' << SA->UseSlots[i];
1279034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    dbgs() << '\n';
1280034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  });
1281034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1282b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // Since we allow local split results to be split again, there is a risk of
1283b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // creating infinite loops. It is tempting to require that the new live
1284b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // ranges have less instructions than the original. That would guarantee
1285b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // convergence, but it is too strict. A live range with 3 instructions can be
1286b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // split 2+3 (including the COPY), and we want to allow that.
1287b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  //
1288b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // Instead we use these rules:
1289b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  //
129049743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  // 1. Allow any split for ranges with getStage() < RS_Split2. (Except for the
1291b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  //    noop split, of course).
129249743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  // 2. Require progress be made for ranges with getStage() == RS_Split2. All
1293b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  //    the new ranges must have fewer instructions than before the split.
129449743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  // 3. New ranges with the same number of instructions are marked RS_Split2,
1295b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  //    smaller ranges are marked RS_New.
1296b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  //
1297b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // These rules allow a 3 -> 2+3 split once, which we need. They also prevent
1298b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // excessive splitting and infinite loops.
1299b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  //
130049743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  bool ProgressRequired = getStage(VirtReg) >= RS_Split2;
1301034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1302b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // Best split candidate.
1303034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  unsigned BestBefore = NumGaps;
1304034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  unsigned BestAfter = 0;
1305034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  float BestDiff = 0;
1306034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
130740a42a2ccaaa19a109667ed7abf224cc8733cd9cJakob Stoklund Olesen  const float blockFreq = SpillPlacer->getBlockFrequency(BI.MBB->getNumber());
1308034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  SmallVector<float, 8> GapWeight;
1309034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1310034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  Order.rewind();
1311034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  while (unsigned PhysReg = Order.next()) {
1312034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // Keep track of the largest spill weight that would need to be evicted in
1313034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // order to make use of PhysReg between UseSlots[i] and UseSlots[i+1].
1314034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    calcGapWeights(PhysReg, GapWeight);
1315034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1316034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // Try to find the best sequence of gaps to close.
1317034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // The new spill weight must be larger than any gap interference.
1318034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1319034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // We will split before Uses[SplitBefore] and after Uses[SplitAfter].
1320b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    unsigned SplitBefore = 0, SplitAfter = 1;
1321034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1322034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // MaxGap should always be max(GapWeight[SplitBefore..SplitAfter-1]).
1323034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // It is the spill weight that needs to be evicted.
1324034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    float MaxGap = GapWeight[0];
1325034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1326034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    for (;;) {
1327034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Live before/after split?
1328034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      const bool LiveBefore = SplitBefore != 0 || BI.LiveIn;
1329034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      const bool LiveAfter = SplitAfter != NumGaps || BI.LiveOut;
1330034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1331034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      DEBUG(dbgs() << PrintReg(PhysReg, TRI) << ' '
1332034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen                   << Uses[SplitBefore] << '-' << Uses[SplitAfter]
1333034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen                   << " i=" << MaxGap);
1334034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1335034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Stop before the interval gets so big we wouldn't be making progress.
1336034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      if (!LiveBefore && !LiveAfter) {
1337034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        DEBUG(dbgs() << " all\n");
1338034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        break;
1339034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      }
1340034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Should the interval be extended or shrunk?
1341034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      bool Shrink = true;
1342034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1343b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      // How many gaps would the new range have?
1344b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      unsigned NewGaps = LiveBefore + SplitAfter - SplitBefore + LiveAfter;
1345b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen
1346b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      // Legally, without causing looping?
1347b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      bool Legal = !ProgressRequired || NewGaps < NumGaps;
1348b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen
1349b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      if (Legal && MaxGap < HUGE_VALF) {
1350b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        // Estimate the new spill weight. Each instruction reads or writes the
1351b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        // register. Conservatively assume there are no read-modify-write
1352b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        // instructions.
1353034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        //
1354b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        // Try to guess the size of the new interval.
1355b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        const float EstWeight = normalizeSpillWeight(blockFreq * (NewGaps + 1),
1356b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen                                 Uses[SplitBefore].distance(Uses[SplitAfter]) +
1357b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen                                 (LiveBefore + LiveAfter)*SlotIndex::InstrDist);
1358034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        // Would this split be possible to allocate?
1359034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        // Never allocate all gaps, we wouldn't be making progress.
136066446c803a11a26e4bb39b74091d146ac850ae4cJakob Stoklund Olesen        DEBUG(dbgs() << " w=" << EstWeight);
136166446c803a11a26e4bb39b74091d146ac850ae4cJakob Stoklund Olesen        if (EstWeight * Hysteresis >= MaxGap) {
1362034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          Shrink = false;
136366446c803a11a26e4bb39b74091d146ac850ae4cJakob Stoklund Olesen          float Diff = EstWeight - MaxGap;
1364034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          if (Diff > BestDiff) {
1365034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen            DEBUG(dbgs() << " (best)");
136666446c803a11a26e4bb39b74091d146ac850ae4cJakob Stoklund Olesen            BestDiff = Hysteresis * Diff;
1367034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen            BestBefore = SplitBefore;
1368034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen            BestAfter = SplitAfter;
1369034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          }
1370034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        }
1371034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      }
1372034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1373034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Try to shrink.
1374034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      if (Shrink) {
1375b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        if (++SplitBefore < SplitAfter) {
1376034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          DEBUG(dbgs() << " shrink\n");
1377034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          // Recompute the max when necessary.
1378034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          if (GapWeight[SplitBefore - 1] >= MaxGap) {
1379034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen            MaxGap = GapWeight[SplitBefore];
1380034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen            for (unsigned i = SplitBefore + 1; i != SplitAfter; ++i)
1381034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen              MaxGap = std::max(MaxGap, GapWeight[i]);
1382034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          }
1383034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          continue;
1384034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        }
1385034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        MaxGap = 0;
1386034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      }
1387034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1388034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Try to extend the interval.
1389034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      if (SplitAfter >= NumGaps) {
1390034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        DEBUG(dbgs() << " end\n");
1391034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        break;
1392034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      }
1393034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1394034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      DEBUG(dbgs() << " extend\n");
1395b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      MaxGap = std::max(MaxGap, GapWeight[SplitAfter++]);
1396034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    }
1397034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  }
1398034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1399034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // Didn't find any candidates?
1400034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  if (BestBefore == NumGaps)
1401034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    return 0;
1402034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1403034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  DEBUG(dbgs() << "Best local split range: " << Uses[BestBefore]
1404034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen               << '-' << Uses[BestAfter] << ", " << BestDiff
1405034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen               << ", " << (BestAfter - BestBefore + 1) << " instrs\n");
1406034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
140792a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen  LiveRangeEdit LREdit(VirtReg, NewVRegs, this);
1408bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen  SE->reset(LREdit);
1409bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen
1410bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen  SE->openIntv();
1411bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen  SlotIndex SegStart = SE->enterIntvBefore(Uses[BestBefore]);
1412bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen  SlotIndex SegStop  = SE->leaveIntvAfter(Uses[BestAfter]);
1413bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen  SE->useIntv(SegStart, SegStop);
1414b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  SmallVector<unsigned, 8> IntvMap;
1415b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  SE->finish(&IntvMap);
1416f42b66169d75301346e3685fd2b3e45e47806367Jakob Stoklund Olesen  DebugVars->splitRegister(VirtReg.reg, LREdit.regs());
1417b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen
1418b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // If the new range has the same number of instructions as before, mark it as
141949743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  // RS_Split2 so the next split will be forced to make progress. Otherwise,
1420b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // leave the new intervals as RS_New so they can compete.
1421b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  bool LiveBefore = BestBefore != 0 || BI.LiveIn;
1422b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  bool LiveAfter = BestAfter != NumGaps || BI.LiveOut;
1423b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  unsigned NewGaps = LiveBefore + BestAfter - BestBefore + LiveAfter;
1424b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  if (NewGaps >= NumGaps) {
1425b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    DEBUG(dbgs() << "Tagging non-progress ranges: ");
1426b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    assert(!ProgressRequired && "Didn't make progress when it was required.");
1427b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    for (unsigned i = 0, e = IntvMap.size(); i != e; ++i)
1428b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      if (IntvMap[i] == 1) {
142949743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen        setStage(*LREdit.get(i), RS_Split2);
1430b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        DEBUG(dbgs() << PrintReg(LREdit.get(i)->reg));
1431b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      }
1432b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    DEBUG(dbgs() << '\n');
1433b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  }
14340db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen  ++NumLocalSplits;
1435034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1436034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  return 0;
1437034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen}
1438034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1439034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen//===----------------------------------------------------------------------===//
1440ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen//                          Live Range Splitting
1441ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen//===----------------------------------------------------------------------===//
1442ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1443ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// trySplit - Try to split VirtReg or one of its interferences, making it
1444ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// assignable.
1445ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// @return Physreg when VirtReg may be assigned and/or new NewVRegs.
1446ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesenunsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order,
1447ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen                            SmallVectorImpl<LiveInterval*>&NewVRegs) {
1448034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // Local intervals are handled separately.
1449a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen  if (LIS->intervalIsInOneMBB(VirtReg)) {
1450a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen    NamedRegionTimer T("Local Splitting", TimerGroupName, TimePassesIsEnabled);
145122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen    SA->analyze(&VirtReg);
1452034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    return tryLocalSplit(VirtReg, Order, NewVRegs);
1453a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen  }
1454a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen
1455a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen  NamedRegionTimer T("Global Splitting", TimerGroupName, TimePassesIsEnabled);
1456ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
145749743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  // Ranges must be Split2 or less.
1458fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen  if (getStage(VirtReg) >= RS_Spill)
145922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen    return 0;
146022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen
146122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  SA->analyze(&VirtReg);
146222a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen
14637d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen  // FIXME: SplitAnalysis may repair broken live ranges coming from the
14647d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen  // coalescer. That may cause the range to become allocatable which means that
14657d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen  // tryRegionSplit won't be making progress. This check should be replaced with
14667d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen  // an assertion when the coalescer is fixed.
14677d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen  if (SA->didRepairRange()) {
14687d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen    // VirtReg has changed, so all cached queries are invalid.
1469bdda37d7fbafe6876f248341837423a4100f95a5Jakob Stoklund Olesen    invalidateVirtRegs();
14707d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen    if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs))
14717d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen      return PhysReg;
14727d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen  }
14737d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen
147449743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  // First try to split around a region spanning multiple blocks. RS_Split2
147549743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  // ranges already made dubious progress with region splitting, so they go
147649743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  // straight to single block splitting.
147749743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  if (getStage(VirtReg) < RS_Split2) {
147849743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen    unsigned PhysReg = tryRegionSplit(VirtReg, Order, NewVRegs);
147949743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen    if (PhysReg || !NewVRegs.empty())
148049743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen      return PhysReg;
148149743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  }
1482ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1483ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  // Then isolate blocks with multiple uses.
1484fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen  SplitAnalysis::BlockPtrSet Blocks;
1485fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen  if (SA->getMultiUseBlocks(Blocks)) {
1486fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen    LiveRangeEdit LREdit(VirtReg, NewVRegs, this);
1487fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen    SE->reset(LREdit);
1488fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen    SE->splitSingleBlocks(Blocks);
1489fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    setStage(NewVRegs.begin(), NewVRegs.end(), RS_Spill);
1490fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen    if (VerifyEnabled)
1491fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen      MF->verify(this, "After splitting live range around basic blocks");
1492ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  }
1493ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1494ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  // Don't assign any physregs.
1495ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  return 0;
1496ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen}
1497ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1498ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1499b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen//===----------------------------------------------------------------------===//
1500770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen//                            Main Entry Point
1501770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen//===----------------------------------------------------------------------===//
1502770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen
1503770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesenunsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg,
1504ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen                                 SmallVectorImpl<LiveInterval*> &NewVRegs) {
1505770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen  // First try assigning a free register.
15065f2316a3b55f88dab2190212210770180a32aa95Jakob Stoklund Olesen  AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo);
15076bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs))
15086bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen    return PhysReg;
1509b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick
1510b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen  LiveRangeStage Stage = getStage(VirtReg);
15111a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  DEBUG(dbgs() << StageName[Stage]
15121a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen               << " Cascade " << ExtraRegInfo[VirtReg.reg].Cascade << '\n');
1513b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen
151476395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen  // Try to evict a less worthy live range, but only for ranges from the primary
1515fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen  // queue. The RS_Split ranges already failed to do this, and they should not
151676395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen  // get a second chance until they have been split.
1517fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen  if (Stage != RS_Split)
151876395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen    if (unsigned PhysReg = tryEvict(VirtReg, Order, NewVRegs))
151976395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen      return PhysReg;
1520b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick
1521ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  assert(NewVRegs.empty() && "Cannot append to existing NewVRegs");
1522ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1523107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen  // The first time we see a live range, don't try to split or spill.
1524107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen  // Wait until the second time, when all smaller ranges have been allocated.
1525107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen  // This gives a better picture of the interference to split around.
1526fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen  if (Stage < RS_Split) {
1527fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    setStage(VirtReg, RS_Split);
1528c1655e1a3c3a566b91b0513b56d61b58da1e36baJakob Stoklund Olesen    DEBUG(dbgs() << "wait for second round\n");
1529107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen    NewVRegs.push_back(&VirtReg);
1530107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen    return 0;
1531107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen  }
1532107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen
1533bf4e10f2f69db24c107cb61d6fe10ed5b2047374Jakob Stoklund Olesen  // If we couldn't allocate a register from spilling, there is probably some
1534bf4e10f2f69db24c107cb61d6fe10ed5b2047374Jakob Stoklund Olesen  // invalid inline assembly. The base class wil report it.
1535fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen  if (Stage >= RS_Done || !VirtReg.isSpillable())
1536bf4e10f2f69db24c107cb61d6fe10ed5b2047374Jakob Stoklund Olesen    return ~0u;
153722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen
153846c83c80c5737743c955ff007fa6409804a7abf0Jakob Stoklund Olesen  // Try splitting VirtReg or interferences.
1539ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  unsigned PhysReg = trySplit(VirtReg, Order, NewVRegs);
1540ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  if (PhysReg || !NewVRegs.empty())
1541b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen    return PhysReg;
1542b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen
1543770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen  // Finally spill VirtReg itself.
1544770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen  NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled);
154547dbf6cef761c25cfeb0aa7d624a6f98288bb96aJakob Stoklund Olesen  LiveRangeEdit LRE(VirtReg, NewVRegs, this);
154647dbf6cef761c25cfeb0aa7d624a6f98288bb96aJakob Stoklund Olesen  spiller().spill(LRE);
1547fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen  setStage(NewVRegs.begin(), NewVRegs.end(), RS_Done);
1548cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
1549c46570dc05851395829bef904bb6ddb1260400d1Jakob Stoklund Olesen  if (VerifyEnabled)
1550c46570dc05851395829bef904bb6ddb1260400d1Jakob Stoklund Olesen    MF->verify(this, "After spilling");
1551c46570dc05851395829bef904bb6ddb1260400d1Jakob Stoklund Olesen
1552cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  // The live virtual register requesting allocation was spilled, so tell
1553cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  // the caller not to allocate anything during this round.
1554cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  return 0;
1555cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen}
1556cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
1557cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenbool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
1558cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n"
1559cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen               << "********** Function: "
1560cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen               << ((Value*)mf.getFunction())->getName() << '\n');
1561cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
1562cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  MF = &mf;
1563af24964251e27c2dd863239ba66ffd967b593be5Jakob Stoklund Olesen  if (VerifyEnabled)
156489cab93fe999f6d81b4b99a71ac797b7ecfec277Jakob Stoklund Olesen    MF->verify(this, "Before greedy register allocator");
1565af24964251e27c2dd863239ba66ffd967b593be5Jakob Stoklund Olesen
15664680dec5fb3a1b624f13ca9b2a555ca90a07973eJakob Stoklund Olesen  RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>());
1567b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  Indexes = &getAnalysis<SlotIndexes>();
1568f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen  DomTree = &getAnalysis<MachineDominatorTree>();
1569f6dff84d4e44d6c4a46c4f8a18e13c78f804547cJakob Stoklund Olesen  SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
1570d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen  Loops = &getAnalysis<MachineLoopInfo>();
1571b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  Bundles = &getAnalysis<EdgeBundles>();
1572b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  SpillPlacer = &getAnalysis<SpillPlacement>();
1573f42b66169d75301346e3685fd2b3e45e47806367Jakob Stoklund Olesen  DebugVars = &getAnalysis<LiveDebugVariables>();
1574b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
15751b847deb26b52051de39f4cbecd224c9fbd0d1c2Jakob Stoklund Olesen  SA.reset(new SplitAnalysis(*VRM, *LIS, *Loops));
1576bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen  SE.reset(new SplitEditor(*SA, *LIS, *VRM, *DomTree));
15771a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  ExtraRegInfo.clear();
15781a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  ExtraRegInfo.resize(MRI->getNumVirtRegs());
15791a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  NextCascade = 1;
1580eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen  IntfCache.init(MF, &PhysReg2LiveUnion[0], Indexes, TRI);
158100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  GlobalCand.resize(32);  // This will grow as needed.
1582d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen
1583cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  allocatePhysRegs();
1584cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  addMBBLiveIns(MF);
15858a61da8a689ee95874c833af4c7aa965fab5c0a9Jakob Stoklund Olesen  LIS->addKillFlags();
1586cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
1587cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  // Run rewriter
1588533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen  {
1589533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen    NamedRegionTimer T("Rewriter", TimerGroupName, TimePassesIsEnabled);
1590ba05c01dabc40373760a20c874103fc58d4377f0Jakob Stoklund Olesen    VRM->rewrite(Indexes);
1591533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen  }
1592cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
1593cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen  // Write out new DBG_VALUE instructions.
1594c47690264abd6ec6bdeab86ce057e99bb5d39fe4Jakob Stoklund Olesen  {
1595c47690264abd6ec6bdeab86ce057e99bb5d39fe4Jakob Stoklund Olesen    NamedRegionTimer T("Emit Debug Info", TimerGroupName, TimePassesIsEnabled);
1596c47690264abd6ec6bdeab86ce057e99bb5d39fe4Jakob Stoklund Olesen    DebugVars->emitDebugValues(VRM);
1597c47690264abd6ec6bdeab86ce057e99bb5d39fe4Jakob Stoklund Olesen  }
1598cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen
1599cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  // The pass output is in VirtRegMap. Release all the transient data.
1600cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  releaseMemory();
1601cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
1602cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  return true;
1603cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen}
1604