1 //===-- OptimizeExts.cpp - Optimize sign / zero extension instrs -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass performs optimization of sign / zero extension instructions. It
11 // may be extended to handle other instructions of similar property.
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
18 //===----------------------------------------------------------------------===//
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"
32 static cl::opt<bool> Aggressive("aggressive-ext-opt", cl::Hidden,
33 cl::desc("Aggressive extension optimization"));
35 STATISTIC(NumReuse, "Number of extension results reused");
38 class OptimizeExts : public MachineFunctionPass {
39 const TargetMachine *TM;
40 const TargetInstrInfo *TII;
41 MachineRegisterInfo *MRI;
42 MachineDominatorTree *DT; // Machine dominator tree
45 static char ID; // Pass identification
46 OptimizeExts() : MachineFunctionPass(&ID) {}
48 virtual bool runOnMachineFunction(MachineFunction &MF);
50 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52 MachineFunctionPass::getAnalysisUsage(AU);
54 AU.addRequired<MachineDominatorTree>();
55 AU.addPreserved<MachineDominatorTree>();
60 bool OptimizeInstr(MachineInstr *MI, MachineBasicBlock *MBB,
61 SmallPtrSet<MachineInstr*, 8> &LocalMIs);
65 char OptimizeExts::ID = 0;
66 static RegisterPass<OptimizeExts>
67 X("opt-exts", "Optimize sign / zero extensions");
69 FunctionPass *llvm::createOptimizeExtsPass() { return new OptimizeExts(); }
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
76 bool OptimizeExts::OptimizeInstr(MachineInstr *MI, MachineBasicBlock *MBB,
77 SmallPtrSet<MachineInstr*, 8> &LocalMIs) {
81 unsigned SrcReg, DstReg, SubIdx;
82 if (TII->isCoalescableExtInstr(*MI, SrcReg, DstReg, SubIdx)) {
83 if (TargetRegisterInfo::isPhysicalRegister(DstReg) ||
84 TargetRegisterInfo::isPhysicalRegister(SrcReg))
87 MachineRegisterInfo::use_iterator UI = MRI->use_begin(SrcReg);
88 if (++UI == MRI->use_end())
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;
98 ReachedBBs.insert(UI->getParent());
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;
106 UI = MRI->use_begin(SrcReg);
107 for (MachineRegisterInfo::use_iterator UE = MRI->use_end(); UI != UE;
109 MachineOperand &UseMO = UI.getOperand();
110 MachineInstr *UseMI = &*UI;
113 MachineBasicBlock *UseMBB = UseMI->getParent();
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
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);
127 // Both will be live out of the def MBB anyway. Don't extend live
128 // range of the extension result.
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));
139 // Now replace all uses.
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);
160 bool OptimizeExts::runOnMachineFunction(MachineFunction &MF) {
161 TM = &MF.getTarget();
162 TII = TM->getInstrInfo();
163 MRI = &MF.getRegInfo();
164 DT = Aggressive ? &getAnalysis<MachineDominatorTree>() : 0;
166 bool Changed = false;
168 SmallPtrSet<MachineInstr*, 8> LocalMIs;
169 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
170 MachineBasicBlock *MBB = &*I;
172 for (MachineBasicBlock::iterator MII = I->begin(), ME = I->end(); MII != ME;
174 MachineInstr *MI = &*MII;
175 Changed |= OptimizeInstr(MI, MBB, LocalMIs);