blob: ad1c537c1911a357d6b203d8727d8378109029a9 [file] [log] [blame]
Christopher Lamb10a95ba2007-07-26 08:18:32 +00001//===-- LowerSubregs.cpp - Subregister Lowering instruction pass ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-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 Lamb10a95ba2007-07-26 08:18:32 +00007//
8//===----------------------------------------------------------------------===//
Dan Gohmanc93e7072008-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 Lamb10a95ba2007-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 Olesenc1b906f2009-08-03 20:08:18 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner1b989192007-12-31 04:13:23 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000024#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb10a95ba2007-07-26 08:18:32 +000025#include "llvm/Target/TargetInstrInfo.h"
26#include "llvm/Target/TargetMachine.h"
27#include "llvm/Support/Debug.h"
Daniel Dunbar005975c2009-07-25 00:23:56 +000028#include "llvm/Support/raw_ostream.h"
Christopher Lamb10a95ba2007-07-26 08:18:32 +000029using namespace llvm;
30
31namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000032 struct LowerSubregsInstructionPass : public MachineFunctionPass {
Evan Cheng381f0972009-10-25 07:49:57 +000033 private:
34 const TargetRegisterInfo *TRI;
35 const TargetInstrInfo *TII;
36
37 public:
Christopher Lamb10a95ba2007-07-26 08:18:32 +000038 static char ID; // Pass identification, replacement for typeid
Owen Anderson75693222010-08-06 18:33:48 +000039 LowerSubregsInstructionPass() : MachineFunctionPass(ID) {}
Christopher Lamb10a95ba2007-07-26 08:18:32 +000040
41 const char *getPassName() const {
42 return "Subregister lowering instruction pass";
43 }
44
Evan Cheng465a66e2008-09-22 20:58:04 +000045 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmanecb436f2009-07-31 23:37:33 +000046 AU.setPreservesCFG();
Evan Cheng1b42ac12008-09-22 22:21:38 +000047 AU.addPreservedID(MachineLoopInfoID);
48 AU.addPreservedID(MachineDominatorsID);
Evan Cheng465a66e2008-09-22 20:58:04 +000049 MachineFunctionPass::getAnalysisUsage(AU);
50 }
51
Christopher Lamb10a95ba2007-07-26 08:18:32 +000052 /// runOnMachineFunction - pass entry point
53 bool runOnMachineFunction(MachineFunction&);
Evan Cheng381f0972009-10-25 07:49:57 +000054
55 private:
Christopher Lamb76d72da2008-03-16 03:12:01 +000056 bool LowerSubregToReg(MachineInstr *MI);
Jakob Stoklund Olesen6f754852010-07-02 22:29:50 +000057 bool LowerCopy(MachineInstr *MI);
Dan Gohman048d94a2008-12-18 22:14:08 +000058
59 void TransferDeadFlag(MachineInstr *MI, unsigned DstReg,
Evan Cheng381f0972009-10-25 07:49:57 +000060 const TargetRegisterInfo *TRI);
Bob Wilson73fe9442010-06-29 18:42:49 +000061 void TransferImplicitDefs(MachineInstr *MI);
Christopher Lamb10a95ba2007-07-26 08:18:32 +000062 };
63
64 char LowerSubregsInstructionPass::ID = 0;
65}
66
67FunctionPass *llvm::createLowerSubregsPass() {
68 return new LowerSubregsInstructionPass();
69}
70
Dan Gohman048d94a2008-12-18 22:14:08 +000071/// 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.
74void
75LowerSubregsInstructionPass::TransferDeadFlag(MachineInstr *MI,
76 unsigned DstReg,
Evan Cheng381f0972009-10-25 07:49:57 +000077 const TargetRegisterInfo *TRI) {
Dan Gohman048d94a2008-12-18 22:14:08 +000078 for (MachineBasicBlock::iterator MII =
79 prior(MachineBasicBlock::iterator(MI)); ; --MII) {
Evan Cheng381f0972009-10-25 07:49:57 +000080 if (MII->addRegisterDead(DstReg, TRI))
Dan Gohman048d94a2008-12-18 22:14:08 +000081 break;
82 assert(MII != MI->getParent()->begin() &&
Jakob Stoklund Olesen7eb196d2010-07-08 05:01:41 +000083 "copyPhysReg output doesn't reference destination register!");
Dan Gohman048d94a2008-12-18 22:14:08 +000084 }
85}
86
Bob Wilson73fe9442010-06-29 18:42:49 +000087/// 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.
90void
91LowerSubregsInstructionPass::TransferImplicitDefs(MachineInstr *MI) {
92 MachineBasicBlock::iterator CopyMI = MI;
93 --CopyMI;
94
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())
98 continue;
99 CopyMI->addOperand(MachineOperand::CreateReg(MO.getReg(), true, true));
100 }
101}
102
Christopher Lamb76d72da2008-03-16 03:12:01 +0000103bool LowerSubregsInstructionPass::LowerSubregToReg(MachineInstr *MI) {
104 MachineBasicBlock *MBB = MI->getParent();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000105 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");
Jakob Stoklund Olesena5b260b2010-06-22 22:11:07 +0000109
Christopher Lamb76d72da2008-03-16 03:12:01 +0000110 unsigned DstReg = MI->getOperand(0).getReg();
111 unsigned InsReg = MI->getOperand(2).getReg();
Jakob Stoklund Olesena5b260b2010-06-22 22:11:07 +0000112 assert(!MI->getOperand(2).getSubReg() && "SubIdx on physreg?");
Evan Chenge8746322009-03-23 07:19:58 +0000113 unsigned SubIdx = MI->getOperand(3).getImm();
Christopher Lamb76d72da2008-03-16 03:12:01 +0000114
115 assert(SubIdx != 0 && "Invalid index for insert_subreg");
Evan Cheng381f0972009-10-25 07:49:57 +0000116 unsigned DstSubReg = TRI->getSubReg(DstReg, SubIdx);
Evan Chenge8746322009-03-23 07:19:58 +0000117
Christopher Lamb76d72da2008-03-16 03:12:01 +0000118 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");
122
David Greene33e8afe2010-01-04 23:06:47 +0000123 DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
Christopher Lamb76d72da2008-03-16 03:12:01 +0000124
Jakob Stoklund Olesena5b260b2010-06-22 22:11:07 +0000125 if (DstSubReg == InsReg) {
Dan Gohman47a419d2008-08-07 02:54:50 +0000126 // No need to insert an identify copy instruction.
Evan Chenge8746322009-03-23 07:19:58 +0000127 // Watch out for case like this:
Jakob Stoklund Olesena5b260b2010-06-22 22:11:07 +0000128 // %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);
135 return true;
136 }
David Greene33e8afe2010-01-04 23:06:47 +0000137 DEBUG(dbgs() << "subreg: eliminated!");
Dan Gohman47a419d2008-08-07 02:54:50 +0000138 } else {
Jakob Stoklund Olesen7eb196d2010-07-08 05:01:41 +0000139 TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg,
140 MI->getOperand(2).isKill());
Dan Gohman048d94a2008-12-18 22:14:08 +0000141 // Transfer the kill/dead flags, if needed.
142 if (MI->getOperand(0).isDead())
143 TransferDeadFlag(MI, DstSubReg, TRI);
Bill Wendling22491a62009-08-22 20:23:49 +0000144 DEBUG({
145 MachineBasicBlock::iterator dMI = MI;
David Greene33e8afe2010-01-04 23:06:47 +0000146 dbgs() << "subreg: " << *(--dMI);
Bill Wendling22491a62009-08-22 20:23:49 +0000147 });
Dan Gohman47a419d2008-08-07 02:54:50 +0000148 }
Christopher Lamb76d72da2008-03-16 03:12:01 +0000149
David Greene33e8afe2010-01-04 23:06:47 +0000150 DEBUG(dbgs() << '\n');
Dan Gohman8b3b5172008-07-17 23:49:46 +0000151 MBB->erase(MI);
Anton Korobeynikov8e4c4272009-10-24 00:27:00 +0000152 return true;
Christopher Lamb76d72da2008-03-16 03:12:01 +0000153}
Christopher Lamba5bb7e42007-08-06 16:33:56 +0000154
Jakob Stoklund Olesen6f754852010-07-02 22:29:50 +0000155bool LowerSubregsInstructionPass::LowerCopy(MachineInstr *MI) {
156 MachineOperand &DstMO = MI->getOperand(0);
157 MachineOperand &SrcMO = MI->getOperand(1);
158
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);
168 return true;
169 }
170 // Vanilla identity copy.
171 MI->eraseFromParent();
172 return true;
173 }
174
175 DEBUG(dbgs() << "real copy: " << *MI);
Jakob Stoklund Olesen7eb196d2010-07-08 05:01:41 +0000176 TII->copyPhysReg(*MI->getParent(), MI, MI->getDebugLoc(),
177 DstMO.getReg(), SrcMO.getReg(), SrcMO.isKill());
Jakob Stoklund Olesen6f754852010-07-02 22:29:50 +0000178
179 if (DstMO.isDead())
180 TransferDeadFlag(MI, DstMO.getReg(), TRI);
Jakob Stoklund Olesen6f754852010-07-02 22:29:50 +0000181 if (MI->getNumOperands() > 2)
182 TransferImplicitDefs(MI);
183 DEBUG({
184 MachineBasicBlock::iterator dMI = MI;
185 dbgs() << "replaced by: " << *(--dMI);
186 });
187 MI->eraseFromParent();
188 return true;
189}
190
Christopher Lamb10a95ba2007-07-26 08:18:32 +0000191/// runOnMachineFunction - Reduce subregister inserts and extracts to register
192/// copies.
193///
194bool LowerSubregsInstructionPass::runOnMachineFunction(MachineFunction &MF) {
David Greene33e8afe2010-01-04 23:06:47 +0000195 DEBUG(dbgs() << "Machine Function\n"
Bill Wendling22491a62009-08-22 20:23:49 +0000196 << "********** LOWERING SUBREG INSTRS **********\n"
197 << "********** Function: "
198 << MF.getFunction()->getName() << '\n');
Evan Cheng381f0972009-10-25 07:49:57 +0000199 TRI = MF.getTarget().getRegisterInfo();
200 TII = MF.getTarget().getInstrInfo();
Christopher Lamb10a95ba2007-07-26 08:18:32 +0000201
Bill Wendling22491a62009-08-22 20:23:49 +0000202 bool MadeChange = false;
Christopher Lamb10a95ba2007-07-26 08:18:32 +0000203
204 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
205 mbbi != mbbe; ++mbbi) {
206 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
Christopher Lamba5bb7e42007-08-06 16:33:56 +0000207 mi != me;) {
Chris Lattnerb44b4292009-12-03 00:50:42 +0000208 MachineBasicBlock::iterator nmi = llvm::next(mi);
Evan Cheng381f0972009-10-25 07:49:57 +0000209 MachineInstr *MI = mi;
Jakob Stoklund Olesen68abc432010-07-08 16:40:15 +0000210 assert(!MI->isInsertSubreg() && "INSERT_SUBREG should no longer appear");
Jakob Stoklund Olesen4dc8a1e2010-07-08 16:40:22 +0000211 assert(MI->getOpcode() != TargetOpcode::EXTRACT_SUBREG &&
212 "EXTRACT_SUBREG should no longer appear");
213 if (MI->isSubregToReg()) {
Christopher Lamb76d72da2008-03-16 03:12:01 +0000214 MadeChange |= LowerSubregToReg(MI);
Jakob Stoklund Olesen6f754852010-07-02 22:29:50 +0000215 } else if (MI->isCopy()) {
216 MadeChange |= LowerCopy(MI);
Christopher Lamb10a95ba2007-07-26 08:18:32 +0000217 }
Evan Cheng381f0972009-10-25 07:49:57 +0000218 mi = nmi;
Christopher Lamb10a95ba2007-07-26 08:18:32 +0000219 }
220 }
221
222 return MadeChange;
223}