1 //===-- ExpandPostRAPseudos.cpp - Pseudo instruction expansion pass -------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines a pass that expands COPY and SUBREG_TO_REG pseudo
10 // instructions after register allocation.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/MachineFunctionPass.h"
15 #include "llvm/CodeGen/MachineInstr.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/CodeGen/TargetInstrInfo.h"
20 #include "llvm/CodeGen/TargetRegisterInfo.h"
21 #include "llvm/CodeGen/TargetSubtargetInfo.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/raw_ostream.h"
27 #define DEBUG_TYPE "postrapseudos"
30 struct ExpandPostRA
: public MachineFunctionPass
{
32 const TargetRegisterInfo
*TRI
;
33 const TargetInstrInfo
*TII
;
36 static char ID
; // Pass identification, replacement for typeid
37 ExpandPostRA() : MachineFunctionPass(ID
) {}
39 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
41 AU
.addPreservedID(MachineLoopInfoID
);
42 AU
.addPreservedID(MachineDominatorsID
);
43 MachineFunctionPass::getAnalysisUsage(AU
);
46 /// runOnMachineFunction - pass entry point
47 bool runOnMachineFunction(MachineFunction
&) override
;
50 bool LowerSubregToReg(MachineInstr
*MI
);
51 bool LowerCopy(MachineInstr
*MI
);
53 void TransferImplicitOperands(MachineInstr
*MI
);
55 } // end anonymous namespace
57 char ExpandPostRA::ID
= 0;
58 char &llvm::ExpandPostRAPseudosID
= ExpandPostRA::ID
;
60 INITIALIZE_PASS(ExpandPostRA
, DEBUG_TYPE
,
61 "Post-RA pseudo instruction expansion pass", false, false)
63 /// TransferImplicitOperands - MI is a pseudo-instruction, and the lowered
64 /// replacement instructions immediately precede it. Copy any implicit
65 /// operands from MI to the replacement instruction.
66 void ExpandPostRA::TransferImplicitOperands(MachineInstr
*MI
) {
67 MachineBasicBlock::iterator CopyMI
= MI
;
70 for (const MachineOperand
&MO
: MI
->implicit_operands())
72 CopyMI
->addOperand(MO
);
75 bool ExpandPostRA::LowerSubregToReg(MachineInstr
*MI
) {
76 MachineBasicBlock
*MBB
= MI
->getParent();
77 assert((MI
->getOperand(0).isReg() && MI
->getOperand(0).isDef()) &&
78 MI
->getOperand(1).isImm() &&
79 (MI
->getOperand(2).isReg() && MI
->getOperand(2).isUse()) &&
80 MI
->getOperand(3).isImm() && "Invalid subreg_to_reg");
82 Register DstReg
= MI
->getOperand(0).getReg();
83 Register InsReg
= MI
->getOperand(2).getReg();
84 assert(!MI
->getOperand(2).getSubReg() && "SubIdx on physreg?");
85 unsigned SubIdx
= MI
->getOperand(3).getImm();
87 assert(SubIdx
!= 0 && "Invalid index for insert_subreg");
88 Register DstSubReg
= TRI
->getSubReg(DstReg
, SubIdx
);
90 assert(Register::isPhysicalRegister(DstReg
) &&
91 "Insert destination must be in a physical register");
92 assert(Register::isPhysicalRegister(InsReg
) &&
93 "Inserted value must be in a physical register");
95 LLVM_DEBUG(dbgs() << "subreg: CONVERTING: " << *MI
);
97 if (MI
->allDefsAreDead()) {
98 MI
->setDesc(TII
->get(TargetOpcode::KILL
));
99 MI
->RemoveOperand(3); // SubIdx
100 MI
->RemoveOperand(1); // Imm
101 LLVM_DEBUG(dbgs() << "subreg: replaced by: " << *MI
);
105 if (DstSubReg
== InsReg
) {
106 // No need to insert an identity copy instruction.
107 // Watch out for case like this:
108 // %rax = SUBREG_TO_REG 0, killed %eax, 3
109 // We must leave %rax live.
110 if (DstReg
!= InsReg
) {
111 MI
->setDesc(TII
->get(TargetOpcode::KILL
));
112 MI
->RemoveOperand(3); // SubIdx
113 MI
->RemoveOperand(1); // Imm
114 LLVM_DEBUG(dbgs() << "subreg: replace by: " << *MI
);
117 LLVM_DEBUG(dbgs() << "subreg: eliminated!");
119 TII
->copyPhysReg(*MBB
, MI
, MI
->getDebugLoc(), DstSubReg
, InsReg
,
120 MI
->getOperand(2).isKill());
122 // Implicitly define DstReg for subsequent uses.
123 MachineBasicBlock::iterator CopyMI
= MI
;
125 CopyMI
->addRegisterDefined(DstReg
);
126 LLVM_DEBUG(dbgs() << "subreg: " << *CopyMI
);
129 LLVM_DEBUG(dbgs() << '\n');
134 bool ExpandPostRA::LowerCopy(MachineInstr
*MI
) {
136 if (MI
->allDefsAreDead()) {
137 LLVM_DEBUG(dbgs() << "dead copy: " << *MI
);
138 MI
->setDesc(TII
->get(TargetOpcode::KILL
));
139 LLVM_DEBUG(dbgs() << "replaced by: " << *MI
);
143 MachineOperand
&DstMO
= MI
->getOperand(0);
144 MachineOperand
&SrcMO
= MI
->getOperand(1);
146 bool IdentityCopy
= (SrcMO
.getReg() == DstMO
.getReg());
147 if (IdentityCopy
|| SrcMO
.isUndef()) {
148 LLVM_DEBUG(dbgs() << (IdentityCopy
? "identity copy: " : "undef copy: ")
150 // No need to insert an identity copy instruction, but replace with a KILL
151 // if liveness is changed.
152 if (SrcMO
.isUndef() || MI
->getNumOperands() > 2) {
153 // We must make sure the super-register gets killed. Replace the
154 // instruction with KILL.
155 MI
->setDesc(TII
->get(TargetOpcode::KILL
));
156 LLVM_DEBUG(dbgs() << "replaced by: " << *MI
);
159 // Vanilla identity copy.
160 MI
->eraseFromParent();
164 LLVM_DEBUG(dbgs() << "real copy: " << *MI
);
165 TII
->copyPhysReg(*MI
->getParent(), MI
, MI
->getDebugLoc(),
166 DstMO
.getReg(), SrcMO
.getReg(), SrcMO
.isKill());
168 if (MI
->getNumOperands() > 2)
169 TransferImplicitOperands(MI
);
171 MachineBasicBlock::iterator dMI
= MI
;
172 dbgs() << "replaced by: " << *(--dMI
);
174 MI
->eraseFromParent();
178 /// runOnMachineFunction - Reduce subregister inserts and extracts to register
181 bool ExpandPostRA::runOnMachineFunction(MachineFunction
&MF
) {
182 LLVM_DEBUG(dbgs() << "Machine Function\n"
183 << "********** EXPANDING POST-RA PSEUDO INSTRS **********\n"
184 << "********** Function: " << MF
.getName() << '\n');
185 TRI
= MF
.getSubtarget().getRegisterInfo();
186 TII
= MF
.getSubtarget().getInstrInfo();
188 bool MadeChange
= false;
190 for (MachineFunction::iterator mbbi
= MF
.begin(), mbbe
= MF
.end();
191 mbbi
!= mbbe
; ++mbbi
) {
192 for (MachineBasicBlock::iterator mi
= mbbi
->begin(), me
= mbbi
->end();
194 MachineInstr
&MI
= *mi
;
195 // Advance iterator here because MI may be erased.
198 // Only expand pseudos.
202 // Give targets a chance to expand even standard pseudos.
203 if (TII
->expandPostRAPseudo(MI
)) {
208 // Expand standard pseudos.
209 switch (MI
.getOpcode()) {
210 case TargetOpcode::SUBREG_TO_REG
:
211 MadeChange
|= LowerSubregToReg(&MI
);
213 case TargetOpcode::COPY
:
214 MadeChange
|= LowerCopy(&MI
);
216 case TargetOpcode::DBG_VALUE
:
218 case TargetOpcode::INSERT_SUBREG
:
219 case TargetOpcode::EXTRACT_SUBREG
:
220 llvm_unreachable("Sub-register pseudos should have been eliminated.");