1 //===-- LVLGen.cpp - LVL instruction generator ----------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 #include "VESubtarget.h"
11 #include "llvm/CodeGen/MachineFunctionPass.h"
12 #include "llvm/CodeGen/MachineInstrBuilder.h"
13 #include "llvm/CodeGen/TargetInstrInfo.h"
17 #define DEBUG_TYPE "lvl-gen"
20 struct LVLGen
: public MachineFunctionPass
{
21 const TargetInstrInfo
*TII
;
22 const TargetRegisterInfo
*TRI
;
25 LVLGen() : MachineFunctionPass(ID
) {}
26 bool runOnMachineBasicBlock(MachineBasicBlock
&MBB
);
27 bool runOnMachineFunction(MachineFunction
&F
) override
;
29 unsigned getVL(const MachineInstr
&MI
);
30 int getVLIndex(unsigned Opcode
);
34 } // end of anonymous namespace
36 FunctionPass
*llvm::createLVLGenPass() { return new LVLGen
; }
38 int LVLGen::getVLIndex(unsigned Opcode
) {
39 const MCInstrDesc
&MCID
= TII
->get(Opcode
);
41 // If an instruction has VLIndex information, return it.
42 if (HAS_VLINDEX(MCID
.TSFlags
))
43 return GET_VLINDEX(MCID
.TSFlags
);
48 // returns a register holding a vector length. NoRegister is returned when
49 // this MI does not have a vector length.
50 unsigned LVLGen::getVL(const MachineInstr
&MI
) {
51 int Index
= getVLIndex(MI
.getOpcode());
53 return MI
.getOperand(Index
).getReg();
55 return VE::NoRegister
;
58 bool LVLGen::runOnMachineBasicBlock(MachineBasicBlock
&MBB
) {
60 (MBB.getParent()->getSubtarget<VESubtarget>().getRegisterInfo()->getName(no))
63 bool HasRegForVL
= false;
66 for (MachineBasicBlock::iterator I
= MBB
.begin(); I
!= MBB
.end();) {
67 MachineBasicBlock::iterator MI
= I
;
69 // Check whether MI uses a vector length operand. If so, we prepare for VL
70 // register. We would like to reuse VL register as much as possible. We
71 // also would like to keep the number of LEA instructions as fewer as
72 // possible. Therefore, we use a regular scalar register to hold immediate
73 // values to load VL register. And try to reuse identical scalar registers
74 // to avoid new LVLr instructions as much as possible.
75 unsigned Reg
= getVL(*MI
);
76 if (Reg
!= VE::NoRegister
) {
77 LLVM_DEBUG(dbgs() << "Vector instruction found: ");
78 LLVM_DEBUG(MI
->dump());
79 LLVM_DEBUG(dbgs() << "Vector length is " << RegName(Reg
) << ". ");
80 LLVM_DEBUG(dbgs() << "Current VL is "
81 << (HasRegForVL
? RegName(RegForVL
) : "unknown")
84 if (!HasRegForVL
|| RegForVL
!= Reg
) {
85 // Use VL, but a different value in a different scalar register.
86 // So, generate new LVL instruction just before the current instruction.
87 LLVM_DEBUG(dbgs() << "Generate a LVL instruction to load "
88 << RegName(Reg
) << ".\n");
89 BuildMI(MBB
, I
, MI
->getDebugLoc(), TII
->get(VE::LVLr
)).addReg(Reg
);
94 LLVM_DEBUG(dbgs() << "Reuse current VL.\n");
97 // Check the update of a given scalar register holding an immediate value
98 // for VL register. Also, a call doesn't preserve VL register.
100 if (MI
->definesRegister(RegForVL
, TRI
) ||
101 MI
->modifiesRegister(RegForVL
, TRI
) ||
102 MI
->killsRegister(RegForVL
, TRI
) || MI
->isCall()) {
103 // The latest VL is needed to be updated, so disable HasRegForVL.
104 LLVM_DEBUG(dbgs() << RegName(RegForVL
) << " is needed to be updated: ");
105 LLVM_DEBUG(MI
->dump());
115 bool LVLGen::runOnMachineFunction(MachineFunction
&F
) {
116 LLVM_DEBUG(dbgs() << "********** Begin LVLGen **********\n");
117 LLVM_DEBUG(dbgs() << "********** Function: " << F
.getName() << '\n');
118 LLVM_DEBUG(F
.dump());
120 bool Changed
= false;
122 const VESubtarget
&Subtarget
= F
.getSubtarget
<VESubtarget
>();
123 TII
= Subtarget
.getInstrInfo();
124 TRI
= Subtarget
.getRegisterInfo();
126 for (MachineBasicBlock
&MBB
: F
)
127 Changed
|= runOnMachineBasicBlock(MBB
);
130 LLVM_DEBUG(dbgs() << "\n");
131 LLVM_DEBUG(F
.dump());
133 LLVM_DEBUG(dbgs() << "********** End LVLGen **********\n");