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 LowerExtract(MachineInstr *MI);
57 bool LowerInsert(MachineInstr *MI);
58 bool LowerSubregToReg(MachineInstr *MI);
60 void TransferDeadFlag(MachineInstr *MI, unsigned DstReg,
61 const TargetRegisterInfo *TRI);
62 void TransferKillFlag(MachineInstr *MI, unsigned SrcReg,
63 const TargetRegisterInfo *TRI,
64 bool AddIfNotFound = false);
67 char LowerSubregsInstructionPass::ID = 0;
70 FunctionPass *llvm::createLowerSubregsPass() {
71 return new LowerSubregsInstructionPass();
74 /// TransferDeadFlag - MI is a pseudo-instruction with DstReg dead,
75 /// and the lowered replacement instructions immediately precede it.
76 /// Mark the replacement instructions with the dead flag.
78 LowerSubregsInstructionPass::TransferDeadFlag(MachineInstr *MI,
80 const TargetRegisterInfo *TRI) {
81 for (MachineBasicBlock::iterator MII =
82 prior(MachineBasicBlock::iterator(MI)); ; --MII) {
83 if (MII->addRegisterDead(DstReg, TRI))
85 assert(MII != MI->getParent()->begin() &&
86 "copyRegToReg output doesn't reference destination register!");
90 /// TransferKillFlag - MI is a pseudo-instruction with SrcReg killed,
91 /// and the lowered replacement instructions immediately precede it.
92 /// Mark the replacement instructions with the kill flag.
94 LowerSubregsInstructionPass::TransferKillFlag(MachineInstr *MI,
96 const TargetRegisterInfo *TRI,
98 for (MachineBasicBlock::iterator MII =
99 prior(MachineBasicBlock::iterator(MI)); ; --MII) {
100 if (MII->addRegisterKilled(SrcReg, TRI, AddIfNotFound))
102 assert(MII != MI->getParent()->begin() &&
103 "copyRegToReg output doesn't reference source register!");
107 bool LowerSubregsInstructionPass::LowerExtract(MachineInstr *MI) {
108 MachineBasicBlock *MBB = MI->getParent();
110 assert(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
111 MI->getOperand(1).isReg() && MI->getOperand(1).isUse() &&
112 MI->getOperand(2).isImm() && "Malformed extract_subreg");
114 unsigned DstReg = MI->getOperand(0).getReg();
115 unsigned SuperReg = MI->getOperand(1).getReg();
116 unsigned SubIdx = MI->getOperand(2).getImm();
117 unsigned SrcReg = TRI->getSubReg(SuperReg, SubIdx);
119 assert(TargetRegisterInfo::isPhysicalRegister(SuperReg) &&
120 "Extract supperg source must be a physical register");
121 assert(TargetRegisterInfo::isPhysicalRegister(DstReg) &&
122 "Extract destination must be in a physical register");
123 assert(SrcReg && "invalid subregister index for register");
125 DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
127 if (SrcReg == DstReg) {
128 // No need to insert an identity copy instruction.
129 if (MI->getOperand(1).isKill()) {
130 // We must make sure the super-register gets killed. Replace the
131 // instruction with KILL.
132 MI->setDesc(TII->get(TargetOpcode::KILL));
133 MI->RemoveOperand(2); // SubIdx
134 DEBUG(dbgs() << "subreg: replace by: " << *MI);
138 DEBUG(dbgs() << "subreg: eliminated!");
141 const TargetRegisterClass *TRCS = TRI->getPhysicalRegisterRegClass(DstReg);
142 const TargetRegisterClass *TRCD = TRI->getPhysicalRegisterRegClass(SrcReg);
143 bool Emitted = TII->copyRegToReg(*MBB, MI, DstReg, SrcReg, TRCD, TRCS,
146 assert(Emitted && "Subreg and Dst must be of compatible register class");
147 // Transfer the kill/dead flags, if needed.
148 if (MI->getOperand(0).isDead())
149 TransferDeadFlag(MI, DstReg, TRI);
150 if (MI->getOperand(1).isKill())
151 TransferKillFlag(MI, SuperReg, TRI, true);
153 MachineBasicBlock::iterator dMI = MI;
154 dbgs() << "subreg: " << *(--dMI);
158 DEBUG(dbgs() << '\n');
163 bool LowerSubregsInstructionPass::LowerSubregToReg(MachineInstr *MI) {
164 MachineBasicBlock *MBB = MI->getParent();
165 assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
166 MI->getOperand(1).isImm() &&
167 (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
168 MI->getOperand(3).isImm() && "Invalid subreg_to_reg");
170 unsigned DstReg = MI->getOperand(0).getReg();
171 unsigned InsReg = MI->getOperand(2).getReg();
172 unsigned InsSIdx = MI->getOperand(2).getSubReg();
173 unsigned SubIdx = MI->getOperand(3).getImm();
175 assert(SubIdx != 0 && "Invalid index for insert_subreg");
176 unsigned DstSubReg = TRI->getSubReg(DstReg, SubIdx);
178 assert(TargetRegisterInfo::isPhysicalRegister(DstReg) &&
179 "Insert destination must be in a physical register");
180 assert(TargetRegisterInfo::isPhysicalRegister(InsReg) &&
181 "Inserted value must be in a physical register");
183 DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
185 if (DstSubReg == InsReg && InsSIdx == 0) {
186 // No need to insert an identify copy instruction.
187 // Watch out for case like this:
189 // %RAX<def> = SUBREG_TO_REG 0, %EAX:3<kill>, 3
190 // The first def is defining RAX, not EAX so the top bits were not
192 DEBUG(dbgs() << "subreg: eliminated!");
194 // Insert sub-register copy
195 const TargetRegisterClass *TRC0= TRI->getPhysicalRegisterRegClass(DstSubReg);
196 const TargetRegisterClass *TRC1= TRI->getPhysicalRegisterRegClass(InsReg);
197 bool Emitted = TII->copyRegToReg(*MBB, MI, DstSubReg, InsReg, TRC0, TRC1,
200 assert(Emitted && "Subreg and Dst must be of compatible register class");
201 // Transfer the kill/dead flags, if needed.
202 if (MI->getOperand(0).isDead())
203 TransferDeadFlag(MI, DstSubReg, TRI);
204 if (MI->getOperand(2).isKill())
205 TransferKillFlag(MI, InsReg, TRI);
207 MachineBasicBlock::iterator dMI = MI;
208 dbgs() << "subreg: " << *(--dMI);
212 DEBUG(dbgs() << '\n');
217 bool LowerSubregsInstructionPass::LowerInsert(MachineInstr *MI) {
218 MachineBasicBlock *MBB = MI->getParent();
219 assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
220 (MI->getOperand(1).isReg() && MI->getOperand(1).isUse()) &&
221 (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
222 MI->getOperand(3).isImm() && "Invalid insert_subreg");
224 unsigned DstReg = MI->getOperand(0).getReg();
226 unsigned SrcReg = MI->getOperand(1).getReg();
228 unsigned InsReg = MI->getOperand(2).getReg();
229 unsigned SubIdx = MI->getOperand(3).getImm();
231 assert(DstReg == SrcReg && "insert_subreg not a two-address instruction?");
232 assert(SubIdx != 0 && "Invalid index for insert_subreg");
233 unsigned DstSubReg = TRI->getSubReg(DstReg, SubIdx);
234 assert(DstSubReg && "invalid subregister index for register");
235 assert(TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
236 "Insert superreg source must be in a physical register");
237 assert(TargetRegisterInfo::isPhysicalRegister(InsReg) &&
238 "Inserted value must be in a physical register");
240 DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
242 if (DstSubReg == InsReg) {
243 // No need to insert an identity copy instruction. If the SrcReg was
244 // <undef>, we need to make sure it is alive by inserting a KILL
245 if (MI->getOperand(1).isUndef() && !MI->getOperand(0).isDead()) {
246 MachineInstrBuilder MIB = BuildMI(*MBB, MI, MI->getDebugLoc(),
247 TII->get(TargetOpcode::KILL), DstReg);
248 if (MI->getOperand(2).isUndef())
249 MIB.addReg(InsReg, RegState::Undef);
251 MIB.addReg(InsReg, RegState::Kill);
253 DEBUG(dbgs() << "subreg: eliminated!\n");
258 // Insert sub-register copy
259 const TargetRegisterClass *TRC0= TRI->getPhysicalRegisterRegClass(DstSubReg);
260 const TargetRegisterClass *TRC1= TRI->getPhysicalRegisterRegClass(InsReg);
261 if (MI->getOperand(2).isUndef())
262 // If the source register being inserted is undef, then this becomes a
264 BuildMI(*MBB, MI, MI->getDebugLoc(),
265 TII->get(TargetOpcode::KILL), DstSubReg);
267 bool Emitted = TII->copyRegToReg(*MBB, MI, DstSubReg, InsReg, TRC0, TRC1,
270 assert(Emitted && "Subreg and Dst must be of compatible register class");
272 MachineBasicBlock::iterator CopyMI = MI;
275 // INSERT_SUBREG is a two-address instruction so it implicitly kills SrcReg.
276 if (!MI->getOperand(1).isUndef())
277 CopyMI->addOperand(MachineOperand::CreateReg(DstReg, false, true, true));
279 // Transfer the kill/dead flags, if needed.
280 if (MI->getOperand(0).isDead()) {
281 TransferDeadFlag(MI, DstSubReg, TRI);
283 // Make sure the full DstReg is live after this replacement.
284 CopyMI->addOperand(MachineOperand::CreateReg(DstReg, true, true));
287 // Make sure the inserted register gets killed
288 if (MI->getOperand(2).isKill() && !MI->getOperand(2).isUndef())
289 TransferKillFlag(MI, InsReg, TRI);
293 MachineBasicBlock::iterator dMI = MI;
294 dbgs() << "subreg: " << *(--dMI) << "\n";
301 /// runOnMachineFunction - Reduce subregister inserts and extracts to register
304 bool LowerSubregsInstructionPass::runOnMachineFunction(MachineFunction &MF) {
305 DEBUG(dbgs() << "Machine Function\n"
306 << "********** LOWERING SUBREG INSTRS **********\n"
307 << "********** Function: "
308 << MF.getFunction()->getName() << '\n');
309 TRI = MF.getTarget().getRegisterInfo();
310 TII = MF.getTarget().getInstrInfo();
312 bool MadeChange = false;
314 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
315 mbbi != mbbe; ++mbbi) {
316 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
318 MachineBasicBlock::iterator nmi = llvm::next(mi);
319 MachineInstr *MI = mi;
320 if (MI->isExtractSubreg()) {
321 MadeChange |= LowerExtract(MI);
322 } else if (MI->isInsertSubreg()) {
323 MadeChange |= LowerInsert(MI);
324 } else if (MI->isSubregToReg()) {
325 MadeChange |= LowerSubregToReg(MI);