TwoAddressInstructionPass.cpp 67 KB

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