TwoAddressInstructionPass.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  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. #define DEBUG_TYPE "twoaddrinstr"
  30. #include "llvm/CodeGen/Passes.h"
  31. #include "llvm/Function.h"
  32. #include "llvm/CodeGen/LiveVariables.h"
  33. #include "llvm/CodeGen/MachineFunctionPass.h"
  34. #include "llvm/CodeGen/MachineInstr.h"
  35. #include "llvm/CodeGen/MachineInstrBuilder.h"
  36. #include "llvm/CodeGen/MachineRegisterInfo.h"
  37. #include "llvm/Analysis/AliasAnalysis.h"
  38. #include "llvm/Target/TargetRegisterInfo.h"
  39. #include "llvm/Target/TargetInstrInfo.h"
  40. #include "llvm/Target/TargetMachine.h"
  41. #include "llvm/Target/TargetOptions.h"
  42. #include "llvm/Support/Debug.h"
  43. #include "llvm/Support/ErrorHandling.h"
  44. #include "llvm/ADT/BitVector.h"
  45. #include "llvm/ADT/DenseMap.h"
  46. #include "llvm/ADT/SmallSet.h"
  47. #include "llvm/ADT/Statistic.h"
  48. #include "llvm/ADT/STLExtras.h"
  49. using namespace llvm;
  50. STATISTIC(NumTwoAddressInstrs, "Number of two-address instructions");
  51. STATISTIC(NumCommuted , "Number of instructions commuted to coalesce");
  52. STATISTIC(NumAggrCommuted , "Number of instructions aggressively commuted");
  53. STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address");
  54. STATISTIC(Num3AddrSunk, "Number of 3-address instructions sunk");
  55. STATISTIC(NumReMats, "Number of instructions re-materialized");
  56. STATISTIC(NumDeletes, "Number of dead instructions deleted");
  57. namespace {
  58. class TwoAddressInstructionPass : public MachineFunctionPass {
  59. const TargetInstrInfo *TII;
  60. const TargetRegisterInfo *TRI;
  61. MachineRegisterInfo *MRI;
  62. LiveVariables *LV;
  63. AliasAnalysis *AA;
  64. // DistanceMap - Keep track the distance of a MI from the start of the
  65. // current basic block.
  66. DenseMap<MachineInstr*, unsigned> DistanceMap;
  67. // SrcRegMap - A map from virtual registers to physical registers which
  68. // are likely targets to be coalesced to due to copies from physical
  69. // registers to virtual registers. e.g. v1024 = move r0.
  70. DenseMap<unsigned, unsigned> SrcRegMap;
  71. // DstRegMap - A map from virtual registers to physical registers which
  72. // are likely targets to be coalesced to due to copies to physical
  73. // registers from virtual registers. e.g. r1 = move v1024.
  74. DenseMap<unsigned, unsigned> DstRegMap;
  75. /// RegSequences - Keep track the list of REG_SEQUENCE instructions seen
  76. /// during the initial walk of the machine function.
  77. SmallVector<MachineInstr*, 16> RegSequences;
  78. bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI,
  79. unsigned Reg,
  80. MachineBasicBlock::iterator OldPos);
  81. bool isProfitableToReMat(unsigned Reg, const TargetRegisterClass *RC,
  82. MachineInstr *MI, MachineInstr *DefMI,
  83. MachineBasicBlock *MBB, unsigned Loc);
  84. bool NoUseAfterLastDef(unsigned Reg, MachineBasicBlock *MBB, unsigned Dist,
  85. unsigned &LastDef);
  86. MachineInstr *FindLastUseInMBB(unsigned Reg, MachineBasicBlock *MBB,
  87. unsigned Dist);
  88. bool isProfitableToCommute(unsigned regB, unsigned regC,
  89. MachineInstr *MI, MachineBasicBlock *MBB,
  90. unsigned Dist);
  91. bool CommuteInstruction(MachineBasicBlock::iterator &mi,
  92. MachineFunction::iterator &mbbi,
  93. unsigned RegB, unsigned RegC, unsigned Dist);
  94. bool isProfitableToConv3Addr(unsigned RegA);
  95. bool ConvertInstTo3Addr(MachineBasicBlock::iterator &mi,
  96. MachineBasicBlock::iterator &nmi,
  97. MachineFunction::iterator &mbbi,
  98. unsigned RegB, unsigned Dist);
  99. typedef std::pair<std::pair<unsigned, bool>, MachineInstr*> NewKill;
  100. bool canUpdateDeletedKills(SmallVector<unsigned, 4> &Kills,
  101. SmallVector<NewKill, 4> &NewKills,
  102. MachineBasicBlock *MBB, unsigned Dist);
  103. bool DeleteUnusedInstr(MachineBasicBlock::iterator &mi,
  104. MachineBasicBlock::iterator &nmi,
  105. MachineFunction::iterator &mbbi, unsigned Dist);
  106. bool TryInstructionTransform(MachineBasicBlock::iterator &mi,
  107. MachineBasicBlock::iterator &nmi,
  108. MachineFunction::iterator &mbbi,
  109. unsigned SrcIdx, unsigned DstIdx,
  110. unsigned Dist);
  111. void ProcessCopy(MachineInstr *MI, MachineBasicBlock *MBB,
  112. SmallPtrSet<MachineInstr*, 8> &Processed);
  113. void CoalesceExtSubRegs(SmallVector<unsigned,4> &Srcs, unsigned DstReg);
  114. /// EliminateRegSequences - Eliminate REG_SEQUENCE instructions as part
  115. /// of the de-ssa process. This replaces sources of REG_SEQUENCE as
  116. /// sub-register references of the register defined by REG_SEQUENCE.
  117. bool EliminateRegSequences();
  118. public:
  119. static char ID; // Pass identification, replacement for typeid
  120. TwoAddressInstructionPass() : MachineFunctionPass(&ID) {}
  121. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  122. AU.setPreservesCFG();
  123. AU.addRequired<AliasAnalysis>();
  124. AU.addPreserved<LiveVariables>();
  125. AU.addPreservedID(MachineLoopInfoID);
  126. AU.addPreservedID(MachineDominatorsID);
  127. if (StrongPHIElim)
  128. AU.addPreservedID(StrongPHIEliminationID);
  129. else
  130. AU.addPreservedID(PHIEliminationID);
  131. MachineFunctionPass::getAnalysisUsage(AU);
  132. }
  133. /// runOnMachineFunction - Pass entry point.
  134. bool runOnMachineFunction(MachineFunction&);
  135. };
  136. }
  137. char TwoAddressInstructionPass::ID = 0;
  138. static RegisterPass<TwoAddressInstructionPass>
  139. X("twoaddressinstruction", "Two-Address instruction pass");
  140. const PassInfo *const llvm::TwoAddressInstructionPassID = &X;
  141. /// Sink3AddrInstruction - A two-address instruction has been converted to a
  142. /// three-address instruction to avoid clobbering a register. Try to sink it
  143. /// past the instruction that would kill the above mentioned register to reduce
  144. /// register pressure.
  145. bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
  146. MachineInstr *MI, unsigned SavedReg,
  147. MachineBasicBlock::iterator OldPos) {
  148. // Check if it's safe to move this instruction.
  149. bool SeenStore = true; // Be conservative.
  150. if (!MI->isSafeToMove(TII, AA, SeenStore))
  151. return false;
  152. unsigned DefReg = 0;
  153. SmallSet<unsigned, 4> UseRegs;
  154. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  155. const MachineOperand &MO = MI->getOperand(i);
  156. if (!MO.isReg())
  157. continue;
  158. unsigned MOReg = MO.getReg();
  159. if (!MOReg)
  160. continue;
  161. if (MO.isUse() && MOReg != SavedReg)
  162. UseRegs.insert(MO.getReg());
  163. if (!MO.isDef())
  164. continue;
  165. if (MO.isImplicit())
  166. // Don't try to move it if it implicitly defines a register.
  167. return false;
  168. if (DefReg)
  169. // For now, don't move any instructions that define multiple registers.
  170. return false;
  171. DefReg = MO.getReg();
  172. }
  173. // Find the instruction that kills SavedReg.
  174. MachineInstr *KillMI = NULL;
  175. for (MachineRegisterInfo::use_nodbg_iterator
  176. UI = MRI->use_nodbg_begin(SavedReg),
  177. UE = MRI->use_nodbg_end(); UI != UE; ++UI) {
  178. MachineOperand &UseMO = UI.getOperand();
  179. if (!UseMO.isKill())
  180. continue;
  181. KillMI = UseMO.getParent();
  182. break;
  183. }
  184. if (!KillMI || KillMI->getParent() != MBB || KillMI == MI)
  185. return false;
  186. // If any of the definitions are used by another instruction between the
  187. // position and the kill use, then it's not safe to sink it.
  188. //
  189. // FIXME: This can be sped up if there is an easy way to query whether an
  190. // instruction is before or after another instruction. Then we can use
  191. // MachineRegisterInfo def / use instead.
  192. MachineOperand *KillMO = NULL;
  193. MachineBasicBlock::iterator KillPos = KillMI;
  194. ++KillPos;
  195. unsigned NumVisited = 0;
  196. for (MachineBasicBlock::iterator I = llvm::next(OldPos); I != KillPos; ++I) {
  197. MachineInstr *OtherMI = I;
  198. // DBG_VALUE cannot be counted against the limit.
  199. if (OtherMI->isDebugValue())
  200. continue;
  201. if (NumVisited > 30) // FIXME: Arbitrary limit to reduce compile time cost.
  202. return false;
  203. ++NumVisited;
  204. for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
  205. MachineOperand &MO = OtherMI->getOperand(i);
  206. if (!MO.isReg())
  207. continue;
  208. unsigned MOReg = MO.getReg();
  209. if (!MOReg)
  210. continue;
  211. if (DefReg == MOReg)
  212. return false;
  213. if (MO.isKill()) {
  214. if (OtherMI == KillMI && MOReg == SavedReg)
  215. // Save the operand that kills the register. We want to unset the kill
  216. // marker if we can sink MI past it.
  217. KillMO = &MO;
  218. else if (UseRegs.count(MOReg))
  219. // One of the uses is killed before the destination.
  220. return false;
  221. }
  222. }
  223. }
  224. // Update kill and LV information.
  225. KillMO->setIsKill(false);
  226. KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI);
  227. KillMO->setIsKill(true);
  228. if (LV)
  229. LV->replaceKillInstruction(SavedReg, KillMI, MI);
  230. // Move instruction to its destination.
  231. MBB->remove(MI);
  232. MBB->insert(KillPos, MI);
  233. ++Num3AddrSunk;
  234. return true;
  235. }
  236. /// isTwoAddrUse - Return true if the specified MI is using the specified
  237. /// register as a two-address operand.
  238. static bool isTwoAddrUse(MachineInstr *UseMI, unsigned Reg) {
  239. const TargetInstrDesc &TID = UseMI->getDesc();
  240. for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
  241. MachineOperand &MO = UseMI->getOperand(i);
  242. if (MO.isReg() && MO.getReg() == Reg &&
  243. (MO.isDef() || UseMI->isRegTiedToDefOperand(i)))
  244. // Earlier use is a two-address one.
  245. return true;
  246. }
  247. return false;
  248. }
  249. /// isProfitableToReMat - Return true if the heuristics determines it is likely
  250. /// to be profitable to re-materialize the definition of Reg rather than copy
  251. /// the register.
  252. bool
  253. TwoAddressInstructionPass::isProfitableToReMat(unsigned Reg,
  254. const TargetRegisterClass *RC,
  255. MachineInstr *MI, MachineInstr *DefMI,
  256. MachineBasicBlock *MBB, unsigned Loc) {
  257. bool OtherUse = false;
  258. for (MachineRegisterInfo::use_nodbg_iterator UI = MRI->use_nodbg_begin(Reg),
  259. UE = MRI->use_nodbg_end(); UI != UE; ++UI) {
  260. MachineOperand &UseMO = UI.getOperand();
  261. MachineInstr *UseMI = UseMO.getParent();
  262. MachineBasicBlock *UseMBB = UseMI->getParent();
  263. if (UseMBB == MBB) {
  264. DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI);
  265. if (DI != DistanceMap.end() && DI->second == Loc)
  266. continue; // Current use.
  267. OtherUse = true;
  268. // There is at least one other use in the MBB that will clobber the
  269. // register.
  270. if (isTwoAddrUse(UseMI, Reg))
  271. return true;
  272. }
  273. }
  274. // If other uses in MBB are not two-address uses, then don't remat.
  275. if (OtherUse)
  276. return false;
  277. // No other uses in the same block, remat if it's defined in the same
  278. // block so it does not unnecessarily extend the live range.
  279. return MBB == DefMI->getParent();
  280. }
  281. /// NoUseAfterLastDef - Return true if there are no intervening uses between the
  282. /// last instruction in the MBB that defines the specified register and the
  283. /// two-address instruction which is being processed. It also returns the last
  284. /// def location by reference
  285. bool TwoAddressInstructionPass::NoUseAfterLastDef(unsigned Reg,
  286. MachineBasicBlock *MBB, unsigned Dist,
  287. unsigned &LastDef) {
  288. LastDef = 0;
  289. unsigned LastUse = Dist;
  290. for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(Reg),
  291. E = MRI->reg_end(); I != E; ++I) {
  292. MachineOperand &MO = I.getOperand();
  293. MachineInstr *MI = MO.getParent();
  294. if (MI->getParent() != MBB || MI->isDebugValue())
  295. continue;
  296. DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI);
  297. if (DI == DistanceMap.end())
  298. continue;
  299. if (MO.isUse() && DI->second < LastUse)
  300. LastUse = DI->second;
  301. if (MO.isDef() && DI->second > LastDef)
  302. LastDef = DI->second;
  303. }
  304. return !(LastUse > LastDef && LastUse < Dist);
  305. }
  306. MachineInstr *TwoAddressInstructionPass::FindLastUseInMBB(unsigned Reg,
  307. MachineBasicBlock *MBB,
  308. unsigned Dist) {
  309. unsigned LastUseDist = 0;
  310. MachineInstr *LastUse = 0;
  311. for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(Reg),
  312. E = MRI->reg_end(); I != E; ++I) {
  313. MachineOperand &MO = I.getOperand();
  314. MachineInstr *MI = MO.getParent();
  315. if (MI->getParent() != MBB || MI->isDebugValue())
  316. continue;
  317. DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI);
  318. if (DI == DistanceMap.end())
  319. continue;
  320. if (DI->second >= Dist)
  321. continue;
  322. if (MO.isUse() && DI->second > LastUseDist) {
  323. LastUse = DI->first;
  324. LastUseDist = DI->second;
  325. }
  326. }
  327. return LastUse;
  328. }
  329. /// isCopyToReg - Return true if the specified MI is a copy instruction or
  330. /// a extract_subreg instruction. It also returns the source and destination
  331. /// registers and whether they are physical registers by reference.
  332. static bool isCopyToReg(MachineInstr &MI, const TargetInstrInfo *TII,
  333. unsigned &SrcReg, unsigned &DstReg,
  334. bool &IsSrcPhys, bool &IsDstPhys) {
  335. SrcReg = 0;
  336. DstReg = 0;
  337. unsigned SrcSubIdx, DstSubIdx;
  338. if (!TII->isMoveInstr(MI, SrcReg, DstReg, SrcSubIdx, DstSubIdx)) {
  339. if (MI.isCopy() || MI.isExtractSubreg()) {
  340. DstReg = MI.getOperand(0).getReg();
  341. SrcReg = MI.getOperand(1).getReg();
  342. } else if (MI.isInsertSubreg()) {
  343. DstReg = MI.getOperand(0).getReg();
  344. SrcReg = MI.getOperand(2).getReg();
  345. } else if (MI.isSubregToReg()) {
  346. DstReg = MI.getOperand(0).getReg();
  347. SrcReg = MI.getOperand(2).getReg();
  348. }
  349. }
  350. if (DstReg) {
  351. IsSrcPhys = TargetRegisterInfo::isPhysicalRegister(SrcReg);
  352. IsDstPhys = TargetRegisterInfo::isPhysicalRegister(DstReg);
  353. return true;
  354. }
  355. return false;
  356. }
  357. /// isKilled - Test if the given register value, which is used by the given
  358. /// instruction, is killed by the given instruction. This looks through
  359. /// coalescable copies to see if the original value is potentially not killed.
  360. ///
  361. /// For example, in this code:
  362. ///
  363. /// %reg1034 = copy %reg1024
  364. /// %reg1035 = copy %reg1025<kill>
  365. /// %reg1036 = add %reg1034<kill>, %reg1035<kill>
  366. ///
  367. /// %reg1034 is not considered to be killed, since it is copied from a
  368. /// register which is not killed. Treating it as not killed lets the
  369. /// normal heuristics commute the (two-address) add, which lets
  370. /// coalescing eliminate the extra copy.
  371. ///
  372. static bool isKilled(MachineInstr &MI, unsigned Reg,
  373. const MachineRegisterInfo *MRI,
  374. const TargetInstrInfo *TII) {
  375. MachineInstr *DefMI = &MI;
  376. for (;;) {
  377. if (!DefMI->killsRegister(Reg))
  378. return false;
  379. if (TargetRegisterInfo::isPhysicalRegister(Reg))
  380. return true;
  381. MachineRegisterInfo::def_iterator Begin = MRI->def_begin(Reg);
  382. // If there are multiple defs, we can't do a simple analysis, so just
  383. // go with what the kill flag says.
  384. if (llvm::next(Begin) != MRI->def_end())
  385. return true;
  386. DefMI = &*Begin;
  387. bool IsSrcPhys, IsDstPhys;
  388. unsigned SrcReg, DstReg;
  389. // If the def is something other than a copy, then it isn't going to
  390. // be coalesced, so follow the kill flag.
  391. if (!isCopyToReg(*DefMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
  392. return true;
  393. Reg = SrcReg;
  394. }
  395. }
  396. /// isTwoAddrUse - Return true if the specified MI uses the specified register
  397. /// as a two-address use. If so, return the destination register by reference.
  398. static bool isTwoAddrUse(MachineInstr &MI, unsigned Reg, unsigned &DstReg) {
  399. const TargetInstrDesc &TID = MI.getDesc();
  400. unsigned NumOps = MI.isInlineAsm() ? MI.getNumOperands():TID.getNumOperands();
  401. for (unsigned i = 0; i != NumOps; ++i) {
  402. const MachineOperand &MO = MI.getOperand(i);
  403. if (!MO.isReg() || !MO.isUse() || MO.getReg() != Reg)
  404. continue;
  405. unsigned ti;
  406. if (MI.isRegTiedToDefOperand(i, &ti)) {
  407. DstReg = MI.getOperand(ti).getReg();
  408. return true;
  409. }
  410. }
  411. return false;
  412. }
  413. /// findOnlyInterestingUse - Given a register, if has a single in-basic block
  414. /// use, return the use instruction if it's a copy or a two-address use.
  415. static
  416. MachineInstr *findOnlyInterestingUse(unsigned Reg, MachineBasicBlock *MBB,
  417. MachineRegisterInfo *MRI,
  418. const TargetInstrInfo *TII,
  419. bool &IsCopy,
  420. unsigned &DstReg, bool &IsDstPhys) {
  421. if (!MRI->hasOneNonDBGUse(Reg))
  422. // None or more than one use.
  423. return 0;
  424. MachineInstr &UseMI = *MRI->use_nodbg_begin(Reg);
  425. if (UseMI.getParent() != MBB)
  426. return 0;
  427. unsigned SrcReg;
  428. bool IsSrcPhys;
  429. if (isCopyToReg(UseMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys)) {
  430. IsCopy = true;
  431. return &UseMI;
  432. }
  433. IsDstPhys = false;
  434. if (isTwoAddrUse(UseMI, Reg, DstReg)) {
  435. IsDstPhys = TargetRegisterInfo::isPhysicalRegister(DstReg);
  436. return &UseMI;
  437. }
  438. return 0;
  439. }
  440. /// getMappedReg - Return the physical register the specified virtual register
  441. /// might be mapped to.
  442. static unsigned
  443. getMappedReg(unsigned Reg, DenseMap<unsigned, unsigned> &RegMap) {
  444. while (TargetRegisterInfo::isVirtualRegister(Reg)) {
  445. DenseMap<unsigned, unsigned>::iterator SI = RegMap.find(Reg);
  446. if (SI == RegMap.end())
  447. return 0;
  448. Reg = SI->second;
  449. }
  450. if (TargetRegisterInfo::isPhysicalRegister(Reg))
  451. return Reg;
  452. return 0;
  453. }
  454. /// regsAreCompatible - Return true if the two registers are equal or aliased.
  455. ///
  456. static bool
  457. regsAreCompatible(unsigned RegA, unsigned RegB, const TargetRegisterInfo *TRI) {
  458. if (RegA == RegB)
  459. return true;
  460. if (!RegA || !RegB)
  461. return false;
  462. return TRI->regsOverlap(RegA, RegB);
  463. }
  464. /// isProfitableToReMat - Return true if it's potentially profitable to commute
  465. /// the two-address instruction that's being processed.
  466. bool
  467. TwoAddressInstructionPass::isProfitableToCommute(unsigned regB, unsigned regC,
  468. MachineInstr *MI, MachineBasicBlock *MBB,
  469. unsigned Dist) {
  470. // Determine if it's profitable to commute this two address instruction. In
  471. // general, we want no uses between this instruction and the definition of
  472. // the two-address register.
  473. // e.g.
  474. // %reg1028<def> = EXTRACT_SUBREG %reg1027<kill>, 1
  475. // %reg1029<def> = MOV8rr %reg1028
  476. // %reg1029<def> = SHR8ri %reg1029, 7, %EFLAGS<imp-def,dead>
  477. // insert => %reg1030<def> = MOV8rr %reg1028
  478. // %reg1030<def> = ADD8rr %reg1028<kill>, %reg1029<kill>, %EFLAGS<imp-def,dead>
  479. // In this case, it might not be possible to coalesce the second MOV8rr
  480. // instruction if the first one is coalesced. So it would be profitable to
  481. // commute it:
  482. // %reg1028<def> = EXTRACT_SUBREG %reg1027<kill>, 1
  483. // %reg1029<def> = MOV8rr %reg1028
  484. // %reg1029<def> = SHR8ri %reg1029, 7, %EFLAGS<imp-def,dead>
  485. // insert => %reg1030<def> = MOV8rr %reg1029
  486. // %reg1030<def> = ADD8rr %reg1029<kill>, %reg1028<kill>, %EFLAGS<imp-def,dead>
  487. if (!MI->killsRegister(regC))
  488. return false;
  489. // Ok, we have something like:
  490. // %reg1030<def> = ADD8rr %reg1028<kill>, %reg1029<kill>, %EFLAGS<imp-def,dead>
  491. // let's see if it's worth commuting it.
  492. // Look for situations like this:
  493. // %reg1024<def> = MOV r1
  494. // %reg1025<def> = MOV r0
  495. // %reg1026<def> = ADD %reg1024, %reg1025
  496. // r0 = MOV %reg1026
  497. // Commute the ADD to hopefully eliminate an otherwise unavoidable copy.
  498. unsigned FromRegB = getMappedReg(regB, SrcRegMap);
  499. unsigned FromRegC = getMappedReg(regC, SrcRegMap);
  500. unsigned ToRegB = getMappedReg(regB, DstRegMap);
  501. unsigned ToRegC = getMappedReg(regC, DstRegMap);
  502. if (!regsAreCompatible(FromRegB, ToRegB, TRI) &&
  503. (regsAreCompatible(FromRegB, ToRegC, TRI) ||
  504. regsAreCompatible(FromRegC, ToRegB, TRI)))
  505. return true;
  506. // If there is a use of regC between its last def (could be livein) and this
  507. // instruction, then bail.
  508. unsigned LastDefC = 0;
  509. if (!NoUseAfterLastDef(regC, MBB, Dist, LastDefC))
  510. return false;
  511. // If there is a use of regB between its last def (could be livein) and this
  512. // instruction, then go ahead and make this transformation.
  513. unsigned LastDefB = 0;
  514. if (!NoUseAfterLastDef(regB, MBB, Dist, LastDefB))
  515. return true;
  516. // Since there are no intervening uses for both registers, then commute
  517. // if the def of regC is closer. Its live interval is shorter.
  518. return LastDefB && LastDefC && LastDefC > LastDefB;
  519. }
  520. /// CommuteInstruction - Commute a two-address instruction and update the basic
  521. /// block, distance map, and live variables if needed. Return true if it is
  522. /// successful.
  523. bool
  524. TwoAddressInstructionPass::CommuteInstruction(MachineBasicBlock::iterator &mi,
  525. MachineFunction::iterator &mbbi,
  526. unsigned RegB, unsigned RegC, unsigned Dist) {
  527. MachineInstr *MI = mi;
  528. DEBUG(dbgs() << "2addr: COMMUTING : " << *MI);
  529. MachineInstr *NewMI = TII->commuteInstruction(MI);
  530. if (NewMI == 0) {
  531. DEBUG(dbgs() << "2addr: COMMUTING FAILED!\n");
  532. return false;
  533. }
  534. DEBUG(dbgs() << "2addr: COMMUTED TO: " << *NewMI);
  535. // If the instruction changed to commute it, update livevar.
  536. if (NewMI != MI) {
  537. if (LV)
  538. // Update live variables
  539. LV->replaceKillInstruction(RegC, MI, NewMI);
  540. mbbi->insert(mi, NewMI); // Insert the new inst
  541. mbbi->erase(mi); // Nuke the old inst.
  542. mi = NewMI;
  543. DistanceMap.insert(std::make_pair(NewMI, Dist));
  544. }
  545. // Update source register map.
  546. unsigned FromRegC = getMappedReg(RegC, SrcRegMap);
  547. if (FromRegC) {
  548. unsigned RegA = MI->getOperand(0).getReg();
  549. SrcRegMap[RegA] = FromRegC;
  550. }
  551. return true;
  552. }
  553. /// isProfitableToConv3Addr - Return true if it is profitable to convert the
  554. /// given 2-address instruction to a 3-address one.
  555. bool
  556. TwoAddressInstructionPass::isProfitableToConv3Addr(unsigned RegA) {
  557. // Look for situations like this:
  558. // %reg1024<def> = MOV r1
  559. // %reg1025<def> = MOV r0
  560. // %reg1026<def> = ADD %reg1024, %reg1025
  561. // r2 = MOV %reg1026
  562. // Turn ADD into a 3-address instruction to avoid a copy.
  563. unsigned FromRegA = getMappedReg(RegA, SrcRegMap);
  564. unsigned ToRegA = getMappedReg(RegA, DstRegMap);
  565. return (FromRegA && ToRegA && !regsAreCompatible(FromRegA, ToRegA, TRI));
  566. }
  567. /// ConvertInstTo3Addr - Convert the specified two-address instruction into a
  568. /// three address one. Return true if this transformation was successful.
  569. bool
  570. TwoAddressInstructionPass::ConvertInstTo3Addr(MachineBasicBlock::iterator &mi,
  571. MachineBasicBlock::iterator &nmi,
  572. MachineFunction::iterator &mbbi,
  573. unsigned RegB, unsigned Dist) {
  574. MachineInstr *NewMI = TII->convertToThreeAddress(mbbi, mi, LV);
  575. if (NewMI) {
  576. DEBUG(dbgs() << "2addr: CONVERTING 2-ADDR: " << *mi);
  577. DEBUG(dbgs() << "2addr: TO 3-ADDR: " << *NewMI);
  578. bool Sunk = false;
  579. if (NewMI->findRegisterUseOperand(RegB, false, TRI))
  580. // FIXME: Temporary workaround. If the new instruction doesn't
  581. // uses RegB, convertToThreeAddress must have created more
  582. // then one instruction.
  583. Sunk = Sink3AddrInstruction(mbbi, NewMI, RegB, mi);
  584. mbbi->erase(mi); // Nuke the old inst.
  585. if (!Sunk) {
  586. DistanceMap.insert(std::make_pair(NewMI, Dist));
  587. mi = NewMI;
  588. nmi = llvm::next(mi);
  589. }
  590. return true;
  591. }
  592. return false;
  593. }
  594. /// ProcessCopy - If the specified instruction is not yet processed, process it
  595. /// if it's a copy. For a copy instruction, we find the physical registers the
  596. /// source and destination registers might be mapped to. These are kept in
  597. /// point-to maps used to determine future optimizations. e.g.
  598. /// v1024 = mov r0
  599. /// v1025 = mov r1
  600. /// v1026 = add v1024, v1025
  601. /// r1 = mov r1026
  602. /// If 'add' is a two-address instruction, v1024, v1026 are both potentially
  603. /// coalesced to r0 (from the input side). v1025 is mapped to r1. v1026 is
  604. /// potentially joined with r1 on the output side. It's worthwhile to commute
  605. /// 'add' to eliminate a copy.
  606. void TwoAddressInstructionPass::ProcessCopy(MachineInstr *MI,
  607. MachineBasicBlock *MBB,
  608. SmallPtrSet<MachineInstr*, 8> &Processed) {
  609. if (Processed.count(MI))
  610. return;
  611. bool IsSrcPhys, IsDstPhys;
  612. unsigned SrcReg, DstReg;
  613. if (!isCopyToReg(*MI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
  614. return;
  615. if (IsDstPhys && !IsSrcPhys)
  616. DstRegMap.insert(std::make_pair(SrcReg, DstReg));
  617. else if (!IsDstPhys && IsSrcPhys) {
  618. bool isNew = SrcRegMap.insert(std::make_pair(DstReg, SrcReg)).second;
  619. if (!isNew)
  620. assert(SrcRegMap[DstReg] == SrcReg &&
  621. "Can't map to two src physical registers!");
  622. SmallVector<unsigned, 4> VirtRegPairs;
  623. bool IsCopy = false;
  624. unsigned NewReg = 0;
  625. while (MachineInstr *UseMI = findOnlyInterestingUse(DstReg, MBB, MRI,TII,
  626. IsCopy, NewReg, IsDstPhys)) {
  627. if (IsCopy) {
  628. if (!Processed.insert(UseMI))
  629. break;
  630. }
  631. DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI);
  632. if (DI != DistanceMap.end())
  633. // Earlier in the same MBB.Reached via a back edge.
  634. break;
  635. if (IsDstPhys) {
  636. VirtRegPairs.push_back(NewReg);
  637. break;
  638. }
  639. bool isNew = SrcRegMap.insert(std::make_pair(NewReg, DstReg)).second;
  640. if (!isNew)
  641. assert(SrcRegMap[NewReg] == DstReg &&
  642. "Can't map to two src physical registers!");
  643. VirtRegPairs.push_back(NewReg);
  644. DstReg = NewReg;
  645. }
  646. if (!VirtRegPairs.empty()) {
  647. unsigned ToReg = VirtRegPairs.back();
  648. VirtRegPairs.pop_back();
  649. while (!VirtRegPairs.empty()) {
  650. unsigned FromReg = VirtRegPairs.back();
  651. VirtRegPairs.pop_back();
  652. bool isNew = DstRegMap.insert(std::make_pair(FromReg, ToReg)).second;
  653. if (!isNew)
  654. assert(DstRegMap[FromReg] == ToReg &&
  655. "Can't map to two dst physical registers!");
  656. ToReg = FromReg;
  657. }
  658. }
  659. }
  660. Processed.insert(MI);
  661. }
  662. /// isSafeToDelete - If the specified instruction does not produce any side
  663. /// effects and all of its defs are dead, then it's safe to delete.
  664. static bool isSafeToDelete(MachineInstr *MI,
  665. const TargetInstrInfo *TII,
  666. SmallVector<unsigned, 4> &Kills) {
  667. const TargetInstrDesc &TID = MI->getDesc();
  668. if (TID.mayStore() || TID.isCall())
  669. return false;
  670. if (TID.isTerminator() || TID.hasUnmodeledSideEffects())
  671. return false;
  672. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  673. MachineOperand &MO = MI->getOperand(i);
  674. if (!MO.isReg())
  675. continue;
  676. if (MO.isDef() && !MO.isDead())
  677. return false;
  678. if (MO.isUse() && MO.isKill())
  679. Kills.push_back(MO.getReg());
  680. }
  681. return true;
  682. }
  683. /// canUpdateDeletedKills - Check if all the registers listed in Kills are
  684. /// killed by instructions in MBB preceding the current instruction at
  685. /// position Dist. If so, return true and record information about the
  686. /// preceding kills in NewKills.
  687. bool TwoAddressInstructionPass::
  688. canUpdateDeletedKills(SmallVector<unsigned, 4> &Kills,
  689. SmallVector<NewKill, 4> &NewKills,
  690. MachineBasicBlock *MBB, unsigned Dist) {
  691. while (!Kills.empty()) {
  692. unsigned Kill = Kills.back();
  693. Kills.pop_back();
  694. if (TargetRegisterInfo::isPhysicalRegister(Kill))
  695. return false;
  696. MachineInstr *LastKill = FindLastUseInMBB(Kill, MBB, Dist);
  697. if (!LastKill)
  698. return false;
  699. bool isModRef = LastKill->definesRegister(Kill);
  700. NewKills.push_back(std::make_pair(std::make_pair(Kill, isModRef),
  701. LastKill));
  702. }
  703. return true;
  704. }
  705. /// DeleteUnusedInstr - If an instruction with a tied register operand can
  706. /// be safely deleted, just delete it.
  707. bool
  708. TwoAddressInstructionPass::DeleteUnusedInstr(MachineBasicBlock::iterator &mi,
  709. MachineBasicBlock::iterator &nmi,
  710. MachineFunction::iterator &mbbi,
  711. unsigned Dist) {
  712. // Check if the instruction has no side effects and if all its defs are dead.
  713. SmallVector<unsigned, 4> Kills;
  714. if (!isSafeToDelete(mi, TII, Kills))
  715. return false;
  716. // If this instruction kills some virtual registers, we need to
  717. // update the kill information. If it's not possible to do so,
  718. // then bail out.
  719. SmallVector<NewKill, 4> NewKills;
  720. if (!canUpdateDeletedKills(Kills, NewKills, &*mbbi, Dist))
  721. return false;
  722. if (LV) {
  723. while (!NewKills.empty()) {
  724. MachineInstr *NewKill = NewKills.back().second;
  725. unsigned Kill = NewKills.back().first.first;
  726. bool isDead = NewKills.back().first.second;
  727. NewKills.pop_back();
  728. if (LV->removeVirtualRegisterKilled(Kill, mi)) {
  729. if (isDead)
  730. LV->addVirtualRegisterDead(Kill, NewKill);
  731. else
  732. LV->addVirtualRegisterKilled(Kill, NewKill);
  733. }
  734. }
  735. }
  736. mbbi->erase(mi); // Nuke the old inst.
  737. mi = nmi;
  738. return true;
  739. }
  740. /// TryInstructionTransform - For the case where an instruction has a single
  741. /// pair of tied register operands, attempt some transformations that may
  742. /// either eliminate the tied operands or improve the opportunities for
  743. /// coalescing away the register copy. Returns true if the tied operands
  744. /// are eliminated altogether.
  745. bool TwoAddressInstructionPass::
  746. TryInstructionTransform(MachineBasicBlock::iterator &mi,
  747. MachineBasicBlock::iterator &nmi,
  748. MachineFunction::iterator &mbbi,
  749. unsigned SrcIdx, unsigned DstIdx, unsigned Dist) {
  750. const TargetInstrDesc &TID = mi->getDesc();
  751. unsigned regA = mi->getOperand(DstIdx).getReg();
  752. unsigned regB = mi->getOperand(SrcIdx).getReg();
  753. assert(TargetRegisterInfo::isVirtualRegister(regB) &&
  754. "cannot make instruction into two-address form");
  755. // If regA is dead and the instruction can be deleted, just delete
  756. // it so it doesn't clobber regB.
  757. bool regBKilled = isKilled(*mi, regB, MRI, TII);
  758. if (!regBKilled && mi->getOperand(DstIdx).isDead() &&
  759. DeleteUnusedInstr(mi, nmi, mbbi, Dist)) {
  760. ++NumDeletes;
  761. return true; // Done with this instruction.
  762. }
  763. // Check if it is profitable to commute the operands.
  764. unsigned SrcOp1, SrcOp2;
  765. unsigned regC = 0;
  766. unsigned regCIdx = ~0U;
  767. bool TryCommute = false;
  768. bool AggressiveCommute = false;
  769. if (TID.isCommutable() && mi->getNumOperands() >= 3 &&
  770. TII->findCommutedOpIndices(mi, SrcOp1, SrcOp2)) {
  771. if (SrcIdx == SrcOp1)
  772. regCIdx = SrcOp2;
  773. else if (SrcIdx == SrcOp2)
  774. regCIdx = SrcOp1;
  775. if (regCIdx != ~0U) {
  776. regC = mi->getOperand(regCIdx).getReg();
  777. if (!regBKilled && isKilled(*mi, regC, MRI, TII))
  778. // If C dies but B does not, swap the B and C operands.
  779. // This makes the live ranges of A and C joinable.
  780. TryCommute = true;
  781. else if (isProfitableToCommute(regB, regC, mi, mbbi, Dist)) {
  782. TryCommute = true;
  783. AggressiveCommute = true;
  784. }
  785. }
  786. }
  787. // If it's profitable to commute, try to do so.
  788. if (TryCommute && CommuteInstruction(mi, mbbi, regB, regC, Dist)) {
  789. ++NumCommuted;
  790. if (AggressiveCommute)
  791. ++NumAggrCommuted;
  792. return false;
  793. }
  794. if (TID.isConvertibleTo3Addr()) {
  795. // This instruction is potentially convertible to a true
  796. // three-address instruction. Check if it is profitable.
  797. if (!regBKilled || isProfitableToConv3Addr(regA)) {
  798. // Try to convert it.
  799. if (ConvertInstTo3Addr(mi, nmi, mbbi, regB, Dist)) {
  800. ++NumConvertedTo3Addr;
  801. return true; // Done with this instruction.
  802. }
  803. }
  804. }
  805. // If this is an instruction with a load folded into it, try unfolding
  806. // the load, e.g. avoid this:
  807. // movq %rdx, %rcx
  808. // addq (%rax), %rcx
  809. // in favor of this:
  810. // movq (%rax), %rcx
  811. // addq %rdx, %rcx
  812. // because it's preferable to schedule a load than a register copy.
  813. if (TID.mayLoad() && !regBKilled) {
  814. // Determine if a load can be unfolded.
  815. unsigned LoadRegIndex;
  816. unsigned NewOpc =
  817. TII->getOpcodeAfterMemoryUnfold(mi->getOpcode(),
  818. /*UnfoldLoad=*/true,
  819. /*UnfoldStore=*/false,
  820. &LoadRegIndex);
  821. if (NewOpc != 0) {
  822. const TargetInstrDesc &UnfoldTID = TII->get(NewOpc);
  823. if (UnfoldTID.getNumDefs() == 1) {
  824. MachineFunction &MF = *mbbi->getParent();
  825. // Unfold the load.
  826. DEBUG(dbgs() << "2addr: UNFOLDING: " << *mi);
  827. const TargetRegisterClass *RC =
  828. UnfoldTID.OpInfo[LoadRegIndex].getRegClass(TRI);
  829. unsigned Reg = MRI->createVirtualRegister(RC);
  830. SmallVector<MachineInstr *, 2> NewMIs;
  831. if (!TII->unfoldMemoryOperand(MF, mi, Reg,
  832. /*UnfoldLoad=*/true,/*UnfoldStore=*/false,
  833. NewMIs)) {
  834. DEBUG(dbgs() << "2addr: ABANDONING UNFOLD\n");
  835. return false;
  836. }
  837. assert(NewMIs.size() == 2 &&
  838. "Unfolded a load into multiple instructions!");
  839. // The load was previously folded, so this is the only use.
  840. NewMIs[1]->addRegisterKilled(Reg, TRI);
  841. // Tentatively insert the instructions into the block so that they
  842. // look "normal" to the transformation logic.
  843. mbbi->insert(mi, NewMIs[0]);
  844. mbbi->insert(mi, NewMIs[1]);
  845. DEBUG(dbgs() << "2addr: NEW LOAD: " << *NewMIs[0]
  846. << "2addr: NEW INST: " << *NewMIs[1]);
  847. // Transform the instruction, now that it no longer has a load.
  848. unsigned NewDstIdx = NewMIs[1]->findRegisterDefOperandIdx(regA);
  849. unsigned NewSrcIdx = NewMIs[1]->findRegisterUseOperandIdx(regB);
  850. MachineBasicBlock::iterator NewMI = NewMIs[1];
  851. bool TransformSuccess =
  852. TryInstructionTransform(NewMI, mi, mbbi,
  853. NewSrcIdx, NewDstIdx, Dist);
  854. if (TransformSuccess ||
  855. NewMIs[1]->getOperand(NewSrcIdx).isKill()) {
  856. // Success, or at least we made an improvement. Keep the unfolded
  857. // instructions and discard the original.
  858. if (LV) {
  859. for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
  860. MachineOperand &MO = mi->getOperand(i);
  861. if (MO.isReg() && MO.getReg() != 0 &&
  862. TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
  863. if (MO.isUse()) {
  864. if (MO.isKill()) {
  865. if (NewMIs[0]->killsRegister(MO.getReg()))
  866. LV->replaceKillInstruction(MO.getReg(), mi, NewMIs[0]);
  867. else {
  868. assert(NewMIs[1]->killsRegister(MO.getReg()) &&
  869. "Kill missing after load unfold!");
  870. LV->replaceKillInstruction(MO.getReg(), mi, NewMIs[1]);
  871. }
  872. }
  873. } else if (LV->removeVirtualRegisterDead(MO.getReg(), mi)) {
  874. if (NewMIs[1]->registerDefIsDead(MO.getReg()))
  875. LV->addVirtualRegisterDead(MO.getReg(), NewMIs[1]);
  876. else {
  877. assert(NewMIs[0]->registerDefIsDead(MO.getReg()) &&
  878. "Dead flag missing after load unfold!");
  879. LV->addVirtualRegisterDead(MO.getReg(), NewMIs[0]);
  880. }
  881. }
  882. }
  883. }
  884. LV->addVirtualRegisterKilled(Reg, NewMIs[1]);
  885. }
  886. mi->eraseFromParent();
  887. mi = NewMIs[1];
  888. if (TransformSuccess)
  889. return true;
  890. } else {
  891. // Transforming didn't eliminate the tie and didn't lead to an
  892. // improvement. Clean up the unfolded instructions and keep the
  893. // original.
  894. DEBUG(dbgs() << "2addr: ABANDONING UNFOLD\n");
  895. NewMIs[0]->eraseFromParent();
  896. NewMIs[1]->eraseFromParent();
  897. }
  898. }
  899. }
  900. }
  901. return false;
  902. }
  903. /// runOnMachineFunction - Reduce two-address instructions to two operands.
  904. ///
  905. bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
  906. DEBUG(dbgs() << "Machine Function\n");
  907. const TargetMachine &TM = MF.getTarget();
  908. MRI = &MF.getRegInfo();
  909. TII = TM.getInstrInfo();
  910. TRI = TM.getRegisterInfo();
  911. LV = getAnalysisIfAvailable<LiveVariables>();
  912. AA = &getAnalysis<AliasAnalysis>();
  913. bool MadeChange = false;
  914. DEBUG(dbgs() << "********** REWRITING TWO-ADDR INSTRS **********\n");
  915. DEBUG(dbgs() << "********** Function: "
  916. << MF.getFunction()->getName() << '\n');
  917. // ReMatRegs - Keep track of the registers whose def's are remat'ed.
  918. BitVector ReMatRegs;
  919. ReMatRegs.resize(MRI->getLastVirtReg()+1);
  920. typedef DenseMap<unsigned, SmallVector<std::pair<unsigned, unsigned>, 4> >
  921. TiedOperandMap;
  922. TiedOperandMap TiedOperands(4);
  923. SmallPtrSet<MachineInstr*, 8> Processed;
  924. for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
  925. mbbi != mbbe; ++mbbi) {
  926. unsigned Dist = 0;
  927. DistanceMap.clear();
  928. SrcRegMap.clear();
  929. DstRegMap.clear();
  930. Processed.clear();
  931. for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
  932. mi != me; ) {
  933. MachineBasicBlock::iterator nmi = llvm::next(mi);
  934. if (mi->isDebugValue()) {
  935. mi = nmi;
  936. continue;
  937. }
  938. // Remember REG_SEQUENCE instructions, we'll deal with them later.
  939. if (mi->isRegSequence())
  940. RegSequences.push_back(&*mi);
  941. const TargetInstrDesc &TID = mi->getDesc();
  942. bool FirstTied = true;
  943. DistanceMap.insert(std::make_pair(mi, ++Dist));
  944. ProcessCopy(&*mi, &*mbbi, Processed);
  945. // First scan through all the tied register uses in this instruction
  946. // and record a list of pairs of tied operands for each register.
  947. unsigned NumOps = mi->isInlineAsm()
  948. ? mi->getNumOperands() : TID.getNumOperands();
  949. for (unsigned SrcIdx = 0; SrcIdx < NumOps; ++SrcIdx) {
  950. unsigned DstIdx = 0;
  951. if (!mi->isRegTiedToDefOperand(SrcIdx, &DstIdx))
  952. continue;
  953. if (FirstTied) {
  954. FirstTied = false;
  955. ++NumTwoAddressInstrs;
  956. DEBUG(dbgs() << '\t' << *mi);
  957. }
  958. assert(mi->getOperand(SrcIdx).isReg() &&
  959. mi->getOperand(SrcIdx).getReg() &&
  960. mi->getOperand(SrcIdx).isUse() &&
  961. "two address instruction invalid");
  962. unsigned regB = mi->getOperand(SrcIdx).getReg();
  963. TiedOperandMap::iterator OI = TiedOperands.find(regB);
  964. if (OI == TiedOperands.end()) {
  965. SmallVector<std::pair<unsigned, unsigned>, 4> TiedPair;
  966. OI = TiedOperands.insert(std::make_pair(regB, TiedPair)).first;
  967. }
  968. OI->second.push_back(std::make_pair(SrcIdx, DstIdx));
  969. }
  970. // Now iterate over the information collected above.
  971. for (TiedOperandMap::iterator OI = TiedOperands.begin(),
  972. OE = TiedOperands.end(); OI != OE; ++OI) {
  973. SmallVector<std::pair<unsigned, unsigned>, 4> &TiedPairs = OI->second;
  974. // If the instruction has a single pair of tied operands, try some
  975. // transformations that may either eliminate the tied operands or
  976. // improve the opportunities for coalescing away the register copy.
  977. if (TiedOperands.size() == 1 && TiedPairs.size() == 1) {
  978. unsigned SrcIdx = TiedPairs[0].first;
  979. unsigned DstIdx = TiedPairs[0].second;
  980. // If the registers are already equal, nothing needs to be done.
  981. if (mi->getOperand(SrcIdx).getReg() ==
  982. mi->getOperand(DstIdx).getReg())
  983. break; // Done with this instruction.
  984. if (TryInstructionTransform(mi, nmi, mbbi, SrcIdx, DstIdx, Dist))
  985. break; // The tied operands have been eliminated.
  986. }
  987. bool RemovedKillFlag = false;
  988. bool AllUsesCopied = true;
  989. unsigned LastCopiedReg = 0;
  990. unsigned regB = OI->first;
  991. for (unsigned tpi = 0, tpe = TiedPairs.size(); tpi != tpe; ++tpi) {
  992. unsigned SrcIdx = TiedPairs[tpi].first;
  993. unsigned DstIdx = TiedPairs[tpi].second;
  994. unsigned regA = mi->getOperand(DstIdx).getReg();
  995. // Grab regB from the instruction because it may have changed if the
  996. // instruction was commuted.
  997. regB = mi->getOperand(SrcIdx).getReg();
  998. if (regA == regB) {
  999. // The register is tied to multiple destinations (or else we would
  1000. // not have continued this far), but this use of the register
  1001. // already matches the tied destination. Leave it.
  1002. AllUsesCopied = false;
  1003. continue;
  1004. }
  1005. LastCopiedReg = regA;
  1006. assert(TargetRegisterInfo::isVirtualRegister(regB) &&
  1007. "cannot make instruction into two-address form");
  1008. #ifndef NDEBUG
  1009. // First, verify that we don't have a use of "a" in the instruction
  1010. // (a = b + a for example) because our transformation will not
  1011. // work. This should never occur because we are in SSA form.
  1012. for (unsigned i = 0; i != mi->getNumOperands(); ++i)
  1013. assert(i == DstIdx ||
  1014. !mi->getOperand(i).isReg() ||
  1015. mi->getOperand(i).getReg() != regA);
  1016. #endif
  1017. // Emit a copy or rematerialize the definition.
  1018. const TargetRegisterClass *rc = MRI->getRegClass(regB);
  1019. MachineInstr *DefMI = MRI->getVRegDef(regB);
  1020. // If it's safe and profitable, remat the definition instead of
  1021. // copying it.
  1022. if (DefMI &&
  1023. DefMI->getDesc().isAsCheapAsAMove() &&
  1024. DefMI->isSafeToReMat(TII, AA, regB) &&
  1025. isProfitableToReMat(regB, rc, mi, DefMI, mbbi, Dist)){
  1026. DEBUG(dbgs() << "2addr: REMATTING : " << *DefMI << "\n");
  1027. unsigned regASubIdx = mi->getOperand(DstIdx).getSubReg();
  1028. TII->reMaterialize(*mbbi, mi, regA, regASubIdx, DefMI, *TRI);
  1029. ReMatRegs.set(regB);
  1030. ++NumReMats;
  1031. } else {
  1032. bool Emitted = TII->copyRegToReg(*mbbi, mi, regA, regB, rc, rc,
  1033. mi->getDebugLoc());
  1034. (void)Emitted;
  1035. assert(Emitted && "Unable to issue a copy instruction!\n");
  1036. }
  1037. MachineBasicBlock::iterator prevMI = prior(mi);
  1038. // Update DistanceMap.
  1039. DistanceMap.insert(std::make_pair(prevMI, Dist));
  1040. DistanceMap[mi] = ++Dist;
  1041. DEBUG(dbgs() << "\t\tprepend:\t" << *prevMI);
  1042. MachineOperand &MO = mi->getOperand(SrcIdx);
  1043. assert(MO.isReg() && MO.getReg() == regB && MO.isUse() &&
  1044. "inconsistent operand info for 2-reg pass");
  1045. if (MO.isKill()) {
  1046. MO.setIsKill(false);
  1047. RemovedKillFlag = true;
  1048. }
  1049. MO.setReg(regA);
  1050. }
  1051. if (AllUsesCopied) {
  1052. // Replace other (un-tied) uses of regB with LastCopiedReg.
  1053. for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
  1054. MachineOperand &MO = mi->getOperand(i);
  1055. if (MO.isReg() && MO.getReg() == regB && MO.isUse()) {
  1056. if (MO.isKill()) {
  1057. MO.setIsKill(false);
  1058. RemovedKillFlag = true;
  1059. }
  1060. MO.setReg(LastCopiedReg);
  1061. }
  1062. }
  1063. // Update live variables for regB.
  1064. if (RemovedKillFlag && LV && LV->getVarInfo(regB).removeKill(mi))
  1065. LV->addVirtualRegisterKilled(regB, prior(mi));
  1066. } else if (RemovedKillFlag) {
  1067. // Some tied uses of regB matched their destination registers, so
  1068. // regB is still used in this instruction, but a kill flag was
  1069. // removed from a different tied use of regB, so now we need to add
  1070. // a kill flag to one of the remaining uses of regB.
  1071. for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
  1072. MachineOperand &MO = mi->getOperand(i);
  1073. if (MO.isReg() && MO.getReg() == regB && MO.isUse()) {
  1074. MO.setIsKill(true);
  1075. break;
  1076. }
  1077. }
  1078. }
  1079. // Schedule the source copy / remat inserted to form two-address
  1080. // instruction. FIXME: Does it matter the distance map may not be
  1081. // accurate after it's scheduled?
  1082. TII->scheduleTwoAddrSource(prior(mi), mi, *TRI);
  1083. MadeChange = true;
  1084. DEBUG(dbgs() << "\t\trewrite to:\t" << *mi);
  1085. }
  1086. // Rewrite INSERT_SUBREG as COPY now that we no longer need SSA form.
  1087. if (mi->isInsertSubreg()) {
  1088. // From %reg = INSERT_SUBREG %reg, %subreg, subidx
  1089. // To %reg:subidx = COPY %subreg
  1090. unsigned SubIdx = mi->getOperand(3).getImm();
  1091. mi->RemoveOperand(3);
  1092. assert(mi->getOperand(0).getSubReg() == 0 && "Unexpected subreg idx");
  1093. mi->getOperand(0).setSubReg(SubIdx);
  1094. mi->RemoveOperand(1);
  1095. mi->setDesc(TII->get(TargetOpcode::COPY));
  1096. DEBUG(dbgs() << "\t\tconvert to:\t" << *mi);
  1097. }
  1098. // Clear TiedOperands here instead of at the top of the loop
  1099. // since most instructions do not have tied operands.
  1100. TiedOperands.clear();
  1101. mi = nmi;
  1102. }
  1103. }
  1104. // Some remat'ed instructions are dead.
  1105. int VReg = ReMatRegs.find_first();
  1106. while (VReg != -1) {
  1107. if (MRI->use_nodbg_empty(VReg)) {
  1108. MachineInstr *DefMI = MRI->getVRegDef(VReg);
  1109. DefMI->eraseFromParent();
  1110. }
  1111. VReg = ReMatRegs.find_next(VReg);
  1112. }
  1113. // Eliminate REG_SEQUENCE instructions. Their whole purpose was to preseve
  1114. // SSA form. It's now safe to de-SSA.
  1115. MadeChange |= EliminateRegSequences();
  1116. return MadeChange;
  1117. }
  1118. static void UpdateRegSequenceSrcs(unsigned SrcReg,
  1119. unsigned DstReg, unsigned SubIdx,
  1120. MachineRegisterInfo *MRI,
  1121. const TargetRegisterInfo &TRI) {
  1122. for (MachineRegisterInfo::reg_iterator RI = MRI->reg_begin(SrcReg),
  1123. RE = MRI->reg_end(); RI != RE; ) {
  1124. MachineOperand &MO = RI.getOperand();
  1125. ++RI;
  1126. MO.substVirtReg(DstReg, SubIdx, TRI);
  1127. }
  1128. }
  1129. /// CoalesceExtSubRegs - If a number of sources of the REG_SEQUENCE are
  1130. /// EXTRACT_SUBREG from the same register and to the same virtual register
  1131. /// with different sub-register indices, attempt to combine the
  1132. /// EXTRACT_SUBREGs and pre-coalesce them. e.g.
  1133. /// %reg1026<def> = VLDMQ %reg1025<kill>, 260, pred:14, pred:%reg0
  1134. /// %reg1029:6<def> = EXTRACT_SUBREG %reg1026, 6
  1135. /// %reg1029:5<def> = EXTRACT_SUBREG %reg1026<kill>, 5
  1136. /// Since D subregs 5, 6 can combine to a Q register, we can coalesce
  1137. /// reg1026 to reg1029.
  1138. void
  1139. TwoAddressInstructionPass::CoalesceExtSubRegs(SmallVector<unsigned,4> &Srcs,
  1140. unsigned DstReg) {
  1141. SmallSet<unsigned, 4> Seen;
  1142. for (unsigned i = 0, e = Srcs.size(); i != e; ++i) {
  1143. unsigned SrcReg = Srcs[i];
  1144. if (!Seen.insert(SrcReg))
  1145. continue;
  1146. // Check that the instructions are all in the same basic block.
  1147. MachineInstr *SrcDefMI = MRI->getVRegDef(SrcReg);
  1148. MachineInstr *DstDefMI = MRI->getVRegDef(DstReg);
  1149. if (SrcDefMI->getParent() != DstDefMI->getParent())
  1150. continue;
  1151. // If there are no other uses than extract_subreg which feed into
  1152. // the reg_sequence, then we might be able to coalesce them.
  1153. bool CanCoalesce = true;
  1154. SmallVector<unsigned, 4> SrcSubIndices, DstSubIndices;
  1155. for (MachineRegisterInfo::use_nodbg_iterator
  1156. UI = MRI->use_nodbg_begin(SrcReg),
  1157. UE = MRI->use_nodbg_end(); UI != UE; ++UI) {
  1158. MachineInstr *UseMI = &*UI;
  1159. if (!UseMI->isExtractSubreg() ||
  1160. UseMI->getOperand(0).getReg() != DstReg ||
  1161. UseMI->getOperand(1).getSubReg() != 0) {
  1162. CanCoalesce = false;
  1163. break;
  1164. }
  1165. SrcSubIndices.push_back(UseMI->getOperand(2).getImm());
  1166. DstSubIndices.push_back(UseMI->getOperand(0).getSubReg());
  1167. }
  1168. if (!CanCoalesce || SrcSubIndices.size() < 2)
  1169. continue;
  1170. // Check that the source subregisters can be combined.
  1171. std::sort(SrcSubIndices.begin(), SrcSubIndices.end());
  1172. unsigned NewSrcSubIdx = 0;
  1173. if (!TRI->canCombineSubRegIndices(MRI->getRegClass(SrcReg), SrcSubIndices,
  1174. NewSrcSubIdx))
  1175. continue;
  1176. // Check that the destination subregisters can also be combined.
  1177. std::sort(DstSubIndices.begin(), DstSubIndices.end());
  1178. unsigned NewDstSubIdx = 0;
  1179. if (!TRI->canCombineSubRegIndices(MRI->getRegClass(DstReg), DstSubIndices,
  1180. NewDstSubIdx))
  1181. continue;
  1182. // If neither source nor destination can be combined to the full register,
  1183. // just give up. This could be improved if it ever matters.
  1184. if (NewSrcSubIdx != 0 && NewDstSubIdx != 0)
  1185. continue;
  1186. // Now that we know that all the uses are extract_subregs and that those
  1187. // subregs can somehow be combined, scan all the extract_subregs again to
  1188. // make sure the subregs are in the right order and can be composed.
  1189. MachineInstr *SomeMI = 0;
  1190. CanCoalesce = true;
  1191. for (MachineRegisterInfo::use_nodbg_iterator
  1192. UI = MRI->use_nodbg_begin(SrcReg),
  1193. UE = MRI->use_nodbg_end(); UI != UE; ++UI) {
  1194. MachineInstr *UseMI = &*UI;
  1195. assert(UseMI->isExtractSubreg());
  1196. unsigned DstSubIdx = UseMI->getOperand(0).getSubReg();
  1197. unsigned SrcSubIdx = UseMI->getOperand(2).getImm();
  1198. assert(DstSubIdx != 0 && "missing subreg from RegSequence elimination");
  1199. if ((NewDstSubIdx == 0 &&
  1200. TRI->composeSubRegIndices(NewSrcSubIdx, DstSubIdx) != SrcSubIdx) ||
  1201. (NewSrcSubIdx == 0 &&
  1202. TRI->composeSubRegIndices(NewDstSubIdx, SrcSubIdx) != DstSubIdx)) {
  1203. CanCoalesce = false;
  1204. break;
  1205. }
  1206. // Keep track of one of the uses.
  1207. SomeMI = UseMI;
  1208. }
  1209. if (!CanCoalesce)
  1210. continue;
  1211. // Insert a copy or an extract to replace the original extracts.
  1212. MachineBasicBlock::iterator InsertLoc = SomeMI;
  1213. MachineInstr *CopyMI = BuildMI(*SomeMI->getParent(), SomeMI,
  1214. SomeMI->getDebugLoc(),
  1215. TII->get(TargetOpcode::COPY))
  1216. .addReg(DstReg, RegState::Define, NewDstSubIdx)
  1217. .addReg(SrcReg, 0, NewSrcSubIdx);
  1218. // Remove all the old extract instructions.
  1219. for (MachineRegisterInfo::use_nodbg_iterator
  1220. UI = MRI->use_nodbg_begin(SrcReg),
  1221. UE = MRI->use_nodbg_end(); UI != UE; ) {
  1222. MachineInstr *UseMI = &*UI;
  1223. ++UI;
  1224. if (UseMI == CopyMI)
  1225. continue;
  1226. assert(UseMI->isExtractSubreg());
  1227. // Move any kills to the new copy or extract instruction.
  1228. if (UseMI->getOperand(1).isKill()) {
  1229. MachineOperand *KillMO = CopyMI->findRegisterUseOperand(SrcReg);
  1230. KillMO->setIsKill();
  1231. if (LV)
  1232. // Update live variables
  1233. LV->replaceKillInstruction(SrcReg, UseMI, &*CopyMI);
  1234. }
  1235. UseMI->eraseFromParent();
  1236. }
  1237. }
  1238. }
  1239. static bool HasOtherRegSequenceUses(unsigned Reg, MachineInstr *RegSeq,
  1240. MachineRegisterInfo *MRI) {
  1241. for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg),
  1242. UE = MRI->use_end(); UI != UE; ++UI) {
  1243. MachineInstr *UseMI = &*UI;
  1244. if (UseMI != RegSeq && UseMI->isRegSequence())
  1245. return true;
  1246. }
  1247. return false;
  1248. }
  1249. /// EliminateRegSequences - Eliminate REG_SEQUENCE instructions as part
  1250. /// of the de-ssa process. This replaces sources of REG_SEQUENCE as
  1251. /// sub-register references of the register defined by REG_SEQUENCE. e.g.
  1252. ///
  1253. /// %reg1029<def>, %reg1030<def> = VLD1q16 %reg1024<kill>, ...
  1254. /// %reg1031<def> = REG_SEQUENCE %reg1029<kill>, 5, %reg1030<kill>, 6
  1255. /// =>
  1256. /// %reg1031:5<def>, %reg1031:6<def> = VLD1q16 %reg1024<kill>, ...
  1257. bool TwoAddressInstructionPass::EliminateRegSequences() {
  1258. if (RegSequences.empty())
  1259. return false;
  1260. for (unsigned i = 0, e = RegSequences.size(); i != e; ++i) {
  1261. MachineInstr *MI = RegSequences[i];
  1262. unsigned DstReg = MI->getOperand(0).getReg();
  1263. if (MI->getOperand(0).getSubReg() ||
  1264. TargetRegisterInfo::isPhysicalRegister(DstReg) ||
  1265. !(MI->getNumOperands() & 1)) {
  1266. DEBUG(dbgs() << "Illegal REG_SEQUENCE instruction:" << *MI);
  1267. llvm_unreachable(0);
  1268. }
  1269. bool IsImpDef = true;
  1270. SmallVector<unsigned, 4> RealSrcs;
  1271. SmallSet<unsigned, 4> Seen;
  1272. for (unsigned i = 1, e = MI->getNumOperands(); i < e; i += 2) {
  1273. unsigned SrcReg = MI->getOperand(i).getReg();
  1274. if (MI->getOperand(i).getSubReg() ||
  1275. TargetRegisterInfo::isPhysicalRegister(SrcReg)) {
  1276. DEBUG(dbgs() << "Illegal REG_SEQUENCE instruction:" << *MI);
  1277. llvm_unreachable(0);
  1278. }
  1279. MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
  1280. if (DefMI->isImplicitDef()) {
  1281. DefMI->eraseFromParent();
  1282. continue;
  1283. }
  1284. IsImpDef = false;
  1285. // Remember EXTRACT_SUBREG sources. These might be candidate for
  1286. // coalescing.
  1287. if (DefMI->isExtractSubreg())
  1288. RealSrcs.push_back(DefMI->getOperand(1).getReg());
  1289. if (!Seen.insert(SrcReg) ||
  1290. MI->getParent() != DefMI->getParent() ||
  1291. !MI->getOperand(i).isKill() ||
  1292. HasOtherRegSequenceUses(SrcReg, MI, MRI)) {
  1293. // REG_SEQUENCE cannot have duplicated operands, add a copy.
  1294. // Also add an copy if the source is live-in the block. We don't want
  1295. // to end up with a partial-redef of a livein, e.g.
  1296. // BB0:
  1297. // reg1051:10<def> =
  1298. // ...
  1299. // BB1:
  1300. // ... = reg1051:10
  1301. // BB2:
  1302. // reg1051:9<def> =
  1303. // LiveIntervalAnalysis won't like it.
  1304. //
  1305. // If the REG_SEQUENCE doesn't kill its source, keeping live variables
  1306. // correctly up to date becomes very difficult. Insert a copy.
  1307. //
  1308. const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
  1309. unsigned NewReg = MRI->createVirtualRegister(RC);
  1310. MachineBasicBlock::iterator InsertLoc = MI;
  1311. bool Emitted =
  1312. TII->copyRegToReg(*MI->getParent(), InsertLoc, NewReg, SrcReg, RC, RC,
  1313. MI->getDebugLoc());
  1314. (void)Emitted;
  1315. assert(Emitted && "Unable to issue a copy instruction!\n");
  1316. MI->getOperand(i).setReg(NewReg);
  1317. if (MI->getOperand(i).isKill()) {
  1318. MachineBasicBlock::iterator CopyMI = prior(InsertLoc);
  1319. MachineOperand *KillMO = CopyMI->findRegisterUseOperand(SrcReg);
  1320. KillMO->setIsKill();
  1321. if (LV)
  1322. // Update live variables
  1323. LV->replaceKillInstruction(SrcReg, MI, &*CopyMI);
  1324. }
  1325. }
  1326. }
  1327. for (unsigned i = 1, e = MI->getNumOperands(); i < e; i += 2) {
  1328. unsigned SrcReg = MI->getOperand(i).getReg();
  1329. unsigned SubIdx = MI->getOperand(i+1).getImm();
  1330. UpdateRegSequenceSrcs(SrcReg, DstReg, SubIdx, MRI, *TRI);
  1331. }
  1332. if (IsImpDef) {
  1333. DEBUG(dbgs() << "Turned: " << *MI << " into an IMPLICIT_DEF");
  1334. MI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
  1335. for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j)
  1336. MI->RemoveOperand(j);
  1337. } else {
  1338. DEBUG(dbgs() << "Eliminated: " << *MI);
  1339. MI->eraseFromParent();
  1340. }
  1341. // Try coalescing some EXTRACT_SUBREG instructions. This can create
  1342. // INSERT_SUBREG instructions that must have <undef> flags added by
  1343. // LiveIntervalAnalysis, so only run it when LiveVariables is available.
  1344. if (LV)
  1345. CoalesceExtSubRegs(RealSrcs, DstReg);
  1346. }
  1347. RegSequences.clear();
  1348. return true;
  1349. }