RegAllocLinearScan.cpp 56 KB

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