blob: dfd666f96db593299499dfdde709d6f00e1c86b5 [file] [log] [blame]
Christopher Lambbab24742007-07-26 08:18:32 +00001//===-- LowerSubregs.cpp - Subregister Lowering instruction pass ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Christopher Lambbab24742007-07-26 08:18:32 +00007//
8//===----------------------------------------------------------------------===//
Dan Gohmanbd0f1442008-09-24 23:44:12 +00009//
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.
14//
15//===----------------------------------------------------------------------===//
Christopher Lambbab24742007-07-26 08:18:32 +000016
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"
Jakob Stoklund Olesen980daea2009-08-03 20:08:18 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000024#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lambbab24742007-07-26 08:18:32 +000025#include "llvm/Target/TargetInstrInfo.h"
26#include "llvm/Target/TargetMachine.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/Compiler.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000029#include "llvm/Support/raw_ostream.h"
Christopher Lambbab24742007-07-26 08:18:32 +000030using namespace llvm;
31
32namespace {
33 struct VISIBILITY_HIDDEN LowerSubregsInstructionPass
34 : public MachineFunctionPass {
35 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000036 LowerSubregsInstructionPass() : MachineFunctionPass(&ID) {}
Christopher Lambbab24742007-07-26 08:18:32 +000037
38 const char *getPassName() const {
39 return "Subregister lowering instruction pass";
40 }
41
Evan Chengbbeeb2a2008-09-22 20:58:04 +000042 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000043 AU.setPreservesCFG();
Evan Cheng8b56a902008-09-22 22:21:38 +000044 AU.addPreservedID(MachineLoopInfoID);
45 AU.addPreservedID(MachineDominatorsID);
Evan Chengbbeeb2a2008-09-22 20:58:04 +000046 MachineFunctionPass::getAnalysisUsage(AU);
47 }
48
Christopher Lambbab24742007-07-26 08:18:32 +000049 /// runOnMachineFunction - pass entry point
50 bool runOnMachineFunction(MachineFunction&);
Christopher Lamb98363222007-08-06 16:33:56 +000051
52 bool LowerExtract(MachineInstr *MI);
53 bool LowerInsert(MachineInstr *MI);
Christopher Lambc9298232008-03-16 03:12:01 +000054 bool LowerSubregToReg(MachineInstr *MI);
Dan Gohmana5b2fee2008-12-18 22:14:08 +000055
56 void TransferDeadFlag(MachineInstr *MI, unsigned DstReg,
57 const TargetRegisterInfo &TRI);
58 void TransferKillFlag(MachineInstr *MI, unsigned SrcReg,
59 const TargetRegisterInfo &TRI);
Christopher Lambbab24742007-07-26 08:18:32 +000060 };
61
62 char LowerSubregsInstructionPass::ID = 0;
63}
64
65FunctionPass *llvm::createLowerSubregsPass() {
66 return new LowerSubregsInstructionPass();
67}
68
Dan Gohmana5b2fee2008-12-18 22:14:08 +000069/// TransferDeadFlag - MI is a pseudo-instruction with DstReg dead,
70/// and the lowered replacement instructions immediately precede it.
71/// Mark the replacement instructions with the dead flag.
72void
73LowerSubregsInstructionPass::TransferDeadFlag(MachineInstr *MI,
74 unsigned DstReg,
75 const TargetRegisterInfo &TRI) {
76 for (MachineBasicBlock::iterator MII =
77 prior(MachineBasicBlock::iterator(MI)); ; --MII) {
78 if (MII->addRegisterDead(DstReg, &TRI))
79 break;
80 assert(MII != MI->getParent()->begin() &&
81 "copyRegToReg output doesn't reference destination register!");
82 }
83}
84
85/// TransferKillFlag - MI is a pseudo-instruction with SrcReg killed,
86/// and the lowered replacement instructions immediately precede it.
87/// Mark the replacement instructions with the kill flag.
88void
89LowerSubregsInstructionPass::TransferKillFlag(MachineInstr *MI,
90 unsigned SrcReg,
91 const TargetRegisterInfo &TRI) {
92 for (MachineBasicBlock::iterator MII =
93 prior(MachineBasicBlock::iterator(MI)); ; --MII) {
94 if (MII->addRegisterKilled(SrcReg, &TRI))
95 break;
96 assert(MII != MI->getParent()->begin() &&
97 "copyRegToReg output doesn't reference source register!");
98 }
99}
100
Christopher Lamb98363222007-08-06 16:33:56 +0000101bool LowerSubregsInstructionPass::LowerExtract(MachineInstr *MI) {
Dan Gohman07af7652008-12-18 22:06:01 +0000102 MachineBasicBlock *MBB = MI->getParent();
103 MachineFunction &MF = *MBB->getParent();
104 const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo();
105 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
106
107 assert(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
108 MI->getOperand(1).isReg() && MI->getOperand(1).isUse() &&
109 MI->getOperand(2).isImm() && "Malformed extract_subreg");
Christopher Lamb98363222007-08-06 16:33:56 +0000110
Dan Gohman07af7652008-12-18 22:06:01 +0000111 unsigned DstReg = MI->getOperand(0).getReg();
112 unsigned SuperReg = MI->getOperand(1).getReg();
113 unsigned SubIdx = MI->getOperand(2).getImm();
114 unsigned SrcReg = TRI.getSubReg(SuperReg, SubIdx);
Christopher Lamb98363222007-08-06 16:33:56 +0000115
Dan Gohman07af7652008-12-18 22:06:01 +0000116 assert(TargetRegisterInfo::isPhysicalRegister(SuperReg) &&
117 "Extract supperg source must be a physical register");
118 assert(TargetRegisterInfo::isPhysicalRegister(DstReg) &&
Dan Gohmanf04865f2008-12-18 22:07:25 +0000119 "Extract destination must be in a physical register");
Dan Gohman07af7652008-12-18 22:06:01 +0000120
121 DOUT << "subreg: CONVERTING: " << *MI;
Christopher Lamb98363222007-08-06 16:33:56 +0000122
Dan Gohman98c20692008-12-18 22:11:34 +0000123 if (SrcReg == DstReg) {
124 // No need to insert an identify copy instruction.
125 DOUT << "subreg: eliminated!";
Dan Gohmana5b2fee2008-12-18 22:14:08 +0000126 // Find the kill of the destination register's live range, and insert
127 // a kill of the source register at that point.
128 if (MI->getOperand(1).isKill() && !MI->getOperand(0).isDead())
129 for (MachineBasicBlock::iterator MII =
130 next(MachineBasicBlock::iterator(MI));
131 MII != MBB->end(); ++MII)
132 if (MII->killsRegister(DstReg, &TRI)) {
133 MII->addRegisterKilled(SuperReg, &TRI, /*AddIfNotFound=*/true);
Jakob Stoklund Olesen980daea2009-08-03 20:08:18 +0000134 DOUT << "\nsubreg: killed here: " << *MII;
Dan Gohmana5b2fee2008-12-18 22:14:08 +0000135 break;
136 }
Dan Gohman98c20692008-12-18 22:11:34 +0000137 } else {
138 // Insert copy
Anton Korobeynikovd5197562009-07-16 13:55:26 +0000139 const TargetRegisterClass *TRCS = TRI.getPhysicalRegisterRegClass(DstReg);
140 const TargetRegisterClass *TRCD = TRI.getPhysicalRegisterRegClass(SrcReg);
141 bool Emitted = TII.copyRegToReg(*MBB, MI, DstReg, SrcReg, TRCD, TRCS);
142 (void)Emitted;
143 assert(Emitted && "Subreg and Dst must be of compatible register class");
Dan Gohmana5b2fee2008-12-18 22:14:08 +0000144 // Transfer the kill/dead flags, if needed.
145 if (MI->getOperand(0).isDead())
146 TransferDeadFlag(MI, DstReg, TRI);
147 if (MI->getOperand(1).isKill())
148 TransferKillFlag(MI, SrcReg, TRI);
149
Christopher Lambc9298232008-03-16 03:12:01 +0000150#ifndef NDEBUG
Dan Gohman07af7652008-12-18 22:06:01 +0000151 MachineBasicBlock::iterator dMI = MI;
152 DOUT << "subreg: " << *(--dMI);
Christopher Lambc9298232008-03-16 03:12:01 +0000153#endif
Dan Gohman07af7652008-12-18 22:06:01 +0000154 }
Christopher Lamb98363222007-08-06 16:33:56 +0000155
Dan Gohman07af7652008-12-18 22:06:01 +0000156 DOUT << "\n";
157 MBB->erase(MI);
158 return true;
Christopher Lamb98363222007-08-06 16:33:56 +0000159}
160
Christopher Lambc9298232008-03-16 03:12:01 +0000161bool LowerSubregsInstructionPass::LowerSubregToReg(MachineInstr *MI) {
162 MachineBasicBlock *MBB = MI->getParent();
163 MachineFunction &MF = *MBB->getParent();
164 const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo();
165 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
Dan Gohmand735b802008-10-03 15:45:36 +0000166 assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
167 MI->getOperand(1).isImm() &&
168 (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
169 MI->getOperand(3).isImm() && "Invalid subreg_to_reg");
Christopher Lambc9298232008-03-16 03:12:01 +0000170
171 unsigned DstReg = MI->getOperand(0).getReg();
172 unsigned InsReg = MI->getOperand(2).getReg();
Evan Cheng7d6d4b32009-03-23 07:19:58 +0000173 unsigned InsSIdx = MI->getOperand(2).getSubReg();
174 unsigned SubIdx = MI->getOperand(3).getImm();
Christopher Lambc9298232008-03-16 03:12:01 +0000175
176 assert(SubIdx != 0 && "Invalid index for insert_subreg");
177 unsigned DstSubReg = TRI.getSubReg(DstReg, SubIdx);
Evan Cheng7d6d4b32009-03-23 07:19:58 +0000178
Christopher Lambc9298232008-03-16 03:12:01 +0000179 assert(TargetRegisterInfo::isPhysicalRegister(DstReg) &&
180 "Insert destination must be in a physical register");
181 assert(TargetRegisterInfo::isPhysicalRegister(InsReg) &&
182 "Inserted value must be in a physical register");
183
184 DOUT << "subreg: CONVERTING: " << *MI;
185
Evan Cheng7d6d4b32009-03-23 07:19:58 +0000186 if (DstSubReg == InsReg && InsSIdx == 0) {
Dan Gohmane3d92062008-08-07 02:54:50 +0000187 // No need to insert an identify copy instruction.
Evan Cheng7d6d4b32009-03-23 07:19:58 +0000188 // Watch out for case like this:
189 // %RAX<def> = ...
190 // %RAX<def> = SUBREG_TO_REG 0, %EAX:3<kill>, 3
191 // The first def is defining RAX, not EAX so the top bits were not
192 // zero extended.
Dan Gohmane3d92062008-08-07 02:54:50 +0000193 DOUT << "subreg: eliminated!";
194 } else {
195 // Insert sub-register copy
196 const TargetRegisterClass *TRC0= TRI.getPhysicalRegisterRegClass(DstSubReg);
197 const TargetRegisterClass *TRC1= TRI.getPhysicalRegisterRegClass(InsReg);
198 TII.copyRegToReg(*MBB, MI, DstSubReg, InsReg, TRC0, TRC1);
Dan Gohmana5b2fee2008-12-18 22:14:08 +0000199 // Transfer the kill/dead flags, if needed.
200 if (MI->getOperand(0).isDead())
201 TransferDeadFlag(MI, DstSubReg, TRI);
202 if (MI->getOperand(2).isKill())
203 TransferKillFlag(MI, InsReg, TRI);
Christopher Lambc9298232008-03-16 03:12:01 +0000204
205#ifndef NDEBUG
Dan Gohman08293f62008-08-20 13:50:12 +0000206 MachineBasicBlock::iterator dMI = MI;
207 DOUT << "subreg: " << *(--dMI);
Christopher Lambc9298232008-03-16 03:12:01 +0000208#endif
Dan Gohmane3d92062008-08-07 02:54:50 +0000209 }
Christopher Lambc9298232008-03-16 03:12:01 +0000210
211 DOUT << "\n";
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000212 MBB->erase(MI);
Christopher Lambc9298232008-03-16 03:12:01 +0000213 return true;
214}
Christopher Lamb98363222007-08-06 16:33:56 +0000215
216bool LowerSubregsInstructionPass::LowerInsert(MachineInstr *MI) {
217 MachineBasicBlock *MBB = MI->getParent();
218 MachineFunction &MF = *MBB->getParent();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000219 const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo();
Owen Andersond10fd972007-12-31 06:32:00 +0000220 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
Dan Gohmand735b802008-10-03 15:45:36 +0000221 assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
222 (MI->getOperand(1).isReg() && MI->getOperand(1).isUse()) &&
223 (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
224 MI->getOperand(3).isImm() && "Invalid insert_subreg");
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000225
226 unsigned DstReg = MI->getOperand(0).getReg();
Devang Patel59500c82008-11-21 20:00:59 +0000227#ifndef NDEBUG
Christopher Lambc9298232008-03-16 03:12:01 +0000228 unsigned SrcReg = MI->getOperand(1).getReg();
Devang Patel59500c82008-11-21 20:00:59 +0000229#endif
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000230 unsigned InsReg = MI->getOperand(2).getReg();
231 unsigned SubIdx = MI->getOperand(3).getImm();
Christopher Lamb98363222007-08-06 16:33:56 +0000232
Christopher Lambc9298232008-03-16 03:12:01 +0000233 assert(DstReg == SrcReg && "insert_subreg not a two-address instruction?");
234 assert(SubIdx != 0 && "Invalid index for insert_subreg");
Dan Gohman6f0d0242008-02-10 18:45:23 +0000235 unsigned DstSubReg = TRI.getSubReg(DstReg, SubIdx);
Jakob Stoklund Olesen980daea2009-08-03 20:08:18 +0000236 assert(DstSubReg && "invalid subregister index for register");
Dan Gohman6f0d0242008-02-10 18:45:23 +0000237 assert(TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
Christopher Lamb98363222007-08-06 16:33:56 +0000238 "Insert superreg source must be in a physical register");
Dan Gohman6f0d0242008-02-10 18:45:23 +0000239 assert(TargetRegisterInfo::isPhysicalRegister(InsReg) &&
Christopher Lamb98363222007-08-06 16:33:56 +0000240 "Inserted value must be in a physical register");
241
242 DOUT << "subreg: CONVERTING: " << *MI;
Christopher Lambc9298232008-03-16 03:12:01 +0000243
Evan Chengc3de8022008-06-16 22:52:53 +0000244 if (DstSubReg == InsReg) {
Jakob Stoklund Olesen980daea2009-08-03 20:08:18 +0000245 // No need to insert an identity copy instruction. If the SrcReg was
246 // <undef>, we need to make sure it is alive by inserting an IMPLICIT_DEF
247 if (MI->getOperand(1).isUndef() && !MI->getOperand(0).isDead()) {
248 BuildMI(*MBB, MI, MI->getDebugLoc(),
249 TII.get(TargetInstrInfo::IMPLICIT_DEF), DstReg)
250 .addReg(InsReg, RegState::ImplicitKill);
251 } else {
252 DOUT << "subreg: eliminated!\n";
253 MBB->erase(MI);
254 return true;
255 }
Evan Chengc3de8022008-06-16 22:52:53 +0000256 } else {
257 // Insert sub-register copy
258 const TargetRegisterClass *TRC0= TRI.getPhysicalRegisterRegClass(DstSubReg);
259 const TargetRegisterClass *TRC1= TRI.getPhysicalRegisterRegClass(InsReg);
260 TII.copyRegToReg(*MBB, MI, DstSubReg, InsReg, TRC0, TRC1);
Jakob Stoklund Olesen980daea2009-08-03 20:08:18 +0000261 MachineBasicBlock::iterator CopyMI = MI;
262 --CopyMI;
263
Dan Gohmana5b2fee2008-12-18 22:14:08 +0000264 // Transfer the kill/dead flags, if needed.
Jakob Stoklund Olesen980daea2009-08-03 20:08:18 +0000265 if (MI->getOperand(0).isDead()) {
Dan Gohmana5b2fee2008-12-18 22:14:08 +0000266 TransferDeadFlag(MI, DstSubReg, TRI);
Jakob Stoklund Olesen980daea2009-08-03 20:08:18 +0000267 // Also add a SrcReg<imp-kill> of the super register.
268 CopyMI->addOperand(MachineOperand::CreateReg(DstReg, false, true, true));
269 } else if (MI->getOperand(1).isUndef()) {
270 // If SrcReg was marked <undef> we must make sure it is alive after this
271 // replacement. Add a SrcReg<imp-def> operand.
272 CopyMI->addOperand(MachineOperand::CreateReg(DstReg, true, true));
273 }
274
275 // Make sure the inserted register gets killed
276 if (MI->getOperand(2).isKill())
Dan Gohmana5b2fee2008-12-18 22:14:08 +0000277 TransferKillFlag(MI, InsReg, TRI);
Jakob Stoklund Olesen980daea2009-08-03 20:08:18 +0000278 }
Dan Gohman98c20692008-12-18 22:11:34 +0000279
Christopher Lamb8b165732007-08-10 21:11:55 +0000280#ifndef NDEBUG
Jakob Stoklund Olesen980daea2009-08-03 20:08:18 +0000281 MachineBasicBlock::iterator dMI = MI;
282 DOUT << "subreg: " << *(--dMI);
Christopher Lamb8b165732007-08-10 21:11:55 +0000283#endif
Christopher Lamb98363222007-08-06 16:33:56 +0000284
285 DOUT << "\n";
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000286 MBB->erase(MI);
Christopher Lamb98363222007-08-06 16:33:56 +0000287 return true;
288}
Christopher Lambbab24742007-07-26 08:18:32 +0000289
290/// runOnMachineFunction - Reduce subregister inserts and extracts to register
291/// copies.
292///
293bool LowerSubregsInstructionPass::runOnMachineFunction(MachineFunction &MF) {
294 DOUT << "Machine Function\n";
Christopher Lambbab24742007-07-26 08:18:32 +0000295
296 bool MadeChange = false;
297
298 DOUT << "********** LOWERING SUBREG INSTRS **********\n";
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000299 DEBUG(errs() << "********** Function: "
300 << MF.getFunction()->getName() << '\n');
Christopher Lambbab24742007-07-26 08:18:32 +0000301
302 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
303 mbbi != mbbe; ++mbbi) {
304 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
Christopher Lamb98363222007-08-06 16:33:56 +0000305 mi != me;) {
306 MachineInstr *MI = mi++;
307
308 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
309 MadeChange |= LowerExtract(MI);
310 } else if (MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG) {
311 MadeChange |= LowerInsert(MI);
Christopher Lambc9298232008-03-16 03:12:01 +0000312 } else if (MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG) {
313 MadeChange |= LowerSubregToReg(MI);
Christopher Lambbab24742007-07-26 08:18:32 +0000314 }
315 }
316 }
317
318 return MadeChange;
319}