16122f3e6SDimitry Andric //===-- ExpandPostRAPseudos.cpp - Pseudo instruction expansion pass -------===//
26122f3e6SDimitry Andric //
36122f3e6SDimitry Andric // The LLVM Compiler Infrastructure
46122f3e6SDimitry Andric //
56122f3e6SDimitry Andric // This file is distributed under the University of Illinois Open Source
66122f3e6SDimitry Andric // License. See LICENSE.TXT for details.
76122f3e6SDimitry Andric //
86122f3e6SDimitry Andric //===----------------------------------------------------------------------===//
96122f3e6SDimitry Andric //
106122f3e6SDimitry Andric // This file defines a pass that expands COPY and SUBREG_TO_REG pseudo
116122f3e6SDimitry Andric // instructions after register allocation.
126122f3e6SDimitry Andric //
136122f3e6SDimitry Andric //===----------------------------------------------------------------------===//
146122f3e6SDimitry Andric
156122f3e6SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
166122f3e6SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
176122f3e6SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
186122f3e6SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
19db17bf38SDimitry Andric #include "llvm/CodeGen/Passes.h"
202cab237bSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
212cab237bSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
222cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
236122f3e6SDimitry Andric #include "llvm/Support/Debug.h"
246122f3e6SDimitry Andric #include "llvm/Support/raw_ostream.h"
2539d628a0SDimitry Andric
266122f3e6SDimitry Andric using namespace llvm;
276122f3e6SDimitry Andric
2891bc56edSDimitry Andric #define DEBUG_TYPE "postrapseudos"
2991bc56edSDimitry Andric
306122f3e6SDimitry Andric namespace {
316122f3e6SDimitry Andric struct ExpandPostRA : public MachineFunctionPass {
326122f3e6SDimitry Andric private:
336122f3e6SDimitry Andric const TargetRegisterInfo *TRI;
346122f3e6SDimitry Andric const TargetInstrInfo *TII;
356122f3e6SDimitry Andric
366122f3e6SDimitry Andric public:
376122f3e6SDimitry Andric static char ID; // Pass identification, replacement for typeid
ExpandPostRA__anon17e6ebd50111::ExpandPostRA386122f3e6SDimitry Andric ExpandPostRA() : MachineFunctionPass(ID) {}
396122f3e6SDimitry Andric
getAnalysisUsage__anon17e6ebd50111::ExpandPostRA4091bc56edSDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
416122f3e6SDimitry Andric AU.setPreservesCFG();
426122f3e6SDimitry Andric AU.addPreservedID(MachineLoopInfoID);
436122f3e6SDimitry Andric AU.addPreservedID(MachineDominatorsID);
446122f3e6SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
456122f3e6SDimitry Andric }
466122f3e6SDimitry Andric
476122f3e6SDimitry Andric /// runOnMachineFunction - pass entry point
4891bc56edSDimitry Andric bool runOnMachineFunction(MachineFunction&) override;
496122f3e6SDimitry Andric
506122f3e6SDimitry Andric private:
516122f3e6SDimitry Andric bool LowerSubregToReg(MachineInstr *MI);
526122f3e6SDimitry Andric bool LowerCopy(MachineInstr *MI);
536122f3e6SDimitry Andric
543ca95b02SDimitry Andric void TransferImplicitOperands(MachineInstr *MI);
556122f3e6SDimitry Andric };
566122f3e6SDimitry Andric } // end anonymous namespace
576122f3e6SDimitry Andric
586122f3e6SDimitry Andric char ExpandPostRA::ID = 0;
59dff0c46cSDimitry Andric char &llvm::ExpandPostRAPseudosID = ExpandPostRA::ID;
606122f3e6SDimitry Andric
61302affcbSDimitry Andric INITIALIZE_PASS(ExpandPostRA, DEBUG_TYPE,
62dff0c46cSDimitry Andric "Post-RA pseudo instruction expansion pass", false, false)
636122f3e6SDimitry Andric
643ca95b02SDimitry Andric /// TransferImplicitOperands - MI is a pseudo-instruction, and the lowered
653ca95b02SDimitry Andric /// replacement instructions immediately precede it. Copy any implicit
666122f3e6SDimitry Andric /// operands from MI to the replacement instruction.
TransferImplicitOperands(MachineInstr * MI)673ca95b02SDimitry Andric void ExpandPostRA::TransferImplicitOperands(MachineInstr *MI) {
686122f3e6SDimitry Andric MachineBasicBlock::iterator CopyMI = MI;
696122f3e6SDimitry Andric --CopyMI;
706122f3e6SDimitry Andric
713ca95b02SDimitry Andric for (const MachineOperand &MO : MI->implicit_operands())
723ca95b02SDimitry Andric if (MO.isReg())
733ca95b02SDimitry Andric CopyMI->addOperand(MO);
746122f3e6SDimitry Andric }
756122f3e6SDimitry Andric
LowerSubregToReg(MachineInstr * MI)766122f3e6SDimitry Andric bool ExpandPostRA::LowerSubregToReg(MachineInstr *MI) {
776122f3e6SDimitry Andric MachineBasicBlock *MBB = MI->getParent();
786122f3e6SDimitry Andric assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
796122f3e6SDimitry Andric MI->getOperand(1).isImm() &&
806122f3e6SDimitry Andric (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
816122f3e6SDimitry Andric MI->getOperand(3).isImm() && "Invalid subreg_to_reg");
826122f3e6SDimitry Andric
836122f3e6SDimitry Andric unsigned DstReg = MI->getOperand(0).getReg();
846122f3e6SDimitry Andric unsigned InsReg = MI->getOperand(2).getReg();
856122f3e6SDimitry Andric assert(!MI->getOperand(2).getSubReg() && "SubIdx on physreg?");
866122f3e6SDimitry Andric unsigned SubIdx = MI->getOperand(3).getImm();
876122f3e6SDimitry Andric
886122f3e6SDimitry Andric assert(SubIdx != 0 && "Invalid index for insert_subreg");
896122f3e6SDimitry Andric unsigned DstSubReg = TRI->getSubReg(DstReg, SubIdx);
906122f3e6SDimitry Andric
916122f3e6SDimitry Andric assert(TargetRegisterInfo::isPhysicalRegister(DstReg) &&
926122f3e6SDimitry Andric "Insert destination must be in a physical register");
936122f3e6SDimitry Andric assert(TargetRegisterInfo::isPhysicalRegister(InsReg) &&
946122f3e6SDimitry Andric "Inserted value must be in a physical register");
956122f3e6SDimitry Andric
964ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
976122f3e6SDimitry Andric
98139f7f9bSDimitry Andric if (MI->allDefsAreDead()) {
99139f7f9bSDimitry Andric MI->setDesc(TII->get(TargetOpcode::KILL));
100*b5893f02SDimitry Andric MI->RemoveOperand(3); // SubIdx
101*b5893f02SDimitry Andric MI->RemoveOperand(1); // Imm
1024ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "subreg: replaced by: " << *MI);
103139f7f9bSDimitry Andric return true;
104139f7f9bSDimitry Andric }
105139f7f9bSDimitry Andric
1066122f3e6SDimitry Andric if (DstSubReg == InsReg) {
107f785676fSDimitry Andric // No need to insert an identity copy instruction.
1086122f3e6SDimitry Andric // Watch out for case like this:
1092cab237bSDimitry Andric // %rax = SUBREG_TO_REG 0, killed %eax, 3
1102cab237bSDimitry Andric // We must leave %rax live.
1116122f3e6SDimitry Andric if (DstReg != InsReg) {
1126122f3e6SDimitry Andric MI->setDesc(TII->get(TargetOpcode::KILL));
1136122f3e6SDimitry Andric MI->RemoveOperand(3); // SubIdx
1146122f3e6SDimitry Andric MI->RemoveOperand(1); // Imm
1154ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "subreg: replace by: " << *MI);
1166122f3e6SDimitry Andric return true;
1176122f3e6SDimitry Andric }
1184ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "subreg: eliminated!");
1196122f3e6SDimitry Andric } else {
1206122f3e6SDimitry Andric TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg,
1216122f3e6SDimitry Andric MI->getOperand(2).isKill());
1227ae0e2c9SDimitry Andric
1237ae0e2c9SDimitry Andric // Implicitly define DstReg for subsequent uses.
1247ae0e2c9SDimitry Andric MachineBasicBlock::iterator CopyMI = MI;
1257ae0e2c9SDimitry Andric --CopyMI;
1267ae0e2c9SDimitry Andric CopyMI->addRegisterDefined(DstReg);
1274ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "subreg: " << *CopyMI);
1286122f3e6SDimitry Andric }
1296122f3e6SDimitry Andric
1304ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << '\n');
1316122f3e6SDimitry Andric MBB->erase(MI);
1326122f3e6SDimitry Andric return true;
1336122f3e6SDimitry Andric }
1346122f3e6SDimitry Andric
LowerCopy(MachineInstr * MI)1356122f3e6SDimitry Andric bool ExpandPostRA::LowerCopy(MachineInstr *MI) {
136139f7f9bSDimitry Andric
137139f7f9bSDimitry Andric if (MI->allDefsAreDead()) {
1384ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "dead copy: " << *MI);
139139f7f9bSDimitry Andric MI->setDesc(TII->get(TargetOpcode::KILL));
1404ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "replaced by: " << *MI);
141139f7f9bSDimitry Andric return true;
142139f7f9bSDimitry Andric }
143139f7f9bSDimitry Andric
1446122f3e6SDimitry Andric MachineOperand &DstMO = MI->getOperand(0);
1456122f3e6SDimitry Andric MachineOperand &SrcMO = MI->getOperand(1);
1466122f3e6SDimitry Andric
1475517e702SDimitry Andric bool IdentityCopy = (SrcMO.getReg() == DstMO.getReg());
1485517e702SDimitry Andric if (IdentityCopy || SrcMO.isUndef()) {
1494ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << (IdentityCopy ? "identity copy: " : "undef copy: ")
1504ba319b5SDimitry Andric << *MI);
1516122f3e6SDimitry Andric // No need to insert an identity copy instruction, but replace with a KILL
1526122f3e6SDimitry Andric // if liveness is changed.
153139f7f9bSDimitry Andric if (SrcMO.isUndef() || MI->getNumOperands() > 2) {
1546122f3e6SDimitry Andric // We must make sure the super-register gets killed. Replace the
1556122f3e6SDimitry Andric // instruction with KILL.
1566122f3e6SDimitry Andric MI->setDesc(TII->get(TargetOpcode::KILL));
1574ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "replaced by: " << *MI);
1586122f3e6SDimitry Andric return true;
1596122f3e6SDimitry Andric }
1606122f3e6SDimitry Andric // Vanilla identity copy.
1616122f3e6SDimitry Andric MI->eraseFromParent();
1626122f3e6SDimitry Andric return true;
1636122f3e6SDimitry Andric }
1646122f3e6SDimitry Andric
1654ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "real copy: " << *MI);
1666122f3e6SDimitry Andric TII->copyPhysReg(*MI->getParent(), MI, MI->getDebugLoc(),
1676122f3e6SDimitry Andric DstMO.getReg(), SrcMO.getReg(), SrcMO.isKill());
1686122f3e6SDimitry Andric
1696122f3e6SDimitry Andric if (MI->getNumOperands() > 2)
1703ca95b02SDimitry Andric TransferImplicitOperands(MI);
1714ba319b5SDimitry Andric LLVM_DEBUG({
1726122f3e6SDimitry Andric MachineBasicBlock::iterator dMI = MI;
1736122f3e6SDimitry Andric dbgs() << "replaced by: " << *(--dMI);
1746122f3e6SDimitry Andric });
1756122f3e6SDimitry Andric MI->eraseFromParent();
1766122f3e6SDimitry Andric return true;
1776122f3e6SDimitry Andric }
1786122f3e6SDimitry Andric
1796122f3e6SDimitry Andric /// runOnMachineFunction - Reduce subregister inserts and extracts to register
1806122f3e6SDimitry Andric /// copies.
1816122f3e6SDimitry Andric ///
runOnMachineFunction(MachineFunction & MF)1826122f3e6SDimitry Andric bool ExpandPostRA::runOnMachineFunction(MachineFunction &MF) {
1834ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Machine Function\n"
1846122f3e6SDimitry Andric << "********** EXPANDING POST-RA PSEUDO INSTRS **********\n"
1853861d79fSDimitry Andric << "********** Function: " << MF.getName() << '\n');
18639d628a0SDimitry Andric TRI = MF.getSubtarget().getRegisterInfo();
18739d628a0SDimitry Andric TII = MF.getSubtarget().getInstrInfo();
1886122f3e6SDimitry Andric
1896122f3e6SDimitry Andric bool MadeChange = false;
1906122f3e6SDimitry Andric
1916122f3e6SDimitry Andric for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
1926122f3e6SDimitry Andric mbbi != mbbe; ++mbbi) {
1936122f3e6SDimitry Andric for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
1946122f3e6SDimitry Andric mi != me;) {
1953ca95b02SDimitry Andric MachineInstr &MI = *mi;
1966122f3e6SDimitry Andric // Advance iterator here because MI may be erased.
1976122f3e6SDimitry Andric ++mi;
1986122f3e6SDimitry Andric
1996122f3e6SDimitry Andric // Only expand pseudos.
2003ca95b02SDimitry Andric if (!MI.isPseudo())
2016122f3e6SDimitry Andric continue;
2026122f3e6SDimitry Andric
2036122f3e6SDimitry Andric // Give targets a chance to expand even standard pseudos.
2046122f3e6SDimitry Andric if (TII->expandPostRAPseudo(MI)) {
2056122f3e6SDimitry Andric MadeChange = true;
2066122f3e6SDimitry Andric continue;
2076122f3e6SDimitry Andric }
2086122f3e6SDimitry Andric
2096122f3e6SDimitry Andric // Expand standard pseudos.
2103ca95b02SDimitry Andric switch (MI.getOpcode()) {
2116122f3e6SDimitry Andric case TargetOpcode::SUBREG_TO_REG:
2123ca95b02SDimitry Andric MadeChange |= LowerSubregToReg(&MI);
2136122f3e6SDimitry Andric break;
2146122f3e6SDimitry Andric case TargetOpcode::COPY:
2153ca95b02SDimitry Andric MadeChange |= LowerCopy(&MI);
2166122f3e6SDimitry Andric break;
2176122f3e6SDimitry Andric case TargetOpcode::DBG_VALUE:
2186122f3e6SDimitry Andric continue;
2196122f3e6SDimitry Andric case TargetOpcode::INSERT_SUBREG:
2206122f3e6SDimitry Andric case TargetOpcode::EXTRACT_SUBREG:
2216122f3e6SDimitry Andric llvm_unreachable("Sub-register pseudos should have been eliminated.");
2226122f3e6SDimitry Andric }
2236122f3e6SDimitry Andric }
2246122f3e6SDimitry Andric }
2256122f3e6SDimitry Andric
2266122f3e6SDimitry Andric return MadeChange;
2276122f3e6SDimitry Andric }
228