RegAllocLinearScan.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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 "llvm/CodeGen/LiveIntervalAnalysis.h"
  15. #include "PhysRegTracker.h"
  16. #include "VirtRegMap.h"
  17. #include "llvm/Function.h"
  18. #include "llvm/CodeGen/MachineFunctionPass.h"
  19. #include "llvm/CodeGen/MachineInstr.h"
  20. #include "llvm/CodeGen/MachineLoopInfo.h"
  21. #include "llvm/CodeGen/MachineRegisterInfo.h"
  22. #include "llvm/CodeGen/Passes.h"
  23. #include "llvm/CodeGen/RegAllocRegistry.h"
  24. #include "llvm/CodeGen/RegisterCoalescer.h"
  25. #include "llvm/Target/MRegisterInfo.h"
  26. #include "llvm/Target/TargetMachine.h"
  27. #include "llvm/Target/TargetInstrInfo.h"
  28. #include "llvm/ADT/EquivalenceClasses.h"
  29. #include "llvm/ADT/Statistic.h"
  30. #include "llvm/ADT/STLExtras.h"
  31. #include "llvm/Support/Debug.h"
  32. #include "llvm/Support/Compiler.h"
  33. #include <algorithm>
  34. #include <set>
  35. #include <queue>
  36. #include <memory>
  37. #include <cmath>
  38. using namespace llvm;
  39. STATISTIC(NumIters , "Number of iterations performed");
  40. STATISTIC(NumBacktracks, "Number of times we had to backtrack");
  41. STATISTIC(NumCoalesce, "Number of copies coalesced");
  42. static RegisterRegAlloc
  43. linearscanRegAlloc("linearscan", " linear scan register allocator",
  44. createLinearScanRegisterAllocator);
  45. namespace {
  46. struct VISIBILITY_HIDDEN RALinScan : public MachineFunctionPass {
  47. static char ID;
  48. RALinScan() : MachineFunctionPass((intptr_t)&ID) {}
  49. typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr;
  50. typedef std::vector<IntervalPtr> IntervalPtrs;
  51. private:
  52. /// RelatedRegClasses - This structure is built the first time a function is
  53. /// compiled, and keeps track of which register classes have registers that
  54. /// belong to multiple classes or have aliases that are in other classes.
  55. EquivalenceClasses<const TargetRegisterClass*> RelatedRegClasses;
  56. std::map<unsigned, const TargetRegisterClass*> OneClassForEachPhysReg;
  57. MachineFunction* mf_;
  58. const TargetMachine* tm_;
  59. const MRegisterInfo* mri_;
  60. const TargetInstrInfo* tii_;
  61. MachineRegisterInfo *reginfo_;
  62. BitVector allocatableRegs_;
  63. LiveIntervals* li_;
  64. const MachineLoopInfo *loopInfo;
  65. /// handled_ - Intervals are added to the handled_ set in the order of their
  66. /// start value. This is uses for backtracking.
  67. std::vector<LiveInterval*> handled_;
  68. /// fixed_ - Intervals that correspond to machine registers.
  69. ///
  70. IntervalPtrs fixed_;
  71. /// active_ - Intervals that are currently being processed, and which have a
  72. /// live range active for the current point.
  73. IntervalPtrs active_;
  74. /// inactive_ - Intervals that are currently being processed, but which have
  75. /// a hold at the current point.
  76. IntervalPtrs inactive_;
  77. typedef std::priority_queue<LiveInterval*,
  78. std::vector<LiveInterval*>,
  79. greater_ptr<LiveInterval> > IntervalHeap;
  80. IntervalHeap unhandled_;
  81. std::auto_ptr<PhysRegTracker> prt_;
  82. std::auto_ptr<VirtRegMap> vrm_;
  83. std::auto_ptr<Spiller> spiller_;
  84. public:
  85. virtual const char* getPassName() const {
  86. return "Linear Scan Register Allocator";
  87. }
  88. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  89. AU.addRequired<LiveIntervals>();
  90. // Make sure PassManager knows which analyses to make available
  91. // to coalescing and which analyses coalescing invalidates.
  92. AU.addRequiredTransitive<RegisterCoalescer>();
  93. AU.addRequired<MachineLoopInfo>();
  94. AU.addPreserved<MachineLoopInfo>();
  95. AU.addPreservedID(MachineDominatorsID);
  96. MachineFunctionPass::getAnalysisUsage(AU);
  97. }
  98. /// runOnMachineFunction - register allocate the whole function
  99. bool runOnMachineFunction(MachineFunction&);
  100. private:
  101. /// linearScan - the linear scan algorithm
  102. void linearScan();
  103. /// initIntervalSets - initialize the interval sets.
  104. ///
  105. void initIntervalSets();
  106. /// processActiveIntervals - expire old intervals and move non-overlapping
  107. /// ones to the inactive list.
  108. void processActiveIntervals(unsigned CurPoint);
  109. /// processInactiveIntervals - expire old intervals and move overlapping
  110. /// ones to the active list.
  111. void processInactiveIntervals(unsigned CurPoint);
  112. /// assignRegOrStackSlotAtInterval - assign a register if one
  113. /// is available, or spill.
  114. void assignRegOrStackSlotAtInterval(LiveInterval* cur);
  115. /// attemptTrivialCoalescing - If a simple interval is defined by a copy,
  116. /// try allocate the definition the same register as the source register
  117. /// if the register is not defined during live time of the interval. This
  118. /// eliminate a copy. This is used to coalesce copies which were not
  119. /// coalesced away before allocation either due to dest and src being in
  120. /// different register classes or because the coalescer was overly
  121. /// conservative.
  122. unsigned attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg);
  123. ///
  124. /// register handling helpers
  125. ///
  126. /// getFreePhysReg - return a free physical register for this virtual
  127. /// register interval if we have one, otherwise return 0.
  128. unsigned getFreePhysReg(LiveInterval* cur);
  129. /// assignVirt2StackSlot - assigns this virtual register to a
  130. /// stack slot. returns the stack slot
  131. int assignVirt2StackSlot(unsigned virtReg);
  132. void ComputeRelatedRegClasses();
  133. template <typename ItTy>
  134. void printIntervals(const char* const str, ItTy i, ItTy e) const {
  135. if (str) DOUT << str << " intervals:\n";
  136. for (; i != e; ++i) {
  137. DOUT << "\t" << *i->first << " -> ";
  138. unsigned reg = i->first->reg;
  139. if (MRegisterInfo::isVirtualRegister(reg)) {
  140. reg = vrm_->getPhys(reg);
  141. }
  142. DOUT << mri_->getName(reg) << '\n';
  143. }
  144. }
  145. };
  146. char RALinScan::ID = 0;
  147. }
  148. void RALinScan::ComputeRelatedRegClasses() {
  149. const MRegisterInfo &MRI = *mri_;
  150. // First pass, add all reg classes to the union, and determine at least one
  151. // reg class that each register is in.
  152. bool HasAliases = false;
  153. for (MRegisterInfo::regclass_iterator RCI = MRI.regclass_begin(),
  154. E = MRI.regclass_end(); RCI != E; ++RCI) {
  155. RelatedRegClasses.insert(*RCI);
  156. for (TargetRegisterClass::iterator I = (*RCI)->begin(), E = (*RCI)->end();
  157. I != E; ++I) {
  158. HasAliases = HasAliases || *MRI.getAliasSet(*I) != 0;
  159. const TargetRegisterClass *&PRC = OneClassForEachPhysReg[*I];
  160. if (PRC) {
  161. // Already processed this register. Just make sure we know that
  162. // multiple register classes share a register.
  163. RelatedRegClasses.unionSets(PRC, *RCI);
  164. } else {
  165. PRC = *RCI;
  166. }
  167. }
  168. }
  169. // Second pass, now that we know conservatively what register classes each reg
  170. // belongs to, add info about aliases. We don't need to do this for targets
  171. // without register aliases.
  172. if (HasAliases)
  173. for (std::map<unsigned, const TargetRegisterClass*>::iterator
  174. I = OneClassForEachPhysReg.begin(), E = OneClassForEachPhysReg.end();
  175. I != E; ++I)
  176. for (const unsigned *AS = MRI.getAliasSet(I->first); *AS; ++AS)
  177. RelatedRegClasses.unionSets(I->second, OneClassForEachPhysReg[*AS]);
  178. }
  179. /// attemptTrivialCoalescing - If a simple interval is defined by a copy,
  180. /// try allocate the definition the same register as the source register
  181. /// if the register is not defined during live time of the interval. This
  182. /// eliminate a copy. This is used to coalesce copies which were not
  183. /// coalesced away before allocation either due to dest and src being in
  184. /// different register classes or because the coalescer was overly
  185. /// conservative.
  186. unsigned RALinScan::attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg) {
  187. if ((cur.preference && cur.preference == Reg) || !cur.containsOneValue())
  188. return Reg;
  189. VNInfo *vni = cur.getValNumInfo(0);
  190. if (!vni->def || vni->def == ~1U || vni->def == ~0U)
  191. return Reg;
  192. MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def);
  193. unsigned SrcReg, DstReg;
  194. if (!CopyMI || !tii_->isMoveInstr(*CopyMI, SrcReg, DstReg))
  195. return Reg;
  196. if (MRegisterInfo::isVirtualRegister(SrcReg))
  197. if (!vrm_->isAssignedReg(SrcReg))
  198. return Reg;
  199. else
  200. SrcReg = vrm_->getPhys(SrcReg);
  201. if (Reg == SrcReg)
  202. return Reg;
  203. const TargetRegisterClass *RC = reginfo_->getRegClass(cur.reg);
  204. if (!RC->contains(SrcReg))
  205. return Reg;
  206. // Try to coalesce.
  207. if (!li_->conflictsWithPhysRegDef(cur, *vrm_, SrcReg)) {
  208. DOUT << "Coalescing: " << cur << " -> " << mri_->getName(SrcReg) << '\n';
  209. vrm_->clearVirt(cur.reg);
  210. vrm_->assignVirt2Phys(cur.reg, SrcReg);
  211. ++NumCoalesce;
  212. return SrcReg;
  213. }
  214. return Reg;
  215. }
  216. bool RALinScan::runOnMachineFunction(MachineFunction &fn) {
  217. mf_ = &fn;
  218. tm_ = &fn.getTarget();
  219. mri_ = tm_->getRegisterInfo();
  220. tii_ = tm_->getInstrInfo();
  221. reginfo_ = &mf_->getRegInfo();
  222. allocatableRegs_ = mri_->getAllocatableSet(fn);
  223. li_ = &getAnalysis<LiveIntervals>();
  224. loopInfo = &getAnalysis<MachineLoopInfo>();
  225. // We don't run the coalescer here because we have no reason to
  226. // interact with it. If the coalescer requires interaction, it
  227. // won't do anything. If it doesn't require interaction, we assume
  228. // it was run as a separate pass.
  229. // If this is the first function compiled, compute the related reg classes.
  230. if (RelatedRegClasses.empty())
  231. ComputeRelatedRegClasses();
  232. if (!prt_.get()) prt_.reset(new PhysRegTracker(*mri_));
  233. vrm_.reset(new VirtRegMap(*mf_));
  234. if (!spiller_.get()) spiller_.reset(createSpiller());
  235. initIntervalSets();
  236. linearScan();
  237. // Rewrite spill code and update the PhysRegsUsed set.
  238. spiller_->runOnMachineFunction(*mf_, *vrm_);
  239. vrm_.reset(); // Free the VirtRegMap
  240. while (!unhandled_.empty()) unhandled_.pop();
  241. fixed_.clear();
  242. active_.clear();
  243. inactive_.clear();
  244. handled_.clear();
  245. return true;
  246. }
  247. /// initIntervalSets - initialize the interval sets.
  248. ///
  249. void RALinScan::initIntervalSets()
  250. {
  251. assert(unhandled_.empty() && fixed_.empty() &&
  252. active_.empty() && inactive_.empty() &&
  253. "interval sets should be empty on initialization");
  254. for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
  255. if (MRegisterInfo::isPhysicalRegister(i->second.reg)) {
  256. reginfo_->setPhysRegUsed(i->second.reg);
  257. fixed_.push_back(std::make_pair(&i->second, i->second.begin()));
  258. } else
  259. unhandled_.push(&i->second);
  260. }
  261. }
  262. void RALinScan::linearScan()
  263. {
  264. // linear scan algorithm
  265. DOUT << "********** LINEAR SCAN **********\n";
  266. DOUT << "********** Function: " << mf_->getFunction()->getName() << '\n';
  267. DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end()));
  268. while (!unhandled_.empty()) {
  269. // pick the interval with the earliest start point
  270. LiveInterval* cur = unhandled_.top();
  271. unhandled_.pop();
  272. ++NumIters;
  273. DOUT << "\n*** CURRENT ***: " << *cur << '\n';
  274. processActiveIntervals(cur->beginNumber());
  275. processInactiveIntervals(cur->beginNumber());
  276. assert(MRegisterInfo::isVirtualRegister(cur->reg) &&
  277. "Can only allocate virtual registers!");
  278. // Allocating a virtual register. try to find a free
  279. // physical register or spill an interval (possibly this one) in order to
  280. // assign it one.
  281. assignRegOrStackSlotAtInterval(cur);
  282. DEBUG(printIntervals("active", active_.begin(), active_.end()));
  283. DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
  284. }
  285. // expire any remaining active intervals
  286. while (!active_.empty()) {
  287. IntervalPtr &IP = active_.back();
  288. unsigned reg = IP.first->reg;
  289. DOUT << "\tinterval " << *IP.first << " expired\n";
  290. assert(MRegisterInfo::isVirtualRegister(reg) &&
  291. "Can only allocate virtual registers!");
  292. reg = vrm_->getPhys(reg);
  293. prt_->delRegUse(reg);
  294. active_.pop_back();
  295. }
  296. // expire any remaining inactive intervals
  297. DEBUG(for (IntervalPtrs::reverse_iterator
  298. i = inactive_.rbegin(); i != inactive_.rend(); ++i)
  299. DOUT << "\tinterval " << *i->first << " expired\n");
  300. inactive_.clear();
  301. // Add live-ins to every BB except for entry. Also perform trivial coalescing.
  302. MachineFunction::iterator EntryMBB = mf_->begin();
  303. SmallVector<MachineBasicBlock*, 8> LiveInMBBs;
  304. for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
  305. LiveInterval &cur = i->second;
  306. unsigned Reg = 0;
  307. bool isPhys = MRegisterInfo::isPhysicalRegister(cur.reg);
  308. if (isPhys)
  309. Reg = i->second.reg;
  310. else if (vrm_->isAssignedReg(cur.reg))
  311. Reg = attemptTrivialCoalescing(cur, vrm_->getPhys(cur.reg));
  312. if (!Reg)
  313. continue;
  314. // Ignore splited live intervals.
  315. if (!isPhys && vrm_->getPreSplitReg(cur.reg))
  316. continue;
  317. for (LiveInterval::Ranges::const_iterator I = cur.begin(), E = cur.end();
  318. I != E; ++I) {
  319. const LiveRange &LR = *I;
  320. if (li_->findLiveInMBBs(LR, LiveInMBBs)) {
  321. for (unsigned i = 0, e = LiveInMBBs.size(); i != e; ++i)
  322. if (LiveInMBBs[i] != EntryMBB)
  323. LiveInMBBs[i]->addLiveIn(Reg);
  324. LiveInMBBs.clear();
  325. }
  326. }
  327. }
  328. DOUT << *vrm_;
  329. }
  330. /// processActiveIntervals - expire old intervals and move non-overlapping ones
  331. /// to the inactive list.
  332. void RALinScan::processActiveIntervals(unsigned CurPoint)
  333. {
  334. DOUT << "\tprocessing active intervals:\n";
  335. for (unsigned i = 0, e = active_.size(); i != e; ++i) {
  336. LiveInterval *Interval = active_[i].first;
  337. LiveInterval::iterator IntervalPos = active_[i].second;
  338. unsigned reg = Interval->reg;
  339. IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
  340. if (IntervalPos == Interval->end()) { // Remove expired intervals.
  341. DOUT << "\t\tinterval " << *Interval << " expired\n";
  342. assert(MRegisterInfo::isVirtualRegister(reg) &&
  343. "Can only allocate virtual registers!");
  344. reg = vrm_->getPhys(reg);
  345. prt_->delRegUse(reg);
  346. // Pop off the end of the list.
  347. active_[i] = active_.back();
  348. active_.pop_back();
  349. --i; --e;
  350. } else if (IntervalPos->start > CurPoint) {
  351. // Move inactive intervals to inactive list.
  352. DOUT << "\t\tinterval " << *Interval << " inactive\n";
  353. assert(MRegisterInfo::isVirtualRegister(reg) &&
  354. "Can only allocate virtual registers!");
  355. reg = vrm_->getPhys(reg);
  356. prt_->delRegUse(reg);
  357. // add to inactive.
  358. inactive_.push_back(std::make_pair(Interval, IntervalPos));
  359. // Pop off the end of the list.
  360. active_[i] = active_.back();
  361. active_.pop_back();
  362. --i; --e;
  363. } else {
  364. // Otherwise, just update the iterator position.
  365. active_[i].second = IntervalPos;
  366. }
  367. }
  368. }
  369. /// processInactiveIntervals - expire old intervals and move overlapping
  370. /// ones to the active list.
  371. void RALinScan::processInactiveIntervals(unsigned CurPoint)
  372. {
  373. DOUT << "\tprocessing inactive intervals:\n";
  374. for (unsigned i = 0, e = inactive_.size(); i != e; ++i) {
  375. LiveInterval *Interval = inactive_[i].first;
  376. LiveInterval::iterator IntervalPos = inactive_[i].second;
  377. unsigned reg = Interval->reg;
  378. IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
  379. if (IntervalPos == Interval->end()) { // remove expired intervals.
  380. DOUT << "\t\tinterval " << *Interval << " expired\n";
  381. // Pop off the end of the list.
  382. inactive_[i] = inactive_.back();
  383. inactive_.pop_back();
  384. --i; --e;
  385. } else if (IntervalPos->start <= CurPoint) {
  386. // move re-activated intervals in active list
  387. DOUT << "\t\tinterval " << *Interval << " active\n";
  388. assert(MRegisterInfo::isVirtualRegister(reg) &&
  389. "Can only allocate virtual registers!");
  390. reg = vrm_->getPhys(reg);
  391. prt_->addRegUse(reg);
  392. // add to active
  393. active_.push_back(std::make_pair(Interval, IntervalPos));
  394. // Pop off the end of the list.
  395. inactive_[i] = inactive_.back();
  396. inactive_.pop_back();
  397. --i; --e;
  398. } else {
  399. // Otherwise, just update the iterator position.
  400. inactive_[i].second = IntervalPos;
  401. }
  402. }
  403. }
  404. /// updateSpillWeights - updates the spill weights of the specifed physical
  405. /// register and its weight.
  406. static void updateSpillWeights(std::vector<float> &Weights,
  407. unsigned reg, float weight,
  408. const MRegisterInfo *MRI) {
  409. Weights[reg] += weight;
  410. for (const unsigned* as = MRI->getAliasSet(reg); *as; ++as)
  411. Weights[*as] += weight;
  412. }
  413. static
  414. RALinScan::IntervalPtrs::iterator
  415. FindIntervalInVector(RALinScan::IntervalPtrs &IP, LiveInterval *LI) {
  416. for (RALinScan::IntervalPtrs::iterator I = IP.begin(), E = IP.end();
  417. I != E; ++I)
  418. if (I->first == LI) return I;
  419. return IP.end();
  420. }
  421. static void RevertVectorIteratorsTo(RALinScan::IntervalPtrs &V, unsigned Point){
  422. for (unsigned i = 0, e = V.size(); i != e; ++i) {
  423. RALinScan::IntervalPtr &IP = V[i];
  424. LiveInterval::iterator I = std::upper_bound(IP.first->begin(),
  425. IP.second, Point);
  426. if (I != IP.first->begin()) --I;
  427. IP.second = I;
  428. }
  429. }
  430. /// assignRegOrStackSlotAtInterval - assign a register if one is available, or
  431. /// spill.
  432. void RALinScan::assignRegOrStackSlotAtInterval(LiveInterval* cur)
  433. {
  434. DOUT << "\tallocating current interval: ";
  435. PhysRegTracker backupPrt = *prt_;
  436. std::vector<std::pair<unsigned, float> > SpillWeightsToAdd;
  437. unsigned StartPosition = cur->beginNumber();
  438. const TargetRegisterClass *RC = reginfo_->getRegClass(cur->reg);
  439. const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
  440. // If this live interval is defined by a move instruction and its source is
  441. // assigned a physical register that is compatible with the target register
  442. // class, then we should try to assign it the same register.
  443. // This can happen when the move is from a larger register class to a smaller
  444. // one, e.g. X86::mov32to32_. These move instructions are not coalescable.
  445. if (!cur->preference && cur->containsOneValue()) {
  446. VNInfo *vni = cur->getValNumInfo(0);
  447. if (vni->def && vni->def != ~1U && vni->def != ~0U) {
  448. MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def);
  449. unsigned SrcReg, DstReg;
  450. if (tii_->isMoveInstr(*CopyMI, SrcReg, DstReg)) {
  451. unsigned Reg = 0;
  452. if (MRegisterInfo::isPhysicalRegister(SrcReg))
  453. Reg = SrcReg;
  454. else if (vrm_->isAssignedReg(SrcReg))
  455. Reg = vrm_->getPhys(SrcReg);
  456. if (Reg && allocatableRegs_[Reg] && RC->contains(Reg))
  457. cur->preference = Reg;
  458. }
  459. }
  460. }
  461. // for every interval in inactive we overlap with, mark the
  462. // register as not free and update spill weights.
  463. for (IntervalPtrs::const_iterator i = inactive_.begin(),
  464. e = inactive_.end(); i != e; ++i) {
  465. unsigned Reg = i->first->reg;
  466. assert(MRegisterInfo::isVirtualRegister(Reg) &&
  467. "Can only allocate virtual registers!");
  468. const TargetRegisterClass *RegRC = reginfo_->getRegClass(Reg);
  469. // If this is not in a related reg class to the register we're allocating,
  470. // don't check it.
  471. if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
  472. cur->overlapsFrom(*i->first, i->second-1)) {
  473. Reg = vrm_->getPhys(Reg);
  474. prt_->addRegUse(Reg);
  475. SpillWeightsToAdd.push_back(std::make_pair(Reg, i->first->weight));
  476. }
  477. }
  478. // Speculatively check to see if we can get a register right now. If not,
  479. // we know we won't be able to by adding more constraints. If so, we can
  480. // check to see if it is valid. Doing an exhaustive search of the fixed_ list
  481. // is very bad (it contains all callee clobbered registers for any functions
  482. // with a call), so we want to avoid doing that if possible.
  483. unsigned physReg = getFreePhysReg(cur);
  484. if (physReg) {
  485. // We got a register. However, if it's in the fixed_ list, we might
  486. // conflict with it. Check to see if we conflict with it or any of its
  487. // aliases.
  488. SmallSet<unsigned, 8> RegAliases;
  489. for (const unsigned *AS = mri_->getAliasSet(physReg); *AS; ++AS)
  490. RegAliases.insert(*AS);
  491. bool ConflictsWithFixed = false;
  492. for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
  493. IntervalPtr &IP = fixed_[i];
  494. if (physReg == IP.first->reg || RegAliases.count(IP.first->reg)) {
  495. // Okay, this reg is on the fixed list. Check to see if we actually
  496. // conflict.
  497. LiveInterval *I = IP.first;
  498. if (I->endNumber() > StartPosition) {
  499. LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
  500. IP.second = II;
  501. if (II != I->begin() && II->start > StartPosition)
  502. --II;
  503. if (cur->overlapsFrom(*I, II)) {
  504. ConflictsWithFixed = true;
  505. break;
  506. }
  507. }
  508. }
  509. }
  510. // Okay, the register picked by our speculative getFreePhysReg call turned
  511. // out to be in use. Actually add all of the conflicting fixed registers to
  512. // prt so we can do an accurate query.
  513. if (ConflictsWithFixed) {
  514. // For every interval in fixed we overlap with, mark the register as not
  515. // free and update spill weights.
  516. for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
  517. IntervalPtr &IP = fixed_[i];
  518. LiveInterval *I = IP.first;
  519. const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg];
  520. if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
  521. I->endNumber() > StartPosition) {
  522. LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
  523. IP.second = II;
  524. if (II != I->begin() && II->start > StartPosition)
  525. --II;
  526. if (cur->overlapsFrom(*I, II)) {
  527. unsigned reg = I->reg;
  528. prt_->addRegUse(reg);
  529. SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight));
  530. }
  531. }
  532. }
  533. // Using the newly updated prt_ object, which includes conflicts in the
  534. // future, see if there are any registers available.
  535. physReg = getFreePhysReg(cur);
  536. }
  537. }
  538. // Restore the physical register tracker, removing information about the
  539. // future.
  540. *prt_ = backupPrt;
  541. // if we find a free register, we are done: assign this virtual to
  542. // the free physical register and add this interval to the active
  543. // list.
  544. if (physReg) {
  545. DOUT << mri_->getName(physReg) << '\n';
  546. vrm_->assignVirt2Phys(cur->reg, physReg);
  547. prt_->addRegUse(physReg);
  548. active_.push_back(std::make_pair(cur, cur->begin()));
  549. handled_.push_back(cur);
  550. return;
  551. }
  552. DOUT << "no free registers\n";
  553. // Compile the spill weights into an array that is better for scanning.
  554. std::vector<float> SpillWeights(mri_->getNumRegs(), 0.0);
  555. for (std::vector<std::pair<unsigned, float> >::iterator
  556. I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I)
  557. updateSpillWeights(SpillWeights, I->first, I->second, mri_);
  558. // for each interval in active, update spill weights.
  559. for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
  560. i != e; ++i) {
  561. unsigned reg = i->first->reg;
  562. assert(MRegisterInfo::isVirtualRegister(reg) &&
  563. "Can only allocate virtual registers!");
  564. reg = vrm_->getPhys(reg);
  565. updateSpillWeights(SpillWeights, reg, i->first->weight, mri_);
  566. }
  567. DOUT << "\tassigning stack slot at interval "<< *cur << ":\n";
  568. // Find a register to spill.
  569. float minWeight = HUGE_VALF;
  570. unsigned minReg = cur->preference; // Try the preferred register first.
  571. if (!minReg || SpillWeights[minReg] == HUGE_VALF)
  572. for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
  573. e = RC->allocation_order_end(*mf_); i != e; ++i) {
  574. unsigned reg = *i;
  575. if (minWeight > SpillWeights[reg]) {
  576. minWeight = SpillWeights[reg];
  577. minReg = reg;
  578. }
  579. }
  580. // If we didn't find a register that is spillable, try aliases?
  581. if (!minReg) {
  582. for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
  583. e = RC->allocation_order_end(*mf_); i != e; ++i) {
  584. unsigned reg = *i;
  585. // No need to worry about if the alias register size < regsize of RC.
  586. // We are going to spill all registers that alias it anyway.
  587. for (const unsigned* as = mri_->getAliasSet(reg); *as; ++as) {
  588. if (minWeight > SpillWeights[*as]) {
  589. minWeight = SpillWeights[*as];
  590. minReg = *as;
  591. }
  592. }
  593. }
  594. // All registers must have inf weight. Just grab one!
  595. if (!minReg)
  596. minReg = *RC->allocation_order_begin(*mf_);
  597. }
  598. DOUT << "\t\tregister with min weight: "
  599. << mri_->getName(minReg) << " (" << minWeight << ")\n";
  600. // if the current has the minimum weight, we need to spill it and
  601. // add any added intervals back to unhandled, and restart
  602. // linearscan.
  603. if (cur->weight != HUGE_VALF && cur->weight <= minWeight) {
  604. DOUT << "\t\t\tspilling(c): " << *cur << '\n';
  605. std::vector<LiveInterval*> added =
  606. li_->addIntervalsForSpills(*cur, loopInfo, *vrm_);
  607. if (added.empty())
  608. return; // Early exit if all spills were folded.
  609. // Merge added with unhandled. Note that we know that
  610. // addIntervalsForSpills returns intervals sorted by their starting
  611. // point.
  612. for (unsigned i = 0, e = added.size(); i != e; ++i)
  613. unhandled_.push(added[i]);
  614. return;
  615. }
  616. ++NumBacktracks;
  617. // push the current interval back to unhandled since we are going
  618. // to re-run at least this iteration. Since we didn't modify it it
  619. // should go back right in the front of the list
  620. unhandled_.push(cur);
  621. // otherwise we spill all intervals aliasing the register with
  622. // minimum weight, rollback to the interval with the earliest
  623. // start point and let the linear scan algorithm run again
  624. std::vector<LiveInterval*> added;
  625. assert(MRegisterInfo::isPhysicalRegister(minReg) &&
  626. "did not choose a register to spill?");
  627. BitVector toSpill(mri_->getNumRegs());
  628. // We are going to spill minReg and all its aliases.
  629. toSpill[minReg] = true;
  630. for (const unsigned* as = mri_->getAliasSet(minReg); *as; ++as)
  631. toSpill[*as] = true;
  632. // the earliest start of a spilled interval indicates up to where
  633. // in handled we need to roll back
  634. unsigned earliestStart = cur->beginNumber();
  635. // set of spilled vregs (used later to rollback properly)
  636. SmallSet<unsigned, 32> spilled;
  637. // spill live intervals of virtual regs mapped to the physical register we
  638. // want to clear (and its aliases). We only spill those that overlap with the
  639. // current interval as the rest do not affect its allocation. we also keep
  640. // track of the earliest start of all spilled live intervals since this will
  641. // mark our rollback point.
  642. for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
  643. unsigned reg = i->first->reg;
  644. if (//MRegisterInfo::isVirtualRegister(reg) &&
  645. toSpill[vrm_->getPhys(reg)] &&
  646. cur->overlapsFrom(*i->first, i->second)) {
  647. DOUT << "\t\t\tspilling(a): " << *i->first << '\n';
  648. earliestStart = std::min(earliestStart, i->first->beginNumber());
  649. std::vector<LiveInterval*> newIs =
  650. li_->addIntervalsForSpills(*i->first, loopInfo, *vrm_);
  651. std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
  652. spilled.insert(reg);
  653. }
  654. }
  655. for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){
  656. unsigned reg = i->first->reg;
  657. if (//MRegisterInfo::isVirtualRegister(reg) &&
  658. toSpill[vrm_->getPhys(reg)] &&
  659. cur->overlapsFrom(*i->first, i->second-1)) {
  660. DOUT << "\t\t\tspilling(i): " << *i->first << '\n';
  661. earliestStart = std::min(earliestStart, i->first->beginNumber());
  662. std::vector<LiveInterval*> newIs =
  663. li_->addIntervalsForSpills(*i->first, loopInfo, *vrm_);
  664. std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
  665. spilled.insert(reg);
  666. }
  667. }
  668. DOUT << "\t\trolling back to: " << earliestStart << '\n';
  669. // Scan handled in reverse order up to the earliest start of a
  670. // spilled live interval and undo each one, restoring the state of
  671. // unhandled.
  672. while (!handled_.empty()) {
  673. LiveInterval* i = handled_.back();
  674. // If this interval starts before t we are done.
  675. if (i->beginNumber() < earliestStart)
  676. break;
  677. DOUT << "\t\t\tundo changes for: " << *i << '\n';
  678. handled_.pop_back();
  679. // When undoing a live interval allocation we must know if it is active or
  680. // inactive to properly update the PhysRegTracker and the VirtRegMap.
  681. IntervalPtrs::iterator it;
  682. if ((it = FindIntervalInVector(active_, i)) != active_.end()) {
  683. active_.erase(it);
  684. assert(!MRegisterInfo::isPhysicalRegister(i->reg));
  685. if (!spilled.count(i->reg))
  686. unhandled_.push(i);
  687. prt_->delRegUse(vrm_->getPhys(i->reg));
  688. vrm_->clearVirt(i->reg);
  689. } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) {
  690. inactive_.erase(it);
  691. assert(!MRegisterInfo::isPhysicalRegister(i->reg));
  692. if (!spilled.count(i->reg))
  693. unhandled_.push(i);
  694. vrm_->clearVirt(i->reg);
  695. } else {
  696. assert(MRegisterInfo::isVirtualRegister(i->reg) &&
  697. "Can only allocate virtual registers!");
  698. vrm_->clearVirt(i->reg);
  699. unhandled_.push(i);
  700. }
  701. // It interval has a preference, it must be defined by a copy. Clear the
  702. // preference now since the source interval allocation may have been undone
  703. // as well.
  704. i->preference = 0;
  705. }
  706. // Rewind the iterators in the active, inactive, and fixed lists back to the
  707. // point we reverted to.
  708. RevertVectorIteratorsTo(active_, earliestStart);
  709. RevertVectorIteratorsTo(inactive_, earliestStart);
  710. RevertVectorIteratorsTo(fixed_, earliestStart);
  711. // scan the rest and undo each interval that expired after t and
  712. // insert it in active (the next iteration of the algorithm will
  713. // put it in inactive if required)
  714. for (unsigned i = 0, e = handled_.size(); i != e; ++i) {
  715. LiveInterval *HI = handled_[i];
  716. if (!HI->expiredAt(earliestStart) &&
  717. HI->expiredAt(cur->beginNumber())) {
  718. DOUT << "\t\t\tundo changes for: " << *HI << '\n';
  719. active_.push_back(std::make_pair(HI, HI->begin()));
  720. assert(!MRegisterInfo::isPhysicalRegister(HI->reg));
  721. prt_->addRegUse(vrm_->getPhys(HI->reg));
  722. }
  723. }
  724. // merge added with unhandled
  725. for (unsigned i = 0, e = added.size(); i != e; ++i)
  726. unhandled_.push(added[i]);
  727. }
  728. /// getFreePhysReg - return a free physical register for this virtual register
  729. /// interval if we have one, otherwise return 0.
  730. unsigned RALinScan::getFreePhysReg(LiveInterval *cur) {
  731. std::vector<unsigned> inactiveCounts(mri_->getNumRegs(), 0);
  732. unsigned MaxInactiveCount = 0;
  733. const TargetRegisterClass *RC = reginfo_->getRegClass(cur->reg);
  734. const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
  735. for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end();
  736. i != e; ++i) {
  737. unsigned reg = i->first->reg;
  738. assert(MRegisterInfo::isVirtualRegister(reg) &&
  739. "Can only allocate virtual registers!");
  740. // If this is not in a related reg class to the register we're allocating,
  741. // don't check it.
  742. const TargetRegisterClass *RegRC = reginfo_->getRegClass(reg);
  743. if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader) {
  744. reg = vrm_->getPhys(reg);
  745. ++inactiveCounts[reg];
  746. MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]);
  747. }
  748. }
  749. unsigned FreeReg = 0;
  750. unsigned FreeRegInactiveCount = 0;
  751. // If copy coalescer has assigned a "preferred" register, check if it's
  752. // available first.
  753. if (cur->preference)
  754. if (prt_->isRegAvail(cur->preference)) {
  755. DOUT << "\t\tassigned the preferred register: "
  756. << mri_->getName(cur->preference) << "\n";
  757. return cur->preference;
  758. } else
  759. DOUT << "\t\tunable to assign the preferred register: "
  760. << mri_->getName(cur->preference) << "\n";
  761. // Scan for the first available register.
  762. TargetRegisterClass::iterator I = RC->allocation_order_begin(*mf_);
  763. TargetRegisterClass::iterator E = RC->allocation_order_end(*mf_);
  764. for (; I != E; ++I)
  765. if (prt_->isRegAvail(*I)) {
  766. FreeReg = *I;
  767. FreeRegInactiveCount = inactiveCounts[FreeReg];
  768. break;
  769. }
  770. // If there are no free regs, or if this reg has the max inactive count,
  771. // return this register.
  772. if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) return FreeReg;
  773. // Continue scanning the registers, looking for the one with the highest
  774. // inactive count. Alkis found that this reduced register pressure very
  775. // slightly on X86 (in rev 1.94 of this file), though this should probably be
  776. // reevaluated now.
  777. for (; I != E; ++I) {
  778. unsigned Reg = *I;
  779. if (prt_->isRegAvail(Reg) && FreeRegInactiveCount < inactiveCounts[Reg]) {
  780. FreeReg = Reg;
  781. FreeRegInactiveCount = inactiveCounts[Reg];
  782. if (FreeRegInactiveCount == MaxInactiveCount)
  783. break; // We found the one with the max inactive count.
  784. }
  785. }
  786. return FreeReg;
  787. }
  788. FunctionPass* llvm::createLinearScanRegisterAllocator() {
  789. return new RALinScan();
  790. }