TwoAddressInstructionPass.cpp 67 KB

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