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