DetectDeadLanes.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. //===- DetectDeadLanes.cpp - SubRegister Lane Usage Analysis --*- C++ -*---===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. /// \file
  10. /// Analysis that tracks defined/used subregister lanes across COPY instructions
  11. /// and instructions that get lowered to a COPY (PHI, REG_SEQUENCE,
  12. /// INSERT_SUBREG, EXTRACT_SUBREG).
  13. /// The information is used to detect dead definitions and the usage of
  14. /// (completely) undefined values and mark the operands as such.
  15. /// This pass is necessary because the dead/undef status is not obvious anymore
  16. /// when subregisters are involved.
  17. ///
  18. /// Example:
  19. /// %0 = some definition
  20. /// %1 = IMPLICIT_DEF
  21. /// %2 = REG_SEQUENCE %0, sub0, %1, sub1
  22. /// %3 = EXTRACT_SUBREG %2, sub1
  23. /// = use %3
  24. /// The %0 definition is dead and %3 contains an undefined value.
  25. //
  26. //===----------------------------------------------------------------------===//
  27. #include "llvm/ADT/BitVector.h"
  28. #include "llvm/CodeGen/MachineFunctionPass.h"
  29. #include "llvm/CodeGen/MachineRegisterInfo.h"
  30. #include "llvm/CodeGen/Passes.h"
  31. #include "llvm/CodeGen/TargetRegisterInfo.h"
  32. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  33. #include "llvm/InitializePasses.h"
  34. #include "llvm/Pass.h"
  35. #include "llvm/PassRegistry.h"
  36. #include "llvm/Support/Debug.h"
  37. #include "llvm/Support/raw_ostream.h"
  38. #include <deque>
  39. using namespace llvm;
  40. #define DEBUG_TYPE "detect-dead-lanes"
  41. namespace {
  42. /// Contains a bitmask of which lanes of a given virtual register are
  43. /// defined and which ones are actually used.
  44. struct VRegInfo {
  45. LaneBitmask UsedLanes;
  46. LaneBitmask DefinedLanes;
  47. };
  48. class DetectDeadLanes : public MachineFunctionPass {
  49. public:
  50. bool runOnMachineFunction(MachineFunction &MF) override;
  51. static char ID;
  52. DetectDeadLanes() : MachineFunctionPass(ID) {}
  53. StringRef getPassName() const override { return "Detect Dead Lanes"; }
  54. void getAnalysisUsage(AnalysisUsage &AU) const override {
  55. AU.setPreservesCFG();
  56. MachineFunctionPass::getAnalysisUsage(AU);
  57. }
  58. private:
  59. /// Add used lane bits on the register used by operand \p MO. This translates
  60. /// the bitmask based on the operands subregister, and puts the register into
  61. /// the worklist if any new bits were added.
  62. void addUsedLanesOnOperand(const MachineOperand &MO, LaneBitmask UsedLanes);
  63. /// Given a bitmask \p UsedLanes for the used lanes on a def output of a
  64. /// COPY-like instruction determine the lanes used on the use operands
  65. /// and call addUsedLanesOnOperand() for them.
  66. void transferUsedLanesStep(const MachineInstr &MI, LaneBitmask UsedLanes);
  67. /// Given a use regiser operand \p Use and a mask of defined lanes, check
  68. /// if the operand belongs to a lowersToCopies() instruction, transfer the
  69. /// mask to the def and put the instruction into the worklist.
  70. void transferDefinedLanesStep(const MachineOperand &Use,
  71. LaneBitmask DefinedLanes);
  72. /// Given a mask \p DefinedLanes of lanes defined at operand \p OpNum
  73. /// of COPY-like instruction, determine which lanes are defined at the output
  74. /// operand \p Def.
  75. LaneBitmask transferDefinedLanes(const MachineOperand &Def, unsigned OpNum,
  76. LaneBitmask DefinedLanes) const;
  77. /// Given a mask \p UsedLanes used from the output of instruction \p MI
  78. /// determine which lanes are used from operand \p MO of this instruction.
  79. LaneBitmask transferUsedLanes(const MachineInstr &MI, LaneBitmask UsedLanes,
  80. const MachineOperand &MO) const;
  81. bool runOnce(MachineFunction &MF);
  82. LaneBitmask determineInitialDefinedLanes(unsigned Reg);
  83. LaneBitmask determineInitialUsedLanes(unsigned Reg);
  84. bool isUndefRegAtInput(const MachineOperand &MO,
  85. const VRegInfo &RegInfo) const;
  86. bool isUndefInput(const MachineOperand &MO, bool *CrossCopy) const;
  87. const MachineRegisterInfo *MRI;
  88. const TargetRegisterInfo *TRI;
  89. void PutInWorklist(unsigned RegIdx) {
  90. if (WorklistMembers.test(RegIdx))
  91. return;
  92. WorklistMembers.set(RegIdx);
  93. Worklist.push_back(RegIdx);
  94. }
  95. VRegInfo *VRegInfos;
  96. /// Worklist containing virtreg indexes.
  97. std::deque<unsigned> Worklist;
  98. BitVector WorklistMembers;
  99. /// This bitvector is set for each vreg index where the vreg is defined
  100. /// by an instruction where lowersToCopies()==true.
  101. BitVector DefinedByCopy;
  102. };
  103. } // end anonymous namespace
  104. char DetectDeadLanes::ID = 0;
  105. char &llvm::DetectDeadLanesID = DetectDeadLanes::ID;
  106. INITIALIZE_PASS(DetectDeadLanes, DEBUG_TYPE, "Detect Dead Lanes", false, false)
  107. /// Returns true if \p MI will get lowered to a series of COPY instructions.
  108. /// We call this a COPY-like instruction.
  109. static bool lowersToCopies(const MachineInstr &MI) {
  110. // Note: We could support instructions with MCInstrDesc::isRegSequenceLike(),
  111. // isExtractSubRegLike(), isInsertSubregLike() in the future even though they
  112. // are not lowered to a COPY.
  113. switch (MI.getOpcode()) {
  114. case TargetOpcode::COPY:
  115. case TargetOpcode::PHI:
  116. case TargetOpcode::INSERT_SUBREG:
  117. case TargetOpcode::REG_SEQUENCE:
  118. case TargetOpcode::EXTRACT_SUBREG:
  119. return true;
  120. }
  121. return false;
  122. }
  123. static bool isCrossCopy(const MachineRegisterInfo &MRI,
  124. const MachineInstr &MI,
  125. const TargetRegisterClass *DstRC,
  126. const MachineOperand &MO) {
  127. assert(lowersToCopies(MI));
  128. Register SrcReg = MO.getReg();
  129. const TargetRegisterClass *SrcRC = MRI.getRegClass(SrcReg);
  130. if (DstRC == SrcRC)
  131. return false;
  132. unsigned SrcSubIdx = MO.getSubReg();
  133. const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
  134. unsigned DstSubIdx = 0;
  135. switch (MI.getOpcode()) {
  136. case TargetOpcode::INSERT_SUBREG:
  137. if (MI.getOperandNo(&MO) == 2)
  138. DstSubIdx = MI.getOperand(3).getImm();
  139. break;
  140. case TargetOpcode::REG_SEQUENCE: {
  141. unsigned OpNum = MI.getOperandNo(&MO);
  142. DstSubIdx = MI.getOperand(OpNum+1).getImm();
  143. break;
  144. }
  145. case TargetOpcode::EXTRACT_SUBREG: {
  146. unsigned SubReg = MI.getOperand(2).getImm();
  147. SrcSubIdx = TRI.composeSubRegIndices(SubReg, SrcSubIdx);
  148. }
  149. }
  150. unsigned PreA, PreB; // Unused.
  151. if (SrcSubIdx && DstSubIdx)
  152. return !TRI.getCommonSuperRegClass(SrcRC, SrcSubIdx, DstRC, DstSubIdx, PreA,
  153. PreB);
  154. if (SrcSubIdx)
  155. return !TRI.getMatchingSuperRegClass(SrcRC, DstRC, SrcSubIdx);
  156. if (DstSubIdx)
  157. return !TRI.getMatchingSuperRegClass(DstRC, SrcRC, DstSubIdx);
  158. return !TRI.getCommonSubClass(SrcRC, DstRC);
  159. }
  160. void DetectDeadLanes::addUsedLanesOnOperand(const MachineOperand &MO,
  161. LaneBitmask UsedLanes) {
  162. if (!MO.readsReg())
  163. return;
  164. Register MOReg = MO.getReg();
  165. if (!Register::isVirtualRegister(MOReg))
  166. return;
  167. unsigned MOSubReg = MO.getSubReg();
  168. if (MOSubReg != 0)
  169. UsedLanes = TRI->composeSubRegIndexLaneMask(MOSubReg, UsedLanes);
  170. UsedLanes &= MRI->getMaxLaneMaskForVReg(MOReg);
  171. unsigned MORegIdx = Register::virtReg2Index(MOReg);
  172. VRegInfo &MORegInfo = VRegInfos[MORegIdx];
  173. LaneBitmask PrevUsedLanes = MORegInfo.UsedLanes;
  174. // Any change at all?
  175. if ((UsedLanes & ~PrevUsedLanes).none())
  176. return;
  177. // Set UsedLanes and remember instruction for further propagation.
  178. MORegInfo.UsedLanes = PrevUsedLanes | UsedLanes;
  179. if (DefinedByCopy.test(MORegIdx))
  180. PutInWorklist(MORegIdx);
  181. }
  182. void DetectDeadLanes::transferUsedLanesStep(const MachineInstr &MI,
  183. LaneBitmask UsedLanes) {
  184. for (const MachineOperand &MO : MI.uses()) {
  185. if (!MO.isReg() || !Register::isVirtualRegister(MO.getReg()))
  186. continue;
  187. LaneBitmask UsedOnMO = transferUsedLanes(MI, UsedLanes, MO);
  188. addUsedLanesOnOperand(MO, UsedOnMO);
  189. }
  190. }
  191. LaneBitmask DetectDeadLanes::transferUsedLanes(const MachineInstr &MI,
  192. LaneBitmask UsedLanes,
  193. const MachineOperand &MO) const {
  194. unsigned OpNum = MI.getOperandNo(&MO);
  195. assert(lowersToCopies(MI) &&
  196. DefinedByCopy[Register::virtReg2Index(MI.getOperand(0).getReg())]);
  197. switch (MI.getOpcode()) {
  198. case TargetOpcode::COPY:
  199. case TargetOpcode::PHI:
  200. return UsedLanes;
  201. case TargetOpcode::REG_SEQUENCE: {
  202. assert(OpNum % 2 == 1);
  203. unsigned SubIdx = MI.getOperand(OpNum + 1).getImm();
  204. return TRI->reverseComposeSubRegIndexLaneMask(SubIdx, UsedLanes);
  205. }
  206. case TargetOpcode::INSERT_SUBREG: {
  207. unsigned SubIdx = MI.getOperand(3).getImm();
  208. LaneBitmask MO2UsedLanes =
  209. TRI->reverseComposeSubRegIndexLaneMask(SubIdx, UsedLanes);
  210. if (OpNum == 2)
  211. return MO2UsedLanes;
  212. const MachineOperand &Def = MI.getOperand(0);
  213. Register DefReg = Def.getReg();
  214. const TargetRegisterClass *RC = MRI->getRegClass(DefReg);
  215. LaneBitmask MO1UsedLanes;
  216. if (RC->CoveredBySubRegs)
  217. MO1UsedLanes = UsedLanes & ~TRI->getSubRegIndexLaneMask(SubIdx);
  218. else
  219. MO1UsedLanes = RC->LaneMask;
  220. assert(OpNum == 1);
  221. return MO1UsedLanes;
  222. }
  223. case TargetOpcode::EXTRACT_SUBREG: {
  224. assert(OpNum == 1);
  225. unsigned SubIdx = MI.getOperand(2).getImm();
  226. return TRI->composeSubRegIndexLaneMask(SubIdx, UsedLanes);
  227. }
  228. default:
  229. llvm_unreachable("function must be called with COPY-like instruction");
  230. }
  231. }
  232. void DetectDeadLanes::transferDefinedLanesStep(const MachineOperand &Use,
  233. LaneBitmask DefinedLanes) {
  234. if (!Use.readsReg())
  235. return;
  236. // Check whether the operand writes a vreg and is part of a COPY-like
  237. // instruction.
  238. const MachineInstr &MI = *Use.getParent();
  239. if (MI.getDesc().getNumDefs() != 1)
  240. return;
  241. // FIXME: PATCHPOINT instructions announce a Def that does not always exist,
  242. // they really need to be modeled differently!
  243. if (MI.getOpcode() == TargetOpcode::PATCHPOINT)
  244. return;
  245. const MachineOperand &Def = *MI.defs().begin();
  246. Register DefReg = Def.getReg();
  247. if (!Register::isVirtualRegister(DefReg))
  248. return;
  249. unsigned DefRegIdx = Register::virtReg2Index(DefReg);
  250. if (!DefinedByCopy.test(DefRegIdx))
  251. return;
  252. unsigned OpNum = MI.getOperandNo(&Use);
  253. DefinedLanes =
  254. TRI->reverseComposeSubRegIndexLaneMask(Use.getSubReg(), DefinedLanes);
  255. DefinedLanes = transferDefinedLanes(Def, OpNum, DefinedLanes);
  256. VRegInfo &RegInfo = VRegInfos[DefRegIdx];
  257. LaneBitmask PrevDefinedLanes = RegInfo.DefinedLanes;
  258. // Any change at all?
  259. if ((DefinedLanes & ~PrevDefinedLanes).none())
  260. return;
  261. RegInfo.DefinedLanes = PrevDefinedLanes | DefinedLanes;
  262. PutInWorklist(DefRegIdx);
  263. }
  264. LaneBitmask DetectDeadLanes::transferDefinedLanes(const MachineOperand &Def,
  265. unsigned OpNum, LaneBitmask DefinedLanes) const {
  266. const MachineInstr &MI = *Def.getParent();
  267. // Translate DefinedLanes if necessary.
  268. switch (MI.getOpcode()) {
  269. case TargetOpcode::REG_SEQUENCE: {
  270. unsigned SubIdx = MI.getOperand(OpNum + 1).getImm();
  271. DefinedLanes = TRI->composeSubRegIndexLaneMask(SubIdx, DefinedLanes);
  272. DefinedLanes &= TRI->getSubRegIndexLaneMask(SubIdx);
  273. break;
  274. }
  275. case TargetOpcode::INSERT_SUBREG: {
  276. unsigned SubIdx = MI.getOperand(3).getImm();
  277. if (OpNum == 2) {
  278. DefinedLanes = TRI->composeSubRegIndexLaneMask(SubIdx, DefinedLanes);
  279. DefinedLanes &= TRI->getSubRegIndexLaneMask(SubIdx);
  280. } else {
  281. assert(OpNum == 1 && "INSERT_SUBREG must have two operands");
  282. // Ignore lanes defined by operand 2.
  283. DefinedLanes &= ~TRI->getSubRegIndexLaneMask(SubIdx);
  284. }
  285. break;
  286. }
  287. case TargetOpcode::EXTRACT_SUBREG: {
  288. unsigned SubIdx = MI.getOperand(2).getImm();
  289. assert(OpNum == 1 && "EXTRACT_SUBREG must have one register operand only");
  290. DefinedLanes = TRI->reverseComposeSubRegIndexLaneMask(SubIdx, DefinedLanes);
  291. break;
  292. }
  293. case TargetOpcode::COPY:
  294. case TargetOpcode::PHI:
  295. break;
  296. default:
  297. llvm_unreachable("function must be called with COPY-like instruction");
  298. }
  299. assert(Def.getSubReg() == 0 &&
  300. "Should not have subregister defs in machine SSA phase");
  301. DefinedLanes &= MRI->getMaxLaneMaskForVReg(Def.getReg());
  302. return DefinedLanes;
  303. }
  304. LaneBitmask DetectDeadLanes::determineInitialDefinedLanes(unsigned Reg) {
  305. // Live-In or unused registers have no definition but are considered fully
  306. // defined.
  307. if (!MRI->hasOneDef(Reg))
  308. return LaneBitmask::getAll();
  309. const MachineOperand &Def = *MRI->def_begin(Reg);
  310. const MachineInstr &DefMI = *Def.getParent();
  311. if (lowersToCopies(DefMI)) {
  312. // Start optimisatically with no used or defined lanes for copy
  313. // instructions. The following dataflow analysis will add more bits.
  314. unsigned RegIdx = Register::virtReg2Index(Reg);
  315. DefinedByCopy.set(RegIdx);
  316. PutInWorklist(RegIdx);
  317. if (Def.isDead())
  318. return LaneBitmask::getNone();
  319. // COPY/PHI can copy across unrelated register classes (example: float/int)
  320. // with incompatible subregister structure. Do not include these in the
  321. // dataflow analysis since we cannot transfer lanemasks in a meaningful way.
  322. const TargetRegisterClass *DefRC = MRI->getRegClass(Reg);
  323. // Determine initially DefinedLanes.
  324. LaneBitmask DefinedLanes;
  325. for (const MachineOperand &MO : DefMI.uses()) {
  326. if (!MO.isReg() || !MO.readsReg())
  327. continue;
  328. Register MOReg = MO.getReg();
  329. if (!MOReg)
  330. continue;
  331. LaneBitmask MODefinedLanes;
  332. if (Register::isPhysicalRegister(MOReg)) {
  333. MODefinedLanes = LaneBitmask::getAll();
  334. } else if (isCrossCopy(*MRI, DefMI, DefRC, MO)) {
  335. MODefinedLanes = LaneBitmask::getAll();
  336. } else {
  337. assert(Register::isVirtualRegister(MOReg));
  338. if (MRI->hasOneDef(MOReg)) {
  339. const MachineOperand &MODef = *MRI->def_begin(MOReg);
  340. const MachineInstr &MODefMI = *MODef.getParent();
  341. // Bits from copy-like operations will be added later.
  342. if (lowersToCopies(MODefMI) || MODefMI.isImplicitDef())
  343. continue;
  344. }
  345. unsigned MOSubReg = MO.getSubReg();
  346. MODefinedLanes = MRI->getMaxLaneMaskForVReg(MOReg);
  347. MODefinedLanes = TRI->reverseComposeSubRegIndexLaneMask(
  348. MOSubReg, MODefinedLanes);
  349. }
  350. unsigned OpNum = DefMI.getOperandNo(&MO);
  351. DefinedLanes |= transferDefinedLanes(Def, OpNum, MODefinedLanes);
  352. }
  353. return DefinedLanes;
  354. }
  355. if (DefMI.isImplicitDef() || Def.isDead())
  356. return LaneBitmask::getNone();
  357. assert(Def.getSubReg() == 0 &&
  358. "Should not have subregister defs in machine SSA phase");
  359. return MRI->getMaxLaneMaskForVReg(Reg);
  360. }
  361. LaneBitmask DetectDeadLanes::determineInitialUsedLanes(unsigned Reg) {
  362. LaneBitmask UsedLanes = LaneBitmask::getNone();
  363. for (const MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
  364. if (!MO.readsReg())
  365. continue;
  366. const MachineInstr &UseMI = *MO.getParent();
  367. if (UseMI.isKill())
  368. continue;
  369. unsigned SubReg = MO.getSubReg();
  370. if (lowersToCopies(UseMI)) {
  371. assert(UseMI.getDesc().getNumDefs() == 1);
  372. const MachineOperand &Def = *UseMI.defs().begin();
  373. Register DefReg = Def.getReg();
  374. // The used lanes of COPY-like instruction operands are determined by the
  375. // following dataflow analysis.
  376. if (Register::isVirtualRegister(DefReg)) {
  377. // But ignore copies across incompatible register classes.
  378. bool CrossCopy = false;
  379. if (lowersToCopies(UseMI)) {
  380. const TargetRegisterClass *DstRC = MRI->getRegClass(DefReg);
  381. CrossCopy = isCrossCopy(*MRI, UseMI, DstRC, MO);
  382. if (CrossCopy)
  383. LLVM_DEBUG(dbgs() << "Copy across incompatible classes: " << UseMI);
  384. }
  385. if (!CrossCopy)
  386. continue;
  387. }
  388. }
  389. // Shortcut: All lanes are used.
  390. if (SubReg == 0)
  391. return MRI->getMaxLaneMaskForVReg(Reg);
  392. UsedLanes |= TRI->getSubRegIndexLaneMask(SubReg);
  393. }
  394. return UsedLanes;
  395. }
  396. bool DetectDeadLanes::isUndefRegAtInput(const MachineOperand &MO,
  397. const VRegInfo &RegInfo) const {
  398. unsigned SubReg = MO.getSubReg();
  399. LaneBitmask Mask = TRI->getSubRegIndexLaneMask(SubReg);
  400. return (RegInfo.DefinedLanes & RegInfo.UsedLanes & Mask).none();
  401. }
  402. bool DetectDeadLanes::isUndefInput(const MachineOperand &MO,
  403. bool *CrossCopy) const {
  404. if (!MO.isUse())
  405. return false;
  406. const MachineInstr &MI = *MO.getParent();
  407. if (!lowersToCopies(MI))
  408. return false;
  409. const MachineOperand &Def = MI.getOperand(0);
  410. Register DefReg = Def.getReg();
  411. if (!Register::isVirtualRegister(DefReg))
  412. return false;
  413. unsigned DefRegIdx = Register::virtReg2Index(DefReg);
  414. if (!DefinedByCopy.test(DefRegIdx))
  415. return false;
  416. const VRegInfo &DefRegInfo = VRegInfos[DefRegIdx];
  417. LaneBitmask UsedLanes = transferUsedLanes(MI, DefRegInfo.UsedLanes, MO);
  418. if (UsedLanes.any())
  419. return false;
  420. Register MOReg = MO.getReg();
  421. if (Register::isVirtualRegister(MOReg)) {
  422. const TargetRegisterClass *DstRC = MRI->getRegClass(DefReg);
  423. *CrossCopy = isCrossCopy(*MRI, MI, DstRC, MO);
  424. }
  425. return true;
  426. }
  427. bool DetectDeadLanes::runOnce(MachineFunction &MF) {
  428. // First pass: Populate defs/uses of vregs with initial values
  429. unsigned NumVirtRegs = MRI->getNumVirtRegs();
  430. for (unsigned RegIdx = 0; RegIdx < NumVirtRegs; ++RegIdx) {
  431. unsigned Reg = Register::index2VirtReg(RegIdx);
  432. // Determine used/defined lanes and add copy instructions to worklist.
  433. VRegInfo &Info = VRegInfos[RegIdx];
  434. Info.DefinedLanes = determineInitialDefinedLanes(Reg);
  435. Info.UsedLanes = determineInitialUsedLanes(Reg);
  436. }
  437. // Iterate as long as defined lanes/used lanes keep changing.
  438. while (!Worklist.empty()) {
  439. unsigned RegIdx = Worklist.front();
  440. Worklist.pop_front();
  441. WorklistMembers.reset(RegIdx);
  442. VRegInfo &Info = VRegInfos[RegIdx];
  443. unsigned Reg = Register::index2VirtReg(RegIdx);
  444. // Transfer UsedLanes to operands of DefMI (backwards dataflow).
  445. MachineOperand &Def = *MRI->def_begin(Reg);
  446. const MachineInstr &MI = *Def.getParent();
  447. transferUsedLanesStep(MI, Info.UsedLanes);
  448. // Transfer DefinedLanes to users of Reg (forward dataflow).
  449. for (const MachineOperand &MO : MRI->use_nodbg_operands(Reg))
  450. transferDefinedLanesStep(MO, Info.DefinedLanes);
  451. }
  452. LLVM_DEBUG({
  453. dbgs() << "Defined/Used lanes:\n";
  454. for (unsigned RegIdx = 0; RegIdx < NumVirtRegs; ++RegIdx) {
  455. unsigned Reg = Register::index2VirtReg(RegIdx);
  456. const VRegInfo &Info = VRegInfos[RegIdx];
  457. dbgs() << printReg(Reg, nullptr)
  458. << " Used: " << PrintLaneMask(Info.UsedLanes)
  459. << " Def: " << PrintLaneMask(Info.DefinedLanes) << '\n';
  460. }
  461. dbgs() << "\n";
  462. });
  463. bool Again = false;
  464. // Mark operands as dead/unused.
  465. for (MachineBasicBlock &MBB : MF) {
  466. for (MachineInstr &MI : MBB) {
  467. for (MachineOperand &MO : MI.operands()) {
  468. if (!MO.isReg())
  469. continue;
  470. Register Reg = MO.getReg();
  471. if (!Register::isVirtualRegister(Reg))
  472. continue;
  473. unsigned RegIdx = Register::virtReg2Index(Reg);
  474. const VRegInfo &RegInfo = VRegInfos[RegIdx];
  475. if (MO.isDef() && !MO.isDead() && RegInfo.UsedLanes.none()) {
  476. LLVM_DEBUG(dbgs()
  477. << "Marking operand '" << MO << "' as dead in " << MI);
  478. MO.setIsDead();
  479. }
  480. if (MO.readsReg()) {
  481. bool CrossCopy = false;
  482. if (isUndefRegAtInput(MO, RegInfo)) {
  483. LLVM_DEBUG(dbgs()
  484. << "Marking operand '" << MO << "' as undef in " << MI);
  485. MO.setIsUndef();
  486. } else if (isUndefInput(MO, &CrossCopy)) {
  487. LLVM_DEBUG(dbgs()
  488. << "Marking operand '" << MO << "' as undef in " << MI);
  489. MO.setIsUndef();
  490. if (CrossCopy)
  491. Again = true;
  492. }
  493. }
  494. }
  495. }
  496. }
  497. return Again;
  498. }
  499. bool DetectDeadLanes::runOnMachineFunction(MachineFunction &MF) {
  500. // Don't bother if we won't track subregister liveness later. This pass is
  501. // required for correctness if subregister liveness is enabled because the
  502. // register coalescer cannot deal with hidden dead defs. However without
  503. // subregister liveness enabled, the expected benefits of this pass are small
  504. // so we safe the compile time.
  505. MRI = &MF.getRegInfo();
  506. if (!MRI->subRegLivenessEnabled()) {
  507. LLVM_DEBUG(dbgs() << "Skipping Detect dead lanes pass\n");
  508. return false;
  509. }
  510. TRI = MRI->getTargetRegisterInfo();
  511. unsigned NumVirtRegs = MRI->getNumVirtRegs();
  512. VRegInfos = new VRegInfo[NumVirtRegs];
  513. WorklistMembers.resize(NumVirtRegs);
  514. DefinedByCopy.resize(NumVirtRegs);
  515. bool Again;
  516. do {
  517. Again = runOnce(MF);
  518. } while(Again);
  519. DefinedByCopy.clear();
  520. WorklistMembers.clear();
  521. delete[] VRegInfos;
  522. return true;
  523. }