TwoAddressInstructionPass.cpp 60 KB

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