LLVM 20.0.0git
HexagonLoopAlign.cpp
Go to the documentation of this file.
1//===----- HexagonLoopAlign.cpp - Generate loop alignment directives -----===//
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// Inspect a basic block and if its single basic block loop with a small
9// number of instructions, set the prefLoopAlignment to 32 bytes (5).
10//===----------------------------------------------------------------------===//
11
12#define DEBUG_TYPE "hexagon-loop-align"
13
17#include "llvm/Support/Debug.h"
18
19using namespace llvm;
20
21static cl::opt<bool>
22 DisableLoopAlign("disable-hexagon-loop-align", cl::Hidden,
23 cl::desc("Disable Hexagon loop alignment pass"));
24
26 "hexagon-hvx-loop-align-limit-ub", cl::Hidden, cl::init(16),
27 cl::desc("Set hexagon hvx loop upper bound align limit"));
28
30 "hexagon-tiny-loop-align-limit-ub", cl::Hidden, cl::init(16),
31 cl::desc("Set hexagon tiny-core loop upper bound align limit"));
32
34 LoopAlignLimitUB("hexagon-loop-align-limit-ub", cl::Hidden, cl::init(8),
35 cl::desc("Set hexagon loop upper bound align limit"));
36
38 LoopAlignLimitLB("hexagon-loop-align-limit-lb", cl::Hidden, cl::init(4),
39 cl::desc("Set hexagon loop lower bound align limit"));
40
42 LoopBndlAlignLimit("hexagon-loop-bundle-align-limit", cl::Hidden,
43 cl::init(4),
44 cl::desc("Set hexagon loop align bundle limit"));
45
47 "hexagon-tiny-loop-bundle-align-limit", cl::Hidden, cl::init(8),
48 cl::desc("Set hexagon tiny-core loop align bundle limit"));
49
51 LoopEdgeThreshold("hexagon-loop-edge-threshold", cl::Hidden, cl::init(7500),
52 cl::desc("Set hexagon loop align edge theshold"));
53
54namespace llvm {
57} // namespace llvm
58
59namespace {
60
61class HexagonLoopAlign : public MachineFunctionPass {
62 const HexagonSubtarget *HST = nullptr;
63 const TargetMachine *HTM = nullptr;
64 const HexagonInstrInfo *HII = nullptr;
65
66public:
67 static char ID;
68 HexagonLoopAlign() : MachineFunctionPass(ID) {
70 }
71 bool shouldBalignLoop(MachineBasicBlock &BB, bool AboveThres);
72 bool isSingleLoop(MachineBasicBlock &MBB);
73 bool attemptToBalignSmallLoop(MachineFunction &MF, MachineBasicBlock &MBB);
74
75 void getAnalysisUsage(AnalysisUsage &AU) const override {
79 }
80
81 StringRef getPassName() const override { return "Hexagon LoopAlign pass"; }
82 bool runOnMachineFunction(MachineFunction &MF) override;
83};
84
85char HexagonLoopAlign::ID = 0;
86
87bool HexagonLoopAlign::shouldBalignLoop(MachineBasicBlock &BB,
88 bool AboveThres) {
89 bool isVec = false;
90 unsigned InstCnt = 0;
91 unsigned BndlCnt = 0;
92
94 IE = BB.instr_end();
95 II != IE; ++II) {
96
97 // End if the instruction is endloop.
98 if (HII->isEndLoopN(II->getOpcode()))
99 break;
100 // Count the number of bundles.
101 if (II->isBundle()) {
102 BndlCnt++;
103 continue;
104 }
105 // Skip over debug instructions.
106 if (II->isDebugInstr())
107 continue;
108 // Check if there are any HVX instructions in loop.
109 isVec |= HII->isHVXVec(*II);
110 // Count the number of instructions.
111 InstCnt++;
112 }
113
114 LLVM_DEBUG({
115 dbgs() << "Bundle Count : " << BndlCnt << "\n";
116 dbgs() << "Instruction Count : " << InstCnt << "\n";
117 });
118
119 unsigned LimitUB = 0;
120 unsigned LimitBndl = LoopBndlAlignLimit;
121 // The conditions in the order of priority.
122 if (HST->isTinyCore()) {
123 LimitUB = TinyLoopAlignLimitUB;
124 LimitBndl = TinyLoopBndlAlignLimit;
125 } else if (isVec)
126 LimitUB = HVXLoopAlignLimitUB;
127 else if (AboveThres)
128 LimitUB = LoopAlignLimitUB;
129
130 // if the upper bound is not set to a value, implies we didn't meet
131 // the criteria.
132 if (LimitUB == 0)
133 return false;
134
135 return InstCnt >= LoopAlignLimitLB && InstCnt <= LimitUB &&
136 BndlCnt <= LimitBndl;
137}
138
139bool HexagonLoopAlign::isSingleLoop(MachineBasicBlock &MBB) {
140 int Succs = MBB.succ_size();
141 return (MBB.isSuccessor(&MBB) && (Succs == 2));
142}
143
144bool HexagonLoopAlign::attemptToBalignSmallLoop(MachineFunction &MF,
146 if (!isSingleLoop(MBB))
147 return false;
148
149 const MachineBranchProbabilityInfo *MBPI =
150 &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
151 const MachineBlockFrequencyInfo *MBFI =
152 &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
153
154 // Compute frequency of back edge,
155 BlockFrequency BlockFreq = MBFI->getBlockFreq(&MBB);
157 BlockFrequency EdgeFreq = BlockFreq * BrProb;
158 LLVM_DEBUG({
159 dbgs() << "Loop Align Pass:\n";
160 dbgs() << "\tedge with freq(" << EdgeFreq.getFrequency() << ")\n";
161 });
162
163 bool AboveThres = EdgeFreq.getFrequency() > LoopEdgeThreshold;
164 if (shouldBalignLoop(MBB, AboveThres)) {
165 // We found a loop, change its alignment to be 32 (5).
167 return true;
168 }
169 return false;
170}
171
172// Inspect each basic block, and if its a single BB loop, see if it
173// meets the criteria for increasing alignment to 32.
174
175bool HexagonLoopAlign::runOnMachineFunction(MachineFunction &MF) {
176
177 HST = &MF.getSubtarget<HexagonSubtarget>();
178 HII = HST->getInstrInfo();
179 HTM = &MF.getTarget();
180
181 if (skipFunction(MF.getFunction()))
182 return false;
184 return false;
185
186 // This optimization is performed at
187 // i) -O2 and above, and when the loop has a HVX instruction.
188 // ii) -O3
189 if (HST->useHVXOps()) {
190 if (HTM->getOptLevel() < CodeGenOptLevel::Default)
191 return false;
192 } else {
193 if (HTM->getOptLevel() < CodeGenOptLevel::Aggressive)
194 return false;
195 }
196
197 bool Changed = false;
198 for (MachineFunction::iterator MBBi = MF.begin(), MBBe = MF.end();
199 MBBi != MBBe; ++MBBi) {
200 MachineBasicBlock &MBB = *MBBi;
201 Changed |= attemptToBalignSmallLoop(MF, MBB);
202 }
203 return Changed;
204}
205
206} // namespace
207
208INITIALIZE_PASS(HexagonLoopAlign, "hexagon-loop-align",
209 "Hexagon LoopAlign pass", false, false)
210
211//===----------------------------------------------------------------------===//
212// Public Constructor Functions
213//===----------------------------------------------------------------------===//
214
215FunctionPass *llvm::createHexagonLoopAlign() { return new HexagonLoopAlign(); }
MachineBasicBlock & MBB
#define LLVM_DEBUG(...)
Definition: Debug.h:106
static cl::opt< uint32_t > LoopEdgeThreshold("hexagon-loop-edge-threshold", cl::Hidden, cl::init(7500), cl::desc("Set hexagon loop align edge theshold"))
static cl::opt< uint32_t > LoopBndlAlignLimit("hexagon-loop-bundle-align-limit", cl::Hidden, cl::init(4), cl::desc("Set hexagon loop align bundle limit"))
static cl::opt< uint32_t > LoopAlignLimitUB("hexagon-loop-align-limit-ub", cl::Hidden, cl::init(8), cl::desc("Set hexagon loop upper bound align limit"))
static cl::opt< uint32_t > LoopAlignLimitLB("hexagon-loop-align-limit-lb", cl::Hidden, cl::init(4), cl::desc("Set hexagon loop lower bound align limit"))
static cl::opt< uint32_t > HVXLoopAlignLimitUB("hexagon-hvx-loop-align-limit-ub", cl::Hidden, cl::init(16), cl::desc("Set hexagon hvx loop upper bound align limit"))
static cl::opt< bool > DisableLoopAlign("disable-hexagon-loop-align", cl::Hidden, cl::desc("Disable Hexagon loop alignment pass"))
static cl::opt< uint32_t > TinyLoopAlignLimitUB("hexagon-tiny-loop-align-limit-ub", cl::Hidden, cl::init(16), cl::desc("Set hexagon tiny-core loop upper bound align limit"))
static cl::opt< uint32_t > TinyLoopBndlAlignLimit("hexagon-tiny-loop-bundle-align-limit", cl::Hidden, cl::init(8), cl::desc("Set hexagon tiny-core loop align bundle limit"))
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
bool isHVXVec(const MachineInstr &MI) const
bool isEndLoopN(unsigned Opcode) const
const HexagonInstrInfo * getInstrInfo() const override
instr_iterator instr_begin()
unsigned succ_size() const
void setAlignment(Align A)
Set alignment of the basic block.
Instructions::iterator instr_iterator
instr_iterator instr_end()
bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const
getblockFreq - Return block frequency.
BranchProbability getEdgeProbability(const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
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.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:37
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
FunctionPass * createHexagonLoopAlign()
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void initializeHexagonLoopAlignPass(PassRegistry &)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39