TwoAddressInstructionPass.cpp 70 KB

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