10b57cec5SDimitry Andric //===-- HardwareLoops.cpp - Target Independent Hardware Loops --*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric /// \file 90b57cec5SDimitry Andric /// Insert hardware loop intrinsics into loops which are deemed profitable by 100b57cec5SDimitry Andric /// the target, by querying TargetTransformInfo. A hardware loop comprises of 110b57cec5SDimitry Andric /// two intrinsics: one, outside the loop, to set the loop iteration count and 120b57cec5SDimitry Andric /// another, in the exit block, to decrement the counter. The decremented value 130b57cec5SDimitry Andric /// can either be carried through the loop via a phi or handled in some opaque 140b57cec5SDimitry Andric /// way by the target. 150b57cec5SDimitry Andric /// 160b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 170b57cec5SDimitry Andric 1806c3fb27SDimitry Andric #include "llvm/CodeGen/HardwareLoops.h" 190b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 200b57cec5SDimitry Andric #include "llvm/Analysis/AssumptionCache.h" 2106c3fb27SDimitry Andric #include "llvm/Analysis/BranchProbabilityInfo.h" 220b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h" 23480093f4SDimitry Andric #include "llvm/Analysis/OptimizationRemarkEmitter.h" 240b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolution.h" 255ffd83dbSDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h" 260b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h" 270b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h" 280b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 29480093f4SDimitry Andric #include "llvm/IR/Constants.h" 300b57cec5SDimitry Andric #include "llvm/IR/Dominators.h" 310b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h" 320b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 330b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 340b57cec5SDimitry Andric #include "llvm/IR/Value.h" 35480093f4SDimitry Andric #include "llvm/InitializePasses.h" 36480093f4SDimitry Andric #include "llvm/Pass.h" 37480093f4SDimitry Andric #include "llvm/PassRegistry.h" 38480093f4SDimitry Andric #include "llvm/Support/CommandLine.h" 390b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 400b57cec5SDimitry Andric #include "llvm/Transforms/Utils.h" 410b57cec5SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h" 420b57cec5SDimitry Andric #include "llvm/Transforms/Utils/Local.h" 430b57cec5SDimitry Andric #include "llvm/Transforms/Utils/LoopUtils.h" 445ffd83dbSDimitry Andric #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h" 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric #define DEBUG_TYPE "hardware-loops" 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric #define HW_LOOPS_NAME "Hardware Loop Insertion" 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric using namespace llvm; 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric static cl::opt<bool> 530b57cec5SDimitry Andric ForceHardwareLoops("force-hardware-loops", cl::Hidden, cl::init(false), 540b57cec5SDimitry Andric cl::desc("Force hardware loops intrinsics to be inserted")); 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric static cl::opt<bool> 570b57cec5SDimitry Andric ForceHardwareLoopPHI( 580b57cec5SDimitry Andric "force-hardware-loop-phi", cl::Hidden, cl::init(false), 590b57cec5SDimitry Andric cl::desc("Force hardware loop counter to be updated through a phi")); 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric static cl::opt<bool> 620b57cec5SDimitry Andric ForceNestedLoop("force-nested-hardware-loop", cl::Hidden, cl::init(false), 630b57cec5SDimitry Andric cl::desc("Force allowance of nested hardware loops")); 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric static cl::opt<unsigned> 660b57cec5SDimitry Andric LoopDecrement("hardware-loop-decrement", cl::Hidden, cl::init(1), 670b57cec5SDimitry Andric cl::desc("Set the loop decrement value")); 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric static cl::opt<unsigned> 700b57cec5SDimitry Andric CounterBitWidth("hardware-loop-counter-bitwidth", cl::Hidden, cl::init(32), 710b57cec5SDimitry Andric cl::desc("Set the loop counter bitwidth")); 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric static cl::opt<bool> 740b57cec5SDimitry Andric ForceGuardLoopEntry( 750b57cec5SDimitry Andric "force-hardware-loop-guard", cl::Hidden, cl::init(false), 760b57cec5SDimitry Andric cl::desc("Force generation of loop guard intrinsic")); 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric STATISTIC(NumHWLoops, "Number of loops converted to hardware loops"); 790b57cec5SDimitry Andric 80480093f4SDimitry Andric #ifndef NDEBUG 81480093f4SDimitry Andric static void debugHWLoopFailure(const StringRef DebugMsg, 82480093f4SDimitry Andric Instruction *I) { 83480093f4SDimitry Andric dbgs() << "HWLoops: " << DebugMsg; 84480093f4SDimitry Andric if (I) 85480093f4SDimitry Andric dbgs() << ' ' << *I; 86480093f4SDimitry Andric else 87480093f4SDimitry Andric dbgs() << '.'; 88480093f4SDimitry Andric dbgs() << '\n'; 89480093f4SDimitry Andric } 90480093f4SDimitry Andric #endif 91480093f4SDimitry Andric 92480093f4SDimitry Andric static OptimizationRemarkAnalysis 93480093f4SDimitry Andric createHWLoopAnalysis(StringRef RemarkName, Loop *L, Instruction *I) { 94480093f4SDimitry Andric Value *CodeRegion = L->getHeader(); 95480093f4SDimitry Andric DebugLoc DL = L->getStartLoc(); 96480093f4SDimitry Andric 97480093f4SDimitry Andric if (I) { 98480093f4SDimitry Andric CodeRegion = I->getParent(); 99480093f4SDimitry Andric // If there is no debug location attached to the instruction, revert back to 100480093f4SDimitry Andric // using the loop's. 101480093f4SDimitry Andric if (I->getDebugLoc()) 102480093f4SDimitry Andric DL = I->getDebugLoc(); 103480093f4SDimitry Andric } 104480093f4SDimitry Andric 105480093f4SDimitry Andric OptimizationRemarkAnalysis R(DEBUG_TYPE, RemarkName, DL, CodeRegion); 106480093f4SDimitry Andric R << "hardware-loop not created: "; 107480093f4SDimitry Andric return R; 108480093f4SDimitry Andric } 109480093f4SDimitry Andric 1100b57cec5SDimitry Andric namespace { 1110b57cec5SDimitry Andric 112480093f4SDimitry Andric void reportHWLoopFailure(const StringRef Msg, const StringRef ORETag, 113480093f4SDimitry Andric OptimizationRemarkEmitter *ORE, Loop *TheLoop, Instruction *I = nullptr) { 114480093f4SDimitry Andric LLVM_DEBUG(debugHWLoopFailure(Msg, I)); 115480093f4SDimitry Andric ORE->emit(createHWLoopAnalysis(ORETag, TheLoop, I) << Msg); 116480093f4SDimitry Andric } 117480093f4SDimitry Andric 1180b57cec5SDimitry Andric using TTI = TargetTransformInfo; 1190b57cec5SDimitry Andric 12006c3fb27SDimitry Andric class HardwareLoopsLegacy : public FunctionPass { 1210b57cec5SDimitry Andric public: 1220b57cec5SDimitry Andric static char ID; 1230b57cec5SDimitry Andric 12406c3fb27SDimitry Andric HardwareLoopsLegacy() : FunctionPass(ID) { 12506c3fb27SDimitry Andric initializeHardwareLoopsLegacyPass(*PassRegistry::getPassRegistry()); 1260b57cec5SDimitry Andric } 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric bool runOnFunction(Function &F) override; 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 1310b57cec5SDimitry Andric AU.addRequired<LoopInfoWrapperPass>(); 1320b57cec5SDimitry Andric AU.addPreserved<LoopInfoWrapperPass>(); 1330b57cec5SDimitry Andric AU.addRequired<DominatorTreeWrapperPass>(); 1340b57cec5SDimitry Andric AU.addPreserved<DominatorTreeWrapperPass>(); 1350b57cec5SDimitry Andric AU.addRequired<ScalarEvolutionWrapperPass>(); 13606c3fb27SDimitry Andric AU.addPreserved<ScalarEvolutionWrapperPass>(); 1370b57cec5SDimitry Andric AU.addRequired<AssumptionCacheTracker>(); 1380b57cec5SDimitry Andric AU.addRequired<TargetTransformInfoWrapperPass>(); 139480093f4SDimitry Andric AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); 14006c3fb27SDimitry Andric AU.addPreserved<BranchProbabilityInfoWrapperPass>(); 1410b57cec5SDimitry Andric } 14206c3fb27SDimitry Andric }; 1430b57cec5SDimitry Andric 14406c3fb27SDimitry Andric class HardwareLoopsImpl { 14506c3fb27SDimitry Andric public: 14606c3fb27SDimitry Andric HardwareLoopsImpl(ScalarEvolution &SE, LoopInfo &LI, bool PreserveLCSSA, 14706c3fb27SDimitry Andric DominatorTree &DT, const DataLayout &DL, 14806c3fb27SDimitry Andric const TargetTransformInfo &TTI, TargetLibraryInfo *TLI, 14906c3fb27SDimitry Andric AssumptionCache &AC, OptimizationRemarkEmitter *ORE, 15006c3fb27SDimitry Andric HardwareLoopOptions &Opts) 15106c3fb27SDimitry Andric : SE(SE), LI(LI), PreserveLCSSA(PreserveLCSSA), DT(DT), DL(DL), TTI(TTI), 15206c3fb27SDimitry Andric TLI(TLI), AC(AC), ORE(ORE), Opts(Opts) { } 15306c3fb27SDimitry Andric 15406c3fb27SDimitry Andric bool run(Function &F); 15506c3fb27SDimitry Andric 15606c3fb27SDimitry Andric private: 1570b57cec5SDimitry Andric // Try to convert the given Loop into a hardware loop. 15806c3fb27SDimitry Andric bool TryConvertLoop(Loop *L, LLVMContext &Ctx); 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric // Given that the target believes the loop to be profitable, try to 1610b57cec5SDimitry Andric // convert it. 1620b57cec5SDimitry Andric bool TryConvertLoop(HardwareLoopInfo &HWLoopInfo); 1630b57cec5SDimitry Andric 16406c3fb27SDimitry Andric ScalarEvolution &SE; 16506c3fb27SDimitry Andric LoopInfo &LI; 16606c3fb27SDimitry Andric bool PreserveLCSSA; 16706c3fb27SDimitry Andric DominatorTree &DT; 16806c3fb27SDimitry Andric const DataLayout &DL; 16906c3fb27SDimitry Andric const TargetTransformInfo &TTI; 17006c3fb27SDimitry Andric TargetLibraryInfo *TLI = nullptr; 17106c3fb27SDimitry Andric AssumptionCache &AC; 17206c3fb27SDimitry Andric OptimizationRemarkEmitter *ORE; 17306c3fb27SDimitry Andric HardwareLoopOptions &Opts; 1740b57cec5SDimitry Andric bool MadeChange = false; 1750b57cec5SDimitry Andric }; 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric class HardwareLoop { 1780b57cec5SDimitry Andric // Expand the trip count scev into a value that we can use. 1790b57cec5SDimitry Andric Value *InitLoopCount(); 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric // Insert the set_loop_iteration intrinsic. 182e8d8bef9SDimitry Andric Value *InsertIterationSetup(Value *LoopCountInit); 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric // Insert the loop_decrement intrinsic. 1850b57cec5SDimitry Andric void InsertLoopDec(); 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric // Insert the loop_decrement_reg intrinsic. 1880b57cec5SDimitry Andric Instruction *InsertLoopRegDec(Value *EltsRem); 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric // If the target requires the counter value to be updated in the loop, 1910b57cec5SDimitry Andric // insert a phi to hold the value. The intended purpose is for use by 1920b57cec5SDimitry Andric // loop_decrement_reg. 1930b57cec5SDimitry Andric PHINode *InsertPHICounter(Value *NumElts, Value *EltsRem); 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric // Create a new cmp, that checks the returned value of loop_decrement*, 1960b57cec5SDimitry Andric // and update the exit branch to use it. 1970b57cec5SDimitry Andric void UpdateBranch(Value *EltsRem); 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric public: 2000b57cec5SDimitry Andric HardwareLoop(HardwareLoopInfo &Info, ScalarEvolution &SE, 201480093f4SDimitry Andric const DataLayout &DL, 20206c3fb27SDimitry Andric OptimizationRemarkEmitter *ORE, 20306c3fb27SDimitry Andric HardwareLoopOptions &Opts) : 20406c3fb27SDimitry Andric SE(SE), DL(DL), ORE(ORE), Opts(Opts), L(Info.L), M(L->getHeader()->getModule()), 205f21fcae4SAlfredo Dal'Ava Junior ExitCount(Info.ExitCount), 2060b57cec5SDimitry Andric CountType(Info.CountType), 2070b57cec5SDimitry Andric ExitBranch(Info.ExitBranch), 2080b57cec5SDimitry Andric LoopDecrement(Info.LoopDecrement), 2090b57cec5SDimitry Andric UsePHICounter(Info.CounterInReg), 2100b57cec5SDimitry Andric UseLoopGuard(Info.PerformEntryTest) { } 2110b57cec5SDimitry Andric 2120b57cec5SDimitry Andric void Create(); 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric private: 2150b57cec5SDimitry Andric ScalarEvolution &SE; 2160b57cec5SDimitry Andric const DataLayout &DL; 217480093f4SDimitry Andric OptimizationRemarkEmitter *ORE = nullptr; 21806c3fb27SDimitry Andric HardwareLoopOptions &Opts; 2190b57cec5SDimitry Andric Loop *L = nullptr; 2200b57cec5SDimitry Andric Module *M = nullptr; 221f21fcae4SAlfredo Dal'Ava Junior const SCEV *ExitCount = nullptr; 2220b57cec5SDimitry Andric Type *CountType = nullptr; 2230b57cec5SDimitry Andric BranchInst *ExitBranch = nullptr; 2240b57cec5SDimitry Andric Value *LoopDecrement = nullptr; 2250b57cec5SDimitry Andric bool UsePHICounter = false; 2260b57cec5SDimitry Andric bool UseLoopGuard = false; 2270b57cec5SDimitry Andric BasicBlock *BeginBB = nullptr; 2280b57cec5SDimitry Andric }; 2290b57cec5SDimitry Andric } 2300b57cec5SDimitry Andric 23106c3fb27SDimitry Andric char HardwareLoopsLegacy::ID = 0; 2320b57cec5SDimitry Andric 23306c3fb27SDimitry Andric bool HardwareLoopsLegacy::runOnFunction(Function &F) { 2340b57cec5SDimitry Andric if (skipFunction(F)) 2350b57cec5SDimitry Andric return false; 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "HWLoops: Running on " << F.getName() << "\n"); 2380b57cec5SDimitry Andric 23906c3fb27SDimitry Andric auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 24006c3fb27SDimitry Andric auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); 24106c3fb27SDimitry Andric auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 24206c3fb27SDimitry Andric auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); 243*0fca6ea1SDimitry Andric auto &DL = F.getDataLayout(); 24406c3fb27SDimitry Andric auto *ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); 2450b57cec5SDimitry Andric auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>(); 24606c3fb27SDimitry Andric auto *TLI = TLIP ? &TLIP->getTLI(F) : nullptr; 24706c3fb27SDimitry Andric auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); 24806c3fb27SDimitry Andric bool PreserveLCSSA = mustPreserveAnalysisID(LCSSAID); 2490b57cec5SDimitry Andric 25006c3fb27SDimitry Andric HardwareLoopOptions Opts; 25106c3fb27SDimitry Andric if (ForceHardwareLoops.getNumOccurrences()) 25206c3fb27SDimitry Andric Opts.setForce(ForceHardwareLoops); 25306c3fb27SDimitry Andric if (ForceHardwareLoopPHI.getNumOccurrences()) 25406c3fb27SDimitry Andric Opts.setForcePhi(ForceHardwareLoopPHI); 25506c3fb27SDimitry Andric if (ForceNestedLoop.getNumOccurrences()) 25606c3fb27SDimitry Andric Opts.setForceNested(ForceNestedLoop); 25706c3fb27SDimitry Andric if (ForceGuardLoopEntry.getNumOccurrences()) 25806c3fb27SDimitry Andric Opts.setForceGuard(ForceGuardLoopEntry); 25906c3fb27SDimitry Andric if (LoopDecrement.getNumOccurrences()) 26006c3fb27SDimitry Andric Opts.setDecrement(LoopDecrement); 26106c3fb27SDimitry Andric if (CounterBitWidth.getNumOccurrences()) 26206c3fb27SDimitry Andric Opts.setCounterBitwidth(CounterBitWidth); 26306c3fb27SDimitry Andric 26406c3fb27SDimitry Andric HardwareLoopsImpl Impl(SE, LI, PreserveLCSSA, DT, DL, TTI, TLI, AC, ORE, 26506c3fb27SDimitry Andric Opts); 26606c3fb27SDimitry Andric return Impl.run(F); 26706c3fb27SDimitry Andric } 26806c3fb27SDimitry Andric 26906c3fb27SDimitry Andric PreservedAnalyses HardwareLoopsPass::run(Function &F, 27006c3fb27SDimitry Andric FunctionAnalysisManager &AM) { 27106c3fb27SDimitry Andric auto &LI = AM.getResult<LoopAnalysis>(F); 27206c3fb27SDimitry Andric auto &SE = AM.getResult<ScalarEvolutionAnalysis>(F); 27306c3fb27SDimitry Andric auto &DT = AM.getResult<DominatorTreeAnalysis>(F); 27406c3fb27SDimitry Andric auto &TTI = AM.getResult<TargetIRAnalysis>(F); 27506c3fb27SDimitry Andric auto *TLI = &AM.getResult<TargetLibraryAnalysis>(F); 27606c3fb27SDimitry Andric auto &AC = AM.getResult<AssumptionAnalysis>(F); 27706c3fb27SDimitry Andric auto *ORE = &AM.getResult<OptimizationRemarkEmitterAnalysis>(F); 278*0fca6ea1SDimitry Andric auto &DL = F.getDataLayout(); 27906c3fb27SDimitry Andric 28006c3fb27SDimitry Andric HardwareLoopsImpl Impl(SE, LI, true, DT, DL, TTI, TLI, AC, ORE, Opts); 28106c3fb27SDimitry Andric bool Changed = Impl.run(F); 28206c3fb27SDimitry Andric if (!Changed) 28306c3fb27SDimitry Andric return PreservedAnalyses::all(); 28406c3fb27SDimitry Andric 28506c3fb27SDimitry Andric PreservedAnalyses PA; 28606c3fb27SDimitry Andric PA.preserve<LoopAnalysis>(); 28706c3fb27SDimitry Andric PA.preserve<ScalarEvolutionAnalysis>(); 28806c3fb27SDimitry Andric PA.preserve<DominatorTreeAnalysis>(); 28906c3fb27SDimitry Andric PA.preserve<BranchProbabilityAnalysis>(); 29006c3fb27SDimitry Andric return PA; 29106c3fb27SDimitry Andric } 29206c3fb27SDimitry Andric 29306c3fb27SDimitry Andric bool HardwareLoopsImpl::run(Function &F) { 294*0fca6ea1SDimitry Andric LLVMContext &Ctx = F.getContext(); 29506c3fb27SDimitry Andric for (Loop *L : LI) 296e8d8bef9SDimitry Andric if (L->isOutermost()) 29706c3fb27SDimitry Andric TryConvertLoop(L, Ctx); 2980b57cec5SDimitry Andric return MadeChange; 2990b57cec5SDimitry Andric } 3000b57cec5SDimitry Andric 3010b57cec5SDimitry Andric // Return true if the search should stop, which will be when an inner loop is 3020b57cec5SDimitry Andric // converted and the parent loop doesn't support containing a hardware loop. 30306c3fb27SDimitry Andric bool HardwareLoopsImpl::TryConvertLoop(Loop *L, LLVMContext &Ctx) { 3040b57cec5SDimitry Andric // Process nested loops first. 3055ffd83dbSDimitry Andric bool AnyChanged = false; 3065ffd83dbSDimitry Andric for (Loop *SL : *L) 30706c3fb27SDimitry Andric AnyChanged |= TryConvertLoop(SL, Ctx); 3085ffd83dbSDimitry Andric if (AnyChanged) { 309480093f4SDimitry Andric reportHWLoopFailure("nested hardware-loops not supported", "HWLoopNested", 310480093f4SDimitry Andric ORE, L); 3110b57cec5SDimitry Andric return true; // Stop search. 312480093f4SDimitry Andric } 3135ffd83dbSDimitry Andric 3145ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "HWLoops: Loop " << L->getHeader()->getName() << "\n"); 3150b57cec5SDimitry Andric 3160b57cec5SDimitry Andric HardwareLoopInfo HWLoopInfo(L); 31706c3fb27SDimitry Andric if (!HWLoopInfo.canAnalyze(LI)) { 318480093f4SDimitry Andric reportHWLoopFailure("cannot analyze loop, irreducible control flow", 319480093f4SDimitry Andric "HWLoopCannotAnalyze", ORE, L); 3200b57cec5SDimitry Andric return false; 321480093f4SDimitry Andric } 3220b57cec5SDimitry Andric 32306c3fb27SDimitry Andric if (!Opts.Force && 32406c3fb27SDimitry Andric !TTI.isHardwareLoopProfitable(L, SE, AC, TLI, HWLoopInfo)) { 325480093f4SDimitry Andric reportHWLoopFailure("it's not profitable to create a hardware-loop", 326480093f4SDimitry Andric "HWLoopNotProfitable", ORE, L); 327480093f4SDimitry Andric return false; 328480093f4SDimitry Andric } 3290b57cec5SDimitry Andric 3300b57cec5SDimitry Andric // Allow overriding of the counter width and loop decrement value. 33106c3fb27SDimitry Andric if (Opts.Bitwidth.has_value()) { 33206c3fb27SDimitry Andric HWLoopInfo.CountType = IntegerType::get(Ctx, Opts.Bitwidth.value()); 3330b57cec5SDimitry Andric } 3340b57cec5SDimitry Andric 33506c3fb27SDimitry Andric if (Opts.Decrement.has_value()) 33606c3fb27SDimitry Andric HWLoopInfo.LoopDecrement = 33706c3fb27SDimitry Andric ConstantInt::get(HWLoopInfo.CountType, Opts.Decrement.value()); 33806c3fb27SDimitry Andric 33906c3fb27SDimitry Andric MadeChange |= TryConvertLoop(HWLoopInfo); 34006c3fb27SDimitry Andric return MadeChange && (!HWLoopInfo.IsNestingLegal && !Opts.ForceNested); 34106c3fb27SDimitry Andric } 34206c3fb27SDimitry Andric 34306c3fb27SDimitry Andric bool HardwareLoopsImpl::TryConvertLoop(HardwareLoopInfo &HWLoopInfo) { 3440b57cec5SDimitry Andric 3450b57cec5SDimitry Andric Loop *L = HWLoopInfo.L; 3460b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "HWLoops: Try to convert profitable loop: " << *L); 3470b57cec5SDimitry Andric 34806c3fb27SDimitry Andric if (!HWLoopInfo.isHardwareLoopCandidate(SE, LI, DT, Opts.getForceNested(), 34906c3fb27SDimitry Andric Opts.getForcePhi())) { 350480093f4SDimitry Andric // TODO: there can be many reasons a loop is not considered a 351480093f4SDimitry Andric // candidate, so we should let isHardwareLoopCandidate fill in the 352480093f4SDimitry Andric // reason and then report a better message here. 353480093f4SDimitry Andric reportHWLoopFailure("loop is not a candidate", "HWLoopNoCandidate", ORE, L); 3540b57cec5SDimitry Andric return false; 355480093f4SDimitry Andric } 3560b57cec5SDimitry Andric 3570b57cec5SDimitry Andric assert( 358f21fcae4SAlfredo Dal'Ava Junior (HWLoopInfo.ExitBlock && HWLoopInfo.ExitBranch && HWLoopInfo.ExitCount) && 3590b57cec5SDimitry Andric "Hardware Loop must have set exit info."); 3600b57cec5SDimitry Andric 3610b57cec5SDimitry Andric BasicBlock *Preheader = L->getLoopPreheader(); 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric // If we don't have a preheader, then insert one. 3640b57cec5SDimitry Andric if (!Preheader) 36506c3fb27SDimitry Andric Preheader = InsertPreheaderForLoop(L, &DT, &LI, nullptr, PreserveLCSSA); 3660b57cec5SDimitry Andric if (!Preheader) 3670b57cec5SDimitry Andric return false; 3680b57cec5SDimitry Andric 36906c3fb27SDimitry Andric HardwareLoop HWLoop(HWLoopInfo, SE, DL, ORE, Opts); 3700b57cec5SDimitry Andric HWLoop.Create(); 3710b57cec5SDimitry Andric ++NumHWLoops; 3720b57cec5SDimitry Andric return true; 3730b57cec5SDimitry Andric } 3740b57cec5SDimitry Andric 3750b57cec5SDimitry Andric void HardwareLoop::Create() { 3760b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "HWLoops: Converting loop..\n"); 3770b57cec5SDimitry Andric 3780b57cec5SDimitry Andric Value *LoopCountInit = InitLoopCount(); 379480093f4SDimitry Andric if (!LoopCountInit) { 380480093f4SDimitry Andric reportHWLoopFailure("could not safely create a loop count expression", 381480093f4SDimitry Andric "HWLoopNotSafe", ORE, L); 3820b57cec5SDimitry Andric return; 383480093f4SDimitry Andric } 3840b57cec5SDimitry Andric 385e8d8bef9SDimitry Andric Value *Setup = InsertIterationSetup(LoopCountInit); 3860b57cec5SDimitry Andric 38706c3fb27SDimitry Andric if (UsePHICounter || Opts.ForcePhi) { 3880b57cec5SDimitry Andric Instruction *LoopDec = InsertLoopRegDec(LoopCountInit); 389e8d8bef9SDimitry Andric Value *EltsRem = InsertPHICounter(Setup, LoopDec); 3900b57cec5SDimitry Andric LoopDec->setOperand(0, EltsRem); 3910b57cec5SDimitry Andric UpdateBranch(LoopDec); 3920b57cec5SDimitry Andric } else 3930b57cec5SDimitry Andric InsertLoopDec(); 3940b57cec5SDimitry Andric 3950b57cec5SDimitry Andric // Run through the basic blocks of the loop and see if any of them have dead 3960b57cec5SDimitry Andric // PHIs that can be removed. 397fcaf7f86SDimitry Andric for (auto *I : L->blocks()) 3980b57cec5SDimitry Andric DeleteDeadPHIs(I); 3990b57cec5SDimitry Andric } 4000b57cec5SDimitry Andric 4010b57cec5SDimitry Andric static bool CanGenerateTest(Loop *L, Value *Count) { 4020b57cec5SDimitry Andric BasicBlock *Preheader = L->getLoopPreheader(); 4030b57cec5SDimitry Andric if (!Preheader->getSinglePredecessor()) 4040b57cec5SDimitry Andric return false; 4050b57cec5SDimitry Andric 4060b57cec5SDimitry Andric BasicBlock *Pred = Preheader->getSinglePredecessor(); 4070b57cec5SDimitry Andric if (!isa<BranchInst>(Pred->getTerminator())) 4080b57cec5SDimitry Andric return false; 4090b57cec5SDimitry Andric 4100b57cec5SDimitry Andric auto *BI = cast<BranchInst>(Pred->getTerminator()); 4110b57cec5SDimitry Andric if (BI->isUnconditional() || !isa<ICmpInst>(BI->getCondition())) 4120b57cec5SDimitry Andric return false; 4130b57cec5SDimitry Andric 4140b57cec5SDimitry Andric // Check that the icmp is checking for equality of Count and zero and that 4150b57cec5SDimitry Andric // a non-zero value results in entering the loop. 4160b57cec5SDimitry Andric auto ICmp = cast<ICmpInst>(BI->getCondition()); 4170b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - Found condition: " << *ICmp << "\n"); 4180b57cec5SDimitry Andric if (!ICmp->isEquality()) 4190b57cec5SDimitry Andric return false; 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andric auto IsCompareZero = [](ICmpInst *ICmp, Value *Count, unsigned OpIdx) { 4220b57cec5SDimitry Andric if (auto *Const = dyn_cast<ConstantInt>(ICmp->getOperand(OpIdx))) 4230b57cec5SDimitry Andric return Const->isZero() && ICmp->getOperand(OpIdx ^ 1) == Count; 4240b57cec5SDimitry Andric return false; 4250b57cec5SDimitry Andric }; 4260b57cec5SDimitry Andric 427349cc55cSDimitry Andric // Check if Count is a zext. 428349cc55cSDimitry Andric Value *CountBefZext = 429349cc55cSDimitry Andric isa<ZExtInst>(Count) ? cast<ZExtInst>(Count)->getOperand(0) : nullptr; 430349cc55cSDimitry Andric 431349cc55cSDimitry Andric if (!IsCompareZero(ICmp, Count, 0) && !IsCompareZero(ICmp, Count, 1) && 432349cc55cSDimitry Andric !IsCompareZero(ICmp, CountBefZext, 0) && 433349cc55cSDimitry Andric !IsCompareZero(ICmp, CountBefZext, 1)) 4340b57cec5SDimitry Andric return false; 4350b57cec5SDimitry Andric 4360b57cec5SDimitry Andric unsigned SuccIdx = ICmp->getPredicate() == ICmpInst::ICMP_NE ? 0 : 1; 4370b57cec5SDimitry Andric if (BI->getSuccessor(SuccIdx) != Preheader) 4380b57cec5SDimitry Andric return false; 4390b57cec5SDimitry Andric 4400b57cec5SDimitry Andric return true; 4410b57cec5SDimitry Andric } 4420b57cec5SDimitry Andric 4430b57cec5SDimitry Andric Value *HardwareLoop::InitLoopCount() { 4440b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "HWLoops: Initialising loop counter value:\n"); 4450b57cec5SDimitry Andric // Can we replace a conditional branch with an intrinsic that sets the 4460b57cec5SDimitry Andric // loop counter and tests that is not zero? 4470b57cec5SDimitry Andric 4480b57cec5SDimitry Andric SCEVExpander SCEVE(SE, DL, "loopcnt"); 449f21fcae4SAlfredo Dal'Ava Junior if (!ExitCount->getType()->isPointerTy() && 450f21fcae4SAlfredo Dal'Ava Junior ExitCount->getType() != CountType) 451f21fcae4SAlfredo Dal'Ava Junior ExitCount = SE.getZeroExtendExpr(ExitCount, CountType); 452f21fcae4SAlfredo Dal'Ava Junior 453f21fcae4SAlfredo Dal'Ava Junior ExitCount = SE.getAddExpr(ExitCount, SE.getOne(CountType)); 454f21fcae4SAlfredo Dal'Ava Junior 4550b57cec5SDimitry Andric // If we're trying to use the 'test and set' form of the intrinsic, we need 4560b57cec5SDimitry Andric // to replace a conditional branch that is controlling entry to the loop. It 4570b57cec5SDimitry Andric // is likely (guaranteed?) that the preheader has an unconditional branch to 4580b57cec5SDimitry Andric // the loop header, so also check if it has a single predecessor. 459f21fcae4SAlfredo Dal'Ava Junior if (SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, ExitCount, 460f21fcae4SAlfredo Dal'Ava Junior SE.getZero(ExitCount->getType()))) { 4610b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - Attempting to use test.set counter.\n"); 46206c3fb27SDimitry Andric if (Opts.ForceGuard) 46306c3fb27SDimitry Andric UseLoopGuard = true; 4640b57cec5SDimitry Andric } else 4650b57cec5SDimitry Andric UseLoopGuard = false; 4660b57cec5SDimitry Andric 4670b57cec5SDimitry Andric BasicBlock *BB = L->getLoopPreheader(); 4680b57cec5SDimitry Andric if (UseLoopGuard && BB->getSinglePredecessor() && 469e8d8bef9SDimitry Andric cast<BranchInst>(BB->getTerminator())->isUnconditional()) { 470e8d8bef9SDimitry Andric BasicBlock *Predecessor = BB->getSinglePredecessor(); 471e8d8bef9SDimitry Andric // If it's not safe to create a while loop then don't force it and create a 472e8d8bef9SDimitry Andric // do-while loop instead 473fcaf7f86SDimitry Andric if (!SCEVE.isSafeToExpandAt(ExitCount, Predecessor->getTerminator())) 474e8d8bef9SDimitry Andric UseLoopGuard = false; 475e8d8bef9SDimitry Andric else 476e8d8bef9SDimitry Andric BB = Predecessor; 477e8d8bef9SDimitry Andric } 4780b57cec5SDimitry Andric 479fcaf7f86SDimitry Andric if (!SCEVE.isSafeToExpandAt(ExitCount, BB->getTerminator())) { 480f21fcae4SAlfredo Dal'Ava Junior LLVM_DEBUG(dbgs() << "- Bailing, unsafe to expand ExitCount " 481f21fcae4SAlfredo Dal'Ava Junior << *ExitCount << "\n"); 4820b57cec5SDimitry Andric return nullptr; 4830b57cec5SDimitry Andric } 4840b57cec5SDimitry Andric 485f21fcae4SAlfredo Dal'Ava Junior Value *Count = SCEVE.expandCodeFor(ExitCount, CountType, 4860b57cec5SDimitry Andric BB->getTerminator()); 4870b57cec5SDimitry Andric 4880b57cec5SDimitry Andric // FIXME: We've expanded Count where we hope to insert the counter setting 4890b57cec5SDimitry Andric // intrinsic. But, in the case of the 'test and set' form, we may fallback to 4900b57cec5SDimitry Andric // the just 'set' form and in which case the insertion block is most likely 4910b57cec5SDimitry Andric // different. It means there will be instruction(s) in a block that possibly 4920b57cec5SDimitry Andric // aren't needed. The isLoopEntryGuardedByCond is trying to avoid this issue, 4930b57cec5SDimitry Andric // but it's doesn't appear to work in all cases. 4940b57cec5SDimitry Andric 4950b57cec5SDimitry Andric UseLoopGuard = UseLoopGuard && CanGenerateTest(L, Count); 4960b57cec5SDimitry Andric BeginBB = UseLoopGuard ? BB : L->getLoopPreheader(); 4970b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - Loop Count: " << *Count << "\n" 4980b57cec5SDimitry Andric << " - Expanded Count in " << BB->getName() << "\n" 4990b57cec5SDimitry Andric << " - Will insert set counter intrinsic into: " 5000b57cec5SDimitry Andric << BeginBB->getName() << "\n"); 5010b57cec5SDimitry Andric return Count; 5020b57cec5SDimitry Andric } 5030b57cec5SDimitry Andric 504e8d8bef9SDimitry Andric Value* HardwareLoop::InsertIterationSetup(Value *LoopCountInit) { 5050b57cec5SDimitry Andric IRBuilder<> Builder(BeginBB->getTerminator()); 506*0fca6ea1SDimitry Andric if (BeginBB->getParent()->getAttributes().hasFnAttr(Attribute::StrictFP)) 507*0fca6ea1SDimitry Andric Builder.setIsFPConstrained(true); 5080b57cec5SDimitry Andric Type *Ty = LoopCountInit->getType(); 50906c3fb27SDimitry Andric bool UsePhi = UsePHICounter || Opts.ForcePhi; 510fe6060f1SDimitry Andric Intrinsic::ID ID = UseLoopGuard 511fe6060f1SDimitry Andric ? (UsePhi ? Intrinsic::test_start_loop_iterations 512fe6060f1SDimitry Andric : Intrinsic::test_set_loop_iterations) 513e8d8bef9SDimitry Andric : (UsePhi ? Intrinsic::start_loop_iterations 514e8d8bef9SDimitry Andric : Intrinsic::set_loop_iterations); 5150b57cec5SDimitry Andric Function *LoopIter = Intrinsic::getDeclaration(M, ID, Ty); 516fe6060f1SDimitry Andric Value *LoopSetup = Builder.CreateCall(LoopIter, LoopCountInit); 5170b57cec5SDimitry Andric 5180b57cec5SDimitry Andric // Use the return value of the intrinsic to control the entry of the loop. 5190b57cec5SDimitry Andric if (UseLoopGuard) { 5200b57cec5SDimitry Andric assert((isa<BranchInst>(BeginBB->getTerminator()) && 5210b57cec5SDimitry Andric cast<BranchInst>(BeginBB->getTerminator())->isConditional()) && 5220b57cec5SDimitry Andric "Expected conditional branch"); 523fe6060f1SDimitry Andric 524fe6060f1SDimitry Andric Value *SetCount = 525fe6060f1SDimitry Andric UsePhi ? Builder.CreateExtractValue(LoopSetup, 1) : LoopSetup; 5260b57cec5SDimitry Andric auto *LoopGuard = cast<BranchInst>(BeginBB->getTerminator()); 5270b57cec5SDimitry Andric LoopGuard->setCondition(SetCount); 5280b57cec5SDimitry Andric if (LoopGuard->getSuccessor(0) != L->getLoopPreheader()) 5290b57cec5SDimitry Andric LoopGuard->swapSuccessors(); 5300b57cec5SDimitry Andric } 531fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "HWLoops: Inserted loop counter: " << *LoopSetup 532fe6060f1SDimitry Andric << "\n"); 533fe6060f1SDimitry Andric if (UsePhi && UseLoopGuard) 534fe6060f1SDimitry Andric LoopSetup = Builder.CreateExtractValue(LoopSetup, 0); 535fe6060f1SDimitry Andric return !UsePhi ? LoopCountInit : LoopSetup; 5360b57cec5SDimitry Andric } 5370b57cec5SDimitry Andric 5380b57cec5SDimitry Andric void HardwareLoop::InsertLoopDec() { 5390b57cec5SDimitry Andric IRBuilder<> CondBuilder(ExitBranch); 540*0fca6ea1SDimitry Andric if (ExitBranch->getParent()->getParent()->getAttributes().hasFnAttr( 541*0fca6ea1SDimitry Andric Attribute::StrictFP)) 542*0fca6ea1SDimitry Andric CondBuilder.setIsFPConstrained(true); 5430b57cec5SDimitry Andric 5440b57cec5SDimitry Andric Function *DecFunc = 5450b57cec5SDimitry Andric Intrinsic::getDeclaration(M, Intrinsic::loop_decrement, 5460b57cec5SDimitry Andric LoopDecrement->getType()); 5470b57cec5SDimitry Andric Value *Ops[] = { LoopDecrement }; 5480b57cec5SDimitry Andric Value *NewCond = CondBuilder.CreateCall(DecFunc, Ops); 5490b57cec5SDimitry Andric Value *OldCond = ExitBranch->getCondition(); 5500b57cec5SDimitry Andric ExitBranch->setCondition(NewCond); 5510b57cec5SDimitry Andric 5520b57cec5SDimitry Andric // The false branch must exit the loop. 5530b57cec5SDimitry Andric if (!L->contains(ExitBranch->getSuccessor(0))) 5540b57cec5SDimitry Andric ExitBranch->swapSuccessors(); 5550b57cec5SDimitry Andric 5560b57cec5SDimitry Andric // The old condition may be dead now, and may have even created a dead PHI 5570b57cec5SDimitry Andric // (the original induction variable). 5580b57cec5SDimitry Andric RecursivelyDeleteTriviallyDeadInstructions(OldCond); 5590b57cec5SDimitry Andric 5600b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "HWLoops: Inserted loop dec: " << *NewCond << "\n"); 5610b57cec5SDimitry Andric } 5620b57cec5SDimitry Andric 5630b57cec5SDimitry Andric Instruction* HardwareLoop::InsertLoopRegDec(Value *EltsRem) { 5640b57cec5SDimitry Andric IRBuilder<> CondBuilder(ExitBranch); 565*0fca6ea1SDimitry Andric if (ExitBranch->getParent()->getParent()->getAttributes().hasFnAttr( 566*0fca6ea1SDimitry Andric Attribute::StrictFP)) 567*0fca6ea1SDimitry Andric CondBuilder.setIsFPConstrained(true); 5680b57cec5SDimitry Andric 5690b57cec5SDimitry Andric Function *DecFunc = 5700b57cec5SDimitry Andric Intrinsic::getDeclaration(M, Intrinsic::loop_decrement_reg, 5715ffd83dbSDimitry Andric { EltsRem->getType() }); 5720b57cec5SDimitry Andric Value *Ops[] = { EltsRem, LoopDecrement }; 5730b57cec5SDimitry Andric Value *Call = CondBuilder.CreateCall(DecFunc, Ops); 5740b57cec5SDimitry Andric 5750b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "HWLoops: Inserted loop dec: " << *Call << "\n"); 5760b57cec5SDimitry Andric return cast<Instruction>(Call); 5770b57cec5SDimitry Andric } 5780b57cec5SDimitry Andric 5790b57cec5SDimitry Andric PHINode* HardwareLoop::InsertPHICounter(Value *NumElts, Value *EltsRem) { 5800b57cec5SDimitry Andric BasicBlock *Preheader = L->getLoopPreheader(); 5810b57cec5SDimitry Andric BasicBlock *Header = L->getHeader(); 5820b57cec5SDimitry Andric BasicBlock *Latch = ExitBranch->getParent(); 583*0fca6ea1SDimitry Andric IRBuilder<> Builder(Header, Header->getFirstNonPHIIt()); 5840b57cec5SDimitry Andric PHINode *Index = Builder.CreatePHI(NumElts->getType(), 2); 5850b57cec5SDimitry Andric Index->addIncoming(NumElts, Preheader); 5860b57cec5SDimitry Andric Index->addIncoming(EltsRem, Latch); 5870b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "HWLoops: PHI Counter: " << *Index << "\n"); 5880b57cec5SDimitry Andric return Index; 5890b57cec5SDimitry Andric } 5900b57cec5SDimitry Andric 5910b57cec5SDimitry Andric void HardwareLoop::UpdateBranch(Value *EltsRem) { 5920b57cec5SDimitry Andric IRBuilder<> CondBuilder(ExitBranch); 5930b57cec5SDimitry Andric Value *NewCond = 5940b57cec5SDimitry Andric CondBuilder.CreateICmpNE(EltsRem, ConstantInt::get(EltsRem->getType(), 0)); 5950b57cec5SDimitry Andric Value *OldCond = ExitBranch->getCondition(); 5960b57cec5SDimitry Andric ExitBranch->setCondition(NewCond); 5970b57cec5SDimitry Andric 5980b57cec5SDimitry Andric // The false branch must exit the loop. 5990b57cec5SDimitry Andric if (!L->contains(ExitBranch->getSuccessor(0))) 6000b57cec5SDimitry Andric ExitBranch->swapSuccessors(); 6010b57cec5SDimitry Andric 6020b57cec5SDimitry Andric // The old condition may be dead now, and may have even created a dead PHI 6030b57cec5SDimitry Andric // (the original induction variable). 6040b57cec5SDimitry Andric RecursivelyDeleteTriviallyDeadInstructions(OldCond); 6050b57cec5SDimitry Andric } 6060b57cec5SDimitry Andric 60706c3fb27SDimitry Andric INITIALIZE_PASS_BEGIN(HardwareLoopsLegacy, DEBUG_TYPE, HW_LOOPS_NAME, false, false) 6080b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 6090b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) 6100b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) 611480093f4SDimitry Andric INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass) 61206c3fb27SDimitry Andric INITIALIZE_PASS_END(HardwareLoopsLegacy, DEBUG_TYPE, HW_LOOPS_NAME, false, false) 6130b57cec5SDimitry Andric 61406c3fb27SDimitry Andric FunctionPass *llvm::createHardwareLoopsLegacyPass() { return new HardwareLoopsLegacy(); } 615