LLVM 19.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
18#include "llvm/Support/Debug.h"
19
20using namespace llvm;
21
22static cl::opt<bool>
23 DisableLoopAlign("disable-hexagon-loop-align", cl::Hidden,
24 cl::desc("Disable Hexagon loop alignment pass"));
25
27 "hexagon-hvx-loop-align-limit-ub", cl::Hidden, cl::init(16),
28 cl::desc("Set hexagon hvx loop upper bound align limit"));
29
31 "hexagon-tiny-loop-align-limit-ub", cl::Hidden, cl::init(16),
32 cl::desc("Set hexagon tiny-core loop upper bound align limit"));
33
35 LoopAlignLimitUB("hexagon-loop-align-limit-ub", cl::Hidden, cl::init(8),
36 cl::desc("Set hexagon loop upper bound align limit"));
37
39 LoopAlignLimitLB("hexagon-loop-align-limit-lb", cl::Hidden, cl::init(4),
40 cl::desc("Set hexagon loop lower bound align limit"));
41
43 LoopBndlAlignLimit("hexagon-loop-bundle-align-limit", cl::Hidden,
44 cl::init(4),
45 cl::desc("Set hexagon loop align bundle limit"));
46
48 "hexagon-tiny-loop-bundle-align-limit", cl::Hidden, cl::init(8),
49 cl::desc("Set hexagon tiny-core loop align bundle limit"));
50
52 LoopEdgeThreshold("hexagon-loop-edge-threshold", cl::Hidden, cl::init(7500),
53 cl::desc("Set hexagon loop align edge theshold"));
54
55namespace llvm {
58} // namespace llvm
59
60namespace {
61
62class HexagonLoopAlign : public MachineFunctionPass {
63 const HexagonSubtarget *HST = nullptr;
64 const TargetMachine *HTM = nullptr;
65 const HexagonInstrInfo *HII = nullptr;
66
67public:
68 static char ID;
69 HexagonLoopAlign() : MachineFunctionPass(ID) {
71 }
72 bool shouldBalignLoop(MachineBasicBlock &BB, bool AboveThres);
73 bool isSingleLoop(MachineBasicBlock &MBB);
74 bool attemptToBalignSmallLoop(MachineFunction &MF, MachineBasicBlock &MBB);
75
76 void getAnalysisUsage(AnalysisUsage &AU) const override {
80 }
81
82 StringRef getPassName() const override { return "Hexagon LoopAlign pass"; }
83 bool runOnMachineFunction(MachineFunction &MF) override;
84};
85
86char HexagonLoopAlign::ID = 0;
87
88bool HexagonLoopAlign::shouldBalignLoop(MachineBasicBlock &BB,
89 bool AboveThres) {
90 bool isVec = false;
91 unsigned InstCnt = 0;
92 unsigned BndlCnt = 0;
93
95 IE = BB.instr_end();
96 II != IE; ++II) {
97
98 // End if the instruction is endloop.
99 if (HII->isEndLoopN(II->getOpcode()))
100 break;
101 // Count the number of bundles.
102 if (II->isBundle()) {
103 BndlCnt++;
104 continue;
105 }
106 // Skip over debug instructions.
107 if (II->isDebugInstr())
108 continue;
109 // Check if there are any HVX instructions in loop.
110 isVec |= HII->isHVXVec(*II);
111 // Count the number of instructions.
112 InstCnt++;
113 }
114
115 LLVM_DEBUG({
116 dbgs() << "Bundle Count : " << BndlCnt << "\n";
117 dbgs() << "Instruction Count : " << InstCnt << "\n";
118 });
119
120 unsigned LimitUB = 0;
121 unsigned LimitBndl = LoopBndlAlignLimit;
122 // The conditions in the order of priority.
123 if (HST->isTinyCore()) {
124 LimitUB = TinyLoopAlignLimitUB;
125 LimitBndl = TinyLoopBndlAlignLimit;
126 } else if (isVec)
127 LimitUB = HVXLoopAlignLimitUB;
128 else if (AboveThres)
129 LimitUB = LoopAlignLimitUB;
130
131 // if the upper bound is not set to a value, implies we didn't meet
132 // the criteria.
133 if (LimitUB == 0)
134 return false;
135
136 return InstCnt >= LoopAlignLimitLB && InstCnt <= LimitUB &&
137 BndlCnt <= LimitBndl;
138}
139
140bool HexagonLoopAlign::isSingleLoop(MachineBasicBlock &MBB) {
141 int Succs = MBB.succ_size();
142 return (MBB.isSuccessor(&MBB) && (Succs == 2));
143}
144
145bool HexagonLoopAlign::attemptToBalignSmallLoop(MachineFunction &MF,
147 if (!isSingleLoop(MBB))
148 return false;
149
150 const MachineBranchProbabilityInfo *MBPI =
151 &getAnalysis<MachineBranchProbabilityInfo>();
152 const MachineBlockFrequencyInfo *MBFI =
153 &getAnalysis<MachineBlockFrequencyInfo>();
154
155 // Compute frequency of back edge,
156 BlockFrequency BlockFreq = MBFI->getBlockFreq(&MBB);
158 BlockFrequency EdgeFreq = BlockFreq * BrProb;
159 LLVM_DEBUG({
160 dbgs() << "Loop Align Pass:\n";
161 dbgs() << "\tedge with freq(" << EdgeFreq.getFrequency() << ")\n";
162 });
163
164 bool AboveThres = EdgeFreq.getFrequency() > LoopEdgeThreshold;
165 if (shouldBalignLoop(MBB, AboveThres)) {
166 // We found a loop, change its alignment to be 32 (5).
168 return true;
169 }
170 return false;
171}
172
173// Inspect each basic block, and if its a single BB loop, see if it
174// meets the criteria for increasing alignment to 32.
175
176bool HexagonLoopAlign::runOnMachineFunction(MachineFunction &MF) {
177
178 HST = &MF.getSubtarget<HexagonSubtarget>();
179 HII = HST->getInstrInfo();
180 HTM = &MF.getTarget();
181
182 if (skipFunction(MF.getFunction()))
183 return false;
185 return false;
186
187 // This optimization is performed at
188 // i) -O2 and above, and when the loop has a HVX instruction.
189 // ii) -O3
190 if (HST->useHVXOps()) {
191 if (HTM->getOptLevel() < CodeGenOptLevel::Default)
192 return false;
193 } else {
194 if (HTM->getOptLevel() < CodeGenOptLevel::Aggressive)
195 return false;
196 }
197
198 bool Changed = false;
199 for (MachineFunction::iterator MBBi = MF.begin(), MBBe = MF.end();
200 MBBi != MBBe; ++MBBi) {
201 MachineBasicBlock &MBB = *MBBi;
202 Changed |= attemptToBalignSmallLoop(MF, MBB);
203 }
204 return Changed;
205}
206
207} // namespace
208
209INITIALIZE_PASS(HexagonLoopAlign, "hexagon-loop-align",
210 "Hexagon LoopAlign pass", false, false)
211
212//===----------------------------------------------------------------------===//
213// Public Constructor Functions
214//===----------------------------------------------------------------------===//
215
216FunctionPass *llvm::createHexagonLoopAlign() { return new HexagonLoopAlign(); }
MachineBasicBlock & MBB
#define LLVM_DEBUG(X)
Definition: Debug.h:101
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"))
#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:311
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 LLVMTargetMachine & 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:50
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:76
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:450
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