PageRenderTime 65ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/3rd_party/llvm/lib/CodeGen/RegAllocLinearScan.cpp

https://code.google.com/p/softart/
C++ | 1543 lines | 1064 code | 197 blank | 282 comment | 308 complexity | d4371db70422068c05f461884bceb37d MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, JSON, MPL-2.0-no-copyleft-exception, GPL-2.0, GPL-3.0, LGPL-3.0, BSD-2-Clause
  1. //===-- RegAllocLinearScan.cpp - Linear Scan register allocator -----------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements a linear scan register allocator.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #define DEBUG_TYPE "regalloc"
  14. #include "LiveDebugVariables.h"
  15. #include "LiveRangeEdit.h"
  16. #include "VirtRegMap.h"
  17. #include "VirtRegRewriter.h"
  18. #include "RegisterClassInfo.h"
  19. #include "Spiller.h"
  20. #include "llvm/Analysis/AliasAnalysis.h"
  21. #include "llvm/Function.h"
  22. #include "llvm/CodeGen/CalcSpillWeights.h"
  23. #include "llvm/CodeGen/LiveIntervalAnalysis.h"
  24. #include "llvm/CodeGen/MachineFunctionPass.h"
  25. #include "llvm/CodeGen/MachineInstr.h"
  26. #include "llvm/CodeGen/MachineLoopInfo.h"
  27. #include "llvm/CodeGen/MachineRegisterInfo.h"
  28. #include "llvm/CodeGen/Passes.h"
  29. #include "llvm/CodeGen/RegAllocRegistry.h"
  30. #include "llvm/Target/TargetRegisterInfo.h"
  31. #include "llvm/Target/TargetMachine.h"
  32. #include "llvm/Target/TargetOptions.h"
  33. #include "llvm/Target/TargetInstrInfo.h"
  34. #include "llvm/ADT/EquivalenceClasses.h"
  35. #include "llvm/ADT/SmallSet.h"
  36. #include "llvm/ADT/Statistic.h"
  37. #include "llvm/ADT/STLExtras.h"
  38. #include "llvm/Support/Debug.h"
  39. #include "llvm/Support/ErrorHandling.h"
  40. #include "llvm/Support/raw_ostream.h"
  41. #include <algorithm>
  42. #include <queue>
  43. #include <memory>
  44. #include <cmath>
  45. using namespace llvm;
  46. STATISTIC(NumIters , "Number of iterations performed");
  47. STATISTIC(NumBacktracks, "Number of times we had to backtrack");
  48. STATISTIC(NumCoalesce, "Number of copies coalesced");
  49. STATISTIC(NumDowngrade, "Number of registers downgraded");
  50. static cl::opt<bool>
  51. NewHeuristic("new-spilling-heuristic",
  52. cl::desc("Use new spilling heuristic"),
  53. cl::init(false), cl::Hidden);
  54. static cl::opt<bool>
  55. TrivCoalesceEnds("trivial-coalesce-ends",
  56. cl::desc("Attempt trivial coalescing of interval ends"),
  57. cl::init(false), cl::Hidden);
  58. static cl::opt<bool>
  59. AvoidWAWHazard("avoid-waw-hazard",
  60. cl::desc("Avoid write-write hazards for some register classes"),
  61. cl::init(false), cl::Hidden);
  62. static RegisterRegAlloc
  63. linearscanRegAlloc("linearscan", "linear scan register allocator",
  64. createLinearScanRegisterAllocator);
  65. namespace {
  66. // When we allocate a register, add it to a fixed-size queue of
  67. // registers to skip in subsequent allocations. This trades a small
  68. // amount of register pressure and increased spills for flexibility in
  69. // the post-pass scheduler.
  70. //
  71. // Note that in a the number of registers used for reloading spills
  72. // will be one greater than the value of this option.
  73. //
  74. // One big limitation of this is that it doesn't differentiate between
  75. // different register classes. So on x86-64, if there is xmm register
  76. // pressure, it can caused fewer GPRs to be held in the queue.
  77. static cl::opt<unsigned>
  78. NumRecentlyUsedRegs("linearscan-skip-count",
  79. cl::desc("Number of registers for linearscan to remember"
  80. "to skip."),
  81. cl::init(0),
  82. cl::Hidden);
  83. struct RALinScan : public MachineFunctionPass {
  84. static char ID;
  85. RALinScan() : MachineFunctionPass(ID) {
  86. initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
  87. initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
  88. initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry());
  89. initializeRegisterCoalescerPass(
  90. *PassRegistry::getPassRegistry());
  91. initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
  92. initializeLiveStacksPass(*PassRegistry::getPassRegistry());
  93. initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
  94. initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
  95. initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
  96. initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
  97. // Initialize the queue to record recently-used registers.
  98. if (NumRecentlyUsedRegs > 0)
  99. RecentRegs.resize(NumRecentlyUsedRegs, 0);
  100. RecentNext = RecentRegs.begin();
  101. avoidWAW_ = 0;
  102. }
  103. typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr;
  104. typedef SmallVector<IntervalPtr, 32> IntervalPtrs;
  105. private:
  106. /// RelatedRegClasses - This structure is built the first time a function is
  107. /// compiled, and keeps track of which register classes have registers that
  108. /// belong to multiple classes or have aliases that are in other classes.
  109. EquivalenceClasses<const TargetRegisterClass*> RelatedRegClasses;
  110. DenseMap<unsigned, const TargetRegisterClass*> OneClassForEachPhysReg;
  111. // NextReloadMap - For each register in the map, it maps to the another
  112. // register which is defined by a reload from the same stack slot and
  113. // both reloads are in the same basic block.
  114. DenseMap<unsigned, unsigned> NextReloadMap;
  115. // DowngradedRegs - A set of registers which are being "downgraded", i.e.
  116. // un-favored for allocation.
  117. SmallSet<unsigned, 8> DowngradedRegs;
  118. // DowngradeMap - A map from virtual registers to physical registers being
  119. // downgraded for the virtual registers.
  120. DenseMap<unsigned, unsigned> DowngradeMap;
  121. MachineFunction* mf_;
  122. MachineRegisterInfo* mri_;
  123. const TargetMachine* tm_;
  124. const TargetRegisterInfo* tri_;
  125. const TargetInstrInfo* tii_;
  126. BitVector allocatableRegs_;
  127. BitVector reservedRegs_;
  128. LiveIntervals* li_;
  129. MachineLoopInfo *loopInfo;
  130. RegisterClassInfo RegClassInfo;
  131. /// handled_ - Intervals are added to the handled_ set in the order of their
  132. /// start value. This is uses for backtracking.
  133. std::vector<LiveInterval*> handled_;
  134. /// fixed_ - Intervals that correspond to machine registers.
  135. ///
  136. IntervalPtrs fixed_;
  137. /// active_ - Intervals that are currently being processed, and which have a
  138. /// live range active for the current point.
  139. IntervalPtrs active_;
  140. /// inactive_ - Intervals that are currently being processed, but which have
  141. /// a hold at the current point.
  142. IntervalPtrs inactive_;
  143. typedef std::priority_queue<LiveInterval*,
  144. SmallVector<LiveInterval*, 64>,
  145. greater_ptr<LiveInterval> > IntervalHeap;
  146. IntervalHeap unhandled_;
  147. /// regUse_ - Tracks register usage.
  148. SmallVector<unsigned, 32> regUse_;
  149. SmallVector<unsigned, 32> regUseBackUp_;
  150. /// vrm_ - Tracks register assignments.
  151. VirtRegMap* vrm_;
  152. std::auto_ptr<VirtRegRewriter> rewriter_;
  153. std::auto_ptr<Spiller> spiller_;
  154. // The queue of recently-used registers.
  155. SmallVector<unsigned, 4> RecentRegs;
  156. SmallVector<unsigned, 4>::iterator RecentNext;
  157. // Last write-after-write register written.
  158. unsigned avoidWAW_;
  159. // Record that we just picked this register.
  160. void recordRecentlyUsed(unsigned reg) {
  161. assert(reg != 0 && "Recently used register is NOREG!");
  162. if (!RecentRegs.empty()) {
  163. *RecentNext++ = reg;
  164. if (RecentNext == RecentRegs.end())
  165. RecentNext = RecentRegs.begin();
  166. }
  167. }
  168. public:
  169. virtual const char* getPassName() const {
  170. return "Linear Scan Register Allocator";
  171. }
  172. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  173. AU.setPreservesCFG();
  174. AU.addRequired<AliasAnalysis>();
  175. AU.addPreserved<AliasAnalysis>();
  176. AU.addRequired<LiveIntervals>();
  177. AU.addPreserved<SlotIndexes>();
  178. if (StrongPHIElim)
  179. AU.addRequiredID(StrongPHIEliminationID);
  180. // Make sure PassManager knows which analyses to make available
  181. // to coalescing and which analyses coalescing invalidates.
  182. AU.addRequiredTransitiveID(RegisterCoalescerPassID);
  183. AU.addRequired<CalculateSpillWeights>();
  184. AU.addRequiredID(LiveStacksID);
  185. AU.addPreservedID(LiveStacksID);
  186. AU.addRequired<MachineLoopInfo>();
  187. AU.addPreserved<MachineLoopInfo>();
  188. AU.addRequired<VirtRegMap>();
  189. AU.addPreserved<VirtRegMap>();
  190. AU.addRequired<LiveDebugVariables>();
  191. AU.addPreserved<LiveDebugVariables>();
  192. AU.addRequiredID(MachineDominatorsID);
  193. AU.addPreservedID(MachineDominatorsID);
  194. MachineFunctionPass::getAnalysisUsage(AU);
  195. }
  196. /// runOnMachineFunction - register allocate the whole function
  197. bool runOnMachineFunction(MachineFunction&);
  198. // Determine if we skip this register due to its being recently used.
  199. bool isRecentlyUsed(unsigned reg) const {
  200. return reg == avoidWAW_ ||
  201. std::find(RecentRegs.begin(), RecentRegs.end(), reg) != RecentRegs.end();
  202. }
  203. private:
  204. /// linearScan - the linear scan algorithm
  205. void linearScan();
  206. /// initIntervalSets - initialize the interval sets.
  207. ///
  208. void initIntervalSets();
  209. /// processActiveIntervals - expire old intervals and move non-overlapping
  210. /// ones to the inactive list.
  211. void processActiveIntervals(SlotIndex CurPoint);
  212. /// processInactiveIntervals - expire old intervals and move overlapping
  213. /// ones to the active list.
  214. void processInactiveIntervals(SlotIndex CurPoint);
  215. /// hasNextReloadInterval - Return the next liveinterval that's being
  216. /// defined by a reload from the same SS as the specified one.
  217. LiveInterval *hasNextReloadInterval(LiveInterval *cur);
  218. /// DowngradeRegister - Downgrade a register for allocation.
  219. void DowngradeRegister(LiveInterval *li, unsigned Reg);
  220. /// UpgradeRegister - Upgrade a register for allocation.
  221. void UpgradeRegister(unsigned Reg);
  222. /// assignRegOrStackSlotAtInterval - assign a register if one
  223. /// is available, or spill.
  224. void assignRegOrStackSlotAtInterval(LiveInterval* cur);
  225. void updateSpillWeights(std::vector<float> &Weights,
  226. unsigned reg, float weight,
  227. const TargetRegisterClass *RC);
  228. /// findIntervalsToSpill - Determine the intervals to spill for the
  229. /// specified interval. It's passed the physical registers whose spill
  230. /// weight is the lowest among all the registers whose live intervals
  231. /// conflict with the interval.
  232. void findIntervalsToSpill(LiveInterval *cur,
  233. std::vector<std::pair<unsigned,float> > &Candidates,
  234. unsigned NumCands,
  235. SmallVector<LiveInterval*, 8> &SpillIntervals);
  236. /// attemptTrivialCoalescing - If a simple interval is defined by a copy,
  237. /// try to allocate the definition to the same register as the source,
  238. /// if the register is not defined during the life time of the interval.
  239. /// This eliminates a copy, and is used to coalesce copies which were not
  240. /// coalesced away before allocation either due to dest and src being in
  241. /// different register classes or because the coalescer was overly
  242. /// conservative.
  243. unsigned attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg);
  244. ///
  245. /// Register usage / availability tracking helpers.
  246. ///
  247. void initRegUses() {
  248. regUse_.resize(tri_->getNumRegs(), 0);
  249. regUseBackUp_.resize(tri_->getNumRegs(), 0);
  250. }
  251. void finalizeRegUses() {
  252. #ifndef NDEBUG
  253. // Verify all the registers are "freed".
  254. bool Error = false;
  255. for (unsigned i = 0, e = tri_->getNumRegs(); i != e; ++i) {
  256. if (regUse_[i] != 0) {
  257. dbgs() << tri_->getName(i) << " is still in use!\n";
  258. Error = true;
  259. }
  260. }
  261. if (Error)
  262. llvm_unreachable(0);
  263. #endif
  264. regUse_.clear();
  265. regUseBackUp_.clear();
  266. }
  267. void addRegUse(unsigned physReg) {
  268. assert(TargetRegisterInfo::isPhysicalRegister(physReg) &&
  269. "should be physical register!");
  270. ++regUse_[physReg];
  271. for (const unsigned* as = tri_->getAliasSet(physReg); *as; ++as)
  272. ++regUse_[*as];
  273. }
  274. void delRegUse(unsigned physReg) {
  275. assert(TargetRegisterInfo::isPhysicalRegister(physReg) &&
  276. "should be physical register!");
  277. assert(regUse_[physReg] != 0);
  278. --regUse_[physReg];
  279. for (const unsigned* as = tri_->getAliasSet(physReg); *as; ++as) {
  280. assert(regUse_[*as] != 0);
  281. --regUse_[*as];
  282. }
  283. }
  284. bool isRegAvail(unsigned physReg) const {
  285. assert(TargetRegisterInfo::isPhysicalRegister(physReg) &&
  286. "should be physical register!");
  287. return regUse_[physReg] == 0;
  288. }
  289. void backUpRegUses() {
  290. regUseBackUp_ = regUse_;
  291. }
  292. void restoreRegUses() {
  293. regUse_ = regUseBackUp_;
  294. }
  295. ///
  296. /// Register handling helpers.
  297. ///
  298. /// getFreePhysReg - return a free physical register for this virtual
  299. /// register interval if we have one, otherwise return 0.
  300. unsigned getFreePhysReg(LiveInterval* cur);
  301. unsigned getFreePhysReg(LiveInterval* cur,
  302. const TargetRegisterClass *RC,
  303. unsigned MaxInactiveCount,
  304. SmallVector<unsigned, 256> &inactiveCounts,
  305. bool SkipDGRegs);
  306. /// getFirstNonReservedPhysReg - return the first non-reserved physical
  307. /// register in the register class.
  308. unsigned getFirstNonReservedPhysReg(const TargetRegisterClass *RC) {
  309. ArrayRef<unsigned> O = RegClassInfo.getOrder(RC);
  310. assert(!O.empty() && "All registers reserved?!");
  311. return O.front();
  312. }
  313. void ComputeRelatedRegClasses();
  314. template <typename ItTy>
  315. void printIntervals(const char* const str, ItTy i, ItTy e) const {
  316. DEBUG({
  317. if (str)
  318. dbgs() << str << " intervals:\n";
  319. for (; i != e; ++i) {
  320. dbgs() << '\t' << *i->first << " -> ";
  321. unsigned reg = i->first->reg;
  322. if (TargetRegisterInfo::isVirtualRegister(reg))
  323. reg = vrm_->getPhys(reg);
  324. dbgs() << tri_->getName(reg) << '\n';
  325. }
  326. });
  327. }
  328. };
  329. char RALinScan::ID = 0;
  330. }
  331. INITIALIZE_PASS_BEGIN(RALinScan, "linearscan-regalloc",
  332. "Linear Scan Register Allocator", false, false)
  333. INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
  334. INITIALIZE_PASS_DEPENDENCY(StrongPHIElimination)
  335. INITIALIZE_PASS_DEPENDENCY(CalculateSpillWeights)
  336. INITIALIZE_PASS_DEPENDENCY(LiveStacks)
  337. INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
  338. INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
  339. INITIALIZE_PASS_DEPENDENCY(RegisterCoalescer)
  340. INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
  341. INITIALIZE_PASS_END(RALinScan, "linearscan-regalloc",
  342. "Linear Scan Register Allocator", false, false)
  343. void RALinScan::ComputeRelatedRegClasses() {
  344. // First pass, add all reg classes to the union, and determine at least one
  345. // reg class that each register is in.
  346. bool HasAliases = false;
  347. for (TargetRegisterInfo::regclass_iterator RCI = tri_->regclass_begin(),
  348. E = tri_->regclass_end(); RCI != E; ++RCI) {
  349. RelatedRegClasses.insert(*RCI);
  350. for (TargetRegisterClass::iterator I = (*RCI)->begin(), E = (*RCI)->end();
  351. I != E; ++I) {
  352. HasAliases = HasAliases || *tri_->getAliasSet(*I) != 0;
  353. const TargetRegisterClass *&PRC = OneClassForEachPhysReg[*I];
  354. if (PRC) {
  355. // Already processed this register. Just make sure we know that
  356. // multiple register classes share a register.
  357. RelatedRegClasses.unionSets(PRC, *RCI);
  358. } else {
  359. PRC = *RCI;
  360. }
  361. }
  362. }
  363. // Second pass, now that we know conservatively what register classes each reg
  364. // belongs to, add info about aliases. We don't need to do this for targets
  365. // without register aliases.
  366. if (HasAliases)
  367. for (DenseMap<unsigned, const TargetRegisterClass*>::iterator
  368. I = OneClassForEachPhysReg.begin(), E = OneClassForEachPhysReg.end();
  369. I != E; ++I)
  370. for (const unsigned *AS = tri_->getAliasSet(I->first); *AS; ++AS) {
  371. const TargetRegisterClass *AliasClass =
  372. OneClassForEachPhysReg.lookup(*AS);
  373. if (AliasClass)
  374. RelatedRegClasses.unionSets(I->second, AliasClass);
  375. }
  376. }
  377. /// attemptTrivialCoalescing - If a simple interval is defined by a copy, try
  378. /// allocate the definition the same register as the source register if the
  379. /// register is not defined during live time of the interval. If the interval is
  380. /// killed by a copy, try to use the destination register. This eliminates a
  381. /// copy. This is used to coalesce copies which were not coalesced away before
  382. /// allocation either due to dest and src being in different register classes or
  383. /// because the coalescer was overly conservative.
  384. unsigned RALinScan::attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg) {
  385. unsigned Preference = vrm_->getRegAllocPref(cur.reg);
  386. if ((Preference && Preference == Reg) || !cur.containsOneValue())
  387. return Reg;
  388. // We cannot handle complicated live ranges. Simple linear stuff only.
  389. if (cur.ranges.size() != 1)
  390. return Reg;
  391. const LiveRange &range = cur.ranges.front();
  392. VNInfo *vni = range.valno;
  393. if (vni->isUnused() || !vni->def.isValid())
  394. return Reg;
  395. unsigned CandReg;
  396. {
  397. MachineInstr *CopyMI;
  398. if ((CopyMI = li_->getInstructionFromIndex(vni->def)) && CopyMI->isCopy())
  399. // Defined by a copy, try to extend SrcReg forward
  400. CandReg = CopyMI->getOperand(1).getReg();
  401. else if (TrivCoalesceEnds &&
  402. (CopyMI = li_->getInstructionFromIndex(range.end.getBaseIndex())) &&
  403. CopyMI->isCopy() && cur.reg == CopyMI->getOperand(1).getReg())
  404. // Only used by a copy, try to extend DstReg backwards
  405. CandReg = CopyMI->getOperand(0).getReg();
  406. else
  407. return Reg;
  408. // If the target of the copy is a sub-register then don't coalesce.
  409. if(CopyMI->getOperand(0).getSubReg())
  410. return Reg;
  411. }
  412. if (TargetRegisterInfo::isVirtualRegister(CandReg)) {
  413. if (!vrm_->isAssignedReg(CandReg))
  414. return Reg;
  415. CandReg = vrm_->getPhys(CandReg);
  416. }
  417. if (Reg == CandReg)
  418. return Reg;
  419. const TargetRegisterClass *RC = mri_->getRegClass(cur.reg);
  420. if (!RC->contains(CandReg))
  421. return Reg;
  422. if (li_->conflictsWithPhysReg(cur, *vrm_, CandReg))
  423. return Reg;
  424. // Try to coalesce.
  425. DEBUG(dbgs() << "Coalescing: " << cur << " -> " << tri_->getName(CandReg)
  426. << '\n');
  427. vrm_->clearVirt(cur.reg);
  428. vrm_->assignVirt2Phys(cur.reg, CandReg);
  429. ++NumCoalesce;
  430. return CandReg;
  431. }
  432. bool RALinScan::runOnMachineFunction(MachineFunction &fn) {
  433. mf_ = &fn;
  434. mri_ = &fn.getRegInfo();
  435. tm_ = &fn.getTarget();
  436. tri_ = tm_->getRegisterInfo();
  437. tii_ = tm_->getInstrInfo();
  438. allocatableRegs_ = tri_->getAllocatableSet(fn);
  439. reservedRegs_ = tri_->getReservedRegs(fn);
  440. li_ = &getAnalysis<LiveIntervals>();
  441. loopInfo = &getAnalysis<MachineLoopInfo>();
  442. RegClassInfo.runOnMachineFunction(fn);
  443. // We don't run the coalescer here because we have no reason to
  444. // interact with it. If the coalescer requires interaction, it
  445. // won't do anything. If it doesn't require interaction, we assume
  446. // it was run as a separate pass.
  447. // If this is the first function compiled, compute the related reg classes.
  448. if (RelatedRegClasses.empty())
  449. ComputeRelatedRegClasses();
  450. // Also resize register usage trackers.
  451. initRegUses();
  452. vrm_ = &getAnalysis<VirtRegMap>();
  453. if (!rewriter_.get()) rewriter_.reset(createVirtRegRewriter());
  454. spiller_.reset(createSpiller(*this, *mf_, *vrm_));
  455. initIntervalSets();
  456. linearScan();
  457. // Rewrite spill code and update the PhysRegsUsed set.
  458. rewriter_->runOnMachineFunction(*mf_, *vrm_, li_);
  459. // Write out new DBG_VALUE instructions.
  460. getAnalysis<LiveDebugVariables>().emitDebugValues(vrm_);
  461. assert(unhandled_.empty() && "Unhandled live intervals remain!");
  462. finalizeRegUses();
  463. fixed_.clear();
  464. active_.clear();
  465. inactive_.clear();
  466. handled_.clear();
  467. NextReloadMap.clear();
  468. DowngradedRegs.clear();
  469. DowngradeMap.clear();
  470. spiller_.reset(0);
  471. return true;
  472. }
  473. /// initIntervalSets - initialize the interval sets.
  474. ///
  475. void RALinScan::initIntervalSets()
  476. {
  477. assert(unhandled_.empty() && fixed_.empty() &&
  478. active_.empty() && inactive_.empty() &&
  479. "interval sets should be empty on initialization");
  480. handled_.reserve(li_->getNumIntervals());
  481. for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
  482. if (TargetRegisterInfo::isPhysicalRegister(i->second->reg)) {
  483. if (!i->second->empty() && allocatableRegs_.test(i->second->reg)) {
  484. mri_->setPhysRegUsed(i->second->reg);
  485. fixed_.push_back(std::make_pair(i->second, i->second->begin()));
  486. }
  487. } else {
  488. if (i->second->empty()) {
  489. assignRegOrStackSlotAtInterval(i->second);
  490. }
  491. else
  492. unhandled_.push(i->second);
  493. }
  494. }
  495. }
  496. void RALinScan::linearScan() {
  497. // linear scan algorithm
  498. DEBUG({
  499. dbgs() << "********** LINEAR SCAN **********\n"
  500. << "********** Function: "
  501. << mf_->getFunction()->getName() << '\n';
  502. printIntervals("fixed", fixed_.begin(), fixed_.end());
  503. });
  504. while (!unhandled_.empty()) {
  505. // pick the interval with the earliest start point
  506. LiveInterval* cur = unhandled_.top();
  507. unhandled_.pop();
  508. ++NumIters;
  509. DEBUG(dbgs() << "\n*** CURRENT ***: " << *cur << '\n');
  510. assert(!cur->empty() && "Empty interval in unhandled set.");
  511. processActiveIntervals(cur->beginIndex());
  512. processInactiveIntervals(cur->beginIndex());
  513. assert(TargetRegisterInfo::isVirtualRegister(cur->reg) &&
  514. "Can only allocate virtual registers!");
  515. // Allocating a virtual register. try to find a free
  516. // physical register or spill an interval (possibly this one) in order to
  517. // assign it one.
  518. assignRegOrStackSlotAtInterval(cur);
  519. DEBUG({
  520. printIntervals("active", active_.begin(), active_.end());
  521. printIntervals("inactive", inactive_.begin(), inactive_.end());
  522. });
  523. }
  524. // Expire any remaining active intervals
  525. while (!active_.empty()) {
  526. IntervalPtr &IP = active_.back();
  527. unsigned reg = IP.first->reg;
  528. DEBUG(dbgs() << "\tinterval " << *IP.first << " expired\n");
  529. assert(TargetRegisterInfo::isVirtualRegister(reg) &&
  530. "Can only allocate virtual registers!");
  531. reg = vrm_->getPhys(reg);
  532. delRegUse(reg);
  533. active_.pop_back();
  534. }
  535. // Expire any remaining inactive intervals
  536. DEBUG({
  537. for (IntervalPtrs::reverse_iterator
  538. i = inactive_.rbegin(); i != inactive_.rend(); ++i)
  539. dbgs() << "\tinterval " << *i->first << " expired\n";
  540. });
  541. inactive_.clear();
  542. // Add live-ins to every BB except for entry. Also perform trivial coalescing.
  543. MachineFunction::iterator EntryMBB = mf_->begin();
  544. SmallVector<MachineBasicBlock*, 8> LiveInMBBs;
  545. for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
  546. LiveInterval &cur = *i->second;
  547. unsigned Reg = 0;
  548. bool isPhys = TargetRegisterInfo::isPhysicalRegister(cur.reg);
  549. if (isPhys)
  550. Reg = cur.reg;
  551. else if (vrm_->isAssignedReg(cur.reg))
  552. Reg = attemptTrivialCoalescing(cur, vrm_->getPhys(cur.reg));
  553. if (!Reg)
  554. continue;
  555. // Ignore splited live intervals.
  556. if (!isPhys && vrm_->getPreSplitReg(cur.reg))
  557. continue;
  558. for (LiveInterval::Ranges::const_iterator I = cur.begin(), E = cur.end();
  559. I != E; ++I) {
  560. const LiveRange &LR = *I;
  561. if (li_->findLiveInMBBs(LR.start, LR.end, LiveInMBBs)) {
  562. for (unsigned i = 0, e = LiveInMBBs.size(); i != e; ++i)
  563. if (LiveInMBBs[i] != EntryMBB) {
  564. assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
  565. "Adding a virtual register to livein set?");
  566. LiveInMBBs[i]->addLiveIn(Reg);
  567. }
  568. LiveInMBBs.clear();
  569. }
  570. }
  571. }
  572. DEBUG(dbgs() << *vrm_);
  573. // Look for physical registers that end up not being allocated even though
  574. // register allocator had to spill other registers in its register class.
  575. if (!vrm_->FindUnusedRegisters(li_))
  576. return;
  577. }
  578. /// processActiveIntervals - expire old intervals and move non-overlapping ones
  579. /// to the inactive list.
  580. void RALinScan::processActiveIntervals(SlotIndex CurPoint)
  581. {
  582. DEBUG(dbgs() << "\tprocessing active intervals:\n");
  583. for (unsigned i = 0, e = active_.size(); i != e; ++i) {
  584. LiveInterval *Interval = active_[i].first;
  585. LiveInterval::iterator IntervalPos = active_[i].second;
  586. unsigned reg = Interval->reg;
  587. IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
  588. if (IntervalPos == Interval->end()) { // Remove expired intervals.
  589. DEBUG(dbgs() << "\t\tinterval " << *Interval << " expired\n");
  590. assert(TargetRegisterInfo::isVirtualRegister(reg) &&
  591. "Can only allocate virtual registers!");
  592. reg = vrm_->getPhys(reg);
  593. delRegUse(reg);
  594. // Pop off the end of the list.
  595. active_[i] = active_.back();
  596. active_.pop_back();
  597. --i; --e;
  598. } else if (IntervalPos->start > CurPoint) {
  599. // Move inactive intervals to inactive list.
  600. DEBUG(dbgs() << "\t\tinterval " << *Interval << " inactive\n");
  601. assert(TargetRegisterInfo::isVirtualRegister(reg) &&
  602. "Can only allocate virtual registers!");
  603. reg = vrm_->getPhys(reg);
  604. delRegUse(reg);
  605. // add to inactive.
  606. inactive_.push_back(std::make_pair(Interval, IntervalPos));
  607. // Pop off the end of the list.
  608. active_[i] = active_.back();
  609. active_.pop_back();
  610. --i; --e;
  611. } else {
  612. // Otherwise, just update the iterator position.
  613. active_[i].second = IntervalPos;
  614. }
  615. }
  616. }
  617. /// processInactiveIntervals - expire old intervals and move overlapping
  618. /// ones to the active list.
  619. void RALinScan::processInactiveIntervals(SlotIndex CurPoint)
  620. {
  621. DEBUG(dbgs() << "\tprocessing inactive intervals:\n");
  622. for (unsigned i = 0, e = inactive_.size(); i != e; ++i) {
  623. LiveInterval *Interval = inactive_[i].first;
  624. LiveInterval::iterator IntervalPos = inactive_[i].second;
  625. unsigned reg = Interval->reg;
  626. IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
  627. if (IntervalPos == Interval->end()) { // remove expired intervals.
  628. DEBUG(dbgs() << "\t\tinterval " << *Interval << " expired\n");
  629. // Pop off the end of the list.
  630. inactive_[i] = inactive_.back();
  631. inactive_.pop_back();
  632. --i; --e;
  633. } else if (IntervalPos->start <= CurPoint) {
  634. // move re-activated intervals in active list
  635. DEBUG(dbgs() << "\t\tinterval " << *Interval << " active\n");
  636. assert(TargetRegisterInfo::isVirtualRegister(reg) &&
  637. "Can only allocate virtual registers!");
  638. reg = vrm_->getPhys(reg);
  639. addRegUse(reg);
  640. // add to active
  641. active_.push_back(std::make_pair(Interval, IntervalPos));
  642. // Pop off the end of the list.
  643. inactive_[i] = inactive_.back();
  644. inactive_.pop_back();
  645. --i; --e;
  646. } else {
  647. // Otherwise, just update the iterator position.
  648. inactive_[i].second = IntervalPos;
  649. }
  650. }
  651. }
  652. /// updateSpillWeights - updates the spill weights of the specifed physical
  653. /// register and its weight.
  654. void RALinScan::updateSpillWeights(std::vector<float> &Weights,
  655. unsigned reg, float weight,
  656. const TargetRegisterClass *RC) {
  657. SmallSet<unsigned, 4> Processed;
  658. SmallSet<unsigned, 4> SuperAdded;
  659. SmallVector<unsigned, 4> Supers;
  660. Weights[reg] += weight;
  661. Processed.insert(reg);
  662. for (const unsigned* as = tri_->getAliasSet(reg); *as; ++as) {
  663. Weights[*as] += weight;
  664. Processed.insert(*as);
  665. if (tri_->isSubRegister(*as, reg) &&
  666. SuperAdded.insert(*as) &&
  667. RC->contains(*as)) {
  668. Supers.push_back(*as);
  669. }
  670. }
  671. // If the alias is a super-register, and the super-register is in the
  672. // register class we are trying to allocate. Then add the weight to all
  673. // sub-registers of the super-register even if they are not aliases.
  674. // e.g. allocating for GR32, bh is not used, updating bl spill weight.
  675. // bl should get the same spill weight otherwise it will be chosen
  676. // as a spill candidate since spilling bh doesn't make ebx available.
  677. for (unsigned i = 0, e = Supers.size(); i != e; ++i) {
  678. for (const unsigned *sr = tri_->getSubRegisters(Supers[i]); *sr; ++sr)
  679. if (!Processed.count(*sr))
  680. Weights[*sr] += weight;
  681. }
  682. }
  683. static
  684. RALinScan::IntervalPtrs::iterator
  685. FindIntervalInVector(RALinScan::IntervalPtrs &IP, LiveInterval *LI) {
  686. for (RALinScan::IntervalPtrs::iterator I = IP.begin(), E = IP.end();
  687. I != E; ++I)
  688. if (I->first == LI) return I;
  689. return IP.end();
  690. }
  691. static void RevertVectorIteratorsTo(RALinScan::IntervalPtrs &V,
  692. SlotIndex Point){
  693. for (unsigned i = 0, e = V.size(); i != e; ++i) {
  694. RALinScan::IntervalPtr &IP = V[i];
  695. LiveInterval::iterator I = std::upper_bound(IP.first->begin(),
  696. IP.second, Point);
  697. if (I != IP.first->begin()) --I;
  698. IP.second = I;
  699. }
  700. }
  701. /// getConflictWeight - Return the number of conflicts between cur
  702. /// live interval and defs and uses of Reg weighted by loop depthes.
  703. static
  704. float getConflictWeight(LiveInterval *cur, unsigned Reg, LiveIntervals *li_,
  705. MachineRegisterInfo *mri_,
  706. MachineLoopInfo *loopInfo) {
  707. float Conflicts = 0;
  708. for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(Reg),
  709. E = mri_->reg_end(); I != E; ++I) {
  710. MachineInstr *MI = &*I;
  711. if (cur->liveAt(li_->getInstructionIndex(MI))) {
  712. unsigned loopDepth = loopInfo->getLoopDepth(MI->getParent());
  713. Conflicts += std::pow(10.0f, (float)loopDepth);
  714. }
  715. }
  716. return Conflicts;
  717. }
  718. /// findIntervalsToSpill - Determine the intervals to spill for the
  719. /// specified interval. It's passed the physical registers whose spill
  720. /// weight is the lowest among all the registers whose live intervals
  721. /// conflict with the interval.
  722. void RALinScan::findIntervalsToSpill(LiveInterval *cur,
  723. std::vector<std::pair<unsigned,float> > &Candidates,
  724. unsigned NumCands,
  725. SmallVector<LiveInterval*, 8> &SpillIntervals) {
  726. // We have figured out the *best* register to spill. But there are other
  727. // registers that are pretty good as well (spill weight within 3%). Spill
  728. // the one that has fewest defs and uses that conflict with cur.
  729. float Conflicts[3] = { 0.0f, 0.0f, 0.0f };
  730. SmallVector<LiveInterval*, 8> SLIs[3];
  731. DEBUG({
  732. dbgs() << "\tConsidering " << NumCands << " candidates: ";
  733. for (unsigned i = 0; i != NumCands; ++i)
  734. dbgs() << tri_->getName(Candidates[i].first) << " ";
  735. dbgs() << "\n";
  736. });
  737. // Calculate the number of conflicts of each candidate.
  738. for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
  739. unsigned Reg = i->first->reg;
  740. unsigned PhysReg = vrm_->getPhys(Reg);
  741. if (!cur->overlapsFrom(*i->first, i->second))
  742. continue;
  743. for (unsigned j = 0; j < NumCands; ++j) {
  744. unsigned Candidate = Candidates[j].first;
  745. if (tri_->regsOverlap(PhysReg, Candidate)) {
  746. if (NumCands > 1)
  747. Conflicts[j] += getConflictWeight(cur, Reg, li_, mri_, loopInfo);
  748. SLIs[j].push_back(i->first);
  749. }
  750. }
  751. }
  752. for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){
  753. unsigned Reg = i->first->reg;
  754. unsigned PhysReg = vrm_->getPhys(Reg);
  755. if (!cur->overlapsFrom(*i->first, i->second-1))
  756. continue;
  757. for (unsigned j = 0; j < NumCands; ++j) {
  758. unsigned Candidate = Candidates[j].first;
  759. if (tri_->regsOverlap(PhysReg, Candidate)) {
  760. if (NumCands > 1)
  761. Conflicts[j] += getConflictWeight(cur, Reg, li_, mri_, loopInfo);
  762. SLIs[j].push_back(i->first);
  763. }
  764. }
  765. }
  766. // Which is the best candidate?
  767. unsigned BestCandidate = 0;
  768. float MinConflicts = Conflicts[0];
  769. for (unsigned i = 1; i != NumCands; ++i) {
  770. if (Conflicts[i] < MinConflicts) {
  771. BestCandidate = i;
  772. MinConflicts = Conflicts[i];
  773. }
  774. }
  775. std::copy(SLIs[BestCandidate].begin(), SLIs[BestCandidate].end(),
  776. std::back_inserter(SpillIntervals));
  777. }
  778. namespace {
  779. struct WeightCompare {
  780. private:
  781. const RALinScan &Allocator;
  782. public:
  783. WeightCompare(const RALinScan &Alloc) : Allocator(Alloc) {}
  784. typedef std::pair<unsigned, float> RegWeightPair;
  785. bool operator()(const RegWeightPair &LHS, const RegWeightPair &RHS) const {
  786. return LHS.second < RHS.second && !Allocator.isRecentlyUsed(LHS.first);
  787. }
  788. };
  789. }
  790. static bool weightsAreClose(float w1, float w2) {
  791. if (!NewHeuristic)
  792. return false;
  793. float diff = w1 - w2;
  794. if (diff <= 0.02f) // Within 0.02f
  795. return true;
  796. return (diff / w2) <= 0.05f; // Within 5%.
  797. }
  798. LiveInterval *RALinScan::hasNextReloadInterval(LiveInterval *cur) {
  799. DenseMap<unsigned, unsigned>::iterator I = NextReloadMap.find(cur->reg);
  800. if (I == NextReloadMap.end())
  801. return 0;
  802. return &li_->getInterval(I->second);
  803. }
  804. void RALinScan::DowngradeRegister(LiveInterval *li, unsigned Reg) {
  805. for (const unsigned *AS = tri_->getOverlaps(Reg); *AS; ++AS) {
  806. bool isNew = DowngradedRegs.insert(*AS);
  807. (void)isNew; // Silence compiler warning.
  808. assert(isNew && "Multiple reloads holding the same register?");
  809. DowngradeMap.insert(std::make_pair(li->reg, *AS));
  810. }
  811. ++NumDowngrade;
  812. }
  813. void RALinScan::UpgradeRegister(unsigned Reg) {
  814. if (Reg) {
  815. DowngradedRegs.erase(Reg);
  816. for (const unsigned *AS = tri_->getAliasSet(Reg); *AS; ++AS)
  817. DowngradedRegs.erase(*AS);
  818. }
  819. }
  820. namespace {
  821. struct LISorter {
  822. bool operator()(LiveInterval* A, LiveInterval* B) {
  823. return A->beginIndex() < B->beginIndex();
  824. }
  825. };
  826. }
  827. /// assignRegOrStackSlotAtInterval - assign a register if one is available, or
  828. /// spill.
  829. void RALinScan::assignRegOrStackSlotAtInterval(LiveInterval* cur) {
  830. const TargetRegisterClass *RC = mri_->getRegClass(cur->reg);
  831. DEBUG(dbgs() << "\tallocating current interval from "
  832. << RC->getName() << ": ");
  833. // This is an implicitly defined live interval, just assign any register.
  834. if (cur->empty()) {
  835. unsigned physReg = vrm_->getRegAllocPref(cur->reg);
  836. if (!physReg)
  837. physReg = getFirstNonReservedPhysReg(RC);
  838. DEBUG(dbgs() << tri_->getName(physReg) << '\n');
  839. // Note the register is not really in use.
  840. vrm_->assignVirt2Phys(cur->reg, physReg);
  841. return;
  842. }
  843. backUpRegUses();
  844. std::vector<std::pair<unsigned, float> > SpillWeightsToAdd;
  845. SlotIndex StartPosition = cur->beginIndex();
  846. const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
  847. // If start of this live interval is defined by a move instruction and its
  848. // source is assigned a physical register that is compatible with the target
  849. // register class, then we should try to assign it the same register.
  850. // This can happen when the move is from a larger register class to a smaller
  851. // one, e.g. X86::mov32to32_. These move instructions are not coalescable.
  852. if (!vrm_->getRegAllocPref(cur->reg) && cur->hasAtLeastOneValue()) {
  853. VNInfo *vni = cur->begin()->valno;
  854. if (!vni->isUnused() && vni->def.isValid()) {
  855. MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def);
  856. if (CopyMI && CopyMI->isCopy()) {
  857. unsigned DstSubReg = CopyMI->getOperand(0).getSubReg();
  858. unsigned SrcReg = CopyMI->getOperand(1).getReg();
  859. unsigned SrcSubReg = CopyMI->getOperand(1).getSubReg();
  860. unsigned Reg = 0;
  861. if (TargetRegisterInfo::isPhysicalRegister(SrcReg))
  862. Reg = SrcReg;
  863. else if (vrm_->isAssignedReg(SrcReg))
  864. Reg = vrm_->getPhys(SrcReg);
  865. if (Reg) {
  866. if (SrcSubReg)
  867. Reg = tri_->getSubReg(Reg, SrcSubReg);
  868. if (DstSubReg)
  869. Reg = tri_->getMatchingSuperReg(Reg, DstSubReg, RC);
  870. if (Reg && allocatableRegs_[Reg] && RC->contains(Reg))
  871. mri_->setRegAllocationHint(cur->reg, 0, Reg);
  872. }
  873. }
  874. }
  875. }
  876. // For every interval in inactive we overlap with, mark the
  877. // register as not free and update spill weights.
  878. for (IntervalPtrs::const_iterator i = inactive_.begin(),
  879. e = inactive_.end(); i != e; ++i) {
  880. unsigned Reg = i->first->reg;
  881. assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
  882. "Can only allocate virtual registers!");
  883. const TargetRegisterClass *RegRC = mri_->getRegClass(Reg);
  884. // If this is not in a related reg class to the register we're allocating,
  885. // don't check it.
  886. if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
  887. cur->overlapsFrom(*i->first, i->second-1)) {
  888. Reg = vrm_->getPhys(Reg);
  889. addRegUse(Reg);
  890. SpillWeightsToAdd.push_back(std::make_pair(Reg, i->first->weight));
  891. }
  892. }
  893. // Speculatively check to see if we can get a register right now. If not,
  894. // we know we won't be able to by adding more constraints. If so, we can
  895. // check to see if it is valid. Doing an exhaustive search of the fixed_ list
  896. // is very bad (it contains all callee clobbered registers for any functions
  897. // with a call), so we want to avoid doing that if possible.
  898. unsigned physReg = getFreePhysReg(cur);
  899. unsigned BestPhysReg = physReg;
  900. if (physReg) {
  901. // We got a register. However, if it's in the fixed_ list, we might
  902. // conflict with it. Check to see if we conflict with it or any of its
  903. // aliases.
  904. SmallSet<unsigned, 8> RegAliases;
  905. for (const unsigned *AS = tri_->getAliasSet(physReg); *AS; ++AS)
  906. RegAliases.insert(*AS);
  907. bool ConflictsWithFixed = false;
  908. for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
  909. IntervalPtr &IP = fixed_[i];
  910. if (physReg == IP.first->reg || RegAliases.count(IP.first->reg)) {
  911. // Okay, this reg is on the fixed list. Check to see if we actually
  912. // conflict.
  913. LiveInterval *I = IP.first;
  914. if (I->endIndex() > StartPosition) {
  915. LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
  916. IP.second = II;
  917. if (II != I->begin() && II->start > StartPosition)
  918. --II;
  919. if (cur->overlapsFrom(*I, II)) {
  920. ConflictsWithFixed = true;
  921. break;
  922. }
  923. }
  924. }
  925. }
  926. // Okay, the register picked by our speculative getFreePhysReg call turned
  927. // out to be in use. Actually add all of the conflicting fixed registers to
  928. // regUse_ so we can do an accurate query.
  929. if (ConflictsWithFixed) {
  930. // For every interval in fixed we overlap with, mark the register as not
  931. // free and update spill weights.
  932. for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
  933. IntervalPtr &IP = fixed_[i];
  934. LiveInterval *I = IP.first;
  935. const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg];
  936. if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
  937. I->endIndex() > StartPosition) {
  938. LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
  939. IP.second = II;
  940. if (II != I->begin() && II->start > StartPosition)
  941. --II;
  942. if (cur->overlapsFrom(*I, II)) {
  943. unsigned reg = I->reg;
  944. addRegUse(reg);
  945. SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight));
  946. }
  947. }
  948. }
  949. // Using the newly updated regUse_ object, which includes conflicts in the
  950. // future, see if there are any registers available.
  951. physReg = getFreePhysReg(cur);
  952. }
  953. }
  954. // Restore the physical register tracker, removing information about the
  955. // future.
  956. restoreRegUses();
  957. // If we find a free register, we are done: assign this virtual to
  958. // the free physical register and add this interval to the active
  959. // list.
  960. if (physReg) {
  961. DEBUG(dbgs() << tri_->getName(physReg) << '\n');
  962. assert(RC->contains(physReg) && "Invalid candidate");
  963. vrm_->assignVirt2Phys(cur->reg, physReg);
  964. addRegUse(physReg);
  965. active_.push_back(std::make_pair(cur, cur->begin()));
  966. handled_.push_back(cur);
  967. // Remember physReg for avoiding a write-after-write hazard in the next
  968. // instruction.
  969. if (AvoidWAWHazard &&
  970. tri_->avoidWriteAfterWrite(mri_->getRegClass(cur->reg)))
  971. avoidWAW_ = physReg;
  972. // "Upgrade" the physical register since it has been allocated.
  973. UpgradeRegister(physReg);
  974. if (LiveInterval *NextReloadLI = hasNextReloadInterval(cur)) {
  975. // "Downgrade" physReg to try to keep physReg from being allocated until
  976. // the next reload from the same SS is allocated.
  977. mri_->setRegAllocationHint(NextReloadLI->reg, 0, physReg);
  978. DowngradeRegister(cur, physReg);
  979. }
  980. return;
  981. }
  982. DEBUG(dbgs() << "no free registers\n");
  983. // Compile the spill weights into an array that is better for scanning.
  984. std::vector<float> SpillWeights(tri_->getNumRegs(), 0.0f);
  985. for (std::vector<std::pair<unsigned, float> >::iterator
  986. I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I)
  987. updateSpillWeights(SpillWeights, I->first, I->second, RC);
  988. // for each interval in active, update spill weights.
  989. for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
  990. i != e; ++i) {
  991. unsigned reg = i->first->reg;
  992. assert(TargetRegisterInfo::isVirtualRegister(reg) &&
  993. "Can only allocate virtual registers!");
  994. reg = vrm_->getPhys(reg);
  995. updateSpillWeights(SpillWeights, reg, i->first->weight, RC);
  996. }
  997. DEBUG(dbgs() << "\tassigning stack slot at interval "<< *cur << ":\n");
  998. // Find a register to spill.
  999. float minWeight = HUGE_VALF;
  1000. unsigned minReg = 0;
  1001. bool Found = false;
  1002. std::vector<std::pair<unsigned,float> > RegsWeights;
  1003. ArrayRef<unsigned> Order = RegClassInfo.getOrder(RC);
  1004. if (!minReg || SpillWeights[minReg] == HUGE_VALF)
  1005. for (unsigned i = 0; i != Order.size(); ++i) {
  1006. unsigned reg = Order[i];
  1007. float regWeight = SpillWeights[reg];
  1008. // Skip recently allocated registers and reserved registers.
  1009. if (minWeight > regWeight && !isRecentlyUsed(reg))
  1010. Found = true;
  1011. RegsWeights.push_back(std::make_pair(reg, regWeight));
  1012. }
  1013. // If we didn't find a register that is spillable, try aliases?
  1014. if (!Found) {
  1015. for (unsigned i = 0; i != Order.size(); ++i) {
  1016. unsigned reg = Order[i];
  1017. // No need to worry about if the alias register size < regsize of RC.
  1018. // We are going to spill all registers that alias it anyway.
  1019. for (const unsigned* as = tri_->getAliasSet(reg); *as; ++as)
  1020. RegsWeights.push_back(std::make_pair(*as, SpillWeights[*as]));
  1021. }
  1022. }
  1023. // Sort all potential spill candidates by weight.
  1024. std::sort(RegsWeights.begin(), RegsWeights.end(), WeightCompare(*this));
  1025. minReg = RegsWeights[0].first;
  1026. minWeight = RegsWeights[0].second;
  1027. if (minWeight == HUGE_VALF) {
  1028. // All registers must have inf weight. Just grab one!
  1029. minReg = BestPhysReg ? BestPhysReg : getFirstNonReservedPhysReg(RC);
  1030. if (cur->weight == HUGE_VALF ||
  1031. li_->getApproximateInstructionCount(*cur) == 0) {
  1032. // Spill a physical register around defs and uses.
  1033. if (li_->spillPhysRegAroundRegDefsUses(*cur, minReg, *vrm_)) {
  1034. // spillPhysRegAroundRegDefsUses may have invalidated iterator stored
  1035. // in fixed_. Reset them.
  1036. for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
  1037. IntervalPtr &IP = fixed_[i];
  1038. LiveInterval *I = IP.first;
  1039. if (I->reg == minReg || tri_->isSubRegister(minReg, I->reg))
  1040. IP.second = I->advanceTo(I->begin(), StartPosition);
  1041. }
  1042. DowngradedRegs.clear();
  1043. assignRegOrStackSlotAtInterval(cur);
  1044. } else {
  1045. assert(false && "Ran out of registers during register allocation!");
  1046. report_fatal_error("Ran out of registers during register allocation!");
  1047. }
  1048. return;
  1049. }
  1050. }
  1051. // Find up to 3 registers to consider as spill candidates.
  1052. unsigned LastCandidate = RegsWeights.size() >= 3 ? 3 : 1;
  1053. while (LastCandidate > 1) {
  1054. if (weightsAreClose(RegsWeights[LastCandidate-1].second, minWeight))
  1055. break;
  1056. --LastCandidate;
  1057. }
  1058. DEBUG({
  1059. dbgs() << "\t\tregister(s) with min weight(s): ";
  1060. for (unsigned i = 0; i != LastCandidate; ++i)
  1061. dbgs() << tri_->getName(RegsWeights[i].first)
  1062. << " (" << RegsWeights[i].second << ")\n";
  1063. });
  1064. // If the current has the minimum weight, we need to spill it and
  1065. // add any added intervals back to unhandled, and restart
  1066. // linearscan.
  1067. if (cur->weight != HUGE_VALF && cur->weight <= minWeight) {
  1068. DEBUG(dbgs() << "\t\t\tspilling(c): " << *cur << '\n');
  1069. SmallVector<LiveInterval*, 8> added;
  1070. LiveRangeEdit LRE(*cur, added);
  1071. spiller_->spill(LRE);
  1072. std::sort(added.begin(), added.end(), LISorter());
  1073. if (added.empty())
  1074. return; // Early exit if all spills were folded.
  1075. // Merge added with unhandled. Note that we have already sorted
  1076. // intervals returned by addIntervalsForSpills by their starting
  1077. // point.
  1078. // This also update the NextReloadMap. That is, it adds mapping from a
  1079. // register defined by a reload from SS to the next reload from SS in the
  1080. // same basic block.
  1081. MachineBasicBlock *LastReloadMBB = 0;
  1082. LiveInterval *LastReload = 0;
  1083. int LastReloadSS = VirtRegMap::NO_STACK_SLOT;
  1084. for (unsigned i = 0, e = added.size(); i != e; ++i) {
  1085. LiveInterval *ReloadLi = added[i];
  1086. if (ReloadLi->weight == HUGE_VALF &&
  1087. li_->getApproximateInstructionCount(*ReloadLi) == 0) {
  1088. SlotIndex ReloadIdx = ReloadLi->beginIndex();
  1089. MachineBasicBlock *ReloadMBB = li_->getMBBFromIndex(ReloadIdx);
  1090. int ReloadSS = vrm_->getStackSlot(ReloadLi->reg);
  1091. if (LastReloadMBB == ReloadMBB && LastReloadSS == ReloadSS) {
  1092. // Last reload of same SS is in the same MBB. We want to try to
  1093. // allocate both reloads the same register and make sure the reg
  1094. // isn't clobbered in between if at all possible.
  1095. assert(LastReload->beginIndex() < ReloadIdx);
  1096. NextReloadMap.insert(std::make_pair(LastReload->reg, ReloadLi->reg));
  1097. }
  1098. LastReloadMBB = ReloadMBB;
  1099. LastReload = ReloadLi;
  1100. LastReloadSS = ReloadSS;
  1101. }
  1102. unhandled_.push(ReloadLi);
  1103. }
  1104. return;
  1105. }
  1106. ++NumBacktracks;
  1107. // Push the current interval back to unhandled since we are going
  1108. // to re-run at least this iteration. Since we didn't modify it it
  1109. // should go back right in the front of the list
  1110. unhandled_.push(cur);
  1111. assert(TargetRegisterInfo::isPhysicalRegister(minReg) &&
  1112. "did not choose a register to spill?");
  1113. // We spill all intervals aliasing the register with
  1114. // minimum weight, rollback to the interval with the earliest
  1115. // start point and let the linear scan algorithm run again
  1116. SmallVector<LiveInterval*, 8> spillIs;
  1117. // Determine which intervals have to be spilled.
  1118. findIntervalsToSpill(cur, RegsWeights, LastCandidate, spillIs);
  1119. // Set of spilled vregs (used later to rollback properly)
  1120. SmallSet<unsigned, 8> spilled;
  1121. // The earliest start of a Spilled interval indicates up to where
  1122. // in handled we need to roll back
  1123. assert(!spillIs.empty() && "No spill intervals?");
  1124. SlotIndex earliestStart = spillIs[0]->beginIndex();
  1125. // Spill live intervals of virtual regs mapped to the physical register we
  1126. // want to clear (and its aliases). We only spill those that overlap with the
  1127. // current interval as the rest do not affect its allocation. we also keep
  1128. // track of the earliest start of all spilled live intervals since this will
  1129. // mark our rollback point.
  1130. SmallVector<LiveInterval*, 8> added;
  1131. while (!spillIs.empty()) {
  1132. LiveInterval *sli = spillIs.back();
  1133. spillIs.pop_back();
  1134. DEBUG(dbgs() << "\t\t\tspilling(a): " << *sli << '\n');
  1135. if (sli->beginIndex() < earliestStart)
  1136. earliestStart = sli->beginIndex();
  1137. LiveRangeEdit LRE(*sli, added, 0, &spillIs);
  1138. spiller_->spill(LRE);
  1139. spilled.insert(sli->reg);
  1140. }
  1141. // Include any added intervals in earliestStart.
  1142. for (unsigned i = 0, e = added.size(); i != e; ++i) {
  1143. SlotIndex SI = added[i]->beginIndex();
  1144. if (SI < earliestStart)
  1145. earliestStart = SI;
  1146. }
  1147. DEBUG(dbgs() << "\t\trolling back to: " << earliestStart << '\n');
  1148. // Scan handled in reverse order up to the earliest start of a
  1149. // spilled live interval and undo each one, restoring the state of
  1150. // unhandled.
  1151. while (!handled_.empty()) {
  1152. LiveInterval* i = handled_.back();
  1153. // If this interval starts before t we are done.
  1154. if (!i->empty() && i->beginIndex() < earliestStart)
  1155. break;
  1156. DEBUG(dbgs() << "\t\t\tundo changes for: " << *i << '\n');
  1157. handled_.pop_back();
  1158. // When undoing a live interval allocation we must know if it is active or
  1159. // inactive to properly update regUse_ and the VirtRegMap.
  1160. IntervalPtrs::iterator it;
  1161. if ((it = FindIntervalInVector(active_, i)) != active_.end()) {
  1162. active_.erase(it);
  1163. assert(!TargetRegisterInfo::isPhysicalRegister(i->reg));
  1164. if (!spilled.count(i->reg))
  1165. unhandled_.push(i);
  1166. delRegUse(vrm_->getPhys(i->reg));
  1167. vrm_->clearVirt(i->reg);
  1168. } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) {
  1169. inactive_.erase(it);
  1170. assert(!TargetRegisterInfo::isPhysicalRegister(i->reg));
  1171. if (!spilled.count(i->reg))
  1172. unhandled_.push(i);
  1173. vrm_->clearVirt(i->reg);
  1174. } else {
  1175. assert(TargetRegisterInfo::isVirtualRegister(i->reg) &&
  1176. "Can only allocate virtual registers!");
  1177. vrm_->clearVirt(i->reg);
  1178. unhandled_.push(i);
  1179. }
  1180. DenseMap<unsigned, unsigned>::iterator ii = DowngradeMap.find(i->reg);
  1181. if (ii == DowngradeMap.end())
  1182. // It interval has a preference, it must be defined by a copy. Clear the
  1183. // preference now since the source interval allocation may have been
  1184. // undone as well.
  1185. mri_->setRegAllocationHint(i->reg, 0, 0);
  1186. else {
  1187. UpgradeRegister(ii->second);
  1188. }
  1189. }
  1190. // Rewind the iterators in the active, inactive, and fixed lists back to the
  1191. // point we reverted to.
  1192. RevertVectorIteratorsTo(active_, earliestStart);
  1193. RevertVectorIteratorsTo(inactive_, earliestStart);
  1194. RevertVectorIteratorsTo(fixed_, earliestStart);
  1195. // Scan the rest and undo each interval that expired after t and
  1196. // insert it in active (the next iteration of the algorithm will
  1197. // put it in inactive if required)
  1198. for (unsigned i = 0, e = handled_.size(); i != e; ++i) {
  1199. LiveInterval *HI = handled_[i];
  1200. if (!HI->expiredAt(earliestStart) &&
  1201. HI->expiredAt(cur->beginIndex())) {
  1202. DEBUG(dbgs() << "\t\t\tundo changes for: " << *HI << '\n');
  1203. active_.push_back(std::make_pair(HI, HI->begin()));
  1204. assert(!TargetRegisterInfo::isPhysicalRegister(HI->reg));
  1205. addRegUse(vrm_->getPhys(HI->reg));
  1206. }
  1207. }
  1208. // Merge added with unhandled.
  1209. // This also update the NextReloadMap. That is, it adds mapping from a
  1210. // register defined by a reload from SS to the next reload from SS in the
  1211. // same basic block.
  1212. MachineBasicBlock *LastReloadMBB = 0;
  1213. LiveInterval *LastReload = 0;
  1214. int LastReloadSS = VirtRegMap::NO_STACK_SLOT;
  1215. std::sort(added.begin(), added.end(), LISorter());
  1216. for (unsigned i = 0, e = added.size(); i != e; ++i) {
  1217. LiveInterval *ReloadLi = added[i];
  1218. if (ReloadLi->weight == HUGE_VALF &&
  1219. li_->getApproximateInstructionCount(*ReloadLi) == 0) {
  1220. SlotIndex ReloadIdx = ReloadLi->beginIndex();
  1221. MachineBasicBlock *ReloadMBB = li_->getMBBFromIndex(ReloadIdx);
  1222. int ReloadSS = vrm_->getStackSlot(ReloadLi->reg);
  1223. if (LastReloadMBB == ReloadMBB && LastReloadSS == ReloadSS) {
  1224. // Last reload of same SS is in the same MBB. We want to try to
  1225. // allocate both reloads the same register and make sure the reg
  1226. // isn't clobbered in between if at all possible.
  1227. assert(LastReload->beginIndex() < ReloadIdx);
  1228. NextReloadMap.insert(std::make_pair(LastReload->reg, ReloadLi->reg));
  1229. }
  1230. LastReloadMBB = ReloadMBB;
  1231. LastReload = ReloadLi;
  1232. LastReloadSS = ReloadSS;
  1233. }
  1234. unhandled_.push(ReloadLi);
  1235. }
  1236. }
  1237. unsigned RALinScan::getFreePhysReg(LiveInterval* cur,
  1238. const TargetRegisterClass *RC,
  1239. unsigned MaxInactiveCount,
  1240. SmallVector<unsigned, 256> &inactiveCounts,
  1241. bool SkipDGRegs) {
  1242. unsigned FreeReg = 0;
  1243. unsigned FreeRegInactiveCount = 0;
  1244. std::pair<unsigned, unsigned> Hint = mri_->getRegAllocationHint(cur->reg);
  1245. // Resolve second part of the hint (if possible) given the current allocation.
  1246. unsigned physReg = Hint.second;
  1247. if (TargetRegisterInfo::isVirtualRegister(physReg) && vrm_->hasPhys(physReg))
  1248. physReg = vrm_->getPhys(physReg);
  1249. ArrayRef<unsigned> Order;
  1250. if (Hint.first)
  1251. Order = tri_->getRawAllocationOrder(RC, Hint.first, physReg, *mf_);
  1252. else
  1253. Order = RegClassInfo.getOrder(RC);
  1254. assert(!Order.empty() && "No allocatable register in this register class!");
  1255. // Scan for the first available register.
  1256. for (unsigned i = 0; i != Order.size(); ++i) {
  1257. unsigned Reg = Order[i];
  1258. // Ignore "downgraded" registers.
  1259. if (SkipDGRegs && DowngradedRegs.count(Reg))
  1260. continue;
  1261. // Skip reserved registers.
  1262. if (reservedRegs_.test(Reg))
  1263. continue;
  1264. // Skip recently allocated registers.
  1265. if (isRegAvail(Reg) && (!SkipDGRegs || !isRecentlyUsed(Reg))) {
  1266. FreeReg = Reg;
  1267. if (FreeReg < inactiveCounts.size())
  1268. FreeRegInactiveCount = inactiveCounts[FreeReg];
  1269. else
  1270. FreeRegInactiveCount = 0;
  1271. break;
  1272. }
  1273. }
  1274. // If there are no free regs, or if this reg has the max inactive count,
  1275. // return this register.
  1276. if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) {
  1277. // Remember what register we picked so we can skip it next time.
  1278. if (FreeReg != 0) recordRecentlyUsed(FreeReg);
  1279. return FreeReg;
  1280. }
  1281. // Continue scanning the registers, looking for the one with the highest
  1282. // inactive count. Alkis found that this reduced register pressure very
  1283. // slightly on X86 (in rev 1.94 of this file), though this should probably be
  1284. // reevaluated now.
  1285. for (unsigned i = 0; i != Order.size(); ++i) {
  1286. unsigned Reg = Order[i];
  1287. // Ignore "downgraded" registers.
  1288. if (SkipDGRegs && DowngradedRegs.count(Reg))
  1289. continue;
  1290. // Skip reserved registers.
  1291. if (reservedRegs_.test(Reg))
  1292. continue;
  1293. if (isRegAvail(Reg) && Reg < inactiveCounts.size() &&
  1294. FreeRegInactiveCount < inactiveCounts[Reg] &&
  1295. (!SkipDGRegs || !isRecentlyUsed(Reg))) {
  1296. FreeReg = Reg;
  1297. FreeRegInactiveCount = inactiveCounts[Reg];
  1298. if (FreeRegInactiveCount == MaxInactiveCount)
  1299. break; // We found the one with the max inactive count.
  1300. }
  1301. }
  1302. // Remember what register we picked so we can skip it next time.
  1303. recordRecentlyUsed(FreeReg);
  1304. return FreeReg;
  1305. }
  1306. /// getFreePhysReg - return a free physical register for this virtual register
  1307. /// interval if we have one, otherwise return 0.
  1308. unsigned RALinScan::getFreePhysReg(LiveInterval *cur) {
  1309. SmallVector<unsigned, 256> inactiveCounts;
  1310. unsigned MaxInactiveCount = 0;
  1311. const TargetRegisterClass *RC = mri_->getRegClass(cur->reg);
  1312. const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
  1313. for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end();
  1314. i != e; ++i) {
  1315. unsigned reg = i->first->reg;
  1316. assert(TargetRegisterInfo::isVirtualRegister(reg) &&
  1317. "Can only allocate virtual registers!");
  1318. // If this is not in a related reg class to the register we're allocating,
  1319. // don't check it.
  1320. const TargetRegisterClass *RegRC = mri_->getRegClass(reg);
  1321. if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader) {
  1322. reg = vrm_->getPhys(reg);
  1323. if (inactiveCounts.size() <= reg)
  1324. inactiveCounts.resize(reg+1);
  1325. ++inactiveCounts[reg];
  1326. MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]);
  1327. }
  1328. }
  1329. // If copy coalescer has assigned a "preferred" register, check if it's
  1330. // available first.
  1331. unsigned Preference = vrm_->getRegAllocPref(cur->reg);
  1332. if (Preference) {
  1333. DEBUG(dbgs() << "(preferred: " << tri_->getName(Preference) << ") ");
  1334. if (isRegAvail(Preference) &&
  1335. RC->contains(Preference))
  1336. return Preference;
  1337. }
  1338. unsigned FreeReg = getFreePhysReg(cur, RC, MaxInactiveCount, inactiveCounts,
  1339. true);
  1340. if (FreeReg)
  1341. return FreeReg;
  1342. return getFreePhysReg(cur, RC, MaxInactiveCount, inactiveCounts, false);
  1343. }
  1344. FunctionPass* llvm::createLinearScanRegisterAllocator() {
  1345. return new RALinScan();
  1346. }