RegAllocFast.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  1. //===- RegAllocFast.cpp - A fast register allocator for debug code --------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. /// \file This register allocator allocates registers to a basic block at a
  10. /// time, attempting to keep values in registers and reusing registers as
  11. /// appropriate.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/IndexedMap.h"
  17. #include "llvm/ADT/MapVector.h"
  18. #include "llvm/ADT/SmallSet.h"
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/ADT/SparseSet.h"
  21. #include "llvm/ADT/Statistic.h"
  22. #include "llvm/CodeGen/MachineBasicBlock.h"
  23. #include "llvm/CodeGen/MachineFrameInfo.h"
  24. #include "llvm/CodeGen/MachineFunction.h"
  25. #include "llvm/CodeGen/MachineFunctionPass.h"
  26. #include "llvm/CodeGen/MachineInstr.h"
  27. #include "llvm/CodeGen/MachineInstrBuilder.h"
  28. #include "llvm/CodeGen/MachineOperand.h"
  29. #include "llvm/CodeGen/MachineRegisterInfo.h"
  30. #include "llvm/CodeGen/RegAllocCommon.h"
  31. #include "llvm/CodeGen/RegAllocRegistry.h"
  32. #include "llvm/CodeGen/RegisterClassInfo.h"
  33. #include "llvm/CodeGen/TargetInstrInfo.h"
  34. #include "llvm/CodeGen/TargetOpcodes.h"
  35. #include "llvm/CodeGen/TargetRegisterInfo.h"
  36. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  37. #include "llvm/IR/DebugLoc.h"
  38. #include "llvm/IR/Metadata.h"
  39. #include "llvm/InitializePasses.h"
  40. #include "llvm/MC/MCInstrDesc.h"
  41. #include "llvm/MC/MCRegisterInfo.h"
  42. #include "llvm/Pass.h"
  43. #include "llvm/Support/Casting.h"
  44. #include "llvm/Support/Compiler.h"
  45. #include "llvm/Support/Debug.h"
  46. #include "llvm/Support/ErrorHandling.h"
  47. #include "llvm/Support/raw_ostream.h"
  48. #include <cassert>
  49. #include <tuple>
  50. #include <vector>
  51. using namespace llvm;
  52. #define DEBUG_TYPE "regalloc"
  53. STATISTIC(NumStores, "Number of stores added");
  54. STATISTIC(NumLoads , "Number of loads added");
  55. STATISTIC(NumCoalesced, "Number of copies coalesced");
  56. // FIXME: Remove this switch when all testcases are fixed!
  57. static cl::opt<bool> IgnoreMissingDefs("rafast-ignore-missing-defs",
  58. cl::Hidden);
  59. static RegisterRegAlloc
  60. fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
  61. namespace {
  62. class RegAllocFast : public MachineFunctionPass {
  63. public:
  64. static char ID;
  65. RegAllocFast(const RegClassFilterFunc F = allocateAllRegClasses,
  66. bool ClearVirtRegs_ = true) :
  67. MachineFunctionPass(ID),
  68. ShouldAllocateClass(F),
  69. StackSlotForVirtReg(-1),
  70. ClearVirtRegs(ClearVirtRegs_) {
  71. }
  72. private:
  73. MachineFrameInfo *MFI;
  74. MachineRegisterInfo *MRI;
  75. const TargetRegisterInfo *TRI;
  76. const TargetInstrInfo *TII;
  77. RegisterClassInfo RegClassInfo;
  78. const RegClassFilterFunc ShouldAllocateClass;
  79. /// Basic block currently being allocated.
  80. MachineBasicBlock *MBB;
  81. /// Maps virtual regs to the frame index where these values are spilled.
  82. IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
  83. bool ClearVirtRegs;
  84. /// Everything we know about a live virtual register.
  85. struct LiveReg {
  86. MachineInstr *LastUse = nullptr; ///< Last instr to use reg.
  87. Register VirtReg; ///< Virtual register number.
  88. MCPhysReg PhysReg = 0; ///< Currently held here.
  89. bool LiveOut = false; ///< Register is possibly live out.
  90. bool Reloaded = false; ///< Register was reloaded.
  91. bool Error = false; ///< Could not allocate.
  92. explicit LiveReg(Register VirtReg) : VirtReg(VirtReg) {}
  93. unsigned getSparseSetIndex() const {
  94. return Register::virtReg2Index(VirtReg);
  95. }
  96. };
  97. using LiveRegMap = SparseSet<LiveReg>;
  98. /// This map contains entries for each virtual register that is currently
  99. /// available in a physical register.
  100. LiveRegMap LiveVirtRegs;
  101. /// Stores assigned virtual registers present in the bundle MI.
  102. DenseMap<Register, MCPhysReg> BundleVirtRegsMap;
  103. DenseMap<unsigned, SmallVector<MachineOperand *, 2>> LiveDbgValueMap;
  104. /// List of DBG_VALUE that we encountered without the vreg being assigned
  105. /// because they were placed after the last use of the vreg.
  106. DenseMap<unsigned, SmallVector<MachineInstr *, 1>> DanglingDbgValues;
  107. /// Has a bit set for every virtual register for which it was determined
  108. /// that it is alive across blocks.
  109. BitVector MayLiveAcrossBlocks;
  110. /// State of a register unit.
  111. enum RegUnitState {
  112. /// A free register is not currently in use and can be allocated
  113. /// immediately without checking aliases.
  114. regFree,
  115. /// A pre-assigned register has been assigned before register allocation
  116. /// (e.g., setting up a call parameter).
  117. regPreAssigned,
  118. /// Used temporarily in reloadAtBegin() to mark register units that are
  119. /// live-in to the basic block.
  120. regLiveIn,
  121. /// A register state may also be a virtual register number, indication
  122. /// that the physical register is currently allocated to a virtual
  123. /// register. In that case, LiveVirtRegs contains the inverse mapping.
  124. };
  125. /// Maps each physical register to a RegUnitState enum or virtual register.
  126. std::vector<unsigned> RegUnitStates;
  127. SmallVector<MachineInstr *, 32> Coalesced;
  128. using RegUnitSet = SparseSet<uint16_t, identity<uint16_t>>;
  129. /// Set of register units that are used in the current instruction, and so
  130. /// cannot be allocated.
  131. RegUnitSet UsedInInstr;
  132. RegUnitSet PhysRegUses;
  133. SmallVector<uint16_t, 8> DefOperandIndexes;
  134. // Register masks attached to the current instruction.
  135. SmallVector<const uint32_t *> RegMasks;
  136. void setPhysRegState(MCPhysReg PhysReg, unsigned NewState);
  137. bool isPhysRegFree(MCPhysReg PhysReg) const;
  138. /// Mark a physreg as used in this instruction.
  139. void markRegUsedInInstr(MCPhysReg PhysReg) {
  140. for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
  141. UsedInInstr.insert(*Units);
  142. }
  143. // Check if physreg is clobbered by instruction's regmask(s).
  144. bool isClobberedByRegMasks(MCPhysReg PhysReg) const {
  145. return llvm::any_of(RegMasks, [PhysReg](const uint32_t *Mask) {
  146. return MachineOperand::clobbersPhysReg(Mask, PhysReg);
  147. });
  148. }
  149. /// Check if a physreg or any of its aliases are used in this instruction.
  150. bool isRegUsedInInstr(MCPhysReg PhysReg, bool LookAtPhysRegUses) const {
  151. if (LookAtPhysRegUses && isClobberedByRegMasks(PhysReg))
  152. return true;
  153. for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
  154. if (UsedInInstr.count(*Units))
  155. return true;
  156. if (LookAtPhysRegUses && PhysRegUses.count(*Units))
  157. return true;
  158. }
  159. return false;
  160. }
  161. /// Mark physical register as being used in a register use operand.
  162. /// This is only used by the special livethrough handling code.
  163. void markPhysRegUsedInInstr(MCPhysReg PhysReg) {
  164. for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
  165. PhysRegUses.insert(*Units);
  166. }
  167. /// Remove mark of physical register being used in the instruction.
  168. void unmarkRegUsedInInstr(MCPhysReg PhysReg) {
  169. for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
  170. UsedInInstr.erase(*Units);
  171. }
  172. enum : unsigned {
  173. spillClean = 50,
  174. spillDirty = 100,
  175. spillPrefBonus = 20,
  176. spillImpossible = ~0u
  177. };
  178. public:
  179. StringRef getPassName() const override { return "Fast Register Allocator"; }
  180. void getAnalysisUsage(AnalysisUsage &AU) const override {
  181. AU.setPreservesCFG();
  182. MachineFunctionPass::getAnalysisUsage(AU);
  183. }
  184. MachineFunctionProperties getRequiredProperties() const override {
  185. return MachineFunctionProperties().set(
  186. MachineFunctionProperties::Property::NoPHIs);
  187. }
  188. MachineFunctionProperties getSetProperties() const override {
  189. if (ClearVirtRegs) {
  190. return MachineFunctionProperties().set(
  191. MachineFunctionProperties::Property::NoVRegs);
  192. }
  193. return MachineFunctionProperties();
  194. }
  195. MachineFunctionProperties getClearedProperties() const override {
  196. return MachineFunctionProperties().set(
  197. MachineFunctionProperties::Property::IsSSA);
  198. }
  199. private:
  200. bool runOnMachineFunction(MachineFunction &MF) override;
  201. void allocateBasicBlock(MachineBasicBlock &MBB);
  202. void addRegClassDefCounts(std::vector<unsigned> &RegClassDefCounts,
  203. Register Reg) const;
  204. void allocateInstruction(MachineInstr &MI);
  205. void handleDebugValue(MachineInstr &MI);
  206. void handleBundle(MachineInstr &MI);
  207. bool usePhysReg(MachineInstr &MI, MCPhysReg PhysReg);
  208. bool definePhysReg(MachineInstr &MI, MCPhysReg PhysReg);
  209. bool displacePhysReg(MachineInstr &MI, MCPhysReg PhysReg);
  210. void freePhysReg(MCPhysReg PhysReg);
  211. unsigned calcSpillCost(MCPhysReg PhysReg) const;
  212. LiveRegMap::iterator findLiveVirtReg(Register VirtReg) {
  213. return LiveVirtRegs.find(Register::virtReg2Index(VirtReg));
  214. }
  215. LiveRegMap::const_iterator findLiveVirtReg(Register VirtReg) const {
  216. return LiveVirtRegs.find(Register::virtReg2Index(VirtReg));
  217. }
  218. void assignVirtToPhysReg(MachineInstr &MI, LiveReg &, MCPhysReg PhysReg);
  219. void allocVirtReg(MachineInstr &MI, LiveReg &LR, Register Hint,
  220. bool LookAtPhysRegUses = false);
  221. void allocVirtRegUndef(MachineOperand &MO);
  222. void assignDanglingDebugValues(MachineInstr &Def, Register VirtReg,
  223. MCPhysReg Reg);
  224. void defineLiveThroughVirtReg(MachineInstr &MI, unsigned OpNum,
  225. Register VirtReg);
  226. void defineVirtReg(MachineInstr &MI, unsigned OpNum, Register VirtReg,
  227. bool LookAtPhysRegUses = false);
  228. void useVirtReg(MachineInstr &MI, unsigned OpNum, Register VirtReg);
  229. MachineBasicBlock::iterator
  230. getMBBBeginInsertionPoint(MachineBasicBlock &MBB,
  231. SmallSet<Register, 2> &PrologLiveIns) const;
  232. void reloadAtBegin(MachineBasicBlock &MBB);
  233. void setPhysReg(MachineInstr &MI, MachineOperand &MO, MCPhysReg PhysReg);
  234. Register traceCopies(Register VirtReg) const;
  235. Register traceCopyChain(Register Reg) const;
  236. int getStackSpaceFor(Register VirtReg);
  237. void spill(MachineBasicBlock::iterator Before, Register VirtReg,
  238. MCPhysReg AssignedReg, bool Kill, bool LiveOut);
  239. void reload(MachineBasicBlock::iterator Before, Register VirtReg,
  240. MCPhysReg PhysReg);
  241. bool mayLiveOut(Register VirtReg);
  242. bool mayLiveIn(Register VirtReg);
  243. void dumpState() const;
  244. };
  245. } // end anonymous namespace
  246. char RegAllocFast::ID = 0;
  247. INITIALIZE_PASS(RegAllocFast, "regallocfast", "Fast Register Allocator", false,
  248. false)
  249. void RegAllocFast::setPhysRegState(MCPhysReg PhysReg, unsigned NewState) {
  250. for (MCRegUnitIterator UI(PhysReg, TRI); UI.isValid(); ++UI)
  251. RegUnitStates[*UI] = NewState;
  252. }
  253. bool RegAllocFast::isPhysRegFree(MCPhysReg PhysReg) const {
  254. for (MCRegUnitIterator UI(PhysReg, TRI); UI.isValid(); ++UI) {
  255. if (RegUnitStates[*UI] != regFree)
  256. return false;
  257. }
  258. return true;
  259. }
  260. /// This allocates space for the specified virtual register to be held on the
  261. /// stack.
  262. int RegAllocFast::getStackSpaceFor(Register VirtReg) {
  263. // Find the location Reg would belong...
  264. int SS = StackSlotForVirtReg[VirtReg];
  265. // Already has space allocated?
  266. if (SS != -1)
  267. return SS;
  268. // Allocate a new stack object for this spill location...
  269. const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
  270. unsigned Size = TRI->getSpillSize(RC);
  271. Align Alignment = TRI->getSpillAlign(RC);
  272. int FrameIdx = MFI->CreateSpillStackObject(Size, Alignment);
  273. // Assign the slot.
  274. StackSlotForVirtReg[VirtReg] = FrameIdx;
  275. return FrameIdx;
  276. }
  277. static bool dominates(MachineBasicBlock &MBB,
  278. MachineBasicBlock::const_iterator A,
  279. MachineBasicBlock::const_iterator B) {
  280. auto MBBEnd = MBB.end();
  281. if (B == MBBEnd)
  282. return true;
  283. MachineBasicBlock::const_iterator I = MBB.begin();
  284. for (; &*I != A && &*I != B; ++I)
  285. ;
  286. return &*I == A;
  287. }
  288. /// Returns false if \p VirtReg is known to not live out of the current block.
  289. bool RegAllocFast::mayLiveOut(Register VirtReg) {
  290. if (MayLiveAcrossBlocks.test(Register::virtReg2Index(VirtReg))) {
  291. // Cannot be live-out if there are no successors.
  292. return !MBB->succ_empty();
  293. }
  294. const MachineInstr *SelfLoopDef = nullptr;
  295. // If this block loops back to itself, it is necessary to check whether the
  296. // use comes after the def.
  297. if (MBB->isSuccessor(MBB)) {
  298. SelfLoopDef = MRI->getUniqueVRegDef(VirtReg);
  299. if (!SelfLoopDef) {
  300. MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg));
  301. return true;
  302. }
  303. }
  304. // See if the first \p Limit uses of the register are all in the current
  305. // block.
  306. static const unsigned Limit = 8;
  307. unsigned C = 0;
  308. for (const MachineInstr &UseInst : MRI->use_nodbg_instructions(VirtReg)) {
  309. if (UseInst.getParent() != MBB || ++C >= Limit) {
  310. MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg));
  311. // Cannot be live-out if there are no successors.
  312. return !MBB->succ_empty();
  313. }
  314. if (SelfLoopDef) {
  315. // Try to handle some simple cases to avoid spilling and reloading every
  316. // value inside a self looping block.
  317. if (SelfLoopDef == &UseInst ||
  318. !dominates(*MBB, SelfLoopDef->getIterator(), UseInst.getIterator())) {
  319. MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg));
  320. return true;
  321. }
  322. }
  323. }
  324. return false;
  325. }
  326. /// Returns false if \p VirtReg is known to not be live into the current block.
  327. bool RegAllocFast::mayLiveIn(Register VirtReg) {
  328. if (MayLiveAcrossBlocks.test(Register::virtReg2Index(VirtReg)))
  329. return !MBB->pred_empty();
  330. // See if the first \p Limit def of the register are all in the current block.
  331. static const unsigned Limit = 8;
  332. unsigned C = 0;
  333. for (const MachineInstr &DefInst : MRI->def_instructions(VirtReg)) {
  334. if (DefInst.getParent() != MBB || ++C >= Limit) {
  335. MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg));
  336. return !MBB->pred_empty();
  337. }
  338. }
  339. return false;
  340. }
  341. /// Insert spill instruction for \p AssignedReg before \p Before. Update
  342. /// DBG_VALUEs with \p VirtReg operands with the stack slot.
  343. void RegAllocFast::spill(MachineBasicBlock::iterator Before, Register VirtReg,
  344. MCPhysReg AssignedReg, bool Kill, bool LiveOut) {
  345. LLVM_DEBUG(dbgs() << "Spilling " << printReg(VirtReg, TRI)
  346. << " in " << printReg(AssignedReg, TRI));
  347. int FI = getStackSpaceFor(VirtReg);
  348. LLVM_DEBUG(dbgs() << " to stack slot #" << FI << '\n');
  349. const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
  350. TII->storeRegToStackSlot(*MBB, Before, AssignedReg, Kill, FI, &RC, TRI);
  351. ++NumStores;
  352. MachineBasicBlock::iterator FirstTerm = MBB->getFirstTerminator();
  353. // When we spill a virtual register, we will have spill instructions behind
  354. // every definition of it, meaning we can switch all the DBG_VALUEs over
  355. // to just reference the stack slot.
  356. SmallVectorImpl<MachineOperand *> &LRIDbgOperands = LiveDbgValueMap[VirtReg];
  357. SmallMapVector<MachineInstr *, SmallVector<const MachineOperand *>, 2>
  358. SpilledOperandsMap;
  359. for (MachineOperand *MO : LRIDbgOperands)
  360. SpilledOperandsMap[MO->getParent()].push_back(MO);
  361. for (auto MISpilledOperands : SpilledOperandsMap) {
  362. MachineInstr &DBG = *MISpilledOperands.first;
  363. MachineInstr *NewDV = buildDbgValueForSpill(
  364. *MBB, Before, *MISpilledOperands.first, FI, MISpilledOperands.second);
  365. assert(NewDV->getParent() == MBB && "dangling parent pointer");
  366. (void)NewDV;
  367. LLVM_DEBUG(dbgs() << "Inserting debug info due to spill:\n" << *NewDV);
  368. if (LiveOut) {
  369. // We need to insert a DBG_VALUE at the end of the block if the spill slot
  370. // is live out, but there is another use of the value after the
  371. // spill. This will allow LiveDebugValues to see the correct live out
  372. // value to propagate to the successors.
  373. MachineInstr *ClonedDV = MBB->getParent()->CloneMachineInstr(NewDV);
  374. MBB->insert(FirstTerm, ClonedDV);
  375. LLVM_DEBUG(dbgs() << "Cloning debug info due to live out spill\n");
  376. }
  377. // Rewrite unassigned dbg_values to use the stack slot.
  378. // TODO We can potentially do this for list debug values as well if we know
  379. // how the dbg_values are getting unassigned.
  380. if (DBG.isNonListDebugValue()) {
  381. MachineOperand &MO = DBG.getDebugOperand(0);
  382. if (MO.isReg() && MO.getReg() == 0) {
  383. updateDbgValueForSpill(DBG, FI, 0);
  384. }
  385. }
  386. }
  387. // Now this register is spilled there is should not be any DBG_VALUE
  388. // pointing to this register because they are all pointing to spilled value
  389. // now.
  390. LRIDbgOperands.clear();
  391. }
  392. /// Insert reload instruction for \p PhysReg before \p Before.
  393. void RegAllocFast::reload(MachineBasicBlock::iterator Before, Register VirtReg,
  394. MCPhysReg PhysReg) {
  395. LLVM_DEBUG(dbgs() << "Reloading " << printReg(VirtReg, TRI) << " into "
  396. << printReg(PhysReg, TRI) << '\n');
  397. int FI = getStackSpaceFor(VirtReg);
  398. const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
  399. TII->loadRegFromStackSlot(*MBB, Before, PhysReg, FI, &RC, TRI);
  400. ++NumLoads;
  401. }
  402. /// Get basic block begin insertion point.
  403. /// This is not just MBB.begin() because surprisingly we have EH_LABEL
  404. /// instructions marking the begin of a basic block. This means we must insert
  405. /// new instructions after such labels...
  406. MachineBasicBlock::iterator
  407. RegAllocFast::getMBBBeginInsertionPoint(
  408. MachineBasicBlock &MBB, SmallSet<Register, 2> &PrologLiveIns) const {
  409. MachineBasicBlock::iterator I = MBB.begin();
  410. while (I != MBB.end()) {
  411. if (I->isLabel()) {
  412. ++I;
  413. continue;
  414. }
  415. // Most reloads should be inserted after prolog instructions.
  416. if (!TII->isBasicBlockPrologue(*I))
  417. break;
  418. // However if a prolog instruction reads a register that needs to be
  419. // reloaded, the reload should be inserted before the prolog.
  420. for (MachineOperand &MO : I->operands()) {
  421. if (MO.isReg())
  422. PrologLiveIns.insert(MO.getReg());
  423. }
  424. ++I;
  425. }
  426. return I;
  427. }
  428. /// Reload all currently assigned virtual registers.
  429. void RegAllocFast::reloadAtBegin(MachineBasicBlock &MBB) {
  430. if (LiveVirtRegs.empty())
  431. return;
  432. for (MachineBasicBlock::RegisterMaskPair P : MBB.liveins()) {
  433. MCPhysReg Reg = P.PhysReg;
  434. // Set state to live-in. This possibly overrides mappings to virtual
  435. // registers but we don't care anymore at this point.
  436. setPhysRegState(Reg, regLiveIn);
  437. }
  438. SmallSet<Register, 2> PrologLiveIns;
  439. // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
  440. // of spilling here is deterministic, if arbitrary.
  441. MachineBasicBlock::iterator InsertBefore
  442. = getMBBBeginInsertionPoint(MBB, PrologLiveIns);
  443. for (const LiveReg &LR : LiveVirtRegs) {
  444. MCPhysReg PhysReg = LR.PhysReg;
  445. if (PhysReg == 0)
  446. continue;
  447. MCRegister FirstUnit = *MCRegUnitIterator(PhysReg, TRI);
  448. if (RegUnitStates[FirstUnit] == regLiveIn)
  449. continue;
  450. assert((&MBB != &MBB.getParent()->front() || IgnoreMissingDefs) &&
  451. "no reload in start block. Missing vreg def?");
  452. if (PrologLiveIns.count(PhysReg)) {
  453. // FIXME: Theoretically this should use an insert point skipping labels
  454. // but I'm not sure how labels should interact with prolog instruction
  455. // that need reloads.
  456. reload(MBB.begin(), LR.VirtReg, PhysReg);
  457. } else
  458. reload(InsertBefore, LR.VirtReg, PhysReg);
  459. }
  460. LiveVirtRegs.clear();
  461. }
  462. /// Handle the direct use of a physical register. Check that the register is
  463. /// not used by a virtreg. Kill the physreg, marking it free. This may add
  464. /// implicit kills to MO->getParent() and invalidate MO.
  465. bool RegAllocFast::usePhysReg(MachineInstr &MI, MCPhysReg Reg) {
  466. assert(Register::isPhysicalRegister(Reg) && "expected physreg");
  467. bool displacedAny = displacePhysReg(MI, Reg);
  468. setPhysRegState(Reg, regPreAssigned);
  469. markRegUsedInInstr(Reg);
  470. return displacedAny;
  471. }
  472. bool RegAllocFast::definePhysReg(MachineInstr &MI, MCPhysReg Reg) {
  473. bool displacedAny = displacePhysReg(MI, Reg);
  474. setPhysRegState(Reg, regPreAssigned);
  475. return displacedAny;
  476. }
  477. /// Mark PhysReg as reserved or free after spilling any virtregs. This is very
  478. /// similar to defineVirtReg except the physreg is reserved instead of
  479. /// allocated.
  480. bool RegAllocFast::displacePhysReg(MachineInstr &MI, MCPhysReg PhysReg) {
  481. bool displacedAny = false;
  482. for (MCRegUnitIterator UI(PhysReg, TRI); UI.isValid(); ++UI) {
  483. unsigned Unit = *UI;
  484. switch (unsigned VirtReg = RegUnitStates[Unit]) {
  485. default: {
  486. LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
  487. assert(LRI != LiveVirtRegs.end() && "datastructures in sync");
  488. MachineBasicBlock::iterator ReloadBefore =
  489. std::next((MachineBasicBlock::iterator)MI.getIterator());
  490. reload(ReloadBefore, VirtReg, LRI->PhysReg);
  491. setPhysRegState(LRI->PhysReg, regFree);
  492. LRI->PhysReg = 0;
  493. LRI->Reloaded = true;
  494. displacedAny = true;
  495. break;
  496. }
  497. case regPreAssigned:
  498. RegUnitStates[Unit] = regFree;
  499. displacedAny = true;
  500. break;
  501. case regFree:
  502. break;
  503. }
  504. }
  505. return displacedAny;
  506. }
  507. void RegAllocFast::freePhysReg(MCPhysReg PhysReg) {
  508. LLVM_DEBUG(dbgs() << "Freeing " << printReg(PhysReg, TRI) << ':');
  509. MCRegister FirstUnit = *MCRegUnitIterator(PhysReg, TRI);
  510. switch (unsigned VirtReg = RegUnitStates[FirstUnit]) {
  511. case regFree:
  512. LLVM_DEBUG(dbgs() << '\n');
  513. return;
  514. case regPreAssigned:
  515. LLVM_DEBUG(dbgs() << '\n');
  516. setPhysRegState(PhysReg, regFree);
  517. return;
  518. default: {
  519. LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
  520. assert(LRI != LiveVirtRegs.end());
  521. LLVM_DEBUG(dbgs() << ' ' << printReg(LRI->VirtReg, TRI) << '\n');
  522. setPhysRegState(LRI->PhysReg, regFree);
  523. LRI->PhysReg = 0;
  524. }
  525. return;
  526. }
  527. }
  528. /// Return the cost of spilling clearing out PhysReg and aliases so it is free
  529. /// for allocation. Returns 0 when PhysReg is free or disabled with all aliases
  530. /// disabled - it can be allocated directly.
  531. /// \returns spillImpossible when PhysReg or an alias can't be spilled.
  532. unsigned RegAllocFast::calcSpillCost(MCPhysReg PhysReg) const {
  533. for (MCRegUnitIterator UI(PhysReg, TRI); UI.isValid(); ++UI) {
  534. switch (unsigned VirtReg = RegUnitStates[*UI]) {
  535. case regFree:
  536. break;
  537. case regPreAssigned:
  538. LLVM_DEBUG(dbgs() << "Cannot spill pre-assigned "
  539. << printReg(PhysReg, TRI) << '\n');
  540. return spillImpossible;
  541. default: {
  542. bool SureSpill = StackSlotForVirtReg[VirtReg] != -1 ||
  543. findLiveVirtReg(VirtReg)->LiveOut;
  544. return SureSpill ? spillClean : spillDirty;
  545. }
  546. }
  547. }
  548. return 0;
  549. }
  550. void RegAllocFast::assignDanglingDebugValues(MachineInstr &Definition,
  551. Register VirtReg, MCPhysReg Reg) {
  552. auto UDBGValIter = DanglingDbgValues.find(VirtReg);
  553. if (UDBGValIter == DanglingDbgValues.end())
  554. return;
  555. SmallVectorImpl<MachineInstr*> &Dangling = UDBGValIter->second;
  556. for (MachineInstr *DbgValue : Dangling) {
  557. assert(DbgValue->isDebugValue());
  558. if (!DbgValue->hasDebugOperandForReg(VirtReg))
  559. continue;
  560. // Test whether the physreg survives from the definition to the DBG_VALUE.
  561. MCPhysReg SetToReg = Reg;
  562. unsigned Limit = 20;
  563. for (MachineBasicBlock::iterator I = std::next(Definition.getIterator()),
  564. E = DbgValue->getIterator(); I != E; ++I) {
  565. if (I->modifiesRegister(Reg, TRI) || --Limit == 0) {
  566. LLVM_DEBUG(dbgs() << "Register did not survive for " << *DbgValue
  567. << '\n');
  568. SetToReg = 0;
  569. break;
  570. }
  571. }
  572. for (MachineOperand &MO : DbgValue->getDebugOperandsForReg(VirtReg)) {
  573. MO.setReg(SetToReg);
  574. if (SetToReg != 0)
  575. MO.setIsRenamable();
  576. }
  577. }
  578. Dangling.clear();
  579. }
  580. /// This method updates local state so that we know that PhysReg is the
  581. /// proper container for VirtReg now. The physical register must not be used
  582. /// for anything else when this is called.
  583. void RegAllocFast::assignVirtToPhysReg(MachineInstr &AtMI, LiveReg &LR,
  584. MCPhysReg PhysReg) {
  585. Register VirtReg = LR.VirtReg;
  586. LLVM_DEBUG(dbgs() << "Assigning " << printReg(VirtReg, TRI) << " to "
  587. << printReg(PhysReg, TRI) << '\n');
  588. assert(LR.PhysReg == 0 && "Already assigned a physreg");
  589. assert(PhysReg != 0 && "Trying to assign no register");
  590. LR.PhysReg = PhysReg;
  591. setPhysRegState(PhysReg, VirtReg);
  592. assignDanglingDebugValues(AtMI, VirtReg, PhysReg);
  593. }
  594. static bool isCoalescable(const MachineInstr &MI) {
  595. return MI.isFullCopy();
  596. }
  597. Register RegAllocFast::traceCopyChain(Register Reg) const {
  598. static const unsigned ChainLengthLimit = 3;
  599. unsigned C = 0;
  600. do {
  601. if (Reg.isPhysical())
  602. return Reg;
  603. assert(Reg.isVirtual());
  604. MachineInstr *VRegDef = MRI->getUniqueVRegDef(Reg);
  605. if (!VRegDef || !isCoalescable(*VRegDef))
  606. return 0;
  607. Reg = VRegDef->getOperand(1).getReg();
  608. } while (++C <= ChainLengthLimit);
  609. return 0;
  610. }
  611. /// Check if any of \p VirtReg's definitions is a copy. If it is follow the
  612. /// chain of copies to check whether we reach a physical register we can
  613. /// coalesce with.
  614. Register RegAllocFast::traceCopies(Register VirtReg) const {
  615. static const unsigned DefLimit = 3;
  616. unsigned C = 0;
  617. for (const MachineInstr &MI : MRI->def_instructions(VirtReg)) {
  618. if (isCoalescable(MI)) {
  619. Register Reg = MI.getOperand(1).getReg();
  620. Reg = traceCopyChain(Reg);
  621. if (Reg.isValid())
  622. return Reg;
  623. }
  624. if (++C >= DefLimit)
  625. break;
  626. }
  627. return Register();
  628. }
  629. /// Allocates a physical register for VirtReg.
  630. void RegAllocFast::allocVirtReg(MachineInstr &MI, LiveReg &LR,
  631. Register Hint0, bool LookAtPhysRegUses) {
  632. const Register VirtReg = LR.VirtReg;
  633. assert(LR.PhysReg == 0);
  634. const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
  635. LLVM_DEBUG(dbgs() << "Search register for " << printReg(VirtReg)
  636. << " in class " << TRI->getRegClassName(&RC)
  637. << " with hint " << printReg(Hint0, TRI) << '\n');
  638. // Take hint when possible.
  639. if (Hint0.isPhysical() && MRI->isAllocatable(Hint0) && RC.contains(Hint0) &&
  640. !isRegUsedInInstr(Hint0, LookAtPhysRegUses)) {
  641. // Take hint if the register is currently free.
  642. if (isPhysRegFree(Hint0)) {
  643. LLVM_DEBUG(dbgs() << "\tPreferred Register 1: " << printReg(Hint0, TRI)
  644. << '\n');
  645. assignVirtToPhysReg(MI, LR, Hint0);
  646. return;
  647. } else {
  648. LLVM_DEBUG(dbgs() << "\tPreferred Register 0: " << printReg(Hint0, TRI)
  649. << " occupied\n");
  650. }
  651. } else {
  652. Hint0 = Register();
  653. }
  654. // Try other hint.
  655. Register Hint1 = traceCopies(VirtReg);
  656. if (Hint1.isPhysical() && MRI->isAllocatable(Hint1) && RC.contains(Hint1) &&
  657. !isRegUsedInInstr(Hint1, LookAtPhysRegUses)) {
  658. // Take hint if the register is currently free.
  659. if (isPhysRegFree(Hint1)) {
  660. LLVM_DEBUG(dbgs() << "\tPreferred Register 0: " << printReg(Hint1, TRI)
  661. << '\n');
  662. assignVirtToPhysReg(MI, LR, Hint1);
  663. return;
  664. } else {
  665. LLVM_DEBUG(dbgs() << "\tPreferred Register 1: " << printReg(Hint1, TRI)
  666. << " occupied\n");
  667. }
  668. } else {
  669. Hint1 = Register();
  670. }
  671. MCPhysReg BestReg = 0;
  672. unsigned BestCost = spillImpossible;
  673. ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC);
  674. for (MCPhysReg PhysReg : AllocationOrder) {
  675. LLVM_DEBUG(dbgs() << "\tRegister: " << printReg(PhysReg, TRI) << ' ');
  676. if (isRegUsedInInstr(PhysReg, LookAtPhysRegUses)) {
  677. LLVM_DEBUG(dbgs() << "already used in instr.\n");
  678. continue;
  679. }
  680. unsigned Cost = calcSpillCost(PhysReg);
  681. LLVM_DEBUG(dbgs() << "Cost: " << Cost << " BestCost: " << BestCost << '\n');
  682. // Immediate take a register with cost 0.
  683. if (Cost == 0) {
  684. assignVirtToPhysReg(MI, LR, PhysReg);
  685. return;
  686. }
  687. if (PhysReg == Hint0 || PhysReg == Hint1)
  688. Cost -= spillPrefBonus;
  689. if (Cost < BestCost) {
  690. BestReg = PhysReg;
  691. BestCost = Cost;
  692. }
  693. }
  694. if (!BestReg) {
  695. // Nothing we can do: Report an error and keep going with an invalid
  696. // allocation.
  697. if (MI.isInlineAsm())
  698. MI.emitError("inline assembly requires more registers than available");
  699. else
  700. MI.emitError("ran out of registers during register allocation");
  701. LR.Error = true;
  702. LR.PhysReg = 0;
  703. return;
  704. }
  705. displacePhysReg(MI, BestReg);
  706. assignVirtToPhysReg(MI, LR, BestReg);
  707. }
  708. void RegAllocFast::allocVirtRegUndef(MachineOperand &MO) {
  709. assert(MO.isUndef() && "expected undef use");
  710. Register VirtReg = MO.getReg();
  711. assert(Register::isVirtualRegister(VirtReg) && "Expected virtreg");
  712. LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg);
  713. MCPhysReg PhysReg;
  714. if (LRI != LiveVirtRegs.end() && LRI->PhysReg) {
  715. PhysReg = LRI->PhysReg;
  716. } else {
  717. const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
  718. ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC);
  719. assert(!AllocationOrder.empty() && "Allocation order must not be empty");
  720. PhysReg = AllocationOrder[0];
  721. }
  722. unsigned SubRegIdx = MO.getSubReg();
  723. if (SubRegIdx != 0) {
  724. PhysReg = TRI->getSubReg(PhysReg, SubRegIdx);
  725. MO.setSubReg(0);
  726. }
  727. MO.setReg(PhysReg);
  728. MO.setIsRenamable(true);
  729. }
  730. /// Variation of defineVirtReg() with special handling for livethrough regs
  731. /// (tied or earlyclobber) that may interfere with preassigned uses.
  732. void RegAllocFast::defineLiveThroughVirtReg(MachineInstr &MI, unsigned OpNum,
  733. Register VirtReg) {
  734. LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
  735. if (LRI != LiveVirtRegs.end()) {
  736. MCPhysReg PrevReg = LRI->PhysReg;
  737. if (PrevReg != 0 && isRegUsedInInstr(PrevReg, true)) {
  738. LLVM_DEBUG(dbgs() << "Need new assignment for " << printReg(PrevReg, TRI)
  739. << " (tied/earlyclobber resolution)\n");
  740. freePhysReg(PrevReg);
  741. LRI->PhysReg = 0;
  742. allocVirtReg(MI, *LRI, 0, true);
  743. MachineBasicBlock::iterator InsertBefore =
  744. std::next((MachineBasicBlock::iterator)MI.getIterator());
  745. LLVM_DEBUG(dbgs() << "Copy " << printReg(LRI->PhysReg, TRI) << " to "
  746. << printReg(PrevReg, TRI) << '\n');
  747. BuildMI(*MBB, InsertBefore, MI.getDebugLoc(),
  748. TII->get(TargetOpcode::COPY), PrevReg)
  749. .addReg(LRI->PhysReg, llvm::RegState::Kill);
  750. }
  751. MachineOperand &MO = MI.getOperand(OpNum);
  752. if (MO.getSubReg() && !MO.isUndef()) {
  753. LRI->LastUse = &MI;
  754. }
  755. }
  756. return defineVirtReg(MI, OpNum, VirtReg, true);
  757. }
  758. /// Allocates a register for VirtReg definition. Typically the register is
  759. /// already assigned from a use of the virtreg, however we still need to
  760. /// perform an allocation if:
  761. /// - It is a dead definition without any uses.
  762. /// - The value is live out and all uses are in different basic blocks.
  763. void RegAllocFast::defineVirtReg(MachineInstr &MI, unsigned OpNum,
  764. Register VirtReg, bool LookAtPhysRegUses) {
  765. assert(VirtReg.isVirtual() && "Not a virtual register");
  766. MachineOperand &MO = MI.getOperand(OpNum);
  767. LiveRegMap::iterator LRI;
  768. bool New;
  769. std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
  770. if (New) {
  771. if (!MO.isDead()) {
  772. if (mayLiveOut(VirtReg)) {
  773. LRI->LiveOut = true;
  774. } else {
  775. // It is a dead def without the dead flag; add the flag now.
  776. MO.setIsDead(true);
  777. }
  778. }
  779. }
  780. if (LRI->PhysReg == 0)
  781. allocVirtReg(MI, *LRI, 0, LookAtPhysRegUses);
  782. else {
  783. assert(!isRegUsedInInstr(LRI->PhysReg, LookAtPhysRegUses) &&
  784. "TODO: preassign mismatch");
  785. LLVM_DEBUG(dbgs() << "In def of " << printReg(VirtReg, TRI)
  786. << " use existing assignment to "
  787. << printReg(LRI->PhysReg, TRI) << '\n');
  788. }
  789. MCPhysReg PhysReg = LRI->PhysReg;
  790. assert(PhysReg != 0 && "Register not assigned");
  791. if (LRI->Reloaded || LRI->LiveOut) {
  792. if (!MI.isImplicitDef()) {
  793. MachineBasicBlock::iterator SpillBefore =
  794. std::next((MachineBasicBlock::iterator)MI.getIterator());
  795. LLVM_DEBUG(dbgs() << "Spill Reason: LO: " << LRI->LiveOut << " RL: "
  796. << LRI->Reloaded << '\n');
  797. bool Kill = LRI->LastUse == nullptr;
  798. spill(SpillBefore, VirtReg, PhysReg, Kill, LRI->LiveOut);
  799. LRI->LastUse = nullptr;
  800. }
  801. LRI->LiveOut = false;
  802. LRI->Reloaded = false;
  803. }
  804. if (MI.getOpcode() == TargetOpcode::BUNDLE) {
  805. BundleVirtRegsMap[VirtReg] = PhysReg;
  806. }
  807. markRegUsedInInstr(PhysReg);
  808. setPhysReg(MI, MO, PhysReg);
  809. }
  810. /// Allocates a register for a VirtReg use.
  811. void RegAllocFast::useVirtReg(MachineInstr &MI, unsigned OpNum,
  812. Register VirtReg) {
  813. assert(VirtReg.isVirtual() && "Not a virtual register");
  814. MachineOperand &MO = MI.getOperand(OpNum);
  815. LiveRegMap::iterator LRI;
  816. bool New;
  817. std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
  818. if (New) {
  819. MachineOperand &MO = MI.getOperand(OpNum);
  820. if (!MO.isKill()) {
  821. if (mayLiveOut(VirtReg)) {
  822. LRI->LiveOut = true;
  823. } else {
  824. // It is a last (killing) use without the kill flag; add the flag now.
  825. MO.setIsKill(true);
  826. }
  827. }
  828. } else {
  829. assert((!MO.isKill() || LRI->LastUse == &MI) && "Invalid kill flag");
  830. }
  831. // If necessary allocate a register.
  832. if (LRI->PhysReg == 0) {
  833. assert(!MO.isTied() && "tied op should be allocated");
  834. Register Hint;
  835. if (MI.isCopy() && MI.getOperand(1).getSubReg() == 0) {
  836. Hint = MI.getOperand(0).getReg();
  837. assert(Hint.isPhysical() &&
  838. "Copy destination should already be assigned");
  839. }
  840. allocVirtReg(MI, *LRI, Hint, false);
  841. if (LRI->Error) {
  842. const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
  843. ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC);
  844. setPhysReg(MI, MO, *AllocationOrder.begin());
  845. return;
  846. }
  847. }
  848. LRI->LastUse = &MI;
  849. if (MI.getOpcode() == TargetOpcode::BUNDLE) {
  850. BundleVirtRegsMap[VirtReg] = LRI->PhysReg;
  851. }
  852. markRegUsedInInstr(LRI->PhysReg);
  853. setPhysReg(MI, MO, LRI->PhysReg);
  854. }
  855. /// Changes operand OpNum in MI the refer the PhysReg, considering subregs. This
  856. /// may invalidate any operand pointers. Return true if the operand kills its
  857. /// register.
  858. void RegAllocFast::setPhysReg(MachineInstr &MI, MachineOperand &MO,
  859. MCPhysReg PhysReg) {
  860. if (!MO.getSubReg()) {
  861. MO.setReg(PhysReg);
  862. MO.setIsRenamable(true);
  863. return;
  864. }
  865. // Handle subregister index.
  866. MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : MCRegister());
  867. MO.setIsRenamable(true);
  868. // Note: We leave the subreg number around a little longer in case of defs.
  869. // This is so that the register freeing logic in allocateInstruction can still
  870. // recognize this as subregister defs. The code there will clear the number.
  871. if (!MO.isDef())
  872. MO.setSubReg(0);
  873. // A kill flag implies killing the full register. Add corresponding super
  874. // register kill.
  875. if (MO.isKill()) {
  876. MI.addRegisterKilled(PhysReg, TRI, true);
  877. return;
  878. }
  879. // A <def,read-undef> of a sub-register requires an implicit def of the full
  880. // register.
  881. if (MO.isDef() && MO.isUndef()) {
  882. if (MO.isDead())
  883. MI.addRegisterDead(PhysReg, TRI, true);
  884. else
  885. MI.addRegisterDefined(PhysReg, TRI);
  886. }
  887. }
  888. #ifndef NDEBUG
  889. void RegAllocFast::dumpState() const {
  890. for (unsigned Unit = 1, UnitE = TRI->getNumRegUnits(); Unit != UnitE;
  891. ++Unit) {
  892. switch (unsigned VirtReg = RegUnitStates[Unit]) {
  893. case regFree:
  894. break;
  895. case regPreAssigned:
  896. dbgs() << " " << printRegUnit(Unit, TRI) << "[P]";
  897. break;
  898. case regLiveIn:
  899. llvm_unreachable("Should not have regLiveIn in map");
  900. default: {
  901. dbgs() << ' ' << printRegUnit(Unit, TRI) << '=' << printReg(VirtReg);
  902. LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
  903. assert(I != LiveVirtRegs.end() && "have LiveVirtRegs entry");
  904. if (I->LiveOut || I->Reloaded) {
  905. dbgs() << '[';
  906. if (I->LiveOut) dbgs() << 'O';
  907. if (I->Reloaded) dbgs() << 'R';
  908. dbgs() << ']';
  909. }
  910. assert(TRI->hasRegUnit(I->PhysReg, Unit) && "inverse mapping present");
  911. break;
  912. }
  913. }
  914. }
  915. dbgs() << '\n';
  916. // Check that LiveVirtRegs is the inverse.
  917. for (const LiveReg &LR : LiveVirtRegs) {
  918. Register VirtReg = LR.VirtReg;
  919. assert(VirtReg.isVirtual() && "Bad map key");
  920. MCPhysReg PhysReg = LR.PhysReg;
  921. if (PhysReg != 0) {
  922. assert(Register::isPhysicalRegister(PhysReg) &&
  923. "mapped to physreg");
  924. for (MCRegUnitIterator UI(PhysReg, TRI); UI.isValid(); ++UI) {
  925. assert(RegUnitStates[*UI] == VirtReg && "inverse map valid");
  926. }
  927. }
  928. }
  929. }
  930. #endif
  931. /// Count number of defs consumed from each register class by \p Reg
  932. void RegAllocFast::addRegClassDefCounts(std::vector<unsigned> &RegClassDefCounts,
  933. Register Reg) const {
  934. assert(RegClassDefCounts.size() == TRI->getNumRegClasses());
  935. if (Reg.isVirtual()) {
  936. const TargetRegisterClass *OpRC = MRI->getRegClass(Reg);
  937. for (unsigned RCIdx = 0, RCIdxEnd = TRI->getNumRegClasses();
  938. RCIdx != RCIdxEnd; ++RCIdx) {
  939. const TargetRegisterClass *IdxRC = TRI->getRegClass(RCIdx);
  940. // FIXME: Consider aliasing sub/super registers.
  941. if (OpRC->hasSubClassEq(IdxRC))
  942. ++RegClassDefCounts[RCIdx];
  943. }
  944. return;
  945. }
  946. for (unsigned RCIdx = 0, RCIdxEnd = TRI->getNumRegClasses();
  947. RCIdx != RCIdxEnd; ++RCIdx) {
  948. const TargetRegisterClass *IdxRC = TRI->getRegClass(RCIdx);
  949. for (MCRegAliasIterator Alias(Reg, TRI, true); Alias.isValid(); ++Alias) {
  950. if (IdxRC->contains(*Alias)) {
  951. ++RegClassDefCounts[RCIdx];
  952. break;
  953. }
  954. }
  955. }
  956. }
  957. void RegAllocFast::allocateInstruction(MachineInstr &MI) {
  958. // The basic algorithm here is:
  959. // 1. Mark registers of def operands as free
  960. // 2. Allocate registers to use operands and place reload instructions for
  961. // registers displaced by the allocation.
  962. //
  963. // However we need to handle some corner cases:
  964. // - pre-assigned defs and uses need to be handled before the other def/use
  965. // operands are processed to avoid the allocation heuristics clashing with
  966. // the pre-assignment.
  967. // - The "free def operands" step has to come last instead of first for tied
  968. // operands and early-clobbers.
  969. UsedInInstr.clear();
  970. RegMasks.clear();
  971. BundleVirtRegsMap.clear();
  972. // Scan for special cases; Apply pre-assigned register defs to state.
  973. bool HasPhysRegUse = false;
  974. bool HasRegMask = false;
  975. bool HasVRegDef = false;
  976. bool HasDef = false;
  977. bool HasEarlyClobber = false;
  978. bool NeedToAssignLiveThroughs = false;
  979. for (MachineOperand &MO : MI.operands()) {
  980. if (MO.isReg()) {
  981. Register Reg = MO.getReg();
  982. if (Reg.isVirtual()) {
  983. if (MO.isDef()) {
  984. HasDef = true;
  985. HasVRegDef = true;
  986. if (MO.isEarlyClobber()) {
  987. HasEarlyClobber = true;
  988. NeedToAssignLiveThroughs = true;
  989. }
  990. if (MO.isTied() || (MO.getSubReg() != 0 && !MO.isUndef()))
  991. NeedToAssignLiveThroughs = true;
  992. }
  993. } else if (Reg.isPhysical()) {
  994. if (!MRI->isReserved(Reg)) {
  995. if (MO.isDef()) {
  996. HasDef = true;
  997. bool displacedAny = definePhysReg(MI, Reg);
  998. if (MO.isEarlyClobber())
  999. HasEarlyClobber = true;
  1000. if (!displacedAny)
  1001. MO.setIsDead(true);
  1002. }
  1003. if (MO.readsReg())
  1004. HasPhysRegUse = true;
  1005. }
  1006. }
  1007. } else if (MO.isRegMask()) {
  1008. HasRegMask = true;
  1009. RegMasks.push_back(MO.getRegMask());
  1010. }
  1011. }
  1012. // Allocate virtreg defs.
  1013. if (HasDef) {
  1014. if (HasVRegDef) {
  1015. // Special handling for early clobbers, tied operands or subregister defs:
  1016. // Compared to "normal" defs these:
  1017. // - Must not use a register that is pre-assigned for a use operand.
  1018. // - In order to solve tricky inline assembly constraints we change the
  1019. // heuristic to figure out a good operand order before doing
  1020. // assignments.
  1021. if (NeedToAssignLiveThroughs) {
  1022. DefOperandIndexes.clear();
  1023. PhysRegUses.clear();
  1024. // Track number of defs which may consume a register from the class.
  1025. std::vector<unsigned> RegClassDefCounts(TRI->getNumRegClasses(), 0);
  1026. assert(RegClassDefCounts[0] == 0);
  1027. LLVM_DEBUG(dbgs() << "Need to assign livethroughs\n");
  1028. for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
  1029. const MachineOperand &MO = MI.getOperand(I);
  1030. if (!MO.isReg())
  1031. continue;
  1032. Register Reg = MO.getReg();
  1033. if (MO.readsReg()) {
  1034. if (Reg.isPhysical()) {
  1035. LLVM_DEBUG(dbgs() << "mark extra used: " << printReg(Reg, TRI)
  1036. << '\n');
  1037. markPhysRegUsedInInstr(Reg);
  1038. }
  1039. }
  1040. if (MO.isDef()) {
  1041. if (Reg.isVirtual())
  1042. DefOperandIndexes.push_back(I);
  1043. addRegClassDefCounts(RegClassDefCounts, Reg);
  1044. }
  1045. }
  1046. llvm::sort(DefOperandIndexes, [&](uint16_t I0, uint16_t I1) {
  1047. const MachineOperand &MO0 = MI.getOperand(I0);
  1048. const MachineOperand &MO1 = MI.getOperand(I1);
  1049. Register Reg0 = MO0.getReg();
  1050. Register Reg1 = MO1.getReg();
  1051. const TargetRegisterClass &RC0 = *MRI->getRegClass(Reg0);
  1052. const TargetRegisterClass &RC1 = *MRI->getRegClass(Reg1);
  1053. // Identify regclass that are easy to use up completely just in this
  1054. // instruction.
  1055. unsigned ClassSize0 = RegClassInfo.getOrder(&RC0).size();
  1056. unsigned ClassSize1 = RegClassInfo.getOrder(&RC1).size();
  1057. bool SmallClass0 = ClassSize0 < RegClassDefCounts[RC0.getID()];
  1058. bool SmallClass1 = ClassSize1 < RegClassDefCounts[RC1.getID()];
  1059. if (SmallClass0 > SmallClass1)
  1060. return true;
  1061. if (SmallClass0 < SmallClass1)
  1062. return false;
  1063. // Allocate early clobbers and livethrough operands first.
  1064. bool Livethrough0 = MO0.isEarlyClobber() || MO0.isTied() ||
  1065. (MO0.getSubReg() == 0 && !MO0.isUndef());
  1066. bool Livethrough1 = MO1.isEarlyClobber() || MO1.isTied() ||
  1067. (MO1.getSubReg() == 0 && !MO1.isUndef());
  1068. if (Livethrough0 > Livethrough1)
  1069. return true;
  1070. if (Livethrough0 < Livethrough1)
  1071. return false;
  1072. // Tie-break rule: operand index.
  1073. return I0 < I1;
  1074. });
  1075. for (uint16_t OpIdx : DefOperandIndexes) {
  1076. MachineOperand &MO = MI.getOperand(OpIdx);
  1077. LLVM_DEBUG(dbgs() << "Allocating " << MO << '\n');
  1078. unsigned Reg = MO.getReg();
  1079. if (MO.isEarlyClobber() || MO.isTied() ||
  1080. (MO.getSubReg() && !MO.isUndef())) {
  1081. defineLiveThroughVirtReg(MI, OpIdx, Reg);
  1082. } else {
  1083. defineVirtReg(MI, OpIdx, Reg);
  1084. }
  1085. }
  1086. } else {
  1087. // Assign virtual register defs.
  1088. for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
  1089. MachineOperand &MO = MI.getOperand(I);
  1090. if (!MO.isReg() || !MO.isDef())
  1091. continue;
  1092. Register Reg = MO.getReg();
  1093. if (Reg.isVirtual())
  1094. defineVirtReg(MI, I, Reg);
  1095. }
  1096. }
  1097. }
  1098. // Free registers occupied by defs.
  1099. // Iterate operands in reverse order, so we see the implicit super register
  1100. // defs first (we added them earlier in case of <def,read-undef>).
  1101. for (MachineOperand &MO : llvm::reverse(MI.operands())) {
  1102. if (!MO.isReg() || !MO.isDef())
  1103. continue;
  1104. // subreg defs don't free the full register. We left the subreg number
  1105. // around as a marker in setPhysReg() to recognize this case here.
  1106. if (MO.getSubReg() != 0) {
  1107. MO.setSubReg(0);
  1108. continue;
  1109. }
  1110. assert((!MO.isTied() || !isClobberedByRegMasks(MO.getReg())) &&
  1111. "tied def assigned to clobbered register");
  1112. // Do not free tied operands and early clobbers.
  1113. if (MO.isTied() || MO.isEarlyClobber())
  1114. continue;
  1115. Register Reg = MO.getReg();
  1116. if (!Reg)
  1117. continue;
  1118. assert(Reg.isPhysical());
  1119. if (MRI->isReserved(Reg))
  1120. continue;
  1121. freePhysReg(Reg);
  1122. unmarkRegUsedInInstr(Reg);
  1123. }
  1124. }
  1125. // Displace clobbered registers.
  1126. if (HasRegMask) {
  1127. assert(!RegMasks.empty() && "expected RegMask");
  1128. // MRI bookkeeping.
  1129. for (const auto *RM : RegMasks)
  1130. MRI->addPhysRegsUsedFromRegMask(RM);
  1131. // Displace clobbered registers.
  1132. for (const LiveReg &LR : LiveVirtRegs) {
  1133. MCPhysReg PhysReg = LR.PhysReg;
  1134. if (PhysReg != 0 && isClobberedByRegMasks(PhysReg))
  1135. displacePhysReg(MI, PhysReg);
  1136. }
  1137. }
  1138. // Apply pre-assigned register uses to state.
  1139. if (HasPhysRegUse) {
  1140. for (MachineOperand &MO : MI.operands()) {
  1141. if (!MO.isReg() || !MO.readsReg())
  1142. continue;
  1143. Register Reg = MO.getReg();
  1144. if (!Reg.isPhysical())
  1145. continue;
  1146. if (MRI->isReserved(Reg))
  1147. continue;
  1148. bool displacedAny = usePhysReg(MI, Reg);
  1149. if (!displacedAny && !MRI->isReserved(Reg))
  1150. MO.setIsKill(true);
  1151. }
  1152. }
  1153. // Allocate virtreg uses and insert reloads as necessary.
  1154. bool HasUndefUse = false;
  1155. for (unsigned I = 0; I < MI.getNumOperands(); ++I) {
  1156. MachineOperand &MO = MI.getOperand(I);
  1157. if (!MO.isReg() || !MO.isUse())
  1158. continue;
  1159. Register Reg = MO.getReg();
  1160. if (!Reg.isVirtual())
  1161. continue;
  1162. if (MO.isUndef()) {
  1163. HasUndefUse = true;
  1164. continue;
  1165. }
  1166. // Populate MayLiveAcrossBlocks in case the use block is allocated before
  1167. // the def block (removing the vreg uses).
  1168. mayLiveIn(Reg);
  1169. assert(!MO.isInternalRead() && "Bundles not supported");
  1170. assert(MO.readsReg() && "reading use");
  1171. useVirtReg(MI, I, Reg);
  1172. }
  1173. // Allocate undef operands. This is a separate step because in a situation
  1174. // like ` = OP undef %X, %X` both operands need the same register assign
  1175. // so we should perform the normal assignment first.
  1176. if (HasUndefUse) {
  1177. for (MachineOperand &MO : MI.uses()) {
  1178. if (!MO.isReg() || !MO.isUse())
  1179. continue;
  1180. Register Reg = MO.getReg();
  1181. if (!Reg.isVirtual())
  1182. continue;
  1183. assert(MO.isUndef() && "Should only have undef virtreg uses left");
  1184. allocVirtRegUndef(MO);
  1185. }
  1186. }
  1187. // Free early clobbers.
  1188. if (HasEarlyClobber) {
  1189. for (MachineOperand &MO : llvm::reverse(MI.operands())) {
  1190. if (!MO.isReg() || !MO.isDef() || !MO.isEarlyClobber())
  1191. continue;
  1192. // subreg defs don't free the full register. We left the subreg number
  1193. // around as a marker in setPhysReg() to recognize this case here.
  1194. if (MO.getSubReg() != 0) {
  1195. MO.setSubReg(0);
  1196. continue;
  1197. }
  1198. Register Reg = MO.getReg();
  1199. if (!Reg)
  1200. continue;
  1201. assert(Reg.isPhysical() && "should have register assigned");
  1202. // We sometimes get odd situations like:
  1203. // early-clobber %x0 = INSTRUCTION %x0
  1204. // which is semantically questionable as the early-clobber should
  1205. // apply before the use. But in practice we consider the use to
  1206. // happen before the early clobber now. Don't free the early clobber
  1207. // register in this case.
  1208. if (MI.readsRegister(Reg, TRI))
  1209. continue;
  1210. freePhysReg(Reg);
  1211. }
  1212. }
  1213. LLVM_DEBUG(dbgs() << "<< " << MI);
  1214. if (MI.isCopy() && MI.getOperand(0).getReg() == MI.getOperand(1).getReg() &&
  1215. MI.getNumOperands() == 2) {
  1216. LLVM_DEBUG(dbgs() << "Mark identity copy for removal\n");
  1217. Coalesced.push_back(&MI);
  1218. }
  1219. }
  1220. void RegAllocFast::handleDebugValue(MachineInstr &MI) {
  1221. // Ignore DBG_VALUEs that aren't based on virtual registers. These are
  1222. // mostly constants and frame indices.
  1223. for (Register Reg : MI.getUsedDebugRegs()) {
  1224. if (!Register::isVirtualRegister(Reg))
  1225. continue;
  1226. // Already spilled to a stackslot?
  1227. int SS = StackSlotForVirtReg[Reg];
  1228. if (SS != -1) {
  1229. // Modify DBG_VALUE now that the value is in a spill slot.
  1230. updateDbgValueForSpill(MI, SS, Reg);
  1231. LLVM_DEBUG(dbgs() << "Rewrite DBG_VALUE for spilled memory: " << MI);
  1232. continue;
  1233. }
  1234. // See if this virtual register has already been allocated to a physical
  1235. // register or spilled to a stack slot.
  1236. LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
  1237. SmallVector<MachineOperand *> DbgOps;
  1238. for (MachineOperand &Op : MI.getDebugOperandsForReg(Reg))
  1239. DbgOps.push_back(&Op);
  1240. if (LRI != LiveVirtRegs.end() && LRI->PhysReg) {
  1241. // Update every use of Reg within MI.
  1242. for (auto &RegMO : DbgOps)
  1243. setPhysReg(MI, *RegMO, LRI->PhysReg);
  1244. } else {
  1245. DanglingDbgValues[Reg].push_back(&MI);
  1246. }
  1247. // If Reg hasn't been spilled, put this DBG_VALUE in LiveDbgValueMap so
  1248. // that future spills of Reg will have DBG_VALUEs.
  1249. LiveDbgValueMap[Reg].append(DbgOps.begin(), DbgOps.end());
  1250. }
  1251. }
  1252. void RegAllocFast::handleBundle(MachineInstr &MI) {
  1253. MachineBasicBlock::instr_iterator BundledMI = MI.getIterator();
  1254. ++BundledMI;
  1255. while (BundledMI->isBundledWithPred()) {
  1256. for (MachineOperand &MO : BundledMI->operands()) {
  1257. if (!MO.isReg())
  1258. continue;
  1259. Register Reg = MO.getReg();
  1260. if (!Reg.isVirtual())
  1261. continue;
  1262. DenseMap<Register, MCPhysReg>::iterator DI;
  1263. DI = BundleVirtRegsMap.find(Reg);
  1264. assert(DI != BundleVirtRegsMap.end() && "Unassigned virtual register");
  1265. setPhysReg(MI, MO, DI->second);
  1266. }
  1267. ++BundledMI;
  1268. }
  1269. }
  1270. void RegAllocFast::allocateBasicBlock(MachineBasicBlock &MBB) {
  1271. this->MBB = &MBB;
  1272. LLVM_DEBUG(dbgs() << "\nAllocating " << MBB);
  1273. RegUnitStates.assign(TRI->getNumRegUnits(), regFree);
  1274. assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
  1275. for (auto &LiveReg : MBB.liveouts())
  1276. setPhysRegState(LiveReg.PhysReg, regPreAssigned);
  1277. Coalesced.clear();
  1278. // Traverse block in reverse order allocating instructions one by one.
  1279. for (MachineInstr &MI : reverse(MBB)) {
  1280. LLVM_DEBUG(
  1281. dbgs() << "\n>> " << MI << "Regs:";
  1282. dumpState()
  1283. );
  1284. // Special handling for debug values. Note that they are not allowed to
  1285. // affect codegen of the other instructions in any way.
  1286. if (MI.isDebugValue()) {
  1287. handleDebugValue(MI);
  1288. continue;
  1289. }
  1290. allocateInstruction(MI);
  1291. // Once BUNDLE header is assigned registers, same assignments need to be
  1292. // done for bundled MIs.
  1293. if (MI.getOpcode() == TargetOpcode::BUNDLE) {
  1294. handleBundle(MI);
  1295. }
  1296. }
  1297. LLVM_DEBUG(
  1298. dbgs() << "Begin Regs:";
  1299. dumpState()
  1300. );
  1301. // Spill all physical registers holding virtual registers now.
  1302. LLVM_DEBUG(dbgs() << "Loading live registers at begin of block.\n");
  1303. reloadAtBegin(MBB);
  1304. // Erase all the coalesced copies. We are delaying it until now because
  1305. // LiveVirtRegs might refer to the instrs.
  1306. for (MachineInstr *MI : Coalesced)
  1307. MBB.erase(MI);
  1308. NumCoalesced += Coalesced.size();
  1309. for (auto &UDBGPair : DanglingDbgValues) {
  1310. for (MachineInstr *DbgValue : UDBGPair.second) {
  1311. assert(DbgValue->isDebugValue() && "expected DBG_VALUE");
  1312. // Nothing to do if the vreg was spilled in the meantime.
  1313. if (!DbgValue->hasDebugOperandForReg(UDBGPair.first))
  1314. continue;
  1315. LLVM_DEBUG(dbgs() << "Register did not survive for " << *DbgValue
  1316. << '\n');
  1317. DbgValue->setDebugValueUndef();
  1318. }
  1319. }
  1320. DanglingDbgValues.clear();
  1321. LLVM_DEBUG(MBB.dump());
  1322. }
  1323. bool RegAllocFast::runOnMachineFunction(MachineFunction &MF) {
  1324. LLVM_DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
  1325. << "********** Function: " << MF.getName() << '\n');
  1326. MRI = &MF.getRegInfo();
  1327. const TargetSubtargetInfo &STI = MF.getSubtarget();
  1328. TRI = STI.getRegisterInfo();
  1329. TII = STI.getInstrInfo();
  1330. MFI = &MF.getFrameInfo();
  1331. MRI->freezeReservedRegs(MF);
  1332. RegClassInfo.runOnMachineFunction(MF);
  1333. unsigned NumRegUnits = TRI->getNumRegUnits();
  1334. UsedInInstr.clear();
  1335. UsedInInstr.setUniverse(NumRegUnits);
  1336. PhysRegUses.clear();
  1337. PhysRegUses.setUniverse(NumRegUnits);
  1338. // initialize the virtual->physical register map to have a 'null'
  1339. // mapping for all virtual registers
  1340. unsigned NumVirtRegs = MRI->getNumVirtRegs();
  1341. StackSlotForVirtReg.resize(NumVirtRegs);
  1342. LiveVirtRegs.setUniverse(NumVirtRegs);
  1343. MayLiveAcrossBlocks.clear();
  1344. MayLiveAcrossBlocks.resize(NumVirtRegs);
  1345. // Loop over all of the basic blocks, eliminating virtual register references
  1346. for (MachineBasicBlock &MBB : MF)
  1347. allocateBasicBlock(MBB);
  1348. if (ClearVirtRegs) {
  1349. // All machine operands and other references to virtual registers have been
  1350. // replaced. Remove the virtual registers.
  1351. MRI->clearVirtRegs();
  1352. }
  1353. StackSlotForVirtReg.clear();
  1354. LiveDbgValueMap.clear();
  1355. return true;
  1356. }
  1357. FunctionPass *llvm::createFastRegisterAllocator() {
  1358. return new RegAllocFast();
  1359. }
  1360. FunctionPass *llvm::createFastRegisterAllocator(
  1361. std::function<bool(const TargetRegisterInfo &TRI,
  1362. const TargetRegisterClass &RC)> Ftor, bool ClearVirtRegs) {
  1363. return new RegAllocFast(Ftor, ClearVirtRegs);
  1364. }