21
21
#include " llvm/ADT/Statistic.h"
22
22
#include " llvm/Analysis/AssumptionCache.h"
23
23
#include " llvm/Analysis/LoopInfo.h"
24
- #include " llvm/Analysis/OptimizationRemarkEmitter.h"
25
24
#include " llvm/Analysis/ScalarEvolution.h"
26
25
#include " llvm/Analysis/ScalarEvolutionExpander.h"
27
26
#include " llvm/Analysis/TargetTransformInfo.h"
@@ -76,44 +75,8 @@ ForceGuardLoopEntry(
76
75
77
76
STATISTIC (NumHWLoops, " Number of loops converted to hardware loops" );
78
77
79
- #ifndef NDEBUG
80
- static void debugHWLoopFailure (const StringRef DebugMsg,
81
- Instruction *I) {
82
- dbgs () << " HWLoops: " << DebugMsg;
83
- if (I)
84
- dbgs () << ' ' << *I;
85
- else
86
- dbgs () << ' .' ;
87
- dbgs () << ' \n ' ;
88
- }
89
- #endif
90
-
91
- static OptimizationRemarkAnalysis
92
- createHWLoopAnalysis (StringRef RemarkName, Loop *L, Instruction *I) {
93
- Value *CodeRegion = L->getHeader ();
94
- DebugLoc DL = L->getStartLoc ();
95
-
96
- if (I) {
97
- CodeRegion = I->getParent ();
98
- // If there is no debug location attached to the instruction, revert back to
99
- // using the loop's.
100
- if (I->getDebugLoc ())
101
- DL = I->getDebugLoc ();
102
- }
103
-
104
- OptimizationRemarkAnalysis R (DEBUG_TYPE, RemarkName, DL, CodeRegion);
105
- R << " hardware-loop not created: " ;
106
- return R;
107
- }
108
-
109
78
namespace {
110
79
111
- void reportHWLoopFailure (const StringRef Msg, const StringRef ORETag,
112
- OptimizationRemarkEmitter *ORE, Loop *TheLoop, Instruction *I = nullptr ) {
113
- LLVM_DEBUG (debugHWLoopFailure (Msg, I));
114
- ORE->emit (createHWLoopAnalysis (ORETag, TheLoop, I) << Msg);
115
- }
116
-
117
80
using TTI = TargetTransformInfo;
118
81
119
82
class HardwareLoops : public FunctionPass {
@@ -134,7 +97,6 @@ namespace {
134
97
AU.addRequired <ScalarEvolutionWrapperPass>();
135
98
AU.addRequired <AssumptionCacheTracker>();
136
99
AU.addRequired <TargetTransformInfoWrapperPass>();
137
- AU.addRequired <OptimizationRemarkEmitterWrapperPass>();
138
100
}
139
101
140
102
// Try to convert the given Loop into a hardware loop.
@@ -148,7 +110,6 @@ namespace {
148
110
ScalarEvolution *SE = nullptr ;
149
111
LoopInfo *LI = nullptr ;
150
112
const DataLayout *DL = nullptr ;
151
- OptimizationRemarkEmitter *ORE = nullptr ;
152
113
const TargetTransformInfo *TTI = nullptr ;
153
114
DominatorTree *DT = nullptr ;
154
115
bool PreserveLCSSA = false ;
@@ -182,9 +143,8 @@ namespace {
182
143
183
144
public:
184
145
HardwareLoop (HardwareLoopInfo &Info, ScalarEvolution &SE,
185
- const DataLayout &DL,
186
- OptimizationRemarkEmitter *ORE) :
187
- SE (SE), DL(DL), ORE(ORE), L(Info.L), M(L->getHeader ()->getModule()),
146
+ const DataLayout &DL) :
147
+ SE (SE), DL(DL), L(Info.L), M(L->getHeader ()->getModule()),
188
148
ExitCount(Info.ExitCount),
189
149
CountType(Info.CountType),
190
150
ExitBranch(Info.ExitBranch),
@@ -197,7 +157,6 @@ namespace {
197
157
private:
198
158
ScalarEvolution &SE;
199
159
const DataLayout &DL;
200
- OptimizationRemarkEmitter *ORE = nullptr ;
201
160
Loop *L = nullptr ;
202
161
Module *M = nullptr ;
203
162
const SCEV *ExitCount = nullptr ;
@@ -223,7 +182,6 @@ bool HardwareLoops::runOnFunction(Function &F) {
223
182
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree ();
224
183
TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI (F);
225
184
DL = &F.getParent ()->getDataLayout ();
226
- ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE ();
227
185
auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
228
186
LibInfo = TLIP ? &TLIP->getTLI (F) : nullptr ;
229
187
PreserveLCSSA = mustPreserveAnalysisID (LCSSAID);
@@ -243,39 +201,31 @@ bool HardwareLoops::runOnFunction(Function &F) {
243
201
// converted and the parent loop doesn't support containing a hardware loop.
244
202
bool HardwareLoops::TryConvertLoop (Loop *L) {
245
203
// Process nested loops first.
246
- for (Loop::iterator I = L->begin (), E = L->end (); I != E; ++I) {
247
- if (TryConvertLoop (*I)) {
248
- reportHWLoopFailure (" nested hardware-loops not supported" , " HWLoopNested" ,
249
- ORE, L);
204
+ for (Loop::iterator I = L->begin (), E = L->end (); I != E; ++I)
205
+ if (TryConvertLoop (*I))
250
206
return true ; // Stop search.
251
- }
252
- }
253
207
254
208
HardwareLoopInfo HWLoopInfo (L);
255
- if (!HWLoopInfo.canAnalyze (*LI)) {
256
- reportHWLoopFailure (" cannot analyze loop, irreducible control flow" ,
257
- " HWLoopCannotAnalyze" , ORE, L);
209
+ if (!HWLoopInfo.canAnalyze (*LI))
258
210
return false ;
259
- }
260
211
261
- if (!ForceHardwareLoops &&
262
- !TTI->isHardwareLoopProfitable (L, *SE, *AC, LibInfo, HWLoopInfo)) {
263
- reportHWLoopFailure (" it's not profitable to create a hardware-loop" ,
264
- " HWLoopNotProfitable" , ORE, L);
265
- return false ;
266
- }
212
+ if (TTI->isHardwareLoopProfitable (L, *SE, *AC, LibInfo, HWLoopInfo) ||
213
+ ForceHardwareLoops) {
214
+
215
+ // Allow overriding of the counter width and loop decrement value.
216
+ if (CounterBitWidth.getNumOccurrences ())
217
+ HWLoopInfo.CountType =
218
+ IntegerType::get (M->getContext (), CounterBitWidth);
267
219
268
- // Allow overriding of the counter width and loop decrement value.
269
- if (CounterBitWidth.getNumOccurrences ())
270
- HWLoopInfo.CountType =
271
- IntegerType::get (M->getContext (), CounterBitWidth);
220
+ if (LoopDecrement.getNumOccurrences ())
221
+ HWLoopInfo.LoopDecrement =
222
+ ConstantInt::get (HWLoopInfo.CountType , LoopDecrement);
272
223
273
- if (LoopDecrement. getNumOccurrences ())
274
- HWLoopInfo.LoopDecrement =
275
- ConstantInt::get (HWLoopInfo. CountType , LoopDecrement);
224
+ MadeChange |= TryConvertLoop (HWLoopInfo);
225
+ return MadeChange && (! HWLoopInfo.IsNestingLegal && !ForceNestedLoop);
226
+ }
276
227
277
- MadeChange |= TryConvertLoop (HWLoopInfo);
278
- return MadeChange && (!HWLoopInfo.IsNestingLegal && !ForceNestedLoop);
228
+ return false ;
279
229
}
280
230
281
231
bool HardwareLoops::TryConvertLoop (HardwareLoopInfo &HWLoopInfo) {
@@ -284,13 +234,8 @@ bool HardwareLoops::TryConvertLoop(HardwareLoopInfo &HWLoopInfo) {
284
234
LLVM_DEBUG (dbgs () << " HWLoops: Try to convert profitable loop: " << *L);
285
235
286
236
if (!HWLoopInfo.isHardwareLoopCandidate (*SE, *LI, *DT, ForceNestedLoop,
287
- ForceHardwareLoopPHI)) {
288
- // TODO: there can be many reasons a loop is not considered a
289
- // candidate, so we should let isHardwareLoopCandidate fill in the
290
- // reason and then report a better message here.
291
- reportHWLoopFailure (" loop is not a candidate" , " HWLoopNoCandidate" , ORE, L);
237
+ ForceHardwareLoopPHI))
292
238
return false ;
293
- }
294
239
295
240
assert (
296
241
(HWLoopInfo.ExitBlock && HWLoopInfo.ExitBranch && HWLoopInfo.ExitCount ) &&
@@ -304,21 +249,18 @@ bool HardwareLoops::TryConvertLoop(HardwareLoopInfo &HWLoopInfo) {
304
249
if (!Preheader)
305
250
return false ;
306
251
307
- HardwareLoop HWLoop (HWLoopInfo, *SE, *DL, ORE );
252
+ HardwareLoop HWLoop (HWLoopInfo, *SE, *DL);
308
253
HWLoop.Create ();
309
254
++NumHWLoops;
310
255
return true ;
311
256
}
312
257
313
258
void HardwareLoop::Create () {
314
259
LLVM_DEBUG (dbgs () << " HWLoops: Converting loop..\n " );
315
-
260
+
316
261
Value *LoopCountInit = InitLoopCount ();
317
- if (!LoopCountInit) {
318
- reportHWLoopFailure (" could not safely create a loop count expression" ,
319
- " HWLoopNotSafe" , ORE, L);
262
+ if (!LoopCountInit)
320
263
return ;
321
- }
322
264
323
265
InsertIterationSetup (LoopCountInit);
324
266
0 commit comments