Evan Cheng | 7da9ecf | 2010-01-13 00:30:23 +0000 | [diff] [blame] | 1 | //===-- OptimizeExts.cpp - Optimize sign / zero extension instrs -----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Evan Cheng | d89d518 | 2010-01-13 07:59:13 +0000 | [diff] [blame] | 9 | // |
| 10 | // This pass performs optimization of sign / zero extension instructions. It |
| 11 | // may be extended to handle other instructions of similar property. |
| 12 | // |
| 13 | // On some targets, some instructions, e.g. X86 sign / zero extension, may |
| 14 | // leave the source value in the lower part of the result. This pass will |
| 15 | // replace (some) uses of the pre-extension value with uses of the sub-register |
| 16 | // of the results. |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
Evan Cheng | 7da9ecf | 2010-01-13 00:30:23 +0000 | [diff] [blame] | 19 | |
| 20 | #define DEBUG_TYPE "ext-opt" |
| 21 | #include "llvm/CodeGen/Passes.h" |
| 22 | #include "llvm/CodeGen/MachineDominators.h" |
| 23 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 25 | #include "llvm/Target/TargetInstrInfo.h" |
| 26 | #include "llvm/Target/TargetRegisterInfo.h" |
| 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/ADT/SmallPtrSet.h" |
| 29 | #include "llvm/ADT/Statistic.h" |
| 30 | using namespace llvm; |
| 31 | |
| 32 | static cl::opt<bool> Aggressive("aggressive-ext-opt", cl::Hidden, |
| 33 | cl::desc("Aggressive extension optimization")); |
| 34 | |
| 35 | STATISTIC(NumReuse, "Number of extension results reused"); |
| 36 | |
| 37 | namespace { |
| 38 | class OptimizeExts : public MachineFunctionPass { |
| 39 | const TargetMachine *TM; |
| 40 | const TargetInstrInfo *TII; |
| 41 | MachineRegisterInfo *MRI; |
| 42 | MachineDominatorTree *DT; // Machine dominator tree |
| 43 | |
| 44 | public: |
| 45 | static char ID; // Pass identification |
| 46 | OptimizeExts() : MachineFunctionPass(&ID) {} |
| 47 | |
| 48 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 49 | |
| 50 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 51 | AU.setPreservesCFG(); |
| 52 | MachineFunctionPass::getAnalysisUsage(AU); |
Evan Cheng | d89d518 | 2010-01-13 07:59:13 +0000 | [diff] [blame] | 53 | if (Aggressive) { |
| 54 | AU.addRequired<MachineDominatorTree>(); |
| 55 | AU.addPreserved<MachineDominatorTree>(); |
| 56 | } |
Evan Cheng | 7da9ecf | 2010-01-13 00:30:23 +0000 | [diff] [blame] | 57 | } |
Evan Cheng | d89d518 | 2010-01-13 07:59:13 +0000 | [diff] [blame] | 58 | |
| 59 | private: |
| 60 | bool OptimizeInstr(MachineInstr *MI, MachineBasicBlock *MBB, |
| 61 | SmallPtrSet<MachineInstr*, 8> &LocalMIs); |
Evan Cheng | 7da9ecf | 2010-01-13 00:30:23 +0000 | [diff] [blame] | 62 | }; |
| 63 | } |
| 64 | |
| 65 | char OptimizeExts::ID = 0; |
| 66 | static RegisterPass<OptimizeExts> |
| 67 | X("opt-exts", "Optimize sign / zero extensions"); |
| 68 | |
| 69 | FunctionPass *llvm::createOptimizeExtsPass() { return new OptimizeExts(); } |
| 70 | |
Evan Cheng | d89d518 | 2010-01-13 07:59:13 +0000 | [diff] [blame] | 71 | /// OptimizeInstr - If instruction is a copy-like instruction, i.e. it reads |
| 72 | /// a single register and writes a single register and it does not modify |
| 73 | /// the source, and if the source value is preserved as a sub-register of |
| 74 | /// the result, then replace all reachable uses of the source with the subreg |
| 75 | /// of the result. |
Dale Johannesen | 1feeada | 2010-03-26 00:02:44 +0000 | [diff] [blame] | 76 | /// Do not generate an EXTRACT that is used only in a debug use, as this |
| 77 | /// changes the code. Since this code does not currently share EXTRACTs, just |
| 78 | /// ignore all debug uses. |
Evan Cheng | d89d518 | 2010-01-13 07:59:13 +0000 | [diff] [blame] | 79 | bool OptimizeExts::OptimizeInstr(MachineInstr *MI, MachineBasicBlock *MBB, |
| 80 | SmallPtrSet<MachineInstr*, 8> &LocalMIs) { |
| 81 | bool Changed = false; |
| 82 | LocalMIs.insert(MI); |
| 83 | |
| 84 | unsigned SrcReg, DstReg, SubIdx; |
| 85 | if (TII->isCoalescableExtInstr(*MI, SrcReg, DstReg, SubIdx)) { |
| 86 | if (TargetRegisterInfo::isPhysicalRegister(DstReg) || |
| 87 | TargetRegisterInfo::isPhysicalRegister(SrcReg)) |
| 88 | return false; |
| 89 | |
Dale Johannesen | 1feeada | 2010-03-26 00:02:44 +0000 | [diff] [blame] | 90 | MachineRegisterInfo::use_nodbg_iterator UI = MRI->use_nodbg_begin(SrcReg); |
| 91 | if (++UI == MRI->use_nodbg_end()) |
Evan Cheng | d89d518 | 2010-01-13 07:59:13 +0000 | [diff] [blame] | 92 | // No other uses. |
| 93 | return false; |
| 94 | |
| 95 | // Ok, the source has other uses. See if we can replace the other uses |
| 96 | // with use of the result of the extension. |
| 97 | SmallPtrSet<MachineBasicBlock*, 4> ReachedBBs; |
Dale Johannesen | 1feeada | 2010-03-26 00:02:44 +0000 | [diff] [blame] | 98 | UI = MRI->use_nodbg_begin(DstReg); |
| 99 | for (MachineRegisterInfo::use_nodbg_iterator UE = MRI->use_nodbg_end(); |
| 100 | UI != UE; ++UI) |
Evan Cheng | d89d518 | 2010-01-13 07:59:13 +0000 | [diff] [blame] | 101 | ReachedBBs.insert(UI->getParent()); |
| 102 | |
| 103 | bool ExtendLife = true; |
| 104 | // Uses that are in the same BB of uses of the result of the instruction. |
| 105 | SmallVector<MachineOperand*, 8> Uses; |
| 106 | // Uses that the result of the instruction can reach. |
| 107 | SmallVector<MachineOperand*, 8> ExtendedUses; |
| 108 | |
Dale Johannesen | 1feeada | 2010-03-26 00:02:44 +0000 | [diff] [blame] | 109 | UI = MRI->use_nodbg_begin(SrcReg); |
| 110 | for (MachineRegisterInfo::use_nodbg_iterator UE = MRI->use_nodbg_end(); |
| 111 | UI != UE; ++UI) { |
Evan Cheng | d89d518 | 2010-01-13 07:59:13 +0000 | [diff] [blame] | 112 | MachineOperand &UseMO = UI.getOperand(); |
| 113 | MachineInstr *UseMI = &*UI; |
| 114 | if (UseMI == MI) |
| 115 | continue; |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 116 | if (UseMI->isPHI()) { |
Evan Cheng | eb18812 | 2010-01-13 19:16:39 +0000 | [diff] [blame] | 117 | ExtendLife = false; |
| 118 | continue; |
| 119 | } |
| 120 | |
Bill Wendling | d64ba3e | 2010-06-09 19:00:55 +0000 | [diff] [blame] | 121 | // It's an error to translate this: |
| 122 | // |
| 123 | // %reg1025 = <sext> %reg1024 |
| 124 | // ... |
| 125 | // %reg1026 = SUBREG_TO_REG 0, %reg1024, 4 |
| 126 | // |
| 127 | // into this: |
| 128 | // |
| 129 | // %reg1025 = <sext> %reg1024 |
| 130 | // ... |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 131 | // %reg1027 = COPY %reg1025:4 |
Bill Wendling | d64ba3e | 2010-06-09 19:00:55 +0000 | [diff] [blame] | 132 | // %reg1026 = SUBREG_TO_REG 0, %reg1027, 4 |
| 133 | // |
| 134 | // The problem here is that SUBREG_TO_REG is there to assert that an |
| 135 | // implicit zext occurs. It doesn't insert a zext instruction. If we allow |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 136 | // the COPY here, it will give us the value after the <sext>, |
Bill Wendling | d64ba3e | 2010-06-09 19:00:55 +0000 | [diff] [blame] | 137 | // not the original value of %reg1024 before <sext>. |
| 138 | if (UseMI->getOpcode() == TargetOpcode::SUBREG_TO_REG) |
| 139 | continue; |
| 140 | |
Evan Cheng | d89d518 | 2010-01-13 07:59:13 +0000 | [diff] [blame] | 141 | MachineBasicBlock *UseMBB = UseMI->getParent(); |
| 142 | if (UseMBB == MBB) { |
| 143 | // Local uses that come after the extension. |
| 144 | if (!LocalMIs.count(UseMI)) |
| 145 | Uses.push_back(&UseMO); |
| 146 | } else if (ReachedBBs.count(UseMBB)) |
| 147 | // Non-local uses where the result of extension is used. Always |
Evan Cheng | eb18812 | 2010-01-13 19:16:39 +0000 | [diff] [blame] | 148 | // replace these unless it's a PHI. |
Evan Cheng | d89d518 | 2010-01-13 07:59:13 +0000 | [diff] [blame] | 149 | Uses.push_back(&UseMO); |
| 150 | else if (Aggressive && DT->dominates(MBB, UseMBB)) |
| 151 | // We may want to extend live range of the extension result in order |
| 152 | // to replace these uses. |
| 153 | ExtendedUses.push_back(&UseMO); |
| 154 | else { |
| 155 | // Both will be live out of the def MBB anyway. Don't extend live |
| 156 | // range of the extension result. |
| 157 | ExtendLife = false; |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if (ExtendLife && !ExtendedUses.empty()) |
| 163 | // Ok, we'll extend the liveness of the extension result. |
| 164 | std::copy(ExtendedUses.begin(), ExtendedUses.end(), |
| 165 | std::back_inserter(Uses)); |
| 166 | |
| 167 | // Now replace all uses. |
| 168 | if (!Uses.empty()) { |
Evan Cheng | 590d16b | 2010-01-19 19:45:51 +0000 | [diff] [blame] | 169 | SmallPtrSet<MachineBasicBlock*, 4> PHIBBs; |
| 170 | // Look for PHI uses of the extended result, we don't want to extend the |
| 171 | // liveness of a PHI input. It breaks all kinds of assumptions down |
| 172 | // stream. A PHI use is expected to be the kill of its source values. |
Dale Johannesen | 1feeada | 2010-03-26 00:02:44 +0000 | [diff] [blame] | 173 | UI = MRI->use_nodbg_begin(DstReg); |
| 174 | for (MachineRegisterInfo::use_nodbg_iterator UE = MRI->use_nodbg_end(); |
| 175 | UI != UE; ++UI) |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 176 | if (UI->isPHI()) |
Evan Cheng | 590d16b | 2010-01-19 19:45:51 +0000 | [diff] [blame] | 177 | PHIBBs.insert(UI->getParent()); |
| 178 | |
Evan Cheng | d89d518 | 2010-01-13 07:59:13 +0000 | [diff] [blame] | 179 | const TargetRegisterClass *RC = MRI->getRegClass(SrcReg); |
| 180 | for (unsigned i = 0, e = Uses.size(); i != e; ++i) { |
| 181 | MachineOperand *UseMO = Uses[i]; |
| 182 | MachineInstr *UseMI = UseMO->getParent(); |
| 183 | MachineBasicBlock *UseMBB = UseMI->getParent(); |
Evan Cheng | 590d16b | 2010-01-19 19:45:51 +0000 | [diff] [blame] | 184 | if (PHIBBs.count(UseMBB)) |
| 185 | continue; |
Evan Cheng | d89d518 | 2010-01-13 07:59:13 +0000 | [diff] [blame] | 186 | unsigned NewVR = MRI->createVirtualRegister(RC); |
| 187 | BuildMI(*UseMBB, UseMI, UseMI->getDebugLoc(), |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 188 | TII->get(TargetOpcode::COPY), NewVR) |
| 189 | .addReg(DstReg, 0, SubIdx); |
Evan Cheng | d89d518 | 2010-01-13 07:59:13 +0000 | [diff] [blame] | 190 | UseMO->setReg(NewVR); |
| 191 | ++NumReuse; |
| 192 | Changed = true; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | return Changed; |
| 198 | } |
| 199 | |
Evan Cheng | 7da9ecf | 2010-01-13 00:30:23 +0000 | [diff] [blame] | 200 | bool OptimizeExts::runOnMachineFunction(MachineFunction &MF) { |
| 201 | TM = &MF.getTarget(); |
| 202 | TII = TM->getInstrInfo(); |
| 203 | MRI = &MF.getRegInfo(); |
Evan Cheng | d89d518 | 2010-01-13 07:59:13 +0000 | [diff] [blame] | 204 | DT = Aggressive ? &getAnalysis<MachineDominatorTree>() : 0; |
Evan Cheng | 7da9ecf | 2010-01-13 00:30:23 +0000 | [diff] [blame] | 205 | |
| 206 | bool Changed = false; |
| 207 | |
| 208 | SmallPtrSet<MachineInstr*, 8> LocalMIs; |
| 209 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) { |
| 210 | MachineBasicBlock *MBB = &*I; |
Evan Cheng | d89d518 | 2010-01-13 07:59:13 +0000 | [diff] [blame] | 211 | LocalMIs.clear(); |
Evan Cheng | 7da9ecf | 2010-01-13 00:30:23 +0000 | [diff] [blame] | 212 | for (MachineBasicBlock::iterator MII = I->begin(), ME = I->end(); MII != ME; |
| 213 | ++MII) { |
| 214 | MachineInstr *MI = &*MII; |
Evan Cheng | d89d518 | 2010-01-13 07:59:13 +0000 | [diff] [blame] | 215 | Changed |= OptimizeInstr(MI, MBB, LocalMIs); |
Evan Cheng | 7da9ecf | 2010-01-13 00:30:23 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | return Changed; |
| 220 | } |