1 //===-- LowerSubregs.cpp - Subregister Lowering instruction 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 MachineFunction pass which runs after register
11 // allocation that turns subreg insert/extract instructions into register
12 // copies, as needed. This ensures correct codegen even if the coalescer
13 // isn't able to remove all subreg instructions.
15 //===----------------------------------------------------------------------===//
17 #define DEBUG_TYPE "lowersubregs"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/Function.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/MachineInstr.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/Target/TargetRegisterInfo.h"
25 #include "llvm/Target/TargetInstrInfo.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/raw_ostream.h"
32 struct LowerSubregsInstructionPass
: public MachineFunctionPass
{
34 const TargetRegisterInfo
*TRI
;
35 const TargetInstrInfo
*TII
;
38 static char ID
; // Pass identification, replacement for typeid
39 LowerSubregsInstructionPass() : MachineFunctionPass(ID
) {}
41 const char *getPassName() const {
42 return "Subregister lowering instruction pass";
45 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
47 AU
.addPreservedID(MachineLoopInfoID
);
48 AU
.addPreservedID(MachineDominatorsID
);
49 MachineFunctionPass::getAnalysisUsage(AU
);
52 /// runOnMachineFunction - pass entry point
53 bool runOnMachineFunction(MachineFunction
&);
56 bool LowerSubregToReg(MachineInstr
*MI
);
57 bool LowerCopy(MachineInstr
*MI
);
59 void TransferDeadFlag(MachineInstr
*MI
, unsigned DstReg
,
60 const TargetRegisterInfo
*TRI
);
61 void TransferImplicitDefs(MachineInstr
*MI
);
64 char LowerSubregsInstructionPass::ID
= 0;
67 FunctionPass
*llvm::createLowerSubregsPass() {
68 return new LowerSubregsInstructionPass();
71 /// TransferDeadFlag - MI is a pseudo-instruction with DstReg dead,
72 /// and the lowered replacement instructions immediately precede it.
73 /// Mark the replacement instructions with the dead flag.
75 LowerSubregsInstructionPass::TransferDeadFlag(MachineInstr
*MI
,
77 const TargetRegisterInfo
*TRI
) {
78 for (MachineBasicBlock::iterator MII
=
79 prior(MachineBasicBlock::iterator(MI
)); ; --MII
) {
80 if (MII
->addRegisterDead(DstReg
, TRI
))
82 assert(MII
!= MI
->getParent()->begin() &&
83 "copyPhysReg output doesn't reference destination register!");
87 /// TransferImplicitDefs - MI is a pseudo-instruction, and the lowered
88 /// replacement instructions immediately precede it. Copy any implicit-def
89 /// operands from MI to the replacement instruction.
91 LowerSubregsInstructionPass::TransferImplicitDefs(MachineInstr
*MI
) {
92 MachineBasicBlock::iterator CopyMI
= MI
;
95 for (unsigned i
= 0, e
= MI
->getNumOperands(); i
!= e
; ++i
) {
96 MachineOperand
&MO
= MI
->getOperand(i
);
97 if (!MO
.isReg() || !MO
.isImplicit() || MO
.isUse())
99 CopyMI
->addOperand(MachineOperand::CreateReg(MO
.getReg(), true, true));
103 bool LowerSubregsInstructionPass::LowerSubregToReg(MachineInstr
*MI
) {
104 MachineBasicBlock
*MBB
= MI
->getParent();
105 assert((MI
->getOperand(0).isReg() && MI
->getOperand(0).isDef()) &&
106 MI
->getOperand(1).isImm() &&
107 (MI
->getOperand(2).isReg() && MI
->getOperand(2).isUse()) &&
108 MI
->getOperand(3).isImm() && "Invalid subreg_to_reg");
110 unsigned DstReg
= MI
->getOperand(0).getReg();
111 unsigned InsReg
= MI
->getOperand(2).getReg();
112 assert(!MI
->getOperand(2).getSubReg() && "SubIdx on physreg?");
113 unsigned SubIdx
= MI
->getOperand(3).getImm();
115 assert(SubIdx
!= 0 && "Invalid index for insert_subreg");
116 unsigned DstSubReg
= TRI
->getSubReg(DstReg
, SubIdx
);
118 assert(TargetRegisterInfo::isPhysicalRegister(DstReg
) &&
119 "Insert destination must be in a physical register");
120 assert(TargetRegisterInfo::isPhysicalRegister(InsReg
) &&
121 "Inserted value must be in a physical register");
123 DEBUG(dbgs() << "subreg: CONVERTING: " << *MI
);
125 if (DstSubReg
== InsReg
) {
126 // No need to insert an identify copy instruction.
127 // Watch out for case like this:
128 // %RAX<def> = SUBREG_TO_REG 0, %EAX<kill>, 3
129 // We must leave %RAX live.
130 if (DstReg
!= InsReg
) {
131 MI
->setDesc(TII
->get(TargetOpcode::KILL
));
132 MI
->RemoveOperand(3); // SubIdx
133 MI
->RemoveOperand(1); // Imm
134 DEBUG(dbgs() << "subreg: replace by: " << *MI
);
137 DEBUG(dbgs() << "subreg: eliminated!");
139 TII
->copyPhysReg(*MBB
, MI
, MI
->getDebugLoc(), DstSubReg
, InsReg
,
140 MI
->getOperand(2).isKill());
141 // Transfer the kill/dead flags, if needed.
142 if (MI
->getOperand(0).isDead())
143 TransferDeadFlag(MI
, DstSubReg
, TRI
);
145 MachineBasicBlock::iterator dMI
= MI
;
146 dbgs() << "subreg: " << *(--dMI
);
150 DEBUG(dbgs() << '\n');
155 bool LowerSubregsInstructionPass::LowerCopy(MachineInstr
*MI
) {
156 MachineOperand
&DstMO
= MI
->getOperand(0);
157 MachineOperand
&SrcMO
= MI
->getOperand(1);
159 if (SrcMO
.getReg() == DstMO
.getReg()) {
160 DEBUG(dbgs() << "identity copy: " << *MI
);
161 // No need to insert an identity copy instruction, but replace with a KILL
162 // if liveness is changed.
163 if (DstMO
.isDead() || SrcMO
.isUndef() || MI
->getNumOperands() > 2) {
164 // We must make sure the super-register gets killed. Replace the
165 // instruction with KILL.
166 MI
->setDesc(TII
->get(TargetOpcode::KILL
));
167 DEBUG(dbgs() << "replaced by: " << *MI
);
170 // Vanilla identity copy.
171 MI
->eraseFromParent();
175 DEBUG(dbgs() << "real copy: " << *MI
);
176 TII
->copyPhysReg(*MI
->getParent(), MI
, MI
->getDebugLoc(),
177 DstMO
.getReg(), SrcMO
.getReg(), SrcMO
.isKill());
180 TransferDeadFlag(MI
, DstMO
.getReg(), TRI
);
181 if (MI
->getNumOperands() > 2)
182 TransferImplicitDefs(MI
);
184 MachineBasicBlock::iterator dMI
= MI
;
185 dbgs() << "replaced by: " << *(--dMI
);
187 MI
->eraseFromParent();
191 /// runOnMachineFunction - Reduce subregister inserts and extracts to register
194 bool LowerSubregsInstructionPass::runOnMachineFunction(MachineFunction
&MF
) {
195 DEBUG(dbgs() << "Machine Function\n"
196 << "********** LOWERING SUBREG INSTRS **********\n"
197 << "********** Function: "
198 << MF
.getFunction()->getName() << '\n');
199 TRI
= MF
.getTarget().getRegisterInfo();
200 TII
= MF
.getTarget().getInstrInfo();
202 bool MadeChange
= false;
204 for (MachineFunction::iterator mbbi
= MF
.begin(), mbbe
= MF
.end();
205 mbbi
!= mbbe
; ++mbbi
) {
206 for (MachineBasicBlock::iterator mi
= mbbi
->begin(), me
= mbbi
->end();
208 MachineBasicBlock::iterator nmi
= llvm::next(mi
);
209 MachineInstr
*MI
= mi
;
210 assert(!MI
->isInsertSubreg() && "INSERT_SUBREG should no longer appear");
211 assert(MI
->getOpcode() != TargetOpcode::EXTRACT_SUBREG
&&
212 "EXTRACT_SUBREG should no longer appear");
213 if (MI
->isSubregToReg()) {
214 MadeChange
|= LowerSubregToReg(MI
);
215 } else if (MI
->isCopy()) {
216 MadeChange
|= LowerCopy(MI
);