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/Passes.h"
17 #include "llvm/CodeGen/TargetInstrInfo.h"
18 #include "llvm/CodeGen/TargetRegisterInfo.h"
19 #include "llvm/CodeGen/TargetSubtargetInfo.h"
20 #include "llvm/InitializePasses.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
26 #define DEBUG_TYPE "postrapseudos"
29 struct ExpandPostRA
: public MachineFunctionPass
{
31 const TargetRegisterInfo
*TRI
= nullptr;
32 const TargetInstrInfo
*TII
= nullptr;
35 static char ID
; // Pass identification, replacement for typeid
36 ExpandPostRA() : MachineFunctionPass(ID
) {}
38 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
40 AU
.addPreservedID(MachineLoopInfoID
);
41 AU
.addPreservedID(MachineDominatorsID
);
42 MachineFunctionPass::getAnalysisUsage(AU
);
45 /// runOnMachineFunction - pass entry point
46 bool runOnMachineFunction(MachineFunction
&) override
;
49 bool LowerSubregToReg(MachineInstr
*MI
);
51 } // end anonymous namespace
53 char ExpandPostRA::ID
= 0;
54 char &llvm::ExpandPostRAPseudosID
= ExpandPostRA::ID
;
56 INITIALIZE_PASS(ExpandPostRA
, DEBUG_TYPE
,
57 "Post-RA pseudo instruction expansion pass", false, false)
59 bool ExpandPostRA::LowerSubregToReg(MachineInstr
*MI
) {
60 MachineBasicBlock
*MBB
= MI
->getParent();
61 assert((MI
->getOperand(0).isReg() && MI
->getOperand(0).isDef()) &&
62 MI
->getOperand(1).isImm() &&
63 (MI
->getOperand(2).isReg() && MI
->getOperand(2).isUse()) &&
64 MI
->getOperand(3).isImm() && "Invalid subreg_to_reg");
66 Register DstReg
= MI
->getOperand(0).getReg();
67 Register InsReg
= MI
->getOperand(2).getReg();
68 assert(!MI
->getOperand(2).getSubReg() && "SubIdx on physreg?");
69 unsigned SubIdx
= MI
->getOperand(3).getImm();
71 assert(SubIdx
!= 0 && "Invalid index for insert_subreg");
72 Register DstSubReg
= TRI
->getSubReg(DstReg
, SubIdx
);
74 assert(DstReg
.isPhysical() &&
75 "Insert destination must be in a physical register");
76 assert(InsReg
.isPhysical() &&
77 "Inserted value must be in a physical register");
79 LLVM_DEBUG(dbgs() << "subreg: CONVERTING: " << *MI
);
81 if (MI
->allDefsAreDead()) {
82 MI
->setDesc(TII
->get(TargetOpcode::KILL
));
83 MI
->removeOperand(3); // SubIdx
84 MI
->removeOperand(1); // Imm
85 LLVM_DEBUG(dbgs() << "subreg: replaced by: " << *MI
);
89 if (DstSubReg
== InsReg
) {
90 // No need to insert an identity copy instruction.
91 // Watch out for case like this:
92 // %rax = SUBREG_TO_REG 0, killed %eax, 3
93 // We must leave %rax live.
94 if (DstReg
!= InsReg
) {
95 MI
->setDesc(TII
->get(TargetOpcode::KILL
));
96 MI
->removeOperand(3); // SubIdx
97 MI
->removeOperand(1); // Imm
98 LLVM_DEBUG(dbgs() << "subreg: replace by: " << *MI
);
101 LLVM_DEBUG(dbgs() << "subreg: eliminated!");
103 TII
->copyPhysReg(*MBB
, MI
, MI
->getDebugLoc(), DstSubReg
, InsReg
,
104 MI
->getOperand(2).isKill());
106 // Implicitly define DstReg for subsequent uses.
107 MachineBasicBlock::iterator CopyMI
= MI
;
109 CopyMI
->addRegisterDefined(DstReg
);
110 LLVM_DEBUG(dbgs() << "subreg: " << *CopyMI
);
113 LLVM_DEBUG(dbgs() << '\n');
118 /// runOnMachineFunction - Reduce subregister inserts and extracts to register
121 bool ExpandPostRA::runOnMachineFunction(MachineFunction
&MF
) {
122 LLVM_DEBUG(dbgs() << "Machine Function\n"
123 << "********** EXPANDING POST-RA PSEUDO INSTRS **********\n"
124 << "********** Function: " << MF
.getName() << '\n');
125 TRI
= MF
.getSubtarget().getRegisterInfo();
126 TII
= MF
.getSubtarget().getInstrInfo();
128 bool MadeChange
= false;
130 for (MachineBasicBlock
&MBB
: MF
) {
131 for (MachineInstr
&MI
: llvm::make_early_inc_range(MBB
)) {
132 // Only expand pseudos.
136 // Give targets a chance to expand even standard pseudos.
137 if (TII
->expandPostRAPseudo(MI
)) {
142 // Expand standard pseudos.
143 switch (MI
.getOpcode()) {
144 case TargetOpcode::SUBREG_TO_REG
:
145 MadeChange
|= LowerSubregToReg(&MI
);
147 case TargetOpcode::COPY
:
148 TII
->lowerCopy(&MI
, TRI
);
151 case TargetOpcode::DBG_VALUE
:
153 case TargetOpcode::INSERT_SUBREG
:
154 case TargetOpcode::EXTRACT_SUBREG
:
155 llvm_unreachable("Sub-register pseudos should have been eliminated.");