It's not legal to fold a load from a narrower stack slot into a wider instruction...
[llvm/avr.git] / lib / CodeGen / LowerSubregs.cpp
blob812d97d66c37133da4a9e4b4ae406b68cb762345
1 //===-- LowerSubregs.cpp - Subregister Lowering instruction pass ----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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/Compiler.h"
29 #include "llvm/Support/raw_ostream.h"
30 using namespace llvm;
32 namespace {
33 struct VISIBILITY_HIDDEN LowerSubregsInstructionPass
34 : public MachineFunctionPass {
35 static char ID; // Pass identification, replacement for typeid
36 LowerSubregsInstructionPass() : MachineFunctionPass(&ID) {}
38 const char *getPassName() const {
39 return "Subregister lowering instruction pass";
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.setPreservesCFG();
44 AU.addPreservedID(MachineLoopInfoID);
45 AU.addPreservedID(MachineDominatorsID);
46 MachineFunctionPass::getAnalysisUsage(AU);
49 /// runOnMachineFunction - pass entry point
50 bool runOnMachineFunction(MachineFunction&);
52 bool LowerExtract(MachineInstr *MI);
53 bool LowerInsert(MachineInstr *MI);
54 bool LowerSubregToReg(MachineInstr *MI);
56 void TransferDeadFlag(MachineInstr *MI, unsigned DstReg,
57 const TargetRegisterInfo &TRI);
58 void TransferKillFlag(MachineInstr *MI, unsigned SrcReg,
59 const TargetRegisterInfo &TRI,
60 bool AddIfNotFound = false);
63 char LowerSubregsInstructionPass::ID = 0;
66 FunctionPass *llvm::createLowerSubregsPass() {
67 return new LowerSubregsInstructionPass();
70 /// TransferDeadFlag - MI is a pseudo-instruction with DstReg dead,
71 /// and the lowered replacement instructions immediately precede it.
72 /// Mark the replacement instructions with the dead flag.
73 void
74 LowerSubregsInstructionPass::TransferDeadFlag(MachineInstr *MI,
75 unsigned DstReg,
76 const TargetRegisterInfo &TRI) {
77 for (MachineBasicBlock::iterator MII =
78 prior(MachineBasicBlock::iterator(MI)); ; --MII) {
79 if (MII->addRegisterDead(DstReg, &TRI))
80 break;
81 assert(MII != MI->getParent()->begin() &&
82 "copyRegToReg output doesn't reference destination register!");
86 /// TransferKillFlag - MI is a pseudo-instruction with SrcReg killed,
87 /// and the lowered replacement instructions immediately precede it.
88 /// Mark the replacement instructions with the kill flag.
89 void
90 LowerSubregsInstructionPass::TransferKillFlag(MachineInstr *MI,
91 unsigned SrcReg,
92 const TargetRegisterInfo &TRI,
93 bool AddIfNotFound) {
94 for (MachineBasicBlock::iterator MII =
95 prior(MachineBasicBlock::iterator(MI)); ; --MII) {
96 if (MII->addRegisterKilled(SrcReg, &TRI, AddIfNotFound))
97 break;
98 assert(MII != MI->getParent()->begin() &&
99 "copyRegToReg output doesn't reference source register!");
103 bool LowerSubregsInstructionPass::LowerExtract(MachineInstr *MI) {
104 MachineBasicBlock *MBB = MI->getParent();
105 MachineFunction &MF = *MBB->getParent();
106 const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo();
107 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
109 assert(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
110 MI->getOperand(1).isReg() && MI->getOperand(1).isUse() &&
111 MI->getOperand(2).isImm() && "Malformed extract_subreg");
113 unsigned DstReg = MI->getOperand(0).getReg();
114 unsigned SuperReg = MI->getOperand(1).getReg();
115 unsigned SubIdx = MI->getOperand(2).getImm();
116 unsigned SrcReg = TRI.getSubReg(SuperReg, SubIdx);
118 assert(TargetRegisterInfo::isPhysicalRegister(SuperReg) &&
119 "Extract supperg source must be a physical register");
120 assert(TargetRegisterInfo::isPhysicalRegister(DstReg) &&
121 "Extract destination must be in a physical register");
122 assert(SrcReg && "invalid subregister index for register");
124 DEBUG(errs() << "subreg: CONVERTING: " << *MI);
126 if (SrcReg == DstReg) {
127 // No need to insert an identity copy instruction.
128 if (MI->getOperand(1).isKill()) {
129 // We must make sure the super-register gets killed.Replace the
130 // instruction with IMPLICIT_DEF.
131 MI->setDesc(TII.get(TargetInstrInfo::IMPLICIT_DEF));
132 MI->RemoveOperand(2); // SubIdx
133 DEBUG(errs() << "subreg: replace by: " << *MI);
134 return true;
137 DEBUG(errs() << "subreg: eliminated!");
138 } else {
139 // Insert copy
140 const TargetRegisterClass *TRCS = TRI.getPhysicalRegisterRegClass(DstReg);
141 const TargetRegisterClass *TRCD = TRI.getPhysicalRegisterRegClass(SrcReg);
142 bool Emitted = TII.copyRegToReg(*MBB, MI, DstReg, SrcReg, TRCD, TRCS);
143 (void)Emitted;
144 assert(Emitted && "Subreg and Dst must be of compatible register class");
145 // Transfer the kill/dead flags, if needed.
146 if (MI->getOperand(0).isDead())
147 TransferDeadFlag(MI, DstReg, TRI);
148 if (MI->getOperand(1).isKill())
149 TransferKillFlag(MI, SuperReg, TRI, true);
150 DEBUG({
151 MachineBasicBlock::iterator dMI = MI;
152 errs() << "subreg: " << *(--dMI);
156 DEBUG(errs() << '\n');
157 MBB->erase(MI);
158 return true;
161 bool 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();
166 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");
171 unsigned DstReg = MI->getOperand(0).getReg();
172 unsigned InsReg = MI->getOperand(2).getReg();
173 unsigned InsSIdx = MI->getOperand(2).getSubReg();
174 unsigned SubIdx = MI->getOperand(3).getImm();
176 assert(SubIdx != 0 && "Invalid index for insert_subreg");
177 unsigned DstSubReg = TRI.getSubReg(DstReg, SubIdx);
179 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");
184 DEBUG(errs() << "subreg: CONVERTING: " << *MI);
186 if (DstSubReg == InsReg && InsSIdx == 0) {
187 // No need to insert an identify copy instruction.
188 // 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.
193 DEBUG(errs() << "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);
199 // 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);
204 DEBUG({
205 MachineBasicBlock::iterator dMI = MI;
206 errs() << "subreg: " << *(--dMI);
210 DEBUG(errs() << '\n');
211 MBB->erase(MI);
212 return true;
215 bool LowerSubregsInstructionPass::LowerInsert(MachineInstr *MI) {
216 MachineBasicBlock *MBB = MI->getParent();
217 MachineFunction &MF = *MBB->getParent();
218 const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo();
219 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
220 assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
221 (MI->getOperand(1).isReg() && MI->getOperand(1).isUse()) &&
222 (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
223 MI->getOperand(3).isImm() && "Invalid insert_subreg");
225 unsigned DstReg = MI->getOperand(0).getReg();
226 #ifndef NDEBUG
227 unsigned SrcReg = MI->getOperand(1).getReg();
228 #endif
229 unsigned InsReg = MI->getOperand(2).getReg();
230 unsigned SubIdx = MI->getOperand(3).getImm();
232 assert(DstReg == SrcReg && "insert_subreg not a two-address instruction?");
233 assert(SubIdx != 0 && "Invalid index for insert_subreg");
234 unsigned DstSubReg = TRI.getSubReg(DstReg, SubIdx);
235 assert(DstSubReg && "invalid subregister index for register");
236 assert(TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
237 "Insert superreg source must be in a physical register");
238 assert(TargetRegisterInfo::isPhysicalRegister(InsReg) &&
239 "Inserted value must be in a physical register");
241 DEBUG(errs() << "subreg: CONVERTING: " << *MI);
243 if (DstSubReg == InsReg) {
244 // No need to insert an identity copy instruction. If the SrcReg was
245 // <undef>, we need to make sure it is alive by inserting an IMPLICIT_DEF
246 if (MI->getOperand(1).isUndef() && !MI->getOperand(0).isDead()) {
247 MachineInstrBuilder MIB = BuildMI(*MBB, MI, MI->getDebugLoc(),
248 TII.get(TargetInstrInfo::IMPLICIT_DEF), DstReg);
249 if (MI->getOperand(2).isUndef())
250 MIB.addReg(InsReg, RegState::Implicit | RegState::Undef);
251 else
252 MIB.addReg(InsReg, RegState::ImplicitKill);
253 } else {
254 DEBUG(errs() << "subreg: eliminated!\n");
255 MBB->erase(MI);
256 return true;
258 } else {
259 // Insert sub-register copy
260 const TargetRegisterClass *TRC0= TRI.getPhysicalRegisterRegClass(DstSubReg);
261 const TargetRegisterClass *TRC1= TRI.getPhysicalRegisterRegClass(InsReg);
262 if (MI->getOperand(2).isUndef())
263 // If the source register being inserted is undef, then this becomes an
264 // implicit_def.
265 BuildMI(*MBB, MI, MI->getDebugLoc(),
266 TII.get(TargetInstrInfo::IMPLICIT_DEF), DstSubReg);
267 else
268 TII.copyRegToReg(*MBB, MI, DstSubReg, InsReg, TRC0, TRC1);
269 MachineBasicBlock::iterator CopyMI = MI;
270 --CopyMI;
272 // INSERT_SUBREG is a two-address instruction so it implicitly kills SrcReg.
273 if (!MI->getOperand(1).isUndef())
274 CopyMI->addOperand(MachineOperand::CreateReg(DstReg, false, true, true));
276 // Transfer the kill/dead flags, if needed.
277 if (MI->getOperand(0).isDead()) {
278 TransferDeadFlag(MI, DstSubReg, TRI);
279 } else {
280 // Make sure the full DstReg is live after this replacement.
281 CopyMI->addOperand(MachineOperand::CreateReg(DstReg, true, true));
284 // Make sure the inserted register gets killed
285 if (MI->getOperand(2).isKill() && !MI->getOperand(2).isUndef())
286 TransferKillFlag(MI, InsReg, TRI);
289 DEBUG({
290 MachineBasicBlock::iterator dMI = MI;
291 errs() << "subreg: " << *(--dMI) << "\n";
294 MBB->erase(MI);
295 return true;
298 /// runOnMachineFunction - Reduce subregister inserts and extracts to register
299 /// copies.
301 bool LowerSubregsInstructionPass::runOnMachineFunction(MachineFunction &MF) {
302 DEBUG(errs() << "Machine Function\n"
303 << "********** LOWERING SUBREG INSTRS **********\n"
304 << "********** Function: "
305 << MF.getFunction()->getName() << '\n');
307 bool MadeChange = false;
309 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
310 mbbi != mbbe; ++mbbi) {
311 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
312 mi != me;) {
313 MachineInstr *MI = mi++;
315 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
316 MadeChange |= LowerExtract(MI);
317 } else if (MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG) {
318 MadeChange |= LowerInsert(MI);
319 } else if (MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG) {
320 MadeChange |= LowerSubregToReg(MI);
325 return MadeChange;