LLVM 24.0.0git
HexagonMergeActivateWeight.cpp
Go to the documentation of this file.
1//===-- HexagonMergeActivateWeight.cpp - Pair HMX activate/weight ---------===//
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#define DEBUG_TYPE "merge-hmx-activate-weight"
10
11#include "Hexagon.h"
12#include "HexagonInstrInfo.h"
13#include "HexagonSubtarget.h"
25#include "llvm/Support/Debug.h"
27#include <iterator>
28
29using namespace llvm;
30
31namespace llvm {
32
33// A class to generate a warning message about weight and activate
35
36public:
39 : DiagnosticInfoResourceLimit(Fn, "activate weight pair", S, Severity) {}
40
41 void print(DiagnosticPrinter &DP) const override {
42 DP << "In " << getFunction() << ", "
43 << "expected activate and weight pair";
44 }
45
46 static bool classof(const DiagnosticInfo *DI) {
47 return DI->getKind() == DK_ResourceLimit;
48 }
49};
50
51// Return true if \p Moved can be moved past every instruction in the range
52// [Begin, End), in either direction. \p Moved is an HMX activation or weight
53// instruction, which reads memory and updates the HMX state, so it must not be
54// reordered with:
55// - calls, inline asm, barriers or terminators,
56// - side-effecting instructions (e.g. mxclracc, bias/cvt mxmem forms and any
57// other HMX instruction),
58// - volatile or atomic memory accesses,
59// - stores, which may write the memory read by \p Moved,
60// - instructions that redefine a register used by \p Moved, or that read or
61// redefine a register defined by \p Moved.
62static bool canMoveAcross(const MachineInstr &Moved,
65 const TargetRegisterInfo *TRI) {
66 for (const MachineInstr &MI : make_range(Begin, End)) {
67 if (MI.isDebugInstr() || &MI == &Moved)
68 continue;
69
70 if (MI.isCall() || MI.isInlineAsm() || MI.isBarrier() ||
71 MI.isTerminator() || MI.hasUnmodeledSideEffects() ||
72 MI.hasOrderedMemoryRef() || MI.mayStore()) {
73 LLVM_DEBUG(dbgs() << "Cannot move " << Moved << " past " << MI);
74 return false;
75 }
76
77 for (const MachineOperand &MO : Moved.operands()) {
78 if (!MO.isReg() || !MO.getReg())
79 continue;
80 Register R = MO.getReg();
81 if (MI.modifiesRegister(R, TRI) ||
82 (MO.isDef() && MI.readsRegister(R, TRI))) {
83 LLVM_DEBUG(dbgs() << "Register " << printReg(R, TRI)
84 << " prevents moving " << Moved << " past " << MI);
85 return false;
86 }
87 }
88 }
89 return true;
90}
91
92// Move each HMX activation instruction next to its matching weight instruction
93// (or vice versa), so that the packetizer can put them in the same packet, as
94// required by the hardware. Warn about activation or weight instructions that
95// cannot be paired.
97 auto &HST = MF.getSubtarget<HexagonSubtarget>();
98 const HexagonInstrInfo *HII = HST.getInstrInfo();
99 const TargetRegisterInfo *TRI = HST.getRegisterInfo();
100 const Function &F = MF.getFunction();
101 bool Changed = false;
102
103 auto Warn = [&]() {
104 DiagnosticInfoWeightActivate WeightActivate(F, 1);
105 F.getContext().diagnose(WeightActivate);
106 };
107
108 for (MachineBasicBlock &MBB : MF) {
109 // The first instruction of a pair, waiting for its partner.
110 MachineInstr *Pending = nullptr;
111
113 bool IsActivate = HII->isHMXActivateInstr(MI);
114 if (!IsActivate && !HII->isHMXWeightInstr(MI))
115 continue;
116 if (!Pending) {
117 Pending = &MI;
118 continue;
119 }
120 if (IsActivate == HII->isHMXActivateInstr(*Pending)) {
121 // Two activations or two weights in a row.
122 Warn();
123 Pending = &MI;
124 continue;
125 }
126
127 // Pending and MI form a pair. Sink the first one down to the second,
128 // or, if that is not possible, hoist the second one up to the first.
130 std::next(MachineBasicBlock::iterator(Pending));
132 if (canMoveAcross(*Pending, Between, Second, TRI)) {
133 if (Between != Second) {
134 MBB.splice(Second, &MBB, Pending);
135 if (LIS)
136 LIS->handleMove(*Pending, /*UpdateFlags=*/true);
137 Changed = true;
138 }
139 } else if (canMoveAcross(MI, Between, Second, TRI)) {
140 MBB.splice(Between, &MBB, &MI);
141 if (LIS)
142 LIS->handleMove(MI, /*UpdateFlags=*/true);
143 Changed = true;
144 } else {
145 Warn();
146 }
147 Pending = nullptr;
148 }
149
150 if (Pending)
151 Warn();
152 }
153
154 return Changed;
155}
156
158public:
159 static char ID;
164
165 StringRef getPassName() const override {
166 return "Hexagon Merge Activate Weight Instructions After Machine Scheduler";
167 }
169 return runActivateWeight(MF,
171 }
172
180};
181
184
186public:
187 static char ID;
192
193 StringRef getPassName() const override {
194 return "Hexagon Merge Activate Weight Instructions Before Packetizer";
195 }
197 return runActivateWeight(MF, /*LIS=*/nullptr);
198 }
199};
200
202
203} // namespace llvm
204
205//===----------------------------------------------------------------------===//
206// Public Constructor Functions
207//===----------------------------------------------------------------------===//
208
210 "hexagon-merge-activate-weight",
211 "Hexagon merge activate weight after machine scheduler",
212 false, false)
214INITIALIZE_PASS_END(HexagonMergeActivateWeight, "hexagon-merge-activate-weight",
215 "Hexagon merge activate weight after machine scheduler",
217
218INITIALIZE_PASS(HexagonMergeActivateWeight2, "hexagon-merge-activate-weight-2",
219 "Hexagon merge activate weight before packetizer", false, false)
220
222 return new HexagonMergeActivateWeight();
223}
224
MachineBasicBlock & MBB
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:54
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
#define LLVM_DEBUG(...)
Definition Debug.h:119
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:280
const Function & getFunction() const
DiagnosticInfoResourceLimit(const Function &Fn, const Twine &ResourceName LLVM_LIFETIME_BOUND, uint64_t ResourceSize, uint64_t ResourceLimit, DiagnosticSeverity Severity=DS_Warning, DiagnosticKind Kind=DK_ResourceLimit)
The function that is concerned by this stack size diagnostic.
static bool classof(const DiagnosticInfo *DI)
void print(DiagnosticPrinter &DP) const override
DiagnosticInfoWeightActivate(const Function &Fn, unsigned S, DiagnosticSeverity Severity=DS_Warning)
This is the base abstract class for diagnostic reporting in the backend.
Interface for custom diagnostic printing.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
LLVM_ABI void handleMove(MachineInstr &MI, bool UpdateFlags=false)
Call this method to notify LiveIntervals that instruction MI has been moved within a basic block.
MachineInstrBundleIterator< const MachineInstr > const_iterator
MachineInstrBundleIterator< MachineInstr > iterator
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
mop_range operands()
MachineOperand class - Representation of each machine instruction operand.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
Wrapper class representing virtual and physical registers.
Definition Register.h:20
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition Registry.h:116
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TargetPassConfig.
This is an optimization pass for GlobalISel generic memory operations.
FunctionPass * createHexagonMergeActivateWeight()
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
static bool runActivateWeight(MachineFunction &MF, LiveIntervals *LIS)
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:649
FunctionPass * createHexagonMergeActivateWeight2()
@ DK_ResourceLimit
char & HexagonMergeActivateWeightID
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
void initializeHexagonMergeActivateWeight2Pass(PassRegistry &)
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
static bool canMoveAcross(const MachineInstr &Moved, MachineBasicBlock::const_iterator Begin, MachineBasicBlock::const_iterator End, const TargetRegisterInfo *TRI)
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
void initializeHexagonMergeActivateWeightPass(PassRegistry &)
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.