LLVM 24.0.0git
HardwareLoops.cpp
Go to the documentation of this file.
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//===----------------------------------------------------------------------===//
17
19#include "CodeGenOptions.h"
20#include "llvm/ADT/Statistic.h"
28#include "llvm/CodeGen/Passes.h"
29#include "llvm/IR/BasicBlock.h"
30#include "llvm/IR/Constants.h"
31#include "llvm/IR/Dominators.h"
32#include "llvm/IR/IRBuilder.h"
34#include "llvm/IR/Value.h"
36#include "llvm/Pass.h"
37#include "llvm/Support/Debug.h"
43
44#define DEBUG_TYPE "hardware-loops"
45
46#define HW_LOOPS_NAME "Hardware Loop Insertion"
47
48using namespace llvm;
49
50STATISTIC(NumHWLoops, "Number of loops converted to hardware loops");
51
52#ifndef NDEBUG
53static void debugHWLoopFailure(const StringRef DebugMsg,
54 Instruction *I) {
55 dbgs() << "HWLoops: " << DebugMsg;
56 if (I)
57 dbgs() << ' ' << *I;
58 else
59 dbgs() << '.';
60 dbgs() << '\n';
61}
62#endif
63
66 BasicBlock *CodeRegion = L->getHeader();
67 DebugLoc DL = L->getStartLoc();
68
69 if (I) {
70 CodeRegion = I->getParent();
71 // If there is no debug location attached to the instruction, revert back to
72 // using the loop's.
73 if (I->getDebugLoc())
74 DL = I->getDebugLoc();
75 }
76
77 OptimizationRemarkAnalysis R(DEBUG_TYPE, RemarkName, DL, CodeRegion);
78 R << "hardware-loop not created: ";
79 return R;
80}
81
82namespace {
83
84 void reportHWLoopFailure(const StringRef Msg, const StringRef ORETag,
85 OptimizationRemarkEmitter *ORE, Loop *TheLoop, Instruction *I = nullptr) {
87 ORE->emit(createHWLoopAnalysis(ORETag, TheLoop, I) << Msg);
88 }
89
90 class HardwareLoopsLegacy : public FunctionPass {
91 public:
92 static char ID;
93
94 HardwareLoopsLegacy() : FunctionPass(ID) {}
95
96 bool runOnFunction(Function &F) override;
97
98 void getAnalysisUsage(AnalysisUsage &AU) const override {
99 AU.addRequired<LoopInfoWrapperPass>();
100 AU.addPreserved<LoopInfoWrapperPass>();
101 AU.addRequired<DominatorTreeWrapperPass>();
102 AU.addPreserved<DominatorTreeWrapperPass>();
103 AU.addRequired<ScalarEvolutionWrapperPass>();
104 AU.addPreserved<ScalarEvolutionWrapperPass>();
105 AU.addRequired<AssumptionCacheTracker>();
106 AU.addRequired<TargetTransformInfoWrapperPass>();
107 AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
108 AU.addPreserved<BranchProbabilityInfoWrapperPass>();
109 }
110 };
111
112 class HardwareLoopsImpl {
113 public:
114 HardwareLoopsImpl(ScalarEvolution &SE, LoopInfo &LI, bool PreserveLCSSA,
115 DominatorTree &DT, const TargetTransformInfo &TTI,
116 TargetLibraryInfo *TLI, AssumptionCache &AC,
117 OptimizationRemarkEmitter *ORE, HardwareLoopOptions &Opts)
118 : SE(SE), LI(LI), PreserveLCSSA(PreserveLCSSA), DT(DT), TTI(TTI),
119 TLI(TLI), AC(AC), ORE(ORE), Opts(Opts) {}
120
121 bool run(Function &F);
122
123 private:
124 // Try to convert the given Loop into a hardware loop.
125 bool TryConvertLoop(Loop *L, LLVMContext &Ctx);
126
127 // Given that the target believes the loop to be profitable, try to
128 // convert it.
129 bool TryConvertLoop(HardwareLoopInfo &HWLoopInfo);
130
131 ScalarEvolution &SE;
132 LoopInfo &LI;
133 bool PreserveLCSSA;
134 DominatorTree &DT;
135 const TargetTransformInfo &TTI;
136 TargetLibraryInfo *TLI = nullptr;
137 AssumptionCache &AC;
138 OptimizationRemarkEmitter *ORE;
139 HardwareLoopOptions &Opts;
140 bool MadeChange = false;
141 };
142
143 class HardwareLoop {
144 // Expand the trip count scev into a value that we can use.
145 Value *InitLoopCount();
146
147 // Insert the set_loop_iteration intrinsic.
148 Value *InsertIterationSetup(Value *LoopCountInit);
149
150 // Insert the loop_decrement intrinsic.
151 void InsertLoopDec();
152
153 // Insert the loop_decrement_reg intrinsic.
154 Instruction *InsertLoopRegDec(Value *EltsRem);
155
156 // If the target requires the counter value to be updated in the loop,
157 // insert a phi to hold the value. The intended purpose is for use by
158 // loop_decrement_reg.
159 PHINode *InsertPHICounter(Value *NumElts, Value *EltsRem);
160
161 // Create a new cmp, that checks the returned value of loop_decrement*,
162 // and update the exit branch to use it.
163 void UpdateBranch(Value *EltsRem);
164
165 public:
166 HardwareLoop(HardwareLoopInfo &Info, ScalarEvolution &SE,
167 OptimizationRemarkEmitter *ORE, HardwareLoopOptions &Opts)
168 : SE(SE), ORE(ORE), Opts(Opts), L(Info.L),
169 M(L->getHeader()->getModule()), ExitCount(Info.ExitCount),
170 CountType(Info.CountType), ExitBranch(Info.ExitBranch),
171 LoopDecrement(Info.LoopDecrement), UsePHICounter(Info.CounterInReg),
172 UseLoopGuard(Info.PerformEntryTest) {}
173
174 void Create();
175
176 private:
177 ScalarEvolution &SE;
178 OptimizationRemarkEmitter *ORE = nullptr;
179 HardwareLoopOptions &Opts;
180 Loop *L = nullptr;
181 Module *M = nullptr;
182 const SCEV *ExitCount = nullptr;
183 Type *CountType = nullptr;
184 CondBrInst *ExitBranch = nullptr;
185 Value *LoopDecrement = nullptr;
186 bool UsePHICounter = false;
187 bool UseLoopGuard = false;
188 BasicBlock *BeginBB = nullptr;
189 };
190}
191
192char HardwareLoopsLegacy::ID = 0;
193
194bool HardwareLoopsLegacy::runOnFunction(Function &F) {
195 if (skipFunction(F))
196 return false;
197
198 LLVM_DEBUG(dbgs() << "HWLoops: Running on " << F.getName() << "\n");
199
200 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
201 auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
202 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
203 auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
204 auto *ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
205 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
206 auto *TLI = TLIP ? &TLIP->getTLI(F) : nullptr;
207 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
208 bool PreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
209
210 const CodeGenOptions &CGOpts = getCodeGenOptions(F);
211 HardwareLoopOptions Opts;
212 Opts.Force = toOptional(CGOpts.force_hardware_loops);
213 Opts.ForcePhi = toOptional(CGOpts.force_hardware_loop_phi);
214 Opts.ForceNested = toOptional(CGOpts.force_nested_hardware_loop);
215 Opts.ForceGuard = toOptional(CGOpts.force_hardware_loop_guard);
216 Opts.Decrement = CGOpts.hardware_loop_decrement;
217 Opts.Bitwidth = CGOpts.hardware_loop_counter_bitwidth;
218
219 HardwareLoopsImpl Impl(SE, LI, PreserveLCSSA, DT, TTI, TLI, AC, ORE, Opts);
220 return Impl.run(F);
221}
222
225 auto &LI = AM.getResult<LoopAnalysis>(F);
226 auto &SE = AM.getResult<ScalarEvolutionAnalysis>(F);
227 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
228 auto &TTI = AM.getResult<TargetIRAnalysis>(F);
229 auto *TLI = &AM.getResult<TargetLibraryAnalysis>(F);
230 auto &AC = AM.getResult<AssumptionAnalysis>(F);
232
233 HardwareLoopsImpl Impl(SE, LI, true, DT, TTI, TLI, AC, ORE, Opts);
234 bool Changed = Impl.run(F);
235 if (!Changed)
236 return PreservedAnalyses::all();
237
243 return PA;
244}
245
246bool HardwareLoopsImpl::run(Function &F) {
247 LLVMContext &Ctx = F.getContext();
248 for (Loop *L : LI)
249 if (L->isOutermost())
250 TryConvertLoop(L, Ctx);
251 return MadeChange;
252}
253
254// Return true if the search should stop, which will be when an inner loop is
255// converted and the parent loop doesn't support containing a hardware loop.
256bool HardwareLoopsImpl::TryConvertLoop(Loop *L, LLVMContext &Ctx) {
257 // Process nested loops first.
258 bool AnyChanged = false;
259 for (Loop *SL : *L)
260 AnyChanged |= TryConvertLoop(SL, Ctx);
261 if (AnyChanged) {
262 reportHWLoopFailure("nested hardware-loops not supported", "HWLoopNested",
263 ORE, L);
264 return true; // Stop search.
265 }
266
267 LLVM_DEBUG(dbgs() << "HWLoops: Loop " << L->getHeader()->getName() << "\n");
268
269 HardwareLoopInfo HWLoopInfo(L);
270 if (!HWLoopInfo.canAnalyze(LI)) {
271 reportHWLoopFailure("cannot analyze loop, irreducible control flow",
272 "HWLoopCannotAnalyze", ORE, L);
273 return false;
274 }
275
276 if (!Opts.Force &&
277 !TTI.isHardwareLoopProfitable(L, SE, AC, TLI, HWLoopInfo)) {
278 reportHWLoopFailure("it's not profitable to create a hardware-loop",
279 "HWLoopNotProfitable", ORE, L);
280 return false;
281 }
282
283 // Allow overriding of the counter width and loop decrement value.
284 if (Opts.Bitwidth.has_value()) {
285 HWLoopInfo.CountType = IntegerType::get(Ctx, Opts.Bitwidth.value());
286 }
287
288 if (Opts.Decrement.has_value())
289 HWLoopInfo.LoopDecrement =
290 ConstantInt::get(HWLoopInfo.CountType, Opts.Decrement.value());
291
292 MadeChange |= TryConvertLoop(HWLoopInfo);
293 return MadeChange && (!HWLoopInfo.IsNestingLegal && !Opts.ForceNested);
294}
295
296bool HardwareLoopsImpl::TryConvertLoop(HardwareLoopInfo &HWLoopInfo) {
297
298 Loop *L = HWLoopInfo.L;
299 LLVM_DEBUG(dbgs() << "HWLoops: Try to convert profitable loop: " << *L);
300
301 if (!HWLoopInfo.isHardwareLoopCandidate(SE, LI, DT, Opts.getForceNested(),
302 Opts.getForcePhi())) {
303 // TODO: there can be many reasons a loop is not considered a
304 // candidate, so we should let isHardwareLoopCandidate fill in the
305 // reason and then report a better message here.
306 reportHWLoopFailure("loop is not a candidate", "HWLoopNoCandidate", ORE, L);
307 return false;
308 }
309
310 assert(
311 (HWLoopInfo.ExitBlock && HWLoopInfo.ExitBranch && HWLoopInfo.ExitCount) &&
312 "Hardware Loop must have set exit info.");
313
314 BasicBlock *Preheader = L->getLoopPreheader();
315
316 // If we don't have a preheader, then insert one.
317 if (!Preheader)
318 Preheader = InsertPreheaderForLoop(L, &DT, &LI, nullptr, PreserveLCSSA);
319 if (!Preheader)
320 return false;
321
322 HardwareLoop HWLoop(HWLoopInfo, SE, ORE, Opts);
323 HWLoop.Create();
324 ++NumHWLoops;
325 return true;
326}
327
328void HardwareLoop::Create() {
329 LLVM_DEBUG(dbgs() << "HWLoops: Converting loop..\n");
330
331 Value *LoopCountInit = InitLoopCount();
332 if (!LoopCountInit) {
333 reportHWLoopFailure("could not safely create a loop count expression",
334 "HWLoopNotSafe", ORE, L);
335 return;
336 }
337
338 Value *Setup = InsertIterationSetup(LoopCountInit);
339
340 if (UsePHICounter || Opts.ForcePhi) {
341 Instruction *LoopDec = InsertLoopRegDec(LoopCountInit);
342 Value *EltsRem = InsertPHICounter(Setup, LoopDec);
343 LoopDec->setOperand(0, EltsRem);
344 UpdateBranch(LoopDec);
345 } else
346 InsertLoopDec();
347
348 // Run through the basic blocks of the loop and see if any of them have dead
349 // PHIs that can be removed.
350 for (auto *I : L->blocks())
352}
353
354static bool CanGenerateTest(Loop *L, Value *Count) {
355 BasicBlock *Preheader = L->getLoopPreheader();
356 if (!Preheader->getSinglePredecessor())
357 return false;
358
359 BasicBlock *Pred = Preheader->getSinglePredecessor();
360 auto *BI = dyn_cast<CondBrInst>(Pred->getTerminator());
361 if (!BI || !isa<ICmpInst>(BI->getCondition()))
362 return false;
363
364 // Check that the icmp is checking for equality of Count and zero and that
365 // a non-zero value results in entering the loop.
366 auto ICmp = cast<ICmpInst>(BI->getCondition());
367 LLVM_DEBUG(dbgs() << " - Found condition: " << *ICmp << "\n");
368 if (!ICmp->isEquality())
369 return false;
370
371 auto IsCompareZero = [](ICmpInst *ICmp, Value *Count, unsigned OpIdx) {
372 if (auto *Const = dyn_cast<ConstantInt>(ICmp->getOperand(OpIdx)))
373 return Const->isZero() && ICmp->getOperand(OpIdx ^ 1) == Count;
374 return false;
375 };
376
377 // Check if Count is a zext.
378 Value *CountBefZext =
379 isa<ZExtInst>(Count) ? cast<ZExtInst>(Count)->getOperand(0) : nullptr;
380
381 if (!IsCompareZero(ICmp, Count, 0) && !IsCompareZero(ICmp, Count, 1) &&
382 !IsCompareZero(ICmp, CountBefZext, 0) &&
383 !IsCompareZero(ICmp, CountBefZext, 1))
384 return false;
385
386 unsigned SuccIdx = ICmp->getPredicate() == ICmpInst::ICMP_NE ? 0 : 1;
387 if (BI->getSuccessor(SuccIdx) != Preheader)
388 return false;
389
390 return true;
391}
392
393Value *HardwareLoop::InitLoopCount() {
394 LLVM_DEBUG(dbgs() << "HWLoops: Initialising loop counter value:\n");
395 // Can we replace a conditional branch with an intrinsic that sets the
396 // loop counter and tests that is not zero?
397
398 SCEVExpander SCEVE(SE, "loopcnt");
399 if (!ExitCount->getType()->isPointerTy() &&
400 ExitCount->getType() != CountType)
401 ExitCount = SE.getZeroExtendExpr(ExitCount, CountType);
402
403 ExitCount = SE.getAddExpr(ExitCount, SE.getOne(CountType));
404
405 // If we're trying to use the 'test and set' form of the intrinsic, we need
406 // to replace a conditional branch that is controlling entry to the loop. It
407 // is likely (guaranteed?) that the preheader has an unconditional branch to
408 // the loop header, so also check if it has a single predecessor.
409 if (SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, ExitCount,
410 SE.getZero(ExitCount->getType()))) {
411 LLVM_DEBUG(dbgs() << " - Attempting to use test.set counter.\n");
412 if (Opts.ForceGuard)
413 UseLoopGuard = true;
414 } else
415 UseLoopGuard = false;
416
417 BasicBlock *BB = L->getLoopPreheader();
418 if (UseLoopGuard && BB->getSinglePredecessor() &&
420 BasicBlock *Predecessor = BB->getSinglePredecessor();
421 // If it's not safe to create a while loop then don't force it and create a
422 // do-while loop instead
423 if (!SCEVE.isSafeToExpandAt(ExitCount, Predecessor->getTerminator()))
424 UseLoopGuard = false;
425 else
426 BB = Predecessor;
427 }
428
429 if (!SCEVE.isSafeToExpandAt(ExitCount, BB->getTerminator())) {
430 LLVM_DEBUG(dbgs() << "- Bailing, unsafe to expand ExitCount "
431 << *ExitCount << "\n");
432 return nullptr;
433 }
434
435 Value *Count = SCEVE.expandCodeFor(ExitCount, CountType,
436 BB->getTerminator());
437
438 // FIXME: We've expanded Count where we hope to insert the counter setting
439 // intrinsic. But, in the case of the 'test and set' form, we may fallback to
440 // the just 'set' form and in which case the insertion block is most likely
441 // different. It means there will be instruction(s) in a block that possibly
442 // aren't needed. The isLoopEntryGuardedByCond is trying to avoid this issue,
443 // but it's doesn't appear to work in all cases.
444
445 UseLoopGuard = UseLoopGuard && CanGenerateTest(L, Count);
446 BeginBB = UseLoopGuard ? BB : L->getLoopPreheader();
447 LLVM_DEBUG(dbgs() << " - Loop Count: " << *Count << "\n"
448 << " - Expanded Count in " << BB->getName() << "\n"
449 << " - Will insert set counter intrinsic into: "
450 << BeginBB->getName() << "\n");
451 return Count;
452}
453
454Value* HardwareLoop::InsertIterationSetup(Value *LoopCountInit) {
455 IRBuilder<> Builder(BeginBB->getTerminator());
456 if (BeginBB->getParent()->getAttributes().hasFnAttr(Attribute::StrictFP))
457 Builder.setIsFPConstrained(true);
458 Type *Ty = LoopCountInit->getType();
459 bool UsePhi = UsePHICounter || Opts.ForcePhi;
460 Intrinsic::ID ID = UseLoopGuard
461 ? (UsePhi ? Intrinsic::test_start_loop_iterations
462 : Intrinsic::test_set_loop_iterations)
463 : (UsePhi ? Intrinsic::start_loop_iterations
464 : Intrinsic::set_loop_iterations);
465 Value *LoopSetup = Builder.CreateIntrinsic(ID, Ty, LoopCountInit);
466
467 // Use the return value of the intrinsic to control the entry of the loop.
468 if (UseLoopGuard) {
469 Value *SetCount =
470 UsePhi ? Builder.CreateExtractValue(LoopSetup, 1) : LoopSetup;
471 auto *LoopGuard = cast<CondBrInst>(BeginBB->getTerminator());
472 LoopGuard->setCondition(SetCount);
473 if (LoopGuard->getSuccessor(0) != L->getLoopPreheader())
474 LoopGuard->swapSuccessors();
475 }
476 LLVM_DEBUG(dbgs() << "HWLoops: Inserted loop counter: " << *LoopSetup
477 << "\n");
478 if (UsePhi && UseLoopGuard)
479 LoopSetup = Builder.CreateExtractValue(LoopSetup, 0);
480 return !UsePhi ? LoopCountInit : LoopSetup;
481}
482
483void HardwareLoop::InsertLoopDec() {
484 IRBuilder<> CondBuilder(ExitBranch);
485 if (ExitBranch->getParent()->getParent()->getAttributes().hasFnAttr(
486 Attribute::StrictFP))
487 CondBuilder.setIsFPConstrained(true);
488
489 Value *Ops[] = { LoopDecrement };
490 Value *NewCond = CondBuilder.CreateIntrinsic(Intrinsic::loop_decrement,
491 LoopDecrement->getType(), Ops);
492 Value *OldCond = ExitBranch->getCondition();
493 ExitBranch->setCondition(NewCond);
494
495 // The false branch must exit the loop.
496 if (!L->contains(ExitBranch->getSuccessor(0)))
497 ExitBranch->swapSuccessors();
498
499 // The old condition may be dead now, and may have even created a dead PHI
500 // (the original induction variable).
502
503 LLVM_DEBUG(dbgs() << "HWLoops: Inserted loop dec: " << *NewCond << "\n");
504}
505
506Instruction* HardwareLoop::InsertLoopRegDec(Value *EltsRem) {
507 IRBuilder<> CondBuilder(ExitBranch);
508 if (ExitBranch->getParent()->getParent()->getAttributes().hasFnAttr(
509 Attribute::StrictFP))
510 CondBuilder.setIsFPConstrained(true);
511
512 Value *Ops[] = { EltsRem, LoopDecrement };
513 Value *Call = CondBuilder.CreateIntrinsic(Intrinsic::loop_decrement_reg,
514 {EltsRem->getType()}, Ops);
515
516 LLVM_DEBUG(dbgs() << "HWLoops: Inserted loop dec: " << *Call << "\n");
517 return cast<Instruction>(Call);
518}
519
520PHINode* HardwareLoop::InsertPHICounter(Value *NumElts, Value *EltsRem) {
521 BasicBlock *Preheader = L->getLoopPreheader();
522 BasicBlock *Header = L->getHeader();
523 BasicBlock *Latch = ExitBranch->getParent();
524 IRBuilder<> Builder(Header->getFirstNonPHIIt());
525 PHINode *Index = Builder.CreatePHI(NumElts->getType(), 2);
526 Index->addIncoming(NumElts, Preheader);
527 Index->addIncoming(EltsRem, Latch);
528 LLVM_DEBUG(dbgs() << "HWLoops: PHI Counter: " << *Index << "\n");
529 return Index;
530}
531
532void HardwareLoop::UpdateBranch(Value *EltsRem) {
533 IRBuilder<> CondBuilder(ExitBranch);
534 Value *NewCond =
535 CondBuilder.CreateICmpNE(EltsRem, ConstantInt::get(EltsRem->getType(), 0));
536 Value *OldCond = ExitBranch->getCondition();
537 ExitBranch->setCondition(NewCond);
538
539 // The false branch must exit the loop.
540 if (!L->contains(ExitBranch->getSuccessor(0)))
541 ExitBranch->swapSuccessors();
542
543 // The old condition may be dead now, and may have even created a dead PHI
544 // (the original induction variable).
546}
547
548INITIALIZE_PASS_BEGIN(HardwareLoopsLegacy, DEBUG_TYPE, HW_LOOPS_NAME, false, false)
549INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
550INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
551INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
552INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
553INITIALIZE_PASS_END(HardwareLoopsLegacy, DEBUG_TYPE, HW_LOOPS_NAME, false, false)
554
555FunctionPass *llvm::createHardwareLoopsLegacyPass() { return new HardwareLoopsLegacy(); }
const TargetLowering & TLI
SDValue Pred
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool runOnFunction(Function &F, bool PostInlining)
#define DEBUG_TYPE
#define HW_LOOPS_NAME
static OptimizationRemarkAnalysis createHWLoopAnalysis(StringRef RemarkName, Loop *L, Instruction *I)
static void debugHWLoopFailure(const StringRef DebugMsg, Instruction *I)
static bool CanGenerateTest(Loop *L, Value *Count)
Defines an IR pass for the creation of hardware loops.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
const char * Msg
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
This pass exposes codegen information to IR-level passes.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
A function analysis which provides an AssumptionCache.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
Analysis pass which computes BranchProbabilityInfo.
@ ICMP_NE
not equal
Definition InstrTypes.h:762
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:828
LLVM_ABI void swapSuccessors()
Swap the successors of this branch instruction.
void setCondition(Value *V)
Value * getCondition() const
BasicBlock * getSuccessor(unsigned i) const
A debug info location.
Definition DebugLoc.h:126
Analysis pass which computes a DominatorTree.
Definition Dominators.h:241
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:329
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
This instruction compares its operands according to the predicate given to the constructor.
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:333
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
Analysis pass that exposes the LoopInfo for a function.
Definition LoopInfo.h:598
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Diagnostic information for optimization analysis remarks.
The optimization diagnostic interface.
LLVM_ABI void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:133
Type * getType() const
Return the LLVM type of this SCEV expression.
Analysis pass that exposes the ScalarEvolution for a function.
LLVM_ABI const SCEV * getZeroExtendExpr(SCEVUse Op, Type *Ty, unsigned Depth=0)
LLVM_ABI bool isLoopEntryGuardedByCond(const Loop *L, CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Test whether entry to the loop is protected by a conditional between LHS and RHS.
const SCEV * getZero(Type *Ty)
Return a SCEV for the constant 0 of a specific type.
const SCEV * getOne(Type *Ty)
Return a SCEV for the constant 1 of a specific type.
LLVM_ABI SCEVUse getAddExpr(SmallVectorImpl< SCEVUse > &Ops, SCEVFlagsPair Flags={}, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Analysis pass providing the TargetTransformInfo.
Analysis pass providing the TargetLibraryInfo.
LLVM_ABI bool isHardwareLoopProfitable(Loop *L, ScalarEvolution &SE, AssumptionCache &AC, TargetLibraryInfo *LibInfo, HardwareLoopInfo &HWLoopInfo) const
Query the target whether it would be profitable to convert the given loop into a hardware loop.
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:277
void setOperand(unsigned i, Value *Val)
Definition User.h:212
Value * getOperand(unsigned i) const
Definition User.h:207
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:314
const ParentTy * getParent() const
Definition ilist_node.h:34
CallInst * Call
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:83
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI BasicBlock * InsertPreheaderForLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, MemorySSAUpdater *MSSAU, bool PreserveLCSSA)
InsertPreheaderForLoop - Once we discover that a loop doesn't have a preheader, this method is called...
LLVM_ABI bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
If the specified value is a trivially dead instruction, delete it.
Definition Local.cpp:522
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, SmallPtrSetImpl< PHINode * > *KnownNonDeadPHIs=nullptr)
Examine each PHI in the given block and delete it if it is dead.
LLVM_ABI char & LCSSAID
Definition LCSSA.cpp:545
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
IRBuilder(LLVMContext &, FolderTy, InserterTy) -> IRBuilder< FolderTy, InserterTy >
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
TargetTransformInfo TTI
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
constexpr std::optional< bool > toOptional(BoolOrDefault X)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI FunctionPass * createHardwareLoopsLegacyPass()
Create Hardware Loop pass.
const CodeGenOptions & getCodeGenOptions(const LLVMContext &C)
LLVM_ABI bool isHardwareLoopCandidate(ScalarEvolution &SE, LoopInfo &LI, DominatorTree &DT, bool ForceNestedLoop=false, bool ForceHardwareLoopPHI=false)
std::optional< bool > Force
std::optional< bool > ForceGuard
std::optional< unsigned > Decrement
std::optional< unsigned > Bitwidth
std::optional< bool > ForcePhi
std::optional< bool > ForceNested