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. |
| 76 | bool OptimizeExts::OptimizeInstr(MachineInstr *MI, MachineBasicBlock *MBB, |
| 77 | SmallPtrSet<MachineInstr*, 8> &LocalMIs) { |
| 78 | bool Changed = false; |
| 79 | LocalMIs.insert(MI); |
| 80 | |
| 81 | unsigned SrcReg, DstReg, SubIdx; |
| 82 | if (TII->isCoalescableExtInstr(*MI, SrcReg, DstReg, SubIdx)) { |
| 83 | if (TargetRegisterInfo::isPhysicalRegister(DstReg) || |
| 84 | TargetRegisterInfo::isPhysicalRegister(SrcReg)) |
| 85 | return false; |
| 86 | |
| 87 | MachineRegisterInfo::use_iterator UI = MRI->use_begin(SrcReg); |
| 88 | if (++UI == MRI->use_end()) |
| 89 | // No other uses. |
| 90 | return false; |
| 91 | |
| 92 | // Ok, the source has other uses. See if we can replace the other uses |
| 93 | // with use of the result of the extension. |
| 94 | SmallPtrSet<MachineBasicBlock*, 4> ReachedBBs; |
| 95 | UI = MRI->use_begin(DstReg); |
| 96 | for (MachineRegisterInfo::use_iterator UE = MRI->use_end(); UI != UE; |
| 97 | ++UI) |
| 98 | ReachedBBs.insert(UI->getParent()); |
| 99 | |
| 100 | bool ExtendLife = true; |
| 101 | // Uses that are in the same BB of uses of the result of the instruction. |
| 102 | SmallVector<MachineOperand*, 8> Uses; |
| 103 | // Uses that the result of the instruction can reach. |
| 104 | SmallVector<MachineOperand*, 8> ExtendedUses; |
| 105 | |
| 106 | UI = MRI->use_begin(SrcReg); |
| 107 | for (MachineRegisterInfo::use_iterator UE = MRI->use_end(); UI != UE; |
| 108 | ++UI) { |
| 109 | MachineOperand &UseMO = UI.getOperand(); |
| 110 | MachineInstr *UseMI = &*UI; |
| 111 | if (UseMI == MI) |
| 112 | continue; |
| 113 | MachineBasicBlock *UseMBB = UseMI->getParent(); |
| 114 | if (UseMBB == MBB) { |
| 115 | // Local uses that come after the extension. |
| 116 | if (!LocalMIs.count(UseMI)) |
| 117 | Uses.push_back(&UseMO); |
| 118 | } else if (ReachedBBs.count(UseMBB)) |
| 119 | // Non-local uses where the result of extension is used. Always |
| 120 | // replace these. |
| 121 | Uses.push_back(&UseMO); |
| 122 | else if (Aggressive && DT->dominates(MBB, UseMBB)) |
| 123 | // We may want to extend live range of the extension result in order |
| 124 | // to replace these uses. |
| 125 | ExtendedUses.push_back(&UseMO); |
| 126 | else { |
| 127 | // Both will be live out of the def MBB anyway. Don't extend live |
| 128 | // range of the extension result. |
| 129 | ExtendLife = false; |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if (ExtendLife && !ExtendedUses.empty()) |
| 135 | // Ok, we'll extend the liveness of the extension result. |
| 136 | std::copy(ExtendedUses.begin(), ExtendedUses.end(), |
| 137 | std::back_inserter(Uses)); |
| 138 | |
| 139 | // Now replace all uses. |
| 140 | if (!Uses.empty()) { |
| 141 | const TargetRegisterClass *RC = MRI->getRegClass(SrcReg); |
| 142 | for (unsigned i = 0, e = Uses.size(); i != e; ++i) { |
| 143 | MachineOperand *UseMO = Uses[i]; |
| 144 | MachineInstr *UseMI = UseMO->getParent(); |
| 145 | MachineBasicBlock *UseMBB = UseMI->getParent(); |
| 146 | unsigned NewVR = MRI->createVirtualRegister(RC); |
| 147 | BuildMI(*UseMBB, UseMI, UseMI->getDebugLoc(), |
| 148 | TII->get(TargetInstrInfo::EXTRACT_SUBREG), NewVR) |
| 149 | .addReg(DstReg).addImm(SubIdx); |
| 150 | UseMO->setReg(NewVR); |
| 151 | ++NumReuse; |
| 152 | Changed = true; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return Changed; |
| 158 | } |
| 159 | |
Evan Cheng | 7da9ecf | 2010-01-13 00:30:23 +0000 | [diff] [blame] | 160 | bool OptimizeExts::runOnMachineFunction(MachineFunction &MF) { |
| 161 | TM = &MF.getTarget(); |
| 162 | TII = TM->getInstrInfo(); |
| 163 | MRI = &MF.getRegInfo(); |
Evan Cheng | d89d518 | 2010-01-13 07:59:13 +0000 | [diff] [blame^] | 164 | DT = Aggressive ? &getAnalysis<MachineDominatorTree>() : 0; |
Evan Cheng | 7da9ecf | 2010-01-13 00:30:23 +0000 | [diff] [blame] | 165 | |
| 166 | bool Changed = false; |
| 167 | |
| 168 | SmallPtrSet<MachineInstr*, 8> LocalMIs; |
| 169 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) { |
| 170 | MachineBasicBlock *MBB = &*I; |
Evan Cheng | d89d518 | 2010-01-13 07:59:13 +0000 | [diff] [blame^] | 171 | LocalMIs.clear(); |
Evan Cheng | 7da9ecf | 2010-01-13 00:30:23 +0000 | [diff] [blame] | 172 | for (MachineBasicBlock::iterator MII = I->begin(), ME = I->end(); MII != ME; |
| 173 | ++MII) { |
| 174 | MachineInstr *MI = &*MII; |
Evan Cheng | d89d518 | 2010-01-13 07:59:13 +0000 | [diff] [blame^] | 175 | Changed |= OptimizeInstr(MI, MBB, LocalMIs); |
Evan Cheng | 7da9ecf | 2010-01-13 00:30:23 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | return Changed; |
| 180 | } |