[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / lib / CodeGen / HardwareLoops.cpp
blob258ad1931b12bb3acea6027f9708c8f0b8f7dbf8
1 //===-- HardwareLoops.cpp - Target Independent Hardware Loops --*- C++ -*-===//
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 /// \file
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.
15 ///
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/IR/BasicBlock.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/Dominators.h"
29 #include "llvm/IR/IRBuilder.h"
30 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/IntrinsicInst.h"
32 #include "llvm/IR/Value.h"
33 #include "llvm/InitializePasses.h"
34 #include "llvm/Pass.h"
35 #include "llvm/PassRegistry.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Transforms/Utils.h"
39 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
40 #include "llvm/Transforms/Utils/Local.h"
41 #include "llvm/Transforms/Utils/LoopUtils.h"
42 #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
44 #define DEBUG_TYPE "hardware-loops"
46 #define HW_LOOPS_NAME "Hardware Loop Insertion"
48 using namespace llvm;
50 static cl::opt<bool>
51 ForceHardwareLoops("force-hardware-loops", cl::Hidden, cl::init(false),
52 cl::desc("Force hardware loops intrinsics to be inserted"));
54 static cl::opt<bool>
55 ForceHardwareLoopPHI(
56 "force-hardware-loop-phi", cl::Hidden, cl::init(false),
57 cl::desc("Force hardware loop counter to be updated through a phi"));
59 static cl::opt<bool>
60 ForceNestedLoop("force-nested-hardware-loop", cl::Hidden, cl::init(false),
61 cl::desc("Force allowance of nested hardware loops"));
63 static cl::opt<unsigned>
64 LoopDecrement("hardware-loop-decrement", cl::Hidden, cl::init(1),
65 cl::desc("Set the loop decrement value"));
67 static cl::opt<unsigned>
68 CounterBitWidth("hardware-loop-counter-bitwidth", cl::Hidden, cl::init(32),
69 cl::desc("Set the loop counter bitwidth"));
71 static cl::opt<bool>
72 ForceGuardLoopEntry(
73 "force-hardware-loop-guard", cl::Hidden, cl::init(false),
74 cl::desc("Force generation of loop guard intrinsic"));
76 STATISTIC(NumHWLoops, "Number of loops converted to hardware loops");
78 #ifndef NDEBUG
79 static void debugHWLoopFailure(const StringRef DebugMsg,
80 Instruction *I) {
81 dbgs() << "HWLoops: " << DebugMsg;
82 if (I)
83 dbgs() << ' ' << *I;
84 else
85 dbgs() << '.';
86 dbgs() << '\n';
88 #endif
90 static OptimizationRemarkAnalysis
91 createHWLoopAnalysis(StringRef RemarkName, Loop *L, Instruction *I) {
92 Value *CodeRegion = L->getHeader();
93 DebugLoc DL = L->getStartLoc();
95 if (I) {
96 CodeRegion = I->getParent();
97 // If there is no debug location attached to the instruction, revert back to
98 // using the loop's.
99 if (I->getDebugLoc())
100 DL = I->getDebugLoc();
103 OptimizationRemarkAnalysis R(DEBUG_TYPE, RemarkName, DL, CodeRegion);
104 R << "hardware-loop not created: ";
105 return R;
108 namespace {
110 void reportHWLoopFailure(const StringRef Msg, const StringRef ORETag,
111 OptimizationRemarkEmitter *ORE, Loop *TheLoop, Instruction *I = nullptr) {
112 LLVM_DEBUG(debugHWLoopFailure(Msg, I));
113 ORE->emit(createHWLoopAnalysis(ORETag, TheLoop, I) << Msg);
116 using TTI = TargetTransformInfo;
118 class HardwareLoops : public FunctionPass {
119 public:
120 static char ID;
122 HardwareLoops() : FunctionPass(ID) {
123 initializeHardwareLoopsPass(*PassRegistry::getPassRegistry());
126 bool runOnFunction(Function &F) override;
128 void getAnalysisUsage(AnalysisUsage &AU) const override {
129 AU.addRequired<LoopInfoWrapperPass>();
130 AU.addPreserved<LoopInfoWrapperPass>();
131 AU.addRequired<DominatorTreeWrapperPass>();
132 AU.addPreserved<DominatorTreeWrapperPass>();
133 AU.addRequired<ScalarEvolutionWrapperPass>();
134 AU.addRequired<AssumptionCacheTracker>();
135 AU.addRequired<TargetTransformInfoWrapperPass>();
136 AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
139 // Try to convert the given Loop into a hardware loop.
140 bool TryConvertLoop(Loop *L);
142 // Given that the target believes the loop to be profitable, try to
143 // convert it.
144 bool TryConvertLoop(HardwareLoopInfo &HWLoopInfo);
146 private:
147 ScalarEvolution *SE = nullptr;
148 LoopInfo *LI = nullptr;
149 const DataLayout *DL = nullptr;
150 OptimizationRemarkEmitter *ORE = nullptr;
151 const TargetTransformInfo *TTI = nullptr;
152 DominatorTree *DT = nullptr;
153 bool PreserveLCSSA = false;
154 AssumptionCache *AC = nullptr;
155 TargetLibraryInfo *LibInfo = nullptr;
156 Module *M = nullptr;
157 bool MadeChange = false;
160 class HardwareLoop {
161 // Expand the trip count scev into a value that we can use.
162 Value *InitLoopCount();
164 // Insert the set_loop_iteration intrinsic.
165 Value *InsertIterationSetup(Value *LoopCountInit);
167 // Insert the loop_decrement intrinsic.
168 void InsertLoopDec();
170 // Insert the loop_decrement_reg intrinsic.
171 Instruction *InsertLoopRegDec(Value *EltsRem);
173 // If the target requires the counter value to be updated in the loop,
174 // insert a phi to hold the value. The intended purpose is for use by
175 // loop_decrement_reg.
176 PHINode *InsertPHICounter(Value *NumElts, Value *EltsRem);
178 // Create a new cmp, that checks the returned value of loop_decrement*,
179 // and update the exit branch to use it.
180 void UpdateBranch(Value *EltsRem);
182 public:
183 HardwareLoop(HardwareLoopInfo &Info, ScalarEvolution &SE,
184 const DataLayout &DL,
185 OptimizationRemarkEmitter *ORE) :
186 SE(SE), DL(DL), ORE(ORE), L(Info.L), M(L->getHeader()->getModule()),
187 ExitCount(Info.ExitCount),
188 CountType(Info.CountType),
189 ExitBranch(Info.ExitBranch),
190 LoopDecrement(Info.LoopDecrement),
191 UsePHICounter(Info.CounterInReg),
192 UseLoopGuard(Info.PerformEntryTest) { }
194 void Create();
196 private:
197 ScalarEvolution &SE;
198 const DataLayout &DL;
199 OptimizationRemarkEmitter *ORE = nullptr;
200 Loop *L = nullptr;
201 Module *M = nullptr;
202 const SCEV *ExitCount = nullptr;
203 Type *CountType = nullptr;
204 BranchInst *ExitBranch = nullptr;
205 Value *LoopDecrement = nullptr;
206 bool UsePHICounter = false;
207 bool UseLoopGuard = false;
208 BasicBlock *BeginBB = nullptr;
212 char HardwareLoops::ID = 0;
214 bool HardwareLoops::runOnFunction(Function &F) {
215 if (skipFunction(F))
216 return false;
218 LLVM_DEBUG(dbgs() << "HWLoops: Running on " << F.getName() << "\n");
220 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
221 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
222 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
223 TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
224 DL = &F.getParent()->getDataLayout();
225 ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
226 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
227 LibInfo = TLIP ? &TLIP->getTLI(F) : nullptr;
228 PreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
229 AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
230 M = F.getParent();
232 for (Loop *L : *LI)
233 if (L->isOutermost())
234 TryConvertLoop(L);
236 return MadeChange;
239 // Return true if the search should stop, which will be when an inner loop is
240 // converted and the parent loop doesn't support containing a hardware loop.
241 bool HardwareLoops::TryConvertLoop(Loop *L) {
242 // Process nested loops first.
243 bool AnyChanged = false;
244 for (Loop *SL : *L)
245 AnyChanged |= TryConvertLoop(SL);
246 if (AnyChanged) {
247 reportHWLoopFailure("nested hardware-loops not supported", "HWLoopNested",
248 ORE, L);
249 return true; // Stop search.
252 LLVM_DEBUG(dbgs() << "HWLoops: Loop " << L->getHeader()->getName() << "\n");
254 HardwareLoopInfo HWLoopInfo(L);
255 if (!HWLoopInfo.canAnalyze(*LI)) {
256 reportHWLoopFailure("cannot analyze loop, irreducible control flow",
257 "HWLoopCannotAnalyze", ORE, L);
258 return false;
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;
268 // Allow overriding of the counter width and loop decrement value.
269 if (CounterBitWidth.getNumOccurrences())
270 HWLoopInfo.CountType =
271 IntegerType::get(M->getContext(), CounterBitWidth);
273 if (LoopDecrement.getNumOccurrences())
274 HWLoopInfo.LoopDecrement =
275 ConstantInt::get(HWLoopInfo.CountType, LoopDecrement);
277 MadeChange |= TryConvertLoop(HWLoopInfo);
278 return MadeChange && (!HWLoopInfo.IsNestingLegal && !ForceNestedLoop);
281 bool HardwareLoops::TryConvertLoop(HardwareLoopInfo &HWLoopInfo) {
283 Loop *L = HWLoopInfo.L;
284 LLVM_DEBUG(dbgs() << "HWLoops: Try to convert profitable loop: " << *L);
286 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);
292 return false;
295 assert(
296 (HWLoopInfo.ExitBlock && HWLoopInfo.ExitBranch && HWLoopInfo.ExitCount) &&
297 "Hardware Loop must have set exit info.");
299 BasicBlock *Preheader = L->getLoopPreheader();
301 // If we don't have a preheader, then insert one.
302 if (!Preheader)
303 Preheader = InsertPreheaderForLoop(L, DT, LI, nullptr, PreserveLCSSA);
304 if (!Preheader)
305 return false;
307 HardwareLoop HWLoop(HWLoopInfo, *SE, *DL, ORE);
308 HWLoop.Create();
309 ++NumHWLoops;
310 return true;
313 void HardwareLoop::Create() {
314 LLVM_DEBUG(dbgs() << "HWLoops: Converting loop..\n");
316 Value *LoopCountInit = InitLoopCount();
317 if (!LoopCountInit) {
318 reportHWLoopFailure("could not safely create a loop count expression",
319 "HWLoopNotSafe", ORE, L);
320 return;
323 Value *Setup = InsertIterationSetup(LoopCountInit);
325 if (UsePHICounter || ForceHardwareLoopPHI) {
326 Instruction *LoopDec = InsertLoopRegDec(LoopCountInit);
327 Value *EltsRem = InsertPHICounter(Setup, LoopDec);
328 LoopDec->setOperand(0, EltsRem);
329 UpdateBranch(LoopDec);
330 } else
331 InsertLoopDec();
333 // Run through the basic blocks of the loop and see if any of them have dead
334 // PHIs that can be removed.
335 for (auto *I : L->blocks())
336 DeleteDeadPHIs(I);
339 static bool CanGenerateTest(Loop *L, Value *Count) {
340 BasicBlock *Preheader = L->getLoopPreheader();
341 if (!Preheader->getSinglePredecessor())
342 return false;
344 BasicBlock *Pred = Preheader->getSinglePredecessor();
345 if (!isa<BranchInst>(Pred->getTerminator()))
346 return false;
348 auto *BI = cast<BranchInst>(Pred->getTerminator());
349 if (BI->isUnconditional() || !isa<ICmpInst>(BI->getCondition()))
350 return false;
352 // Check that the icmp is checking for equality of Count and zero and that
353 // a non-zero value results in entering the loop.
354 auto ICmp = cast<ICmpInst>(BI->getCondition());
355 LLVM_DEBUG(dbgs() << " - Found condition: " << *ICmp << "\n");
356 if (!ICmp->isEquality())
357 return false;
359 auto IsCompareZero = [](ICmpInst *ICmp, Value *Count, unsigned OpIdx) {
360 if (auto *Const = dyn_cast<ConstantInt>(ICmp->getOperand(OpIdx)))
361 return Const->isZero() && ICmp->getOperand(OpIdx ^ 1) == Count;
362 return false;
365 // Check if Count is a zext.
366 Value *CountBefZext =
367 isa<ZExtInst>(Count) ? cast<ZExtInst>(Count)->getOperand(0) : nullptr;
369 if (!IsCompareZero(ICmp, Count, 0) && !IsCompareZero(ICmp, Count, 1) &&
370 !IsCompareZero(ICmp, CountBefZext, 0) &&
371 !IsCompareZero(ICmp, CountBefZext, 1))
372 return false;
374 unsigned SuccIdx = ICmp->getPredicate() == ICmpInst::ICMP_NE ? 0 : 1;
375 if (BI->getSuccessor(SuccIdx) != Preheader)
376 return false;
378 return true;
381 Value *HardwareLoop::InitLoopCount() {
382 LLVM_DEBUG(dbgs() << "HWLoops: Initialising loop counter value:\n");
383 // Can we replace a conditional branch with an intrinsic that sets the
384 // loop counter and tests that is not zero?
386 SCEVExpander SCEVE(SE, DL, "loopcnt");
387 if (!ExitCount->getType()->isPointerTy() &&
388 ExitCount->getType() != CountType)
389 ExitCount = SE.getZeroExtendExpr(ExitCount, CountType);
391 ExitCount = SE.getAddExpr(ExitCount, SE.getOne(CountType));
393 // If we're trying to use the 'test and set' form of the intrinsic, we need
394 // to replace a conditional branch that is controlling entry to the loop. It
395 // is likely (guaranteed?) that the preheader has an unconditional branch to
396 // the loop header, so also check if it has a single predecessor.
397 if (SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, ExitCount,
398 SE.getZero(ExitCount->getType()))) {
399 LLVM_DEBUG(dbgs() << " - Attempting to use test.set counter.\n");
400 UseLoopGuard |= ForceGuardLoopEntry;
401 } else
402 UseLoopGuard = false;
404 BasicBlock *BB = L->getLoopPreheader();
405 if (UseLoopGuard && BB->getSinglePredecessor() &&
406 cast<BranchInst>(BB->getTerminator())->isUnconditional()) {
407 BasicBlock *Predecessor = BB->getSinglePredecessor();
408 // If it's not safe to create a while loop then don't force it and create a
409 // do-while loop instead
410 if (!SCEVE.isSafeToExpandAt(ExitCount, Predecessor->getTerminator()))
411 UseLoopGuard = false;
412 else
413 BB = Predecessor;
416 if (!SCEVE.isSafeToExpandAt(ExitCount, BB->getTerminator())) {
417 LLVM_DEBUG(dbgs() << "- Bailing, unsafe to expand ExitCount "
418 << *ExitCount << "\n");
419 return nullptr;
422 Value *Count = SCEVE.expandCodeFor(ExitCount, CountType,
423 BB->getTerminator());
425 // FIXME: We've expanded Count where we hope to insert the counter setting
426 // intrinsic. But, in the case of the 'test and set' form, we may fallback to
427 // the just 'set' form and in which case the insertion block is most likely
428 // different. It means there will be instruction(s) in a block that possibly
429 // aren't needed. The isLoopEntryGuardedByCond is trying to avoid this issue,
430 // but it's doesn't appear to work in all cases.
432 UseLoopGuard = UseLoopGuard && CanGenerateTest(L, Count);
433 BeginBB = UseLoopGuard ? BB : L->getLoopPreheader();
434 LLVM_DEBUG(dbgs() << " - Loop Count: " << *Count << "\n"
435 << " - Expanded Count in " << BB->getName() << "\n"
436 << " - Will insert set counter intrinsic into: "
437 << BeginBB->getName() << "\n");
438 return Count;
441 Value* HardwareLoop::InsertIterationSetup(Value *LoopCountInit) {
442 IRBuilder<> Builder(BeginBB->getTerminator());
443 Type *Ty = LoopCountInit->getType();
444 bool UsePhi = UsePHICounter || ForceHardwareLoopPHI;
445 Intrinsic::ID ID = UseLoopGuard
446 ? (UsePhi ? Intrinsic::test_start_loop_iterations
447 : Intrinsic::test_set_loop_iterations)
448 : (UsePhi ? Intrinsic::start_loop_iterations
449 : Intrinsic::set_loop_iterations);
450 Function *LoopIter = Intrinsic::getDeclaration(M, ID, Ty);
451 Value *LoopSetup = Builder.CreateCall(LoopIter, LoopCountInit);
453 // Use the return value of the intrinsic to control the entry of the loop.
454 if (UseLoopGuard) {
455 assert((isa<BranchInst>(BeginBB->getTerminator()) &&
456 cast<BranchInst>(BeginBB->getTerminator())->isConditional()) &&
457 "Expected conditional branch");
459 Value *SetCount =
460 UsePhi ? Builder.CreateExtractValue(LoopSetup, 1) : LoopSetup;
461 auto *LoopGuard = cast<BranchInst>(BeginBB->getTerminator());
462 LoopGuard->setCondition(SetCount);
463 if (LoopGuard->getSuccessor(0) != L->getLoopPreheader())
464 LoopGuard->swapSuccessors();
466 LLVM_DEBUG(dbgs() << "HWLoops: Inserted loop counter: " << *LoopSetup
467 << "\n");
468 if (UsePhi && UseLoopGuard)
469 LoopSetup = Builder.CreateExtractValue(LoopSetup, 0);
470 return !UsePhi ? LoopCountInit : LoopSetup;
473 void HardwareLoop::InsertLoopDec() {
474 IRBuilder<> CondBuilder(ExitBranch);
476 Function *DecFunc =
477 Intrinsic::getDeclaration(M, Intrinsic::loop_decrement,
478 LoopDecrement->getType());
479 Value *Ops[] = { LoopDecrement };
480 Value *NewCond = CondBuilder.CreateCall(DecFunc, Ops);
481 Value *OldCond = ExitBranch->getCondition();
482 ExitBranch->setCondition(NewCond);
484 // The false branch must exit the loop.
485 if (!L->contains(ExitBranch->getSuccessor(0)))
486 ExitBranch->swapSuccessors();
488 // The old condition may be dead now, and may have even created a dead PHI
489 // (the original induction variable).
490 RecursivelyDeleteTriviallyDeadInstructions(OldCond);
492 LLVM_DEBUG(dbgs() << "HWLoops: Inserted loop dec: " << *NewCond << "\n");
495 Instruction* HardwareLoop::InsertLoopRegDec(Value *EltsRem) {
496 IRBuilder<> CondBuilder(ExitBranch);
498 Function *DecFunc =
499 Intrinsic::getDeclaration(M, Intrinsic::loop_decrement_reg,
500 { EltsRem->getType() });
501 Value *Ops[] = { EltsRem, LoopDecrement };
502 Value *Call = CondBuilder.CreateCall(DecFunc, Ops);
504 LLVM_DEBUG(dbgs() << "HWLoops: Inserted loop dec: " << *Call << "\n");
505 return cast<Instruction>(Call);
508 PHINode* HardwareLoop::InsertPHICounter(Value *NumElts, Value *EltsRem) {
509 BasicBlock *Preheader = L->getLoopPreheader();
510 BasicBlock *Header = L->getHeader();
511 BasicBlock *Latch = ExitBranch->getParent();
512 IRBuilder<> Builder(Header->getFirstNonPHI());
513 PHINode *Index = Builder.CreatePHI(NumElts->getType(), 2);
514 Index->addIncoming(NumElts, Preheader);
515 Index->addIncoming(EltsRem, Latch);
516 LLVM_DEBUG(dbgs() << "HWLoops: PHI Counter: " << *Index << "\n");
517 return Index;
520 void HardwareLoop::UpdateBranch(Value *EltsRem) {
521 IRBuilder<> CondBuilder(ExitBranch);
522 Value *NewCond =
523 CondBuilder.CreateICmpNE(EltsRem, ConstantInt::get(EltsRem->getType(), 0));
524 Value *OldCond = ExitBranch->getCondition();
525 ExitBranch->setCondition(NewCond);
527 // The false branch must exit the loop.
528 if (!L->contains(ExitBranch->getSuccessor(0)))
529 ExitBranch->swapSuccessors();
531 // The old condition may be dead now, and may have even created a dead PHI
532 // (the original induction variable).
533 RecursivelyDeleteTriviallyDeadInstructions(OldCond);
536 INITIALIZE_PASS_BEGIN(HardwareLoops, DEBUG_TYPE, HW_LOOPS_NAME, false, false)
537 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
538 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
539 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
540 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
541 INITIALIZE_PASS_END(HardwareLoops, DEBUG_TYPE, HW_LOOPS_NAME, false, false)
543 FunctionPass *llvm::createHardwareLoopsPass() { return new HardwareLoops(); }