1//===-- LVLGen.cpp - LVL instruction generator ----------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "VE.h"
10#include "VEInstrInfo.h"
11#include "VERegisterInfo.h"
12#include "VESubtarget.h"
13#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
14#include "llvm/CodeGen/MachineFunctionPass.h"
15#include "llvm/CodeGen/MachineInstrBuilder.h"
16#include "llvm/CodeGen/MachinePassManager.h"
17#include "llvm/IR/Analysis.h"
18#include "llvm/Pass.h"
19
20using namespace llvm;
21
22#define DEBUG_TYPE "lvl-gen"
23
24namespace {
25struct VELVLGenLegacy : public MachineFunctionPass {
26 static char ID;
27 VELVLGenLegacy() : MachineFunctionPass(ID) {}
28 bool runOnMachineFunction(MachineFunction &F) override;
29
30 void getAnalysisUsage(AnalysisUsage &AU) const override {
31 AU.setPreservesCFG();
32 MachineFunctionPass::getAnalysisUsage(AU);
33 }
34};
35char VELVLGenLegacy::ID = 0;
36
37} // end of anonymous namespace
38
39FunctionPass *llvm::createVELVLGenLegacyPass() { return new VELVLGenLegacy; }
40
41static int getVLIndex(unsigned Opcode, const VEInstrInfo *TII) {
42 const MCInstrDesc &MCID = TII->get(Opcode);
43
44 // If an instruction has VLIndex information, return it.
45 if (HAS_VLINDEX(MCID.TSFlags))
46 return GET_VLINDEX(MCID.TSFlags);
47
48 return -1;
49}
50
51// returns a register holding a vector length. NoRegister is returned when
52// this MI does not have a vector length.
53static unsigned getVL(const MachineInstr &MI, const VEInstrInfo *TII) {
54 int Index = getVLIndex(Opcode: MI.getOpcode(), TII);
55 if (Index >= 0)
56 return MI.getOperand(i: Index).getReg();
57
58 return VE::NoRegister;
59}
60
61static bool runLVLGenOnMachineBasicBlock(MachineBasicBlock &MBB,
62 const VEInstrInfo *TII,
63 const VERegisterInfo *TRI) {
64#define RegName(no) \
65 (MBB.getParent()->getSubtarget<VESubtarget>().getRegisterInfo()->getName(no))
66
67 bool Changed = false;
68 bool HasRegForVL = false;
69 unsigned RegForVL;
70
71 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end();) {
72 MachineBasicBlock::iterator MI = I;
73
74 // Check whether MI uses a vector length operand. If so, we prepare for VL
75 // register. We would like to reuse VL register as much as possible. We
76 // also would like to keep the number of LEA instructions as fewer as
77 // possible. Therefore, we use a regular scalar register to hold immediate
78 // values to load VL register. And try to reuse identical scalar registers
79 // to avoid new LVLr instructions as much as possible.
80 unsigned Reg = getVL(MI: *MI, TII);
81 if (Reg != VE::NoRegister) {
82 LLVM_DEBUG(dbgs() << "Vector instruction found: ");
83 LLVM_DEBUG(MI->dump());
84 LLVM_DEBUG(dbgs() << "Vector length is " << RegName(Reg) << ". ");
85 LLVM_DEBUG(dbgs() << "Current VL is "
86 << (HasRegForVL ? RegName(RegForVL) : "unknown")
87 << ". ");
88
89 if (!HasRegForVL || RegForVL != Reg) {
90 // Use VL, but a different value in a different scalar register.
91 // So, generate new LVL instruction just before the current instruction.
92 LLVM_DEBUG(dbgs() << "Generate a LVL instruction to load "
93 << RegName(Reg) << ".\n");
94 BuildMI(BB&: MBB, I, MIMD: MI->getDebugLoc(), MCID: TII->get(Opcode: VE::LVLr)).addReg(RegNo: Reg);
95 HasRegForVL = true;
96 RegForVL = Reg;
97 Changed = true;
98 } else {
99 LLVM_DEBUG(dbgs() << "Reuse current VL.\n");
100 }
101 }
102 // Check the update of a given scalar register holding an immediate value
103 // for VL register. Also, a call doesn't preserve VL register.
104 if (HasRegForVL) {
105 if (MI->definesRegister(Reg: RegForVL, TRI) ||
106 MI->modifiesRegister(Reg: RegForVL, TRI) ||
107 MI->killsRegister(Reg: RegForVL, TRI) || MI->isCall()) {
108 // The latest VL is needed to be updated, so disable HasRegForVL.
109 LLVM_DEBUG(dbgs() << RegName(RegForVL) << " is needed to be updated: ");
110 LLVM_DEBUG(MI->dump());
111 HasRegForVL = false;
112 }
113 }
114
115 ++I;
116 }
117 return Changed;
118}
119
120static bool runLVLGenOnMachineFunction(MachineFunction &F) {
121 LLVM_DEBUG(dbgs() << "********** Begin LVLGen **********\n");
122 LLVM_DEBUG(dbgs() << "********** Function: " << F.getName() << '\n');
123 LLVM_DEBUG(F.dump());
124
125 bool Changed = false;
126
127 const VESubtarget &Subtarget = F.getSubtarget<VESubtarget>();
128 const VEInstrInfo *TII = Subtarget.getInstrInfo();
129 const VERegisterInfo *TRI = Subtarget.getRegisterInfo();
130
131 for (MachineBasicBlock &MBB : F)
132 Changed |= runLVLGenOnMachineBasicBlock(MBB, TII, TRI);
133
134 if (Changed) {
135 LLVM_DEBUG(dbgs() << "\n");
136 LLVM_DEBUG(F.dump());
137 }
138 LLVM_DEBUG(dbgs() << "********** End LVLGen **********\n");
139 return Changed;
140}
141
142bool VELVLGenLegacy::runOnMachineFunction(MachineFunction &MF) {
143 return runLVLGenOnMachineFunction(F&: MF);
144}
145
146PreservedAnalyses VELVLGenPass::run(MachineFunction &MF,
147 MachineFunctionAnalysisManager &MFAM) {
148 return runLVLGenOnMachineFunction(F&: MF)
149 ? getMachineFunctionPassPreservedAnalyses()
150 .preserveSet<CFGAnalyses>()
151 : PreservedAnalyses::all();
152}
153