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); |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 57 | bool LowerSubregToReg(MachineInstr *MI); |
Jakob Stoklund Olesen | a4e1ba5 | 2010-07-02 22:29:50 +0000 | [diff] [blame] | 58 | bool LowerCopy(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() && |
Jakob Stoklund Olesen | 3651d92 | 2010-07-08 05:01:41 +0000 | [diff] [blame] | 87 | "copyPhysReg output doesn't reference destination register!"); |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 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() && |
Jakob Stoklund Olesen | 3651d92 | 2010-07-08 05:01:41 +0000 | [diff] [blame] | 104 | "copyPhysReg output doesn't reference source register!"); |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 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 { |
Jakob Stoklund Olesen | 3651d92 | 2010-07-08 05:01:41 +0000 | [diff] [blame] | 157 | TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstReg, SrcReg, false); |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 158 | // Transfer the kill/dead flags, if needed. |
| 159 | if (MI->getOperand(0).isDead()) |
| 160 | TransferDeadFlag(MI, DstReg, TRI); |
| 161 | if (MI->getOperand(1).isKill()) |
Evan Cheng | b018a1e | 2009-08-05 02:25:11 +0000 | [diff] [blame] | 162 | TransferKillFlag(MI, SuperReg, TRI, true); |
Bob Wilson | 5d52165 | 2010-06-29 18:42:49 +0000 | [diff] [blame] | 163 | TransferImplicitDefs(MI); |
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 | 07af765 | 2008-12-18 22:06:01 +0000 | [diff] [blame] | 168 | } |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 169 | |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 170 | DEBUG(dbgs() << '\n'); |
Dan Gohman | 07af765 | 2008-12-18 22:06:01 +0000 | [diff] [blame] | 171 | MBB->erase(MI); |
| 172 | return true; |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 175 | bool LowerSubregsInstructionPass::LowerSubregToReg(MachineInstr *MI) { |
| 176 | MachineBasicBlock *MBB = MI->getParent(); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 177 | assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) && |
| 178 | MI->getOperand(1).isImm() && |
| 179 | (MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) && |
| 180 | MI->getOperand(3).isImm() && "Invalid subreg_to_reg"); |
Jakob Stoklund Olesen | f175c5c | 2010-06-22 22:11:07 +0000 | [diff] [blame] | 181 | |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 182 | unsigned DstReg = MI->getOperand(0).getReg(); |
| 183 | unsigned InsReg = MI->getOperand(2).getReg(); |
Jakob Stoklund Olesen | f175c5c | 2010-06-22 22:11:07 +0000 | [diff] [blame] | 184 | assert(!MI->getOperand(2).getSubReg() && "SubIdx on physreg?"); |
Evan Cheng | 7d6d4b3 | 2009-03-23 07:19:58 +0000 | [diff] [blame] | 185 | unsigned SubIdx = MI->getOperand(3).getImm(); |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 186 | |
| 187 | assert(SubIdx != 0 && "Invalid index for insert_subreg"); |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 188 | unsigned DstSubReg = TRI->getSubReg(DstReg, SubIdx); |
Evan Cheng | 7d6d4b3 | 2009-03-23 07:19:58 +0000 | [diff] [blame] | 189 | |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 190 | assert(TargetRegisterInfo::isPhysicalRegister(DstReg) && |
| 191 | "Insert destination must be in a physical register"); |
| 192 | assert(TargetRegisterInfo::isPhysicalRegister(InsReg) && |
| 193 | "Inserted value must be in a physical register"); |
| 194 | |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 195 | DEBUG(dbgs() << "subreg: CONVERTING: " << *MI); |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 196 | |
Jakob Stoklund Olesen | f175c5c | 2010-06-22 22:11:07 +0000 | [diff] [blame] | 197 | if (DstSubReg == InsReg) { |
Dan Gohman | e3d9206 | 2008-08-07 02:54:50 +0000 | [diff] [blame] | 198 | // No need to insert an identify copy instruction. |
Evan Cheng | 7d6d4b3 | 2009-03-23 07:19:58 +0000 | [diff] [blame] | 199 | // Watch out for case like this: |
Jakob Stoklund Olesen | f175c5c | 2010-06-22 22:11:07 +0000 | [diff] [blame] | 200 | // %RAX<def> = SUBREG_TO_REG 0, %EAX<kill>, 3 |
| 201 | // We must leave %RAX live. |
| 202 | if (DstReg != InsReg) { |
| 203 | MI->setDesc(TII->get(TargetOpcode::KILL)); |
| 204 | MI->RemoveOperand(3); // SubIdx |
| 205 | MI->RemoveOperand(1); // Imm |
| 206 | DEBUG(dbgs() << "subreg: replace by: " << *MI); |
| 207 | return true; |
| 208 | } |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 209 | DEBUG(dbgs() << "subreg: eliminated!"); |
Dan Gohman | e3d9206 | 2008-08-07 02:54:50 +0000 | [diff] [blame] | 210 | } else { |
Jakob Stoklund Olesen | 3651d92 | 2010-07-08 05:01:41 +0000 | [diff] [blame] | 211 | TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg, |
| 212 | MI->getOperand(2).isKill()); |
Dan Gohman | a5b2fee | 2008-12-18 22:14:08 +0000 | [diff] [blame] | 213 | // Transfer the kill/dead flags, if needed. |
| 214 | if (MI->getOperand(0).isDead()) |
| 215 | TransferDeadFlag(MI, DstSubReg, TRI); |
Bill Wendling | 0d6b1b1 | 2009-08-22 20:23:49 +0000 | [diff] [blame] | 216 | DEBUG({ |
| 217 | MachineBasicBlock::iterator dMI = MI; |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 218 | dbgs() << "subreg: " << *(--dMI); |
Bill Wendling | 0d6b1b1 | 2009-08-22 20:23:49 +0000 | [diff] [blame] | 219 | }); |
Dan Gohman | e3d9206 | 2008-08-07 02:54:50 +0000 | [diff] [blame] | 220 | } |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 221 | |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 222 | DEBUG(dbgs() << '\n'); |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 223 | MBB->erase(MI); |
Anton Korobeynikov | efcd89a | 2009-10-24 00:27:00 +0000 | [diff] [blame] | 224 | return true; |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 225 | } |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 226 | |
Jakob Stoklund Olesen | a4e1ba5 | 2010-07-02 22:29:50 +0000 | [diff] [blame] | 227 | bool LowerSubregsInstructionPass::LowerCopy(MachineInstr *MI) { |
| 228 | MachineOperand &DstMO = MI->getOperand(0); |
| 229 | MachineOperand &SrcMO = MI->getOperand(1); |
| 230 | |
| 231 | if (SrcMO.getReg() == DstMO.getReg()) { |
| 232 | DEBUG(dbgs() << "identity copy: " << *MI); |
| 233 | // No need to insert an identity copy instruction, but replace with a KILL |
| 234 | // if liveness is changed. |
| 235 | if (DstMO.isDead() || SrcMO.isUndef() || MI->getNumOperands() > 2) { |
| 236 | // We must make sure the super-register gets killed. Replace the |
| 237 | // instruction with KILL. |
| 238 | MI->setDesc(TII->get(TargetOpcode::KILL)); |
| 239 | DEBUG(dbgs() << "replaced by: " << *MI); |
| 240 | return true; |
| 241 | } |
| 242 | // Vanilla identity copy. |
| 243 | MI->eraseFromParent(); |
| 244 | return true; |
| 245 | } |
| 246 | |
| 247 | DEBUG(dbgs() << "real copy: " << *MI); |
Jakob Stoklund Olesen | 3651d92 | 2010-07-08 05:01:41 +0000 | [diff] [blame] | 248 | TII->copyPhysReg(*MI->getParent(), MI, MI->getDebugLoc(), |
| 249 | DstMO.getReg(), SrcMO.getReg(), SrcMO.isKill()); |
Jakob Stoklund Olesen | a4e1ba5 | 2010-07-02 22:29:50 +0000 | [diff] [blame] | 250 | |
| 251 | if (DstMO.isDead()) |
| 252 | TransferDeadFlag(MI, DstMO.getReg(), TRI); |
Jakob Stoklund Olesen | a4e1ba5 | 2010-07-02 22:29:50 +0000 | [diff] [blame] | 253 | if (MI->getNumOperands() > 2) |
| 254 | TransferImplicitDefs(MI); |
| 255 | DEBUG({ |
| 256 | MachineBasicBlock::iterator dMI = MI; |
| 257 | dbgs() << "replaced by: " << *(--dMI); |
| 258 | }); |
| 259 | MI->eraseFromParent(); |
| 260 | return true; |
| 261 | } |
| 262 | |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 263 | /// runOnMachineFunction - Reduce subregister inserts and extracts to register |
| 264 | /// copies. |
| 265 | /// |
| 266 | bool LowerSubregsInstructionPass::runOnMachineFunction(MachineFunction &MF) { |
David Greene | 6d206f8 | 2010-01-04 23:06:47 +0000 | [diff] [blame] | 267 | DEBUG(dbgs() << "Machine Function\n" |
Bill Wendling | 0d6b1b1 | 2009-08-22 20:23:49 +0000 | [diff] [blame] | 268 | << "********** LOWERING SUBREG INSTRS **********\n" |
| 269 | << "********** Function: " |
| 270 | << MF.getFunction()->getName() << '\n'); |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 271 | TRI = MF.getTarget().getRegisterInfo(); |
| 272 | TII = MF.getTarget().getInstrInfo(); |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 273 | |
Bill Wendling | 0d6b1b1 | 2009-08-22 20:23:49 +0000 | [diff] [blame] | 274 | bool MadeChange = false; |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 275 | |
| 276 | for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end(); |
| 277 | mbbi != mbbe; ++mbbi) { |
| 278 | for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end(); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 279 | mi != me;) { |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 280 | MachineBasicBlock::iterator nmi = llvm::next(mi); |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 281 | MachineInstr *MI = mi; |
Jakob Stoklund Olesen | 5c00e07 | 2010-07-08 16:40:15 +0000 | [diff] [blame^] | 282 | assert(!MI->isInsertSubreg() && "INSERT_SUBREG should no longer appear"); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 283 | if (MI->isExtractSubreg()) { |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 284 | MadeChange |= LowerExtract(MI); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 285 | } else if (MI->isSubregToReg()) { |
Christopher Lamb | c929823 | 2008-03-16 03:12:01 +0000 | [diff] [blame] | 286 | MadeChange |= LowerSubregToReg(MI); |
Jakob Stoklund Olesen | a4e1ba5 | 2010-07-02 22:29:50 +0000 | [diff] [blame] | 287 | } else if (MI->isCopy()) { |
| 288 | MadeChange |= LowerCopy(MI); |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 289 | } |
Evan Cheng | d98e30f | 2009-10-25 07:49:57 +0000 | [diff] [blame] | 290 | mi = nmi; |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | return MadeChange; |
| 295 | } |