blob: 7a17331ba1d621e4747b402202c6bd993786a8d7 [file] [log] [blame]
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +00001//===-- ExpandPostRAPseudos.cpp - Pseudo instruction expansion pass -------===//
Christopher Lambbab24742007-07-26 08:18:32 +00002//
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//
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000010// This file defines a pass that expands COPY and SUBREG_TO_REG pseudo
11// instructions after register allocation.
Dan Gohmanbd0f1442008-09-24 23:44:12 +000012//
13//===----------------------------------------------------------------------===//
Christopher Lambbab24742007-07-26 08:18:32 +000014
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000015#define DEBUG_TYPE "postrapseudos"
Christopher Lambbab24742007-07-26 08:18:32 +000016#include "llvm/CodeGen/Passes.h"
17#include "llvm/Function.h"
18#include "llvm/CodeGen/MachineFunctionPass.h"
19#include "llvm/CodeGen/MachineInstr.h"
Jakob Stoklund Olesen980daea2009-08-03 20:08:18 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000022#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lambbab24742007-07-26 08:18:32 +000023#include "llvm/Target/TargetInstrInfo.h"
24#include "llvm/Target/TargetMachine.h"
25#include "llvm/Support/Debug.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000026#include "llvm/Support/raw_ostream.h"
Christopher Lambbab24742007-07-26 08:18:32 +000027using namespace llvm;
28
29namespace {
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000030struct ExpandPostRA : public MachineFunctionPass {
31private:
32 const TargetRegisterInfo *TRI;
33 const TargetInstrInfo *TII;
Evan Chengd98e30f2009-10-25 07:49:57 +000034
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000035public:
36 static char ID; // Pass identification, replacement for typeid
37 ExpandPostRA() : MachineFunctionPass(ID) {}
Jim Grosbach08da6362011-02-25 22:53:20 +000038
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000039 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.setPreservesCFG();
41 AU.addPreservedID(MachineLoopInfoID);
42 AU.addPreservedID(MachineDominatorsID);
43 MachineFunctionPass::getAnalysisUsage(AU);
44 }
Evan Chengbbeeb2a2008-09-22 20:58:04 +000045
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000046 /// runOnMachineFunction - pass entry point
47 bool runOnMachineFunction(MachineFunction&);
Evan Chengd98e30f2009-10-25 07:49:57 +000048
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000049private:
50 bool LowerSubregToReg(MachineInstr *MI);
51 bool LowerCopy(MachineInstr *MI);
Dan Gohmana5b2fee2008-12-18 22:14:08 +000052
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000053 void TransferDeadFlag(MachineInstr *MI, unsigned DstReg,
54 const TargetRegisterInfo *TRI);
55 void TransferImplicitDefs(MachineInstr *MI);
56};
57} // end anonymous namespace
Christopher Lambbab24742007-07-26 08:18:32 +000058
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000059char ExpandPostRA::ID = 0;
Andrew Trick1dd8c852012-02-08 21:23:13 +000060char &llvm::ExpandPostRAPseudosID = ExpandPostRA::ID;
Christopher Lambbab24742007-07-26 08:18:32 +000061
Andrew Trick1dd8c852012-02-08 21:23:13 +000062INITIALIZE_PASS(ExpandPostRA, "postrapseudos",
63 "Post-RA pseudo instruction expansion pass", false, false)
Christopher Lambbab24742007-07-26 08:18:32 +000064
Dan Gohmana5b2fee2008-12-18 22:14:08 +000065/// TransferDeadFlag - MI is a pseudo-instruction with DstReg dead,
66/// and the lowered replacement instructions immediately precede it.
67/// Mark the replacement instructions with the dead flag.
68void
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000069ExpandPostRA::TransferDeadFlag(MachineInstr *MI, unsigned DstReg,
70 const TargetRegisterInfo *TRI) {
Dan Gohmana5b2fee2008-12-18 22:14:08 +000071 for (MachineBasicBlock::iterator MII =
72 prior(MachineBasicBlock::iterator(MI)); ; --MII) {
Evan Chengd98e30f2009-10-25 07:49:57 +000073 if (MII->addRegisterDead(DstReg, TRI))
Dan Gohmana5b2fee2008-12-18 22:14:08 +000074 break;
75 assert(MII != MI->getParent()->begin() &&
Jakob Stoklund Olesen3651d922010-07-08 05:01:41 +000076 "copyPhysReg output doesn't reference destination register!");
Dan Gohmana5b2fee2008-12-18 22:14:08 +000077 }
78}
79
Bob Wilson5d521652010-06-29 18:42:49 +000080/// TransferImplicitDefs - MI is a pseudo-instruction, and the lowered
81/// replacement instructions immediately precede it. Copy any implicit-def
82/// operands from MI to the replacement instruction.
83void
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000084ExpandPostRA::TransferImplicitDefs(MachineInstr *MI) {
Bob Wilson5d521652010-06-29 18:42:49 +000085 MachineBasicBlock::iterator CopyMI = MI;
86 --CopyMI;
87
88 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
89 MachineOperand &MO = MI->getOperand(i);
90 if (!MO.isReg() || !MO.isImplicit() || MO.isUse())
91 continue;
92 CopyMI->addOperand(MachineOperand::CreateReg(MO.getReg(), true, true));
93 }
94}
95
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000096bool ExpandPostRA::LowerSubregToReg(MachineInstr *MI) {
Christopher Lambc9298232008-03-16 03:12:01 +000097 MachineBasicBlock *MBB = MI->getParent();
Dan Gohmand735b802008-10-03 15:45:36 +000098 assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
99 MI->getOperand(1).isImm() &&
100 (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
101 MI->getOperand(3).isImm() && "Invalid subreg_to_reg");
Jakob Stoklund Olesenf175c5c2010-06-22 22:11:07 +0000102
Christopher Lambc9298232008-03-16 03:12:01 +0000103 unsigned DstReg = MI->getOperand(0).getReg();
104 unsigned InsReg = MI->getOperand(2).getReg();
Jakob Stoklund Olesenf175c5c2010-06-22 22:11:07 +0000105 assert(!MI->getOperand(2).getSubReg() && "SubIdx on physreg?");
Evan Cheng7d6d4b32009-03-23 07:19:58 +0000106 unsigned SubIdx = MI->getOperand(3).getImm();
Christopher Lambc9298232008-03-16 03:12:01 +0000107
108 assert(SubIdx != 0 && "Invalid index for insert_subreg");
Evan Chengd98e30f2009-10-25 07:49:57 +0000109 unsigned DstSubReg = TRI->getSubReg(DstReg, SubIdx);
Evan Cheng7d6d4b32009-03-23 07:19:58 +0000110
Christopher Lambc9298232008-03-16 03:12:01 +0000111 assert(TargetRegisterInfo::isPhysicalRegister(DstReg) &&
112 "Insert destination must be in a physical register");
113 assert(TargetRegisterInfo::isPhysicalRegister(InsReg) &&
114 "Inserted value must be in a physical register");
115
David Greene6d206f82010-01-04 23:06:47 +0000116 DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
Christopher Lambc9298232008-03-16 03:12:01 +0000117
Jakob Stoklund Olesenf175c5c2010-06-22 22:11:07 +0000118 if (DstSubReg == InsReg) {
Dan Gohmane3d92062008-08-07 02:54:50 +0000119 // No need to insert an identify copy instruction.
Evan Cheng7d6d4b32009-03-23 07:19:58 +0000120 // Watch out for case like this:
Jakob Stoklund Olesenf175c5c2010-06-22 22:11:07 +0000121 // %RAX<def> = SUBREG_TO_REG 0, %EAX<kill>, 3
122 // We must leave %RAX live.
123 if (DstReg != InsReg) {
124 MI->setDesc(TII->get(TargetOpcode::KILL));
125 MI->RemoveOperand(3); // SubIdx
126 MI->RemoveOperand(1); // Imm
127 DEBUG(dbgs() << "subreg: replace by: " << *MI);
128 return true;
129 }
David Greene6d206f82010-01-04 23:06:47 +0000130 DEBUG(dbgs() << "subreg: eliminated!");
Dan Gohmane3d92062008-08-07 02:54:50 +0000131 } else {
Jakob Stoklund Olesen3651d922010-07-08 05:01:41 +0000132 TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg,
133 MI->getOperand(2).isKill());
Jakob Stoklund Olesen72e7dbf2012-07-27 20:19:49 +0000134
135 // Implicitly define DstReg for subsequent uses.
136 MachineBasicBlock::iterator CopyMI = MI;
137 --CopyMI;
138 CopyMI->addRegisterDefined(DstReg);
139
Dan Gohmana5b2fee2008-12-18 22:14:08 +0000140 // Transfer the kill/dead flags, if needed.
141 if (MI->getOperand(0).isDead())
142 TransferDeadFlag(MI, DstSubReg, TRI);
Jakob Stoklund Olesen72e7dbf2012-07-27 20:19:49 +0000143 DEBUG(dbgs() << "subreg: " << *CopyMI);
Dan Gohmane3d92062008-08-07 02:54:50 +0000144 }
Christopher Lambc9298232008-03-16 03:12:01 +0000145
David Greene6d206f82010-01-04 23:06:47 +0000146 DEBUG(dbgs() << '\n');
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000147 MBB->erase(MI);
Anton Korobeynikovefcd89a2009-10-24 00:27:00 +0000148 return true;
Christopher Lambc9298232008-03-16 03:12:01 +0000149}
Christopher Lamb98363222007-08-06 16:33:56 +0000150
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +0000151bool ExpandPostRA::LowerCopy(MachineInstr *MI) {
Jakob Stoklund Olesena4e1ba52010-07-02 22:29:50 +0000152 MachineOperand &DstMO = MI->getOperand(0);
153 MachineOperand &SrcMO = MI->getOperand(1);
154
155 if (SrcMO.getReg() == DstMO.getReg()) {
156 DEBUG(dbgs() << "identity copy: " << *MI);
157 // No need to insert an identity copy instruction, but replace with a KILL
158 // if liveness is changed.
159 if (DstMO.isDead() || SrcMO.isUndef() || MI->getNumOperands() > 2) {
160 // We must make sure the super-register gets killed. Replace the
161 // instruction with KILL.
162 MI->setDesc(TII->get(TargetOpcode::KILL));
163 DEBUG(dbgs() << "replaced by: " << *MI);
164 return true;
165 }
166 // Vanilla identity copy.
167 MI->eraseFromParent();
168 return true;
169 }
170
171 DEBUG(dbgs() << "real copy: " << *MI);
Jakob Stoklund Olesen3651d922010-07-08 05:01:41 +0000172 TII->copyPhysReg(*MI->getParent(), MI, MI->getDebugLoc(),
173 DstMO.getReg(), SrcMO.getReg(), SrcMO.isKill());
Jakob Stoklund Olesena4e1ba52010-07-02 22:29:50 +0000174
175 if (DstMO.isDead())
176 TransferDeadFlag(MI, DstMO.getReg(), TRI);
Jakob Stoklund Olesena4e1ba52010-07-02 22:29:50 +0000177 if (MI->getNumOperands() > 2)
178 TransferImplicitDefs(MI);
179 DEBUG({
180 MachineBasicBlock::iterator dMI = MI;
181 dbgs() << "replaced by: " << *(--dMI);
182 });
183 MI->eraseFromParent();
184 return true;
185}
186
Christopher Lambbab24742007-07-26 08:18:32 +0000187/// runOnMachineFunction - Reduce subregister inserts and extracts to register
188/// copies.
189///
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +0000190bool ExpandPostRA::runOnMachineFunction(MachineFunction &MF) {
Jim Grosbach08da6362011-02-25 22:53:20 +0000191 DEBUG(dbgs() << "Machine Function\n"
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +0000192 << "********** EXPANDING POST-RA PSEUDO INSTRS **********\n"
Jim Grosbach08da6362011-02-25 22:53:20 +0000193 << "********** Function: "
Bill Wendling0d6b1b12009-08-22 20:23:49 +0000194 << MF.getFunction()->getName() << '\n');
Evan Chengd98e30f2009-10-25 07:49:57 +0000195 TRI = MF.getTarget().getRegisterInfo();
196 TII = MF.getTarget().getInstrInfo();
Christopher Lambbab24742007-07-26 08:18:32 +0000197
Bill Wendling0d6b1b12009-08-22 20:23:49 +0000198 bool MadeChange = false;
Christopher Lambbab24742007-07-26 08:18:32 +0000199
200 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
201 mbbi != mbbe; ++mbbi) {
202 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
Christopher Lamb98363222007-08-06 16:33:56 +0000203 mi != me;) {
Evan Chengd98e30f2009-10-25 07:49:57 +0000204 MachineInstr *MI = mi;
Jakob Stoklund Olesenc291e2f2011-09-25 19:21:35 +0000205 // Advance iterator here because MI may be erased.
206 ++mi;
Jakob Stoklund Olesen735fe0f2011-10-10 20:34:28 +0000207
208 // Only expand pseudos.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000209 if (!MI->isPseudo())
Jakob Stoklund Olesen735fe0f2011-10-10 20:34:28 +0000210 continue;
211
212 // Give targets a chance to expand even standard pseudos.
213 if (TII->expandPostRAPseudo(MI)) {
214 MadeChange = true;
215 continue;
216 }
217
218 // Expand standard pseudos.
Jakob Stoklund Olesenc291e2f2011-09-25 19:21:35 +0000219 switch (MI->getOpcode()) {
220 case TargetOpcode::SUBREG_TO_REG:
Christopher Lambc9298232008-03-16 03:12:01 +0000221 MadeChange |= LowerSubregToReg(MI);
Jakob Stoklund Olesenc291e2f2011-09-25 19:21:35 +0000222 break;
223 case TargetOpcode::COPY:
Jakob Stoklund Olesena4e1ba52010-07-02 22:29:50 +0000224 MadeChange |= LowerCopy(MI);
Jakob Stoklund Olesenc291e2f2011-09-25 19:21:35 +0000225 break;
226 case TargetOpcode::DBG_VALUE:
227 continue;
228 case TargetOpcode::INSERT_SUBREG:
229 case TargetOpcode::EXTRACT_SUBREG:
230 llvm_unreachable("Sub-register pseudos should have been eliminated.");
Christopher Lambbab24742007-07-26 08:18:32 +0000231 }
Christopher Lambbab24742007-07-26 08:18:32 +0000232 }
233 }
234
235 return MadeChange;
236}