Fix typo in file header.
[oota-llvm.git] / lib / Target / Hexagon / HexagonPeephole.cpp
1 //===-- 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 //    ...
13 //    %vreg176<def> = COPY %vreg170:subreg_loreg
14 //
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>
32 // redundant and relies on some form of dead removal instrucions, like
33 // DCE or DIE to actually eliminate them.
34
35
36 //===----------------------------------------------------------------------===//
37
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"
53 #include <algorithm>
54 #include "Hexagon.h"
55 #include "HexagonTargetMachine.h"
56
57 #include "llvm/Support/CommandLine.h"
58
59 using namespace llvm;
60
61 static cl::opt<bool> DisableHexagonPeephole("disable-hexagon-peephole",
62     cl::Hidden, cl::ZeroOrMore, cl::init(false),
63     cl::desc("Disable Peephole Optimization"));
64
65 static cl::opt<int>
66 DbgPNPCount("pnp-count", cl::init(-1), cl::Hidden,
67   cl::desc("Maximum number of P=NOT(P) to be optimized"));
68
69 static cl::opt<bool> DisablePNotP("disable-hexagon-pnotp",
70     cl::Hidden, cl::ZeroOrMore, cl::init(false),
71     cl::desc("Disable Optimization of PNotP"));
72
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"));
76
77 namespace {
78   struct HexagonPeephole : public MachineFunctionPass {
79     const HexagonInstrInfo    *QII;
80     const HexagonRegisterInfo *QRI;
81     const MachineRegisterInfo *MRI;
82
83   public:
84     static char ID;
85     HexagonPeephole() : MachineFunctionPass(ID) { }
86
87     bool runOnMachineFunction(MachineFunction &MF);
88
89     const char *getPassName() const {
90       return "Hexagon optimize redundant zero and size extends";
91     }
92
93     void getAnalysisUsage(AnalysisUsage &AU) const {
94       MachineFunctionPass::getAnalysisUsage(AU);
95     }
96
97   private:
98     void ChangeOpInto(MachineOperand &Dst, MachineOperand &Src);
99   };
100 }
101
102 char HexagonPeephole::ID = 0;
103
104 bool HexagonPeephole::runOnMachineFunction(MachineFunction &MF) {
105
106   QII = static_cast<const HexagonInstrInfo *>(MF.getTarget().
107                                         getInstrInfo());
108   QRI = static_cast<const HexagonRegisterInfo *>(MF.getTarget().
109                                        getRegisterInfo());
110   MRI = &MF.getRegInfo();
111
112   DenseMap<unsigned, unsigned> PeepholeMap;
113
114   if (DisableHexagonPeephole) return false;
115
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;
120     PeepholeMap.clear();
121
122     // Traverse the basic block.
123     for (MachineBasicBlock::iterator MII = MBB->begin(); MII != MBB->end();
124                                      ++MII) {
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;
141         }
142       }
143
144       // Look for P=NOT(P).
145       if (!DisablePNotP &&
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;
159         }
160       }
161
162       // Look for copy:
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);
168
169         // Make sure we are copying the lower 32 bits.
170         if (Src.getSubReg() != Hexagon::subreg_loreg)
171           continue;
172
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));
182           }
183         }
184       }
185
186       // Look for Predicated instructions.
187       if (!DisablePNotP) {
188         bool Done = false;
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));
203                 Done = true;
204               }
205             }
206           }
207         }
208
209         if (!Done) {
210           // Handle special instructions.
211           unsigned Op = MI->getOpcode();
212           unsigned NewOp = 0;
213           unsigned PR = 1, S1 = 2, S2 = 3;   // Operand indices.
214
215           switch (Op) {
216             case Hexagon::TFR_condset_rr:
217             case Hexagon::TFR_condset_ii:
218             case Hexagon::MUX_ii:
219             case Hexagon::MUX_rr:
220               NewOp = Op;
221               break;
222             case Hexagon::TFR_condset_ri:
223               NewOp = Hexagon::TFR_condset_ir;
224               break;
225             case Hexagon::TFR_condset_ir:
226               NewOp = Hexagon::TFR_condset_ri;
227               break;
228             case Hexagon::MUX_ri:
229               NewOp = Hexagon::MUX_ir;
230               break;
231             case Hexagon::MUX_ir:
232               NewOp = Hexagon::MUX_ri;
233               break;
234           }
235           if (NewOp) {
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);
245             }
246           } // if (NewOp)
247         } // if (!Done)
248
249       } // if (!DisablePNotP)
250
251     } // Instruction
252   } // Basic Block
253   return true;
254 }
255
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:
260       if (Src.isReg()) {
261         Dst.setReg(Src.getReg());
262       } else if (Src.isImm()) {
263         Dst.ChangeToImmediate(Src.getImm());
264       } else {
265         llvm_unreachable("Unexpected src operand type");
266       }
267       break;
268
269     case MachineOperand::MO_Immediate:
270       if (Src.isImm()) {
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(),
275                              Src.isDebug());
276       } else {
277         llvm_unreachable("Unexpected src operand type");
278       }
279       break;
280
281     default:
282       llvm_unreachable("Unexpected dst operand type");
283       break;
284   }
285 }
286
287 FunctionPass *llvm::createHexagonPeephole() {
288   return new HexagonPeephole();
289 }