SimpleRegisterCoalescing.cpp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  1. //===-- SimpleRegisterCoalescing.cpp - Register Coalescing ----------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements a simple register coalescing pass that attempts to
  11. // aggressively coalesce every register copy that it can.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #define DEBUG_TYPE "regcoalescing"
  15. #include "SimpleRegisterCoalescing.h"
  16. #include "VirtRegMap.h"
  17. #include "llvm/CodeGen/LiveIntervalAnalysis.h"
  18. #include "llvm/Value.h"
  19. #include "llvm/CodeGen/LiveVariables.h"
  20. #include "llvm/CodeGen/MachineFrameInfo.h"
  21. #include "llvm/CodeGen/MachineInstr.h"
  22. #include "llvm/CodeGen/MachineLoopInfo.h"
  23. #include "llvm/CodeGen/MachineRegisterInfo.h"
  24. #include "llvm/CodeGen/Passes.h"
  25. #include "llvm/CodeGen/RegisterCoalescer.h"
  26. #include "llvm/Target/TargetInstrInfo.h"
  27. #include "llvm/Target/TargetMachine.h"
  28. #include "llvm/Support/CommandLine.h"
  29. #include "llvm/Support/Debug.h"
  30. #include "llvm/ADT/SmallSet.h"
  31. #include "llvm/ADT/Statistic.h"
  32. #include "llvm/ADT/STLExtras.h"
  33. #include <algorithm>
  34. #include <cmath>
  35. using namespace llvm;
  36. STATISTIC(numJoins , "Number of interval joins performed");
  37. STATISTIC(numPeep , "Number of identity moves eliminated after coalescing");
  38. STATISTIC(numAborts , "Number of times interval joining aborted");
  39. char SimpleRegisterCoalescing::ID = 0;
  40. namespace {
  41. static cl::opt<bool>
  42. EnableJoining("join-liveintervals",
  43. cl::desc("Coalesce copies (default=true)"),
  44. cl::init(true));
  45. static cl::opt<bool>
  46. NewHeuristic("new-coalescer-heuristic",
  47. cl::desc("Use new coalescer heuristic"),
  48. cl::init(false));
  49. static cl::opt<bool>
  50. ReMatSpillWeight("tweak-remat-spill-weight",
  51. cl::desc("Tweak spill weight of re-materializable intervals"),
  52. cl::init(true));
  53. RegisterPass<SimpleRegisterCoalescing>
  54. X("simple-register-coalescing", "Simple Register Coalescing");
  55. // Declare that we implement the RegisterCoalescer interface
  56. RegisterAnalysisGroup<RegisterCoalescer, true/*The Default*/> V(X);
  57. }
  58. const PassInfo *llvm::SimpleRegisterCoalescingID = X.getPassInfo();
  59. void SimpleRegisterCoalescing::getAnalysisUsage(AnalysisUsage &AU) const {
  60. AU.addPreserved<LiveIntervals>();
  61. AU.addPreserved<MachineLoopInfo>();
  62. AU.addPreservedID(MachineDominatorsID);
  63. AU.addPreservedID(PHIEliminationID);
  64. AU.addPreservedID(TwoAddressInstructionPassID);
  65. AU.addRequired<LiveVariables>();
  66. AU.addRequired<LiveIntervals>();
  67. AU.addRequired<MachineLoopInfo>();
  68. MachineFunctionPass::getAnalysisUsage(AU);
  69. }
  70. /// AdjustCopiesBackFrom - We found a non-trivially-coalescable copy with IntA
  71. /// being the source and IntB being the dest, thus this defines a value number
  72. /// in IntB. If the source value number (in IntA) is defined by a copy from B,
  73. /// see if we can merge these two pieces of B into a single value number,
  74. /// eliminating a copy. For example:
  75. ///
  76. /// A3 = B0
  77. /// ...
  78. /// B1 = A3 <- this copy
  79. ///
  80. /// In this case, B0 can be extended to where the B1 copy lives, allowing the B1
  81. /// value number to be replaced with B0 (which simplifies the B liveinterval).
  82. ///
  83. /// This returns true if an interval was modified.
  84. ///
  85. bool SimpleRegisterCoalescing::AdjustCopiesBackFrom(LiveInterval &IntA,
  86. LiveInterval &IntB,
  87. MachineInstr *CopyMI) {
  88. unsigned CopyIdx = li_->getDefIndex(li_->getInstructionIndex(CopyMI));
  89. // BValNo is a value number in B that is defined by a copy from A. 'B3' in
  90. // the example above.
  91. LiveInterval::iterator BLR = IntB.FindLiveRangeContaining(CopyIdx);
  92. VNInfo *BValNo = BLR->valno;
  93. // Get the location that B is defined at. Two options: either this value has
  94. // an unknown definition point or it is defined at CopyIdx. If unknown, we
  95. // can't process it.
  96. if (!BValNo->reg) return false;
  97. assert(BValNo->def == CopyIdx &&
  98. "Copy doesn't define the value?");
  99. // AValNo is the value number in A that defines the copy, A0 in the example.
  100. LiveInterval::iterator AValLR = IntA.FindLiveRangeContaining(CopyIdx-1);
  101. VNInfo *AValNo = AValLR->valno;
  102. // If AValNo is defined as a copy from IntB, we can potentially process this.
  103. // Get the instruction that defines this value number.
  104. unsigned SrcReg = AValNo->reg;
  105. if (!SrcReg) return false; // Not defined by a copy.
  106. // If the value number is not defined by a copy instruction, ignore it.
  107. // If the source register comes from an interval other than IntB, we can't
  108. // handle this.
  109. if (rep(SrcReg) != IntB.reg) return false;
  110. // Get the LiveRange in IntB that this value number starts with.
  111. LiveInterval::iterator ValLR = IntB.FindLiveRangeContaining(AValNo->def-1);
  112. // Make sure that the end of the live range is inside the same block as
  113. // CopyMI.
  114. MachineInstr *ValLREndInst = li_->getInstructionFromIndex(ValLR->end-1);
  115. if (!ValLREndInst ||
  116. ValLREndInst->getParent() != CopyMI->getParent()) return false;
  117. // Okay, we now know that ValLR ends in the same block that the CopyMI
  118. // live-range starts. If there are no intervening live ranges between them in
  119. // IntB, we can merge them.
  120. if (ValLR+1 != BLR) return false;
  121. // If a live interval is a physical register, conservatively check if any
  122. // of its sub-registers is overlapping the live interval of the virtual
  123. // register. If so, do not coalesce.
  124. if (MRegisterInfo::isPhysicalRegister(IntB.reg) &&
  125. *mri_->getSubRegisters(IntB.reg)) {
  126. for (const unsigned* SR = mri_->getSubRegisters(IntB.reg); *SR; ++SR)
  127. if (li_->hasInterval(*SR) && IntA.overlaps(li_->getInterval(*SR))) {
  128. DOUT << "Interfere with sub-register ";
  129. DEBUG(li_->getInterval(*SR).print(DOUT, mri_));
  130. return false;
  131. }
  132. }
  133. DOUT << "\nExtending: "; IntB.print(DOUT, mri_);
  134. unsigned FillerStart = ValLR->end, FillerEnd = BLR->start;
  135. // We are about to delete CopyMI, so need to remove it as the 'instruction
  136. // that defines this value #'. Update the the valnum with the new defining
  137. // instruction #.
  138. BValNo->def = FillerStart;
  139. BValNo->reg = 0;
  140. // Okay, we can merge them. We need to insert a new liverange:
  141. // [ValLR.end, BLR.begin) of either value number, then we merge the
  142. // two value numbers.
  143. IntB.addRange(LiveRange(FillerStart, FillerEnd, BValNo));
  144. // If the IntB live range is assigned to a physical register, and if that
  145. // physreg has aliases,
  146. if (MRegisterInfo::isPhysicalRegister(IntB.reg)) {
  147. // Update the liveintervals of sub-registers.
  148. for (const unsigned *AS = mri_->getSubRegisters(IntB.reg); *AS; ++AS) {
  149. LiveInterval &AliasLI = li_->getInterval(*AS);
  150. AliasLI.addRange(LiveRange(FillerStart, FillerEnd,
  151. AliasLI.getNextValue(FillerStart, 0, li_->getVNInfoAllocator())));
  152. }
  153. }
  154. // Okay, merge "B1" into the same value number as "B0".
  155. if (BValNo != ValLR->valno)
  156. IntB.MergeValueNumberInto(BValNo, ValLR->valno);
  157. DOUT << " result = "; IntB.print(DOUT, mri_);
  158. DOUT << "\n";
  159. // If the source instruction was killing the source register before the
  160. // merge, unset the isKill marker given the live range has been extended.
  161. int UIdx = ValLREndInst->findRegisterUseOperandIdx(IntB.reg, true);
  162. if (UIdx != -1)
  163. ValLREndInst->getOperand(UIdx).setIsKill(false);
  164. ++numPeep;
  165. return true;
  166. }
  167. /// AddSubRegIdxPairs - Recursively mark all the registers represented by the
  168. /// specified register as sub-registers. The recursion level is expected to be
  169. /// shallow.
  170. void SimpleRegisterCoalescing::AddSubRegIdxPairs(unsigned Reg, unsigned SubIdx) {
  171. std::vector<unsigned> &JoinedRegs = r2rRevMap_[Reg];
  172. for (unsigned i = 0, e = JoinedRegs.size(); i != e; ++i) {
  173. SubRegIdxes.push_back(std::make_pair(JoinedRegs[i], SubIdx));
  174. AddSubRegIdxPairs(JoinedRegs[i], SubIdx);
  175. }
  176. }
  177. /// isBackEdgeCopy - Returns true if CopyMI is a back edge copy.
  178. ///
  179. bool SimpleRegisterCoalescing::isBackEdgeCopy(MachineInstr *CopyMI,
  180. unsigned DstReg) {
  181. MachineBasicBlock *MBB = CopyMI->getParent();
  182. const MachineLoop *L = loopInfo->getLoopFor(MBB);
  183. if (!L)
  184. return false;
  185. if (MBB != L->getLoopLatch())
  186. return false;
  187. DstReg = rep(DstReg);
  188. LiveInterval &LI = li_->getInterval(DstReg);
  189. unsigned DefIdx = li_->getInstructionIndex(CopyMI);
  190. LiveInterval::const_iterator DstLR =
  191. LI.FindLiveRangeContaining(li_->getDefIndex(DefIdx));
  192. if (DstLR == LI.end())
  193. return false;
  194. unsigned KillIdx = li_->getInstructionIndex(&MBB->back()) + InstrSlots::NUM-1;
  195. if (DstLR->valno->kills.size() == 1 && DstLR->valno->kills[0] == KillIdx)
  196. return true;
  197. return false;
  198. }
  199. /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
  200. /// which are the src/dst of the copy instruction CopyMI. This returns true
  201. /// if the copy was successfully coalesced away. If it is not currently
  202. /// possible to coalesce this interval, but it may be possible if other
  203. /// things get coalesced, then it returns true by reference in 'Again'.
  204. bool SimpleRegisterCoalescing::JoinCopy(CopyRec TheCopy, bool &Again) {
  205. MachineInstr *CopyMI = TheCopy.MI;
  206. Again = false;
  207. if (JoinedCopies.count(CopyMI))
  208. return false; // Already done.
  209. DOUT << li_->getInstructionIndex(CopyMI) << '\t' << *CopyMI;
  210. // Get representative registers.
  211. unsigned SrcReg = TheCopy.SrcReg;
  212. unsigned DstReg = TheCopy.DstReg;
  213. unsigned repSrcReg = rep(SrcReg);
  214. unsigned repDstReg = rep(DstReg);
  215. // If they are already joined we continue.
  216. if (repSrcReg == repDstReg) {
  217. DOUT << "\tCopy already coalesced.\n";
  218. return false; // Not coalescable.
  219. }
  220. bool SrcIsPhys = MRegisterInfo::isPhysicalRegister(repSrcReg);
  221. bool DstIsPhys = MRegisterInfo::isPhysicalRegister(repDstReg);
  222. // If they are both physical registers, we cannot join them.
  223. if (SrcIsPhys && DstIsPhys) {
  224. DOUT << "\tCan not coalesce physregs.\n";
  225. return false; // Not coalescable.
  226. }
  227. // We only join virtual registers with allocatable physical registers.
  228. if (SrcIsPhys && !allocatableRegs_[repSrcReg]) {
  229. DOUT << "\tSrc reg is unallocatable physreg.\n";
  230. return false; // Not coalescable.
  231. }
  232. if (DstIsPhys && !allocatableRegs_[repDstReg]) {
  233. DOUT << "\tDst reg is unallocatable physreg.\n";
  234. return false; // Not coalescable.
  235. }
  236. bool isExtSubReg = CopyMI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG;
  237. unsigned RealDstReg = 0;
  238. if (isExtSubReg) {
  239. unsigned SubIdx = CopyMI->getOperand(2).getImm();
  240. if (SrcIsPhys)
  241. // r1024 = EXTRACT_SUBREG EAX, 0 then r1024 is really going to be
  242. // coalesced with AX.
  243. repSrcReg = mri_->getSubReg(repSrcReg, SubIdx);
  244. else if (DstIsPhys) {
  245. // If this is a extract_subreg where dst is a physical register, e.g.
  246. // cl = EXTRACT_SUBREG reg1024, 1
  247. // then create and update the actual physical register allocated to RHS.
  248. const TargetRegisterClass *RC=mf_->getRegInfo().getRegClass(repSrcReg);
  249. for (const unsigned *SRs = mri_->getSuperRegisters(repDstReg);
  250. unsigned SR = *SRs; ++SRs) {
  251. if (repDstReg == mri_->getSubReg(SR, SubIdx) &&
  252. RC->contains(SR)) {
  253. RealDstReg = SR;
  254. break;
  255. }
  256. }
  257. assert(RealDstReg && "Invalid extra_subreg instruction!");
  258. // For this type of EXTRACT_SUBREG, conservatively
  259. // check if the live interval of the source register interfere with the
  260. // actual super physical register we are trying to coalesce with.
  261. LiveInterval &RHS = li_->getInterval(repSrcReg);
  262. if (li_->hasInterval(RealDstReg) &&
  263. RHS.overlaps(li_->getInterval(RealDstReg))) {
  264. DOUT << "Interfere with register ";
  265. DEBUG(li_->getInterval(RealDstReg).print(DOUT, mri_));
  266. return false; // Not coalescable
  267. }
  268. for (const unsigned* SR = mri_->getSubRegisters(RealDstReg); *SR; ++SR)
  269. if (li_->hasInterval(*SR) && RHS.overlaps(li_->getInterval(*SR))) {
  270. DOUT << "Interfere with sub-register ";
  271. DEBUG(li_->getInterval(*SR).print(DOUT, mri_));
  272. return false; // Not coalescable
  273. }
  274. } else {
  275. unsigned SrcSize= li_->getInterval(repSrcReg).getSize() / InstrSlots::NUM;
  276. unsigned DstSize= li_->getInterval(repDstReg).getSize() / InstrSlots::NUM;
  277. const TargetRegisterClass *RC=mf_->getRegInfo().getRegClass(repDstReg);
  278. unsigned Threshold = allocatableRCRegs_[RC].count();
  279. // Be conservative. If both sides are virtual registers, do not coalesce
  280. // if this will cause a high use density interval to target a smaller set
  281. // of registers.
  282. if (DstSize > Threshold || SrcSize > Threshold) {
  283. LiveVariables::VarInfo &svi = lv_->getVarInfo(repSrcReg);
  284. LiveVariables::VarInfo &dvi = lv_->getVarInfo(repDstReg);
  285. if ((float)dvi.NumUses / DstSize < (float)svi.NumUses / SrcSize) {
  286. Again = true; // May be possible to coalesce later.
  287. return false;
  288. }
  289. }
  290. }
  291. } else if (differingRegisterClasses(repSrcReg, repDstReg)) {
  292. // If they are not of the same register class, we cannot join them.
  293. DOUT << "\tSrc/Dest are different register classes.\n";
  294. // Allow the coalescer to try again in case either side gets coalesced to
  295. // a physical register that's compatible with the other side. e.g.
  296. // r1024 = MOV32to32_ r1025
  297. // but later r1024 is assigned EAX then r1025 may be coalesced with EAX.
  298. Again = true; // May be possible to coalesce later.
  299. return false;
  300. }
  301. LiveInterval &SrcInt = li_->getInterval(repSrcReg);
  302. LiveInterval &DstInt = li_->getInterval(repDstReg);
  303. assert(SrcInt.reg == repSrcReg && DstInt.reg == repDstReg &&
  304. "Register mapping is horribly broken!");
  305. DOUT << "\t\tInspecting "; SrcInt.print(DOUT, mri_);
  306. DOUT << " and "; DstInt.print(DOUT, mri_);
  307. DOUT << ": ";
  308. // Check if it is necessary to propagate "isDead" property before intervals
  309. // are joined.
  310. MachineOperand *mopd = CopyMI->findRegisterDefOperand(DstReg);
  311. bool isDead = mopd->isDead();
  312. bool isShorten = false;
  313. unsigned SrcStart = 0, RemoveStart = 0;
  314. unsigned SrcEnd = 0, RemoveEnd = 0;
  315. if (isDead) {
  316. unsigned CopyIdx = li_->getInstructionIndex(CopyMI);
  317. LiveInterval::iterator SrcLR =
  318. SrcInt.FindLiveRangeContaining(li_->getUseIndex(CopyIdx));
  319. RemoveStart = SrcStart = SrcLR->start;
  320. RemoveEnd = SrcEnd = SrcLR->end;
  321. // The instruction which defines the src is only truly dead if there are
  322. // no intermediate uses and there isn't a use beyond the copy.
  323. // FIXME: find the last use, mark is kill and shorten the live range.
  324. if (SrcEnd > li_->getDefIndex(CopyIdx)) {
  325. isDead = false;
  326. } else {
  327. MachineOperand *MOU;
  328. MachineInstr *LastUse= lastRegisterUse(SrcStart, CopyIdx, repSrcReg, MOU);
  329. if (LastUse) {
  330. // Shorten the liveinterval to the end of last use.
  331. MOU->setIsKill();
  332. isDead = false;
  333. isShorten = true;
  334. RemoveStart = li_->getDefIndex(li_->getInstructionIndex(LastUse));
  335. RemoveEnd = SrcEnd;
  336. } else {
  337. MachineInstr *SrcMI = li_->getInstructionFromIndex(SrcStart);
  338. if (SrcMI) {
  339. MachineOperand *mops = findDefOperand(SrcMI, repSrcReg);
  340. if (mops)
  341. // A dead def should have a single cycle interval.
  342. ++RemoveStart;
  343. }
  344. }
  345. }
  346. }
  347. // We need to be careful about coalescing a source physical register with a
  348. // virtual register. Once the coalescing is done, it cannot be broken and
  349. // these are not spillable! If the destination interval uses are far away,
  350. // think twice about coalescing them!
  351. if (!mopd->isDead() && (SrcIsPhys || DstIsPhys) && !isExtSubReg) {
  352. LiveInterval &JoinVInt = SrcIsPhys ? DstInt : SrcInt;
  353. unsigned JoinVReg = SrcIsPhys ? repDstReg : repSrcReg;
  354. unsigned JoinPReg = SrcIsPhys ? repSrcReg : repDstReg;
  355. const TargetRegisterClass *RC = mf_->getRegInfo().getRegClass(JoinVReg);
  356. unsigned Threshold = allocatableRCRegs_[RC].count() * 2;
  357. if (TheCopy.isBackEdge)
  358. Threshold *= 2; // Favors back edge copies.
  359. // If the virtual register live interval is long but it has low use desity,
  360. // do not join them, instead mark the physical register as its allocation
  361. // preference.
  362. unsigned Length = JoinVInt.getSize() / InstrSlots::NUM;
  363. LiveVariables::VarInfo &vi = lv_->getVarInfo(JoinVReg);
  364. if (Length > Threshold &&
  365. (((float)vi.NumUses / Length) < (1.0 / Threshold))) {
  366. JoinVInt.preference = JoinPReg;
  367. ++numAborts;
  368. DOUT << "\tMay tie down a physical register, abort!\n";
  369. Again = true; // May be possible to coalesce later.
  370. return false;
  371. }
  372. }
  373. // Okay, attempt to join these two intervals. On failure, this returns false.
  374. // Otherwise, if one of the intervals being joined is a physreg, this method
  375. // always canonicalizes DstInt to be it. The output "SrcInt" will not have
  376. // been modified, so we can use this information below to update aliases.
  377. bool Swapped = false;
  378. if (JoinIntervals(DstInt, SrcInt, Swapped)) {
  379. if (isDead) {
  380. // Result of the copy is dead. Propagate this property.
  381. if (SrcStart == 0) {
  382. assert(MRegisterInfo::isPhysicalRegister(repSrcReg) &&
  383. "Live-in must be a physical register!");
  384. // Live-in to the function but dead. Remove it from entry live-in set.
  385. // JoinIntervals may end up swapping the two intervals.
  386. mf_->begin()->removeLiveIn(repSrcReg);
  387. } else {
  388. MachineInstr *SrcMI = li_->getInstructionFromIndex(SrcStart);
  389. if (SrcMI) {
  390. MachineOperand *mops = findDefOperand(SrcMI, repSrcReg);
  391. if (mops)
  392. mops->setIsDead();
  393. }
  394. }
  395. }
  396. if (isShorten || isDead) {
  397. // Shorten the destination live interval.
  398. if (Swapped)
  399. SrcInt.removeRange(RemoveStart, RemoveEnd);
  400. }
  401. } else {
  402. // Coalescing failed.
  403. // If we can eliminate the copy without merging the live ranges, do so now.
  404. if (!isExtSubReg && AdjustCopiesBackFrom(SrcInt, DstInt, CopyMI)) {
  405. JoinedCopies.insert(CopyMI);
  406. return true;
  407. }
  408. // Otherwise, we are unable to join the intervals.
  409. DOUT << "Interference!\n";
  410. Again = true; // May be possible to coalesce later.
  411. return false;
  412. }
  413. LiveInterval *ResSrcInt = &SrcInt;
  414. LiveInterval *ResDstInt = &DstInt;
  415. if (Swapped) {
  416. std::swap(repSrcReg, repDstReg);
  417. std::swap(ResSrcInt, ResDstInt);
  418. }
  419. assert(MRegisterInfo::isVirtualRegister(repSrcReg) &&
  420. "LiveInterval::join didn't work right!");
  421. // If we're about to merge live ranges into a physical register live range,
  422. // we have to update any aliased register's live ranges to indicate that they
  423. // have clobbered values for this range.
  424. if (MRegisterInfo::isPhysicalRegister(repDstReg)) {
  425. // Unset unnecessary kills.
  426. if (!ResDstInt->containsOneValue()) {
  427. for (LiveInterval::Ranges::const_iterator I = ResSrcInt->begin(),
  428. E = ResSrcInt->end(); I != E; ++I)
  429. unsetRegisterKills(I->start, I->end, repDstReg);
  430. }
  431. // If this is a extract_subreg where dst is a physical register, e.g.
  432. // cl = EXTRACT_SUBREG reg1024, 1
  433. // then create and update the actual physical register allocated to RHS.
  434. if (RealDstReg) {
  435. LiveInterval &RealDstInt = li_->getOrCreateInterval(RealDstReg);
  436. SmallSet<const VNInfo*, 4> CopiedValNos;
  437. for (LiveInterval::Ranges::const_iterator I = ResSrcInt->ranges.begin(),
  438. E = ResSrcInt->ranges.end(); I != E; ++I) {
  439. LiveInterval::const_iterator DstLR =
  440. ResDstInt->FindLiveRangeContaining(I->start);
  441. assert(DstLR != ResDstInt->end() && "Invalid joined interval!");
  442. const VNInfo *DstValNo = DstLR->valno;
  443. if (CopiedValNos.insert(DstValNo)) {
  444. VNInfo *ValNo = RealDstInt.getNextValue(DstValNo->def, DstValNo->reg,
  445. li_->getVNInfoAllocator());
  446. ValNo->hasPHIKill = DstValNo->hasPHIKill;
  447. RealDstInt.addKills(ValNo, DstValNo->kills);
  448. RealDstInt.MergeValueInAsValue(*ResDstInt, DstValNo, ValNo);
  449. }
  450. }
  451. repDstReg = RealDstReg;
  452. }
  453. // Update the liveintervals of sub-registers.
  454. for (const unsigned *AS = mri_->getSubRegisters(repDstReg); *AS; ++AS)
  455. li_->getOrCreateInterval(*AS).MergeInClobberRanges(*ResSrcInt,
  456. li_->getVNInfoAllocator());
  457. } else {
  458. // Merge use info if the destination is a virtual register.
  459. LiveVariables::VarInfo& dVI = lv_->getVarInfo(repDstReg);
  460. LiveVariables::VarInfo& sVI = lv_->getVarInfo(repSrcReg);
  461. dVI.NumUses += sVI.NumUses;
  462. }
  463. // Remember these liveintervals have been joined.
  464. JoinedLIs.set(repSrcReg - MRegisterInfo::FirstVirtualRegister);
  465. if (MRegisterInfo::isVirtualRegister(repDstReg))
  466. JoinedLIs.set(repDstReg - MRegisterInfo::FirstVirtualRegister);
  467. if (isExtSubReg && !SrcIsPhys && !DstIsPhys) {
  468. if (!Swapped) {
  469. // Make sure we allocate the larger super-register.
  470. ResSrcInt->Copy(*ResDstInt, li_->getVNInfoAllocator());
  471. std::swap(repSrcReg, repDstReg);
  472. std::swap(ResSrcInt, ResDstInt);
  473. }
  474. unsigned SubIdx = CopyMI->getOperand(2).getImm();
  475. SubRegIdxes.push_back(std::make_pair(repSrcReg, SubIdx));
  476. AddSubRegIdxPairs(repSrcReg, SubIdx);
  477. }
  478. if (NewHeuristic) {
  479. for (LiveInterval::const_vni_iterator i = ResSrcInt->vni_begin(),
  480. e = ResSrcInt->vni_end(); i != e; ++i) {
  481. const VNInfo *vni = *i;
  482. if (vni->def && vni->def != ~1U && vni->def != ~0U) {
  483. MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def);
  484. unsigned SrcReg, DstReg;
  485. if (CopyMI && tii_->isMoveInstr(*CopyMI, SrcReg, DstReg) &&
  486. JoinedCopies.count(CopyMI) == 0) {
  487. unsigned LoopDepth = loopInfo->getLoopDepth(CopyMI->getParent());
  488. JoinQueue->push(CopyRec(CopyMI, SrcReg, DstReg, LoopDepth,
  489. isBackEdgeCopy(CopyMI, DstReg)));
  490. }
  491. }
  492. }
  493. }
  494. DOUT << "\n\t\tJoined. Result = "; ResDstInt->print(DOUT, mri_);
  495. DOUT << "\n";
  496. // repSrcReg is guarateed to be the register whose live interval that is
  497. // being merged.
  498. li_->removeInterval(repSrcReg);
  499. r2rMap_[repSrcReg] = repDstReg;
  500. r2rRevMap_[repDstReg].push_back(repSrcReg);
  501. // Finally, delete the copy instruction.
  502. JoinedCopies.insert(CopyMI);
  503. ++numPeep;
  504. ++numJoins;
  505. return true;
  506. }
  507. /// ComputeUltimateVN - Assuming we are going to join two live intervals,
  508. /// compute what the resultant value numbers for each value in the input two
  509. /// ranges will be. This is complicated by copies between the two which can
  510. /// and will commonly cause multiple value numbers to be merged into one.
  511. ///
  512. /// VN is the value number that we're trying to resolve. InstDefiningValue
  513. /// keeps track of the new InstDefiningValue assignment for the result
  514. /// LiveInterval. ThisFromOther/OtherFromThis are sets that keep track of
  515. /// whether a value in this or other is a copy from the opposite set.
  516. /// ThisValNoAssignments/OtherValNoAssignments keep track of value #'s that have
  517. /// already been assigned.
  518. ///
  519. /// ThisFromOther[x] - If x is defined as a copy from the other interval, this
  520. /// contains the value number the copy is from.
  521. ///
  522. static unsigned ComputeUltimateVN(VNInfo *VNI,
  523. SmallVector<VNInfo*, 16> &NewVNInfo,
  524. DenseMap<VNInfo*, VNInfo*> &ThisFromOther,
  525. DenseMap<VNInfo*, VNInfo*> &OtherFromThis,
  526. SmallVector<int, 16> &ThisValNoAssignments,
  527. SmallVector<int, 16> &OtherValNoAssignments) {
  528. unsigned VN = VNI->id;
  529. // If the VN has already been computed, just return it.
  530. if (ThisValNoAssignments[VN] >= 0)
  531. return ThisValNoAssignments[VN];
  532. // assert(ThisValNoAssignments[VN] != -2 && "Cyclic case?");
  533. // If this val is not a copy from the other val, then it must be a new value
  534. // number in the destination.
  535. DenseMap<VNInfo*, VNInfo*>::iterator I = ThisFromOther.find(VNI);
  536. if (I == ThisFromOther.end()) {
  537. NewVNInfo.push_back(VNI);
  538. return ThisValNoAssignments[VN] = NewVNInfo.size()-1;
  539. }
  540. VNInfo *OtherValNo = I->second;
  541. // Otherwise, this *is* a copy from the RHS. If the other side has already
  542. // been computed, return it.
  543. if (OtherValNoAssignments[OtherValNo->id] >= 0)
  544. return ThisValNoAssignments[VN] = OtherValNoAssignments[OtherValNo->id];
  545. // Mark this value number as currently being computed, then ask what the
  546. // ultimate value # of the other value is.
  547. ThisValNoAssignments[VN] = -2;
  548. unsigned UltimateVN =
  549. ComputeUltimateVN(OtherValNo, NewVNInfo, OtherFromThis, ThisFromOther,
  550. OtherValNoAssignments, ThisValNoAssignments);
  551. return ThisValNoAssignments[VN] = UltimateVN;
  552. }
  553. static bool InVector(VNInfo *Val, const SmallVector<VNInfo*, 8> &V) {
  554. return std::find(V.begin(), V.end(), Val) != V.end();
  555. }
  556. /// SimpleJoin - Attempt to joint the specified interval into this one. The
  557. /// caller of this method must guarantee that the RHS only contains a single
  558. /// value number and that the RHS is not defined by a copy from this
  559. /// interval. This returns false if the intervals are not joinable, or it
  560. /// joins them and returns true.
  561. bool SimpleRegisterCoalescing::SimpleJoin(LiveInterval &LHS, LiveInterval &RHS){
  562. assert(RHS.containsOneValue());
  563. // Some number (potentially more than one) value numbers in the current
  564. // interval may be defined as copies from the RHS. Scan the overlapping
  565. // portions of the LHS and RHS, keeping track of this and looking for
  566. // overlapping live ranges that are NOT defined as copies. If these exist, we
  567. // cannot coalesce.
  568. LiveInterval::iterator LHSIt = LHS.begin(), LHSEnd = LHS.end();
  569. LiveInterval::iterator RHSIt = RHS.begin(), RHSEnd = RHS.end();
  570. if (LHSIt->start < RHSIt->start) {
  571. LHSIt = std::upper_bound(LHSIt, LHSEnd, RHSIt->start);
  572. if (LHSIt != LHS.begin()) --LHSIt;
  573. } else if (RHSIt->start < LHSIt->start) {
  574. RHSIt = std::upper_bound(RHSIt, RHSEnd, LHSIt->start);
  575. if (RHSIt != RHS.begin()) --RHSIt;
  576. }
  577. SmallVector<VNInfo*, 8> EliminatedLHSVals;
  578. while (1) {
  579. // Determine if these live intervals overlap.
  580. bool Overlaps = false;
  581. if (LHSIt->start <= RHSIt->start)
  582. Overlaps = LHSIt->end > RHSIt->start;
  583. else
  584. Overlaps = RHSIt->end > LHSIt->start;
  585. // If the live intervals overlap, there are two interesting cases: if the
  586. // LHS interval is defined by a copy from the RHS, it's ok and we record
  587. // that the LHS value # is the same as the RHS. If it's not, then we cannot
  588. // coalesce these live ranges and we bail out.
  589. if (Overlaps) {
  590. // If we haven't already recorded that this value # is safe, check it.
  591. if (!InVector(LHSIt->valno, EliminatedLHSVals)) {
  592. // Copy from the RHS?
  593. unsigned SrcReg = LHSIt->valno->reg;
  594. if (rep(SrcReg) != RHS.reg)
  595. return false; // Nope, bail out.
  596. EliminatedLHSVals.push_back(LHSIt->valno);
  597. }
  598. // We know this entire LHS live range is okay, so skip it now.
  599. if (++LHSIt == LHSEnd) break;
  600. continue;
  601. }
  602. if (LHSIt->end < RHSIt->end) {
  603. if (++LHSIt == LHSEnd) break;
  604. } else {
  605. // One interesting case to check here. It's possible that we have
  606. // something like "X3 = Y" which defines a new value number in the LHS,
  607. // and is the last use of this liverange of the RHS. In this case, we
  608. // want to notice this copy (so that it gets coalesced away) even though
  609. // the live ranges don't actually overlap.
  610. if (LHSIt->start == RHSIt->end) {
  611. if (InVector(LHSIt->valno, EliminatedLHSVals)) {
  612. // We already know that this value number is going to be merged in
  613. // if coalescing succeeds. Just skip the liverange.
  614. if (++LHSIt == LHSEnd) break;
  615. } else {
  616. // Otherwise, if this is a copy from the RHS, mark it as being merged
  617. // in.
  618. if (rep(LHSIt->valno->reg) == RHS.reg) {
  619. EliminatedLHSVals.push_back(LHSIt->valno);
  620. // We know this entire LHS live range is okay, so skip it now.
  621. if (++LHSIt == LHSEnd) break;
  622. }
  623. }
  624. }
  625. if (++RHSIt == RHSEnd) break;
  626. }
  627. }
  628. // If we got here, we know that the coalescing will be successful and that
  629. // the value numbers in EliminatedLHSVals will all be merged together. Since
  630. // the most common case is that EliminatedLHSVals has a single number, we
  631. // optimize for it: if there is more than one value, we merge them all into
  632. // the lowest numbered one, then handle the interval as if we were merging
  633. // with one value number.
  634. VNInfo *LHSValNo;
  635. if (EliminatedLHSVals.size() > 1) {
  636. // Loop through all the equal value numbers merging them into the smallest
  637. // one.
  638. VNInfo *Smallest = EliminatedLHSVals[0];
  639. for (unsigned i = 1, e = EliminatedLHSVals.size(); i != e; ++i) {
  640. if (EliminatedLHSVals[i]->id < Smallest->id) {
  641. // Merge the current notion of the smallest into the smaller one.
  642. LHS.MergeValueNumberInto(Smallest, EliminatedLHSVals[i]);
  643. Smallest = EliminatedLHSVals[i];
  644. } else {
  645. // Merge into the smallest.
  646. LHS.MergeValueNumberInto(EliminatedLHSVals[i], Smallest);
  647. }
  648. }
  649. LHSValNo = Smallest;
  650. } else {
  651. assert(!EliminatedLHSVals.empty() && "No copies from the RHS?");
  652. LHSValNo = EliminatedLHSVals[0];
  653. }
  654. // Okay, now that there is a single LHS value number that we're merging the
  655. // RHS into, update the value number info for the LHS to indicate that the
  656. // value number is defined where the RHS value number was.
  657. const VNInfo *VNI = RHS.getValNumInfo(0);
  658. LHSValNo->def = VNI->def;
  659. LHSValNo->reg = VNI->reg;
  660. // Okay, the final step is to loop over the RHS live intervals, adding them to
  661. // the LHS.
  662. LHSValNo->hasPHIKill |= VNI->hasPHIKill;
  663. LHS.addKills(LHSValNo, VNI->kills);
  664. LHS.MergeRangesInAsValue(RHS, LHSValNo);
  665. LHS.weight += RHS.weight;
  666. if (RHS.preference && !LHS.preference)
  667. LHS.preference = RHS.preference;
  668. return true;
  669. }
  670. /// JoinIntervals - Attempt to join these two intervals. On failure, this
  671. /// returns false. Otherwise, if one of the intervals being joined is a
  672. /// physreg, this method always canonicalizes LHS to be it. The output
  673. /// "RHS" will not have been modified, so we can use this information
  674. /// below to update aliases.
  675. bool SimpleRegisterCoalescing::JoinIntervals(LiveInterval &LHS,
  676. LiveInterval &RHS, bool &Swapped) {
  677. // Compute the final value assignment, assuming that the live ranges can be
  678. // coalesced.
  679. SmallVector<int, 16> LHSValNoAssignments;
  680. SmallVector<int, 16> RHSValNoAssignments;
  681. DenseMap<VNInfo*, VNInfo*> LHSValsDefinedFromRHS;
  682. DenseMap<VNInfo*, VNInfo*> RHSValsDefinedFromLHS;
  683. SmallVector<VNInfo*, 16> NewVNInfo;
  684. // If a live interval is a physical register, conservatively check if any
  685. // of its sub-registers is overlapping the live interval of the virtual
  686. // register. If so, do not coalesce.
  687. if (MRegisterInfo::isPhysicalRegister(LHS.reg) &&
  688. *mri_->getSubRegisters(LHS.reg)) {
  689. for (const unsigned* SR = mri_->getSubRegisters(LHS.reg); *SR; ++SR)
  690. if (li_->hasInterval(*SR) && RHS.overlaps(li_->getInterval(*SR))) {
  691. DOUT << "Interfere with sub-register ";
  692. DEBUG(li_->getInterval(*SR).print(DOUT, mri_));
  693. return false;
  694. }
  695. } else if (MRegisterInfo::isPhysicalRegister(RHS.reg) &&
  696. *mri_->getSubRegisters(RHS.reg)) {
  697. for (const unsigned* SR = mri_->getSubRegisters(RHS.reg); *SR; ++SR)
  698. if (li_->hasInterval(*SR) && LHS.overlaps(li_->getInterval(*SR))) {
  699. DOUT << "Interfere with sub-register ";
  700. DEBUG(li_->getInterval(*SR).print(DOUT, mri_));
  701. return false;
  702. }
  703. }
  704. // Compute ultimate value numbers for the LHS and RHS values.
  705. if (RHS.containsOneValue()) {
  706. // Copies from a liveinterval with a single value are simple to handle and
  707. // very common, handle the special case here. This is important, because
  708. // often RHS is small and LHS is large (e.g. a physreg).
  709. // Find out if the RHS is defined as a copy from some value in the LHS.
  710. int RHSVal0DefinedFromLHS = -1;
  711. int RHSValID = -1;
  712. VNInfo *RHSValNoInfo = NULL;
  713. VNInfo *RHSValNoInfo0 = RHS.getValNumInfo(0);
  714. unsigned RHSSrcReg = RHSValNoInfo0->reg;
  715. if ((RHSSrcReg == 0 || rep(RHSSrcReg) != LHS.reg)) {
  716. // If RHS is not defined as a copy from the LHS, we can use simpler and
  717. // faster checks to see if the live ranges are coalescable. This joiner
  718. // can't swap the LHS/RHS intervals though.
  719. if (!MRegisterInfo::isPhysicalRegister(RHS.reg)) {
  720. return SimpleJoin(LHS, RHS);
  721. } else {
  722. RHSValNoInfo = RHSValNoInfo0;
  723. }
  724. } else {
  725. // It was defined as a copy from the LHS, find out what value # it is.
  726. RHSValNoInfo = LHS.getLiveRangeContaining(RHSValNoInfo0->def-1)->valno;
  727. RHSValID = RHSValNoInfo->id;
  728. RHSVal0DefinedFromLHS = RHSValID;
  729. }
  730. LHSValNoAssignments.resize(LHS.getNumValNums(), -1);
  731. RHSValNoAssignments.resize(RHS.getNumValNums(), -1);
  732. NewVNInfo.resize(LHS.getNumValNums(), NULL);
  733. // Okay, *all* of the values in LHS that are defined as a copy from RHS
  734. // should now get updated.
  735. for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end();
  736. i != e; ++i) {
  737. VNInfo *VNI = *i;
  738. unsigned VN = VNI->id;
  739. if (unsigned LHSSrcReg = VNI->reg) {
  740. if (rep(LHSSrcReg) != RHS.reg) {
  741. // If this is not a copy from the RHS, its value number will be
  742. // unmodified by the coalescing.
  743. NewVNInfo[VN] = VNI;
  744. LHSValNoAssignments[VN] = VN;
  745. } else if (RHSValID == -1) {
  746. // Otherwise, it is a copy from the RHS, and we don't already have a
  747. // value# for it. Keep the current value number, but remember it.
  748. LHSValNoAssignments[VN] = RHSValID = VN;
  749. NewVNInfo[VN] = RHSValNoInfo;
  750. LHSValsDefinedFromRHS[VNI] = RHSValNoInfo0;
  751. } else {
  752. // Otherwise, use the specified value #.
  753. LHSValNoAssignments[VN] = RHSValID;
  754. if (VN == (unsigned)RHSValID) { // Else this val# is dead.
  755. NewVNInfo[VN] = RHSValNoInfo;
  756. LHSValsDefinedFromRHS[VNI] = RHSValNoInfo0;
  757. }
  758. }
  759. } else {
  760. NewVNInfo[VN] = VNI;
  761. LHSValNoAssignments[VN] = VN;
  762. }
  763. }
  764. assert(RHSValID != -1 && "Didn't find value #?");
  765. RHSValNoAssignments[0] = RHSValID;
  766. if (RHSVal0DefinedFromLHS != -1) {
  767. // This path doesn't go through ComputeUltimateVN so just set
  768. // it to anything.
  769. RHSValsDefinedFromLHS[RHSValNoInfo0] = (VNInfo*)1;
  770. }
  771. } else {
  772. // Loop over the value numbers of the LHS, seeing if any are defined from
  773. // the RHS.
  774. for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end();
  775. i != e; ++i) {
  776. VNInfo *VNI = *i;
  777. unsigned ValSrcReg = VNI->reg;
  778. if (VNI->def == ~1U ||ValSrcReg == 0) // Src not defined by a copy?
  779. continue;
  780. // DstReg is known to be a register in the LHS interval. If the src is
  781. // from the RHS interval, we can use its value #.
  782. if (rep(ValSrcReg) != RHS.reg)
  783. continue;
  784. // Figure out the value # from the RHS.
  785. LHSValsDefinedFromRHS[VNI]=RHS.getLiveRangeContaining(VNI->def-1)->valno;
  786. }
  787. // Loop over the value numbers of the RHS, seeing if any are defined from
  788. // the LHS.
  789. for (LiveInterval::vni_iterator i = RHS.vni_begin(), e = RHS.vni_end();
  790. i != e; ++i) {
  791. VNInfo *VNI = *i;
  792. unsigned ValSrcReg = VNI->reg;
  793. if (VNI->def == ~1U || ValSrcReg == 0) // Src not defined by a copy?
  794. continue;
  795. // DstReg is known to be a register in the RHS interval. If the src is
  796. // from the LHS interval, we can use its value #.
  797. if (rep(ValSrcReg) != LHS.reg)
  798. continue;
  799. // Figure out the value # from the LHS.
  800. RHSValsDefinedFromLHS[VNI]=LHS.getLiveRangeContaining(VNI->def-1)->valno;
  801. }
  802. LHSValNoAssignments.resize(LHS.getNumValNums(), -1);
  803. RHSValNoAssignments.resize(RHS.getNumValNums(), -1);
  804. NewVNInfo.reserve(LHS.getNumValNums() + RHS.getNumValNums());
  805. for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end();
  806. i != e; ++i) {
  807. VNInfo *VNI = *i;
  808. unsigned VN = VNI->id;
  809. if (LHSValNoAssignments[VN] >= 0 || VNI->def == ~1U)
  810. continue;
  811. ComputeUltimateVN(VNI, NewVNInfo,
  812. LHSValsDefinedFromRHS, RHSValsDefinedFromLHS,
  813. LHSValNoAssignments, RHSValNoAssignments);
  814. }
  815. for (LiveInterval::vni_iterator i = RHS.vni_begin(), e = RHS.vni_end();
  816. i != e; ++i) {
  817. VNInfo *VNI = *i;
  818. unsigned VN = VNI->id;
  819. if (RHSValNoAssignments[VN] >= 0 || VNI->def == ~1U)
  820. continue;
  821. // If this value number isn't a copy from the LHS, it's a new number.
  822. if (RHSValsDefinedFromLHS.find(VNI) == RHSValsDefinedFromLHS.end()) {
  823. NewVNInfo.push_back(VNI);
  824. RHSValNoAssignments[VN] = NewVNInfo.size()-1;
  825. continue;
  826. }
  827. ComputeUltimateVN(VNI, NewVNInfo,
  828. RHSValsDefinedFromLHS, LHSValsDefinedFromRHS,
  829. RHSValNoAssignments, LHSValNoAssignments);
  830. }
  831. }
  832. // Armed with the mappings of LHS/RHS values to ultimate values, walk the
  833. // interval lists to see if these intervals are coalescable.
  834. LiveInterval::const_iterator I = LHS.begin();
  835. LiveInterval::const_iterator IE = LHS.end();
  836. LiveInterval::const_iterator J = RHS.begin();
  837. LiveInterval::const_iterator JE = RHS.end();
  838. // Skip ahead until the first place of potential sharing.
  839. if (I->start < J->start) {
  840. I = std::upper_bound(I, IE, J->start);
  841. if (I != LHS.begin()) --I;
  842. } else if (J->start < I->start) {
  843. J = std::upper_bound(J, JE, I->start);
  844. if (J != RHS.begin()) --J;
  845. }
  846. while (1) {
  847. // Determine if these two live ranges overlap.
  848. bool Overlaps;
  849. if (I->start < J->start) {
  850. Overlaps = I->end > J->start;
  851. } else {
  852. Overlaps = J->end > I->start;
  853. }
  854. // If so, check value # info to determine if they are really different.
  855. if (Overlaps) {
  856. // If the live range overlap will map to the same value number in the
  857. // result liverange, we can still coalesce them. If not, we can't.
  858. if (LHSValNoAssignments[I->valno->id] !=
  859. RHSValNoAssignments[J->valno->id])
  860. return false;
  861. }
  862. if (I->end < J->end) {
  863. ++I;
  864. if (I == IE) break;
  865. } else {
  866. ++J;
  867. if (J == JE) break;
  868. }
  869. }
  870. // Update kill info. Some live ranges are extended due to copy coalescing.
  871. for (DenseMap<VNInfo*, VNInfo*>::iterator I = LHSValsDefinedFromRHS.begin(),
  872. E = LHSValsDefinedFromRHS.end(); I != E; ++I) {
  873. VNInfo *VNI = I->first;
  874. unsigned LHSValID = LHSValNoAssignments[VNI->id];
  875. LiveInterval::removeKill(NewVNInfo[LHSValID], VNI->def);
  876. NewVNInfo[LHSValID]->hasPHIKill |= VNI->hasPHIKill;
  877. RHS.addKills(NewVNInfo[LHSValID], VNI->kills);
  878. }
  879. // Update kill info. Some live ranges are extended due to copy coalescing.
  880. for (DenseMap<VNInfo*, VNInfo*>::iterator I = RHSValsDefinedFromLHS.begin(),
  881. E = RHSValsDefinedFromLHS.end(); I != E; ++I) {
  882. VNInfo *VNI = I->first;
  883. unsigned RHSValID = RHSValNoAssignments[VNI->id];
  884. LiveInterval::removeKill(NewVNInfo[RHSValID], VNI->def);
  885. NewVNInfo[RHSValID]->hasPHIKill |= VNI->hasPHIKill;
  886. LHS.addKills(NewVNInfo[RHSValID], VNI->kills);
  887. }
  888. // If we get here, we know that we can coalesce the live ranges. Ask the
  889. // intervals to coalesce themselves now.
  890. if ((RHS.ranges.size() > LHS.ranges.size() &&
  891. MRegisterInfo::isVirtualRegister(LHS.reg)) ||
  892. MRegisterInfo::isPhysicalRegister(RHS.reg)) {
  893. RHS.join(LHS, &RHSValNoAssignments[0], &LHSValNoAssignments[0], NewVNInfo);
  894. Swapped = true;
  895. } else {
  896. LHS.join(RHS, &LHSValNoAssignments[0], &RHSValNoAssignments[0], NewVNInfo);
  897. Swapped = false;
  898. }
  899. return true;
  900. }
  901. namespace {
  902. // DepthMBBCompare - Comparison predicate that sort first based on the loop
  903. // depth of the basic block (the unsigned), and then on the MBB number.
  904. struct DepthMBBCompare {
  905. typedef std::pair<unsigned, MachineBasicBlock*> DepthMBBPair;
  906. bool operator()(const DepthMBBPair &LHS, const DepthMBBPair &RHS) const {
  907. if (LHS.first > RHS.first) return true; // Deeper loops first
  908. return LHS.first == RHS.first &&
  909. LHS.second->getNumber() < RHS.second->getNumber();
  910. }
  911. };
  912. }
  913. /// getRepIntervalSize - Returns the size of the interval that represents the
  914. /// specified register.
  915. template<class SF>
  916. unsigned JoinPriorityQueue<SF>::getRepIntervalSize(unsigned Reg) {
  917. return Rc->getRepIntervalSize(Reg);
  918. }
  919. /// CopyRecSort::operator - Join priority queue sorting function.
  920. ///
  921. bool CopyRecSort::operator()(CopyRec left, CopyRec right) const {
  922. // Inner loops first.
  923. if (left.LoopDepth > right.LoopDepth)
  924. return false;
  925. else if (left.LoopDepth == right.LoopDepth) {
  926. if (left.isBackEdge && !right.isBackEdge)
  927. return false;
  928. else if (left.isBackEdge == right.isBackEdge) {
  929. // Join virtuals to physical registers first.
  930. bool LDstIsPhys = MRegisterInfo::isPhysicalRegister(left.DstReg);
  931. bool LSrcIsPhys = MRegisterInfo::isPhysicalRegister(left.SrcReg);
  932. bool LIsPhys = LDstIsPhys || LSrcIsPhys;
  933. bool RDstIsPhys = MRegisterInfo::isPhysicalRegister(right.DstReg);
  934. bool RSrcIsPhys = MRegisterInfo::isPhysicalRegister(right.SrcReg);
  935. bool RIsPhys = RDstIsPhys || RSrcIsPhys;
  936. if (LIsPhys && !RIsPhys)
  937. return false;
  938. else if (LIsPhys == RIsPhys) {
  939. // Join shorter intervals first.
  940. unsigned LSize = 0;
  941. unsigned RSize = 0;
  942. if (LIsPhys) {
  943. LSize = LDstIsPhys ? 0 : JPQ->getRepIntervalSize(left.DstReg);
  944. LSize += LSrcIsPhys ? 0 : JPQ->getRepIntervalSize(left.SrcReg);
  945. RSize = RDstIsPhys ? 0 : JPQ->getRepIntervalSize(right.DstReg);
  946. RSize += RSrcIsPhys ? 0 : JPQ->getRepIntervalSize(right.SrcReg);
  947. } else {
  948. LSize = std::min(JPQ->getRepIntervalSize(left.DstReg),
  949. JPQ->getRepIntervalSize(left.SrcReg));
  950. RSize = std::min(JPQ->getRepIntervalSize(right.DstReg),
  951. JPQ->getRepIntervalSize(right.SrcReg));
  952. }
  953. if (LSize < RSize)
  954. return false;
  955. }
  956. }
  957. }
  958. return true;
  959. }
  960. void SimpleRegisterCoalescing::CopyCoalesceInMBB(MachineBasicBlock *MBB,
  961. std::vector<CopyRec> &TryAgain) {
  962. DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
  963. std::vector<CopyRec> VirtCopies;
  964. std::vector<CopyRec> PhysCopies;
  965. unsigned LoopDepth = loopInfo->getLoopDepth(MBB);
  966. for (MachineBasicBlock::iterator MII = MBB->begin(), E = MBB->end();
  967. MII != E;) {
  968. MachineInstr *Inst = MII++;
  969. // If this isn't a copy nor a extract_subreg, we can't join intervals.
  970. unsigned SrcReg, DstReg;
  971. if (Inst->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
  972. DstReg = Inst->getOperand(0).getReg();
  973. SrcReg = Inst->getOperand(1).getReg();
  974. } else if (!tii_->isMoveInstr(*Inst, SrcReg, DstReg))
  975. continue;
  976. unsigned repSrcReg = rep(SrcReg);
  977. unsigned repDstReg = rep(DstReg);
  978. bool SrcIsPhys = MRegisterInfo::isPhysicalRegister(repSrcReg);
  979. bool DstIsPhys = MRegisterInfo::isPhysicalRegister(repDstReg);
  980. if (NewHeuristic) {
  981. JoinQueue->push(CopyRec(Inst, SrcReg, DstReg, LoopDepth,
  982. isBackEdgeCopy(Inst, DstReg)));
  983. } else {
  984. if (SrcIsPhys || DstIsPhys)
  985. PhysCopies.push_back(CopyRec(Inst, SrcReg, DstReg, 0, false));
  986. else
  987. VirtCopies.push_back(CopyRec(Inst, SrcReg, DstReg, 0, false));
  988. }
  989. }
  990. if (NewHeuristic)
  991. return;
  992. // Try coalescing physical register + virtual register first.
  993. for (unsigned i = 0, e = PhysCopies.size(); i != e; ++i) {
  994. CopyRec &TheCopy = PhysCopies[i];
  995. bool Again = false;
  996. if (!JoinCopy(TheCopy, Again))
  997. if (Again)
  998. TryAgain.push_back(TheCopy);
  999. }
  1000. for (unsigned i = 0, e = VirtCopies.size(); i != e; ++i) {
  1001. CopyRec &TheCopy = VirtCopies[i];
  1002. bool Again = false;
  1003. if (!JoinCopy(TheCopy, Again))
  1004. if (Again)
  1005. TryAgain.push_back(TheCopy);
  1006. }
  1007. }
  1008. void SimpleRegisterCoalescing::joinIntervals() {
  1009. DOUT << "********** JOINING INTERVALS ***********\n";
  1010. if (NewHeuristic)
  1011. JoinQueue = new JoinPriorityQueue<CopyRecSort>(this);
  1012. JoinedLIs.resize(li_->getNumIntervals());
  1013. JoinedLIs.reset();
  1014. std::vector<CopyRec> TryAgainList;
  1015. if (loopInfo->begin() == loopInfo->end()) {
  1016. // If there are no loops in the function, join intervals in function order.
  1017. for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
  1018. I != E; ++I)
  1019. CopyCoalesceInMBB(I, TryAgainList);
  1020. } else {
  1021. // Otherwise, join intervals in inner loops before other intervals.
  1022. // Unfortunately we can't just iterate over loop hierarchy here because
  1023. // there may be more MBB's than BB's. Collect MBB's for sorting.
  1024. // Join intervals in the function prolog first. We want to join physical
  1025. // registers with virtual registers before the intervals got too long.
  1026. std::vector<std::pair<unsigned, MachineBasicBlock*> > MBBs;
  1027. for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();I != E;++I){
  1028. MachineBasicBlock *MBB = I;
  1029. MBBs.push_back(std::make_pair(loopInfo->getLoopDepth(MBB), I));
  1030. }
  1031. // Sort by loop depth.
  1032. std::sort(MBBs.begin(), MBBs.end(), DepthMBBCompare());
  1033. // Finally, join intervals in loop nest order.
  1034. for (unsigned i = 0, e = MBBs.size(); i != e; ++i)
  1035. CopyCoalesceInMBB(MBBs[i].second, TryAgainList);
  1036. }
  1037. // Joining intervals can allow other intervals to be joined. Iteratively join
  1038. // until we make no progress.
  1039. if (NewHeuristic) {
  1040. SmallVector<CopyRec, 16> TryAgain;
  1041. bool ProgressMade = true;
  1042. while (ProgressMade) {
  1043. ProgressMade = false;
  1044. while (!JoinQueue->empty()) {
  1045. CopyRec R = JoinQueue->pop();
  1046. bool Again = false;
  1047. bool Success = JoinCopy(R, Again);
  1048. if (Success)
  1049. ProgressMade = true;
  1050. else if (Again)
  1051. TryAgain.push_back(R);
  1052. }
  1053. if (ProgressMade) {
  1054. while (!TryAgain.empty()) {
  1055. JoinQueue->push(TryAgain.back());
  1056. TryAgain.pop_back();
  1057. }
  1058. }
  1059. }
  1060. } else {
  1061. bool ProgressMade = true;
  1062. while (ProgressMade) {
  1063. ProgressMade = false;
  1064. for (unsigned i = 0, e = TryAgainList.size(); i != e; ++i) {
  1065. CopyRec &TheCopy = TryAgainList[i];
  1066. if (TheCopy.MI) {
  1067. bool Again = false;
  1068. bool Success = JoinCopy(TheCopy, Again);
  1069. if (Success || !Again) {
  1070. TheCopy.MI = 0; // Mark this one as done.
  1071. ProgressMade = true;
  1072. }
  1073. }
  1074. }
  1075. }
  1076. }
  1077. // Some live range has been lengthened due to colaescing, eliminate the
  1078. // unnecessary kills.
  1079. int RegNum = JoinedLIs.find_first();
  1080. while (RegNum != -1) {
  1081. unsigned Reg = RegNum + MRegisterInfo::FirstVirtualRegister;
  1082. unsigned repReg = rep(Reg);
  1083. LiveInterval &LI = li_->getInterval(repReg);
  1084. LiveVariables::VarInfo& svi = lv_->getVarInfo(Reg);
  1085. for (unsigned i = 0, e = svi.Kills.size(); i != e; ++i) {
  1086. MachineInstr *Kill = svi.Kills[i];
  1087. // Suppose vr1 = op vr2, x
  1088. // and vr1 and vr2 are coalesced. vr2 should still be marked kill
  1089. // unless it is a two-address operand.
  1090. if (li_->isRemoved(Kill) || hasRegisterDef(Kill, repReg))
  1091. continue;
  1092. if (LI.liveAt(li_->getInstructionIndex(Kill) + InstrSlots::NUM))
  1093. unsetRegisterKill(Kill, repReg);
  1094. }
  1095. RegNum = JoinedLIs.find_next(RegNum);
  1096. }
  1097. if (NewHeuristic)
  1098. delete JoinQueue;
  1099. DOUT << "*** Register mapping ***\n";
  1100. for (unsigned i = 0, e = r2rMap_.size(); i != e; ++i)
  1101. if (r2rMap_[i]) {
  1102. DOUT << " reg " << i << " -> ";
  1103. DEBUG(printRegName(r2rMap_[i]));
  1104. DOUT << "\n";
  1105. }
  1106. }
  1107. /// Return true if the two specified registers belong to different register
  1108. /// classes. The registers may be either phys or virt regs.
  1109. bool SimpleRegisterCoalescing::differingRegisterClasses(unsigned RegA,
  1110. unsigned RegB) const {
  1111. // Get the register classes for the first reg.
  1112. if (MRegisterInfo::isPhysicalRegister(RegA)) {
  1113. assert(MRegisterInfo::isVirtualRegister(RegB) &&
  1114. "Shouldn't consider two physregs!");
  1115. return !mf_->getRegInfo().getRegClass(RegB)->contains(RegA);
  1116. }
  1117. // Compare against the regclass for the second reg.
  1118. const TargetRegisterClass *RegClass = mf_->getRegInfo().getRegClass(RegA);
  1119. if (MRegisterInfo::isVirtualRegister(RegB))
  1120. return RegClass != mf_->getRegInfo().getRegClass(RegB);
  1121. else
  1122. return !RegClass->contains(RegB);
  1123. }
  1124. /// lastRegisterUse - Returns the last use of the specific register between
  1125. /// cycles Start and End. It also returns the use operand by reference. It
  1126. /// returns NULL if there are no uses.
  1127. MachineInstr *
  1128. SimpleRegisterCoalescing::lastRegisterUse(unsigned Start, unsigned End,
  1129. unsigned Reg, MachineOperand *&MOU) {
  1130. int e = (End-1) / InstrSlots::NUM * InstrSlots::NUM;
  1131. int s = Start;
  1132. while (e >= s) {
  1133. // Skip deleted instructions
  1134. MachineInstr *MI = li_->getInstructionFromIndex(e);
  1135. while ((e - InstrSlots::NUM) >= s && !MI) {
  1136. e -= InstrSlots::NUM;
  1137. MI = li_->getInstructionFromIndex(e);
  1138. }
  1139. if (e < s || MI == NULL)
  1140. return NULL;
  1141. for (unsigned i = 0, NumOps = MI->getNumOperands(); i != NumOps; ++i) {
  1142. MachineOperand &MO = MI->getOperand(i);
  1143. if (MO.isRegister() && MO.isUse() && MO.getReg() &&
  1144. mri_->regsOverlap(rep(MO.getReg()), Reg)) {
  1145. MOU = &MO;
  1146. return MI;
  1147. }
  1148. }
  1149. e -= InstrSlots::NUM;
  1150. }
  1151. return NULL;
  1152. }
  1153. /// findDefOperand - Returns the MachineOperand that is a def of the specific
  1154. /// register. It returns NULL if the def is not found.
  1155. MachineOperand *SimpleRegisterCoalescing::findDefOperand(MachineInstr *MI,
  1156. unsigned Reg) {
  1157. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  1158. MachineOperand &MO = MI->getOperand(i);
  1159. if (MO.isRegister() && MO.isDef() &&
  1160. mri_->regsOverlap(rep(MO.getReg()), Reg))
  1161. return &MO;
  1162. }
  1163. return NULL;
  1164. }
  1165. /// unsetRegisterKill - Unset IsKill property of all uses of specific register
  1166. /// of the specific instruction.
  1167. void SimpleRegisterCoalescing::unsetRegisterKill(MachineInstr *MI,
  1168. unsigned Reg) {
  1169. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  1170. MachineOperand &MO = MI->getOperand(i);
  1171. if (MO.isRegister() && MO.isKill() && MO.getReg() &&
  1172. mri_->regsOverlap(rep(MO.getReg()), Reg))
  1173. MO.setIsKill(false);
  1174. }
  1175. }
  1176. /// unsetRegisterKills - Unset IsKill property of all uses of specific register
  1177. /// between cycles Start and End.
  1178. void SimpleRegisterCoalescing::unsetRegisterKills(unsigned Start, unsigned End,
  1179. unsigned Reg) {
  1180. int e = (End-1) / InstrSlots::NUM * InstrSlots::NUM;
  1181. int s = Start;
  1182. while (e >= s) {
  1183. // Skip deleted instructions
  1184. MachineInstr *MI = li_->getInstructionFromIndex(e);
  1185. while ((e - InstrSlots::NUM) >= s && !MI) {
  1186. e -= InstrSlots::NUM;
  1187. MI = li_->getInstructionFromIndex(e);
  1188. }
  1189. if (e < s || MI == NULL)
  1190. return;
  1191. for (unsigned i = 0, NumOps = MI->getNumOperands(); i != NumOps; ++i) {
  1192. MachineOperand &MO = MI->getOperand(i);
  1193. if (MO.isRegister() && MO.isKill() && MO.getReg() &&
  1194. mri_->regsOverlap(rep(MO.getReg()), Reg)) {
  1195. MO.setIsKill(false);
  1196. }
  1197. }
  1198. e -= InstrSlots::NUM;
  1199. }
  1200. }
  1201. /// hasRegisterDef - True if the instruction defines the specific register.
  1202. ///
  1203. bool SimpleRegisterCoalescing::hasRegisterDef(MachineInstr *MI, unsigned Reg) {
  1204. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  1205. MachineOperand &MO = MI->getOperand(i);
  1206. if (MO.isRegister() && MO.isDef() &&
  1207. mri_->regsOverlap(rep(MO.getReg()), Reg))
  1208. return true;
  1209. }
  1210. return false;
  1211. }
  1212. void SimpleRegisterCoalescing::printRegName(unsigned reg) const {
  1213. if (MRegisterInfo::isPhysicalRegister(reg))
  1214. cerr << mri_->getName(reg);
  1215. else
  1216. cerr << "%reg" << reg;
  1217. }
  1218. void SimpleRegisterCoalescing::releaseMemory() {
  1219. for (unsigned i = 0, e = r2rMap_.size(); i != e; ++i)
  1220. r2rRevMap_[i].clear();
  1221. r2rRevMap_.clear();
  1222. r2rMap_.clear();
  1223. JoinedLIs.clear();
  1224. SubRegIdxes.clear();
  1225. JoinedCopies.clear();
  1226. }
  1227. static bool isZeroLengthInterval(LiveInterval *li) {
  1228. for (LiveInterval::Ranges::const_iterator
  1229. i = li->ranges.begin(), e = li->ranges.end(); i != e; ++i)
  1230. if (i->end - i->start > LiveIntervals::InstrSlots::NUM)
  1231. return false;
  1232. return true;
  1233. }
  1234. bool SimpleRegisterCoalescing::runOnMachineFunction(MachineFunction &fn) {
  1235. mf_ = &fn;
  1236. tm_ = &fn.getTarget();
  1237. mri_ = tm_->getRegisterInfo();
  1238. tii_ = tm_->getInstrInfo();
  1239. li_ = &getAnalysis<LiveIntervals>();
  1240. lv_ = &getAnalysis<LiveVariables>();
  1241. loopInfo = &getAnalysis<MachineLoopInfo>();
  1242. DOUT << "********** SIMPLE REGISTER COALESCING **********\n"
  1243. << "********** Function: "
  1244. << ((Value*)mf_->getFunction())->getName() << '\n';
  1245. allocatableRegs_ = mri_->getAllocatableSet(fn);
  1246. for (MRegisterInfo::regclass_iterator I = mri_->regclass_begin(),
  1247. E = mri_->regclass_end(); I != E; ++I)
  1248. allocatableRCRegs_.insert(std::make_pair(*I,
  1249. mri_->getAllocatableSet(fn, *I)));
  1250. MachineRegisterInfo &RegInfo = mf_->getRegInfo();
  1251. r2rMap_.grow(RegInfo.getLastVirtReg());
  1252. r2rRevMap_.grow(RegInfo.getLastVirtReg());
  1253. // Join (coalesce) intervals if requested.
  1254. IndexedMap<unsigned, VirtReg2IndexFunctor> RegSubIdxMap;
  1255. if (EnableJoining) {
  1256. joinIntervals();
  1257. DOUT << "********** INTERVALS POST JOINING **********\n";
  1258. for (LiveIntervals::iterator I = li_->begin(), E = li_->end(); I != E; ++I){
  1259. I->second.print(DOUT, mri_);
  1260. DOUT << "\n";
  1261. }
  1262. // Delete all coalesced copies.
  1263. for (SmallPtrSet<MachineInstr*,32>::iterator I = JoinedCopies.begin(),
  1264. E = JoinedCopies.end(); I != E; ++I) {
  1265. li_->RemoveMachineInstrFromMaps(*I);
  1266. (*I)->eraseFromParent();
  1267. }
  1268. // Transfer sub-registers info to MachineRegisterInfo now that coalescing
  1269. // information is complete.
  1270. RegSubIdxMap.grow(RegInfo.getLastVirtReg()+1);
  1271. while (!SubRegIdxes.empty()) {
  1272. std::pair<unsigned, unsigned> RI = SubRegIdxes.back();
  1273. SubRegIdxes.pop_back();
  1274. RegSubIdxMap[RI.first] = RI.second;
  1275. }
  1276. }
  1277. // perform a final pass over the instructions and compute spill
  1278. // weights, coalesce virtual registers and remove identity moves.
  1279. for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
  1280. mbbi != mbbe; ++mbbi) {
  1281. MachineBasicBlock* mbb = mbbi;
  1282. unsigned loopDepth = loopInfo->getLoopDepth(mbb);
  1283. for (MachineBasicBlock::iterator mii = mbb->begin(), mie = mbb->end();
  1284. mii != mie; ) {
  1285. // if the move will be an identity move delete it
  1286. unsigned srcReg, dstReg, RegRep;
  1287. if (tii_->isMoveInstr(*mii, srcReg, dstReg) &&
  1288. (RegRep = rep(srcReg)) == rep(dstReg)) {
  1289. // remove from def list
  1290. LiveInterval &RegInt = li_->getOrCreateInterval(RegRep);
  1291. MachineOperand *MO = mii->findRegisterDefOperand(dstReg);
  1292. // If def of this move instruction is dead, remove its live range from
  1293. // the dstination register's live interval.
  1294. if (MO->isDead()) {
  1295. unsigned MoveIdx = li_->getDefIndex(li_->getInstructionIndex(mii));
  1296. LiveInterval::iterator MLR = RegInt.FindLiveRangeContaining(MoveIdx);
  1297. RegInt.removeRange(MLR->start, MoveIdx+1);
  1298. if (RegInt.empty())
  1299. li_->removeInterval(RegRep);
  1300. }
  1301. li_->RemoveMachineInstrFromMaps(mii);
  1302. mii = mbbi->erase(mii);
  1303. ++numPeep;
  1304. } else {
  1305. SmallSet<unsigned, 4> UniqueUses;
  1306. for (unsigned i = 0, e = mii->getNumOperands(); i != e; ++i) {
  1307. const MachineOperand &mop = mii->getOperand(i);
  1308. if (mop.isRegister() && mop.getReg() &&
  1309. MRegisterInfo::isVirtualRegister(mop.getReg())) {
  1310. // replace register with representative register
  1311. unsigned OrigReg = mop.getReg();
  1312. unsigned reg = rep(OrigReg);
  1313. unsigned SubIdx = RegSubIdxMap[OrigReg];
  1314. if (SubIdx && MRegisterInfo::isPhysicalRegister(reg))
  1315. mii->getOperand(i).setReg(mri_->getSubReg(reg, SubIdx));
  1316. else {
  1317. mii->getOperand(i).setReg(reg);
  1318. mii->getOperand(i).setSubReg(SubIdx);
  1319. }
  1320. // Multiple uses of reg by the same instruction. It should not
  1321. // contribute to spill weight again.
  1322. if (UniqueUses.count(reg) != 0)
  1323. continue;
  1324. LiveInterval &RegInt = li_->getInterval(reg);
  1325. RegInt.weight +=
  1326. li_->getSpillWeight(mop.isDef(), mop.isUse(), loopDepth);
  1327. UniqueUses.insert(reg);
  1328. }
  1329. }
  1330. ++mii;
  1331. }
  1332. }
  1333. }
  1334. for (LiveIntervals::iterator I = li_->begin(), E = li_->end(); I != E; ++I) {
  1335. LiveInterval &LI = I->second;
  1336. if (MRegisterInfo::isVirtualRegister(LI.reg)) {
  1337. // If the live interval length is essentially zero, i.e. in every live
  1338. // range the use follows def immediately, it doesn't make sense to spill
  1339. // it and hope it will be easier to allocate for this li.
  1340. if (isZeroLengthInterval(&LI))
  1341. LI.weight = HUGE_VALF;
  1342. else {
  1343. bool isLoad = false;
  1344. if (ReMatSpillWeight && li_->isReMaterializable(LI, isLoad)) {
  1345. // If all of the definitions of the interval are re-materializable,
  1346. // it is a preferred candidate for spilling. If non of the defs are
  1347. // loads, then it's potentially very cheap to re-materialize.
  1348. // FIXME: this gets much more complicated once we support non-trivial
  1349. // re-materialization.
  1350. if (isLoad)
  1351. LI.weight *= 0.9F;
  1352. else
  1353. LI.weight *= 0.5F;
  1354. }
  1355. }
  1356. // Slightly prefer live interval that has been assigned a preferred reg.
  1357. if (LI.preference)
  1358. LI.weight *= 1.01F;
  1359. // Divide the weight of the interval by its size. This encourages
  1360. // spilling of intervals that are large and have few uses, and
  1361. // discourages spilling of small intervals with many uses.
  1362. LI.weight /= LI.getSize();
  1363. }
  1364. }
  1365. DEBUG(dump());
  1366. return true;
  1367. }
  1368. /// print - Implement the dump method.
  1369. void SimpleRegisterCoalescing::print(std::ostream &O, const Module* m) const {
  1370. li_->print(O, m);
  1371. }
  1372. RegisterCoalescer* llvm::createSimpleRegisterCoalescer() {
  1373. return new SimpleRegisterCoalescing();
  1374. }
  1375. // Make sure that anything that uses RegisterCoalescer pulls in this file...
  1376. DEFINING_FILE_FOR(SimpleRegisterCoalescing)