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