TwoAddressInstructionPass.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  1. //===-- TwoAddressInstructionPass.cpp - Two-Address instruction pass ------===//
  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 the TwoAddress instruction pass which is used
  11. // by most register allocators. Two-Address instructions are rewritten
  12. // from:
  13. //
  14. // A = B op C
  15. //
  16. // to:
  17. //
  18. // A = B
  19. // A op= C
  20. //
  21. // Note that if a register allocator chooses to use this pass, that it
  22. // has to be capable of handling the non-SSA nature of these rewritten
  23. // virtual registers.
  24. //
  25. // It is also worth noting that the duplicate operand of the two
  26. // address instruction is removed.
  27. //
  28. //===----------------------------------------------------------------------===//
  29. #define DEBUG_TYPE "twoaddrinstr"
  30. #include "llvm/CodeGen/Passes.h"
  31. #include "llvm/Function.h"
  32. #include "llvm/CodeGen/LiveVariables.h"
  33. #include "llvm/CodeGen/MachineFunctionPass.h"
  34. #include "llvm/CodeGen/MachineInstr.h"
  35. #include "llvm/CodeGen/MachineInstrBuilder.h"
  36. #include "llvm/CodeGen/MachineRegisterInfo.h"
  37. #include "llvm/Analysis/AliasAnalysis.h"
  38. #include "llvm/Target/TargetRegisterInfo.h"
  39. #include "llvm/Target/TargetInstrInfo.h"
  40. #include "llvm/Target/TargetMachine.h"
  41. #include "llvm/Target/TargetOptions.h"
  42. #include "llvm/Support/Debug.h"
  43. #include "llvm/Support/ErrorHandling.h"
  44. #include "llvm/ADT/BitVector.h"
  45. #include "llvm/ADT/DenseMap.h"
  46. #include "llvm/ADT/SmallSet.h"
  47. #include "llvm/ADT/Statistic.h"
  48. #include "llvm/ADT/STLExtras.h"
  49. using namespace llvm;
  50. STATISTIC(NumTwoAddressInstrs, "Number of two-address instructions");
  51. STATISTIC(NumCommuted , "Number of instructions commuted to coalesce");
  52. STATISTIC(NumAggrCommuted , "Number of instructions aggressively commuted");
  53. STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address");
  54. STATISTIC(Num3AddrSunk, "Number of 3-address instructions sunk");
  55. STATISTIC(NumReMats, "Number of instructions re-materialized");
  56. STATISTIC(NumDeletes, "Number of dead instructions deleted");
  57. namespace {
  58. class TwoAddressInstructionPass : public MachineFunctionPass {
  59. const TargetInstrInfo *TII;
  60. const TargetRegisterInfo *TRI;
  61. MachineRegisterInfo *MRI;
  62. LiveVariables *LV;
  63. AliasAnalysis *AA;
  64. // DistanceMap - Keep track the distance of a MI from the start of the
  65. // current basic block.
  66. DenseMap<MachineInstr*, unsigned> DistanceMap;
  67. // SrcRegMap - A map from virtual registers to physical registers which
  68. // are likely targets to be coalesced to due to copies from physical
  69. // registers to virtual registers. e.g. v1024 = move r0.
  70. DenseMap<unsigned, unsigned> SrcRegMap;
  71. // DstRegMap - A map from virtual registers to physical registers which
  72. // are likely targets to be coalesced to due to copies to physical
  73. // registers from virtual registers. e.g. r1 = move v1024.
  74. DenseMap<unsigned, unsigned> DstRegMap;
  75. /// RegSequences - Keep track the list of REG_SEQUENCE instructions seen
  76. /// during the initial walk of the machine function.
  77. SmallVector<MachineInstr*, 16> RegSequences;
  78. bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI,
  79. unsigned Reg,
  80. MachineBasicBlock::iterator OldPos);
  81. bool isProfitableToReMat(unsigned Reg, const TargetRegisterClass *RC,
  82. MachineInstr *MI, MachineInstr *DefMI,
  83. MachineBasicBlock *MBB, unsigned Loc);
  84. bool NoUseAfterLastDef(unsigned Reg, MachineBasicBlock *MBB, unsigned Dist,
  85. unsigned &LastDef);
  86. MachineInstr *FindLastUseInMBB(unsigned Reg, MachineBasicBlock *MBB,
  87. unsigned Dist);
  88. bool isProfitableToCommute(unsigned regB, unsigned regC,
  89. MachineInstr *MI, MachineBasicBlock *MBB,
  90. unsigned Dist);
  91. bool CommuteInstruction(MachineBasicBlock::iterator &mi,
  92. MachineFunction::iterator &mbbi,
  93. unsigned RegB, unsigned RegC, unsigned Dist);
  94. bool isProfitableToConv3Addr(unsigned RegA);
  95. bool ConvertInstTo3Addr(MachineBasicBlock::iterator &mi,
  96. MachineBasicBlock::iterator &nmi,
  97. MachineFunction::iterator &mbbi,
  98. unsigned RegB, unsigned Dist);
  99. typedef std::pair<std::pair<unsigned, bool>, MachineInstr*> NewKill;
  100. bool canUpdateDeletedKills(SmallVector<unsigned, 4> &Kills,
  101. SmallVector<NewKill, 4> &NewKills,
  102. MachineBasicBlock *MBB, unsigned Dist);
  103. bool DeleteUnusedInstr(MachineBasicBlock::iterator &mi,
  104. MachineBasicBlock::iterator &nmi,
  105. MachineFunction::iterator &mbbi, unsigned Dist);
  106. bool TryInstructionTransform(MachineBasicBlock::iterator &mi,
  107. MachineBasicBlock::iterator &nmi,
  108. MachineFunction::iterator &mbbi,
  109. unsigned SrcIdx, unsigned DstIdx,
  110. unsigned Dist);
  111. void ProcessCopy(MachineInstr *MI, MachineBasicBlock *MBB,
  112. SmallPtrSet<MachineInstr*, 8> &Processed);
  113. void CoalesceExtSubRegs(SmallVector<unsigned,4> &Srcs, unsigned DstReg);
  114. /// EliminateRegSequences - Eliminate REG_SEQUENCE instructions as part
  115. /// of the de-ssa process. This replaces sources of REG_SEQUENCE as
  116. /// sub-register references of the register defined by REG_SEQUENCE.
  117. bool EliminateRegSequences();
  118. public:
  119. static char ID; // Pass identification, replacement for typeid
  120. TwoAddressInstructionPass() : MachineFunctionPass(&ID) {}
  121. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  122. AU.setPreservesCFG();
  123. AU.addRequired<AliasAnalysis>();
  124. AU.addPreserved<LiveVariables>();
  125. AU.addPreservedID(MachineLoopInfoID);
  126. AU.addPreservedID(MachineDominatorsID);
  127. if (StrongPHIElim)
  128. AU.addPreservedID(StrongPHIEliminationID);
  129. else
  130. AU.addPreservedID(PHIEliminationID);
  131. MachineFunctionPass::getAnalysisUsage(AU);
  132. }
  133. /// runOnMachineFunction - Pass entry point.
  134. bool runOnMachineFunction(MachineFunction&);
  135. };
  136. }
  137. char TwoAddressInstructionPass::ID = 0;
  138. static RegisterPass<TwoAddressInstructionPass>
  139. X("twoaddressinstruction", "Two-Address instruction pass");
  140. const PassInfo *const llvm::TwoAddressInstructionPassID = &X;
  141. /// Sink3AddrInstruction - A two-address instruction has been converted to a
  142. /// three-address instruction to avoid clobbering a register. Try to sink it
  143. /// past the instruction that would kill the above mentioned register to reduce
  144. /// register pressure.
  145. bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
  146. MachineInstr *MI, unsigned SavedReg,
  147. MachineBasicBlock::iterator OldPos) {
  148. // Check if it's safe to move this instruction.
  149. bool SeenStore = true; // Be conservative.
  150. if (!MI->isSafeToMove(TII, AA, SeenStore))
  151. return false;
  152. unsigned DefReg = 0;
  153. SmallSet<unsigned, 4> UseRegs;
  154. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  155. const MachineOperand &MO = MI->getOperand(i);
  156. if (!MO.isReg())
  157. continue;
  158. unsigned MOReg = MO.getReg();
  159. if (!MOReg)
  160. continue;
  161. if (MO.isUse() && MOReg != SavedReg)
  162. UseRegs.insert(MO.getReg());
  163. if (!MO.isDef())
  164. continue;
  165. if (MO.isImplicit())
  166. // Don't try to move it if it implicitly defines a register.
  167. return false;
  168. if (DefReg)
  169. // For now, don't move any instructions that define multiple registers.
  170. return false;
  171. DefReg = MO.getReg();
  172. }
  173. // Find the instruction that kills SavedReg.
  174. MachineInstr *KillMI = NULL;
  175. for (MachineRegisterInfo::use_nodbg_iterator
  176. UI = MRI->use_nodbg_begin(SavedReg),
  177. UE = MRI->use_nodbg_end(); UI != UE; ++UI) {
  178. MachineOperand &UseMO = UI.getOperand();
  179. if (!UseMO.isKill())
  180. continue;
  181. KillMI = UseMO.getParent();
  182. break;
  183. }
  184. if (!KillMI || KillMI->getParent() != MBB || KillMI == MI)
  185. return false;
  186. // If any of the definitions are used by another instruction between the
  187. // position and the kill use, then it's not safe to sink it.
  188. //
  189. // FIXME: This can be sped up if there is an easy way to query whether an
  190. // instruction is before or after another instruction. Then we can use
  191. // MachineRegisterInfo def / use instead.
  192. MachineOperand *KillMO = NULL;
  193. MachineBasicBlock::iterator KillPos = KillMI;
  194. ++KillPos;
  195. unsigned NumVisited = 0;
  196. for (MachineBasicBlock::iterator I = llvm::next(OldPos); I != KillPos; ++I) {
  197. MachineInstr *OtherMI = I;
  198. // DBG_VALUE cannot be counted against the limit.
  199. if (OtherMI->isDebugValue())
  200. continue;
  201. if (NumVisited > 30) // FIXME: Arbitrary limit to reduce compile time cost.
  202. return false;
  203. ++NumVisited;
  204. for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
  205. MachineOperand &MO = OtherMI->getOperand(i);
  206. if (!MO.isReg())
  207. continue;
  208. unsigned MOReg = MO.getReg();
  209. if (!MOReg)
  210. continue;
  211. if (DefReg == MOReg)
  212. return false;
  213. if (MO.isKill()) {
  214. if (OtherMI == KillMI && MOReg == SavedReg)
  215. // Save the operand that kills the register. We want to unset the kill
  216. // marker if we can sink MI past it.
  217. KillMO = &MO;
  218. else if (UseRegs.count(MOReg))
  219. // One of the uses is killed before the destination.
  220. return false;
  221. }
  222. }
  223. }
  224. // Update kill and LV information.
  225. KillMO->setIsKill(false);
  226. KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI);
  227. KillMO->setIsKill(true);
  228. if (LV)
  229. LV->replaceKillInstruction(SavedReg, KillMI, MI);
  230. // Move instruction to its destination.
  231. MBB->remove(MI);
  232. MBB->insert(KillPos, MI);
  233. ++Num3AddrSunk;
  234. return true;
  235. }
  236. /// isTwoAddrUse - Return true if the specified MI is using the specified
  237. /// register as a two-address operand.
  238. static bool isTwoAddrUse(MachineInstr *UseMI, unsigned Reg) {
  239. const TargetInstrDesc &TID = UseMI->getDesc();
  240. for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
  241. MachineOperand &MO = UseMI->getOperand(i);
  242. if (MO.isReg() && MO.getReg() == Reg &&
  243. (MO.isDef() || UseMI->isRegTiedToDefOperand(i)))
  244. // Earlier use is a two-address one.
  245. return true;
  246. }
  247. return false;
  248. }
  249. /// isProfitableToReMat - Return true if the heuristics determines it is likely
  250. /// to be profitable to re-materialize the definition of Reg rather than copy
  251. /// the register.
  252. bool
  253. TwoAddressInstructionPass::isProfitableToReMat(unsigned Reg,
  254. const TargetRegisterClass *RC,
  255. MachineInstr *MI, MachineInstr *DefMI,
  256. MachineBasicBlock *MBB, unsigned Loc) {
  257. bool OtherUse = false;
  258. for (MachineRegisterInfo::use_nodbg_iterator UI = MRI->use_nodbg_begin(Reg),
  259. UE = MRI->use_nodbg_end(); UI != UE; ++UI) {
  260. MachineOperand &UseMO = UI.getOperand();
  261. MachineInstr *UseMI = UseMO.getParent();
  262. MachineBasicBlock *UseMBB = UseMI->getParent();
  263. if (UseMBB == MBB) {
  264. DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI);
  265. if (DI != DistanceMap.end() && DI->second == Loc)
  266. continue; // Current use.
  267. OtherUse = true;
  268. // There is at least one other use in the MBB that will clobber the
  269. // register.
  270. if (isTwoAddrUse(UseMI, Reg))
  271. return true;
  272. }
  273. }
  274. // If other uses in MBB are not two-address uses, then don't remat.
  275. if (OtherUse)
  276. return false;
  277. // No other uses in the same block, remat if it's defined in the same
  278. // block so it does not unnecessarily extend the live range.
  279. return MBB == DefMI->getParent();
  280. }
  281. /// NoUseAfterLastDef - Return true if there are no intervening uses between the
  282. /// last instruction in the MBB that defines the specified register and the
  283. /// two-address instruction which is being processed. It also returns the last
  284. /// def location by reference
  285. bool TwoAddressInstructionPass::NoUseAfterLastDef(unsigned Reg,
  286. MachineBasicBlock *MBB, unsigned Dist,
  287. unsigned &LastDef) {
  288. LastDef = 0;
  289. unsigned LastUse = Dist;
  290. for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(Reg),
  291. E = MRI->reg_end(); I != E; ++I) {
  292. MachineOperand &MO = I.getOperand();
  293. MachineInstr *MI = MO.getParent();
  294. if (MI->getParent() != MBB || MI->isDebugValue())
  295. continue;
  296. DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI);
  297. if (DI == DistanceMap.end())
  298. continue;
  299. if (MO.isUse() && DI->second < LastUse)
  300. LastUse = DI->second;
  301. if (MO.isDef() && DI->second > LastDef)
  302. LastDef = DI->second;
  303. }
  304. return !(LastUse > LastDef && LastUse < Dist);
  305. }
  306. MachineInstr *TwoAddressInstructionPass::FindLastUseInMBB(unsigned Reg,
  307. MachineBasicBlock *MBB,
  308. unsigned Dist) {
  309. unsigned LastUseDist = 0;
  310. MachineInstr *LastUse = 0;
  311. for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(Reg),
  312. E = MRI->reg_end(); I != E; ++I) {
  313. MachineOperand &MO = I.getOperand();
  314. MachineInstr *MI = MO.getParent();
  315. if (MI->getParent() != MBB || MI->isDebugValue())
  316. continue;
  317. DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI);
  318. if (DI == DistanceMap.end())
  319. continue;
  320. if (DI->second >= Dist)
  321. continue;
  322. if (MO.isUse() && DI->second > LastUseDist) {
  323. LastUse = DI->first;
  324. LastUseDist = DI->second;
  325. }
  326. }
  327. return LastUse;
  328. }
  329. /// isCopyToReg - Return true if the specified MI is a copy instruction or
  330. /// a extract_subreg instruction. It also returns the source and destination
  331. /// registers and whether they are physical registers by reference.
  332. static bool isCopyToReg(MachineInstr &MI, const TargetInstrInfo *TII,
  333. unsigned &SrcReg, unsigned &DstReg,
  334. bool &IsSrcPhys, bool &IsDstPhys) {
  335. SrcReg = 0;
  336. DstReg = 0;
  337. unsigned SrcSubIdx, DstSubIdx;
  338. if (!TII->isMoveInstr(MI, SrcReg, DstReg, SrcSubIdx, DstSubIdx)) {
  339. if (MI.isExtractSubreg()) {
  340. DstReg = MI.getOperand(0).getReg();
  341. SrcReg = MI.getOperand(1).getReg();
  342. } else if (MI.isInsertSubreg()) {
  343. DstReg = MI.getOperand(0).getReg();
  344. SrcReg = MI.getOperand(2).getReg();
  345. } else if (MI.isSubregToReg()) {
  346. DstReg = MI.getOperand(0).getReg();
  347. SrcReg = MI.getOperand(2).getReg();
  348. }
  349. }
  350. if (DstReg) {
  351. IsSrcPhys = TargetRegisterInfo::isPhysicalRegister(SrcReg);
  352. IsDstPhys = TargetRegisterInfo::isPhysicalRegister(DstReg);
  353. return true;
  354. }
  355. return false;
  356. }
  357. /// isKilled - Test if the given register value, which is used by the given
  358. /// instruction, is killed by the given instruction. This looks through
  359. /// coalescable copies to see if the original value is potentially not killed.
  360. ///
  361. /// For example, in this code:
  362. ///
  363. /// %reg1034 = copy %reg1024
  364. /// %reg1035 = copy %reg1025<kill>
  365. /// %reg1036 = add %reg1034<kill>, %reg1035<kill>
  366. ///
  367. /// %reg1034 is not considered to be killed, since it is copied from a
  368. /// register which is not killed. Treating it as not killed lets the
  369. /// normal heuristics commute the (two-address) add, which lets
  370. /// coalescing eliminate the extra copy.
  371. ///
  372. static bool isKilled(MachineInstr &MI, unsigned Reg,
  373. const MachineRegisterInfo *MRI,
  374. const TargetInstrInfo *TII) {
  375. MachineInstr *DefMI = &MI;
  376. for (;;) {
  377. if (!DefMI->killsRegister(Reg))
  378. return false;
  379. if (TargetRegisterInfo::isPhysicalRegister(Reg))
  380. return true;
  381. MachineRegisterInfo::def_iterator Begin = MRI->def_begin(Reg);
  382. // If there are multiple defs, we can't do a simple analysis, so just
  383. // go with what the kill flag says.
  384. if (llvm::next(Begin) != MRI->def_end())
  385. return true;
  386. DefMI = &*Begin;
  387. bool IsSrcPhys, IsDstPhys;
  388. unsigned SrcReg, DstReg;
  389. // If the def is something other than a copy, then it isn't going to
  390. // be coalesced, so follow the kill flag.
  391. if (!isCopyToReg(*DefMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
  392. return true;
  393. Reg = SrcReg;
  394. }
  395. }
  396. /// isTwoAddrUse - Return true if the specified MI uses the specified register
  397. /// as a two-address use. If so, return the destination register by reference.
  398. static bool isTwoAddrUse(MachineInstr &MI, unsigned Reg, unsigned &DstReg) {
  399. const TargetInstrDesc &TID = MI.getDesc();
  400. unsigned NumOps = MI.isInlineAsm() ? MI.getNumOperands():TID.getNumOperands();
  401. for (unsigned i = 0; i != NumOps; ++i) {
  402. const MachineOperand &MO = MI.getOperand(i);
  403. if (!MO.isReg() || !MO.isUse() || MO.getReg() != Reg)
  404. continue;
  405. unsigned ti;
  406. if (MI.isRegTiedToDefOperand(i, &ti)) {
  407. DstReg = MI.getOperand(ti).getReg();
  408. return true;
  409. }
  410. }
  411. return false;
  412. }
  413. /// findOnlyInterestingUse - Given a register, if has a single in-basic block
  414. /// use, return the use instruction if it's a copy or a two-address use.
  415. static
  416. MachineInstr *findOnlyInterestingUse(unsigned Reg, MachineBasicBlock *MBB,
  417. MachineRegisterInfo *MRI,
  418. const TargetInstrInfo *TII,
  419. bool &IsCopy,
  420. unsigned &DstReg, bool &IsDstPhys) {
  421. if (!MRI->hasOneNonDBGUse(Reg))
  422. // None or more than one use.
  423. return 0;
  424. MachineInstr &UseMI = *MRI->use_nodbg_begin(Reg);
  425. if (UseMI.getParent() != MBB)
  426. return 0;
  427. unsigned SrcReg;
  428. bool IsSrcPhys;
  429. if (isCopyToReg(UseMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys)) {
  430. IsCopy = true;
  431. return &UseMI;
  432. }
  433. IsDstPhys = false;
  434. if (isTwoAddrUse(UseMI, Reg, DstReg)) {
  435. IsDstPhys = TargetRegisterInfo::isPhysicalRegister(DstReg);
  436. return &UseMI;
  437. }
  438. return 0;
  439. }
  440. /// getMappedReg - Return the physical register the specified virtual register
  441. /// might be mapped to.
  442. static unsigned
  443. getMappedReg(unsigned Reg, DenseMap<unsigned, unsigned> &RegMap) {
  444. while (TargetRegisterInfo::isVirtualRegister(Reg)) {
  445. DenseMap<unsigned, unsigned>::iterator SI = RegMap.find(Reg);
  446. if (SI == RegMap.end())
  447. return 0;
  448. Reg = SI->second;
  449. }
  450. if (TargetRegisterInfo::isPhysicalRegister(Reg))
  451. return Reg;
  452. return 0;
  453. }
  454. /// regsAreCompatible - Return true if the two registers are equal or aliased.
  455. ///
  456. static bool
  457. regsAreCompatible(unsigned RegA, unsigned RegB, const TargetRegisterInfo *TRI) {
  458. if (RegA == RegB)
  459. return true;
  460. if (!RegA || !RegB)
  461. return false;
  462. return TRI->regsOverlap(RegA, RegB);
  463. }
  464. /// isProfitableToReMat - Return true if it's potentially profitable to commute
  465. /// the two-address instruction that's being processed.
  466. bool
  467. TwoAddressInstructionPass::isProfitableToCommute(unsigned regB, unsigned regC,
  468. MachineInstr *MI, MachineBasicBlock *MBB,
  469. unsigned Dist) {
  470. // Determine if it's profitable to commute this two address instruction. In
  471. // general, we want no uses between this instruction and the definition of
  472. // the two-address register.
  473. // e.g.
  474. // %reg1028<def> = EXTRACT_SUBREG %reg1027<kill>, 1
  475. // %reg1029<def> = MOV8rr %reg1028
  476. // %reg1029<def> = SHR8ri %reg1029, 7, %EFLAGS<imp-def,dead>
  477. // insert => %reg1030<def> = MOV8rr %reg1028
  478. // %reg1030<def> = ADD8rr %reg1028<kill>, %reg1029<kill>, %EFLAGS<imp-def,dead>
  479. // In this case, it might not be possible to coalesce the second MOV8rr
  480. // instruction if the first one is coalesced. So it would be profitable to
  481. // commute it:
  482. // %reg1028<def> = EXTRACT_SUBREG %reg1027<kill>, 1
  483. // %reg1029<def> = MOV8rr %reg1028
  484. // %reg1029<def> = SHR8ri %reg1029, 7, %EFLAGS<imp-def,dead>
  485. // insert => %reg1030<def> = MOV8rr %reg1029
  486. // %reg1030<def> = ADD8rr %reg1029<kill>, %reg1028<kill>, %EFLAGS<imp-def,dead>
  487. if (!MI->killsRegister(regC))
  488. return false;
  489. // Ok, we have something like:
  490. // %reg1030<def> = ADD8rr %reg1028<kill>, %reg1029<kill>, %EFLAGS<imp-def,dead>
  491. // let's see if it's worth commuting it.
  492. // Look for situations like this:
  493. // %reg1024<def> = MOV r1
  494. // %reg1025<def> = MOV r0
  495. // %reg1026<def> = ADD %reg1024, %reg1025
  496. // r0 = MOV %reg1026
  497. // Commute the ADD to hopefully eliminate an otherwise unavoidable copy.
  498. unsigned FromRegB = getMappedReg(regB, SrcRegMap);
  499. unsigned FromRegC = getMappedReg(regC, SrcRegMap);
  500. unsigned ToRegB = getMappedReg(regB, DstRegMap);
  501. unsigned ToRegC = getMappedReg(regC, DstRegMap);
  502. if (!regsAreCompatible(FromRegB, ToRegB, TRI) &&
  503. (regsAreCompatible(FromRegB, ToRegC, TRI) ||
  504. regsAreCompatible(FromRegC, ToRegB, TRI)))
  505. return true;
  506. // If there is a use of regC between its last def (could be livein) and this
  507. // instruction, then bail.
  508. unsigned LastDefC = 0;
  509. if (!NoUseAfterLastDef(regC, MBB, Dist, LastDefC))
  510. return false;
  511. // If there is a use of regB between its last def (could be livein) and this
  512. // instruction, then go ahead and make this transformation.
  513. unsigned LastDefB = 0;
  514. if (!NoUseAfterLastDef(regB, MBB, Dist, LastDefB))
  515. return true;
  516. // Since there are no intervening uses for both registers, then commute
  517. // if the def of regC is closer. Its live interval is shorter.
  518. return LastDefB && LastDefC && LastDefC > LastDefB;
  519. }
  520. /// CommuteInstruction - Commute a two-address instruction and update the basic
  521. /// block, distance map, and live variables if needed. Return true if it is
  522. /// successful.
  523. bool
  524. TwoAddressInstructionPass::CommuteInstruction(MachineBasicBlock::iterator &mi,
  525. MachineFunction::iterator &mbbi,
  526. unsigned RegB, unsigned RegC, unsigned Dist) {
  527. MachineInstr *MI = mi;
  528. DEBUG(dbgs() << "2addr: COMMUTING : " << *MI);
  529. MachineInstr *NewMI = TII->commuteInstruction(MI);
  530. if (NewMI == 0) {
  531. DEBUG(dbgs() << "2addr: COMMUTING FAILED!\n");
  532. return false;
  533. }
  534. DEBUG(dbgs() << "2addr: COMMUTED TO: " << *NewMI);
  535. // If the instruction changed to commute it, update livevar.
  536. if (NewMI != MI) {
  537. if (LV)
  538. // Update live variables
  539. LV->replaceKillInstruction(RegC, MI, NewMI);
  540. mbbi->insert(mi, NewMI); // Insert the new inst
  541. mbbi->erase(mi); // Nuke the old inst.
  542. mi = NewMI;
  543. DistanceMap.insert(std::make_pair(NewMI, Dist));
  544. }
  545. // Update source register map.
  546. unsigned FromRegC = getMappedReg(RegC, SrcRegMap);
  547. if (FromRegC) {
  548. unsigned RegA = MI->getOperand(0).getReg();
  549. SrcRegMap[RegA] = FromRegC;
  550. }
  551. return true;
  552. }
  553. /// isProfitableToConv3Addr - Return true if it is profitable to convert the
  554. /// given 2-address instruction to a 3-address one.
  555. bool
  556. TwoAddressInstructionPass::isProfitableToConv3Addr(unsigned RegA) {
  557. // Look for situations like this:
  558. // %reg1024<def> = MOV r1
  559. // %reg1025<def> = MOV r0
  560. // %reg1026<def> = ADD %reg1024, %reg1025
  561. // r2 = MOV %reg1026
  562. // Turn ADD into a 3-address instruction to avoid a copy.
  563. unsigned FromRegA = getMappedReg(RegA, SrcRegMap);
  564. unsigned ToRegA = getMappedReg(RegA, DstRegMap);
  565. return (FromRegA && ToRegA && !regsAreCompatible(FromRegA, ToRegA, TRI));
  566. }
  567. /// ConvertInstTo3Addr - Convert the specified two-address instruction into a
  568. /// three address one. Return true if this transformation was successful.
  569. bool
  570. TwoAddressInstructionPass::ConvertInstTo3Addr(MachineBasicBlock::iterator &mi,
  571. MachineBasicBlock::iterator &nmi,
  572. MachineFunction::iterator &mbbi,
  573. unsigned RegB, unsigned Dist) {
  574. MachineInstr *NewMI = TII->convertToThreeAddress(mbbi, mi, LV);
  575. if (NewMI) {
  576. DEBUG(dbgs() << "2addr: CONVERTING 2-ADDR: " << *mi);
  577. DEBUG(dbgs() << "2addr: TO 3-ADDR: " << *NewMI);
  578. bool Sunk = false;
  579. if (NewMI->findRegisterUseOperand(RegB, false, TRI))
  580. // FIXME: Temporary workaround. If the new instruction doesn't
  581. // uses RegB, convertToThreeAddress must have created more
  582. // then one instruction.
  583. Sunk = Sink3AddrInstruction(mbbi, NewMI, RegB, mi);
  584. mbbi->erase(mi); // Nuke the old inst.
  585. if (!Sunk) {
  586. DistanceMap.insert(std::make_pair(NewMI, Dist));
  587. mi = NewMI;
  588. nmi = llvm::next(mi);
  589. }
  590. return true;
  591. }
  592. return false;
  593. }
  594. /// ProcessCopy - If the specified instruction is not yet processed, process it
  595. /// if it's a copy. For a copy instruction, we find the physical registers the
  596. /// source and destination registers might be mapped to. These are kept in
  597. /// point-to maps used to determine future optimizations. e.g.
  598. /// v1024 = mov r0
  599. /// v1025 = mov r1
  600. /// v1026 = add v1024, v1025
  601. /// r1 = mov r1026
  602. /// If 'add' is a two-address instruction, v1024, v1026 are both potentially
  603. /// coalesced to r0 (from the input side). v1025 is mapped to r1. v1026 is
  604. /// potentially joined with r1 on the output side. It's worthwhile to commute
  605. /// 'add' to eliminate a copy.
  606. void TwoAddressInstructionPass::ProcessCopy(MachineInstr *MI,
  607. MachineBasicBlock *MBB,
  608. SmallPtrSet<MachineInstr*, 8> &Processed) {
  609. if (Processed.count(MI))
  610. return;
  611. bool IsSrcPhys, IsDstPhys;
  612. unsigned SrcReg, DstReg;
  613. if (!isCopyToReg(*MI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
  614. return;
  615. if (IsDstPhys && !IsSrcPhys)
  616. DstRegMap.insert(std::make_pair(SrcReg, DstReg));
  617. else if (!IsDstPhys && IsSrcPhys) {
  618. bool isNew = SrcRegMap.insert(std::make_pair(DstReg, SrcReg)).second;
  619. if (!isNew)
  620. assert(SrcRegMap[DstReg] == SrcReg &&
  621. "Can't map to two src physical registers!");
  622. SmallVector<unsigned, 4> VirtRegPairs;
  623. bool IsCopy = false;
  624. unsigned NewReg = 0;
  625. while (MachineInstr *UseMI = findOnlyInterestingUse(DstReg, MBB, MRI,TII,
  626. IsCopy, NewReg, IsDstPhys)) {
  627. if (IsCopy) {
  628. if (!Processed.insert(UseMI))
  629. break;
  630. }
  631. DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI);
  632. if (DI != DistanceMap.end())
  633. // Earlier in the same MBB.Reached via a back edge.
  634. break;
  635. if (IsDstPhys) {
  636. VirtRegPairs.push_back(NewReg);
  637. break;
  638. }
  639. bool isNew = SrcRegMap.insert(std::make_pair(NewReg, DstReg)).second;
  640. if (!isNew)
  641. assert(SrcRegMap[NewReg] == DstReg &&
  642. "Can't map to two src physical registers!");
  643. VirtRegPairs.push_back(NewReg);
  644. DstReg = NewReg;
  645. }
  646. if (!VirtRegPairs.empty()) {
  647. unsigned ToReg = VirtRegPairs.back();
  648. VirtRegPairs.pop_back();
  649. while (!VirtRegPairs.empty()) {
  650. unsigned FromReg = VirtRegPairs.back();
  651. VirtRegPairs.pop_back();
  652. bool isNew = DstRegMap.insert(std::make_pair(FromReg, ToReg)).second;
  653. if (!isNew)
  654. assert(DstRegMap[FromReg] == ToReg &&
  655. "Can't map to two dst physical registers!");
  656. ToReg = FromReg;
  657. }
  658. }
  659. }
  660. Processed.insert(MI);
  661. }
  662. /// isSafeToDelete - If the specified instruction does not produce any side
  663. /// effects and all of its defs are dead, then it's safe to delete.
  664. static bool isSafeToDelete(MachineInstr *MI,
  665. const TargetInstrInfo *TII,
  666. SmallVector<unsigned, 4> &Kills) {
  667. const TargetInstrDesc &TID = MI->getDesc();
  668. if (TID.mayStore() || TID.isCall())
  669. return false;
  670. if (TID.isTerminator() || TID.hasUnmodeledSideEffects())
  671. return false;
  672. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  673. MachineOperand &MO = MI->getOperand(i);
  674. if (!MO.isReg())
  675. continue;
  676. if (MO.isDef() && !MO.isDead())
  677. return false;
  678. if (MO.isUse() && MO.isKill())
  679. Kills.push_back(MO.getReg());
  680. }
  681. return true;
  682. }
  683. /// canUpdateDeletedKills - Check if all the registers listed in Kills are
  684. /// killed by instructions in MBB preceding the current instruction at
  685. /// position Dist. If so, return true and record information about the
  686. /// preceding kills in NewKills.
  687. bool TwoAddressInstructionPass::
  688. canUpdateDeletedKills(SmallVector<unsigned, 4> &Kills,
  689. SmallVector<NewKill, 4> &NewKills,
  690. MachineBasicBlock *MBB, unsigned Dist) {
  691. while (!Kills.empty()) {
  692. unsigned Kill = Kills.back();
  693. Kills.pop_back();
  694. if (TargetRegisterInfo::isPhysicalRegister(Kill))
  695. return false;
  696. MachineInstr *LastKill = FindLastUseInMBB(Kill, MBB, Dist);
  697. if (!LastKill)
  698. return false;
  699. bool isModRef = LastKill->definesRegister(Kill);
  700. NewKills.push_back(std::make_pair(std::make_pair(Kill, isModRef),
  701. LastKill));
  702. }
  703. return true;
  704. }
  705. /// DeleteUnusedInstr - If an instruction with a tied register operand can
  706. /// be safely deleted, just delete it.
  707. bool
  708. TwoAddressInstructionPass::DeleteUnusedInstr(MachineBasicBlock::iterator &mi,
  709. MachineBasicBlock::iterator &nmi,
  710. MachineFunction::iterator &mbbi,
  711. unsigned Dist) {
  712. // Check if the instruction has no side effects and if all its defs are dead.
  713. SmallVector<unsigned, 4> Kills;
  714. if (!isSafeToDelete(mi, TII, Kills))
  715. return false;
  716. // If this instruction kills some virtual registers, we need to
  717. // update the kill information. If it's not possible to do so,
  718. // then bail out.
  719. SmallVector<NewKill, 4> NewKills;
  720. if (!canUpdateDeletedKills(Kills, NewKills, &*mbbi, Dist))
  721. return false;
  722. if (LV) {
  723. while (!NewKills.empty()) {
  724. MachineInstr *NewKill = NewKills.back().second;
  725. unsigned Kill = NewKills.back().first.first;
  726. bool isDead = NewKills.back().first.second;
  727. NewKills.pop_back();
  728. if (LV->removeVirtualRegisterKilled(Kill, mi)) {
  729. if (isDead)
  730. LV->addVirtualRegisterDead(Kill, NewKill);
  731. else
  732. LV->addVirtualRegisterKilled(Kill, NewKill);
  733. }
  734. }
  735. }
  736. mbbi->erase(mi); // Nuke the old inst.
  737. mi = nmi;
  738. return true;
  739. }
  740. /// TryInstructionTransform - For the case where an instruction has a single
  741. /// pair of tied register operands, attempt some transformations that may
  742. /// either eliminate the tied operands or improve the opportunities for
  743. /// coalescing away the register copy. Returns true if the tied operands
  744. /// are eliminated altogether.
  745. bool TwoAddressInstructionPass::
  746. TryInstructionTransform(MachineBasicBlock::iterator &mi,
  747. MachineBasicBlock::iterator &nmi,
  748. MachineFunction::iterator &mbbi,
  749. unsigned SrcIdx, unsigned DstIdx, unsigned Dist) {
  750. const TargetInstrDesc &TID = mi->getDesc();
  751. unsigned regA = mi->getOperand(DstIdx).getReg();
  752. unsigned regB = mi->getOperand(SrcIdx).getReg();
  753. assert(TargetRegisterInfo::isVirtualRegister(regB) &&
  754. "cannot make instruction into two-address form");
  755. // If regA is dead and the instruction can be deleted, just delete
  756. // it so it doesn't clobber regB.
  757. bool regBKilled = isKilled(*mi, regB, MRI, TII);
  758. if (!regBKilled && mi->getOperand(DstIdx).isDead() &&
  759. DeleteUnusedInstr(mi, nmi, mbbi, Dist)) {
  760. ++NumDeletes;
  761. return true; // Done with this instruction.
  762. }
  763. // Check if it is profitable to commute the operands.
  764. unsigned SrcOp1, SrcOp2;
  765. unsigned regC = 0;
  766. unsigned regCIdx = ~0U;
  767. bool TryCommute = false;
  768. bool AggressiveCommute = false;
  769. if (TID.isCommutable() && mi->getNumOperands() >= 3 &&
  770. TII->findCommutedOpIndices(mi, SrcOp1, SrcOp2)) {
  771. if (SrcIdx == SrcOp1)
  772. regCIdx = SrcOp2;
  773. else if (SrcIdx == SrcOp2)
  774. regCIdx = SrcOp1;
  775. if (regCIdx != ~0U) {
  776. regC = mi->getOperand(regCIdx).getReg();
  777. if (!regBKilled && isKilled(*mi, regC, MRI, TII))
  778. // If C dies but B does not, swap the B and C operands.
  779. // This makes the live ranges of A and C joinable.
  780. TryCommute = true;
  781. else if (isProfitableToCommute(regB, regC, mi, mbbi, Dist)) {
  782. TryCommute = true;
  783. AggressiveCommute = true;
  784. }
  785. }
  786. }
  787. // If it's profitable to commute, try to do so.
  788. if (TryCommute && CommuteInstruction(mi, mbbi, regB, regC, Dist)) {
  789. ++NumCommuted;
  790. if (AggressiveCommute)
  791. ++NumAggrCommuted;
  792. return false;
  793. }
  794. if (TID.isConvertibleTo3Addr()) {
  795. // This instruction is potentially convertible to a true
  796. // three-address instruction. Check if it is profitable.
  797. if (!regBKilled || isProfitableToConv3Addr(regA)) {
  798. // Try to convert it.
  799. if (ConvertInstTo3Addr(mi, nmi, mbbi, regB, Dist)) {
  800. ++NumConvertedTo3Addr;
  801. return true; // Done with this instruction.
  802. }
  803. }
  804. }
  805. // If this is an instruction with a load folded into it, try unfolding
  806. // the load, e.g. avoid this:
  807. // movq %rdx, %rcx
  808. // addq (%rax), %rcx
  809. // in favor of this:
  810. // movq (%rax), %rcx
  811. // addq %rdx, %rcx
  812. // because it's preferable to schedule a load than a register copy.
  813. if (TID.mayLoad() && !regBKilled) {
  814. // Determine if a load can be unfolded.
  815. unsigned LoadRegIndex;
  816. unsigned NewOpc =
  817. TII->getOpcodeAfterMemoryUnfold(mi->getOpcode(),
  818. /*UnfoldLoad=*/true,
  819. /*UnfoldStore=*/false,
  820. &LoadRegIndex);
  821. if (NewOpc != 0) {
  822. const TargetInstrDesc &UnfoldTID = TII->get(NewOpc);
  823. if (UnfoldTID.getNumDefs() == 1) {
  824. MachineFunction &MF = *mbbi->getParent();
  825. // Unfold the load.
  826. DEBUG(dbgs() << "2addr: UNFOLDING: " << *mi);
  827. const TargetRegisterClass *RC =
  828. UnfoldTID.OpInfo[LoadRegIndex].getRegClass(TRI);
  829. unsigned Reg = MRI->createVirtualRegister(RC);
  830. SmallVector<MachineInstr *, 2> NewMIs;
  831. bool Success =
  832. TII->unfoldMemoryOperand(MF, mi, Reg,
  833. /*UnfoldLoad=*/true, /*UnfoldStore=*/false,
  834. NewMIs);
  835. (void)Success;
  836. assert(Success &&
  837. "unfoldMemoryOperand failed when getOpcodeAfterMemoryUnfold "
  838. "succeeded!");
  839. assert(NewMIs.size() == 2 &&
  840. "Unfolded a load into multiple instructions!");
  841. // The load was previously folded, so this is the only use.
  842. NewMIs[1]->addRegisterKilled(Reg, TRI);
  843. // Tentatively insert the instructions into the block so that they
  844. // look "normal" to the transformation logic.
  845. mbbi->insert(mi, NewMIs[0]);
  846. mbbi->insert(mi, NewMIs[1]);
  847. DEBUG(dbgs() << "2addr: NEW LOAD: " << *NewMIs[0]
  848. << "2addr: NEW INST: " << *NewMIs[1]);
  849. // Transform the instruction, now that it no longer has a load.
  850. unsigned NewDstIdx = NewMIs[1]->findRegisterDefOperandIdx(regA);
  851. unsigned NewSrcIdx = NewMIs[1]->findRegisterUseOperandIdx(regB);
  852. MachineBasicBlock::iterator NewMI = NewMIs[1];
  853. bool TransformSuccess =
  854. TryInstructionTransform(NewMI, mi, mbbi,
  855. NewSrcIdx, NewDstIdx, Dist);
  856. if (TransformSuccess ||
  857. NewMIs[1]->getOperand(NewSrcIdx).isKill()) {
  858. // Success, or at least we made an improvement. Keep the unfolded
  859. // instructions and discard the original.
  860. if (LV) {
  861. for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
  862. MachineOperand &MO = mi->getOperand(i);
  863. if (MO.isReg() && MO.getReg() != 0 &&
  864. TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
  865. if (MO.isUse()) {
  866. if (MO.isKill())
  867. LV->replaceKillInstruction(MO.getReg(), mi, NewMIs[0]);
  868. } else if (LV->removeVirtualRegisterDead(MO.getReg(), mi))
  869. LV->addVirtualRegisterDead(MO.getReg(), NewMIs[1]);
  870. }
  871. }
  872. LV->addVirtualRegisterKilled(Reg, NewMIs[1]);
  873. }
  874. mi->eraseFromParent();
  875. mi = NewMIs[1];
  876. if (TransformSuccess)
  877. return true;
  878. } else {
  879. // Transforming didn't eliminate the tie and didn't lead to an
  880. // improvement. Clean up the unfolded instructions and keep the
  881. // original.
  882. DEBUG(dbgs() << "2addr: ABANDONING UNFOLD\n");
  883. NewMIs[0]->eraseFromParent();
  884. NewMIs[1]->eraseFromParent();
  885. }
  886. }
  887. }
  888. }
  889. return false;
  890. }
  891. /// runOnMachineFunction - Reduce two-address instructions to two operands.
  892. ///
  893. bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
  894. DEBUG(dbgs() << "Machine Function\n");
  895. const TargetMachine &TM = MF.getTarget();
  896. MRI = &MF.getRegInfo();
  897. TII = TM.getInstrInfo();
  898. TRI = TM.getRegisterInfo();
  899. LV = getAnalysisIfAvailable<LiveVariables>();
  900. AA = &getAnalysis<AliasAnalysis>();
  901. bool MadeChange = false;
  902. DEBUG(dbgs() << "********** REWRITING TWO-ADDR INSTRS **********\n");
  903. DEBUG(dbgs() << "********** Function: "
  904. << MF.getFunction()->getName() << '\n');
  905. // ReMatRegs - Keep track of the registers whose def's are remat'ed.
  906. BitVector ReMatRegs;
  907. ReMatRegs.resize(MRI->getLastVirtReg()+1);
  908. typedef DenseMap<unsigned, SmallVector<std::pair<unsigned, unsigned>, 4> >
  909. TiedOperandMap;
  910. TiedOperandMap TiedOperands(4);
  911. SmallPtrSet<MachineInstr*, 8> Processed;
  912. for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
  913. mbbi != mbbe; ++mbbi) {
  914. unsigned Dist = 0;
  915. DistanceMap.clear();
  916. SrcRegMap.clear();
  917. DstRegMap.clear();
  918. Processed.clear();
  919. for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
  920. mi != me; ) {
  921. MachineBasicBlock::iterator nmi = llvm::next(mi);
  922. if (mi->isDebugValue()) {
  923. mi = nmi;
  924. continue;
  925. }
  926. // Remember REG_SEQUENCE instructions, we'll deal with them later.
  927. if (mi->isRegSequence())
  928. RegSequences.push_back(&*mi);
  929. const TargetInstrDesc &TID = mi->getDesc();
  930. bool FirstTied = true;
  931. DistanceMap.insert(std::make_pair(mi, ++Dist));
  932. ProcessCopy(&*mi, &*mbbi, Processed);
  933. // First scan through all the tied register uses in this instruction
  934. // and record a list of pairs of tied operands for each register.
  935. unsigned NumOps = mi->isInlineAsm()
  936. ? mi->getNumOperands() : TID.getNumOperands();
  937. for (unsigned SrcIdx = 0; SrcIdx < NumOps; ++SrcIdx) {
  938. unsigned DstIdx = 0;
  939. if (!mi->isRegTiedToDefOperand(SrcIdx, &DstIdx))
  940. continue;
  941. if (FirstTied) {
  942. FirstTied = false;
  943. ++NumTwoAddressInstrs;
  944. DEBUG(dbgs() << '\t' << *mi);
  945. }
  946. assert(mi->getOperand(SrcIdx).isReg() &&
  947. mi->getOperand(SrcIdx).getReg() &&
  948. mi->getOperand(SrcIdx).isUse() &&
  949. "two address instruction invalid");
  950. unsigned regB = mi->getOperand(SrcIdx).getReg();
  951. TiedOperandMap::iterator OI = TiedOperands.find(regB);
  952. if (OI == TiedOperands.end()) {
  953. SmallVector<std::pair<unsigned, unsigned>, 4> TiedPair;
  954. OI = TiedOperands.insert(std::make_pair(regB, TiedPair)).first;
  955. }
  956. OI->second.push_back(std::make_pair(SrcIdx, DstIdx));
  957. }
  958. // Now iterate over the information collected above.
  959. for (TiedOperandMap::iterator OI = TiedOperands.begin(),
  960. OE = TiedOperands.end(); OI != OE; ++OI) {
  961. SmallVector<std::pair<unsigned, unsigned>, 4> &TiedPairs = OI->second;
  962. // If the instruction has a single pair of tied operands, try some
  963. // transformations that may either eliminate the tied operands or
  964. // improve the opportunities for coalescing away the register copy.
  965. if (TiedOperands.size() == 1 && TiedPairs.size() == 1) {
  966. unsigned SrcIdx = TiedPairs[0].first;
  967. unsigned DstIdx = TiedPairs[0].second;
  968. // If the registers are already equal, nothing needs to be done.
  969. if (mi->getOperand(SrcIdx).getReg() ==
  970. mi->getOperand(DstIdx).getReg())
  971. break; // Done with this instruction.
  972. if (TryInstructionTransform(mi, nmi, mbbi, SrcIdx, DstIdx, Dist))
  973. break; // The tied operands have been eliminated.
  974. }
  975. bool RemovedKillFlag = false;
  976. bool AllUsesCopied = true;
  977. unsigned LastCopiedReg = 0;
  978. unsigned regB = OI->first;
  979. for (unsigned tpi = 0, tpe = TiedPairs.size(); tpi != tpe; ++tpi) {
  980. unsigned SrcIdx = TiedPairs[tpi].first;
  981. unsigned DstIdx = TiedPairs[tpi].second;
  982. unsigned regA = mi->getOperand(DstIdx).getReg();
  983. // Grab regB from the instruction because it may have changed if the
  984. // instruction was commuted.
  985. regB = mi->getOperand(SrcIdx).getReg();
  986. if (regA == regB) {
  987. // The register is tied to multiple destinations (or else we would
  988. // not have continued this far), but this use of the register
  989. // already matches the tied destination. Leave it.
  990. AllUsesCopied = false;
  991. continue;
  992. }
  993. LastCopiedReg = regA;
  994. assert(TargetRegisterInfo::isVirtualRegister(regB) &&
  995. "cannot make instruction into two-address form");
  996. #ifndef NDEBUG
  997. // First, verify that we don't have a use of "a" in the instruction
  998. // (a = b + a for example) because our transformation will not
  999. // work. This should never occur because we are in SSA form.
  1000. for (unsigned i = 0; i != mi->getNumOperands(); ++i)
  1001. assert(i == DstIdx ||
  1002. !mi->getOperand(i).isReg() ||
  1003. mi->getOperand(i).getReg() != regA);
  1004. #endif
  1005. // Emit a copy or rematerialize the definition.
  1006. const TargetRegisterClass *rc = MRI->getRegClass(regB);
  1007. MachineInstr *DefMI = MRI->getVRegDef(regB);
  1008. // If it's safe and profitable, remat the definition instead of
  1009. // copying it.
  1010. if (DefMI &&
  1011. DefMI->getDesc().isAsCheapAsAMove() &&
  1012. DefMI->isSafeToReMat(TII, AA, regB) &&
  1013. isProfitableToReMat(regB, rc, mi, DefMI, mbbi, Dist)){
  1014. DEBUG(dbgs() << "2addr: REMATTING : " << *DefMI << "\n");
  1015. unsigned regASubIdx = mi->getOperand(DstIdx).getSubReg();
  1016. TII->reMaterialize(*mbbi, mi, regA, regASubIdx, DefMI, *TRI);
  1017. ReMatRegs.set(regB);
  1018. ++NumReMats;
  1019. } else {
  1020. bool Emitted = TII->copyRegToReg(*mbbi, mi, regA, regB, rc, rc,
  1021. mi->getDebugLoc());
  1022. (void)Emitted;
  1023. assert(Emitted && "Unable to issue a copy instruction!\n");
  1024. }
  1025. MachineBasicBlock::iterator prevMI = prior(mi);
  1026. // Update DistanceMap.
  1027. DistanceMap.insert(std::make_pair(prevMI, Dist));
  1028. DistanceMap[mi] = ++Dist;
  1029. DEBUG(dbgs() << "\t\tprepend:\t" << *prevMI);
  1030. MachineOperand &MO = mi->getOperand(SrcIdx);
  1031. assert(MO.isReg() && MO.getReg() == regB && MO.isUse() &&
  1032. "inconsistent operand info for 2-reg pass");
  1033. if (MO.isKill()) {
  1034. MO.setIsKill(false);
  1035. RemovedKillFlag = true;
  1036. }
  1037. MO.setReg(regA);
  1038. }
  1039. if (AllUsesCopied) {
  1040. // Replace other (un-tied) uses of regB with LastCopiedReg.
  1041. for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
  1042. MachineOperand &MO = mi->getOperand(i);
  1043. if (MO.isReg() && MO.getReg() == regB && MO.isUse()) {
  1044. if (MO.isKill()) {
  1045. MO.setIsKill(false);
  1046. RemovedKillFlag = true;
  1047. }
  1048. MO.setReg(LastCopiedReg);
  1049. }
  1050. }
  1051. // Update live variables for regB.
  1052. if (RemovedKillFlag && LV && LV->getVarInfo(regB).removeKill(mi))
  1053. LV->addVirtualRegisterKilled(regB, prior(mi));
  1054. } else if (RemovedKillFlag) {
  1055. // Some tied uses of regB matched their destination registers, so
  1056. // regB is still used in this instruction, but a kill flag was
  1057. // removed from a different tied use of regB, so now we need to add
  1058. // a kill flag to one of the remaining uses of regB.
  1059. for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
  1060. MachineOperand &MO = mi->getOperand(i);
  1061. if (MO.isReg() && MO.getReg() == regB && MO.isUse()) {
  1062. MO.setIsKill(true);
  1063. break;
  1064. }
  1065. }
  1066. }
  1067. // Schedule the source copy / remat inserted to form two-address
  1068. // instruction. FIXME: Does it matter the distance map may not be
  1069. // accurate after it's scheduled?
  1070. TII->scheduleTwoAddrSource(prior(mi), mi, *TRI);
  1071. MadeChange = true;
  1072. DEBUG(dbgs() << "\t\trewrite to:\t" << *mi);
  1073. }
  1074. // Clear TiedOperands here instead of at the top of the loop
  1075. // since most instructions do not have tied operands.
  1076. TiedOperands.clear();
  1077. mi = nmi;
  1078. }
  1079. }
  1080. // Some remat'ed instructions are dead.
  1081. int VReg = ReMatRegs.find_first();
  1082. while (VReg != -1) {
  1083. if (MRI->use_nodbg_empty(VReg)) {
  1084. MachineInstr *DefMI = MRI->getVRegDef(VReg);
  1085. DefMI->eraseFromParent();
  1086. }
  1087. VReg = ReMatRegs.find_next(VReg);
  1088. }
  1089. // Eliminate REG_SEQUENCE instructions. Their whole purpose was to preseve
  1090. // SSA form. It's now safe to de-SSA.
  1091. MadeChange |= EliminateRegSequences();
  1092. return MadeChange;
  1093. }
  1094. static void UpdateRegSequenceSrcs(unsigned SrcReg,
  1095. unsigned DstReg, unsigned SubIdx,
  1096. MachineRegisterInfo *MRI,
  1097. const TargetRegisterInfo &TRI) {
  1098. for (MachineRegisterInfo::reg_iterator RI = MRI->reg_begin(SrcReg),
  1099. RE = MRI->reg_end(); RI != RE; ) {
  1100. MachineOperand &MO = RI.getOperand();
  1101. ++RI;
  1102. MO.substVirtReg(DstReg, SubIdx, TRI);
  1103. }
  1104. }
  1105. /// CoalesceExtSubRegs - If a number of sources of the REG_SEQUENCE are
  1106. /// EXTRACT_SUBREG from the same register and to the same virtual register
  1107. /// with different sub-register indices, attempt to combine the
  1108. /// EXTRACT_SUBREGs and pre-coalesce them. e.g.
  1109. /// %reg1026<def> = VLDMQ %reg1025<kill>, 260, pred:14, pred:%reg0
  1110. /// %reg1029:6<def> = EXTRACT_SUBREG %reg1026, 6
  1111. /// %reg1029:5<def> = EXTRACT_SUBREG %reg1026<kill>, 5
  1112. /// Since D subregs 5, 6 can combine to a Q register, we can coalesce
  1113. /// reg1026 to reg1029.
  1114. void
  1115. TwoAddressInstructionPass::CoalesceExtSubRegs(SmallVector<unsigned,4> &Srcs,
  1116. unsigned DstReg) {
  1117. SmallSet<unsigned, 4> Seen;
  1118. for (unsigned i = 0, e = Srcs.size(); i != e; ++i) {
  1119. unsigned SrcReg = Srcs[i];
  1120. if (!Seen.insert(SrcReg))
  1121. continue;
  1122. // Check that the instructions are all in the same basic block.
  1123. MachineInstr *SrcDefMI = MRI->getVRegDef(SrcReg);
  1124. MachineInstr *DstDefMI = MRI->getVRegDef(DstReg);
  1125. if (SrcDefMI->getParent() != DstDefMI->getParent())
  1126. continue;
  1127. // If there are no other uses than extract_subreg which feed into
  1128. // the reg_sequence, then we might be able to coalesce them.
  1129. bool CanCoalesce = true;
  1130. SmallVector<unsigned, 4> SrcSubIndices, DstSubIndices;
  1131. for (MachineRegisterInfo::use_nodbg_iterator
  1132. UI = MRI->use_nodbg_begin(SrcReg),
  1133. UE = MRI->use_nodbg_end(); UI != UE; ++UI) {
  1134. MachineInstr *UseMI = &*UI;
  1135. if (!UseMI->isExtractSubreg() ||
  1136. UseMI->getOperand(0).getReg() != DstReg ||
  1137. UseMI->getOperand(1).getSubReg() != 0) {
  1138. CanCoalesce = false;
  1139. break;
  1140. }
  1141. SrcSubIndices.push_back(UseMI->getOperand(2).getImm());
  1142. DstSubIndices.push_back(UseMI->getOperand(0).getSubReg());
  1143. }
  1144. if (!CanCoalesce || SrcSubIndices.size() < 2)
  1145. continue;
  1146. // Check that the source subregisters can be combined.
  1147. std::sort(SrcSubIndices.begin(), SrcSubIndices.end());
  1148. unsigned NewSrcSubIdx = 0;
  1149. if (!TRI->canCombineSubRegIndices(MRI->getRegClass(SrcReg), SrcSubIndices,
  1150. NewSrcSubIdx))
  1151. continue;
  1152. // Check that the destination subregisters can also be combined.
  1153. std::sort(DstSubIndices.begin(), DstSubIndices.end());
  1154. unsigned NewDstSubIdx = 0;
  1155. if (!TRI->canCombineSubRegIndices(MRI->getRegClass(DstReg), DstSubIndices,
  1156. NewDstSubIdx))
  1157. continue;
  1158. // If neither source nor destination can be combined to the full register,
  1159. // just give up. This could be improved if it ever matters.
  1160. if (NewSrcSubIdx != 0 && NewDstSubIdx != 0)
  1161. continue;
  1162. // Now that we know that all the uses are extract_subregs and that those
  1163. // subregs can somehow be combined, scan all the extract_subregs again to
  1164. // make sure the subregs are in the right order and can be composed.
  1165. MachineInstr *SomeMI = 0;
  1166. CanCoalesce = true;
  1167. for (MachineRegisterInfo::use_nodbg_iterator
  1168. UI = MRI->use_nodbg_begin(SrcReg),
  1169. UE = MRI->use_nodbg_end(); UI != UE; ++UI) {
  1170. MachineInstr *UseMI = &*UI;
  1171. assert(UseMI->isExtractSubreg());
  1172. unsigned DstSubIdx = UseMI->getOperand(0).getSubReg();
  1173. unsigned SrcSubIdx = UseMI->getOperand(2).getImm();
  1174. assert(DstSubIdx != 0 && "missing subreg from RegSequence elimination");
  1175. if ((NewDstSubIdx == 0 &&
  1176. TRI->composeSubRegIndices(NewSrcSubIdx, DstSubIdx) != SrcSubIdx) ||
  1177. (NewSrcSubIdx == 0 &&
  1178. TRI->composeSubRegIndices(NewDstSubIdx, SrcSubIdx) != DstSubIdx)) {
  1179. CanCoalesce = false;
  1180. break;
  1181. }
  1182. // Keep track of one of the uses.
  1183. SomeMI = UseMI;
  1184. }
  1185. if (!CanCoalesce)
  1186. continue;
  1187. // Insert a copy or an extract to replace the original extracts.
  1188. MachineBasicBlock::iterator InsertLoc = SomeMI;
  1189. if (NewSrcSubIdx) {
  1190. // Insert an extract subreg.
  1191. BuildMI(*SomeMI->getParent(), InsertLoc, SomeMI->getDebugLoc(),
  1192. TII->get(TargetOpcode::EXTRACT_SUBREG), DstReg)
  1193. .addReg(SrcReg).addImm(NewSrcSubIdx);
  1194. } else if (NewDstSubIdx) {
  1195. // Do a subreg insertion.
  1196. BuildMI(*SomeMI->getParent(), InsertLoc, SomeMI->getDebugLoc(),
  1197. TII->get(TargetOpcode::INSERT_SUBREG), DstReg)
  1198. .addReg(DstReg).addReg(SrcReg).addImm(NewDstSubIdx);
  1199. } else {
  1200. // Insert a copy.
  1201. bool Emitted =
  1202. TII->copyRegToReg(*SomeMI->getParent(), InsertLoc, DstReg, SrcReg,
  1203. MRI->getRegClass(DstReg), MRI->getRegClass(SrcReg),
  1204. SomeMI->getDebugLoc());
  1205. (void)Emitted;
  1206. }
  1207. MachineBasicBlock::iterator CopyMI = prior(InsertLoc);
  1208. // Remove all the old extract instructions.
  1209. for (MachineRegisterInfo::use_nodbg_iterator
  1210. UI = MRI->use_nodbg_begin(SrcReg),
  1211. UE = MRI->use_nodbg_end(); UI != UE; ) {
  1212. MachineInstr *UseMI = &*UI;
  1213. ++UI;
  1214. if (UseMI == CopyMI)
  1215. continue;
  1216. assert(UseMI->isExtractSubreg());
  1217. // Move any kills to the new copy or extract instruction.
  1218. if (UseMI->getOperand(1).isKill()) {
  1219. MachineOperand *KillMO = CopyMI->findRegisterUseOperand(SrcReg);
  1220. KillMO->setIsKill();
  1221. if (LV)
  1222. // Update live variables
  1223. LV->replaceKillInstruction(SrcReg, UseMI, &*CopyMI);
  1224. }
  1225. UseMI->eraseFromParent();
  1226. }
  1227. }
  1228. }
  1229. static bool HasOtherRegSequenceUses(unsigned Reg, MachineInstr *RegSeq,
  1230. MachineRegisterInfo *MRI) {
  1231. for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg),
  1232. UE = MRI->use_end(); UI != UE; ++UI) {
  1233. MachineInstr *UseMI = &*UI;
  1234. if (UseMI != RegSeq && UseMI->isRegSequence())
  1235. return true;
  1236. }
  1237. return false;
  1238. }
  1239. /// EliminateRegSequences - Eliminate REG_SEQUENCE instructions as part
  1240. /// of the de-ssa process. This replaces sources of REG_SEQUENCE as
  1241. /// sub-register references of the register defined by REG_SEQUENCE. e.g.
  1242. ///
  1243. /// %reg1029<def>, %reg1030<def> = VLD1q16 %reg1024<kill>, ...
  1244. /// %reg1031<def> = REG_SEQUENCE %reg1029<kill>, 5, %reg1030<kill>, 6
  1245. /// =>
  1246. /// %reg1031:5<def>, %reg1031:6<def> = VLD1q16 %reg1024<kill>, ...
  1247. bool TwoAddressInstructionPass::EliminateRegSequences() {
  1248. if (RegSequences.empty())
  1249. return false;
  1250. for (unsigned i = 0, e = RegSequences.size(); i != e; ++i) {
  1251. MachineInstr *MI = RegSequences[i];
  1252. unsigned DstReg = MI->getOperand(0).getReg();
  1253. if (MI->getOperand(0).getSubReg() ||
  1254. TargetRegisterInfo::isPhysicalRegister(DstReg) ||
  1255. !(MI->getNumOperands() & 1)) {
  1256. DEBUG(dbgs() << "Illegal REG_SEQUENCE instruction:" << *MI);
  1257. llvm_unreachable(0);
  1258. }
  1259. bool IsImpDef = true;
  1260. SmallVector<unsigned, 4> RealSrcs;
  1261. SmallSet<unsigned, 4> Seen;
  1262. for (unsigned i = 1, e = MI->getNumOperands(); i < e; i += 2) {
  1263. unsigned SrcReg = MI->getOperand(i).getReg();
  1264. if (MI->getOperand(i).getSubReg() ||
  1265. TargetRegisterInfo::isPhysicalRegister(SrcReg)) {
  1266. DEBUG(dbgs() << "Illegal REG_SEQUENCE instruction:" << *MI);
  1267. llvm_unreachable(0);
  1268. }
  1269. MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
  1270. if (DefMI->isImplicitDef()) {
  1271. DefMI->eraseFromParent();
  1272. continue;
  1273. }
  1274. IsImpDef = false;
  1275. // Remember EXTRACT_SUBREG sources. These might be candidate for
  1276. // coalescing.
  1277. if (DefMI->isExtractSubreg())
  1278. RealSrcs.push_back(DefMI->getOperand(1).getReg());
  1279. if (!Seen.insert(SrcReg) ||
  1280. MI->getParent() != DefMI->getParent() ||
  1281. !MI->getOperand(i).isKill() ||
  1282. HasOtherRegSequenceUses(SrcReg, MI, MRI)) {
  1283. // REG_SEQUENCE cannot have duplicated operands, add a copy.
  1284. // Also add an copy if the source is live-in the block. We don't want
  1285. // to end up with a partial-redef of a livein, e.g.
  1286. // BB0:
  1287. // reg1051:10<def> =
  1288. // ...
  1289. // BB1:
  1290. // ... = reg1051:10
  1291. // BB2:
  1292. // reg1051:9<def> =
  1293. // LiveIntervalAnalysis won't like it.
  1294. //
  1295. // If the REG_SEQUENCE doesn't kill its source, keeping live variables
  1296. // correctly up to date becomes very difficult. Insert a copy.
  1297. //
  1298. const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
  1299. unsigned NewReg = MRI->createVirtualRegister(RC);
  1300. MachineBasicBlock::iterator InsertLoc = MI;
  1301. bool Emitted =
  1302. TII->copyRegToReg(*MI->getParent(), InsertLoc, NewReg, SrcReg, RC, RC,
  1303. MI->getDebugLoc());
  1304. (void)Emitted;
  1305. assert(Emitted && "Unable to issue a copy instruction!\n");
  1306. MI->getOperand(i).setReg(NewReg);
  1307. if (MI->getOperand(i).isKill()) {
  1308. MachineBasicBlock::iterator CopyMI = prior(InsertLoc);
  1309. MachineOperand *KillMO = CopyMI->findRegisterUseOperand(SrcReg);
  1310. KillMO->setIsKill();
  1311. if (LV)
  1312. // Update live variables
  1313. LV->replaceKillInstruction(SrcReg, MI, &*CopyMI);
  1314. }
  1315. }
  1316. }
  1317. for (unsigned i = 1, e = MI->getNumOperands(); i < e; i += 2) {
  1318. unsigned SrcReg = MI->getOperand(i).getReg();
  1319. unsigned SubIdx = MI->getOperand(i+1).getImm();
  1320. UpdateRegSequenceSrcs(SrcReg, DstReg, SubIdx, MRI, *TRI);
  1321. }
  1322. if (IsImpDef) {
  1323. DEBUG(dbgs() << "Turned: " << *MI << " into an IMPLICIT_DEF");
  1324. MI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
  1325. for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j)
  1326. MI->RemoveOperand(j);
  1327. } else {
  1328. DEBUG(dbgs() << "Eliminated: " << *MI);
  1329. MI->eraseFromParent();
  1330. }
  1331. // Try coalescing some EXTRACT_SUBREG instructions. This can create
  1332. // INSERT_SUBREG instructions that must have <undef> flags added by
  1333. // LiveIntervalAnalysis, so only run it when LiveVariables is available.
  1334. if (LV)
  1335. CoalesceExtSubRegs(RealSrcs, DstReg);
  1336. }
  1337. RegSequences.clear();
  1338. return true;
  1339. }