blob: 78382f548cbf73d0977da2000f2443bd654a70c7 [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 const char *getPassName() const {
40 return "Post-RA pseudo instruction expansion pass";
41 }
Christopher Lambbab24742007-07-26 08:18:32 +000042
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000043 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
44 AU.setPreservesCFG();
45 AU.addPreservedID(MachineLoopInfoID);
46 AU.addPreservedID(MachineDominatorsID);
47 MachineFunctionPass::getAnalysisUsage(AU);
48 }
Evan Chengbbeeb2a2008-09-22 20:58:04 +000049
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000050 /// runOnMachineFunction - pass entry point
51 bool runOnMachineFunction(MachineFunction&);
Evan Chengd98e30f2009-10-25 07:49:57 +000052
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000053private:
54 bool LowerSubregToReg(MachineInstr *MI);
55 bool LowerCopy(MachineInstr *MI);
Dan Gohmana5b2fee2008-12-18 22:14:08 +000056
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000057 void TransferDeadFlag(MachineInstr *MI, unsigned DstReg,
58 const TargetRegisterInfo *TRI);
59 void TransferImplicitDefs(MachineInstr *MI);
60};
61} // end anonymous namespace
Christopher Lambbab24742007-07-26 08:18:32 +000062
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000063char ExpandPostRA::ID = 0;
Christopher Lambbab24742007-07-26 08:18:32 +000064
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000065FunctionPass *llvm::createExpandPostRAPseudosPass() {
66 return new ExpandPostRA();
Christopher Lambbab24742007-07-26 08:18:32 +000067}
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
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000073ExpandPostRA::TransferDeadFlag(MachineInstr *MI, unsigned DstReg,
74 const TargetRegisterInfo *TRI) {
Dan Gohmana5b2fee2008-12-18 22:14:08 +000075 for (MachineBasicBlock::iterator MII =
76 prior(MachineBasicBlock::iterator(MI)); ; --MII) {
Evan Chengd98e30f2009-10-25 07:49:57 +000077 if (MII->addRegisterDead(DstReg, TRI))
Dan Gohmana5b2fee2008-12-18 22:14:08 +000078 break;
79 assert(MII != MI->getParent()->begin() &&
Jakob Stoklund Olesen3651d922010-07-08 05:01:41 +000080 "copyPhysReg output doesn't reference destination register!");
Dan Gohmana5b2fee2008-12-18 22:14:08 +000081 }
82}
83
Bob Wilson5d521652010-06-29 18:42:49 +000084/// TransferImplicitDefs - MI is a pseudo-instruction, and the lowered
85/// replacement instructions immediately precede it. Copy any implicit-def
86/// operands from MI to the replacement instruction.
87void
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +000088ExpandPostRA::TransferImplicitDefs(MachineInstr *MI) {
Bob Wilson5d521652010-06-29 18:42:49 +000089 MachineBasicBlock::iterator CopyMI = MI;
90 --CopyMI;
91
92 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
93 MachineOperand &MO = MI->getOperand(i);
94 if (!MO.isReg() || !MO.isImplicit() || MO.isUse())
95 continue;
96 CopyMI->addOperand(MachineOperand::CreateReg(MO.getReg(), true, true));
97 }
98}
99
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +0000100bool ExpandPostRA::LowerSubregToReg(MachineInstr *MI) {
Christopher Lambc9298232008-03-16 03:12:01 +0000101 MachineBasicBlock *MBB = MI->getParent();
Dan Gohmand735b802008-10-03 15:45:36 +0000102 assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
103 MI->getOperand(1).isImm() &&
104 (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
105 MI->getOperand(3).isImm() && "Invalid subreg_to_reg");
Jakob Stoklund Olesenf175c5c2010-06-22 22:11:07 +0000106
Christopher Lambc9298232008-03-16 03:12:01 +0000107 unsigned DstReg = MI->getOperand(0).getReg();
108 unsigned InsReg = MI->getOperand(2).getReg();
Jakob Stoklund Olesenf175c5c2010-06-22 22:11:07 +0000109 assert(!MI->getOperand(2).getSubReg() && "SubIdx on physreg?");
Evan Cheng7d6d4b32009-03-23 07:19:58 +0000110 unsigned SubIdx = MI->getOperand(3).getImm();
Christopher Lambc9298232008-03-16 03:12:01 +0000111
112 assert(SubIdx != 0 && "Invalid index for insert_subreg");
Evan Chengd98e30f2009-10-25 07:49:57 +0000113 unsigned DstSubReg = TRI->getSubReg(DstReg, SubIdx);
Evan Cheng7d6d4b32009-03-23 07:19:58 +0000114
Christopher Lambc9298232008-03-16 03:12:01 +0000115 assert(TargetRegisterInfo::isPhysicalRegister(DstReg) &&
116 "Insert destination must be in a physical register");
117 assert(TargetRegisterInfo::isPhysicalRegister(InsReg) &&
118 "Inserted value must be in a physical register");
119
David Greene6d206f82010-01-04 23:06:47 +0000120 DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
Christopher Lambc9298232008-03-16 03:12:01 +0000121
Jakob Stoklund Olesenf175c5c2010-06-22 22:11:07 +0000122 if (DstSubReg == InsReg) {
Dan Gohmane3d92062008-08-07 02:54:50 +0000123 // No need to insert an identify copy instruction.
Evan Cheng7d6d4b32009-03-23 07:19:58 +0000124 // Watch out for case like this:
Jakob Stoklund Olesenf175c5c2010-06-22 22:11:07 +0000125 // %RAX<def> = SUBREG_TO_REG 0, %EAX<kill>, 3
126 // We must leave %RAX live.
127 if (DstReg != InsReg) {
128 MI->setDesc(TII->get(TargetOpcode::KILL));
129 MI->RemoveOperand(3); // SubIdx
130 MI->RemoveOperand(1); // Imm
131 DEBUG(dbgs() << "subreg: replace by: " << *MI);
132 return true;
133 }
David Greene6d206f82010-01-04 23:06:47 +0000134 DEBUG(dbgs() << "subreg: eliminated!");
Dan Gohmane3d92062008-08-07 02:54:50 +0000135 } else {
Jakob Stoklund Olesen3651d922010-07-08 05:01:41 +0000136 TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg,
137 MI->getOperand(2).isKill());
Dan Gohmana5b2fee2008-12-18 22:14:08 +0000138 // Transfer the kill/dead flags, if needed.
139 if (MI->getOperand(0).isDead())
140 TransferDeadFlag(MI, DstSubReg, TRI);
Bill Wendling0d6b1b12009-08-22 20:23:49 +0000141 DEBUG({
142 MachineBasicBlock::iterator dMI = MI;
David Greene6d206f82010-01-04 23:06:47 +0000143 dbgs() << "subreg: " << *(--dMI);
Bill Wendling0d6b1b12009-08-22 20:23:49 +0000144 });
Dan Gohmane3d92062008-08-07 02:54:50 +0000145 }
Christopher Lambc9298232008-03-16 03:12:01 +0000146
David Greene6d206f82010-01-04 23:06:47 +0000147 DEBUG(dbgs() << '\n');
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000148 MBB->erase(MI);
Anton Korobeynikovefcd89a2009-10-24 00:27:00 +0000149 return true;
Christopher Lambc9298232008-03-16 03:12:01 +0000150}
Christopher Lamb98363222007-08-06 16:33:56 +0000151
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +0000152bool ExpandPostRA::LowerCopy(MachineInstr *MI) {
Jakob Stoklund Olesena4e1ba52010-07-02 22:29:50 +0000153 MachineOperand &DstMO = MI->getOperand(0);
154 MachineOperand &SrcMO = MI->getOperand(1);
155
156 if (SrcMO.getReg() == DstMO.getReg()) {
157 DEBUG(dbgs() << "identity copy: " << *MI);
158 // No need to insert an identity copy instruction, but replace with a KILL
159 // if liveness is changed.
160 if (DstMO.isDead() || SrcMO.isUndef() || MI->getNumOperands() > 2) {
161 // We must make sure the super-register gets killed. Replace the
162 // instruction with KILL.
163 MI->setDesc(TII->get(TargetOpcode::KILL));
164 DEBUG(dbgs() << "replaced by: " << *MI);
165 return true;
166 }
167 // Vanilla identity copy.
168 MI->eraseFromParent();
169 return true;
170 }
171
172 DEBUG(dbgs() << "real copy: " << *MI);
Jakob Stoklund Olesen3651d922010-07-08 05:01:41 +0000173 TII->copyPhysReg(*MI->getParent(), MI, MI->getDebugLoc(),
174 DstMO.getReg(), SrcMO.getReg(), SrcMO.isKill());
Jakob Stoklund Olesena4e1ba52010-07-02 22:29:50 +0000175
176 if (DstMO.isDead())
177 TransferDeadFlag(MI, DstMO.getReg(), TRI);
Jakob Stoklund Olesena4e1ba52010-07-02 22:29:50 +0000178 if (MI->getNumOperands() > 2)
179 TransferImplicitDefs(MI);
180 DEBUG({
181 MachineBasicBlock::iterator dMI = MI;
182 dbgs() << "replaced by: " << *(--dMI);
183 });
184 MI->eraseFromParent();
185 return true;
186}
187
Christopher Lambbab24742007-07-26 08:18:32 +0000188/// runOnMachineFunction - Reduce subregister inserts and extracts to register
189/// copies.
190///
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +0000191bool ExpandPostRA::runOnMachineFunction(MachineFunction &MF) {
Jim Grosbach08da6362011-02-25 22:53:20 +0000192 DEBUG(dbgs() << "Machine Function\n"
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +0000193 << "********** EXPANDING POST-RA PSEUDO INSTRS **********\n"
Jim Grosbach08da6362011-02-25 22:53:20 +0000194 << "********** Function: "
Bill Wendling0d6b1b12009-08-22 20:23:49 +0000195 << MF.getFunction()->getName() << '\n');
Evan Chengd98e30f2009-10-25 07:49:57 +0000196 TRI = MF.getTarget().getRegisterInfo();
197 TII = MF.getTarget().getInstrInfo();
Christopher Lambbab24742007-07-26 08:18:32 +0000198
Bill Wendling0d6b1b12009-08-22 20:23:49 +0000199 bool MadeChange = false;
Christopher Lambbab24742007-07-26 08:18:32 +0000200
201 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
202 mbbi != mbbe; ++mbbi) {
203 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
Christopher Lamb98363222007-08-06 16:33:56 +0000204 mi != me;) {
Chris Lattner7896c9f2009-12-03 00:50:42 +0000205 MachineBasicBlock::iterator nmi = llvm::next(mi);
Evan Chengd98e30f2009-10-25 07:49:57 +0000206 MachineInstr *MI = mi;
Jakob Stoklund Olesen5c00e072010-07-08 16:40:15 +0000207 assert(!MI->isInsertSubreg() && "INSERT_SUBREG should no longer appear");
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +0000208 assert(MI->getOpcode() != TargetOpcode::EXTRACT_SUBREG &&
209 "EXTRACT_SUBREG should no longer appear");
210 if (MI->isSubregToReg()) {
Christopher Lambc9298232008-03-16 03:12:01 +0000211 MadeChange |= LowerSubregToReg(MI);
Jakob Stoklund Olesena4e1ba52010-07-02 22:29:50 +0000212 } else if (MI->isCopy()) {
213 MadeChange |= LowerCopy(MI);
Christopher Lambbab24742007-07-26 08:18:32 +0000214 }
Evan Chengd98e30f2009-10-25 07:49:57 +0000215 mi = nmi;
Christopher Lambbab24742007-07-26 08:18:32 +0000216 }
217 }
218
219 return MadeChange;
220}