1 //===-- HardwareLoops.cpp - Target Independent Hardware Loops --*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 /// Insert hardware loop intrinsics into loops which are deemed profitable by
10 /// the target, by querying TargetTransformInfo. A hardware loop comprises of
11 /// two intrinsics: one, outside the loop, to set the loop iteration count and
12 /// another, in the exit block, to decrement the counter. The decremented value
13 /// can either be carried through the loop via a phi or handled in some opaque
14 /// way by the target.
16 //===----------------------------------------------------------------------===//
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/Analysis/AssumptionCache.h"
20 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
22 #include "llvm/Analysis/ScalarEvolution.h"
23 #include "llvm/Analysis/TargetLibraryInfo.h"
24 #include "llvm/Analysis/TargetTransformInfo.h"
25 #include "llvm/CodeGen/Passes.h"
26 #include "llvm/CodeGen/TargetPassConfig.h"
27 #include "llvm/IR/BasicBlock.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/Dominators.h"
31 #include "llvm/IR/IRBuilder.h"
32 #include "llvm/IR/Instructions.h"
33 #include "llvm/IR/IntrinsicInst.h"
34 #include "llvm/IR/Value.h"
35 #include "llvm/InitializePasses.h"
36 #include "llvm/Pass.h"
37 #include "llvm/PassRegistry.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Transforms/Scalar.h"
41 #include "llvm/Transforms/Utils.h"
42 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
43 #include "llvm/Transforms/Utils/Local.h"
44 #include "llvm/Transforms/Utils/LoopUtils.h"
45 #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
47 #define DEBUG_TYPE "hardware-loops"
49 #define HW_LOOPS_NAME "Hardware Loop Insertion"
54 ForceHardwareLoops("force-hardware-loops", cl::Hidden
, cl::init(false),
55 cl::desc("Force hardware loops intrinsics to be inserted"));
59 "force-hardware-loop-phi", cl::Hidden
, cl::init(false),
60 cl::desc("Force hardware loop counter to be updated through a phi"));
63 ForceNestedLoop("force-nested-hardware-loop", cl::Hidden
, cl::init(false),
64 cl::desc("Force allowance of nested hardware loops"));
66 static cl::opt
<unsigned>
67 LoopDecrement("hardware-loop-decrement", cl::Hidden
, cl::init(1),
68 cl::desc("Set the loop decrement value"));
70 static cl::opt
<unsigned>
71 CounterBitWidth("hardware-loop-counter-bitwidth", cl::Hidden
, cl::init(32),
72 cl::desc("Set the loop counter bitwidth"));
76 "force-hardware-loop-guard", cl::Hidden
, cl::init(false),
77 cl::desc("Force generation of loop guard intrinsic"));
79 STATISTIC(NumHWLoops
, "Number of loops converted to hardware loops");
82 static void debugHWLoopFailure(const StringRef DebugMsg
,
84 dbgs() << "HWLoops: " << DebugMsg
;
93 static OptimizationRemarkAnalysis
94 createHWLoopAnalysis(StringRef RemarkName
, Loop
*L
, Instruction
*I
) {
95 Value
*CodeRegion
= L
->getHeader();
96 DebugLoc DL
= L
->getStartLoc();
99 CodeRegion
= I
->getParent();
100 // If there is no debug location attached to the instruction, revert back to
102 if (I
->getDebugLoc())
103 DL
= I
->getDebugLoc();
106 OptimizationRemarkAnalysis
R(DEBUG_TYPE
, RemarkName
, DL
, CodeRegion
);
107 R
<< "hardware-loop not created: ";
113 void reportHWLoopFailure(const StringRef Msg
, const StringRef ORETag
,
114 OptimizationRemarkEmitter
*ORE
, Loop
*TheLoop
, Instruction
*I
= nullptr) {
115 LLVM_DEBUG(debugHWLoopFailure(Msg
, I
));
116 ORE
->emit(createHWLoopAnalysis(ORETag
, TheLoop
, I
) << Msg
);
119 using TTI
= TargetTransformInfo
;
121 class HardwareLoops
: public FunctionPass
{
125 HardwareLoops() : FunctionPass(ID
) {
126 initializeHardwareLoopsPass(*PassRegistry::getPassRegistry());
129 bool runOnFunction(Function
&F
) override
;
131 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
132 AU
.addRequired
<LoopInfoWrapperPass
>();
133 AU
.addPreserved
<LoopInfoWrapperPass
>();
134 AU
.addRequired
<DominatorTreeWrapperPass
>();
135 AU
.addPreserved
<DominatorTreeWrapperPass
>();
136 AU
.addRequired
<ScalarEvolutionWrapperPass
>();
137 AU
.addRequired
<AssumptionCacheTracker
>();
138 AU
.addRequired
<TargetTransformInfoWrapperPass
>();
139 AU
.addRequired
<OptimizationRemarkEmitterWrapperPass
>();
142 // Try to convert the given Loop into a hardware loop.
143 bool TryConvertLoop(Loop
*L
);
145 // Given that the target believes the loop to be profitable, try to
147 bool TryConvertLoop(HardwareLoopInfo
&HWLoopInfo
);
150 ScalarEvolution
*SE
= nullptr;
151 LoopInfo
*LI
= nullptr;
152 const DataLayout
*DL
= nullptr;
153 OptimizationRemarkEmitter
*ORE
= nullptr;
154 const TargetTransformInfo
*TTI
= nullptr;
155 DominatorTree
*DT
= nullptr;
156 bool PreserveLCSSA
= false;
157 AssumptionCache
*AC
= nullptr;
158 TargetLibraryInfo
*LibInfo
= nullptr;
160 bool MadeChange
= false;
164 // Expand the trip count scev into a value that we can use.
165 Value
*InitLoopCount();
167 // Insert the set_loop_iteration intrinsic.
168 Value
*InsertIterationSetup(Value
*LoopCountInit
);
170 // Insert the loop_decrement intrinsic.
171 void InsertLoopDec();
173 // Insert the loop_decrement_reg intrinsic.
174 Instruction
*InsertLoopRegDec(Value
*EltsRem
);
176 // If the target requires the counter value to be updated in the loop,
177 // insert a phi to hold the value. The intended purpose is for use by
178 // loop_decrement_reg.
179 PHINode
*InsertPHICounter(Value
*NumElts
, Value
*EltsRem
);
181 // Create a new cmp, that checks the returned value of loop_decrement*,
182 // and update the exit branch to use it.
183 void UpdateBranch(Value
*EltsRem
);
186 HardwareLoop(HardwareLoopInfo
&Info
, ScalarEvolution
&SE
,
187 const DataLayout
&DL
,
188 OptimizationRemarkEmitter
*ORE
) :
189 SE(SE
), DL(DL
), ORE(ORE
), L(Info
.L
), M(L
->getHeader()->getModule()),
190 TripCount(Info
.TripCount
),
191 CountType(Info
.CountType
),
192 ExitBranch(Info
.ExitBranch
),
193 LoopDecrement(Info
.LoopDecrement
),
194 UsePHICounter(Info
.CounterInReg
),
195 UseLoopGuard(Info
.PerformEntryTest
) { }
201 const DataLayout
&DL
;
202 OptimizationRemarkEmitter
*ORE
= nullptr;
205 const SCEV
*TripCount
= nullptr;
206 Type
*CountType
= nullptr;
207 BranchInst
*ExitBranch
= nullptr;
208 Value
*LoopDecrement
= nullptr;
209 bool UsePHICounter
= false;
210 bool UseLoopGuard
= false;
211 BasicBlock
*BeginBB
= nullptr;
215 char HardwareLoops::ID
= 0;
217 bool HardwareLoops::runOnFunction(Function
&F
) {
221 LLVM_DEBUG(dbgs() << "HWLoops: Running on " << F
.getName() << "\n");
223 LI
= &getAnalysis
<LoopInfoWrapperPass
>().getLoopInfo();
224 SE
= &getAnalysis
<ScalarEvolutionWrapperPass
>().getSE();
225 DT
= &getAnalysis
<DominatorTreeWrapperPass
>().getDomTree();
226 TTI
= &getAnalysis
<TargetTransformInfoWrapperPass
>().getTTI(F
);
227 DL
= &F
.getParent()->getDataLayout();
228 ORE
= &getAnalysis
<OptimizationRemarkEmitterWrapperPass
>().getORE();
229 auto *TLIP
= getAnalysisIfAvailable
<TargetLibraryInfoWrapperPass
>();
230 LibInfo
= TLIP
? &TLIP
->getTLI(F
) : nullptr;
231 PreserveLCSSA
= mustPreserveAnalysisID(LCSSAID
);
232 AC
= &getAnalysis
<AssumptionCacheTracker
>().getAssumptionCache(F
);
236 if (L
->isOutermost())
242 // Return true if the search should stop, which will be when an inner loop is
243 // converted and the parent loop doesn't support containing a hardware loop.
244 bool HardwareLoops::TryConvertLoop(Loop
*L
) {
245 // Process nested loops first.
246 bool AnyChanged
= false;
248 AnyChanged
|= TryConvertLoop(SL
);
250 reportHWLoopFailure("nested hardware-loops not supported", "HWLoopNested",
252 return true; // Stop search.
255 LLVM_DEBUG(dbgs() << "HWLoops: Loop " << L
->getHeader()->getName() << "\n");
257 HardwareLoopInfo
HWLoopInfo(L
);
258 if (!HWLoopInfo
.canAnalyze(*LI
)) {
259 reportHWLoopFailure("cannot analyze loop, irreducible control flow",
260 "HWLoopCannotAnalyze", ORE
, L
);
264 if (!ForceHardwareLoops
&&
265 !TTI
->isHardwareLoopProfitable(L
, *SE
, *AC
, LibInfo
, HWLoopInfo
)) {
266 reportHWLoopFailure("it's not profitable to create a hardware-loop",
267 "HWLoopNotProfitable", ORE
, L
);
271 // Allow overriding of the counter width and loop decrement value.
272 if (CounterBitWidth
.getNumOccurrences())
273 HWLoopInfo
.CountType
=
274 IntegerType::get(M
->getContext(), CounterBitWidth
);
276 if (LoopDecrement
.getNumOccurrences())
277 HWLoopInfo
.LoopDecrement
=
278 ConstantInt::get(HWLoopInfo
.CountType
, LoopDecrement
);
280 MadeChange
|= TryConvertLoop(HWLoopInfo
);
281 return MadeChange
&& (!HWLoopInfo
.IsNestingLegal
&& !ForceNestedLoop
);
284 bool HardwareLoops::TryConvertLoop(HardwareLoopInfo
&HWLoopInfo
) {
286 Loop
*L
= HWLoopInfo
.L
;
287 LLVM_DEBUG(dbgs() << "HWLoops: Try to convert profitable loop: " << *L
);
289 if (!HWLoopInfo
.isHardwareLoopCandidate(*SE
, *LI
, *DT
, ForceNestedLoop
,
290 ForceHardwareLoopPHI
)) {
291 // TODO: there can be many reasons a loop is not considered a
292 // candidate, so we should let isHardwareLoopCandidate fill in the
293 // reason and then report a better message here.
294 reportHWLoopFailure("loop is not a candidate", "HWLoopNoCandidate", ORE
, L
);
299 (HWLoopInfo
.ExitBlock
&& HWLoopInfo
.ExitBranch
&& HWLoopInfo
.TripCount
) &&
300 "Hardware Loop must have set exit info.");
302 BasicBlock
*Preheader
= L
->getLoopPreheader();
304 // If we don't have a preheader, then insert one.
306 Preheader
= InsertPreheaderForLoop(L
, DT
, LI
, nullptr, PreserveLCSSA
);
310 HardwareLoop
HWLoop(HWLoopInfo
, *SE
, *DL
, ORE
);
316 void HardwareLoop::Create() {
317 LLVM_DEBUG(dbgs() << "HWLoops: Converting loop..\n");
319 Value
*LoopCountInit
= InitLoopCount();
320 if (!LoopCountInit
) {
321 reportHWLoopFailure("could not safely create a loop count expression",
322 "HWLoopNotSafe", ORE
, L
);
326 Value
*Setup
= InsertIterationSetup(LoopCountInit
);
328 if (UsePHICounter
|| ForceHardwareLoopPHI
) {
329 Instruction
*LoopDec
= InsertLoopRegDec(LoopCountInit
);
330 Value
*EltsRem
= InsertPHICounter(Setup
, LoopDec
);
331 LoopDec
->setOperand(0, EltsRem
);
332 UpdateBranch(LoopDec
);
336 // Run through the basic blocks of the loop and see if any of them have dead
337 // PHIs that can be removed.
338 for (auto I
: L
->blocks())
342 static bool CanGenerateTest(Loop
*L
, Value
*Count
) {
343 BasicBlock
*Preheader
= L
->getLoopPreheader();
344 if (!Preheader
->getSinglePredecessor())
347 BasicBlock
*Pred
= Preheader
->getSinglePredecessor();
348 if (!isa
<BranchInst
>(Pred
->getTerminator()))
351 auto *BI
= cast
<BranchInst
>(Pred
->getTerminator());
352 if (BI
->isUnconditional() || !isa
<ICmpInst
>(BI
->getCondition()))
355 // Check that the icmp is checking for equality of Count and zero and that
356 // a non-zero value results in entering the loop.
357 auto ICmp
= cast
<ICmpInst
>(BI
->getCondition());
358 LLVM_DEBUG(dbgs() << " - Found condition: " << *ICmp
<< "\n");
359 if (!ICmp
->isEquality())
362 auto IsCompareZero
= [](ICmpInst
*ICmp
, Value
*Count
, unsigned OpIdx
) {
363 if (auto *Const
= dyn_cast
<ConstantInt
>(ICmp
->getOperand(OpIdx
)))
364 return Const
->isZero() && ICmp
->getOperand(OpIdx
^ 1) == Count
;
368 if (!IsCompareZero(ICmp
, Count
, 0) && !IsCompareZero(ICmp
, Count
, 1))
371 unsigned SuccIdx
= ICmp
->getPredicate() == ICmpInst::ICMP_NE
? 0 : 1;
372 if (BI
->getSuccessor(SuccIdx
) != Preheader
)
378 Value
*HardwareLoop::InitLoopCount() {
379 LLVM_DEBUG(dbgs() << "HWLoops: Initialising loop counter value:\n");
380 // Can we replace a conditional branch with an intrinsic that sets the
381 // loop counter and tests that is not zero?
383 SCEVExpander
SCEVE(SE
, DL
, "loopcnt");
385 // If we're trying to use the 'test and set' form of the intrinsic, we need
386 // to replace a conditional branch that is controlling entry to the loop. It
387 // is likely (guaranteed?) that the preheader has an unconditional branch to
388 // the loop header, so also check if it has a single predecessor.
389 if (SE
.isLoopEntryGuardedByCond(L
, ICmpInst::ICMP_NE
, TripCount
,
390 SE
.getZero(TripCount
->getType()))) {
391 LLVM_DEBUG(dbgs() << " - Attempting to use test.set counter.\n");
392 UseLoopGuard
|= ForceGuardLoopEntry
;
394 UseLoopGuard
= false;
396 BasicBlock
*BB
= L
->getLoopPreheader();
397 if (UseLoopGuard
&& BB
->getSinglePredecessor() &&
398 cast
<BranchInst
>(BB
->getTerminator())->isUnconditional()) {
399 BasicBlock
*Predecessor
= BB
->getSinglePredecessor();
400 // If it's not safe to create a while loop then don't force it and create a
401 // do-while loop instead
402 if (!isSafeToExpandAt(TripCount
, Predecessor
->getTerminator(), SE
))
403 UseLoopGuard
= false;
408 if (!isSafeToExpandAt(TripCount
, BB
->getTerminator(), SE
)) {
409 LLVM_DEBUG(dbgs() << "- Bailing, unsafe to expand TripCount " << *TripCount
414 Value
*Count
= SCEVE
.expandCodeFor(TripCount
, CountType
,
415 BB
->getTerminator());
417 // FIXME: We've expanded Count where we hope to insert the counter setting
418 // intrinsic. But, in the case of the 'test and set' form, we may fallback to
419 // the just 'set' form and in which case the insertion block is most likely
420 // different. It means there will be instruction(s) in a block that possibly
421 // aren't needed. The isLoopEntryGuardedByCond is trying to avoid this issue,
422 // but it's doesn't appear to work in all cases.
424 UseLoopGuard
= UseLoopGuard
&& CanGenerateTest(L
, Count
);
425 BeginBB
= UseLoopGuard
? BB
: L
->getLoopPreheader();
426 LLVM_DEBUG(dbgs() << " - Loop Count: " << *Count
<< "\n"
427 << " - Expanded Count in " << BB
->getName() << "\n"
428 << " - Will insert set counter intrinsic into: "
429 << BeginBB
->getName() << "\n");
433 Value
* HardwareLoop::InsertIterationSetup(Value
*LoopCountInit
) {
434 IRBuilder
<> Builder(BeginBB
->getTerminator());
435 Type
*Ty
= LoopCountInit
->getType();
436 bool UsePhi
= UsePHICounter
|| ForceHardwareLoopPHI
;
437 Intrinsic::ID ID
= UseLoopGuard
438 ? (UsePhi
? Intrinsic::test_start_loop_iterations
439 : Intrinsic::test_set_loop_iterations
)
440 : (UsePhi
? Intrinsic::start_loop_iterations
441 : Intrinsic::set_loop_iterations
);
442 Function
*LoopIter
= Intrinsic::getDeclaration(M
, ID
, Ty
);
443 Value
*LoopSetup
= Builder
.CreateCall(LoopIter
, LoopCountInit
);
445 // Use the return value of the intrinsic to control the entry of the loop.
447 assert((isa
<BranchInst
>(BeginBB
->getTerminator()) &&
448 cast
<BranchInst
>(BeginBB
->getTerminator())->isConditional()) &&
449 "Expected conditional branch");
452 UsePhi
? Builder
.CreateExtractValue(LoopSetup
, 1) : LoopSetup
;
453 auto *LoopGuard
= cast
<BranchInst
>(BeginBB
->getTerminator());
454 LoopGuard
->setCondition(SetCount
);
455 if (LoopGuard
->getSuccessor(0) != L
->getLoopPreheader())
456 LoopGuard
->swapSuccessors();
458 LLVM_DEBUG(dbgs() << "HWLoops: Inserted loop counter: " << *LoopSetup
460 if (UsePhi
&& UseLoopGuard
)
461 LoopSetup
= Builder
.CreateExtractValue(LoopSetup
, 0);
462 return !UsePhi
? LoopCountInit
: LoopSetup
;
465 void HardwareLoop::InsertLoopDec() {
466 IRBuilder
<> CondBuilder(ExitBranch
);
469 Intrinsic::getDeclaration(M
, Intrinsic::loop_decrement
,
470 LoopDecrement
->getType());
471 Value
*Ops
[] = { LoopDecrement
};
472 Value
*NewCond
= CondBuilder
.CreateCall(DecFunc
, Ops
);
473 Value
*OldCond
= ExitBranch
->getCondition();
474 ExitBranch
->setCondition(NewCond
);
476 // The false branch must exit the loop.
477 if (!L
->contains(ExitBranch
->getSuccessor(0)))
478 ExitBranch
->swapSuccessors();
480 // The old condition may be dead now, and may have even created a dead PHI
481 // (the original induction variable).
482 RecursivelyDeleteTriviallyDeadInstructions(OldCond
);
484 LLVM_DEBUG(dbgs() << "HWLoops: Inserted loop dec: " << *NewCond
<< "\n");
487 Instruction
* HardwareLoop::InsertLoopRegDec(Value
*EltsRem
) {
488 IRBuilder
<> CondBuilder(ExitBranch
);
491 Intrinsic::getDeclaration(M
, Intrinsic::loop_decrement_reg
,
492 { EltsRem
->getType() });
493 Value
*Ops
[] = { EltsRem
, LoopDecrement
};
494 Value
*Call
= CondBuilder
.CreateCall(DecFunc
, Ops
);
496 LLVM_DEBUG(dbgs() << "HWLoops: Inserted loop dec: " << *Call
<< "\n");
497 return cast
<Instruction
>(Call
);
500 PHINode
* HardwareLoop::InsertPHICounter(Value
*NumElts
, Value
*EltsRem
) {
501 BasicBlock
*Preheader
= L
->getLoopPreheader();
502 BasicBlock
*Header
= L
->getHeader();
503 BasicBlock
*Latch
= ExitBranch
->getParent();
504 IRBuilder
<> Builder(Header
->getFirstNonPHI());
505 PHINode
*Index
= Builder
.CreatePHI(NumElts
->getType(), 2);
506 Index
->addIncoming(NumElts
, Preheader
);
507 Index
->addIncoming(EltsRem
, Latch
);
508 LLVM_DEBUG(dbgs() << "HWLoops: PHI Counter: " << *Index
<< "\n");
512 void HardwareLoop::UpdateBranch(Value
*EltsRem
) {
513 IRBuilder
<> CondBuilder(ExitBranch
);
515 CondBuilder
.CreateICmpNE(EltsRem
, ConstantInt::get(EltsRem
->getType(), 0));
516 Value
*OldCond
= ExitBranch
->getCondition();
517 ExitBranch
->setCondition(NewCond
);
519 // The false branch must exit the loop.
520 if (!L
->contains(ExitBranch
->getSuccessor(0)))
521 ExitBranch
->swapSuccessors();
523 // The old condition may be dead now, and may have even created a dead PHI
524 // (the original induction variable).
525 RecursivelyDeleteTriviallyDeadInstructions(OldCond
);
528 INITIALIZE_PASS_BEGIN(HardwareLoops
, DEBUG_TYPE
, HW_LOOPS_NAME
, false, false)
529 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass
)
530 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass
)
531 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass
)
532 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass
)
533 INITIALIZE_PASS_END(HardwareLoops
, DEBUG_TYPE
, HW_LOOPS_NAME
, false, false)
535 FunctionPass
*llvm::createHardwareLoopsPass() { return new HardwareLoops(); }