SimpleRegisterCoalescing.cpp 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894
  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/MachineFrameInfo.h"
  20. #include "llvm/CodeGen/MachineInstr.h"
  21. #include "llvm/CodeGen/MachineLoopInfo.h"
  22. #include "llvm/CodeGen/MachineRegisterInfo.h"
  23. #include "llvm/CodeGen/Passes.h"
  24. #include "llvm/CodeGen/RegisterCoalescer.h"
  25. #include "llvm/Target/TargetInstrInfo.h"
  26. #include "llvm/Target/TargetMachine.h"
  27. #include "llvm/Target/TargetOptions.h"
  28. #include "llvm/Support/CommandLine.h"
  29. #include "llvm/Support/Debug.h"
  30. #include "llvm/Support/ErrorHandling.h"
  31. #include "llvm/ADT/SmallSet.h"
  32. #include "llvm/ADT/Statistic.h"
  33. #include "llvm/ADT/STLExtras.h"
  34. #include <algorithm>
  35. #include <cmath>
  36. using namespace llvm;
  37. STATISTIC(numJoins , "Number of interval joins performed");
  38. STATISTIC(numCrossRCs , "Number of cross class joins performed");
  39. STATISTIC(numCommutes , "Number of instruction commuting performed");
  40. STATISTIC(numExtends , "Number of copies extended");
  41. STATISTIC(NumReMats , "Number of instructions re-materialized");
  42. STATISTIC(numPeep , "Number of identity moves eliminated after coalescing");
  43. STATISTIC(numAborts , "Number of times interval joining aborted");
  44. STATISTIC(numDeadValNo, "Number of valno def marked dead");
  45. char SimpleRegisterCoalescing::ID = 0;
  46. static cl::opt<bool>
  47. EnableJoining("join-liveintervals",
  48. cl::desc("Coalesce copies (default=true)"),
  49. cl::init(true));
  50. static cl::opt<bool>
  51. NewHeuristic("new-coalescer-heuristic",
  52. cl::desc("Use new coalescer heuristic"),
  53. cl::init(false), cl::Hidden);
  54. static cl::opt<bool>
  55. CrossClassJoin("join-cross-class-copies",
  56. cl::desc("Coalesce cross register class copies"),
  57. cl::init(false), cl::Hidden);
  58. static cl::opt<bool>
  59. PhysJoinTweak("tweak-phys-join-heuristics",
  60. cl::desc("Tweak heuristics for joining phys reg with vr"),
  61. cl::init(false), cl::Hidden);
  62. static RegisterPass<SimpleRegisterCoalescing>
  63. X("simple-register-coalescing", "Simple Register Coalescing");
  64. // Declare that we implement the RegisterCoalescer interface
  65. static RegisterAnalysisGroup<RegisterCoalescer, true/*The Default*/> V(X);
  66. const PassInfo *const llvm::SimpleRegisterCoalescingID = &X;
  67. void SimpleRegisterCoalescing::getAnalysisUsage(AnalysisUsage &AU) const {
  68. AU.addRequired<LiveIntervals>();
  69. AU.addPreserved<LiveIntervals>();
  70. AU.addRequired<MachineLoopInfo>();
  71. AU.addPreserved<MachineLoopInfo>();
  72. AU.addPreservedID(MachineDominatorsID);
  73. if (StrongPHIElim)
  74. AU.addPreservedID(StrongPHIEliminationID);
  75. else
  76. AU.addPreservedID(PHIEliminationID);
  77. AU.addPreservedID(TwoAddressInstructionPassID);
  78. MachineFunctionPass::getAnalysisUsage(AU);
  79. }
  80. /// AdjustCopiesBackFrom - We found a non-trivially-coalescable copy with IntA
  81. /// being the source and IntB being the dest, thus this defines a value number
  82. /// in IntB. If the source value number (in IntA) is defined by a copy from B,
  83. /// see if we can merge these two pieces of B into a single value number,
  84. /// eliminating a copy. For example:
  85. ///
  86. /// A3 = B0
  87. /// ...
  88. /// B1 = A3 <- this copy
  89. ///
  90. /// In this case, B0 can be extended to where the B1 copy lives, allowing the B1
  91. /// value number to be replaced with B0 (which simplifies the B liveinterval).
  92. ///
  93. /// This returns true if an interval was modified.
  94. ///
  95. bool SimpleRegisterCoalescing::AdjustCopiesBackFrom(LiveInterval &IntA,
  96. LiveInterval &IntB,
  97. MachineInstr *CopyMI) {
  98. unsigned CopyIdx = li_->getDefIndex(li_->getInstructionIndex(CopyMI));
  99. // BValNo is a value number in B that is defined by a copy from A. 'B3' in
  100. // the example above.
  101. LiveInterval::iterator BLR = IntB.FindLiveRangeContaining(CopyIdx);
  102. assert(BLR != IntB.end() && "Live range not found!");
  103. VNInfo *BValNo = BLR->valno;
  104. // Get the location that B is defined at. Two options: either this value has
  105. // an unknown definition point or it is defined at CopyIdx. If unknown, we
  106. // can't process it.
  107. if (!BValNo->copy) return false;
  108. assert(BValNo->def == CopyIdx && "Copy doesn't define the value?");
  109. // AValNo is the value number in A that defines the copy, A3 in the example.
  110. LiveInterval::iterator ALR = IntA.FindLiveRangeContaining(CopyIdx-1);
  111. assert(ALR != IntA.end() && "Live range not found!");
  112. VNInfo *AValNo = ALR->valno;
  113. // If it's re-defined by an early clobber somewhere in the live range, then
  114. // it's not safe to eliminate the copy. FIXME: This is a temporary workaround.
  115. // See PR3149:
  116. // 172 %ECX<def> = MOV32rr %reg1039<kill>
  117. // 180 INLINEASM <es:subl $5,$1
  118. // sbbl $3,$0>, 10, %EAX<def>, 14, %ECX<earlyclobber,def>, 9, %EAX<kill>,
  119. // 36, <fi#0>, 1, %reg0, 0, 9, %ECX<kill>, 36, <fi#1>, 1, %reg0, 0
  120. // 188 %EAX<def> = MOV32rr %EAX<kill>
  121. // 196 %ECX<def> = MOV32rr %ECX<kill>
  122. // 204 %ECX<def> = MOV32rr %ECX<kill>
  123. // 212 %EAX<def> = MOV32rr %EAX<kill>
  124. // 220 %EAX<def> = MOV32rr %EAX
  125. // 228 %reg1039<def> = MOV32rr %ECX<kill>
  126. // The early clobber operand ties ECX input to the ECX def.
  127. //
  128. // The live interval of ECX is represented as this:
  129. // %reg20,inf = [46,47:1)[174,230:0) 0@174-(230) 1@46-(47)
  130. // The coalescer has no idea there was a def in the middle of [174,230].
  131. if (AValNo->hasRedefByEC())
  132. return false;
  133. // If AValNo is defined as a copy from IntB, we can potentially process this.
  134. // Get the instruction that defines this value number.
  135. unsigned SrcReg = li_->getVNInfoSourceReg(AValNo);
  136. if (!SrcReg) return false; // Not defined by a copy.
  137. // If the value number is not defined by a copy instruction, ignore it.
  138. // If the source register comes from an interval other than IntB, we can't
  139. // handle this.
  140. if (SrcReg != IntB.reg) return false;
  141. // Get the LiveRange in IntB that this value number starts with.
  142. LiveInterval::iterator ValLR = IntB.FindLiveRangeContaining(AValNo->def-1);
  143. assert(ValLR != IntB.end() && "Live range not found!");
  144. // Make sure that the end of the live range is inside the same block as
  145. // CopyMI.
  146. MachineInstr *ValLREndInst = li_->getInstructionFromIndex(ValLR->end-1);
  147. if (!ValLREndInst ||
  148. ValLREndInst->getParent() != CopyMI->getParent()) return false;
  149. // Okay, we now know that ValLR ends in the same block that the CopyMI
  150. // live-range starts. If there are no intervening live ranges between them in
  151. // IntB, we can merge them.
  152. if (ValLR+1 != BLR) return false;
  153. // If a live interval is a physical register, conservatively check if any
  154. // of its sub-registers is overlapping the live interval of the virtual
  155. // register. If so, do not coalesce.
  156. if (TargetRegisterInfo::isPhysicalRegister(IntB.reg) &&
  157. *tri_->getSubRegisters(IntB.reg)) {
  158. for (const unsigned* SR = tri_->getSubRegisters(IntB.reg); *SR; ++SR)
  159. if (li_->hasInterval(*SR) && IntA.overlaps(li_->getInterval(*SR))) {
  160. DOUT << "Interfere with sub-register ";
  161. DEBUG(li_->getInterval(*SR).print(DOUT, tri_));
  162. return false;
  163. }
  164. }
  165. DOUT << "\nExtending: "; IntB.print(DOUT, tri_);
  166. unsigned FillerStart = ValLR->end, FillerEnd = BLR->start;
  167. // We are about to delete CopyMI, so need to remove it as the 'instruction
  168. // that defines this value #'. Update the the valnum with the new defining
  169. // instruction #.
  170. BValNo->def = FillerStart;
  171. BValNo->copy = NULL;
  172. // Okay, we can merge them. We need to insert a new liverange:
  173. // [ValLR.end, BLR.begin) of either value number, then we merge the
  174. // two value numbers.
  175. IntB.addRange(LiveRange(FillerStart, FillerEnd, BValNo));
  176. // If the IntB live range is assigned to a physical register, and if that
  177. // physreg has sub-registers, update their live intervals as well.
  178. if (TargetRegisterInfo::isPhysicalRegister(IntB.reg)) {
  179. for (const unsigned *SR = tri_->getSubRegisters(IntB.reg); *SR; ++SR) {
  180. LiveInterval &SRLI = li_->getInterval(*SR);
  181. SRLI.addRange(LiveRange(FillerStart, FillerEnd,
  182. SRLI.getNextValue(FillerStart, 0, true,
  183. li_->getVNInfoAllocator())));
  184. }
  185. }
  186. // Okay, merge "B1" into the same value number as "B0".
  187. if (BValNo != ValLR->valno) {
  188. IntB.addKills(ValLR->valno, BValNo->kills);
  189. IntB.MergeValueNumberInto(BValNo, ValLR->valno);
  190. }
  191. DOUT << " result = "; IntB.print(DOUT, tri_);
  192. DOUT << "\n";
  193. // If the source instruction was killing the source register before the
  194. // merge, unset the isKill marker given the live range has been extended.
  195. int UIdx = ValLREndInst->findRegisterUseOperandIdx(IntB.reg, true);
  196. if (UIdx != -1) {
  197. ValLREndInst->getOperand(UIdx).setIsKill(false);
  198. IntB.removeKill(ValLR->valno, FillerStart);
  199. }
  200. ++numExtends;
  201. return true;
  202. }
  203. /// HasOtherReachingDefs - Return true if there are definitions of IntB
  204. /// other than BValNo val# that can reach uses of AValno val# of IntA.
  205. bool SimpleRegisterCoalescing::HasOtherReachingDefs(LiveInterval &IntA,
  206. LiveInterval &IntB,
  207. VNInfo *AValNo,
  208. VNInfo *BValNo) {
  209. for (LiveInterval::iterator AI = IntA.begin(), AE = IntA.end();
  210. AI != AE; ++AI) {
  211. if (AI->valno != AValNo) continue;
  212. LiveInterval::Ranges::iterator BI =
  213. std::upper_bound(IntB.ranges.begin(), IntB.ranges.end(), AI->start);
  214. if (BI != IntB.ranges.begin())
  215. --BI;
  216. for (; BI != IntB.ranges.end() && AI->end >= BI->start; ++BI) {
  217. if (BI->valno == BValNo)
  218. continue;
  219. if (BI->start <= AI->start && BI->end > AI->start)
  220. return true;
  221. if (BI->start > AI->start && BI->start < AI->end)
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. /// RemoveCopyByCommutingDef - We found a non-trivially-coalescable copy with IntA
  228. /// being the source and IntB being the dest, thus this defines a value number
  229. /// in IntB. If the source value number (in IntA) is defined by a commutable
  230. /// instruction and its other operand is coalesced to the copy dest register,
  231. /// see if we can transform the copy into a noop by commuting the definition. For
  232. /// example,
  233. ///
  234. /// A3 = op A2 B0<kill>
  235. /// ...
  236. /// B1 = A3 <- this copy
  237. /// ...
  238. /// = op A3 <- more uses
  239. ///
  240. /// ==>
  241. ///
  242. /// B2 = op B0 A2<kill>
  243. /// ...
  244. /// B1 = B2 <- now an identify copy
  245. /// ...
  246. /// = op B2 <- more uses
  247. ///
  248. /// This returns true if an interval was modified.
  249. ///
  250. bool SimpleRegisterCoalescing::RemoveCopyByCommutingDef(LiveInterval &IntA,
  251. LiveInterval &IntB,
  252. MachineInstr *CopyMI) {
  253. unsigned CopyIdx = li_->getDefIndex(li_->getInstructionIndex(CopyMI));
  254. // FIXME: For now, only eliminate the copy by commuting its def when the
  255. // source register is a virtual register. We want to guard against cases
  256. // where the copy is a back edge copy and commuting the def lengthen the
  257. // live interval of the source register to the entire loop.
  258. if (TargetRegisterInfo::isPhysicalRegister(IntA.reg))
  259. return false;
  260. // BValNo is a value number in B that is defined by a copy from A. 'B3' in
  261. // the example above.
  262. LiveInterval::iterator BLR = IntB.FindLiveRangeContaining(CopyIdx);
  263. assert(BLR != IntB.end() && "Live range not found!");
  264. VNInfo *BValNo = BLR->valno;
  265. // Get the location that B is defined at. Two options: either this value has
  266. // an unknown definition point or it is defined at CopyIdx. If unknown, we
  267. // can't process it.
  268. if (!BValNo->copy) return false;
  269. assert(BValNo->def == CopyIdx && "Copy doesn't define the value?");
  270. // AValNo is the value number in A that defines the copy, A3 in the example.
  271. LiveInterval::iterator ALR = IntA.FindLiveRangeContaining(CopyIdx-1);
  272. assert(ALR != IntA.end() && "Live range not found!");
  273. VNInfo *AValNo = ALR->valno;
  274. // If other defs can reach uses of this def, then it's not safe to perform
  275. // the optimization. FIXME: Do isPHIDef and isDefAccurate both need to be
  276. // tested?
  277. if (AValNo->isPHIDef() || !AValNo->isDefAccurate() ||
  278. AValNo->isUnused() || AValNo->hasPHIKill())
  279. return false;
  280. MachineInstr *DefMI = li_->getInstructionFromIndex(AValNo->def);
  281. const TargetInstrDesc &TID = DefMI->getDesc();
  282. if (!TID.isCommutable())
  283. return false;
  284. // If DefMI is a two-address instruction then commuting it will change the
  285. // destination register.
  286. int DefIdx = DefMI->findRegisterDefOperandIdx(IntA.reg);
  287. assert(DefIdx != -1);
  288. unsigned UseOpIdx;
  289. if (!DefMI->isRegTiedToUseOperand(DefIdx, &UseOpIdx))
  290. return false;
  291. unsigned Op1, Op2, NewDstIdx;
  292. if (!tii_->findCommutedOpIndices(DefMI, Op1, Op2))
  293. return false;
  294. if (Op1 == UseOpIdx)
  295. NewDstIdx = Op2;
  296. else if (Op2 == UseOpIdx)
  297. NewDstIdx = Op1;
  298. else
  299. return false;
  300. MachineOperand &NewDstMO = DefMI->getOperand(NewDstIdx);
  301. unsigned NewReg = NewDstMO.getReg();
  302. if (NewReg != IntB.reg || !NewDstMO.isKill())
  303. return false;
  304. // Make sure there are no other definitions of IntB that would reach the
  305. // uses which the new definition can reach.
  306. if (HasOtherReachingDefs(IntA, IntB, AValNo, BValNo))
  307. return false;
  308. // If some of the uses of IntA.reg is already coalesced away, return false.
  309. // It's not possible to determine whether it's safe to perform the coalescing.
  310. for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(IntA.reg),
  311. UE = mri_->use_end(); UI != UE; ++UI) {
  312. MachineInstr *UseMI = &*UI;
  313. unsigned UseIdx = li_->getInstructionIndex(UseMI);
  314. LiveInterval::iterator ULR = IntA.FindLiveRangeContaining(UseIdx);
  315. if (ULR == IntA.end())
  316. continue;
  317. if (ULR->valno == AValNo && JoinedCopies.count(UseMI))
  318. return false;
  319. }
  320. // At this point we have decided that it is legal to do this
  321. // transformation. Start by commuting the instruction.
  322. MachineBasicBlock *MBB = DefMI->getParent();
  323. MachineInstr *NewMI = tii_->commuteInstruction(DefMI);
  324. if (!NewMI)
  325. return false;
  326. if (NewMI != DefMI) {
  327. li_->ReplaceMachineInstrInMaps(DefMI, NewMI);
  328. MBB->insert(DefMI, NewMI);
  329. MBB->erase(DefMI);
  330. }
  331. unsigned OpIdx = NewMI->findRegisterUseOperandIdx(IntA.reg, false);
  332. NewMI->getOperand(OpIdx).setIsKill();
  333. bool BHasPHIKill = BValNo->hasPHIKill();
  334. SmallVector<VNInfo*, 4> BDeadValNos;
  335. VNInfo::KillSet BKills;
  336. std::map<unsigned, unsigned> BExtend;
  337. // If ALR and BLR overlaps and end of BLR extends beyond end of ALR, e.g.
  338. // A = or A, B
  339. // ...
  340. // B = A
  341. // ...
  342. // C = A<kill>
  343. // ...
  344. // = B
  345. //
  346. // then do not add kills of A to the newly created B interval.
  347. bool Extended = BLR->end > ALR->end && ALR->end != ALR->start;
  348. if (Extended)
  349. BExtend[ALR->end] = BLR->end;
  350. // Update uses of IntA of the specific Val# with IntB.
  351. bool BHasSubRegs = false;
  352. if (TargetRegisterInfo::isPhysicalRegister(IntB.reg))
  353. BHasSubRegs = *tri_->getSubRegisters(IntB.reg);
  354. for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(IntA.reg),
  355. UE = mri_->use_end(); UI != UE;) {
  356. MachineOperand &UseMO = UI.getOperand();
  357. MachineInstr *UseMI = &*UI;
  358. ++UI;
  359. if (JoinedCopies.count(UseMI))
  360. continue;
  361. unsigned UseIdx = li_->getInstructionIndex(UseMI);
  362. LiveInterval::iterator ULR = IntA.FindLiveRangeContaining(UseIdx);
  363. if (ULR == IntA.end() || ULR->valno != AValNo)
  364. continue;
  365. UseMO.setReg(NewReg);
  366. if (UseMI == CopyMI)
  367. continue;
  368. if (UseMO.isKill()) {
  369. if (Extended)
  370. UseMO.setIsKill(false);
  371. else
  372. BKills.push_back(VNInfo::KillInfo(false, li_->getUseIndex(UseIdx)+1));
  373. }
  374. unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
  375. if (!tii_->isMoveInstr(*UseMI, SrcReg, DstReg, SrcSubIdx, DstSubIdx))
  376. continue;
  377. if (DstReg == IntB.reg) {
  378. // This copy will become a noop. If it's defining a new val#,
  379. // remove that val# as well. However this live range is being
  380. // extended to the end of the existing live range defined by the copy.
  381. unsigned DefIdx = li_->getDefIndex(UseIdx);
  382. const LiveRange *DLR = IntB.getLiveRangeContaining(DefIdx);
  383. BHasPHIKill |= DLR->valno->hasPHIKill();
  384. assert(DLR->valno->def == DefIdx);
  385. BDeadValNos.push_back(DLR->valno);
  386. BExtend[DLR->start] = DLR->end;
  387. JoinedCopies.insert(UseMI);
  388. // If this is a kill but it's going to be removed, the last use
  389. // of the same val# is the new kill.
  390. if (UseMO.isKill())
  391. BKills.pop_back();
  392. }
  393. }
  394. // We need to insert a new liverange: [ALR.start, LastUse). It may be we can
  395. // simply extend BLR if CopyMI doesn't end the range.
  396. DOUT << "\nExtending: "; IntB.print(DOUT, tri_);
  397. // Remove val#'s defined by copies that will be coalesced away.
  398. for (unsigned i = 0, e = BDeadValNos.size(); i != e; ++i) {
  399. VNInfo *DeadVNI = BDeadValNos[i];
  400. if (BHasSubRegs) {
  401. for (const unsigned *SR = tri_->getSubRegisters(IntB.reg); *SR; ++SR) {
  402. LiveInterval &SRLI = li_->getInterval(*SR);
  403. const LiveRange *SRLR = SRLI.getLiveRangeContaining(DeadVNI->def);
  404. SRLI.removeValNo(SRLR->valno);
  405. }
  406. }
  407. IntB.removeValNo(BDeadValNos[i]);
  408. }
  409. // Extend BValNo by merging in IntA live ranges of AValNo. Val# definition
  410. // is updated. Kills are also updated.
  411. VNInfo *ValNo = BValNo;
  412. ValNo->def = AValNo->def;
  413. ValNo->copy = NULL;
  414. for (unsigned j = 0, ee = ValNo->kills.size(); j != ee; ++j) {
  415. unsigned Kill = ValNo->kills[j].killIdx;
  416. if (Kill != BLR->end)
  417. BKills.push_back(VNInfo::KillInfo(ValNo->kills[j].isPHIKill, Kill));
  418. }
  419. ValNo->kills.clear();
  420. for (LiveInterval::iterator AI = IntA.begin(), AE = IntA.end();
  421. AI != AE; ++AI) {
  422. if (AI->valno != AValNo) continue;
  423. unsigned End = AI->end;
  424. std::map<unsigned, unsigned>::iterator EI = BExtend.find(End);
  425. if (EI != BExtend.end())
  426. End = EI->second;
  427. IntB.addRange(LiveRange(AI->start, End, ValNo));
  428. // If the IntB live range is assigned to a physical register, and if that
  429. // physreg has sub-registers, update their live intervals as well.
  430. if (BHasSubRegs) {
  431. for (const unsigned *SR = tri_->getSubRegisters(IntB.reg); *SR; ++SR) {
  432. LiveInterval &SRLI = li_->getInterval(*SR);
  433. SRLI.MergeInClobberRange(AI->start, End, li_->getVNInfoAllocator());
  434. }
  435. }
  436. }
  437. IntB.addKills(ValNo, BKills);
  438. ValNo->setHasPHIKill(BHasPHIKill);
  439. DOUT << " result = "; IntB.print(DOUT, tri_);
  440. DOUT << "\n";
  441. DOUT << "\nShortening: "; IntA.print(DOUT, tri_);
  442. IntA.removeValNo(AValNo);
  443. DOUT << " result = "; IntA.print(DOUT, tri_);
  444. DOUT << "\n";
  445. ++numCommutes;
  446. return true;
  447. }
  448. /// isSameOrFallThroughBB - Return true if MBB == SuccMBB or MBB simply
  449. /// fallthoughs to SuccMBB.
  450. static bool isSameOrFallThroughBB(MachineBasicBlock *MBB,
  451. MachineBasicBlock *SuccMBB,
  452. const TargetInstrInfo *tii_) {
  453. if (MBB == SuccMBB)
  454. return true;
  455. MachineBasicBlock *TBB = 0, *FBB = 0;
  456. SmallVector<MachineOperand, 4> Cond;
  457. return !tii_->AnalyzeBranch(*MBB, TBB, FBB, Cond) && !TBB && !FBB &&
  458. MBB->isSuccessor(SuccMBB);
  459. }
  460. /// removeRange - Wrapper for LiveInterval::removeRange. This removes a range
  461. /// from a physical register live interval as well as from the live intervals
  462. /// of its sub-registers.
  463. static void removeRange(LiveInterval &li, unsigned Start, unsigned End,
  464. LiveIntervals *li_, const TargetRegisterInfo *tri_) {
  465. li.removeRange(Start, End, true);
  466. if (TargetRegisterInfo::isPhysicalRegister(li.reg)) {
  467. for (const unsigned* SR = tri_->getSubRegisters(li.reg); *SR; ++SR) {
  468. if (!li_->hasInterval(*SR))
  469. continue;
  470. LiveInterval &sli = li_->getInterval(*SR);
  471. unsigned RemoveEnd = Start;
  472. while (RemoveEnd != End) {
  473. LiveInterval::iterator LR = sli.FindLiveRangeContaining(Start);
  474. if (LR == sli.end())
  475. break;
  476. RemoveEnd = (LR->end < End) ? LR->end : End;
  477. sli.removeRange(Start, RemoveEnd, true);
  478. Start = RemoveEnd;
  479. }
  480. }
  481. }
  482. }
  483. /// TrimLiveIntervalToLastUse - If there is a last use in the same basic block
  484. /// as the copy instruction, trim the live interval to the last use and return
  485. /// true.
  486. bool
  487. SimpleRegisterCoalescing::TrimLiveIntervalToLastUse(unsigned CopyIdx,
  488. MachineBasicBlock *CopyMBB,
  489. LiveInterval &li,
  490. const LiveRange *LR) {
  491. unsigned MBBStart = li_->getMBBStartIdx(CopyMBB);
  492. unsigned LastUseIdx;
  493. MachineOperand *LastUse = lastRegisterUse(LR->start, CopyIdx-1, li.reg,
  494. LastUseIdx);
  495. if (LastUse) {
  496. MachineInstr *LastUseMI = LastUse->getParent();
  497. if (!isSameOrFallThroughBB(LastUseMI->getParent(), CopyMBB, tii_)) {
  498. // r1024 = op
  499. // ...
  500. // BB1:
  501. // = r1024
  502. //
  503. // BB2:
  504. // r1025<dead> = r1024<kill>
  505. if (MBBStart < LR->end)
  506. removeRange(li, MBBStart, LR->end, li_, tri_);
  507. return true;
  508. }
  509. // There are uses before the copy, just shorten the live range to the end
  510. // of last use.
  511. LastUse->setIsKill();
  512. removeRange(li, li_->getDefIndex(LastUseIdx), LR->end, li_, tri_);
  513. li.addKill(LR->valno, LastUseIdx+1, false);
  514. unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
  515. if (tii_->isMoveInstr(*LastUseMI, SrcReg, DstReg, SrcSubIdx, DstSubIdx) &&
  516. DstReg == li.reg) {
  517. // Last use is itself an identity code.
  518. int DeadIdx = LastUseMI->findRegisterDefOperandIdx(li.reg, false, tri_);
  519. LastUseMI->getOperand(DeadIdx).setIsDead();
  520. }
  521. return true;
  522. }
  523. // Is it livein?
  524. if (LR->start <= MBBStart && LR->end > MBBStart) {
  525. if (LR->start == 0) {
  526. assert(TargetRegisterInfo::isPhysicalRegister(li.reg));
  527. // Live-in to the function but dead. Remove it from entry live-in set.
  528. mf_->begin()->removeLiveIn(li.reg);
  529. }
  530. // FIXME: Shorten intervals in BBs that reaches this BB.
  531. }
  532. return false;
  533. }
  534. /// ReMaterializeTrivialDef - If the source of a copy is defined by a trivial
  535. /// computation, replace the copy by rematerialize the definition.
  536. bool SimpleRegisterCoalescing::ReMaterializeTrivialDef(LiveInterval &SrcInt,
  537. unsigned DstReg,
  538. MachineInstr *CopyMI) {
  539. unsigned CopyIdx = li_->getUseIndex(li_->getInstructionIndex(CopyMI));
  540. LiveInterval::iterator SrcLR = SrcInt.FindLiveRangeContaining(CopyIdx);
  541. assert(SrcLR != SrcInt.end() && "Live range not found!");
  542. VNInfo *ValNo = SrcLR->valno;
  543. // If other defs can reach uses of this def, then it's not safe to perform
  544. // the optimization. FIXME: Do isPHIDef and isDefAccurate both need to be
  545. // tested?
  546. if (ValNo->isPHIDef() || !ValNo->isDefAccurate() ||
  547. ValNo->isUnused() || ValNo->hasPHIKill())
  548. return false;
  549. MachineInstr *DefMI = li_->getInstructionFromIndex(ValNo->def);
  550. const TargetInstrDesc &TID = DefMI->getDesc();
  551. if (!TID.isAsCheapAsAMove())
  552. return false;
  553. if (!DefMI->getDesc().isRematerializable() ||
  554. !tii_->isTriviallyReMaterializable(DefMI))
  555. return false;
  556. bool SawStore = false;
  557. if (!DefMI->isSafeToMove(tii_, SawStore))
  558. return false;
  559. unsigned DefIdx = li_->getDefIndex(CopyIdx);
  560. const LiveRange *DLR= li_->getInterval(DstReg).getLiveRangeContaining(DefIdx);
  561. DLR->valno->copy = NULL;
  562. // Don't forget to update sub-register intervals.
  563. if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
  564. for (const unsigned* SR = tri_->getSubRegisters(DstReg); *SR; ++SR) {
  565. if (!li_->hasInterval(*SR))
  566. continue;
  567. DLR = li_->getInterval(*SR).getLiveRangeContaining(DefIdx);
  568. if (DLR && DLR->valno->copy == CopyMI)
  569. DLR->valno->copy = NULL;
  570. }
  571. }
  572. // If copy kills the source register, find the last use and propagate
  573. // kill.
  574. bool checkForDeadDef = false;
  575. MachineBasicBlock *MBB = CopyMI->getParent();
  576. if (CopyMI->killsRegister(SrcInt.reg))
  577. if (!TrimLiveIntervalToLastUse(CopyIdx, MBB, SrcInt, SrcLR)) {
  578. checkForDeadDef = true;
  579. }
  580. MachineBasicBlock::iterator MII = next(MachineBasicBlock::iterator(CopyMI));
  581. tii_->reMaterialize(*MBB, MII, DstReg, DefMI);
  582. MachineInstr *NewMI = prior(MII);
  583. if (checkForDeadDef) {
  584. // PR4090 fix: Trim interval failed because there was no use of the
  585. // source interval in this MBB. If the def is in this MBB too then we
  586. // should mark it dead:
  587. if (DefMI->getParent() == MBB) {
  588. DefMI->addRegisterDead(SrcInt.reg, tri_);
  589. SrcLR->end = SrcLR->start + 1;
  590. }
  591. }
  592. // CopyMI may have implicit operands, transfer them over to the newly
  593. // rematerialized instruction. And update implicit def interval valnos.
  594. for (unsigned i = CopyMI->getDesc().getNumOperands(),
  595. e = CopyMI->getNumOperands(); i != e; ++i) {
  596. MachineOperand &MO = CopyMI->getOperand(i);
  597. if (MO.isReg() && MO.isImplicit())
  598. NewMI->addOperand(MO);
  599. if (MO.isDef() && li_->hasInterval(MO.getReg())) {
  600. unsigned Reg = MO.getReg();
  601. DLR = li_->getInterval(Reg).getLiveRangeContaining(DefIdx);
  602. if (DLR && DLR->valno->copy == CopyMI)
  603. DLR->valno->copy = NULL;
  604. }
  605. }
  606. li_->ReplaceMachineInstrInMaps(CopyMI, NewMI);
  607. CopyMI->eraseFromParent();
  608. ReMatCopies.insert(CopyMI);
  609. ReMatDefs.insert(DefMI);
  610. ++NumReMats;
  611. return true;
  612. }
  613. /// isBackEdgeCopy - Returns true if CopyMI is a back edge copy.
  614. ///
  615. bool SimpleRegisterCoalescing::isBackEdgeCopy(MachineInstr *CopyMI,
  616. unsigned DstReg) const {
  617. MachineBasicBlock *MBB = CopyMI->getParent();
  618. const MachineLoop *L = loopInfo->getLoopFor(MBB);
  619. if (!L)
  620. return false;
  621. if (MBB != L->getLoopLatch())
  622. return false;
  623. LiveInterval &LI = li_->getInterval(DstReg);
  624. unsigned DefIdx = li_->getInstructionIndex(CopyMI);
  625. LiveInterval::const_iterator DstLR =
  626. LI.FindLiveRangeContaining(li_->getDefIndex(DefIdx));
  627. if (DstLR == LI.end())
  628. return false;
  629. if (DstLR->valno->kills.size() == 1 && DstLR->valno->kills[0].isPHIKill)
  630. return true;
  631. return false;
  632. }
  633. /// UpdateRegDefsUses - Replace all defs and uses of SrcReg to DstReg and
  634. /// update the subregister number if it is not zero. If DstReg is a
  635. /// physical register and the existing subregister number of the def / use
  636. /// being updated is not zero, make sure to set it to the correct physical
  637. /// subregister.
  638. void
  639. SimpleRegisterCoalescing::UpdateRegDefsUses(unsigned SrcReg, unsigned DstReg,
  640. unsigned SubIdx) {
  641. bool DstIsPhys = TargetRegisterInfo::isPhysicalRegister(DstReg);
  642. if (DstIsPhys && SubIdx) {
  643. // Figure out the real physical register we are updating with.
  644. DstReg = tri_->getSubReg(DstReg, SubIdx);
  645. SubIdx = 0;
  646. }
  647. for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(SrcReg),
  648. E = mri_->reg_end(); I != E; ) {
  649. MachineOperand &O = I.getOperand();
  650. MachineInstr *UseMI = &*I;
  651. ++I;
  652. unsigned OldSubIdx = O.getSubReg();
  653. if (DstIsPhys) {
  654. unsigned UseDstReg = DstReg;
  655. if (OldSubIdx)
  656. UseDstReg = tri_->getSubReg(DstReg, OldSubIdx);
  657. unsigned CopySrcReg, CopyDstReg, CopySrcSubIdx, CopyDstSubIdx;
  658. if (tii_->isMoveInstr(*UseMI, CopySrcReg, CopyDstReg,
  659. CopySrcSubIdx, CopyDstSubIdx) &&
  660. CopySrcReg != CopyDstReg &&
  661. CopySrcReg == SrcReg && CopyDstReg != UseDstReg) {
  662. // If the use is a copy and it won't be coalesced away, and its source
  663. // is defined by a trivial computation, try to rematerialize it instead.
  664. if (ReMaterializeTrivialDef(li_->getInterval(SrcReg), CopyDstReg,UseMI))
  665. continue;
  666. }
  667. O.setReg(UseDstReg);
  668. O.setSubReg(0);
  669. continue;
  670. }
  671. // Sub-register indexes goes from small to large. e.g.
  672. // RAX: 1 -> AL, 2 -> AX, 3 -> EAX
  673. // EAX: 1 -> AL, 2 -> AX
  674. // So RAX's sub-register 2 is AX, RAX's sub-regsiter 3 is EAX, whose
  675. // sub-register 2 is also AX.
  676. if (SubIdx && OldSubIdx && SubIdx != OldSubIdx)
  677. assert(OldSubIdx < SubIdx && "Conflicting sub-register index!");
  678. else if (SubIdx)
  679. O.setSubReg(SubIdx);
  680. // Remove would-be duplicated kill marker.
  681. if (O.isKill() && UseMI->killsRegister(DstReg))
  682. O.setIsKill(false);
  683. O.setReg(DstReg);
  684. // After updating the operand, check if the machine instruction has
  685. // become a copy. If so, update its val# information.
  686. if (JoinedCopies.count(UseMI))
  687. continue;
  688. const TargetInstrDesc &TID = UseMI->getDesc();
  689. unsigned CopySrcReg, CopyDstReg, CopySrcSubIdx, CopyDstSubIdx;
  690. if (TID.getNumDefs() == 1 && TID.getNumOperands() > 2 &&
  691. tii_->isMoveInstr(*UseMI, CopySrcReg, CopyDstReg,
  692. CopySrcSubIdx, CopyDstSubIdx) &&
  693. CopySrcReg != CopyDstReg &&
  694. (TargetRegisterInfo::isVirtualRegister(CopyDstReg) ||
  695. allocatableRegs_[CopyDstReg])) {
  696. LiveInterval &LI = li_->getInterval(CopyDstReg);
  697. unsigned DefIdx = li_->getDefIndex(li_->getInstructionIndex(UseMI));
  698. if (const LiveRange *DLR = LI.getLiveRangeContaining(DefIdx)) {
  699. if (DLR->valno->def == DefIdx)
  700. DLR->valno->copy = UseMI;
  701. }
  702. }
  703. }
  704. }
  705. /// RemoveDeadImpDef - Remove implicit_def instructions which are "re-defining"
  706. /// registers due to insert_subreg coalescing. e.g.
  707. /// r1024 = op
  708. /// r1025 = implicit_def
  709. /// r1025 = insert_subreg r1025, r1024
  710. /// = op r1025
  711. /// =>
  712. /// r1025 = op
  713. /// r1025 = implicit_def
  714. /// r1025 = insert_subreg r1025, r1025
  715. /// = op r1025
  716. void
  717. SimpleRegisterCoalescing::RemoveDeadImpDef(unsigned Reg, LiveInterval &LI) {
  718. for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(Reg),
  719. E = mri_->reg_end(); I != E; ) {
  720. MachineOperand &O = I.getOperand();
  721. MachineInstr *DefMI = &*I;
  722. ++I;
  723. if (!O.isDef())
  724. continue;
  725. if (DefMI->getOpcode() != TargetInstrInfo::IMPLICIT_DEF)
  726. continue;
  727. if (!LI.liveBeforeAndAt(li_->getInstructionIndex(DefMI)))
  728. continue;
  729. li_->RemoveMachineInstrFromMaps(DefMI);
  730. DefMI->eraseFromParent();
  731. }
  732. }
  733. /// RemoveUnnecessaryKills - Remove kill markers that are no longer accurate
  734. /// due to live range lengthening as the result of coalescing.
  735. void SimpleRegisterCoalescing::RemoveUnnecessaryKills(unsigned Reg,
  736. LiveInterval &LI) {
  737. for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(Reg),
  738. UE = mri_->use_end(); UI != UE; ++UI) {
  739. MachineOperand &UseMO = UI.getOperand();
  740. if (UseMO.isKill()) {
  741. MachineInstr *UseMI = UseMO.getParent();
  742. unsigned UseIdx = li_->getUseIndex(li_->getInstructionIndex(UseMI));
  743. const LiveRange *UI = LI.getLiveRangeContaining(UseIdx);
  744. if (!UI || !LI.isKill(UI->valno, UseIdx+1))
  745. UseMO.setIsKill(false);
  746. }
  747. }
  748. }
  749. /// removeIntervalIfEmpty - Check if the live interval of a physical register
  750. /// is empty, if so remove it and also remove the empty intervals of its
  751. /// sub-registers. Return true if live interval is removed.
  752. static bool removeIntervalIfEmpty(LiveInterval &li, LiveIntervals *li_,
  753. const TargetRegisterInfo *tri_) {
  754. if (li.empty()) {
  755. if (TargetRegisterInfo::isPhysicalRegister(li.reg))
  756. for (const unsigned* SR = tri_->getSubRegisters(li.reg); *SR; ++SR) {
  757. if (!li_->hasInterval(*SR))
  758. continue;
  759. LiveInterval &sli = li_->getInterval(*SR);
  760. if (sli.empty())
  761. li_->removeInterval(*SR);
  762. }
  763. li_->removeInterval(li.reg);
  764. return true;
  765. }
  766. return false;
  767. }
  768. /// ShortenDeadCopyLiveRange - Shorten a live range defined by a dead copy.
  769. /// Return true if live interval is removed.
  770. bool SimpleRegisterCoalescing::ShortenDeadCopyLiveRange(LiveInterval &li,
  771. MachineInstr *CopyMI) {
  772. unsigned CopyIdx = li_->getInstructionIndex(CopyMI);
  773. LiveInterval::iterator MLR =
  774. li.FindLiveRangeContaining(li_->getDefIndex(CopyIdx));
  775. if (MLR == li.end())
  776. return false; // Already removed by ShortenDeadCopySrcLiveRange.
  777. unsigned RemoveStart = MLR->start;
  778. unsigned RemoveEnd = MLR->end;
  779. // Remove the liverange that's defined by this.
  780. if (RemoveEnd == li_->getDefIndex(CopyIdx)+1) {
  781. removeRange(li, RemoveStart, RemoveEnd, li_, tri_);
  782. return removeIntervalIfEmpty(li, li_, tri_);
  783. }
  784. return false;
  785. }
  786. /// RemoveDeadDef - If a def of a live interval is now determined dead, remove
  787. /// the val# it defines. If the live interval becomes empty, remove it as well.
  788. bool SimpleRegisterCoalescing::RemoveDeadDef(LiveInterval &li,
  789. MachineInstr *DefMI) {
  790. unsigned DefIdx = li_->getDefIndex(li_->getInstructionIndex(DefMI));
  791. LiveInterval::iterator MLR = li.FindLiveRangeContaining(DefIdx);
  792. if (DefIdx != MLR->valno->def)
  793. return false;
  794. li.removeValNo(MLR->valno);
  795. return removeIntervalIfEmpty(li, li_, tri_);
  796. }
  797. /// PropagateDeadness - Propagate the dead marker to the instruction which
  798. /// defines the val#.
  799. static void PropagateDeadness(LiveInterval &li, MachineInstr *CopyMI,
  800. unsigned &LRStart, LiveIntervals *li_,
  801. const TargetRegisterInfo* tri_) {
  802. MachineInstr *DefMI =
  803. li_->getInstructionFromIndex(li_->getDefIndex(LRStart));
  804. if (DefMI && DefMI != CopyMI) {
  805. int DeadIdx = DefMI->findRegisterDefOperandIdx(li.reg, false, tri_);
  806. if (DeadIdx != -1) {
  807. DefMI->getOperand(DeadIdx).setIsDead();
  808. // A dead def should have a single cycle interval.
  809. ++LRStart;
  810. }
  811. }
  812. }
  813. /// ShortenDeadCopySrcLiveRange - Shorten a live range as it's artificially
  814. /// extended by a dead copy. Mark the last use (if any) of the val# as kill as
  815. /// ends the live range there. If there isn't another use, then this live range
  816. /// is dead. Return true if live interval is removed.
  817. bool
  818. SimpleRegisterCoalescing::ShortenDeadCopySrcLiveRange(LiveInterval &li,
  819. MachineInstr *CopyMI) {
  820. unsigned CopyIdx = li_->getInstructionIndex(CopyMI);
  821. if (CopyIdx == 0) {
  822. // FIXME: special case: function live in. It can be a general case if the
  823. // first instruction index starts at > 0 value.
  824. assert(TargetRegisterInfo::isPhysicalRegister(li.reg));
  825. // Live-in to the function but dead. Remove it from entry live-in set.
  826. if (mf_->begin()->isLiveIn(li.reg))
  827. mf_->begin()->removeLiveIn(li.reg);
  828. const LiveRange *LR = li.getLiveRangeContaining(CopyIdx);
  829. removeRange(li, LR->start, LR->end, li_, tri_);
  830. return removeIntervalIfEmpty(li, li_, tri_);
  831. }
  832. LiveInterval::iterator LR = li.FindLiveRangeContaining(CopyIdx-1);
  833. if (LR == li.end())
  834. // Livein but defined by a phi.
  835. return false;
  836. unsigned RemoveStart = LR->start;
  837. unsigned RemoveEnd = li_->getDefIndex(CopyIdx)+1;
  838. if (LR->end > RemoveEnd)
  839. // More uses past this copy? Nothing to do.
  840. return false;
  841. // If there is a last use in the same bb, we can't remove the live range.
  842. // Shorten the live interval and return.
  843. MachineBasicBlock *CopyMBB = CopyMI->getParent();
  844. if (TrimLiveIntervalToLastUse(CopyIdx, CopyMBB, li, LR))
  845. return false;
  846. MachineBasicBlock *StartMBB = li_->getMBBFromIndex(RemoveStart);
  847. if (!isSameOrFallThroughBB(StartMBB, CopyMBB, tii_))
  848. // If the live range starts in another mbb and the copy mbb is not a fall
  849. // through mbb, then we can only cut the range from the beginning of the
  850. // copy mbb.
  851. RemoveStart = li_->getMBBStartIdx(CopyMBB) + 1;
  852. if (LR->valno->def == RemoveStart) {
  853. // If the def MI defines the val# and this copy is the only kill of the
  854. // val#, then propagate the dead marker.
  855. if (li.isOnlyLROfValNo(LR)) {
  856. PropagateDeadness(li, CopyMI, RemoveStart, li_, tri_);
  857. ++numDeadValNo;
  858. }
  859. if (li.isKill(LR->valno, RemoveEnd))
  860. li.removeKill(LR->valno, RemoveEnd);
  861. }
  862. removeRange(li, RemoveStart, RemoveEnd, li_, tri_);
  863. return removeIntervalIfEmpty(li, li_, tri_);
  864. }
  865. /// CanCoalesceWithImpDef - Returns true if the specified copy instruction
  866. /// from an implicit def to another register can be coalesced away.
  867. bool SimpleRegisterCoalescing::CanCoalesceWithImpDef(MachineInstr *CopyMI,
  868. LiveInterval &li,
  869. LiveInterval &ImpLi) const{
  870. if (!CopyMI->killsRegister(ImpLi.reg))
  871. return false;
  872. unsigned CopyIdx = li_->getDefIndex(li_->getInstructionIndex(CopyMI));
  873. LiveInterval::iterator LR = li.FindLiveRangeContaining(CopyIdx);
  874. if (LR == li.end())
  875. return false;
  876. if (LR->valno->hasPHIKill())
  877. return false;
  878. if (LR->valno->def != CopyIdx)
  879. return false;
  880. // Make sure all of val# uses are copies.
  881. for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(li.reg),
  882. UE = mri_->use_end(); UI != UE;) {
  883. MachineInstr *UseMI = &*UI;
  884. ++UI;
  885. if (JoinedCopies.count(UseMI))
  886. continue;
  887. unsigned UseIdx = li_->getUseIndex(li_->getInstructionIndex(UseMI));
  888. LiveInterval::iterator ULR = li.FindLiveRangeContaining(UseIdx);
  889. if (ULR == li.end() || ULR->valno != LR->valno)
  890. continue;
  891. // If the use is not a use, then it's not safe to coalesce the move.
  892. unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
  893. if (!tii_->isMoveInstr(*UseMI, SrcReg, DstReg, SrcSubIdx, DstSubIdx)) {
  894. if (UseMI->getOpcode() == TargetInstrInfo::INSERT_SUBREG &&
  895. UseMI->getOperand(1).getReg() == li.reg)
  896. continue;
  897. return false;
  898. }
  899. }
  900. return true;
  901. }
  902. /// TurnCopiesFromValNoToImpDefs - The specified value# is defined by an
  903. /// implicit_def and it is being removed. Turn all copies from this value#
  904. /// into implicit_defs.
  905. void SimpleRegisterCoalescing::TurnCopiesFromValNoToImpDefs(LiveInterval &li,
  906. VNInfo *VNI) {
  907. SmallVector<MachineInstr*, 4> ImpDefs;
  908. MachineOperand *LastUse = NULL;
  909. unsigned LastUseIdx = li_->getUseIndex(VNI->def);
  910. for (MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg),
  911. RE = mri_->reg_end(); RI != RE;) {
  912. MachineOperand *MO = &RI.getOperand();
  913. MachineInstr *MI = &*RI;
  914. ++RI;
  915. if (MO->isDef()) {
  916. if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
  917. ImpDefs.push_back(MI);
  918. continue;
  919. }
  920. if (JoinedCopies.count(MI))
  921. continue;
  922. unsigned UseIdx = li_->getUseIndex(li_->getInstructionIndex(MI));
  923. LiveInterval::iterator ULR = li.FindLiveRangeContaining(UseIdx);
  924. if (ULR == li.end() || ULR->valno != VNI)
  925. continue;
  926. // If the use is a copy, turn it into an identity copy.
  927. unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
  928. if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubIdx, DstSubIdx) &&
  929. SrcReg == li.reg) {
  930. // Change it to an implicit_def.
  931. MI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
  932. for (int i = MI->getNumOperands() - 1, e = 0; i > e; --i)
  933. MI->RemoveOperand(i);
  934. // It's no longer a copy, update the valno it defines.
  935. unsigned DefIdx = li_->getDefIndex(UseIdx);
  936. LiveInterval &DstInt = li_->getInterval(DstReg);
  937. LiveInterval::iterator DLR = DstInt.FindLiveRangeContaining(DefIdx);
  938. assert(DLR != DstInt.end() && "Live range not found!");
  939. assert(DLR->valno->copy == MI);
  940. DLR->valno->copy = NULL;
  941. ReMatCopies.insert(MI);
  942. } else if (UseIdx > LastUseIdx) {
  943. LastUseIdx = UseIdx;
  944. LastUse = MO;
  945. }
  946. }
  947. if (LastUse) {
  948. LastUse->setIsKill();
  949. li.addKill(VNI, LastUseIdx+1, false);
  950. } else {
  951. // Remove dead implicit_def's.
  952. while (!ImpDefs.empty()) {
  953. MachineInstr *ImpDef = ImpDefs.back();
  954. ImpDefs.pop_back();
  955. li_->RemoveMachineInstrFromMaps(ImpDef);
  956. ImpDef->eraseFromParent();
  957. }
  958. }
  959. }
  960. /// isWinToJoinVRWithSrcPhysReg - Return true if it's worth while to join a
  961. /// a virtual destination register with physical source register.
  962. bool
  963. SimpleRegisterCoalescing::isWinToJoinVRWithSrcPhysReg(MachineInstr *CopyMI,
  964. MachineBasicBlock *CopyMBB,
  965. LiveInterval &DstInt,
  966. LiveInterval &SrcInt) {
  967. // If the virtual register live interval is long but it has low use desity,
  968. // do not join them, instead mark the physical register as its allocation
  969. // preference.
  970. const TargetRegisterClass *RC = mri_->getRegClass(DstInt.reg);
  971. unsigned Threshold = allocatableRCRegs_[RC].count() * 2;
  972. unsigned Length = li_->getApproximateInstructionCount(DstInt);
  973. if (Length > Threshold &&
  974. (((float)std::distance(mri_->use_begin(DstInt.reg),
  975. mri_->use_end()) / Length) < (1.0 / Threshold)))
  976. return false;
  977. // If the virtual register live interval extends into a loop, turn down
  978. // aggressiveness.
  979. unsigned CopyIdx = li_->getDefIndex(li_->getInstructionIndex(CopyMI));
  980. const MachineLoop *L = loopInfo->getLoopFor(CopyMBB);
  981. if (!L) {
  982. // Let's see if the virtual register live interval extends into the loop.
  983. LiveInterval::iterator DLR = DstInt.FindLiveRangeContaining(CopyIdx);
  984. assert(DLR != DstInt.end() && "Live range not found!");
  985. DLR = DstInt.FindLiveRangeContaining(DLR->end+1);
  986. if (DLR != DstInt.end()) {
  987. CopyMBB = li_->getMBBFromIndex(DLR->start);
  988. L = loopInfo->getLoopFor(CopyMBB);
  989. }
  990. }
  991. if (!L || Length <= Threshold)
  992. return true;
  993. unsigned UseIdx = li_->getUseIndex(CopyIdx);
  994. LiveInterval::iterator SLR = SrcInt.FindLiveRangeContaining(UseIdx);
  995. MachineBasicBlock *SMBB = li_->getMBBFromIndex(SLR->start);
  996. if (loopInfo->getLoopFor(SMBB) != L) {
  997. if (!loopInfo->isLoopHeader(CopyMBB))
  998. return false;
  999. // If vr's live interval extends pass the loop header, do not join.
  1000. for (MachineBasicBlock::succ_iterator SI = CopyMBB->succ_begin(),
  1001. SE = CopyMBB->succ_end(); SI != SE; ++SI) {
  1002. MachineBasicBlock *SuccMBB = *SI;
  1003. if (SuccMBB == CopyMBB)
  1004. continue;
  1005. if (DstInt.overlaps(li_->getMBBStartIdx(SuccMBB),
  1006. li_->getMBBEndIdx(SuccMBB)+1))
  1007. return false;
  1008. }
  1009. }
  1010. return true;
  1011. }
  1012. /// isWinToJoinVRWithDstPhysReg - Return true if it's worth while to join a
  1013. /// copy from a virtual source register to a physical destination register.
  1014. bool
  1015. SimpleRegisterCoalescing::isWinToJoinVRWithDstPhysReg(MachineInstr *CopyMI,
  1016. MachineBasicBlock *CopyMBB,
  1017. LiveInterval &DstInt,
  1018. LiveInterval &SrcInt) {
  1019. // If the virtual register live interval is long but it has low use desity,
  1020. // do not join them, instead mark the physical register as its allocation
  1021. // preference.
  1022. const TargetRegisterClass *RC = mri_->getRegClass(SrcInt.reg);
  1023. unsigned Threshold = allocatableRCRegs_[RC].count() * 2;
  1024. unsigned Length = li_->getApproximateInstructionCount(SrcInt);
  1025. if (Length > Threshold &&
  1026. (((float)std::distance(mri_->use_begin(SrcInt.reg),
  1027. mri_->use_end()) / Length) < (1.0 / Threshold)))
  1028. return false;
  1029. if (SrcInt.empty())
  1030. // Must be implicit_def.
  1031. return false;
  1032. // If the virtual register live interval is defined or cross a loop, turn
  1033. // down aggressiveness.
  1034. unsigned CopyIdx = li_->getDefIndex(li_->getInstructionIndex(CopyMI));
  1035. unsigned UseIdx = li_->getUseIndex(CopyIdx);
  1036. LiveInterval::iterator SLR = SrcInt.FindLiveRangeContaining(UseIdx);
  1037. assert(SLR != SrcInt.end() && "Live range not found!");
  1038. SLR = SrcInt.FindLiveRangeContaining(SLR->start-1);
  1039. if (SLR == SrcInt.end())
  1040. return true;
  1041. MachineBasicBlock *SMBB = li_->getMBBFromIndex(SLR->start);
  1042. const MachineLoop *L = loopInfo->getLoopFor(SMBB);
  1043. if (!L || Length <= Threshold)
  1044. return true;
  1045. if (loopInfo->getLoopFor(CopyMBB) != L) {
  1046. if (SMBB != L->getLoopLatch())
  1047. return false;
  1048. // If vr's live interval is extended from before the loop latch, do not
  1049. // join.
  1050. for (MachineBasicBlock::pred_iterator PI = SMBB->pred_begin(),
  1051. PE = SMBB->pred_end(); PI != PE; ++PI) {
  1052. MachineBasicBlock *PredMBB = *PI;
  1053. if (PredMBB == SMBB)
  1054. continue;
  1055. if (SrcInt.overlaps(li_->getMBBStartIdx(PredMBB),
  1056. li_->getMBBEndIdx(PredMBB)+1))
  1057. return false;
  1058. }
  1059. }
  1060. return true;
  1061. }
  1062. /// isWinToJoinCrossClass - Return true if it's profitable to coalesce
  1063. /// two virtual registers from different register classes.
  1064. bool
  1065. SimpleRegisterCoalescing::isWinToJoinCrossClass(unsigned LargeReg,
  1066. unsigned SmallReg,
  1067. unsigned Threshold) {
  1068. // Then make sure the intervals are *short*.
  1069. LiveInterval &LargeInt = li_->getInterval(LargeReg);
  1070. LiveInterval &SmallInt = li_->getInterval(SmallReg);
  1071. unsigned LargeSize = li_->getApproximateInstructionCount(LargeInt);
  1072. unsigned SmallSize = li_->getApproximateInstructionCount(SmallInt);
  1073. if (SmallSize > Threshold || LargeSize > Threshold)
  1074. if ((float)std::distance(mri_->use_begin(SmallReg),
  1075. mri_->use_end()) / SmallSize <
  1076. (float)std::distance(mri_->use_begin(LargeReg),
  1077. mri_->use_end()) / LargeSize)
  1078. return false;
  1079. return true;
  1080. }
  1081. /// HasIncompatibleSubRegDefUse - If we are trying to coalesce a virtual
  1082. /// register with a physical register, check if any of the virtual register
  1083. /// operand is a sub-register use or def. If so, make sure it won't result
  1084. /// in an illegal extract_subreg or insert_subreg instruction. e.g.
  1085. /// vr1024 = extract_subreg vr1025, 1
  1086. /// ...
  1087. /// vr1024 = mov8rr AH
  1088. /// If vr1024 is coalesced with AH, the extract_subreg is now illegal since
  1089. /// AH does not have a super-reg whose sub-register 1 is AH.
  1090. bool
  1091. SimpleRegisterCoalescing::HasIncompatibleSubRegDefUse(MachineInstr *CopyMI,
  1092. unsigned VirtReg,
  1093. unsigned PhysReg) {
  1094. for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(VirtReg),
  1095. E = mri_->reg_end(); I != E; ++I) {
  1096. MachineOperand &O = I.getOperand();
  1097. MachineInstr *MI = &*I;
  1098. if (MI == CopyMI || JoinedCopies.count(MI))
  1099. continue;
  1100. unsigned SubIdx = O.getSubReg();
  1101. if (SubIdx && !tri_->getSubReg(PhysReg, SubIdx))
  1102. return true;
  1103. if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
  1104. SubIdx = MI->getOperand(2).getImm();
  1105. if (O.isUse() && !tri_->getSubReg(PhysReg, SubIdx))
  1106. return true;
  1107. if (O.isDef()) {
  1108. unsigned SrcReg = MI->getOperand(1).getReg();
  1109. const TargetRegisterClass *RC =
  1110. TargetRegisterInfo::isPhysicalRegister(SrcReg)
  1111. ? tri_->getPhysicalRegisterRegClass(SrcReg)
  1112. : mri_->getRegClass(SrcReg);
  1113. if (!tri_->getMatchingSuperReg(PhysReg, SubIdx, RC))
  1114. return true;
  1115. }
  1116. }
  1117. if (MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
  1118. MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG) {
  1119. SubIdx = MI->getOperand(3).getImm();
  1120. if (VirtReg == MI->getOperand(0).getReg()) {
  1121. if (!tri_->getSubReg(PhysReg, SubIdx))
  1122. return true;
  1123. } else {
  1124. unsigned DstReg = MI->getOperand(0).getReg();
  1125. const TargetRegisterClass *RC =
  1126. TargetRegisterInfo::isPhysicalRegister(DstReg)
  1127. ? tri_->getPhysicalRegisterRegClass(DstReg)
  1128. : mri_->getRegClass(DstReg);
  1129. if (!tri_->getMatchingSuperReg(PhysReg, SubIdx, RC))
  1130. return true;
  1131. }
  1132. }
  1133. }
  1134. return false;
  1135. }
  1136. /// CanJoinExtractSubRegToPhysReg - Return true if it's possible to coalesce
  1137. /// an extract_subreg where dst is a physical register, e.g.
  1138. /// cl = EXTRACT_SUBREG reg1024, 1
  1139. bool
  1140. SimpleRegisterCoalescing::CanJoinExtractSubRegToPhysReg(unsigned DstReg,
  1141. unsigned SrcReg, unsigned SubIdx,
  1142. unsigned &RealDstReg) {
  1143. const TargetRegisterClass *RC = mri_->getRegClass(SrcReg);
  1144. RealDstReg = tri_->getMatchingSuperReg(DstReg, SubIdx, RC);
  1145. assert(RealDstReg && "Invalid extract_subreg instruction!");
  1146. // For this type of EXTRACT_SUBREG, conservatively
  1147. // check if the live interval of the source register interfere with the
  1148. // actual super physical register we are trying to coalesce with.
  1149. LiveInterval &RHS = li_->getInterval(SrcReg);
  1150. if (li_->hasInterval(RealDstReg) &&
  1151. RHS.overlaps(li_->getInterval(RealDstReg))) {
  1152. DOUT << "Interfere with register ";
  1153. DEBUG(li_->getInterval(RealDstReg).print(DOUT, tri_));
  1154. return false; // Not coalescable
  1155. }
  1156. for (const unsigned* SR = tri_->getSubRegisters(RealDstReg); *SR; ++SR)
  1157. if (li_->hasInterval(*SR) && RHS.overlaps(li_->getInterval(*SR))) {
  1158. DOUT << "Interfere with sub-register ";
  1159. DEBUG(li_->getInterval(*SR).print(DOUT, tri_));
  1160. return false; // Not coalescable
  1161. }
  1162. return true;
  1163. }
  1164. /// CanJoinInsertSubRegToPhysReg - Return true if it's possible to coalesce
  1165. /// an insert_subreg where src is a physical register, e.g.
  1166. /// reg1024 = INSERT_SUBREG reg1024, c1, 0
  1167. bool
  1168. SimpleRegisterCoalescing::CanJoinInsertSubRegToPhysReg(unsigned DstReg,
  1169. unsigned SrcReg, unsigned SubIdx,
  1170. unsigned &RealSrcReg) {
  1171. const TargetRegisterClass *RC = mri_->getRegClass(DstReg);
  1172. RealSrcReg = tri_->getMatchingSuperReg(SrcReg, SubIdx, RC);
  1173. assert(RealSrcReg && "Invalid extract_subreg instruction!");
  1174. LiveInterval &RHS = li_->getInterval(DstReg);
  1175. if (li_->hasInterval(RealSrcReg) &&
  1176. RHS.overlaps(li_->getInterval(RealSrcReg))) {
  1177. DOUT << "Interfere with register ";
  1178. DEBUG(li_->getInterval(RealSrcReg).print(DOUT, tri_));
  1179. return false; // Not coalescable
  1180. }
  1181. for (const unsigned* SR = tri_->getSubRegisters(RealSrcReg); *SR; ++SR)
  1182. if (li_->hasInterval(*SR) && RHS.overlaps(li_->getInterval(*SR))) {
  1183. DOUT << "Interfere with sub-register ";
  1184. DEBUG(li_->getInterval(*SR).print(DOUT, tri_));
  1185. return false; // Not coalescable
  1186. }
  1187. return true;
  1188. }
  1189. /// getRegAllocPreference - Return register allocation preference register.
  1190. ///
  1191. static unsigned getRegAllocPreference(unsigned Reg, MachineFunction &MF,
  1192. MachineRegisterInfo *MRI,
  1193. const TargetRegisterInfo *TRI) {
  1194. if (TargetRegisterInfo::isPhysicalRegister(Reg))
  1195. return 0;
  1196. std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg);
  1197. return TRI->ResolveRegAllocHint(Hint.first, Hint.second, MF);
  1198. }
  1199. /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
  1200. /// which are the src/dst of the copy instruction CopyMI. This returns true
  1201. /// if the copy was successfully coalesced away. If it is not currently
  1202. /// possible to coalesce this interval, but it may be possible if other
  1203. /// things get coalesced, then it returns true by reference in 'Again'.
  1204. bool SimpleRegisterCoalescing::JoinCopy(CopyRec &TheCopy, bool &Again) {
  1205. MachineInstr *CopyMI = TheCopy.MI;
  1206. Again = false;
  1207. if (JoinedCopies.count(CopyMI) || ReMatCopies.count(CopyMI))
  1208. return false; // Already done.
  1209. DOUT << li_->getInstructionIndex(CopyMI) << '\t' << *CopyMI;
  1210. unsigned SrcReg, DstReg, SrcSubIdx = 0, DstSubIdx = 0;
  1211. bool isExtSubReg = CopyMI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG;
  1212. bool isInsSubReg = CopyMI->getOpcode() == TargetInstrInfo::INSERT_SUBREG;
  1213. bool isSubRegToReg = CopyMI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG;
  1214. unsigned SubIdx = 0;
  1215. if (isExtSubReg) {
  1216. DstReg = CopyMI->getOperand(0).getReg();
  1217. DstSubIdx = CopyMI->getOperand(0).getSubReg();
  1218. SrcReg = CopyMI->getOperand(1).getReg();
  1219. SrcSubIdx = CopyMI->getOperand(2).getImm();
  1220. } else if (isInsSubReg || isSubRegToReg) {
  1221. if (CopyMI->getOperand(2).getSubReg()) {
  1222. DOUT << "\tSource of insert_subreg is already coalesced "
  1223. << "to another register.\n";
  1224. return false; // Not coalescable.
  1225. }
  1226. DstReg = CopyMI->getOperand(0).getReg();
  1227. DstSubIdx = CopyMI->getOperand(3).getImm();
  1228. SrcReg = CopyMI->getOperand(2).getReg();
  1229. } else if (!tii_->isMoveInstr(*CopyMI, SrcReg, DstReg, SrcSubIdx, DstSubIdx)){
  1230. LLVM_UNREACHABLE("Unrecognized copy instruction!");
  1231. }
  1232. // If they are already joined we continue.
  1233. if (SrcReg == DstReg) {
  1234. DOUT << "\tCopy already coalesced.\n";
  1235. return false; // Not coalescable.
  1236. }
  1237. bool SrcIsPhys = TargetRegisterInfo::isPhysicalRegister(SrcReg);
  1238. bool DstIsPhys = TargetRegisterInfo::isPhysicalRegister(DstReg);
  1239. // If they are both physical registers, we cannot join them.
  1240. if (SrcIsPhys && DstIsPhys) {
  1241. DOUT << "\tCan not coalesce physregs.\n";
  1242. return false; // Not coalescable.
  1243. }
  1244. // We only join virtual registers with allocatable physical registers.
  1245. if (SrcIsPhys && !allocatableRegs_[SrcReg]) {
  1246. DOUT << "\tSrc reg is unallocatable physreg.\n";
  1247. return false; // Not coalescable.
  1248. }
  1249. if (DstIsPhys && !allocatableRegs_[DstReg]) {
  1250. DOUT << "\tDst reg is unallocatable physreg.\n";
  1251. return false; // Not coalescable.
  1252. }
  1253. // Check that a physical source register is compatible with dst regclass
  1254. if (SrcIsPhys) {
  1255. unsigned SrcSubReg = SrcSubIdx ?
  1256. tri_->getSubReg(SrcReg, SrcSubIdx) : SrcReg;
  1257. const TargetRegisterClass *DstRC = mri_->getRegClass(DstReg);
  1258. const TargetRegisterClass *DstSubRC = DstRC;
  1259. if (DstSubIdx)
  1260. DstSubRC = DstRC->getSubRegisterRegClass(DstSubIdx);
  1261. assert(DstSubRC && "Illegal subregister index");
  1262. if (!DstSubRC->contains(SrcSubReg)) {
  1263. DOUT << "\tIncompatible destination regclass: "
  1264. << tri_->getName(SrcSubReg) << " not in " << DstSubRC->getName()
  1265. << ".\n";
  1266. return false; // Not coalescable.
  1267. }
  1268. }
  1269. // Check that a physical dst register is compatible with source regclass
  1270. if (DstIsPhys) {
  1271. unsigned DstSubReg = DstSubIdx ?
  1272. tri_->getSubReg(DstReg, DstSubIdx) : DstReg;
  1273. const TargetRegisterClass *SrcRC = mri_->getRegClass(SrcReg);
  1274. const TargetRegisterClass *SrcSubRC = SrcRC;
  1275. if (SrcSubIdx)
  1276. SrcSubRC = SrcRC->getSubRegisterRegClass(SrcSubIdx);
  1277. assert(SrcSubRC && "Illegal subregister index");
  1278. if (!SrcSubRC->contains(DstReg)) {
  1279. DOUT << "\tIncompatible source regclass: "
  1280. << tri_->getName(DstSubReg) << " not in " << SrcSubRC->getName()
  1281. << ".\n";
  1282. return false; // Not coalescable.
  1283. }
  1284. }
  1285. // Should be non-null only when coalescing to a sub-register class.
  1286. bool CrossRC = false;
  1287. const TargetRegisterClass *NewRC = NULL;
  1288. MachineBasicBlock *CopyMBB = CopyMI->getParent();
  1289. unsigned RealDstReg = 0;
  1290. unsigned RealSrcReg = 0;
  1291. if (isExtSubReg || isInsSubReg || isSubRegToReg) {
  1292. SubIdx = CopyMI->getOperand(isExtSubReg ? 2 : 3).getImm();
  1293. if (SrcIsPhys && isExtSubReg) {
  1294. // r1024 = EXTRACT_SUBREG EAX, 0 then r1024 is really going to be
  1295. // coalesced with AX.
  1296. unsigned DstSubIdx = CopyMI->getOperand(0).getSubReg();
  1297. if (DstSubIdx) {
  1298. // r1024<2> = EXTRACT_SUBREG EAX, 2. Then r1024 has already been
  1299. // coalesced to a larger register so the subreg indices cancel out.
  1300. if (DstSubIdx != SubIdx) {
  1301. DOUT << "\t Sub-register indices mismatch.\n";
  1302. return false; // Not coalescable.
  1303. }
  1304. } else
  1305. SrcReg = tri_->getSubReg(SrcReg, SubIdx);
  1306. SubIdx = 0;
  1307. } else if (DstIsPhys && (isInsSubReg || isSubRegToReg)) {
  1308. // EAX = INSERT_SUBREG EAX, r1024, 0
  1309. unsigned SrcSubIdx = CopyMI->getOperand(2).getSubReg();
  1310. if (SrcSubIdx) {
  1311. // EAX = INSERT_SUBREG EAX, r1024<2>, 2 Then r1024 has already been
  1312. // coalesced to a larger register so the subreg indices cancel out.
  1313. if (SrcSubIdx != SubIdx) {
  1314. DOUT << "\t Sub-register indices mismatch.\n";
  1315. return false; // Not coalescable.
  1316. }
  1317. } else
  1318. DstReg = tri_->getSubReg(DstReg, SubIdx);
  1319. SubIdx = 0;
  1320. } else if ((DstIsPhys && isExtSubReg) ||
  1321. (SrcIsPhys && (isInsSubReg || isSubRegToReg))) {
  1322. if (!isSubRegToReg && CopyMI->getOperand(1).getSubReg()) {
  1323. DOUT << "\tSrc of extract_subreg already coalesced with reg"
  1324. << " of a super-class.\n";
  1325. return false; // Not coalescable.
  1326. }
  1327. if (isExtSubReg) {
  1328. if (!CanJoinExtractSubRegToPhysReg(DstReg, SrcReg, SubIdx, RealDstReg))
  1329. return false; // Not coalescable
  1330. } else {
  1331. if (!CanJoinInsertSubRegToPhysReg(DstReg, SrcReg, SubIdx, RealSrcReg))
  1332. return false; // Not coalescable
  1333. }
  1334. SubIdx = 0;
  1335. } else {
  1336. unsigned OldSubIdx = isExtSubReg ? CopyMI->getOperand(0).getSubReg()
  1337. : CopyMI->getOperand(2).getSubReg();
  1338. if (OldSubIdx) {
  1339. if (OldSubIdx == SubIdx && !differingRegisterClasses(SrcReg, DstReg))
  1340. // r1024<2> = EXTRACT_SUBREG r1025, 2. Then r1024 has already been
  1341. // coalesced to a larger register so the subreg indices cancel out.
  1342. // Also check if the other larger register is of the same register
  1343. // class as the would be resulting register.
  1344. SubIdx = 0;
  1345. else {
  1346. DOUT << "\t Sub-register indices mismatch.\n";
  1347. return false; // Not coalescable.
  1348. }
  1349. }
  1350. if (SubIdx) {
  1351. unsigned LargeReg = isExtSubReg ? SrcReg : DstReg;
  1352. unsigned SmallReg = isExtSubReg ? DstReg : SrcReg;
  1353. unsigned Limit= allocatableRCRegs_[mri_->getRegClass(SmallReg)].count();
  1354. if (!isWinToJoinCrossClass(LargeReg, SmallReg, Limit)) {
  1355. Again = true; // May be possible to coalesce later.
  1356. return false;
  1357. }
  1358. }
  1359. }
  1360. } else if (differingRegisterClasses(SrcReg, DstReg)) {
  1361. if (!CrossClassJoin)
  1362. return false;
  1363. CrossRC = true;
  1364. // FIXME: What if the result of a EXTRACT_SUBREG is then coalesced
  1365. // with another? If it's the resulting destination register, then
  1366. // the subidx must be propagated to uses (but only those defined
  1367. // by the EXTRACT_SUBREG). If it's being coalesced into another
  1368. // register, it should be safe because register is assumed to have
  1369. // the register class of the super-register.
  1370. // Process moves where one of the registers have a sub-register index.
  1371. MachineOperand *DstMO = CopyMI->findRegisterDefOperand(DstReg);
  1372. MachineOperand *SrcMO = CopyMI->findRegisterUseOperand(SrcReg);
  1373. SubIdx = DstMO->getSubReg();
  1374. if (SubIdx) {
  1375. if (SrcMO->getSubReg())
  1376. // FIXME: can we handle this?
  1377. return false;
  1378. // This is not an insert_subreg but it looks like one.
  1379. // e.g. %reg1024:4 = MOV32rr %EAX
  1380. isInsSubReg = true;
  1381. if (SrcIsPhys) {
  1382. if (!CanJoinInsertSubRegToPhysReg(DstReg, SrcReg, SubIdx, RealSrcReg))
  1383. return false; // Not coalescable
  1384. SubIdx = 0;
  1385. }
  1386. } else {
  1387. SubIdx = SrcMO->getSubReg();
  1388. if (SubIdx) {
  1389. // This is not a extract_subreg but it looks like one.
  1390. // e.g. %cl = MOV16rr %reg1024:1
  1391. isExtSubReg = true;
  1392. if (DstIsPhys) {
  1393. if (!CanJoinExtractSubRegToPhysReg(DstReg, SrcReg, SubIdx,RealDstReg))
  1394. return false; // Not coalescable
  1395. SubIdx = 0;
  1396. }
  1397. }
  1398. }
  1399. const TargetRegisterClass *SrcRC= SrcIsPhys ? 0 : mri_->getRegClass(SrcReg);
  1400. const TargetRegisterClass *DstRC= DstIsPhys ? 0 : mri_->getRegClass(DstReg);
  1401. unsigned LargeReg = SrcReg;
  1402. unsigned SmallReg = DstReg;
  1403. unsigned Limit = 0;
  1404. // Now determine the register class of the joined register.
  1405. if (isExtSubReg) {
  1406. if (SubIdx && DstRC && DstRC->isASubClass()) {
  1407. // This is a move to a sub-register class. However, the source is a
  1408. // sub-register of a larger register class. We don't know what should
  1409. // the register class be. FIXME.
  1410. Again = true;
  1411. return false;
  1412. }
  1413. Limit = allocatableRCRegs_[DstRC].count();
  1414. } else if (!SrcIsPhys && !DstIsPhys) {
  1415. NewRC = getCommonSubClass(SrcRC, DstRC);
  1416. if (!NewRC) {
  1417. DOUT << "\tDisjoint regclasses: "
  1418. << SrcRC->getName() << ", "
  1419. << DstRC->getName() << ".\n";
  1420. return false; // Not coalescable.
  1421. }
  1422. if (DstRC->getSize() > SrcRC->getSize())
  1423. std::swap(LargeReg, SmallReg);
  1424. }
  1425. // If we are joining two virtual registers and the resulting register
  1426. // class is more restrictive (fewer register, smaller size). Check if it's
  1427. // worth doing the merge.
  1428. if (!SrcIsPhys && !DstIsPhys &&
  1429. (isExtSubReg || DstRC->isASubClass()) &&
  1430. !isWinToJoinCrossClass(LargeReg, SmallReg,
  1431. allocatableRCRegs_[NewRC].count())) {
  1432. DOUT << "\tSrc/Dest are different register classes.\n";
  1433. // Allow the coalescer to try again in case either side gets coalesced to
  1434. // a physical register that's compatible with the other side. e.g.
  1435. // r1024 = MOV32to32_ r1025
  1436. // But later r1024 is assigned EAX then r1025 may be coalesced with EAX.
  1437. Again = true; // May be possible to coalesce later.
  1438. return false;
  1439. }
  1440. }
  1441. // Will it create illegal extract_subreg / insert_subreg?
  1442. if (SrcIsPhys && HasIncompatibleSubRegDefUse(CopyMI, DstReg, SrcReg))
  1443. return false;
  1444. if (DstIsPhys && HasIncompatibleSubRegDefUse(CopyMI, SrcReg, DstReg))
  1445. return false;
  1446. LiveInterval &SrcInt = li_->getInterval(SrcReg);
  1447. LiveInterval &DstInt = li_->getInterval(DstReg);
  1448. assert(SrcInt.reg == SrcReg && DstInt.reg == DstReg &&
  1449. "Register mapping is horribly broken!");
  1450. DOUT << "\t\tInspecting "; SrcInt.print(DOUT, tri_);
  1451. DOUT << " and "; DstInt.print(DOUT, tri_);
  1452. DOUT << ": ";
  1453. // Save a copy of the virtual register live interval. We'll manually
  1454. // merge this into the "real" physical register live interval this is
  1455. // coalesced with.
  1456. LiveInterval *SavedLI = 0;
  1457. if (RealDstReg)
  1458. SavedLI = li_->dupInterval(&SrcInt);
  1459. else if (RealSrcReg)
  1460. SavedLI = li_->dupInterval(&DstInt);
  1461. // Check if it is necessary to propagate "isDead" property.
  1462. if (!isExtSubReg && !isInsSubReg && !isSubRegToReg) {
  1463. MachineOperand *mopd = CopyMI->findRegisterDefOperand(DstReg, false);
  1464. bool isDead = mopd->isDead();
  1465. // We need to be careful about coalescing a source physical register with a
  1466. // virtual register. Once the coalescing is done, it cannot be broken and
  1467. // these are not spillable! If the destination interval uses are far away,
  1468. // think twice about coalescing them!
  1469. if (!isDead && (SrcIsPhys || DstIsPhys)) {
  1470. // If the copy is in a loop, take care not to coalesce aggressively if the
  1471. // src is coming in from outside the loop (or the dst is out of the loop).
  1472. // If it's not in a loop, then determine whether to join them base purely
  1473. // by the length of the interval.
  1474. if (PhysJoinTweak) {
  1475. if (SrcIsPhys) {
  1476. if (!isWinToJoinVRWithSrcPhysReg(CopyMI, CopyMBB, DstInt, SrcInt)) {
  1477. mri_->setRegAllocationHint(DstInt.reg, 0, SrcReg);
  1478. ++numAborts;
  1479. DOUT << "\tMay tie down a physical register, abort!\n";
  1480. Again = true; // May be possible to coalesce later.
  1481. return false;
  1482. }
  1483. } else {
  1484. if (!isWinToJoinVRWithDstPhysReg(CopyMI, CopyMBB, DstInt, SrcInt)) {
  1485. mri_->setRegAllocationHint(SrcInt.reg, 0, DstReg);
  1486. ++numAborts;
  1487. DOUT << "\tMay tie down a physical register, abort!\n";
  1488. Again = true; // May be possible to coalesce later.
  1489. return false;
  1490. }
  1491. }
  1492. } else {
  1493. // If the virtual register live interval is long but it has low use desity,
  1494. // do not join them, instead mark the physical register as its allocation
  1495. // preference.
  1496. LiveInterval &JoinVInt = SrcIsPhys ? DstInt : SrcInt;
  1497. unsigned JoinVReg = SrcIsPhys ? DstReg : SrcReg;
  1498. unsigned JoinPReg = SrcIsPhys ? SrcReg : DstReg;
  1499. const TargetRegisterClass *RC = mri_->getRegClass(JoinVReg);
  1500. unsigned Threshold = allocatableRCRegs_[RC].count() * 2;
  1501. if (TheCopy.isBackEdge)
  1502. Threshold *= 2; // Favors back edge copies.
  1503. unsigned Length = li_->getApproximateInstructionCount(JoinVInt);
  1504. float Ratio = 1.0 / Threshold;
  1505. if (Length > Threshold &&
  1506. (((float)std::distance(mri_->use_begin(JoinVReg),
  1507. mri_->use_end()) / Length) < Ratio)) {
  1508. mri_->setRegAllocationHint(JoinVInt.reg, 0, JoinPReg);
  1509. ++numAborts;
  1510. DOUT << "\tMay tie down a physical register, abort!\n";
  1511. Again = true; // May be possible to coalesce later.
  1512. return false;
  1513. }
  1514. }
  1515. }
  1516. }
  1517. // Okay, attempt to join these two intervals. On failure, this returns false.
  1518. // Otherwise, if one of the intervals being joined is a physreg, this method
  1519. // always canonicalizes DstInt to be it. The output "SrcInt" will not have
  1520. // been modified, so we can use this information below to update aliases.
  1521. bool Swapped = false;
  1522. // If SrcInt is implicitly defined, it's safe to coalesce.
  1523. bool isEmpty = SrcInt.empty();
  1524. if (isEmpty && !CanCoalesceWithImpDef(CopyMI, DstInt, SrcInt)) {
  1525. // Only coalesce an empty interval (defined by implicit_def) with
  1526. // another interval which has a valno defined by the CopyMI and the CopyMI
  1527. // is a kill of the implicit def.
  1528. DOUT << "Not profitable!\n";
  1529. return false;
  1530. }
  1531. if (!isEmpty && !JoinIntervals(DstInt, SrcInt, Swapped)) {
  1532. // Coalescing failed.
  1533. // If definition of source is defined by trivial computation, try
  1534. // rematerializing it.
  1535. if (!isExtSubReg && !isInsSubReg && !isSubRegToReg &&
  1536. ReMaterializeTrivialDef(SrcInt, DstInt.reg, CopyMI))
  1537. return true;
  1538. // If we can eliminate the copy without merging the live ranges, do so now.
  1539. if (!isExtSubReg && !isInsSubReg && !isSubRegToReg &&
  1540. (AdjustCopiesBackFrom(SrcInt, DstInt, CopyMI) ||
  1541. RemoveCopyByCommutingDef(SrcInt, DstInt, CopyMI))) {
  1542. JoinedCopies.insert(CopyMI);
  1543. return true;
  1544. }
  1545. // Otherwise, we are unable to join the intervals.
  1546. DOUT << "Interference!\n";
  1547. Again = true; // May be possible to coalesce later.
  1548. return false;
  1549. }
  1550. LiveInterval *ResSrcInt = &SrcInt;
  1551. LiveInterval *ResDstInt = &DstInt;
  1552. if (Swapped) {
  1553. std::swap(SrcReg, DstReg);
  1554. std::swap(ResSrcInt, ResDstInt);
  1555. }
  1556. assert(TargetRegisterInfo::isVirtualRegister(SrcReg) &&
  1557. "LiveInterval::join didn't work right!");
  1558. // If we're about to merge live ranges into a physical register live interval,
  1559. // we have to update any aliased register's live ranges to indicate that they
  1560. // have clobbered values for this range.
  1561. if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
  1562. // If this is a extract_subreg where dst is a physical register, e.g.
  1563. // cl = EXTRACT_SUBREG reg1024, 1
  1564. // then create and update the actual physical register allocated to RHS.
  1565. if (RealDstReg || RealSrcReg) {
  1566. LiveInterval &RealInt =
  1567. li_->getOrCreateInterval(RealDstReg ? RealDstReg : RealSrcReg);
  1568. for (LiveInterval::const_vni_iterator I = SavedLI->vni_begin(),
  1569. E = SavedLI->vni_end(); I != E; ++I) {
  1570. const VNInfo *ValNo = *I;
  1571. VNInfo *NewValNo = RealInt.getNextValue(ValNo->def, ValNo->copy,
  1572. false, // updated at *
  1573. li_->getVNInfoAllocator());
  1574. NewValNo->setFlags(ValNo->getFlags()); // * updated here.
  1575. RealInt.addKills(NewValNo, ValNo->kills);
  1576. RealInt.MergeValueInAsValue(*SavedLI, ValNo, NewValNo);
  1577. }
  1578. RealInt.weight += SavedLI->weight;
  1579. DstReg = RealDstReg ? RealDstReg : RealSrcReg;
  1580. }
  1581. // Update the liveintervals of sub-registers.
  1582. for (const unsigned *AS = tri_->getSubRegisters(DstReg); *AS; ++AS)
  1583. li_->getOrCreateInterval(*AS).MergeInClobberRanges(*ResSrcInt,
  1584. li_->getVNInfoAllocator());
  1585. }
  1586. // If this is a EXTRACT_SUBREG, make sure the result of coalescing is the
  1587. // larger super-register.
  1588. if ((isExtSubReg || isInsSubReg || isSubRegToReg) &&
  1589. !SrcIsPhys && !DstIsPhys) {
  1590. if ((isExtSubReg && !Swapped) ||
  1591. ((isInsSubReg || isSubRegToReg) && Swapped)) {
  1592. ResSrcInt->Copy(*ResDstInt, mri_, li_->getVNInfoAllocator());
  1593. std::swap(SrcReg, DstReg);
  1594. std::swap(ResSrcInt, ResDstInt);
  1595. }
  1596. }
  1597. // Coalescing to a virtual register that is of a sub-register class of the
  1598. // other. Make sure the resulting register is set to the right register class.
  1599. if (CrossRC) {
  1600. ++numCrossRCs;
  1601. if (NewRC)
  1602. mri_->setRegClass(DstReg, NewRC);
  1603. }
  1604. if (NewHeuristic) {
  1605. // Add all copies that define val# in the source interval into the queue.
  1606. for (LiveInterval::const_vni_iterator i = ResSrcInt->vni_begin(),
  1607. e = ResSrcInt->vni_end(); i != e; ++i) {
  1608. const VNInfo *vni = *i;
  1609. // FIXME: Do isPHIDef and isDefAccurate both need to be tested?
  1610. if (!vni->def || vni->isUnused() || vni->isPHIDef() || !vni->isDefAccurate())
  1611. continue;
  1612. MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def);
  1613. unsigned NewSrcReg, NewDstReg, NewSrcSubIdx, NewDstSubIdx;
  1614. if (CopyMI &&
  1615. JoinedCopies.count(CopyMI) == 0 &&
  1616. tii_->isMoveInstr(*CopyMI, NewSrcReg, NewDstReg,
  1617. NewSrcSubIdx, NewDstSubIdx)) {
  1618. unsigned LoopDepth = loopInfo->getLoopDepth(CopyMBB);
  1619. JoinQueue->push(CopyRec(CopyMI, LoopDepth,
  1620. isBackEdgeCopy(CopyMI, DstReg)));
  1621. }
  1622. }
  1623. }
  1624. // Remember to delete the copy instruction.
  1625. JoinedCopies.insert(CopyMI);
  1626. // Some live range has been lengthened due to colaescing, eliminate the
  1627. // unnecessary kills.
  1628. RemoveUnnecessaryKills(SrcReg, *ResDstInt);
  1629. if (TargetRegisterInfo::isVirtualRegister(DstReg))
  1630. RemoveUnnecessaryKills(DstReg, *ResDstInt);
  1631. if (isInsSubReg)
  1632. // Avoid:
  1633. // r1024 = op
  1634. // r1024 = implicit_def
  1635. // ...
  1636. // = r1024
  1637. RemoveDeadImpDef(DstReg, *ResDstInt);
  1638. UpdateRegDefsUses(SrcReg, DstReg, SubIdx);
  1639. // SrcReg is guarateed to be the register whose live interval that is
  1640. // being merged.
  1641. li_->removeInterval(SrcReg);
  1642. // Update regalloc hint.
  1643. tri_->UpdateRegAllocHint(SrcReg, DstReg, *mf_);
  1644. // Manually deleted the live interval copy.
  1645. if (SavedLI) {
  1646. SavedLI->clear();
  1647. delete SavedLI;
  1648. }
  1649. if (isEmpty) {
  1650. // Now the copy is being coalesced away, the val# previously defined
  1651. // by the copy is being defined by an IMPLICIT_DEF which defines a zero
  1652. // length interval. Remove the val#.
  1653. unsigned CopyIdx = li_->getDefIndex(li_->getInstructionIndex(CopyMI));
  1654. const LiveRange *LR = ResDstInt->getLiveRangeContaining(CopyIdx);
  1655. VNInfo *ImpVal = LR->valno;
  1656. assert(ImpVal->def == CopyIdx);
  1657. unsigned NextDef = LR->end;
  1658. TurnCopiesFromValNoToImpDefs(*ResDstInt, ImpVal);
  1659. ResDstInt->removeValNo(ImpVal);
  1660. LR = ResDstInt->FindLiveRangeContaining(NextDef);
  1661. if (LR != ResDstInt->end() && LR->valno->def == NextDef) {
  1662. // Special case: vr1024 = implicit_def
  1663. // vr1024 = insert_subreg vr1024, vr1025, c
  1664. // The insert_subreg becomes a "copy" that defines a val# which can itself
  1665. // be coalesced away.
  1666. MachineInstr *DefMI = li_->getInstructionFromIndex(NextDef);
  1667. if (DefMI->getOpcode() == TargetInstrInfo::INSERT_SUBREG)
  1668. LR->valno->copy = DefMI;
  1669. }
  1670. }
  1671. // If resulting interval has a preference that no longer fits because of subreg
  1672. // coalescing, just clear the preference.
  1673. unsigned Preference = getRegAllocPreference(ResDstInt->reg, *mf_, mri_, tri_);
  1674. if (Preference && (isExtSubReg || isInsSubReg || isSubRegToReg) &&
  1675. TargetRegisterInfo::isVirtualRegister(ResDstInt->reg)) {
  1676. const TargetRegisterClass *RC = mri_->getRegClass(ResDstInt->reg);
  1677. if (!RC->contains(Preference))
  1678. mri_->setRegAllocationHint(ResDstInt->reg, 0, 0);
  1679. }
  1680. DOUT << "\n\t\tJoined. Result = "; ResDstInt->print(DOUT, tri_);
  1681. DOUT << "\n";
  1682. ++numJoins;
  1683. return true;
  1684. }
  1685. /// ComputeUltimateVN - Assuming we are going to join two live intervals,
  1686. /// compute what the resultant value numbers for each value in the input two
  1687. /// ranges will be. This is complicated by copies between the two which can
  1688. /// and will commonly cause multiple value numbers to be merged into one.
  1689. ///
  1690. /// VN is the value number that we're trying to resolve. InstDefiningValue
  1691. /// keeps track of the new InstDefiningValue assignment for the result
  1692. /// LiveInterval. ThisFromOther/OtherFromThis are sets that keep track of
  1693. /// whether a value in this or other is a copy from the opposite set.
  1694. /// ThisValNoAssignments/OtherValNoAssignments keep track of value #'s that have
  1695. /// already been assigned.
  1696. ///
  1697. /// ThisFromOther[x] - If x is defined as a copy from the other interval, this
  1698. /// contains the value number the copy is from.
  1699. ///
  1700. static unsigned ComputeUltimateVN(VNInfo *VNI,
  1701. SmallVector<VNInfo*, 16> &NewVNInfo,
  1702. DenseMap<VNInfo*, VNInfo*> &ThisFromOther,
  1703. DenseMap<VNInfo*, VNInfo*> &OtherFromThis,
  1704. SmallVector<int, 16> &ThisValNoAssignments,
  1705. SmallVector<int, 16> &OtherValNoAssignments) {
  1706. unsigned VN = VNI->id;
  1707. // If the VN has already been computed, just return it.
  1708. if (ThisValNoAssignments[VN] >= 0)
  1709. return ThisValNoAssignments[VN];
  1710. // assert(ThisValNoAssignments[VN] != -2 && "Cyclic case?");
  1711. // If this val is not a copy from the other val, then it must be a new value
  1712. // number in the destination.
  1713. DenseMap<VNInfo*, VNInfo*>::iterator I = ThisFromOther.find(VNI);
  1714. if (I == ThisFromOther.end()) {
  1715. NewVNInfo.push_back(VNI);
  1716. return ThisValNoAssignments[VN] = NewVNInfo.size()-1;
  1717. }
  1718. VNInfo *OtherValNo = I->second;
  1719. // Otherwise, this *is* a copy from the RHS. If the other side has already
  1720. // been computed, return it.
  1721. if (OtherValNoAssignments[OtherValNo->id] >= 0)
  1722. return ThisValNoAssignments[VN] = OtherValNoAssignments[OtherValNo->id];
  1723. // Mark this value number as currently being computed, then ask what the
  1724. // ultimate value # of the other value is.
  1725. ThisValNoAssignments[VN] = -2;
  1726. unsigned UltimateVN =
  1727. ComputeUltimateVN(OtherValNo, NewVNInfo, OtherFromThis, ThisFromOther,
  1728. OtherValNoAssignments, ThisValNoAssignments);
  1729. return ThisValNoAssignments[VN] = UltimateVN;
  1730. }
  1731. static bool InVector(VNInfo *Val, const SmallVector<VNInfo*, 8> &V) {
  1732. return std::find(V.begin(), V.end(), Val) != V.end();
  1733. }
  1734. /// RangeIsDefinedByCopyFromReg - Return true if the specified live range of
  1735. /// the specified live interval is defined by a copy from the specified
  1736. /// register.
  1737. bool SimpleRegisterCoalescing::RangeIsDefinedByCopyFromReg(LiveInterval &li,
  1738. LiveRange *LR,
  1739. unsigned Reg) {
  1740. unsigned SrcReg = li_->getVNInfoSourceReg(LR->valno);
  1741. if (SrcReg == Reg)
  1742. return true;
  1743. // FIXME: Do isPHIDef and isDefAccurate both need to be tested?
  1744. if ((LR->valno->isPHIDef() || !LR->valno->isDefAccurate()) &&
  1745. TargetRegisterInfo::isPhysicalRegister(li.reg) &&
  1746. *tri_->getSuperRegisters(li.reg)) {
  1747. // It's a sub-register live interval, we may not have precise information.
  1748. // Re-compute it.
  1749. MachineInstr *DefMI = li_->getInstructionFromIndex(LR->start);
  1750. unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
  1751. if (DefMI &&
  1752. tii_->isMoveInstr(*DefMI, SrcReg, DstReg, SrcSubIdx, DstSubIdx) &&
  1753. DstReg == li.reg && SrcReg == Reg) {
  1754. // Cache computed info.
  1755. LR->valno->def = LR->start;
  1756. LR->valno->copy = DefMI;
  1757. return true;
  1758. }
  1759. }
  1760. return false;
  1761. }
  1762. /// SimpleJoin - Attempt to joint the specified interval into this one. The
  1763. /// caller of this method must guarantee that the RHS only contains a single
  1764. /// value number and that the RHS is not defined by a copy from this
  1765. /// interval. This returns false if the intervals are not joinable, or it
  1766. /// joins them and returns true.
  1767. bool SimpleRegisterCoalescing::SimpleJoin(LiveInterval &LHS, LiveInterval &RHS){
  1768. assert(RHS.containsOneValue());
  1769. // Some number (potentially more than one) value numbers in the current
  1770. // interval may be defined as copies from the RHS. Scan the overlapping
  1771. // portions of the LHS and RHS, keeping track of this and looking for
  1772. // overlapping live ranges that are NOT defined as copies. If these exist, we
  1773. // cannot coalesce.
  1774. LiveInterval::iterator LHSIt = LHS.begin(), LHSEnd = LHS.end();
  1775. LiveInterval::iterator RHSIt = RHS.begin(), RHSEnd = RHS.end();
  1776. if (LHSIt->start < RHSIt->start) {
  1777. LHSIt = std::upper_bound(LHSIt, LHSEnd, RHSIt->start);
  1778. if (LHSIt != LHS.begin()) --LHSIt;
  1779. } else if (RHSIt->start < LHSIt->start) {
  1780. RHSIt = std::upper_bound(RHSIt, RHSEnd, LHSIt->start);
  1781. if (RHSIt != RHS.begin()) --RHSIt;
  1782. }
  1783. SmallVector<VNInfo*, 8> EliminatedLHSVals;
  1784. while (1) {
  1785. // Determine if these live intervals overlap.
  1786. bool Overlaps = false;
  1787. if (LHSIt->start <= RHSIt->start)
  1788. Overlaps = LHSIt->end > RHSIt->start;
  1789. else
  1790. Overlaps = RHSIt->end > LHSIt->start;
  1791. // If the live intervals overlap, there are two interesting cases: if the
  1792. // LHS interval is defined by a copy from the RHS, it's ok and we record
  1793. // that the LHS value # is the same as the RHS. If it's not, then we cannot
  1794. // coalesce these live ranges and we bail out.
  1795. if (Overlaps) {
  1796. // If we haven't already recorded that this value # is safe, check it.
  1797. if (!InVector(LHSIt->valno, EliminatedLHSVals)) {
  1798. // Copy from the RHS?
  1799. if (!RangeIsDefinedByCopyFromReg(LHS, LHSIt, RHS.reg))
  1800. return false; // Nope, bail out.
  1801. if (LHSIt->contains(RHSIt->valno->def))
  1802. // Here is an interesting situation:
  1803. // BB1:
  1804. // vr1025 = copy vr1024
  1805. // ..
  1806. // BB2:
  1807. // vr1024 = op
  1808. // = vr1025
  1809. // Even though vr1025 is copied from vr1024, it's not safe to
  1810. // coalesce them since the live range of vr1025 intersects the
  1811. // def of vr1024. This happens because vr1025 is assigned the
  1812. // value of the previous iteration of vr1024.
  1813. return false;
  1814. EliminatedLHSVals.push_back(LHSIt->valno);
  1815. }
  1816. // We know this entire LHS live range is okay, so skip it now.
  1817. if (++LHSIt == LHSEnd) break;
  1818. continue;
  1819. }
  1820. if (LHSIt->end < RHSIt->end) {
  1821. if (++LHSIt == LHSEnd) break;
  1822. } else {
  1823. // One interesting case to check here. It's possible that we have
  1824. // something like "X3 = Y" which defines a new value number in the LHS,
  1825. // and is the last use of this liverange of the RHS. In this case, we
  1826. // want to notice this copy (so that it gets coalesced away) even though
  1827. // the live ranges don't actually overlap.
  1828. if (LHSIt->start == RHSIt->end) {
  1829. if (InVector(LHSIt->valno, EliminatedLHSVals)) {
  1830. // We already know that this value number is going to be merged in
  1831. // if coalescing succeeds. Just skip the liverange.
  1832. if (++LHSIt == LHSEnd) break;
  1833. } else {
  1834. // Otherwise, if this is a copy from the RHS, mark it as being merged
  1835. // in.
  1836. if (RangeIsDefinedByCopyFromReg(LHS, LHSIt, RHS.reg)) {
  1837. if (LHSIt->contains(RHSIt->valno->def))
  1838. // Here is an interesting situation:
  1839. // BB1:
  1840. // vr1025 = copy vr1024
  1841. // ..
  1842. // BB2:
  1843. // vr1024 = op
  1844. // = vr1025
  1845. // Even though vr1025 is copied from vr1024, it's not safe to
  1846. // coalesced them since live range of vr1025 intersects the
  1847. // def of vr1024. This happens because vr1025 is assigned the
  1848. // value of the previous iteration of vr1024.
  1849. return false;
  1850. EliminatedLHSVals.push_back(LHSIt->valno);
  1851. // We know this entire LHS live range is okay, so skip it now.
  1852. if (++LHSIt == LHSEnd) break;
  1853. }
  1854. }
  1855. }
  1856. if (++RHSIt == RHSEnd) break;
  1857. }
  1858. }
  1859. // If we got here, we know that the coalescing will be successful and that
  1860. // the value numbers in EliminatedLHSVals will all be merged together. Since
  1861. // the most common case is that EliminatedLHSVals has a single number, we
  1862. // optimize for it: if there is more than one value, we merge them all into
  1863. // the lowest numbered one, then handle the interval as if we were merging
  1864. // with one value number.
  1865. VNInfo *LHSValNo = NULL;
  1866. if (EliminatedLHSVals.size() > 1) {
  1867. // Loop through all the equal value numbers merging them into the smallest
  1868. // one.
  1869. VNInfo *Smallest = EliminatedLHSVals[0];
  1870. for (unsigned i = 1, e = EliminatedLHSVals.size(); i != e; ++i) {
  1871. if (EliminatedLHSVals[i]->id < Smallest->id) {
  1872. // Merge the current notion of the smallest into the smaller one.
  1873. LHS.MergeValueNumberInto(Smallest, EliminatedLHSVals[i]);
  1874. Smallest = EliminatedLHSVals[i];
  1875. } else {
  1876. // Merge into the smallest.
  1877. LHS.MergeValueNumberInto(EliminatedLHSVals[i], Smallest);
  1878. }
  1879. }
  1880. LHSValNo = Smallest;
  1881. } else if (EliminatedLHSVals.empty()) {
  1882. if (TargetRegisterInfo::isPhysicalRegister(LHS.reg) &&
  1883. *tri_->getSuperRegisters(LHS.reg))
  1884. // Imprecise sub-register information. Can't handle it.
  1885. return false;
  1886. LLVM_UNREACHABLE("No copies from the RHS?");
  1887. } else {
  1888. LHSValNo = EliminatedLHSVals[0];
  1889. }
  1890. // Okay, now that there is a single LHS value number that we're merging the
  1891. // RHS into, update the value number info for the LHS to indicate that the
  1892. // value number is defined where the RHS value number was.
  1893. const VNInfo *VNI = RHS.getValNumInfo(0);
  1894. LHSValNo->def = VNI->def;
  1895. LHSValNo->copy = VNI->copy;
  1896. // Okay, the final step is to loop over the RHS live intervals, adding them to
  1897. // the LHS.
  1898. if (VNI->hasPHIKill())
  1899. LHSValNo->setHasPHIKill(true);
  1900. LHS.addKills(LHSValNo, VNI->kills);
  1901. LHS.MergeRangesInAsValue(RHS, LHSValNo);
  1902. LHS.weight += RHS.weight;
  1903. // Update regalloc hint if both are virtual registers.
  1904. if (TargetRegisterInfo::isVirtualRegister(LHS.reg) &&
  1905. TargetRegisterInfo::isVirtualRegister(RHS.reg)) {
  1906. std::pair<unsigned, unsigned> RHSPref = mri_->getRegAllocationHint(RHS.reg);
  1907. std::pair<unsigned, unsigned> LHSPref = mri_->getRegAllocationHint(LHS.reg);
  1908. if (RHSPref != LHSPref)
  1909. mri_->setRegAllocationHint(LHS.reg, RHSPref.first, RHSPref.second);
  1910. }
  1911. // Update the liveintervals of sub-registers.
  1912. if (TargetRegisterInfo::isPhysicalRegister(LHS.reg))
  1913. for (const unsigned *AS = tri_->getSubRegisters(LHS.reg); *AS; ++AS)
  1914. li_->getOrCreateInterval(*AS).MergeInClobberRanges(LHS,
  1915. li_->getVNInfoAllocator());
  1916. return true;
  1917. }
  1918. /// JoinIntervals - Attempt to join these two intervals. On failure, this
  1919. /// returns false. Otherwise, if one of the intervals being joined is a
  1920. /// physreg, this method always canonicalizes LHS to be it. The output
  1921. /// "RHS" will not have been modified, so we can use this information
  1922. /// below to update aliases.
  1923. bool
  1924. SimpleRegisterCoalescing::JoinIntervals(LiveInterval &LHS, LiveInterval &RHS,
  1925. bool &Swapped) {
  1926. // Compute the final value assignment, assuming that the live ranges can be
  1927. // coalesced.
  1928. SmallVector<int, 16> LHSValNoAssignments;
  1929. SmallVector<int, 16> RHSValNoAssignments;
  1930. DenseMap<VNInfo*, VNInfo*> LHSValsDefinedFromRHS;
  1931. DenseMap<VNInfo*, VNInfo*> RHSValsDefinedFromLHS;
  1932. SmallVector<VNInfo*, 16> NewVNInfo;
  1933. // If a live interval is a physical register, conservatively check if any
  1934. // of its sub-registers is overlapping the live interval of the virtual
  1935. // register. If so, do not coalesce.
  1936. if (TargetRegisterInfo::isPhysicalRegister(LHS.reg) &&
  1937. *tri_->getSubRegisters(LHS.reg)) {
  1938. // If it's coalescing a virtual register to a physical register, estimate
  1939. // its live interval length. This is the *cost* of scanning an entire live
  1940. // interval. If the cost is low, we'll do an exhaustive check instead.
  1941. // If this is something like this:
  1942. // BB1:
  1943. // v1024 = op
  1944. // ...
  1945. // BB2:
  1946. // ...
  1947. // RAX = v1024
  1948. //
  1949. // That is, the live interval of v1024 crosses a bb. Then we can't rely on
  1950. // less conservative check. It's possible a sub-register is defined before
  1951. // v1024 (or live in) and live out of BB1.
  1952. if (RHS.containsOneValue() &&
  1953. li_->intervalIsInOneMBB(RHS) &&
  1954. li_->getApproximateInstructionCount(RHS) <= 10) {
  1955. // Perform a more exhaustive check for some common cases.
  1956. if (li_->conflictsWithPhysRegRef(RHS, LHS.reg, true, JoinedCopies))
  1957. return false;
  1958. } else {
  1959. for (const unsigned* SR = tri_->getSubRegisters(LHS.reg); *SR; ++SR)
  1960. if (li_->hasInterval(*SR) && RHS.overlaps(li_->getInterval(*SR))) {
  1961. DOUT << "Interfere with sub-register ";
  1962. DEBUG(li_->getInterval(*SR).print(DOUT, tri_));
  1963. return false;
  1964. }
  1965. }
  1966. } else if (TargetRegisterInfo::isPhysicalRegister(RHS.reg) &&
  1967. *tri_->getSubRegisters(RHS.reg)) {
  1968. if (LHS.containsOneValue() &&
  1969. li_->getApproximateInstructionCount(LHS) <= 10) {
  1970. // Perform a more exhaustive check for some common cases.
  1971. if (li_->conflictsWithPhysRegRef(LHS, RHS.reg, false, JoinedCopies))
  1972. return false;
  1973. } else {
  1974. for (const unsigned* SR = tri_->getSubRegisters(RHS.reg); *SR; ++SR)
  1975. if (li_->hasInterval(*SR) && LHS.overlaps(li_->getInterval(*SR))) {
  1976. DOUT << "Interfere with sub-register ";
  1977. DEBUG(li_->getInterval(*SR).print(DOUT, tri_));
  1978. return false;
  1979. }
  1980. }
  1981. }
  1982. // Compute ultimate value numbers for the LHS and RHS values.
  1983. if (RHS.containsOneValue()) {
  1984. // Copies from a liveinterval with a single value are simple to handle and
  1985. // very common, handle the special case here. This is important, because
  1986. // often RHS is small and LHS is large (e.g. a physreg).
  1987. // Find out if the RHS is defined as a copy from some value in the LHS.
  1988. int RHSVal0DefinedFromLHS = -1;
  1989. int RHSValID = -1;
  1990. VNInfo *RHSValNoInfo = NULL;
  1991. VNInfo *RHSValNoInfo0 = RHS.getValNumInfo(0);
  1992. unsigned RHSSrcReg = li_->getVNInfoSourceReg(RHSValNoInfo0);
  1993. if (RHSSrcReg == 0 || RHSSrcReg != LHS.reg) {
  1994. // If RHS is not defined as a copy from the LHS, we can use simpler and
  1995. // faster checks to see if the live ranges are coalescable. This joiner
  1996. // can't swap the LHS/RHS intervals though.
  1997. if (!TargetRegisterInfo::isPhysicalRegister(RHS.reg)) {
  1998. return SimpleJoin(LHS, RHS);
  1999. } else {
  2000. RHSValNoInfo = RHSValNoInfo0;
  2001. }
  2002. } else {
  2003. // It was defined as a copy from the LHS, find out what value # it is.
  2004. RHSValNoInfo = LHS.getLiveRangeContaining(RHSValNoInfo0->def-1)->valno;
  2005. RHSValID = RHSValNoInfo->id;
  2006. RHSVal0DefinedFromLHS = RHSValID;
  2007. }
  2008. LHSValNoAssignments.resize(LHS.getNumValNums(), -1);
  2009. RHSValNoAssignments.resize(RHS.getNumValNums(), -1);
  2010. NewVNInfo.resize(LHS.getNumValNums(), NULL);
  2011. // Okay, *all* of the values in LHS that are defined as a copy from RHS
  2012. // should now get updated.
  2013. for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end();
  2014. i != e; ++i) {
  2015. VNInfo *VNI = *i;
  2016. unsigned VN = VNI->id;
  2017. if (unsigned LHSSrcReg = li_->getVNInfoSourceReg(VNI)) {
  2018. if (LHSSrcReg != RHS.reg) {
  2019. // If this is not a copy from the RHS, its value number will be
  2020. // unmodified by the coalescing.
  2021. NewVNInfo[VN] = VNI;
  2022. LHSValNoAssignments[VN] = VN;
  2023. } else if (RHSValID == -1) {
  2024. // Otherwise, it is a copy from the RHS, and we don't already have a
  2025. // value# for it. Keep the current value number, but remember it.
  2026. LHSValNoAssignments[VN] = RHSValID = VN;
  2027. NewVNInfo[VN] = RHSValNoInfo;
  2028. LHSValsDefinedFromRHS[VNI] = RHSValNoInfo0;
  2029. } else {
  2030. // Otherwise, use the specified value #.
  2031. LHSValNoAssignments[VN] = RHSValID;
  2032. if (VN == (unsigned)RHSValID) { // Else this val# is dead.
  2033. NewVNInfo[VN] = RHSValNoInfo;
  2034. LHSValsDefinedFromRHS[VNI] = RHSValNoInfo0;
  2035. }
  2036. }
  2037. } else {
  2038. NewVNInfo[VN] = VNI;
  2039. LHSValNoAssignments[VN] = VN;
  2040. }
  2041. }
  2042. assert(RHSValID != -1 && "Didn't find value #?");
  2043. RHSValNoAssignments[0] = RHSValID;
  2044. if (RHSVal0DefinedFromLHS != -1) {
  2045. // This path doesn't go through ComputeUltimateVN so just set
  2046. // it to anything.
  2047. RHSValsDefinedFromLHS[RHSValNoInfo0] = (VNInfo*)1;
  2048. }
  2049. } else {
  2050. // Loop over the value numbers of the LHS, seeing if any are defined from
  2051. // the RHS.
  2052. for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end();
  2053. i != e; ++i) {
  2054. VNInfo *VNI = *i;
  2055. if (VNI->isUnused() || VNI->copy == 0) // Src not defined by a copy?
  2056. continue;
  2057. // DstReg is known to be a register in the LHS interval. If the src is
  2058. // from the RHS interval, we can use its value #.
  2059. if (li_->getVNInfoSourceReg(VNI) != RHS.reg)
  2060. continue;
  2061. // Figure out the value # from the RHS.
  2062. LHSValsDefinedFromRHS[VNI]=RHS.getLiveRangeContaining(VNI->def-1)->valno;
  2063. }
  2064. // Loop over the value numbers of the RHS, seeing if any are defined from
  2065. // the LHS.
  2066. for (LiveInterval::vni_iterator i = RHS.vni_begin(), e = RHS.vni_end();
  2067. i != e; ++i) {
  2068. VNInfo *VNI = *i;
  2069. if (VNI->isUnused() || VNI->copy == 0) // Src not defined by a copy?
  2070. continue;
  2071. // DstReg is known to be a register in the RHS interval. If the src is
  2072. // from the LHS interval, we can use its value #.
  2073. if (li_->getVNInfoSourceReg(VNI) != LHS.reg)
  2074. continue;
  2075. // Figure out the value # from the LHS.
  2076. RHSValsDefinedFromLHS[VNI]=LHS.getLiveRangeContaining(VNI->def-1)->valno;
  2077. }
  2078. LHSValNoAssignments.resize(LHS.getNumValNums(), -1);
  2079. RHSValNoAssignments.resize(RHS.getNumValNums(), -1);
  2080. NewVNInfo.reserve(LHS.getNumValNums() + RHS.getNumValNums());
  2081. for (LiveInterval::vni_iterator i = LHS.vni_begin(), e = LHS.vni_end();
  2082. i != e; ++i) {
  2083. VNInfo *VNI = *i;
  2084. unsigned VN = VNI->id;
  2085. if (LHSValNoAssignments[VN] >= 0 || VNI->isUnused())
  2086. continue;
  2087. ComputeUltimateVN(VNI, NewVNInfo,
  2088. LHSValsDefinedFromRHS, RHSValsDefinedFromLHS,
  2089. LHSValNoAssignments, RHSValNoAssignments);
  2090. }
  2091. for (LiveInterval::vni_iterator i = RHS.vni_begin(), e = RHS.vni_end();
  2092. i != e; ++i) {
  2093. VNInfo *VNI = *i;
  2094. unsigned VN = VNI->id;
  2095. if (RHSValNoAssignments[VN] >= 0 || VNI->isUnused())
  2096. continue;
  2097. // If this value number isn't a copy from the LHS, it's a new number.
  2098. if (RHSValsDefinedFromLHS.find(VNI) == RHSValsDefinedFromLHS.end()) {
  2099. NewVNInfo.push_back(VNI);
  2100. RHSValNoAssignments[VN] = NewVNInfo.size()-1;
  2101. continue;
  2102. }
  2103. ComputeUltimateVN(VNI, NewVNInfo,
  2104. RHSValsDefinedFromLHS, LHSValsDefinedFromRHS,
  2105. RHSValNoAssignments, LHSValNoAssignments);
  2106. }
  2107. }
  2108. // Armed with the mappings of LHS/RHS values to ultimate values, walk the
  2109. // interval lists to see if these intervals are coalescable.
  2110. LiveInterval::const_iterator I = LHS.begin();
  2111. LiveInterval::const_iterator IE = LHS.end();
  2112. LiveInterval::const_iterator J = RHS.begin();
  2113. LiveInterval::const_iterator JE = RHS.end();
  2114. // Skip ahead until the first place of potential sharing.
  2115. if (I->start < J->start) {
  2116. I = std::upper_bound(I, IE, J->start);
  2117. if (I != LHS.begin()) --I;
  2118. } else if (J->start < I->start) {
  2119. J = std::upper_bound(J, JE, I->start);
  2120. if (J != RHS.begin()) --J;
  2121. }
  2122. while (1) {
  2123. // Determine if these two live ranges overlap.
  2124. bool Overlaps;
  2125. if (I->start < J->start) {
  2126. Overlaps = I->end > J->start;
  2127. } else {
  2128. Overlaps = J->end > I->start;
  2129. }
  2130. // If so, check value # info to determine if they are really different.
  2131. if (Overlaps) {
  2132. // If the live range overlap will map to the same value number in the
  2133. // result liverange, we can still coalesce them. If not, we can't.
  2134. if (LHSValNoAssignments[I->valno->id] !=
  2135. RHSValNoAssignments[J->valno->id])
  2136. return false;
  2137. }
  2138. if (I->end < J->end) {
  2139. ++I;
  2140. if (I == IE) break;
  2141. } else {
  2142. ++J;
  2143. if (J == JE) break;
  2144. }
  2145. }
  2146. // Update kill info. Some live ranges are extended due to copy coalescing.
  2147. for (DenseMap<VNInfo*, VNInfo*>::iterator I = LHSValsDefinedFromRHS.begin(),
  2148. E = LHSValsDefinedFromRHS.end(); I != E; ++I) {
  2149. VNInfo *VNI = I->first;
  2150. unsigned LHSValID = LHSValNoAssignments[VNI->id];
  2151. LiveInterval::removeKill(NewVNInfo[LHSValID], VNI->def);
  2152. if (VNI->hasPHIKill())
  2153. NewVNInfo[LHSValID]->setHasPHIKill(true);
  2154. RHS.addKills(NewVNInfo[LHSValID], VNI->kills);
  2155. }
  2156. // Update kill info. Some live ranges are extended due to copy coalescing.
  2157. for (DenseMap<VNInfo*, VNInfo*>::iterator I = RHSValsDefinedFromLHS.begin(),
  2158. E = RHSValsDefinedFromLHS.end(); I != E; ++I) {
  2159. VNInfo *VNI = I->first;
  2160. unsigned RHSValID = RHSValNoAssignments[VNI->id];
  2161. LiveInterval::removeKill(NewVNInfo[RHSValID], VNI->def);
  2162. if (VNI->hasPHIKill())
  2163. NewVNInfo[RHSValID]->setHasPHIKill(true);
  2164. LHS.addKills(NewVNInfo[RHSValID], VNI->kills);
  2165. }
  2166. // If we get here, we know that we can coalesce the live ranges. Ask the
  2167. // intervals to coalesce themselves now.
  2168. if ((RHS.ranges.size() > LHS.ranges.size() &&
  2169. TargetRegisterInfo::isVirtualRegister(LHS.reg)) ||
  2170. TargetRegisterInfo::isPhysicalRegister(RHS.reg)) {
  2171. RHS.join(LHS, &RHSValNoAssignments[0], &LHSValNoAssignments[0], NewVNInfo,
  2172. mri_);
  2173. Swapped = true;
  2174. } else {
  2175. LHS.join(RHS, &LHSValNoAssignments[0], &RHSValNoAssignments[0], NewVNInfo,
  2176. mri_);
  2177. Swapped = false;
  2178. }
  2179. return true;
  2180. }
  2181. namespace {
  2182. // DepthMBBCompare - Comparison predicate that sort first based on the loop
  2183. // depth of the basic block (the unsigned), and then on the MBB number.
  2184. struct DepthMBBCompare {
  2185. typedef std::pair<unsigned, MachineBasicBlock*> DepthMBBPair;
  2186. bool operator()(const DepthMBBPair &LHS, const DepthMBBPair &RHS) const {
  2187. if (LHS.first > RHS.first) return true; // Deeper loops first
  2188. return LHS.first == RHS.first &&
  2189. LHS.second->getNumber() < RHS.second->getNumber();
  2190. }
  2191. };
  2192. }
  2193. /// getRepIntervalSize - Returns the size of the interval that represents the
  2194. /// specified register.
  2195. template<class SF>
  2196. unsigned JoinPriorityQueue<SF>::getRepIntervalSize(unsigned Reg) {
  2197. return Rc->getRepIntervalSize(Reg);
  2198. }
  2199. /// CopyRecSort::operator - Join priority queue sorting function.
  2200. ///
  2201. bool CopyRecSort::operator()(CopyRec left, CopyRec right) const {
  2202. // Inner loops first.
  2203. if (left.LoopDepth > right.LoopDepth)
  2204. return false;
  2205. else if (left.LoopDepth == right.LoopDepth)
  2206. if (left.isBackEdge && !right.isBackEdge)
  2207. return false;
  2208. return true;
  2209. }
  2210. void SimpleRegisterCoalescing::CopyCoalesceInMBB(MachineBasicBlock *MBB,
  2211. std::vector<CopyRec> &TryAgain) {
  2212. DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
  2213. std::vector<CopyRec> VirtCopies;
  2214. std::vector<CopyRec> PhysCopies;
  2215. std::vector<CopyRec> ImpDefCopies;
  2216. unsigned LoopDepth = loopInfo->getLoopDepth(MBB);
  2217. for (MachineBasicBlock::iterator MII = MBB->begin(), E = MBB->end();
  2218. MII != E;) {
  2219. MachineInstr *Inst = MII++;
  2220. // If this isn't a copy nor a extract_subreg, we can't join intervals.
  2221. unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
  2222. if (Inst->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
  2223. DstReg = Inst->getOperand(0).getReg();
  2224. SrcReg = Inst->getOperand(1).getReg();
  2225. } else if (Inst->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
  2226. Inst->getOpcode() == TargetInstrInfo::SUBREG_TO_REG) {
  2227. DstReg = Inst->getOperand(0).getReg();
  2228. SrcReg = Inst->getOperand(2).getReg();
  2229. } else if (!tii_->isMoveInstr(*Inst, SrcReg, DstReg, SrcSubIdx, DstSubIdx))
  2230. continue;
  2231. bool SrcIsPhys = TargetRegisterInfo::isPhysicalRegister(SrcReg);
  2232. bool DstIsPhys = TargetRegisterInfo::isPhysicalRegister(DstReg);
  2233. if (NewHeuristic) {
  2234. JoinQueue->push(CopyRec(Inst, LoopDepth, isBackEdgeCopy(Inst, DstReg)));
  2235. } else {
  2236. if (li_->hasInterval(SrcReg) && li_->getInterval(SrcReg).empty())
  2237. ImpDefCopies.push_back(CopyRec(Inst, 0, false));
  2238. else if (SrcIsPhys || DstIsPhys)
  2239. PhysCopies.push_back(CopyRec(Inst, 0, false));
  2240. else
  2241. VirtCopies.push_back(CopyRec(Inst, 0, false));
  2242. }
  2243. }
  2244. if (NewHeuristic)
  2245. return;
  2246. // Try coalescing implicit copies first, followed by copies to / from
  2247. // physical registers, then finally copies from virtual registers to
  2248. // virtual registers.
  2249. for (unsigned i = 0, e = ImpDefCopies.size(); i != e; ++i) {
  2250. CopyRec &TheCopy = ImpDefCopies[i];
  2251. bool Again = false;
  2252. if (!JoinCopy(TheCopy, Again))
  2253. if (Again)
  2254. TryAgain.push_back(TheCopy);
  2255. }
  2256. for (unsigned i = 0, e = PhysCopies.size(); i != e; ++i) {
  2257. CopyRec &TheCopy = PhysCopies[i];
  2258. bool Again = false;
  2259. if (!JoinCopy(TheCopy, Again))
  2260. if (Again)
  2261. TryAgain.push_back(TheCopy);
  2262. }
  2263. for (unsigned i = 0, e = VirtCopies.size(); i != e; ++i) {
  2264. CopyRec &TheCopy = VirtCopies[i];
  2265. bool Again = false;
  2266. if (!JoinCopy(TheCopy, Again))
  2267. if (Again)
  2268. TryAgain.push_back(TheCopy);
  2269. }
  2270. }
  2271. void SimpleRegisterCoalescing::joinIntervals() {
  2272. DOUT << "********** JOINING INTERVALS ***********\n";
  2273. if (NewHeuristic)
  2274. JoinQueue = new JoinPriorityQueue<CopyRecSort>(this);
  2275. std::vector<CopyRec> TryAgainList;
  2276. if (loopInfo->empty()) {
  2277. // If there are no loops in the function, join intervals in function order.
  2278. for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
  2279. I != E; ++I)
  2280. CopyCoalesceInMBB(I, TryAgainList);
  2281. } else {
  2282. // Otherwise, join intervals in inner loops before other intervals.
  2283. // Unfortunately we can't just iterate over loop hierarchy here because
  2284. // there may be more MBB's than BB's. Collect MBB's for sorting.
  2285. // Join intervals in the function prolog first. We want to join physical
  2286. // registers with virtual registers before the intervals got too long.
  2287. std::vector<std::pair<unsigned, MachineBasicBlock*> > MBBs;
  2288. for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();I != E;++I){
  2289. MachineBasicBlock *MBB = I;
  2290. MBBs.push_back(std::make_pair(loopInfo->getLoopDepth(MBB), I));
  2291. }
  2292. // Sort by loop depth.
  2293. std::sort(MBBs.begin(), MBBs.end(), DepthMBBCompare());
  2294. // Finally, join intervals in loop nest order.
  2295. for (unsigned i = 0, e = MBBs.size(); i != e; ++i)
  2296. CopyCoalesceInMBB(MBBs[i].second, TryAgainList);
  2297. }
  2298. // Joining intervals can allow other intervals to be joined. Iteratively join
  2299. // until we make no progress.
  2300. if (NewHeuristic) {
  2301. SmallVector<CopyRec, 16> TryAgain;
  2302. bool ProgressMade = true;
  2303. while (ProgressMade) {
  2304. ProgressMade = false;
  2305. while (!JoinQueue->empty()) {
  2306. CopyRec R = JoinQueue->pop();
  2307. bool Again = false;
  2308. bool Success = JoinCopy(R, Again);
  2309. if (Success)
  2310. ProgressMade = true;
  2311. else if (Again)
  2312. TryAgain.push_back(R);
  2313. }
  2314. if (ProgressMade) {
  2315. while (!TryAgain.empty()) {
  2316. JoinQueue->push(TryAgain.back());
  2317. TryAgain.pop_back();
  2318. }
  2319. }
  2320. }
  2321. } else {
  2322. bool ProgressMade = true;
  2323. while (ProgressMade) {
  2324. ProgressMade = false;
  2325. for (unsigned i = 0, e = TryAgainList.size(); i != e; ++i) {
  2326. CopyRec &TheCopy = TryAgainList[i];
  2327. if (TheCopy.MI) {
  2328. bool Again = false;
  2329. bool Success = JoinCopy(TheCopy, Again);
  2330. if (Success || !Again) {
  2331. TheCopy.MI = 0; // Mark this one as done.
  2332. ProgressMade = true;
  2333. }
  2334. }
  2335. }
  2336. }
  2337. }
  2338. if (NewHeuristic)
  2339. delete JoinQueue;
  2340. }
  2341. /// Return true if the two specified registers belong to different register
  2342. /// classes. The registers may be either phys or virt regs.
  2343. bool
  2344. SimpleRegisterCoalescing::differingRegisterClasses(unsigned RegA,
  2345. unsigned RegB) const {
  2346. // Get the register classes for the first reg.
  2347. if (TargetRegisterInfo::isPhysicalRegister(RegA)) {
  2348. assert(TargetRegisterInfo::isVirtualRegister(RegB) &&
  2349. "Shouldn't consider two physregs!");
  2350. return !mri_->getRegClass(RegB)->contains(RegA);
  2351. }
  2352. // Compare against the regclass for the second reg.
  2353. const TargetRegisterClass *RegClassA = mri_->getRegClass(RegA);
  2354. if (TargetRegisterInfo::isVirtualRegister(RegB)) {
  2355. const TargetRegisterClass *RegClassB = mri_->getRegClass(RegB);
  2356. return RegClassA != RegClassB;
  2357. }
  2358. return !RegClassA->contains(RegB);
  2359. }
  2360. /// lastRegisterUse - Returns the last use of the specific register between
  2361. /// cycles Start and End or NULL if there are no uses.
  2362. MachineOperand *
  2363. SimpleRegisterCoalescing::lastRegisterUse(unsigned Start, unsigned End,
  2364. unsigned Reg, unsigned &UseIdx) const{
  2365. UseIdx = 0;
  2366. if (TargetRegisterInfo::isVirtualRegister(Reg)) {
  2367. MachineOperand *LastUse = NULL;
  2368. for (MachineRegisterInfo::use_iterator I = mri_->use_begin(Reg),
  2369. E = mri_->use_end(); I != E; ++I) {
  2370. MachineOperand &Use = I.getOperand();
  2371. MachineInstr *UseMI = Use.getParent();
  2372. unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
  2373. if (tii_->isMoveInstr(*UseMI, SrcReg, DstReg, SrcSubIdx, DstSubIdx) &&
  2374. SrcReg == DstReg)
  2375. // Ignore identity copies.
  2376. continue;
  2377. unsigned Idx = li_->getInstructionIndex(UseMI);
  2378. if (Idx >= Start && Idx < End && Idx >= UseIdx) {
  2379. LastUse = &Use;
  2380. UseIdx = li_->getUseIndex(Idx);
  2381. }
  2382. }
  2383. return LastUse;
  2384. }
  2385. int e = (End-1) / InstrSlots::NUM * InstrSlots::NUM;
  2386. int s = Start;
  2387. while (e >= s) {
  2388. // Skip deleted instructions
  2389. MachineInstr *MI = li_->getInstructionFromIndex(e);
  2390. while ((e - InstrSlots::NUM) >= s && !MI) {
  2391. e -= InstrSlots::NUM;
  2392. MI = li_->getInstructionFromIndex(e);
  2393. }
  2394. if (e < s || MI == NULL)
  2395. return NULL;
  2396. // Ignore identity copies.
  2397. unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
  2398. if (!(tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubIdx, DstSubIdx) &&
  2399. SrcReg == DstReg))
  2400. for (unsigned i = 0, NumOps = MI->getNumOperands(); i != NumOps; ++i) {
  2401. MachineOperand &Use = MI->getOperand(i);
  2402. if (Use.isReg() && Use.isUse() && Use.getReg() &&
  2403. tri_->regsOverlap(Use.getReg(), Reg)) {
  2404. UseIdx = li_->getUseIndex(e);
  2405. return &Use;
  2406. }
  2407. }
  2408. e -= InstrSlots::NUM;
  2409. }
  2410. return NULL;
  2411. }
  2412. void SimpleRegisterCoalescing::printRegName(unsigned reg) const {
  2413. if (TargetRegisterInfo::isPhysicalRegister(reg))
  2414. cerr << tri_->getName(reg);
  2415. else
  2416. cerr << "%reg" << reg;
  2417. }
  2418. void SimpleRegisterCoalescing::releaseMemory() {
  2419. JoinedCopies.clear();
  2420. ReMatCopies.clear();
  2421. ReMatDefs.clear();
  2422. }
  2423. static bool isZeroLengthInterval(LiveInterval *li) {
  2424. for (LiveInterval::Ranges::const_iterator
  2425. i = li->ranges.begin(), e = li->ranges.end(); i != e; ++i)
  2426. if (i->end - i->start > LiveInterval::InstrSlots::NUM)
  2427. return false;
  2428. return true;
  2429. }
  2430. /// TurnCopyIntoImpDef - If source of the specified copy is an implicit def,
  2431. /// turn the copy into an implicit def.
  2432. bool
  2433. SimpleRegisterCoalescing::TurnCopyIntoImpDef(MachineBasicBlock::iterator &I,
  2434. MachineBasicBlock *MBB,
  2435. unsigned DstReg, unsigned SrcReg) {
  2436. MachineInstr *CopyMI = &*I;
  2437. unsigned CopyIdx = li_->getDefIndex(li_->getInstructionIndex(CopyMI));
  2438. if (!li_->hasInterval(SrcReg))
  2439. return false;
  2440. LiveInterval &SrcInt = li_->getInterval(SrcReg);
  2441. if (!SrcInt.empty())
  2442. return false;
  2443. if (!li_->hasInterval(DstReg))
  2444. return false;
  2445. LiveInterval &DstInt = li_->getInterval(DstReg);
  2446. const LiveRange *DstLR = DstInt.getLiveRangeContaining(CopyIdx);
  2447. // If the valno extends beyond this basic block, then it's not safe to delete
  2448. // the val# or else livein information won't be correct.
  2449. MachineBasicBlock *EndMBB = li_->getMBBFromIndex(DstLR->end);
  2450. if (EndMBB != MBB)
  2451. return false;
  2452. DstInt.removeValNo(DstLR->valno);
  2453. CopyMI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
  2454. for (int i = CopyMI->getNumOperands() - 1, e = 0; i > e; --i)
  2455. CopyMI->RemoveOperand(i);
  2456. CopyMI->getOperand(0).setIsUndef();
  2457. bool NoUse = mri_->use_empty(SrcReg);
  2458. if (NoUse) {
  2459. for (MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(SrcReg),
  2460. RE = mri_->reg_end(); RI != RE; ) {
  2461. assert(RI.getOperand().isDef());
  2462. MachineInstr *DefMI = &*RI;
  2463. ++RI;
  2464. // The implicit_def source has no other uses, delete it.
  2465. assert(DefMI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF);
  2466. li_->RemoveMachineInstrFromMaps(DefMI);
  2467. DefMI->eraseFromParent();
  2468. }
  2469. }
  2470. // Mark uses of implicit_def isUndef.
  2471. for (MachineRegisterInfo::use_iterator RI = mri_->use_begin(DstReg),
  2472. RE = mri_->use_end(); RI != RE; ++RI) {
  2473. assert((*RI).getParent() == MBB);
  2474. RI.getOperand().setIsUndef();
  2475. }
  2476. ++I;
  2477. return true;
  2478. }
  2479. bool SimpleRegisterCoalescing::runOnMachineFunction(MachineFunction &fn) {
  2480. mf_ = &fn;
  2481. mri_ = &fn.getRegInfo();
  2482. tm_ = &fn.getTarget();
  2483. tri_ = tm_->getRegisterInfo();
  2484. tii_ = tm_->getInstrInfo();
  2485. li_ = &getAnalysis<LiveIntervals>();
  2486. loopInfo = &getAnalysis<MachineLoopInfo>();
  2487. DOUT << "********** SIMPLE REGISTER COALESCING **********\n"
  2488. << "********** Function: "
  2489. << ((Value*)mf_->getFunction())->getName() << '\n';
  2490. allocatableRegs_ = tri_->getAllocatableSet(fn);
  2491. for (TargetRegisterInfo::regclass_iterator I = tri_->regclass_begin(),
  2492. E = tri_->regclass_end(); I != E; ++I)
  2493. allocatableRCRegs_.insert(std::make_pair(*I,
  2494. tri_->getAllocatableSet(fn, *I)));
  2495. // Join (coalesce) intervals if requested.
  2496. if (EnableJoining) {
  2497. joinIntervals();
  2498. DEBUG({
  2499. DOUT << "********** INTERVALS POST JOINING **********\n";
  2500. for (LiveIntervals::iterator I = li_->begin(), E = li_->end(); I != E; ++I){
  2501. I->second->print(DOUT, tri_);
  2502. DOUT << "\n";
  2503. }
  2504. });
  2505. }
  2506. // Perform a final pass over the instructions and compute spill weights
  2507. // and remove identity moves.
  2508. SmallVector<unsigned, 4> DeadDefs;
  2509. for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
  2510. mbbi != mbbe; ++mbbi) {
  2511. MachineBasicBlock* mbb = mbbi;
  2512. unsigned loopDepth = loopInfo->getLoopDepth(mbb);
  2513. for (MachineBasicBlock::iterator mii = mbb->begin(), mie = mbb->end();
  2514. mii != mie; ) {
  2515. MachineInstr *MI = mii;
  2516. unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
  2517. if (JoinedCopies.count(MI)) {
  2518. // Delete all coalesced copies.
  2519. if (!tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubIdx, DstSubIdx)) {
  2520. assert((MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
  2521. MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
  2522. MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG) &&
  2523. "Unrecognized copy instruction");
  2524. DstReg = MI->getOperand(0).getReg();
  2525. }
  2526. if (MI->registerDefIsDead(DstReg)) {
  2527. LiveInterval &li = li_->getInterval(DstReg);
  2528. if (!ShortenDeadCopySrcLiveRange(li, MI))
  2529. ShortenDeadCopyLiveRange(li, MI);
  2530. }
  2531. li_->RemoveMachineInstrFromMaps(MI);
  2532. mii = mbbi->erase(mii);
  2533. ++numPeep;
  2534. continue;
  2535. }
  2536. // Now check if this is a remat'ed def instruction which is now dead.
  2537. if (ReMatDefs.count(MI)) {
  2538. bool isDead = true;
  2539. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  2540. const MachineOperand &MO = MI->getOperand(i);
  2541. if (!MO.isReg())
  2542. continue;
  2543. unsigned Reg = MO.getReg();
  2544. if (!Reg)
  2545. continue;
  2546. if (TargetRegisterInfo::isVirtualRegister(Reg))
  2547. DeadDefs.push_back(Reg);
  2548. if (MO.isDead())
  2549. continue;
  2550. if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
  2551. !mri_->use_empty(Reg)) {
  2552. isDead = false;
  2553. break;
  2554. }
  2555. }
  2556. if (isDead) {
  2557. while (!DeadDefs.empty()) {
  2558. unsigned DeadDef = DeadDefs.back();
  2559. DeadDefs.pop_back();
  2560. RemoveDeadDef(li_->getInterval(DeadDef), MI);
  2561. }
  2562. li_->RemoveMachineInstrFromMaps(mii);
  2563. mii = mbbi->erase(mii);
  2564. continue;
  2565. } else
  2566. DeadDefs.clear();
  2567. }
  2568. // If the move will be an identity move delete it
  2569. bool isMove= tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubIdx, DstSubIdx);
  2570. if (isMove && SrcReg == DstReg) {
  2571. if (li_->hasInterval(SrcReg)) {
  2572. LiveInterval &RegInt = li_->getInterval(SrcReg);
  2573. // If def of this move instruction is dead, remove its live range
  2574. // from the dstination register's live interval.
  2575. if (MI->registerDefIsDead(DstReg)) {
  2576. if (!ShortenDeadCopySrcLiveRange(RegInt, MI))
  2577. ShortenDeadCopyLiveRange(RegInt, MI);
  2578. }
  2579. }
  2580. li_->RemoveMachineInstrFromMaps(MI);
  2581. mii = mbbi->erase(mii);
  2582. ++numPeep;
  2583. } else if (!isMove || !TurnCopyIntoImpDef(mii, mbb, DstReg, SrcReg)) {
  2584. SmallSet<unsigned, 4> UniqueUses;
  2585. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  2586. const MachineOperand &mop = MI->getOperand(i);
  2587. if (mop.isReg() && mop.getReg() &&
  2588. TargetRegisterInfo::isVirtualRegister(mop.getReg())) {
  2589. unsigned reg = mop.getReg();
  2590. // Multiple uses of reg by the same instruction. It should not
  2591. // contribute to spill weight again.
  2592. if (UniqueUses.count(reg) != 0)
  2593. continue;
  2594. LiveInterval &RegInt = li_->getInterval(reg);
  2595. RegInt.weight +=
  2596. li_->getSpillWeight(mop.isDef(), mop.isUse(), loopDepth);
  2597. UniqueUses.insert(reg);
  2598. }
  2599. }
  2600. ++mii;
  2601. }
  2602. }
  2603. }
  2604. for (LiveIntervals::iterator I = li_->begin(), E = li_->end(); I != E; ++I) {
  2605. LiveInterval &LI = *I->second;
  2606. if (TargetRegisterInfo::isVirtualRegister(LI.reg)) {
  2607. // If the live interval length is essentially zero, i.e. in every live
  2608. // range the use follows def immediately, it doesn't make sense to spill
  2609. // it and hope it will be easier to allocate for this li.
  2610. if (isZeroLengthInterval(&LI))
  2611. LI.weight = HUGE_VALF;
  2612. else {
  2613. bool isLoad = false;
  2614. SmallVector<LiveInterval*, 4> SpillIs;
  2615. if (li_->isReMaterializable(LI, SpillIs, isLoad)) {
  2616. // If all of the definitions of the interval are re-materializable,
  2617. // it is a preferred candidate for spilling. If non of the defs are
  2618. // loads, then it's potentially very cheap to re-materialize.
  2619. // FIXME: this gets much more complicated once we support non-trivial
  2620. // re-materialization.
  2621. if (isLoad)
  2622. LI.weight *= 0.9F;
  2623. else
  2624. LI.weight *= 0.5F;
  2625. }
  2626. }
  2627. // Slightly prefer live interval that has been assigned a preferred reg.
  2628. std::pair<unsigned, unsigned> Hint = mri_->getRegAllocationHint(LI.reg);
  2629. if (Hint.first || Hint.second)
  2630. LI.weight *= 1.01F;
  2631. // Divide the weight of the interval by its size. This encourages
  2632. // spilling of intervals that are large and have few uses, and
  2633. // discourages spilling of small intervals with many uses.
  2634. LI.weight /= li_->getApproximateInstructionCount(LI) * InstrSlots::NUM;
  2635. }
  2636. }
  2637. DEBUG(dump());
  2638. return true;
  2639. }
  2640. /// print - Implement the dump method.
  2641. void SimpleRegisterCoalescing::print(std::ostream &O, const Module* m) const {
  2642. li_->print(O, m);
  2643. }
  2644. RegisterCoalescer* llvm::createSimpleRegisterCoalescer() {
  2645. return new SimpleRegisterCoalescing();
  2646. }
  2647. // Make sure that anything that uses RegisterCoalescer pulls in this file...
  2648. DEFINING_FILE_FOR(SimpleRegisterCoalescing)