RegAllocLinearScan.cpp 52 KB

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