VirtRegMap.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. //===- llvm/CodeGen/VirtRegMap.cpp - Virtual Register Map -----------------===//
  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 VirtRegMap class.
  11. //
  12. // It also contains implementations of the Spiller interface, which, given a
  13. // virtual register map and a machine function, eliminates all virtual
  14. // references by replacing them with physical register references - adding spill
  15. // code as necessary.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #include "llvm/CodeGen/VirtRegMap.h"
  19. #include "LiveDebugVariables.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/ADT/Statistic.h"
  22. #include "llvm/CodeGen/LiveInterval.h"
  23. #include "llvm/CodeGen/LiveIntervals.h"
  24. #include "llvm/CodeGen/LiveStacks.h"
  25. #include "llvm/CodeGen/MachineBasicBlock.h"
  26. #include "llvm/CodeGen/MachineFrameInfo.h"
  27. #include "llvm/CodeGen/MachineFunction.h"
  28. #include "llvm/CodeGen/MachineFunctionPass.h"
  29. #include "llvm/CodeGen/MachineInstr.h"
  30. #include "llvm/CodeGen/MachineOperand.h"
  31. #include "llvm/CodeGen/MachineRegisterInfo.h"
  32. #include "llvm/CodeGen/SlotIndexes.h"
  33. #include "llvm/CodeGen/TargetInstrInfo.h"
  34. #include "llvm/CodeGen/TargetOpcodes.h"
  35. #include "llvm/CodeGen/TargetRegisterInfo.h"
  36. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  37. #include "llvm/Config/llvm-config.h"
  38. #include "llvm/MC/LaneBitmask.h"
  39. #include "llvm/Pass.h"
  40. #include "llvm/Support/Compiler.h"
  41. #include "llvm/Support/Debug.h"
  42. #include "llvm/Support/raw_ostream.h"
  43. #include <cassert>
  44. #include <iterator>
  45. #include <utility>
  46. using namespace llvm;
  47. #define DEBUG_TYPE "regalloc"
  48. STATISTIC(NumSpillSlots, "Number of spill slots allocated");
  49. STATISTIC(NumIdCopies, "Number of identity moves eliminated after rewriting");
  50. //===----------------------------------------------------------------------===//
  51. // VirtRegMap implementation
  52. //===----------------------------------------------------------------------===//
  53. char VirtRegMap::ID = 0;
  54. INITIALIZE_PASS(VirtRegMap, "virtregmap", "Virtual Register Map", false, false)
  55. bool VirtRegMap::runOnMachineFunction(MachineFunction &mf) {
  56. MRI = &mf.getRegInfo();
  57. TII = mf.getSubtarget().getInstrInfo();
  58. TRI = mf.getSubtarget().getRegisterInfo();
  59. MF = &mf;
  60. Virt2PhysMap.clear();
  61. Virt2StackSlotMap.clear();
  62. Virt2SplitMap.clear();
  63. grow();
  64. return false;
  65. }
  66. void VirtRegMap::grow() {
  67. unsigned NumRegs = MF->getRegInfo().getNumVirtRegs();
  68. Virt2PhysMap.resize(NumRegs);
  69. Virt2StackSlotMap.resize(NumRegs);
  70. Virt2SplitMap.resize(NumRegs);
  71. }
  72. void VirtRegMap::assignVirt2Phys(unsigned virtReg, MCPhysReg physReg) {
  73. assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
  74. TargetRegisterInfo::isPhysicalRegister(physReg));
  75. assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
  76. "attempt to assign physical register to already mapped "
  77. "virtual register");
  78. assert(!getRegInfo().isReserved(physReg) &&
  79. "Attempt to map virtReg to a reserved physReg");
  80. Virt2PhysMap[virtReg] = physReg;
  81. }
  82. unsigned VirtRegMap::createSpillSlot(const TargetRegisterClass *RC) {
  83. unsigned Size = TRI->getSpillSize(*RC);
  84. unsigned Align = TRI->getSpillAlignment(*RC);
  85. int SS = MF->getFrameInfo().CreateSpillStackObject(Size, Align);
  86. ++NumSpillSlots;
  87. return SS;
  88. }
  89. bool VirtRegMap::hasPreferredPhys(unsigned VirtReg) {
  90. unsigned Hint = MRI->getSimpleHint(VirtReg);
  91. if (!Hint)
  92. return false;
  93. if (TargetRegisterInfo::isVirtualRegister(Hint))
  94. Hint = getPhys(Hint);
  95. return getPhys(VirtReg) == Hint;
  96. }
  97. bool VirtRegMap::hasKnownPreference(unsigned VirtReg) {
  98. std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(VirtReg);
  99. if (TargetRegisterInfo::isPhysicalRegister(Hint.second))
  100. return true;
  101. if (TargetRegisterInfo::isVirtualRegister(Hint.second))
  102. return hasPhys(Hint.second);
  103. return false;
  104. }
  105. int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
  106. assert(TargetRegisterInfo::isVirtualRegister(virtReg));
  107. assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
  108. "attempt to assign stack slot to already spilled register");
  109. const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtReg);
  110. return Virt2StackSlotMap[virtReg] = createSpillSlot(RC);
  111. }
  112. void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int SS) {
  113. assert(TargetRegisterInfo::isVirtualRegister(virtReg));
  114. assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
  115. "attempt to assign stack slot to already spilled register");
  116. assert((SS >= 0 ||
  117. (SS >= MF->getFrameInfo().getObjectIndexBegin())) &&
  118. "illegal fixed frame index");
  119. Virt2StackSlotMap[virtReg] = SS;
  120. }
  121. void VirtRegMap::print(raw_ostream &OS, const Module*) const {
  122. OS << "********** REGISTER MAP **********\n";
  123. for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
  124. unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
  125. if (Virt2PhysMap[Reg] != (unsigned)VirtRegMap::NO_PHYS_REG) {
  126. OS << '[' << printReg(Reg, TRI) << " -> "
  127. << printReg(Virt2PhysMap[Reg], TRI) << "] "
  128. << TRI->getRegClassName(MRI->getRegClass(Reg)) << "\n";
  129. }
  130. }
  131. for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
  132. unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
  133. if (Virt2StackSlotMap[Reg] != VirtRegMap::NO_STACK_SLOT) {
  134. OS << '[' << printReg(Reg, TRI) << " -> fi#" << Virt2StackSlotMap[Reg]
  135. << "] " << TRI->getRegClassName(MRI->getRegClass(Reg)) << "\n";
  136. }
  137. }
  138. OS << '\n';
  139. }
  140. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  141. LLVM_DUMP_METHOD void VirtRegMap::dump() const {
  142. print(dbgs());
  143. }
  144. #endif
  145. //===----------------------------------------------------------------------===//
  146. // VirtRegRewriter
  147. //===----------------------------------------------------------------------===//
  148. //
  149. // The VirtRegRewriter is the last of the register allocator passes.
  150. // It rewrites virtual registers to physical registers as specified in the
  151. // VirtRegMap analysis. It also updates live-in information on basic blocks
  152. // according to LiveIntervals.
  153. //
  154. namespace {
  155. class VirtRegRewriter : public MachineFunctionPass {
  156. MachineFunction *MF;
  157. const TargetRegisterInfo *TRI;
  158. const TargetInstrInfo *TII;
  159. MachineRegisterInfo *MRI;
  160. SlotIndexes *Indexes;
  161. LiveIntervals *LIS;
  162. VirtRegMap *VRM;
  163. void rewrite();
  164. void addMBBLiveIns();
  165. bool readsUndefSubreg(const MachineOperand &MO) const;
  166. void addLiveInsForSubRanges(const LiveInterval &LI, unsigned PhysReg) const;
  167. void handleIdentityCopy(MachineInstr &MI) const;
  168. void expandCopyBundle(MachineInstr &MI) const;
  169. bool subRegLiveThrough(const MachineInstr &MI, unsigned SuperPhysReg) const;
  170. public:
  171. static char ID;
  172. VirtRegRewriter() : MachineFunctionPass(ID) {}
  173. void getAnalysisUsage(AnalysisUsage &AU) const override;
  174. bool runOnMachineFunction(MachineFunction&) override;
  175. MachineFunctionProperties getSetProperties() const override {
  176. return MachineFunctionProperties().set(
  177. MachineFunctionProperties::Property::NoVRegs);
  178. }
  179. };
  180. } // end anonymous namespace
  181. char VirtRegRewriter::ID = 0;
  182. char &llvm::VirtRegRewriterID = VirtRegRewriter::ID;
  183. INITIALIZE_PASS_BEGIN(VirtRegRewriter, "virtregrewriter",
  184. "Virtual Register Rewriter", false, false)
  185. INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
  186. INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
  187. INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
  188. INITIALIZE_PASS_DEPENDENCY(LiveStacks)
  189. INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
  190. INITIALIZE_PASS_END(VirtRegRewriter, "virtregrewriter",
  191. "Virtual Register Rewriter", false, false)
  192. void VirtRegRewriter::getAnalysisUsage(AnalysisUsage &AU) const {
  193. AU.setPreservesCFG();
  194. AU.addRequired<LiveIntervals>();
  195. AU.addRequired<SlotIndexes>();
  196. AU.addPreserved<SlotIndexes>();
  197. AU.addRequired<LiveDebugVariables>();
  198. AU.addRequired<LiveStacks>();
  199. AU.addPreserved<LiveStacks>();
  200. AU.addRequired<VirtRegMap>();
  201. MachineFunctionPass::getAnalysisUsage(AU);
  202. }
  203. bool VirtRegRewriter::runOnMachineFunction(MachineFunction &fn) {
  204. MF = &fn;
  205. TRI = MF->getSubtarget().getRegisterInfo();
  206. TII = MF->getSubtarget().getInstrInfo();
  207. MRI = &MF->getRegInfo();
  208. Indexes = &getAnalysis<SlotIndexes>();
  209. LIS = &getAnalysis<LiveIntervals>();
  210. VRM = &getAnalysis<VirtRegMap>();
  211. DEBUG(dbgs() << "********** REWRITE VIRTUAL REGISTERS **********\n"
  212. << "********** Function: "
  213. << MF->getName() << '\n');
  214. DEBUG(VRM->dump());
  215. // Add kill flags while we still have virtual registers.
  216. LIS->addKillFlags(VRM);
  217. // Live-in lists on basic blocks are required for physregs.
  218. addMBBLiveIns();
  219. // Rewrite virtual registers.
  220. rewrite();
  221. // Write out new DBG_VALUE instructions.
  222. getAnalysis<LiveDebugVariables>().emitDebugValues(VRM);
  223. // All machine operands and other references to virtual registers have been
  224. // replaced. Remove the virtual registers and release all the transient data.
  225. VRM->clearAllVirt();
  226. MRI->clearVirtRegs();
  227. return true;
  228. }
  229. void VirtRegRewriter::addLiveInsForSubRanges(const LiveInterval &LI,
  230. unsigned PhysReg) const {
  231. assert(!LI.empty());
  232. assert(LI.hasSubRanges());
  233. using SubRangeIteratorPair =
  234. std::pair<const LiveInterval::SubRange *, LiveInterval::const_iterator>;
  235. SmallVector<SubRangeIteratorPair, 4> SubRanges;
  236. SlotIndex First;
  237. SlotIndex Last;
  238. for (const LiveInterval::SubRange &SR : LI.subranges()) {
  239. SubRanges.push_back(std::make_pair(&SR, SR.begin()));
  240. if (!First.isValid() || SR.segments.front().start < First)
  241. First = SR.segments.front().start;
  242. if (!Last.isValid() || SR.segments.back().end > Last)
  243. Last = SR.segments.back().end;
  244. }
  245. // Check all mbb start positions between First and Last while
  246. // simulatenously advancing an iterator for each subrange.
  247. for (SlotIndexes::MBBIndexIterator MBBI = Indexes->findMBBIndex(First);
  248. MBBI != Indexes->MBBIndexEnd() && MBBI->first <= Last; ++MBBI) {
  249. SlotIndex MBBBegin = MBBI->first;
  250. // Advance all subrange iterators so that their end position is just
  251. // behind MBBBegin (or the iterator is at the end).
  252. LaneBitmask LaneMask;
  253. for (auto &RangeIterPair : SubRanges) {
  254. const LiveInterval::SubRange *SR = RangeIterPair.first;
  255. LiveInterval::const_iterator &SRI = RangeIterPair.second;
  256. while (SRI != SR->end() && SRI->end <= MBBBegin)
  257. ++SRI;
  258. if (SRI == SR->end())
  259. continue;
  260. if (SRI->start <= MBBBegin)
  261. LaneMask |= SR->LaneMask;
  262. }
  263. if (LaneMask.none())
  264. continue;
  265. MachineBasicBlock *MBB = MBBI->second;
  266. MBB->addLiveIn(PhysReg, LaneMask);
  267. }
  268. }
  269. // Compute MBB live-in lists from virtual register live ranges and their
  270. // assignments.
  271. void VirtRegRewriter::addMBBLiveIns() {
  272. for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
  273. unsigned VirtReg = TargetRegisterInfo::index2VirtReg(Idx);
  274. if (MRI->reg_nodbg_empty(VirtReg))
  275. continue;
  276. LiveInterval &LI = LIS->getInterval(VirtReg);
  277. if (LI.empty() || LIS->intervalIsInOneMBB(LI))
  278. continue;
  279. // This is a virtual register that is live across basic blocks. Its
  280. // assigned PhysReg must be marked as live-in to those blocks.
  281. unsigned PhysReg = VRM->getPhys(VirtReg);
  282. assert(PhysReg != VirtRegMap::NO_PHYS_REG && "Unmapped virtual register.");
  283. if (LI.hasSubRanges()) {
  284. addLiveInsForSubRanges(LI, PhysReg);
  285. } else {
  286. // Go over MBB begin positions and see if we have segments covering them.
  287. // The following works because segments and the MBBIndex list are both
  288. // sorted by slot indexes.
  289. SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin();
  290. for (const auto &Seg : LI) {
  291. I = Indexes->advanceMBBIndex(I, Seg.start);
  292. for (; I != Indexes->MBBIndexEnd() && I->first < Seg.end; ++I) {
  293. MachineBasicBlock *MBB = I->second;
  294. MBB->addLiveIn(PhysReg);
  295. }
  296. }
  297. }
  298. }
  299. // Sort and unique MBB LiveIns as we've not checked if SubReg/PhysReg were in
  300. // each MBB's LiveIns set before calling addLiveIn on them.
  301. for (MachineBasicBlock &MBB : *MF)
  302. MBB.sortUniqueLiveIns();
  303. }
  304. /// Returns true if the given machine operand \p MO only reads undefined lanes.
  305. /// The function only works for use operands with a subregister set.
  306. bool VirtRegRewriter::readsUndefSubreg(const MachineOperand &MO) const {
  307. // Shortcut if the operand is already marked undef.
  308. if (MO.isUndef())
  309. return true;
  310. unsigned Reg = MO.getReg();
  311. const LiveInterval &LI = LIS->getInterval(Reg);
  312. const MachineInstr &MI = *MO.getParent();
  313. SlotIndex BaseIndex = LIS->getInstructionIndex(MI);
  314. // This code is only meant to handle reading undefined subregisters which
  315. // we couldn't properly detect before.
  316. assert(LI.liveAt(BaseIndex) &&
  317. "Reads of completely dead register should be marked undef already");
  318. unsigned SubRegIdx = MO.getSubReg();
  319. assert(SubRegIdx != 0 && LI.hasSubRanges());
  320. LaneBitmask UseMask = TRI->getSubRegIndexLaneMask(SubRegIdx);
  321. // See if any of the relevant subregister liveranges is defined at this point.
  322. for (const LiveInterval::SubRange &SR : LI.subranges()) {
  323. if ((SR.LaneMask & UseMask).any() && SR.liveAt(BaseIndex))
  324. return false;
  325. }
  326. return true;
  327. }
  328. void VirtRegRewriter::handleIdentityCopy(MachineInstr &MI) const {
  329. if (!MI.isIdentityCopy())
  330. return;
  331. DEBUG(dbgs() << "Identity copy: " << MI);
  332. ++NumIdCopies;
  333. // Copies like:
  334. // %r0 = COPY undef %r0
  335. // %al = COPY %al, implicit-def %eax
  336. // give us additional liveness information: The target (super-)register
  337. // must not be valid before this point. Replace the COPY with a KILL
  338. // instruction to maintain this information.
  339. if (MI.getOperand(0).isUndef() || MI.getNumOperands() > 2) {
  340. MI.setDesc(TII->get(TargetOpcode::KILL));
  341. DEBUG(dbgs() << " replace by: " << MI);
  342. return;
  343. }
  344. if (Indexes)
  345. Indexes->removeSingleMachineInstrFromMaps(MI);
  346. MI.eraseFromBundle();
  347. DEBUG(dbgs() << " deleted.\n");
  348. }
  349. /// The liverange splitting logic sometimes produces bundles of copies when
  350. /// subregisters are involved. Expand these into a sequence of copy instructions
  351. /// after processing the last in the bundle. Does not update LiveIntervals
  352. /// which we shouldn't need for this instruction anymore.
  353. void VirtRegRewriter::expandCopyBundle(MachineInstr &MI) const {
  354. if (!MI.isCopy())
  355. return;
  356. if (MI.isBundledWithPred() && !MI.isBundledWithSucc()) {
  357. // Only do this when the complete bundle is made out of COPYs.
  358. MachineBasicBlock &MBB = *MI.getParent();
  359. for (MachineBasicBlock::reverse_instr_iterator I =
  360. std::next(MI.getReverseIterator()), E = MBB.instr_rend();
  361. I != E && I->isBundledWithSucc(); ++I) {
  362. if (!I->isCopy())
  363. return;
  364. }
  365. for (MachineBasicBlock::reverse_instr_iterator I = MI.getReverseIterator();
  366. I->isBundledWithPred(); ) {
  367. MachineInstr &MI = *I;
  368. ++I;
  369. MI.unbundleFromPred();
  370. if (Indexes)
  371. Indexes->insertMachineInstrInMaps(MI);
  372. }
  373. }
  374. }
  375. /// Check whether (part of) \p SuperPhysReg is live through \p MI.
  376. /// \pre \p MI defines a subregister of a virtual register that
  377. /// has been assigned to \p SuperPhysReg.
  378. bool VirtRegRewriter::subRegLiveThrough(const MachineInstr &MI,
  379. unsigned SuperPhysReg) const {
  380. SlotIndex MIIndex = LIS->getInstructionIndex(MI);
  381. SlotIndex BeforeMIUses = MIIndex.getBaseIndex();
  382. SlotIndex AfterMIDefs = MIIndex.getBoundaryIndex();
  383. for (MCRegUnitIterator Unit(SuperPhysReg, TRI); Unit.isValid(); ++Unit) {
  384. const LiveRange &UnitRange = LIS->getRegUnit(*Unit);
  385. // If the regunit is live both before and after MI,
  386. // we assume it is live through.
  387. // Generally speaking, this is not true, because something like
  388. // "RU = op RU" would match that description.
  389. // However, we know that we are trying to assess whether
  390. // a def of a virtual reg, vreg, is live at the same time of RU.
  391. // If we are in the "RU = op RU" situation, that means that vreg
  392. // is defined at the same time as RU (i.e., "vreg, RU = op RU").
  393. // Thus, vreg and RU interferes and vreg cannot be assigned to
  394. // SuperPhysReg. Therefore, this situation cannot happen.
  395. if (UnitRange.liveAt(AfterMIDefs) && UnitRange.liveAt(BeforeMIUses))
  396. return true;
  397. }
  398. return false;
  399. }
  400. void VirtRegRewriter::rewrite() {
  401. bool NoSubRegLiveness = !MRI->subRegLivenessEnabled();
  402. SmallVector<unsigned, 8> SuperDeads;
  403. SmallVector<unsigned, 8> SuperDefs;
  404. SmallVector<unsigned, 8> SuperKills;
  405. for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
  406. MBBI != MBBE; ++MBBI) {
  407. DEBUG(MBBI->print(dbgs(), Indexes));
  408. for (MachineBasicBlock::instr_iterator
  409. MII = MBBI->instr_begin(), MIE = MBBI->instr_end(); MII != MIE;) {
  410. MachineInstr *MI = &*MII;
  411. ++MII;
  412. for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
  413. MOE = MI->operands_end(); MOI != MOE; ++MOI) {
  414. MachineOperand &MO = *MOI;
  415. // Make sure MRI knows about registers clobbered by regmasks.
  416. if (MO.isRegMask())
  417. MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
  418. if (!MO.isReg() || !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
  419. continue;
  420. unsigned VirtReg = MO.getReg();
  421. unsigned PhysReg = VRM->getPhys(VirtReg);
  422. assert(PhysReg != VirtRegMap::NO_PHYS_REG &&
  423. "Instruction uses unmapped VirtReg");
  424. assert(!MRI->isReserved(PhysReg) && "Reserved register assignment");
  425. // Preserve semantics of sub-register operands.
  426. unsigned SubReg = MO.getSubReg();
  427. if (SubReg != 0) {
  428. if (NoSubRegLiveness) {
  429. // A virtual register kill refers to the whole register, so we may
  430. // have to add implicit killed operands for the super-register. A
  431. // partial redef always kills and redefines the super-register.
  432. if ((MO.readsReg() && (MO.isDef() || MO.isKill())) ||
  433. (MO.isDef() && subRegLiveThrough(*MI, PhysReg)))
  434. SuperKills.push_back(PhysReg);
  435. if (MO.isDef()) {
  436. // Also add implicit defs for the super-register.
  437. if (MO.isDead())
  438. SuperDeads.push_back(PhysReg);
  439. else
  440. SuperDefs.push_back(PhysReg);
  441. }
  442. } else {
  443. if (MO.isUse()) {
  444. if (readsUndefSubreg(MO))
  445. // We need to add an <undef> flag if the subregister is
  446. // completely undefined (and we are not adding super-register
  447. // defs).
  448. MO.setIsUndef(true);
  449. } else if (!MO.isDead()) {
  450. assert(MO.isDef());
  451. }
  452. }
  453. // The def undef and def internal flags only make sense for
  454. // sub-register defs, and we are substituting a full physreg. An
  455. // implicit killed operand from the SuperKills list will represent the
  456. // partial read of the super-register.
  457. if (MO.isDef()) {
  458. MO.setIsUndef(false);
  459. MO.setIsInternalRead(false);
  460. }
  461. // PhysReg operands cannot have subregister indexes.
  462. PhysReg = TRI->getSubReg(PhysReg, SubReg);
  463. assert(PhysReg && "Invalid SubReg for physical register");
  464. MO.setSubReg(0);
  465. }
  466. // Rewrite. Note we could have used MachineOperand::substPhysReg(), but
  467. // we need the inlining here.
  468. MO.setReg(PhysReg);
  469. MO.setIsRenamable(true);
  470. }
  471. // Add any missing super-register kills after rewriting the whole
  472. // instruction.
  473. while (!SuperKills.empty())
  474. MI->addRegisterKilled(SuperKills.pop_back_val(), TRI, true);
  475. while (!SuperDeads.empty())
  476. MI->addRegisterDead(SuperDeads.pop_back_val(), TRI, true);
  477. while (!SuperDefs.empty())
  478. MI->addRegisterDefined(SuperDefs.pop_back_val(), TRI);
  479. DEBUG(dbgs() << "> " << *MI);
  480. expandCopyBundle(*MI);
  481. // We can remove identity copies right now.
  482. handleIdentityCopy(*MI);
  483. }
  484. }
  485. }