1 //===-- ExpandPostRAPseudos.cpp - Pseudo instruction expansion pass -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a pass that expands COPY and SUBREG_TO_REG pseudo
11 // instructions after register allocation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/Passes.h"
16 #include "llvm/CodeGen/MachineFunctionPass.h"
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Target/TargetRegisterInfo.h"
24 #include "llvm/Target/TargetSubtargetInfo.h"
28 #define DEBUG_TYPE "postrapseudos"
31 struct ExpandPostRA : public MachineFunctionPass {
33 const TargetRegisterInfo *TRI;
34 const TargetInstrInfo *TII;
37 static char ID; // Pass identification, replacement for typeid
38 ExpandPostRA() : MachineFunctionPass(ID) {}
40 void getAnalysisUsage(AnalysisUsage &AU) const override {
42 AU.addPreservedID(MachineLoopInfoID);
43 AU.addPreservedID(MachineDominatorsID);
44 MachineFunctionPass::getAnalysisUsage(AU);
47 /// runOnMachineFunction - pass entry point
48 bool runOnMachineFunction(MachineFunction&) override;
51 bool LowerSubregToReg(MachineInstr *MI);
52 bool LowerCopy(MachineInstr *MI);
54 void TransferImplicitDefs(MachineInstr *MI);
56 } // end anonymous namespace
58 char ExpandPostRA::ID = 0;
59 char &llvm::ExpandPostRAPseudosID = ExpandPostRA::ID;
61 INITIALIZE_PASS(ExpandPostRA, "postrapseudos",
62 "Post-RA pseudo instruction expansion pass", false, false)
64 /// TransferImplicitDefs - MI is a pseudo-instruction, and the lowered
65 /// replacement instructions immediately precede it. Copy any implicit-def
66 /// operands from MI to the replacement instruction.
68 ExpandPostRA::TransferImplicitDefs(MachineInstr *MI) {
69 MachineBasicBlock::iterator CopyMI = MI;
72 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
73 MachineOperand &MO = MI->getOperand(i);
74 if (!MO.isReg() || !MO.isImplicit() || MO.isUse())
76 CopyMI->addOperand(MachineOperand::CreateReg(MO.getReg(), true, true));
80 bool ExpandPostRA::LowerSubregToReg(MachineInstr *MI) {
81 MachineBasicBlock *MBB = MI->getParent();
82 assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
83 MI->getOperand(1).isImm() &&
84 (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
85 MI->getOperand(3).isImm() && "Invalid subreg_to_reg");
87 unsigned DstReg = MI->getOperand(0).getReg();
88 unsigned InsReg = MI->getOperand(2).getReg();
89 assert(!MI->getOperand(2).getSubReg() && "SubIdx on physreg?");
90 unsigned SubIdx = MI->getOperand(3).getImm();
92 assert(SubIdx != 0 && "Invalid index for insert_subreg");
93 unsigned DstSubReg = TRI->getSubReg(DstReg, SubIdx);
95 assert(TargetRegisterInfo::isPhysicalRegister(DstReg) &&
96 "Insert destination must be in a physical register");
97 assert(TargetRegisterInfo::isPhysicalRegister(InsReg) &&
98 "Inserted value must be in a physical register");
100 DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
102 if (MI->allDefsAreDead()) {
103 MI->setDesc(TII->get(TargetOpcode::KILL));
104 DEBUG(dbgs() << "subreg: replaced by: " << *MI);
108 if (DstSubReg == InsReg) {
109 // No need to insert an identity copy instruction.
110 // Watch out for case like this:
111 // %RAX<def> = SUBREG_TO_REG 0, %EAX<kill>, 3
112 // We must leave %RAX live.
113 if (DstReg != InsReg) {
114 MI->setDesc(TII->get(TargetOpcode::KILL));
115 MI->RemoveOperand(3); // SubIdx
116 MI->RemoveOperand(1); // Imm
117 DEBUG(dbgs() << "subreg: replace by: " << *MI);
120 DEBUG(dbgs() << "subreg: eliminated!");
122 TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg,
123 MI->getOperand(2).isKill());
125 // Implicitly define DstReg for subsequent uses.
126 MachineBasicBlock::iterator CopyMI = MI;
128 CopyMI->addRegisterDefined(DstReg);
129 DEBUG(dbgs() << "subreg: " << *CopyMI);
132 DEBUG(dbgs() << '\n');
137 bool ExpandPostRA::LowerCopy(MachineInstr *MI) {
139 if (MI->allDefsAreDead()) {
140 DEBUG(dbgs() << "dead copy: " << *MI);
141 MI->setDesc(TII->get(TargetOpcode::KILL));
142 DEBUG(dbgs() << "replaced by: " << *MI);
146 MachineOperand &DstMO = MI->getOperand(0);
147 MachineOperand &SrcMO = MI->getOperand(1);
149 if (SrcMO.getReg() == DstMO.getReg()) {
150 DEBUG(dbgs() << "identity copy: " << *MI);
151 // No need to insert an identity copy instruction, but replace with a KILL
152 // if liveness is changed.
153 if (SrcMO.isUndef() || MI->getNumOperands() > 2) {
154 // We must make sure the super-register gets killed. Replace the
155 // instruction with KILL.
156 MI->setDesc(TII->get(TargetOpcode::KILL));
157 DEBUG(dbgs() << "replaced by: " << *MI);
160 // Vanilla identity copy.
161 MI->eraseFromParent();
165 DEBUG(dbgs() << "real copy: " << *MI);
166 TII->copyPhysReg(*MI->getParent(), MI, MI->getDebugLoc(),
167 DstMO.getReg(), SrcMO.getReg(), SrcMO.isKill());
169 if (MI->getNumOperands() > 2)
170 TransferImplicitDefs(MI);
172 MachineBasicBlock::iterator dMI = MI;
173 dbgs() << "replaced by: " << *(--dMI);
175 MI->eraseFromParent();
179 /// runOnMachineFunction - Reduce subregister inserts and extracts to register
182 bool ExpandPostRA::runOnMachineFunction(MachineFunction &MF) {
183 DEBUG(dbgs() << "Machine Function\n"
184 << "********** EXPANDING POST-RA PSEUDO INSTRS **********\n"
185 << "********** Function: " << MF.getName() << '\n');
186 TRI = MF.getSubtarget().getRegisterInfo();
187 TII = MF.getSubtarget().getInstrInfo();
189 bool MadeChange = false;
191 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
192 mbbi != mbbe; ++mbbi) {
193 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
195 MachineInstr *MI = mi;
196 // Advance iterator here because MI may be erased.
199 // Only expand pseudos.
203 // Give targets a chance to expand even standard pseudos.
204 if (TII->expandPostRAPseudo(MI)) {
209 // Expand standard pseudos.
210 switch (MI->getOpcode()) {
211 case TargetOpcode::SUBREG_TO_REG:
212 MadeChange |= LowerSubregToReg(MI);
214 case TargetOpcode::COPY:
215 MadeChange |= LowerCopy(MI);
217 case TargetOpcode::DBG_VALUE:
219 case TargetOpcode::INSERT_SUBREG:
220 case TargetOpcode::EXTRACT_SUBREG:
221 llvm_unreachable("Sub-register pseudos should have been eliminated.");