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 |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +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 | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 56 | bool LowerExtract(MachineInstr *MI); |
| 57 | bool LowerInsert(MachineInstr *MI); |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 58 | bool LowerSubregToReg(MachineInstr *MI); |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 59 | |
| 60 | void TransferDeadFlag(MachineInstr *MI, unsigned DstReg, |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 61 | const TargetRegisterInfo *TRI); |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 62 | void TransferKillFlag(MachineInstr *MI, unsigned SrcReg, |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 63 | const TargetRegisterInfo *TRI, |
Evan Cheng | b018a1e | 2009-08-05 02:25:11 +0000 | [diff] [blame] | 64 | bool AddIfNotFound = false); |
Bob Wilson | 5d52165 | 2010-06-29 18:42:49 +0000 | [diff] [blame^] | 65 | void TransferImplicitDefs(MachineInstr *MI); |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | char LowerSubregsInstructionPass::ID = 0; |
| 69 | } |
| 70 | |
| 71 | FunctionPass *llvm::createLowerSubregsPass() { |
| 72 | return new LowerSubregsInstructionPass(); |
| 73 | } |
| 74 | |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 75 | /// TransferDeadFlag - MI is a pseudo-instruction with DstReg dead, |
| 76 | /// and the lowered replacement instructions immediately precede it. |
| 77 | /// Mark the replacement instructions with the dead flag. |
| 78 | void |
| 79 | LowerSubregsInstructionPass::TransferDeadFlag(MachineInstr *MI, |
| 80 | unsigned DstReg, |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 81 | const TargetRegisterInfo *TRI) { |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 82 | for (MachineBasicBlock::iterator MII = |
| 83 | prior(MachineBasicBlock::iterator(MI)); ; --MII) { |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 84 | if (MII->addRegisterDead(DstReg, TRI)) |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 85 | break; |
| 86 | assert(MII != MI->getParent()->begin() && |
| 87 | "copyRegToReg output doesn't reference destination register!"); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /// TransferKillFlag - MI is a pseudo-instruction with SrcReg killed, |
| 92 | /// and the lowered replacement instructions immediately precede it. |
| 93 | /// Mark the replacement instructions with the kill flag. |
| 94 | void |
| 95 | LowerSubregsInstructionPass::TransferKillFlag(MachineInstr *MI, |
| 96 | unsigned SrcReg, |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 97 | const TargetRegisterInfo *TRI, |
Evan Cheng | b018a1e | 2009-08-05 02:25:11 +0000 | [diff] [blame] | 98 | bool AddIfNotFound) { |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 99 | for (MachineBasicBlock::iterator MII = |
| 100 | prior(MachineBasicBlock::iterator(MI)); ; --MII) { |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 101 | if (MII->addRegisterKilled(SrcReg, TRI, AddIfNotFound)) |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 102 | break; |
| 103 | assert(MII != MI->getParent()->begin() && |
| 104 | "copyRegToReg output doesn't reference source register!"); |
| 105 | } |
| 106 | } |
| 107 | |
Bob Wilson | 5d52165 | 2010-06-29 18:42:49 +0000 | [diff] [blame^] | 108 | /// TransferImplicitDefs - MI is a pseudo-instruction, and the lowered |
| 109 | /// replacement instructions immediately precede it. Copy any implicit-def |
| 110 | /// operands from MI to the replacement instruction. |
| 111 | void |
| 112 | LowerSubregsInstructionPass::TransferImplicitDefs(MachineInstr *MI) { |
| 113 | MachineBasicBlock::iterator CopyMI = MI; |
| 114 | --CopyMI; |
| 115 | |
| 116 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 117 | MachineOperand &MO = MI->getOperand(i); |
| 118 | if (!MO.isReg() || !MO.isImplicit() || MO.isUse()) |
| 119 | continue; |
| 120 | CopyMI->addOperand(MachineOperand::CreateReg(MO.getReg(), true, true)); |
| 121 | } |
| 122 | } |
| 123 | |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 124 | bool LowerSubregsInstructionPass::LowerExtract(MachineInstr *MI) { |
Dan Gohman | 07af765 | 2008-12-18 22:06:01 +0000 | [diff] [blame] | 125 | MachineBasicBlock *MBB = MI->getParent(); |
Jakob Stoklund Olesen | ded2e3b | 2009-08-04 20:01:11 +0000 | [diff] [blame] | 126 | |
Dan Gohman | 07af765 | 2008-12-18 22:06:01 +0000 | [diff] [blame] | 127 | assert(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() && |
| 128 | MI->getOperand(1).isReg() && MI->getOperand(1).isUse() && |
| 129 | MI->getOperand(2).isImm() && "Malformed extract_subreg"); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 130 | |
Dan Gohman | 07af765 | 2008-12-18 22:06:01 +0000 | [diff] [blame] | 131 | unsigned DstReg = MI->getOperand(0).getReg(); |
| 132 | unsigned SuperReg = MI->getOperand(1).getReg(); |
| 133 | unsigned SubIdx = MI->getOperand(2).getImm(); |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 134 | unsigned SrcReg = TRI->getSubReg(SuperReg, SubIdx); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 135 | |
Dan Gohman | 07af765 | 2008-12-18 22:06:01 +0000 | [diff] [blame] | 136 | assert(TargetRegisterInfo::isPhysicalRegister(SuperReg) && |
| 137 | "Extract supperg source must be a physical register"); |
| 138 | assert(TargetRegisterInfo::isPhysicalRegister(DstReg) && |
Dan Gohman | f04865f | 2008-12-18 22:07:25 +0000 | [diff] [blame] | 139 | "Extract destination must be in a physical register"); |
Evan Cheng | 6ade93b | 2009-08-05 03:53:14 +0000 | [diff] [blame] | 140 | assert(SrcReg && "invalid subregister index for register"); |
Jakob Stoklund Olesen | ded2e3b | 2009-08-04 20:01:11 +0000 | [diff] [blame] | 141 | |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 142 | DEBUG(dbgs() << "subreg: CONVERTING: " << *MI); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 143 | |
Dan Gohman | 98c2069 | 2008-12-18 22:11:34 +0000 | [diff] [blame] | 144 | if (SrcReg == DstReg) { |
Jakob Stoklund Olesen | ded2e3b | 2009-08-04 20:01:11 +0000 | [diff] [blame] | 145 | // No need to insert an identity copy instruction. |
| 146 | if (MI->getOperand(1).isKill()) { |
Jakob Stoklund Olesen | 544df36 | 2009-09-28 20:32:46 +0000 | [diff] [blame] | 147 | // We must make sure the super-register gets killed. Replace the |
| 148 | // instruction with KILL. |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 149 | MI->setDesc(TII->get(TargetOpcode::KILL)); |
Jakob Stoklund Olesen | ded2e3b | 2009-08-04 20:01:11 +0000 | [diff] [blame] | 150 | MI->RemoveOperand(2); // SubIdx |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 151 | DEBUG(dbgs() << "subreg: replace by: " << *MI); |
Jakob Stoklund Olesen | ded2e3b | 2009-08-04 20:01:11 +0000 | [diff] [blame] | 152 | return true; |
| 153 | } |
Bill Wendling | 0d6b1b1 | 2009-08-22 20:23:49 +0000 | [diff] [blame] | 154 | |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 155 | DEBUG(dbgs() << "subreg: eliminated!"); |
Dan Gohman | 98c2069 | 2008-12-18 22:11:34 +0000 | [diff] [blame] | 156 | } else { |
| 157 | // Insert copy |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 158 | const TargetRegisterClass *TRCS = TRI->getPhysicalRegisterRegClass(DstReg); |
| 159 | const TargetRegisterClass *TRCD = TRI->getPhysicalRegisterRegClass(SrcReg); |
Dan Gohman | 34dcc6f | 2010-05-06 20:33:48 +0000 | [diff] [blame] | 160 | bool Emitted = TII->copyRegToReg(*MBB, MI, DstReg, SrcReg, TRCD, TRCS, |
| 161 | MI->getDebugLoc()); |
Anton Korobeynikov | d519756 | 2009-07-16 13:55:26 +0000 | [diff] [blame] | 162 | (void)Emitted; |
| 163 | assert(Emitted && "Subreg and Dst must be of compatible register class"); |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 164 | // Transfer the kill/dead flags, if needed. |
| 165 | if (MI->getOperand(0).isDead()) |
| 166 | TransferDeadFlag(MI, DstReg, TRI); |
| 167 | if (MI->getOperand(1).isKill()) |
Evan Cheng | b018a1e | 2009-08-05 02:25:11 +0000 | [diff] [blame] | 168 | TransferKillFlag(MI, SuperReg, TRI, true); |
Bob Wilson | 5d52165 | 2010-06-29 18:42:49 +0000 | [diff] [blame^] | 169 | TransferImplicitDefs(MI); |
Bill Wendling | 0d6b1b1 | 2009-08-22 20:23:49 +0000 | [diff] [blame] | 170 | DEBUG({ |
| 171 | MachineBasicBlock::iterator dMI = MI; |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 172 | dbgs() << "subreg: " << *(--dMI); |
Bill Wendling | 0d6b1b1 | 2009-08-22 20:23:49 +0000 | [diff] [blame] | 173 | }); |
Dan Gohman | 07af765 | 2008-12-18 22:06:01 +0000 | [diff] [blame] | 174 | } |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 175 | |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 176 | DEBUG(dbgs() << '\n'); |
Dan Gohman | 07af765 | 2008-12-18 22:06:01 +0000 | [diff] [blame] | 177 | MBB->erase(MI); |
| 178 | return true; |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 181 | bool LowerSubregsInstructionPass::LowerSubregToReg(MachineInstr *MI) { |
| 182 | MachineBasicBlock *MBB = MI->getParent(); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 183 | assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) && |
| 184 | MI->getOperand(1).isImm() && |
| 185 | (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) && |
| 186 | MI->getOperand(3).isImm() && "Invalid subreg_to_reg"); |
Jakob Stoklund Olesen | f175c5c | 2010-06-22 22:11:07 +0000 | [diff] [blame] | 187 | |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 188 | unsigned DstReg = MI->getOperand(0).getReg(); |
| 189 | unsigned InsReg = MI->getOperand(2).getReg(); |
Jakob Stoklund Olesen | f175c5c | 2010-06-22 22:11:07 +0000 | [diff] [blame] | 190 | assert(!MI->getOperand(2).getSubReg() && "SubIdx on physreg?"); |
Evan Cheng | 7d6d4b3 | 2009-03-23 07:19:58 +0000 | [diff] [blame] | 191 | unsigned SubIdx = MI->getOperand(3).getImm(); |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 192 | |
| 193 | assert(SubIdx != 0 && "Invalid index for insert_subreg"); |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 194 | unsigned DstSubReg = TRI->getSubReg(DstReg, SubIdx); |
Evan Cheng | 7d6d4b3 | 2009-03-23 07:19:58 +0000 | [diff] [blame] | 195 | |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 196 | assert(TargetRegisterInfo::isPhysicalRegister(DstReg) && |
| 197 | "Insert destination must be in a physical register"); |
| 198 | assert(TargetRegisterInfo::isPhysicalRegister(InsReg) && |
| 199 | "Inserted value must be in a physical register"); |
| 200 | |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 201 | DEBUG(dbgs() << "subreg: CONVERTING: " << *MI); |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 202 | |
Jakob Stoklund Olesen | f175c5c | 2010-06-22 22:11:07 +0000 | [diff] [blame] | 203 | if (DstSubReg == InsReg) { |
Dan Gohman | e3d9206 | 2008-08-07 02:54:50 +0000 | [diff] [blame] | 204 | // No need to insert an identify copy instruction. |
Evan Cheng | 7d6d4b3 | 2009-03-23 07:19:58 +0000 | [diff] [blame] | 205 | // Watch out for case like this: |
Jakob Stoklund Olesen | f175c5c | 2010-06-22 22:11:07 +0000 | [diff] [blame] | 206 | // %RAX<def> = SUBREG_TO_REG 0, %EAX<kill>, 3 |
| 207 | // We must leave %RAX live. |
| 208 | if (DstReg != InsReg) { |
| 209 | MI->setDesc(TII->get(TargetOpcode::KILL)); |
| 210 | MI->RemoveOperand(3); // SubIdx |
| 211 | MI->RemoveOperand(1); // Imm |
| 212 | DEBUG(dbgs() << "subreg: replace by: " << *MI); |
| 213 | return true; |
| 214 | } |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 215 | DEBUG(dbgs() << "subreg: eliminated!"); |
Dan Gohman | e3d9206 | 2008-08-07 02:54:50 +0000 | [diff] [blame] | 216 | } else { |
| 217 | // Insert sub-register copy |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 218 | const TargetRegisterClass *TRC0= TRI->getPhysicalRegisterRegClass(DstSubReg); |
| 219 | const TargetRegisterClass *TRC1= TRI->getPhysicalRegisterRegClass(InsReg); |
Dan Gohman | 34dcc6f | 2010-05-06 20:33:48 +0000 | [diff] [blame] | 220 | bool Emitted = TII->copyRegToReg(*MBB, MI, DstSubReg, InsReg, TRC0, TRC1, |
| 221 | MI->getDebugLoc()); |
Anton Korobeynikov | efcd89a | 2009-10-24 00:27:00 +0000 | [diff] [blame] | 222 | (void)Emitted; |
| 223 | assert(Emitted && "Subreg and Dst must be of compatible register class"); |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 224 | // Transfer the kill/dead flags, if needed. |
| 225 | if (MI->getOperand(0).isDead()) |
| 226 | TransferDeadFlag(MI, DstSubReg, TRI); |
| 227 | if (MI->getOperand(2).isKill()) |
| 228 | TransferKillFlag(MI, InsReg, TRI); |
Bill Wendling | 0d6b1b1 | 2009-08-22 20:23:49 +0000 | [diff] [blame] | 229 | DEBUG({ |
| 230 | MachineBasicBlock::iterator dMI = MI; |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 231 | dbgs() << "subreg: " << *(--dMI); |
Bill Wendling | 0d6b1b1 | 2009-08-22 20:23:49 +0000 | [diff] [blame] | 232 | }); |
Dan Gohman | e3d9206 | 2008-08-07 02:54:50 +0000 | [diff] [blame] | 233 | } |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 234 | |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 235 | DEBUG(dbgs() << '\n'); |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 236 | MBB->erase(MI); |
Anton Korobeynikov | efcd89a | 2009-10-24 00:27:00 +0000 | [diff] [blame] | 237 | return true; |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 238 | } |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 239 | |
| 240 | bool LowerSubregsInstructionPass::LowerInsert(MachineInstr *MI) { |
| 241 | MachineBasicBlock *MBB = MI->getParent(); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 242 | assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) && |
| 243 | (MI->getOperand(1).isReg() && MI->getOperand(1).isUse()) && |
| 244 | (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) && |
| 245 | MI->getOperand(3).isImm() && "Invalid insert_subreg"); |
Christopher Lamb | 1fab4a6 | 2008-03-11 10:09:17 +0000 | [diff] [blame] | 246 | |
| 247 | unsigned DstReg = MI->getOperand(0).getReg(); |
Devang Patel | 59500c8 | 2008-11-21 20:00:59 +0000 | [diff] [blame] | 248 | #ifndef NDEBUG |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 249 | unsigned SrcReg = MI->getOperand(1).getReg(); |
Devang Patel | 59500c8 | 2008-11-21 20:00:59 +0000 | [diff] [blame] | 250 | #endif |
Christopher Lamb | 1fab4a6 | 2008-03-11 10:09:17 +0000 | [diff] [blame] | 251 | unsigned InsReg = MI->getOperand(2).getReg(); |
| 252 | unsigned SubIdx = MI->getOperand(3).getImm(); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 253 | |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 254 | assert(DstReg == SrcReg && "insert_subreg not a two-address instruction?"); |
| 255 | assert(SubIdx != 0 && "Invalid index for insert_subreg"); |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 256 | unsigned DstSubReg = TRI->getSubReg(DstReg, SubIdx); |
Jakob Stoklund Olesen | 980daea | 2009-08-03 20:08:18 +0000 | [diff] [blame] | 257 | assert(DstSubReg && "invalid subregister index for register"); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 258 | assert(TargetRegisterInfo::isPhysicalRegister(SrcReg) && |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 259 | "Insert superreg source must be in a physical register"); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 260 | assert(TargetRegisterInfo::isPhysicalRegister(InsReg) && |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 261 | "Inserted value must be in a physical register"); |
| 262 | |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 263 | DEBUG(dbgs() << "subreg: CONVERTING: " << *MI); |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 264 | |
Evan Cheng | c3de802 | 2008-06-16 22:52:53 +0000 | [diff] [blame] | 265 | if (DstSubReg == InsReg) { |
Jakob Stoklund Olesen | 980daea | 2009-08-03 20:08:18 +0000 | [diff] [blame] | 266 | // No need to insert an identity copy instruction. If the SrcReg was |
Jakob Stoklund Olesen | 544df36 | 2009-09-28 20:32:46 +0000 | [diff] [blame] | 267 | // <undef>, we need to make sure it is alive by inserting a KILL |
Jakob Stoklund Olesen | 980daea | 2009-08-03 20:08:18 +0000 | [diff] [blame] | 268 | if (MI->getOperand(1).isUndef() && !MI->getOperand(0).isDead()) { |
Evan Cheng | a72dfb5 | 2009-08-05 01:57:22 +0000 | [diff] [blame] | 269 | MachineInstrBuilder MIB = BuildMI(*MBB, MI, MI->getDebugLoc(), |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 270 | TII->get(TargetOpcode::KILL), DstReg); |
Evan Cheng | a72dfb5 | 2009-08-05 01:57:22 +0000 | [diff] [blame] | 271 | if (MI->getOperand(2).isUndef()) |
Jakob Stoklund Olesen | 544df36 | 2009-09-28 20:32:46 +0000 | [diff] [blame] | 272 | MIB.addReg(InsReg, RegState::Undef); |
Evan Cheng | a72dfb5 | 2009-08-05 01:57:22 +0000 | [diff] [blame] | 273 | else |
Jakob Stoklund Olesen | 544df36 | 2009-09-28 20:32:46 +0000 | [diff] [blame] | 274 | MIB.addReg(InsReg, RegState::Kill); |
Jakob Stoklund Olesen | 980daea | 2009-08-03 20:08:18 +0000 | [diff] [blame] | 275 | } else { |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 276 | DEBUG(dbgs() << "subreg: eliminated!\n"); |
Jakob Stoklund Olesen | 980daea | 2009-08-03 20:08:18 +0000 | [diff] [blame] | 277 | MBB->erase(MI); |
| 278 | return true; |
| 279 | } |
Evan Cheng | c3de802 | 2008-06-16 22:52:53 +0000 | [diff] [blame] | 280 | } else { |
| 281 | // Insert sub-register copy |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 282 | const TargetRegisterClass *TRC0= TRI->getPhysicalRegisterRegClass(DstSubReg); |
| 283 | const TargetRegisterClass *TRC1= TRI->getPhysicalRegisterRegClass(InsReg); |
Evan Cheng | 518ad1a | 2009-08-05 01:29:24 +0000 | [diff] [blame] | 284 | if (MI->getOperand(2).isUndef()) |
Jakob Stoklund Olesen | 544df36 | 2009-09-28 20:32:46 +0000 | [diff] [blame] | 285 | // If the source register being inserted is undef, then this becomes a |
| 286 | // KILL. |
Evan Cheng | 518ad1a | 2009-08-05 01:29:24 +0000 | [diff] [blame] | 287 | BuildMI(*MBB, MI, MI->getDebugLoc(), |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 288 | TII->get(TargetOpcode::KILL), DstSubReg); |
Anton Korobeynikov | efcd89a | 2009-10-24 00:27:00 +0000 | [diff] [blame] | 289 | else { |
Dan Gohman | 34dcc6f | 2010-05-06 20:33:48 +0000 | [diff] [blame] | 290 | bool Emitted = TII->copyRegToReg(*MBB, MI, DstSubReg, InsReg, TRC0, TRC1, |
| 291 | MI->getDebugLoc()); |
Anton Korobeynikov | efcd89a | 2009-10-24 00:27:00 +0000 | [diff] [blame] | 292 | (void)Emitted; |
| 293 | assert(Emitted && "Subreg and Dst must be of compatible register class"); |
| 294 | } |
Jakob Stoklund Olesen | 980daea | 2009-08-03 20:08:18 +0000 | [diff] [blame] | 295 | MachineBasicBlock::iterator CopyMI = MI; |
| 296 | --CopyMI; |
| 297 | |
Jakob Stoklund Olesen | 9390cd0 | 2009-08-08 13:19:10 +0000 | [diff] [blame] | 298 | // INSERT_SUBREG is a two-address instruction so it implicitly kills SrcReg. |
| 299 | if (!MI->getOperand(1).isUndef()) |
| 300 | CopyMI->addOperand(MachineOperand::CreateReg(DstReg, false, true, true)); |
| 301 | |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 302 | // Transfer the kill/dead flags, if needed. |
Jakob Stoklund Olesen | 980daea | 2009-08-03 20:08:18 +0000 | [diff] [blame] | 303 | if (MI->getOperand(0).isDead()) { |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 304 | TransferDeadFlag(MI, DstSubReg, TRI); |
Jakob Stoklund Olesen | 9390cd0 | 2009-08-08 13:19:10 +0000 | [diff] [blame] | 305 | } else { |
| 306 | // Make sure the full DstReg is live after this replacement. |
Jakob Stoklund Olesen | 980daea | 2009-08-03 20:08:18 +0000 | [diff] [blame] | 307 | CopyMI->addOperand(MachineOperand::CreateReg(DstReg, true, true)); |
| 308 | } |
| 309 | |
| 310 | // Make sure the inserted register gets killed |
Evan Cheng | 518ad1a | 2009-08-05 01:29:24 +0000 | [diff] [blame] | 311 | if (MI->getOperand(2).isKill() && !MI->getOperand(2).isUndef()) |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 312 | TransferKillFlag(MI, InsReg, TRI); |
Jakob Stoklund Olesen | 980daea | 2009-08-03 20:08:18 +0000 | [diff] [blame] | 313 | } |
Dan Gohman | 98c2069 | 2008-12-18 22:11:34 +0000 | [diff] [blame] | 314 | |
Bill Wendling | 0d6b1b1 | 2009-08-22 20:23:49 +0000 | [diff] [blame] | 315 | DEBUG({ |
| 316 | MachineBasicBlock::iterator dMI = MI; |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 317 | dbgs() << "subreg: " << *(--dMI) << "\n"; |
Bill Wendling | 0d6b1b1 | 2009-08-22 20:23:49 +0000 | [diff] [blame] | 318 | }); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 319 | |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 320 | MBB->erase(MI); |
Jakob Stoklund Olesen | 9390cd0 | 2009-08-08 13:19:10 +0000 | [diff] [blame] | 321 | return true; |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 322 | } |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 323 | |
| 324 | /// runOnMachineFunction - Reduce subregister inserts and extracts to register |
| 325 | /// copies. |
| 326 | /// |
| 327 | bool LowerSubregsInstructionPass::runOnMachineFunction(MachineFunction &MF) { |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 328 | DEBUG(dbgs() << "Machine Function\n" |
Bill Wendling | 0d6b1b1 | 2009-08-22 20:23:49 +0000 | [diff] [blame] | 329 | << "********** LOWERING SUBREG INSTRS **********\n" |
| 330 | << "********** Function: " |
| 331 | << MF.getFunction()->getName() << '\n'); |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 332 | TRI = MF.getTarget().getRegisterInfo(); |
| 333 | TII = MF.getTarget().getInstrInfo(); |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 334 | |
Bill Wendling | 0d6b1b1 | 2009-08-22 20:23:49 +0000 | [diff] [blame] | 335 | bool MadeChange = false; |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 336 | |
| 337 | for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end(); |
| 338 | mbbi != mbbe; ++mbbi) { |
| 339 | for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end(); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 340 | mi != me;) { |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 341 | MachineBasicBlock::iterator nmi = llvm::next(mi); |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 342 | MachineInstr *MI = mi; |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 343 | if (MI->isExtractSubreg()) { |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 344 | MadeChange |= LowerExtract(MI); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 345 | } else if (MI->isInsertSubreg()) { |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 346 | MadeChange |= LowerInsert(MI); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 347 | } else if (MI->isSubregToReg()) { |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 348 | MadeChange |= LowerSubregToReg(MI); |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 349 | } |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 350 | mi = nmi; |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
| 354 | return MadeChange; |
| 355 | } |