TwoAddressInstructionPass.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  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. INITIALIZE_PASS(TwoAddressInstructionPass, "twoaddressinstruction",
  139. "Two-Address instruction pass", false, false);
  140. char &llvm::TwoAddressInstructionPassID = TwoAddressInstructionPass::ID;
  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. if (MI.isCopy()) {
  338. DstReg = MI.getOperand(0).getReg();
  339. SrcReg = MI.getOperand(1).getReg();
  340. } else if (MI.isInsertSubreg() || MI.isSubregToReg()) {
  341. DstReg = MI.getOperand(0).getReg();
  342. SrcReg = MI.getOperand(2).getReg();
  343. } else
  344. return false;
  345. IsSrcPhys = TargetRegisterInfo::isPhysicalRegister(SrcReg);
  346. IsDstPhys = TargetRegisterInfo::isPhysicalRegister(DstReg);
  347. return true;
  348. }
  349. /// isKilled - Test if the given register value, which is used by the given
  350. /// instruction, is killed by the given instruction. This looks through
  351. /// coalescable copies to see if the original value is potentially not killed.
  352. ///
  353. /// For example, in this code:
  354. ///
  355. /// %reg1034 = copy %reg1024
  356. /// %reg1035 = copy %reg1025<kill>
  357. /// %reg1036 = add %reg1034<kill>, %reg1035<kill>
  358. ///
  359. /// %reg1034 is not considered to be killed, since it is copied from a
  360. /// register which is not killed. Treating it as not killed lets the
  361. /// normal heuristics commute the (two-address) add, which lets
  362. /// coalescing eliminate the extra copy.
  363. ///
  364. static bool isKilled(MachineInstr &MI, unsigned Reg,
  365. const MachineRegisterInfo *MRI,
  366. const TargetInstrInfo *TII) {
  367. MachineInstr *DefMI = &MI;
  368. for (;;) {
  369. if (!DefMI->killsRegister(Reg))
  370. return false;
  371. if (TargetRegisterInfo::isPhysicalRegister(Reg))
  372. return true;
  373. MachineRegisterInfo::def_iterator Begin = MRI->def_begin(Reg);
  374. // If there are multiple defs, we can't do a simple analysis, so just
  375. // go with what the kill flag says.
  376. if (llvm::next(Begin) != MRI->def_end())
  377. return true;
  378. DefMI = &*Begin;
  379. bool IsSrcPhys, IsDstPhys;
  380. unsigned SrcReg, DstReg;
  381. // If the def is something other than a copy, then it isn't going to
  382. // be coalesced, so follow the kill flag.
  383. if (!isCopyToReg(*DefMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
  384. return true;
  385. Reg = SrcReg;
  386. }
  387. }
  388. /// isTwoAddrUse - Return true if the specified MI uses the specified register
  389. /// as a two-address use. If so, return the destination register by reference.
  390. static bool isTwoAddrUse(MachineInstr &MI, unsigned Reg, unsigned &DstReg) {
  391. const TargetInstrDesc &TID = MI.getDesc();
  392. unsigned NumOps = MI.isInlineAsm() ? MI.getNumOperands():TID.getNumOperands();
  393. for (unsigned i = 0; i != NumOps; ++i) {
  394. const MachineOperand &MO = MI.getOperand(i);
  395. if (!MO.isReg() || !MO.isUse() || MO.getReg() != Reg)
  396. continue;
  397. unsigned ti;
  398. if (MI.isRegTiedToDefOperand(i, &ti)) {
  399. DstReg = MI.getOperand(ti).getReg();
  400. return true;
  401. }
  402. }
  403. return false;
  404. }
  405. /// findOnlyInterestingUse - Given a register, if has a single in-basic block
  406. /// use, return the use instruction if it's a copy or a two-address use.
  407. static
  408. MachineInstr *findOnlyInterestingUse(unsigned Reg, MachineBasicBlock *MBB,
  409. MachineRegisterInfo *MRI,
  410. const TargetInstrInfo *TII,
  411. bool &IsCopy,
  412. unsigned &DstReg, bool &IsDstPhys) {
  413. if (!MRI->hasOneNonDBGUse(Reg))
  414. // None or more than one use.
  415. return 0;
  416. MachineInstr &UseMI = *MRI->use_nodbg_begin(Reg);
  417. if (UseMI.getParent() != MBB)
  418. return 0;
  419. unsigned SrcReg;
  420. bool IsSrcPhys;
  421. if (isCopyToReg(UseMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys)) {
  422. IsCopy = true;
  423. return &UseMI;
  424. }
  425. IsDstPhys = false;
  426. if (isTwoAddrUse(UseMI, Reg, DstReg)) {
  427. IsDstPhys = TargetRegisterInfo::isPhysicalRegister(DstReg);
  428. return &UseMI;
  429. }
  430. return 0;
  431. }
  432. /// getMappedReg - Return the physical register the specified virtual register
  433. /// might be mapped to.
  434. static unsigned
  435. getMappedReg(unsigned Reg, DenseMap<unsigned, unsigned> &RegMap) {
  436. while (TargetRegisterInfo::isVirtualRegister(Reg)) {
  437. DenseMap<unsigned, unsigned>::iterator SI = RegMap.find(Reg);
  438. if (SI == RegMap.end())
  439. return 0;
  440. Reg = SI->second;
  441. }
  442. if (TargetRegisterInfo::isPhysicalRegister(Reg))
  443. return Reg;
  444. return 0;
  445. }
  446. /// regsAreCompatible - Return true if the two registers are equal or aliased.
  447. ///
  448. static bool
  449. regsAreCompatible(unsigned RegA, unsigned RegB, const TargetRegisterInfo *TRI) {
  450. if (RegA == RegB)
  451. return true;
  452. if (!RegA || !RegB)
  453. return false;
  454. return TRI->regsOverlap(RegA, RegB);
  455. }
  456. /// isProfitableToReMat - Return true if it's potentially profitable to commute
  457. /// the two-address instruction that's being processed.
  458. bool
  459. TwoAddressInstructionPass::isProfitableToCommute(unsigned regB, unsigned regC,
  460. MachineInstr *MI, MachineBasicBlock *MBB,
  461. unsigned Dist) {
  462. // Determine if it's profitable to commute this two address instruction. In
  463. // general, we want no uses between this instruction and the definition of
  464. // the two-address register.
  465. // e.g.
  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 %reg1028
  470. // %reg1030<def> = ADD8rr %reg1028<kill>, %reg1029<kill>, %EFLAGS<imp-def,dead>
  471. // In this case, it might not be possible to coalesce the second MOV8rr
  472. // instruction if the first one is coalesced. So it would be profitable to
  473. // commute it:
  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 %reg1029
  478. // %reg1030<def> = ADD8rr %reg1029<kill>, %reg1028<kill>, %EFLAGS<imp-def,dead>
  479. if (!MI->killsRegister(regC))
  480. return false;
  481. // Ok, we have something like:
  482. // %reg1030<def> = ADD8rr %reg1028<kill>, %reg1029<kill>, %EFLAGS<imp-def,dead>
  483. // let's see if it's worth commuting it.
  484. // Look for situations like this:
  485. // %reg1024<def> = MOV r1
  486. // %reg1025<def> = MOV r0
  487. // %reg1026<def> = ADD %reg1024, %reg1025
  488. // r0 = MOV %reg1026
  489. // Commute the ADD to hopefully eliminate an otherwise unavoidable copy.
  490. unsigned FromRegB = getMappedReg(regB, SrcRegMap);
  491. unsigned FromRegC = getMappedReg(regC, SrcRegMap);
  492. unsigned ToRegB = getMappedReg(regB, DstRegMap);
  493. unsigned ToRegC = getMappedReg(regC, DstRegMap);
  494. if (!regsAreCompatible(FromRegB, ToRegB, TRI) &&
  495. (regsAreCompatible(FromRegB, ToRegC, TRI) ||
  496. regsAreCompatible(FromRegC, ToRegB, TRI)))
  497. return true;
  498. // If there is a use of regC between its last def (could be livein) and this
  499. // instruction, then bail.
  500. unsigned LastDefC = 0;
  501. if (!NoUseAfterLastDef(regC, MBB, Dist, LastDefC))
  502. return false;
  503. // If there is a use of regB between its last def (could be livein) and this
  504. // instruction, then go ahead and make this transformation.
  505. unsigned LastDefB = 0;
  506. if (!NoUseAfterLastDef(regB, MBB, Dist, LastDefB))
  507. return true;
  508. // Since there are no intervening uses for both registers, then commute
  509. // if the def of regC is closer. Its live interval is shorter.
  510. return LastDefB && LastDefC && LastDefC > LastDefB;
  511. }
  512. /// CommuteInstruction - Commute a two-address instruction and update the basic
  513. /// block, distance map, and live variables if needed. Return true if it is
  514. /// successful.
  515. bool
  516. TwoAddressInstructionPass::CommuteInstruction(MachineBasicBlock::iterator &mi,
  517. MachineFunction::iterator &mbbi,
  518. unsigned RegB, unsigned RegC, unsigned Dist) {
  519. MachineInstr *MI = mi;
  520. DEBUG(dbgs() << "2addr: COMMUTING : " << *MI);
  521. MachineInstr *NewMI = TII->commuteInstruction(MI);
  522. if (NewMI == 0) {
  523. DEBUG(dbgs() << "2addr: COMMUTING FAILED!\n");
  524. return false;
  525. }
  526. DEBUG(dbgs() << "2addr: COMMUTED TO: " << *NewMI);
  527. // If the instruction changed to commute it, update livevar.
  528. if (NewMI != MI) {
  529. if (LV)
  530. // Update live variables
  531. LV->replaceKillInstruction(RegC, MI, NewMI);
  532. mbbi->insert(mi, NewMI); // Insert the new inst
  533. mbbi->erase(mi); // Nuke the old inst.
  534. mi = NewMI;
  535. DistanceMap.insert(std::make_pair(NewMI, Dist));
  536. }
  537. // Update source register map.
  538. unsigned FromRegC = getMappedReg(RegC, SrcRegMap);
  539. if (FromRegC) {
  540. unsigned RegA = MI->getOperand(0).getReg();
  541. SrcRegMap[RegA] = FromRegC;
  542. }
  543. return true;
  544. }
  545. /// isProfitableToConv3Addr - Return true if it is profitable to convert the
  546. /// given 2-address instruction to a 3-address one.
  547. bool
  548. TwoAddressInstructionPass::isProfitableToConv3Addr(unsigned RegA) {
  549. // Look for situations like this:
  550. // %reg1024<def> = MOV r1
  551. // %reg1025<def> = MOV r0
  552. // %reg1026<def> = ADD %reg1024, %reg1025
  553. // r2 = MOV %reg1026
  554. // Turn ADD into a 3-address instruction to avoid a copy.
  555. unsigned FromRegA = getMappedReg(RegA, SrcRegMap);
  556. unsigned ToRegA = getMappedReg(RegA, DstRegMap);
  557. return (FromRegA && ToRegA && !regsAreCompatible(FromRegA, ToRegA, TRI));
  558. }
  559. /// ConvertInstTo3Addr - Convert the specified two-address instruction into a
  560. /// three address one. Return true if this transformation was successful.
  561. bool
  562. TwoAddressInstructionPass::ConvertInstTo3Addr(MachineBasicBlock::iterator &mi,
  563. MachineBasicBlock::iterator &nmi,
  564. MachineFunction::iterator &mbbi,
  565. unsigned RegB, unsigned Dist) {
  566. MachineInstr *NewMI = TII->convertToThreeAddress(mbbi, mi, LV);
  567. if (NewMI) {
  568. DEBUG(dbgs() << "2addr: CONVERTING 2-ADDR: " << *mi);
  569. DEBUG(dbgs() << "2addr: TO 3-ADDR: " << *NewMI);
  570. bool Sunk = false;
  571. if (NewMI->findRegisterUseOperand(RegB, false, TRI))
  572. // FIXME: Temporary workaround. If the new instruction doesn't
  573. // uses RegB, convertToThreeAddress must have created more
  574. // then one instruction.
  575. Sunk = Sink3AddrInstruction(mbbi, NewMI, RegB, mi);
  576. mbbi->erase(mi); // Nuke the old inst.
  577. if (!Sunk) {
  578. DistanceMap.insert(std::make_pair(NewMI, Dist));
  579. mi = NewMI;
  580. nmi = llvm::next(mi);
  581. }
  582. return true;
  583. }
  584. return false;
  585. }
  586. /// ProcessCopy - If the specified instruction is not yet processed, process it
  587. /// if it's a copy. For a copy instruction, we find the physical registers the
  588. /// source and destination registers might be mapped to. These are kept in
  589. /// point-to maps used to determine future optimizations. e.g.
  590. /// v1024 = mov r0
  591. /// v1025 = mov r1
  592. /// v1026 = add v1024, v1025
  593. /// r1 = mov r1026
  594. /// If 'add' is a two-address instruction, v1024, v1026 are both potentially
  595. /// coalesced to r0 (from the input side). v1025 is mapped to r1. v1026 is
  596. /// potentially joined with r1 on the output side. It's worthwhile to commute
  597. /// 'add' to eliminate a copy.
  598. void TwoAddressInstructionPass::ProcessCopy(MachineInstr *MI,
  599. MachineBasicBlock *MBB,
  600. SmallPtrSet<MachineInstr*, 8> &Processed) {
  601. if (Processed.count(MI))
  602. return;
  603. bool IsSrcPhys, IsDstPhys;
  604. unsigned SrcReg, DstReg;
  605. if (!isCopyToReg(*MI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
  606. return;
  607. if (IsDstPhys && !IsSrcPhys)
  608. DstRegMap.insert(std::make_pair(SrcReg, DstReg));
  609. else if (!IsDstPhys && IsSrcPhys) {
  610. bool isNew = SrcRegMap.insert(std::make_pair(DstReg, SrcReg)).second;
  611. if (!isNew)
  612. assert(SrcRegMap[DstReg] == SrcReg &&
  613. "Can't map to two src physical registers!");
  614. SmallVector<unsigned, 4> VirtRegPairs;
  615. bool IsCopy = false;
  616. unsigned NewReg = 0;
  617. while (MachineInstr *UseMI = findOnlyInterestingUse(DstReg, MBB, MRI,TII,
  618. IsCopy, NewReg, IsDstPhys)) {
  619. if (IsCopy) {
  620. if (!Processed.insert(UseMI))
  621. break;
  622. }
  623. DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI);
  624. if (DI != DistanceMap.end())
  625. // Earlier in the same MBB.Reached via a back edge.
  626. break;
  627. if (IsDstPhys) {
  628. VirtRegPairs.push_back(NewReg);
  629. break;
  630. }
  631. bool isNew = SrcRegMap.insert(std::make_pair(NewReg, DstReg)).second;
  632. if (!isNew)
  633. assert(SrcRegMap[NewReg] == DstReg &&
  634. "Can't map to two src physical registers!");
  635. VirtRegPairs.push_back(NewReg);
  636. DstReg = NewReg;
  637. }
  638. if (!VirtRegPairs.empty()) {
  639. unsigned ToReg = VirtRegPairs.back();
  640. VirtRegPairs.pop_back();
  641. while (!VirtRegPairs.empty()) {
  642. unsigned FromReg = VirtRegPairs.back();
  643. VirtRegPairs.pop_back();
  644. bool isNew = DstRegMap.insert(std::make_pair(FromReg, ToReg)).second;
  645. if (!isNew)
  646. assert(DstRegMap[FromReg] == ToReg &&
  647. "Can't map to two dst physical registers!");
  648. ToReg = FromReg;
  649. }
  650. }
  651. }
  652. Processed.insert(MI);
  653. }
  654. /// isSafeToDelete - If the specified instruction does not produce any side
  655. /// effects and all of its defs are dead, then it's safe to delete.
  656. static bool isSafeToDelete(MachineInstr *MI,
  657. const TargetInstrInfo *TII,
  658. SmallVector<unsigned, 4> &Kills) {
  659. const TargetInstrDesc &TID = MI->getDesc();
  660. if (TID.mayStore() || TID.isCall())
  661. return false;
  662. if (TID.isTerminator() || TID.hasUnmodeledSideEffects())
  663. return false;
  664. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  665. MachineOperand &MO = MI->getOperand(i);
  666. if (!MO.isReg())
  667. continue;
  668. if (MO.isDef() && !MO.isDead())
  669. return false;
  670. if (MO.isUse() && MO.isKill())
  671. Kills.push_back(MO.getReg());
  672. }
  673. return true;
  674. }
  675. /// canUpdateDeletedKills - Check if all the registers listed in Kills are
  676. /// killed by instructions in MBB preceding the current instruction at
  677. /// position Dist. If so, return true and record information about the
  678. /// preceding kills in NewKills.
  679. bool TwoAddressInstructionPass::
  680. canUpdateDeletedKills(SmallVector<unsigned, 4> &Kills,
  681. SmallVector<NewKill, 4> &NewKills,
  682. MachineBasicBlock *MBB, unsigned Dist) {
  683. while (!Kills.empty()) {
  684. unsigned Kill = Kills.back();
  685. Kills.pop_back();
  686. if (TargetRegisterInfo::isPhysicalRegister(Kill))
  687. return false;
  688. MachineInstr *LastKill = FindLastUseInMBB(Kill, MBB, Dist);
  689. if (!LastKill)
  690. return false;
  691. bool isModRef = LastKill->definesRegister(Kill);
  692. NewKills.push_back(std::make_pair(std::make_pair(Kill, isModRef),
  693. LastKill));
  694. }
  695. return true;
  696. }
  697. /// DeleteUnusedInstr - If an instruction with a tied register operand can
  698. /// be safely deleted, just delete it.
  699. bool
  700. TwoAddressInstructionPass::DeleteUnusedInstr(MachineBasicBlock::iterator &mi,
  701. MachineBasicBlock::iterator &nmi,
  702. MachineFunction::iterator &mbbi,
  703. unsigned Dist) {
  704. // Check if the instruction has no side effects and if all its defs are dead.
  705. SmallVector<unsigned, 4> Kills;
  706. if (!isSafeToDelete(mi, TII, Kills))
  707. return false;
  708. // If this instruction kills some virtual registers, we need to
  709. // update the kill information. If it's not possible to do so,
  710. // then bail out.
  711. SmallVector<NewKill, 4> NewKills;
  712. if (!canUpdateDeletedKills(Kills, NewKills, &*mbbi, Dist))
  713. return false;
  714. if (LV) {
  715. while (!NewKills.empty()) {
  716. MachineInstr *NewKill = NewKills.back().second;
  717. unsigned Kill = NewKills.back().first.first;
  718. bool isDead = NewKills.back().first.second;
  719. NewKills.pop_back();
  720. if (LV->removeVirtualRegisterKilled(Kill, mi)) {
  721. if (isDead)
  722. LV->addVirtualRegisterDead(Kill, NewKill);
  723. else
  724. LV->addVirtualRegisterKilled(Kill, NewKill);
  725. }
  726. }
  727. }
  728. mbbi->erase(mi); // Nuke the old inst.
  729. mi = nmi;
  730. return true;
  731. }
  732. /// TryInstructionTransform - For the case where an instruction has a single
  733. /// pair of tied register operands, attempt some transformations that may
  734. /// either eliminate the tied operands or improve the opportunities for
  735. /// coalescing away the register copy. Returns true if the tied operands
  736. /// are eliminated altogether.
  737. bool TwoAddressInstructionPass::
  738. TryInstructionTransform(MachineBasicBlock::iterator &mi,
  739. MachineBasicBlock::iterator &nmi,
  740. MachineFunction::iterator &mbbi,
  741. unsigned SrcIdx, unsigned DstIdx, unsigned Dist) {
  742. const TargetInstrDesc &TID = mi->getDesc();
  743. unsigned regA = mi->getOperand(DstIdx).getReg();
  744. unsigned regB = mi->getOperand(SrcIdx).getReg();
  745. assert(TargetRegisterInfo::isVirtualRegister(regB) &&
  746. "cannot make instruction into two-address form");
  747. // If regA is dead and the instruction can be deleted, just delete
  748. // it so it doesn't clobber regB.
  749. bool regBKilled = isKilled(*mi, regB, MRI, TII);
  750. if (!regBKilled && mi->getOperand(DstIdx).isDead() &&
  751. DeleteUnusedInstr(mi, nmi, mbbi, Dist)) {
  752. ++NumDeletes;
  753. return true; // Done with this instruction.
  754. }
  755. // Check if it is profitable to commute the operands.
  756. unsigned SrcOp1, SrcOp2;
  757. unsigned regC = 0;
  758. unsigned regCIdx = ~0U;
  759. bool TryCommute = false;
  760. bool AggressiveCommute = false;
  761. if (TID.isCommutable() && mi->getNumOperands() >= 3 &&
  762. TII->findCommutedOpIndices(mi, SrcOp1, SrcOp2)) {
  763. if (SrcIdx == SrcOp1)
  764. regCIdx = SrcOp2;
  765. else if (SrcIdx == SrcOp2)
  766. regCIdx = SrcOp1;
  767. if (regCIdx != ~0U) {
  768. regC = mi->getOperand(regCIdx).getReg();
  769. if (!regBKilled && isKilled(*mi, regC, MRI, TII))
  770. // If C dies but B does not, swap the B and C operands.
  771. // This makes the live ranges of A and C joinable.
  772. TryCommute = true;
  773. else if (isProfitableToCommute(regB, regC, mi, mbbi, Dist)) {
  774. TryCommute = true;
  775. AggressiveCommute = true;
  776. }
  777. }
  778. }
  779. // If it's profitable to commute, try to do so.
  780. if (TryCommute && CommuteInstruction(mi, mbbi, regB, regC, Dist)) {
  781. ++NumCommuted;
  782. if (AggressiveCommute)
  783. ++NumAggrCommuted;
  784. return false;
  785. }
  786. if (TID.isConvertibleTo3Addr()) {
  787. // This instruction is potentially convertible to a true
  788. // three-address instruction. Check if it is profitable.
  789. if (!regBKilled || isProfitableToConv3Addr(regA)) {
  790. // Try to convert it.
  791. if (ConvertInstTo3Addr(mi, nmi, mbbi, regB, Dist)) {
  792. ++NumConvertedTo3Addr;
  793. return true; // Done with this instruction.
  794. }
  795. }
  796. }
  797. // If this is an instruction with a load folded into it, try unfolding
  798. // the load, e.g. avoid this:
  799. // movq %rdx, %rcx
  800. // addq (%rax), %rcx
  801. // in favor of this:
  802. // movq (%rax), %rcx
  803. // addq %rdx, %rcx
  804. // because it's preferable to schedule a load than a register copy.
  805. if (TID.mayLoad() && !regBKilled) {
  806. // Determine if a load can be unfolded.
  807. unsigned LoadRegIndex;
  808. unsigned NewOpc =
  809. TII->getOpcodeAfterMemoryUnfold(mi->getOpcode(),
  810. /*UnfoldLoad=*/true,
  811. /*UnfoldStore=*/false,
  812. &LoadRegIndex);
  813. if (NewOpc != 0) {
  814. const TargetInstrDesc &UnfoldTID = TII->get(NewOpc);
  815. if (UnfoldTID.getNumDefs() == 1) {
  816. MachineFunction &MF = *mbbi->getParent();
  817. // Unfold the load.
  818. DEBUG(dbgs() << "2addr: UNFOLDING: " << *mi);
  819. const TargetRegisterClass *RC =
  820. UnfoldTID.OpInfo[LoadRegIndex].getRegClass(TRI);
  821. unsigned Reg = MRI->createVirtualRegister(RC);
  822. SmallVector<MachineInstr *, 2> NewMIs;
  823. if (!TII->unfoldMemoryOperand(MF, mi, Reg,
  824. /*UnfoldLoad=*/true,/*UnfoldStore=*/false,
  825. NewMIs)) {
  826. DEBUG(dbgs() << "2addr: ABANDONING UNFOLD\n");
  827. return false;
  828. }
  829. assert(NewMIs.size() == 2 &&
  830. "Unfolded a load into multiple instructions!");
  831. // The load was previously folded, so this is the only use.
  832. NewMIs[1]->addRegisterKilled(Reg, TRI);
  833. // Tentatively insert the instructions into the block so that they
  834. // look "normal" to the transformation logic.
  835. mbbi->insert(mi, NewMIs[0]);
  836. mbbi->insert(mi, NewMIs[1]);
  837. DEBUG(dbgs() << "2addr: NEW LOAD: " << *NewMIs[0]
  838. << "2addr: NEW INST: " << *NewMIs[1]);
  839. // Transform the instruction, now that it no longer has a load.
  840. unsigned NewDstIdx = NewMIs[1]->findRegisterDefOperandIdx(regA);
  841. unsigned NewSrcIdx = NewMIs[1]->findRegisterUseOperandIdx(regB);
  842. MachineBasicBlock::iterator NewMI = NewMIs[1];
  843. bool TransformSuccess =
  844. TryInstructionTransform(NewMI, mi, mbbi,
  845. NewSrcIdx, NewDstIdx, Dist);
  846. if (TransformSuccess ||
  847. NewMIs[1]->getOperand(NewSrcIdx).isKill()) {
  848. // Success, or at least we made an improvement. Keep the unfolded
  849. // instructions and discard the original.
  850. if (LV) {
  851. for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
  852. MachineOperand &MO = mi->getOperand(i);
  853. if (MO.isReg() && MO.getReg() != 0 &&
  854. TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
  855. if (MO.isUse()) {
  856. if (MO.isKill()) {
  857. if (NewMIs[0]->killsRegister(MO.getReg()))
  858. LV->replaceKillInstruction(MO.getReg(), mi, NewMIs[0]);
  859. else {
  860. assert(NewMIs[1]->killsRegister(MO.getReg()) &&
  861. "Kill missing after load unfold!");
  862. LV->replaceKillInstruction(MO.getReg(), mi, NewMIs[1]);
  863. }
  864. }
  865. } else if (LV->removeVirtualRegisterDead(MO.getReg(), mi)) {
  866. if (NewMIs[1]->registerDefIsDead(MO.getReg()))
  867. LV->addVirtualRegisterDead(MO.getReg(), NewMIs[1]);
  868. else {
  869. assert(NewMIs[0]->registerDefIsDead(MO.getReg()) &&
  870. "Dead flag missing after load unfold!");
  871. LV->addVirtualRegisterDead(MO.getReg(), NewMIs[0]);
  872. }
  873. }
  874. }
  875. }
  876. LV->addVirtualRegisterKilled(Reg, NewMIs[1]);
  877. }
  878. mi->eraseFromParent();
  879. mi = NewMIs[1];
  880. if (TransformSuccess)
  881. return true;
  882. } else {
  883. // Transforming didn't eliminate the tie and didn't lead to an
  884. // improvement. Clean up the unfolded instructions and keep the
  885. // original.
  886. DEBUG(dbgs() << "2addr: ABANDONING UNFOLD\n");
  887. NewMIs[0]->eraseFromParent();
  888. NewMIs[1]->eraseFromParent();
  889. }
  890. }
  891. }
  892. }
  893. return false;
  894. }
  895. /// runOnMachineFunction - Reduce two-address instructions to two operands.
  896. ///
  897. bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
  898. DEBUG(dbgs() << "Machine Function\n");
  899. const TargetMachine &TM = MF.getTarget();
  900. MRI = &MF.getRegInfo();
  901. TII = TM.getInstrInfo();
  902. TRI = TM.getRegisterInfo();
  903. LV = getAnalysisIfAvailable<LiveVariables>();
  904. AA = &getAnalysis<AliasAnalysis>();
  905. bool MadeChange = false;
  906. DEBUG(dbgs() << "********** REWRITING TWO-ADDR INSTRS **********\n");
  907. DEBUG(dbgs() << "********** Function: "
  908. << MF.getFunction()->getName() << '\n');
  909. // ReMatRegs - Keep track of the registers whose def's are remat'ed.
  910. BitVector ReMatRegs;
  911. ReMatRegs.resize(MRI->getLastVirtReg()+1);
  912. typedef DenseMap<unsigned, SmallVector<std::pair<unsigned, unsigned>, 4> >
  913. TiedOperandMap;
  914. TiedOperandMap TiedOperands(4);
  915. SmallPtrSet<MachineInstr*, 8> Processed;
  916. for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
  917. mbbi != mbbe; ++mbbi) {
  918. unsigned Dist = 0;
  919. DistanceMap.clear();
  920. SrcRegMap.clear();
  921. DstRegMap.clear();
  922. Processed.clear();
  923. for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
  924. mi != me; ) {
  925. MachineBasicBlock::iterator nmi = llvm::next(mi);
  926. if (mi->isDebugValue()) {
  927. mi = nmi;
  928. continue;
  929. }
  930. // Remember REG_SEQUENCE instructions, we'll deal with them later.
  931. if (mi->isRegSequence())
  932. RegSequences.push_back(&*mi);
  933. const TargetInstrDesc &TID = mi->getDesc();
  934. bool FirstTied = true;
  935. DistanceMap.insert(std::make_pair(mi, ++Dist));
  936. ProcessCopy(&*mi, &*mbbi, Processed);
  937. // First scan through all the tied register uses in this instruction
  938. // and record a list of pairs of tied operands for each register.
  939. unsigned NumOps = mi->isInlineAsm()
  940. ? mi->getNumOperands() : TID.getNumOperands();
  941. for (unsigned SrcIdx = 0; SrcIdx < NumOps; ++SrcIdx) {
  942. unsigned DstIdx = 0;
  943. if (!mi->isRegTiedToDefOperand(SrcIdx, &DstIdx))
  944. continue;
  945. if (FirstTied) {
  946. FirstTied = false;
  947. ++NumTwoAddressInstrs;
  948. DEBUG(dbgs() << '\t' << *mi);
  949. }
  950. assert(mi->getOperand(SrcIdx).isReg() &&
  951. mi->getOperand(SrcIdx).getReg() &&
  952. mi->getOperand(SrcIdx).isUse() &&
  953. "two address instruction invalid");
  954. unsigned regB = mi->getOperand(SrcIdx).getReg();
  955. TiedOperandMap::iterator OI = TiedOperands.find(regB);
  956. if (OI == TiedOperands.end()) {
  957. SmallVector<std::pair<unsigned, unsigned>, 4> TiedPair;
  958. OI = TiedOperands.insert(std::make_pair(regB, TiedPair)).first;
  959. }
  960. OI->second.push_back(std::make_pair(SrcIdx, DstIdx));
  961. }
  962. // Now iterate over the information collected above.
  963. for (TiedOperandMap::iterator OI = TiedOperands.begin(),
  964. OE = TiedOperands.end(); OI != OE; ++OI) {
  965. SmallVector<std::pair<unsigned, unsigned>, 4> &TiedPairs = OI->second;
  966. // If the instruction has a single pair of tied operands, try some
  967. // transformations that may either eliminate the tied operands or
  968. // improve the opportunities for coalescing away the register copy.
  969. if (TiedOperands.size() == 1 && TiedPairs.size() == 1) {
  970. unsigned SrcIdx = TiedPairs[0].first;
  971. unsigned DstIdx = TiedPairs[0].second;
  972. // If the registers are already equal, nothing needs to be done.
  973. if (mi->getOperand(SrcIdx).getReg() ==
  974. mi->getOperand(DstIdx).getReg())
  975. break; // Done with this instruction.
  976. if (TryInstructionTransform(mi, nmi, mbbi, SrcIdx, DstIdx, Dist))
  977. break; // The tied operands have been eliminated.
  978. }
  979. bool RemovedKillFlag = false;
  980. bool AllUsesCopied = true;
  981. unsigned LastCopiedReg = 0;
  982. unsigned regB = OI->first;
  983. for (unsigned tpi = 0, tpe = TiedPairs.size(); tpi != tpe; ++tpi) {
  984. unsigned SrcIdx = TiedPairs[tpi].first;
  985. unsigned DstIdx = TiedPairs[tpi].second;
  986. unsigned regA = mi->getOperand(DstIdx).getReg();
  987. // Grab regB from the instruction because it may have changed if the
  988. // instruction was commuted.
  989. regB = mi->getOperand(SrcIdx).getReg();
  990. if (regA == regB) {
  991. // The register is tied to multiple destinations (or else we would
  992. // not have continued this far), but this use of the register
  993. // already matches the tied destination. Leave it.
  994. AllUsesCopied = false;
  995. continue;
  996. }
  997. LastCopiedReg = regA;
  998. assert(TargetRegisterInfo::isVirtualRegister(regB) &&
  999. "cannot make instruction into two-address form");
  1000. #ifndef NDEBUG
  1001. // First, verify that we don't have a use of "a" in the instruction
  1002. // (a = b + a for example) because our transformation will not
  1003. // work. This should never occur because we are in SSA form.
  1004. for (unsigned i = 0; i != mi->getNumOperands(); ++i)
  1005. assert(i == DstIdx ||
  1006. !mi->getOperand(i).isReg() ||
  1007. mi->getOperand(i).getReg() != regA);
  1008. #endif
  1009. // Emit a copy or rematerialize the definition.
  1010. const TargetRegisterClass *rc = MRI->getRegClass(regB);
  1011. MachineInstr *DefMI = MRI->getVRegDef(regB);
  1012. // If it's safe and profitable, remat the definition instead of
  1013. // copying it.
  1014. if (DefMI &&
  1015. DefMI->getDesc().isAsCheapAsAMove() &&
  1016. DefMI->isSafeToReMat(TII, AA, regB) &&
  1017. isProfitableToReMat(regB, rc, mi, DefMI, mbbi, Dist)){
  1018. DEBUG(dbgs() << "2addr: REMATTING : " << *DefMI << "\n");
  1019. unsigned regASubIdx = mi->getOperand(DstIdx).getSubReg();
  1020. TII->reMaterialize(*mbbi, mi, regA, regASubIdx, DefMI, *TRI);
  1021. ReMatRegs.set(regB);
  1022. ++NumReMats;
  1023. } else {
  1024. BuildMI(*mbbi, mi, mi->getDebugLoc(), TII->get(TargetOpcode::COPY),
  1025. regA).addReg(regB);
  1026. }
  1027. MachineBasicBlock::iterator prevMI = prior(mi);
  1028. // Update DistanceMap.
  1029. DistanceMap.insert(std::make_pair(prevMI, Dist));
  1030. DistanceMap[mi] = ++Dist;
  1031. DEBUG(dbgs() << "\t\tprepend:\t" << *prevMI);
  1032. MachineOperand &MO = mi->getOperand(SrcIdx);
  1033. assert(MO.isReg() && MO.getReg() == regB && MO.isUse() &&
  1034. "inconsistent operand info for 2-reg pass");
  1035. if (MO.isKill()) {
  1036. MO.setIsKill(false);
  1037. RemovedKillFlag = true;
  1038. }
  1039. MO.setReg(regA);
  1040. }
  1041. if (AllUsesCopied) {
  1042. // Replace other (un-tied) uses of regB with LastCopiedReg.
  1043. for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
  1044. MachineOperand &MO = mi->getOperand(i);
  1045. if (MO.isReg() && MO.getReg() == regB && MO.isUse()) {
  1046. if (MO.isKill()) {
  1047. MO.setIsKill(false);
  1048. RemovedKillFlag = true;
  1049. }
  1050. MO.setReg(LastCopiedReg);
  1051. }
  1052. }
  1053. // Update live variables for regB.
  1054. if (RemovedKillFlag && LV && LV->getVarInfo(regB).removeKill(mi))
  1055. LV->addVirtualRegisterKilled(regB, prior(mi));
  1056. } else if (RemovedKillFlag) {
  1057. // Some tied uses of regB matched their destination registers, so
  1058. // regB is still used in this instruction, but a kill flag was
  1059. // removed from a different tied use of regB, so now we need to add
  1060. // a kill flag to one of the remaining uses of regB.
  1061. for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
  1062. MachineOperand &MO = mi->getOperand(i);
  1063. if (MO.isReg() && MO.getReg() == regB && MO.isUse()) {
  1064. MO.setIsKill(true);
  1065. break;
  1066. }
  1067. }
  1068. }
  1069. // Schedule the source copy / remat inserted to form two-address
  1070. // instruction. FIXME: Does it matter the distance map may not be
  1071. // accurate after it's scheduled?
  1072. TII->scheduleTwoAddrSource(prior(mi), mi, *TRI);
  1073. MadeChange = true;
  1074. DEBUG(dbgs() << "\t\trewrite to:\t" << *mi);
  1075. }
  1076. // Rewrite INSERT_SUBREG as COPY now that we no longer need SSA form.
  1077. if (mi->isInsertSubreg()) {
  1078. // From %reg = INSERT_SUBREG %reg, %subreg, subidx
  1079. // To %reg:subidx = COPY %subreg
  1080. unsigned SubIdx = mi->getOperand(3).getImm();
  1081. mi->RemoveOperand(3);
  1082. assert(mi->getOperand(0).getSubReg() == 0 && "Unexpected subreg idx");
  1083. mi->getOperand(0).setSubReg(SubIdx);
  1084. mi->RemoveOperand(1);
  1085. mi->setDesc(TII->get(TargetOpcode::COPY));
  1086. DEBUG(dbgs() << "\t\tconvert to:\t" << *mi);
  1087. }
  1088. // Clear TiedOperands here instead of at the top of the loop
  1089. // since most instructions do not have tied operands.
  1090. TiedOperands.clear();
  1091. mi = nmi;
  1092. }
  1093. }
  1094. // Some remat'ed instructions are dead.
  1095. int VReg = ReMatRegs.find_first();
  1096. while (VReg != -1) {
  1097. if (MRI->use_nodbg_empty(VReg)) {
  1098. MachineInstr *DefMI = MRI->getVRegDef(VReg);
  1099. DefMI->eraseFromParent();
  1100. }
  1101. VReg = ReMatRegs.find_next(VReg);
  1102. }
  1103. // Eliminate REG_SEQUENCE instructions. Their whole purpose was to preseve
  1104. // SSA form. It's now safe to de-SSA.
  1105. MadeChange |= EliminateRegSequences();
  1106. return MadeChange;
  1107. }
  1108. static void UpdateRegSequenceSrcs(unsigned SrcReg,
  1109. unsigned DstReg, unsigned SubIdx,
  1110. MachineRegisterInfo *MRI,
  1111. const TargetRegisterInfo &TRI) {
  1112. for (MachineRegisterInfo::reg_iterator RI = MRI->reg_begin(SrcReg),
  1113. RE = MRI->reg_end(); RI != RE; ) {
  1114. MachineOperand &MO = RI.getOperand();
  1115. ++RI;
  1116. MO.substVirtReg(DstReg, SubIdx, TRI);
  1117. }
  1118. }
  1119. /// CoalesceExtSubRegs - If a number of sources of the REG_SEQUENCE are
  1120. /// EXTRACT_SUBREG from the same register and to the same virtual register
  1121. /// with different sub-register indices, attempt to combine the
  1122. /// EXTRACT_SUBREGs and pre-coalesce them. e.g.
  1123. /// %reg1026<def> = VLDMQ %reg1025<kill>, 260, pred:14, pred:%reg0
  1124. /// %reg1029:6<def> = EXTRACT_SUBREG %reg1026, 6
  1125. /// %reg1029:5<def> = EXTRACT_SUBREG %reg1026<kill>, 5
  1126. /// Since D subregs 5, 6 can combine to a Q register, we can coalesce
  1127. /// reg1026 to reg1029.
  1128. void
  1129. TwoAddressInstructionPass::CoalesceExtSubRegs(SmallVector<unsigned,4> &Srcs,
  1130. unsigned DstReg) {
  1131. SmallSet<unsigned, 4> Seen;
  1132. for (unsigned i = 0, e = Srcs.size(); i != e; ++i) {
  1133. unsigned SrcReg = Srcs[i];
  1134. if (!Seen.insert(SrcReg))
  1135. continue;
  1136. // Check that the instructions are all in the same basic block.
  1137. MachineInstr *SrcDefMI = MRI->getVRegDef(SrcReg);
  1138. MachineInstr *DstDefMI = MRI->getVRegDef(DstReg);
  1139. if (SrcDefMI->getParent() != DstDefMI->getParent())
  1140. continue;
  1141. // If there are no other uses than copies which feed into
  1142. // the reg_sequence, then we might be able to coalesce them.
  1143. bool CanCoalesce = true;
  1144. SmallVector<unsigned, 4> SrcSubIndices, DstSubIndices;
  1145. for (MachineRegisterInfo::use_nodbg_iterator
  1146. UI = MRI->use_nodbg_begin(SrcReg),
  1147. UE = MRI->use_nodbg_end(); UI != UE; ++UI) {
  1148. MachineInstr *UseMI = &*UI;
  1149. if (!UseMI->isCopy() || UseMI->getOperand(0).getReg() != DstReg) {
  1150. CanCoalesce = false;
  1151. break;
  1152. }
  1153. SrcSubIndices.push_back(UseMI->getOperand(1).getSubReg());
  1154. DstSubIndices.push_back(UseMI->getOperand(0).getSubReg());
  1155. }
  1156. if (!CanCoalesce || SrcSubIndices.size() < 2)
  1157. continue;
  1158. // Check that the source subregisters can be combined.
  1159. std::sort(SrcSubIndices.begin(), SrcSubIndices.end());
  1160. unsigned NewSrcSubIdx = 0;
  1161. if (!TRI->canCombineSubRegIndices(MRI->getRegClass(SrcReg), SrcSubIndices,
  1162. NewSrcSubIdx))
  1163. continue;
  1164. // Check that the destination subregisters can also be combined.
  1165. std::sort(DstSubIndices.begin(), DstSubIndices.end());
  1166. unsigned NewDstSubIdx = 0;
  1167. if (!TRI->canCombineSubRegIndices(MRI->getRegClass(DstReg), DstSubIndices,
  1168. NewDstSubIdx))
  1169. continue;
  1170. // If neither source nor destination can be combined to the full register,
  1171. // just give up. This could be improved if it ever matters.
  1172. if (NewSrcSubIdx != 0 && NewDstSubIdx != 0)
  1173. continue;
  1174. // Now that we know that all the uses are extract_subregs and that those
  1175. // subregs can somehow be combined, scan all the extract_subregs again to
  1176. // make sure the subregs are in the right order and can be composed.
  1177. MachineInstr *SomeMI = 0;
  1178. CanCoalesce = true;
  1179. for (MachineRegisterInfo::use_nodbg_iterator
  1180. UI = MRI->use_nodbg_begin(SrcReg),
  1181. UE = MRI->use_nodbg_end(); UI != UE; ++UI) {
  1182. MachineInstr *UseMI = &*UI;
  1183. assert(UseMI->isCopy());
  1184. unsigned DstSubIdx = UseMI->getOperand(0).getSubReg();
  1185. unsigned SrcSubIdx = UseMI->getOperand(1).getSubReg();
  1186. assert(DstSubIdx != 0 && "missing subreg from RegSequence elimination");
  1187. if ((NewDstSubIdx == 0 &&
  1188. TRI->composeSubRegIndices(NewSrcSubIdx, DstSubIdx) != SrcSubIdx) ||
  1189. (NewSrcSubIdx == 0 &&
  1190. TRI->composeSubRegIndices(NewDstSubIdx, SrcSubIdx) != DstSubIdx)) {
  1191. CanCoalesce = false;
  1192. break;
  1193. }
  1194. // Keep track of one of the uses.
  1195. SomeMI = UseMI;
  1196. }
  1197. if (!CanCoalesce)
  1198. continue;
  1199. // Insert a copy to replace the original.
  1200. MachineBasicBlock::iterator InsertLoc = SomeMI;
  1201. MachineInstr *CopyMI = BuildMI(*SomeMI->getParent(), SomeMI,
  1202. SomeMI->getDebugLoc(),
  1203. TII->get(TargetOpcode::COPY))
  1204. .addReg(DstReg, RegState::Define, NewDstSubIdx)
  1205. .addReg(SrcReg, 0, NewSrcSubIdx);
  1206. // Remove all the old extract instructions.
  1207. for (MachineRegisterInfo::use_nodbg_iterator
  1208. UI = MRI->use_nodbg_begin(SrcReg),
  1209. UE = MRI->use_nodbg_end(); UI != UE; ) {
  1210. MachineInstr *UseMI = &*UI;
  1211. ++UI;
  1212. if (UseMI == CopyMI)
  1213. continue;
  1214. assert(UseMI->isCopy());
  1215. // Move any kills to the new copy or extract instruction.
  1216. if (UseMI->getOperand(1).isKill()) {
  1217. CopyMI->getOperand(1).setIsKill();
  1218. if (LV)
  1219. // Update live variables
  1220. LV->replaceKillInstruction(SrcReg, UseMI, &*CopyMI);
  1221. }
  1222. UseMI->eraseFromParent();
  1223. }
  1224. }
  1225. }
  1226. static bool HasOtherRegSequenceUses(unsigned Reg, MachineInstr *RegSeq,
  1227. MachineRegisterInfo *MRI) {
  1228. for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg),
  1229. UE = MRI->use_end(); UI != UE; ++UI) {
  1230. MachineInstr *UseMI = &*UI;
  1231. if (UseMI != RegSeq && UseMI->isRegSequence())
  1232. return true;
  1233. }
  1234. return false;
  1235. }
  1236. /// EliminateRegSequences - Eliminate REG_SEQUENCE instructions as part
  1237. /// of the de-ssa process. This replaces sources of REG_SEQUENCE as
  1238. /// sub-register references of the register defined by REG_SEQUENCE. e.g.
  1239. ///
  1240. /// %reg1029<def>, %reg1030<def> = VLD1q16 %reg1024<kill>, ...
  1241. /// %reg1031<def> = REG_SEQUENCE %reg1029<kill>, 5, %reg1030<kill>, 6
  1242. /// =>
  1243. /// %reg1031:5<def>, %reg1031:6<def> = VLD1q16 %reg1024<kill>, ...
  1244. bool TwoAddressInstructionPass::EliminateRegSequences() {
  1245. if (RegSequences.empty())
  1246. return false;
  1247. for (unsigned i = 0, e = RegSequences.size(); i != e; ++i) {
  1248. MachineInstr *MI = RegSequences[i];
  1249. unsigned DstReg = MI->getOperand(0).getReg();
  1250. if (MI->getOperand(0).getSubReg() ||
  1251. TargetRegisterInfo::isPhysicalRegister(DstReg) ||
  1252. !(MI->getNumOperands() & 1)) {
  1253. DEBUG(dbgs() << "Illegal REG_SEQUENCE instruction:" << *MI);
  1254. llvm_unreachable(0);
  1255. }
  1256. bool IsImpDef = true;
  1257. SmallVector<unsigned, 4> RealSrcs;
  1258. SmallSet<unsigned, 4> Seen;
  1259. for (unsigned i = 1, e = MI->getNumOperands(); i < e; i += 2) {
  1260. unsigned SrcReg = MI->getOperand(i).getReg();
  1261. if (MI->getOperand(i).getSubReg() ||
  1262. TargetRegisterInfo::isPhysicalRegister(SrcReg)) {
  1263. DEBUG(dbgs() << "Illegal REG_SEQUENCE instruction:" << *MI);
  1264. llvm_unreachable(0);
  1265. }
  1266. MachineInstr *DefMI = MRI->getVRegDef(SrcReg);
  1267. if (DefMI->isImplicitDef()) {
  1268. DefMI->eraseFromParent();
  1269. continue;
  1270. }
  1271. IsImpDef = false;
  1272. // Remember COPY sources. These might be candidate for coalescing.
  1273. if (DefMI->isCopy() && DefMI->getOperand(1).getSubReg())
  1274. RealSrcs.push_back(DefMI->getOperand(1).getReg());
  1275. bool isKill = MI->getOperand(i).isKill();
  1276. if (!Seen.insert(SrcReg) || MI->getParent() != DefMI->getParent() ||
  1277. !isKill || HasOtherRegSequenceUses(SrcReg, MI, MRI)) {
  1278. // REG_SEQUENCE cannot have duplicated operands, add a copy.
  1279. // Also add an copy if the source is live-in the block. We don't want
  1280. // to end up with a partial-redef of a livein, e.g.
  1281. // BB0:
  1282. // reg1051:10<def> =
  1283. // ...
  1284. // BB1:
  1285. // ... = reg1051:10
  1286. // BB2:
  1287. // reg1051:9<def> =
  1288. // LiveIntervalAnalysis won't like it.
  1289. //
  1290. // If the REG_SEQUENCE doesn't kill its source, keeping live variables
  1291. // correctly up to date becomes very difficult. Insert a copy.
  1292. // Defer any kill flag to the last operand using SrcReg. Otherwise, we
  1293. // might insert a COPY that uses SrcReg after is was killed.
  1294. if (isKill)
  1295. for (unsigned j = i + 2; j < e; j += 2)
  1296. if (MI->getOperand(j).getReg() == SrcReg) {
  1297. MI->getOperand(j).setIsKill();
  1298. isKill = false;
  1299. break;
  1300. }
  1301. MachineBasicBlock::iterator InsertLoc = MI;
  1302. MachineInstr *CopyMI = BuildMI(*MI->getParent(), InsertLoc,
  1303. MI->getDebugLoc(), TII->get(TargetOpcode::COPY))
  1304. .addReg(DstReg, RegState::Define, MI->getOperand(i+1).getImm())
  1305. .addReg(SrcReg, getKillRegState(isKill));
  1306. MI->getOperand(i).setReg(0);
  1307. if (LV && isKill)
  1308. LV->replaceKillInstruction(SrcReg, MI, CopyMI);
  1309. DEBUG(dbgs() << "Inserted: " << *CopyMI);
  1310. }
  1311. }
  1312. for (unsigned i = 1, e = MI->getNumOperands(); i < e; i += 2) {
  1313. unsigned SrcReg = MI->getOperand(i).getReg();
  1314. if (!SrcReg) continue;
  1315. unsigned SubIdx = MI->getOperand(i+1).getImm();
  1316. UpdateRegSequenceSrcs(SrcReg, DstReg, SubIdx, MRI, *TRI);
  1317. }
  1318. if (IsImpDef) {
  1319. DEBUG(dbgs() << "Turned: " << *MI << " into an IMPLICIT_DEF");
  1320. MI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
  1321. for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j)
  1322. MI->RemoveOperand(j);
  1323. } else {
  1324. DEBUG(dbgs() << "Eliminated: " << *MI);
  1325. MI->eraseFromParent();
  1326. }
  1327. // Try coalescing some EXTRACT_SUBREG instructions. This can create
  1328. // INSERT_SUBREG instructions that must have <undef> flags added by
  1329. // LiveIntervalAnalysis, so only run it when LiveVariables is available.
  1330. if (LV)
  1331. CoalesceExtSubRegs(RealSrcs, DstReg);
  1332. }
  1333. RegSequences.clear();
  1334. return true;
  1335. }