xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/Hexagon/HexagonPeephole.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
10b57cec5SDimitry Andric //===-- HexagonPeephole.cpp - Hexagon Peephole Optimiztions ---------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric // This peephole pass optimizes in the following cases.
80b57cec5SDimitry Andric // 1. Optimizes redundant sign extends for the following case
90b57cec5SDimitry Andric //    Transform the following pattern
100b57cec5SDimitry Andric //    %170 = SXTW %166
110b57cec5SDimitry Andric //    ...
120b57cec5SDimitry Andric //    %176 = COPY %170:isub_lo
130b57cec5SDimitry Andric //
140b57cec5SDimitry Andric //    Into
150b57cec5SDimitry Andric //    %176 = COPY %166
160b57cec5SDimitry Andric //
170b57cec5SDimitry Andric //  2. Optimizes redundant negation of predicates.
180b57cec5SDimitry Andric //     %15 = CMPGTrr %6, %2
190b57cec5SDimitry Andric //     ...
200b57cec5SDimitry Andric //     %16 = NOT_p killed %15
210b57cec5SDimitry Andric //     ...
220b57cec5SDimitry Andric //     JMP_c killed %16, <%bb.1>, implicit dead %pc
230b57cec5SDimitry Andric //
240b57cec5SDimitry Andric //     Into
250b57cec5SDimitry Andric //     %15 = CMPGTrr %6, %2;
260b57cec5SDimitry Andric //     ...
270b57cec5SDimitry Andric //     JMP_cNot killed %15, <%bb.1>, implicit dead %pc;
280b57cec5SDimitry Andric //
290b57cec5SDimitry Andric // Note: The peephole pass makes the instrucstions like
300b57cec5SDimitry Andric // %170 = SXTW %166 or %16 = NOT_p killed %15
310b57cec5SDimitry Andric // redundant and relies on some form of dead removal instructions, like
320b57cec5SDimitry Andric // DCE or DIE to actually eliminate them.
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric #include "Hexagon.h"
370b57cec5SDimitry Andric #include "HexagonTargetMachine.h"
380b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
390b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
400b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
410b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
420b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
430b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
440b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
450b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
460b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
470b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
485ffd83dbSDimitry Andric #include "llvm/Pass.h"
490b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
500b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
510b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
520b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
530b57cec5SDimitry Andric #include <algorithm>
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric using namespace llvm;
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric #define DEBUG_TYPE "hexagon-peephole"
580b57cec5SDimitry Andric 
59*81ad6265SDimitry Andric static cl::opt<bool>
60*81ad6265SDimitry Andric     DisableHexagonPeephole("disable-hexagon-peephole", cl::Hidden,
610b57cec5SDimitry Andric                            cl::desc("Disable Peephole Optimization"));
620b57cec5SDimitry Andric 
63*81ad6265SDimitry Andric static cl::opt<bool> DisablePNotP("disable-hexagon-pnotp", cl::Hidden,
640b57cec5SDimitry Andric                                   cl::desc("Disable Optimization of PNotP"));
650b57cec5SDimitry Andric 
66*81ad6265SDimitry Andric static cl::opt<bool>
67*81ad6265SDimitry Andric     DisableOptSZExt("disable-hexagon-optszext", cl::Hidden, cl::init(true),
680b57cec5SDimitry Andric                     cl::desc("Disable Optimization of Sign/Zero Extends"));
690b57cec5SDimitry Andric 
70*81ad6265SDimitry Andric static cl::opt<bool>
71*81ad6265SDimitry Andric     DisableOptExtTo64("disable-hexagon-opt-ext-to-64", cl::Hidden,
72*81ad6265SDimitry Andric                       cl::init(true),
730b57cec5SDimitry Andric                       cl::desc("Disable Optimization of extensions to i64."));
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric namespace llvm {
760b57cec5SDimitry Andric   FunctionPass *createHexagonPeephole();
770b57cec5SDimitry Andric   void initializeHexagonPeepholePass(PassRegistry&);
780b57cec5SDimitry Andric }
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric namespace {
810b57cec5SDimitry Andric   struct HexagonPeephole : public MachineFunctionPass {
820b57cec5SDimitry Andric     const HexagonInstrInfo    *QII;
830b57cec5SDimitry Andric     const HexagonRegisterInfo *QRI;
840b57cec5SDimitry Andric     const MachineRegisterInfo *MRI;
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric   public:
870b57cec5SDimitry Andric     static char ID;
HexagonPeephole__anon893e9e4d0111::HexagonPeephole880b57cec5SDimitry Andric     HexagonPeephole() : MachineFunctionPass(ID) {
890b57cec5SDimitry Andric       initializeHexagonPeepholePass(*PassRegistry::getPassRegistry());
900b57cec5SDimitry Andric     }
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric     bool runOnMachineFunction(MachineFunction &MF) override;
930b57cec5SDimitry Andric 
getPassName__anon893e9e4d0111::HexagonPeephole940b57cec5SDimitry Andric     StringRef getPassName() const override {
950b57cec5SDimitry Andric       return "Hexagon optimize redundant zero and size extends";
960b57cec5SDimitry Andric     }
970b57cec5SDimitry Andric 
getAnalysisUsage__anon893e9e4d0111::HexagonPeephole980b57cec5SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
990b57cec5SDimitry Andric       MachineFunctionPass::getAnalysisUsage(AU);
1000b57cec5SDimitry Andric     }
1010b57cec5SDimitry Andric   };
1020b57cec5SDimitry Andric }
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric char HexagonPeephole::ID = 0;
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric INITIALIZE_PASS(HexagonPeephole, "hexagon-peephole", "Hexagon Peephole",
1070b57cec5SDimitry Andric                 false, false)
1080b57cec5SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)1090b57cec5SDimitry Andric bool HexagonPeephole::runOnMachineFunction(MachineFunction &MF) {
1100b57cec5SDimitry Andric   if (skipFunction(MF.getFunction()))
1110b57cec5SDimitry Andric     return false;
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric   QII = static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo());
1140b57cec5SDimitry Andric   QRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
1150b57cec5SDimitry Andric   MRI = &MF.getRegInfo();
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   DenseMap<unsigned, unsigned> PeepholeMap;
1180b57cec5SDimitry Andric   DenseMap<unsigned, std::pair<unsigned, unsigned> > PeepholeDoubleRegsMap;
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric   if (DisableHexagonPeephole) return false;
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric   // Loop over all of the basic blocks.
1234824e7fdSDimitry Andric   for (MachineBasicBlock &MBB : MF) {
1240b57cec5SDimitry Andric     PeepholeMap.clear();
1250b57cec5SDimitry Andric     PeepholeDoubleRegsMap.clear();
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric     // Traverse the basic block.
1284824e7fdSDimitry Andric     for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
1290b57cec5SDimitry Andric       // Look for sign extends:
1300b57cec5SDimitry Andric       // %170 = SXTW %166
1310b57cec5SDimitry Andric       if (!DisableOptSZExt && MI.getOpcode() == Hexagon::A2_sxtw) {
1320b57cec5SDimitry Andric         assert(MI.getNumOperands() == 2);
1330b57cec5SDimitry Andric         MachineOperand &Dst = MI.getOperand(0);
1340b57cec5SDimitry Andric         MachineOperand &Src = MI.getOperand(1);
1358bcb0991SDimitry Andric         Register DstReg = Dst.getReg();
1368bcb0991SDimitry Andric         Register SrcReg = Src.getReg();
1370b57cec5SDimitry Andric         // Just handle virtual registers.
138e8d8bef9SDimitry Andric         if (DstReg.isVirtual() && SrcReg.isVirtual()) {
1390b57cec5SDimitry Andric           // Map the following:
1400b57cec5SDimitry Andric           // %170 = SXTW %166
1410b57cec5SDimitry Andric           // PeepholeMap[170] = %166
1420b57cec5SDimitry Andric           PeepholeMap[DstReg] = SrcReg;
1430b57cec5SDimitry Andric         }
1440b57cec5SDimitry Andric       }
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric       // Look for  %170 = COMBINE_ir_V4 (0, %169)
1470b57cec5SDimitry Andric       // %170:DoublRegs, %169:IntRegs
1480b57cec5SDimitry Andric       if (!DisableOptExtTo64 && MI.getOpcode() == Hexagon::A4_combineir) {
1490b57cec5SDimitry Andric         assert(MI.getNumOperands() == 3);
1500b57cec5SDimitry Andric         MachineOperand &Dst = MI.getOperand(0);
1510b57cec5SDimitry Andric         MachineOperand &Src1 = MI.getOperand(1);
1520b57cec5SDimitry Andric         MachineOperand &Src2 = MI.getOperand(2);
1530b57cec5SDimitry Andric         if (Src1.getImm() != 0)
1540b57cec5SDimitry Andric           continue;
1558bcb0991SDimitry Andric         Register DstReg = Dst.getReg();
1568bcb0991SDimitry Andric         Register SrcReg = Src2.getReg();
1570b57cec5SDimitry Andric         PeepholeMap[DstReg] = SrcReg;
1580b57cec5SDimitry Andric       }
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric       // Look for this sequence below
1610b57cec5SDimitry Andric       // %DoubleReg1 = LSRd_ri %DoubleReg0, 32
1620b57cec5SDimitry Andric       // %IntReg = COPY %DoubleReg1:isub_lo.
1630b57cec5SDimitry Andric       // and convert into
1640b57cec5SDimitry Andric       // %IntReg = COPY %DoubleReg0:isub_hi.
1650b57cec5SDimitry Andric       if (MI.getOpcode() == Hexagon::S2_lsr_i_p) {
1660b57cec5SDimitry Andric         assert(MI.getNumOperands() == 3);
1670b57cec5SDimitry Andric         MachineOperand &Dst = MI.getOperand(0);
1680b57cec5SDimitry Andric         MachineOperand &Src1 = MI.getOperand(1);
1690b57cec5SDimitry Andric         MachineOperand &Src2 = MI.getOperand(2);
1700b57cec5SDimitry Andric         if (Src2.getImm() != 32)
1710b57cec5SDimitry Andric           continue;
1728bcb0991SDimitry Andric         Register DstReg = Dst.getReg();
1738bcb0991SDimitry Andric         Register SrcReg = Src1.getReg();
1740b57cec5SDimitry Andric         PeepholeDoubleRegsMap[DstReg] =
1750b57cec5SDimitry Andric           std::make_pair(*&SrcReg, Hexagon::isub_hi);
1760b57cec5SDimitry Andric       }
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric       // Look for P=NOT(P).
1790b57cec5SDimitry Andric       if (!DisablePNotP && MI.getOpcode() == Hexagon::C2_not) {
1800b57cec5SDimitry Andric         assert(MI.getNumOperands() == 2);
1810b57cec5SDimitry Andric         MachineOperand &Dst = MI.getOperand(0);
1820b57cec5SDimitry Andric         MachineOperand &Src = MI.getOperand(1);
1838bcb0991SDimitry Andric         Register DstReg = Dst.getReg();
1848bcb0991SDimitry Andric         Register SrcReg = Src.getReg();
1850b57cec5SDimitry Andric         // Just handle virtual registers.
186e8d8bef9SDimitry Andric         if (DstReg.isVirtual() && SrcReg.isVirtual()) {
1870b57cec5SDimitry Andric           // Map the following:
1880b57cec5SDimitry Andric           // %170 = NOT_xx %166
1890b57cec5SDimitry Andric           // PeepholeMap[170] = %166
1900b57cec5SDimitry Andric           PeepholeMap[DstReg] = SrcReg;
1910b57cec5SDimitry Andric         }
1920b57cec5SDimitry Andric       }
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric       // Look for copy:
1950b57cec5SDimitry Andric       // %176 = COPY %170:isub_lo
1960b57cec5SDimitry Andric       if (!DisableOptSZExt && MI.isCopy()) {
1970b57cec5SDimitry Andric         assert(MI.getNumOperands() == 2);
1980b57cec5SDimitry Andric         MachineOperand &Dst = MI.getOperand(0);
1990b57cec5SDimitry Andric         MachineOperand &Src = MI.getOperand(1);
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric         // Make sure we are copying the lower 32 bits.
2020b57cec5SDimitry Andric         if (Src.getSubReg() != Hexagon::isub_lo)
2030b57cec5SDimitry Andric           continue;
2040b57cec5SDimitry Andric 
2058bcb0991SDimitry Andric         Register DstReg = Dst.getReg();
2068bcb0991SDimitry Andric         Register SrcReg = Src.getReg();
207e8d8bef9SDimitry Andric         if (DstReg.isVirtual() && SrcReg.isVirtual()) {
2080b57cec5SDimitry Andric           // Try to find in the map.
2090b57cec5SDimitry Andric           if (unsigned PeepholeSrc = PeepholeMap.lookup(SrcReg)) {
2100b57cec5SDimitry Andric             // Change the 1st operand.
211*81ad6265SDimitry Andric             MI.removeOperand(1);
2120b57cec5SDimitry Andric             MI.addOperand(MachineOperand::CreateReg(PeepholeSrc, false));
2130b57cec5SDimitry Andric           } else  {
2140b57cec5SDimitry Andric             DenseMap<unsigned, std::pair<unsigned, unsigned> >::iterator DI =
2150b57cec5SDimitry Andric               PeepholeDoubleRegsMap.find(SrcReg);
2160b57cec5SDimitry Andric             if (DI != PeepholeDoubleRegsMap.end()) {
2170b57cec5SDimitry Andric               std::pair<unsigned,unsigned> PeepholeSrc = DI->second;
218*81ad6265SDimitry Andric               MI.removeOperand(1);
2190b57cec5SDimitry Andric               MI.addOperand(MachineOperand::CreateReg(
2200b57cec5SDimitry Andric                   PeepholeSrc.first, false /*isDef*/, false /*isImp*/,
2210b57cec5SDimitry Andric                   false /*isKill*/, false /*isDead*/, false /*isUndef*/,
2220b57cec5SDimitry Andric                   false /*isEarlyClobber*/, PeepholeSrc.second));
2230b57cec5SDimitry Andric             }
2240b57cec5SDimitry Andric           }
2250b57cec5SDimitry Andric         }
2260b57cec5SDimitry Andric       }
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric       // Look for Predicated instructions.
2290b57cec5SDimitry Andric       if (!DisablePNotP) {
2300b57cec5SDimitry Andric         bool Done = false;
2310b57cec5SDimitry Andric         if (QII->isPredicated(MI)) {
2320b57cec5SDimitry Andric           MachineOperand &Op0 = MI.getOperand(0);
2338bcb0991SDimitry Andric           Register Reg0 = Op0.getReg();
2340b57cec5SDimitry Andric           const TargetRegisterClass *RC0 = MRI->getRegClass(Reg0);
2350b57cec5SDimitry Andric           if (RC0->getID() == Hexagon::PredRegsRegClassID) {
2360b57cec5SDimitry Andric             // Handle instructions that have a prediate register in op0
2370b57cec5SDimitry Andric             // (most cases of predicable instructions).
238e8d8bef9SDimitry Andric             if (Reg0.isVirtual()) {
2390b57cec5SDimitry Andric               // Try to find in the map.
2400b57cec5SDimitry Andric               if (unsigned PeepholeSrc = PeepholeMap.lookup(Reg0)) {
2410b57cec5SDimitry Andric                 // Change the 1st operand and, flip the opcode.
2420b57cec5SDimitry Andric                 MI.getOperand(0).setReg(PeepholeSrc);
2430b57cec5SDimitry Andric                 MRI->clearKillFlags(PeepholeSrc);
2440b57cec5SDimitry Andric                 int NewOp = QII->getInvertedPredicatedOpcode(MI.getOpcode());
2450b57cec5SDimitry Andric                 MI.setDesc(QII->get(NewOp));
2460b57cec5SDimitry Andric                 Done = true;
2470b57cec5SDimitry Andric               }
2480b57cec5SDimitry Andric             }
2490b57cec5SDimitry Andric           }
2500b57cec5SDimitry Andric         }
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric         if (!Done) {
2530b57cec5SDimitry Andric           // Handle special instructions.
2540b57cec5SDimitry Andric           unsigned Op = MI.getOpcode();
2550b57cec5SDimitry Andric           unsigned NewOp = 0;
2560b57cec5SDimitry Andric           unsigned PR = 1, S1 = 2, S2 = 3;   // Operand indices.
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric           switch (Op) {
2590b57cec5SDimitry Andric             case Hexagon::C2_mux:
2600b57cec5SDimitry Andric             case Hexagon::C2_muxii:
2610b57cec5SDimitry Andric               NewOp = Op;
2620b57cec5SDimitry Andric               break;
2630b57cec5SDimitry Andric             case Hexagon::C2_muxri:
2640b57cec5SDimitry Andric               NewOp = Hexagon::C2_muxir;
2650b57cec5SDimitry Andric               break;
2660b57cec5SDimitry Andric             case Hexagon::C2_muxir:
2670b57cec5SDimitry Andric               NewOp = Hexagon::C2_muxri;
2680b57cec5SDimitry Andric               break;
2690b57cec5SDimitry Andric           }
2700b57cec5SDimitry Andric           if (NewOp) {
2718bcb0991SDimitry Andric             Register PSrc = MI.getOperand(PR).getReg();
2720b57cec5SDimitry Andric             if (unsigned POrig = PeepholeMap.lookup(PSrc)) {
2734824e7fdSDimitry Andric               BuildMI(MBB, MI.getIterator(), MI.getDebugLoc(), QII->get(NewOp),
2744824e7fdSDimitry Andric                       MI.getOperand(0).getReg())
2750b57cec5SDimitry Andric                   .addReg(POrig)
2760b57cec5SDimitry Andric                   .add(MI.getOperand(S2))
2770b57cec5SDimitry Andric                   .add(MI.getOperand(S1));
2780b57cec5SDimitry Andric               MRI->clearKillFlags(POrig);
2790b57cec5SDimitry Andric               MI.eraseFromParent();
2800b57cec5SDimitry Andric             }
2810b57cec5SDimitry Andric           } // if (NewOp)
2820b57cec5SDimitry Andric         } // if (!Done)
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric       } // if (!DisablePNotP)
2850b57cec5SDimitry Andric 
2860b57cec5SDimitry Andric     } // Instruction
2870b57cec5SDimitry Andric   } // Basic Block
2880b57cec5SDimitry Andric   return true;
2890b57cec5SDimitry Andric }
2900b57cec5SDimitry Andric 
createHexagonPeephole()2910b57cec5SDimitry Andric FunctionPass *llvm::createHexagonPeephole() {
2920b57cec5SDimitry Andric   return new HexagonPeephole();
2930b57cec5SDimitry Andric }
294