LLVM 24.0.0git
LVLGen.cpp
Go to the documentation of this file.
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"
18#include "llvm/IR/Analysis.h"
19#include "llvm/Pass.h"
20
21using namespace llvm;
22
23#define DEBUG_TYPE "lvl-gen"
24
25namespace {
26struct VELVLGenLegacy : public MachineFunctionPass {
27 static char ID;
28 VELVLGenLegacy() : MachineFunctionPass(ID) {}
29 bool runOnMachineFunction(MachineFunction &F) override;
30
31 void getAnalysisUsage(AnalysisUsage &AU) const override {
32 AU.setPreservesCFG();
34 }
35};
36char VELVLGenLegacy::ID = 0;
37
38} // end of anonymous namespace
39
40FunctionPass *llvm::createVELVLGenLegacyPass() { return new VELVLGenLegacy; }
41
42static int getVLIndex(unsigned Opcode, const VEInstrInfo *TII) {
43 const MCInstrDesc &MCID = TII->get(Opcode);
44
45 // If an instruction has VLIndex information, return it.
46 if (HAS_VLINDEX(MCID.TSFlags))
47 return GET_VLINDEX(MCID.TSFlags);
48
49 return -1;
50}
51
52// returns a register holding a vector length. NoRegister is returned when
53// this MI does not have a vector length.
54static unsigned getVL(const MachineInstr &MI, const VEInstrInfo *TII) {
55 int Index = getVLIndex(MI.getOpcode(), TII);
56 if (Index >= 0)
57 return MI.getOperand(Index).getReg();
58
59 return VE::NoRegister;
60}
61
63 const VEInstrInfo *TII,
64 const VERegisterInfo *TRI) {
65#define RegName(no) \
66 (MBB.getParent()->getSubtarget<VESubtarget>().getRegisterInfo()->getName(no))
67
68 bool Changed = false;
69 bool HasRegForVL = false;
70 unsigned RegForVL;
71
72 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end();) {
74
75 // Check whether MI uses a vector length operand. If so, we prepare for VL
76 // register. We would like to reuse VL register as much as possible. We
77 // also would like to keep the number of LEA instructions as fewer as
78 // possible. Therefore, we use a regular scalar register to hold immediate
79 // values to load VL register. And try to reuse identical scalar registers
80 // to avoid new LVLr instructions as much as possible.
81 unsigned Reg = getVL(*MI, TII);
82 if (Reg != VE::NoRegister) {
83 LLVM_DEBUG(dbgs() << "Vector instruction found: ");
84 LLVM_DEBUG(MI->dump());
85 LLVM_DEBUG(dbgs() << "Vector length is " << RegName(Reg) << ". ");
86 LLVM_DEBUG(dbgs() << "Current VL is "
87 << (HasRegForVL ? RegName(RegForVL) : "unknown")
88 << ". ");
89
90 if (!HasRegForVL || RegForVL != Reg) {
91 // Use VL, but a different value in a different scalar register.
92 // So, generate new LVL instruction just before the current instruction.
93 LLVM_DEBUG(dbgs() << "Generate a LVL instruction to load "
94 << RegName(Reg) << ".\n");
95 BuildMI(MBB, I, MI->getDebugLoc(), TII->get(VE::LVLr)).addReg(Reg);
96 HasRegForVL = true;
97 RegForVL = Reg;
98 Changed = true;
99 } else {
100 LLVM_DEBUG(dbgs() << "Reuse current VL.\n");
101 }
102 }
103 // Check the update of a given scalar register holding an immediate value
104 // for VL register. Also, a call doesn't preserve VL register.
105 if (HasRegForVL) {
106 if (MI->definesRegister(RegForVL, TRI) ||
107 MI->modifiesRegister(RegForVL, TRI) ||
108 MI->killsRegister(RegForVL, TRI) || MI->isCall()) {
109 // The latest VL is needed to be updated, so disable HasRegForVL.
110 LLVM_DEBUG(dbgs() << RegName(RegForVL) << " is needed to be updated: ");
111 LLVM_DEBUG(MI->dump());
112 HasRegForVL = false;
113 }
114 }
115
116 ++I;
117 }
118 return Changed;
119}
120
122 LLVM_DEBUG(dbgs() << "********** Begin LVLGen **********\n");
123 LLVM_DEBUG(dbgs() << "********** Function: " << F.getName() << '\n');
124 LLVM_DEBUG(F.dump());
125
126 bool Changed = false;
127
128 const VESubtarget &Subtarget = F.getSubtarget<VESubtarget>();
129 const VEInstrInfo *TII = Subtarget.getInstrInfo();
130 const VERegisterInfo *TRI = Subtarget.getRegisterInfo();
131
132 for (MachineBasicBlock &MBB : F)
134
135 if (Changed) {
136 LLVM_DEBUG(dbgs() << "\n");
137 LLVM_DEBUG(F.dump());
138 }
139 LLVM_DEBUG(dbgs() << "********** End LVLGen **********\n");
140 return Changed;
141}
142
143bool VELVLGenLegacy::runOnMachineFunction(MachineFunction &MF) {
145}
146
MachineBasicBlock & MBB
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define RegName(no)
static bool runLVLGenOnMachineFunction(MachineFunction &F)
Definition LVLGen.cpp:121
static bool runLVLGenOnMachineBasicBlock(MachineBasicBlock &MBB, const VEInstrInfo *TII, const VERegisterInfo *TRI)
Definition LVLGen.cpp:62
static unsigned getVL(const MachineInstr &MI, const VEInstrInfo *TII)
Definition LVLGen.cpp:54
static int getVLIndex(unsigned Opcode, const VEInstrInfo *TII)
Definition LVLGen.cpp:42
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
#define LLVM_DEBUG(...)
Definition Debug.h:119
#define GET_VLINDEX(TSF)
Definition VEInstrInfo.h:47
#define HAS_VLINDEX(TSF)
Definition VEInstrInfo.h:46
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:280
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
Describe properties that are true of each instruction in the target description file.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Definition LVLGen.cpp:147
const VEInstrInfo * getInstrInfo() const override
Definition VESubtarget.h:52
const VERegisterInfo * getRegisterInfo() const override
Definition VESubtarget.h:56
Pass manager infrastructure for declaring and invalidating analyses.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
FunctionPass * createVELVLGenLegacyPass()
Definition LVLGen.cpp:40