TwoAddressInstructionPass.cpp 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874
  1. //===- TwoAddressInstructionPass.cpp - Two-Address instruction pass -------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the TwoAddress instruction pass which is used
  10. // by most register allocators. Two-Address instructions are rewritten
  11. // from:
  12. //
  13. // A = B op C
  14. //
  15. // to:
  16. //
  17. // A = B
  18. // A op= C
  19. //
  20. // Note that if a register allocator chooses to use this pass, that it
  21. // has to be capable of handling the non-SSA nature of these rewritten
  22. // virtual registers.
  23. //
  24. // It is also worth noting that the duplicate operand of the two
  25. // address instruction is removed.
  26. //
  27. //===----------------------------------------------------------------------===//
  28. #include "llvm/ADT/DenseMap.h"
  29. #include "llvm/ADT/SmallPtrSet.h"
  30. #include "llvm/ADT/SmallSet.h"
  31. #include "llvm/ADT/SmallVector.h"
  32. #include "llvm/ADT/Statistic.h"
  33. #include "llvm/ADT/iterator_range.h"
  34. #include "llvm/Analysis/AliasAnalysis.h"
  35. #include "llvm/CodeGen/LiveInterval.h"
  36. #include "llvm/CodeGen/LiveIntervals.h"
  37. #include "llvm/CodeGen/LiveVariables.h"
  38. #include "llvm/CodeGen/MachineBasicBlock.h"
  39. #include "llvm/CodeGen/MachineFunction.h"
  40. #include "llvm/CodeGen/MachineFunctionPass.h"
  41. #include "llvm/CodeGen/MachineInstr.h"
  42. #include "llvm/CodeGen/MachineInstrBuilder.h"
  43. #include "llvm/CodeGen/MachineOperand.h"
  44. #include "llvm/CodeGen/MachineRegisterInfo.h"
  45. #include "llvm/CodeGen/Passes.h"
  46. #include "llvm/CodeGen/SlotIndexes.h"
  47. #include "llvm/CodeGen/TargetInstrInfo.h"
  48. #include "llvm/CodeGen/TargetOpcodes.h"
  49. #include "llvm/CodeGen/TargetRegisterInfo.h"
  50. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  51. #include "llvm/MC/MCInstrDesc.h"
  52. #include "llvm/MC/MCInstrItineraries.h"
  53. #include "llvm/Pass.h"
  54. #include "llvm/Support/CodeGen.h"
  55. #include "llvm/Support/CommandLine.h"
  56. #include "llvm/Support/Debug.h"
  57. #include "llvm/Support/ErrorHandling.h"
  58. #include "llvm/Support/raw_ostream.h"
  59. #include "llvm/Target/TargetMachine.h"
  60. #include <cassert>
  61. #include <iterator>
  62. #include <utility>
  63. using namespace llvm;
  64. #define DEBUG_TYPE "twoaddressinstruction"
  65. STATISTIC(NumTwoAddressInstrs, "Number of two-address instructions");
  66. STATISTIC(NumCommuted , "Number of instructions commuted to coalesce");
  67. STATISTIC(NumAggrCommuted , "Number of instructions aggressively commuted");
  68. STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address");
  69. STATISTIC(Num3AddrSunk, "Number of 3-address instructions sunk");
  70. STATISTIC(NumReSchedUps, "Number of instructions re-scheduled up");
  71. STATISTIC(NumReSchedDowns, "Number of instructions re-scheduled down");
  72. // Temporary flag to disable rescheduling.
  73. static cl::opt<bool>
  74. EnableRescheduling("twoaddr-reschedule",
  75. cl::desc("Coalesce copies by rescheduling (default=true)"),
  76. cl::init(true), cl::Hidden);
  77. // Limit the number of dataflow edges to traverse when evaluating the benefit
  78. // of commuting operands.
  79. static cl::opt<unsigned> MaxDataFlowEdge(
  80. "dataflow-edge-limit", cl::Hidden, cl::init(3),
  81. cl::desc("Maximum number of dataflow edges to traverse when evaluating "
  82. "the benefit of commuting operands"));
  83. namespace {
  84. class TwoAddressInstructionPass : public MachineFunctionPass {
  85. MachineFunction *MF;
  86. const TargetInstrInfo *TII;
  87. const TargetRegisterInfo *TRI;
  88. const InstrItineraryData *InstrItins;
  89. MachineRegisterInfo *MRI;
  90. LiveVariables *LV;
  91. LiveIntervals *LIS;
  92. AliasAnalysis *AA;
  93. CodeGenOpt::Level OptLevel;
  94. // The current basic block being processed.
  95. MachineBasicBlock *MBB;
  96. // Keep track the distance of a MI from the start of the current basic block.
  97. DenseMap<MachineInstr*, unsigned> DistanceMap;
  98. // Set of already processed instructions in the current block.
  99. SmallPtrSet<MachineInstr*, 8> Processed;
  100. // Set of instructions converted to three-address by target and then sunk
  101. // down current basic block.
  102. SmallPtrSet<MachineInstr*, 8> SunkInstrs;
  103. // A map from virtual registers to physical registers which are likely targets
  104. // to be coalesced to due to copies from physical registers to virtual
  105. // registers. e.g. v1024 = move r0.
  106. DenseMap<unsigned, unsigned> SrcRegMap;
  107. // A map from virtual registers to physical registers which are likely targets
  108. // to be coalesced to due to copies to physical registers from virtual
  109. // registers. e.g. r1 = move v1024.
  110. DenseMap<unsigned, unsigned> DstRegMap;
  111. bool sink3AddrInstruction(MachineInstr *MI, unsigned Reg,
  112. MachineBasicBlock::iterator OldPos);
  113. bool isRevCopyChain(unsigned FromReg, unsigned ToReg, int Maxlen);
  114. bool noUseAfterLastDef(unsigned Reg, unsigned Dist, unsigned &LastDef);
  115. bool isProfitableToCommute(unsigned regA, unsigned regB, unsigned regC,
  116. MachineInstr *MI, unsigned Dist);
  117. bool commuteInstruction(MachineInstr *MI, unsigned DstIdx,
  118. unsigned RegBIdx, unsigned RegCIdx, unsigned Dist);
  119. bool isProfitableToConv3Addr(unsigned RegA, unsigned RegB);
  120. bool convertInstTo3Addr(MachineBasicBlock::iterator &mi,
  121. MachineBasicBlock::iterator &nmi,
  122. unsigned RegA, unsigned RegB, unsigned Dist);
  123. bool isDefTooClose(unsigned Reg, unsigned Dist, MachineInstr *MI);
  124. bool rescheduleMIBelowKill(MachineBasicBlock::iterator &mi,
  125. MachineBasicBlock::iterator &nmi,
  126. unsigned Reg);
  127. bool rescheduleKillAboveMI(MachineBasicBlock::iterator &mi,
  128. MachineBasicBlock::iterator &nmi,
  129. unsigned Reg);
  130. bool tryInstructionTransform(MachineBasicBlock::iterator &mi,
  131. MachineBasicBlock::iterator &nmi,
  132. unsigned SrcIdx, unsigned DstIdx,
  133. unsigned Dist, bool shouldOnlyCommute);
  134. bool tryInstructionCommute(MachineInstr *MI,
  135. unsigned DstOpIdx,
  136. unsigned BaseOpIdx,
  137. bool BaseOpKilled,
  138. unsigned Dist);
  139. void scanUses(unsigned DstReg);
  140. void processCopy(MachineInstr *MI);
  141. using TiedPairList = SmallVector<std::pair<unsigned, unsigned>, 4>;
  142. using TiedOperandMap = SmallDenseMap<unsigned, TiedPairList>;
  143. bool collectTiedOperands(MachineInstr *MI, TiedOperandMap&);
  144. void processTiedPairs(MachineInstr *MI, TiedPairList&, unsigned &Dist);
  145. void eliminateRegSequence(MachineBasicBlock::iterator&);
  146. public:
  147. static char ID; // Pass identification, replacement for typeid
  148. TwoAddressInstructionPass() : MachineFunctionPass(ID) {
  149. initializeTwoAddressInstructionPassPass(*PassRegistry::getPassRegistry());
  150. }
  151. void getAnalysisUsage(AnalysisUsage &AU) const override {
  152. AU.setPreservesCFG();
  153. AU.addUsedIfAvailable<AAResultsWrapperPass>();
  154. AU.addUsedIfAvailable<LiveVariables>();
  155. AU.addPreserved<LiveVariables>();
  156. AU.addPreserved<SlotIndexes>();
  157. AU.addPreserved<LiveIntervals>();
  158. AU.addPreservedID(MachineLoopInfoID);
  159. AU.addPreservedID(MachineDominatorsID);
  160. MachineFunctionPass::getAnalysisUsage(AU);
  161. }
  162. /// Pass entry point.
  163. bool runOnMachineFunction(MachineFunction&) override;
  164. };
  165. } // end anonymous namespace
  166. char TwoAddressInstructionPass::ID = 0;
  167. char &llvm::TwoAddressInstructionPassID = TwoAddressInstructionPass::ID;
  168. INITIALIZE_PASS_BEGIN(TwoAddressInstructionPass, DEBUG_TYPE,
  169. "Two-Address instruction pass", false, false)
  170. INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
  171. INITIALIZE_PASS_END(TwoAddressInstructionPass, DEBUG_TYPE,
  172. "Two-Address instruction pass", false, false)
  173. static bool isPlainlyKilled(MachineInstr *MI, unsigned Reg, LiveIntervals *LIS);
  174. /// A two-address instruction has been converted to a three-address instruction
  175. /// to avoid clobbering a register. Try to sink it past the instruction that
  176. /// would kill the above mentioned register to reduce register pressure.
  177. bool TwoAddressInstructionPass::
  178. sink3AddrInstruction(MachineInstr *MI, unsigned SavedReg,
  179. MachineBasicBlock::iterator OldPos) {
  180. // FIXME: Shouldn't we be trying to do this before we three-addressify the
  181. // instruction? After this transformation is done, we no longer need
  182. // the instruction to be in three-address form.
  183. // Check if it's safe to move this instruction.
  184. bool SeenStore = true; // Be conservative.
  185. if (!MI->isSafeToMove(AA, SeenStore))
  186. return false;
  187. unsigned DefReg = 0;
  188. SmallSet<unsigned, 4> UseRegs;
  189. for (const MachineOperand &MO : MI->operands()) {
  190. if (!MO.isReg())
  191. continue;
  192. Register MOReg = MO.getReg();
  193. if (!MOReg)
  194. continue;
  195. if (MO.isUse() && MOReg != SavedReg)
  196. UseRegs.insert(MO.getReg());
  197. if (!MO.isDef())
  198. continue;
  199. if (MO.isImplicit())
  200. // Don't try to move it if it implicitly defines a register.
  201. return false;
  202. if (DefReg)
  203. // For now, don't move any instructions that define multiple registers.
  204. return false;
  205. DefReg = MO.getReg();
  206. }
  207. // Find the instruction that kills SavedReg.
  208. MachineInstr *KillMI = nullptr;
  209. if (LIS) {
  210. LiveInterval &LI = LIS->getInterval(SavedReg);
  211. assert(LI.end() != LI.begin() &&
  212. "Reg should not have empty live interval.");
  213. SlotIndex MBBEndIdx = LIS->getMBBEndIdx(MBB).getPrevSlot();
  214. LiveInterval::const_iterator I = LI.find(MBBEndIdx);
  215. if (I != LI.end() && I->start < MBBEndIdx)
  216. return false;
  217. --I;
  218. KillMI = LIS->getInstructionFromIndex(I->end);
  219. }
  220. if (!KillMI) {
  221. for (MachineOperand &UseMO : MRI->use_nodbg_operands(SavedReg)) {
  222. if (!UseMO.isKill())
  223. continue;
  224. KillMI = UseMO.getParent();
  225. break;
  226. }
  227. }
  228. // If we find the instruction that kills SavedReg, and it is in an
  229. // appropriate location, we can try to sink the current instruction
  230. // past it.
  231. if (!KillMI || KillMI->getParent() != MBB || KillMI == MI ||
  232. MachineBasicBlock::iterator(KillMI) == OldPos || KillMI->isTerminator())
  233. return false;
  234. // If any of the definitions are used by another instruction between the
  235. // position and the kill use, then it's not safe to sink it.
  236. //
  237. // FIXME: This can be sped up if there is an easy way to query whether an
  238. // instruction is before or after another instruction. Then we can use
  239. // MachineRegisterInfo def / use instead.
  240. MachineOperand *KillMO = nullptr;
  241. MachineBasicBlock::iterator KillPos = KillMI;
  242. ++KillPos;
  243. unsigned NumVisited = 0;
  244. for (MachineInstr &OtherMI : make_range(std::next(OldPos), KillPos)) {
  245. // Debug instructions cannot be counted against the limit.
  246. if (OtherMI.isDebugInstr())
  247. continue;
  248. if (NumVisited > 30) // FIXME: Arbitrary limit to reduce compile time cost.
  249. return false;
  250. ++NumVisited;
  251. for (unsigned i = 0, e = OtherMI.getNumOperands(); i != e; ++i) {
  252. MachineOperand &MO = OtherMI.getOperand(i);
  253. if (!MO.isReg())
  254. continue;
  255. Register MOReg = MO.getReg();
  256. if (!MOReg)
  257. continue;
  258. if (DefReg == MOReg)
  259. return false;
  260. if (MO.isKill() || (LIS && isPlainlyKilled(&OtherMI, MOReg, LIS))) {
  261. if (&OtherMI == KillMI && MOReg == SavedReg)
  262. // Save the operand that kills the register. We want to unset the kill
  263. // marker if we can sink MI past it.
  264. KillMO = &MO;
  265. else if (UseRegs.count(MOReg))
  266. // One of the uses is killed before the destination.
  267. return false;
  268. }
  269. }
  270. }
  271. assert(KillMO && "Didn't find kill");
  272. if (!LIS) {
  273. // Update kill and LV information.
  274. KillMO->setIsKill(false);
  275. KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI);
  276. KillMO->setIsKill(true);
  277. if (LV)
  278. LV->replaceKillInstruction(SavedReg, *KillMI, *MI);
  279. }
  280. // Move instruction to its destination.
  281. MBB->remove(MI);
  282. MBB->insert(KillPos, MI);
  283. if (LIS)
  284. LIS->handleMove(*MI);
  285. ++Num3AddrSunk;
  286. return true;
  287. }
  288. /// Return the MachineInstr* if it is the single def of the Reg in current BB.
  289. static MachineInstr *getSingleDef(unsigned Reg, MachineBasicBlock *BB,
  290. const MachineRegisterInfo *MRI) {
  291. MachineInstr *Ret = nullptr;
  292. for (MachineInstr &DefMI : MRI->def_instructions(Reg)) {
  293. if (DefMI.getParent() != BB || DefMI.isDebugValue())
  294. continue;
  295. if (!Ret)
  296. Ret = &DefMI;
  297. else if (Ret != &DefMI)
  298. return nullptr;
  299. }
  300. return Ret;
  301. }
  302. /// Check if there is a reversed copy chain from FromReg to ToReg:
  303. /// %Tmp1 = copy %Tmp2;
  304. /// %FromReg = copy %Tmp1;
  305. /// %ToReg = add %FromReg ...
  306. /// %Tmp2 = copy %ToReg;
  307. /// MaxLen specifies the maximum length of the copy chain the func
  308. /// can walk through.
  309. bool TwoAddressInstructionPass::isRevCopyChain(unsigned FromReg, unsigned ToReg,
  310. int Maxlen) {
  311. unsigned TmpReg = FromReg;
  312. for (int i = 0; i < Maxlen; i++) {
  313. MachineInstr *Def = getSingleDef(TmpReg, MBB, MRI);
  314. if (!Def || !Def->isCopy())
  315. return false;
  316. TmpReg = Def->getOperand(1).getReg();
  317. if (TmpReg == ToReg)
  318. return true;
  319. }
  320. return false;
  321. }
  322. /// Return true if there are no intervening uses between the last instruction
  323. /// in the MBB that defines the specified register and the two-address
  324. /// instruction which is being processed. It also returns the last def location
  325. /// by reference.
  326. bool TwoAddressInstructionPass::noUseAfterLastDef(unsigned Reg, unsigned Dist,
  327. unsigned &LastDef) {
  328. LastDef = 0;
  329. unsigned LastUse = Dist;
  330. for (MachineOperand &MO : MRI->reg_operands(Reg)) {
  331. MachineInstr *MI = MO.getParent();
  332. if (MI->getParent() != MBB || MI->isDebugValue())
  333. continue;
  334. DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI);
  335. if (DI == DistanceMap.end())
  336. continue;
  337. if (MO.isUse() && DI->second < LastUse)
  338. LastUse = DI->second;
  339. if (MO.isDef() && DI->second > LastDef)
  340. LastDef = DI->second;
  341. }
  342. return !(LastUse > LastDef && LastUse < Dist);
  343. }
  344. /// Return true if the specified MI is a copy instruction or an extract_subreg
  345. /// instruction. It also returns the source and destination registers and
  346. /// whether they are physical registers by reference.
  347. static bool isCopyToReg(MachineInstr &MI, const TargetInstrInfo *TII,
  348. unsigned &SrcReg, unsigned &DstReg,
  349. bool &IsSrcPhys, bool &IsDstPhys) {
  350. SrcReg = 0;
  351. DstReg = 0;
  352. if (MI.isCopy()) {
  353. DstReg = MI.getOperand(0).getReg();
  354. SrcReg = MI.getOperand(1).getReg();
  355. } else if (MI.isInsertSubreg() || MI.isSubregToReg()) {
  356. DstReg = MI.getOperand(0).getReg();
  357. SrcReg = MI.getOperand(2).getReg();
  358. } else
  359. return false;
  360. IsSrcPhys = Register::isPhysicalRegister(SrcReg);
  361. IsDstPhys = Register::isPhysicalRegister(DstReg);
  362. return true;
  363. }
  364. /// Test if the given register value, which is used by the
  365. /// given instruction, is killed by the given instruction.
  366. static bool isPlainlyKilled(MachineInstr *MI, unsigned Reg,
  367. LiveIntervals *LIS) {
  368. if (LIS && Register::isVirtualRegister(Reg) && !LIS->isNotInMIMap(*MI)) {
  369. // FIXME: Sometimes tryInstructionTransform() will add instructions and
  370. // test whether they can be folded before keeping them. In this case it
  371. // sets a kill before recursively calling tryInstructionTransform() again.
  372. // If there is no interval available, we assume that this instruction is
  373. // one of those. A kill flag is manually inserted on the operand so the
  374. // check below will handle it.
  375. LiveInterval &LI = LIS->getInterval(Reg);
  376. // This is to match the kill flag version where undefs don't have kill
  377. // flags.
  378. if (!LI.hasAtLeastOneValue())
  379. return false;
  380. SlotIndex useIdx = LIS->getInstructionIndex(*MI);
  381. LiveInterval::const_iterator I = LI.find(useIdx);
  382. assert(I != LI.end() && "Reg must be live-in to use.");
  383. return !I->end.isBlock() && SlotIndex::isSameInstr(I->end, useIdx);
  384. }
  385. return MI->killsRegister(Reg);
  386. }
  387. /// Test if the given register value, which is used by the given
  388. /// instruction, is killed by the given instruction. This looks through
  389. /// coalescable copies to see if the original value is potentially not killed.
  390. ///
  391. /// For example, in this code:
  392. ///
  393. /// %reg1034 = copy %reg1024
  394. /// %reg1035 = copy killed %reg1025
  395. /// %reg1036 = add killed %reg1034, killed %reg1035
  396. ///
  397. /// %reg1034 is not considered to be killed, since it is copied from a
  398. /// register which is not killed. Treating it as not killed lets the
  399. /// normal heuristics commute the (two-address) add, which lets
  400. /// coalescing eliminate the extra copy.
  401. ///
  402. /// If allowFalsePositives is true then likely kills are treated as kills even
  403. /// if it can't be proven that they are kills.
  404. static bool isKilled(MachineInstr &MI, unsigned Reg,
  405. const MachineRegisterInfo *MRI,
  406. const TargetInstrInfo *TII,
  407. LiveIntervals *LIS,
  408. bool allowFalsePositives) {
  409. MachineInstr *DefMI = &MI;
  410. while (true) {
  411. // All uses of physical registers are likely to be kills.
  412. if (Register::isPhysicalRegister(Reg) &&
  413. (allowFalsePositives || MRI->hasOneUse(Reg)))
  414. return true;
  415. if (!isPlainlyKilled(DefMI, Reg, LIS))
  416. return false;
  417. if (Register::isPhysicalRegister(Reg))
  418. return true;
  419. MachineRegisterInfo::def_iterator Begin = MRI->def_begin(Reg);
  420. // If there are multiple defs, we can't do a simple analysis, so just
  421. // go with what the kill flag says.
  422. if (std::next(Begin) != MRI->def_end())
  423. return true;
  424. DefMI = Begin->getParent();
  425. bool IsSrcPhys, IsDstPhys;
  426. unsigned SrcReg, DstReg;
  427. // If the def is something other than a copy, then it isn't going to
  428. // be coalesced, so follow the kill flag.
  429. if (!isCopyToReg(*DefMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
  430. return true;
  431. Reg = SrcReg;
  432. }
  433. }
  434. /// Return true if the specified MI uses the specified register as a two-address
  435. /// use. If so, return the destination register by reference.
  436. static bool isTwoAddrUse(MachineInstr &MI, unsigned Reg, unsigned &DstReg) {
  437. for (unsigned i = 0, NumOps = MI.getNumOperands(); i != NumOps; ++i) {
  438. const MachineOperand &MO = MI.getOperand(i);
  439. if (!MO.isReg() || !MO.isUse() || MO.getReg() != Reg)
  440. continue;
  441. unsigned ti;
  442. if (MI.isRegTiedToDefOperand(i, &ti)) {
  443. DstReg = MI.getOperand(ti).getReg();
  444. return true;
  445. }
  446. }
  447. return false;
  448. }
  449. /// Given a register, if has a single in-basic block use, return the use
  450. /// instruction if it's a copy or a two-address use.
  451. static
  452. MachineInstr *findOnlyInterestingUse(unsigned Reg, MachineBasicBlock *MBB,
  453. MachineRegisterInfo *MRI,
  454. const TargetInstrInfo *TII,
  455. bool &IsCopy,
  456. unsigned &DstReg, bool &IsDstPhys) {
  457. if (!MRI->hasOneNonDBGUse(Reg))
  458. // None or more than one use.
  459. return nullptr;
  460. MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(Reg);
  461. if (UseMI.getParent() != MBB)
  462. return nullptr;
  463. unsigned SrcReg;
  464. bool IsSrcPhys;
  465. if (isCopyToReg(UseMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys)) {
  466. IsCopy = true;
  467. return &UseMI;
  468. }
  469. IsDstPhys = false;
  470. if (isTwoAddrUse(UseMI, Reg, DstReg)) {
  471. IsDstPhys = Register::isPhysicalRegister(DstReg);
  472. return &UseMI;
  473. }
  474. return nullptr;
  475. }
  476. /// Return the physical register the specified virtual register might be mapped
  477. /// to.
  478. static unsigned
  479. getMappedReg(unsigned Reg, DenseMap<unsigned, unsigned> &RegMap) {
  480. while (Register::isVirtualRegister(Reg)) {
  481. DenseMap<unsigned, unsigned>::iterator SI = RegMap.find(Reg);
  482. if (SI == RegMap.end())
  483. return 0;
  484. Reg = SI->second;
  485. }
  486. if (Register::isPhysicalRegister(Reg))
  487. return Reg;
  488. return 0;
  489. }
  490. /// Return true if the two registers are equal or aliased.
  491. static bool
  492. regsAreCompatible(unsigned RegA, unsigned RegB, const TargetRegisterInfo *TRI) {
  493. if (RegA == RegB)
  494. return true;
  495. if (!RegA || !RegB)
  496. return false;
  497. return TRI->regsOverlap(RegA, RegB);
  498. }
  499. // Returns true if Reg is equal or aliased to at least one register in Set.
  500. static bool regOverlapsSet(const SmallVectorImpl<unsigned> &Set, unsigned Reg,
  501. const TargetRegisterInfo *TRI) {
  502. for (unsigned R : Set)
  503. if (TRI->regsOverlap(R, Reg))
  504. return true;
  505. return false;
  506. }
  507. /// Return true if it's potentially profitable to commute the two-address
  508. /// instruction that's being processed.
  509. bool
  510. TwoAddressInstructionPass::
  511. isProfitableToCommute(unsigned regA, unsigned regB, unsigned regC,
  512. MachineInstr *MI, unsigned Dist) {
  513. if (OptLevel == CodeGenOpt::None)
  514. return false;
  515. // Determine if it's profitable to commute this two address instruction. In
  516. // general, we want no uses between this instruction and the definition of
  517. // the two-address register.
  518. // e.g.
  519. // %reg1028 = EXTRACT_SUBREG killed %reg1027, 1
  520. // %reg1029 = COPY %reg1028
  521. // %reg1029 = SHR8ri %reg1029, 7, implicit dead %eflags
  522. // insert => %reg1030 = COPY %reg1028
  523. // %reg1030 = ADD8rr killed %reg1028, killed %reg1029, implicit dead %eflags
  524. // In this case, it might not be possible to coalesce the second COPY
  525. // instruction if the first one is coalesced. So it would be profitable to
  526. // commute it:
  527. // %reg1028 = EXTRACT_SUBREG killed %reg1027, 1
  528. // %reg1029 = COPY %reg1028
  529. // %reg1029 = SHR8ri %reg1029, 7, implicit dead %eflags
  530. // insert => %reg1030 = COPY %reg1029
  531. // %reg1030 = ADD8rr killed %reg1029, killed %reg1028, implicit dead %eflags
  532. if (!isPlainlyKilled(MI, regC, LIS))
  533. return false;
  534. // Ok, we have something like:
  535. // %reg1030 = ADD8rr killed %reg1028, killed %reg1029, implicit dead %eflags
  536. // let's see if it's worth commuting it.
  537. // Look for situations like this:
  538. // %reg1024 = MOV r1
  539. // %reg1025 = MOV r0
  540. // %reg1026 = ADD %reg1024, %reg1025
  541. // r0 = MOV %reg1026
  542. // Commute the ADD to hopefully eliminate an otherwise unavoidable copy.
  543. unsigned ToRegA = getMappedReg(regA, DstRegMap);
  544. if (ToRegA) {
  545. unsigned FromRegB = getMappedReg(regB, SrcRegMap);
  546. unsigned FromRegC = getMappedReg(regC, SrcRegMap);
  547. bool CompB = FromRegB && regsAreCompatible(FromRegB, ToRegA, TRI);
  548. bool CompC = FromRegC && regsAreCompatible(FromRegC, ToRegA, TRI);
  549. // Compute if any of the following are true:
  550. // -RegB is not tied to a register and RegC is compatible with RegA.
  551. // -RegB is tied to the wrong physical register, but RegC is.
  552. // -RegB is tied to the wrong physical register, and RegC isn't tied.
  553. if ((!FromRegB && CompC) || (FromRegB && !CompB && (!FromRegC || CompC)))
  554. return true;
  555. // Don't compute if any of the following are true:
  556. // -RegC is not tied to a register and RegB is compatible with RegA.
  557. // -RegC is tied to the wrong physical register, but RegB is.
  558. // -RegC is tied to the wrong physical register, and RegB isn't tied.
  559. if ((!FromRegC && CompB) || (FromRegC && !CompC && (!FromRegB || CompB)))
  560. return false;
  561. }
  562. // If there is a use of regC between its last def (could be livein) and this
  563. // instruction, then bail.
  564. unsigned LastDefC = 0;
  565. if (!noUseAfterLastDef(regC, Dist, LastDefC))
  566. return false;
  567. // If there is a use of regB between its last def (could be livein) and this
  568. // instruction, then go ahead and make this transformation.
  569. unsigned LastDefB = 0;
  570. if (!noUseAfterLastDef(regB, Dist, LastDefB))
  571. return true;
  572. // Look for situation like this:
  573. // %reg101 = MOV %reg100
  574. // %reg102 = ...
  575. // %reg103 = ADD %reg102, %reg101
  576. // ... = %reg103 ...
  577. // %reg100 = MOV %reg103
  578. // If there is a reversed copy chain from reg101 to reg103, commute the ADD
  579. // to eliminate an otherwise unavoidable copy.
  580. // FIXME:
  581. // We can extend the logic further: If an pair of operands in an insn has
  582. // been merged, the insn could be regarded as a virtual copy, and the virtual
  583. // copy could also be used to construct a copy chain.
  584. // To more generally minimize register copies, ideally the logic of two addr
  585. // instruction pass should be integrated with register allocation pass where
  586. // interference graph is available.
  587. if (isRevCopyChain(regC, regA, MaxDataFlowEdge))
  588. return true;
  589. if (isRevCopyChain(regB, regA, MaxDataFlowEdge))
  590. return false;
  591. // Since there are no intervening uses for both registers, then commute
  592. // if the def of regC is closer. Its live interval is shorter.
  593. return LastDefB && LastDefC && LastDefC > LastDefB;
  594. }
  595. /// Commute a two-address instruction and update the basic block, distance map,
  596. /// and live variables if needed. Return true if it is successful.
  597. bool TwoAddressInstructionPass::commuteInstruction(MachineInstr *MI,
  598. unsigned DstIdx,
  599. unsigned RegBIdx,
  600. unsigned RegCIdx,
  601. unsigned Dist) {
  602. Register RegC = MI->getOperand(RegCIdx).getReg();
  603. LLVM_DEBUG(dbgs() << "2addr: COMMUTING : " << *MI);
  604. MachineInstr *NewMI = TII->commuteInstruction(*MI, false, RegBIdx, RegCIdx);
  605. if (NewMI == nullptr) {
  606. LLVM_DEBUG(dbgs() << "2addr: COMMUTING FAILED!\n");
  607. return false;
  608. }
  609. LLVM_DEBUG(dbgs() << "2addr: COMMUTED TO: " << *NewMI);
  610. assert(NewMI == MI &&
  611. "TargetInstrInfo::commuteInstruction() should not return a new "
  612. "instruction unless it was requested.");
  613. // Update source register map.
  614. unsigned FromRegC = getMappedReg(RegC, SrcRegMap);
  615. if (FromRegC) {
  616. Register RegA = MI->getOperand(DstIdx).getReg();
  617. SrcRegMap[RegA] = FromRegC;
  618. }
  619. return true;
  620. }
  621. /// Return true if it is profitable to convert the given 2-address instruction
  622. /// to a 3-address one.
  623. bool
  624. TwoAddressInstructionPass::isProfitableToConv3Addr(unsigned RegA,unsigned RegB){
  625. // Look for situations like this:
  626. // %reg1024 = MOV r1
  627. // %reg1025 = MOV r0
  628. // %reg1026 = ADD %reg1024, %reg1025
  629. // r2 = MOV %reg1026
  630. // Turn ADD into a 3-address instruction to avoid a copy.
  631. unsigned FromRegB = getMappedReg(RegB, SrcRegMap);
  632. if (!FromRegB)
  633. return false;
  634. unsigned ToRegA = getMappedReg(RegA, DstRegMap);
  635. return (ToRegA && !regsAreCompatible(FromRegB, ToRegA, TRI));
  636. }
  637. /// Convert the specified two-address instruction into a three address one.
  638. /// Return true if this transformation was successful.
  639. bool
  640. TwoAddressInstructionPass::convertInstTo3Addr(MachineBasicBlock::iterator &mi,
  641. MachineBasicBlock::iterator &nmi,
  642. unsigned RegA, unsigned RegB,
  643. unsigned Dist) {
  644. // FIXME: Why does convertToThreeAddress() need an iterator reference?
  645. MachineFunction::iterator MFI = MBB->getIterator();
  646. MachineInstr *NewMI = TII->convertToThreeAddress(MFI, *mi, LV);
  647. assert(MBB->getIterator() == MFI &&
  648. "convertToThreeAddress changed iterator reference");
  649. if (!NewMI)
  650. return false;
  651. LLVM_DEBUG(dbgs() << "2addr: CONVERTING 2-ADDR: " << *mi);
  652. LLVM_DEBUG(dbgs() << "2addr: TO 3-ADDR: " << *NewMI);
  653. bool Sunk = false;
  654. if (LIS)
  655. LIS->ReplaceMachineInstrInMaps(*mi, *NewMI);
  656. if (NewMI->findRegisterUseOperand(RegB, false, TRI))
  657. // FIXME: Temporary workaround. If the new instruction doesn't
  658. // uses RegB, convertToThreeAddress must have created more
  659. // then one instruction.
  660. Sunk = sink3AddrInstruction(NewMI, RegB, mi);
  661. MBB->erase(mi); // Nuke the old inst.
  662. if (!Sunk) {
  663. DistanceMap.insert(std::make_pair(NewMI, Dist));
  664. mi = NewMI;
  665. nmi = std::next(mi);
  666. }
  667. else
  668. SunkInstrs.insert(NewMI);
  669. // Update source and destination register maps.
  670. SrcRegMap.erase(RegA);
  671. DstRegMap.erase(RegB);
  672. return true;
  673. }
  674. /// Scan forward recursively for only uses, update maps if the use is a copy or
  675. /// a two-address instruction.
  676. void
  677. TwoAddressInstructionPass::scanUses(unsigned DstReg) {
  678. SmallVector<unsigned, 4> VirtRegPairs;
  679. bool IsDstPhys;
  680. bool IsCopy = false;
  681. unsigned NewReg = 0;
  682. unsigned Reg = DstReg;
  683. while (MachineInstr *UseMI = findOnlyInterestingUse(Reg, MBB, MRI, TII,IsCopy,
  684. NewReg, IsDstPhys)) {
  685. if (IsCopy && !Processed.insert(UseMI).second)
  686. break;
  687. DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI);
  688. if (DI != DistanceMap.end())
  689. // Earlier in the same MBB.Reached via a back edge.
  690. break;
  691. if (IsDstPhys) {
  692. VirtRegPairs.push_back(NewReg);
  693. break;
  694. }
  695. bool isNew = SrcRegMap.insert(std::make_pair(NewReg, Reg)).second;
  696. if (!isNew)
  697. assert(SrcRegMap[NewReg] == Reg && "Can't map to two src registers!");
  698. VirtRegPairs.push_back(NewReg);
  699. Reg = NewReg;
  700. }
  701. if (!VirtRegPairs.empty()) {
  702. unsigned ToReg = VirtRegPairs.back();
  703. VirtRegPairs.pop_back();
  704. while (!VirtRegPairs.empty()) {
  705. unsigned FromReg = VirtRegPairs.back();
  706. VirtRegPairs.pop_back();
  707. bool isNew = DstRegMap.insert(std::make_pair(FromReg, ToReg)).second;
  708. if (!isNew)
  709. assert(DstRegMap[FromReg] == ToReg &&"Can't map to two dst registers!");
  710. ToReg = FromReg;
  711. }
  712. bool isNew = DstRegMap.insert(std::make_pair(DstReg, ToReg)).second;
  713. if (!isNew)
  714. assert(DstRegMap[DstReg] == ToReg && "Can't map to two dst registers!");
  715. }
  716. }
  717. /// If the specified instruction is not yet processed, process it if it's a
  718. /// copy. For a copy instruction, we find the physical registers the
  719. /// source and destination registers might be mapped to. These are kept in
  720. /// point-to maps used to determine future optimizations. e.g.
  721. /// v1024 = mov r0
  722. /// v1025 = mov r1
  723. /// v1026 = add v1024, v1025
  724. /// r1 = mov r1026
  725. /// If 'add' is a two-address instruction, v1024, v1026 are both potentially
  726. /// coalesced to r0 (from the input side). v1025 is mapped to r1. v1026 is
  727. /// potentially joined with r1 on the output side. It's worthwhile to commute
  728. /// 'add' to eliminate a copy.
  729. void TwoAddressInstructionPass::processCopy(MachineInstr *MI) {
  730. if (Processed.count(MI))
  731. return;
  732. bool IsSrcPhys, IsDstPhys;
  733. unsigned SrcReg, DstReg;
  734. if (!isCopyToReg(*MI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
  735. return;
  736. if (IsDstPhys && !IsSrcPhys)
  737. DstRegMap.insert(std::make_pair(SrcReg, DstReg));
  738. else if (!IsDstPhys && IsSrcPhys) {
  739. bool isNew = SrcRegMap.insert(std::make_pair(DstReg, SrcReg)).second;
  740. if (!isNew)
  741. assert(SrcRegMap[DstReg] == SrcReg &&
  742. "Can't map to two src physical registers!");
  743. scanUses(DstReg);
  744. }
  745. Processed.insert(MI);
  746. }
  747. /// If there is one more local instruction that reads 'Reg' and it kills 'Reg,
  748. /// consider moving the instruction below the kill instruction in order to
  749. /// eliminate the need for the copy.
  750. bool TwoAddressInstructionPass::
  751. rescheduleMIBelowKill(MachineBasicBlock::iterator &mi,
  752. MachineBasicBlock::iterator &nmi,
  753. unsigned Reg) {
  754. // Bail immediately if we don't have LV or LIS available. We use them to find
  755. // kills efficiently.
  756. if (!LV && !LIS)
  757. return false;
  758. MachineInstr *MI = &*mi;
  759. DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI);
  760. if (DI == DistanceMap.end())
  761. // Must be created from unfolded load. Don't waste time trying this.
  762. return false;
  763. MachineInstr *KillMI = nullptr;
  764. if (LIS) {
  765. LiveInterval &LI = LIS->getInterval(Reg);
  766. assert(LI.end() != LI.begin() &&
  767. "Reg should not have empty live interval.");
  768. SlotIndex MBBEndIdx = LIS->getMBBEndIdx(MBB).getPrevSlot();
  769. LiveInterval::const_iterator I = LI.find(MBBEndIdx);
  770. if (I != LI.end() && I->start < MBBEndIdx)
  771. return false;
  772. --I;
  773. KillMI = LIS->getInstructionFromIndex(I->end);
  774. } else {
  775. KillMI = LV->getVarInfo(Reg).findKill(MBB);
  776. }
  777. if (!KillMI || MI == KillMI || KillMI->isCopy() || KillMI->isCopyLike())
  778. // Don't mess with copies, they may be coalesced later.
  779. return false;
  780. if (KillMI->hasUnmodeledSideEffects() || KillMI->isCall() ||
  781. KillMI->isBranch() || KillMI->isTerminator())
  782. // Don't move pass calls, etc.
  783. return false;
  784. unsigned DstReg;
  785. if (isTwoAddrUse(*KillMI, Reg, DstReg))
  786. return false;
  787. bool SeenStore = true;
  788. if (!MI->isSafeToMove(AA, SeenStore))
  789. return false;
  790. if (TII->getInstrLatency(InstrItins, *MI) > 1)
  791. // FIXME: Needs more sophisticated heuristics.
  792. return false;
  793. SmallVector<unsigned, 2> Uses;
  794. SmallVector<unsigned, 2> Kills;
  795. SmallVector<unsigned, 2> Defs;
  796. for (const MachineOperand &MO : MI->operands()) {
  797. if (!MO.isReg())
  798. continue;
  799. Register MOReg = MO.getReg();
  800. if (!MOReg)
  801. continue;
  802. if (MO.isDef())
  803. Defs.push_back(MOReg);
  804. else {
  805. Uses.push_back(MOReg);
  806. if (MOReg != Reg && (MO.isKill() ||
  807. (LIS && isPlainlyKilled(MI, MOReg, LIS))))
  808. Kills.push_back(MOReg);
  809. }
  810. }
  811. // Move the copies connected to MI down as well.
  812. MachineBasicBlock::iterator Begin = MI;
  813. MachineBasicBlock::iterator AfterMI = std::next(Begin);
  814. MachineBasicBlock::iterator End = AfterMI;
  815. while (End != MBB->end()) {
  816. End = skipDebugInstructionsForward(End, MBB->end());
  817. if (End->isCopy() && regOverlapsSet(Defs, End->getOperand(1).getReg(), TRI))
  818. Defs.push_back(End->getOperand(0).getReg());
  819. else
  820. break;
  821. ++End;
  822. }
  823. // Check if the reschedule will not break dependencies.
  824. unsigned NumVisited = 0;
  825. MachineBasicBlock::iterator KillPos = KillMI;
  826. ++KillPos;
  827. for (MachineInstr &OtherMI : make_range(End, KillPos)) {
  828. // Debug instructions cannot be counted against the limit.
  829. if (OtherMI.isDebugInstr())
  830. continue;
  831. if (NumVisited > 10) // FIXME: Arbitrary limit to reduce compile time cost.
  832. return false;
  833. ++NumVisited;
  834. if (OtherMI.hasUnmodeledSideEffects() || OtherMI.isCall() ||
  835. OtherMI.isBranch() || OtherMI.isTerminator())
  836. // Don't move pass calls, etc.
  837. return false;
  838. for (const MachineOperand &MO : OtherMI.operands()) {
  839. if (!MO.isReg())
  840. continue;
  841. Register MOReg = MO.getReg();
  842. if (!MOReg)
  843. continue;
  844. if (MO.isDef()) {
  845. if (regOverlapsSet(Uses, MOReg, TRI))
  846. // Physical register use would be clobbered.
  847. return false;
  848. if (!MO.isDead() && regOverlapsSet(Defs, MOReg, TRI))
  849. // May clobber a physical register def.
  850. // FIXME: This may be too conservative. It's ok if the instruction
  851. // is sunken completely below the use.
  852. return false;
  853. } else {
  854. if (regOverlapsSet(Defs, MOReg, TRI))
  855. return false;
  856. bool isKill =
  857. MO.isKill() || (LIS && isPlainlyKilled(&OtherMI, MOReg, LIS));
  858. if (MOReg != Reg && ((isKill && regOverlapsSet(Uses, MOReg, TRI)) ||
  859. regOverlapsSet(Kills, MOReg, TRI)))
  860. // Don't want to extend other live ranges and update kills.
  861. return false;
  862. if (MOReg == Reg && !isKill)
  863. // We can't schedule across a use of the register in question.
  864. return false;
  865. // Ensure that if this is register in question, its the kill we expect.
  866. assert((MOReg != Reg || &OtherMI == KillMI) &&
  867. "Found multiple kills of a register in a basic block");
  868. }
  869. }
  870. }
  871. // Move debug info as well.
  872. while (Begin != MBB->begin() && std::prev(Begin)->isDebugInstr())
  873. --Begin;
  874. nmi = End;
  875. MachineBasicBlock::iterator InsertPos = KillPos;
  876. if (LIS) {
  877. // We have to move the copies first so that the MBB is still well-formed
  878. // when calling handleMove().
  879. for (MachineBasicBlock::iterator MBBI = AfterMI; MBBI != End;) {
  880. auto CopyMI = MBBI++;
  881. MBB->splice(InsertPos, MBB, CopyMI);
  882. LIS->handleMove(*CopyMI);
  883. InsertPos = CopyMI;
  884. }
  885. End = std::next(MachineBasicBlock::iterator(MI));
  886. }
  887. // Copies following MI may have been moved as well.
  888. MBB->splice(InsertPos, MBB, Begin, End);
  889. DistanceMap.erase(DI);
  890. // Update live variables
  891. if (LIS) {
  892. LIS->handleMove(*MI);
  893. } else {
  894. LV->removeVirtualRegisterKilled(Reg, *KillMI);
  895. LV->addVirtualRegisterKilled(Reg, *MI);
  896. }
  897. LLVM_DEBUG(dbgs() << "\trescheduled below kill: " << *KillMI);
  898. return true;
  899. }
  900. /// Return true if the re-scheduling will put the given instruction too close
  901. /// to the defs of its register dependencies.
  902. bool TwoAddressInstructionPass::isDefTooClose(unsigned Reg, unsigned Dist,
  903. MachineInstr *MI) {
  904. for (MachineInstr &DefMI : MRI->def_instructions(Reg)) {
  905. if (DefMI.getParent() != MBB || DefMI.isCopy() || DefMI.isCopyLike())
  906. continue;
  907. if (&DefMI == MI)
  908. return true; // MI is defining something KillMI uses
  909. DenseMap<MachineInstr*, unsigned>::iterator DDI = DistanceMap.find(&DefMI);
  910. if (DDI == DistanceMap.end())
  911. return true; // Below MI
  912. unsigned DefDist = DDI->second;
  913. assert(Dist > DefDist && "Visited def already?");
  914. if (TII->getInstrLatency(InstrItins, DefMI) > (Dist - DefDist))
  915. return true;
  916. }
  917. return false;
  918. }
  919. /// If there is one more local instruction that reads 'Reg' and it kills 'Reg,
  920. /// consider moving the kill instruction above the current two-address
  921. /// instruction in order to eliminate the need for the copy.
  922. bool TwoAddressInstructionPass::
  923. rescheduleKillAboveMI(MachineBasicBlock::iterator &mi,
  924. MachineBasicBlock::iterator &nmi,
  925. unsigned Reg) {
  926. // Bail immediately if we don't have LV or LIS available. We use them to find
  927. // kills efficiently.
  928. if (!LV && !LIS)
  929. return false;
  930. MachineInstr *MI = &*mi;
  931. DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI);
  932. if (DI == DistanceMap.end())
  933. // Must be created from unfolded load. Don't waste time trying this.
  934. return false;
  935. MachineInstr *KillMI = nullptr;
  936. if (LIS) {
  937. LiveInterval &LI = LIS->getInterval(Reg);
  938. assert(LI.end() != LI.begin() &&
  939. "Reg should not have empty live interval.");
  940. SlotIndex MBBEndIdx = LIS->getMBBEndIdx(MBB).getPrevSlot();
  941. LiveInterval::const_iterator I = LI.find(MBBEndIdx);
  942. if (I != LI.end() && I->start < MBBEndIdx)
  943. return false;
  944. --I;
  945. KillMI = LIS->getInstructionFromIndex(I->end);
  946. } else {
  947. KillMI = LV->getVarInfo(Reg).findKill(MBB);
  948. }
  949. if (!KillMI || MI == KillMI || KillMI->isCopy() || KillMI->isCopyLike())
  950. // Don't mess with copies, they may be coalesced later.
  951. return false;
  952. unsigned DstReg;
  953. if (isTwoAddrUse(*KillMI, Reg, DstReg))
  954. return false;
  955. bool SeenStore = true;
  956. if (!KillMI->isSafeToMove(AA, SeenStore))
  957. return false;
  958. SmallSet<unsigned, 2> Uses;
  959. SmallSet<unsigned, 2> Kills;
  960. SmallSet<unsigned, 2> Defs;
  961. SmallSet<unsigned, 2> LiveDefs;
  962. for (const MachineOperand &MO : KillMI->operands()) {
  963. if (!MO.isReg())
  964. continue;
  965. Register MOReg = MO.getReg();
  966. if (MO.isUse()) {
  967. if (!MOReg)
  968. continue;
  969. if (isDefTooClose(MOReg, DI->second, MI))
  970. return false;
  971. bool isKill = MO.isKill() || (LIS && isPlainlyKilled(KillMI, MOReg, LIS));
  972. if (MOReg == Reg && !isKill)
  973. return false;
  974. Uses.insert(MOReg);
  975. if (isKill && MOReg != Reg)
  976. Kills.insert(MOReg);
  977. } else if (Register::isPhysicalRegister(MOReg)) {
  978. Defs.insert(MOReg);
  979. if (!MO.isDead())
  980. LiveDefs.insert(MOReg);
  981. }
  982. }
  983. // Check if the reschedule will not break depedencies.
  984. unsigned NumVisited = 0;
  985. for (MachineInstr &OtherMI :
  986. make_range(mi, MachineBasicBlock::iterator(KillMI))) {
  987. // Debug instructions cannot be counted against the limit.
  988. if (OtherMI.isDebugInstr())
  989. continue;
  990. if (NumVisited > 10) // FIXME: Arbitrary limit to reduce compile time cost.
  991. return false;
  992. ++NumVisited;
  993. if (OtherMI.hasUnmodeledSideEffects() || OtherMI.isCall() ||
  994. OtherMI.isBranch() || OtherMI.isTerminator())
  995. // Don't move pass calls, etc.
  996. return false;
  997. SmallVector<unsigned, 2> OtherDefs;
  998. for (const MachineOperand &MO : OtherMI.operands()) {
  999. if (!MO.isReg())
  1000. continue;
  1001. Register MOReg = MO.getReg();
  1002. if (!MOReg)
  1003. continue;
  1004. if (MO.isUse()) {
  1005. if (Defs.count(MOReg))
  1006. // Moving KillMI can clobber the physical register if the def has
  1007. // not been seen.
  1008. return false;
  1009. if (Kills.count(MOReg))
  1010. // Don't want to extend other live ranges and update kills.
  1011. return false;
  1012. if (&OtherMI != MI && MOReg == Reg &&
  1013. !(MO.isKill() || (LIS && isPlainlyKilled(&OtherMI, MOReg, LIS))))
  1014. // We can't schedule across a use of the register in question.
  1015. return false;
  1016. } else {
  1017. OtherDefs.push_back(MOReg);
  1018. }
  1019. }
  1020. for (unsigned i = 0, e = OtherDefs.size(); i != e; ++i) {
  1021. unsigned MOReg = OtherDefs[i];
  1022. if (Uses.count(MOReg))
  1023. return false;
  1024. if (Register::isPhysicalRegister(MOReg) && LiveDefs.count(MOReg))
  1025. return false;
  1026. // Physical register def is seen.
  1027. Defs.erase(MOReg);
  1028. }
  1029. }
  1030. // Move the old kill above MI, don't forget to move debug info as well.
  1031. MachineBasicBlock::iterator InsertPos = mi;
  1032. while (InsertPos != MBB->begin() && std::prev(InsertPos)->isDebugInstr())
  1033. --InsertPos;
  1034. MachineBasicBlock::iterator From = KillMI;
  1035. MachineBasicBlock::iterator To = std::next(From);
  1036. while (std::prev(From)->isDebugInstr())
  1037. --From;
  1038. MBB->splice(InsertPos, MBB, From, To);
  1039. nmi = std::prev(InsertPos); // Backtrack so we process the moved instr.
  1040. DistanceMap.erase(DI);
  1041. // Update live variables
  1042. if (LIS) {
  1043. LIS->handleMove(*KillMI);
  1044. } else {
  1045. LV->removeVirtualRegisterKilled(Reg, *KillMI);
  1046. LV->addVirtualRegisterKilled(Reg, *MI);
  1047. }
  1048. LLVM_DEBUG(dbgs() << "\trescheduled kill: " << *KillMI);
  1049. return true;
  1050. }
  1051. /// Tries to commute the operand 'BaseOpIdx' and some other operand in the
  1052. /// given machine instruction to improve opportunities for coalescing and
  1053. /// elimination of a register to register copy.
  1054. ///
  1055. /// 'DstOpIdx' specifies the index of MI def operand.
  1056. /// 'BaseOpKilled' specifies if the register associated with 'BaseOpIdx'
  1057. /// operand is killed by the given instruction.
  1058. /// The 'Dist' arguments provides the distance of MI from the start of the
  1059. /// current basic block and it is used to determine if it is profitable
  1060. /// to commute operands in the instruction.
  1061. ///
  1062. /// Returns true if the transformation happened. Otherwise, returns false.
  1063. bool TwoAddressInstructionPass::tryInstructionCommute(MachineInstr *MI,
  1064. unsigned DstOpIdx,
  1065. unsigned BaseOpIdx,
  1066. bool BaseOpKilled,
  1067. unsigned Dist) {
  1068. if (!MI->isCommutable())
  1069. return false;
  1070. bool MadeChange = false;
  1071. Register DstOpReg = MI->getOperand(DstOpIdx).getReg();
  1072. Register BaseOpReg = MI->getOperand(BaseOpIdx).getReg();
  1073. unsigned OpsNum = MI->getDesc().getNumOperands();
  1074. unsigned OtherOpIdx = MI->getDesc().getNumDefs();
  1075. for (; OtherOpIdx < OpsNum; OtherOpIdx++) {
  1076. // The call of findCommutedOpIndices below only checks if BaseOpIdx
  1077. // and OtherOpIdx are commutable, it does not really search for
  1078. // other commutable operands and does not change the values of passed
  1079. // variables.
  1080. if (OtherOpIdx == BaseOpIdx || !MI->getOperand(OtherOpIdx).isReg() ||
  1081. !TII->findCommutedOpIndices(*MI, BaseOpIdx, OtherOpIdx))
  1082. continue;
  1083. Register OtherOpReg = MI->getOperand(OtherOpIdx).getReg();
  1084. bool AggressiveCommute = false;
  1085. // If OtherOp dies but BaseOp does not, swap the OtherOp and BaseOp
  1086. // operands. This makes the live ranges of DstOp and OtherOp joinable.
  1087. bool OtherOpKilled = isKilled(*MI, OtherOpReg, MRI, TII, LIS, false);
  1088. bool DoCommute = !BaseOpKilled && OtherOpKilled;
  1089. if (!DoCommute &&
  1090. isProfitableToCommute(DstOpReg, BaseOpReg, OtherOpReg, MI, Dist)) {
  1091. DoCommute = true;
  1092. AggressiveCommute = true;
  1093. }
  1094. // If it's profitable to commute, try to do so.
  1095. if (DoCommute && commuteInstruction(MI, DstOpIdx, BaseOpIdx, OtherOpIdx,
  1096. Dist)) {
  1097. MadeChange = true;
  1098. ++NumCommuted;
  1099. if (AggressiveCommute) {
  1100. ++NumAggrCommuted;
  1101. // There might be more than two commutable operands, update BaseOp and
  1102. // continue scanning.
  1103. // FIXME: This assumes that the new instruction's operands are in the
  1104. // same positions and were simply swapped.
  1105. BaseOpReg = OtherOpReg;
  1106. BaseOpKilled = OtherOpKilled;
  1107. // Resamples OpsNum in case the number of operands was reduced. This
  1108. // happens with X86.
  1109. OpsNum = MI->getDesc().getNumOperands();
  1110. continue;
  1111. }
  1112. // If this was a commute based on kill, we won't do better continuing.
  1113. return MadeChange;
  1114. }
  1115. }
  1116. return MadeChange;
  1117. }
  1118. /// For the case where an instruction has a single pair of tied register
  1119. /// operands, attempt some transformations that may either eliminate the tied
  1120. /// operands or improve the opportunities for coalescing away the register copy.
  1121. /// Returns true if no copy needs to be inserted to untie mi's operands
  1122. /// (either because they were untied, or because mi was rescheduled, and will
  1123. /// be visited again later). If the shouldOnlyCommute flag is true, only
  1124. /// instruction commutation is attempted.
  1125. bool TwoAddressInstructionPass::
  1126. tryInstructionTransform(MachineBasicBlock::iterator &mi,
  1127. MachineBasicBlock::iterator &nmi,
  1128. unsigned SrcIdx, unsigned DstIdx,
  1129. unsigned Dist, bool shouldOnlyCommute) {
  1130. if (OptLevel == CodeGenOpt::None)
  1131. return false;
  1132. MachineInstr &MI = *mi;
  1133. Register regA = MI.getOperand(DstIdx).getReg();
  1134. Register regB = MI.getOperand(SrcIdx).getReg();
  1135. assert(Register::isVirtualRegister(regB) &&
  1136. "cannot make instruction into two-address form");
  1137. bool regBKilled = isKilled(MI, regB, MRI, TII, LIS, true);
  1138. if (Register::isVirtualRegister(regA))
  1139. scanUses(regA);
  1140. bool Commuted = tryInstructionCommute(&MI, DstIdx, SrcIdx, regBKilled, Dist);
  1141. // If the instruction is convertible to 3 Addr, instead
  1142. // of returning try 3 Addr transformation aggresively and
  1143. // use this variable to check later. Because it might be better.
  1144. // For example, we can just use `leal (%rsi,%rdi), %eax` and `ret`
  1145. // instead of the following code.
  1146. // addl %esi, %edi
  1147. // movl %edi, %eax
  1148. // ret
  1149. if (Commuted && !MI.isConvertibleTo3Addr())
  1150. return false;
  1151. if (shouldOnlyCommute)
  1152. return false;
  1153. // If there is one more use of regB later in the same MBB, consider
  1154. // re-schedule this MI below it.
  1155. if (!Commuted && EnableRescheduling && rescheduleMIBelowKill(mi, nmi, regB)) {
  1156. ++NumReSchedDowns;
  1157. return true;
  1158. }
  1159. // If we commuted, regB may have changed so we should re-sample it to avoid
  1160. // confusing the three address conversion below.
  1161. if (Commuted) {
  1162. regB = MI.getOperand(SrcIdx).getReg();
  1163. regBKilled = isKilled(MI, regB, MRI, TII, LIS, true);
  1164. }
  1165. if (MI.isConvertibleTo3Addr()) {
  1166. // This instruction is potentially convertible to a true
  1167. // three-address instruction. Check if it is profitable.
  1168. if (!regBKilled || isProfitableToConv3Addr(regA, regB)) {
  1169. // Try to convert it.
  1170. if (convertInstTo3Addr(mi, nmi, regA, regB, Dist)) {
  1171. ++NumConvertedTo3Addr;
  1172. return true; // Done with this instruction.
  1173. }
  1174. }
  1175. }
  1176. // Return if it is commuted but 3 addr conversion is failed.
  1177. if (Commuted)
  1178. return false;
  1179. // If there is one more use of regB later in the same MBB, consider
  1180. // re-schedule it before this MI if it's legal.
  1181. if (EnableRescheduling && rescheduleKillAboveMI(mi, nmi, regB)) {
  1182. ++NumReSchedUps;
  1183. return true;
  1184. }
  1185. // If this is an instruction with a load folded into it, try unfolding
  1186. // the load, e.g. avoid this:
  1187. // movq %rdx, %rcx
  1188. // addq (%rax), %rcx
  1189. // in favor of this:
  1190. // movq (%rax), %rcx
  1191. // addq %rdx, %rcx
  1192. // because it's preferable to schedule a load than a register copy.
  1193. if (MI.mayLoad() && !regBKilled) {
  1194. // Determine if a load can be unfolded.
  1195. unsigned LoadRegIndex;
  1196. unsigned NewOpc =
  1197. TII->getOpcodeAfterMemoryUnfold(MI.getOpcode(),
  1198. /*UnfoldLoad=*/true,
  1199. /*UnfoldStore=*/false,
  1200. &LoadRegIndex);
  1201. if (NewOpc != 0) {
  1202. const MCInstrDesc &UnfoldMCID = TII->get(NewOpc);
  1203. if (UnfoldMCID.getNumDefs() == 1) {
  1204. // Unfold the load.
  1205. LLVM_DEBUG(dbgs() << "2addr: UNFOLDING: " << MI);
  1206. const TargetRegisterClass *RC =
  1207. TRI->getAllocatableClass(
  1208. TII->getRegClass(UnfoldMCID, LoadRegIndex, TRI, *MF));
  1209. Register Reg = MRI->createVirtualRegister(RC);
  1210. SmallVector<MachineInstr *, 2> NewMIs;
  1211. if (!TII->unfoldMemoryOperand(*MF, MI, Reg,
  1212. /*UnfoldLoad=*/true,
  1213. /*UnfoldStore=*/false, NewMIs)) {
  1214. LLVM_DEBUG(dbgs() << "2addr: ABANDONING UNFOLD\n");
  1215. return false;
  1216. }
  1217. assert(NewMIs.size() == 2 &&
  1218. "Unfolded a load into multiple instructions!");
  1219. // The load was previously folded, so this is the only use.
  1220. NewMIs[1]->addRegisterKilled(Reg, TRI);
  1221. // Tentatively insert the instructions into the block so that they
  1222. // look "normal" to the transformation logic.
  1223. MBB->insert(mi, NewMIs[0]);
  1224. MBB->insert(mi, NewMIs[1]);
  1225. LLVM_DEBUG(dbgs() << "2addr: NEW LOAD: " << *NewMIs[0]
  1226. << "2addr: NEW INST: " << *NewMIs[1]);
  1227. // Transform the instruction, now that it no longer has a load.
  1228. unsigned NewDstIdx = NewMIs[1]->findRegisterDefOperandIdx(regA);
  1229. unsigned NewSrcIdx = NewMIs[1]->findRegisterUseOperandIdx(regB);
  1230. MachineBasicBlock::iterator NewMI = NewMIs[1];
  1231. bool TransformResult =
  1232. tryInstructionTransform(NewMI, mi, NewSrcIdx, NewDstIdx, Dist, true);
  1233. (void)TransformResult;
  1234. assert(!TransformResult &&
  1235. "tryInstructionTransform() should return false.");
  1236. if (NewMIs[1]->getOperand(NewSrcIdx).isKill()) {
  1237. // Success, or at least we made an improvement. Keep the unfolded
  1238. // instructions and discard the original.
  1239. if (LV) {
  1240. for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
  1241. MachineOperand &MO = MI.getOperand(i);
  1242. if (MO.isReg() && Register::isVirtualRegister(MO.getReg())) {
  1243. if (MO.isUse()) {
  1244. if (MO.isKill()) {
  1245. if (NewMIs[0]->killsRegister(MO.getReg()))
  1246. LV->replaceKillInstruction(MO.getReg(), MI, *NewMIs[0]);
  1247. else {
  1248. assert(NewMIs[1]->killsRegister(MO.getReg()) &&
  1249. "Kill missing after load unfold!");
  1250. LV->replaceKillInstruction(MO.getReg(), MI, *NewMIs[1]);
  1251. }
  1252. }
  1253. } else if (LV->removeVirtualRegisterDead(MO.getReg(), MI)) {
  1254. if (NewMIs[1]->registerDefIsDead(MO.getReg()))
  1255. LV->addVirtualRegisterDead(MO.getReg(), *NewMIs[1]);
  1256. else {
  1257. assert(NewMIs[0]->registerDefIsDead(MO.getReg()) &&
  1258. "Dead flag missing after load unfold!");
  1259. LV->addVirtualRegisterDead(MO.getReg(), *NewMIs[0]);
  1260. }
  1261. }
  1262. }
  1263. }
  1264. LV->addVirtualRegisterKilled(Reg, *NewMIs[1]);
  1265. }
  1266. SmallVector<unsigned, 4> OrigRegs;
  1267. if (LIS) {
  1268. for (const MachineOperand &MO : MI.operands()) {
  1269. if (MO.isReg())
  1270. OrigRegs.push_back(MO.getReg());
  1271. }
  1272. }
  1273. MI.eraseFromParent();
  1274. // Update LiveIntervals.
  1275. if (LIS) {
  1276. MachineBasicBlock::iterator Begin(NewMIs[0]);
  1277. MachineBasicBlock::iterator End(NewMIs[1]);
  1278. LIS->repairIntervalsInRange(MBB, Begin, End, OrigRegs);
  1279. }
  1280. mi = NewMIs[1];
  1281. } else {
  1282. // Transforming didn't eliminate the tie and didn't lead to an
  1283. // improvement. Clean up the unfolded instructions and keep the
  1284. // original.
  1285. LLVM_DEBUG(dbgs() << "2addr: ABANDONING UNFOLD\n");
  1286. NewMIs[0]->eraseFromParent();
  1287. NewMIs[1]->eraseFromParent();
  1288. }
  1289. }
  1290. }
  1291. }
  1292. return false;
  1293. }
  1294. // Collect tied operands of MI that need to be handled.
  1295. // Rewrite trivial cases immediately.
  1296. // Return true if any tied operands where found, including the trivial ones.
  1297. bool TwoAddressInstructionPass::
  1298. collectTiedOperands(MachineInstr *MI, TiedOperandMap &TiedOperands) {
  1299. const MCInstrDesc &MCID = MI->getDesc();
  1300. bool AnyOps = false;
  1301. unsigned NumOps = MI->getNumOperands();
  1302. for (unsigned SrcIdx = 0; SrcIdx < NumOps; ++SrcIdx) {
  1303. unsigned DstIdx = 0;
  1304. if (!MI->isRegTiedToDefOperand(SrcIdx, &DstIdx))
  1305. continue;
  1306. AnyOps = true;
  1307. MachineOperand &SrcMO = MI->getOperand(SrcIdx);
  1308. MachineOperand &DstMO = MI->getOperand(DstIdx);
  1309. Register SrcReg = SrcMO.getReg();
  1310. Register DstReg = DstMO.getReg();
  1311. // Tied constraint already satisfied?
  1312. if (SrcReg == DstReg)
  1313. continue;
  1314. assert(SrcReg && SrcMO.isUse() && "two address instruction invalid");
  1315. // Deal with undef uses immediately - simply rewrite the src operand.
  1316. if (SrcMO.isUndef() && !DstMO.getSubReg()) {
  1317. // Constrain the DstReg register class if required.
  1318. if (Register::isVirtualRegister(DstReg))
  1319. if (const TargetRegisterClass *RC = TII->getRegClass(MCID, SrcIdx,
  1320. TRI, *MF))
  1321. MRI->constrainRegClass(DstReg, RC);
  1322. SrcMO.setReg(DstReg);
  1323. SrcMO.setSubReg(0);
  1324. LLVM_DEBUG(dbgs() << "\t\trewrite undef:\t" << *MI);
  1325. continue;
  1326. }
  1327. TiedOperands[SrcReg].push_back(std::make_pair(SrcIdx, DstIdx));
  1328. }
  1329. return AnyOps;
  1330. }
  1331. // Process a list of tied MI operands that all use the same source register.
  1332. // The tied pairs are of the form (SrcIdx, DstIdx).
  1333. void
  1334. TwoAddressInstructionPass::processTiedPairs(MachineInstr *MI,
  1335. TiedPairList &TiedPairs,
  1336. unsigned &Dist) {
  1337. bool IsEarlyClobber = false;
  1338. for (unsigned tpi = 0, tpe = TiedPairs.size(); tpi != tpe; ++tpi) {
  1339. const MachineOperand &DstMO = MI->getOperand(TiedPairs[tpi].second);
  1340. IsEarlyClobber |= DstMO.isEarlyClobber();
  1341. }
  1342. bool RemovedKillFlag = false;
  1343. bool AllUsesCopied = true;
  1344. unsigned LastCopiedReg = 0;
  1345. SlotIndex LastCopyIdx;
  1346. unsigned RegB = 0;
  1347. unsigned SubRegB = 0;
  1348. for (unsigned tpi = 0, tpe = TiedPairs.size(); tpi != tpe; ++tpi) {
  1349. unsigned SrcIdx = TiedPairs[tpi].first;
  1350. unsigned DstIdx = TiedPairs[tpi].second;
  1351. const MachineOperand &DstMO = MI->getOperand(DstIdx);
  1352. Register RegA = DstMO.getReg();
  1353. // Grab RegB from the instruction because it may have changed if the
  1354. // instruction was commuted.
  1355. RegB = MI->getOperand(SrcIdx).getReg();
  1356. SubRegB = MI->getOperand(SrcIdx).getSubReg();
  1357. if (RegA == RegB) {
  1358. // The register is tied to multiple destinations (or else we would
  1359. // not have continued this far), but this use of the register
  1360. // already matches the tied destination. Leave it.
  1361. AllUsesCopied = false;
  1362. continue;
  1363. }
  1364. LastCopiedReg = RegA;
  1365. assert(Register::isVirtualRegister(RegB) &&
  1366. "cannot make instruction into two-address form");
  1367. #ifndef NDEBUG
  1368. // First, verify that we don't have a use of "a" in the instruction
  1369. // (a = b + a for example) because our transformation will not
  1370. // work. This should never occur because we are in SSA form.
  1371. for (unsigned i = 0; i != MI->getNumOperands(); ++i)
  1372. assert(i == DstIdx ||
  1373. !MI->getOperand(i).isReg() ||
  1374. MI->getOperand(i).getReg() != RegA);
  1375. #endif
  1376. // Emit a copy.
  1377. MachineInstrBuilder MIB = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
  1378. TII->get(TargetOpcode::COPY), RegA);
  1379. // If this operand is folding a truncation, the truncation now moves to the
  1380. // copy so that the register classes remain valid for the operands.
  1381. MIB.addReg(RegB, 0, SubRegB);
  1382. const TargetRegisterClass *RC = MRI->getRegClass(RegB);
  1383. if (SubRegB) {
  1384. if (Register::isVirtualRegister(RegA)) {
  1385. assert(TRI->getMatchingSuperRegClass(RC, MRI->getRegClass(RegA),
  1386. SubRegB) &&
  1387. "tied subregister must be a truncation");
  1388. // The superreg class will not be used to constrain the subreg class.
  1389. RC = nullptr;
  1390. } else {
  1391. assert(TRI->getMatchingSuperReg(RegA, SubRegB, MRI->getRegClass(RegB))
  1392. && "tied subregister must be a truncation");
  1393. }
  1394. }
  1395. // Update DistanceMap.
  1396. MachineBasicBlock::iterator PrevMI = MI;
  1397. --PrevMI;
  1398. DistanceMap.insert(std::make_pair(&*PrevMI, Dist));
  1399. DistanceMap[MI] = ++Dist;
  1400. if (LIS) {
  1401. LastCopyIdx = LIS->InsertMachineInstrInMaps(*PrevMI).getRegSlot();
  1402. if (Register::isVirtualRegister(RegA)) {
  1403. LiveInterval &LI = LIS->getInterval(RegA);
  1404. VNInfo *VNI = LI.getNextValue(LastCopyIdx, LIS->getVNInfoAllocator());
  1405. SlotIndex endIdx =
  1406. LIS->getInstructionIndex(*MI).getRegSlot(IsEarlyClobber);
  1407. LI.addSegment(LiveInterval::Segment(LastCopyIdx, endIdx, VNI));
  1408. }
  1409. }
  1410. LLVM_DEBUG(dbgs() << "\t\tprepend:\t" << *MIB);
  1411. MachineOperand &MO = MI->getOperand(SrcIdx);
  1412. assert(MO.isReg() && MO.getReg() == RegB && MO.isUse() &&
  1413. "inconsistent operand info for 2-reg pass");
  1414. if (MO.isKill()) {
  1415. MO.setIsKill(false);
  1416. RemovedKillFlag = true;
  1417. }
  1418. // Make sure regA is a legal regclass for the SrcIdx operand.
  1419. if (Register::isVirtualRegister(RegA) && Register::isVirtualRegister(RegB))
  1420. MRI->constrainRegClass(RegA, RC);
  1421. MO.setReg(RegA);
  1422. // The getMatchingSuper asserts guarantee that the register class projected
  1423. // by SubRegB is compatible with RegA with no subregister. So regardless of
  1424. // whether the dest oper writes a subreg, the source oper should not.
  1425. MO.setSubReg(0);
  1426. // Propagate SrcRegMap.
  1427. SrcRegMap[RegA] = RegB;
  1428. }
  1429. if (AllUsesCopied) {
  1430. bool ReplacedAllUntiedUses = true;
  1431. if (!IsEarlyClobber) {
  1432. // Replace other (un-tied) uses of regB with LastCopiedReg.
  1433. for (MachineOperand &MO : MI->operands()) {
  1434. if (MO.isReg() && MO.getReg() == RegB && MO.isUse()) {
  1435. if (MO.getSubReg() == SubRegB) {
  1436. if (MO.isKill()) {
  1437. MO.setIsKill(false);
  1438. RemovedKillFlag = true;
  1439. }
  1440. MO.setReg(LastCopiedReg);
  1441. MO.setSubReg(0);
  1442. } else {
  1443. ReplacedAllUntiedUses = false;
  1444. }
  1445. }
  1446. }
  1447. }
  1448. // Update live variables for regB.
  1449. if (RemovedKillFlag && ReplacedAllUntiedUses &&
  1450. LV && LV->getVarInfo(RegB).removeKill(*MI)) {
  1451. MachineBasicBlock::iterator PrevMI = MI;
  1452. --PrevMI;
  1453. LV->addVirtualRegisterKilled(RegB, *PrevMI);
  1454. }
  1455. // Update LiveIntervals.
  1456. if (LIS) {
  1457. LiveInterval &LI = LIS->getInterval(RegB);
  1458. SlotIndex MIIdx = LIS->getInstructionIndex(*MI);
  1459. LiveInterval::const_iterator I = LI.find(MIIdx);
  1460. assert(I != LI.end() && "RegB must be live-in to use.");
  1461. SlotIndex UseIdx = MIIdx.getRegSlot(IsEarlyClobber);
  1462. if (I->end == UseIdx)
  1463. LI.removeSegment(LastCopyIdx, UseIdx);
  1464. }
  1465. } else if (RemovedKillFlag) {
  1466. // Some tied uses of regB matched their destination registers, so
  1467. // regB is still used in this instruction, but a kill flag was
  1468. // removed from a different tied use of regB, so now we need to add
  1469. // a kill flag to one of the remaining uses of regB.
  1470. for (MachineOperand &MO : MI->operands()) {
  1471. if (MO.isReg() && MO.getReg() == RegB && MO.isUse()) {
  1472. MO.setIsKill(true);
  1473. break;
  1474. }
  1475. }
  1476. }
  1477. }
  1478. /// Reduce two-address instructions to two operands.
  1479. bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &Func) {
  1480. MF = &Func;
  1481. const TargetMachine &TM = MF->getTarget();
  1482. MRI = &MF->getRegInfo();
  1483. TII = MF->getSubtarget().getInstrInfo();
  1484. TRI = MF->getSubtarget().getRegisterInfo();
  1485. InstrItins = MF->getSubtarget().getInstrItineraryData();
  1486. LV = getAnalysisIfAvailable<LiveVariables>();
  1487. LIS = getAnalysisIfAvailable<LiveIntervals>();
  1488. if (auto *AAPass = getAnalysisIfAvailable<AAResultsWrapperPass>())
  1489. AA = &AAPass->getAAResults();
  1490. else
  1491. AA = nullptr;
  1492. OptLevel = TM.getOptLevel();
  1493. // Disable optimizations if requested. We cannot skip the whole pass as some
  1494. // fixups are necessary for correctness.
  1495. if (skipFunction(Func.getFunction()))
  1496. OptLevel = CodeGenOpt::None;
  1497. bool MadeChange = false;
  1498. LLVM_DEBUG(dbgs() << "********** REWRITING TWO-ADDR INSTRS **********\n");
  1499. LLVM_DEBUG(dbgs() << "********** Function: " << MF->getName() << '\n');
  1500. // This pass takes the function out of SSA form.
  1501. MRI->leaveSSA();
  1502. TiedOperandMap TiedOperands;
  1503. for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
  1504. MBBI != MBBE; ++MBBI) {
  1505. MBB = &*MBBI;
  1506. unsigned Dist = 0;
  1507. DistanceMap.clear();
  1508. SrcRegMap.clear();
  1509. DstRegMap.clear();
  1510. Processed.clear();
  1511. SunkInstrs.clear();
  1512. for (MachineBasicBlock::iterator mi = MBB->begin(), me = MBB->end();
  1513. mi != me; ) {
  1514. MachineBasicBlock::iterator nmi = std::next(mi);
  1515. // Don't revisit an instruction previously converted by target. It may
  1516. // contain undef register operands (%noreg), which are not handled.
  1517. if (mi->isDebugInstr() || SunkInstrs.count(&*mi)) {
  1518. mi = nmi;
  1519. continue;
  1520. }
  1521. // Expand REG_SEQUENCE instructions. This will position mi at the first
  1522. // expanded instruction.
  1523. if (mi->isRegSequence())
  1524. eliminateRegSequence(mi);
  1525. DistanceMap.insert(std::make_pair(&*mi, ++Dist));
  1526. processCopy(&*mi);
  1527. // First scan through all the tied register uses in this instruction
  1528. // and record a list of pairs of tied operands for each register.
  1529. if (!collectTiedOperands(&*mi, TiedOperands)) {
  1530. mi = nmi;
  1531. continue;
  1532. }
  1533. ++NumTwoAddressInstrs;
  1534. MadeChange = true;
  1535. LLVM_DEBUG(dbgs() << '\t' << *mi);
  1536. // If the instruction has a single pair of tied operands, try some
  1537. // transformations that may either eliminate the tied operands or
  1538. // improve the opportunities for coalescing away the register copy.
  1539. if (TiedOperands.size() == 1) {
  1540. SmallVectorImpl<std::pair<unsigned, unsigned>> &TiedPairs
  1541. = TiedOperands.begin()->second;
  1542. if (TiedPairs.size() == 1) {
  1543. unsigned SrcIdx = TiedPairs[0].first;
  1544. unsigned DstIdx = TiedPairs[0].second;
  1545. Register SrcReg = mi->getOperand(SrcIdx).getReg();
  1546. Register DstReg = mi->getOperand(DstIdx).getReg();
  1547. if (SrcReg != DstReg &&
  1548. tryInstructionTransform(mi, nmi, SrcIdx, DstIdx, Dist, false)) {
  1549. // The tied operands have been eliminated or shifted further down
  1550. // the block to ease elimination. Continue processing with 'nmi'.
  1551. TiedOperands.clear();
  1552. mi = nmi;
  1553. continue;
  1554. }
  1555. }
  1556. }
  1557. // Now iterate over the information collected above.
  1558. for (auto &TO : TiedOperands) {
  1559. processTiedPairs(&*mi, TO.second, Dist);
  1560. LLVM_DEBUG(dbgs() << "\t\trewrite to:\t" << *mi);
  1561. }
  1562. // Rewrite INSERT_SUBREG as COPY now that we no longer need SSA form.
  1563. if (mi->isInsertSubreg()) {
  1564. // From %reg = INSERT_SUBREG %reg, %subreg, subidx
  1565. // To %reg:subidx = COPY %subreg
  1566. unsigned SubIdx = mi->getOperand(3).getImm();
  1567. mi->RemoveOperand(3);
  1568. assert(mi->getOperand(0).getSubReg() == 0 && "Unexpected subreg idx");
  1569. mi->getOperand(0).setSubReg(SubIdx);
  1570. mi->getOperand(0).setIsUndef(mi->getOperand(1).isUndef());
  1571. mi->RemoveOperand(1);
  1572. mi->setDesc(TII->get(TargetOpcode::COPY));
  1573. LLVM_DEBUG(dbgs() << "\t\tconvert to:\t" << *mi);
  1574. }
  1575. // Clear TiedOperands here instead of at the top of the loop
  1576. // since most instructions do not have tied operands.
  1577. TiedOperands.clear();
  1578. mi = nmi;
  1579. }
  1580. }
  1581. if (LIS)
  1582. MF->verify(this, "After two-address instruction pass");
  1583. return MadeChange;
  1584. }
  1585. /// Eliminate a REG_SEQUENCE instruction as part of the de-ssa process.
  1586. ///
  1587. /// The instruction is turned into a sequence of sub-register copies:
  1588. ///
  1589. /// %dst = REG_SEQUENCE %v1, ssub0, %v2, ssub1
  1590. ///
  1591. /// Becomes:
  1592. ///
  1593. /// undef %dst:ssub0 = COPY %v1
  1594. /// %dst:ssub1 = COPY %v2
  1595. void TwoAddressInstructionPass::
  1596. eliminateRegSequence(MachineBasicBlock::iterator &MBBI) {
  1597. MachineInstr &MI = *MBBI;
  1598. Register DstReg = MI.getOperand(0).getReg();
  1599. if (MI.getOperand(0).getSubReg() || Register::isPhysicalRegister(DstReg) ||
  1600. !(MI.getNumOperands() & 1)) {
  1601. LLVM_DEBUG(dbgs() << "Illegal REG_SEQUENCE instruction:" << MI);
  1602. llvm_unreachable(nullptr);
  1603. }
  1604. SmallVector<unsigned, 4> OrigRegs;
  1605. if (LIS) {
  1606. OrigRegs.push_back(MI.getOperand(0).getReg());
  1607. for (unsigned i = 1, e = MI.getNumOperands(); i < e; i += 2)
  1608. OrigRegs.push_back(MI.getOperand(i).getReg());
  1609. }
  1610. bool DefEmitted = false;
  1611. for (unsigned i = 1, e = MI.getNumOperands(); i < e; i += 2) {
  1612. MachineOperand &UseMO = MI.getOperand(i);
  1613. Register SrcReg = UseMO.getReg();
  1614. unsigned SubIdx = MI.getOperand(i+1).getImm();
  1615. // Nothing needs to be inserted for undef operands.
  1616. if (UseMO.isUndef())
  1617. continue;
  1618. // Defer any kill flag to the last operand using SrcReg. Otherwise, we
  1619. // might insert a COPY that uses SrcReg after is was killed.
  1620. bool isKill = UseMO.isKill();
  1621. if (isKill)
  1622. for (unsigned j = i + 2; j < e; j += 2)
  1623. if (MI.getOperand(j).getReg() == SrcReg) {
  1624. MI.getOperand(j).setIsKill();
  1625. UseMO.setIsKill(false);
  1626. isKill = false;
  1627. break;
  1628. }
  1629. // Insert the sub-register copy.
  1630. MachineInstr *CopyMI = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(),
  1631. TII->get(TargetOpcode::COPY))
  1632. .addReg(DstReg, RegState::Define, SubIdx)
  1633. .add(UseMO);
  1634. // The first def needs an undef flag because there is no live register
  1635. // before it.
  1636. if (!DefEmitted) {
  1637. CopyMI->getOperand(0).setIsUndef(true);
  1638. // Return an iterator pointing to the first inserted instr.
  1639. MBBI = CopyMI;
  1640. }
  1641. DefEmitted = true;
  1642. // Update LiveVariables' kill info.
  1643. if (LV && isKill && !Register::isPhysicalRegister(SrcReg))
  1644. LV->replaceKillInstruction(SrcReg, MI, *CopyMI);
  1645. LLVM_DEBUG(dbgs() << "Inserted: " << *CopyMI);
  1646. }
  1647. MachineBasicBlock::iterator EndMBBI =
  1648. std::next(MachineBasicBlock::iterator(MI));
  1649. if (!DefEmitted) {
  1650. LLVM_DEBUG(dbgs() << "Turned: " << MI << " into an IMPLICIT_DEF");
  1651. MI.setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
  1652. for (int j = MI.getNumOperands() - 1, ee = 0; j > ee; --j)
  1653. MI.RemoveOperand(j);
  1654. } else {
  1655. LLVM_DEBUG(dbgs() << "Eliminated: " << MI);
  1656. MI.eraseFromParent();
  1657. }
  1658. // Udpate LiveIntervals.
  1659. if (LIS)
  1660. LIS->repairIntervalsInRange(MBB, MBBI, EndMBBI, OrigRegs);
  1661. }