Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 1 | //===-- LowerSubregs.cpp - Subregister Lowering instruction pass ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Dan Gohman | bd0f144 | 2008-09-24 23:44:12 +0000 | [diff] [blame] | 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. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 16 | |
| 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 Olesen | 980daea | 2009-08-03 20:08:18 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetRegisterInfo.h" |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetInstrInfo.h" |
| 26 | #include "llvm/Target/TargetMachine.h" |
| 27 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
| 31 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 32 | struct LowerSubregsInstructionPass : public MachineFunctionPass { |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 33 | private: |
| 34 | const TargetRegisterInfo *TRI; |
| 35 | const TargetInstrInfo *TII; |
| 36 | |
| 37 | public: |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 38 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 1f74590 | 2010-08-06 00:23:35 +0000 | [diff] [blame^] | 39 | LowerSubregsInstructionPass() : MachineFunctionPass(&ID) {} |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 40 | |
| 41 | const char *getPassName() const { |
| 42 | return "Subregister lowering instruction pass"; |
| 43 | } |
| 44 | |
Evan Cheng | bbeeb2a | 2008-09-22 20:58:04 +0000 | [diff] [blame] | 45 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 845012e | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 46 | AU.setPreservesCFG(); |
Evan Cheng | 8b56a90 | 2008-09-22 22:21:38 +0000 | [diff] [blame] | 47 | AU.addPreservedID(MachineLoopInfoID); |
| 48 | AU.addPreservedID(MachineDominatorsID); |
Evan Cheng | bbeeb2a | 2008-09-22 20:58:04 +0000 | [diff] [blame] | 49 | MachineFunctionPass::getAnalysisUsage(AU); |
| 50 | } |
| 51 | |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 52 | /// runOnMachineFunction - pass entry point |
| 53 | bool runOnMachineFunction(MachineFunction&); |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 54 | |
| 55 | private: |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 56 | bool LowerSubregToReg(MachineInstr *MI); |
Jakob Stoklund Olesen | a4e1ba5 | 2010-07-02 22:29:50 +0000 | [diff] [blame] | 57 | bool LowerCopy(MachineInstr *MI); |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 58 | |
| 59 | void TransferDeadFlag(MachineInstr *MI, unsigned DstReg, |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 60 | const TargetRegisterInfo *TRI); |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 61 | void TransferKillFlag(MachineInstr *MI, unsigned SrcReg, |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 62 | const TargetRegisterInfo *TRI, |
Evan Cheng | b018a1e | 2009-08-05 02:25:11 +0000 | [diff] [blame] | 63 | bool AddIfNotFound = false); |
Bob Wilson | 5d52165 | 2010-06-29 18:42:49 +0000 | [diff] [blame] | 64 | void TransferImplicitDefs(MachineInstr *MI); |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | char LowerSubregsInstructionPass::ID = 0; |
| 68 | } |
| 69 | |
| 70 | FunctionPass *llvm::createLowerSubregsPass() { |
| 71 | return new LowerSubregsInstructionPass(); |
| 72 | } |
| 73 | |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 74 | /// TransferDeadFlag - MI is a pseudo-instruction with DstReg dead, |
| 75 | /// and the lowered replacement instructions immediately precede it. |
| 76 | /// Mark the replacement instructions with the dead flag. |
| 77 | void |
| 78 | LowerSubregsInstructionPass::TransferDeadFlag(MachineInstr *MI, |
| 79 | unsigned DstReg, |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 80 | const TargetRegisterInfo *TRI) { |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 81 | for (MachineBasicBlock::iterator MII = |
| 82 | prior(MachineBasicBlock::iterator(MI)); ; --MII) { |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 83 | if (MII->addRegisterDead(DstReg, TRI)) |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 84 | break; |
| 85 | assert(MII != MI->getParent()->begin() && |
Jakob Stoklund Olesen | 3651d92 | 2010-07-08 05:01:41 +0000 | [diff] [blame] | 86 | "copyPhysReg output doesn't reference destination register!"); |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | /// TransferKillFlag - MI is a pseudo-instruction with SrcReg killed, |
| 91 | /// and the lowered replacement instructions immediately precede it. |
| 92 | /// Mark the replacement instructions with the kill flag. |
| 93 | void |
| 94 | LowerSubregsInstructionPass::TransferKillFlag(MachineInstr *MI, |
| 95 | unsigned SrcReg, |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 96 | const TargetRegisterInfo *TRI, |
Evan Cheng | b018a1e | 2009-08-05 02:25:11 +0000 | [diff] [blame] | 97 | bool AddIfNotFound) { |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 98 | for (MachineBasicBlock::iterator MII = |
| 99 | prior(MachineBasicBlock::iterator(MI)); ; --MII) { |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 100 | if (MII->addRegisterKilled(SrcReg, TRI, AddIfNotFound)) |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 101 | break; |
| 102 | assert(MII != MI->getParent()->begin() && |
Jakob Stoklund Olesen | 3651d92 | 2010-07-08 05:01:41 +0000 | [diff] [blame] | 103 | "copyPhysReg output doesn't reference source register!"); |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Bob Wilson | 5d52165 | 2010-06-29 18:42:49 +0000 | [diff] [blame] | 107 | /// TransferImplicitDefs - MI is a pseudo-instruction, and the lowered |
| 108 | /// replacement instructions immediately precede it. Copy any implicit-def |
| 109 | /// operands from MI to the replacement instruction. |
| 110 | void |
| 111 | LowerSubregsInstructionPass::TransferImplicitDefs(MachineInstr *MI) { |
| 112 | MachineBasicBlock::iterator CopyMI = MI; |
| 113 | --CopyMI; |
| 114 | |
| 115 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 116 | MachineOperand &MO = MI->getOperand(i); |
| 117 | if (!MO.isReg() || !MO.isImplicit() || MO.isUse()) |
| 118 | continue; |
| 119 | CopyMI->addOperand(MachineOperand::CreateReg(MO.getReg(), true, true)); |
| 120 | } |
| 121 | } |
| 122 | |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 123 | bool LowerSubregsInstructionPass::LowerSubregToReg(MachineInstr *MI) { |
| 124 | MachineBasicBlock *MBB = MI->getParent(); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 125 | assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) && |
| 126 | MI->getOperand(1).isImm() && |
| 127 | (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) && |
| 128 | MI->getOperand(3).isImm() && "Invalid subreg_to_reg"); |
Jakob Stoklund Olesen | f175c5c | 2010-06-22 22:11:07 +0000 | [diff] [blame] | 129 | |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 130 | unsigned DstReg = MI->getOperand(0).getReg(); |
| 131 | unsigned InsReg = MI->getOperand(2).getReg(); |
Jakob Stoklund Olesen | f175c5c | 2010-06-22 22:11:07 +0000 | [diff] [blame] | 132 | assert(!MI->getOperand(2).getSubReg() && "SubIdx on physreg?"); |
Evan Cheng | 7d6d4b3 | 2009-03-23 07:19:58 +0000 | [diff] [blame] | 133 | unsigned SubIdx = MI->getOperand(3).getImm(); |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 134 | |
| 135 | assert(SubIdx != 0 && "Invalid index for insert_subreg"); |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 136 | unsigned DstSubReg = TRI->getSubReg(DstReg, SubIdx); |
Evan Cheng | 7d6d4b3 | 2009-03-23 07:19:58 +0000 | [diff] [blame] | 137 | |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 138 | assert(TargetRegisterInfo::isPhysicalRegister(DstReg) && |
| 139 | "Insert destination must be in a physical register"); |
| 140 | assert(TargetRegisterInfo::isPhysicalRegister(InsReg) && |
| 141 | "Inserted value must be in a physical register"); |
| 142 | |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 143 | DEBUG(dbgs() << "subreg: CONVERTING: " << *MI); |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 144 | |
Jakob Stoklund Olesen | f175c5c | 2010-06-22 22:11:07 +0000 | [diff] [blame] | 145 | if (DstSubReg == InsReg) { |
Dan Gohman | e3d9206 | 2008-08-07 02:54:50 +0000 | [diff] [blame] | 146 | // No need to insert an identify copy instruction. |
Evan Cheng | 7d6d4b3 | 2009-03-23 07:19:58 +0000 | [diff] [blame] | 147 | // Watch out for case like this: |
Jakob Stoklund Olesen | f175c5c | 2010-06-22 22:11:07 +0000 | [diff] [blame] | 148 | // %RAX<def> = SUBREG_TO_REG 0, %EAX<kill>, 3 |
| 149 | // We must leave %RAX live. |
| 150 | if (DstReg != InsReg) { |
| 151 | MI->setDesc(TII->get(TargetOpcode::KILL)); |
| 152 | MI->RemoveOperand(3); // SubIdx |
| 153 | MI->RemoveOperand(1); // Imm |
| 154 | DEBUG(dbgs() << "subreg: replace by: " << *MI); |
| 155 | return true; |
| 156 | } |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 157 | DEBUG(dbgs() << "subreg: eliminated!"); |
Dan Gohman | e3d9206 | 2008-08-07 02:54:50 +0000 | [diff] [blame] | 158 | } else { |
Jakob Stoklund Olesen | 3651d92 | 2010-07-08 05:01:41 +0000 | [diff] [blame] | 159 | TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg, |
| 160 | MI->getOperand(2).isKill()); |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 161 | // Transfer the kill/dead flags, if needed. |
| 162 | if (MI->getOperand(0).isDead()) |
| 163 | TransferDeadFlag(MI, DstSubReg, TRI); |
Bill Wendling | 0d6b1b1 | 2009-08-22 20:23:49 +0000 | [diff] [blame] | 164 | DEBUG({ |
| 165 | MachineBasicBlock::iterator dMI = MI; |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 166 | dbgs() << "subreg: " << *(--dMI); |
Bill Wendling | 0d6b1b1 | 2009-08-22 20:23:49 +0000 | [diff] [blame] | 167 | }); |
Dan Gohman | e3d9206 | 2008-08-07 02:54:50 +0000 | [diff] [blame] | 168 | } |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 169 | |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 170 | DEBUG(dbgs() << '\n'); |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 171 | MBB->erase(MI); |
Anton Korobeynikov | efcd89a | 2009-10-24 00:27:00 +0000 | [diff] [blame] | 172 | return true; |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 173 | } |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 174 | |
Jakob Stoklund Olesen | a4e1ba5 | 2010-07-02 22:29:50 +0000 | [diff] [blame] | 175 | bool LowerSubregsInstructionPass::LowerCopy(MachineInstr *MI) { |
| 176 | MachineOperand &DstMO = MI->getOperand(0); |
| 177 | MachineOperand &SrcMO = MI->getOperand(1); |
| 178 | |
| 179 | if (SrcMO.getReg() == DstMO.getReg()) { |
| 180 | DEBUG(dbgs() << "identity copy: " << *MI); |
| 181 | // No need to insert an identity copy instruction, but replace with a KILL |
| 182 | // if liveness is changed. |
| 183 | if (DstMO.isDead() || SrcMO.isUndef() || MI->getNumOperands() > 2) { |
| 184 | // We must make sure the super-register gets killed. Replace the |
| 185 | // instruction with KILL. |
| 186 | MI->setDesc(TII->get(TargetOpcode::KILL)); |
| 187 | DEBUG(dbgs() << "replaced by: " << *MI); |
| 188 | return true; |
| 189 | } |
| 190 | // Vanilla identity copy. |
| 191 | MI->eraseFromParent(); |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | DEBUG(dbgs() << "real copy: " << *MI); |
Jakob Stoklund Olesen | 3651d92 | 2010-07-08 05:01:41 +0000 | [diff] [blame] | 196 | TII->copyPhysReg(*MI->getParent(), MI, MI->getDebugLoc(), |
| 197 | DstMO.getReg(), SrcMO.getReg(), SrcMO.isKill()); |
Jakob Stoklund Olesen | a4e1ba5 | 2010-07-02 22:29:50 +0000 | [diff] [blame] | 198 | |
| 199 | if (DstMO.isDead()) |
| 200 | TransferDeadFlag(MI, DstMO.getReg(), TRI); |
Jakob Stoklund Olesen | a4e1ba5 | 2010-07-02 22:29:50 +0000 | [diff] [blame] | 201 | if (MI->getNumOperands() > 2) |
| 202 | TransferImplicitDefs(MI); |
| 203 | DEBUG({ |
| 204 | MachineBasicBlock::iterator dMI = MI; |
| 205 | dbgs() << "replaced by: " << *(--dMI); |
| 206 | }); |
| 207 | MI->eraseFromParent(); |
| 208 | return true; |
| 209 | } |
| 210 | |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 211 | /// runOnMachineFunction - Reduce subregister inserts and extracts to register |
| 212 | /// copies. |
| 213 | /// |
| 214 | bool LowerSubregsInstructionPass::runOnMachineFunction(MachineFunction &MF) { |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 215 | DEBUG(dbgs() << "Machine Function\n" |
Bill Wendling | 0d6b1b1 | 2009-08-22 20:23:49 +0000 | [diff] [blame] | 216 | << "********** LOWERING SUBREG INSTRS **********\n" |
| 217 | << "********** Function: " |
| 218 | << MF.getFunction()->getName() << '\n'); |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 219 | TRI = MF.getTarget().getRegisterInfo(); |
| 220 | TII = MF.getTarget().getInstrInfo(); |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 221 | |
Bill Wendling | 0d6b1b1 | 2009-08-22 20:23:49 +0000 | [diff] [blame] | 222 | bool MadeChange = false; |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 223 | |
| 224 | for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end(); |
| 225 | mbbi != mbbe; ++mbbi) { |
| 226 | for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end(); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 227 | mi != me;) { |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 228 | MachineBasicBlock::iterator nmi = llvm::next(mi); |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 229 | MachineInstr *MI = mi; |
Jakob Stoklund Olesen | 5c00e07 | 2010-07-08 16:40:15 +0000 | [diff] [blame] | 230 | assert(!MI->isInsertSubreg() && "INSERT_SUBREG should no longer appear"); |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 231 | assert(MI->getOpcode() != TargetOpcode::EXTRACT_SUBREG && |
| 232 | "EXTRACT_SUBREG should no longer appear"); |
| 233 | if (MI->isSubregToReg()) { |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 234 | MadeChange |= LowerSubregToReg(MI); |
Jakob Stoklund Olesen | a4e1ba5 | 2010-07-02 22:29:50 +0000 | [diff] [blame] | 235 | } else if (MI->isCopy()) { |
| 236 | MadeChange |= LowerCopy(MI); |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 237 | } |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 238 | mi = nmi; |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | return MadeChange; |
| 243 | } |