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 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #define DEBUG_TYPE "lowersubregs" |
| 11 | #include "llvm/CodeGen/Passes.h" |
| 12 | #include "llvm/Function.h" |
| 13 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 14 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 16 | #include "llvm/Target/TargetRegisterInfo.h" |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 17 | #include "llvm/Target/TargetInstrInfo.h" |
| 18 | #include "llvm/Target/TargetMachine.h" |
| 19 | #include "llvm/Support/Debug.h" |
| 20 | #include "llvm/Support/Compiler.h" |
| 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | struct VISIBILITY_HIDDEN LowerSubregsInstructionPass |
| 25 | : public MachineFunctionPass { |
| 26 | static char ID; // Pass identification, replacement for typeid |
| 27 | LowerSubregsInstructionPass() : MachineFunctionPass((intptr_t)&ID) {} |
| 28 | |
| 29 | const char *getPassName() const { |
| 30 | return "Subregister lowering instruction pass"; |
| 31 | } |
| 32 | |
| 33 | /// runOnMachineFunction - pass entry point |
| 34 | bool runOnMachineFunction(MachineFunction&); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 35 | |
| 36 | bool LowerExtract(MachineInstr *MI); |
| 37 | bool LowerInsert(MachineInstr *MI); |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | char LowerSubregsInstructionPass::ID = 0; |
| 41 | } |
| 42 | |
| 43 | FunctionPass *llvm::createLowerSubregsPass() { |
| 44 | return new LowerSubregsInstructionPass(); |
| 45 | } |
| 46 | |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 47 | // Returns the Register Class of a physical register. |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 48 | static const TargetRegisterClass *getPhysicalRegisterRegClass( |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 49 | const TargetRegisterInfo &TRI, |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 50 | unsigned reg) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 51 | assert(TargetRegisterInfo::isPhysicalRegister(reg) && |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 52 | "reg must be a physical register"); |
| 53 | // Pick the register class of the right type that contains this physreg. |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 54 | for (TargetRegisterInfo::regclass_iterator I = TRI.regclass_begin(), |
| 55 | E = TRI.regclass_end(); I != E; ++I) |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 56 | if ((*I)->contains(reg)) |
| 57 | return *I; |
| 58 | assert(false && "Couldn't find the register class"); |
| 59 | return 0; |
| 60 | } |
| 61 | |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 62 | bool LowerSubregsInstructionPass::LowerExtract(MachineInstr *MI) { |
| 63 | MachineBasicBlock *MBB = MI->getParent(); |
| 64 | MachineFunction &MF = *MBB->getParent(); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 65 | const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo(); |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 66 | const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 67 | |
| 68 | assert(MI->getOperand(0).isRegister() && MI->getOperand(0).isDef() && |
| 69 | MI->getOperand(1).isRegister() && MI->getOperand(1).isUse() && |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 70 | MI->getOperand(2).isImmediate() && "Malformed extract_subreg"); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 71 | |
| 72 | unsigned SuperReg = MI->getOperand(1).getReg(); |
| 73 | unsigned SubIdx = MI->getOperand(2).getImm(); |
| 74 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 75 | assert(TargetRegisterInfo::isPhysicalRegister(SuperReg) && |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 76 | "Extract supperg source must be a physical register"); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 77 | unsigned SrcReg = TRI.getSubReg(SuperReg, SubIdx); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 78 | unsigned DstReg = MI->getOperand(0).getReg(); |
| 79 | |
| 80 | DOUT << "subreg: CONVERTING: " << *MI; |
| 81 | |
| 82 | if (SrcReg != DstReg) { |
| 83 | const TargetRegisterClass *TRC = 0; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 84 | if (TargetRegisterInfo::isPhysicalRegister(DstReg)) { |
| 85 | TRC = getPhysicalRegisterRegClass(TRI, DstReg); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 86 | } else { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 87 | TRC = MF.getRegInfo().getRegClass(DstReg); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 88 | } |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 89 | assert(TRC == getPhysicalRegisterRegClass(TRI, SrcReg) && |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 90 | "Extract subreg and Dst must be of same register class"); |
| 91 | |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 92 | TII.copyRegToReg(*MBB, MI, DstReg, SrcReg, TRC, TRC); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 93 | MachineBasicBlock::iterator dMI = MI; |
| 94 | DOUT << "subreg: " << *(--dMI); |
| 95 | } |
| 96 | |
| 97 | DOUT << "\n"; |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 98 | MBB->remove(MI); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 99 | return true; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | bool LowerSubregsInstructionPass::LowerInsert(MachineInstr *MI) { |
| 104 | MachineBasicBlock *MBB = MI->getParent(); |
| 105 | MachineFunction &MF = *MBB->getParent(); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 106 | const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo(); |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 107 | const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); |
Christopher Lamb | 3feb017 | 2008-03-10 06:12:08 +0000 | [diff] [blame^] | 108 | assert((MI->getOperand(0).isRegister() && MI->getOperand(0).isDef()) && |
| 109 | ((MI->getOperand(1).isRegister() && MI->getOperand(1).isUse()) || |
| 110 | MI->getOperand(1).isImmediate()) && |
| 111 | (MI->getOperand(2).isRegister() && MI->getOperand(2).isUse()) && |
| 112 | MI->getOperand(3).isImmediate() && "Invalid insert_subreg"); |
| 113 | |
| 114 | unsigned DstReg = MI->getOperand(0).getReg(); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 115 | unsigned SrcReg = 0; |
Christopher Lamb | 3feb017 | 2008-03-10 06:12:08 +0000 | [diff] [blame^] | 116 | // Check if we're inserting into an implicit value. |
| 117 | if (MI->getOperand(1).isImmediate()) |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 118 | SrcReg = DstReg; |
Christopher Lamb | 3feb017 | 2008-03-10 06:12:08 +0000 | [diff] [blame^] | 119 | else |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 120 | SrcReg = MI->getOperand(1).getReg(); |
Christopher Lamb | 3feb017 | 2008-03-10 06:12:08 +0000 | [diff] [blame^] | 121 | unsigned InsReg = MI->getOperand(2).getReg(); |
| 122 | unsigned SubIdx = MI->getOperand(3).getImm(); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 123 | |
| 124 | assert(SubIdx != 0 && "Invalid index for extract_subreg"); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 125 | unsigned DstSubReg = TRI.getSubReg(DstReg, SubIdx); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 126 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 127 | assert(TargetRegisterInfo::isPhysicalRegister(SrcReg) && |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 128 | "Insert superreg source must be in a physical register"); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 129 | assert(TargetRegisterInfo::isPhysicalRegister(DstReg) && |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 130 | "Insert destination must be in a physical register"); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 131 | assert(TargetRegisterInfo::isPhysicalRegister(InsReg) && |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 132 | "Inserted value must be in a physical register"); |
| 133 | |
| 134 | DOUT << "subreg: CONVERTING: " << *MI; |
| 135 | |
| 136 | // If the inserted register is already allocated into a subregister |
| 137 | // of the destination, we copy the subreg into the source |
| 138 | // However, this is only safe if the insert instruction is the kill |
| 139 | // of the source register |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 140 | bool revCopyOrder = TRI.isSubRegister(DstReg, InsReg); |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 141 | if (revCopyOrder && InsReg != DstSubReg) { |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 142 | if (MI->getOperand(1).isKill()) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 143 | DstSubReg = TRI.getSubReg(SrcReg, SubIdx); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 144 | // Insert sub-register copy |
| 145 | const TargetRegisterClass *TRC1 = 0; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 146 | if (TargetRegisterInfo::isPhysicalRegister(InsReg)) { |
| 147 | TRC1 = getPhysicalRegisterRegClass(TRI, InsReg); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 148 | } else { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 149 | TRC1 = MF.getRegInfo().getRegClass(InsReg); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 150 | } |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 151 | TII.copyRegToReg(*MBB, MI, DstSubReg, InsReg, TRC1, TRC1); |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 152 | |
| 153 | #ifndef NDEBUG |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 154 | MachineBasicBlock::iterator dMI = MI; |
| 155 | DOUT << "subreg: " << *(--dMI); |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 156 | #endif |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 157 | } else { |
| 158 | assert(0 && "Don't know how to convert this insert"); |
| 159 | } |
| 160 | } |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 161 | #ifndef NDEBUG |
| 162 | if (InsReg == DstSubReg) { |
| 163 | DOUT << "subreg: Eliminated subreg copy\n"; |
| 164 | } |
| 165 | #endif |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 166 | |
| 167 | if (SrcReg != DstReg) { |
| 168 | // Insert super-register copy |
| 169 | const TargetRegisterClass *TRC0 = 0; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 170 | if (TargetRegisterInfo::isPhysicalRegister(DstReg)) { |
| 171 | TRC0 = getPhysicalRegisterRegClass(TRI, DstReg); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 172 | } else { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 173 | TRC0 = MF.getRegInfo().getRegClass(DstReg); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 174 | } |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 175 | assert(TRC0 == getPhysicalRegisterRegClass(TRI, SrcReg) && |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 176 | "Insert superreg and Dst must be of same register class"); |
| 177 | |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 178 | TII.copyRegToReg(*MBB, MI, DstReg, SrcReg, TRC0, TRC0); |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 179 | |
| 180 | #ifndef NDEBUG |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 181 | MachineBasicBlock::iterator dMI = MI; |
| 182 | DOUT << "subreg: " << *(--dMI); |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 183 | #endif |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 184 | } |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 185 | |
| 186 | #ifndef NDEBUG |
| 187 | if (SrcReg == DstReg) { |
| 188 | DOUT << "subreg: Eliminated superreg copy\n"; |
| 189 | } |
| 190 | #endif |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 191 | |
| 192 | if (!revCopyOrder && InsReg != DstSubReg) { |
| 193 | // Insert sub-register copy |
| 194 | const TargetRegisterClass *TRC1 = 0; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 195 | if (TargetRegisterInfo::isPhysicalRegister(InsReg)) { |
| 196 | TRC1 = getPhysicalRegisterRegClass(TRI, InsReg); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 197 | } else { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 198 | TRC1 = MF.getRegInfo().getRegClass(InsReg); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 199 | } |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 200 | TII.copyRegToReg(*MBB, MI, DstSubReg, InsReg, TRC1, TRC1); |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 201 | |
| 202 | #ifndef NDEBUG |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 203 | MachineBasicBlock::iterator dMI = MI; |
| 204 | DOUT << "subreg: " << *(--dMI); |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 205 | #endif |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | DOUT << "\n"; |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 209 | MBB->remove(MI); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 210 | return true; |
| 211 | } |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 212 | |
| 213 | /// runOnMachineFunction - Reduce subregister inserts and extracts to register |
| 214 | /// copies. |
| 215 | /// |
| 216 | bool LowerSubregsInstructionPass::runOnMachineFunction(MachineFunction &MF) { |
| 217 | DOUT << "Machine Function\n"; |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 218 | |
| 219 | bool MadeChange = false; |
| 220 | |
| 221 | DOUT << "********** LOWERING SUBREG INSTRS **********\n"; |
| 222 | DOUT << "********** Function: " << MF.getFunction()->getName() << '\n'; |
| 223 | |
| 224 | for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end(); |
| 225 | mbbi != mbbe; ++mbbi) { |
| 226 | for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end(); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 227 | mi != me;) { |
| 228 | MachineInstr *MI = mi++; |
| 229 | |
| 230 | if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) { |
| 231 | MadeChange |= LowerExtract(MI); |
| 232 | } else if (MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG) { |
| 233 | MadeChange |= LowerInsert(MI); |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | return MadeChange; |
| 239 | } |