RegAllocGreedy.cpp revision 2d6d86be84ee355223ccd20b7f87a0c9971c50c9
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
55a92afc18a8ff72b905902d75a2a4920d5438ca0cJakob Stoklund Olesencl::opt<bool> CompactRegions("compact-regions", cl::init(true));
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
809b87f91b063a0ac853735f2af3bd94fb8551a11ffJakob Stoklund Olesen      // Provide a strong negative bias on through blocks to prevent unwanted
810b87f91b063a0ac853735f2af3bd94fb8551a11ffJakob Stoklund Olesen      // liveness on loop backedges.
811b87f91b063a0ac853735f2af3bd94fb8551a11ffJakob Stoklund Olesen      SpillPlacer->addPrefSpill(NewBlocks, /* Strong= */ true);
812549019792a8b14500cab093ac8f3c5f7331e86d7Jakob Stoklund Olesen    AddedTo = ActiveBlocks.size();
813549019792a8b14500cab093ac8f3c5f7331e86d7Jakob Stoklund Olesen
814f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    // Perhaps iterating can enable more bundles?
815f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    SpillPlacer->iterate();
816f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  }
817f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  DEBUG(dbgs() << ", v=" << Visited);
818f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen}
81996dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen
82087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen/// calcCompactRegion - Compute the set of edge bundles that should be live
82187972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen/// when splitting the current live range into compact regions.  Compact
82287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen/// regions can be computed without looking at interference.  They are the
82387972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen/// regions formed by removing all the live-through blocks from the live range.
82487972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen///
82587972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen/// Returns false if the current live range is already compact, or if the
82687972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen/// compact regions would form single block regions anyway.
82787972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesenbool RAGreedy::calcCompactRegion(GlobalSplitCandidate &Cand) {
82887972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  // Without any through blocks, the live range is already compact.
82987972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  if (!SA->getNumThroughBlocks())
83087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    return false;
83187972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen
83287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  // Compact regions don't correspond to any physreg.
83387972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  Cand.reset(IntfCache, 0);
83487972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen
83587972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  DEBUG(dbgs() << "Compact region bundles");
83687972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen
83787972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  // Use the spill placer to determine the live bundles. GrowRegion pretends
83887972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  // that all the through blocks have interference when PhysReg is unset.
83987972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  SpillPlacer->prepare(Cand.LiveBundles);
84087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen
84187972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  // The static split cost will be zero since Cand.Intf reports no interference.
84287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  float Cost;
84387972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  if (!addSplitConstraints(Cand.Intf, Cost)) {
84487972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    DEBUG(dbgs() << ", none.\n");
84587972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    return false;
84687972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  }
84787972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen
84887972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  growRegion(Cand);
84987972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  SpillPlacer->finish();
85087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen
85187972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  if (!Cand.LiveBundles.any()) {
85287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    DEBUG(dbgs() << ", none.\n");
85387972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    return false;
85487972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  }
85587972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen
85687972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  DEBUG({
85787972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    for (int i = Cand.LiveBundles.find_first(); i>=0;
85887972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen         i = Cand.LiveBundles.find_next(i))
85987972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    dbgs() << " EB#" << i;
86087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    dbgs() << ".\n";
86187972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  });
86287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  return true;
86387972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen}
86487972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen
865200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen/// calcSpillCost - Compute how expensive it would be to split the live range in
866200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen/// SA around all use blocks instead of forming bundle regions.
867200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesenfloat RAGreedy::calcSpillCost() {
868200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen  float Cost = 0;
869200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen  ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
870200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen  for (unsigned i = 0; i != UseBlocks.size(); ++i) {
871200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
872200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    unsigned Number = BI.MBB->getNumber();
873200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    // We normally only need one spill instruction - a load or a store.
874200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    Cost += SpillPlacer->getBlockFrequency(Number);
875200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen
876200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    // Unless the value is redefined in the block.
8773f5beede1bb97ba4e06dc300e00b70e1013e7216Jakob Stoklund Olesen    if (BI.LiveIn && BI.LiveOut && BI.FirstDef)
8783f5beede1bb97ba4e06dc300e00b70e1013e7216Jakob Stoklund Olesen      Cost += SpillPlacer->getBlockFrequency(Number);
879200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen  }
880200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen  return Cost;
881200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen}
882200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen
883b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen/// calcGlobalSplitCost - Return the global split cost of following the split
884b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen/// pattern in LiveBundles. This cost should be added to the local cost of the
88596dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen/// interference pattern in SplitConstraints.
886b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen///
887c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesenfloat RAGreedy::calcGlobalSplitCost(GlobalSplitCandidate &Cand) {
888b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  float GlobalCost = 0;
8895db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen  const BitVector &LiveBundles = Cand.LiveBundles;
890db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
891db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  for (unsigned i = 0; i != UseBlocks.size(); ++i) {
892db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
89396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen    SpillPlacement::BlockConstraint &BC = SplitConstraints[i];
894874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen    bool RegIn  = LiveBundles[Bundles->getBundle(BC.Number, 0)];
895874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen    bool RegOut = LiveBundles[Bundles->getBundle(BC.Number, 1)];
896874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen    unsigned Ins = 0;
897874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen
898db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    if (BI.LiveIn)
899db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen      Ins += RegIn != (BC.Entry == SpillPlacement::PrefReg);
900db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    if (BI.LiveOut)
901db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen      Ins += RegOut != (BC.Exit == SpillPlacement::PrefReg);
902874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen    if (Ins)
903874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen      GlobalCost += Ins * SpillPlacer->getBlockFrequency(BC.Number);
904b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  }
905db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen
9065db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen  for (unsigned i = 0, e = Cand.ActiveBlocks.size(); i != e; ++i) {
9075db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen    unsigned Number = Cand.ActiveBlocks[i];
908db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    bool RegIn  = LiveBundles[Bundles->getBundle(Number, 0)];
909db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    bool RegOut = LiveBundles[Bundles->getBundle(Number, 1)];
9109a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen    if (!RegIn && !RegOut)
9119a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen      continue;
9129a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen    if (RegIn && RegOut) {
9139a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen      // We need double spill code if this block has interference.
914c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen      Cand.Intf.moveToBlock(Number);
915c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen      if (Cand.Intf.hasInterference())
9169a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen        GlobalCost += 2*SpillPlacer->getBlockFrequency(Number);
9179a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen      continue;
9189a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen    }
9199a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen    // live-in / stack-out or stack-in live-out.
9209a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen    GlobalCost += SpillPlacer->getBlockFrequency(Number);
921db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  }
922b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  return GlobalCost;
923b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen}
924b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
92500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// splitAroundRegion - Split the current live range around the regions
92600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// determined by BundleCand and GlobalCand.
927ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen///
92800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// Before calling this function, GlobalCand and BundleCand must be initialized
92900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// so each bundle is assigned to a valid candidate, or NoCand for the
93000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// stack-bound bundles.  The shared SA/SE SplitAnalysis and SplitEditor
93100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// objects must be initialized for the current live range, and intervals
93200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// created for the used candidates.
933ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen///
93400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// @param LREdit    The LiveRangeEdit object handling the current split.
93500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen/// @param UsedCands List of used GlobalCand entries. Every BundleCand value
93600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen///                  must appear in this list.
93700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesenvoid RAGreedy::splitAroundRegion(LiveRangeEdit &LREdit,
93800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen                                 ArrayRef<unsigned> UsedCands) {
93900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // These are the intervals created for new global ranges. We may create more
94000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // intervals for local ranges.
94100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  const unsigned NumGlobalIntvs = LREdit.size();
94200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  DEBUG(dbgs() << "splitAroundRegion with " << NumGlobalIntvs << " globals.\n");
94300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  assert(NumGlobalIntvs && "No global intervals configured");
944ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
9452d6d86be84ee355223ccd20b7f87a0c9971c50c9Jakob Stoklund Olesen  // Isolate even single instructions when dealing with a proper sub-class.
9462d6d86be84ee355223ccd20b7f87a0c9971c50c9Jakob Stoklund Olesen  // That giarantees register class inflation for the stack interval because it
9472d6d86be84ee355223ccd20b7f87a0c9971c50c9Jakob Stoklund Olesen  // is all copies.
9482d6d86be84ee355223ccd20b7f87a0c9971c50c9Jakob Stoklund Olesen  unsigned Reg = SA->getParent().reg;
9492d6d86be84ee355223ccd20b7f87a0c9971c50c9Jakob Stoklund Olesen  bool SingleInstrs = RegClassInfo.isProperSubClass(MRI->getRegClass(Reg));
9502d6d86be84ee355223ccd20b7f87a0c9971c50c9Jakob Stoklund Olesen
95187360f73ae205854f100ba5fb7eef7b90ac3bc27Jakob Stoklund Olesen  // First handle all the blocks with uses.
952db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
953db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  for (unsigned i = 0; i != UseBlocks.size(); ++i) {
954db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
95500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    unsigned Number = BI.MBB->getNumber();
95600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    unsigned IntvIn = 0, IntvOut = 0;
95700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    SlotIndex IntfIn, IntfOut;
95800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (BI.LiveIn) {
95900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      unsigned CandIn = BundleCand[Bundles->getBundle(Number, 0)];
96000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      if (CandIn != NoCand) {
96100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        GlobalSplitCandidate &Cand = GlobalCand[CandIn];
96200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        IntvIn = Cand.IntvIdx;
96300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        Cand.Intf.moveToBlock(Number);
96400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        IntfIn = Cand.Intf.first();
96500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      }
96600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    }
96700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (BI.LiveOut) {
96800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      unsigned CandOut = BundleCand[Bundles->getBundle(Number, 1)];
96900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      if (CandOut != NoCand) {
97000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        GlobalSplitCandidate &Cand = GlobalCand[CandOut];
97100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        IntvOut = Cand.IntvIdx;
97200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        Cand.Intf.moveToBlock(Number);
97300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        IntfOut = Cand.Intf.last();
97400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      }
97500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    }
976ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
977fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen    // Create separate intervals for isolated blocks with multiple uses.
97800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (!IntvIn && !IntvOut) {
979fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen      DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " isolated.\n");
9802d6d86be84ee355223ccd20b7f87a0c9971c50c9Jakob Stoklund Olesen      if (SA->shouldSplitSingleBlock(BI, SingleInstrs))
98187360f73ae205854f100ba5fb7eef7b90ac3bc27Jakob Stoklund Olesen        SE->splitSingleBlock(BI);
982fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen      continue;
983fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen    }
984fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen
98500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (IntvIn && IntvOut)
98600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut);
98700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    else if (IntvIn)
98800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      SE->splitRegInBlock(BI, IntvIn, IntfIn);
989b4ddedce599183362b0f0333922c2fe0e163a129Jakob Stoklund Olesen    else
99000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      SE->splitRegOutBlock(BI, IntvOut, IntfOut);
991ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  }
992ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
99300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // Handle live-through blocks. The relevant live-through blocks are stored in
99400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // the ActiveBlocks list with each candidate. We need to filter out
99500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // duplicates.
99600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  BitVector Todo = SA->getThroughBlocks();
99700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  for (unsigned c = 0; c != UsedCands.size(); ++c) {
99800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    ArrayRef<unsigned> Blocks = GlobalCand[UsedCands[c]].ActiveBlocks;
99900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    for (unsigned i = 0, e = Blocks.size(); i != e; ++i) {
100000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      unsigned Number = Blocks[i];
100100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      if (!Todo.test(Number))
100200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        continue;
100300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      Todo.reset(Number);
100400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
100500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      unsigned IntvIn = 0, IntvOut = 0;
100600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      SlotIndex IntfIn, IntfOut;
100700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
100800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      unsigned CandIn = BundleCand[Bundles->getBundle(Number, 0)];
100900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      if (CandIn != NoCand) {
101000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        GlobalSplitCandidate &Cand = GlobalCand[CandIn];
101100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        IntvIn = Cand.IntvIdx;
101200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        Cand.Intf.moveToBlock(Number);
101300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        IntfIn = Cand.Intf.first();
101400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      }
101500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
101600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      unsigned CandOut = BundleCand[Bundles->getBundle(Number, 1)];
101700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      if (CandOut != NoCand) {
101800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        GlobalSplitCandidate &Cand = GlobalCand[CandOut];
101900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        IntvOut = Cand.IntvIdx;
102000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        Cand.Intf.moveToBlock(Number);
102100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        IntfOut = Cand.Intf.last();
102200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      }
102300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      if (!IntvIn && !IntvOut)
102400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        continue;
102500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut);
102600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    }
1027db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  }
1028db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen
10290db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen  ++NumGlobalSplits;
1030ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
10315928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  SmallVector<unsigned, 8> IntvMap;
10325928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  SE->finish(&IntvMap);
103300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  DebugVars->splitRegister(SA->getParent().reg, LREdit.regs());
1034f42b66169d75301346e3685fd2b3e45e47806367Jakob Stoklund Olesen
10351a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  ExtraRegInfo.resize(MRI->getNumVirtRegs());
1036b2abfa0bf30edf292a27a06e091d03983e644c9bJakob Stoklund Olesen  unsigned OrigBlocks = SA->getNumLiveBlocks();
10375928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen
10385928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  // Sort out the new intervals created by splitting. We get four kinds:
10395928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  // - Remainder intervals should not be split again.
10405928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  // - Candidate intervals can be assigned to Cand.PhysReg.
10415928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  // - Block-local splits are candidates for local splitting.
10425928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  // - DCE leftovers should go back on the queue.
10435928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  for (unsigned i = 0, e = LREdit.size(); i != e; ++i) {
10441a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    LiveInterval &Reg = *LREdit.get(i);
10455928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen
10465928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen    // Ignore old intervals from DCE.
10471a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    if (getStage(Reg) != RS_New)
10485928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen      continue;
10495928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen
10505928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen    // Remainder interval. Don't try splitting again, spill if it doesn't
10515928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen    // allocate.
10525928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen    if (IntvMap[i] == 0) {
1053fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen      setStage(Reg, RS_Spill);
10545928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen      continue;
10555928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen    }
10565928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen
105700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    // Global intervals. Allow repeated splitting as long as the number of live
105800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    // blocks is strictly decreasing.
105900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (IntvMap[i] < NumGlobalIntvs) {
10601a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen      if (SA->countLiveBlocks(&Reg) >= OrigBlocks) {
10619f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen        DEBUG(dbgs() << "Main interval covers the same " << OrigBlocks
10629f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen                     << " blocks as original.\n");
10639f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen        // Don't allow repeated splitting as a safe guard against looping.
106449743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen        setStage(Reg, RS_Split2);
10659f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen      }
10669f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen      continue;
10679f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen    }
10689f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen
10699f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen    // Other intervals are treated as new. This includes local intervals created
10709f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen    // for blocks with multiple uses, and anything created by DCE.
10715928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  }
10725928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen
1073eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen  if (VerifyEnabled)
1074ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen    MF->verify(this, "After splitting live range around region");
1075ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen}
1076ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1077b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesenunsigned RAGreedy::tryRegionSplit(LiveInterval &VirtReg, AllocationOrder &Order,
1078b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen                                  SmallVectorImpl<LiveInterval*> &NewVRegs) {
1079c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen  unsigned NumCands = 0;
108000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  unsigned BestCand = NoCand;
108100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  float BestCost;
108200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  SmallVector<unsigned, 8> UsedCands;
108300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
108400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // Check if we can split this live range around a compact region.
108500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  bool HasCompact = CompactRegions && calcCompactRegion(GlobalCand.front());
108600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  if (HasCompact) {
108700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    // Yes, keep GlobalCand[0] as the compact region candidate.
108800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    NumCands = 1;
108900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    BestCost = HUGE_VALF;
109000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  } else {
109100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    // No benefit from the compact region, our fallback will be per-block
109200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    // splitting. Make sure we find a solution that is cheaper than spilling.
109300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    BestCost = Hysteresis * calcSpillCost();
109400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    DEBUG(dbgs() << "Cost of isolating all blocks = " << BestCost << '\n');
109500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  }
109696dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen
1097b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  Order.rewind();
1098c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen  while (unsigned PhysReg = Order.next()) {
1099f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen    // Discard bad candidates before we run out of interference cache cursors.
1100f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen    // This will only affect register classes with a lot of registers (>32).
1101f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen    if (NumCands == IntfCache.getMaxCursors()) {
1102f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen      unsigned WorstCount = ~0u;
1103f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen      unsigned Worst = 0;
1104f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen      for (unsigned i = 0; i != NumCands; ++i) {
110500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        if (i == BestCand || !GlobalCand[i].PhysReg)
1106f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen          continue;
1107f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen        unsigned Count = GlobalCand[i].LiveBundles.count();
1108f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen        if (Count < WorstCount)
1109f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen          Worst = i, WorstCount = Count;
1110f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen      }
1111f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen      --NumCands;
1112f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen      GlobalCand[Worst] = GlobalCand[NumCands];
1113f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen    }
1114f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen
1115c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    if (GlobalCand.size() <= NumCands)
1116c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen      GlobalCand.resize(NumCands+1);
1117c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    GlobalSplitCandidate &Cand = GlobalCand[NumCands];
1118c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    Cand.reset(IntfCache, PhysReg);
111996dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen
1120c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    SpillPlacer->prepare(Cand.LiveBundles);
11211b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen    float Cost;
1122c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    if (!addSplitConstraints(Cand.Intf, Cost)) {
1123f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen      DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tno positive bundles\n");
11241b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen      continue;
11251b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen    }
1126f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tstatic = " << Cost);
1127200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    if (Cost >= BestCost) {
1128200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen      DEBUG({
1129200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen        if (BestCand == NoCand)
1130200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen          dbgs() << " worse than no bundles\n";
1131200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen        else
1132200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen          dbgs() << " worse than "
1133200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen                 << PrintReg(GlobalCand[BestCand].PhysReg, TRI) << '\n';
1134200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen      });
1135b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen      continue;
1136874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen    }
1137c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    growRegion(Cand);
1138ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
11399efa2a263ea470caacef1c85f6ca45e32bf516d3Jakob Stoklund Olesen    SpillPlacer->finish();
11409efa2a263ea470caacef1c85f6ca45e32bf516d3Jakob Stoklund Olesen
1141ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen    // No live bundles, defer to splitSingleBlocks().
1142c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    if (!Cand.LiveBundles.any()) {
1143874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen      DEBUG(dbgs() << " no bundles.\n");
1144ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen      continue;
1145874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen    }
1146ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1147c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    Cost += calcGlobalSplitCost(Cand);
1148874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen    DEBUG({
1149874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen      dbgs() << ", total = " << Cost << " with bundles";
1150c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen      for (int i = Cand.LiveBundles.find_first(); i>=0;
1151c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen           i = Cand.LiveBundles.find_next(i))
1152874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen        dbgs() << " EB#" << i;
1153874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen      dbgs() << ".\n";
1154874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen    });
1155200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    if (Cost < BestCost) {
1156c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen      BestCand = NumCands;
1157200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen      BestCost = Hysteresis * Cost; // Prevent rounding effects.
1158b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen    }
1159c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    ++NumCands;
1160b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  }
1161ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
116200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // No solutions found, fall back to single block splitting.
116300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  if (!HasCompact && BestCand == NoCand)
1164ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen    return 0;
1165ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
116600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // Prepare split editor.
116700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  LiveRangeEdit LREdit(VirtReg, NewVRegs, this);
116800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  SE->reset(LREdit);
116900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
117000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // Assign all edge bundles to the preferred candidate, or NoCand.
117100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  BundleCand.assign(Bundles->getNumBundles(), NoCand);
117200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
117300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // Assign bundles for the best candidate region.
117400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  if (BestCand != NoCand) {
117500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    GlobalSplitCandidate &Cand = GlobalCand[BestCand];
117600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (unsigned B = Cand.getBundles(BundleCand, BestCand)) {
117700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      UsedCands.push_back(BestCand);
117800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      Cand.IntvIdx = SE->openIntv();
117900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      DEBUG(dbgs() << "Split for " << PrintReg(Cand.PhysReg, TRI) << " in "
118000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen                   << B << " bundles, intv " << Cand.IntvIdx << ".\n");
118132668ea7a290ee1cb6bfe8cd677cdd4e5df05b4dChandler Carruth      (void)B;
118200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    }
118300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  }
118400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
118500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // Assign bundles for the compact region.
118600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  if (HasCompact) {
118700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    GlobalSplitCandidate &Cand = GlobalCand.front();
118800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    assert(!Cand.PhysReg && "Compact region has no physreg");
118900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (unsigned B = Cand.getBundles(BundleCand, 0)) {
119000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      UsedCands.push_back(0);
119100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      Cand.IntvIdx = SE->openIntv();
119200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      DEBUG(dbgs() << "Split for compact region in " << B << " bundles, intv "
119300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen                   << Cand.IntvIdx << ".\n");
119432668ea7a290ee1cb6bfe8cd677cdd4e5df05b4dChandler Carruth      (void)B;
119500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    }
119600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  }
119700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
119800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  splitAroundRegion(LREdit, UsedCands);
1199b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  return 0;
1200b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen}
1201b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
1202ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1203ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen//===----------------------------------------------------------------------===//
1204034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen//                             Local Splitting
1205034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen//===----------------------------------------------------------------------===//
1206034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1207034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1208034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// calcGapWeights - Compute the maximum spill weight that needs to be evicted
1209034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// in order to use PhysReg between two entries in SA->UseSlots.
1210034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen///
1211034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// GapWeight[i] represents the gap between UseSlots[i] and UseSlots[i+1].
1212034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen///
1213034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesenvoid RAGreedy::calcGapWeights(unsigned PhysReg,
1214034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen                              SmallVectorImpl<float> &GapWeight) {
1215db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  assert(SA->getUseBlocks().size() == 1 && "Not a local interval");
1216db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front();
1217034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
1218034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  const unsigned NumGaps = Uses.size()-1;
1219034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1220034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // Start and end points for the interference check.
1221fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen  SlotIndex StartIdx =
1222fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen    BI.LiveIn ? BI.FirstInstr.getBaseIndex() : BI.FirstInstr;
1223fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen  SlotIndex StopIdx =
1224fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen    BI.LiveOut ? BI.LastInstr.getBoundaryIndex() : BI.LastInstr;
1225034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1226034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  GapWeight.assign(NumGaps, 0.0f);
1227034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1228034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // Add interference from each overlapping register.
1229034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
1230034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    if (!query(const_cast<LiveInterval&>(SA->getParent()), *AI)
1231034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen           .checkInterference())
1232034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      continue;
1233034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1234fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen    // We know that VirtReg is a continuous interval from FirstInstr to
1235fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen    // LastInstr, so we don't need InterferenceQuery.
1236034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    //
1237034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // Interference that overlaps an instruction is counted in both gaps
1238034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // surrounding the instruction. The exception is interference before
1239034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // StartIdx and after StopIdx.
1240034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    //
1241034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    LiveIntervalUnion::SegmentIter IntI = PhysReg2LiveUnion[*AI].find(StartIdx);
1242034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    for (unsigned Gap = 0; IntI.valid() && IntI.start() < StopIdx; ++IntI) {
1243034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Skip the gaps before IntI.
1244034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      while (Uses[Gap+1].getBoundaryIndex() < IntI.start())
1245034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        if (++Gap == NumGaps)
1246034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          break;
1247034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      if (Gap == NumGaps)
1248034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        break;
1249034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1250034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Update the gaps covered by IntI.
1251034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      const float weight = IntI.value()->weight;
1252034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      for (; Gap != NumGaps; ++Gap) {
1253034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        GapWeight[Gap] = std::max(GapWeight[Gap], weight);
1254034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        if (Uses[Gap+1].getBaseIndex() >= IntI.stop())
1255034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          break;
1256034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      }
1257034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      if (Gap == NumGaps)
1258034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        break;
1259034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    }
1260034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  }
1261034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen}
1262034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1263034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// tryLocalSplit - Try to split VirtReg into smaller intervals inside its only
1264034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// basic block.
1265034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen///
1266034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesenunsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order,
1267034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen                                 SmallVectorImpl<LiveInterval*> &NewVRegs) {
1268db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  assert(SA->getUseBlocks().size() == 1 && "Not a local interval");
1269db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front();
1270034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1271034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // Note that it is possible to have an interval that is live-in or live-out
1272034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // while only covering a single block - A phi-def can use undef values from
1273034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // predecessors, and the block could be a single-block loop.
1274034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // We don't bother doing anything clever about such a case, we simply assume
1275fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen  // that the interval is continuous from FirstInstr to LastInstr. We should
1276fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen  // make sure that we don't do anything illegal to such an interval, though.
1277034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1278034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
1279034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  if (Uses.size() <= 2)
1280034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    return 0;
1281034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  const unsigned NumGaps = Uses.size()-1;
1282034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1283034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  DEBUG({
1284034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    dbgs() << "tryLocalSplit: ";
1285034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    for (unsigned i = 0, e = Uses.size(); i != e; ++i)
1286034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      dbgs() << ' ' << SA->UseSlots[i];
1287034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    dbgs() << '\n';
1288034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  });
1289034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1290b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // Since we allow local split results to be split again, there is a risk of
1291b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // creating infinite loops. It is tempting to require that the new live
1292b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // ranges have less instructions than the original. That would guarantee
1293b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // convergence, but it is too strict. A live range with 3 instructions can be
1294b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // split 2+3 (including the COPY), and we want to allow that.
1295b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  //
1296b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // Instead we use these rules:
1297b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  //
129849743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  // 1. Allow any split for ranges with getStage() < RS_Split2. (Except for the
1299b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  //    noop split, of course).
130049743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  // 2. Require progress be made for ranges with getStage() == RS_Split2. All
1301b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  //    the new ranges must have fewer instructions than before the split.
130249743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  // 3. New ranges with the same number of instructions are marked RS_Split2,
1303b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  //    smaller ranges are marked RS_New.
1304b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  //
1305b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // These rules allow a 3 -> 2+3 split once, which we need. They also prevent
1306b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // excessive splitting and infinite loops.
1307b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  //
130849743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  bool ProgressRequired = getStage(VirtReg) >= RS_Split2;
1309034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1310b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // Best split candidate.
1311034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  unsigned BestBefore = NumGaps;
1312034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  unsigned BestAfter = 0;
1313034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  float BestDiff = 0;
1314034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
131540a42a2ccaaa19a109667ed7abf224cc8733cd9cJakob Stoklund Olesen  const float blockFreq = SpillPlacer->getBlockFrequency(BI.MBB->getNumber());
1316034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  SmallVector<float, 8> GapWeight;
1317034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1318034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  Order.rewind();
1319034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  while (unsigned PhysReg = Order.next()) {
1320034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // Keep track of the largest spill weight that would need to be evicted in
1321034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // order to make use of PhysReg between UseSlots[i] and UseSlots[i+1].
1322034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    calcGapWeights(PhysReg, GapWeight);
1323034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1324034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // Try to find the best sequence of gaps to close.
1325034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // The new spill weight must be larger than any gap interference.
1326034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1327034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // We will split before Uses[SplitBefore] and after Uses[SplitAfter].
1328b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    unsigned SplitBefore = 0, SplitAfter = 1;
1329034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1330034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // MaxGap should always be max(GapWeight[SplitBefore..SplitAfter-1]).
1331034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // It is the spill weight that needs to be evicted.
1332034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    float MaxGap = GapWeight[0];
1333034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1334034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    for (;;) {
1335034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Live before/after split?
1336034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      const bool LiveBefore = SplitBefore != 0 || BI.LiveIn;
1337034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      const bool LiveAfter = SplitAfter != NumGaps || BI.LiveOut;
1338034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1339034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      DEBUG(dbgs() << PrintReg(PhysReg, TRI) << ' '
1340034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen                   << Uses[SplitBefore] << '-' << Uses[SplitAfter]
1341034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen                   << " i=" << MaxGap);
1342034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1343034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Stop before the interval gets so big we wouldn't be making progress.
1344034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      if (!LiveBefore && !LiveAfter) {
1345034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        DEBUG(dbgs() << " all\n");
1346034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        break;
1347034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      }
1348034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Should the interval be extended or shrunk?
1349034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      bool Shrink = true;
1350034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1351b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      // How many gaps would the new range have?
1352b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      unsigned NewGaps = LiveBefore + SplitAfter - SplitBefore + LiveAfter;
1353b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen
1354b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      // Legally, without causing looping?
1355b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      bool Legal = !ProgressRequired || NewGaps < NumGaps;
1356b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen
1357b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      if (Legal && MaxGap < HUGE_VALF) {
1358b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        // Estimate the new spill weight. Each instruction reads or writes the
1359b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        // register. Conservatively assume there are no read-modify-write
1360b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        // instructions.
1361034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        //
1362b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        // Try to guess the size of the new interval.
1363b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        const float EstWeight = normalizeSpillWeight(blockFreq * (NewGaps + 1),
1364b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen                                 Uses[SplitBefore].distance(Uses[SplitAfter]) +
1365b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen                                 (LiveBefore + LiveAfter)*SlotIndex::InstrDist);
1366034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        // Would this split be possible to allocate?
1367034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        // Never allocate all gaps, we wouldn't be making progress.
136866446c803a11a26e4bb39b74091d146ac850ae4cJakob Stoklund Olesen        DEBUG(dbgs() << " w=" << EstWeight);
136966446c803a11a26e4bb39b74091d146ac850ae4cJakob Stoklund Olesen        if (EstWeight * Hysteresis >= MaxGap) {
1370034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          Shrink = false;
137166446c803a11a26e4bb39b74091d146ac850ae4cJakob Stoklund Olesen          float Diff = EstWeight - MaxGap;
1372034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          if (Diff > BestDiff) {
1373034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen            DEBUG(dbgs() << " (best)");
137466446c803a11a26e4bb39b74091d146ac850ae4cJakob Stoklund Olesen            BestDiff = Hysteresis * Diff;
1375034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen            BestBefore = SplitBefore;
1376034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen            BestAfter = SplitAfter;
1377034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          }
1378034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        }
1379034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      }
1380034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1381034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Try to shrink.
1382034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      if (Shrink) {
1383b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        if (++SplitBefore < SplitAfter) {
1384034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          DEBUG(dbgs() << " shrink\n");
1385034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          // Recompute the max when necessary.
1386034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          if (GapWeight[SplitBefore - 1] >= MaxGap) {
1387034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen            MaxGap = GapWeight[SplitBefore];
1388034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen            for (unsigned i = SplitBefore + 1; i != SplitAfter; ++i)
1389034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen              MaxGap = std::max(MaxGap, GapWeight[i]);
1390034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          }
1391034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          continue;
1392034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        }
1393034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        MaxGap = 0;
1394034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      }
1395034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1396034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Try to extend the interval.
1397034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      if (SplitAfter >= NumGaps) {
1398034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        DEBUG(dbgs() << " end\n");
1399034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        break;
1400034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      }
1401034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1402034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      DEBUG(dbgs() << " extend\n");
1403b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      MaxGap = std::max(MaxGap, GapWeight[SplitAfter++]);
1404034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    }
1405034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  }
1406034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1407034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // Didn't find any candidates?
1408034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  if (BestBefore == NumGaps)
1409034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    return 0;
1410034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1411034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  DEBUG(dbgs() << "Best local split range: " << Uses[BestBefore]
1412034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen               << '-' << Uses[BestAfter] << ", " << BestDiff
1413034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen               << ", " << (BestAfter - BestBefore + 1) << " instrs\n");
1414034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
141592a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen  LiveRangeEdit LREdit(VirtReg, NewVRegs, this);
1416bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen  SE->reset(LREdit);
1417bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen
1418bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen  SE->openIntv();
1419bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen  SlotIndex SegStart = SE->enterIntvBefore(Uses[BestBefore]);
1420bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen  SlotIndex SegStop  = SE->leaveIntvAfter(Uses[BestAfter]);
1421bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen  SE->useIntv(SegStart, SegStop);
1422b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  SmallVector<unsigned, 8> IntvMap;
1423b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  SE->finish(&IntvMap);
1424f42b66169d75301346e3685fd2b3e45e47806367Jakob Stoklund Olesen  DebugVars->splitRegister(VirtReg.reg, LREdit.regs());
1425b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen
1426b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // If the new range has the same number of instructions as before, mark it as
142749743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  // RS_Split2 so the next split will be forced to make progress. Otherwise,
1428b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  // leave the new intervals as RS_New so they can compete.
1429b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  bool LiveBefore = BestBefore != 0 || BI.LiveIn;
1430b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  bool LiveAfter = BestAfter != NumGaps || BI.LiveOut;
1431b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  unsigned NewGaps = LiveBefore + BestAfter - BestBefore + LiveAfter;
1432b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  if (NewGaps >= NumGaps) {
1433b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    DEBUG(dbgs() << "Tagging non-progress ranges: ");
1434b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    assert(!ProgressRequired && "Didn't make progress when it was required.");
1435b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    for (unsigned i = 0, e = IntvMap.size(); i != e; ++i)
1436b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      if (IntvMap[i] == 1) {
143749743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen        setStage(*LREdit.get(i), RS_Split2);
1438b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        DEBUG(dbgs() << PrintReg(LREdit.get(i)->reg));
1439b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      }
1440b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    DEBUG(dbgs() << '\n');
1441b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen  }
14420db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen  ++NumLocalSplits;
1443034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1444034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  return 0;
1445034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen}
1446034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1447034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen//===----------------------------------------------------------------------===//
1448ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen//                          Live Range Splitting
1449ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen//===----------------------------------------------------------------------===//
1450ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1451ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// trySplit - Try to split VirtReg or one of its interferences, making it
1452ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// assignable.
1453ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen/// @return Physreg when VirtReg may be assigned and/or new NewVRegs.
1454ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesenunsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order,
1455ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen                            SmallVectorImpl<LiveInterval*>&NewVRegs) {
1456034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // Local intervals are handled separately.
1457a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen  if (LIS->intervalIsInOneMBB(VirtReg)) {
1458a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen    NamedRegionTimer T("Local Splitting", TimerGroupName, TimePassesIsEnabled);
145922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen    SA->analyze(&VirtReg);
1460034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    return tryLocalSplit(VirtReg, Order, NewVRegs);
1461a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen  }
1462a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen
1463a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen  NamedRegionTimer T("Global Splitting", TimerGroupName, TimePassesIsEnabled);
1464ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
146549743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  // Ranges must be Split2 or less.
1466fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen  if (getStage(VirtReg) >= RS_Spill)
146722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen    return 0;
146822a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen
146922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  SA->analyze(&VirtReg);
147022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen
14717d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen  // FIXME: SplitAnalysis may repair broken live ranges coming from the
14727d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen  // coalescer. That may cause the range to become allocatable which means that
14737d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen  // tryRegionSplit won't be making progress. This check should be replaced with
14747d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen  // an assertion when the coalescer is fixed.
14757d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen  if (SA->didRepairRange()) {
14767d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen    // VirtReg has changed, so all cached queries are invalid.
1477bdda37d7fbafe6876f248341837423a4100f95a5Jakob Stoklund Olesen    invalidateVirtRegs();
14787d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen    if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs))
14797d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen      return PhysReg;
14807d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen  }
14817d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen
148249743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  // First try to split around a region spanning multiple blocks. RS_Split2
148349743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  // ranges already made dubious progress with region splitting, so they go
148449743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  // straight to single block splitting.
148549743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  if (getStage(VirtReg) < RS_Split2) {
148649743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen    unsigned PhysReg = tryRegionSplit(VirtReg, Order, NewVRegs);
148749743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen    if (PhysReg || !NewVRegs.empty())
148849743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen      return PhysReg;
148949743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  }
1490ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1491ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  // Then isolate blocks with multiple uses.
1492fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen  SplitAnalysis::BlockPtrSet Blocks;
1493fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen  if (SA->getMultiUseBlocks(Blocks)) {
1494fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen    LiveRangeEdit LREdit(VirtReg, NewVRegs, this);
1495fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen    SE->reset(LREdit);
1496fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen    SE->splitSingleBlocks(Blocks);
1497fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    setStage(NewVRegs.begin(), NewVRegs.end(), RS_Spill);
1498fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen    if (VerifyEnabled)
1499fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen      MF->verify(this, "After splitting live range around basic blocks");
1500ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  }
1501ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1502ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  // Don't assign any physregs.
1503ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  return 0;
1504ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen}
1505ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1506ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1507b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen//===----------------------------------------------------------------------===//
1508770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen//                            Main Entry Point
1509770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen//===----------------------------------------------------------------------===//
1510770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen
1511770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesenunsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg,
1512ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen                                 SmallVectorImpl<LiveInterval*> &NewVRegs) {
1513770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen  // First try assigning a free register.
15145f2316a3b55f88dab2190212210770180a32aa95Jakob Stoklund Olesen  AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo);
15156bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs))
15166bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen    return PhysReg;
1517b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick
1518b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen  LiveRangeStage Stage = getStage(VirtReg);
15191a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  DEBUG(dbgs() << StageName[Stage]
15201a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen               << " Cascade " << ExtraRegInfo[VirtReg.reg].Cascade << '\n');
1521b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen
152276395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen  // Try to evict a less worthy live range, but only for ranges from the primary
1523fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen  // queue. The RS_Split ranges already failed to do this, and they should not
152476395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen  // get a second chance until they have been split.
1525fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen  if (Stage != RS_Split)
152676395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen    if (unsigned PhysReg = tryEvict(VirtReg, Order, NewVRegs))
152776395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen      return PhysReg;
1528b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick
1529ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  assert(NewVRegs.empty() && "Cannot append to existing NewVRegs");
1530ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1531107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen  // The first time we see a live range, don't try to split or spill.
1532107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen  // Wait until the second time, when all smaller ranges have been allocated.
1533107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen  // This gives a better picture of the interference to split around.
1534fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen  if (Stage < RS_Split) {
1535fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    setStage(VirtReg, RS_Split);
1536c1655e1a3c3a566b91b0513b56d61b58da1e36baJakob Stoklund Olesen    DEBUG(dbgs() << "wait for second round\n");
1537107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen    NewVRegs.push_back(&VirtReg);
1538107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen    return 0;
1539107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen  }
1540107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen
1541bf4e10f2f69db24c107cb61d6fe10ed5b2047374Jakob Stoklund Olesen  // If we couldn't allocate a register from spilling, there is probably some
1542bf4e10f2f69db24c107cb61d6fe10ed5b2047374Jakob Stoklund Olesen  // invalid inline assembly. The base class wil report it.
1543fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen  if (Stage >= RS_Done || !VirtReg.isSpillable())
1544bf4e10f2f69db24c107cb61d6fe10ed5b2047374Jakob Stoklund Olesen    return ~0u;
154522a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen
154646c83c80c5737743c955ff007fa6409804a7abf0Jakob Stoklund Olesen  // Try splitting VirtReg or interferences.
1547ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  unsigned PhysReg = trySplit(VirtReg, Order, NewVRegs);
1548ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  if (PhysReg || !NewVRegs.empty())
1549b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen    return PhysReg;
1550b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen
1551770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen  // Finally spill VirtReg itself.
1552770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen  NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled);
155347dbf6cef761c25cfeb0aa7d624a6f98288bb96aJakob Stoklund Olesen  LiveRangeEdit LRE(VirtReg, NewVRegs, this);
155447dbf6cef761c25cfeb0aa7d624a6f98288bb96aJakob Stoklund Olesen  spiller().spill(LRE);
1555fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen  setStage(NewVRegs.begin(), NewVRegs.end(), RS_Done);
1556cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
1557c46570dc05851395829bef904bb6ddb1260400d1Jakob Stoklund Olesen  if (VerifyEnabled)
1558c46570dc05851395829bef904bb6ddb1260400d1Jakob Stoklund Olesen    MF->verify(this, "After spilling");
1559c46570dc05851395829bef904bb6ddb1260400d1Jakob Stoklund Olesen
1560cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  // The live virtual register requesting allocation was spilled, so tell
1561cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  // the caller not to allocate anything during this round.
1562cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  return 0;
1563cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen}
1564cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
1565cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenbool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
1566cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n"
1567cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen               << "********** Function: "
1568cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen               << ((Value*)mf.getFunction())->getName() << '\n');
1569cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
1570cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  MF = &mf;
1571af24964251e27c2dd863239ba66ffd967b593be5Jakob Stoklund Olesen  if (VerifyEnabled)
157289cab93fe999f6d81b4b99a71ac797b7ecfec277Jakob Stoklund Olesen    MF->verify(this, "Before greedy register allocator");
1573af24964251e27c2dd863239ba66ffd967b593be5Jakob Stoklund Olesen
15744680dec5fb3a1b624f13ca9b2a555ca90a07973eJakob Stoklund Olesen  RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>());
1575b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  Indexes = &getAnalysis<SlotIndexes>();
1576f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen  DomTree = &getAnalysis<MachineDominatorTree>();
1577f6dff84d4e44d6c4a46c4f8a18e13c78f804547cJakob Stoklund Olesen  SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
1578d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen  Loops = &getAnalysis<MachineLoopInfo>();
1579b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  Bundles = &getAnalysis<EdgeBundles>();
1580b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  SpillPlacer = &getAnalysis<SpillPlacement>();
1581f42b66169d75301346e3685fd2b3e45e47806367Jakob Stoklund Olesen  DebugVars = &getAnalysis<LiveDebugVariables>();
1582b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
15831b847deb26b52051de39f4cbecd224c9fbd0d1c2Jakob Stoklund Olesen  SA.reset(new SplitAnalysis(*VRM, *LIS, *Loops));
1584bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen  SE.reset(new SplitEditor(*SA, *LIS, *VRM, *DomTree));
15851a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  ExtraRegInfo.clear();
15861a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  ExtraRegInfo.resize(MRI->getNumVirtRegs());
15871a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  NextCascade = 1;
1588eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen  IntfCache.init(MF, &PhysReg2LiveUnion[0], Indexes, TRI);
158900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  GlobalCand.resize(32);  // This will grow as needed.
1590d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen
1591cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  allocatePhysRegs();
1592cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  addMBBLiveIns(MF);
15938a61da8a689ee95874c833af4c7aa965fab5c0a9Jakob Stoklund Olesen  LIS->addKillFlags();
1594cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
1595cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  // Run rewriter
1596533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen  {
1597533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen    NamedRegionTimer T("Rewriter", TimerGroupName, TimePassesIsEnabled);
1598ba05c01dabc40373760a20c874103fc58d4377f0Jakob Stoklund Olesen    VRM->rewrite(Indexes);
1599533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen  }
1600cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
1601cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen  // Write out new DBG_VALUE instructions.
1602c47690264abd6ec6bdeab86ce057e99bb5d39fe4Jakob Stoklund Olesen  {
1603c47690264abd6ec6bdeab86ce057e99bb5d39fe4Jakob Stoklund Olesen    NamedRegionTimer T("Emit Debug Info", TimerGroupName, TimePassesIsEnabled);
1604c47690264abd6ec6bdeab86ce057e99bb5d39fe4Jakob Stoklund Olesen    DebugVars->emitDebugValues(VRM);
1605c47690264abd6ec6bdeab86ce057e99bb5d39fe4Jakob Stoklund Olesen  }
1606cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen
1607cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  // The pass output is in VirtRegMap. Release all the transient data.
1608cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  releaseMemory();
1609cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
1610cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  return true;
1611cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen}
1612