blob: ee3209354688d8a36eb83534fb3c51db14846cb7 [file] [log] [blame]
Sirish Pande30804c22012-02-15 18:52:27 +00001//===-- HexagonPeephole.cpp - Hexagon Peephole Optimiztions ---------------===//
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// 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
12// ...
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +000013// %vreg176<def> = COPY %vreg170:isub_lo
Sirish Pande30804c22012-02-15 18:52:27 +000014//
15// Into
16// %vreg176<def> = COPY vreg166
17//
18// 2. Optimizes redundant negation of predicates.
19// %vreg15<def> = CMPGTrr %vreg6, %vreg2
20// ...
21// %vreg16<def> = NOT_p %vreg15<kill>
22// ...
23// JMP_c %vreg16<kill>, <BB#1>, %PC<imp-def,dead>
24//
25// Into
26// %vreg15<def> = CMPGTrr %vreg6, %vreg2;
27// ...
28// JMP_cNot %vreg15<kill>, <BB#1>, %PC<imp-def,dead>;
29//
30// Note: The peephole pass makes the instrucstions like
31// %vreg170<def> = SXTW %vreg166 or %vreg16<def> = NOT_p %vreg15<kill>
Robert Wilhelm2788d3e2013-09-28 13:42:22 +000032// redundant and relies on some form of dead removal instructions, like
Sirish Pande30804c22012-02-15 18:52:27 +000033// DCE or DIE to actually eliminate them.
34
35
36//===----------------------------------------------------------------------===//
37
Craig Topperb25fda92012-03-17 18:46:09 +000038#include "Hexagon.h"
39#include "HexagonTargetMachine.h"
Sirish Pande30804c22012-02-15 18:52:27 +000040#include "llvm/ADT/DenseMap.h"
41#include "llvm/ADT/Statistic.h"
Sirish Pande30804c22012-02-15 18:52:27 +000042#include "llvm/CodeGen/MachineFunction.h"
43#include "llvm/CodeGen/MachineFunctionPass.h"
44#include "llvm/CodeGen/MachineInstrBuilder.h"
45#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000046#include "llvm/CodeGen/Passes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000047#include "llvm/IR/Constants.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000048#include "llvm/PassSupport.h"
Craig Topperb25fda92012-03-17 18:46:09 +000049#include "llvm/Support/CommandLine.h"
Sirish Pande30804c22012-02-15 18:52:27 +000050#include "llvm/Support/Debug.h"
51#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000052#include "llvm/Target/TargetInstrInfo.h"
Sirish Pande30804c22012-02-15 18:52:27 +000053#include "llvm/Target/TargetMachine.h"
54#include "llvm/Target/TargetRegisterInfo.h"
Sirish Pande30804c22012-02-15 18:52:27 +000055#include <algorithm>
Sirish Pande30804c22012-02-15 18:52:27 +000056
57using namespace llvm;
58
Chandler Carruth84e68b22014-04-22 02:41:26 +000059#define DEBUG_TYPE "hexagon-peephole"
60
Sirish Pande30804c22012-02-15 18:52:27 +000061static cl::opt<bool> DisableHexagonPeephole("disable-hexagon-peephole",
62 cl::Hidden, cl::ZeroOrMore, cl::init(false),
63 cl::desc("Disable Peephole Optimization"));
64
Sirish Pande30804c22012-02-15 18:52:27 +000065static cl::opt<bool> DisablePNotP("disable-hexagon-pnotp",
66 cl::Hidden, cl::ZeroOrMore, cl::init(false),
67 cl::desc("Disable Optimization of PNotP"));
68
69static cl::opt<bool> DisableOptSZExt("disable-hexagon-optszext",
Krzysztof Parzyszek3af70c12016-04-19 21:36:24 +000070 cl::Hidden, cl::ZeroOrMore, cl::init(true),
Sirish Pande30804c22012-02-15 18:52:27 +000071 cl::desc("Disable Optimization of Sign/Zero Extends"));
72
Pranav Bhandarkar7dda9122013-05-02 20:22:51 +000073static cl::opt<bool> DisableOptExtTo64("disable-hexagon-opt-ext-to-64",
Krzysztof Parzyszek3af70c12016-04-19 21:36:24 +000074 cl::Hidden, cl::ZeroOrMore, cl::init(true),
Pranav Bhandarkar7dda9122013-05-02 20:22:51 +000075 cl::desc("Disable Optimization of extensions to i64."));
76
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000077namespace llvm {
Colin LeMahieu56efafc2015-06-15 19:05:35 +000078 FunctionPass *createHexagonPeephole();
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000079 void initializeHexagonPeepholePass(PassRegistry&);
80}
81
Sirish Pande30804c22012-02-15 18:52:27 +000082namespace {
83 struct HexagonPeephole : public MachineFunctionPass {
84 const HexagonInstrInfo *QII;
85 const HexagonRegisterInfo *QRI;
86 const MachineRegisterInfo *MRI;
87
88 public:
89 static char ID;
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000090 HexagonPeephole() : MachineFunctionPass(ID) {
91 initializeHexagonPeepholePass(*PassRegistry::getPassRegistry());
92 }
Sirish Pande30804c22012-02-15 18:52:27 +000093
Craig Topper906c2cd2014-04-29 07:58:16 +000094 bool runOnMachineFunction(MachineFunction &MF) override;
Sirish Pande30804c22012-02-15 18:52:27 +000095
Mehdi Amini117296c2016-10-01 02:56:57 +000096 StringRef getPassName() const override {
Sirish Pande30804c22012-02-15 18:52:27 +000097 return "Hexagon optimize redundant zero and size extends";
98 }
99
Craig Topper906c2cd2014-04-29 07:58:16 +0000100 void getAnalysisUsage(AnalysisUsage &AU) const override {
Sirish Pande30804c22012-02-15 18:52:27 +0000101 MachineFunctionPass::getAnalysisUsage(AU);
102 }
103
104 private:
105 void ChangeOpInto(MachineOperand &Dst, MachineOperand &Src);
106 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000107}
Sirish Pande30804c22012-02-15 18:52:27 +0000108
109char HexagonPeephole::ID = 0;
110
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +0000111INITIALIZE_PASS(HexagonPeephole, "hexagon-peephole", "Hexagon Peephole",
112 false, false)
Sirish Pande30804c22012-02-15 18:52:27 +0000113
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +0000114bool HexagonPeephole::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000115 if (skipFunction(*MF.getFunction()))
116 return false;
117
Eric Christopherfc6de422014-08-05 02:39:49 +0000118 QII = static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo());
Eric Christopherd5c235d2015-02-02 22:40:56 +0000119 QRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Sirish Pande30804c22012-02-15 18:52:27 +0000120 MRI = &MF.getRegInfo();
121
122 DenseMap<unsigned, unsigned> PeepholeMap;
Pranav Bhandarkar823f9eb2012-09-05 16:01:40 +0000123 DenseMap<unsigned, std::pair<unsigned, unsigned> > PeepholeDoubleRegsMap;
Sirish Pande30804c22012-02-15 18:52:27 +0000124
125 if (DisableHexagonPeephole) return false;
126
127 // Loop over all of the basic blocks.
128 for (MachineFunction::iterator MBBb = MF.begin(), MBBe = MF.end();
129 MBBb != MBBe; ++MBBb) {
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000130 MachineBasicBlock *MBB = &*MBBb;
Sirish Pande30804c22012-02-15 18:52:27 +0000131 PeepholeMap.clear();
Pranav Bhandarkar823f9eb2012-09-05 16:01:40 +0000132 PeepholeDoubleRegsMap.clear();
Sirish Pande30804c22012-02-15 18:52:27 +0000133
134 // Traverse the basic block.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000135 for (MachineInstr &MI : *MBB) {
Sirish Pande30804c22012-02-15 18:52:27 +0000136 // Look for sign extends:
137 // %vreg170<def> = SXTW %vreg166
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000138 if (!DisableOptSZExt && MI.getOpcode() == Hexagon::A2_sxtw) {
139 assert(MI.getNumOperands() == 2);
140 MachineOperand &Dst = MI.getOperand(0);
141 MachineOperand &Src = MI.getOperand(1);
Sirish Pande30804c22012-02-15 18:52:27 +0000142 unsigned DstReg = Dst.getReg();
143 unsigned SrcReg = Src.getReg();
144 // Just handle virtual registers.
145 if (TargetRegisterInfo::isVirtualRegister(DstReg) &&
146 TargetRegisterInfo::isVirtualRegister(SrcReg)) {
147 // Map the following:
148 // %vreg170<def> = SXTW %vreg166
149 // PeepholeMap[170] = vreg166
150 PeepholeMap[DstReg] = SrcReg;
151 }
152 }
153
Pranav Bhandarkar7dda9122013-05-02 20:22:51 +0000154 // Look for %vreg170<def> = COMBINE_ir_V4 (0, %vreg169)
155 // %vreg170:DoublRegs, %vreg169:IntRegs
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000156 if (!DisableOptExtTo64 && MI.getOpcode() == Hexagon::A4_combineir) {
157 assert(MI.getNumOperands() == 3);
158 MachineOperand &Dst = MI.getOperand(0);
159 MachineOperand &Src1 = MI.getOperand(1);
160 MachineOperand &Src2 = MI.getOperand(2);
Pranav Bhandarkar7dda9122013-05-02 20:22:51 +0000161 if (Src1.getImm() != 0)
162 continue;
163 unsigned DstReg = Dst.getReg();
164 unsigned SrcReg = Src2.getReg();
165 PeepholeMap[DstReg] = SrcReg;
166 }
167
Pranav Bhandarkar823f9eb2012-09-05 16:01:40 +0000168 // Look for this sequence below
169 // %vregDoubleReg1 = LSRd_ri %vregDoubleReg0, 32
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000170 // %vregIntReg = COPY %vregDoubleReg1:isub_lo.
Pranav Bhandarkar823f9eb2012-09-05 16:01:40 +0000171 // and convert into
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000172 // %vregIntReg = COPY %vregDoubleReg0:isub_hi.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000173 if (MI.getOpcode() == Hexagon::S2_lsr_i_p) {
174 assert(MI.getNumOperands() == 3);
175 MachineOperand &Dst = MI.getOperand(0);
176 MachineOperand &Src1 = MI.getOperand(1);
177 MachineOperand &Src2 = MI.getOperand(2);
Pranav Bhandarkar823f9eb2012-09-05 16:01:40 +0000178 if (Src2.getImm() != 32)
179 continue;
180 unsigned DstReg = Dst.getReg();
181 unsigned SrcReg = Src1.getReg();
182 PeepholeDoubleRegsMap[DstReg] =
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000183 std::make_pair(*&SrcReg, Hexagon::isub_hi);
Pranav Bhandarkar823f9eb2012-09-05 16:01:40 +0000184 }
185
Sirish Pande30804c22012-02-15 18:52:27 +0000186 // Look for P=NOT(P).
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000187 if (!DisablePNotP && MI.getOpcode() == Hexagon::C2_not) {
188 assert(MI.getNumOperands() == 2);
189 MachineOperand &Dst = MI.getOperand(0);
190 MachineOperand &Src = MI.getOperand(1);
Sirish Pande30804c22012-02-15 18:52:27 +0000191 unsigned DstReg = Dst.getReg();
192 unsigned SrcReg = Src.getReg();
193 // Just handle virtual registers.
194 if (TargetRegisterInfo::isVirtualRegister(DstReg) &&
195 TargetRegisterInfo::isVirtualRegister(SrcReg)) {
196 // Map the following:
197 // %vreg170<def> = NOT_xx %vreg166
198 // PeepholeMap[170] = vreg166
199 PeepholeMap[DstReg] = SrcReg;
200 }
201 }
202
203 // Look for copy:
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000204 // %vreg176<def> = COPY %vreg170:isub_lo
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000205 if (!DisableOptSZExt && MI.isCopy()) {
206 assert(MI.getNumOperands() == 2);
207 MachineOperand &Dst = MI.getOperand(0);
208 MachineOperand &Src = MI.getOperand(1);
Sirish Pande30804c22012-02-15 18:52:27 +0000209
210 // Make sure we are copying the lower 32 bits.
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000211 if (Src.getSubReg() != Hexagon::isub_lo)
Sirish Pande30804c22012-02-15 18:52:27 +0000212 continue;
213
214 unsigned DstReg = Dst.getReg();
215 unsigned SrcReg = Src.getReg();
216 if (TargetRegisterInfo::isVirtualRegister(DstReg) &&
217 TargetRegisterInfo::isVirtualRegister(SrcReg)) {
218 // Try to find in the map.
219 if (unsigned PeepholeSrc = PeepholeMap.lookup(SrcReg)) {
220 // Change the 1st operand.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000221 MI.RemoveOperand(1);
222 MI.addOperand(MachineOperand::CreateReg(PeepholeSrc, false));
Pranav Bhandarkar823f9eb2012-09-05 16:01:40 +0000223 } else {
224 DenseMap<unsigned, std::pair<unsigned, unsigned> >::iterator DI =
225 PeepholeDoubleRegsMap.find(SrcReg);
226 if (DI != PeepholeDoubleRegsMap.end()) {
227 std::pair<unsigned,unsigned> PeepholeSrc = DI->second;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000228 MI.RemoveOperand(1);
229 MI.addOperand(MachineOperand::CreateReg(
230 PeepholeSrc.first, false /*isDef*/, false /*isImp*/,
231 false /*isKill*/, false /*isDead*/, false /*isUndef*/,
232 false /*isEarlyClobber*/, PeepholeSrc.second));
Pranav Bhandarkar823f9eb2012-09-05 16:01:40 +0000233 }
Sirish Pande30804c22012-02-15 18:52:27 +0000234 }
235 }
236 }
237
238 // Look for Predicated instructions.
239 if (!DisablePNotP) {
240 bool Done = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000241 if (QII->isPredicated(MI)) {
242 MachineOperand &Op0 = MI.getOperand(0);
Sirish Pande30804c22012-02-15 18:52:27 +0000243 unsigned Reg0 = Op0.getReg();
244 const TargetRegisterClass *RC0 = MRI->getRegClass(Reg0);
245 if (RC0->getID() == Hexagon::PredRegsRegClassID) {
246 // Handle instructions that have a prediate register in op0
247 // (most cases of predicable instructions).
248 if (TargetRegisterInfo::isVirtualRegister(Reg0)) {
249 // Try to find in the map.
250 if (unsigned PeepholeSrc = PeepholeMap.lookup(Reg0)) {
251 // Change the 1st operand and, flip the opcode.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000252 MI.getOperand(0).setReg(PeepholeSrc);
Krzysztof Parzyszek7773c582016-08-04 14:17:16 +0000253 MRI->clearKillFlags(PeepholeSrc);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000254 int NewOp = QII->getInvertedPredicatedOpcode(MI.getOpcode());
255 MI.setDesc(QII->get(NewOp));
Sirish Pande30804c22012-02-15 18:52:27 +0000256 Done = true;
257 }
258 }
259 }
260 }
261
262 if (!Done) {
263 // Handle special instructions.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000264 unsigned Op = MI.getOpcode();
Sirish Pande30804c22012-02-15 18:52:27 +0000265 unsigned NewOp = 0;
266 unsigned PR = 1, S1 = 2, S2 = 3; // Operand indices.
267
268 switch (Op) {
Colin LeMahieue83bc742014-11-25 20:20:09 +0000269 case Hexagon::C2_mux:
Colin LeMahieu9665f982014-12-05 21:09:27 +0000270 case Hexagon::C2_muxii:
Sirish Pande30804c22012-02-15 18:52:27 +0000271 NewOp = Op;
272 break;
Colin LeMahieu9665f982014-12-05 21:09:27 +0000273 case Hexagon::C2_muxri:
274 NewOp = Hexagon::C2_muxir;
Sirish Pande30804c22012-02-15 18:52:27 +0000275 break;
Colin LeMahieu9665f982014-12-05 21:09:27 +0000276 case Hexagon::C2_muxir:
277 NewOp = Hexagon::C2_muxri;
Sirish Pande30804c22012-02-15 18:52:27 +0000278 break;
279 }
280 if (NewOp) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000281 unsigned PSrc = MI.getOperand(PR).getReg();
Sirish Pande30804c22012-02-15 18:52:27 +0000282 if (unsigned POrig = PeepholeMap.lookup(PSrc)) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000283 MI.getOperand(PR).setReg(POrig);
Krzysztof Parzyszek7773c582016-08-04 14:17:16 +0000284 MRI->clearKillFlags(POrig);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000285 MI.setDesc(QII->get(NewOp));
Sirish Pande30804c22012-02-15 18:52:27 +0000286 // Swap operands S1 and S2.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000287 MachineOperand Op1 = MI.getOperand(S1);
288 MachineOperand Op2 = MI.getOperand(S2);
289 ChangeOpInto(MI.getOperand(S1), Op2);
290 ChangeOpInto(MI.getOperand(S2), Op1);
Sirish Pande30804c22012-02-15 18:52:27 +0000291 }
292 } // if (NewOp)
293 } // if (!Done)
294
295 } // if (!DisablePNotP)
296
297 } // Instruction
298 } // Basic Block
299 return true;
300}
301
302void HexagonPeephole::ChangeOpInto(MachineOperand &Dst, MachineOperand &Src) {
303 assert (&Dst != &Src && "Cannot duplicate into itself");
304 switch (Dst.getType()) {
305 case MachineOperand::MO_Register:
306 if (Src.isReg()) {
307 Dst.setReg(Src.getReg());
Krzysztof Parzyszek3af70c12016-04-19 21:36:24 +0000308 Dst.setSubReg(Src.getSubReg());
Krzysztof Parzyszek7773c582016-08-04 14:17:16 +0000309 MRI->clearKillFlags(Src.getReg());
Sirish Pande30804c22012-02-15 18:52:27 +0000310 } else if (Src.isImm()) {
311 Dst.ChangeToImmediate(Src.getImm());
312 } else {
313 llvm_unreachable("Unexpected src operand type");
314 }
315 break;
316
317 case MachineOperand::MO_Immediate:
318 if (Src.isImm()) {
319 Dst.setImm(Src.getImm());
320 } else if (Src.isReg()) {
321 Dst.ChangeToRegister(Src.getReg(), Src.isDef(), Src.isImplicit(),
Krzysztof Parzyszek7773c582016-08-04 14:17:16 +0000322 false, Src.isDead(), Src.isUndef(),
Sirish Pande30804c22012-02-15 18:52:27 +0000323 Src.isDebug());
Krzysztof Parzyszek3af70c12016-04-19 21:36:24 +0000324 Dst.setSubReg(Src.getSubReg());
Sirish Pande30804c22012-02-15 18:52:27 +0000325 } else {
326 llvm_unreachable("Unexpected src operand type");
327 }
328 break;
329
330 default:
331 llvm_unreachable("Unexpected dst operand type");
332 break;
333 }
334}
335
336FunctionPass *llvm::createHexagonPeephole() {
337 return new HexagonPeephole();
338}