RegAllocLocal.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. //===-- RegAllocLocal.cpp - A BasicBlock generic 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 register allocator allocates registers to a basic block at a time,
  11. // attempting to keep values in registers and reusing registers as appropriate.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #define DEBUG_TYPE "regalloc"
  15. #include "llvm/BasicBlock.h"
  16. #include "llvm/CodeGen/MachineFunctionPass.h"
  17. #include "llvm/CodeGen/MachineInstr.h"
  18. #include "llvm/CodeGen/MachineFrameInfo.h"
  19. #include "llvm/CodeGen/MachineRegisterInfo.h"
  20. #include "llvm/CodeGen/Passes.h"
  21. #include "llvm/CodeGen/RegAllocRegistry.h"
  22. #include "llvm/Target/TargetInstrInfo.h"
  23. #include "llvm/Target/TargetMachine.h"
  24. #include "llvm/Support/CommandLine.h"
  25. #include "llvm/Support/Debug.h"
  26. #include "llvm/Support/ErrorHandling.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. #include "llvm/ADT/DenseMap.h"
  29. #include "llvm/ADT/IndexedMap.h"
  30. #include "llvm/ADT/SmallSet.h"
  31. #include "llvm/ADT/SmallVector.h"
  32. #include "llvm/ADT/Statistic.h"
  33. #include "llvm/ADT/STLExtras.h"
  34. #include <algorithm>
  35. using namespace llvm;
  36. STATISTIC(NumStores, "Number of stores added");
  37. STATISTIC(NumLoads , "Number of loads added");
  38. static RegisterRegAlloc
  39. localRegAlloc("local", "local register allocator",
  40. createLocalRegisterAllocator);
  41. namespace {
  42. class RALocal : public MachineFunctionPass {
  43. public:
  44. static char ID;
  45. RALocal() : MachineFunctionPass(&ID), StackSlotForVirtReg(-1) {}
  46. private:
  47. const TargetMachine *TM;
  48. MachineFunction *MF;
  49. const TargetRegisterInfo *TRI;
  50. const TargetInstrInfo *TII;
  51. // StackSlotForVirtReg - Maps virtual regs to the frame index where these
  52. // values are spilled.
  53. IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
  54. // Virt2PhysRegMap - This map contains entries for each virtual register
  55. // that is currently available in a physical register.
  56. IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
  57. unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
  58. return Virt2PhysRegMap[VirtReg];
  59. }
  60. // PhysRegsUsed - This array is effectively a map, containing entries for
  61. // each physical register that currently has a value (ie, it is in
  62. // Virt2PhysRegMap). The value mapped to is the virtual register
  63. // corresponding to the physical register (the inverse of the
  64. // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned
  65. // because it is used by a future instruction, and to -2 if it is not
  66. // allocatable. If the entry for a physical register is -1, then the
  67. // physical register is "not in the map".
  68. //
  69. std::vector<int> PhysRegsUsed;
  70. // PhysRegsUseOrder - This contains a list of the physical registers that
  71. // currently have a virtual register value in them. This list provides an
  72. // ordering of registers, imposing a reallocation order. This list is only
  73. // used if all registers are allocated and we have to spill one, in which
  74. // case we spill the least recently used register. Entries at the front of
  75. // the list are the least recently used registers, entries at the back are
  76. // the most recently used.
  77. //
  78. std::vector<unsigned> PhysRegsUseOrder;
  79. // Virt2LastUseMap - This maps each virtual register to its last use
  80. // (MachineInstr*, operand index pair).
  81. IndexedMap<std::pair<MachineInstr*, unsigned>, VirtReg2IndexFunctor>
  82. Virt2LastUseMap;
  83. std::pair<MachineInstr*,unsigned>& getVirtRegLastUse(unsigned Reg) {
  84. assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
  85. return Virt2LastUseMap[Reg];
  86. }
  87. // VirtRegModified - This bitset contains information about which virtual
  88. // registers need to be spilled back to memory when their registers are
  89. // scavenged. If a virtual register has simply been rematerialized, there
  90. // is no reason to spill it to memory when we need the register back.
  91. //
  92. BitVector VirtRegModified;
  93. // UsedInMultipleBlocks - Tracks whether a particular register is used in
  94. // more than one block.
  95. BitVector UsedInMultipleBlocks;
  96. void markVirtRegModified(unsigned Reg, bool Val = true) {
  97. assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
  98. Reg -= TargetRegisterInfo::FirstVirtualRegister;
  99. if (Val)
  100. VirtRegModified.set(Reg);
  101. else
  102. VirtRegModified.reset(Reg);
  103. }
  104. bool isVirtRegModified(unsigned Reg) const {
  105. assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
  106. assert(Reg - TargetRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
  107. && "Illegal virtual register!");
  108. return VirtRegModified[Reg - TargetRegisterInfo::FirstVirtualRegister];
  109. }
  110. void AddToPhysRegsUseOrder(unsigned Reg) {
  111. std::vector<unsigned>::iterator It =
  112. std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), Reg);
  113. if (It != PhysRegsUseOrder.end())
  114. PhysRegsUseOrder.erase(It);
  115. PhysRegsUseOrder.push_back(Reg);
  116. }
  117. void MarkPhysRegRecentlyUsed(unsigned Reg) {
  118. if (PhysRegsUseOrder.empty() ||
  119. PhysRegsUseOrder.back() == Reg) return; // Already most recently used
  120. for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
  121. if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
  122. unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
  123. PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
  124. // Add it to the end of the list
  125. PhysRegsUseOrder.push_back(RegMatch);
  126. if (RegMatch == Reg)
  127. return; // Found an exact match, exit early
  128. }
  129. }
  130. public:
  131. virtual const char *getPassName() const {
  132. return "Local Register Allocator";
  133. }
  134. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  135. AU.setPreservesCFG();
  136. AU.addRequiredID(PHIEliminationID);
  137. AU.addRequiredID(TwoAddressInstructionPassID);
  138. MachineFunctionPass::getAnalysisUsage(AU);
  139. }
  140. private:
  141. /// runOnMachineFunction - Register allocate the whole function
  142. bool runOnMachineFunction(MachineFunction &Fn);
  143. /// AllocateBasicBlock - Register allocate the specified basic block.
  144. void AllocateBasicBlock(MachineBasicBlock &MBB);
  145. /// areRegsEqual - This method returns true if the specified registers are
  146. /// related to each other. To do this, it checks to see if they are equal
  147. /// or if the first register is in the alias set of the second register.
  148. ///
  149. bool areRegsEqual(unsigned R1, unsigned R2) const {
  150. if (R1 == R2) return true;
  151. for (const unsigned *AliasSet = TRI->getAliasSet(R2);
  152. *AliasSet; ++AliasSet) {
  153. if (*AliasSet == R1) return true;
  154. }
  155. return false;
  156. }
  157. /// getStackSpaceFor - This returns the frame index of the specified virtual
  158. /// register on the stack, allocating space if necessary.
  159. int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
  160. /// removePhysReg - This method marks the specified physical register as no
  161. /// longer being in use.
  162. ///
  163. void removePhysReg(unsigned PhysReg);
  164. /// spillVirtReg - This method spills the value specified by PhysReg into
  165. /// the virtual register slot specified by VirtReg. It then updates the RA
  166. /// data structures to indicate the fact that PhysReg is now available.
  167. ///
  168. void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
  169. unsigned VirtReg, unsigned PhysReg);
  170. /// spillPhysReg - This method spills the specified physical register into
  171. /// the virtual register slot associated with it. If OnlyVirtRegs is set to
  172. /// true, then the request is ignored if the physical register does not
  173. /// contain a virtual register.
  174. ///
  175. void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
  176. unsigned PhysReg, bool OnlyVirtRegs = false);
  177. /// assignVirtToPhysReg - This method updates local state so that we know
  178. /// that PhysReg is the proper container for VirtReg now. The physical
  179. /// register must not be used for anything else when this is called.
  180. ///
  181. void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
  182. /// isPhysRegAvailable - Return true if the specified physical register is
  183. /// free and available for use. This also includes checking to see if
  184. /// aliased registers are all free...
  185. ///
  186. bool isPhysRegAvailable(unsigned PhysReg) const;
  187. /// getFreeReg - Look to see if there is a free register available in the
  188. /// specified register class. If not, return 0.
  189. ///
  190. unsigned getFreeReg(const TargetRegisterClass *RC);
  191. /// getReg - Find a physical register to hold the specified virtual
  192. /// register. If all compatible physical registers are used, this method
  193. /// spills the last used virtual register to the stack, and uses that
  194. /// register. If NoFree is true, that means the caller knows there isn't
  195. /// a free register, do not call getFreeReg().
  196. unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
  197. unsigned VirtReg, bool NoFree = false);
  198. /// reloadVirtReg - This method transforms the specified virtual
  199. /// register use to refer to a physical register. This method may do this
  200. /// in one of several ways: if the register is available in a physical
  201. /// register already, it uses that physical register. If the value is not
  202. /// in a physical register, and if there are physical registers available,
  203. /// it loads it into a register. If register pressure is high, and it is
  204. /// possible, it tries to fold the load of the virtual register into the
  205. /// instruction itself. It avoids doing this if register pressure is low to
  206. /// improve the chance that subsequent instructions can use the reloaded
  207. /// value. This method returns the modified instruction.
  208. ///
  209. MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
  210. unsigned OpNum, SmallSet<unsigned, 4> &RRegs);
  211. /// ComputeLocalLiveness - Computes liveness of registers within a basic
  212. /// block, setting the killed/dead flags as appropriate.
  213. void ComputeLocalLiveness(MachineBasicBlock& MBB);
  214. void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
  215. unsigned PhysReg);
  216. };
  217. char RALocal::ID = 0;
  218. }
  219. /// getStackSpaceFor - This allocates space for the specified virtual register
  220. /// to be held on the stack.
  221. int RALocal::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
  222. // Find the location Reg would belong...
  223. int SS = StackSlotForVirtReg[VirtReg];
  224. if (SS != -1)
  225. return SS; // Already has space allocated?
  226. // Allocate a new stack object for this spill location...
  227. int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
  228. RC->getAlignment(),true);
  229. // Assign the slot...
  230. StackSlotForVirtReg[VirtReg] = FrameIdx;
  231. return FrameIdx;
  232. }
  233. /// removePhysReg - This method marks the specified physical register as no
  234. /// longer being in use.
  235. ///
  236. void RALocal::removePhysReg(unsigned PhysReg) {
  237. PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
  238. std::vector<unsigned>::iterator It =
  239. std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
  240. if (It != PhysRegsUseOrder.end())
  241. PhysRegsUseOrder.erase(It);
  242. }
  243. /// spillVirtReg - This method spills the value specified by PhysReg into the
  244. /// virtual register slot specified by VirtReg. It then updates the RA data
  245. /// structures to indicate the fact that PhysReg is now available.
  246. ///
  247. void RALocal::spillVirtReg(MachineBasicBlock &MBB,
  248. MachineBasicBlock::iterator I,
  249. unsigned VirtReg, unsigned PhysReg) {
  250. assert(VirtReg && "Spilling a physical register is illegal!"
  251. " Must not have appropriate kill for the register or use exists beyond"
  252. " the intended one.");
  253. DEBUG(errs() << " Spilling register " << TRI->getName(PhysReg)
  254. << " containing %reg" << VirtReg);
  255. if (!isVirtRegModified(VirtReg)) {
  256. DEBUG(errs() << " which has not been modified, so no store necessary!");
  257. std::pair<MachineInstr*, unsigned> &LastUse = getVirtRegLastUse(VirtReg);
  258. if (LastUse.first)
  259. LastUse.first->getOperand(LastUse.second).setIsKill();
  260. } else {
  261. // Otherwise, there is a virtual register corresponding to this physical
  262. // register. We only need to spill it into its stack slot if it has been
  263. // modified.
  264. const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
  265. int FrameIndex = getStackSpaceFor(VirtReg, RC);
  266. DEBUG(errs() << " to stack slot #" << FrameIndex);
  267. // If the instruction reads the register that's spilled, (e.g. this can
  268. // happen if it is a move to a physical register), then the spill
  269. // instruction is not a kill.
  270. bool isKill = !(I != MBB.end() && I->readsRegister(PhysReg));
  271. TII->storeRegToStackSlot(MBB, I, PhysReg, isKill, FrameIndex, RC);
  272. ++NumStores; // Update statistics
  273. }
  274. getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
  275. DEBUG(errs() << '\n');
  276. removePhysReg(PhysReg);
  277. }
  278. /// spillPhysReg - This method spills the specified physical register into the
  279. /// virtual register slot associated with it. If OnlyVirtRegs is set to true,
  280. /// then the request is ignored if the physical register does not contain a
  281. /// virtual register.
  282. ///
  283. void RALocal::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
  284. unsigned PhysReg, bool OnlyVirtRegs) {
  285. if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
  286. assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!");
  287. if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
  288. spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
  289. } else {
  290. // If the selected register aliases any other registers, we must make
  291. // sure that one of the aliases isn't alive.
  292. for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
  293. *AliasSet; ++AliasSet)
  294. if (PhysRegsUsed[*AliasSet] != -1 && // Spill aliased register.
  295. PhysRegsUsed[*AliasSet] != -2) // If allocatable.
  296. if (PhysRegsUsed[*AliasSet])
  297. spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
  298. }
  299. }
  300. /// assignVirtToPhysReg - This method updates local state so that we know
  301. /// that PhysReg is the proper container for VirtReg now. The physical
  302. /// register must not be used for anything else when this is called.
  303. ///
  304. void RALocal::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
  305. assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
  306. // Update information to note the fact that this register was just used, and
  307. // it holds VirtReg.
  308. PhysRegsUsed[PhysReg] = VirtReg;
  309. getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
  310. AddToPhysRegsUseOrder(PhysReg); // New use of PhysReg
  311. }
  312. /// isPhysRegAvailable - Return true if the specified physical register is free
  313. /// and available for use. This also includes checking to see if aliased
  314. /// registers are all free...
  315. ///
  316. bool RALocal::isPhysRegAvailable(unsigned PhysReg) const {
  317. if (PhysRegsUsed[PhysReg] != -1) return false;
  318. // If the selected register aliases any other allocated registers, it is
  319. // not free!
  320. for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
  321. *AliasSet; ++AliasSet)
  322. if (PhysRegsUsed[*AliasSet] >= 0) // Aliased register in use?
  323. return false; // Can't use this reg then.
  324. return true;
  325. }
  326. /// getFreeReg - Look to see if there is a free register available in the
  327. /// specified register class. If not, return 0.
  328. ///
  329. unsigned RALocal::getFreeReg(const TargetRegisterClass *RC) {
  330. // Get iterators defining the range of registers that are valid to allocate in
  331. // this class, which also specifies the preferred allocation order.
  332. TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
  333. TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
  334. for (; RI != RE; ++RI)
  335. if (isPhysRegAvailable(*RI)) { // Is reg unused?
  336. assert(*RI != 0 && "Cannot use register!");
  337. return *RI; // Found an unused register!
  338. }
  339. return 0;
  340. }
  341. /// getReg - Find a physical register to hold the specified virtual
  342. /// register. If all compatible physical registers are used, this method spills
  343. /// the last used virtual register to the stack, and uses that register.
  344. ///
  345. unsigned RALocal::getReg(MachineBasicBlock &MBB, MachineInstr *I,
  346. unsigned VirtReg, bool NoFree) {
  347. const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
  348. // First check to see if we have a free register of the requested type...
  349. unsigned PhysReg = NoFree ? 0 : getFreeReg(RC);
  350. // If we didn't find an unused register, scavenge one now!
  351. if (PhysReg == 0) {
  352. assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
  353. // Loop over all of the preallocated registers from the least recently used
  354. // to the most recently used. When we find one that is capable of holding
  355. // our register, use it.
  356. for (unsigned i = 0; PhysReg == 0; ++i) {
  357. assert(i != PhysRegsUseOrder.size() &&
  358. "Couldn't find a register of the appropriate class!");
  359. unsigned R = PhysRegsUseOrder[i];
  360. // We can only use this register if it holds a virtual register (ie, it
  361. // can be spilled). Do not use it if it is an explicitly allocated
  362. // physical register!
  363. assert(PhysRegsUsed[R] != -1 &&
  364. "PhysReg in PhysRegsUseOrder, but is not allocated?");
  365. if (PhysRegsUsed[R] && PhysRegsUsed[R] != -2) {
  366. // If the current register is compatible, use it.
  367. if (RC->contains(R)) {
  368. PhysReg = R;
  369. break;
  370. } else {
  371. // If one of the registers aliased to the current register is
  372. // compatible, use it.
  373. for (const unsigned *AliasIt = TRI->getAliasSet(R);
  374. *AliasIt; ++AliasIt) {
  375. if (RC->contains(*AliasIt) &&
  376. // If this is pinned down for some reason, don't use it. For
  377. // example, if CL is pinned, and we run across CH, don't use
  378. // CH as justification for using scavenging ECX (which will
  379. // fail).
  380. PhysRegsUsed[*AliasIt] != 0 &&
  381. // Make sure the register is allocatable. Don't allocate SIL on
  382. // x86-32.
  383. PhysRegsUsed[*AliasIt] != -2) {
  384. PhysReg = *AliasIt; // Take an aliased register
  385. break;
  386. }
  387. }
  388. }
  389. }
  390. }
  391. assert(PhysReg && "Physical register not assigned!?!?");
  392. // At this point PhysRegsUseOrder[i] is the least recently used register of
  393. // compatible register class. Spill it to memory and reap its remains.
  394. spillPhysReg(MBB, I, PhysReg);
  395. }
  396. // Now that we know which register we need to assign this to, do it now!
  397. assignVirtToPhysReg(VirtReg, PhysReg);
  398. return PhysReg;
  399. }
  400. /// reloadVirtReg - This method transforms the specified virtual
  401. /// register use to refer to a physical register. This method may do this in
  402. /// one of several ways: if the register is available in a physical register
  403. /// already, it uses that physical register. If the value is not in a physical
  404. /// register, and if there are physical registers available, it loads it into a
  405. /// register. If register pressure is high, and it is possible, it tries to
  406. /// fold the load of the virtual register into the instruction itself. It
  407. /// avoids doing this if register pressure is low to improve the chance that
  408. /// subsequent instructions can use the reloaded value. This method returns the
  409. /// modified instruction.
  410. ///
  411. MachineInstr *RALocal::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
  412. unsigned OpNum,
  413. SmallSet<unsigned, 4> &ReloadedRegs) {
  414. unsigned VirtReg = MI->getOperand(OpNum).getReg();
  415. // If the virtual register is already available, just update the instruction
  416. // and return.
  417. if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
  418. MarkPhysRegRecentlyUsed(PR); // Already have this value available!
  419. MI->getOperand(OpNum).setReg(PR); // Assign the input register
  420. getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
  421. return MI;
  422. }
  423. // Otherwise, we need to fold it into the current instruction, or reload it.
  424. // If we have registers available to hold the value, use them.
  425. const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
  426. unsigned PhysReg = getFreeReg(RC);
  427. int FrameIndex = getStackSpaceFor(VirtReg, RC);
  428. if (PhysReg) { // Register is available, allocate it!
  429. assignVirtToPhysReg(VirtReg, PhysReg);
  430. } else { // No registers available.
  431. // Force some poor hapless value out of the register file to
  432. // make room for the new register, and reload it.
  433. PhysReg = getReg(MBB, MI, VirtReg, true);
  434. }
  435. markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
  436. DEBUG(errs() << " Reloading %reg" << VirtReg << " into "
  437. << TRI->getName(PhysReg) << "\n");
  438. // Add move instruction(s)
  439. TII->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
  440. ++NumLoads; // Update statistics
  441. MF->getRegInfo().setPhysRegUsed(PhysReg);
  442. MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
  443. getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
  444. if (!ReloadedRegs.insert(PhysReg)) {
  445. std::string msg;
  446. raw_string_ostream Msg(msg);
  447. Msg << "Ran out of registers during register allocation!";
  448. if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
  449. Msg << "\nPlease check your inline asm statement for invalid "
  450. << "constraints:\n";
  451. MI->print(Msg, TM);
  452. }
  453. llvm_report_error(Msg.str());
  454. }
  455. for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
  456. *SubRegs; ++SubRegs) {
  457. if (!ReloadedRegs.insert(*SubRegs)) {
  458. std::string msg;
  459. raw_string_ostream Msg(msg);
  460. Msg << "Ran out of registers during register allocation!";
  461. if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
  462. Msg << "\nPlease check your inline asm statement for invalid "
  463. << "constraints:\n";
  464. MI->print(Msg, TM);
  465. }
  466. llvm_report_error(Msg.str());
  467. }
  468. }
  469. return MI;
  470. }
  471. /// isReadModWriteImplicitKill - True if this is an implicit kill for a
  472. /// read/mod/write register, i.e. update partial register.
  473. static bool isReadModWriteImplicitKill(MachineInstr *MI, unsigned Reg) {
  474. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  475. MachineOperand& MO = MI->getOperand(i);
  476. if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
  477. MO.isDef() && !MO.isDead())
  478. return true;
  479. }
  480. return false;
  481. }
  482. /// isReadModWriteImplicitDef - True if this is an implicit def for a
  483. /// read/mod/write register, i.e. update partial register.
  484. static bool isReadModWriteImplicitDef(MachineInstr *MI, unsigned Reg) {
  485. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  486. MachineOperand& MO = MI->getOperand(i);
  487. if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
  488. !MO.isDef() && MO.isKill())
  489. return true;
  490. }
  491. return false;
  492. }
  493. // precedes - Helper function to determine with MachineInstr A
  494. // precedes MachineInstr B within the same MBB.
  495. static bool precedes(MachineBasicBlock::iterator A,
  496. MachineBasicBlock::iterator B) {
  497. if (A == B)
  498. return false;
  499. MachineBasicBlock::iterator I = A->getParent()->begin();
  500. while (I != A->getParent()->end()) {
  501. if (I == A)
  502. return true;
  503. else if (I == B)
  504. return false;
  505. ++I;
  506. }
  507. return false;
  508. }
  509. /// ComputeLocalLiveness - Computes liveness of registers within a basic
  510. /// block, setting the killed/dead flags as appropriate.
  511. void RALocal::ComputeLocalLiveness(MachineBasicBlock& MBB) {
  512. MachineRegisterInfo& MRI = MBB.getParent()->getRegInfo();
  513. // Keep track of the most recently seen previous use or def of each reg,
  514. // so that we can update them with dead/kill markers.
  515. DenseMap<unsigned, std::pair<MachineInstr*, unsigned> > LastUseDef;
  516. for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
  517. I != E; ++I) {
  518. for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
  519. MachineOperand& MO = I->getOperand(i);
  520. // Uses don't trigger any flags, but we need to save
  521. // them for later. Also, we have to process these
  522. // _before_ processing the defs, since an instr
  523. // uses regs before it defs them.
  524. if (MO.isReg() && MO.getReg() && MO.isUse()) {
  525. LastUseDef[MO.getReg()] = std::make_pair(I, i);
  526. if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) continue;
  527. const unsigned* Aliases = TRI->getAliasSet(MO.getReg());
  528. if (Aliases) {
  529. while (*Aliases) {
  530. DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
  531. alias = LastUseDef.find(*Aliases);
  532. if (alias != LastUseDef.end() && alias->second.first != I)
  533. LastUseDef[*Aliases] = std::make_pair(I, i);
  534. ++Aliases;
  535. }
  536. }
  537. }
  538. }
  539. for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
  540. MachineOperand& MO = I->getOperand(i);
  541. // Defs others than 2-addr redefs _do_ trigger flag changes:
  542. // - A def followed by a def is dead
  543. // - A use followed by a def is a kill
  544. if (MO.isReg() && MO.getReg() && MO.isDef()) {
  545. DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
  546. last = LastUseDef.find(MO.getReg());
  547. if (last != LastUseDef.end()) {
  548. // Check if this is a two address instruction. If so, then
  549. // the def does not kill the use.
  550. if (last->second.first == I &&
  551. I->isRegTiedToUseOperand(i))
  552. continue;
  553. MachineOperand& lastUD =
  554. last->second.first->getOperand(last->second.second);
  555. if (lastUD.isDef())
  556. lastUD.setIsDead(true);
  557. else
  558. lastUD.setIsKill(true);
  559. }
  560. LastUseDef[MO.getReg()] = std::make_pair(I, i);
  561. }
  562. }
  563. }
  564. // Live-out (of the function) registers contain return values of the function,
  565. // so we need to make sure they are alive at return time.
  566. if (!MBB.empty() && MBB.back().getDesc().isReturn()) {
  567. MachineInstr* Ret = &MBB.back();
  568. for (MachineRegisterInfo::liveout_iterator
  569. I = MF->getRegInfo().liveout_begin(),
  570. E = MF->getRegInfo().liveout_end(); I != E; ++I)
  571. if (!Ret->readsRegister(*I)) {
  572. Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
  573. LastUseDef[*I] = std::make_pair(Ret, Ret->getNumOperands()-1);
  574. }
  575. }
  576. // Finally, loop over the final use/def of each reg
  577. // in the block and determine if it is dead.
  578. for (DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
  579. I = LastUseDef.begin(), E = LastUseDef.end(); I != E; ++I) {
  580. MachineInstr* MI = I->second.first;
  581. unsigned idx = I->second.second;
  582. MachineOperand& MO = MI->getOperand(idx);
  583. bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(MO.getReg());
  584. // A crude approximation of "live-out" calculation
  585. bool usedOutsideBlock = isPhysReg ? false :
  586. UsedInMultipleBlocks.test(MO.getReg() -
  587. TargetRegisterInfo::FirstVirtualRegister);
  588. if (!isPhysReg && !usedOutsideBlock)
  589. for (MachineRegisterInfo::reg_iterator UI = MRI.reg_begin(MO.getReg()),
  590. UE = MRI.reg_end(); UI != UE; ++UI)
  591. // Two cases:
  592. // - used in another block
  593. // - used in the same block before it is defined (loop)
  594. if (UI->getParent() != &MBB ||
  595. (MO.isDef() && UI.getOperand().isUse() && precedes(&*UI, MI))) {
  596. UsedInMultipleBlocks.set(MO.getReg() -
  597. TargetRegisterInfo::FirstVirtualRegister);
  598. usedOutsideBlock = true;
  599. break;
  600. }
  601. // Physical registers and those that are not live-out of the block
  602. // are killed/dead at their last use/def within this block.
  603. if (isPhysReg || !usedOutsideBlock) {
  604. if (MO.isUse()) {
  605. // Don't mark uses that are tied to defs as kills.
  606. if (!MI->isRegTiedToDefOperand(idx))
  607. MO.setIsKill(true);
  608. } else
  609. MO.setIsDead(true);
  610. }
  611. }
  612. }
  613. void RALocal::AllocateBasicBlock(MachineBasicBlock &MBB) {
  614. // loop over each instruction
  615. MachineBasicBlock::iterator MII = MBB.begin();
  616. DEBUG({
  617. const BasicBlock *LBB = MBB.getBasicBlock();
  618. if (LBB)
  619. errs() << "\nStarting RegAlloc of BB: " << LBB->getName();
  620. });
  621. // Add live-in registers as active.
  622. for (MachineBasicBlock::livein_iterator I = MBB.livein_begin(),
  623. E = MBB.livein_end(); I != E; ++I) {
  624. unsigned Reg = *I;
  625. MF->getRegInfo().setPhysRegUsed(Reg);
  626. PhysRegsUsed[Reg] = 0; // It is free and reserved now
  627. AddToPhysRegsUseOrder(Reg);
  628. for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
  629. *SubRegs; ++SubRegs) {
  630. if (PhysRegsUsed[*SubRegs] != -2) {
  631. AddToPhysRegsUseOrder(*SubRegs);
  632. PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
  633. MF->getRegInfo().setPhysRegUsed(*SubRegs);
  634. }
  635. }
  636. }
  637. ComputeLocalLiveness(MBB);
  638. // Otherwise, sequentially allocate each instruction in the MBB.
  639. while (MII != MBB.end()) {
  640. MachineInstr *MI = MII++;
  641. const TargetInstrDesc &TID = MI->getDesc();
  642. DEBUG({
  643. errs() << "\nStarting RegAlloc of: " << *MI;
  644. errs() << " Regs have values: ";
  645. for (unsigned i = 0; i != TRI->getNumRegs(); ++i)
  646. if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
  647. errs() << "[" << TRI->getName(i)
  648. << ",%reg" << PhysRegsUsed[i] << "] ";
  649. errs() << '\n';
  650. });
  651. // Loop over the implicit uses, making sure that they are at the head of the
  652. // use order list, so they don't get reallocated.
  653. if (TID.ImplicitUses) {
  654. for (const unsigned *ImplicitUses = TID.ImplicitUses;
  655. *ImplicitUses; ++ImplicitUses)
  656. MarkPhysRegRecentlyUsed(*ImplicitUses);
  657. }
  658. SmallVector<unsigned, 8> Kills;
  659. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  660. MachineOperand& MO = MI->getOperand(i);
  661. if (MO.isReg() && MO.isKill()) {
  662. if (!MO.isImplicit())
  663. Kills.push_back(MO.getReg());
  664. else if (!isReadModWriteImplicitKill(MI, MO.getReg()))
  665. // These are extra physical register kills when a sub-register
  666. // is defined (def of a sub-register is a read/mod/write of the
  667. // larger registers). Ignore.
  668. Kills.push_back(MO.getReg());
  669. }
  670. }
  671. // If any physical regs are earlyclobber, spill any value they might
  672. // have in them, then mark them unallocatable.
  673. // If any virtual regs are earlyclobber, allocate them now (before
  674. // freeing inputs that are killed).
  675. if (MI->getOpcode()==TargetInstrInfo::INLINEASM) {
  676. for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
  677. MachineOperand& MO = MI->getOperand(i);
  678. if (MO.isReg() && MO.isDef() && MO.isEarlyClobber() &&
  679. MO.getReg()) {
  680. if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
  681. unsigned DestVirtReg = MO.getReg();
  682. unsigned DestPhysReg;
  683. // If DestVirtReg already has a value, use it.
  684. if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
  685. DestPhysReg = getReg(MBB, MI, DestVirtReg);
  686. MF->getRegInfo().setPhysRegUsed(DestPhysReg);
  687. markVirtRegModified(DestVirtReg);
  688. getVirtRegLastUse(DestVirtReg) =
  689. std::make_pair((MachineInstr*)0, 0);
  690. DEBUG(errs() << " Assigning " << TRI->getName(DestPhysReg)
  691. << " to %reg" << DestVirtReg << "\n");
  692. MO.setReg(DestPhysReg); // Assign the earlyclobber register
  693. } else {
  694. unsigned Reg = MO.getReg();
  695. if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
  696. // These are extra physical register defs when a sub-register
  697. // is defined (def of a sub-register is a read/mod/write of the
  698. // larger registers). Ignore.
  699. if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
  700. MF->getRegInfo().setPhysRegUsed(Reg);
  701. spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
  702. PhysRegsUsed[Reg] = 0; // It is free and reserved now
  703. AddToPhysRegsUseOrder(Reg);
  704. for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
  705. *SubRegs; ++SubRegs) {
  706. if (PhysRegsUsed[*SubRegs] != -2) {
  707. MF->getRegInfo().setPhysRegUsed(*SubRegs);
  708. PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
  709. AddToPhysRegsUseOrder(*SubRegs);
  710. }
  711. }
  712. }
  713. }
  714. }
  715. }
  716. // Get the used operands into registers. This has the potential to spill
  717. // incoming values if we are out of registers. Note that we completely
  718. // ignore physical register uses here. We assume that if an explicit
  719. // physical register is referenced by the instruction, that it is guaranteed
  720. // to be live-in, or the input is badly hosed.
  721. //
  722. SmallSet<unsigned, 4> ReloadedRegs;
  723. for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
  724. MachineOperand& MO = MI->getOperand(i);
  725. // here we are looking for only used operands (never def&use)
  726. if (MO.isReg() && !MO.isDef() && MO.getReg() && !MO.isImplicit() &&
  727. TargetRegisterInfo::isVirtualRegister(MO.getReg()))
  728. MI = reloadVirtReg(MBB, MI, i, ReloadedRegs);
  729. }
  730. // If this instruction is the last user of this register, kill the
  731. // value, freeing the register being used, so it doesn't need to be
  732. // spilled to memory.
  733. //
  734. for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
  735. unsigned VirtReg = Kills[i];
  736. unsigned PhysReg = VirtReg;
  737. if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
  738. // If the virtual register was never materialized into a register, it
  739. // might not be in the map, but it won't hurt to zero it out anyway.
  740. unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
  741. PhysReg = PhysRegSlot;
  742. PhysRegSlot = 0;
  743. } else if (PhysRegsUsed[PhysReg] == -2) {
  744. // Unallocatable register dead, ignore.
  745. continue;
  746. } else {
  747. assert((!PhysRegsUsed[PhysReg] || PhysRegsUsed[PhysReg] == -1) &&
  748. "Silently clearing a virtual register?");
  749. }
  750. if (PhysReg) {
  751. DEBUG(errs() << " Last use of " << TRI->getName(PhysReg)
  752. << "[%reg" << VirtReg <<"], removing it from live set\n");
  753. removePhysReg(PhysReg);
  754. for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
  755. *SubRegs; ++SubRegs) {
  756. if (PhysRegsUsed[*SubRegs] != -2) {
  757. DEBUG(errs() << " Last use of "
  758. << TRI->getName(*SubRegs) << "[%reg" << VirtReg
  759. <<"], removing it from live set\n");
  760. removePhysReg(*SubRegs);
  761. }
  762. }
  763. }
  764. }
  765. // Loop over all of the operands of the instruction, spilling registers that
  766. // are defined, and marking explicit destinations in the PhysRegsUsed map.
  767. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  768. MachineOperand& MO = MI->getOperand(i);
  769. if (MO.isReg() && MO.isDef() && !MO.isImplicit() && MO.getReg() &&
  770. !MO.isEarlyClobber() &&
  771. TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
  772. unsigned Reg = MO.getReg();
  773. if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
  774. // These are extra physical register defs when a sub-register
  775. // is defined (def of a sub-register is a read/mod/write of the
  776. // larger registers). Ignore.
  777. if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
  778. MF->getRegInfo().setPhysRegUsed(Reg);
  779. spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
  780. PhysRegsUsed[Reg] = 0; // It is free and reserved now
  781. AddToPhysRegsUseOrder(Reg);
  782. for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
  783. *SubRegs; ++SubRegs) {
  784. if (PhysRegsUsed[*SubRegs] != -2) {
  785. MF->getRegInfo().setPhysRegUsed(*SubRegs);
  786. PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
  787. AddToPhysRegsUseOrder(*SubRegs);
  788. }
  789. }
  790. }
  791. }
  792. // Loop over the implicit defs, spilling them as well.
  793. if (TID.ImplicitDefs) {
  794. for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
  795. *ImplicitDefs; ++ImplicitDefs) {
  796. unsigned Reg = *ImplicitDefs;
  797. if (PhysRegsUsed[Reg] != -2) {
  798. spillPhysReg(MBB, MI, Reg, true);
  799. AddToPhysRegsUseOrder(Reg);
  800. PhysRegsUsed[Reg] = 0; // It is free and reserved now
  801. }
  802. MF->getRegInfo().setPhysRegUsed(Reg);
  803. for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
  804. *SubRegs; ++SubRegs) {
  805. if (PhysRegsUsed[*SubRegs] != -2) {
  806. AddToPhysRegsUseOrder(*SubRegs);
  807. PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
  808. MF->getRegInfo().setPhysRegUsed(*SubRegs);
  809. }
  810. }
  811. }
  812. }
  813. SmallVector<unsigned, 8> DeadDefs;
  814. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  815. MachineOperand& MO = MI->getOperand(i);
  816. if (MO.isReg() && MO.isDead())
  817. DeadDefs.push_back(MO.getReg());
  818. }
  819. // Okay, we have allocated all of the source operands and spilled any values
  820. // that would be destroyed by defs of this instruction. Loop over the
  821. // explicit defs and assign them to a register, spilling incoming values if
  822. // we need to scavenge a register.
  823. //
  824. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  825. MachineOperand& MO = MI->getOperand(i);
  826. if (MO.isReg() && MO.isDef() && MO.getReg() &&
  827. !MO.isEarlyClobber() &&
  828. TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
  829. unsigned DestVirtReg = MO.getReg();
  830. unsigned DestPhysReg;
  831. // If DestVirtReg already has a value, use it.
  832. if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
  833. DestPhysReg = getReg(MBB, MI, DestVirtReg);
  834. MF->getRegInfo().setPhysRegUsed(DestPhysReg);
  835. markVirtRegModified(DestVirtReg);
  836. getVirtRegLastUse(DestVirtReg) = std::make_pair((MachineInstr*)0, 0);
  837. DEBUG(errs() << " Assigning " << TRI->getName(DestPhysReg)
  838. << " to %reg" << DestVirtReg << "\n");
  839. MO.setReg(DestPhysReg); // Assign the output register
  840. }
  841. }
  842. // If this instruction defines any registers that are immediately dead,
  843. // kill them now.
  844. //
  845. for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) {
  846. unsigned VirtReg = DeadDefs[i];
  847. unsigned PhysReg = VirtReg;
  848. if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
  849. unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
  850. PhysReg = PhysRegSlot;
  851. assert(PhysReg != 0);
  852. PhysRegSlot = 0;
  853. } else if (PhysRegsUsed[PhysReg] == -2) {
  854. // Unallocatable register dead, ignore.
  855. continue;
  856. }
  857. if (PhysReg) {
  858. DEBUG(errs() << " Register " << TRI->getName(PhysReg)
  859. << " [%reg" << VirtReg
  860. << "] is never used, removing it from live set\n");
  861. removePhysReg(PhysReg);
  862. for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
  863. *AliasSet; ++AliasSet) {
  864. if (PhysRegsUsed[*AliasSet] != -2) {
  865. DEBUG(errs() << " Register " << TRI->getName(*AliasSet)
  866. << " [%reg" << *AliasSet
  867. << "] is never used, removing it from live set\n");
  868. removePhysReg(*AliasSet);
  869. }
  870. }
  871. }
  872. }
  873. // Finally, if this is a noop copy instruction, zap it. (Except that if
  874. // the copy is dead, it must be kept to avoid messing up liveness info for
  875. // the register scavenger. See pr4100.)
  876. unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
  877. if (TII->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
  878. SrcReg == DstReg && DeadDefs.empty())
  879. MBB.erase(MI);
  880. }
  881. MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
  882. // Spill all physical registers holding virtual registers now.
  883. for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
  884. if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2) {
  885. if (unsigned VirtReg = PhysRegsUsed[i])
  886. spillVirtReg(MBB, MI, VirtReg, i);
  887. else
  888. removePhysReg(i);
  889. }
  890. #if 0
  891. // This checking code is very expensive.
  892. bool AllOk = true;
  893. for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
  894. e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
  895. if (unsigned PR = Virt2PhysRegMap[i]) {
  896. cerr << "Register still mapped: " << i << " -> " << PR << "\n";
  897. AllOk = false;
  898. }
  899. assert(AllOk && "Virtual registers still in phys regs?");
  900. #endif
  901. // Clear any physical register which appear live at the end of the basic
  902. // block, but which do not hold any virtual registers. e.g., the stack
  903. // pointer.
  904. PhysRegsUseOrder.clear();
  905. }
  906. /// runOnMachineFunction - Register allocate the whole function
  907. ///
  908. bool RALocal::runOnMachineFunction(MachineFunction &Fn) {
  909. DEBUG(errs() << "Machine Function\n");
  910. MF = &Fn;
  911. TM = &Fn.getTarget();
  912. TRI = TM->getRegisterInfo();
  913. TII = TM->getInstrInfo();
  914. PhysRegsUsed.assign(TRI->getNumRegs(), -1);
  915. // At various places we want to efficiently check to see whether a register
  916. // is allocatable. To handle this, we mark all unallocatable registers as
  917. // being pinned down, permanently.
  918. {
  919. BitVector Allocable = TRI->getAllocatableSet(Fn);
  920. for (unsigned i = 0, e = Allocable.size(); i != e; ++i)
  921. if (!Allocable[i])
  922. PhysRegsUsed[i] = -2; // Mark the reg unallocable.
  923. }
  924. // initialize the virtual->physical register map to have a 'null'
  925. // mapping for all virtual registers
  926. unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg();
  927. StackSlotForVirtReg.grow(LastVirtReg);
  928. Virt2PhysRegMap.grow(LastVirtReg);
  929. Virt2LastUseMap.grow(LastVirtReg);
  930. VirtRegModified.resize(LastVirtReg+1-TargetRegisterInfo::FirstVirtualRegister);
  931. UsedInMultipleBlocks.resize(LastVirtReg+1-TargetRegisterInfo::FirstVirtualRegister);
  932. // Loop over all of the basic blocks, eliminating virtual register references
  933. for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
  934. MBB != MBBe; ++MBB)
  935. AllocateBasicBlock(*MBB);
  936. StackSlotForVirtReg.clear();
  937. PhysRegsUsed.clear();
  938. VirtRegModified.clear();
  939. UsedInMultipleBlocks.clear();
  940. Virt2PhysRegMap.clear();
  941. Virt2LastUseMap.clear();
  942. return true;
  943. }
  944. FunctionPass *llvm::createLocalRegisterAllocator() {
  945. return new RALocal();
  946. }