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"
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();
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(MI.getOpcode(), TII);
55 if (Index >= 0)
56 return MI.getOperand(Index).getReg();
57
58 return VE::NoRegister;
59}
60
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();) {
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, 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(MBB, I, MI->getDebugLoc(), TII->get(VE::LVLr)).addReg(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(RegForVL, TRI) ||
106 MI->modifiesRegister(RegForVL, TRI) ||
107 MI->killsRegister(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
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)
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) {
144}
145
const AArch64Subtarget & Subtarget
MachineBasicBlock & MBB
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define RegName(no)
static bool runLVLGenOnMachineFunction(MachineFunction &F)
Definition LVLGen.cpp:120
static bool runLVLGenOnMachineBasicBlock(MachineBasicBlock &MBB, const VEInstrInfo *TII, const VERegisterInfo *TRI)
Definition LVLGen.cpp:61
static unsigned getVL(const MachineInstr &MI, const VEInstrInfo *TII)
Definition LVLGen.cpp:53
static int getVLIndex(unsigned Opcode, const VEInstrInfo *TII)
Definition LVLGen.cpp:41
#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:146
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:39