1 //===-- HexagonPeephole.cpp - Hexagon Peephole Optimiztions ---------------===//
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 // This peephole pass optimizes in the following cases.
9 // 1. Optimizes redundant sign extends for the following case
10 // Transform the following pattern
11 // %vreg170<def> = SXTW %vreg166
13 // %vreg176<def> = COPY %vreg170:subreg_loreg
16 // %vreg176<def> = COPY vreg166
18 // 2. Optimizes redundant negation of predicates.
19 // %vreg15<def> = CMPGTrr %vreg6, %vreg2
21 // %vreg16<def> = NOT_p %vreg15<kill>
23 // JMP_c %vreg16<kill>, <BB#1>, %PC<imp-def,dead>
26 // %vreg15<def> = CMPGTrr %vreg6, %vreg2;
28 // JMP_cNot %vreg15<kill>, <BB#1>, %PC<imp-def,dead>;
30 // Note: The peephole pass makes the instrucstions like
31 // %vreg170<def> = SXTW %vreg166 or %vreg16<def> = NOT_p %vreg15<kill>
32 // redundant and relies on some form of dead removal instrucions, like
33 // DCE or DIE to actually eliminate them.
36 //===----------------------------------------------------------------------===//
38 #define DEBUG_TYPE "hexagon-peephole"
39 #include "llvm/Constants.h"
40 #include "llvm/PassSupport.h"
41 #include "llvm/ADT/DenseMap.h"
42 #include "llvm/ADT/Statistic.h"
43 #include "llvm/CodeGen/Passes.h"
44 #include "llvm/CodeGen/MachineFunction.h"
45 #include "llvm/CodeGen/MachineFunctionPass.h"
46 #include "llvm/CodeGen/MachineInstrBuilder.h"
47 #include "llvm/CodeGen/MachineRegisterInfo.h"
48 #include "llvm/Support/Debug.h"
49 #include "llvm/Support/raw_ostream.h"
50 #include "llvm/Target/TargetMachine.h"
51 #include "llvm/Target/TargetRegisterInfo.h"
52 #include "llvm/Target/TargetInstrInfo.h"
55 #include "HexagonTargetMachine.h"
57 #include "llvm/Support/CommandLine.h"
61 static cl::opt<bool> DisableHexagonPeephole("disable-hexagon-peephole",
62 cl::Hidden, cl::ZeroOrMore, cl::init(false),
63 cl::desc("Disable Peephole Optimization"));
66 DbgPNPCount("pnp-count", cl::init(-1), cl::Hidden,
67 cl::desc("Maximum number of P=NOT(P) to be optimized"));
69 static cl::opt<bool> DisablePNotP("disable-hexagon-pnotp",
70 cl::Hidden, cl::ZeroOrMore, cl::init(false),
71 cl::desc("Disable Optimization of PNotP"));
73 static cl::opt<bool> DisableOptSZExt("disable-hexagon-optszext",
74 cl::Hidden, cl::ZeroOrMore, cl::init(false),
75 cl::desc("Disable Optimization of Sign/Zero Extends"));
78 struct HexagonPeephole : public MachineFunctionPass {
79 const HexagonInstrInfo *QII;
80 const HexagonRegisterInfo *QRI;
81 const MachineRegisterInfo *MRI;
85 HexagonPeephole() : MachineFunctionPass(ID) { }
87 bool runOnMachineFunction(MachineFunction &MF);
89 const char *getPassName() const {
90 return "Hexagon optimize redundant zero and size extends";
93 void getAnalysisUsage(AnalysisUsage &AU) const {
94 MachineFunctionPass::getAnalysisUsage(AU);
98 void ChangeOpInto(MachineOperand &Dst, MachineOperand &Src);
102 char HexagonPeephole::ID = 0;
104 bool HexagonPeephole::runOnMachineFunction(MachineFunction &MF) {
106 QII = static_cast<const HexagonInstrInfo *>(MF.getTarget().
108 QRI = static_cast<const HexagonRegisterInfo *>(MF.getTarget().
110 MRI = &MF.getRegInfo();
112 DenseMap<unsigned, unsigned> PeepholeMap;
114 if (DisableHexagonPeephole) return false;
116 // Loop over all of the basic blocks.
117 for (MachineFunction::iterator MBBb = MF.begin(), MBBe = MF.end();
118 MBBb != MBBe; ++MBBb) {
119 MachineBasicBlock* MBB = MBBb;
122 // Traverse the basic block.
123 for (MachineBasicBlock::iterator MII = MBB->begin(); MII != MBB->end();
125 MachineInstr *MI = MII;
126 // Look for sign extends:
127 // %vreg170<def> = SXTW %vreg166
128 if (!DisableOptSZExt && MI->getOpcode() == Hexagon::SXTW) {
129 assert (MI->getNumOperands() == 2);
130 MachineOperand &Dst = MI->getOperand(0);
131 MachineOperand &Src = MI->getOperand(1);
132 unsigned DstReg = Dst.getReg();
133 unsigned SrcReg = Src.getReg();
134 // Just handle virtual registers.
135 if (TargetRegisterInfo::isVirtualRegister(DstReg) &&
136 TargetRegisterInfo::isVirtualRegister(SrcReg)) {
137 // Map the following:
138 // %vreg170<def> = SXTW %vreg166
139 // PeepholeMap[170] = vreg166
140 PeepholeMap[DstReg] = SrcReg;
144 // Look for P=NOT(P).
146 (MI->getOpcode() == Hexagon::NOT_p)) {
147 assert (MI->getNumOperands() == 2);
148 MachineOperand &Dst = MI->getOperand(0);
149 MachineOperand &Src = MI->getOperand(1);
150 unsigned DstReg = Dst.getReg();
151 unsigned SrcReg = Src.getReg();
152 // Just handle virtual registers.
153 if (TargetRegisterInfo::isVirtualRegister(DstReg) &&
154 TargetRegisterInfo::isVirtualRegister(SrcReg)) {
155 // Map the following:
156 // %vreg170<def> = NOT_xx %vreg166
157 // PeepholeMap[170] = vreg166
158 PeepholeMap[DstReg] = SrcReg;
163 // %vreg176<def> = COPY %vreg170:subreg_loreg
164 if (!DisableOptSZExt && MI->isCopy()) {
165 assert (MI->getNumOperands() == 2);
166 MachineOperand &Dst = MI->getOperand(0);
167 MachineOperand &Src = MI->getOperand(1);
169 // Make sure we are copying the lower 32 bits.
170 if (Src.getSubReg() != Hexagon::subreg_loreg)
173 unsigned DstReg = Dst.getReg();
174 unsigned SrcReg = Src.getReg();
175 if (TargetRegisterInfo::isVirtualRegister(DstReg) &&
176 TargetRegisterInfo::isVirtualRegister(SrcReg)) {
177 // Try to find in the map.
178 if (unsigned PeepholeSrc = PeepholeMap.lookup(SrcReg)) {
179 // Change the 1st operand.
180 MI->RemoveOperand(1);
181 MI->addOperand(MachineOperand::CreateReg(PeepholeSrc, false));
186 // Look for Predicated instructions.
189 if (QII->isPredicated(MI)) {
190 MachineOperand &Op0 = MI->getOperand(0);
191 unsigned Reg0 = Op0.getReg();
192 const TargetRegisterClass *RC0 = MRI->getRegClass(Reg0);
193 if (RC0->getID() == Hexagon::PredRegsRegClassID) {
194 // Handle instructions that have a prediate register in op0
195 // (most cases of predicable instructions).
196 if (TargetRegisterInfo::isVirtualRegister(Reg0)) {
197 // Try to find in the map.
198 if (unsigned PeepholeSrc = PeepholeMap.lookup(Reg0)) {
199 // Change the 1st operand and, flip the opcode.
200 MI->getOperand(0).setReg(PeepholeSrc);
201 int NewOp = QII->getInvertedPredicatedOpcode(MI->getOpcode());
202 MI->setDesc(QII->get(NewOp));
210 // Handle special instructions.
211 unsigned Op = MI->getOpcode();
213 unsigned PR = 1, S1 = 2, S2 = 3; // Operand indices.
216 case Hexagon::TFR_condset_rr:
217 case Hexagon::TFR_condset_ii:
218 case Hexagon::MUX_ii:
219 case Hexagon::MUX_rr:
222 case Hexagon::TFR_condset_ri:
223 NewOp = Hexagon::TFR_condset_ir;
225 case Hexagon::TFR_condset_ir:
226 NewOp = Hexagon::TFR_condset_ri;
228 case Hexagon::MUX_ri:
229 NewOp = Hexagon::MUX_ir;
231 case Hexagon::MUX_ir:
232 NewOp = Hexagon::MUX_ri;
236 unsigned PSrc = MI->getOperand(PR).getReg();
237 if (unsigned POrig = PeepholeMap.lookup(PSrc)) {
238 MI->getOperand(PR).setReg(POrig);
239 MI->setDesc(QII->get(NewOp));
240 // Swap operands S1 and S2.
241 MachineOperand Op1 = MI->getOperand(S1);
242 MachineOperand Op2 = MI->getOperand(S2);
243 ChangeOpInto(MI->getOperand(S1), Op2);
244 ChangeOpInto(MI->getOperand(S2), Op1);
249 } // if (!DisablePNotP)
256 void HexagonPeephole::ChangeOpInto(MachineOperand &Dst, MachineOperand &Src) {
257 assert (&Dst != &Src && "Cannot duplicate into itself");
258 switch (Dst.getType()) {
259 case MachineOperand::MO_Register:
261 Dst.setReg(Src.getReg());
262 } else if (Src.isImm()) {
263 Dst.ChangeToImmediate(Src.getImm());
265 llvm_unreachable("Unexpected src operand type");
269 case MachineOperand::MO_Immediate:
271 Dst.setImm(Src.getImm());
272 } else if (Src.isReg()) {
273 Dst.ChangeToRegister(Src.getReg(), Src.isDef(), Src.isImplicit(),
274 Src.isKill(), Src.isDead(), Src.isUndef(),
277 llvm_unreachable("Unexpected src operand type");
282 llvm_unreachable("Unexpected dst operand type");
287 FunctionPass *llvm::createHexagonPeephole() {
288 return new HexagonPeephole();