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