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