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 | bool LowerSubregsInstructionPass::LowerExtract(MachineInstr *MI) { |
| 48 | MachineBasicBlock *MBB = MI->getParent(); |
| 49 | MachineFunction &MF = *MBB->getParent(); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 50 | const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo(); |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 51 | const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 52 | |
| 53 | assert(MI->getOperand(0).isRegister() && MI->getOperand(0).isDef() && |
| 54 | MI->getOperand(1).isRegister() && MI->getOperand(1).isUse() && |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 55 | MI->getOperand(2).isImmediate() && "Malformed extract_subreg"); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 56 | |
| 57 | unsigned SuperReg = MI->getOperand(1).getReg(); |
| 58 | unsigned SubIdx = MI->getOperand(2).getImm(); |
| 59 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 60 | assert(TargetRegisterInfo::isPhysicalRegister(SuperReg) && |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 61 | "Extract supperg source must be a physical register"); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 62 | unsigned SrcReg = TRI.getSubReg(SuperReg, SubIdx); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 63 | unsigned DstReg = MI->getOperand(0).getReg(); |
| 64 | |
| 65 | DOUT << "subreg: CONVERTING: " << *MI; |
| 66 | |
| 67 | if (SrcReg != DstReg) { |
| 68 | const TargetRegisterClass *TRC = 0; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 69 | if (TargetRegisterInfo::isPhysicalRegister(DstReg)) { |
Evan Cheng | ea23781 | 2008-03-11 07:55:13 +0000 | [diff] [blame] | 70 | TRC = TRI.getPhysicalRegisterRegClass(DstReg); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 71 | } else { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 72 | TRC = MF.getRegInfo().getRegClass(DstReg); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 73 | } |
Evan Cheng | ea23781 | 2008-03-11 07:55:13 +0000 | [diff] [blame] | 74 | assert(TRC == TRI.getPhysicalRegisterRegClass(SrcReg) && |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 75 | "Extract subreg and Dst must be of same register class"); |
| 76 | |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 77 | TII.copyRegToReg(*MBB, MI, DstReg, SrcReg, TRC, TRC); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 78 | MachineBasicBlock::iterator dMI = MI; |
| 79 | DOUT << "subreg: " << *(--dMI); |
| 80 | } |
| 81 | |
| 82 | DOUT << "\n"; |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 83 | MBB->remove(MI); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 84 | return true; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | bool LowerSubregsInstructionPass::LowerInsert(MachineInstr *MI) { |
| 89 | MachineBasicBlock *MBB = MI->getParent(); |
| 90 | MachineFunction &MF = *MBB->getParent(); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 91 | const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo(); |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 92 | const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); |
Christopher Lamb | 1fab4a6 | 2008-03-11 10:09:17 +0000 | [diff] [blame] | 93 | assert((MI->getOperand(0).isRegister() && MI->getOperand(0).isDef()) && |
| 94 | ((MI->getOperand(1).isRegister() && MI->getOperand(1).isUse()) || |
| 95 | MI->getOperand(1).isImmediate()) && |
| 96 | (MI->getOperand(2).isRegister() && MI->getOperand(2).isUse()) && |
| 97 | MI->getOperand(3).isImmediate() && "Invalid insert_subreg"); |
| 98 | |
Christopher Lamb | 6634e26 | 2008-03-13 05:47:01 +0000 | [diff] [blame^] | 99 | // Check if we're inserting into an implicit undef value. |
| 100 | bool isImplicit = MI->getOperand(1).isImmediate(); |
Christopher Lamb | 1fab4a6 | 2008-03-11 10:09:17 +0000 | [diff] [blame] | 101 | unsigned DstReg = MI->getOperand(0).getReg(); |
Christopher Lamb | 6634e26 | 2008-03-13 05:47:01 +0000 | [diff] [blame^] | 102 | unsigned SrcReg = isImplicit ? DstReg : MI->getOperand(1).getReg(); |
Christopher Lamb | 1fab4a6 | 2008-03-11 10:09:17 +0000 | [diff] [blame] | 103 | unsigned InsReg = MI->getOperand(2).getReg(); |
| 104 | unsigned SubIdx = MI->getOperand(3).getImm(); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 105 | |
| 106 | assert(SubIdx != 0 && "Invalid index for extract_subreg"); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 107 | unsigned DstSubReg = TRI.getSubReg(DstReg, SubIdx); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 108 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 109 | assert(TargetRegisterInfo::isPhysicalRegister(SrcReg) && |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 110 | "Insert superreg source must be in a physical register"); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 111 | assert(TargetRegisterInfo::isPhysicalRegister(DstReg) && |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 112 | "Insert destination must be in a physical register"); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 113 | assert(TargetRegisterInfo::isPhysicalRegister(InsReg) && |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 114 | "Inserted value must be in a physical register"); |
| 115 | |
| 116 | DOUT << "subreg: CONVERTING: " << *MI; |
| 117 | |
Christopher Lamb | 6634e26 | 2008-03-13 05:47:01 +0000 | [diff] [blame^] | 118 | // Check whether the implict subreg copy has side affects or not. Only copies |
| 119 | // into an undef value have no side affects, that is they can be eliminated |
| 120 | // without changing the semantics of the program. |
| 121 | bool copyHasSideAffects = isImplicit? |
| 122 | MI->getOperand(1).getImm() != TargetInstrInfo::IMPL_VAL_UNDEF |
| 123 | : false; |
| 124 | |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 125 | // If the inserted register is already allocated into a subregister |
| 126 | // of the destination, we copy the subreg into the source |
| 127 | // However, this is only safe if the insert instruction is the kill |
| 128 | // of the source register |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 129 | bool revCopyOrder = TRI.isSubRegister(DstReg, InsReg); |
Christopher Lamb | 6634e26 | 2008-03-13 05:47:01 +0000 | [diff] [blame^] | 130 | if (revCopyOrder && (InsReg != DstSubReg || copyHasSideAffects)) { |
| 131 | if (isImplicit || MI->getOperand(1).isKill()) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 132 | DstSubReg = TRI.getSubReg(SrcReg, SubIdx); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 133 | // Insert sub-register copy |
| 134 | const TargetRegisterClass *TRC1 = 0; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 135 | if (TargetRegisterInfo::isPhysicalRegister(InsReg)) { |
Evan Cheng | ea23781 | 2008-03-11 07:55:13 +0000 | [diff] [blame] | 136 | TRC1 = TRI.getPhysicalRegisterRegClass(InsReg); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 137 | } else { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 138 | TRC1 = MF.getRegInfo().getRegClass(InsReg); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 139 | } |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 140 | TII.copyRegToReg(*MBB, MI, DstSubReg, InsReg, TRC1, TRC1); |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 141 | |
| 142 | #ifndef NDEBUG |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 143 | MachineBasicBlock::iterator dMI = MI; |
| 144 | DOUT << "subreg: " << *(--dMI); |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 145 | #endif |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 146 | } else { |
| 147 | assert(0 && "Don't know how to convert this insert"); |
| 148 | } |
| 149 | } |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 150 | #ifndef NDEBUG |
Christopher Lamb | 6634e26 | 2008-03-13 05:47:01 +0000 | [diff] [blame^] | 151 | if (InsReg == DstSubReg && !copyHasSideAffects) { |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 152 | DOUT << "subreg: Eliminated subreg copy\n"; |
| 153 | } |
| 154 | #endif |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 155 | |
| 156 | if (SrcReg != DstReg) { |
| 157 | // Insert super-register copy |
| 158 | const TargetRegisterClass *TRC0 = 0; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 159 | if (TargetRegisterInfo::isPhysicalRegister(DstReg)) { |
Evan Cheng | ea23781 | 2008-03-11 07:55:13 +0000 | [diff] [blame] | 160 | TRC0 = TRI.getPhysicalRegisterRegClass(DstReg); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 161 | } else { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 162 | TRC0 = MF.getRegInfo().getRegClass(DstReg); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 163 | } |
Evan Cheng | ea23781 | 2008-03-11 07:55:13 +0000 | [diff] [blame] | 164 | assert(TRC0 == TRI.getPhysicalRegisterRegClass(SrcReg) && |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 165 | "Insert superreg and Dst must be of same register class"); |
| 166 | |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 167 | TII.copyRegToReg(*MBB, MI, DstReg, SrcReg, TRC0, TRC0); |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 168 | |
| 169 | #ifndef NDEBUG |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 170 | MachineBasicBlock::iterator dMI = MI; |
| 171 | DOUT << "subreg: " << *(--dMI); |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 172 | #endif |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 173 | } |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 174 | |
| 175 | #ifndef NDEBUG |
| 176 | if (SrcReg == DstReg) { |
| 177 | DOUT << "subreg: Eliminated superreg copy\n"; |
| 178 | } |
| 179 | #endif |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 180 | |
Christopher Lamb | 6634e26 | 2008-03-13 05:47:01 +0000 | [diff] [blame^] | 181 | if (!revCopyOrder && (InsReg != DstSubReg || copyHasSideAffects)) { |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 182 | // Insert sub-register copy |
| 183 | const TargetRegisterClass *TRC1 = 0; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 184 | if (TargetRegisterInfo::isPhysicalRegister(InsReg)) { |
Evan Cheng | ea23781 | 2008-03-11 07:55:13 +0000 | [diff] [blame] | 185 | TRC1 = TRI.getPhysicalRegisterRegClass(InsReg); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 186 | } else { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 187 | TRC1 = MF.getRegInfo().getRegClass(InsReg); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 188 | } |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 189 | TII.copyRegToReg(*MBB, MI, DstSubReg, InsReg, TRC1, TRC1); |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 190 | |
| 191 | #ifndef NDEBUG |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 192 | MachineBasicBlock::iterator dMI = MI; |
| 193 | DOUT << "subreg: " << *(--dMI); |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 194 | #endif |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | DOUT << "\n"; |
Christopher Lamb | 8b16573 | 2007-08-10 21:11:55 +0000 | [diff] [blame] | 198 | MBB->remove(MI); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 199 | return true; |
| 200 | } |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 201 | |
| 202 | /// runOnMachineFunction - Reduce subregister inserts and extracts to register |
| 203 | /// copies. |
| 204 | /// |
| 205 | bool LowerSubregsInstructionPass::runOnMachineFunction(MachineFunction &MF) { |
| 206 | DOUT << "Machine Function\n"; |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 207 | |
| 208 | bool MadeChange = false; |
| 209 | |
| 210 | DOUT << "********** LOWERING SUBREG INSTRS **********\n"; |
| 211 | DOUT << "********** Function: " << MF.getFunction()->getName() << '\n'; |
| 212 | |
| 213 | for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end(); |
| 214 | mbbi != mbbe; ++mbbi) { |
| 215 | for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end(); |
Christopher Lamb | 9836322 | 2007-08-06 16:33:56 +0000 | [diff] [blame] | 216 | mi != me;) { |
| 217 | MachineInstr *MI = mi++; |
| 218 | |
| 219 | if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) { |
| 220 | MadeChange |= LowerExtract(MI); |
| 221 | } else if (MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG) { |
| 222 | MadeChange |= LowerInsert(MI); |
Christopher Lamb | bab2474 | 2007-07-26 08:18:32 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return MadeChange; |
| 228 | } |