xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/VE/LVLGen.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
1*e8d8bef9SDimitry Andric //===-- LVLGen.cpp - LVL instruction generator ----------------------------===//
2*e8d8bef9SDimitry Andric //
3*e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*e8d8bef9SDimitry Andric //
7*e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8*e8d8bef9SDimitry Andric 
9*e8d8bef9SDimitry Andric #include "VE.h"
10*e8d8bef9SDimitry Andric #include "VESubtarget.h"
11*e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
12*e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
13*e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
14*e8d8bef9SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
15*e8d8bef9SDimitry Andric #include "llvm/Target/TargetMachine.h"
16*e8d8bef9SDimitry Andric 
17*e8d8bef9SDimitry Andric using namespace llvm;
18*e8d8bef9SDimitry Andric 
19*e8d8bef9SDimitry Andric #define DEBUG_TYPE "lvl-gen"
20*e8d8bef9SDimitry Andric 
21*e8d8bef9SDimitry Andric namespace {
22*e8d8bef9SDimitry Andric struct LVLGen : public MachineFunctionPass {
23*e8d8bef9SDimitry Andric   const TargetInstrInfo *TII;
24*e8d8bef9SDimitry Andric   const TargetRegisterInfo *TRI;
25*e8d8bef9SDimitry Andric 
26*e8d8bef9SDimitry Andric   static char ID;
27*e8d8bef9SDimitry Andric   LVLGen() : MachineFunctionPass(ID) {}
28*e8d8bef9SDimitry Andric   bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
29*e8d8bef9SDimitry Andric   bool runOnMachineFunction(MachineFunction &F) override;
30*e8d8bef9SDimitry Andric 
31*e8d8bef9SDimitry Andric   unsigned getVL(const MachineInstr &MI);
32*e8d8bef9SDimitry Andric   int getVLIndex(unsigned Opcode);
33*e8d8bef9SDimitry Andric };
34*e8d8bef9SDimitry Andric char LVLGen::ID = 0;
35*e8d8bef9SDimitry Andric 
36*e8d8bef9SDimitry Andric } // end of anonymous namespace
37*e8d8bef9SDimitry Andric 
38*e8d8bef9SDimitry Andric FunctionPass *llvm::createLVLGenPass() { return new LVLGen; }
39*e8d8bef9SDimitry Andric 
40*e8d8bef9SDimitry Andric int LVLGen::getVLIndex(unsigned Opcode) {
41*e8d8bef9SDimitry Andric   const MCInstrDesc &MCID = TII->get(Opcode);
42*e8d8bef9SDimitry Andric 
43*e8d8bef9SDimitry Andric   // If an instruction has VLIndex information, return it.
44*e8d8bef9SDimitry Andric   if (HAS_VLINDEX(MCID.TSFlags))
45*e8d8bef9SDimitry Andric     return GET_VLINDEX(MCID.TSFlags);
46*e8d8bef9SDimitry Andric 
47*e8d8bef9SDimitry Andric   return -1;
48*e8d8bef9SDimitry Andric }
49*e8d8bef9SDimitry Andric 
50*e8d8bef9SDimitry Andric // returns a register holding a vector length. NoRegister is returned when
51*e8d8bef9SDimitry Andric // this MI does not have a vector length.
52*e8d8bef9SDimitry Andric unsigned LVLGen::getVL(const MachineInstr &MI) {
53*e8d8bef9SDimitry Andric   int Index = getVLIndex(MI.getOpcode());
54*e8d8bef9SDimitry Andric   if (Index >= 0)
55*e8d8bef9SDimitry Andric     return MI.getOperand(Index).getReg();
56*e8d8bef9SDimitry Andric 
57*e8d8bef9SDimitry Andric   return VE::NoRegister;
58*e8d8bef9SDimitry Andric }
59*e8d8bef9SDimitry Andric 
60*e8d8bef9SDimitry Andric bool LVLGen::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
61*e8d8bef9SDimitry Andric #define RegName(no)                                                            \
62*e8d8bef9SDimitry Andric   (MBB.getParent()->getSubtarget<VESubtarget>().getRegisterInfo()->getName(no))
63*e8d8bef9SDimitry Andric 
64*e8d8bef9SDimitry Andric   bool Changed = false;
65*e8d8bef9SDimitry Andric   bool HasRegForVL = false;
66*e8d8bef9SDimitry Andric   unsigned RegForVL;
67*e8d8bef9SDimitry Andric 
68*e8d8bef9SDimitry Andric   for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end();) {
69*e8d8bef9SDimitry Andric     MachineBasicBlock::iterator MI = I;
70*e8d8bef9SDimitry Andric 
71*e8d8bef9SDimitry Andric     // Check whether MI uses a vector length operand.  If so, we prepare for VL
72*e8d8bef9SDimitry Andric     // register.  We would like to reuse VL register as much as possible.  We
73*e8d8bef9SDimitry Andric     // also would like to keep the number of LEA instructions as fewer as
74*e8d8bef9SDimitry Andric     // possible.  Therefore, we use a regular scalar register to hold immediate
75*e8d8bef9SDimitry Andric     // values to load VL register.  And try to reuse identical scalar registers
76*e8d8bef9SDimitry Andric     // to avoid new LVLr instructions as much as possible.
77*e8d8bef9SDimitry Andric     unsigned Reg = getVL(*MI);
78*e8d8bef9SDimitry Andric     if (Reg != VE::NoRegister) {
79*e8d8bef9SDimitry Andric       LLVM_DEBUG(dbgs() << "Vector instruction found: ");
80*e8d8bef9SDimitry Andric       LLVM_DEBUG(MI->dump());
81*e8d8bef9SDimitry Andric       LLVM_DEBUG(dbgs() << "Vector length is " << RegName(Reg) << ". ");
82*e8d8bef9SDimitry Andric       LLVM_DEBUG(dbgs() << "Current VL is "
83*e8d8bef9SDimitry Andric                         << (HasRegForVL ? RegName(RegForVL) : "unknown")
84*e8d8bef9SDimitry Andric                         << ". ");
85*e8d8bef9SDimitry Andric 
86*e8d8bef9SDimitry Andric       if (!HasRegForVL || RegForVL != Reg) {
87*e8d8bef9SDimitry Andric         // Use VL, but a different value in a different scalar register.
88*e8d8bef9SDimitry Andric         // So, generate new LVL instruction just before the current instruction.
89*e8d8bef9SDimitry Andric         LLVM_DEBUG(dbgs() << "Generate a LVL instruction to load "
90*e8d8bef9SDimitry Andric                           << RegName(Reg) << ".\n");
91*e8d8bef9SDimitry Andric         BuildMI(MBB, I, MI->getDebugLoc(), TII->get(VE::LVLr)).addReg(Reg);
92*e8d8bef9SDimitry Andric         HasRegForVL = true;
93*e8d8bef9SDimitry Andric         RegForVL = Reg;
94*e8d8bef9SDimitry Andric         Changed = true;
95*e8d8bef9SDimitry Andric       } else {
96*e8d8bef9SDimitry Andric         LLVM_DEBUG(dbgs() << "Reuse current VL.\n");
97*e8d8bef9SDimitry Andric       }
98*e8d8bef9SDimitry Andric     }
99*e8d8bef9SDimitry Andric     // Check the update of a given scalar register holding an immediate value
100*e8d8bef9SDimitry Andric     // for VL register.  Also, a call doesn't preserve VL register.
101*e8d8bef9SDimitry Andric     if (HasRegForVL) {
102*e8d8bef9SDimitry Andric       if (MI->definesRegister(RegForVL, TRI) ||
103*e8d8bef9SDimitry Andric           MI->modifiesRegister(RegForVL, TRI) ||
104*e8d8bef9SDimitry Andric           MI->killsRegister(RegForVL, TRI) || MI->isCall()) {
105*e8d8bef9SDimitry Andric         // The latest VL is needed to be updated, so disable HasRegForVL.
106*e8d8bef9SDimitry Andric         LLVM_DEBUG(dbgs() << RegName(RegForVL) << " is needed to be updated: ");
107*e8d8bef9SDimitry Andric         LLVM_DEBUG(MI->dump());
108*e8d8bef9SDimitry Andric         HasRegForVL = false;
109*e8d8bef9SDimitry Andric       }
110*e8d8bef9SDimitry Andric     }
111*e8d8bef9SDimitry Andric 
112*e8d8bef9SDimitry Andric     ++I;
113*e8d8bef9SDimitry Andric   }
114*e8d8bef9SDimitry Andric   return Changed;
115*e8d8bef9SDimitry Andric }
116*e8d8bef9SDimitry Andric 
117*e8d8bef9SDimitry Andric bool LVLGen::runOnMachineFunction(MachineFunction &F) {
118*e8d8bef9SDimitry Andric   LLVM_DEBUG(dbgs() << "********** Begin LVLGen **********\n");
119*e8d8bef9SDimitry Andric   LLVM_DEBUG(dbgs() << "********** Function: " << F.getName() << '\n');
120*e8d8bef9SDimitry Andric   LLVM_DEBUG(F.dump());
121*e8d8bef9SDimitry Andric 
122*e8d8bef9SDimitry Andric   bool Changed = false;
123*e8d8bef9SDimitry Andric 
124*e8d8bef9SDimitry Andric   const VESubtarget &Subtarget = F.getSubtarget<VESubtarget>();
125*e8d8bef9SDimitry Andric   TII = Subtarget.getInstrInfo();
126*e8d8bef9SDimitry Andric   TRI = Subtarget.getRegisterInfo();
127*e8d8bef9SDimitry Andric 
128*e8d8bef9SDimitry Andric   for (MachineFunction::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
129*e8d8bef9SDimitry Andric     Changed |= runOnMachineBasicBlock(*FI);
130*e8d8bef9SDimitry Andric 
131*e8d8bef9SDimitry Andric   if (Changed) {
132*e8d8bef9SDimitry Andric     LLVM_DEBUG(dbgs() << "\n");
133*e8d8bef9SDimitry Andric     LLVM_DEBUG(F.dump());
134*e8d8bef9SDimitry Andric   }
135*e8d8bef9SDimitry Andric   LLVM_DEBUG(dbgs() << "********** End LVLGen **********\n");
136*e8d8bef9SDimitry Andric   return Changed;
137*e8d8bef9SDimitry Andric }
138